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