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