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