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