]>
Commit | Line | Data |
---|---|---|
2bd0ea18 | 1 | /* |
5e656dbb | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
da23017d | 3 | * All Rights Reserved. |
5000d01d | 4 | * |
da23017d NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
2bd0ea18 | 7 | * published by the Free Software Foundation. |
5000d01d | 8 | * |
da23017d NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
5000d01d | 13 | * |
da23017d NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
2bd0ea18 | 17 | */ |
9c799827 | 18 | #include "libxfs_priv.h" |
b626fb59 DC |
19 | #include "xfs_fs.h" |
20 | #include "xfs_shared.h" | |
21 | #include "xfs_format.h" | |
22 | #include "xfs_log_format.h" | |
23 | #include "xfs_trans_resv.h" | |
24 | #include "xfs_bit.h" | |
25 | #include "xfs_sb.h" | |
26 | #include "xfs_mount.h" | |
f944d3d0 | 27 | #include "xfs_defer.h" |
b626fb59 DC |
28 | #include "xfs_da_format.h" |
29 | #include "xfs_da_btree.h" | |
30 | #include "xfs_dir2.h" | |
31 | #include "xfs_inode.h" | |
32 | #include "xfs_btree.h" | |
33 | #include "xfs_trans.h" | |
34 | #include "xfs_alloc.h" | |
35 | #include "xfs_bmap.h" | |
36 | #include "xfs_bmap_btree.h" | |
37 | #include "xfs_trans_space.h" | |
38 | #include "xfs_trace.h" | |
39 | #include "xfs_attr_leaf.h" | |
40 | #include "xfs_quota_defs.h" | |
85aec44f | 41 | #include "xfs_rmap.h" |
cf8ce220 | 42 | #include "xfs_ag_resv.h" |
cfe32f0d | 43 | #include "xfs_refcount.h" |
b626fb59 | 44 | |
2bd0ea18 | 45 | |
5e656dbb BN |
46 | kmem_zone_t *xfs_bmap_free_item_zone; |
47 | ||
48 | /* | |
49f693fa | 49 | * Miscellaneous helper functions |
5e656dbb | 50 | */ |
5e656dbb | 51 | |
5e656dbb | 52 | /* |
49f693fa DC |
53 | * Compute and fill in the value of the maximum depth of a bmap btree |
54 | * in this filesystem. Done once, during mount. | |
5e656dbb | 55 | */ |
49f693fa DC |
56 | void |
57 | xfs_bmap_compute_maxlevels( | |
58 | xfs_mount_t *mp, /* file system mount structure */ | |
59 | int whichfork) /* data or attr fork */ | |
60 | { | |
61 | int level; /* btree level */ | |
62 | uint maxblocks; /* max blocks at this level */ | |
63 | uint maxleafents; /* max leaf entries possible */ | |
64 | int maxrootrecs; /* max records in root block */ | |
65 | int minleafrecs; /* min records in leaf block */ | |
66 | int minnoderecs; /* min records in node block */ | |
67 | int sz; /* root block size */ | |
5e656dbb | 68 | |
49f693fa DC |
69 | /* |
70 | * The maximum number of extents in a file, hence the maximum | |
71 | * number of leaf entries, is controlled by the type of di_nextents | |
72 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents | |
73 | * (a signed 16-bit number, xfs_aextnum_t). | |
74 | * | |
75 | * Note that we can no longer assume that if we are in ATTR1 that | |
76 | * the fork offset of all the inodes will be | |
77 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted | |
78 | * with ATTR2 and then mounted back with ATTR1, keeping the | |
79 | * di_forkoff's fixed but probably at various positions. Therefore, | |
80 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario | |
81 | * of a minimum size available. | |
82 | */ | |
83 | if (whichfork == XFS_DATA_FORK) { | |
84 | maxleafents = MAXEXTNUM; | |
85 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); | |
86 | } else { | |
87 | maxleafents = MAXAEXTNUM; | |
88 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); | |
89 | } | |
ff105f75 | 90 | maxrootrecs = xfs_bmdr_maxrecs(sz, 0); |
49f693fa DC |
91 | minleafrecs = mp->m_bmap_dmnr[0]; |
92 | minnoderecs = mp->m_bmap_dmnr[1]; | |
93 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; | |
94 | for (level = 1; maxblocks > 1; level++) { | |
95 | if (maxblocks <= maxrootrecs) | |
96 | maxblocks = 1; | |
97 | else | |
98 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; | |
99 | } | |
100 | mp->m_bm_maxlevels[whichfork] = level; | |
101 | } | |
5e656dbb | 102 | |
b194c7d8 BN |
103 | STATIC int /* error */ |
104 | xfs_bmbt_lookup_eq( | |
105 | struct xfs_btree_cur *cur, | |
106 | xfs_fileoff_t off, | |
107 | xfs_fsblock_t bno, | |
108 | xfs_filblks_t len, | |
109 | int *stat) /* success/failure */ | |
110 | { | |
111 | cur->bc_rec.b.br_startoff = off; | |
112 | cur->bc_rec.b.br_startblock = bno; | |
113 | cur->bc_rec.b.br_blockcount = len; | |
114 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); | |
115 | } | |
116 | ||
117 | STATIC int /* error */ | |
118 | xfs_bmbt_lookup_ge( | |
119 | struct xfs_btree_cur *cur, | |
120 | xfs_fileoff_t off, | |
121 | xfs_fsblock_t bno, | |
122 | xfs_filblks_t len, | |
123 | int *stat) /* success/failure */ | |
124 | { | |
125 | cur->bc_rec.b.br_startoff = off; | |
126 | cur->bc_rec.b.br_startblock = bno; | |
127 | cur->bc_rec.b.br_blockcount = len; | |
128 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); | |
129 | } | |
130 | ||
131 | /* | |
a2ceac1f DC |
132 | * Check if the inode needs to be converted to btree format. |
133 | */ | |
134 | static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork) | |
135 | { | |
1277a5e0 DW |
136 | return whichfork != XFS_COW_FORK && |
137 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && | |
a2ceac1f DC |
138 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
139 | XFS_IFORK_MAXEXT(ip, whichfork); | |
140 | } | |
141 | ||
142 | /* | |
143 | * Check if the inode should be converted to extent format. | |
144 | */ | |
145 | static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork) | |
146 | { | |
1277a5e0 DW |
147 | return whichfork != XFS_COW_FORK && |
148 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && | |
a2ceac1f DC |
149 | XFS_IFORK_NEXTENTS(ip, whichfork) <= |
150 | XFS_IFORK_MAXEXT(ip, whichfork); | |
151 | } | |
152 | ||
153 | /* | |
154 | * Update the record referred to by cur to the value given | |
b194c7d8 BN |
155 | * by [off, bno, len, state]. |
156 | * This either works (return 0) or gets an EFSCORRUPTED error. | |
157 | */ | |
158 | STATIC int | |
159 | xfs_bmbt_update( | |
160 | struct xfs_btree_cur *cur, | |
161 | xfs_fileoff_t off, | |
162 | xfs_fsblock_t bno, | |
163 | xfs_filblks_t len, | |
164 | xfs_exntst_t state) | |
165 | { | |
166 | union xfs_btree_rec rec; | |
167 | ||
168 | xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state); | |
169 | return xfs_btree_update(cur, &rec); | |
170 | } | |
171 | ||
5e656dbb | 172 | /* |
49f693fa DC |
173 | * Compute the worst-case number of indirect blocks that will be used |
174 | * for ip's delayed extent of length "len". | |
5e656dbb | 175 | */ |
49f693fa DC |
176 | STATIC xfs_filblks_t |
177 | xfs_bmap_worst_indlen( | |
178 | xfs_inode_t *ip, /* incore inode pointer */ | |
179 | xfs_filblks_t len) /* delayed extent length */ | |
57c9fccb | 180 | { |
49f693fa DC |
181 | int level; /* btree level number */ |
182 | int maxrecs; /* maximum record count at this level */ | |
183 | xfs_mount_t *mp; /* mount structure */ | |
184 | xfs_filblks_t rval; /* return value */ | |
57c9fccb NS |
185 | |
186 | mp = ip->i_mount; | |
49f693fa DC |
187 | maxrecs = mp->m_bmap_dmxr[0]; |
188 | for (level = 0, rval = 0; | |
189 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); | |
190 | level++) { | |
191 | len += maxrecs - 1; | |
192 | do_div(len, maxrecs); | |
193 | rval += len; | |
194 | if (len == 1) | |
195 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - | |
196 | level - 1; | |
197 | if (level == 0) | |
198 | maxrecs = mp->m_bmap_dmxr[1]; | |
57c9fccb | 199 | } |
49f693fa | 200 | return rval; |
57c9fccb NS |
201 | } |
202 | ||
203 | /* | |
49f693fa | 204 | * Calculate the default attribute fork offset for newly created inodes. |
57c9fccb | 205 | */ |
49f693fa DC |
206 | uint |
207 | xfs_default_attroffset( | |
208 | struct xfs_inode *ip) | |
57c9fccb | 209 | { |
49f693fa DC |
210 | struct xfs_mount *mp = ip->i_mount; |
211 | uint offset; | |
57c9fccb | 212 | |
49f693fa DC |
213 | if (mp->m_sb.sb_inodesize == 256) { |
214 | offset = XFS_LITINO(mp, ip->i_d.di_version) - | |
215 | XFS_BMDR_SPACE_CALC(MINABTPTRS); | |
216 | } else { | |
217 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); | |
57c9fccb | 218 | } |
49f693fa DC |
219 | |
220 | ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version)); | |
221 | return offset; | |
57c9fccb NS |
222 | } |
223 | ||
224 | /* | |
49f693fa DC |
225 | * Helper routine to reset inode di_forkoff field when switching |
226 | * attribute fork from local to extent format - we reset it where | |
227 | * possible to make space available for inline data fork extents. | |
57c9fccb | 228 | */ |
49f693fa DC |
229 | STATIC void |
230 | xfs_bmap_forkoff_reset( | |
49f693fa DC |
231 | xfs_inode_t *ip, |
232 | int whichfork) | |
57c9fccb | 233 | { |
49f693fa DC |
234 | if (whichfork == XFS_ATTR_FORK && |
235 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && | |
236 | ip->i_d.di_format != XFS_DINODE_FMT_UUID && | |
237 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { | |
238 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; | |
57c9fccb | 239 | |
49f693fa DC |
240 | if (dfl_forkoff > ip->i_d.di_forkoff) |
241 | ip->i_d.di_forkoff = dfl_forkoff; | |
242 | } | |
57c9fccb NS |
243 | } |
244 | ||
49f693fa DC |
245 | #ifdef DEBUG |
246 | STATIC struct xfs_buf * | |
247 | xfs_bmap_get_bp( | |
248 | struct xfs_btree_cur *cur, | |
249 | xfs_fsblock_t bno) | |
250 | { | |
251 | struct xfs_log_item_desc *lidp; | |
252 | int i; | |
56b2de80 | 253 | |
49f693fa DC |
254 | if (!cur) |
255 | return NULL; | |
2bd0ea18 | 256 | |
49f693fa DC |
257 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
258 | if (!cur->bc_bufs[i]) | |
259 | break; | |
260 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) | |
261 | return cur->bc_bufs[i]; | |
262 | } | |
56b2de80 | 263 | |
49f693fa DC |
264 | /* Chase down all the log items to see if the bp is there */ |
265 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { | |
266 | struct xfs_buf_log_item *bip; | |
267 | bip = (struct xfs_buf_log_item *)lidp->lid_item; | |
268 | if (bip->bli_item.li_type == XFS_LI_BUF && | |
269 | XFS_BUF_ADDR(bip->bli_buf) == bno) | |
270 | return bip->bli_buf; | |
271 | } | |
2bd0ea18 | 272 | |
49f693fa DC |
273 | return NULL; |
274 | } | |
56b2de80 | 275 | |
49f693fa DC |
276 | STATIC void |
277 | xfs_check_block( | |
278 | struct xfs_btree_block *block, | |
279 | xfs_mount_t *mp, | |
280 | int root, | |
281 | short sz) | |
282 | { | |
283 | int i, j, dmxr; | |
284 | __be64 *pp, *thispa; /* pointer to block address */ | |
285 | xfs_bmbt_key_t *prevp, *keyp; | |
2bd0ea18 | 286 | |
49f693fa | 287 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
56b2de80 | 288 | |
49f693fa DC |
289 | prevp = NULL; |
290 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { | |
291 | dmxr = mp->m_bmap_dmxr[0]; | |
292 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); | |
a2ceac1f | 293 | |
49f693fa DC |
294 | if (prevp) { |
295 | ASSERT(be64_to_cpu(prevp->br_startoff) < | |
296 | be64_to_cpu(keyp->br_startoff)); | |
297 | } | |
298 | prevp = keyp; | |
2bd0ea18 | 299 | |
2bd0ea18 | 300 | /* |
49f693fa | 301 | * Compare the block numbers to see if there are dups. |
2bd0ea18 | 302 | */ |
49f693fa DC |
303 | if (root) |
304 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); | |
305 | else | |
306 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); | |
307 | ||
308 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { | |
309 | if (root) | |
310 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); | |
311 | else | |
312 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); | |
313 | if (*thispa == *pp) { | |
314 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", | |
315 | __func__, j, i, | |
316 | (unsigned long long)be64_to_cpu(*thispa)); | |
317 | panic("%s: ptrs are equal in node\n", | |
318 | __func__); | |
319 | } | |
2bd0ea18 | 320 | } |
49f693fa DC |
321 | } |
322 | } | |
a2ceac1f | 323 | |
49f693fa DC |
324 | /* |
325 | * Check that the extents for the inode ip are in the right order in all | |
f07ae2a6 DC |
326 | * btree leaves. THis becomes prohibitively expensive for large extent count |
327 | * files, so don't bother with inodes that have more than 10,000 extents in | |
328 | * them. The btree record ordering checks will still be done, so for such large | |
329 | * bmapbt constructs that is going to catch most corruptions. | |
49f693fa | 330 | */ |
49f693fa DC |
331 | STATIC void |
332 | xfs_bmap_check_leaf_extents( | |
333 | xfs_btree_cur_t *cur, /* btree cursor or null */ | |
334 | xfs_inode_t *ip, /* incore inode pointer */ | |
335 | int whichfork) /* data or attr fork */ | |
336 | { | |
337 | struct xfs_btree_block *block; /* current btree block */ | |
338 | xfs_fsblock_t bno; /* block # of "block" */ | |
339 | xfs_buf_t *bp; /* buffer for "block" */ | |
340 | int error; /* error return value */ | |
341 | xfs_extnum_t i=0, j; /* index into the extents list */ | |
342 | xfs_ifork_t *ifp; /* fork structure */ | |
343 | int level; /* btree level, for checking */ | |
344 | xfs_mount_t *mp; /* file system mount structure */ | |
345 | __be64 *pp; /* pointer to block address */ | |
346 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ | |
347 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ | |
348 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ | |
349 | int bp_release = 0; | |
350 | ||
351 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { | |
352 | return; | |
353 | } | |
354 | ||
f07ae2a6 DC |
355 | /* skip large extent count inodes */ |
356 | if (ip->i_d.di_nextents > 10000) | |
357 | return; | |
358 | ||
49f693fa DC |
359 | bno = NULLFSBLOCK; |
360 | mp = ip->i_mount; | |
361 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
362 | block = ifp->if_broot; | |
363 | /* | |
364 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. | |
365 | */ | |
366 | level = be16_to_cpu(block->bb_level); | |
367 | ASSERT(level > 0); | |
368 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); | |
369 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); | |
370 | bno = be64_to_cpu(*pp); | |
371 | ||
5a35bf2c | 372 | ASSERT(bno != NULLFSBLOCK); |
49f693fa DC |
373 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
374 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); | |
375 | ||
376 | /* | |
377 | * Go down the tree until leaf level is reached, following the first | |
378 | * pointer (leftmost) at each level. | |
379 | */ | |
380 | while (level-- > 0) { | |
381 | /* See if buf is in cur first */ | |
382 | bp_release = 0; | |
383 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); | |
384 | if (!bp) { | |
385 | bp_release = 1; | |
386 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, | |
387 | XFS_BMAP_BTREE_REF, | |
388 | &xfs_bmbt_buf_ops); | |
2bd0ea18 | 389 | if (error) |
49f693fa | 390 | goto error_norelse; |
2bd0ea18 | 391 | } |
49f693fa | 392 | block = XFS_BUF_TO_BLOCK(bp); |
49f693fa DC |
393 | if (level == 0) |
394 | break; | |
2bd0ea18 | 395 | |
2bd0ea18 | 396 | /* |
49f693fa DC |
397 | * Check this block for basic sanity (increasing keys and |
398 | * no duplicate blocks). | |
2bd0ea18 | 399 | */ |
49f693fa DC |
400 | |
401 | xfs_check_block(block, mp, 0, 0); | |
402 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); | |
403 | bno = be64_to_cpu(*pp); | |
19ebedcf DC |
404 | XFS_WANT_CORRUPTED_GOTO(mp, |
405 | XFS_FSB_SANITY_CHECK(mp, bno), error0); | |
49f693fa DC |
406 | if (bp_release) { |
407 | bp_release = 0; | |
408 | xfs_trans_brelse(NULL, bp); | |
2bd0ea18 | 409 | } |
49f693fa | 410 | } |
a2ceac1f | 411 | |
49f693fa DC |
412 | /* |
413 | * Here with bp and block set to the leftmost leaf node in the tree. | |
414 | */ | |
415 | i = 0; | |
a2ceac1f | 416 | |
49f693fa DC |
417 | /* |
418 | * Loop over all leaf nodes checking that all extents are in the right order. | |
419 | */ | |
420 | for (;;) { | |
421 | xfs_fsblock_t nextbno; | |
422 | xfs_extnum_t num_recs; | |
423 | ||
424 | ||
425 | num_recs = xfs_btree_get_numrecs(block); | |
2bd0ea18 | 426 | |
2bd0ea18 | 427 | /* |
49f693fa | 428 | * Read-ahead the next leaf block, if any. |
2bd0ea18 | 429 | */ |
a2ceac1f | 430 | |
49f693fa | 431 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
a2ceac1f | 432 | |
49f693fa DC |
433 | /* |
434 | * Check all the extents to make sure they are OK. | |
435 | * If we had a previous block, the last entry should | |
436 | * conform with the first entry in this one. | |
437 | */ | |
2bd0ea18 | 438 | |
49f693fa DC |
439 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
440 | if (i) { | |
441 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + | |
442 | xfs_bmbt_disk_get_blockcount(&last) <= | |
443 | xfs_bmbt_disk_get_startoff(ep)); | |
444 | } | |
445 | for (j = 1; j < num_recs; j++) { | |
446 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); | |
447 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + | |
448 | xfs_bmbt_disk_get_blockcount(ep) <= | |
449 | xfs_bmbt_disk_get_startoff(nextp)); | |
450 | ep = nextp; | |
451 | } | |
452 | ||
453 | last = *ep; | |
454 | i += num_recs; | |
455 | if (bp_release) { | |
456 | bp_release = 0; | |
457 | xfs_trans_brelse(NULL, bp); | |
458 | } | |
459 | bno = nextbno; | |
2bd0ea18 | 460 | /* |
49f693fa | 461 | * If we've reached the end, stop. |
2bd0ea18 | 462 | */ |
49f693fa DC |
463 | if (bno == NULLFSBLOCK) |
464 | break; | |
a2ceac1f | 465 | |
49f693fa DC |
466 | bp_release = 0; |
467 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); | |
468 | if (!bp) { | |
469 | bp_release = 1; | |
470 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, | |
471 | XFS_BMAP_BTREE_REF, | |
472 | &xfs_bmbt_buf_ops); | |
a2ceac1f | 473 | if (error) |
49f693fa | 474 | goto error_norelse; |
2bd0ea18 | 475 | } |
49f693fa | 476 | block = XFS_BUF_TO_BLOCK(bp); |
a2ceac1f | 477 | } |
4d4a192c | 478 | |
49f693fa | 479 | return; |
a2ceac1f | 480 | |
49f693fa DC |
481 | error0: |
482 | xfs_warn(mp, "%s: at error0", __func__); | |
483 | if (bp_release) | |
484 | xfs_trans_brelse(NULL, bp); | |
485 | error_norelse: | |
486 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", | |
487 | __func__, i); | |
488 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); | |
489 | return; | |
2bd0ea18 NS |
490 | } |
491 | ||
492 | /* | |
49f693fa | 493 | * Add bmap trace insert entries for all the contents of the extent records. |
2bd0ea18 | 494 | */ |
49f693fa DC |
495 | void |
496 | xfs_bmap_trace_exlist( | |
497 | xfs_inode_t *ip, /* incore inode pointer */ | |
498 | xfs_extnum_t cnt, /* count of entries in the list */ | |
499 | int whichfork, /* data or attr fork */ | |
500 | unsigned long caller_ip) | |
2bd0ea18 | 501 | { |
49f693fa DC |
502 | xfs_extnum_t idx; /* extent record index */ |
503 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
504 | int state = 0; | |
a2ceac1f | 505 | |
49f693fa DC |
506 | if (whichfork == XFS_ATTR_FORK) |
507 | state |= BMAP_ATTRFORK; | |
a2ceac1f | 508 | |
49f693fa DC |
509 | ifp = XFS_IFORK_PTR(ip, whichfork); |
510 | ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); | |
511 | for (idx = 0; idx < cnt; idx++) | |
512 | trace_xfs_extlist(ip, idx, whichfork, caller_ip); | |
513 | } | |
a2ceac1f | 514 | |
49f693fa DC |
515 | /* |
516 | * Validate that the bmbt_irecs being returned from bmapi are valid | |
e6d77a21 DC |
517 | * given the caller's original parameters. Specifically check the |
518 | * ranges of the returned irecs to ensure that they only extend beyond | |
49f693fa DC |
519 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
520 | */ | |
521 | STATIC void | |
522 | xfs_bmap_validate_ret( | |
523 | xfs_fileoff_t bno, | |
524 | xfs_filblks_t len, | |
525 | int flags, | |
526 | xfs_bmbt_irec_t *mval, | |
527 | int nmap, | |
528 | int ret_nmap) | |
529 | { | |
530 | int i; /* index to map values */ | |
a2ceac1f | 531 | |
49f693fa | 532 | ASSERT(ret_nmap <= nmap); |
a2ceac1f | 533 | |
49f693fa DC |
534 | for (i = 0; i < ret_nmap; i++) { |
535 | ASSERT(mval[i].br_blockcount > 0); | |
536 | if (!(flags & XFS_BMAPI_ENTIRE)) { | |
537 | ASSERT(mval[i].br_startoff >= bno); | |
538 | ASSERT(mval[i].br_blockcount <= len); | |
539 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= | |
540 | bno + len); | |
541 | } else { | |
542 | ASSERT(mval[i].br_startoff < bno + len); | |
543 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > | |
544 | bno); | |
545 | } | |
546 | ASSERT(i == 0 || | |
547 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == | |
548 | mval[i].br_startoff); | |
549 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && | |
550 | mval[i].br_startblock != HOLESTARTBLOCK); | |
551 | ASSERT(mval[i].br_state == XFS_EXT_NORM || | |
552 | mval[i].br_state == XFS_EXT_UNWRITTEN); | |
553 | } | |
554 | } | |
56b2de80 | 555 | |
49f693fa DC |
556 | #else |
557 | #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0) | |
558 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) | |
559 | #endif /* DEBUG */ | |
56b2de80 | 560 | |
49f693fa DC |
561 | /* |
562 | * bmap free list manipulation functions | |
563 | */ | |
56b2de80 | 564 | |
49f693fa DC |
565 | /* |
566 | * Add the extent to the list of extents to be free at transaction end. | |
567 | * The list is maintained sorted (by block number). | |
568 | */ | |
569 | void | |
570 | xfs_bmap_add_free( | |
85aec44f DW |
571 | struct xfs_mount *mp, |
572 | struct xfs_defer_ops *dfops, | |
573 | xfs_fsblock_t bno, | |
574 | xfs_filblks_t len, | |
575 | struct xfs_owner_info *oinfo) | |
49f693fa | 576 | { |
6f530e9a | 577 | struct xfs_extent_free_item *new; /* new element */ |
49f693fa DC |
578 | #ifdef DEBUG |
579 | xfs_agnumber_t agno; | |
580 | xfs_agblock_t agbno; | |
56b2de80 | 581 | |
49f693fa DC |
582 | ASSERT(bno != NULLFSBLOCK); |
583 | ASSERT(len > 0); | |
584 | ASSERT(len <= MAXEXTLEN); | |
585 | ASSERT(!isnullstartblock(bno)); | |
586 | agno = XFS_FSB_TO_AGNO(mp, bno); | |
587 | agbno = XFS_FSB_TO_AGBNO(mp, bno); | |
588 | ASSERT(agno < mp->m_sb.sb_agcount); | |
589 | ASSERT(agbno < mp->m_sb.sb_agblocks); | |
590 | ASSERT(len < mp->m_sb.sb_agblocks); | |
591 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); | |
592 | #endif | |
593 | ASSERT(xfs_bmap_free_item_zone != NULL); | |
85aec44f | 594 | |
49f693fa | 595 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
6f530e9a DW |
596 | new->xefi_startblock = bno; |
597 | new->xefi_blockcount = (xfs_extlen_t)len; | |
85aec44f DW |
598 | if (oinfo) |
599 | new->xefi_oinfo = *oinfo; | |
600 | else | |
601 | xfs_rmap_skip_owner_update(&new->xefi_oinfo); | |
a9da40de DW |
602 | trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0, |
603 | XFS_FSB_TO_AGBNO(mp, bno), len); | |
f33cea1a | 604 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list); |
49f693fa | 605 | } |
2bd0ea18 | 606 | |
49f693fa DC |
607 | /* |
608 | * Inode fork format manipulation functions | |
609 | */ | |
a2ceac1f | 610 | |
49f693fa DC |
611 | /* |
612 | * Transform a btree format file with only one leaf node, where the | |
613 | * extents list will fit in the inode, into an extents format file. | |
614 | * Since the file extents are already in-core, all we have to do is | |
615 | * give up the space for the btree root and pitch the leaf block. | |
616 | */ | |
617 | STATIC int /* error */ | |
618 | xfs_bmap_btree_to_extents( | |
619 | xfs_trans_t *tp, /* transaction pointer */ | |
620 | xfs_inode_t *ip, /* incore inode pointer */ | |
621 | xfs_btree_cur_t *cur, /* btree cursor */ | |
622 | int *logflagsp, /* inode logging flags */ | |
623 | int whichfork) /* data or attr fork */ | |
624 | { | |
625 | /* REFERENCED */ | |
626 | struct xfs_btree_block *cblock;/* child btree block */ | |
627 | xfs_fsblock_t cbno; /* child block number */ | |
628 | xfs_buf_t *cbp; /* child block's buffer */ | |
629 | int error; /* error return value */ | |
630 | xfs_ifork_t *ifp; /* inode fork data */ | |
631 | xfs_mount_t *mp; /* mount point structure */ | |
632 | __be64 *pp; /* ptr to block address */ | |
633 | struct xfs_btree_block *rblock;/* root btree block */ | |
85aec44f | 634 | struct xfs_owner_info oinfo; |
56b2de80 | 635 | |
49f693fa DC |
636 | mp = ip->i_mount; |
637 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
1277a5e0 | 638 | ASSERT(whichfork != XFS_COW_FORK); |
49f693fa DC |
639 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
640 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); | |
641 | rblock = ifp->if_broot; | |
642 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); | |
643 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); | |
644 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); | |
645 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); | |
646 | cbno = be64_to_cpu(*pp); | |
647 | *logflagsp = 0; | |
648 | #ifdef DEBUG | |
649 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) | |
650 | return error; | |
651 | #endif | |
652 | error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF, | |
653 | &xfs_bmbt_buf_ops); | |
654 | if (error) | |
655 | return error; | |
656 | cblock = XFS_BUF_TO_BLOCK(cbp); | |
657 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) | |
658 | return error; | |
85aec44f DW |
659 | xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork); |
660 | xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo); | |
49f693fa DC |
661 | ip->i_d.di_nblocks--; |
662 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); | |
663 | xfs_trans_binval(tp, cbp); | |
664 | if (cur->bc_bufs[0] == cbp) | |
665 | cur->bc_bufs[0] = NULL; | |
666 | xfs_iroot_realloc(ip, -1, whichfork); | |
667 | ASSERT(ifp->if_broot == NULL); | |
668 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); | |
669 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); | |
670 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); | |
671 | return 0; | |
672 | } | |
2bd0ea18 NS |
673 | |
674 | /* | |
49f693fa DC |
675 | * Convert an extents-format file into a btree-format file. |
676 | * The new file will have a root block (in the inode) and a single child block. | |
2bd0ea18 | 677 | */ |
49f693fa DC |
678 | STATIC int /* error */ |
679 | xfs_bmap_extents_to_btree( | |
680 | xfs_trans_t *tp, /* transaction pointer */ | |
681 | xfs_inode_t *ip, /* incore inode pointer */ | |
682 | xfs_fsblock_t *firstblock, /* first-block-allocated */ | |
f33cea1a | 683 | struct xfs_defer_ops *dfops, /* blocks freed in xaction */ |
49f693fa DC |
684 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
685 | int wasdel, /* converting a delayed alloc */ | |
686 | int *logflagsp, /* inode logging flags */ | |
687 | int whichfork) /* data or attr fork */ | |
2bd0ea18 | 688 | { |
49f693fa DC |
689 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
690 | xfs_buf_t *abp; /* buffer for ablock */ | |
691 | xfs_alloc_arg_t args; /* allocation arguments */ | |
692 | xfs_bmbt_rec_t *arp; /* child record pointer */ | |
693 | struct xfs_btree_block *block; /* btree root block */ | |
694 | xfs_btree_cur_t *cur; /* bmap btree cursor */ | |
695 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ | |
696 | int error; /* error return value */ | |
697 | xfs_extnum_t i, cnt; /* extent record index */ | |
698 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
699 | xfs_bmbt_key_t *kp; /* root block key pointer */ | |
700 | xfs_mount_t *mp; /* mount structure */ | |
701 | xfs_extnum_t nextents; /* number of file extents */ | |
702 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ | |
2bd0ea18 | 703 | |
5dfa5cd2 | 704 | mp = ip->i_mount; |
1277a5e0 | 705 | ASSERT(whichfork != XFS_COW_FORK); |
49f693fa DC |
706 | ifp = XFS_IFORK_PTR(ip, whichfork); |
707 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); | |
56b2de80 | 708 | |
2bd0ea18 | 709 | /* |
49f693fa | 710 | * Make space in the inode incore. |
2bd0ea18 | 711 | */ |
49f693fa DC |
712 | xfs_iroot_realloc(ip, 1, whichfork); |
713 | ifp->if_flags |= XFS_IFBROOT; | |
56b2de80 | 714 | |
2bd0ea18 | 715 | /* |
49f693fa | 716 | * Fill in the root. |
2bd0ea18 | 717 | */ |
49f693fa | 718 | block = ifp->if_broot; |
5dfa5cd2 DC |
719 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
720 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, | |
721 | XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino, | |
722 | XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS); | |
723 | else | |
724 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, | |
725 | XFS_BMAP_MAGIC, 1, 1, ip->i_ino, | |
726 | XFS_BTREE_LONG_PTRS); | |
56b2de80 | 727 | |
49f693fa DC |
728 | /* |
729 | * Need a cursor. Can't allocate until bb_level is filled in. | |
730 | */ | |
49f693fa DC |
731 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
732 | cur->bc_private.b.firstblock = *firstblock; | |
f33cea1a | 733 | cur->bc_private.b.dfops = dfops; |
49f693fa DC |
734 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
735 | /* | |
736 | * Convert to a btree with two levels, one record in root. | |
737 | */ | |
738 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); | |
739 | memset(&args, 0, sizeof(args)); | |
740 | args.tp = tp; | |
741 | args.mp = mp; | |
85aec44f | 742 | xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork); |
49f693fa DC |
743 | args.firstblock = *firstblock; |
744 | if (*firstblock == NULLFSBLOCK) { | |
745 | args.type = XFS_ALLOCTYPE_START_BNO; | |
746 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); | |
f33cea1a | 747 | } else if (dfops->dop_low) { |
68d06dce | 748 | try_another_ag: |
49f693fa DC |
749 | args.type = XFS_ALLOCTYPE_START_BNO; |
750 | args.fsbno = *firstblock; | |
751 | } else { | |
752 | args.type = XFS_ALLOCTYPE_NEAR_BNO; | |
753 | args.fsbno = *firstblock; | |
754 | } | |
755 | args.minlen = args.maxlen = args.prod = 1; | |
756 | args.wasdel = wasdel; | |
757 | *logflagsp = 0; | |
758 | if ((error = xfs_alloc_vextent(&args))) { | |
759 | xfs_iroot_realloc(ip, -1, whichfork); | |
760 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
761 | return error; | |
2bd0ea18 | 762 | } |
68d06dce DW |
763 | |
764 | /* | |
765 | * During a CoW operation, the allocation and bmbt updates occur in | |
766 | * different transactions. The mapping code tries to put new bmbt | |
767 | * blocks near extents being mapped, but the only way to guarantee this | |
768 | * is if the alloc and the mapping happen in a single transaction that | |
769 | * has a block reservation. That isn't the case here, so if we run out | |
770 | * of space we'll try again with another AG. | |
771 | */ | |
772 | if (xfs_sb_version_hasreflink(&cur->bc_mp->m_sb) && | |
773 | args.fsbno == NULLFSBLOCK && | |
774 | args.type == XFS_ALLOCTYPE_NEAR_BNO) { | |
775 | dfops->dop_low = true; | |
776 | goto try_another_ag; | |
777 | } | |
2bd0ea18 | 778 | /* |
49f693fa | 779 | * Allocation can't fail, the space was reserved. |
2bd0ea18 | 780 | */ |
49f693fa DC |
781 | ASSERT(args.fsbno != NULLFSBLOCK); |
782 | ASSERT(*firstblock == NULLFSBLOCK || | |
783 | args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || | |
f33cea1a | 784 | (dfops->dop_low && |
49f693fa DC |
785 | args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); |
786 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; | |
787 | cur->bc_private.b.allocated++; | |
788 | ip->i_d.di_nblocks++; | |
789 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); | |
790 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); | |
2bd0ea18 | 791 | /* |
49f693fa | 792 | * Fill in the child block. |
2bd0ea18 | 793 | */ |
49f693fa DC |
794 | abp->b_ops = &xfs_bmbt_buf_ops; |
795 | ablock = XFS_BUF_TO_BLOCK(abp); | |
5dfa5cd2 DC |
796 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
797 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, | |
798 | XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino, | |
799 | XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS); | |
800 | else | |
801 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, | |
802 | XFS_BMAP_MAGIC, 0, 0, ip->i_ino, | |
803 | XFS_BTREE_LONG_PTRS); | |
804 | ||
49f693fa DC |
805 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
806 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | |
807 | for (cnt = i = 0; i < nextents; i++) { | |
808 | ep = xfs_iext_get_ext(ifp, i); | |
809 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { | |
810 | arp->l0 = cpu_to_be64(ep->l0); | |
811 | arp->l1 = cpu_to_be64(ep->l1); | |
812 | arp++; cnt++; | |
813 | } | |
814 | } | |
815 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); | |
816 | xfs_btree_set_numrecs(ablock, cnt); | |
56b2de80 | 817 | |
49f693fa DC |
818 | /* |
819 | * Fill in the root key and pointer. | |
820 | */ | |
821 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); | |
822 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); | |
823 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); | |
824 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, | |
825 | be16_to_cpu(block->bb_level))); | |
826 | *pp = cpu_to_be64(args.fsbno); | |
2bd0ea18 | 827 | |
49f693fa DC |
828 | /* |
829 | * Do all this logging at the end so that | |
830 | * the root is at the right level. | |
831 | */ | |
5dfa5cd2 | 832 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
613e6057 | 833 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
49f693fa DC |
834 | ASSERT(*curp == NULL); |
835 | *curp = cur; | |
836 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); | |
837 | return 0; | |
838 | } | |
a2ceac1f | 839 | |
49f693fa DC |
840 | /* |
841 | * Convert a local file to an extents file. | |
842 | * This code is out of bounds for data forks of regular files, | |
843 | * since the file data needs to get logged so things will stay consistent. | |
844 | * (The bmap-level manipulations are ok, though). | |
845 | */ | |
3f17ed4b DC |
846 | void |
847 | xfs_bmap_local_to_extents_empty( | |
848 | struct xfs_inode *ip, | |
849 | int whichfork) | |
850 | { | |
851 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); | |
852 | ||
1277a5e0 | 853 | ASSERT(whichfork != XFS_COW_FORK); |
3f17ed4b DC |
854 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
855 | ASSERT(ifp->if_bytes == 0); | |
856 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); | |
857 | ||
ff105f75 | 858 | xfs_bmap_forkoff_reset(ip, whichfork); |
3f17ed4b DC |
859 | ifp->if_flags &= ~XFS_IFINLINE; |
860 | ifp->if_flags |= XFS_IFEXTENTS; | |
861 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); | |
862 | } | |
863 | ||
864 | ||
49f693fa DC |
865 | STATIC int /* error */ |
866 | xfs_bmap_local_to_extents( | |
867 | xfs_trans_t *tp, /* transaction pointer */ | |
868 | xfs_inode_t *ip, /* incore inode pointer */ | |
869 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ | |
870 | xfs_extlen_t total, /* total blocks needed by transaction */ | |
871 | int *logflagsp, /* inode logging flags */ | |
872 | int whichfork, | |
5dfa5cd2 DC |
873 | void (*init_fn)(struct xfs_trans *tp, |
874 | struct xfs_buf *bp, | |
49f693fa DC |
875 | struct xfs_inode *ip, |
876 | struct xfs_ifork *ifp)) | |
877 | { | |
3f17ed4b | 878 | int error = 0; |
49f693fa DC |
879 | int flags; /* logging flags returned */ |
880 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
3f17ed4b DC |
881 | xfs_alloc_arg_t args; /* allocation arguments */ |
882 | xfs_buf_t *bp; /* buffer for extent block */ | |
883 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ | |
2bd0ea18 | 884 | |
49f693fa DC |
885 | /* |
886 | * We don't want to deal with the case of keeping inode data inline yet. | |
887 | * So sending the data fork of a regular inode is invalid. | |
888 | */ | |
e37bf53c | 889 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
49f693fa DC |
890 | ifp = XFS_IFORK_PTR(ip, whichfork); |
891 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); | |
3f17ed4b DC |
892 | |
893 | if (!ifp->if_bytes) { | |
894 | xfs_bmap_local_to_extents_empty(ip, whichfork); | |
895 | flags = XFS_ILOG_CORE; | |
896 | goto done; | |
897 | } | |
898 | ||
49f693fa DC |
899 | flags = 0; |
900 | error = 0; | |
3f17ed4b DC |
901 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == |
902 | XFS_IFINLINE); | |
903 | memset(&args, 0, sizeof(args)); | |
904 | args.tp = tp; | |
905 | args.mp = ip->i_mount; | |
85aec44f | 906 | xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0); |
3f17ed4b DC |
907 | args.firstblock = *firstblock; |
908 | /* | |
909 | * Allocate a block. We know we need only one, since the | |
910 | * file currently fits in an inode. | |
911 | */ | |
912 | if (*firstblock == NULLFSBLOCK) { | |
68d06dce | 913 | try_another_ag: |
3f17ed4b DC |
914 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
915 | args.type = XFS_ALLOCTYPE_START_BNO; | |
49f693fa | 916 | } else { |
3f17ed4b DC |
917 | args.fsbno = *firstblock; |
918 | args.type = XFS_ALLOCTYPE_NEAR_BNO; | |
2bd0ea18 | 919 | } |
3f17ed4b DC |
920 | args.total = total; |
921 | args.minlen = args.maxlen = args.prod = 1; | |
922 | error = xfs_alloc_vextent(&args); | |
923 | if (error) | |
924 | goto done; | |
925 | ||
68d06dce DW |
926 | /* |
927 | * During a CoW operation, the allocation and bmbt updates occur in | |
928 | * different transactions. The mapping code tries to put new bmbt | |
929 | * blocks near extents being mapped, but the only way to guarantee this | |
930 | * is if the alloc and the mapping happen in a single transaction that | |
931 | * has a block reservation. That isn't the case here, so if we run out | |
932 | * of space we'll try again with another AG. | |
933 | */ | |
934 | if (xfs_sb_version_hasreflink(&ip->i_mount->m_sb) && | |
935 | args.fsbno == NULLFSBLOCK && | |
936 | args.type == XFS_ALLOCTYPE_NEAR_BNO) { | |
937 | goto try_another_ag; | |
938 | } | |
3f17ed4b DC |
939 | /* Can't fail, the space was reserved. */ |
940 | ASSERT(args.fsbno != NULLFSBLOCK); | |
941 | ASSERT(args.len == 1); | |
942 | *firstblock = args.fsbno; | |
943 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); | |
944 | ||
19ebedcf | 945 | /* |
f44fbde0 | 946 | * Initialize the block, copy the data and log the remote buffer. |
19ebedcf | 947 | * |
f44fbde0 BF |
948 | * The callout is responsible for logging because the remote format |
949 | * might differ from the local format and thus we don't know how much to | |
950 | * log here. Note that init_fn must also set the buffer log item type | |
951 | * correctly. | |
19ebedcf | 952 | */ |
3f17ed4b DC |
953 | init_fn(tp, bp, ip, ifp); |
954 | ||
f44fbde0 | 955 | /* account for the change in fork size */ |
3f17ed4b DC |
956 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
957 | xfs_bmap_local_to_extents_empty(ip, whichfork); | |
49f693fa | 958 | flags |= XFS_ILOG_CORE; |
3f17ed4b DC |
959 | |
960 | xfs_iext_add(ifp, 0, 1); | |
961 | ep = xfs_iext_get_ext(ifp, 0); | |
962 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); | |
963 | trace_xfs_bmap_post_update(ip, 0, | |
964 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, | |
965 | _THIS_IP_); | |
966 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); | |
967 | ip->i_d.di_nblocks = 1; | |
968 | xfs_trans_mod_dquot_byino(tp, ip, | |
969 | XFS_TRANS_DQ_BCOUNT, 1L); | |
970 | flags |= xfs_ilog_fext(whichfork); | |
971 | ||
49f693fa DC |
972 | done: |
973 | *logflagsp = flags; | |
974 | return error; | |
2bd0ea18 NS |
975 | } |
976 | ||
977 | /* | |
49f693fa | 978 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
2bd0ea18 | 979 | */ |
49f693fa DC |
980 | STATIC int /* error */ |
981 | xfs_bmap_add_attrfork_btree( | |
982 | xfs_trans_t *tp, /* transaction pointer */ | |
983 | xfs_inode_t *ip, /* incore inode pointer */ | |
984 | xfs_fsblock_t *firstblock, /* first block allocated */ | |
f33cea1a | 985 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
49f693fa | 986 | int *flags) /* inode logging flags */ |
2bd0ea18 | 987 | { |
49f693fa DC |
988 | xfs_btree_cur_t *cur; /* btree cursor */ |
989 | int error; /* error return value */ | |
990 | xfs_mount_t *mp; /* file system mount struct */ | |
991 | int stat; /* newroot status */ | |
56b2de80 | 992 | |
49f693fa DC |
993 | mp = ip->i_mount; |
994 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) | |
995 | *flags |= XFS_ILOG_DBROOT; | |
996 | else { | |
997 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); | |
f33cea1a | 998 | cur->bc_private.b.dfops = dfops; |
49f693fa DC |
999 | cur->bc_private.b.firstblock = *firstblock; |
1000 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) | |
1001 | goto error0; | |
1002 | /* must be at least one entry */ | |
19ebedcf | 1003 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
49f693fa DC |
1004 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
1005 | goto error0; | |
1006 | if (stat == 0) { | |
1007 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
12b53197 | 1008 | return -ENOSPC; |
49f693fa DC |
1009 | } |
1010 | *firstblock = cur->bc_private.b.firstblock; | |
1011 | cur->bc_private.b.allocated = 0; | |
1012 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); | |
2bd0ea18 | 1013 | } |
49f693fa DC |
1014 | return 0; |
1015 | error0: | |
1016 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); | |
1017 | return error; | |
1018 | } | |
56b2de80 | 1019 | |
49f693fa DC |
1020 | /* |
1021 | * Called from xfs_bmap_add_attrfork to handle extents format files. | |
1022 | */ | |
1023 | STATIC int /* error */ | |
1024 | xfs_bmap_add_attrfork_extents( | |
1025 | xfs_trans_t *tp, /* transaction pointer */ | |
1026 | xfs_inode_t *ip, /* incore inode pointer */ | |
1027 | xfs_fsblock_t *firstblock, /* first block allocated */ | |
f33cea1a | 1028 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
49f693fa DC |
1029 | int *flags) /* inode logging flags */ |
1030 | { | |
1031 | xfs_btree_cur_t *cur; /* bmap btree cursor */ | |
1032 | int error; /* error return value */ | |
1033 | ||
1034 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) | |
1035 | return 0; | |
1036 | cur = NULL; | |
f33cea1a | 1037 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0, |
49f693fa DC |
1038 | flags, XFS_DATA_FORK); |
1039 | if (cur) { | |
1040 | cur->bc_private.b.allocated = 0; | |
1041 | xfs_btree_del_cursor(cur, | |
1042 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); | |
2bd0ea18 | 1043 | } |
49f693fa DC |
1044 | return error; |
1045 | } | |
56b2de80 | 1046 | |
49f693fa DC |
1047 | /* |
1048 | * Called from xfs_bmap_add_attrfork to handle local format files. Each | |
1049 | * different data fork content type needs a different callout to do the | |
1050 | * conversion. Some are basic and only require special block initialisation | |
1051 | * callouts for the data formating, others (directories) are so specialised they | |
1052 | * handle everything themselves. | |
1053 | * | |
1054 | * XXX (dgc): investigate whether directory conversion can use the generic | |
1055 | * formatting callout. It should be possible - it's just a very complex | |
5dfa5cd2 | 1056 | * formatter. |
49f693fa DC |
1057 | */ |
1058 | STATIC int /* error */ | |
1059 | xfs_bmap_add_attrfork_local( | |
1060 | xfs_trans_t *tp, /* transaction pointer */ | |
1061 | xfs_inode_t *ip, /* incore inode pointer */ | |
1062 | xfs_fsblock_t *firstblock, /* first block allocated */ | |
f33cea1a | 1063 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
49f693fa DC |
1064 | int *flags) /* inode logging flags */ |
1065 | { | |
1066 | xfs_da_args_t dargs; /* args for dir/attr code */ | |
56b2de80 | 1067 | |
49f693fa DC |
1068 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
1069 | return 0; | |
a2ceac1f | 1070 | |
e37bf53c | 1071 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
49f693fa | 1072 | memset(&dargs, 0, sizeof(dargs)); |
ff105f75 | 1073 | dargs.geo = ip->i_mount->m_dir_geo; |
49f693fa DC |
1074 | dargs.dp = ip; |
1075 | dargs.firstblock = firstblock; | |
f33cea1a | 1076 | dargs.dfops = dfops; |
ff105f75 | 1077 | dargs.total = dargs.geo->fsbcount; |
49f693fa DC |
1078 | dargs.whichfork = XFS_DATA_FORK; |
1079 | dargs.trans = tp; | |
1080 | return xfs_dir2_sf_to_block(&dargs); | |
1081 | } | |
2bd0ea18 | 1082 | |
e37bf53c | 1083 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
49f693fa DC |
1084 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, |
1085 | flags, XFS_DATA_FORK, | |
1086 | xfs_symlink_local_to_remote); | |
56b2de80 | 1087 | |
3f17ed4b DC |
1088 | /* should only be called for types that support local format data */ |
1089 | ASSERT(0); | |
12b53197 | 1090 | return -EFSCORRUPTED; |
49f693fa | 1091 | } |
2bd0ea18 | 1092 | |
49f693fa DC |
1093 | /* |
1094 | * Convert inode from non-attributed to attributed. | |
1095 | * Must not be in a transaction, ip must not be locked. | |
1096 | */ | |
1097 | int /* error code */ | |
1098 | xfs_bmap_add_attrfork( | |
1099 | xfs_inode_t *ip, /* incore inode pointer */ | |
1100 | int size, /* space new attribute needs */ | |
1101 | int rsvd) /* xact may use reserved blks */ | |
1102 | { | |
1103 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ | |
f33cea1a | 1104 | struct xfs_defer_ops dfops; /* freed extent records */ |
49f693fa DC |
1105 | xfs_mount_t *mp; /* mount structure */ |
1106 | xfs_trans_t *tp; /* transaction pointer */ | |
1107 | int blks; /* space reservation */ | |
1108 | int version = 1; /* superblock attr version */ | |
49f693fa DC |
1109 | int logflags; /* logging flags */ |
1110 | int error; /* error return value */ | |
56b2de80 | 1111 | |
49f693fa | 1112 | ASSERT(XFS_IFORK_Q(ip) == 0); |
2bd0ea18 | 1113 | |
49f693fa DC |
1114 | mp = ip->i_mount; |
1115 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); | |
9074815c | 1116 | |
49f693fa | 1117 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
9074815c CH |
1118 | |
1119 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, | |
1120 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); | |
1121 | if (error) | |
ff105f75 | 1122 | return error; |
9074815c | 1123 | |
49f693fa DC |
1124 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
1125 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? | |
1126 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : | |
1127 | XFS_QMOPT_RES_REGBLKS); | |
ff105f75 DC |
1128 | if (error) |
1129 | goto trans_cancel; | |
49f693fa | 1130 | if (XFS_IFORK_Q(ip)) |
ff105f75 | 1131 | goto trans_cancel; |
49f693fa | 1132 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
2bd0ea18 | 1133 | /* |
49f693fa | 1134 | * For inodes coming from pre-6.2 filesystems. |
2bd0ea18 | 1135 | */ |
49f693fa DC |
1136 | ASSERT(ip->i_d.di_aformat == 0); |
1137 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; | |
5e656dbb | 1138 | } |
49f693fa | 1139 | ASSERT(ip->i_d.di_anextents == 0); |
a2ceac1f | 1140 | |
ff105f75 | 1141 | xfs_trans_ijoin(tp, ip, 0); |
49f693fa | 1142 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
a2ceac1f | 1143 | |
49f693fa DC |
1144 | switch (ip->i_d.di_format) { |
1145 | case XFS_DINODE_FMT_DEV: | |
1146 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; | |
1147 | break; | |
1148 | case XFS_DINODE_FMT_UUID: | |
1149 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; | |
1150 | break; | |
1151 | case XFS_DINODE_FMT_LOCAL: | |
1152 | case XFS_DINODE_FMT_EXTENTS: | |
1153 | case XFS_DINODE_FMT_BTREE: | |
1154 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); | |
1155 | if (!ip->i_d.di_forkoff) | |
1156 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; | |
1157 | else if (mp->m_flags & XFS_MOUNT_ATTR2) | |
1158 | version = 2; | |
1159 | break; | |
1160 | default: | |
1161 | ASSERT(0); | |
12b53197 | 1162 | error = -EINVAL; |
ff105f75 | 1163 | goto trans_cancel; |
a2ceac1f DC |
1164 | } |
1165 | ||
49f693fa DC |
1166 | ASSERT(ip->i_afp == NULL); |
1167 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); | |
1168 | ip->i_afp->if_flags = XFS_IFEXTENTS; | |
1169 | logflags = 0; | |
f33cea1a | 1170 | xfs_defer_init(&dfops, &firstblock); |
49f693fa DC |
1171 | switch (ip->i_d.di_format) { |
1172 | case XFS_DINODE_FMT_LOCAL: | |
f33cea1a | 1173 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops, |
49f693fa DC |
1174 | &logflags); |
1175 | break; | |
1176 | case XFS_DINODE_FMT_EXTENTS: | |
1177 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, | |
f33cea1a | 1178 | &dfops, &logflags); |
49f693fa DC |
1179 | break; |
1180 | case XFS_DINODE_FMT_BTREE: | |
f33cea1a | 1181 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops, |
49f693fa DC |
1182 | &logflags); |
1183 | break; | |
1184 | default: | |
1185 | error = 0; | |
1186 | break; | |
1187 | } | |
1188 | if (logflags) | |
1189 | xfs_trans_log_inode(tp, ip, logflags); | |
1190 | if (error) | |
ff105f75 | 1191 | goto bmap_cancel; |
49f693fa DC |
1192 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
1193 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { | |
19ebedcf | 1194 | bool log_sb = false; |
a2ceac1f | 1195 | |
49f693fa DC |
1196 | spin_lock(&mp->m_sb_lock); |
1197 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { | |
1198 | xfs_sb_version_addattr(&mp->m_sb); | |
19ebedcf | 1199 | log_sb = true; |
49f693fa DC |
1200 | } |
1201 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { | |
1202 | xfs_sb_version_addattr2(&mp->m_sb); | |
19ebedcf | 1203 | log_sb = true; |
49f693fa | 1204 | } |
19ebedcf DC |
1205 | spin_unlock(&mp->m_sb_lock); |
1206 | if (log_sb) | |
1207 | xfs_log_sb(tp); | |
49f693fa DC |
1208 | } |
1209 | ||
f33cea1a | 1210 | error = xfs_defer_finish(&tp, &dfops, NULL); |
49f693fa | 1211 | if (error) |
ff105f75 | 1212 | goto bmap_cancel; |
de5a3f46 | 1213 | error = xfs_trans_commit(tp); |
ff105f75 DC |
1214 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
1215 | return error; | |
1216 | ||
1217 | bmap_cancel: | |
f33cea1a | 1218 | xfs_defer_cancel(&dfops); |
ff105f75 | 1219 | trans_cancel: |
3d7434fe | 1220 | xfs_trans_cancel(tp); |
49f693fa | 1221 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
5e656dbb | 1222 | return error; |
2bd0ea18 NS |
1223 | } |
1224 | ||
399ab595 | 1225 | /* |
49f693fa DC |
1226 | * Internal and external extent tree search functions. |
1227 | */ | |
399ab595 | 1228 | |
49f693fa DC |
1229 | /* |
1230 | * Read in the extents to if_extents. | |
1231 | * All inode fields are set up by caller, we just traverse the btree | |
1232 | * and copy the records in. If the file system cannot contain unwritten | |
1233 | * extents, the records are checked for no "state" flags. | |
1234 | */ | |
1235 | int /* error */ | |
1236 | xfs_bmap_read_extents( | |
1237 | xfs_trans_t *tp, /* transaction pointer */ | |
1238 | xfs_inode_t *ip, /* incore inode */ | |
1239 | int whichfork) /* data or attr fork */ | |
1240 | { | |
1241 | struct xfs_btree_block *block; /* current btree block */ | |
1242 | xfs_fsblock_t bno; /* block # of "block" */ | |
1243 | xfs_buf_t *bp; /* buffer for "block" */ | |
1244 | int error; /* error return value */ | |
1245 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ | |
1246 | xfs_extnum_t i, j; /* index into the extents list */ | |
1247 | xfs_ifork_t *ifp; /* fork structure */ | |
1248 | int level; /* btree level, for checking */ | |
1249 | xfs_mount_t *mp; /* file system mount structure */ | |
1250 | __be64 *pp; /* pointer to block address */ | |
1251 | /* REFERENCED */ | |
1252 | xfs_extnum_t room; /* number of entries there's room for */ | |
399ab595 | 1253 | |
49f693fa DC |
1254 | bno = NULLFSBLOCK; |
1255 | mp = ip->i_mount; | |
1256 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
1257 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : | |
1258 | XFS_EXTFMT_INODE(ip); | |
1259 | block = ifp->if_broot; | |
399ab595 | 1260 | /* |
49f693fa | 1261 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
399ab595 | 1262 | */ |
49f693fa DC |
1263 | level = be16_to_cpu(block->bb_level); |
1264 | ASSERT(level > 0); | |
1265 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); | |
1266 | bno = be64_to_cpu(*pp); | |
5a35bf2c | 1267 | ASSERT(bno != NULLFSBLOCK); |
49f693fa DC |
1268 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
1269 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); | |
399ab595 | 1270 | /* |
49f693fa DC |
1271 | * Go down the tree until leaf level is reached, following the first |
1272 | * pointer (leftmost) at each level. | |
399ab595 | 1273 | */ |
49f693fa DC |
1274 | while (level-- > 0) { |
1275 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, | |
1276 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); | |
1277 | if (error) | |
1278 | return error; | |
1279 | block = XFS_BUF_TO_BLOCK(bp); | |
49f693fa DC |
1280 | if (level == 0) |
1281 | break; | |
1282 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); | |
1283 | bno = be64_to_cpu(*pp); | |
19ebedcf DC |
1284 | XFS_WANT_CORRUPTED_GOTO(mp, |
1285 | XFS_FSB_SANITY_CHECK(mp, bno), error0); | |
49f693fa | 1286 | xfs_trans_brelse(tp, bp); |
399ab595 NS |
1287 | } |
1288 | /* | |
49f693fa | 1289 | * Here with bp and block set to the leftmost leaf node in the tree. |
399ab595 | 1290 | */ |
49f693fa DC |
1291 | room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
1292 | i = 0; | |
399ab595 | 1293 | /* |
49f693fa | 1294 | * Loop over all leaf nodes. Copy information to the extent records. |
399ab595 | 1295 | */ |
49f693fa DC |
1296 | for (;;) { |
1297 | xfs_bmbt_rec_t *frp; | |
1298 | xfs_fsblock_t nextbno; | |
1299 | xfs_extnum_t num_recs; | |
1300 | xfs_extnum_t start; | |
399ab595 | 1301 | |
49f693fa DC |
1302 | num_recs = xfs_btree_get_numrecs(block); |
1303 | if (unlikely(i + num_recs > room)) { | |
1304 | ASSERT(i + num_recs <= room); | |
1305 | xfs_warn(ip->i_mount, | |
1306 | "corrupt dinode %Lu, (btree extents).", | |
1307 | (unsigned long long) ip->i_ino); | |
1308 | XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)", | |
1309 | XFS_ERRLEVEL_LOW, ip->i_mount, block); | |
1310 | goto error0; | |
399ab595 NS |
1311 | } |
1312 | /* | |
49f693fa | 1313 | * Read-ahead the next leaf block, if any. |
399ab595 | 1314 | */ |
49f693fa DC |
1315 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
1316 | if (nextbno != NULLFSBLOCK) | |
1317 | xfs_btree_reada_bufl(mp, nextbno, 1, | |
1318 | &xfs_bmbt_buf_ops); | |
399ab595 | 1319 | /* |
49f693fa | 1320 | * Copy records into the extent records. |
399ab595 | 1321 | */ |
49f693fa DC |
1322 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
1323 | start = i; | |
1324 | for (j = 0; j < num_recs; j++, i++, frp++) { | |
1325 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); | |
1326 | trp->l0 = be64_to_cpu(frp->l0); | |
1327 | trp->l1 = be64_to_cpu(frp->l1); | |
1328 | } | |
1329 | if (exntf == XFS_EXTFMT_NOSTATE) { | |
1330 | /* | |
1331 | * Check all attribute bmap btree records and | |
1332 | * any "older" data bmap btree records for a | |
1333 | * set bit in the "extent flag" position. | |
1334 | */ | |
1335 | if (unlikely(xfs_check_nostate_extents(ifp, | |
1336 | start, num_recs))) { | |
1337 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", | |
1338 | XFS_ERRLEVEL_LOW, | |
1339 | ip->i_mount); | |
1340 | goto error0; | |
1341 | } | |
399ab595 | 1342 | } |
49f693fa DC |
1343 | xfs_trans_brelse(tp, bp); |
1344 | bno = nextbno; | |
399ab595 | 1345 | /* |
49f693fa | 1346 | * If we've reached the end, stop. |
399ab595 | 1347 | */ |
49f693fa DC |
1348 | if (bno == NULLFSBLOCK) |
1349 | break; | |
1350 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, | |
1351 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); | |
1352 | if (error) | |
1353 | return error; | |
1354 | block = XFS_BUF_TO_BLOCK(bp); | |
399ab595 | 1355 | } |
49f693fa DC |
1356 | ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
1357 | ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork)); | |
1358 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); | |
1359 | return 0; | |
1360 | error0: | |
1361 | xfs_trans_brelse(tp, bp); | |
12b53197 | 1362 | return -EFSCORRUPTED; |
49f693fa | 1363 | } |
399ab595 | 1364 | |
49f693fa DC |
1365 | |
1366 | /* | |
1367 | * Search the extent records for the entry containing block bno. | |
1368 | * If bno lies in a hole, point to the next entry. If bno lies | |
1369 | * past eof, *eofp will be set, and *prevp will contain the last | |
1370 | * entry (null if none). Else, *lastxp will be set to the index | |
1371 | * of the found entry; *gotp will contain the entry. | |
1372 | */ | |
1373 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ | |
1374 | xfs_bmap_search_multi_extents( | |
1375 | xfs_ifork_t *ifp, /* inode fork pointer */ | |
1376 | xfs_fileoff_t bno, /* block number searched for */ | |
1377 | int *eofp, /* out: end of file found */ | |
1378 | xfs_extnum_t *lastxp, /* out: last extent index */ | |
1379 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ | |
1380 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ | |
1381 | { | |
1382 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ | |
1383 | xfs_extnum_t lastx; /* last extent index */ | |
1384 | ||
1385 | /* | |
1386 | * Initialize the extent entry structure to catch access to | |
1387 | * uninitialized br_startblock field. | |
1388 | */ | |
1389 | gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL; | |
1390 | gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL; | |
1391 | gotp->br_state = XFS_EXT_INVALID; | |
49f693fa | 1392 | gotp->br_startblock = 0xffffa5a5a5a5a5a5LL; |
49f693fa | 1393 | prevp->br_startoff = NULLFILEOFF; |
399ab595 | 1394 | |
49f693fa DC |
1395 | ep = xfs_iext_bno_to_ext(ifp, bno, &lastx); |
1396 | if (lastx > 0) { | |
1397 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp); | |
1398 | } | |
1399 | if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { | |
1400 | xfs_bmbt_get_all(ep, gotp); | |
1401 | *eofp = 0; | |
1402 | } else { | |
1403 | if (lastx > 0) { | |
1404 | *gotp = *prevp; | |
1405 | } | |
1406 | *eofp = 1; | |
1407 | ep = NULL; | |
1408 | } | |
1409 | *lastxp = lastx; | |
1410 | return ep; | |
399ab595 NS |
1411 | } |
1412 | ||
49f693fa DC |
1413 | /* |
1414 | * Search the extents list for the inode, for the extent containing bno. | |
1415 | * If bno lies in a hole, point to the next entry. If bno lies past eof, | |
1416 | * *eofp will be set, and *prevp will contain the last entry (null if none). | |
1417 | * Else, *lastxp will be set to the index of the found | |
1418 | * entry; *gotp will contain the entry. | |
1419 | */ | |
1420 | xfs_bmbt_rec_host_t * /* pointer to found extent entry */ | |
1421 | xfs_bmap_search_extents( | |
1422 | xfs_inode_t *ip, /* incore inode pointer */ | |
1423 | xfs_fileoff_t bno, /* block number searched for */ | |
1424 | int fork, /* data or attr fork */ | |
1425 | int *eofp, /* out: end of file found */ | |
1426 | xfs_extnum_t *lastxp, /* out: last extent index */ | |
1427 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ | |
1428 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ | |
2bd0ea18 | 1429 | { |
49f693fa DC |
1430 | xfs_ifork_t *ifp; /* inode fork pointer */ |
1431 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ | |
399ab595 | 1432 | |
79896434 | 1433 | XFS_STATS_INC(ip->i_mount, xs_look_exlist); |
49f693fa | 1434 | ifp = XFS_IFORK_PTR(ip, fork); |
399ab595 | 1435 | |
49f693fa DC |
1436 | ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp); |
1437 | ||
1438 | if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) && | |
1439 | !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) { | |
1440 | xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO, | |
1441 | "Access to block zero in inode %llu " | |
1442 | "start_block: %llx start_off: %llx " | |
12864fd9 | 1443 | "blkcnt: %llx extent-state: %x lastx: %x", |
49f693fa DC |
1444 | (unsigned long long)ip->i_ino, |
1445 | (unsigned long long)gotp->br_startblock, | |
1446 | (unsigned long long)gotp->br_startoff, | |
1447 | (unsigned long long)gotp->br_blockcount, | |
1448 | gotp->br_state, *lastxp); | |
1449 | *lastxp = NULLEXTNUM; | |
1450 | *eofp = 1; | |
1451 | return NULL; | |
2bd0ea18 | 1452 | } |
49f693fa DC |
1453 | return ep; |
1454 | } | |
2bd0ea18 | 1455 | |
49f693fa DC |
1456 | /* |
1457 | * Returns the file-relative block number of the first unused block(s) | |
1458 | * in the file with at least "len" logically contiguous blocks free. | |
1459 | * This is the lowest-address hole if the file has holes, else the first block | |
1460 | * past the end of file. | |
1461 | * Return 0 if the file is currently local (in-inode). | |
1462 | */ | |
1463 | int /* error */ | |
1464 | xfs_bmap_first_unused( | |
1465 | xfs_trans_t *tp, /* transaction pointer */ | |
1466 | xfs_inode_t *ip, /* incore inode */ | |
1467 | xfs_extlen_t len, /* size of hole to find */ | |
1468 | xfs_fileoff_t *first_unused, /* unused block */ | |
1469 | int whichfork) /* data or attr fork */ | |
1470 | { | |
1471 | int error; /* error return value */ | |
1472 | int idx; /* extent record index */ | |
1473 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
1474 | xfs_fileoff_t lastaddr; /* last block number seen */ | |
1475 | xfs_fileoff_t lowest; /* lowest useful block */ | |
1476 | xfs_fileoff_t max; /* starting useful block */ | |
1477 | xfs_fileoff_t off; /* offset for this block */ | |
1478 | xfs_extnum_t nextents; /* number of extent entries */ | |
1479 | ||
1480 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || | |
1481 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || | |
1482 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); | |
1483 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { | |
1484 | *first_unused = 0; | |
1485 | return 0; | |
1486 | } | |
1487 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
1488 | if (!(ifp->if_flags & XFS_IFEXTENTS) && | |
1489 | (error = xfs_iread_extents(tp, ip, whichfork))) | |
1490 | return error; | |
1491 | lowest = *first_unused; | |
1492 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | |
1493 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { | |
1494 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); | |
1495 | off = xfs_bmbt_get_startoff(ep); | |
2bd0ea18 | 1496 | /* |
49f693fa | 1497 | * See if the hole before this extent will work. |
2bd0ea18 | 1498 | */ |
49f693fa DC |
1499 | if (off >= lowest + len && off - max >= len) { |
1500 | *first_unused = max; | |
1501 | return 0; | |
2bd0ea18 | 1502 | } |
49f693fa DC |
1503 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
1504 | max = XFS_FILEOFF_MAX(lastaddr, lowest); | |
1505 | } | |
1506 | *first_unused = max; | |
1507 | return 0; | |
1508 | } | |
1509 | ||
1510 | /* | |
e6d77a21 | 1511 | * Returns the file-relative block number of the last block - 1 before |
49f693fa DC |
1512 | * last_block (input value) in the file. |
1513 | * This is not based on i_size, it is based on the extent records. | |
1514 | * Returns 0 for local files, as they do not have extent records. | |
1515 | */ | |
1516 | int /* error */ | |
1517 | xfs_bmap_last_before( | |
1518 | xfs_trans_t *tp, /* transaction pointer */ | |
1519 | xfs_inode_t *ip, /* incore inode */ | |
1520 | xfs_fileoff_t *last_block, /* last block */ | |
1521 | int whichfork) /* data or attr fork */ | |
1522 | { | |
1523 | xfs_fileoff_t bno; /* input file offset */ | |
1524 | int eof; /* hit end of file */ | |
1525 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ | |
1526 | int error; /* error return value */ | |
1527 | xfs_bmbt_irec_t got; /* current extent value */ | |
1528 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
1529 | xfs_extnum_t lastx; /* last extent used */ | |
1530 | xfs_bmbt_irec_t prev; /* previous extent value */ | |
1531 | ||
1532 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && | |
1533 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && | |
1534 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) | |
12b53197 | 1535 | return -EIO; |
49f693fa DC |
1536 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
1537 | *last_block = 0; | |
1538 | return 0; | |
1539 | } | |
1540 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
1541 | if (!(ifp->if_flags & XFS_IFEXTENTS) && | |
1542 | (error = xfs_iread_extents(tp, ip, whichfork))) | |
1543 | return error; | |
1544 | bno = *last_block - 1; | |
1545 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, | |
1546 | &prev); | |
1547 | if (eof || xfs_bmbt_get_startoff(ep) > bno) { | |
1548 | if (prev.br_startoff == NULLFILEOFF) | |
1549 | *last_block = 0; | |
2bd0ea18 | 1550 | else |
49f693fa | 1551 | *last_block = prev.br_startoff + prev.br_blockcount; |
2bd0ea18 | 1552 | } |
49f693fa DC |
1553 | /* |
1554 | * Otherwise *last_block is already the right answer. | |
1555 | */ | |
1556 | return 0; | |
5e656dbb BN |
1557 | } |
1558 | ||
613e6057 | 1559 | int |
49f693fa DC |
1560 | xfs_bmap_last_extent( |
1561 | struct xfs_trans *tp, | |
1562 | struct xfs_inode *ip, | |
1563 | int whichfork, | |
1564 | struct xfs_bmbt_irec *rec, | |
1565 | int *is_empty) | |
56b2de80 | 1566 | { |
49f693fa | 1567 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
56b2de80 | 1568 | int error; |
49f693fa | 1569 | int nextents; |
56b2de80 | 1570 | |
49f693fa DC |
1571 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
1572 | error = xfs_iread_extents(tp, ip, whichfork); | |
1573 | if (error) | |
1574 | return error; | |
1575 | } | |
1576 | ||
1577 | nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); | |
1578 | if (nextents == 0) { | |
1579 | *is_empty = 1; | |
1580 | return 0; | |
1581 | } | |
1582 | ||
1583 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec); | |
1584 | *is_empty = 0; | |
1585 | return 0; | |
1586 | } | |
1587 | ||
1588 | /* | |
1589 | * Check the last inode extent to determine whether this allocation will result | |
1590 | * in blocks being allocated at the end of the file. When we allocate new data | |
1591 | * blocks at the end of the file which do not start at the previous data block, | |
1592 | * we will try to align the new blocks at stripe unit boundaries. | |
1593 | * | |
ff105f75 | 1594 | * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be |
49f693fa DC |
1595 | * at, or past the EOF. |
1596 | */ | |
1597 | STATIC int | |
1598 | xfs_bmap_isaeof( | |
1599 | struct xfs_bmalloca *bma, | |
1600 | int whichfork) | |
1601 | { | |
1602 | struct xfs_bmbt_irec rec; | |
1603 | int is_empty; | |
1604 | int error; | |
1605 | ||
1606 | bma->aeof = 0; | |
1607 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, | |
1608 | &is_empty); | |
ff105f75 | 1609 | if (error) |
49f693fa | 1610 | return error; |
56b2de80 | 1611 | |
ff105f75 DC |
1612 | if (is_empty) { |
1613 | bma->aeof = 1; | |
1614 | return 0; | |
1615 | } | |
1616 | ||
56b2de80 | 1617 | /* |
49f693fa DC |
1618 | * Check if we are allocation or past the last extent, or at least into |
1619 | * the last delayed allocated extent. | |
56b2de80 | 1620 | */ |
49f693fa DC |
1621 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
1622 | (bma->offset >= rec.br_startoff && | |
1623 | isnullstartblock(rec.br_startblock)); | |
1624 | return 0; | |
1625 | } | |
56b2de80 | 1626 | |
49f693fa DC |
1627 | /* |
1628 | * Returns the file-relative block number of the first block past eof in | |
1629 | * the file. This is not based on i_size, it is based on the extent records. | |
1630 | * Returns 0 for local files, as they do not have extent records. | |
1631 | */ | |
1632 | int | |
1633 | xfs_bmap_last_offset( | |
49f693fa DC |
1634 | struct xfs_inode *ip, |
1635 | xfs_fileoff_t *last_block, | |
1636 | int whichfork) | |
1637 | { | |
1638 | struct xfs_bmbt_irec rec; | |
1639 | int is_empty; | |
1640 | int error; | |
1641 | ||
1642 | *last_block = 0; | |
1643 | ||
1644 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) | |
1645 | return 0; | |
1646 | ||
1647 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && | |
1648 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) | |
12b53197 | 1649 | return -EIO; |
49f693fa DC |
1650 | |
1651 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); | |
1652 | if (error || is_empty) | |
1653 | return error; | |
1654 | ||
1655 | *last_block = rec.br_startoff + rec.br_blockcount; | |
1656 | return 0; | |
1657 | } | |
1658 | ||
1659 | /* | |
1660 | * Returns whether the selected fork of the inode has exactly one | |
1661 | * block or not. For the data fork we check this matches di_size, | |
1662 | * implying the file's range is 0..bsize-1. | |
1663 | */ | |
1664 | int /* 1=>1 block, 0=>otherwise */ | |
1665 | xfs_bmap_one_block( | |
1666 | xfs_inode_t *ip, /* incore inode */ | |
1667 | int whichfork) /* data or attr fork */ | |
1668 | { | |
1669 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ | |
1670 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
1671 | int rval; /* return value */ | |
1672 | xfs_bmbt_irec_t s; /* internal version of extent */ | |
1673 | ||
1674 | #ifndef DEBUG | |
1675 | if (whichfork == XFS_DATA_FORK) | |
1676 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; | |
1677 | #endif /* !DEBUG */ | |
1678 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) | |
1679 | return 0; | |
1680 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) | |
1681 | return 0; | |
1682 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
1683 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); | |
1684 | ep = xfs_iext_get_ext(ifp, 0); | |
1685 | xfs_bmbt_get_all(ep, &s); | |
1686 | rval = s.br_startoff == 0 && s.br_blockcount == 1; | |
1687 | if (rval && whichfork == XFS_DATA_FORK) | |
1688 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); | |
1689 | return rval; | |
1690 | } | |
1691 | ||
1692 | /* | |
1693 | * Extent tree manipulation functions used during allocation. | |
1694 | */ | |
1695 | ||
1696 | /* | |
1697 | * Convert a delayed allocation to a real allocation. | |
1698 | */ | |
1699 | STATIC int /* error */ | |
1700 | xfs_bmap_add_extent_delay_real( | |
1277a5e0 DW |
1701 | struct xfs_bmalloca *bma, |
1702 | int whichfork) | |
49f693fa DC |
1703 | { |
1704 | struct xfs_bmbt_irec *new = &bma->got; | |
1705 | int diff; /* temp value */ | |
1706 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ | |
1707 | int error; /* error return value */ | |
1708 | int i; /* temp state */ | |
1709 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
1710 | xfs_fileoff_t new_endoff; /* end offset of new entry */ | |
1711 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ | |
1712 | /* left is 0, right is 1, prev is 2 */ | |
1713 | int rval=0; /* return value (logging flags) */ | |
1714 | int state = 0;/* state bits, accessed thru macros */ | |
1715 | xfs_filblks_t da_new; /* new count del alloc blocks used */ | |
1716 | xfs_filblks_t da_old; /* old count del alloc blocks used */ | |
1717 | xfs_filblks_t temp=0; /* value for da_new calculations */ | |
1718 | xfs_filblks_t temp2=0;/* value for da_new calculations */ | |
1719 | int tmp_rval; /* partial logging flags */ | |
19ebedcf | 1720 | struct xfs_mount *mp; |
1277a5e0 | 1721 | xfs_extnum_t *nextents; |
49f693fa | 1722 | |
65ca3804 | 1723 | mp = bma->ip->i_mount; |
36e8786d | 1724 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
1277a5e0 DW |
1725 | ASSERT(whichfork != XFS_ATTR_FORK); |
1726 | nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents : | |
1727 | &bma->ip->i_d.di_nextents); | |
56b2de80 | 1728 | |
49f693fa DC |
1729 | ASSERT(bma->idx >= 0); |
1730 | ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); | |
1731 | ASSERT(!isnullstartblock(new->br_startblock)); | |
1732 | ASSERT(!bma->cur || | |
1733 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); | |
56b2de80 | 1734 | |
79896434 | 1735 | XFS_STATS_INC(mp, xs_add_exlist); |
49f693fa DC |
1736 | |
1737 | #define LEFT r[0] | |
1738 | #define RIGHT r[1] | |
1739 | #define PREV r[2] | |
56b2de80 | 1740 | |
1277a5e0 DW |
1741 | if (whichfork == XFS_COW_FORK) |
1742 | state |= BMAP_COWFORK; | |
1743 | ||
56b2de80 | 1744 | /* |
49f693fa | 1745 | * Set up a bunch of variables to make the tests simpler. |
56b2de80 | 1746 | */ |
49f693fa DC |
1747 | ep = xfs_iext_get_ext(ifp, bma->idx); |
1748 | xfs_bmbt_get_all(ep, &PREV); | |
1749 | new_endoff = new->br_startoff + new->br_blockcount; | |
1750 | ASSERT(PREV.br_startoff <= new->br_startoff); | |
1751 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); | |
1752 | ||
1753 | da_old = startblockval(PREV.br_startblock); | |
1754 | da_new = 0; | |
1755 | ||
56b2de80 | 1756 | /* |
49f693fa DC |
1757 | * Set flags determining what part of the previous delayed allocation |
1758 | * extent is being replaced by a real allocation. | |
56b2de80 | 1759 | */ |
49f693fa DC |
1760 | if (PREV.br_startoff == new->br_startoff) |
1761 | state |= BMAP_LEFT_FILLING; | |
1762 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) | |
1763 | state |= BMAP_RIGHT_FILLING; | |
1764 | ||
56b2de80 | 1765 | /* |
49f693fa DC |
1766 | * Check and set flags if this segment has a left neighbor. |
1767 | * Don't set contiguous if the combined extent would be too large. | |
56b2de80 | 1768 | */ |
49f693fa DC |
1769 | if (bma->idx > 0) { |
1770 | state |= BMAP_LEFT_VALID; | |
1771 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT); | |
1772 | ||
1773 | if (isnullstartblock(LEFT.br_startblock)) | |
1774 | state |= BMAP_LEFT_DELAY; | |
1775 | } | |
1776 | ||
1777 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && | |
1778 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && | |
1779 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && | |
1780 | LEFT.br_state == new->br_state && | |
1781 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) | |
1782 | state |= BMAP_LEFT_CONTIG; | |
56b2de80 DC |
1783 | |
1784 | /* | |
49f693fa DC |
1785 | * Check and set flags if this segment has a right neighbor. |
1786 | * Don't set contiguous if the combined extent would be too large. | |
1787 | * Also check for all-three-contiguous being too large. | |
56b2de80 | 1788 | */ |
36e8786d | 1789 | if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) { |
49f693fa DC |
1790 | state |= BMAP_RIGHT_VALID; |
1791 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT); | |
56b2de80 | 1792 | |
49f693fa DC |
1793 | if (isnullstartblock(RIGHT.br_startblock)) |
1794 | state |= BMAP_RIGHT_DELAY; | |
1795 | } | |
56b2de80 | 1796 | |
49f693fa DC |
1797 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
1798 | new_endoff == RIGHT.br_startoff && | |
1799 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && | |
1800 | new->br_state == RIGHT.br_state && | |
1801 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && | |
1802 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | | |
1803 | BMAP_RIGHT_FILLING)) != | |
1804 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | | |
1805 | BMAP_RIGHT_FILLING) || | |
1806 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount | |
1807 | <= MAXEXTLEN)) | |
1808 | state |= BMAP_RIGHT_CONTIG; | |
5e656dbb | 1809 | |
49f693fa DC |
1810 | error = 0; |
1811 | /* | |
1812 | * Switch out based on the FILLING and CONTIG state bits. | |
1813 | */ | |
1814 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | | |
1815 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { | |
1816 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | | |
1817 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: | |
1818 | /* | |
1819 | * Filling in all of a previously delayed allocation extent. | |
1820 | * The left and right neighbors are both contiguous with new. | |
1821 | */ | |
1822 | bma->idx--; | |
1823 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
1824 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), | |
1825 | LEFT.br_blockcount + PREV.br_blockcount + | |
1826 | RIGHT.br_blockcount); | |
1827 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
a2ceac1f | 1828 | |
49f693fa | 1829 | xfs_iext_remove(bma->ip, bma->idx + 1, 2, state); |
1277a5e0 | 1830 | (*nextents)--; |
49f693fa DC |
1831 | if (bma->cur == NULL) |
1832 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
1833 | else { | |
1834 | rval = XFS_ILOG_CORE; | |
1835 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, | |
1836 | RIGHT.br_startblock, | |
1837 | RIGHT.br_blockcount, &i); | |
1838 | if (error) | |
1839 | goto done; | |
19ebedcf | 1840 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
1841 | error = xfs_btree_delete(bma->cur, &i); |
1842 | if (error) | |
1843 | goto done; | |
19ebedcf | 1844 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
1845 | error = xfs_btree_decrement(bma->cur, 0, &i); |
1846 | if (error) | |
1847 | goto done; | |
19ebedcf | 1848 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
1849 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
1850 | LEFT.br_startblock, | |
1851 | LEFT.br_blockcount + | |
1852 | PREV.br_blockcount + | |
1853 | RIGHT.br_blockcount, LEFT.br_state); | |
1854 | if (error) | |
1855 | goto done; | |
5e656dbb | 1856 | } |
49f693fa DC |
1857 | break; |
1858 | ||
1859 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: | |
1860 | /* | |
1861 | * Filling in all of a previously delayed allocation extent. | |
1862 | * The left neighbor is contiguous, the right is not. | |
1863 | */ | |
1864 | bma->idx--; | |
1865 | ||
1866 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
1867 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), | |
1868 | LEFT.br_blockcount + PREV.br_blockcount); | |
1869 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
1870 | ||
1871 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); | |
1872 | if (bma->cur == NULL) | |
1873 | rval = XFS_ILOG_DEXT; | |
1874 | else { | |
1875 | rval = 0; | |
1876 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, | |
1877 | LEFT.br_startblock, LEFT.br_blockcount, | |
1878 | &i); | |
1879 | if (error) | |
1880 | goto done; | |
19ebedcf | 1881 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
1882 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
1883 | LEFT.br_startblock, | |
1884 | LEFT.br_blockcount + | |
1885 | PREV.br_blockcount, LEFT.br_state); | |
1886 | if (error) | |
1887 | goto done; | |
1888 | } | |
1889 | break; | |
1890 | ||
1891 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: | |
1892 | /* | |
1893 | * Filling in all of a previously delayed allocation extent. | |
1894 | * The right neighbor is contiguous, the left is not. | |
1895 | */ | |
1896 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
1897 | xfs_bmbt_set_startblock(ep, new->br_startblock); | |
1898 | xfs_bmbt_set_blockcount(ep, | |
1899 | PREV.br_blockcount + RIGHT.br_blockcount); | |
1900 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
1901 | ||
1902 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); | |
1903 | if (bma->cur == NULL) | |
1904 | rval = XFS_ILOG_DEXT; | |
1905 | else { | |
1906 | rval = 0; | |
1907 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, | |
1908 | RIGHT.br_startblock, | |
1909 | RIGHT.br_blockcount, &i); | |
1910 | if (error) | |
1911 | goto done; | |
19ebedcf | 1912 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
1913 | error = xfs_bmbt_update(bma->cur, PREV.br_startoff, |
1914 | new->br_startblock, | |
1915 | PREV.br_blockcount + | |
1916 | RIGHT.br_blockcount, PREV.br_state); | |
1917 | if (error) | |
1918 | goto done; | |
1919 | } | |
1920 | break; | |
1921 | ||
1922 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: | |
1923 | /* | |
1924 | * Filling in all of a previously delayed allocation extent. | |
1925 | * Neither the left nor right neighbors are contiguous with | |
1926 | * the new one. | |
1927 | */ | |
1928 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
1929 | xfs_bmbt_set_startblock(ep, new->br_startblock); | |
1930 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
5e656dbb | 1931 | |
1277a5e0 | 1932 | (*nextents)++; |
49f693fa DC |
1933 | if (bma->cur == NULL) |
1934 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
1935 | else { | |
1936 | rval = XFS_ILOG_CORE; | |
1937 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, | |
1938 | new->br_startblock, new->br_blockcount, | |
1939 | &i); | |
1940 | if (error) | |
1941 | goto done; | |
19ebedcf | 1942 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
49f693fa DC |
1943 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
1944 | error = xfs_btree_insert(bma->cur, &i); | |
1945 | if (error) | |
1946 | goto done; | |
19ebedcf | 1947 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
1948 | } |
1949 | break; | |
5e656dbb | 1950 | |
49f693fa DC |
1951 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
1952 | /* | |
1953 | * Filling in the first part of a previous delayed allocation. | |
1954 | * The left neighbor is contiguous. | |
1955 | */ | |
1956 | trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_); | |
1957 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1), | |
1958 | LEFT.br_blockcount + new->br_blockcount); | |
1959 | xfs_bmbt_set_startoff(ep, | |
1960 | PREV.br_startoff + new->br_blockcount); | |
1961 | trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_); | |
a2ceac1f | 1962 | |
49f693fa DC |
1963 | temp = PREV.br_blockcount - new->br_blockcount; |
1964 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
1965 | xfs_bmbt_set_blockcount(ep, temp); | |
1966 | if (bma->cur == NULL) | |
1967 | rval = XFS_ILOG_DEXT; | |
1968 | else { | |
1969 | rval = 0; | |
1970 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, | |
1971 | LEFT.br_startblock, LEFT.br_blockcount, | |
1972 | &i); | |
1973 | if (error) | |
1974 | goto done; | |
19ebedcf | 1975 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
1976 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
1977 | LEFT.br_startblock, | |
1978 | LEFT.br_blockcount + | |
1979 | new->br_blockcount, | |
1980 | LEFT.br_state); | |
1981 | if (error) | |
1982 | goto done; | |
2bd0ea18 | 1983 | } |
49f693fa DC |
1984 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
1985 | startblockval(PREV.br_startblock)); | |
1986 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); | |
1987 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
1988 | ||
1989 | bma->idx--; | |
1990 | break; | |
1991 | ||
1992 | case BMAP_LEFT_FILLING: | |
2bd0ea18 | 1993 | /* |
49f693fa DC |
1994 | * Filling in the first part of a previous delayed allocation. |
1995 | * The left neighbor is not contiguous. | |
5000d01d | 1996 | */ |
49f693fa DC |
1997 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
1998 | xfs_bmbt_set_startoff(ep, new_endoff); | |
1999 | temp = PREV.br_blockcount - new->br_blockcount; | |
2000 | xfs_bmbt_set_blockcount(ep, temp); | |
2001 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); | |
1277a5e0 | 2002 | (*nextents)++; |
49f693fa DC |
2003 | if (bma->cur == NULL) |
2004 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2005 | else { | |
2006 | rval = XFS_ILOG_CORE; | |
2007 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, | |
2008 | new->br_startblock, new->br_blockcount, | |
2009 | &i); | |
2010 | if (error) | |
2011 | goto done; | |
19ebedcf | 2012 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
49f693fa DC |
2013 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
2014 | error = xfs_btree_insert(bma->cur, &i); | |
2015 | if (error) | |
2016 | goto done; | |
19ebedcf | 2017 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2018 | } |
2019 | ||
36e8786d | 2020 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
49f693fa | 2021 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
f33cea1a | 2022 | bma->firstblock, bma->dfops, |
36e8786d | 2023 | &bma->cur, 1, &tmp_rval, whichfork); |
49f693fa DC |
2024 | rval |= tmp_rval; |
2025 | if (error) | |
2026 | goto done; | |
2027 | } | |
2028 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), | |
2029 | startblockval(PREV.br_startblock) - | |
2030 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); | |
2031 | ep = xfs_iext_get_ext(ifp, bma->idx + 1); | |
2032 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); | |
2033 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); | |
2034 | break; | |
2035 | ||
2036 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: | |
5e656dbb | 2037 | /* |
49f693fa DC |
2038 | * Filling in the last part of a previous delayed allocation. |
2039 | * The right neighbor is contiguous with the new allocation. | |
5e656dbb | 2040 | */ |
49f693fa DC |
2041 | temp = PREV.br_blockcount - new->br_blockcount; |
2042 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_); | |
2043 | xfs_bmbt_set_blockcount(ep, temp); | |
2044 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1), | |
2045 | new->br_startoff, new->br_startblock, | |
2046 | new->br_blockcount + RIGHT.br_blockcount, | |
2047 | RIGHT.br_state); | |
2048 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); | |
2049 | if (bma->cur == NULL) | |
2050 | rval = XFS_ILOG_DEXT; | |
2051 | else { | |
2052 | rval = 0; | |
2053 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, | |
2054 | RIGHT.br_startblock, | |
2055 | RIGHT.br_blockcount, &i); | |
2056 | if (error) | |
2057 | goto done; | |
19ebedcf | 2058 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2059 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
2060 | new->br_startblock, | |
2061 | new->br_blockcount + | |
2062 | RIGHT.br_blockcount, | |
2063 | RIGHT.br_state); | |
2064 | if (error) | |
2065 | goto done; | |
2066 | } | |
2067 | ||
2068 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), | |
2069 | startblockval(PREV.br_startblock)); | |
2070 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
2071 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); | |
2072 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
2073 | ||
2074 | bma->idx++; | |
2075 | break; | |
2076 | ||
2077 | case BMAP_RIGHT_FILLING: | |
a2ceac1f | 2078 | /* |
49f693fa DC |
2079 | * Filling in the last part of a previous delayed allocation. |
2080 | * The right neighbor is not contiguous. | |
a2ceac1f | 2081 | */ |
49f693fa DC |
2082 | temp = PREV.br_blockcount - new->br_blockcount; |
2083 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
2084 | xfs_bmbt_set_blockcount(ep, temp); | |
2085 | xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state); | |
1277a5e0 | 2086 | (*nextents)++; |
49f693fa DC |
2087 | if (bma->cur == NULL) |
2088 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2089 | else { | |
2090 | rval = XFS_ILOG_CORE; | |
2091 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, | |
2092 | new->br_startblock, new->br_blockcount, | |
2093 | &i); | |
2094 | if (error) | |
2095 | goto done; | |
19ebedcf | 2096 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
49f693fa DC |
2097 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
2098 | error = xfs_btree_insert(bma->cur, &i); | |
2099 | if (error) | |
2100 | goto done; | |
19ebedcf | 2101 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa | 2102 | } |
a2ceac1f | 2103 | |
36e8786d | 2104 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
49f693fa | 2105 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
f33cea1a | 2106 | bma->firstblock, bma->dfops, &bma->cur, 1, |
36e8786d | 2107 | &tmp_rval, whichfork); |
49f693fa DC |
2108 | rval |= tmp_rval; |
2109 | if (error) | |
2110 | goto done; | |
2111 | } | |
2112 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), | |
2113 | startblockval(PREV.br_startblock) - | |
2114 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); | |
2115 | ep = xfs_iext_get_ext(ifp, bma->idx); | |
2116 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); | |
2117 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
2118 | ||
2119 | bma->idx++; | |
2120 | break; | |
2121 | ||
2122 | case 0: | |
5e656dbb | 2123 | /* |
49f693fa DC |
2124 | * Filling in the middle part of a previous delayed allocation. |
2125 | * Contiguity is impossible here. | |
2126 | * This case is avoided almost all the time. | |
2127 | * | |
2128 | * We start with a delayed allocation: | |
2129 | * | |
2130 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ | |
2131 | * PREV @ idx | |
2132 | * | |
2133 | * and we are allocating: | |
2134 | * +rrrrrrrrrrrrrrrrr+ | |
2135 | * new | |
2136 | * | |
2137 | * and we set it up for insertion as: | |
2138 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ | |
2139 | * new | |
2140 | * PREV @ idx LEFT RIGHT | |
2141 | * inserted at idx + 1 | |
5e656dbb | 2142 | */ |
49f693fa DC |
2143 | temp = new->br_startoff - PREV.br_startoff; |
2144 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; | |
2145 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_); | |
2146 | xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */ | |
2147 | LEFT = *new; | |
2148 | RIGHT.br_state = PREV.br_state; | |
2149 | RIGHT.br_startblock = nullstartblock( | |
2150 | (int)xfs_bmap_worst_indlen(bma->ip, temp2)); | |
2151 | RIGHT.br_startoff = new_endoff; | |
2152 | RIGHT.br_blockcount = temp2; | |
2153 | /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */ | |
2154 | xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state); | |
1277a5e0 | 2155 | (*nextents)++; |
49f693fa DC |
2156 | if (bma->cur == NULL) |
2157 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2158 | else { | |
2159 | rval = XFS_ILOG_CORE; | |
2160 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, | |
2161 | new->br_startblock, new->br_blockcount, | |
2162 | &i); | |
2163 | if (error) | |
2164 | goto done; | |
19ebedcf | 2165 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
49f693fa DC |
2166 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
2167 | error = xfs_btree_insert(bma->cur, &i); | |
2168 | if (error) | |
2169 | goto done; | |
19ebedcf | 2170 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa | 2171 | } |
5e656dbb | 2172 | |
36e8786d | 2173 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
49f693fa | 2174 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
f33cea1a | 2175 | bma->firstblock, bma->dfops, &bma->cur, |
36e8786d | 2176 | 1, &tmp_rval, whichfork); |
49f693fa DC |
2177 | rval |= tmp_rval; |
2178 | if (error) | |
2179 | goto done; | |
2180 | } | |
2181 | temp = xfs_bmap_worst_indlen(bma->ip, temp); | |
2182 | temp2 = xfs_bmap_worst_indlen(bma->ip, temp2); | |
2183 | diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - | |
2184 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); | |
2185 | if (diff > 0) { | |
19ebedcf DC |
2186 | error = xfs_mod_fdblocks(bma->ip->i_mount, |
2187 | -((int64_t)diff), false); | |
49f693fa DC |
2188 | ASSERT(!error); |
2189 | if (error) | |
2190 | goto done; | |
2191 | } | |
2bd0ea18 | 2192 | |
49f693fa DC |
2193 | ep = xfs_iext_get_ext(ifp, bma->idx); |
2194 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | |
2195 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
2196 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_); | |
2197 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2), | |
2198 | nullstartblock((int)temp2)); | |
2199 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_); | |
2bd0ea18 | 2200 | |
49f693fa DC |
2201 | bma->idx++; |
2202 | da_new = temp + temp2; | |
2203 | break; | |
2204 | ||
2205 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | |
2206 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | |
2207 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: | |
2208 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: | |
2209 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | |
2210 | case BMAP_LEFT_CONTIG: | |
2211 | case BMAP_RIGHT_CONTIG: | |
2212 | /* | |
2213 | * These cases are all impossible. | |
2214 | */ | |
2215 | ASSERT(0); | |
2216 | } | |
2217 | ||
d7f80320 DW |
2218 | /* add reverse mapping */ |
2219 | error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new); | |
2220 | if (error) | |
2221 | goto done; | |
2222 | ||
49f693fa | 2223 | /* convert to a btree if necessary */ |
36e8786d | 2224 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
49f693fa DC |
2225 | int tmp_logflags; /* partial log flag return val */ |
2226 | ||
2227 | ASSERT(bma->cur == NULL); | |
2228 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, | |
f33cea1a | 2229 | bma->firstblock, bma->dfops, &bma->cur, |
36e8786d | 2230 | da_old > 0, &tmp_logflags, whichfork); |
49f693fa DC |
2231 | bma->logflags |= tmp_logflags; |
2232 | if (error) | |
2233 | goto done; | |
2234 | } | |
2235 | ||
2236 | /* adjust for changes in reserved delayed indirect blocks */ | |
2237 | if (da_old || da_new) { | |
2238 | temp = da_new; | |
2239 | if (bma->cur) | |
2240 | temp += bma->cur->bc_private.b.allocated; | |
2241 | ASSERT(temp <= da_old); | |
2242 | if (temp < da_old) | |
19ebedcf DC |
2243 | xfs_mod_fdblocks(bma->ip->i_mount, |
2244 | (int64_t)(da_old - temp), false); | |
49f693fa DC |
2245 | } |
2246 | ||
2247 | /* clear out the allocated field, done with it now in any case. */ | |
2248 | if (bma->cur) | |
2249 | bma->cur->bc_private.b.allocated = 0; | |
2250 | ||
36e8786d | 2251 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
49f693fa | 2252 | done: |
1277a5e0 DW |
2253 | if (whichfork != XFS_COW_FORK) |
2254 | bma->logflags |= rval; | |
49f693fa DC |
2255 | return error; |
2256 | #undef LEFT | |
2257 | #undef RIGHT | |
2258 | #undef PREV | |
2bd0ea18 NS |
2259 | } |
2260 | ||
2261 | /* | |
49f693fa | 2262 | * Convert an unwritten allocation to a real allocation or vice versa. |
2bd0ea18 NS |
2263 | */ |
2264 | STATIC int /* error */ | |
49f693fa DC |
2265 | xfs_bmap_add_extent_unwritten_real( |
2266 | struct xfs_trans *tp, | |
2bd0ea18 | 2267 | xfs_inode_t *ip, /* incore inode pointer */ |
49f693fa DC |
2268 | xfs_extnum_t *idx, /* extent number to update/insert */ |
2269 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ | |
2270 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ | |
2271 | xfs_fsblock_t *first, /* pointer to firstblock variable */ | |
f33cea1a | 2272 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
49f693fa | 2273 | int *logflagsp) /* inode logging flags */ |
2bd0ea18 | 2274 | { |
49f693fa DC |
2275 | xfs_btree_cur_t *cur; /* btree cursor */ |
2276 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ | |
2bd0ea18 | 2277 | int error; /* error return value */ |
2bd0ea18 NS |
2278 | int i; /* temp state */ |
2279 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
49f693fa DC |
2280 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
2281 | xfs_exntst_t newext; /* new extent state */ | |
2282 | xfs_exntst_t oldext; /* old extent state */ | |
2283 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ | |
2284 | /* left is 0, right is 1, prev is 2 */ | |
2285 | int rval=0; /* return value (logging flags) */ | |
2286 | int state = 0;/* state bits, accessed thru macros */ | |
19ebedcf | 2287 | struct xfs_mount *mp = tp->t_mountp; |
5000d01d | 2288 | |
49f693fa | 2289 | *logflagsp = 0; |
56b2de80 | 2290 | |
49f693fa DC |
2291 | cur = *curp; |
2292 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); | |
56b2de80 | 2293 | |
49f693fa DC |
2294 | ASSERT(*idx >= 0); |
2295 | ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); | |
2296 | ASSERT(!isnullstartblock(new->br_startblock)); | |
2297 | ||
79896434 | 2298 | XFS_STATS_INC(mp, xs_add_exlist); |
49f693fa DC |
2299 | |
2300 | #define LEFT r[0] | |
2301 | #define RIGHT r[1] | |
2302 | #define PREV r[2] | |
2303 | ||
2304 | /* | |
2305 | * Set up a bunch of variables to make the tests simpler. | |
2306 | */ | |
2bd0ea18 | 2307 | error = 0; |
49f693fa DC |
2308 | ep = xfs_iext_get_ext(ifp, *idx); |
2309 | xfs_bmbt_get_all(ep, &PREV); | |
2310 | newext = new->br_state; | |
2311 | oldext = (newext == XFS_EXT_UNWRITTEN) ? | |
2312 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; | |
2313 | ASSERT(PREV.br_state == oldext); | |
2314 | new_endoff = new->br_startoff + new->br_blockcount; | |
2315 | ASSERT(PREV.br_startoff <= new->br_startoff); | |
2316 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); | |
2317 | ||
2bd0ea18 | 2318 | /* |
49f693fa DC |
2319 | * Set flags determining what part of the previous oldext allocation |
2320 | * extent is being replaced by a newext allocation. | |
2bd0ea18 | 2321 | */ |
49f693fa DC |
2322 | if (PREV.br_startoff == new->br_startoff) |
2323 | state |= BMAP_LEFT_FILLING; | |
2324 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) | |
2325 | state |= BMAP_RIGHT_FILLING; | |
2bd0ea18 | 2326 | |
49f693fa DC |
2327 | /* |
2328 | * Check and set flags if this segment has a left neighbor. | |
2329 | * Don't set contiguous if the combined extent would be too large. | |
2330 | */ | |
2331 | if (*idx > 0) { | |
2332 | state |= BMAP_LEFT_VALID; | |
2333 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); | |
2334 | ||
2335 | if (isnullstartblock(LEFT.br_startblock)) | |
2336 | state |= BMAP_LEFT_DELAY; | |
2bd0ea18 | 2337 | } |
49f693fa DC |
2338 | |
2339 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && | |
2340 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && | |
2341 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && | |
2342 | LEFT.br_state == newext && | |
2343 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) | |
2344 | state |= BMAP_LEFT_CONTIG; | |
2345 | ||
2bd0ea18 | 2346 | /* |
49f693fa DC |
2347 | * Check and set flags if this segment has a right neighbor. |
2348 | * Don't set contiguous if the combined extent would be too large. | |
2349 | * Also check for all-three-contiguous being too large. | |
2bd0ea18 | 2350 | */ |
49f693fa DC |
2351 | if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) { |
2352 | state |= BMAP_RIGHT_VALID; | |
2353 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); | |
2354 | if (isnullstartblock(RIGHT.br_startblock)) | |
2355 | state |= BMAP_RIGHT_DELAY; | |
2356 | } | |
a2ceac1f | 2357 | |
49f693fa DC |
2358 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
2359 | new_endoff == RIGHT.br_startoff && | |
2360 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && | |
2361 | newext == RIGHT.br_state && | |
2362 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && | |
2363 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | | |
2364 | BMAP_RIGHT_FILLING)) != | |
2365 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | | |
2366 | BMAP_RIGHT_FILLING) || | |
2367 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount | |
2368 | <= MAXEXTLEN)) | |
2369 | state |= BMAP_RIGHT_CONTIG; | |
2bd0ea18 | 2370 | |
49f693fa DC |
2371 | /* |
2372 | * Switch out based on the FILLING and CONTIG state bits. | |
2373 | */ | |
2374 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | | |
2375 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { | |
2376 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | | |
2377 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: | |
2bd0ea18 | 2378 | /* |
49f693fa DC |
2379 | * Setting all of a previous oldext extent to newext. |
2380 | * The left and right neighbors are both contiguous with new. | |
2bd0ea18 | 2381 | */ |
49f693fa DC |
2382 | --*idx; |
2383 | ||
a2ceac1f | 2384 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
49f693fa DC |
2385 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
2386 | LEFT.br_blockcount + PREV.br_blockcount + | |
2387 | RIGHT.br_blockcount); | |
a2ceac1f | 2388 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
49f693fa DC |
2389 | |
2390 | xfs_iext_remove(ip, *idx + 1, 2, state); | |
2391 | ip->i_d.di_nextents -= 2; | |
2392 | if (cur == NULL) | |
2393 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2394 | else { | |
2395 | rval = XFS_ILOG_CORE; | |
2396 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, | |
2397 | RIGHT.br_startblock, | |
2398 | RIGHT.br_blockcount, &i))) | |
2399 | goto done; | |
19ebedcf | 2400 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2401 | if ((error = xfs_btree_delete(cur, &i))) |
2402 | goto done; | |
19ebedcf | 2403 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2404 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
2405 | goto done; | |
19ebedcf | 2406 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2407 | if ((error = xfs_btree_delete(cur, &i))) |
2408 | goto done; | |
19ebedcf | 2409 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2410 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
2411 | goto done; | |
19ebedcf | 2412 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2413 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
2414 | LEFT.br_startblock, | |
2415 | LEFT.br_blockcount + PREV.br_blockcount + | |
2416 | RIGHT.br_blockcount, LEFT.br_state))) | |
2417 | goto done; | |
2bd0ea18 | 2418 | } |
2bd0ea18 NS |
2419 | break; |
2420 | ||
49f693fa | 2421 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
2bd0ea18 | 2422 | /* |
49f693fa DC |
2423 | * Setting all of a previous oldext extent to newext. |
2424 | * The left neighbor is contiguous, the right is not. | |
2bd0ea18 | 2425 | */ |
49f693fa DC |
2426 | --*idx; |
2427 | ||
a2ceac1f | 2428 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
49f693fa DC |
2429 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
2430 | LEFT.br_blockcount + PREV.br_blockcount); | |
a2ceac1f | 2431 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
49f693fa DC |
2432 | |
2433 | xfs_iext_remove(ip, *idx + 1, 1, state); | |
2434 | ip->i_d.di_nextents--; | |
2435 | if (cur == NULL) | |
2436 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2437 | else { | |
2438 | rval = XFS_ILOG_CORE; | |
2439 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | |
2440 | PREV.br_startblock, PREV.br_blockcount, | |
2441 | &i))) | |
2442 | goto done; | |
19ebedcf | 2443 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2444 | if ((error = xfs_btree_delete(cur, &i))) |
2445 | goto done; | |
19ebedcf | 2446 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2447 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
2448 | goto done; | |
19ebedcf | 2449 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2450 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
2451 | LEFT.br_startblock, | |
2452 | LEFT.br_blockcount + PREV.br_blockcount, | |
2453 | LEFT.br_state))) | |
2454 | goto done; | |
2bd0ea18 | 2455 | } |
2bd0ea18 | 2456 | break; |
5000d01d | 2457 | |
49f693fa | 2458 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
2bd0ea18 | 2459 | /* |
49f693fa DC |
2460 | * Setting all of a previous oldext extent to newext. |
2461 | * The right neighbor is contiguous, the left is not. | |
2bd0ea18 | 2462 | */ |
a2ceac1f | 2463 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
49f693fa DC |
2464 | xfs_bmbt_set_blockcount(ep, |
2465 | PREV.br_blockcount + RIGHT.br_blockcount); | |
2466 | xfs_bmbt_set_state(ep, newext); | |
a2ceac1f | 2467 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
49f693fa DC |
2468 | xfs_iext_remove(ip, *idx + 1, 1, state); |
2469 | ip->i_d.di_nextents--; | |
2470 | if (cur == NULL) | |
2471 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2472 | else { | |
2473 | rval = XFS_ILOG_CORE; | |
2474 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, | |
2475 | RIGHT.br_startblock, | |
2476 | RIGHT.br_blockcount, &i))) | |
2477 | goto done; | |
19ebedcf | 2478 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2479 | if ((error = xfs_btree_delete(cur, &i))) |
2480 | goto done; | |
19ebedcf | 2481 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2482 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
2483 | goto done; | |
19ebedcf | 2484 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2485 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
2486 | new->br_startblock, | |
2487 | new->br_blockcount + RIGHT.br_blockcount, | |
2488 | newext))) | |
2489 | goto done; | |
2490 | } | |
2491 | break; | |
2bd0ea18 | 2492 | |
49f693fa DC |
2493 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
2494 | /* | |
2495 | * Setting all of a previous oldext extent to newext. | |
2496 | * Neither the left nor right neighbors are contiguous with | |
2497 | * the new one. | |
2498 | */ | |
2499 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); | |
2500 | xfs_bmbt_set_state(ep, newext); | |
2501 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
2bd0ea18 | 2502 | |
49f693fa DC |
2503 | if (cur == NULL) |
2504 | rval = XFS_ILOG_DEXT; | |
2505 | else { | |
2506 | rval = 0; | |
2507 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, | |
2508 | new->br_startblock, new->br_blockcount, | |
2509 | &i))) | |
2510 | goto done; | |
19ebedcf | 2511 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2512 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
2513 | new->br_startblock, new->br_blockcount, | |
2514 | newext))) | |
2515 | goto done; | |
2516 | } | |
2517 | break; | |
2bd0ea18 | 2518 | |
49f693fa DC |
2519 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
2520 | /* | |
2521 | * Setting the first part of a previous oldext extent to newext. | |
2522 | * The left neighbor is contiguous. | |
2523 | */ | |
2524 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); | |
2525 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), | |
2526 | LEFT.br_blockcount + new->br_blockcount); | |
2527 | xfs_bmbt_set_startoff(ep, | |
2528 | PREV.br_startoff + new->br_blockcount); | |
2529 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); | |
a2ceac1f | 2530 | |
49f693fa DC |
2531 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
2532 | xfs_bmbt_set_startblock(ep, | |
2533 | new->br_startblock + new->br_blockcount); | |
2534 | xfs_bmbt_set_blockcount(ep, | |
2535 | PREV.br_blockcount - new->br_blockcount); | |
2536 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
b3563c19 | 2537 | |
49f693fa | 2538 | --*idx; |
b3563c19 | 2539 | |
49f693fa DC |
2540 | if (cur == NULL) |
2541 | rval = XFS_ILOG_DEXT; | |
2542 | else { | |
2543 | rval = 0; | |
2544 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | |
2545 | PREV.br_startblock, PREV.br_blockcount, | |
2546 | &i))) | |
2547 | goto done; | |
19ebedcf | 2548 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2549 | if ((error = xfs_bmbt_update(cur, |
2550 | PREV.br_startoff + new->br_blockcount, | |
2551 | PREV.br_startblock + new->br_blockcount, | |
2552 | PREV.br_blockcount - new->br_blockcount, | |
2553 | oldext))) | |
2554 | goto done; | |
2555 | if ((error = xfs_btree_decrement(cur, 0, &i))) | |
2556 | goto done; | |
2557 | error = xfs_bmbt_update(cur, LEFT.br_startoff, | |
2558 | LEFT.br_startblock, | |
2559 | LEFT.br_blockcount + new->br_blockcount, | |
2560 | LEFT.br_state); | |
2561 | if (error) | |
2562 | goto done; | |
2bd0ea18 | 2563 | } |
49f693fa | 2564 | break; |
b3563c19 | 2565 | |
49f693fa DC |
2566 | case BMAP_LEFT_FILLING: |
2567 | /* | |
2568 | * Setting the first part of a previous oldext extent to newext. | |
2569 | * The left neighbor is not contiguous. | |
2570 | */ | |
2571 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); | |
2572 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); | |
2573 | xfs_bmbt_set_startoff(ep, new_endoff); | |
2574 | xfs_bmbt_set_blockcount(ep, | |
2575 | PREV.br_blockcount - new->br_blockcount); | |
2576 | xfs_bmbt_set_startblock(ep, | |
2577 | new->br_startblock + new->br_blockcount); | |
2578 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
2bd0ea18 | 2579 | |
49f693fa DC |
2580 | xfs_iext_insert(ip, *idx, 1, new, state); |
2581 | ip->i_d.di_nextents++; | |
2582 | if (cur == NULL) | |
2583 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2584 | else { | |
2585 | rval = XFS_ILOG_CORE; | |
2586 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | |
2587 | PREV.br_startblock, PREV.br_blockcount, | |
2588 | &i))) | |
2589 | goto done; | |
19ebedcf | 2590 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2591 | if ((error = xfs_bmbt_update(cur, |
2592 | PREV.br_startoff + new->br_blockcount, | |
2593 | PREV.br_startblock + new->br_blockcount, | |
2594 | PREV.br_blockcount - new->br_blockcount, | |
2595 | oldext))) | |
2596 | goto done; | |
2597 | cur->bc_rec.b = *new; | |
2598 | if ((error = xfs_btree_insert(cur, &i))) | |
2599 | goto done; | |
19ebedcf | 2600 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2601 | } |
2602 | break; | |
56b2de80 | 2603 | |
49f693fa DC |
2604 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
2605 | /* | |
2606 | * Setting the last part of a previous oldext extent to newext. | |
2607 | * The right neighbor is contiguous with the new allocation. | |
2608 | */ | |
2609 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); | |
2610 | xfs_bmbt_set_blockcount(ep, | |
2611 | PREV.br_blockcount - new->br_blockcount); | |
2612 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
56b2de80 | 2613 | |
49f693fa | 2614 | ++*idx; |
56b2de80 | 2615 | |
49f693fa DC |
2616 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
2617 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), | |
2618 | new->br_startoff, new->br_startblock, | |
2619 | new->br_blockcount + RIGHT.br_blockcount, newext); | |
2620 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
56b2de80 | 2621 | |
49f693fa DC |
2622 | if (cur == NULL) |
2623 | rval = XFS_ILOG_DEXT; | |
2624 | else { | |
2625 | rval = 0; | |
2626 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | |
2627 | PREV.br_startblock, | |
2628 | PREV.br_blockcount, &i))) | |
2629 | goto done; | |
19ebedcf | 2630 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2631 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
2632 | PREV.br_startblock, | |
2633 | PREV.br_blockcount - new->br_blockcount, | |
2634 | oldext))) | |
2635 | goto done; | |
2636 | if ((error = xfs_btree_increment(cur, 0, &i))) | |
2637 | goto done; | |
2638 | if ((error = xfs_bmbt_update(cur, new->br_startoff, | |
2639 | new->br_startblock, | |
2640 | new->br_blockcount + RIGHT.br_blockcount, | |
2641 | newext))) | |
2642 | goto done; | |
2643 | } | |
2644 | break; | |
ca86e759 | 2645 | |
49f693fa DC |
2646 | case BMAP_RIGHT_FILLING: |
2647 | /* | |
2648 | * Setting the last part of a previous oldext extent to newext. | |
2649 | * The right neighbor is not contiguous. | |
2650 | */ | |
2651 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); | |
2652 | xfs_bmbt_set_blockcount(ep, | |
2653 | PREV.br_blockcount - new->br_blockcount); | |
2654 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
2bd0ea18 | 2655 | |
49f693fa DC |
2656 | ++*idx; |
2657 | xfs_iext_insert(ip, *idx, 1, new, state); | |
2bd0ea18 | 2658 | |
49f693fa DC |
2659 | ip->i_d.di_nextents++; |
2660 | if (cur == NULL) | |
2661 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2662 | else { | |
2663 | rval = XFS_ILOG_CORE; | |
2664 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | |
2665 | PREV.br_startblock, PREV.br_blockcount, | |
2666 | &i))) | |
2667 | goto done; | |
19ebedcf | 2668 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2669 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
2670 | PREV.br_startblock, | |
2671 | PREV.br_blockcount - new->br_blockcount, | |
2672 | oldext))) | |
2673 | goto done; | |
2674 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, | |
2675 | new->br_startblock, new->br_blockcount, | |
2676 | &i))) | |
2677 | goto done; | |
19ebedcf | 2678 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
49f693fa DC |
2679 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
2680 | if ((error = xfs_btree_insert(cur, &i))) | |
2681 | goto done; | |
19ebedcf | 2682 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2683 | } |
2684 | break; | |
2685 | ||
2686 | case 0: | |
2bd0ea18 | 2687 | /* |
49f693fa DC |
2688 | * Setting the middle part of a previous oldext extent to |
2689 | * newext. Contiguity is impossible here. | |
2690 | * One extent becomes three extents. | |
2bd0ea18 | 2691 | */ |
49f693fa DC |
2692 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
2693 | xfs_bmbt_set_blockcount(ep, | |
2694 | new->br_startoff - PREV.br_startoff); | |
2695 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
2696 | ||
2697 | r[0] = *new; | |
2698 | r[1].br_startoff = new_endoff; | |
2699 | r[1].br_blockcount = | |
2700 | PREV.br_startoff + PREV.br_blockcount - new_endoff; | |
2701 | r[1].br_startblock = new->br_startblock + new->br_blockcount; | |
2702 | r[1].br_state = oldext; | |
2703 | ||
2704 | ++*idx; | |
2705 | xfs_iext_insert(ip, *idx, 2, &r[0], state); | |
2706 | ||
2707 | ip->i_d.di_nextents += 2; | |
2708 | if (cur == NULL) | |
2709 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; | |
2710 | else { | |
2711 | rval = XFS_ILOG_CORE; | |
2712 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, | |
2713 | PREV.br_startblock, PREV.br_blockcount, | |
2714 | &i))) | |
2715 | goto done; | |
19ebedcf | 2716 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2717 | /* new right extent - oldext */ |
2718 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, | |
2719 | r[1].br_startblock, r[1].br_blockcount, | |
2720 | r[1].br_state))) | |
2721 | goto done; | |
2722 | /* new left extent - oldext */ | |
2723 | cur->bc_rec.b = PREV; | |
2724 | cur->bc_rec.b.br_blockcount = | |
2725 | new->br_startoff - PREV.br_startoff; | |
2726 | if ((error = xfs_btree_insert(cur, &i))) | |
2727 | goto done; | |
19ebedcf | 2728 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
2729 | /* |
2730 | * Reset the cursor to the position of the new extent | |
2731 | * we are about to insert as we can't trust it after | |
2732 | * the previous insert. | |
2733 | */ | |
2734 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, | |
2735 | new->br_startblock, new->br_blockcount, | |
2736 | &i))) | |
2737 | goto done; | |
19ebedcf | 2738 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
49f693fa DC |
2739 | /* new middle extent - newext */ |
2740 | cur->bc_rec.b.br_state = new->br_state; | |
2741 | if ((error = xfs_btree_insert(cur, &i))) | |
2742 | goto done; | |
19ebedcf | 2743 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
2bd0ea18 | 2744 | } |
49f693fa DC |
2745 | break; |
2746 | ||
2747 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | |
2748 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | |
2749 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: | |
2750 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: | |
2751 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | |
2752 | case BMAP_LEFT_CONTIG: | |
2753 | case BMAP_RIGHT_CONTIG: | |
5000d01d | 2754 | /* |
49f693fa | 2755 | * These cases are all impossible. |
2bd0ea18 | 2756 | */ |
49f693fa DC |
2757 | ASSERT(0); |
2758 | } | |
2759 | ||
d7f80320 DW |
2760 | /* update reverse mappings */ |
2761 | error = xfs_rmap_convert_extent(mp, dfops, ip, XFS_DATA_FORK, new); | |
2762 | if (error) | |
2763 | goto done; | |
2764 | ||
49f693fa DC |
2765 | /* convert to a btree if necessary */ |
2766 | if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) { | |
2767 | int tmp_logflags; /* partial log flag return val */ | |
2768 | ||
2769 | ASSERT(cur == NULL); | |
f33cea1a | 2770 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur, |
49f693fa DC |
2771 | 0, &tmp_logflags, XFS_DATA_FORK); |
2772 | *logflagsp |= tmp_logflags; | |
2773 | if (error) | |
2774 | goto done; | |
ca86e759 | 2775 | } |
49f693fa DC |
2776 | |
2777 | /* clear out the allocated field, done with it now in any case. */ | |
2778 | if (cur) { | |
2779 | cur->bc_private.b.allocated = 0; | |
2780 | *curp = cur; | |
2781 | } | |
2782 | ||
2783 | xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK); | |
2bd0ea18 | 2784 | done: |
49f693fa | 2785 | *logflagsp |= rval; |
2bd0ea18 | 2786 | return error; |
49f693fa DC |
2787 | #undef LEFT |
2788 | #undef RIGHT | |
2789 | #undef PREV | |
2bd0ea18 NS |
2790 | } |
2791 | ||
5e656dbb | 2792 | /* |
49f693fa | 2793 | * Convert a hole to a delayed allocation. |
5e656dbb | 2794 | */ |
49f693fa DC |
2795 | STATIC void |
2796 | xfs_bmap_add_extent_hole_delay( | |
2797 | xfs_inode_t *ip, /* incore inode pointer */ | |
cc66acaf | 2798 | int whichfork, |
49f693fa DC |
2799 | xfs_extnum_t *idx, /* extent number to update/insert */ |
2800 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ | |
2bd0ea18 | 2801 | { |
49f693fa DC |
2802 | xfs_ifork_t *ifp; /* inode fork pointer */ |
2803 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ | |
2804 | xfs_filblks_t newlen=0; /* new indirect size */ | |
2805 | xfs_filblks_t oldlen=0; /* old indirect size */ | |
2806 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ | |
2807 | int state; /* state bits, accessed thru macros */ | |
2808 | xfs_filblks_t temp=0; /* temp for indirect calculations */ | |
2809 | ||
cc66acaf | 2810 | ifp = XFS_IFORK_PTR(ip, whichfork); |
49f693fa | 2811 | state = 0; |
cc66acaf DW |
2812 | if (whichfork == XFS_COW_FORK) |
2813 | state |= BMAP_COWFORK; | |
49f693fa | 2814 | ASSERT(isnullstartblock(new->br_startblock)); |
2bd0ea18 | 2815 | |
062998e3 | 2816 | /* |
49f693fa | 2817 | * Check and set flags if this segment has a left neighbor |
062998e3 | 2818 | */ |
49f693fa DC |
2819 | if (*idx > 0) { |
2820 | state |= BMAP_LEFT_VALID; | |
2821 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); | |
5e656dbb | 2822 | |
49f693fa DC |
2823 | if (isnullstartblock(left.br_startblock)) |
2824 | state |= BMAP_LEFT_DELAY; | |
5e656dbb | 2825 | } |
49f693fa DC |
2826 | |
2827 | /* | |
2828 | * Check and set flags if the current (right) segment exists. | |
2829 | * If it doesn't exist, we're converting the hole at end-of-file. | |
2830 | */ | |
cc66acaf | 2831 | if (*idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
49f693fa DC |
2832 | state |= BMAP_RIGHT_VALID; |
2833 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); | |
2834 | ||
2835 | if (isnullstartblock(right.br_startblock)) | |
2836 | state |= BMAP_RIGHT_DELAY; | |
2837 | } | |
2838 | ||
2839 | /* | |
2840 | * Set contiguity flags on the left and right neighbors. | |
2841 | * Don't let extents get too large, even if the pieces are contiguous. | |
2842 | */ | |
2843 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && | |
2844 | left.br_startoff + left.br_blockcount == new->br_startoff && | |
2845 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) | |
2846 | state |= BMAP_LEFT_CONTIG; | |
2847 | ||
2848 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && | |
2849 | new->br_startoff + new->br_blockcount == right.br_startoff && | |
2850 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && | |
2851 | (!(state & BMAP_LEFT_CONTIG) || | |
2852 | (left.br_blockcount + new->br_blockcount + | |
2853 | right.br_blockcount <= MAXEXTLEN))) | |
2854 | state |= BMAP_RIGHT_CONTIG; | |
2855 | ||
2856 | /* | |
2857 | * Switch out based on the contiguity flags. | |
2858 | */ | |
2859 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { | |
2860 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | |
2861 | /* | |
2862 | * New allocation is contiguous with delayed allocations | |
2863 | * on the left and on the right. | |
2864 | * Merge all three into a single extent record. | |
2865 | */ | |
2866 | --*idx; | |
2867 | temp = left.br_blockcount + new->br_blockcount + | |
2868 | right.br_blockcount; | |
2869 | ||
2870 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); | |
2871 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); | |
2872 | oldlen = startblockval(left.br_startblock) + | |
2873 | startblockval(new->br_startblock) + | |
2874 | startblockval(right.br_startblock); | |
2875 | newlen = xfs_bmap_worst_indlen(ip, temp); | |
2876 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), | |
2877 | nullstartblock((int)newlen)); | |
2878 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
2879 | ||
2880 | xfs_iext_remove(ip, *idx + 1, 1, state); | |
2881 | break; | |
2882 | ||
2883 | case BMAP_LEFT_CONTIG: | |
2884 | /* | |
2885 | * New allocation is contiguous with a delayed allocation | |
2886 | * on the left. | |
2887 | * Merge the new allocation with the left neighbor. | |
2888 | */ | |
2889 | --*idx; | |
2890 | temp = left.br_blockcount + new->br_blockcount; | |
2891 | ||
2892 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); | |
2893 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); | |
2894 | oldlen = startblockval(left.br_startblock) + | |
2895 | startblockval(new->br_startblock); | |
2896 | newlen = xfs_bmap_worst_indlen(ip, temp); | |
2897 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), | |
2898 | nullstartblock((int)newlen)); | |
2899 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
2900 | break; | |
2901 | ||
2902 | case BMAP_RIGHT_CONTIG: | |
2903 | /* | |
2904 | * New allocation is contiguous with a delayed allocation | |
2905 | * on the right. | |
2906 | * Merge the new allocation with the right neighbor. | |
2907 | */ | |
2908 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); | |
2909 | temp = new->br_blockcount + right.br_blockcount; | |
2910 | oldlen = startblockval(new->br_startblock) + | |
2911 | startblockval(right.br_startblock); | |
2912 | newlen = xfs_bmap_worst_indlen(ip, temp); | |
2913 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), | |
2914 | new->br_startoff, | |
2915 | nullstartblock((int)newlen), temp, right.br_state); | |
2916 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
2917 | break; | |
2918 | ||
2919 | case 0: | |
2920 | /* | |
2921 | * New allocation is not contiguous with another | |
2922 | * delayed allocation. | |
2923 | * Insert a new entry. | |
2924 | */ | |
2925 | oldlen = newlen = 0; | |
2926 | xfs_iext_insert(ip, *idx, 1, new, state); | |
2927 | break; | |
2928 | } | |
2929 | if (oldlen != newlen) { | |
2930 | ASSERT(oldlen > newlen); | |
19ebedcf DC |
2931 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
2932 | false); | |
49f693fa DC |
2933 | /* |
2934 | * Nothing to do for disk quota accounting here. | |
2935 | */ | |
2bd0ea18 | 2936 | } |
2bd0ea18 NS |
2937 | } |
2938 | ||
2939 | /* | |
49f693fa | 2940 | * Convert a hole to a real allocation. |
2bd0ea18 | 2941 | */ |
49f693fa DC |
2942 | STATIC int /* error */ |
2943 | xfs_bmap_add_extent_hole_real( | |
2944 | struct xfs_bmalloca *bma, | |
2945 | int whichfork) | |
5000d01d | 2946 | { |
49f693fa DC |
2947 | struct xfs_bmbt_irec *new = &bma->got; |
2948 | int error; /* error return value */ | |
2949 | int i; /* temp state */ | |
2950 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
2951 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ | |
2952 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ | |
2953 | int rval=0; /* return value (logging flags) */ | |
2954 | int state; /* state bits, accessed thru macros */ | |
19ebedcf | 2955 | struct xfs_mount *mp; |
2bd0ea18 | 2956 | |
65ca3804 | 2957 | mp = bma->ip->i_mount; |
49f693fa | 2958 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
2bd0ea18 | 2959 | |
49f693fa DC |
2960 | ASSERT(bma->idx >= 0); |
2961 | ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); | |
2962 | ASSERT(!isnullstartblock(new->br_startblock)); | |
2963 | ASSERT(!bma->cur || | |
2964 | !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); | |
cb8a004a | 2965 | ASSERT(whichfork != XFS_COW_FORK); |
5e656dbb | 2966 | |
79896434 | 2967 | XFS_STATS_INC(mp, xs_add_exlist); |
49f693fa DC |
2968 | |
2969 | state = 0; | |
2970 | if (whichfork == XFS_ATTR_FORK) | |
2971 | state |= BMAP_ATTRFORK; | |
2972 | ||
2973 | /* | |
2974 | * Check and set flags if this segment has a left neighbor. | |
2975 | */ | |
2976 | if (bma->idx > 0) { | |
2977 | state |= BMAP_LEFT_VALID; | |
2978 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left); | |
2979 | if (isnullstartblock(left.br_startblock)) | |
2980 | state |= BMAP_LEFT_DELAY; | |
5e656dbb | 2981 | } |
2bd0ea18 | 2982 | |
49f693fa DC |
2983 | /* |
2984 | * Check and set flags if this segment has a current value. | |
2985 | * Not true if we're inserting into the "hole" at eof. | |
2986 | */ | |
2987 | if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { | |
2988 | state |= BMAP_RIGHT_VALID; | |
2989 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right); | |
2990 | if (isnullstartblock(right.br_startblock)) | |
2991 | state |= BMAP_RIGHT_DELAY; | |
2bd0ea18 | 2992 | } |
2bd0ea18 | 2993 | |
49f693fa DC |
2994 | /* |
2995 | * We're inserting a real allocation between "left" and "right". | |
2996 | * Set the contiguity flags. Don't let extents get too large. | |
2997 | */ | |
2998 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && | |
2999 | left.br_startoff + left.br_blockcount == new->br_startoff && | |
3000 | left.br_startblock + left.br_blockcount == new->br_startblock && | |
3001 | left.br_state == new->br_state && | |
3002 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) | |
3003 | state |= BMAP_LEFT_CONTIG; | |
57c9fccb | 3004 | |
49f693fa DC |
3005 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
3006 | new->br_startoff + new->br_blockcount == right.br_startoff && | |
3007 | new->br_startblock + new->br_blockcount == right.br_startblock && | |
3008 | new->br_state == right.br_state && | |
3009 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && | |
3010 | (!(state & BMAP_LEFT_CONTIG) || | |
3011 | left.br_blockcount + new->br_blockcount + | |
3012 | right.br_blockcount <= MAXEXTLEN)) | |
3013 | state |= BMAP_RIGHT_CONTIG; | |
ca86e759 | 3014 | |
49f693fa DC |
3015 | error = 0; |
3016 | /* | |
3017 | * Select which case we're in here, and implement it. | |
3018 | */ | |
3019 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { | |
3020 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: | |
57c9fccb | 3021 | /* |
49f693fa DC |
3022 | * New allocation is contiguous with real allocations on the |
3023 | * left and on the right. | |
3024 | * Merge all three into a single extent record. | |
57c9fccb | 3025 | */ |
49f693fa DC |
3026 | --bma->idx; |
3027 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
3028 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), | |
3029 | left.br_blockcount + new->br_blockcount + | |
3030 | right.br_blockcount); | |
3031 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
56b2de80 | 3032 | |
49f693fa | 3033 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
56b2de80 | 3034 | |
49f693fa DC |
3035 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
3036 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1); | |
3037 | if (bma->cur == NULL) { | |
3038 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); | |
3039 | } else { | |
3040 | rval = XFS_ILOG_CORE; | |
3041 | error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff, | |
3042 | right.br_startblock, right.br_blockcount, | |
3043 | &i); | |
3044 | if (error) | |
3045 | goto done; | |
19ebedcf | 3046 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
3047 | error = xfs_btree_delete(bma->cur, &i); |
3048 | if (error) | |
3049 | goto done; | |
19ebedcf | 3050 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
3051 | error = xfs_btree_decrement(bma->cur, 0, &i); |
3052 | if (error) | |
3053 | goto done; | |
19ebedcf | 3054 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
3055 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
3056 | left.br_startblock, | |
3057 | left.br_blockcount + | |
3058 | new->br_blockcount + | |
3059 | right.br_blockcount, | |
3060 | left.br_state); | |
3061 | if (error) | |
3062 | goto done; | |
3063 | } | |
57c9fccb | 3064 | break; |
49f693fa DC |
3065 | |
3066 | case BMAP_LEFT_CONTIG: | |
3067 | /* | |
3068 | * New allocation is contiguous with a real allocation | |
3069 | * on the left. | |
3070 | * Merge the new allocation with the left neighbor. | |
3071 | */ | |
3072 | --bma->idx; | |
3073 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
3074 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), | |
3075 | left.br_blockcount + new->br_blockcount); | |
3076 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
3077 | ||
3078 | if (bma->cur == NULL) { | |
3079 | rval = xfs_ilog_fext(whichfork); | |
3080 | } else { | |
3081 | rval = 0; | |
3082 | error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff, | |
3083 | left.br_startblock, left.br_blockcount, | |
3084 | &i); | |
3085 | if (error) | |
3086 | goto done; | |
19ebedcf | 3087 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
3088 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
3089 | left.br_startblock, | |
3090 | left.br_blockcount + | |
3091 | new->br_blockcount, | |
3092 | left.br_state); | |
3093 | if (error) | |
3094 | goto done; | |
3095 | } | |
57c9fccb | 3096 | break; |
49f693fa DC |
3097 | |
3098 | case BMAP_RIGHT_CONTIG: | |
3099 | /* | |
3100 | * New allocation is contiguous with a real allocation | |
3101 | * on the right. | |
3102 | * Merge the new allocation with the right neighbor. | |
3103 | */ | |
3104 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); | |
3105 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx), | |
3106 | new->br_startoff, new->br_startblock, | |
3107 | new->br_blockcount + right.br_blockcount, | |
3108 | right.br_state); | |
3109 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); | |
3110 | ||
3111 | if (bma->cur == NULL) { | |
3112 | rval = xfs_ilog_fext(whichfork); | |
3113 | } else { | |
3114 | rval = 0; | |
3115 | error = xfs_bmbt_lookup_eq(bma->cur, | |
3116 | right.br_startoff, | |
3117 | right.br_startblock, | |
3118 | right.br_blockcount, &i); | |
3119 | if (error) | |
3120 | goto done; | |
19ebedcf | 3121 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
3122 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
3123 | new->br_startblock, | |
3124 | new->br_blockcount + | |
3125 | right.br_blockcount, | |
3126 | right.br_state); | |
3127 | if (error) | |
3128 | goto done; | |
3129 | } | |
3130 | break; | |
3131 | ||
3132 | case 0: | |
3133 | /* | |
3134 | * New allocation is not contiguous with another | |
3135 | * real allocation. | |
3136 | * Insert a new entry. | |
3137 | */ | |
3138 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); | |
3139 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, | |
3140 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1); | |
3141 | if (bma->cur == NULL) { | |
3142 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); | |
3143 | } else { | |
3144 | rval = XFS_ILOG_CORE; | |
3145 | error = xfs_bmbt_lookup_eq(bma->cur, | |
3146 | new->br_startoff, | |
3147 | new->br_startblock, | |
3148 | new->br_blockcount, &i); | |
3149 | if (error) | |
3150 | goto done; | |
19ebedcf | 3151 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
49f693fa DC |
3152 | bma->cur->bc_rec.b.br_state = new->br_state; |
3153 | error = xfs_btree_insert(bma->cur, &i); | |
3154 | if (error) | |
3155 | goto done; | |
19ebedcf | 3156 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa | 3157 | } |
57c9fccb | 3158 | break; |
57c9fccb | 3159 | } |
a2ceac1f | 3160 | |
d7f80320 DW |
3161 | /* add reverse mapping */ |
3162 | error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new); | |
3163 | if (error) | |
3164 | goto done; | |
3165 | ||
49f693fa DC |
3166 | /* convert to a btree if necessary */ |
3167 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { | |
3168 | int tmp_logflags; /* partial log flag return val */ | |
3f853c7a | 3169 | |
49f693fa DC |
3170 | ASSERT(bma->cur == NULL); |
3171 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, | |
f33cea1a | 3172 | bma->firstblock, bma->dfops, &bma->cur, |
49f693fa DC |
3173 | 0, &tmp_logflags, whichfork); |
3174 | bma->logflags |= tmp_logflags; | |
3175 | if (error) | |
3176 | goto done; | |
57c9fccb | 3177 | } |
a2ceac1f | 3178 | |
49f693fa DC |
3179 | /* clear out the allocated field, done with it now in any case. */ |
3180 | if (bma->cur) | |
3181 | bma->cur->bc_private.b.allocated = 0; | |
3182 | ||
3183 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); | |
3184 | done: | |
3185 | bma->logflags |= rval; | |
57c9fccb NS |
3186 | return error; |
3187 | } | |
3188 | ||
2bd0ea18 | 3189 | /* |
49f693fa | 3190 | * Functions used in the extent read, allocate and remove paths |
2bd0ea18 | 3191 | */ |
2bd0ea18 | 3192 | |
5000d01d | 3193 | /* |
49f693fa | 3194 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
2bd0ea18 | 3195 | */ |
613e6057 | 3196 | int |
49f693fa DC |
3197 | xfs_bmap_extsize_align( |
3198 | xfs_mount_t *mp, | |
3199 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ | |
3200 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ | |
3201 | xfs_extlen_t extsz, /* align to this extent size */ | |
3202 | int rt, /* is this a realtime inode? */ | |
3203 | int eof, /* is extent at end-of-file? */ | |
3204 | int delay, /* creating delalloc extent? */ | |
3205 | int convert, /* overwriting unwritten extent? */ | |
3206 | xfs_fileoff_t *offp, /* in/out: aligned offset */ | |
3207 | xfs_extlen_t *lenp) /* in/out: aligned length */ | |
2bd0ea18 | 3208 | { |
49f693fa DC |
3209 | xfs_fileoff_t orig_off; /* original offset */ |
3210 | xfs_extlen_t orig_alen; /* original length */ | |
3211 | xfs_fileoff_t orig_end; /* original off+len */ | |
3212 | xfs_fileoff_t nexto; /* next file offset */ | |
3213 | xfs_fileoff_t prevo; /* previous file offset */ | |
3214 | xfs_fileoff_t align_off; /* temp for offset */ | |
3215 | xfs_extlen_t align_alen; /* temp for length */ | |
3216 | xfs_extlen_t temp; /* temp for calculations */ | |
3217 | ||
3218 | if (convert) | |
3219 | return 0; | |
3220 | ||
3221 | orig_off = align_off = *offp; | |
3222 | orig_alen = align_alen = *lenp; | |
3223 | orig_end = orig_off + orig_alen; | |
2bd0ea18 NS |
3224 | |
3225 | /* | |
49f693fa DC |
3226 | * If this request overlaps an existing extent, then don't |
3227 | * attempt to perform any additional alignment. | |
2bd0ea18 | 3228 | */ |
49f693fa DC |
3229 | if (!delay && !eof && |
3230 | (orig_off >= gotp->br_startoff) && | |
3231 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { | |
3232 | return 0; | |
2bd0ea18 | 3233 | } |
57c9fccb | 3234 | |
49f693fa DC |
3235 | /* |
3236 | * If the file offset is unaligned vs. the extent size | |
3237 | * we need to align it. This will be possible unless | |
3238 | * the file was previously written with a kernel that didn't | |
3239 | * perform this alignment, or if a truncate shot us in the | |
3240 | * foot. | |
3241 | */ | |
3242 | temp = do_mod(orig_off, extsz); | |
3243 | if (temp) { | |
3244 | align_alen += temp; | |
3245 | align_off -= temp; | |
3246 | } | |
7cc23f0c DC |
3247 | |
3248 | /* Same adjustment for the end of the requested area. */ | |
3249 | temp = (align_alen % extsz); | |
3250 | if (temp) | |
3251 | align_alen += extsz - temp; | |
3252 | ||
49f693fa | 3253 | /* |
7cc23f0c DC |
3254 | * For large extent hint sizes, the aligned extent might be larger than |
3255 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls | |
3256 | * the length back under MAXEXTLEN. The outer allocation loops handle | |
3257 | * short allocation just fine, so it is safe to do this. We only want to | |
3258 | * do it when we are forced to, though, because it means more allocation | |
3259 | * operations are required. | |
49f693fa | 3260 | */ |
7cc23f0c DC |
3261 | while (align_alen > MAXEXTLEN) |
3262 | align_alen -= extsz; | |
3263 | ASSERT(align_alen <= MAXEXTLEN); | |
3264 | ||
49f693fa DC |
3265 | /* |
3266 | * If the previous block overlaps with this proposed allocation | |
3267 | * then move the start forward without adjusting the length. | |
3268 | */ | |
3269 | if (prevp->br_startoff != NULLFILEOFF) { | |
3270 | if (prevp->br_startblock == HOLESTARTBLOCK) | |
3271 | prevo = prevp->br_startoff; | |
3272 | else | |
3273 | prevo = prevp->br_startoff + prevp->br_blockcount; | |
3274 | } else | |
3275 | prevo = 0; | |
3276 | if (align_off != orig_off && align_off < prevo) | |
3277 | align_off = prevo; | |
3278 | /* | |
3279 | * If the next block overlaps with this proposed allocation | |
3280 | * then move the start back without adjusting the length, | |
3281 | * but not before offset 0. | |
3282 | * This may of course make the start overlap previous block, | |
3283 | * and if we hit the offset 0 limit then the next block | |
3284 | * can still overlap too. | |
3285 | */ | |
3286 | if (!eof && gotp->br_startoff != NULLFILEOFF) { | |
3287 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || | |
3288 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) | |
3289 | nexto = gotp->br_startoff + gotp->br_blockcount; | |
3290 | else | |
3291 | nexto = gotp->br_startoff; | |
3292 | } else | |
3293 | nexto = NULLFILEOFF; | |
3294 | if (!eof && | |
3295 | align_off + align_alen != orig_end && | |
3296 | align_off + align_alen > nexto) | |
3297 | align_off = nexto > align_alen ? nexto - align_alen : 0; | |
3298 | /* | |
3299 | * If we're now overlapping the next or previous extent that | |
3300 | * means we can't fit an extsz piece in this hole. Just move | |
3301 | * the start forward to the first valid spot and set | |
3302 | * the length so we hit the end. | |
3303 | */ | |
3304 | if (align_off != orig_off && align_off < prevo) | |
3305 | align_off = prevo; | |
3306 | if (align_off + align_alen != orig_end && | |
3307 | align_off + align_alen > nexto && | |
3308 | nexto != NULLFILEOFF) { | |
3309 | ASSERT(nexto > prevo); | |
3310 | align_alen = nexto - align_off; | |
57c9fccb | 3311 | } |
2bd0ea18 | 3312 | |
49f693fa DC |
3313 | /* |
3314 | * If realtime, and the result isn't a multiple of the realtime | |
3315 | * extent size we need to remove blocks until it is. | |
3316 | */ | |
3317 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { | |
2bd0ea18 | 3318 | /* |
49f693fa DC |
3319 | * We're not covering the original request, or |
3320 | * we won't be able to once we fix the length. | |
2bd0ea18 | 3321 | */ |
49f693fa DC |
3322 | if (orig_off < align_off || |
3323 | orig_end > align_off + align_alen || | |
3324 | align_alen - temp < orig_alen) | |
12b53197 | 3325 | return -EINVAL; |
49f693fa DC |
3326 | /* |
3327 | * Try to fix it by moving the start up. | |
3328 | */ | |
3329 | if (align_off + temp <= orig_off) { | |
3330 | align_alen -= temp; | |
3331 | align_off += temp; | |
2bd0ea18 | 3332 | } |
49f693fa DC |
3333 | /* |
3334 | * Try to fix it by moving the end in. | |
3335 | */ | |
3336 | else if (align_off + align_alen - temp >= orig_end) | |
3337 | align_alen -= temp; | |
3338 | /* | |
3339 | * Set the start to the minimum then trim the length. | |
3340 | */ | |
3341 | else { | |
3342 | align_alen -= orig_off - align_off; | |
3343 | align_off = orig_off; | |
3344 | align_alen -= align_alen % mp->m_sb.sb_rextsize; | |
3345 | } | |
3346 | /* | |
3347 | * Result doesn't cover the request, fail it. | |
3348 | */ | |
3349 | if (orig_off < align_off || orig_end > align_off + align_alen) | |
12b53197 | 3350 | return -EINVAL; |
49f693fa DC |
3351 | } else { |
3352 | ASSERT(orig_off >= align_off); | |
7cc23f0c DC |
3353 | /* see MAXEXTLEN handling above */ |
3354 | ASSERT(orig_end <= align_off + align_alen || | |
3355 | align_alen + extsz > MAXEXTLEN); | |
2bd0ea18 | 3356 | } |
49f693fa DC |
3357 | |
3358 | #ifdef DEBUG | |
3359 | if (!eof && gotp->br_startoff != NULLFILEOFF) | |
3360 | ASSERT(align_off + align_alen <= gotp->br_startoff); | |
3361 | if (prevp->br_startoff != NULLFILEOFF) | |
3362 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); | |
3363 | #endif | |
3364 | ||
3365 | *lenp = align_alen; | |
3366 | *offp = align_off; | |
2bd0ea18 NS |
3367 | return 0; |
3368 | } | |
3369 | ||
49f693fa DC |
3370 | #define XFS_ALLOC_GAP_UNITS 4 |
3371 | ||
613e6057 | 3372 | void |
49f693fa | 3373 | xfs_bmap_adjacent( |
613e6057 | 3374 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
2bd0ea18 | 3375 | { |
49f693fa DC |
3376 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
3377 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ | |
3378 | xfs_mount_t *mp; /* mount point structure */ | |
3379 | int nullfb; /* true if ap->firstblock isn't set */ | |
3380 | int rt; /* true if inode is realtime */ | |
2bd0ea18 | 3381 | |
49f693fa DC |
3382 | #define ISVALID(x,y) \ |
3383 | (rt ? \ | |
3384 | (x) < mp->m_sb.sb_rblocks : \ | |
3385 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ | |
3386 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ | |
3387 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) | |
3388 | ||
3389 | mp = ap->ip->i_mount; | |
3390 | nullfb = *ap->firstblock == NULLFSBLOCK; | |
1fccd5c8 DC |
3391 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
3392 | xfs_alloc_is_userdata(ap->datatype); | |
49f693fa DC |
3393 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
3394 | /* | |
3395 | * If allocating at eof, and there's a previous real block, | |
3396 | * try to use its last block as our starting point. | |
3397 | */ | |
3398 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && | |
3399 | !isnullstartblock(ap->prev.br_startblock) && | |
3400 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, | |
3401 | ap->prev.br_startblock)) { | |
3402 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; | |
3403 | /* | |
3404 | * Adjust for the gap between prevp and us. | |
3405 | */ | |
3406 | adjust = ap->offset - | |
3407 | (ap->prev.br_startoff + ap->prev.br_blockcount); | |
3408 | if (adjust && | |
3409 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) | |
3410 | ap->blkno += adjust; | |
2bd0ea18 | 3411 | } |
49f693fa DC |
3412 | /* |
3413 | * If not at eof, then compare the two neighbor blocks. | |
3414 | * Figure out whether either one gives us a good starting point, | |
3415 | * and pick the better one. | |
3416 | */ | |
3417 | else if (!ap->eof) { | |
3418 | xfs_fsblock_t gotbno; /* right side block number */ | |
3419 | xfs_fsblock_t gotdiff=0; /* right side difference */ | |
3420 | xfs_fsblock_t prevbno; /* left side block number */ | |
3421 | xfs_fsblock_t prevdiff=0; /* left side difference */ | |
3422 | ||
3423 | /* | |
3424 | * If there's a previous (left) block, select a requested | |
3425 | * start block based on it. | |
3426 | */ | |
3427 | if (ap->prev.br_startoff != NULLFILEOFF && | |
3428 | !isnullstartblock(ap->prev.br_startblock) && | |
3429 | (prevbno = ap->prev.br_startblock + | |
3430 | ap->prev.br_blockcount) && | |
3431 | ISVALID(prevbno, ap->prev.br_startblock)) { | |
3432 | /* | |
3433 | * Calculate gap to end of previous block. | |
3434 | */ | |
3435 | adjust = prevdiff = ap->offset - | |
3436 | (ap->prev.br_startoff + | |
3437 | ap->prev.br_blockcount); | |
3438 | /* | |
3439 | * Figure the startblock based on the previous block's | |
3440 | * end and the gap size. | |
3441 | * Heuristic! | |
3442 | * If the gap is large relative to the piece we're | |
3443 | * allocating, or using it gives us an invalid block | |
3444 | * number, then just use the end of the previous block. | |
3445 | */ | |
3446 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && | |
3447 | ISVALID(prevbno + prevdiff, | |
3448 | ap->prev.br_startblock)) | |
3449 | prevbno += adjust; | |
3450 | else | |
3451 | prevdiff += adjust; | |
3452 | /* | |
3453 | * If the firstblock forbids it, can't use it, | |
3454 | * must use default. | |
3455 | */ | |
3456 | if (!rt && !nullfb && | |
3457 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) | |
3458 | prevbno = NULLFSBLOCK; | |
3459 | } | |
3460 | /* | |
3461 | * No previous block or can't follow it, just default. | |
3462 | */ | |
3463 | else | |
3464 | prevbno = NULLFSBLOCK; | |
3465 | /* | |
3466 | * If there's a following (right) block, select a requested | |
3467 | * start block based on it. | |
3468 | */ | |
3469 | if (!isnullstartblock(ap->got.br_startblock)) { | |
3470 | /* | |
3471 | * Calculate gap to start of next block. | |
3472 | */ | |
3473 | adjust = gotdiff = ap->got.br_startoff - ap->offset; | |
3474 | /* | |
3475 | * Figure the startblock based on the next block's | |
3476 | * start and the gap size. | |
3477 | */ | |
3478 | gotbno = ap->got.br_startblock; | |
3479 | /* | |
3480 | * Heuristic! | |
3481 | * If the gap is large relative to the piece we're | |
3482 | * allocating, or using it gives us an invalid block | |
3483 | * number, then just use the start of the next block | |
3484 | * offset by our length. | |
3485 | */ | |
3486 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && | |
3487 | ISVALID(gotbno - gotdiff, gotbno)) | |
3488 | gotbno -= adjust; | |
3489 | else if (ISVALID(gotbno - ap->length, gotbno)) { | |
3490 | gotbno -= ap->length; | |
3491 | gotdiff += adjust - ap->length; | |
3492 | } else | |
3493 | gotdiff += adjust; | |
3494 | /* | |
3495 | * If the firstblock forbids it, can't use it, | |
3496 | * must use default. | |
3497 | */ | |
3498 | if (!rt && !nullfb && | |
3499 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) | |
3500 | gotbno = NULLFSBLOCK; | |
3501 | } | |
3502 | /* | |
3503 | * No next block, just default. | |
3504 | */ | |
2bd0ea18 | 3505 | else |
49f693fa DC |
3506 | gotbno = NULLFSBLOCK; |
3507 | /* | |
3508 | * If both valid, pick the better one, else the only good | |
3509 | * one, else ap->blkno is already set (to 0 or the inode block). | |
3510 | */ | |
3511 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) | |
3512 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; | |
3513 | else if (prevbno != NULLFSBLOCK) | |
3514 | ap->blkno = prevbno; | |
3515 | else if (gotbno != NULLFSBLOCK) | |
3516 | ap->blkno = gotbno; | |
a2ceac1f | 3517 | } |
49f693fa | 3518 | #undef ISVALID |
a2ceac1f DC |
3519 | } |
3520 | ||
ff105f75 DC |
3521 | static int |
3522 | xfs_bmap_longest_free_extent( | |
3523 | struct xfs_trans *tp, | |
3524 | xfs_agnumber_t ag, | |
3525 | xfs_extlen_t *blen, | |
3526 | int *notinit) | |
3527 | { | |
3528 | struct xfs_mount *mp = tp->t_mountp; | |
3529 | struct xfs_perag *pag; | |
3530 | xfs_extlen_t longest; | |
3531 | int error = 0; | |
3532 | ||
3533 | pag = xfs_perag_get(mp, ag); | |
3534 | if (!pag->pagf_init) { | |
3535 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); | |
3536 | if (error) | |
3537 | goto out; | |
3538 | ||
3539 | if (!pag->pagf_init) { | |
3540 | *notinit = 1; | |
3541 | goto out; | |
3542 | } | |
3543 | } | |
3544 | ||
72bda06d | 3545 | longest = xfs_alloc_longest_free_extent(mp, pag, |
cf8ce220 DW |
3546 | xfs_alloc_min_freelist(mp, pag), |
3547 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); | |
ff105f75 DC |
3548 | if (*blen < longest) |
3549 | *blen = longest; | |
3550 | ||
3551 | out: | |
3552 | xfs_perag_put(pag); | |
3553 | return error; | |
3554 | } | |
3555 | ||
3556 | static void | |
3557 | xfs_bmap_select_minlen( | |
3558 | struct xfs_bmalloca *ap, | |
3559 | struct xfs_alloc_arg *args, | |
3560 | xfs_extlen_t *blen, | |
3561 | int notinit) | |
3562 | { | |
3563 | if (notinit || *blen < ap->minlen) { | |
3564 | /* | |
3565 | * Since we did a BUF_TRYLOCK above, it is possible that | |
3566 | * there is space for this request. | |
3567 | */ | |
3568 | args->minlen = ap->minlen; | |
3569 | } else if (*blen < args->maxlen) { | |
3570 | /* | |
3571 | * If the best seen length is less than the request length, | |
3572 | * use the best as the minimum. | |
3573 | */ | |
3574 | args->minlen = *blen; | |
3575 | } else { | |
3576 | /* | |
3577 | * Otherwise we've seen an extent as big as maxlen, use that | |
3578 | * as the minimum. | |
3579 | */ | |
3580 | args->minlen = args->maxlen; | |
3581 | } | |
3582 | } | |
3583 | ||
a2ceac1f | 3584 | STATIC int |
49f693fa DC |
3585 | xfs_bmap_btalloc_nullfb( |
3586 | struct xfs_bmalloca *ap, | |
3587 | struct xfs_alloc_arg *args, | |
3588 | xfs_extlen_t *blen) | |
a2ceac1f | 3589 | { |
49f693fa | 3590 | struct xfs_mount *mp = ap->ip->i_mount; |
49f693fa DC |
3591 | xfs_agnumber_t ag, startag; |
3592 | int notinit = 0; | |
a2ceac1f DC |
3593 | int error; |
3594 | ||
ff105f75 | 3595 | args->type = XFS_ALLOCTYPE_START_BNO; |
49f693fa | 3596 | args->total = ap->total; |
a2ceac1f | 3597 | |
49f693fa DC |
3598 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
3599 | if (startag == NULLAGNUMBER) | |
3600 | startag = ag = 0; | |
a2ceac1f | 3601 | |
49f693fa | 3602 | while (*blen < args->maxlen) { |
ff105f75 DC |
3603 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
3604 | ¬init); | |
3605 | if (error) | |
3606 | return error; | |
a2ceac1f | 3607 | |
49f693fa DC |
3608 | if (++ag == mp->m_sb.sb_agcount) |
3609 | ag = 0; | |
3610 | if (ag == startag) | |
3611 | break; | |
49f693fa | 3612 | } |
2bd0ea18 | 3613 | |
ff105f75 DC |
3614 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
3615 | return 0; | |
3616 | } | |
3617 | ||
3618 | STATIC int | |
3619 | xfs_bmap_btalloc_filestreams( | |
3620 | struct xfs_bmalloca *ap, | |
3621 | struct xfs_alloc_arg *args, | |
3622 | xfs_extlen_t *blen) | |
3623 | { | |
3624 | struct xfs_mount *mp = ap->ip->i_mount; | |
3625 | xfs_agnumber_t ag; | |
3626 | int notinit = 0; | |
3627 | int error; | |
3628 | ||
3629 | args->type = XFS_ALLOCTYPE_NEAR_BNO; | |
3630 | args->total = ap->total; | |
3631 | ||
3632 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); | |
3633 | if (ag == NULLAGNUMBER) | |
3634 | ag = 0; | |
3635 | ||
3636 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); | |
3637 | if (error) | |
3638 | return error; | |
3639 | ||
3640 | if (*blen < args->maxlen) { | |
3641 | error = xfs_filestream_new_ag(ap, &ag); | |
3642 | if (error) | |
3643 | return error; | |
3644 | ||
3645 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, | |
3646 | ¬init); | |
3647 | if (error) | |
3648 | return error; | |
3649 | ||
3650 | } | |
3651 | ||
3652 | xfs_bmap_select_minlen(ap, args, blen, notinit); | |
2bd0ea18 | 3653 | |
49f693fa | 3654 | /* |
ff105f75 DC |
3655 | * Set the failure fallback case to look in the selected AG as stream |
3656 | * may have moved. | |
49f693fa | 3657 | */ |
ff105f75 | 3658 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
49f693fa | 3659 | return 0; |
2bd0ea18 NS |
3660 | } |
3661 | ||
b3563c19 | 3662 | STATIC int |
49f693fa | 3663 | xfs_bmap_btalloc( |
613e6057 | 3664 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
b3563c19 | 3665 | { |
49f693fa DC |
3666 | xfs_mount_t *mp; /* mount point structure */ |
3667 | xfs_alloctype_t atype = 0; /* type for allocation routines */ | |
1fccd5c8 | 3668 | xfs_extlen_t align = 0; /* minimum allocation alignment */ |
49f693fa DC |
3669 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
3670 | xfs_agnumber_t ag; | |
3671 | xfs_alloc_arg_t args; | |
3672 | xfs_extlen_t blen; | |
3673 | xfs_extlen_t nextminlen = 0; | |
3674 | int nullfb; /* true if ap->firstblock isn't set */ | |
3675 | int isaligned; | |
3676 | int tryagain; | |
3677 | int error; | |
ff105f75 | 3678 | int stripe_align; |
b3563c19 | 3679 | |
49f693fa | 3680 | ASSERT(ap->length); |
b3563c19 | 3681 | |
49f693fa | 3682 | mp = ap->ip->i_mount; |
ff105f75 DC |
3683 | |
3684 | /* stripe alignment for allocation is determined by mount parameters */ | |
3685 | stripe_align = 0; | |
3686 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) | |
3687 | stripe_align = mp->m_swidth; | |
3688 | else if (mp->m_dalign) | |
3689 | stripe_align = mp->m_dalign; | |
3690 | ||
10460994 DW |
3691 | if (ap->flags & XFS_BMAPI_COWFORK) |
3692 | align = xfs_get_cowextsz_hint(ap->ip); | |
3693 | else if (xfs_alloc_is_userdata(ap->datatype)) | |
1fccd5c8 | 3694 | align = xfs_get_extsz_hint(ap->ip); |
49f693fa DC |
3695 | if (unlikely(align)) { |
3696 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, | |
3697 | align, 0, ap->eof, 0, ap->conv, | |
3698 | &ap->offset, &ap->length); | |
3699 | ASSERT(!error); | |
3700 | ASSERT(ap->length); | |
3701 | } | |
ff105f75 DC |
3702 | |
3703 | ||
49f693fa DC |
3704 | nullfb = *ap->firstblock == NULLFSBLOCK; |
3705 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); | |
3706 | if (nullfb) { | |
1fccd5c8 DC |
3707 | if (xfs_alloc_is_userdata(ap->datatype) && |
3708 | xfs_inode_is_filestream(ap->ip)) { | |
49f693fa DC |
3709 | ag = xfs_filestream_lookup_ag(ap->ip); |
3710 | ag = (ag != NULLAGNUMBER) ? ag : 0; | |
3711 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); | |
3712 | } else { | |
3713 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); | |
3714 | } | |
3715 | } else | |
3716 | ap->blkno = *ap->firstblock; | |
3717 | ||
3718 | xfs_bmap_adjacent(ap); | |
2bd0ea18 | 3719 | |
2bd0ea18 | 3720 | /* |
49f693fa DC |
3721 | * If allowed, use ap->blkno; otherwise must use firstblock since |
3722 | * it's in the right allocation group. | |
2bd0ea18 | 3723 | */ |
49f693fa DC |
3724 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
3725 | ; | |
3726 | else | |
3727 | ap->blkno = *ap->firstblock; | |
2bd0ea18 | 3728 | /* |
49f693fa | 3729 | * Normal allocation, done through xfs_alloc_vextent. |
2bd0ea18 | 3730 | */ |
49f693fa DC |
3731 | tryagain = isaligned = 0; |
3732 | memset(&args, 0, sizeof(args)); | |
3733 | args.tp = ap->tp; | |
3734 | args.mp = mp; | |
3735 | args.fsbno = ap->blkno; | |
85aec44f | 3736 | xfs_rmap_skip_owner_update(&args.oinfo); |
49f693fa DC |
3737 | |
3738 | /* Trim the allocation back to the maximum an AG can fit. */ | |
b8a8d6e5 | 3739 | args.maxlen = MIN(ap->length, mp->m_ag_max_usable); |
49f693fa DC |
3740 | args.firstblock = *ap->firstblock; |
3741 | blen = 0; | |
3742 | if (nullfb) { | |
ff105f75 DC |
3743 | /* |
3744 | * Search for an allocation group with a single extent large | |
3745 | * enough for the request. If one isn't found, then adjust | |
3746 | * the minimum allocation size to the largest space found. | |
3747 | */ | |
1fccd5c8 DC |
3748 | if (xfs_alloc_is_userdata(ap->datatype) && |
3749 | xfs_inode_is_filestream(ap->ip)) | |
ff105f75 DC |
3750 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
3751 | else | |
3752 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); | |
a2ceac1f | 3753 | if (error) |
2bd0ea18 | 3754 | return error; |
f33cea1a | 3755 | } else if (ap->dfops->dop_low) { |
49f693fa DC |
3756 | if (xfs_inode_is_filestream(ap->ip)) |
3757 | args.type = XFS_ALLOCTYPE_FIRST_AG; | |
3758 | else | |
3759 | args.type = XFS_ALLOCTYPE_START_BNO; | |
3760 | args.total = args.minlen = ap->minlen; | |
3761 | } else { | |
3762 | args.type = XFS_ALLOCTYPE_NEAR_BNO; | |
3763 | args.total = ap->total; | |
3764 | args.minlen = ap->minlen; | |
3765 | } | |
3766 | /* apply extent size hints if obtained earlier */ | |
3767 | if (unlikely(align)) { | |
3768 | args.prod = align; | |
3769 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) | |
3770 | args.mod = (xfs_extlen_t)(args.prod - args.mod); | |
b2327e1a | 3771 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
49f693fa DC |
3772 | args.prod = 1; |
3773 | args.mod = 0; | |
3774 | } else { | |
b2327e1a | 3775 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
49f693fa DC |
3776 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
3777 | args.mod = (xfs_extlen_t)(args.prod - args.mod); | |
2bd0ea18 NS |
3778 | } |
3779 | /* | |
49f693fa DC |
3780 | * If we are not low on available data blocks, and the |
3781 | * underlying logical volume manager is a stripe, and | |
3782 | * the file offset is zero then try to allocate data | |
3783 | * blocks on stripe unit boundary. | |
3784 | * NOTE: ap->aeof is only set if the allocation length | |
3785 | * is >= the stripe unit and the allocation offset is | |
3786 | * at the end of file. | |
2bd0ea18 | 3787 | */ |
f33cea1a | 3788 | if (!ap->dfops->dop_low && ap->aeof) { |
49f693fa | 3789 | if (!ap->offset) { |
ff105f75 | 3790 | args.alignment = stripe_align; |
49f693fa DC |
3791 | atype = args.type; |
3792 | isaligned = 1; | |
3793 | /* | |
3794 | * Adjust for alignment | |
3795 | */ | |
3796 | if (blen > args.alignment && blen <= args.maxlen) | |
3797 | args.minlen = blen - args.alignment; | |
3798 | args.minalignslop = 0; | |
3799 | } else { | |
3800 | /* | |
3801 | * First try an exact bno allocation. | |
3802 | * If it fails then do a near or start bno | |
3803 | * allocation with alignment turned on. | |
3804 | */ | |
3805 | atype = args.type; | |
3806 | tryagain = 1; | |
3807 | args.type = XFS_ALLOCTYPE_THIS_BNO; | |
3808 | args.alignment = 1; | |
3809 | /* | |
3810 | * Compute the minlen+alignment for the | |
3811 | * next case. Set slop so that the value | |
3812 | * of minlen+alignment+slop doesn't go up | |
3813 | * between the calls. | |
3814 | */ | |
ff105f75 DC |
3815 | if (blen > stripe_align && blen <= args.maxlen) |
3816 | nextminlen = blen - stripe_align; | |
49f693fa DC |
3817 | else |
3818 | nextminlen = args.minlen; | |
ff105f75 | 3819 | if (nextminlen + stripe_align > args.minlen + 1) |
49f693fa | 3820 | args.minalignslop = |
ff105f75 | 3821 | nextminlen + stripe_align - |
49f693fa DC |
3822 | args.minlen - 1; |
3823 | else | |
3824 | args.minalignslop = 0; | |
2bd0ea18 | 3825 | } |
49f693fa DC |
3826 | } else { |
3827 | args.alignment = 1; | |
3828 | args.minalignslop = 0; | |
3829 | } | |
3830 | args.minleft = ap->minleft; | |
3831 | args.wasdel = ap->wasdel; | |
cf8ce220 | 3832 | args.resv = XFS_AG_RESV_NONE; |
1fccd5c8 DC |
3833 | args.datatype = ap->datatype; |
3834 | if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) | |
9542ae13 DC |
3835 | args.ip = ap->ip; |
3836 | ||
3837 | error = xfs_alloc_vextent(&args); | |
3838 | if (error) | |
49f693fa | 3839 | return error; |
9542ae13 | 3840 | |
49f693fa DC |
3841 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
3842 | /* | |
3843 | * Exact allocation failed. Now try with alignment | |
3844 | * turned on. | |
3845 | */ | |
3846 | args.type = atype; | |
3847 | args.fsbno = ap->blkno; | |
ff105f75 | 3848 | args.alignment = stripe_align; |
49f693fa DC |
3849 | args.minlen = nextminlen; |
3850 | args.minalignslop = 0; | |
3851 | isaligned = 1; | |
3852 | if ((error = xfs_alloc_vextent(&args))) | |
3853 | return error; | |
3854 | } | |
3855 | if (isaligned && args.fsbno == NULLFSBLOCK) { | |
2bd0ea18 | 3856 | /* |
49f693fa DC |
3857 | * allocation failed, so turn off alignment and |
3858 | * try again. | |
2bd0ea18 | 3859 | */ |
49f693fa DC |
3860 | args.type = atype; |
3861 | args.fsbno = ap->blkno; | |
3862 | args.alignment = 0; | |
3863 | if ((error = xfs_alloc_vextent(&args))) | |
3864 | return error; | |
3865 | } | |
3866 | if (args.fsbno == NULLFSBLOCK && nullfb && | |
3867 | args.minlen > ap->minlen) { | |
3868 | args.minlen = ap->minlen; | |
3869 | args.type = XFS_ALLOCTYPE_START_BNO; | |
3870 | args.fsbno = ap->blkno; | |
3871 | if ((error = xfs_alloc_vextent(&args))) | |
3872 | return error; | |
3873 | } | |
3874 | if (args.fsbno == NULLFSBLOCK && nullfb) { | |
3875 | args.fsbno = 0; | |
3876 | args.type = XFS_ALLOCTYPE_FIRST_AG; | |
3877 | args.total = ap->minlen; | |
3878 | args.minleft = 0; | |
3879 | if ((error = xfs_alloc_vextent(&args))) | |
3880 | return error; | |
f33cea1a | 3881 | ap->dfops->dop_low = true; |
49f693fa DC |
3882 | } |
3883 | if (args.fsbno != NULLFSBLOCK) { | |
2bd0ea18 | 3884 | /* |
49f693fa DC |
3885 | * check the allocation happened at the same or higher AG than |
3886 | * the first block that was allocated. | |
2bd0ea18 | 3887 | */ |
49f693fa DC |
3888 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
3889 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) == | |
3890 | XFS_FSB_TO_AGNO(mp, args.fsbno) || | |
f33cea1a | 3891 | (ap->dfops->dop_low && |
49f693fa DC |
3892 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) < |
3893 | XFS_FSB_TO_AGNO(mp, args.fsbno))); | |
3894 | ||
3895 | ap->blkno = args.fsbno; | |
3896 | if (*ap->firstblock == NULLFSBLOCK) | |
3897 | *ap->firstblock = args.fsbno; | |
3898 | ASSERT(nullfb || fb_agno == args.agno || | |
f33cea1a | 3899 | (ap->dfops->dop_low && fb_agno < args.agno)); |
49f693fa | 3900 | ap->length = args.len; |
1277a5e0 DW |
3901 | if (!(ap->flags & XFS_BMAPI_COWFORK)) |
3902 | ap->ip->i_d.di_nblocks += args.len; | |
49f693fa DC |
3903 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
3904 | if (ap->wasdel) | |
3905 | ap->ip->i_delayed_blks -= args.len; | |
2bd0ea18 | 3906 | /* |
49f693fa DC |
3907 | * Adjust the disk quota also. This was reserved |
3908 | * earlier. | |
2bd0ea18 | 3909 | */ |
49f693fa DC |
3910 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
3911 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : | |
3912 | XFS_TRANS_DQ_BCOUNT, | |
3913 | (long) args.len); | |
3914 | } else { | |
3915 | ap->blkno = NULLFSBLOCK; | |
3916 | ap->length = 0; | |
2bd0ea18 | 3917 | } |
2bd0ea18 | 3918 | return 0; |
56b2de80 DC |
3919 | } |
3920 | ||
34621a47 DW |
3921 | /* |
3922 | * For a remap operation, just "allocate" an extent at the address that the | |
3923 | * caller passed in, and ensure that the AGFL is the right size. The caller | |
3924 | * will then map the "allocated" extent into the file somewhere. | |
3925 | */ | |
3926 | STATIC int | |
3927 | xfs_bmap_remap_alloc( | |
3928 | struct xfs_bmalloca *ap) | |
3929 | { | |
3930 | struct xfs_trans *tp = ap->tp; | |
3931 | struct xfs_mount *mp = tp->t_mountp; | |
3932 | xfs_agblock_t bno; | |
3933 | struct xfs_alloc_arg args; | |
3934 | int error; | |
3935 | ||
3936 | /* | |
3937 | * validate that the block number is legal - the enables us to detect | |
3938 | * and handle a silent filesystem corruption rather than crashing. | |
3939 | */ | |
3940 | memset(&args, 0, sizeof(struct xfs_alloc_arg)); | |
3941 | args.tp = ap->tp; | |
3942 | args.mp = ap->tp->t_mountp; | |
3943 | bno = *ap->firstblock; | |
3944 | args.agno = XFS_FSB_TO_AGNO(mp, bno); | |
3945 | args.agbno = XFS_FSB_TO_AGBNO(mp, bno); | |
3946 | if (args.agno >= mp->m_sb.sb_agcount || | |
3947 | args.agbno >= mp->m_sb.sb_agblocks) | |
3948 | return -EFSCORRUPTED; | |
3949 | ||
3950 | /* "Allocate" the extent from the range we passed in. */ | |
3951 | trace_xfs_bmap_remap_alloc(ap->ip, *ap->firstblock, ap->length); | |
3952 | ap->blkno = bno; | |
3953 | ap->ip->i_d.di_nblocks += ap->length; | |
3954 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); | |
3955 | ||
3956 | /* Fix the freelist, like a real allocator does. */ | |
3957 | args.datatype = ap->datatype; | |
3958 | args.pag = xfs_perag_get(args.mp, args.agno); | |
3959 | ASSERT(args.pag); | |
3960 | ||
3961 | /* | |
3962 | * The freelist fixing code will decline the allocation if | |
3963 | * the size and shape of the free space doesn't allow for | |
3964 | * allocating the extent and updating all the metadata that | |
3965 | * happens during an allocation. We're remapping, not | |
3966 | * allocating, so skip that check by pretending to be freeing. | |
3967 | */ | |
3968 | error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING); | |
34621a47 DW |
3969 | xfs_perag_put(args.pag); |
3970 | if (error) | |
3971 | trace_xfs_bmap_remap_alloc_error(ap->ip, error, _RET_IP_); | |
3972 | return error; | |
3973 | } | |
3974 | ||
56b2de80 | 3975 | /* |
49f693fa DC |
3976 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
3977 | * It figures out where to ask the underlying allocator to put the new extent. | |
56b2de80 | 3978 | */ |
49f693fa DC |
3979 | STATIC int |
3980 | xfs_bmap_alloc( | |
613e6057 | 3981 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
56b2de80 | 3982 | { |
34621a47 DW |
3983 | if (ap->flags & XFS_BMAPI_REMAP) |
3984 | return xfs_bmap_remap_alloc(ap); | |
1fccd5c8 DC |
3985 | if (XFS_IS_REALTIME_INODE(ap->ip) && |
3986 | xfs_alloc_is_userdata(ap->datatype)) | |
49f693fa DC |
3987 | return xfs_bmap_rtalloc(ap); |
3988 | return xfs_bmap_btalloc(ap); | |
56b2de80 | 3989 | } |
56b2de80 | 3990 | |
b3fd8db7 DW |
3991 | /* Trim extent to fit a logical block range. */ |
3992 | void | |
3993 | xfs_trim_extent( | |
3994 | struct xfs_bmbt_irec *irec, | |
3995 | xfs_fileoff_t bno, | |
3996 | xfs_filblks_t len) | |
3997 | { | |
3998 | xfs_fileoff_t distance; | |
3999 | xfs_fileoff_t end = bno + len; | |
4000 | ||
4001 | if (irec->br_startoff + irec->br_blockcount <= bno || | |
4002 | irec->br_startoff >= end) { | |
4003 | irec->br_blockcount = 0; | |
4004 | return; | |
4005 | } | |
4006 | ||
4007 | if (irec->br_startoff < bno) { | |
4008 | distance = bno - irec->br_startoff; | |
4009 | if (isnullstartblock(irec->br_startblock)) | |
4010 | irec->br_startblock = DELAYSTARTBLOCK; | |
4011 | if (irec->br_startblock != DELAYSTARTBLOCK && | |
4012 | irec->br_startblock != HOLESTARTBLOCK) | |
4013 | irec->br_startblock += distance; | |
4014 | irec->br_startoff += distance; | |
4015 | irec->br_blockcount -= distance; | |
4016 | } | |
4017 | ||
4018 | if (end < irec->br_startoff + irec->br_blockcount) { | |
4019 | distance = irec->br_startoff + irec->br_blockcount - end; | |
4020 | irec->br_blockcount -= distance; | |
4021 | } | |
4022 | } | |
4023 | ||
2bd0ea18 | 4024 | /* |
a2ceac1f | 4025 | * Trim the returned map to the required bounds |
2bd0ea18 | 4026 | */ |
a2ceac1f DC |
4027 | STATIC void |
4028 | xfs_bmapi_trim_map( | |
4029 | struct xfs_bmbt_irec *mval, | |
4030 | struct xfs_bmbt_irec *got, | |
4031 | xfs_fileoff_t *bno, | |
4032 | xfs_filblks_t len, | |
4033 | xfs_fileoff_t obno, | |
4034 | xfs_fileoff_t end, | |
4035 | int n, | |
4036 | int flags) | |
2bd0ea18 | 4037 | { |
a2ceac1f DC |
4038 | if ((flags & XFS_BMAPI_ENTIRE) || |
4039 | got->br_startoff + got->br_blockcount <= obno) { | |
4040 | *mval = *got; | |
4041 | if (isnullstartblock(got->br_startblock)) | |
4042 | mval->br_startblock = DELAYSTARTBLOCK; | |
4043 | return; | |
63be04eb | 4044 | } |
a2ceac1f DC |
4045 | |
4046 | if (obno > *bno) | |
4047 | *bno = obno; | |
4048 | ASSERT((*bno >= obno) || (n == 0)); | |
4049 | ASSERT(*bno < end); | |
4050 | mval->br_startoff = *bno; | |
4051 | if (isnullstartblock(got->br_startblock)) | |
4052 | mval->br_startblock = DELAYSTARTBLOCK; | |
2bd0ea18 | 4053 | else |
a2ceac1f DC |
4054 | mval->br_startblock = got->br_startblock + |
4055 | (*bno - got->br_startoff); | |
2bd0ea18 | 4056 | /* |
a2ceac1f DC |
4057 | * Return the minimum of what we got and what we asked for for |
4058 | * the length. We can use the len variable here because it is | |
4059 | * modified below and we could have been there before coming | |
4060 | * here if the first part of the allocation didn't overlap what | |
4061 | * was asked for. | |
2bd0ea18 | 4062 | */ |
a2ceac1f DC |
4063 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
4064 | got->br_blockcount - (*bno - got->br_startoff)); | |
4065 | mval->br_state = got->br_state; | |
4066 | ASSERT(mval->br_blockcount <= len); | |
4067 | return; | |
4068 | } | |
56b2de80 | 4069 | |
a2ceac1f DC |
4070 | /* |
4071 | * Update and validate the extent map to return | |
4072 | */ | |
4073 | STATIC void | |
4074 | xfs_bmapi_update_map( | |
4075 | struct xfs_bmbt_irec **map, | |
4076 | xfs_fileoff_t *bno, | |
4077 | xfs_filblks_t *len, | |
4078 | xfs_fileoff_t obno, | |
4079 | xfs_fileoff_t end, | |
4080 | int *n, | |
4081 | int flags) | |
4082 | { | |
4083 | xfs_bmbt_irec_t *mval = *map; | |
4084 | ||
4085 | ASSERT((flags & XFS_BMAPI_ENTIRE) || | |
4086 | ((mval->br_startoff + mval->br_blockcount) <= end)); | |
4087 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || | |
4088 | (mval->br_startoff < obno)); | |
4089 | ||
4090 | *bno = mval->br_startoff + mval->br_blockcount; | |
4091 | *len = end - *bno; | |
4092 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { | |
4093 | /* update previous map with new information */ | |
4094 | ASSERT(mval->br_startblock == mval[-1].br_startblock); | |
4095 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); | |
4096 | ASSERT(mval->br_state == mval[-1].br_state); | |
4097 | mval[-1].br_blockcount = mval->br_blockcount; | |
4098 | mval[-1].br_state = mval->br_state; | |
4099 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && | |
4100 | mval[-1].br_startblock != DELAYSTARTBLOCK && | |
4101 | mval[-1].br_startblock != HOLESTARTBLOCK && | |
4102 | mval->br_startblock == mval[-1].br_startblock + | |
4103 | mval[-1].br_blockcount && | |
4104 | ((flags & XFS_BMAPI_IGSTATE) || | |
4105 | mval[-1].br_state == mval->br_state)) { | |
4106 | ASSERT(mval->br_startoff == | |
4107 | mval[-1].br_startoff + mval[-1].br_blockcount); | |
4108 | mval[-1].br_blockcount += mval->br_blockcount; | |
4109 | } else if (*n > 0 && | |
4110 | mval->br_startblock == DELAYSTARTBLOCK && | |
4111 | mval[-1].br_startblock == DELAYSTARTBLOCK && | |
4112 | mval->br_startoff == | |
4113 | mval[-1].br_startoff + mval[-1].br_blockcount) { | |
4114 | mval[-1].br_blockcount += mval->br_blockcount; | |
4115 | mval[-1].br_state = mval->br_state; | |
4116 | } else if (!((*n == 0) && | |
4117 | ((mval->br_startoff + mval->br_blockcount) <= | |
4118 | obno))) { | |
4119 | mval++; | |
4120 | (*n)++; | |
4121 | } | |
4122 | *map = mval; | |
4123 | } | |
399ab595 | 4124 | |
a2ceac1f DC |
4125 | /* |
4126 | * Map file blocks to filesystem blocks without allocation. | |
4127 | */ | |
4128 | int | |
4129 | xfs_bmapi_read( | |
4130 | struct xfs_inode *ip, | |
4131 | xfs_fileoff_t bno, | |
4132 | xfs_filblks_t len, | |
4133 | struct xfs_bmbt_irec *mval, | |
4134 | int *nmap, | |
4135 | int flags) | |
4136 | { | |
4137 | struct xfs_mount *mp = ip->i_mount; | |
4138 | struct xfs_ifork *ifp; | |
4139 | struct xfs_bmbt_irec got; | |
4140 | struct xfs_bmbt_irec prev; | |
4141 | xfs_fileoff_t obno; | |
4142 | xfs_fileoff_t end; | |
4143 | xfs_extnum_t lastx; | |
4144 | int error; | |
4145 | int eof; | |
4146 | int n = 0; | |
cb8a004a | 4147 | int whichfork = xfs_bmapi_whichfork(flags); |
399ab595 | 4148 | |
a2ceac1f DC |
4149 | ASSERT(*nmap >= 1); |
4150 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| | |
cb8a004a | 4151 | XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK))); |
ff105f75 | 4152 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
062998e3 | 4153 | |
a2ceac1f DC |
4154 | if (unlikely(XFS_TEST_ERROR( |
4155 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && | |
4156 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), | |
4157 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { | |
4158 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); | |
12b53197 | 4159 | return -EFSCORRUPTED; |
a2ceac1f | 4160 | } |
062998e3 | 4161 | |
a2ceac1f | 4162 | if (XFS_FORCED_SHUTDOWN(mp)) |
12b53197 | 4163 | return -EIO; |
399ab595 | 4164 | |
79896434 | 4165 | XFS_STATS_INC(mp, xs_blk_mapr); |
a2ceac1f DC |
4166 | |
4167 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
4168 | ||
cb8a004a DW |
4169 | /* No CoW fork? Return a hole. */ |
4170 | if (whichfork == XFS_COW_FORK && !ifp) { | |
4171 | mval->br_startoff = bno; | |
4172 | mval->br_startblock = HOLESTARTBLOCK; | |
4173 | mval->br_blockcount = len; | |
4174 | mval->br_state = XFS_EXT_NORM; | |
4175 | *nmap = 1; | |
4176 | return 0; | |
4177 | } | |
4178 | ||
a2ceac1f DC |
4179 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
4180 | error = xfs_iread_extents(NULL, ip, whichfork); | |
4181 | if (error) | |
4182 | return error; | |
4183 | } | |
4184 | ||
4185 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev); | |
4186 | end = bno + len; | |
4187 | obno = bno; | |
4188 | ||
4189 | while (bno < end && n < *nmap) { | |
4190 | /* Reading past eof, act as though there's a hole up to end. */ | |
4191 | if (eof) | |
4192 | got.br_startoff = end; | |
4193 | if (got.br_startoff > bno) { | |
4194 | /* Reading in a hole. */ | |
2bd0ea18 NS |
4195 | mval->br_startoff = bno; |
4196 | mval->br_startblock = HOLESTARTBLOCK; | |
4197 | mval->br_blockcount = | |
4198 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); | |
4199 | mval->br_state = XFS_EXT_NORM; | |
4200 | bno += mval->br_blockcount; | |
4201 | len -= mval->br_blockcount; | |
4202 | mval++; | |
4203 | n++; | |
4204 | continue; | |
4205 | } | |
a2ceac1f DC |
4206 | |
4207 | /* set up the extent map to return. */ | |
4208 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); | |
4209 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); | |
4210 | ||
4211 | /* If we're done, stop now. */ | |
4212 | if (bno >= end || n >= *nmap) | |
4213 | break; | |
4214 | ||
4215 | /* Else go on to the next record. */ | |
4216 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) | |
4217 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); | |
4218 | else | |
4219 | eof = 1; | |
4220 | } | |
4221 | *nmap = n; | |
4222 | return 0; | |
4223 | } | |
4224 | ||
4488e421 | 4225 | int |
a2ceac1f DC |
4226 | xfs_bmapi_reserve_delalloc( |
4227 | struct xfs_inode *ip, | |
cc66acaf | 4228 | int whichfork, |
a2ceac1f DC |
4229 | xfs_fileoff_t aoff, |
4230 | xfs_filblks_t len, | |
4231 | struct xfs_bmbt_irec *got, | |
4232 | struct xfs_bmbt_irec *prev, | |
4233 | xfs_extnum_t *lastx, | |
4234 | int eof) | |
4235 | { | |
4236 | struct xfs_mount *mp = ip->i_mount; | |
cc66acaf | 4237 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
a2ceac1f DC |
4238 | xfs_extlen_t alen; |
4239 | xfs_extlen_t indlen; | |
4240 | char rt = XFS_IS_REALTIME_INODE(ip); | |
4241 | xfs_extlen_t extsz; | |
4242 | int error; | |
4243 | ||
4244 | alen = XFS_FILBLKS_MIN(len, MAXEXTLEN); | |
4245 | if (!eof) | |
4246 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); | |
4247 | ||
4248 | /* Figure out the extent size, adjust alen */ | |
10460994 DW |
4249 | if (whichfork == XFS_COW_FORK) |
4250 | extsz = xfs_get_cowextsz_hint(ip); | |
4251 | else | |
4252 | extsz = xfs_get_extsz_hint(ip); | |
a2ceac1f | 4253 | if (extsz) { |
a2ceac1f DC |
4254 | error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof, |
4255 | 1, 0, &aoff, &alen); | |
4256 | ASSERT(!error); | |
4257 | } | |
4258 | ||
4259 | if (rt) | |
4260 | extsz = alen / mp->m_sb.sb_rextsize; | |
4261 | ||
4262 | /* | |
4263 | * Make a transaction-less quota reservation for delayed allocation | |
4264 | * blocks. This number gets adjusted later. We return if we haven't | |
4265 | * allocated blocks already inside this loop. | |
4266 | */ | |
4267 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, | |
4268 | rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); | |
4269 | if (error) | |
4270 | return error; | |
4271 | ||
4272 | /* | |
4273 | * Split changing sb for alen and indlen since they could be coming | |
4274 | * from different places. | |
4275 | */ | |
4276 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); | |
4277 | ASSERT(indlen > 0); | |
4278 | ||
4279 | if (rt) { | |
19ebedcf | 4280 | error = xfs_mod_frextents(mp, -((int64_t)extsz)); |
a2ceac1f | 4281 | } else { |
19ebedcf | 4282 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
a2ceac1f DC |
4283 | } |
4284 | ||
4285 | if (error) | |
4286 | goto out_unreserve_quota; | |
4287 | ||
19ebedcf | 4288 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
a2ceac1f DC |
4289 | if (error) |
4290 | goto out_unreserve_blocks; | |
4291 | ||
4292 | ||
4293 | ip->i_delayed_blks += alen; | |
4294 | ||
4295 | got->br_startoff = aoff; | |
4296 | got->br_startblock = nullstartblock(indlen); | |
4297 | got->br_blockcount = alen; | |
4298 | got->br_state = XFS_EXT_NORM; | |
cc66acaf | 4299 | xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got); |
a2ceac1f DC |
4300 | |
4301 | /* | |
4302 | * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay | |
4303 | * might have merged it into one of the neighbouring ones. | |
4304 | */ | |
4305 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got); | |
4306 | ||
4307 | ASSERT(got->br_startoff <= aoff); | |
4308 | ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen); | |
4309 | ASSERT(isnullstartblock(got->br_startblock)); | |
4310 | ASSERT(got->br_state == XFS_EXT_NORM); | |
4311 | return 0; | |
4312 | ||
4313 | out_unreserve_blocks: | |
4314 | if (rt) | |
19ebedcf | 4315 | xfs_mod_frextents(mp, extsz); |
a2ceac1f | 4316 | else |
19ebedcf | 4317 | xfs_mod_fdblocks(mp, alen, false); |
a2ceac1f DC |
4318 | out_unreserve_quota: |
4319 | if (XFS_IS_QUOTA_ON(mp)) | |
4320 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ? | |
4321 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); | |
4322 | return error; | |
4323 | } | |
4324 | ||
ff105f75 DC |
4325 | static int |
4326 | xfs_bmapi_allocate( | |
a2ceac1f DC |
4327 | struct xfs_bmalloca *bma) |
4328 | { | |
4329 | struct xfs_mount *mp = bma->ip->i_mount; | |
1277a5e0 | 4330 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
a2ceac1f DC |
4331 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
4332 | int tmp_logflags = 0; | |
4333 | int error; | |
4334 | ||
4335 | ASSERT(bma->length > 0); | |
4336 | ||
4337 | /* | |
4338 | * For the wasdelay case, we could also just allocate the stuff asked | |
4339 | * for in this bmap call but that wouldn't be as good. | |
4340 | */ | |
4341 | if (bma->wasdel) { | |
4342 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; | |
4343 | bma->offset = bma->got.br_startoff; | |
4344 | if (bma->idx != NULLEXTNUM && bma->idx) { | |
4345 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), | |
4346 | &bma->prev); | |
4347 | } | |
4348 | } else { | |
4349 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); | |
4350 | if (!bma->eof) | |
4351 | bma->length = XFS_FILBLKS_MIN(bma->length, | |
4352 | bma->got.br_startoff - bma->offset); | |
4353 | } | |
4354 | ||
4355 | /* | |
1fccd5c8 DC |
4356 | * Set the data type being allocated. For the data fork, the first data |
4357 | * in the file is treated differently to all other allocations. For the | |
4358 | * attribute fork, we only need to ensure the allocated range is not on | |
4359 | * the busy list. | |
a2ceac1f DC |
4360 | */ |
4361 | if (!(bma->flags & XFS_BMAPI_METADATA)) { | |
1fccd5c8 DC |
4362 | bma->datatype = XFS_ALLOC_NOBUSY; |
4363 | if (whichfork == XFS_DATA_FORK) { | |
4364 | if (bma->offset == 0) | |
4365 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; | |
4366 | else | |
4367 | bma->datatype |= XFS_ALLOC_USERDATA; | |
4368 | } | |
9542ae13 | 4369 | if (bma->flags & XFS_BMAPI_ZERO) |
1fccd5c8 | 4370 | bma->datatype |= XFS_ALLOC_USERDATA_ZERO; |
a2ceac1f DC |
4371 | } |
4372 | ||
4373 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; | |
4374 | ||
4375 | /* | |
4376 | * Only want to do the alignment at the eof if it is userdata and | |
4377 | * allocation length is larger than a stripe unit. | |
4378 | */ | |
4379 | if (mp->m_dalign && bma->length >= mp->m_dalign && | |
4380 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { | |
4381 | error = xfs_bmap_isaeof(bma, whichfork); | |
4382 | if (error) | |
4383 | return error; | |
4384 | } | |
4385 | ||
a2ceac1f DC |
4386 | error = xfs_bmap_alloc(bma); |
4387 | if (error) | |
4388 | return error; | |
4389 | ||
f33cea1a | 4390 | if (bma->dfops->dop_low) |
a2ceac1f DC |
4391 | bma->minleft = 0; |
4392 | if (bma->cur) | |
4393 | bma->cur->bc_private.b.firstblock = *bma->firstblock; | |
4394 | if (bma->blkno == NULLFSBLOCK) | |
4395 | return 0; | |
4396 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { | |
4397 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); | |
4398 | bma->cur->bc_private.b.firstblock = *bma->firstblock; | |
f33cea1a | 4399 | bma->cur->bc_private.b.dfops = bma->dfops; |
a2ceac1f DC |
4400 | } |
4401 | /* | |
4402 | * Bump the number of extents we've allocated | |
4403 | * in this call. | |
4404 | */ | |
4405 | bma->nallocs++; | |
4406 | ||
4407 | if (bma->cur) | |
4408 | bma->cur->bc_private.b.flags = | |
4409 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; | |
4410 | ||
4411 | bma->got.br_startoff = bma->offset; | |
4412 | bma->got.br_startblock = bma->blkno; | |
4413 | bma->got.br_blockcount = bma->length; | |
4414 | bma->got.br_state = XFS_EXT_NORM; | |
4415 | ||
4416 | /* | |
4417 | * A wasdelay extent has been initialized, so shouldn't be flagged | |
4418 | * as unwritten. | |
4419 | */ | |
4420 | if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) && | |
4421 | xfs_sb_version_hasextflgbit(&mp->m_sb)) | |
4422 | bma->got.br_state = XFS_EXT_UNWRITTEN; | |
4423 | ||
4424 | if (bma->wasdel) | |
1277a5e0 | 4425 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
a2ceac1f DC |
4426 | else |
4427 | error = xfs_bmap_add_extent_hole_real(bma, whichfork); | |
4428 | ||
4429 | bma->logflags |= tmp_logflags; | |
4430 | if (error) | |
4431 | return error; | |
4432 | ||
4433 | /* | |
4434 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real | |
4435 | * or xfs_bmap_add_extent_hole_real might have merged it into one of | |
4436 | * the neighbouring ones. | |
4437 | */ | |
4438 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got); | |
4439 | ||
4440 | ASSERT(bma->got.br_startoff <= bma->offset); | |
4441 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= | |
4442 | bma->offset + bma->length); | |
4443 | ASSERT(bma->got.br_state == XFS_EXT_NORM || | |
4444 | bma->got.br_state == XFS_EXT_UNWRITTEN); | |
4445 | return 0; | |
4446 | } | |
4447 | ||
a2ceac1f DC |
4448 | STATIC int |
4449 | xfs_bmapi_convert_unwritten( | |
4450 | struct xfs_bmalloca *bma, | |
4451 | struct xfs_bmbt_irec *mval, | |
4452 | xfs_filblks_t len, | |
4453 | int flags) | |
4454 | { | |
cb8a004a | 4455 | int whichfork = xfs_bmapi_whichfork(flags); |
a2ceac1f DC |
4456 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
4457 | int tmp_logflags = 0; | |
4458 | int error; | |
4459 | ||
4460 | /* check if we need to do unwritten->real conversion */ | |
4461 | if (mval->br_state == XFS_EXT_UNWRITTEN && | |
4462 | (flags & XFS_BMAPI_PREALLOC)) | |
4463 | return 0; | |
4464 | ||
4465 | /* check if we need to do real->unwritten conversion */ | |
4466 | if (mval->br_state == XFS_EXT_NORM && | |
4467 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != | |
4468 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) | |
4469 | return 0; | |
4470 | ||
cb8a004a DW |
4471 | ASSERT(whichfork != XFS_COW_FORK); |
4472 | ||
a2ceac1f DC |
4473 | /* |
4474 | * Modify (by adding) the state flag, if writing. | |
4475 | */ | |
4476 | ASSERT(mval->br_blockcount <= len); | |
4477 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { | |
4478 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, | |
4479 | bma->ip, whichfork); | |
4480 | bma->cur->bc_private.b.firstblock = *bma->firstblock; | |
f33cea1a | 4481 | bma->cur->bc_private.b.dfops = bma->dfops; |
a2ceac1f DC |
4482 | } |
4483 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) | |
4484 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; | |
4485 | ||
9542ae13 DC |
4486 | /* |
4487 | * Before insertion into the bmbt, zero the range being converted | |
4488 | * if required. | |
4489 | */ | |
4490 | if (flags & XFS_BMAPI_ZERO) { | |
4491 | error = xfs_zero_extent(bma->ip, mval->br_startblock, | |
4492 | mval->br_blockcount); | |
4493 | if (error) | |
4494 | return error; | |
4495 | } | |
4496 | ||
a2ceac1f | 4497 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx, |
f33cea1a | 4498 | &bma->cur, mval, bma->firstblock, bma->dfops, |
a2ceac1f | 4499 | &tmp_logflags); |
23fc058d BF |
4500 | /* |
4501 | * Log the inode core unconditionally in the unwritten extent conversion | |
4502 | * path because the conversion might not have done so (e.g., if the | |
4503 | * extent count hasn't changed). We need to make sure the inode is dirty | |
4504 | * in the transaction for the sake of fsync(), even if nothing has | |
4505 | * changed, because fsync() will not force the log for this transaction | |
4506 | * unless it sees the inode pinned. | |
4507 | */ | |
4508 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; | |
a2ceac1f DC |
4509 | if (error) |
4510 | return error; | |
4511 | ||
4512 | /* | |
4513 | * Update our extent pointer, given that | |
4514 | * xfs_bmap_add_extent_unwritten_real might have merged it into one | |
4515 | * of the neighbouring ones. | |
4516 | */ | |
4517 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got); | |
4518 | ||
4519 | /* | |
4520 | * We may have combined previously unwritten space with written space, | |
4521 | * so generate another request. | |
4522 | */ | |
4523 | if (mval->br_blockcount < len) | |
12b53197 | 4524 | return -EAGAIN; |
a2ceac1f DC |
4525 | return 0; |
4526 | } | |
4527 | ||
4528 | /* | |
4529 | * Map file blocks to filesystem blocks, and allocate blocks or convert the | |
4530 | * extent state if necessary. Details behaviour is controlled by the flags | |
4531 | * parameter. Only allocates blocks from a single allocation group, to avoid | |
4532 | * locking problems. | |
4533 | * | |
4534 | * The returned value in "firstblock" from the first call in a transaction | |
4535 | * must be remembered and presented to subsequent calls in "firstblock". | |
4536 | * An upper bound for the number of blocks to be allocated is supplied to | |
4537 | * the first call in "total"; if no allocation group has that many free | |
4538 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). | |
4539 | */ | |
4540 | int | |
4541 | xfs_bmapi_write( | |
4542 | struct xfs_trans *tp, /* transaction pointer */ | |
4543 | struct xfs_inode *ip, /* incore inode */ | |
4544 | xfs_fileoff_t bno, /* starting file offs. mapped */ | |
4545 | xfs_filblks_t len, /* length to map in file */ | |
4546 | int flags, /* XFS_BMAPI_... */ | |
4547 | xfs_fsblock_t *firstblock, /* first allocated block | |
4548 | controls a.g. for allocs */ | |
4549 | xfs_extlen_t total, /* total blocks needed */ | |
4550 | struct xfs_bmbt_irec *mval, /* output: map values */ | |
4551 | int *nmap, /* i/o: mval size/count */ | |
f33cea1a | 4552 | struct xfs_defer_ops *dfops) /* i/o: list extents to free */ |
a2ceac1f DC |
4553 | { |
4554 | struct xfs_mount *mp = ip->i_mount; | |
4555 | struct xfs_ifork *ifp; | |
389b3b07 | 4556 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
a2ceac1f DC |
4557 | xfs_fileoff_t end; /* end of mapped file region */ |
4558 | int eof; /* after the end of extents */ | |
4559 | int error; /* error return */ | |
4560 | int n; /* current extent index */ | |
4561 | xfs_fileoff_t obno; /* old block number (offset) */ | |
4562 | int whichfork; /* data or attr fork */ | |
4563 | char inhole; /* current location is hole in file */ | |
4564 | char wasdelay; /* old extent was delayed */ | |
4565 | ||
4566 | #ifdef DEBUG | |
4567 | xfs_fileoff_t orig_bno; /* original block number value */ | |
4568 | int orig_flags; /* original flags arg value */ | |
4569 | xfs_filblks_t orig_len; /* original value of len arg */ | |
4570 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ | |
4571 | int orig_nmap; /* original value of *nmap */ | |
4572 | ||
4573 | orig_bno = bno; | |
4574 | orig_len = len; | |
4575 | orig_flags = flags; | |
4576 | orig_mval = mval; | |
4577 | orig_nmap = *nmap; | |
4578 | #endif | |
1277a5e0 | 4579 | whichfork = xfs_bmapi_whichfork(flags); |
a2ceac1f DC |
4580 | |
4581 | ASSERT(*nmap >= 1); | |
4582 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); | |
4583 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); | |
4584 | ASSERT(tp != NULL); | |
4585 | ASSERT(len > 0); | |
3f17ed4b | 4586 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
ff105f75 | 4587 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
34621a47 DW |
4588 | ASSERT(!(flags & XFS_BMAPI_REMAP) || whichfork == XFS_DATA_FORK); |
4589 | ASSERT(!(flags & XFS_BMAPI_PREALLOC) || !(flags & XFS_BMAPI_REMAP)); | |
4590 | ASSERT(!(flags & XFS_BMAPI_CONVERT) || !(flags & XFS_BMAPI_REMAP)); | |
1277a5e0 DW |
4591 | ASSERT(!(flags & XFS_BMAPI_PREALLOC) || whichfork != XFS_COW_FORK); |
4592 | ASSERT(!(flags & XFS_BMAPI_CONVERT) || whichfork != XFS_COW_FORK); | |
a2ceac1f | 4593 | |
9542ae13 DC |
4594 | /* zeroing is for currently only for data extents, not metadata */ |
4595 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != | |
4596 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); | |
4597 | /* | |
4598 | * we can allocate unwritten extents or pre-zero allocated blocks, | |
4599 | * but it makes no sense to do both at once. This would result in | |
4600 | * zeroing the unwritten extent twice, but it still being an | |
4601 | * unwritten extent.... | |
4602 | */ | |
4603 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != | |
4604 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); | |
4605 | ||
a2ceac1f DC |
4606 | if (unlikely(XFS_TEST_ERROR( |
4607 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && | |
3f17ed4b | 4608 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
a2ceac1f DC |
4609 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
4610 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); | |
12b53197 | 4611 | return -EFSCORRUPTED; |
a2ceac1f DC |
4612 | } |
4613 | ||
4614 | if (XFS_FORCED_SHUTDOWN(mp)) | |
12b53197 | 4615 | return -EIO; |
a2ceac1f DC |
4616 | |
4617 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
4618 | ||
79896434 | 4619 | XFS_STATS_INC(mp, xs_blk_mapw); |
a2ceac1f | 4620 | |
a2ceac1f DC |
4621 | if (*firstblock == NULLFSBLOCK) { |
4622 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) | |
4623 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; | |
4624 | else | |
4625 | bma.minleft = 1; | |
4626 | } else { | |
4627 | bma.minleft = 0; | |
4628 | } | |
4629 | ||
4630 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { | |
4631 | error = xfs_iread_extents(tp, ip, whichfork); | |
4632 | if (error) | |
4633 | goto error0; | |
4634 | } | |
4635 | ||
4636 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got, | |
4637 | &bma.prev); | |
4638 | n = 0; | |
4639 | end = bno + len; | |
4640 | obno = bno; | |
4641 | ||
4642 | bma.tp = tp; | |
4643 | bma.ip = ip; | |
4644 | bma.total = total; | |
1fccd5c8 | 4645 | bma.datatype = 0; |
f33cea1a | 4646 | bma.dfops = dfops; |
a2ceac1f DC |
4647 | bma.firstblock = firstblock; |
4648 | ||
4649 | while (bno < end && n < *nmap) { | |
4650 | inhole = eof || bma.got.br_startoff > bno; | |
4651 | wasdelay = !inhole && isnullstartblock(bma.got.br_startblock); | |
4652 | ||
34621a47 DW |
4653 | /* |
4654 | * Make sure we only reflink into a hole. | |
4655 | */ | |
4656 | if (flags & XFS_BMAPI_REMAP) | |
4657 | ASSERT(inhole); | |
1277a5e0 DW |
4658 | if (flags & XFS_BMAPI_COWFORK) |
4659 | ASSERT(!inhole); | |
34621a47 | 4660 | |
2bd0ea18 | 4661 | /* |
a2ceac1f DC |
4662 | * First, deal with the hole before the allocated space |
4663 | * that we found, if any. | |
2bd0ea18 | 4664 | */ |
a2ceac1f DC |
4665 | if (inhole || wasdelay) { |
4666 | bma.eof = eof; | |
4667 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); | |
4668 | bma.wasdel = wasdelay; | |
4669 | bma.offset = bno; | |
4670 | bma.flags = flags; | |
4671 | ||
2bd0ea18 | 4672 | /* |
a2ceac1f DC |
4673 | * There's a 32/64 bit type mismatch between the |
4674 | * allocation length request (which can be 64 bits in | |
4675 | * length) and the bma length request, which is | |
4676 | * xfs_extlen_t and therefore 32 bits. Hence we have to | |
4677 | * check for 32-bit overflows and handle them here. | |
2bd0ea18 | 4678 | */ |
a2ceac1f DC |
4679 | if (len > (xfs_filblks_t)MAXEXTLEN) |
4680 | bma.length = MAXEXTLEN; | |
4681 | else | |
4682 | bma.length = len; | |
4683 | ||
4684 | ASSERT(len > 0); | |
4685 | ASSERT(bma.length > 0); | |
4686 | error = xfs_bmapi_allocate(&bma); | |
2bd0ea18 NS |
4687 | if (error) |
4688 | goto error0; | |
a2ceac1f DC |
4689 | if (bma.blkno == NULLFSBLOCK) |
4690 | break; | |
10e65503 DW |
4691 | |
4692 | /* | |
4693 | * If this is a CoW allocation, record the data in | |
4694 | * the refcount btree for orphan recovery. | |
4695 | */ | |
4696 | if (whichfork == XFS_COW_FORK) { | |
4697 | error = xfs_refcount_alloc_cow_extent(mp, dfops, | |
4698 | bma.blkno, bma.length); | |
4699 | if (error) | |
4700 | goto error0; | |
4701 | } | |
2bd0ea18 NS |
4702 | } |
4703 | ||
a2ceac1f DC |
4704 | /* Deal with the allocated space we found. */ |
4705 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, | |
4706 | end, n, flags); | |
4707 | ||
4708 | /* Execute unwritten extent conversion if necessary */ | |
4709 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); | |
12b53197 | 4710 | if (error == -EAGAIN) |
a2ceac1f DC |
4711 | continue; |
4712 | if (error) | |
4713 | goto error0; | |
4714 | ||
4715 | /* update the extent map to return */ | |
4716 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); | |
4717 | ||
2bd0ea18 NS |
4718 | /* |
4719 | * If we're done, stop now. Stop when we've allocated | |
4720 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise | |
4721 | * the transaction may get too big. | |
4722 | */ | |
a2ceac1f | 4723 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
2bd0ea18 | 4724 | break; |
a2ceac1f DC |
4725 | |
4726 | /* Else go on to the next record. */ | |
4727 | bma.prev = bma.got; | |
4728 | if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) { | |
4729 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx), | |
4730 | &bma.got); | |
4731 | } else | |
2bd0ea18 | 4732 | eof = 1; |
2bd0ea18 | 4733 | } |
2bd0ea18 | 4734 | *nmap = n; |
a2ceac1f | 4735 | |
2bd0ea18 NS |
4736 | /* |
4737 | * Transform from btree to extents, give it cur. | |
4738 | */ | |
a2ceac1f DC |
4739 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
4740 | int tmp_logflags = 0; | |
4741 | ||
4742 | ASSERT(bma.cur); | |
4743 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, | |
4ca431fc | 4744 | &tmp_logflags, whichfork); |
a2ceac1f | 4745 | bma.logflags |= tmp_logflags; |
2bd0ea18 NS |
4746 | if (error) |
4747 | goto error0; | |
4748 | } | |
a2ceac1f | 4749 | |
2bd0ea18 | 4750 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
a2ceac1f DC |
4751 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
4752 | XFS_IFORK_MAXEXT(ip, whichfork)); | |
2bd0ea18 | 4753 | error = 0; |
2bd0ea18 NS |
4754 | error0: |
4755 | /* | |
4756 | * Log everything. Do this after conversion, there's no point in | |
5e656dbb | 4757 | * logging the extent records if we've converted to btree format. |
2bd0ea18 | 4758 | */ |
a2ceac1f | 4759 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
2bd0ea18 | 4760 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
a2ceac1f DC |
4761 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
4762 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && | |
2bd0ea18 | 4763 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
a2ceac1f | 4764 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
2bd0ea18 NS |
4765 | /* |
4766 | * Log whatever the flags say, even if error. Otherwise we might miss | |
4767 | * detecting a case where the data is changed, there's an error, | |
4768 | * and it's not logged so we don't shutdown when we should. | |
4769 | */ | |
a2ceac1f DC |
4770 | if (bma.logflags) |
4771 | xfs_trans_log_inode(tp, ip, bma.logflags); | |
4772 | ||
4773 | if (bma.cur) { | |
2bd0ea18 NS |
4774 | if (!error) { |
4775 | ASSERT(*firstblock == NULLFSBLOCK || | |
eae766ca NS |
4776 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
4777 | XFS_FSB_TO_AGNO(mp, | |
a2ceac1f | 4778 | bma.cur->bc_private.b.firstblock) || |
f33cea1a | 4779 | (dfops->dop_low && |
eae766ca NS |
4780 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
4781 | XFS_FSB_TO_AGNO(mp, | |
a2ceac1f DC |
4782 | bma.cur->bc_private.b.firstblock))); |
4783 | *firstblock = bma.cur->bc_private.b.firstblock; | |
2bd0ea18 | 4784 | } |
a2ceac1f | 4785 | xfs_btree_del_cursor(bma.cur, |
2bd0ea18 NS |
4786 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
4787 | } | |
4788 | if (!error) | |
4789 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, | |
4790 | orig_nmap, *nmap); | |
4791 | return error; | |
4792 | } | |
4793 | ||
01d1b786 BF |
4794 | /* |
4795 | * When a delalloc extent is split (e.g., due to a hole punch), the original | |
4796 | * indlen reservation must be shared across the two new extents that are left | |
4797 | * behind. | |
4798 | * | |
4799 | * Given the original reservation and the worst case indlen for the two new | |
4800 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original | |
731ccdf9 BF |
4801 | * reservation fairly across the two new extents. If necessary, steal available |
4802 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if | |
4803 | * ores == 1). The number of stolen blocks is returned. The availability and | |
4804 | * subsequent accounting of stolen blocks is the responsibility of the caller. | |
01d1b786 | 4805 | */ |
731ccdf9 | 4806 | static xfs_filblks_t |
01d1b786 BF |
4807 | xfs_bmap_split_indlen( |
4808 | xfs_filblks_t ores, /* original res. */ | |
4809 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ | |
731ccdf9 BF |
4810 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
4811 | xfs_filblks_t avail) /* stealable blocks */ | |
01d1b786 BF |
4812 | { |
4813 | xfs_filblks_t len1 = *indlen1; | |
4814 | xfs_filblks_t len2 = *indlen2; | |
4815 | xfs_filblks_t nres = len1 + len2; /* new total res. */ | |
731ccdf9 BF |
4816 | xfs_filblks_t stolen = 0; |
4817 | ||
4818 | /* | |
4819 | * Steal as many blocks as we can to try and satisfy the worst case | |
4820 | * indlen for both new extents. | |
4821 | */ | |
4822 | while (nres > ores && avail) { | |
4823 | nres--; | |
4824 | avail--; | |
4825 | stolen++; | |
4826 | } | |
01d1b786 BF |
4827 | |
4828 | /* | |
731ccdf9 BF |
4829 | * The only blocks available are those reserved for the original |
4830 | * extent and what we can steal from the extent being removed. | |
4831 | * If this still isn't enough to satisfy the combined | |
4832 | * requirements for the two new extents, skim blocks off of each | |
4833 | * of the new reservations until they match what is available. | |
01d1b786 BF |
4834 | */ |
4835 | while (nres > ores) { | |
4836 | if (len1) { | |
4837 | len1--; | |
4838 | nres--; | |
4839 | } | |
4840 | if (nres == ores) | |
4841 | break; | |
4842 | if (len2) { | |
4843 | len2--; | |
4844 | nres--; | |
4845 | } | |
4846 | } | |
4847 | ||
4848 | *indlen1 = len1; | |
4849 | *indlen2 = len2; | |
731ccdf9 BF |
4850 | |
4851 | return stolen; | |
01d1b786 BF |
4852 | } |
4853 | ||
2bd0ea18 | 4854 | /* |
49f693fa DC |
4855 | * Called by xfs_bmapi to update file extent records and the btree |
4856 | * after removing space (or undoing a delayed allocation). | |
2bd0ea18 | 4857 | */ |
49f693fa DC |
4858 | STATIC int /* error */ |
4859 | xfs_bmap_del_extent( | |
4860 | xfs_inode_t *ip, /* incore inode pointer */ | |
4861 | xfs_trans_t *tp, /* current transaction pointer */ | |
4862 | xfs_extnum_t *idx, /* extent number to update/delete */ | |
f33cea1a | 4863 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
49f693fa DC |
4864 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
4865 | xfs_bmbt_irec_t *del, /* data to remove from extents */ | |
4866 | int *logflagsp, /* inode logging flags */ | |
36b16da8 DW |
4867 | int whichfork, /* data or attr fork */ |
4868 | int bflags) /* bmapi flags */ | |
2bd0ea18 | 4869 | { |
49f693fa DC |
4870 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
4871 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ | |
4872 | xfs_fsblock_t del_endblock=0; /* first block past del */ | |
4873 | xfs_fileoff_t del_endoff; /* first offset past del */ | |
4874 | int delay; /* current block is delayed allocated */ | |
4875 | int do_fx; /* free extent at end of routine */ | |
4876 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ | |
4877 | int error; /* error return value */ | |
4878 | int flags; /* inode logging flags */ | |
4879 | xfs_bmbt_irec_t got; /* current extent entry */ | |
4880 | xfs_fileoff_t got_endoff; /* first offset past got */ | |
4881 | int i; /* temp state */ | |
4882 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
4883 | xfs_mount_t *mp; /* mount structure */ | |
4884 | xfs_filblks_t nblks; /* quota/sb block count */ | |
4885 | xfs_bmbt_irec_t new; /* new record to be inserted */ | |
4886 | /* REFERENCED */ | |
4887 | uint qfield; /* quota field to update */ | |
4888 | xfs_filblks_t temp; /* for indirect length calculations */ | |
4889 | xfs_filblks_t temp2; /* for indirect length calculations */ | |
4890 | int state = 0; | |
a2ceac1f | 4891 | |
79896434 BD |
4892 | mp = ip->i_mount; |
4893 | XFS_STATS_INC(mp, xs_del_exlist); | |
a2ceac1f | 4894 | |
49f693fa DC |
4895 | if (whichfork == XFS_ATTR_FORK) |
4896 | state |= BMAP_ATTRFORK; | |
cb8a004a DW |
4897 | else if (whichfork == XFS_COW_FORK) |
4898 | state |= BMAP_COWFORK; | |
56b2de80 | 4899 | |
49f693fa DC |
4900 | ifp = XFS_IFORK_PTR(ip, whichfork); |
4901 | ASSERT((*idx >= 0) && (*idx < ifp->if_bytes / | |
4902 | (uint)sizeof(xfs_bmbt_rec_t))); | |
4903 | ASSERT(del->br_blockcount > 0); | |
4904 | ep = xfs_iext_get_ext(ifp, *idx); | |
4905 | xfs_bmbt_get_all(ep, &got); | |
4906 | ASSERT(got.br_startoff <= del->br_startoff); | |
4907 | del_endoff = del->br_startoff + del->br_blockcount; | |
4908 | got_endoff = got.br_startoff + got.br_blockcount; | |
4909 | ASSERT(got_endoff >= del_endoff); | |
4910 | delay = isnullstartblock(got.br_startblock); | |
4911 | ASSERT(isnullstartblock(del->br_startblock) == delay); | |
4912 | flags = 0; | |
4913 | qfield = 0; | |
4914 | error = 0; | |
2bd0ea18 | 4915 | /* |
49f693fa | 4916 | * If deleting a real allocation, must free up the disk space. |
2bd0ea18 | 4917 | */ |
49f693fa DC |
4918 | if (!delay) { |
4919 | flags = XFS_ILOG_CORE; | |
a2ceac1f | 4920 | /* |
49f693fa | 4921 | * Realtime allocation. Free it and record di_nblocks update. |
a2ceac1f | 4922 | */ |
49f693fa DC |
4923 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
4924 | xfs_fsblock_t bno; | |
4925 | xfs_filblks_t len; | |
a2ceac1f | 4926 | |
49f693fa DC |
4927 | ASSERT(do_mod(del->br_blockcount, |
4928 | mp->m_sb.sb_rextsize) == 0); | |
4929 | ASSERT(do_mod(del->br_startblock, | |
4930 | mp->m_sb.sb_rextsize) == 0); | |
4931 | bno = del->br_startblock; | |
4932 | len = del->br_blockcount; | |
4933 | do_div(bno, mp->m_sb.sb_rextsize); | |
4934 | do_div(len, mp->m_sb.sb_rextsize); | |
4935 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); | |
4936 | if (error) | |
4937 | goto done; | |
4938 | do_fx = 0; | |
4939 | nblks = len * mp->m_sb.sb_rextsize; | |
4940 | qfield = XFS_TRANS_DQ_RTBCOUNT; | |
2bd0ea18 NS |
4941 | } |
4942 | /* | |
49f693fa | 4943 | * Ordinary allocation. |
2bd0ea18 | 4944 | */ |
49f693fa DC |
4945 | else { |
4946 | do_fx = 1; | |
4947 | nblks = del->br_blockcount; | |
4948 | qfield = XFS_TRANS_DQ_BCOUNT; | |
4949 | } | |
2bd0ea18 | 4950 | /* |
49f693fa | 4951 | * Set up del_endblock and cur for later. |
2bd0ea18 | 4952 | */ |
49f693fa DC |
4953 | del_endblock = del->br_startblock + del->br_blockcount; |
4954 | if (cur) { | |
4955 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, | |
4956 | got.br_startblock, got.br_blockcount, | |
4957 | &i))) | |
4958 | goto done; | |
19ebedcf | 4959 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
2bd0ea18 | 4960 | } |
49f693fa DC |
4961 | da_old = da_new = 0; |
4962 | } else { | |
4963 | da_old = startblockval(got.br_startblock); | |
4964 | da_new = 0; | |
4965 | nblks = 0; | |
4966 | do_fx = 0; | |
4967 | } | |
85aec44f | 4968 | |
49f693fa DC |
4969 | /* |
4970 | * Set flag value to use in switch statement. | |
4971 | * Left-contig is 2, right-contig is 1. | |
4972 | */ | |
4973 | switch (((got.br_startoff == del->br_startoff) << 1) | | |
4974 | (got_endoff == del_endoff)) { | |
4975 | case 3: | |
4976 | /* | |
4977 | * Matches the whole extent. Delete the entry. | |
4978 | */ | |
d35205ba | 4979 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
49f693fa DC |
4980 | xfs_iext_remove(ip, *idx, 1, |
4981 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); | |
4982 | --*idx; | |
4983 | if (delay) | |
4984 | break; | |
4985 | ||
4986 | XFS_IFORK_NEXT_SET(ip, whichfork, | |
4987 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); | |
4988 | flags |= XFS_ILOG_CORE; | |
4989 | if (!cur) { | |
4990 | flags |= xfs_ilog_fext(whichfork); | |
4991 | break; | |
2bd0ea18 | 4992 | } |
49f693fa DC |
4993 | if ((error = xfs_btree_delete(cur, &i))) |
4994 | goto done; | |
19ebedcf | 4995 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa | 4996 | break; |
399ab595 | 4997 | |
49f693fa | 4998 | case 2: |
2bd0ea18 | 4999 | /* |
49f693fa | 5000 | * Deleting the first part of the extent. |
2bd0ea18 | 5001 | */ |
49f693fa DC |
5002 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
5003 | xfs_bmbt_set_startoff(ep, del_endoff); | |
5004 | temp = got.br_blockcount - del->br_blockcount; | |
5005 | xfs_bmbt_set_blockcount(ep, temp); | |
5006 | if (delay) { | |
5007 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), | |
5008 | da_old); | |
5009 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | |
5010 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
5011 | da_new = temp; | |
5012 | break; | |
2bd0ea18 | 5013 | } |
49f693fa DC |
5014 | xfs_bmbt_set_startblock(ep, del_endblock); |
5015 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
5016 | if (!cur) { | |
5017 | flags |= xfs_ilog_fext(whichfork); | |
5018 | break; | |
5019 | } | |
5020 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, | |
5021 | got.br_blockcount - del->br_blockcount, | |
5022 | got.br_state))) | |
5023 | goto done; | |
5024 | break; | |
5025 | ||
5026 | case 1: | |
2bd0ea18 | 5027 | /* |
49f693fa | 5028 | * Deleting the last part of the extent. |
2bd0ea18 | 5029 | */ |
49f693fa DC |
5030 | temp = got.br_blockcount - del->br_blockcount; |
5031 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); | |
5032 | xfs_bmbt_set_blockcount(ep, temp); | |
5033 | if (delay) { | |
5034 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), | |
5035 | da_old); | |
5036 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | |
5037 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
5038 | da_new = temp; | |
5039 | break; | |
5040 | } | |
5041 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); | |
5042 | if (!cur) { | |
5043 | flags |= xfs_ilog_fext(whichfork); | |
5044 | break; | |
5045 | } | |
5046 | if ((error = xfs_bmbt_update(cur, got.br_startoff, | |
5047 | got.br_startblock, | |
5048 | got.br_blockcount - del->br_blockcount, | |
5049 | got.br_state))) | |
5050 | goto done; | |
5051 | break; | |
5052 | ||
5053 | case 0: | |
5054 | /* | |
5055 | * Deleting the middle of the extent. | |
5056 | */ | |
5057 | temp = del->br_startoff - got.br_startoff; | |
5058 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); | |
5059 | xfs_bmbt_set_blockcount(ep, temp); | |
5060 | new.br_startoff = del_endoff; | |
5061 | temp2 = got_endoff - del_endoff; | |
5062 | new.br_blockcount = temp2; | |
5063 | new.br_state = got.br_state; | |
5064 | if (!delay) { | |
5065 | new.br_startblock = del_endblock; | |
5066 | flags |= XFS_ILOG_CORE; | |
5067 | if (cur) { | |
5068 | if ((error = xfs_bmbt_update(cur, | |
5069 | got.br_startoff, | |
5070 | got.br_startblock, temp, | |
5071 | got.br_state))) | |
5072 | goto done; | |
5073 | if ((error = xfs_btree_increment(cur, 0, &i))) | |
5074 | goto done; | |
5075 | cur->bc_rec.b = new; | |
5076 | error = xfs_btree_insert(cur, &i); | |
12b53197 | 5077 | if (error && error != -ENOSPC) |
49f693fa DC |
5078 | goto done; |
5079 | /* | |
5080 | * If get no-space back from btree insert, | |
5081 | * it tried a split, and we have a zero | |
5082 | * block reservation. | |
5083 | * Fix up our state and return the error. | |
5084 | */ | |
12b53197 | 5085 | if (error == -ENOSPC) { |
49f693fa DC |
5086 | /* |
5087 | * Reset the cursor, don't trust | |
5088 | * it after any insert operation. | |
5089 | */ | |
5090 | if ((error = xfs_bmbt_lookup_eq(cur, | |
5091 | got.br_startoff, | |
5092 | got.br_startblock, | |
5093 | temp, &i))) | |
5094 | goto done; | |
19ebedcf DC |
5095 | XFS_WANT_CORRUPTED_GOTO(mp, |
5096 | i == 1, done); | |
49f693fa DC |
5097 | /* |
5098 | * Update the btree record back | |
5099 | * to the original value. | |
5100 | */ | |
5101 | if ((error = xfs_bmbt_update(cur, | |
5102 | got.br_startoff, | |
5103 | got.br_startblock, | |
5104 | got.br_blockcount, | |
5105 | got.br_state))) | |
5106 | goto done; | |
5107 | /* | |
5108 | * Reset the extent record back | |
5109 | * to the original value. | |
5110 | */ | |
5111 | xfs_bmbt_set_blockcount(ep, | |
5112 | got.br_blockcount); | |
5113 | flags = 0; | |
12b53197 | 5114 | error = -ENOSPC; |
49f693fa DC |
5115 | goto done; |
5116 | } | |
19ebedcf | 5117 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
49f693fa DC |
5118 | } else |
5119 | flags |= xfs_ilog_fext(whichfork); | |
5120 | XFS_IFORK_NEXT_SET(ip, whichfork, | |
5121 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); | |
5122 | } else { | |
731ccdf9 | 5123 | xfs_filblks_t stolen; |
49f693fa | 5124 | ASSERT(whichfork == XFS_DATA_FORK); |
01d1b786 BF |
5125 | |
5126 | /* | |
5127 | * Distribute the original indlen reservation across the | |
731ccdf9 BF |
5128 | * two new extents. Steal blocks from the deleted extent |
5129 | * if necessary. Stealing blocks simply fudges the | |
5130 | * fdblocks accounting in xfs_bunmapi(). | |
01d1b786 BF |
5131 | */ |
5132 | temp = xfs_bmap_worst_indlen(ip, got.br_blockcount); | |
5133 | temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount); | |
731ccdf9 BF |
5134 | stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2, |
5135 | del->br_blockcount); | |
5136 | da_new = temp + temp2 - stolen; | |
5137 | del->br_blockcount -= stolen; | |
01d1b786 BF |
5138 | |
5139 | /* | |
731ccdf9 BF |
5140 | * Set the reservation for each extent. Warn if either |
5141 | * is zero as this can lead to delalloc problems. | |
01d1b786 | 5142 | */ |
731ccdf9 | 5143 | WARN_ON_ONCE(!temp || !temp2); |
49f693fa | 5144 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
49f693fa | 5145 | new.br_startblock = nullstartblock((int)temp2); |
2bd0ea18 | 5146 | } |
49f693fa DC |
5147 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
5148 | xfs_iext_insert(ip, *idx + 1, 1, &new, state); | |
5149 | ++*idx; | |
5150 | break; | |
2bd0ea18 | 5151 | } |
d7f80320 DW |
5152 | |
5153 | /* remove reverse mapping */ | |
5154 | if (!delay) { | |
5155 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del); | |
5156 | if (error) | |
5157 | goto done; | |
5158 | } | |
5159 | ||
2bd0ea18 | 5160 | /* |
49f693fa | 5161 | * If we need to, add to list of extents to delete. |
2bd0ea18 | 5162 | */ |
36b16da8 | 5163 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
cfe32f0d DW |
5164 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
5165 | error = xfs_refcount_decrease_extent(mp, dfops, del); | |
5166 | if (error) | |
5167 | goto done; | |
5168 | } else | |
5169 | xfs_bmap_add_free(mp, dfops, del->br_startblock, | |
5170 | del->br_blockcount, NULL); | |
5171 | } | |
5172 | ||
2bd0ea18 | 5173 | /* |
49f693fa | 5174 | * Adjust inode # blocks in the file. |
2bd0ea18 | 5175 | */ |
49f693fa DC |
5176 | if (nblks) |
5177 | ip->i_d.di_nblocks -= nblks; | |
2bd0ea18 | 5178 | /* |
49f693fa | 5179 | * Adjust quota data. |
2bd0ea18 | 5180 | */ |
36b16da8 | 5181 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
49f693fa DC |
5182 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
5183 | ||
2bd0ea18 | 5184 | /* |
49f693fa DC |
5185 | * Account for change in delayed indirect blocks. |
5186 | * Nothing to do for disk quota accounting here. | |
2bd0ea18 | 5187 | */ |
49f693fa | 5188 | ASSERT(da_old >= da_new); |
19ebedcf DC |
5189 | if (da_old > da_new) |
5190 | xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false); | |
49f693fa DC |
5191 | done: |
5192 | *logflagsp = flags; | |
2bd0ea18 NS |
5193 | return error; |
5194 | } | |
5195 | ||
d35205ba DW |
5196 | /* Remove an extent from the CoW fork. Similar to xfs_bmap_del_extent. */ |
5197 | int | |
5198 | xfs_bunmapi_cow( | |
5199 | struct xfs_inode *ip, | |
5200 | struct xfs_bmbt_irec *del) | |
5201 | { | |
5202 | xfs_filblks_t da_new; | |
5203 | xfs_filblks_t da_old; | |
5204 | xfs_fsblock_t del_endblock = 0; | |
5205 | xfs_fileoff_t del_endoff; | |
5206 | int delay; | |
5207 | struct xfs_bmbt_rec_host *ep; | |
5208 | int error; | |
5209 | struct xfs_bmbt_irec got; | |
5210 | xfs_fileoff_t got_endoff; | |
5211 | struct xfs_ifork *ifp; | |
5212 | struct xfs_mount *mp; | |
5213 | xfs_filblks_t nblks; | |
5214 | struct xfs_bmbt_irec new; | |
5215 | /* REFERENCED */ | |
5216 | uint qfield; | |
5217 | xfs_filblks_t temp; | |
5218 | xfs_filblks_t temp2; | |
5219 | int state = BMAP_COWFORK; | |
5220 | int eof; | |
5221 | xfs_extnum_t eidx; | |
5222 | ||
5223 | mp = ip->i_mount; | |
5224 | XFS_STATS_INC(mp, xs_del_exlist); | |
5225 | ||
5226 | ep = xfs_bmap_search_extents(ip, del->br_startoff, XFS_COW_FORK, &eof, | |
5227 | &eidx, &got, &new); | |
5228 | ||
dd84fc92 | 5229 | ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
d35205ba DW |
5230 | ASSERT((eidx >= 0) && (eidx < ifp->if_bytes / |
5231 | (uint)sizeof(xfs_bmbt_rec_t))); | |
5232 | ASSERT(del->br_blockcount > 0); | |
5233 | ASSERT(got.br_startoff <= del->br_startoff); | |
5234 | del_endoff = del->br_startoff + del->br_blockcount; | |
5235 | got_endoff = got.br_startoff + got.br_blockcount; | |
5236 | ASSERT(got_endoff >= del_endoff); | |
5237 | delay = isnullstartblock(got.br_startblock); | |
5238 | ASSERT(isnullstartblock(del->br_startblock) == delay); | |
5239 | qfield = 0; | |
5240 | error = 0; | |
5241 | /* | |
5242 | * If deleting a real allocation, must free up the disk space. | |
5243 | */ | |
5244 | if (!delay) { | |
5245 | nblks = del->br_blockcount; | |
5246 | qfield = XFS_TRANS_DQ_BCOUNT; | |
5247 | /* | |
5248 | * Set up del_endblock and cur for later. | |
5249 | */ | |
5250 | del_endblock = del->br_startblock + del->br_blockcount; | |
5251 | da_old = da_new = 0; | |
5252 | } else { | |
5253 | da_old = startblockval(got.br_startblock); | |
5254 | da_new = 0; | |
5255 | nblks = 0; | |
5256 | } | |
5257 | qfield = qfield; | |
5258 | nblks = nblks; | |
5259 | ||
5260 | /* | |
5261 | * Set flag value to use in switch statement. | |
5262 | * Left-contig is 2, right-contig is 1. | |
5263 | */ | |
5264 | switch (((got.br_startoff == del->br_startoff) << 1) | | |
5265 | (got_endoff == del_endoff)) { | |
5266 | case 3: | |
5267 | /* | |
5268 | * Matches the whole extent. Delete the entry. | |
5269 | */ | |
5270 | xfs_iext_remove(ip, eidx, 1, BMAP_COWFORK); | |
5271 | --eidx; | |
5272 | break; | |
5273 | ||
5274 | case 2: | |
5275 | /* | |
5276 | * Deleting the first part of the extent. | |
5277 | */ | |
5278 | trace_xfs_bmap_pre_update(ip, eidx, state, _THIS_IP_); | |
5279 | xfs_bmbt_set_startoff(ep, del_endoff); | |
5280 | temp = got.br_blockcount - del->br_blockcount; | |
5281 | xfs_bmbt_set_blockcount(ep, temp); | |
5282 | if (delay) { | |
5283 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), | |
5284 | da_old); | |
5285 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | |
5286 | trace_xfs_bmap_post_update(ip, eidx, state, _THIS_IP_); | |
5287 | da_new = temp; | |
5288 | break; | |
5289 | } | |
5290 | xfs_bmbt_set_startblock(ep, del_endblock); | |
5291 | trace_xfs_bmap_post_update(ip, eidx, state, _THIS_IP_); | |
5292 | break; | |
5293 | ||
5294 | case 1: | |
5295 | /* | |
5296 | * Deleting the last part of the extent. | |
5297 | */ | |
5298 | temp = got.br_blockcount - del->br_blockcount; | |
5299 | trace_xfs_bmap_pre_update(ip, eidx, state, _THIS_IP_); | |
5300 | xfs_bmbt_set_blockcount(ep, temp); | |
5301 | if (delay) { | |
5302 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), | |
5303 | da_old); | |
5304 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | |
5305 | trace_xfs_bmap_post_update(ip, eidx, state, _THIS_IP_); | |
5306 | da_new = temp; | |
5307 | break; | |
5308 | } | |
5309 | trace_xfs_bmap_post_update(ip, eidx, state, _THIS_IP_); | |
5310 | break; | |
5311 | ||
5312 | case 0: | |
5313 | /* | |
5314 | * Deleting the middle of the extent. | |
5315 | */ | |
5316 | temp = del->br_startoff - got.br_startoff; | |
5317 | trace_xfs_bmap_pre_update(ip, eidx, state, _THIS_IP_); | |
5318 | xfs_bmbt_set_blockcount(ep, temp); | |
5319 | new.br_startoff = del_endoff; | |
5320 | temp2 = got_endoff - del_endoff; | |
5321 | new.br_blockcount = temp2; | |
5322 | new.br_state = got.br_state; | |
5323 | if (!delay) { | |
5324 | new.br_startblock = del_endblock; | |
5325 | } else { | |
5326 | temp = xfs_bmap_worst_indlen(ip, temp); | |
5327 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); | |
5328 | temp2 = xfs_bmap_worst_indlen(ip, temp2); | |
5329 | new.br_startblock = nullstartblock((int)temp2); | |
5330 | da_new = temp + temp2; | |
5331 | while (da_new > da_old) { | |
5332 | if (temp) { | |
5333 | temp--; | |
5334 | da_new--; | |
5335 | xfs_bmbt_set_startblock(ep, | |
5336 | nullstartblock((int)temp)); | |
5337 | } | |
5338 | if (da_new == da_old) | |
5339 | break; | |
5340 | if (temp2) { | |
5341 | temp2--; | |
5342 | da_new--; | |
5343 | new.br_startblock = | |
5344 | nullstartblock((int)temp2); | |
5345 | } | |
5346 | } | |
5347 | } | |
5348 | trace_xfs_bmap_post_update(ip, eidx, state, _THIS_IP_); | |
5349 | xfs_iext_insert(ip, eidx + 1, 1, &new, state); | |
5350 | ++eidx; | |
5351 | break; | |
5352 | } | |
5353 | ||
5354 | /* | |
5355 | * Account for change in delayed indirect blocks. | |
5356 | * Nothing to do for disk quota accounting here. | |
5357 | */ | |
5358 | ASSERT(da_old >= da_new); | |
5359 | if (da_old > da_new) | |
5360 | xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false); | |
5361 | ||
5362 | return error; | |
5363 | } | |
5364 | ||
2bd0ea18 | 5365 | /* |
49f693fa DC |
5366 | * Unmap (remove) blocks from a file. |
5367 | * If nexts is nonzero then the number of extents to remove is limited to | |
5368 | * that value. If not all extents in the block range can be removed then | |
5369 | * *done is set. | |
2bd0ea18 | 5370 | */ |
49f693fa | 5371 | int /* error */ |
3d36acda | 5372 | __xfs_bunmapi( |
49f693fa DC |
5373 | xfs_trans_t *tp, /* transaction pointer */ |
5374 | struct xfs_inode *ip, /* incore inode */ | |
5375 | xfs_fileoff_t bno, /* starting offset to unmap */ | |
3d36acda | 5376 | xfs_filblks_t *rlen, /* i/o: amount remaining */ |
49f693fa DC |
5377 | int flags, /* misc flags */ |
5378 | xfs_extnum_t nexts, /* number of extents max */ | |
5379 | xfs_fsblock_t *firstblock, /* first allocated block | |
5380 | controls a.g. for allocs */ | |
3d36acda | 5381 | struct xfs_defer_ops *dfops) /* i/o: deferred updates */ |
2bd0ea18 | 5382 | { |
49f693fa DC |
5383 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
5384 | xfs_bmbt_irec_t del; /* extent being deleted */ | |
5385 | int eof; /* is deleting at eof */ | |
5386 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ | |
5387 | int error; /* error return value */ | |
5388 | xfs_extnum_t extno; /* extent number in list */ | |
5389 | xfs_bmbt_irec_t got; /* current extent record */ | |
5390 | xfs_ifork_t *ifp; /* inode fork pointer */ | |
5391 | int isrt; /* freeing in rt area */ | |
5392 | xfs_extnum_t lastx; /* last extent index used */ | |
5393 | int logflags; /* transaction logging flags */ | |
5394 | xfs_extlen_t mod; /* rt extent offset */ | |
5395 | xfs_mount_t *mp; /* mount structure */ | |
5396 | xfs_extnum_t nextents; /* number of file extents */ | |
5397 | xfs_bmbt_irec_t prev; /* previous extent record */ | |
5398 | xfs_fileoff_t start; /* first file offset deleted */ | |
5399 | int tmp_logflags; /* partial logging flags */ | |
5400 | int wasdel; /* was a delayed alloc extent */ | |
5401 | int whichfork; /* data or attribute fork */ | |
5402 | xfs_fsblock_t sum; | |
3d36acda | 5403 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
2bd0ea18 | 5404 | |
49f693fa | 5405 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
a2ceac1f | 5406 | |
cb8a004a DW |
5407 | whichfork = xfs_bmapi_whichfork(flags); |
5408 | ASSERT(whichfork != XFS_COW_FORK); | |
a2ceac1f | 5409 | ifp = XFS_IFORK_PTR(ip, whichfork); |
49f693fa DC |
5410 | if (unlikely( |
5411 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && | |
5412 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { | |
5413 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, | |
5414 | ip->i_mount); | |
12b53197 | 5415 | return -EFSCORRUPTED; |
49f693fa DC |
5416 | } |
5417 | mp = ip->i_mount; | |
5418 | if (XFS_FORCED_SHUTDOWN(mp)) | |
12b53197 | 5419 | return -EIO; |
56b2de80 | 5420 | |
ff105f75 | 5421 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
49f693fa DC |
5422 | ASSERT(len > 0); |
5423 | ASSERT(nexts >= 0); | |
56b2de80 | 5424 | |
49f693fa DC |
5425 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
5426 | (error = xfs_iread_extents(tp, ip, whichfork))) | |
5427 | return error; | |
5428 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); | |
5429 | if (nextents == 0) { | |
3d36acda | 5430 | *rlen = 0; |
49f693fa | 5431 | return 0; |
56b2de80 | 5432 | } |
79896434 | 5433 | XFS_STATS_INC(mp, xs_blk_unmap); |
49f693fa DC |
5434 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
5435 | start = bno; | |
5436 | bno = start + len - 1; | |
5437 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, | |
5438 | &prev); | |
a2ceac1f DC |
5439 | |
5440 | /* | |
49f693fa DC |
5441 | * Check to see if the given block number is past the end of the |
5442 | * file, back up to the last block if so... | |
56b2de80 | 5443 | */ |
49f693fa DC |
5444 | if (eof) { |
5445 | ep = xfs_iext_get_ext(ifp, --lastx); | |
5446 | xfs_bmbt_get_all(ep, &got); | |
5447 | bno = got.br_startoff + got.br_blockcount - 1; | |
5448 | } | |
5449 | logflags = 0; | |
5450 | if (ifp->if_flags & XFS_IFBROOT) { | |
5451 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); | |
5452 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); | |
5453 | cur->bc_private.b.firstblock = *firstblock; | |
f33cea1a | 5454 | cur->bc_private.b.dfops = dfops; |
49f693fa DC |
5455 | cur->bc_private.b.flags = 0; |
5456 | } else | |
5457 | cur = NULL; | |
a2ceac1f | 5458 | |
49f693fa | 5459 | if (isrt) { |
a2ceac1f | 5460 | /* |
49f693fa | 5461 | * Synchronize by locking the bitmap inode. |
a2ceac1f | 5462 | */ |
a62ed6d3 | 5463 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
49f693fa | 5464 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
a62ed6d3 DW |
5465 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
5466 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); | |
49f693fa | 5467 | } |
a2ceac1f | 5468 | |
49f693fa DC |
5469 | extno = 0; |
5470 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && | |
5471 | (nexts == 0 || extno < nexts)) { | |
a2ceac1f | 5472 | /* |
49f693fa DC |
5473 | * Is the found extent after a hole in which bno lives? |
5474 | * Just back up to the previous extent, if so. | |
a2ceac1f | 5475 | */ |
49f693fa DC |
5476 | if (got.br_startoff > bno) { |
5477 | if (--lastx < 0) | |
5478 | break; | |
5479 | ep = xfs_iext_get_ext(ifp, lastx); | |
5480 | xfs_bmbt_get_all(ep, &got); | |
a2ceac1f | 5481 | } |
49f693fa DC |
5482 | /* |
5483 | * Is the last block of this extent before the range | |
5484 | * we're supposed to delete? If so, we're done. | |
5485 | */ | |
5486 | bno = XFS_FILEOFF_MIN(bno, | |
5487 | got.br_startoff + got.br_blockcount - 1); | |
5488 | if (bno < start) | |
5489 | break; | |
5490 | /* | |
5491 | * Then deal with the (possibly delayed) allocated space | |
5492 | * we found. | |
5493 | */ | |
5494 | ASSERT(ep != NULL); | |
5495 | del = got; | |
5496 | wasdel = isnullstartblock(del.br_startblock); | |
5497 | if (got.br_startoff < start) { | |
5498 | del.br_startoff = start; | |
5499 | del.br_blockcount -= start - got.br_startoff; | |
5500 | if (!wasdel) | |
5501 | del.br_startblock += start - got.br_startoff; | |
5502 | } | |
5503 | if (del.br_startoff + del.br_blockcount > bno + 1) | |
5504 | del.br_blockcount = bno + 1 - del.br_startoff; | |
5505 | sum = del.br_startblock + del.br_blockcount; | |
5506 | if (isrt && | |
5507 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { | |
5508 | /* | |
5509 | * Realtime extent not lined up at the end. | |
5510 | * The extent could have been split into written | |
5511 | * and unwritten pieces, or we could just be | |
5512 | * unmapping part of it. But we can't really | |
5513 | * get rid of part of a realtime extent. | |
5514 | */ | |
5515 | if (del.br_state == XFS_EXT_UNWRITTEN || | |
5516 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { | |
5517 | /* | |
5518 | * This piece is unwritten, or we're not | |
5519 | * using unwritten extents. Skip over it. | |
5520 | */ | |
5521 | ASSERT(bno >= mod); | |
5522 | bno -= mod > del.br_blockcount ? | |
5523 | del.br_blockcount : mod; | |
5524 | if (bno < got.br_startoff) { | |
5525 | if (--lastx >= 0) | |
5526 | xfs_bmbt_get_all(xfs_iext_get_ext( | |
5527 | ifp, lastx), &got); | |
5528 | } | |
5529 | continue; | |
5530 | } | |
5531 | /* | |
5532 | * It's written, turn it unwritten. | |
5533 | * This is better than zeroing it. | |
5534 | */ | |
5535 | ASSERT(del.br_state == XFS_EXT_NORM); | |
0268fdc3 | 5536 | ASSERT(tp->t_blk_res > 0); |
49f693fa DC |
5537 | /* |
5538 | * If this spans a realtime extent boundary, | |
5539 | * chop it back to the start of the one we end at. | |
5540 | */ | |
5541 | if (del.br_blockcount > mod) { | |
5542 | del.br_startoff += del.br_blockcount - mod; | |
5543 | del.br_startblock += del.br_blockcount - mod; | |
5544 | del.br_blockcount = mod; | |
5545 | } | |
5546 | del.br_state = XFS_EXT_UNWRITTEN; | |
5547 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, | |
f33cea1a | 5548 | &lastx, &cur, &del, firstblock, dfops, |
49f693fa DC |
5549 | &logflags); |
5550 | if (error) | |
5551 | goto error0; | |
5552 | goto nodelete; | |
a2ceac1f | 5553 | } |
49f693fa DC |
5554 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
5555 | /* | |
5556 | * Realtime extent is lined up at the end but not | |
5557 | * at the front. We'll get rid of full extents if | |
5558 | * we can. | |
5559 | */ | |
5560 | mod = mp->m_sb.sb_rextsize - mod; | |
5561 | if (del.br_blockcount > mod) { | |
5562 | del.br_blockcount -= mod; | |
5563 | del.br_startoff += mod; | |
5564 | del.br_startblock += mod; | |
5565 | } else if ((del.br_startoff == start && | |
5566 | (del.br_state == XFS_EXT_UNWRITTEN || | |
0268fdc3 | 5567 | tp->t_blk_res == 0)) || |
49f693fa DC |
5568 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
5569 | /* | |
5570 | * Can't make it unwritten. There isn't | |
5571 | * a full extent here so just skip it. | |
5572 | */ | |
5573 | ASSERT(bno >= del.br_blockcount); | |
5574 | bno -= del.br_blockcount; | |
5575 | if (got.br_startoff > bno) { | |
5576 | if (--lastx >= 0) { | |
5577 | ep = xfs_iext_get_ext(ifp, | |
5578 | lastx); | |
5579 | xfs_bmbt_get_all(ep, &got); | |
5580 | } | |
5581 | } | |
5582 | continue; | |
5583 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { | |
5584 | /* | |
5585 | * This one is already unwritten. | |
5586 | * It must have a written left neighbor. | |
5587 | * Unwrite the killed part of that one and | |
5588 | * try again. | |
5589 | */ | |
5590 | ASSERT(lastx > 0); | |
5591 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, | |
5592 | lastx - 1), &prev); | |
5593 | ASSERT(prev.br_state == XFS_EXT_NORM); | |
5594 | ASSERT(!isnullstartblock(prev.br_startblock)); | |
5595 | ASSERT(del.br_startblock == | |
5596 | prev.br_startblock + prev.br_blockcount); | |
5597 | if (prev.br_startoff < start) { | |
5598 | mod = start - prev.br_startoff; | |
5599 | prev.br_blockcount -= mod; | |
5600 | prev.br_startblock += mod; | |
5601 | prev.br_startoff = start; | |
5602 | } | |
5603 | prev.br_state = XFS_EXT_UNWRITTEN; | |
5604 | lastx--; | |
5605 | error = xfs_bmap_add_extent_unwritten_real(tp, | |
5606 | ip, &lastx, &cur, &prev, | |
f33cea1a | 5607 | firstblock, dfops, &logflags); |
49f693fa DC |
5608 | if (error) |
5609 | goto error0; | |
5610 | goto nodelete; | |
5611 | } else { | |
5612 | ASSERT(del.br_state == XFS_EXT_NORM); | |
5613 | del.br_state = XFS_EXT_UNWRITTEN; | |
5614 | error = xfs_bmap_add_extent_unwritten_real(tp, | |
5615 | ip, &lastx, &cur, &del, | |
f33cea1a | 5616 | firstblock, dfops, &logflags); |
49f693fa DC |
5617 | if (error) |
5618 | goto error0; | |
5619 | goto nodelete; | |
5620 | } | |
5621 | } | |
a2ceac1f | 5622 | |
49f693fa DC |
5623 | /* |
5624 | * If it's the case where the directory code is running | |
5625 | * with no block reservation, and the deleted block is in | |
5626 | * the middle of its extent, and the resulting insert | |
5627 | * of an extent would cause transformation to btree format, | |
5628 | * then reject it. The calling code will then swap | |
5629 | * blocks around instead. | |
5630 | * We have to do this now, rather than waiting for the | |
5631 | * conversion to btree format, since the transaction | |
5632 | * will be dirty. | |
5633 | */ | |
0268fdc3 | 5634 | if (!wasdel && tp->t_blk_res == 0 && |
49f693fa DC |
5635 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
5636 | XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */ | |
5637 | XFS_IFORK_MAXEXT(ip, whichfork) && | |
5638 | del.br_startoff > got.br_startoff && | |
5639 | del.br_startoff + del.br_blockcount < | |
5640 | got.br_startoff + got.br_blockcount) { | |
12b53197 | 5641 | error = -ENOSPC; |
49f693fa | 5642 | goto error0; |
a2ceac1f | 5643 | } |
8359e0b9 BF |
5644 | |
5645 | /* | |
5646 | * Unreserve quota and update realtime free space, if | |
5647 | * appropriate. If delayed allocation, update the inode delalloc | |
5648 | * counter now and wait to update the sb counters as | |
5649 | * xfs_bmap_del_extent() might need to borrow some blocks. | |
5650 | */ | |
5651 | if (wasdel) { | |
5652 | ASSERT(startblockval(del.br_startblock) > 0); | |
5653 | if (isrt) { | |
5654 | xfs_filblks_t rtexts; | |
5655 | ||
5656 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); | |
5657 | do_div(rtexts, mp->m_sb.sb_rextsize); | |
5658 | xfs_mod_frextents(mp, (int64_t)rtexts); | |
5659 | (void)xfs_trans_reserve_quota_nblks(NULL, | |
5660 | ip, -((long)del.br_blockcount), 0, | |
5661 | XFS_QMOPT_RES_RTBLKS); | |
5662 | } else { | |
5663 | (void)xfs_trans_reserve_quota_nblks(NULL, | |
5664 | ip, -((long)del.br_blockcount), 0, | |
5665 | XFS_QMOPT_RES_REGBLKS); | |
5666 | } | |
5667 | ip->i_delayed_blks -= del.br_blockcount; | |
5668 | if (cur) | |
5669 | cur->bc_private.b.flags |= | |
5670 | XFS_BTCUR_BPRV_WASDEL; | |
5671 | } else if (cur) | |
5672 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; | |
5673 | ||
f33cea1a | 5674 | error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del, |
36b16da8 | 5675 | &tmp_logflags, whichfork, flags); |
49f693fa DC |
5676 | logflags |= tmp_logflags; |
5677 | if (error) | |
5678 | goto error0; | |
8359e0b9 BF |
5679 | |
5680 | if (!isrt && wasdel) | |
5681 | xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false); | |
5682 | ||
49f693fa DC |
5683 | bno = del.br_startoff - 1; |
5684 | nodelete: | |
a2ceac1f | 5685 | /* |
49f693fa | 5686 | * If not done go on to the next (previous) record. |
a2ceac1f | 5687 | */ |
49f693fa DC |
5688 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
5689 | if (lastx >= 0) { | |
5690 | ep = xfs_iext_get_ext(ifp, lastx); | |
5691 | if (xfs_bmbt_get_startoff(ep) > bno) { | |
5692 | if (--lastx >= 0) | |
5693 | ep = xfs_iext_get_ext(ifp, | |
5694 | lastx); | |
5695 | } | |
5696 | xfs_bmbt_get_all(ep, &got); | |
5697 | } | |
5698 | extno++; | |
a2ceac1f | 5699 | } |
a2ceac1f | 5700 | } |
3d36acda DW |
5701 | if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0) |
5702 | *rlen = 0; | |
5703 | else | |
5704 | *rlen = bno - start + 1; | |
56b2de80 | 5705 | |
49f693fa DC |
5706 | /* |
5707 | * Convert to a btree if necessary. | |
5708 | */ | |
5709 | if (xfs_bmap_needs_btree(ip, whichfork)) { | |
5710 | ASSERT(cur == NULL); | |
f33cea1a | 5711 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, |
49f693fa DC |
5712 | &cur, 0, &tmp_logflags, whichfork); |
5713 | logflags |= tmp_logflags; | |
5714 | if (error) | |
5715 | goto error0; | |
56b2de80 | 5716 | } |
56b2de80 | 5717 | /* |
49f693fa | 5718 | * transform from btree to extents, give it cur |
56b2de80 | 5719 | */ |
49f693fa DC |
5720 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
5721 | ASSERT(cur != NULL); | |
5722 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, | |
5723 | whichfork); | |
5724 | logflags |= tmp_logflags; | |
5725 | if (error) | |
5726 | goto error0; | |
56b2de80 | 5727 | } |
49f693fa DC |
5728 | /* |
5729 | * transform from extents to local? | |
5730 | */ | |
5731 | error = 0; | |
5732 | error0: | |
5733 | /* | |
5734 | * Log everything. Do this after conversion, there's no point in | |
5735 | * logging the extent records if we've converted to btree format. | |
5736 | */ | |
5737 | if ((logflags & xfs_ilog_fext(whichfork)) && | |
5738 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) | |
5739 | logflags &= ~xfs_ilog_fext(whichfork); | |
5740 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && | |
5741 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) | |
5742 | logflags &= ~xfs_ilog_fbroot(whichfork); | |
5743 | /* | |
5744 | * Log inode even in the error case, if the transaction | |
5745 | * is dirty we'll need to shut down the filesystem. | |
5746 | */ | |
5747 | if (logflags) | |
5748 | xfs_trans_log_inode(tp, ip, logflags); | |
5749 | if (cur) { | |
5750 | if (!error) { | |
5751 | *firstblock = cur->bc_private.b.firstblock; | |
5752 | cur->bc_private.b.allocated = 0; | |
56b2de80 | 5753 | } |
49f693fa DC |
5754 | xfs_btree_del_cursor(cur, |
5755 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); | |
56b2de80 | 5756 | } |
49f693fa | 5757 | return error; |
a2ceac1f | 5758 | } |
ff105f75 | 5759 | |
3d36acda DW |
5760 | /* Unmap a range of a file. */ |
5761 | int | |
5762 | xfs_bunmapi( | |
5763 | xfs_trans_t *tp, | |
5764 | struct xfs_inode *ip, | |
5765 | xfs_fileoff_t bno, | |
5766 | xfs_filblks_t len, | |
5767 | int flags, | |
5768 | xfs_extnum_t nexts, | |
5769 | xfs_fsblock_t *firstblock, | |
5770 | struct xfs_defer_ops *dfops, | |
5771 | int *done) | |
5772 | { | |
5773 | int error; | |
5774 | ||
5775 | error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock, | |
5776 | dfops); | |
5777 | *done = (len == 0); | |
5778 | return error; | |
5779 | } | |
5780 | ||
5a35bf2c DC |
5781 | /* |
5782 | * Determine whether an extent shift can be accomplished by a merge with the | |
5783 | * extent that precedes the target hole of the shift. | |
5784 | */ | |
5785 | STATIC bool | |
5786 | xfs_bmse_can_merge( | |
5787 | struct xfs_bmbt_irec *left, /* preceding extent */ | |
5788 | struct xfs_bmbt_irec *got, /* current extent to shift */ | |
5789 | xfs_fileoff_t shift) /* shift fsb */ | |
5790 | { | |
5791 | xfs_fileoff_t startoff; | |
5792 | ||
5793 | startoff = got->br_startoff - shift; | |
5794 | ||
5795 | /* | |
5796 | * The extent, once shifted, must be adjacent in-file and on-disk with | |
5797 | * the preceding extent. | |
5798 | */ | |
5799 | if ((left->br_startoff + left->br_blockcount != startoff) || | |
5800 | (left->br_startblock + left->br_blockcount != got->br_startblock) || | |
5801 | (left->br_state != got->br_state) || | |
5802 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) | |
5803 | return false; | |
5804 | ||
5805 | return true; | |
5806 | } | |
5807 | ||
5808 | /* | |
5809 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding | |
5810 | * hole in the file. If an extent shift would result in the extent being fully | |
5811 | * adjacent to the extent that currently precedes the hole, we can merge with | |
5812 | * the preceding extent rather than do the shift. | |
5813 | * | |
5814 | * This function assumes the caller has verified a shift-by-merge is possible | |
5815 | * with the provided extents via xfs_bmse_can_merge(). | |
5816 | */ | |
5817 | STATIC int | |
5818 | xfs_bmse_merge( | |
5819 | struct xfs_inode *ip, | |
5820 | int whichfork, | |
5821 | xfs_fileoff_t shift, /* shift fsb */ | |
5822 | int current_ext, /* idx of gotp */ | |
5823 | struct xfs_bmbt_rec_host *gotp, /* extent to shift */ | |
5824 | struct xfs_bmbt_rec_host *leftp, /* preceding extent */ | |
5825 | struct xfs_btree_cur *cur, | |
5826 | int *logflags) /* output */ | |
5827 | { | |
5828 | struct xfs_bmbt_irec got; | |
5829 | struct xfs_bmbt_irec left; | |
5830 | xfs_filblks_t blockcount; | |
5831 | int error, i; | |
19ebedcf | 5832 | struct xfs_mount *mp = ip->i_mount; |
5a35bf2c DC |
5833 | |
5834 | xfs_bmbt_get_all(gotp, &got); | |
5835 | xfs_bmbt_get_all(leftp, &left); | |
5836 | blockcount = left.br_blockcount + got.br_blockcount; | |
5837 | ||
5838 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); | |
5839 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); | |
5840 | ASSERT(xfs_bmse_can_merge(&left, &got, shift)); | |
5841 | ||
5842 | /* | |
5843 | * Merge the in-core extents. Note that the host record pointers and | |
5844 | * current_ext index are invalid once the extent has been removed via | |
5845 | * xfs_iext_remove(). | |
5846 | */ | |
5847 | xfs_bmbt_set_blockcount(leftp, blockcount); | |
5848 | xfs_iext_remove(ip, current_ext, 1, 0); | |
5849 | ||
5850 | /* | |
5851 | * Update the on-disk extent count, the btree if necessary and log the | |
5852 | * inode. | |
5853 | */ | |
5854 | XFS_IFORK_NEXT_SET(ip, whichfork, | |
5855 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); | |
5856 | *logflags |= XFS_ILOG_CORE; | |
5857 | if (!cur) { | |
5858 | *logflags |= XFS_ILOG_DEXT; | |
5859 | return 0; | |
5860 | } | |
5861 | ||
5862 | /* lookup and remove the extent to merge */ | |
5863 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, | |
5864 | got.br_blockcount, &i); | |
5865 | if (error) | |
5866 | return error; | |
19ebedcf | 5867 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
5a35bf2c DC |
5868 | |
5869 | error = xfs_btree_delete(cur, &i); | |
5870 | if (error) | |
5871 | return error; | |
19ebedcf | 5872 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
5a35bf2c DC |
5873 | |
5874 | /* lookup and update size of the previous extent */ | |
5875 | error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock, | |
5876 | left.br_blockcount, &i); | |
5877 | if (error) | |
5878 | return error; | |
19ebedcf | 5879 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
5a35bf2c DC |
5880 | |
5881 | left.br_blockcount = blockcount; | |
5882 | ||
5883 | return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock, | |
5884 | left.br_blockcount, left.br_state); | |
5885 | } | |
5886 | ||
5887 | /* | |
5888 | * Shift a single extent. | |
5889 | */ | |
5890 | STATIC int | |
5891 | xfs_bmse_shift_one( | |
5892 | struct xfs_inode *ip, | |
5893 | int whichfork, | |
5894 | xfs_fileoff_t offset_shift_fsb, | |
5895 | int *current_ext, | |
5896 | struct xfs_bmbt_rec_host *gotp, | |
5897 | struct xfs_btree_cur *cur, | |
19ebedcf | 5898 | int *logflags, |
d7f80320 DW |
5899 | enum shift_direction direction, |
5900 | struct xfs_defer_ops *dfops) | |
5a35bf2c DC |
5901 | { |
5902 | struct xfs_ifork *ifp; | |
19ebedcf | 5903 | struct xfs_mount *mp; |
5a35bf2c | 5904 | xfs_fileoff_t startoff; |
19ebedcf | 5905 | struct xfs_bmbt_rec_host *adj_irecp; |
5a35bf2c | 5906 | struct xfs_bmbt_irec got; |
19ebedcf | 5907 | struct xfs_bmbt_irec adj_irec; |
5a35bf2c DC |
5908 | int error; |
5909 | int i; | |
19ebedcf | 5910 | int total_extents; |
5a35bf2c | 5911 | |
19ebedcf | 5912 | mp = ip->i_mount; |
5a35bf2c | 5913 | ifp = XFS_IFORK_PTR(ip, whichfork); |
19ebedcf | 5914 | total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
5a35bf2c DC |
5915 | |
5916 | xfs_bmbt_get_all(gotp, &got); | |
5a35bf2c DC |
5917 | |
5918 | /* delalloc extents should be prevented by caller */ | |
19ebedcf | 5919 | XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock)); |
5a35bf2c | 5920 | |
19ebedcf DC |
5921 | if (direction == SHIFT_LEFT) { |
5922 | startoff = got.br_startoff - offset_shift_fsb; | |
5923 | ||
5924 | /* | |
5925 | * Check for merge if we've got an extent to the left, | |
5926 | * otherwise make sure there's enough room at the start | |
5927 | * of the file for the shift. | |
5928 | */ | |
5929 | if (!*current_ext) { | |
5930 | if (got.br_startoff < offset_shift_fsb) | |
5931 | return -EINVAL; | |
5932 | goto update_current_ext; | |
5933 | } | |
5934 | /* | |
5935 | * grab the left extent and check for a large | |
5936 | * enough hole. | |
5937 | */ | |
5938 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1); | |
5939 | xfs_bmbt_get_all(adj_irecp, &adj_irec); | |
5a35bf2c | 5940 | |
19ebedcf DC |
5941 | if (startoff < |
5942 | adj_irec.br_startoff + adj_irec.br_blockcount) | |
5a35bf2c DC |
5943 | return -EINVAL; |
5944 | ||
5945 | /* check whether to merge the extent or shift it down */ | |
19ebedcf DC |
5946 | if (xfs_bmse_can_merge(&adj_irec, &got, |
5947 | offset_shift_fsb)) { | |
d7f80320 DW |
5948 | error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb, |
5949 | *current_ext, gotp, adj_irecp, | |
5950 | cur, logflags); | |
5951 | if (error) | |
5952 | return error; | |
5953 | adj_irec = got; | |
5954 | goto update_rmap; | |
5a35bf2c | 5955 | } |
19ebedcf DC |
5956 | } else { |
5957 | startoff = got.br_startoff + offset_shift_fsb; | |
5958 | /* nothing to move if this is the last extent */ | |
5959 | if (*current_ext >= (total_extents - 1)) | |
5960 | goto update_current_ext; | |
5961 | /* | |
5962 | * If this is not the last extent in the file, make sure there | |
5963 | * is enough room between current extent and next extent for | |
5964 | * accommodating the shift. | |
5965 | */ | |
5966 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1); | |
5967 | xfs_bmbt_get_all(adj_irecp, &adj_irec); | |
5968 | if (startoff + got.br_blockcount > adj_irec.br_startoff) | |
5969 | return -EINVAL; | |
5970 | /* | |
5971 | * Unlike a left shift (which involves a hole punch), | |
5972 | * a right shift does not modify extent neighbors | |
5973 | * in any way. We should never find mergeable extents | |
5974 | * in this scenario. Check anyways and warn if we | |
5975 | * encounter two extents that could be one. | |
5976 | */ | |
5977 | if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb)) | |
5978 | WARN_ON_ONCE(1); | |
5979 | } | |
5a35bf2c DC |
5980 | /* |
5981 | * Increment the extent index for the next iteration, update the start | |
5982 | * offset of the in-core extent and update the btree if applicable. | |
5983 | */ | |
19ebedcf DC |
5984 | update_current_ext: |
5985 | if (direction == SHIFT_LEFT) | |
5986 | (*current_ext)++; | |
5987 | else | |
5988 | (*current_ext)--; | |
5a35bf2c DC |
5989 | xfs_bmbt_set_startoff(gotp, startoff); |
5990 | *logflags |= XFS_ILOG_CORE; | |
d7f80320 | 5991 | adj_irec = got; |
5a35bf2c DC |
5992 | if (!cur) { |
5993 | *logflags |= XFS_ILOG_DEXT; | |
d7f80320 | 5994 | goto update_rmap; |
5a35bf2c DC |
5995 | } |
5996 | ||
5997 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, | |
5998 | got.br_blockcount, &i); | |
5999 | if (error) | |
6000 | return error; | |
19ebedcf | 6001 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
5a35bf2c DC |
6002 | |
6003 | got.br_startoff = startoff; | |
d7f80320 DW |
6004 | error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock, |
6005 | got.br_blockcount, got.br_state); | |
6006 | if (error) | |
6007 | return error; | |
6008 | ||
6009 | update_rmap: | |
6010 | /* update reverse mapping */ | |
6011 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec); | |
6012 | if (error) | |
6013 | return error; | |
6014 | adj_irec.br_startoff = startoff; | |
6015 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec); | |
5a35bf2c DC |
6016 | } |
6017 | ||
ff105f75 | 6018 | /* |
19ebedcf | 6019 | * Shift extent records to the left/right to cover/create a hole. |
ff105f75 | 6020 | * |
5a35bf2c | 6021 | * The maximum number of extents to be shifted in a single operation is |
19ebedcf | 6022 | * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the |
5a35bf2c DC |
6023 | * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb |
6024 | * is the length by which each extent is shifted. If there is no hole to shift | |
6025 | * the extents into, this will be considered invalid operation and we abort | |
6026 | * immediately. | |
ff105f75 DC |
6027 | */ |
6028 | int | |
6029 | xfs_bmap_shift_extents( | |
6030 | struct xfs_trans *tp, | |
6031 | struct xfs_inode *ip, | |
19ebedcf | 6032 | xfs_fileoff_t *next_fsb, |
ff105f75 | 6033 | xfs_fileoff_t offset_shift_fsb, |
5a35bf2c | 6034 | int *done, |
19ebedcf | 6035 | xfs_fileoff_t stop_fsb, |
ff105f75 | 6036 | xfs_fsblock_t *firstblock, |
f33cea1a | 6037 | struct xfs_defer_ops *dfops, |
19ebedcf | 6038 | enum shift_direction direction, |
ff105f75 DC |
6039 | int num_exts) |
6040 | { | |
5a35bf2c | 6041 | struct xfs_btree_cur *cur = NULL; |
ff105f75 DC |
6042 | struct xfs_bmbt_rec_host *gotp; |
6043 | struct xfs_bmbt_irec got; | |
ff105f75 DC |
6044 | struct xfs_mount *mp = ip->i_mount; |
6045 | struct xfs_ifork *ifp; | |
6046 | xfs_extnum_t nexts = 0; | |
5a35bf2c | 6047 | xfs_extnum_t current_ext; |
19ebedcf DC |
6048 | xfs_extnum_t total_extents; |
6049 | xfs_extnum_t stop_extent; | |
ff105f75 | 6050 | int error = 0; |
ff105f75 | 6051 | int whichfork = XFS_DATA_FORK; |
5a35bf2c | 6052 | int logflags = 0; |
ff105f75 DC |
6053 | |
6054 | if (unlikely(XFS_TEST_ERROR( | |
6055 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && | |
6056 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), | |
6057 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { | |
6058 | XFS_ERROR_REPORT("xfs_bmap_shift_extents", | |
6059 | XFS_ERRLEVEL_LOW, mp); | |
12b53197 | 6060 | return -EFSCORRUPTED; |
ff105f75 DC |
6061 | } |
6062 | ||
6063 | if (XFS_FORCED_SHUTDOWN(mp)) | |
12b53197 | 6064 | return -EIO; |
ff105f75 | 6065 | |
5a35bf2c DC |
6066 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
6067 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); | |
19ebedcf DC |
6068 | ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT); |
6069 | ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT); | |
ff105f75 DC |
6070 | |
6071 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
6072 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { | |
6073 | /* Read in all the extents */ | |
6074 | error = xfs_iread_extents(tp, ip, whichfork); | |
6075 | if (error) | |
6076 | return error; | |
6077 | } | |
6078 | ||
ff105f75 DC |
6079 | if (ifp->if_flags & XFS_IFBROOT) { |
6080 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); | |
6081 | cur->bc_private.b.firstblock = *firstblock; | |
f33cea1a | 6082 | cur->bc_private.b.dfops = dfops; |
ff105f75 | 6083 | cur->bc_private.b.flags = 0; |
5a35bf2c DC |
6084 | } |
6085 | ||
19ebedcf DC |
6086 | /* |
6087 | * There may be delalloc extents in the data fork before the range we | |
6088 | * are collapsing out, so we cannot use the count of real extents here. | |
6089 | * Instead we have to calculate it from the incore fork. | |
6090 | */ | |
6091 | total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); | |
6092 | if (total_extents == 0) { | |
6093 | *done = 1; | |
6094 | goto del_cursor; | |
6095 | } | |
6096 | ||
6097 | /* | |
6098 | * In case of first right shift, we need to initialize next_fsb | |
6099 | */ | |
6100 | if (*next_fsb == NULLFSBLOCK) { | |
6101 | gotp = xfs_iext_get_ext(ifp, total_extents - 1); | |
6102 | xfs_bmbt_get_all(gotp, &got); | |
6103 | *next_fsb = got.br_startoff; | |
6104 | if (stop_fsb > *next_fsb) { | |
6105 | *done = 1; | |
6106 | goto del_cursor; | |
6107 | } | |
6108 | } | |
6109 | ||
6110 | /* Lookup the extent index at which we have to stop */ | |
6111 | if (direction == SHIFT_RIGHT) { | |
6112 | gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent); | |
6113 | /* Make stop_extent exclusive of shift range */ | |
6114 | stop_extent--; | |
6115 | } else | |
6116 | stop_extent = total_extents; | |
6117 | ||
5a35bf2c DC |
6118 | /* |
6119 | * Look up the extent index for the fsb where we start shifting. We can | |
6120 | * henceforth iterate with current_ext as extent list changes are locked | |
6121 | * out via ilock. | |
6122 | * | |
6123 | * gotp can be null in 2 cases: 1) if there are no extents or 2) | |
19ebedcf | 6124 | * *next_fsb lies in a hole beyond which there are no extents. Either |
5a35bf2c DC |
6125 | * way, we are done. |
6126 | */ | |
19ebedcf | 6127 | gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, ¤t_ext); |
5a35bf2c DC |
6128 | if (!gotp) { |
6129 | *done = 1; | |
6130 | goto del_cursor; | |
ff105f75 DC |
6131 | } |
6132 | ||
19ebedcf DC |
6133 | /* some sanity checking before we finally start shifting extents */ |
6134 | if ((direction == SHIFT_LEFT && current_ext >= stop_extent) || | |
6135 | (direction == SHIFT_RIGHT && current_ext <= stop_extent)) { | |
6136 | error = -EIO; | |
6137 | goto del_cursor; | |
6138 | } | |
6139 | ||
6140 | while (nexts++ < num_exts) { | |
5a35bf2c | 6141 | error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb, |
19ebedcf | 6142 | ¤t_ext, gotp, cur, &logflags, |
d7f80320 | 6143 | direction, dfops); |
ff105f75 DC |
6144 | if (error) |
6145 | goto del_cursor; | |
19ebedcf DC |
6146 | /* |
6147 | * If there was an extent merge during the shift, the extent | |
6148 | * count can change. Update the total and grade the next record. | |
6149 | */ | |
6150 | if (direction == SHIFT_LEFT) { | |
6151 | total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); | |
6152 | stop_extent = total_extents; | |
6153 | } | |
ff105f75 | 6154 | |
19ebedcf DC |
6155 | if (current_ext == stop_extent) { |
6156 | *done = 1; | |
6157 | *next_fsb = NULLFSBLOCK; | |
5a35bf2c | 6158 | break; |
19ebedcf | 6159 | } |
5a35bf2c | 6160 | gotp = xfs_iext_get_ext(ifp, current_ext); |
ff105f75 DC |
6161 | } |
6162 | ||
19ebedcf | 6163 | if (!*done) { |
5a35bf2c DC |
6164 | xfs_bmbt_get_all(gotp, &got); |
6165 | *next_fsb = got.br_startoff; | |
6166 | } | |
ff105f75 DC |
6167 | |
6168 | del_cursor: | |
6169 | if (cur) | |
6170 | xfs_btree_del_cursor(cur, | |
6171 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); | |
6172 | ||
5a35bf2c DC |
6173 | if (logflags) |
6174 | xfs_trans_log_inode(tp, ip, logflags); | |
6175 | ||
ff105f75 DC |
6176 | return error; |
6177 | } | |
19ebedcf DC |
6178 | |
6179 | /* | |
6180 | * Splits an extent into two extents at split_fsb block such that it is | |
6181 | * the first block of the current_ext. @current_ext is a target extent | |
6182 | * to be split. @split_fsb is a block where the extents is split. | |
6183 | * If split_fsb lies in a hole or the first block of extents, just return 0. | |
6184 | */ | |
6185 | STATIC int | |
6186 | xfs_bmap_split_extent_at( | |
6187 | struct xfs_trans *tp, | |
6188 | struct xfs_inode *ip, | |
6189 | xfs_fileoff_t split_fsb, | |
6190 | xfs_fsblock_t *firstfsb, | |
f33cea1a | 6191 | struct xfs_defer_ops *dfops) |
19ebedcf DC |
6192 | { |
6193 | int whichfork = XFS_DATA_FORK; | |
6194 | struct xfs_btree_cur *cur = NULL; | |
6195 | struct xfs_bmbt_rec_host *gotp; | |
6196 | struct xfs_bmbt_irec got; | |
6197 | struct xfs_bmbt_irec new; /* split extent */ | |
6198 | struct xfs_mount *mp = ip->i_mount; | |
6199 | struct xfs_ifork *ifp; | |
6200 | xfs_fsblock_t gotblkcnt; /* new block count for got */ | |
6201 | xfs_extnum_t current_ext; | |
6202 | int error = 0; | |
6203 | int logflags = 0; | |
6204 | int i = 0; | |
6205 | ||
6206 | if (unlikely(XFS_TEST_ERROR( | |
6207 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && | |
6208 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), | |
6209 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { | |
6210 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", | |
6211 | XFS_ERRLEVEL_LOW, mp); | |
6212 | return -EFSCORRUPTED; | |
6213 | } | |
6214 | ||
6215 | if (XFS_FORCED_SHUTDOWN(mp)) | |
6216 | return -EIO; | |
6217 | ||
6218 | ifp = XFS_IFORK_PTR(ip, whichfork); | |
6219 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { | |
6220 | /* Read in all the extents */ | |
6221 | error = xfs_iread_extents(tp, ip, whichfork); | |
6222 | if (error) | |
6223 | return error; | |
6224 | } | |
6225 | ||
6226 | /* | |
6227 | * gotp can be null in 2 cases: 1) if there are no extents | |
6228 | * or 2) split_fsb lies in a hole beyond which there are | |
6229 | * no extents. Either way, we are done. | |
6230 | */ | |
6231 | gotp = xfs_iext_bno_to_ext(ifp, split_fsb, ¤t_ext); | |
6232 | if (!gotp) | |
6233 | return 0; | |
6234 | ||
6235 | xfs_bmbt_get_all(gotp, &got); | |
6236 | ||
6237 | /* | |
6238 | * Check split_fsb lies in a hole or the start boundary offset | |
6239 | * of the extent. | |
6240 | */ | |
6241 | if (got.br_startoff >= split_fsb) | |
6242 | return 0; | |
6243 | ||
6244 | gotblkcnt = split_fsb - got.br_startoff; | |
6245 | new.br_startoff = split_fsb; | |
6246 | new.br_startblock = got.br_startblock + gotblkcnt; | |
6247 | new.br_blockcount = got.br_blockcount - gotblkcnt; | |
6248 | new.br_state = got.br_state; | |
6249 | ||
6250 | if (ifp->if_flags & XFS_IFBROOT) { | |
6251 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); | |
6252 | cur->bc_private.b.firstblock = *firstfsb; | |
f33cea1a | 6253 | cur->bc_private.b.dfops = dfops; |
19ebedcf DC |
6254 | cur->bc_private.b.flags = 0; |
6255 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, | |
6256 | got.br_startblock, | |
6257 | got.br_blockcount, | |
6258 | &i); | |
6259 | if (error) | |
6260 | goto del_cursor; | |
6261 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); | |
6262 | } | |
6263 | ||
6264 | xfs_bmbt_set_blockcount(gotp, gotblkcnt); | |
6265 | got.br_blockcount = gotblkcnt; | |
6266 | ||
6267 | logflags = XFS_ILOG_CORE; | |
6268 | if (cur) { | |
6269 | error = xfs_bmbt_update(cur, got.br_startoff, | |
6270 | got.br_startblock, | |
6271 | got.br_blockcount, | |
6272 | got.br_state); | |
6273 | if (error) | |
6274 | goto del_cursor; | |
6275 | } else | |
6276 | logflags |= XFS_ILOG_DEXT; | |
6277 | ||
6278 | /* Add new extent */ | |
6279 | current_ext++; | |
6280 | xfs_iext_insert(ip, current_ext, 1, &new, 0); | |
6281 | XFS_IFORK_NEXT_SET(ip, whichfork, | |
6282 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); | |
6283 | ||
6284 | if (cur) { | |
6285 | error = xfs_bmbt_lookup_eq(cur, new.br_startoff, | |
6286 | new.br_startblock, new.br_blockcount, | |
6287 | &i); | |
6288 | if (error) | |
6289 | goto del_cursor; | |
6290 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); | |
6291 | cur->bc_rec.b.br_state = new.br_state; | |
6292 | ||
6293 | error = xfs_btree_insert(cur, &i); | |
6294 | if (error) | |
6295 | goto del_cursor; | |
6296 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); | |
6297 | } | |
6298 | ||
6299 | /* | |
6300 | * Convert to a btree if necessary. | |
6301 | */ | |
6302 | if (xfs_bmap_needs_btree(ip, whichfork)) { | |
6303 | int tmp_logflags; /* partial log flag return val */ | |
6304 | ||
6305 | ASSERT(cur == NULL); | |
f33cea1a | 6306 | error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops, |
19ebedcf DC |
6307 | &cur, 0, &tmp_logflags, whichfork); |
6308 | logflags |= tmp_logflags; | |
6309 | } | |
6310 | ||
6311 | del_cursor: | |
6312 | if (cur) { | |
6313 | cur->bc_private.b.allocated = 0; | |
6314 | xfs_btree_del_cursor(cur, | |
6315 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); | |
6316 | } | |
6317 | ||
6318 | if (logflags) | |
6319 | xfs_trans_log_inode(tp, ip, logflags); | |
6320 | return error; | |
6321 | } | |
6322 | ||
6323 | int | |
6324 | xfs_bmap_split_extent( | |
6325 | struct xfs_inode *ip, | |
6326 | xfs_fileoff_t split_fsb) | |
6327 | { | |
6328 | struct xfs_mount *mp = ip->i_mount; | |
6329 | struct xfs_trans *tp; | |
f33cea1a | 6330 | struct xfs_defer_ops dfops; |
19ebedcf | 6331 | xfs_fsblock_t firstfsb; |
19ebedcf DC |
6332 | int error; |
6333 | ||
9074815c CH |
6334 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, |
6335 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); | |
6336 | if (error) | |
19ebedcf | 6337 | return error; |
19ebedcf DC |
6338 | |
6339 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
6340 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); | |
6341 | ||
f33cea1a | 6342 | xfs_defer_init(&dfops, &firstfsb); |
19ebedcf DC |
6343 | |
6344 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb, | |
f33cea1a | 6345 | &firstfsb, &dfops); |
19ebedcf DC |
6346 | if (error) |
6347 | goto out; | |
6348 | ||
f33cea1a | 6349 | error = xfs_defer_finish(&tp, &dfops, NULL); |
19ebedcf DC |
6350 | if (error) |
6351 | goto out; | |
6352 | ||
de5a3f46 | 6353 | return xfs_trans_commit(tp); |
19ebedcf DC |
6354 | |
6355 | out: | |
f33cea1a | 6356 | xfs_defer_cancel(&dfops); |
3d7434fe | 6357 | xfs_trans_cancel(tp); |
19ebedcf DC |
6358 | return error; |
6359 | } | |
aeb88300 DW |
6360 | |
6361 | /* Deferred mapping is only for real extents in the data fork. */ | |
6362 | static bool | |
6363 | xfs_bmap_is_update_needed( | |
6364 | struct xfs_bmbt_irec *bmap) | |
6365 | { | |
6366 | return bmap->br_startblock != HOLESTARTBLOCK && | |
6367 | bmap->br_startblock != DELAYSTARTBLOCK; | |
6368 | } | |
6369 | ||
6370 | /* Record a bmap intent. */ | |
6371 | static int | |
6372 | __xfs_bmap_add( | |
6373 | struct xfs_mount *mp, | |
6374 | struct xfs_defer_ops *dfops, | |
6375 | enum xfs_bmap_intent_type type, | |
6376 | struct xfs_inode *ip, | |
6377 | int whichfork, | |
6378 | struct xfs_bmbt_irec *bmap) | |
6379 | { | |
6380 | int error; | |
6381 | struct xfs_bmap_intent *bi; | |
6382 | ||
6383 | trace_xfs_bmap_defer(mp, | |
6384 | XFS_FSB_TO_AGNO(mp, bmap->br_startblock), | |
6385 | type, | |
6386 | XFS_FSB_TO_AGBNO(mp, bmap->br_startblock), | |
6387 | ip->i_ino, whichfork, | |
6388 | bmap->br_startoff, | |
6389 | bmap->br_blockcount, | |
6390 | bmap->br_state); | |
6391 | ||
6392 | bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS); | |
6393 | INIT_LIST_HEAD(&bi->bi_list); | |
6394 | bi->bi_type = type; | |
6395 | bi->bi_owner = ip; | |
6396 | bi->bi_whichfork = whichfork; | |
6397 | bi->bi_bmap = *bmap; | |
6398 | ||
6399 | error = xfs_defer_join(dfops, bi->bi_owner); | |
6400 | if (error) { | |
6401 | kmem_free(bi); | |
6402 | return error; | |
6403 | } | |
6404 | ||
6405 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list); | |
6406 | return 0; | |
6407 | } | |
6408 | ||
6409 | /* Map an extent into a file. */ | |
6410 | int | |
6411 | xfs_bmap_map_extent( | |
6412 | struct xfs_mount *mp, | |
6413 | struct xfs_defer_ops *dfops, | |
6414 | struct xfs_inode *ip, | |
6415 | struct xfs_bmbt_irec *PREV) | |
6416 | { | |
6417 | if (!xfs_bmap_is_update_needed(PREV)) | |
6418 | return 0; | |
6419 | ||
6420 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip, | |
6421 | XFS_DATA_FORK, PREV); | |
6422 | } | |
6423 | ||
6424 | /* Unmap an extent out of a file. */ | |
6425 | int | |
6426 | xfs_bmap_unmap_extent( | |
6427 | struct xfs_mount *mp, | |
6428 | struct xfs_defer_ops *dfops, | |
6429 | struct xfs_inode *ip, | |
6430 | struct xfs_bmbt_irec *PREV) | |
6431 | { | |
6432 | if (!xfs_bmap_is_update_needed(PREV)) | |
6433 | return 0; | |
6434 | ||
6435 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip, | |
6436 | XFS_DATA_FORK, PREV); | |
6437 | } | |
6438 | ||
6439 | /* | |
6440 | * Process one of the deferred bmap operations. We pass back the | |
6441 | * btree cursor to maintain our lock on the bmapbt between calls. | |
6442 | */ | |
6443 | int | |
6444 | xfs_bmap_finish_one( | |
6445 | struct xfs_trans *tp, | |
6446 | struct xfs_defer_ops *dfops, | |
6447 | struct xfs_inode *ip, | |
6448 | enum xfs_bmap_intent_type type, | |
6449 | int whichfork, | |
6450 | xfs_fileoff_t startoff, | |
6451 | xfs_fsblock_t startblock, | |
6452 | xfs_filblks_t blockcount, | |
6453 | xfs_exntst_t state) | |
6454 | { | |
6455 | struct xfs_bmbt_irec bmap; | |
6456 | int nimaps = 1; | |
6457 | xfs_fsblock_t firstfsb; | |
6458 | int flags = XFS_BMAPI_REMAP; | |
6459 | int done; | |
6460 | int error = 0; | |
6461 | ||
6462 | bmap.br_startblock = startblock; | |
6463 | bmap.br_startoff = startoff; | |
6464 | bmap.br_blockcount = blockcount; | |
6465 | bmap.br_state = state; | |
6466 | ||
6467 | trace_xfs_bmap_deferred(tp->t_mountp, | |
6468 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type, | |
6469 | XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), | |
6470 | ip->i_ino, whichfork, startoff, blockcount, state); | |
6471 | ||
6472 | if (whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK) | |
6473 | return -EFSCORRUPTED; | |
6474 | if (whichfork == XFS_ATTR_FORK) | |
6475 | flags |= XFS_BMAPI_ATTRFORK; | |
6476 | ||
6477 | if (XFS_TEST_ERROR(false, tp->t_mountp, | |
6478 | XFS_ERRTAG_BMAP_FINISH_ONE, | |
6479 | XFS_RANDOM_BMAP_FINISH_ONE)) | |
6480 | return -EIO; | |
6481 | ||
6482 | switch (type) { | |
6483 | case XFS_BMAP_MAP: | |
6484 | firstfsb = bmap.br_startblock; | |
6485 | error = xfs_bmapi_write(tp, ip, bmap.br_startoff, | |
6486 | bmap.br_blockcount, flags, &firstfsb, | |
6487 | bmap.br_blockcount, &bmap, &nimaps, | |
6488 | dfops); | |
6489 | break; | |
6490 | case XFS_BMAP_UNMAP: | |
6491 | error = xfs_bunmapi(tp, ip, bmap.br_startoff, | |
6492 | bmap.br_blockcount, flags, 1, &firstfsb, | |
6493 | dfops, &done); | |
6494 | ASSERT(done); | |
6495 | break; | |
6496 | default: | |
6497 | ASSERT(0); | |
6498 | error = -EFSCORRUPTED; | |
6499 | } | |
6500 | ||
6501 | return error; | |
6502 | } |