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