]>
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 DC |
571 | error = xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo, |
572 | XFS_AG_RESV_NONE); | |
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); |
b37d753d CH |
744 | ifp->if_u1.if_root = NULL; |
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 | |
b37d753d CH |
829 | ifp->if_u1.if_root = NULL; |
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 | ||
613e6057 | 3041 | void |
49f693fa | 3042 | xfs_bmap_adjacent( |
613e6057 | 3043 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
2bd0ea18 | 3044 | { |
49f693fa | 3045 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
49f693fa | 3046 | xfs_mount_t *mp; /* mount point structure */ |
49f693fa | 3047 | int rt; /* true if inode is realtime */ |
2bd0ea18 | 3048 | |
49f693fa DC |
3049 | #define ISVALID(x,y) \ |
3050 | (rt ? \ | |
3051 | (x) < mp->m_sb.sb_rblocks : \ | |
3052 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ | |
3053 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ | |
3054 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) | |
3055 | ||
3056 | mp = ap->ip->i_mount; | |
1fccd5c8 | 3057 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
a85522b6 | 3058 | (ap->datatype & XFS_ALLOC_USERDATA); |
49f693fa DC |
3059 | /* |
3060 | * If allocating at eof, and there's a previous real block, | |
3061 | * try to use its last block as our starting point. | |
3062 | */ | |
3063 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && | |
3064 | !isnullstartblock(ap->prev.br_startblock) && | |
3065 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, | |
3066 | ap->prev.br_startblock)) { | |
3067 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; | |
3068 | /* | |
3069 | * Adjust for the gap between prevp and us. | |
3070 | */ | |
3071 | adjust = ap->offset - | |
3072 | (ap->prev.br_startoff + ap->prev.br_blockcount); | |
3073 | if (adjust && | |
3074 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) | |
3075 | ap->blkno += adjust; | |
2bd0ea18 | 3076 | } |
49f693fa DC |
3077 | /* |
3078 | * If not at eof, then compare the two neighbor blocks. | |
3079 | * Figure out whether either one gives us a good starting point, | |
3080 | * and pick the better one. | |
3081 | */ | |
3082 | else if (!ap->eof) { | |
3083 | xfs_fsblock_t gotbno; /* right side block number */ | |
3084 | xfs_fsblock_t gotdiff=0; /* right side difference */ | |
3085 | xfs_fsblock_t prevbno; /* left side block number */ | |
3086 | xfs_fsblock_t prevdiff=0; /* left side difference */ | |
3087 | ||
3088 | /* | |
3089 | * If there's a previous (left) block, select a requested | |
3090 | * start block based on it. | |
3091 | */ | |
3092 | if (ap->prev.br_startoff != NULLFILEOFF && | |
3093 | !isnullstartblock(ap->prev.br_startblock) && | |
3094 | (prevbno = ap->prev.br_startblock + | |
3095 | ap->prev.br_blockcount) && | |
3096 | ISVALID(prevbno, ap->prev.br_startblock)) { | |
3097 | /* | |
3098 | * Calculate gap to end of previous block. | |
3099 | */ | |
3100 | adjust = prevdiff = ap->offset - | |
3101 | (ap->prev.br_startoff + | |
3102 | ap->prev.br_blockcount); | |
3103 | /* | |
3104 | * Figure the startblock based on the previous block's | |
3105 | * end and the gap size. | |
3106 | * Heuristic! | |
3107 | * If the gap is large relative to the piece we're | |
3108 | * allocating, or using it gives us an invalid block | |
3109 | * number, then just use the end of the previous block. | |
3110 | */ | |
3111 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && | |
3112 | ISVALID(prevbno + prevdiff, | |
3113 | ap->prev.br_startblock)) | |
3114 | prevbno += adjust; | |
3115 | else | |
3116 | prevdiff += adjust; | |
49f693fa DC |
3117 | } |
3118 | /* | |
3119 | * No previous block or can't follow it, just default. | |
3120 | */ | |
3121 | else | |
3122 | prevbno = NULLFSBLOCK; | |
3123 | /* | |
3124 | * If there's a following (right) block, select a requested | |
3125 | * start block based on it. | |
3126 | */ | |
3127 | if (!isnullstartblock(ap->got.br_startblock)) { | |
3128 | /* | |
3129 | * Calculate gap to start of next block. | |
3130 | */ | |
3131 | adjust = gotdiff = ap->got.br_startoff - ap->offset; | |
3132 | /* | |
3133 | * Figure the startblock based on the next block's | |
3134 | * start and the gap size. | |
3135 | */ | |
3136 | gotbno = ap->got.br_startblock; | |
3137 | /* | |
3138 | * Heuristic! | |
3139 | * If the gap is large relative to the piece we're | |
3140 | * allocating, or using it gives us an invalid block | |
3141 | * number, then just use the start of the next block | |
3142 | * offset by our length. | |
3143 | */ | |
3144 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && | |
3145 | ISVALID(gotbno - gotdiff, gotbno)) | |
3146 | gotbno -= adjust; | |
3147 | else if (ISVALID(gotbno - ap->length, gotbno)) { | |
3148 | gotbno -= ap->length; | |
3149 | gotdiff += adjust - ap->length; | |
3150 | } else | |
3151 | gotdiff += adjust; | |
49f693fa DC |
3152 | } |
3153 | /* | |
3154 | * No next block, just default. | |
3155 | */ | |
2bd0ea18 | 3156 | else |
49f693fa DC |
3157 | gotbno = NULLFSBLOCK; |
3158 | /* | |
3159 | * If both valid, pick the better one, else the only good | |
3160 | * one, else ap->blkno is already set (to 0 or the inode block). | |
3161 | */ | |
3162 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) | |
3163 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; | |
3164 | else if (prevbno != NULLFSBLOCK) | |
3165 | ap->blkno = prevbno; | |
3166 | else if (gotbno != NULLFSBLOCK) | |
3167 | ap->blkno = gotbno; | |
a2ceac1f | 3168 | } |
49f693fa | 3169 | #undef ISVALID |
a2ceac1f DC |
3170 | } |
3171 | ||
c92d3040 | 3172 | int |
ff105f75 | 3173 | xfs_bmap_longest_free_extent( |
5cb4ffc8 | 3174 | struct xfs_perag *pag, |
ff105f75 | 3175 | struct xfs_trans *tp, |
4ccd9b2f | 3176 | xfs_extlen_t *blen) |
ff105f75 | 3177 | { |
ff105f75 DC |
3178 | xfs_extlen_t longest; |
3179 | int error = 0; | |
3180 | ||
03dc2ef2 | 3181 | if (!xfs_perag_initialised_agf(pag)) { |
f9084bd9 | 3182 | error = xfs_alloc_read_agf(pag, tp, XFS_ALLOC_FLAG_TRYLOCK, |
87db57ba | 3183 | NULL); |
4ccd9b2f | 3184 | if (error) |
5cb4ffc8 | 3185 | return error; |
ff105f75 DC |
3186 | } |
3187 | ||
1421de38 | 3188 | longest = xfs_alloc_longest_free_extent(pag, |
5cb4ffc8 | 3189 | xfs_alloc_min_freelist(pag->pag_mount, pag), |
cf8ce220 | 3190 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); |
ff105f75 DC |
3191 | if (*blen < longest) |
3192 | *blen = longest; | |
3193 | ||
5cb4ffc8 | 3194 | return 0; |
ff105f75 DC |
3195 | } |
3196 | ||
4ccd9b2f | 3197 | static xfs_extlen_t |
ff105f75 DC |
3198 | xfs_bmap_select_minlen( |
3199 | struct xfs_bmalloca *ap, | |
3200 | struct xfs_alloc_arg *args, | |
4ccd9b2f | 3201 | xfs_extlen_t blen) |
ff105f75 | 3202 | { |
4ccd9b2f DC |
3203 | |
3204 | /* | |
3205 | * Since we used XFS_ALLOC_FLAG_TRYLOCK in _longest_free_extent(), it is | |
3206 | * possible that there is enough contiguous free space for this request. | |
3207 | */ | |
3208 | if (blen < ap->minlen) | |
3209 | return ap->minlen; | |
3210 | ||
3211 | /* | |
3212 | * If the best seen length is less than the request length, | |
3213 | * use the best as the minimum, otherwise we've got the maxlen we | |
3214 | * were asked for. | |
3215 | */ | |
3216 | if (blen < args->maxlen) | |
3217 | return blen; | |
3218 | return args->maxlen; | |
ff105f75 DC |
3219 | } |
3220 | ||
3ed4e682 | 3221 | static int |
80375d24 | 3222 | xfs_bmap_btalloc_select_lengths( |
49f693fa DC |
3223 | struct xfs_bmalloca *ap, |
3224 | struct xfs_alloc_arg *args, | |
3225 | xfs_extlen_t *blen) | |
a2ceac1f | 3226 | { |
3ed4e682 | 3227 | struct xfs_mount *mp = args->mp; |
5cb4ffc8 DC |
3228 | struct xfs_perag *pag; |
3229 | xfs_agnumber_t agno, startag; | |
5cb4ffc8 | 3230 | int error = 0; |
a2ceac1f | 3231 | |
80375d24 DC |
3232 | if (ap->tp->t_flags & XFS_TRANS_LOWMODE) { |
3233 | args->total = ap->minlen; | |
3234 | args->minlen = ap->minlen; | |
3235 | return 0; | |
3236 | } | |
a2ceac1f | 3237 | |
80375d24 | 3238 | args->total = ap->total; |
3ed4e682 | 3239 | startag = XFS_FSB_TO_AGNO(mp, ap->blkno); |
49f693fa | 3240 | if (startag == NULLAGNUMBER) |
5cb4ffc8 | 3241 | startag = 0; |
a2ceac1f | 3242 | |
5cb4ffc8 DC |
3243 | *blen = 0; |
3244 | for_each_perag_wrap(mp, startag, agno, pag) { | |
4ccd9b2f DC |
3245 | error = xfs_bmap_longest_free_extent(pag, args->tp, blen); |
3246 | if (error && error != -EAGAIN) | |
5cb4ffc8 | 3247 | break; |
4ccd9b2f | 3248 | error = 0; |
5cb4ffc8 | 3249 | if (*blen >= args->maxlen) |
49f693fa | 3250 | break; |
49f693fa | 3251 | } |
5cb4ffc8 DC |
3252 | if (pag) |
3253 | xfs_perag_rele(pag); | |
2bd0ea18 | 3254 | |
4ccd9b2f | 3255 | args->minlen = xfs_bmap_select_minlen(ap, args, *blen); |
5cb4ffc8 | 3256 | return error; |
ff105f75 DC |
3257 | } |
3258 | ||
3cb68815 DW |
3259 | /* Update all inode and quota accounting for the allocation we just did. */ |
3260 | static void | |
3261 | xfs_bmap_btalloc_accounting( | |
3262 | struct xfs_bmalloca *ap, | |
3263 | struct xfs_alloc_arg *args) | |
3264 | { | |
d07cc724 DW |
3265 | if (ap->flags & XFS_BMAPI_COWFORK) { |
3266 | /* | |
3267 | * COW fork blocks are in-core only and thus are treated as | |
3268 | * in-core quota reservation (like delalloc blocks) even when | |
3269 | * converted to real blocks. The quota reservation is not | |
3270 | * accounted to disk until blocks are remapped to the data | |
3271 | * fork. So if these blocks were previously delalloc, we | |
3272 | * already have quota reservation and there's nothing to do | |
3273 | * yet. | |
3274 | */ | |
f73690fe DW |
3275 | if (ap->wasdel) { |
3276 | xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len); | |
d07cc724 | 3277 | return; |
f73690fe | 3278 | } |
d07cc724 DW |
3279 | |
3280 | /* | |
3281 | * Otherwise, we've allocated blocks in a hole. The transaction | |
3282 | * has acquired in-core quota reservation for this extent. | |
3283 | * Rather than account these as real blocks, however, we reduce | |
3284 | * the transaction quota reservation based on the allocation. | |
3285 | * This essentially transfers the transaction quota reservation | |
3286 | * to that of a delalloc extent. | |
3287 | */ | |
3288 | ap->ip->i_delayed_blks += args->len; | |
3289 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS, | |
3290 | -(long)args->len); | |
3291 | return; | |
3292 | } | |
3293 | ||
3294 | /* data/attr fork only */ | |
aa00f286 | 3295 | ap->ip->i_nblocks += args->len; |
3cb68815 | 3296 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
f73690fe | 3297 | if (ap->wasdel) { |
3cb68815 | 3298 | ap->ip->i_delayed_blks -= args->len; |
f73690fe DW |
3299 | xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len); |
3300 | } | |
3cb68815 DW |
3301 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
3302 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT, | |
3303 | args->len); | |
3304 | } | |
3305 | ||
3f08f006 CB |
3306 | static int |
3307 | xfs_bmap_compute_alignments( | |
3308 | struct xfs_bmalloca *ap, | |
3309 | struct xfs_alloc_arg *args) | |
3310 | { | |
3311 | struct xfs_mount *mp = args->mp; | |
3312 | xfs_extlen_t align = 0; /* minimum allocation alignment */ | |
3313 | int stripe_align = 0; | |
3f08f006 CB |
3314 | |
3315 | /* stripe alignment for allocation is determined by mount parameters */ | |
914e2a04 | 3316 | if (mp->m_swidth && xfs_has_swalloc(mp)) |
3f08f006 CB |
3317 | stripe_align = mp->m_swidth; |
3318 | else if (mp->m_dalign) | |
3319 | stripe_align = mp->m_dalign; | |
3320 | ||
3321 | if (ap->flags & XFS_BMAPI_COWFORK) | |
3322 | align = xfs_get_cowextsz_hint(ap->ip); | |
3323 | else if (ap->datatype & XFS_ALLOC_USERDATA) | |
3324 | align = xfs_get_extsz_hint(ap->ip); | |
3325 | if (align) { | |
3af6ab0a CB |
3326 | if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0, |
3327 | ap->eof, 0, ap->conv, &ap->offset, | |
3328 | &ap->length)) | |
3329 | ASSERT(0); | |
3f08f006 CB |
3330 | ASSERT(ap->length); |
3331 | } | |
3332 | ||
3333 | /* apply extent size hints if obtained earlier */ | |
3334 | if (align) { | |
3335 | args->prod = align; | |
3336 | div_u64_rem(ap->offset, args->prod, &args->mod); | |
3337 | if (args->mod) | |
3338 | args->mod = args->prod - args->mod; | |
3339 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { | |
3340 | args->prod = 1; | |
3341 | args->mod = 0; | |
3342 | } else { | |
3343 | args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; | |
3344 | div_u64_rem(ap->offset, args->prod, &args->mod); | |
3345 | if (args->mod) | |
3346 | args->mod = args->prod - args->mod; | |
3347 | } | |
3348 | ||
3349 | return stripe_align; | |
3350 | } | |
3351 | ||
fc177ab0 CB |
3352 | static void |
3353 | xfs_bmap_process_allocated_extent( | |
3354 | struct xfs_bmalloca *ap, | |
3355 | struct xfs_alloc_arg *args, | |
3356 | xfs_fileoff_t orig_offset, | |
3357 | xfs_extlen_t orig_length) | |
3358 | { | |
fc177ab0 | 3359 | ap->blkno = args->fsbno; |
fc177ab0 CB |
3360 | ap->length = args->len; |
3361 | /* | |
3362 | * If the extent size hint is active, we tried to round the | |
3363 | * caller's allocation request offset down to extsz and the | |
3364 | * length up to another extsz boundary. If we found a free | |
3365 | * extent we mapped it in starting at this new offset. If the | |
3366 | * newly mapped space isn't long enough to cover any of the | |
3367 | * range of offsets that was originally requested, move the | |
3368 | * mapping up so that we can fill as much of the caller's | |
3369 | * original request as possible. Free space is apparently | |
3370 | * very fragmented so we're unlikely to be able to satisfy the | |
3371 | * hints anyway. | |
3372 | */ | |
3373 | if (ap->length <= orig_length) | |
3374 | ap->offset = orig_offset; | |
3375 | else if (ap->offset + ap->length < orig_offset + orig_length) | |
3376 | ap->offset = orig_offset + orig_length - ap->length; | |
3377 | xfs_bmap_btalloc_accounting(ap, args); | |
3378 | } | |
3379 | ||
3006cea4 CB |
3380 | #ifdef DEBUG |
3381 | static int | |
3382 | xfs_bmap_exact_minlen_extent_alloc( | |
3383 | struct xfs_bmalloca *ap) | |
b3563c19 | 3384 | { |
3006cea4 CB |
3385 | struct xfs_mount *mp = ap->ip->i_mount; |
3386 | struct xfs_alloc_arg args = { .tp = ap->tp, .mp = mp }; | |
3387 | xfs_fileoff_t orig_offset; | |
3388 | xfs_extlen_t orig_length; | |
3389 | int error; | |
b3563c19 | 3390 | |
49f693fa | 3391 | ASSERT(ap->length); |
3006cea4 CB |
3392 | |
3393 | if (ap->minlen != 1) { | |
3394 | ap->blkno = NULLFSBLOCK; | |
3395 | ap->length = 0; | |
3396 | return 0; | |
3397 | } | |
3398 | ||
c38464ff DW |
3399 | orig_offset = ap->offset; |
3400 | orig_length = ap->length; | |
b3563c19 | 3401 | |
3006cea4 | 3402 | args.alloc_minlen_only = 1; |
ff105f75 | 3403 | |
3006cea4 CB |
3404 | xfs_bmap_compute_alignments(ap, &args); |
3405 | ||
80375d24 DC |
3406 | /* |
3407 | * Unlike the longest extent available in an AG, we don't track | |
3408 | * the length of an AG's shortest extent. | |
3409 | * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and | |
3410 | * hence we can afford to start traversing from the 0th AG since | |
3411 | * we need not be concerned about a drop in performance in | |
3412 | * "debug only" code paths. | |
3413 | */ | |
3414 | ap->blkno = XFS_AGB_TO_FSB(mp, 0, 0); | |
3006cea4 | 3415 | |
3006cea4 | 3416 | args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE; |
839b0c6d CB |
3417 | args.minlen = args.maxlen = ap->minlen; |
3418 | args.total = ap->total; | |
3006cea4 CB |
3419 | |
3420 | args.alignment = 1; | |
3421 | args.minalignslop = 0; | |
3422 | ||
3423 | args.minleft = ap->minleft; | |
3424 | args.wasdel = ap->wasdel; | |
3425 | args.resv = XFS_AG_RESV_NONE; | |
3426 | args.datatype = ap->datatype; | |
3427 | ||
5ba1f915 | 3428 | error = xfs_alloc_vextent_first_ag(&args, ap->blkno); |
3006cea4 CB |
3429 | if (error) |
3430 | return error; | |
3431 | ||
3432 | if (args.fsbno != NULLFSBLOCK) { | |
3433 | xfs_bmap_process_allocated_extent(ap, &args, orig_offset, | |
3434 | orig_length); | |
3435 | } else { | |
3436 | ap->blkno = NULLFSBLOCK; | |
3437 | ap->length = 0; | |
3438 | } | |
3439 | ||
3440 | return 0; | |
3441 | } | |
3442 | #else | |
3443 | ||
3444 | #define xfs_bmap_exact_minlen_extent_alloc(bma) (-EFSCORRUPTED) | |
3445 | ||
3446 | #endif | |
3447 | ||
3ed4e682 DC |
3448 | /* |
3449 | * If we are not low on available data blocks and we are allocating at | |
3450 | * EOF, optimise allocation for contiguous file extension and/or stripe | |
3451 | * alignment of the new extent. | |
3452 | * | |
3453 | * NOTE: ap->aeof is only set if the allocation length is >= the | |
3454 | * stripe unit and the allocation offset is at the end of file. | |
3455 | */ | |
3456 | static int | |
3457 | xfs_bmap_btalloc_at_eof( | |
3458 | struct xfs_bmalloca *ap, | |
3459 | struct xfs_alloc_arg *args, | |
3460 | xfs_extlen_t blen, | |
1a5a98d5 DC |
3461 | int stripe_align, |
3462 | bool ag_only) | |
3006cea4 | 3463 | { |
3ed4e682 | 3464 | struct xfs_mount *mp = args->mp; |
c371ca85 | 3465 | struct xfs_perag *caller_pag = args->pag; |
3006cea4 | 3466 | int error; |
3006cea4 | 3467 | |
3ed4e682 DC |
3468 | /* |
3469 | * If there are already extents in the file, try an exact EOF block | |
3470 | * allocation to extend the file as a contiguous extent. If that fails, | |
3471 | * or it's the first allocation in a file, just try for a stripe aligned | |
3472 | * allocation. | |
3473 | */ | |
3474 | if (ap->offset) { | |
3475 | xfs_extlen_t nextminlen = 0; | |
ff105f75 | 3476 | |
3ed4e682 DC |
3477 | /* |
3478 | * Compute the minlen+alignment for the next case. Set slop so | |
3479 | * that the value of minlen+alignment+slop doesn't go up between | |
3480 | * the calls. | |
3481 | */ | |
32cd26bf | 3482 | args->alignment = 1; |
3ed4e682 DC |
3483 | if (blen > stripe_align && blen <= args->maxlen) |
3484 | nextminlen = blen - stripe_align; | |
3485 | else | |
3486 | nextminlen = args->minlen; | |
3487 | if (nextminlen + stripe_align > args->minlen + 1) | |
3488 | args->minalignslop = nextminlen + stripe_align - | |
3489 | args->minlen - 1; | |
3490 | else | |
3491 | args->minalignslop = 0; | |
3492 | ||
c371ca85 DC |
3493 | if (!caller_pag) |
3494 | args->pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, ap->blkno)); | |
c4e10f2b | 3495 | error = xfs_alloc_vextent_exact_bno(args, ap->blkno); |
2897a1c2 | 3496 | if (!caller_pag) { |
c371ca85 | 3497 | xfs_perag_put(args->pag); |
2897a1c2 DW |
3498 | args->pag = NULL; |
3499 | } | |
3ed4e682 DC |
3500 | if (error) |
3501 | return error; | |
ff105f75 | 3502 | |
3ed4e682 DC |
3503 | if (args->fsbno != NULLFSBLOCK) |
3504 | return 0; | |
3505 | /* | |
3506 | * Exact allocation failed. Reset to try an aligned allocation | |
3507 | * according to the original allocation specification. | |
3508 | */ | |
3ed4e682 DC |
3509 | args->alignment = stripe_align; |
3510 | args->minlen = nextminlen; | |
3511 | args->minalignslop = 0; | |
3512 | } else { | |
3ed4e682 DC |
3513 | /* |
3514 | * Adjust minlen to try and preserve alignment if we | |
3515 | * can't guarantee an aligned maxlen extent. | |
3516 | */ | |
32cd26bf | 3517 | args->alignment = stripe_align; |
3ed4e682 DC |
3518 | if (blen > args->alignment && |
3519 | blen <= args->maxlen + args->alignment) | |
3520 | args->minlen = blen - args->alignment; | |
3521 | args->minalignslop = 0; | |
3522 | } | |
3523 | ||
c371ca85 | 3524 | if (ag_only) { |
15653695 | 3525 | error = xfs_alloc_vextent_near_bno(args, ap->blkno); |
c371ca85 DC |
3526 | } else { |
3527 | args->pag = NULL; | |
1a5a98d5 | 3528 | error = xfs_alloc_vextent_start_ag(args, ap->blkno); |
c371ca85 DC |
3529 | ASSERT(args->pag == NULL); |
3530 | args->pag = caller_pag; | |
3531 | } | |
3ed4e682 DC |
3532 | if (error) |
3533 | return error; | |
3534 | ||
3535 | if (args->fsbno != NULLFSBLOCK) | |
3536 | return 0; | |
3537 | ||
3538 | /* | |
3539 | * Allocation failed, so turn return the allocation args to their | |
3540 | * original non-aligned state so the caller can proceed on allocation | |
3541 | * failure as if this function was never called. | |
3542 | */ | |
3ed4e682 DC |
3543 | args->alignment = 1; |
3544 | return 0; | |
3545 | } | |
3546 | ||
451b902f DC |
3547 | /* |
3548 | * We have failed multiple allocation attempts so now are in a low space | |
3549 | * allocation situation. Try a locality first full filesystem minimum length | |
3550 | * allocation whilst still maintaining necessary total block reservation | |
3551 | * requirements. | |
3552 | * | |
3553 | * If that fails, we are now critically low on space, so perform a last resort | |
3554 | * allocation attempt: no reserve, no locality, blocking, minimum length, full | |
3555 | * filesystem free space scan. We also indicate to future allocations in this | |
3556 | * transaction that we are critically low on space so they don't waste time on | |
3557 | * allocation modes that are unlikely to succeed. | |
3558 | */ | |
12ae6818 | 3559 | int |
451b902f DC |
3560 | xfs_bmap_btalloc_low_space( |
3561 | struct xfs_bmalloca *ap, | |
3562 | struct xfs_alloc_arg *args) | |
3563 | { | |
3564 | int error; | |
3565 | ||
3566 | if (args->minlen > ap->minlen) { | |
3567 | args->minlen = ap->minlen; | |
3568 | error = xfs_alloc_vextent_start_ag(args, ap->blkno); | |
3569 | if (error || args->fsbno != NULLFSBLOCK) | |
3570 | return error; | |
3571 | } | |
3572 | ||
3573 | /* Last ditch attempt before failure is declared. */ | |
3574 | args->total = ap->minlen; | |
3575 | error = xfs_alloc_vextent_first_ag(args, 0); | |
3576 | if (error) | |
3577 | return error; | |
3578 | ap->tp->t_flags |= XFS_TRANS_LOWMODE; | |
3579 | return 0; | |
3580 | } | |
3581 | ||
3582 | static int | |
3583 | xfs_bmap_btalloc_filestreams( | |
3ed4e682 DC |
3584 | struct xfs_bmalloca *ap, |
3585 | struct xfs_alloc_arg *args, | |
3586 | int stripe_align) | |
3587 | { | |
3ed4e682 | 3588 | xfs_extlen_t blen = 0; |
c371ca85 | 3589 | int error = 0; |
3ed4e682 | 3590 | |
12ae6818 DC |
3591 | |
3592 | error = xfs_filestream_select_ag(ap, args, &blen); | |
3593 | if (error) | |
3594 | return error; | |
c371ca85 | 3595 | ASSERT(args->pag); |
1a5a98d5 | 3596 | |
3ed4e682 | 3597 | /* |
12ae6818 DC |
3598 | * If we are in low space mode, then optimal allocation will fail so |
3599 | * prepare for minimal allocation and jump to the low space algorithm | |
3600 | * immediately. | |
3ed4e682 | 3601 | */ |
451b902f DC |
3602 | if (ap->tp->t_flags & XFS_TRANS_LOWMODE) { |
3603 | args->minlen = ap->minlen; | |
c371ca85 | 3604 | ASSERT(args->fsbno == NULLFSBLOCK); |
12ae6818 | 3605 | goto out_low_space; |
80375d24 | 3606 | } |
80375d24 | 3607 | |
12ae6818 | 3608 | args->minlen = xfs_bmap_select_minlen(ap, args, blen); |
c371ca85 | 3609 | if (ap->aeof) |
451b902f DC |
3610 | error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align, |
3611 | true); | |
451b902f | 3612 | |
c371ca85 DC |
3613 | if (!error && args->fsbno == NULLFSBLOCK) |
3614 | error = xfs_alloc_vextent_near_bno(args, ap->blkno); | |
3615 | ||
3616 | out_low_space: | |
3617 | /* | |
3618 | * We are now done with the perag reference for the filestreams | |
3619 | * association provided by xfs_filestream_select_ag(). Release it now as | |
3620 | * we've either succeeded, had a fatal error or we are out of space and | |
3621 | * need to do a full filesystem scan for free space which will take it's | |
3622 | * own references. | |
3623 | */ | |
3624 | xfs_perag_rele(args->pag); | |
3625 | args->pag = NULL; | |
451b902f DC |
3626 | if (error || args->fsbno != NULLFSBLOCK) |
3627 | return error; | |
3628 | ||
3629 | return xfs_bmap_btalloc_low_space(ap, args); | |
3630 | } | |
3631 | ||
3632 | static int | |
3633 | xfs_bmap_btalloc_best_length( | |
3634 | struct xfs_bmalloca *ap, | |
3635 | struct xfs_alloc_arg *args, | |
3636 | int stripe_align) | |
3637 | { | |
3638 | xfs_extlen_t blen = 0; | |
3639 | int error; | |
3640 | ||
3641 | ap->blkno = XFS_INO_TO_FSB(args->mp, ap->ip->i_ino); | |
3642 | xfs_bmap_adjacent(ap); | |
3643 | ||
3644 | /* | |
3645 | * Search for an allocation group with a single extent large enough for | |
3646 | * the request. If one isn't found, then adjust the minimum allocation | |
3647 | * size to the largest space found. | |
3648 | */ | |
3649 | error = xfs_bmap_btalloc_select_lengths(ap, args, &blen); | |
80375d24 DC |
3650 | if (error) |
3651 | return error; | |
3f08f006 | 3652 | |
2bd0ea18 | 3653 | /* |
3ed4e682 DC |
3654 | * Don't attempt optimal EOF allocation if previous allocations barely |
3655 | * succeeded due to being near ENOSPC. It is highly unlikely we'll get | |
3656 | * optimal or even aligned allocations in this case, so don't waste time | |
3657 | * trying. | |
2bd0ea18 | 3658 | */ |
3ed4e682 | 3659 | if (ap->aeof && !(ap->tp->t_flags & XFS_TRANS_LOWMODE)) { |
1a5a98d5 | 3660 | error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align, |
451b902f DC |
3661 | false); |
3662 | if (error || args->fsbno != NULLFSBLOCK) | |
3ed4e682 | 3663 | return error; |
49f693fa | 3664 | } |
9542ae13 | 3665 | |
451b902f DC |
3666 | error = xfs_alloc_vextent_start_ag(args, ap->blkno); |
3667 | if (error || args->fsbno != NULLFSBLOCK) | |
49f693fa | 3668 | return error; |
3ed4e682 | 3669 | |
451b902f | 3670 | return xfs_bmap_btalloc_low_space(ap, args); |
3ed4e682 DC |
3671 | } |
3672 | ||
3673 | static int | |
3674 | xfs_bmap_btalloc( | |
3675 | struct xfs_bmalloca *ap) | |
3676 | { | |
3677 | struct xfs_mount *mp = ap->ip->i_mount; | |
3678 | struct xfs_alloc_arg args = { | |
3679 | .tp = ap->tp, | |
3680 | .mp = mp, | |
3681 | .fsbno = NULLFSBLOCK, | |
3682 | .oinfo = XFS_RMAP_OINFO_SKIP_UPDATE, | |
3683 | .minleft = ap->minleft, | |
3684 | .wasdel = ap->wasdel, | |
3685 | .resv = XFS_AG_RESV_NONE, | |
3686 | .datatype = ap->datatype, | |
3687 | .alignment = 1, | |
3688 | .minalignslop = 0, | |
3689 | }; | |
3690 | xfs_fileoff_t orig_offset; | |
3691 | xfs_extlen_t orig_length; | |
3692 | int error; | |
3693 | int stripe_align; | |
3694 | ||
3695 | ASSERT(ap->length); | |
3696 | orig_offset = ap->offset; | |
3697 | orig_length = ap->length; | |
3698 | ||
3699 | stripe_align = xfs_bmap_compute_alignments(ap, &args); | |
3700 | ||
3701 | /* Trim the allocation back to the maximum an AG can fit. */ | |
3702 | args.maxlen = min(ap->length, mp->m_ag_max_usable); | |
3703 | ||
451b902f DC |
3704 | if ((ap->datatype & XFS_ALLOC_USERDATA) && |
3705 | xfs_inode_is_filestream(ap->ip)) | |
3706 | error = xfs_bmap_btalloc_filestreams(ap, &args, stripe_align); | |
3707 | else | |
3708 | error = xfs_bmap_btalloc_best_length(ap, &args, stripe_align); | |
3ed4e682 DC |
3709 | if (error) |
3710 | return error; | |
fc177ab0 | 3711 | |
49f693fa | 3712 | if (args.fsbno != NULLFSBLOCK) { |
fc177ab0 CB |
3713 | xfs_bmap_process_allocated_extent(ap, &args, orig_offset, |
3714 | orig_length); | |
49f693fa DC |
3715 | } else { |
3716 | ap->blkno = NULLFSBLOCK; | |
3717 | ap->length = 0; | |
2bd0ea18 | 3718 | } |
2bd0ea18 | 3719 | return 0; |
56b2de80 DC |
3720 | } |
3721 | ||
b3fd8db7 DW |
3722 | /* Trim extent to fit a logical block range. */ |
3723 | void | |
3724 | xfs_trim_extent( | |
3725 | struct xfs_bmbt_irec *irec, | |
3726 | xfs_fileoff_t bno, | |
3727 | xfs_filblks_t len) | |
3728 | { | |
3729 | xfs_fileoff_t distance; | |
3730 | xfs_fileoff_t end = bno + len; | |
3731 | ||
3732 | if (irec->br_startoff + irec->br_blockcount <= bno || | |
3733 | irec->br_startoff >= end) { | |
3734 | irec->br_blockcount = 0; | |
3735 | return; | |
3736 | } | |
3737 | ||
3738 | if (irec->br_startoff < bno) { | |
3739 | distance = bno - irec->br_startoff; | |
3740 | if (isnullstartblock(irec->br_startblock)) | |
3741 | irec->br_startblock = DELAYSTARTBLOCK; | |
3742 | if (irec->br_startblock != DELAYSTARTBLOCK && | |
3743 | irec->br_startblock != HOLESTARTBLOCK) | |
3744 | irec->br_startblock += distance; | |
3745 | irec->br_startoff += distance; | |
3746 | irec->br_blockcount -= distance; | |
3747 | } | |
3748 | ||
3749 | if (end < irec->br_startoff + irec->br_blockcount) { | |
3750 | distance = irec->br_startoff + irec->br_blockcount - end; | |
3751 | irec->br_blockcount -= distance; | |
3752 | } | |
3753 | } | |
3754 | ||
2bd0ea18 | 3755 | /* |
a2ceac1f | 3756 | * Trim the returned map to the required bounds |
2bd0ea18 | 3757 | */ |
a2ceac1f DC |
3758 | STATIC void |
3759 | xfs_bmapi_trim_map( | |
3760 | struct xfs_bmbt_irec *mval, | |
3761 | struct xfs_bmbt_irec *got, | |
3762 | xfs_fileoff_t *bno, | |
3763 | xfs_filblks_t len, | |
3764 | xfs_fileoff_t obno, | |
3765 | xfs_fileoff_t end, | |
3766 | int n, | |
6e22af31 | 3767 | uint32_t flags) |
2bd0ea18 | 3768 | { |
a2ceac1f DC |
3769 | if ((flags & XFS_BMAPI_ENTIRE) || |
3770 | got->br_startoff + got->br_blockcount <= obno) { | |
3771 | *mval = *got; | |
3772 | if (isnullstartblock(got->br_startblock)) | |
3773 | mval->br_startblock = DELAYSTARTBLOCK; | |
3774 | return; | |
63be04eb | 3775 | } |
a2ceac1f DC |
3776 | |
3777 | if (obno > *bno) | |
3778 | *bno = obno; | |
3779 | ASSERT((*bno >= obno) || (n == 0)); | |
3780 | ASSERT(*bno < end); | |
3781 | mval->br_startoff = *bno; | |
3782 | if (isnullstartblock(got->br_startblock)) | |
3783 | mval->br_startblock = DELAYSTARTBLOCK; | |
2bd0ea18 | 3784 | else |
a2ceac1f DC |
3785 | mval->br_startblock = got->br_startblock + |
3786 | (*bno - got->br_startoff); | |
2bd0ea18 | 3787 | /* |
a2ceac1f DC |
3788 | * Return the minimum of what we got and what we asked for for |
3789 | * the length. We can use the len variable here because it is | |
3790 | * modified below and we could have been there before coming | |
3791 | * here if the first part of the allocation didn't overlap what | |
3792 | * was asked for. | |
2bd0ea18 | 3793 | */ |
a2ceac1f DC |
3794 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
3795 | got->br_blockcount - (*bno - got->br_startoff)); | |
3796 | mval->br_state = got->br_state; | |
3797 | ASSERT(mval->br_blockcount <= len); | |
3798 | return; | |
3799 | } | |
56b2de80 | 3800 | |
a2ceac1f DC |
3801 | /* |
3802 | * Update and validate the extent map to return | |
3803 | */ | |
3804 | STATIC void | |
3805 | xfs_bmapi_update_map( | |
3806 | struct xfs_bmbt_irec **map, | |
3807 | xfs_fileoff_t *bno, | |
3808 | xfs_filblks_t *len, | |
3809 | xfs_fileoff_t obno, | |
3810 | xfs_fileoff_t end, | |
3811 | int *n, | |
6e22af31 | 3812 | uint32_t flags) |
a2ceac1f DC |
3813 | { |
3814 | xfs_bmbt_irec_t *mval = *map; | |
3815 | ||
3816 | ASSERT((flags & XFS_BMAPI_ENTIRE) || | |
3817 | ((mval->br_startoff + mval->br_blockcount) <= end)); | |
3818 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || | |
3819 | (mval->br_startoff < obno)); | |
3820 | ||
3821 | *bno = mval->br_startoff + mval->br_blockcount; | |
3822 | *len = end - *bno; | |
3823 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { | |
3824 | /* update previous map with new information */ | |
3825 | ASSERT(mval->br_startblock == mval[-1].br_startblock); | |
3826 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); | |
3827 | ASSERT(mval->br_state == mval[-1].br_state); | |
3828 | mval[-1].br_blockcount = mval->br_blockcount; | |
3829 | mval[-1].br_state = mval->br_state; | |
3830 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && | |
3831 | mval[-1].br_startblock != DELAYSTARTBLOCK && | |
3832 | mval[-1].br_startblock != HOLESTARTBLOCK && | |
3833 | mval->br_startblock == mval[-1].br_startblock + | |
3834 | mval[-1].br_blockcount && | |
8197bceb | 3835 | mval[-1].br_state == mval->br_state) { |
a2ceac1f DC |
3836 | ASSERT(mval->br_startoff == |
3837 | mval[-1].br_startoff + mval[-1].br_blockcount); | |
3838 | mval[-1].br_blockcount += mval->br_blockcount; | |
3839 | } else if (*n > 0 && | |
3840 | mval->br_startblock == DELAYSTARTBLOCK && | |
3841 | mval[-1].br_startblock == DELAYSTARTBLOCK && | |
3842 | mval->br_startoff == | |
3843 | mval[-1].br_startoff + mval[-1].br_blockcount) { | |
3844 | mval[-1].br_blockcount += mval->br_blockcount; | |
3845 | mval[-1].br_state = mval->br_state; | |
3846 | } else if (!((*n == 0) && | |
3847 | ((mval->br_startoff + mval->br_blockcount) <= | |
3848 | obno))) { | |
3849 | mval++; | |
3850 | (*n)++; | |
3851 | } | |
3852 | *map = mval; | |
3853 | } | |
399ab595 | 3854 | |
a2ceac1f DC |
3855 | /* |
3856 | * Map file blocks to filesystem blocks without allocation. | |
3857 | */ | |
3858 | int | |
3859 | xfs_bmapi_read( | |
3860 | struct xfs_inode *ip, | |
3861 | xfs_fileoff_t bno, | |
3862 | xfs_filblks_t len, | |
3863 | struct xfs_bmbt_irec *mval, | |
3864 | int *nmap, | |
6e22af31 | 3865 | uint32_t flags) |
a2ceac1f DC |
3866 | { |
3867 | struct xfs_mount *mp = ip->i_mount; | |
212be827 | 3868 | int whichfork = xfs_bmapi_whichfork(flags); |
722e81c1 | 3869 | struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); |
a2ceac1f | 3870 | struct xfs_bmbt_irec got; |
a2ceac1f DC |
3871 | xfs_fileoff_t obno; |
3872 | xfs_fileoff_t end; | |
9788e059 | 3873 | struct xfs_iext_cursor icur; |
a2ceac1f | 3874 | int error; |
c2d73ed3 | 3875 | bool eof = false; |
a2ceac1f | 3876 | int n = 0; |
399ab595 | 3877 | |
a2ceac1f | 3878 | ASSERT(*nmap >= 1); |
00773e64 | 3879 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE))); |
ff105f75 | 3880 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
062998e3 | 3881 | |
212be827 CH |
3882 | if (WARN_ON_ONCE(!ifp)) |
3883 | return -EFSCORRUPTED; | |
3884 | ||
d967a68d CH |
3885 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || |
3886 | XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) | |
12b53197 | 3887 | return -EFSCORRUPTED; |
062998e3 | 3888 | |
93adb06a | 3889 | if (xfs_is_shutdown(mp)) |
12b53197 | 3890 | return -EIO; |
399ab595 | 3891 | |
79896434 | 3892 | XFS_STATS_INC(mp, xs_blk_mapr); |
a2ceac1f | 3893 | |
e00c57e7 CH |
3894 | error = xfs_iread_extents(NULL, ip, whichfork); |
3895 | if (error) | |
3896 | return error; | |
a2ceac1f | 3897 | |
9788e059 | 3898 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) |
c2d73ed3 | 3899 | eof = true; |
a2ceac1f DC |
3900 | end = bno + len; |
3901 | obno = bno; | |
3902 | ||
3903 | while (bno < end && n < *nmap) { | |
3904 | /* Reading past eof, act as though there's a hole up to end. */ | |
3905 | if (eof) | |
3906 | got.br_startoff = end; | |
3907 | if (got.br_startoff > bno) { | |
3908 | /* Reading in a hole. */ | |
2bd0ea18 NS |
3909 | mval->br_startoff = bno; |
3910 | mval->br_startblock = HOLESTARTBLOCK; | |
3911 | mval->br_blockcount = | |
3912 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); | |
3913 | mval->br_state = XFS_EXT_NORM; | |
3914 | bno += mval->br_blockcount; | |
3915 | len -= mval->br_blockcount; | |
3916 | mval++; | |
3917 | n++; | |
3918 | continue; | |
3919 | } | |
a2ceac1f DC |
3920 | |
3921 | /* set up the extent map to return. */ | |
3922 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); | |
3923 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); | |
3924 | ||
3925 | /* If we're done, stop now. */ | |
3926 | if (bno >= end || n >= *nmap) | |
3927 | break; | |
3928 | ||
3929 | /* Else go on to the next record. */ | |
9788e059 | 3930 | if (!xfs_iext_next_extent(ifp, &icur, &got)) |
c2d73ed3 | 3931 | eof = true; |
a2ceac1f DC |
3932 | } |
3933 | *nmap = n; | |
3934 | return 0; | |
3935 | } | |
3936 | ||
7a868ee8 BF |
3937 | /* |
3938 | * Add a delayed allocation extent to an inode. Blocks are reserved from the | |
3939 | * global pool and the extent inserted into the inode in-core extent tree. | |
3940 | * | |
3941 | * On entry, got refers to the first extent beyond the offset of the extent to | |
3942 | * allocate or eof is specified if no such extent exists. On return, got refers | |
3943 | * to the extent record that was inserted to the inode fork. | |
3944 | * | |
3945 | * Note that the allocated extent may have been merged with contiguous extents | |
3946 | * during insertion into the inode fork. Thus, got does not reflect the current | |
3947 | * state of the inode fork on return. If necessary, the caller can use lastx to | |
3948 | * look up the updated record in the inode fork. | |
3949 | */ | |
4488e421 | 3950 | int |
a2ceac1f DC |
3951 | xfs_bmapi_reserve_delalloc( |
3952 | struct xfs_inode *ip, | |
cc66acaf | 3953 | int whichfork, |
f7b1a8b1 | 3954 | xfs_fileoff_t off, |
a2ceac1f | 3955 | xfs_filblks_t len, |
f7b1a8b1 | 3956 | xfs_filblks_t prealloc, |
a2ceac1f | 3957 | struct xfs_bmbt_irec *got, |
9788e059 | 3958 | struct xfs_iext_cursor *icur, |
a2ceac1f DC |
3959 | int eof) |
3960 | { | |
3961 | struct xfs_mount *mp = ip->i_mount; | |
722e81c1 | 3962 | struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); |
a2ceac1f DC |
3963 | xfs_extlen_t alen; |
3964 | xfs_extlen_t indlen; | |
a2ceac1f | 3965 | int error; |
f7b1a8b1 | 3966 | xfs_fileoff_t aoff = off; |
a2ceac1f | 3967 | |
f7b1a8b1 BF |
3968 | /* |
3969 | * Cap the alloc length. Keep track of prealloc so we know whether to | |
3970 | * tag the inode before we return. | |
3971 | */ | |
d3e0c71f | 3972 | alen = XFS_FILBLKS_MIN(len + prealloc, XFS_MAX_BMBT_EXTLEN); |
a2ceac1f DC |
3973 | if (!eof) |
3974 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); | |
f7b1a8b1 BF |
3975 | if (prealloc && alen >= len) |
3976 | prealloc = alen - len; | |
a2ceac1f DC |
3977 | |
3978 | /* Figure out the extent size, adjust alen */ | |
885ba5ce | 3979 | if (whichfork == XFS_COW_FORK) { |
d41d2303 | 3980 | struct xfs_bmbt_irec prev; |
885ba5ce | 3981 | xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip); |
d41d2303 | 3982 | |
9788e059 | 3983 | if (!xfs_iext_peek_prev_extent(ifp, icur, &prev)) |
d41d2303 CH |
3984 | prev.br_startoff = NULLFILEOFF; |
3985 | ||
885ba5ce | 3986 | error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof, |
a2ceac1f DC |
3987 | 1, 0, &aoff, &alen); |
3988 | ASSERT(!error); | |
3989 | } | |
3990 | ||
a2ceac1f DC |
3991 | /* |
3992 | * Make a transaction-less quota reservation for delayed allocation | |
3993 | * blocks. This number gets adjusted later. We return if we haven't | |
3994 | * allocated blocks already inside this loop. | |
3995 | */ | |
9fcc3af9 | 3996 | error = xfs_quota_reserve_blkres(ip, alen); |
a2ceac1f DC |
3997 | if (error) |
3998 | return error; | |
3999 | ||
4000 | /* | |
4001 | * Split changing sb for alen and indlen since they could be coming | |
4002 | * from different places. | |
4003 | */ | |
4004 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); | |
4005 | ASSERT(indlen > 0); | |
4006 | ||
885ba5ce | 4007 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
a2ceac1f DC |
4008 | if (error) |
4009 | goto out_unreserve_quota; | |
4010 | ||
19ebedcf | 4011 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
a2ceac1f DC |
4012 | if (error) |
4013 | goto out_unreserve_blocks; | |
4014 | ||
4015 | ||
4016 | ip->i_delayed_blks += alen; | |
f73690fe | 4017 | xfs_mod_delalloc(ip->i_mount, alen + indlen); |
a2ceac1f DC |
4018 | |
4019 | got->br_startoff = aoff; | |
4020 | got->br_startblock = nullstartblock(indlen); | |
4021 | got->br_blockcount = alen; | |
4022 | got->br_state = XFS_EXT_NORM; | |
a2ceac1f | 4023 | |
9788e059 | 4024 | xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got); |
a2ceac1f | 4025 | |
f7b1a8b1 BF |
4026 | /* |
4027 | * Tag the inode if blocks were preallocated. Note that COW fork | |
4028 | * preallocation can occur at the start or end of the extent, even when | |
4029 | * prealloc == 0, so we must also check the aligned offset and length. | |
4030 | */ | |
4031 | if (whichfork == XFS_DATA_FORK && prealloc) | |
4032 | xfs_inode_set_eofblocks_tag(ip); | |
4033 | if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) | |
4034 | xfs_inode_set_cowblocks_tag(ip); | |
4035 | ||
a2ceac1f DC |
4036 | return 0; |
4037 | ||
4038 | out_unreserve_blocks: | |
885ba5ce | 4039 | xfs_mod_fdblocks(mp, alen, false); |
a2ceac1f DC |
4040 | out_unreserve_quota: |
4041 | if (XFS_IS_QUOTA_ON(mp)) | |
9fcc3af9 | 4042 | xfs_quota_unreserve_blkres(ip, alen); |
a2ceac1f DC |
4043 | return error; |
4044 | } | |
4045 | ||
2ac7663a CH |
4046 | static int |
4047 | xfs_bmap_alloc_userdata( | |
4048 | struct xfs_bmalloca *bma) | |
4049 | { | |
4050 | struct xfs_mount *mp = bma->ip->i_mount; | |
4051 | int whichfork = xfs_bmapi_whichfork(bma->flags); | |
4052 | int error; | |
4053 | ||
4054 | /* | |
4055 | * Set the data type being allocated. For the data fork, the first data | |
4056 | * in the file is treated differently to all other allocations. For the | |
4057 | * attribute fork, we only need to ensure the allocated range is not on | |
4058 | * the busy list. | |
4059 | */ | |
4060 | bma->datatype = XFS_ALLOC_NOBUSY; | |
88765eda | 4061 | if (whichfork == XFS_DATA_FORK || whichfork == XFS_COW_FORK) { |
a85522b6 | 4062 | bma->datatype |= XFS_ALLOC_USERDATA; |
2ac7663a CH |
4063 | if (bma->offset == 0) |
4064 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; | |
2ac7663a CH |
4065 | |
4066 | if (mp->m_dalign && bma->length >= mp->m_dalign) { | |
4067 | error = xfs_bmap_isaeof(bma, whichfork); | |
4068 | if (error) | |
4069 | return error; | |
4070 | } | |
4071 | ||
4072 | if (XFS_IS_REALTIME_INODE(bma->ip)) | |
4073 | return xfs_bmap_rtalloc(bma); | |
4074 | } | |
4075 | ||
3006cea4 CB |
4076 | if (unlikely(XFS_TEST_ERROR(false, mp, |
4077 | XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT))) | |
4078 | return xfs_bmap_exact_minlen_extent_alloc(bma); | |
4079 | ||
2ac7663a CH |
4080 | return xfs_bmap_btalloc(bma); |
4081 | } | |
4082 | ||
ff105f75 DC |
4083 | static int |
4084 | xfs_bmapi_allocate( | |
a2ceac1f DC |
4085 | struct xfs_bmalloca *bma) |
4086 | { | |
4087 | struct xfs_mount *mp = bma->ip->i_mount; | |
1277a5e0 | 4088 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
722e81c1 | 4089 | struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork); |
a2ceac1f DC |
4090 | int tmp_logflags = 0; |
4091 | int error; | |
4092 | ||
4093 | ASSERT(bma->length > 0); | |
4094 | ||
4095 | /* | |
4096 | * For the wasdelay case, we could also just allocate the stuff asked | |
4097 | * for in this bmap call but that wouldn't be as good. | |
4098 | */ | |
4099 | if (bma->wasdel) { | |
4100 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; | |
4101 | bma->offset = bma->got.br_startoff; | |
530bc0fc DW |
4102 | if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev)) |
4103 | bma->prev.br_startoff = NULLFILEOFF; | |
a2ceac1f | 4104 | } else { |
d3e0c71f | 4105 | bma->length = XFS_FILBLKS_MIN(bma->length, XFS_MAX_BMBT_EXTLEN); |
a2ceac1f DC |
4106 | if (!bma->eof) |
4107 | bma->length = XFS_FILBLKS_MIN(bma->length, | |
4108 | bma->got.br_startoff - bma->offset); | |
4109 | } | |
4110 | ||
2ac7663a CH |
4111 | if (bma->flags & XFS_BMAPI_CONTIG) |
4112 | bma->minlen = bma->length; | |
4113 | else | |
4114 | bma->minlen = 1; | |
a2ceac1f | 4115 | |
3006cea4 CB |
4116 | if (bma->flags & XFS_BMAPI_METADATA) { |
4117 | if (unlikely(XFS_TEST_ERROR(false, mp, | |
4118 | XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT))) | |
4119 | error = xfs_bmap_exact_minlen_extent_alloc(bma); | |
4120 | else | |
4121 | error = xfs_bmap_btalloc(bma); | |
4122 | } else { | |
2ac7663a | 4123 | error = xfs_bmap_alloc_userdata(bma); |
3006cea4 | 4124 | } |
2ac7663a | 4125 | if (error || bma->blkno == NULLFSBLOCK) |
a2ceac1f DC |
4126 | return error; |
4127 | ||
c3a24cde CH |
4128 | if (bma->flags & XFS_BMAPI_ZERO) { |
4129 | error = xfs_zero_extent(bma->ip, bma->blkno, bma->length); | |
4130 | if (error) | |
4131 | return error; | |
4132 | } | |
4133 | ||
84094546 | 4134 | if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) |
a2ceac1f | 4135 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
a2ceac1f DC |
4136 | /* |
4137 | * Bump the number of extents we've allocated | |
4138 | * in this call. | |
4139 | */ | |
4140 | bma->nallocs++; | |
4141 | ||
4142 | if (bma->cur) | |
116c6a88 | 4143 | bma->cur->bc_ino.flags = |
bbf943f8 | 4144 | bma->wasdel ? XFS_BTCUR_BMBT_WASDEL : 0; |
a2ceac1f DC |
4145 | |
4146 | bma->got.br_startoff = bma->offset; | |
4147 | bma->got.br_startblock = bma->blkno; | |
4148 | bma->got.br_blockcount = bma->length; | |
4149 | bma->got.br_state = XFS_EXT_NORM; | |
4150 | ||
edc9bb69 | 4151 | if (bma->flags & XFS_BMAPI_PREALLOC) |
a2ceac1f DC |
4152 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
4153 | ||
4154 | if (bma->wasdel) | |
1277a5e0 | 4155 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
a2ceac1f | 4156 | else |
972432f2 | 4157 | error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip, |
9788e059 | 4158 | whichfork, &bma->icur, &bma->cur, &bma->got, |
64e8b4a7 | 4159 | &bma->logflags, bma->flags); |
a2ceac1f DC |
4160 | |
4161 | bma->logflags |= tmp_logflags; | |
4162 | if (error) | |
4163 | return error; | |
4164 | ||
4165 | /* | |
4166 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real | |
4167 | * or xfs_bmap_add_extent_hole_real might have merged it into one of | |
4168 | * the neighbouring ones. | |
4169 | */ | |
9788e059 | 4170 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
a2ceac1f DC |
4171 | |
4172 | ASSERT(bma->got.br_startoff <= bma->offset); | |
4173 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= | |
4174 | bma->offset + bma->length); | |
4175 | ASSERT(bma->got.br_state == XFS_EXT_NORM || | |
4176 | bma->got.br_state == XFS_EXT_UNWRITTEN); | |
4177 | return 0; | |
4178 | } | |
4179 | ||
a2ceac1f DC |
4180 | STATIC int |
4181 | xfs_bmapi_convert_unwritten( | |
4182 | struct xfs_bmalloca *bma, | |
4183 | struct xfs_bmbt_irec *mval, | |
4184 | xfs_filblks_t len, | |
6e22af31 | 4185 | uint32_t flags) |
a2ceac1f | 4186 | { |
cb8a004a | 4187 | int whichfork = xfs_bmapi_whichfork(flags); |
722e81c1 | 4188 | struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork); |
a2ceac1f DC |
4189 | int tmp_logflags = 0; |
4190 | int error; | |
4191 | ||
4192 | /* check if we need to do unwritten->real conversion */ | |
4193 | if (mval->br_state == XFS_EXT_UNWRITTEN && | |
4194 | (flags & XFS_BMAPI_PREALLOC)) | |
4195 | return 0; | |
4196 | ||
4197 | /* check if we need to do real->unwritten conversion */ | |
4198 | if (mval->br_state == XFS_EXT_NORM && | |
4199 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != | |
4200 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) | |
4201 | return 0; | |
4202 | ||
4203 | /* | |
4204 | * Modify (by adding) the state flag, if writing. | |
4205 | */ | |
4206 | ASSERT(mval->br_blockcount <= len); | |
84094546 | 4207 | if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) { |
a2ceac1f DC |
4208 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
4209 | bma->ip, whichfork); | |
a2ceac1f DC |
4210 | } |
4211 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) | |
4212 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; | |
4213 | ||
9542ae13 DC |
4214 | /* |
4215 | * Before insertion into the bmbt, zero the range being converted | |
4216 | * if required. | |
4217 | */ | |
4218 | if (flags & XFS_BMAPI_ZERO) { | |
4219 | error = xfs_zero_extent(bma->ip, mval->br_startblock, | |
4220 | mval->br_blockcount); | |
4221 | if (error) | |
4222 | return error; | |
4223 | } | |
4224 | ||
4072e4b4 | 4225 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork, |
64e8b4a7 | 4226 | &bma->icur, &bma->cur, mval, &tmp_logflags); |
23fc058d BF |
4227 | /* |
4228 | * Log the inode core unconditionally in the unwritten extent conversion | |
4229 | * path because the conversion might not have done so (e.g., if the | |
4230 | * extent count hasn't changed). We need to make sure the inode is dirty | |
4231 | * in the transaction for the sake of fsync(), even if nothing has | |
4232 | * changed, because fsync() will not force the log for this transaction | |
4233 | * unless it sees the inode pinned. | |
4072e4b4 DW |
4234 | * |
4235 | * Note: If we're only converting cow fork extents, there aren't | |
4236 | * any on-disk updates to make, so we don't need to log anything. | |
23fc058d | 4237 | */ |
4072e4b4 DW |
4238 | if (whichfork != XFS_COW_FORK) |
4239 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; | |
a2ceac1f DC |
4240 | if (error) |
4241 | return error; | |
4242 | ||
4243 | /* | |
4244 | * Update our extent pointer, given that | |
4245 | * xfs_bmap_add_extent_unwritten_real might have merged it into one | |
4246 | * of the neighbouring ones. | |
4247 | */ | |
9788e059 | 4248 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
a2ceac1f DC |
4249 | |
4250 | /* | |
4251 | * We may have combined previously unwritten space with written space, | |
4252 | * so generate another request. | |
4253 | */ | |
4254 | if (mval->br_blockcount < len) | |
12b53197 | 4255 | return -EAGAIN; |
a2ceac1f DC |
4256 | return 0; |
4257 | } | |
4258 | ||
4442ae95 | 4259 | xfs_extlen_t |
ee622798 CH |
4260 | xfs_bmapi_minleft( |
4261 | struct xfs_trans *tp, | |
4262 | struct xfs_inode *ip, | |
4263 | int fork) | |
4264 | { | |
722e81c1 | 4265 | struct xfs_ifork *ifp = xfs_ifork_ptr(ip, fork); |
d967a68d | 4266 | |
9cdecd7e | 4267 | if (tp && tp->t_highest_agno != NULLAGNUMBER) |
ee622798 | 4268 | return 0; |
d967a68d | 4269 | if (ifp->if_format != XFS_DINODE_FMT_BTREE) |
ee622798 | 4270 | return 1; |
d967a68d | 4271 | return be16_to_cpu(ifp->if_broot->bb_level) + 1; |
ee622798 CH |
4272 | } |
4273 | ||
4274 | /* | |
4275 | * Log whatever the flags say, even if error. Otherwise we might miss detecting | |
4276 | * a case where the data is changed, there's an error, and it's not logged so we | |
4277 | * don't shutdown when we should. Don't bother logging extents/btree changes if | |
4278 | * we converted to the other format. | |
4279 | */ | |
4280 | static void | |
4281 | xfs_bmapi_finish( | |
4282 | struct xfs_bmalloca *bma, | |
4283 | int whichfork, | |
4284 | int error) | |
4285 | { | |
722e81c1 | 4286 | struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork); |
d967a68d | 4287 | |
ee622798 | 4288 | if ((bma->logflags & xfs_ilog_fext(whichfork)) && |
d967a68d | 4289 | ifp->if_format != XFS_DINODE_FMT_EXTENTS) |
ee622798 CH |
4290 | bma->logflags &= ~xfs_ilog_fext(whichfork); |
4291 | else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) && | |
d967a68d | 4292 | ifp->if_format != XFS_DINODE_FMT_BTREE) |
ee622798 CH |
4293 | bma->logflags &= ~xfs_ilog_fbroot(whichfork); |
4294 | ||
4295 | if (bma->logflags) | |
4296 | xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags); | |
4297 | if (bma->cur) | |
4298 | xfs_btree_del_cursor(bma->cur, error); | |
4299 | } | |
4300 | ||
a2ceac1f DC |
4301 | /* |
4302 | * Map file blocks to filesystem blocks, and allocate blocks or convert the | |
4303 | * extent state if necessary. Details behaviour is controlled by the flags | |
4304 | * parameter. Only allocates blocks from a single allocation group, to avoid | |
4305 | * locking problems. | |
a2ceac1f DC |
4306 | */ |
4307 | int | |
4308 | xfs_bmapi_write( | |
4309 | struct xfs_trans *tp, /* transaction pointer */ | |
4310 | struct xfs_inode *ip, /* incore inode */ | |
4311 | xfs_fileoff_t bno, /* starting file offs. mapped */ | |
4312 | xfs_filblks_t len, /* length to map in file */ | |
6e22af31 | 4313 | uint32_t flags, /* XFS_BMAPI_... */ |
a2ceac1f DC |
4314 | xfs_extlen_t total, /* total blocks needed */ |
4315 | struct xfs_bmbt_irec *mval, /* output: map values */ | |
cbdfb3ab | 4316 | int *nmap) /* i/o: mval size/count */ |
a2ceac1f | 4317 | { |
93ad4d9c DW |
4318 | struct xfs_bmalloca bma = { |
4319 | .tp = tp, | |
4320 | .ip = ip, | |
4321 | .total = total, | |
4322 | }; | |
a2ceac1f | 4323 | struct xfs_mount *mp = ip->i_mount; |
d967a68d | 4324 | int whichfork = xfs_bmapi_whichfork(flags); |
722e81c1 | 4325 | struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); |
a2ceac1f | 4326 | xfs_fileoff_t end; /* end of mapped file region */ |
82411945 | 4327 | bool eof = false; /* after the end of extents */ |
a2ceac1f DC |
4328 | int error; /* error return */ |
4329 | int n; /* current extent index */ | |
4330 | xfs_fileoff_t obno; /* old block number (offset) */ | |
a2ceac1f DC |
4331 | |
4332 | #ifdef DEBUG | |
4333 | xfs_fileoff_t orig_bno; /* original block number value */ | |
4334 | int orig_flags; /* original flags arg value */ | |
4335 | xfs_filblks_t orig_len; /* original value of len arg */ | |
4336 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ | |
4337 | int orig_nmap; /* original value of *nmap */ | |
4338 | ||
4339 | orig_bno = bno; | |
4340 | orig_len = len; | |
4341 | orig_flags = flags; | |
4342 | orig_mval = mval; | |
4343 | orig_nmap = *nmap; | |
4344 | #endif | |
4345 | ||
4346 | ASSERT(*nmap >= 1); | |
4347 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); | |
9fa4db19 | 4348 | ASSERT(tp != NULL); |
a2ceac1f | 4349 | ASSERT(len > 0); |
d967a68d | 4350 | ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL); |
ff105f75 | 4351 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
0b44aa85 | 4352 | ASSERT(!(flags & XFS_BMAPI_REMAP)); |
a2ceac1f | 4353 | |
9542ae13 DC |
4354 | /* zeroing is for currently only for data extents, not metadata */ |
4355 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != | |
4356 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); | |
4357 | /* | |
4358 | * we can allocate unwritten extents or pre-zero allocated blocks, | |
4359 | * but it makes no sense to do both at once. This would result in | |
4360 | * zeroing the unwritten extent twice, but it still being an | |
4361 | * unwritten extent.... | |
4362 | */ | |
4363 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != | |
4364 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); | |
4365 | ||
d967a68d | 4366 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || |
bc73da84 | 4367 | XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { |
12b53197 | 4368 | return -EFSCORRUPTED; |
a2ceac1f DC |
4369 | } |
4370 | ||
93adb06a | 4371 | if (xfs_is_shutdown(mp)) |
12b53197 | 4372 | return -EIO; |
a2ceac1f | 4373 | |
79896434 | 4374 | XFS_STATS_INC(mp, xs_blk_mapw); |
a2ceac1f | 4375 | |
e00c57e7 CH |
4376 | error = xfs_iread_extents(tp, ip, whichfork); |
4377 | if (error) | |
4378 | goto error0; | |
a2ceac1f | 4379 | |
9788e059 | 4380 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got)) |
82411945 | 4381 | eof = true; |
9788e059 | 4382 | if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) |
82411945 | 4383 | bma.prev.br_startoff = NULLFILEOFF; |
ee622798 | 4384 | bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork); |
a2ceac1f | 4385 | |
badaa597 BF |
4386 | n = 0; |
4387 | end = bno + len; | |
4388 | obno = bno; | |
a2ceac1f | 4389 | while (bno < end && n < *nmap) { |
7075a23f CH |
4390 | bool need_alloc = false, wasdelay = false; |
4391 | ||
faaad1df | 4392 | /* in hole or beyond EOF? */ |
7075a23f | 4393 | if (eof || bma.got.br_startoff > bno) { |
faaad1df DW |
4394 | /* |
4395 | * CoW fork conversions should /never/ hit EOF or | |
4396 | * holes. There should always be something for us | |
4397 | * to work on. | |
4398 | */ | |
4399 | ASSERT(!((flags & XFS_BMAPI_CONVERT) && | |
4400 | (flags & XFS_BMAPI_COWFORK))); | |
4401 | ||
330a35fe | 4402 | need_alloc = true; |
0b44aa85 CH |
4403 | } else if (isnullstartblock(bma.got.br_startblock)) { |
4404 | wasdelay = true; | |
7075a23f | 4405 | } |
34621a47 | 4406 | |
2bd0ea18 | 4407 | /* |
a2ceac1f DC |
4408 | * First, deal with the hole before the allocated space |
4409 | * that we found, if any. | |
2bd0ea18 | 4410 | */ |
9fa4db19 | 4411 | if (need_alloc || wasdelay) { |
a2ceac1f DC |
4412 | bma.eof = eof; |
4413 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); | |
4414 | bma.wasdel = wasdelay; | |
4415 | bma.offset = bno; | |
4416 | bma.flags = flags; | |
4417 | ||
2bd0ea18 | 4418 | /* |
a2ceac1f DC |
4419 | * There's a 32/64 bit type mismatch between the |
4420 | * allocation length request (which can be 64 bits in | |
4421 | * length) and the bma length request, which is | |
4422 | * xfs_extlen_t and therefore 32 bits. Hence we have to | |
4423 | * check for 32-bit overflows and handle them here. | |
2bd0ea18 | 4424 | */ |
d3e0c71f CB |
4425 | if (len > (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN) |
4426 | bma.length = XFS_MAX_BMBT_EXTLEN; | |
a2ceac1f DC |
4427 | else |
4428 | bma.length = len; | |
4429 | ||
4430 | ASSERT(len > 0); | |
4431 | ASSERT(bma.length > 0); | |
4432 | error = xfs_bmapi_allocate(&bma); | |
2bd0ea18 NS |
4433 | if (error) |
4434 | goto error0; | |
a2ceac1f DC |
4435 | if (bma.blkno == NULLFSBLOCK) |
4436 | break; | |
10e65503 DW |
4437 | |
4438 | /* | |
4439 | * If this is a CoW allocation, record the data in | |
4440 | * the refcount btree for orphan recovery. | |
4441 | */ | |
5965a482 DW |
4442 | if (whichfork == XFS_COW_FORK) |
4443 | xfs_refcount_alloc_cow_extent(tp, bma.blkno, | |
4444 | bma.length); | |
2bd0ea18 NS |
4445 | } |
4446 | ||
a2ceac1f DC |
4447 | /* Deal with the allocated space we found. */ |
4448 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, | |
4449 | end, n, flags); | |
4450 | ||
4451 | /* Execute unwritten extent conversion if necessary */ | |
4452 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); | |
12b53197 | 4453 | if (error == -EAGAIN) |
a2ceac1f DC |
4454 | continue; |
4455 | if (error) | |
4456 | goto error0; | |
4457 | ||
4458 | /* update the extent map to return */ | |
4459 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); | |
4460 | ||
2bd0ea18 NS |
4461 | /* |
4462 | * If we're done, stop now. Stop when we've allocated | |
4463 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise | |
4464 | * the transaction may get too big. | |
4465 | */ | |
a2ceac1f | 4466 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
2bd0ea18 | 4467 | break; |
a2ceac1f DC |
4468 | |
4469 | /* Else go on to the next record. */ | |
4470 | bma.prev = bma.got; | |
9788e059 | 4471 | if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got)) |
82411945 | 4472 | eof = true; |
2bd0ea18 | 4473 | } |
2bd0ea18 | 4474 | *nmap = n; |
a2ceac1f | 4475 | |
939ebc1a CH |
4476 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags, |
4477 | whichfork); | |
4478 | if (error) | |
4479 | goto error0; | |
a2ceac1f | 4480 | |
d967a68d | 4481 | ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE || |
87c472b7 | 4482 | ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork)); |
ee622798 CH |
4483 | xfs_bmapi_finish(&bma, whichfork, 0); |
4484 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, | |
4485 | orig_nmap, *nmap); | |
4486 | return 0; | |
2bd0ea18 | 4487 | error0: |
ee622798 | 4488 | xfs_bmapi_finish(&bma, whichfork, error); |
2bd0ea18 NS |
4489 | return error; |
4490 | } | |
4491 | ||
badaa597 BF |
4492 | /* |
4493 | * Convert an existing delalloc extent to real blocks based on file offset. This | |
4494 | * attempts to allocate the entire delalloc extent and may require multiple | |
4495 | * invocations to allocate the target offset if a large enough physical extent | |
4496 | * is not available. | |
4497 | */ | |
4498 | int | |
4499 | xfs_bmapi_convert_delalloc( | |
badaa597 | 4500 | struct xfs_inode *ip, |
badaa597 | 4501 | int whichfork, |
75f533e6 CH |
4502 | xfs_off_t offset, |
4503 | struct iomap *iomap, | |
c784c9d2 | 4504 | unsigned int *seq) |
badaa597 | 4505 | { |
722e81c1 | 4506 | struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); |
c784c9d2 | 4507 | struct xfs_mount *mp = ip->i_mount; |
75f533e6 | 4508 | xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); |
330a35fe | 4509 | struct xfs_bmalloca bma = { NULL }; |
75f533e6 | 4510 | uint16_t flags = 0; |
c784c9d2 | 4511 | struct xfs_trans *tp; |
badaa597 | 4512 | int error; |
badaa597 | 4513 | |
75f533e6 CH |
4514 | if (whichfork == XFS_COW_FORK) |
4515 | flags |= IOMAP_F_SHARED; | |
4516 | ||
c784c9d2 CH |
4517 | /* |
4518 | * Space for the extent and indirect blocks was reserved when the | |
4519 | * delalloc extent was created so there's no need to do so here. | |
4520 | */ | |
4521 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, | |
4522 | XFS_TRANS_RESERVE, &tp); | |
4523 | if (error) | |
4524 | return error; | |
4525 | ||
4526 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
fcba1629 | 4527 | xfs_trans_ijoin(tp, ip, 0); |
75cb5a60 CB |
4528 | |
4529 | error = xfs_iext_count_may_overflow(ip, whichfork, | |
4530 | XFS_IEXT_ADD_NOSPLIT_CNT); | |
fcba1629 CB |
4531 | if (error == -EFBIG) |
4532 | error = xfs_iext_count_upgrade(tp, ip, | |
4533 | XFS_IEXT_ADD_NOSPLIT_CNT); | |
75cb5a60 CB |
4534 | if (error) |
4535 | goto out_trans_cancel; | |
4536 | ||
330a35fe CH |
4537 | if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) || |
4538 | bma.got.br_startoff > offset_fsb) { | |
4539 | /* | |
4540 | * No extent found in the range we are trying to convert. This | |
4541 | * should only happen for the COW fork, where another thread | |
4542 | * might have moved the extent to the data fork in the meantime. | |
4543 | */ | |
4544 | WARN_ON_ONCE(whichfork != XFS_COW_FORK); | |
c784c9d2 CH |
4545 | error = -EAGAIN; |
4546 | goto out_trans_cancel; | |
330a35fe | 4547 | } |
badaa597 BF |
4548 | |
4549 | /* | |
330a35fe CH |
4550 | * If we find a real extent here we raced with another thread converting |
4551 | * the extent. Just return the real extent at this offset. | |
badaa597 | 4552 | */ |
330a35fe | 4553 | if (!isnullstartblock(bma.got.br_startblock)) { |
1dcdf505 DC |
4554 | xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags, |
4555 | xfs_iomap_inode_sequence(ip, flags)); | |
c784c9d2 CH |
4556 | *seq = READ_ONCE(ifp->if_seq); |
4557 | goto out_trans_cancel; | |
330a35fe CH |
4558 | } |
4559 | ||
4560 | bma.tp = tp; | |
4561 | bma.ip = ip; | |
4562 | bma.wasdel = true; | |
4563 | bma.offset = bma.got.br_startoff; | |
d3e0c71f CB |
4564 | bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount, |
4565 | XFS_MAX_BMBT_EXTLEN); | |
330a35fe | 4566 | bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork); |
edc9bb69 DW |
4567 | |
4568 | /* | |
4569 | * When we're converting the delalloc reservations backing dirty pages | |
4570 | * in the page cache, we must be careful about how we create the new | |
4571 | * extents: | |
4572 | * | |
4573 | * New CoW fork extents are created unwritten, turned into real extents | |
4574 | * when we're about to write the data to disk, and mapped into the data | |
4575 | * fork after the write finishes. End of story. | |
4576 | * | |
4577 | * New data fork extents must be mapped in as unwritten and converted | |
4578 | * to real extents after the write succeeds to avoid exposing stale | |
4579 | * disk contents if we crash. | |
4580 | */ | |
4581 | bma.flags = XFS_BMAPI_PREALLOC; | |
330a35fe | 4582 | if (whichfork == XFS_COW_FORK) |
edc9bb69 | 4583 | bma.flags |= XFS_BMAPI_COWFORK; |
330a35fe CH |
4584 | |
4585 | if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) | |
4586 | bma.prev.br_startoff = NULLFILEOFF; | |
4587 | ||
4588 | error = xfs_bmapi_allocate(&bma); | |
4589 | if (error) | |
4590 | goto out_finish; | |
4591 | ||
4592 | error = -ENOSPC; | |
4593 | if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK)) | |
4594 | goto out_finish; | |
4595 | error = -EFSCORRUPTED; | |
14790ed0 | 4596 | if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock))) |
330a35fe CH |
4597 | goto out_finish; |
4598 | ||
44e165df CH |
4599 | XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length)); |
4600 | XFS_STATS_INC(mp, xs_xstrat_quick); | |
4601 | ||
330a35fe | 4602 | ASSERT(!isnullstartblock(bma.got.br_startblock)); |
1dcdf505 DC |
4603 | xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags, |
4604 | xfs_iomap_inode_sequence(ip, flags)); | |
c784c9d2 | 4605 | *seq = READ_ONCE(ifp->if_seq); |
330a35fe | 4606 | |
5965a482 DW |
4607 | if (whichfork == XFS_COW_FORK) |
4608 | xfs_refcount_alloc_cow_extent(tp, bma.blkno, bma.length); | |
330a35fe CH |
4609 | |
4610 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags, | |
4611 | whichfork); | |
c784c9d2 CH |
4612 | if (error) |
4613 | goto out_finish; | |
4614 | ||
4615 | xfs_bmapi_finish(&bma, whichfork, 0); | |
4616 | error = xfs_trans_commit(tp); | |
4617 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
4618 | return error; | |
4619 | ||
330a35fe CH |
4620 | out_finish: |
4621 | xfs_bmapi_finish(&bma, whichfork, error); | |
c784c9d2 CH |
4622 | out_trans_cancel: |
4623 | xfs_trans_cancel(tp); | |
4624 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
badaa597 BF |
4625 | return error; |
4626 | } | |
4627 | ||
26d6a481 | 4628 | int |
0b44aa85 CH |
4629 | xfs_bmapi_remap( |
4630 | struct xfs_trans *tp, | |
4631 | struct xfs_inode *ip, | |
4632 | xfs_fileoff_t bno, | |
4633 | xfs_filblks_t len, | |
4634 | xfs_fsblock_t startblock, | |
6e22af31 | 4635 | uint32_t flags) |
0b44aa85 CH |
4636 | { |
4637 | struct xfs_mount *mp = ip->i_mount; | |
26d6a481 | 4638 | struct xfs_ifork *ifp; |
0b44aa85 | 4639 | struct xfs_btree_cur *cur = NULL; |
0b44aa85 | 4640 | struct xfs_bmbt_irec got; |
9788e059 | 4641 | struct xfs_iext_cursor icur; |
26d6a481 | 4642 | int whichfork = xfs_bmapi_whichfork(flags); |
0b44aa85 CH |
4643 | int logflags = 0, error; |
4644 | ||
722e81c1 | 4645 | ifp = xfs_ifork_ptr(ip, whichfork); |
0b44aa85 | 4646 | ASSERT(len > 0); |
d3e0c71f | 4647 | ASSERT(len <= (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN); |
0b44aa85 | 4648 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
36cfb334 DW |
4649 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC | |
4650 | XFS_BMAPI_NORMAP))); | |
4651 | ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) != | |
4652 | (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)); | |
0b44aa85 | 4653 | |
d967a68d | 4654 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) || |
bc73da84 | 4655 | XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) { |
0b44aa85 CH |
4656 | return -EFSCORRUPTED; |
4657 | } | |
4658 | ||
93adb06a | 4659 | if (xfs_is_shutdown(mp)) |
0b44aa85 CH |
4660 | return -EIO; |
4661 | ||
e00c57e7 CH |
4662 | error = xfs_iread_extents(tp, ip, whichfork); |
4663 | if (error) | |
4664 | return error; | |
0b44aa85 | 4665 | |
9788e059 | 4666 | if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) { |
0b44aa85 CH |
4667 | /* make sure we only reflink into a hole. */ |
4668 | ASSERT(got.br_startoff > bno); | |
4669 | ASSERT(got.br_startoff - bno >= len); | |
4670 | } | |
4671 | ||
aa00f286 | 4672 | ip->i_nblocks += len; |
05422db6 | 4673 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
0b44aa85 | 4674 | |
84094546 | 4675 | if (ifp->if_format == XFS_DINODE_FMT_BTREE) { |
26d6a481 | 4676 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
116c6a88 | 4677 | cur->bc_ino.flags = 0; |
0b44aa85 CH |
4678 | } |
4679 | ||
4680 | got.br_startoff = bno; | |
4681 | got.br_startblock = startblock; | |
4682 | got.br_blockcount = len; | |
36cfb334 DW |
4683 | if (flags & XFS_BMAPI_PREALLOC) |
4684 | got.br_state = XFS_EXT_UNWRITTEN; | |
4685 | else | |
4686 | got.br_state = XFS_EXT_NORM; | |
0b44aa85 | 4687 | |
26d6a481 | 4688 | error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur, |
64e8b4a7 | 4689 | &cur, &got, &logflags, flags); |
0b44aa85 CH |
4690 | if (error) |
4691 | goto error0; | |
4692 | ||
939ebc1a | 4693 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork); |
0b44aa85 CH |
4694 | |
4695 | error0: | |
d967a68d | 4696 | if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS) |
0b44aa85 | 4697 | logflags &= ~XFS_ILOG_DEXT; |
d967a68d | 4698 | else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE) |
0b44aa85 CH |
4699 | logflags &= ~XFS_ILOG_DBROOT; |
4700 | ||
4701 | if (logflags) | |
4702 | xfs_trans_log_inode(tp, ip, logflags); | |
660265b7 DW |
4703 | if (cur) |
4704 | xfs_btree_del_cursor(cur, error); | |
0b44aa85 CH |
4705 | return error; |
4706 | } | |
4707 | ||
01d1b786 BF |
4708 | /* |
4709 | * When a delalloc extent is split (e.g., due to a hole punch), the original | |
4710 | * indlen reservation must be shared across the two new extents that are left | |
4711 | * behind. | |
4712 | * | |
4713 | * Given the original reservation and the worst case indlen for the two new | |
4714 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original | |
731ccdf9 BF |
4715 | * reservation fairly across the two new extents. If necessary, steal available |
4716 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if | |
4717 | * ores == 1). The number of stolen blocks is returned. The availability and | |
4718 | * subsequent accounting of stolen blocks is the responsibility of the caller. | |
01d1b786 | 4719 | */ |
731ccdf9 | 4720 | static xfs_filblks_t |
01d1b786 BF |
4721 | xfs_bmap_split_indlen( |
4722 | xfs_filblks_t ores, /* original res. */ | |
4723 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ | |
731ccdf9 BF |
4724 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
4725 | xfs_filblks_t avail) /* stealable blocks */ | |
01d1b786 BF |
4726 | { |
4727 | xfs_filblks_t len1 = *indlen1; | |
4728 | xfs_filblks_t len2 = *indlen2; | |
4729 | xfs_filblks_t nres = len1 + len2; /* new total res. */ | |
731ccdf9 | 4730 | xfs_filblks_t stolen = 0; |
775762e4 | 4731 | xfs_filblks_t resfactor; |
731ccdf9 BF |
4732 | |
4733 | /* | |
4734 | * Steal as many blocks as we can to try and satisfy the worst case | |
4735 | * indlen for both new extents. | |
4736 | */ | |
775762e4 BF |
4737 | if (ores < nres && avail) |
4738 | stolen = XFS_FILBLKS_MIN(nres - ores, avail); | |
4739 | ores += stolen; | |
4740 | ||
4741 | /* nothing else to do if we've satisfied the new reservation */ | |
4742 | if (ores >= nres) | |
4743 | return stolen; | |
4744 | ||
4745 | /* | |
4746 | * We can't meet the total required reservation for the two extents. | |
4747 | * Calculate the percent of the overall shortage between both extents | |
4748 | * and apply this percentage to each of the requested indlen values. | |
4749 | * This distributes the shortage fairly and reduces the chances that one | |
4750 | * of the two extents is left with nothing when extents are repeatedly | |
4751 | * split. | |
4752 | */ | |
4753 | resfactor = (ores * 100); | |
4754 | do_div(resfactor, nres); | |
4755 | len1 *= resfactor; | |
4756 | do_div(len1, 100); | |
4757 | len2 *= resfactor; | |
4758 | do_div(len2, 100); | |
4759 | ASSERT(len1 + len2 <= ores); | |
4760 | ASSERT(len1 < *indlen1 && len2 < *indlen2); | |
01d1b786 BF |
4761 | |
4762 | /* | |
775762e4 BF |
4763 | * Hand out the remainder to each extent. If one of the two reservations |
4764 | * is zero, we want to make sure that one gets a block first. The loop | |
4765 | * below starts with len1, so hand len2 a block right off the bat if it | |
4766 | * is zero. | |
01d1b786 | 4767 | */ |
775762e4 BF |
4768 | ores -= (len1 + len2); |
4769 | ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores); | |
4770 | if (ores && !len2 && *indlen2) { | |
4771 | len2++; | |
4772 | ores--; | |
4773 | } | |
4774 | while (ores) { | |
4775 | if (len1 < *indlen1) { | |
4776 | len1++; | |
4777 | ores--; | |
01d1b786 | 4778 | } |
775762e4 | 4779 | if (!ores) |
01d1b786 | 4780 | break; |
775762e4 BF |
4781 | if (len2 < *indlen2) { |
4782 | len2++; | |
4783 | ores--; | |
01d1b786 BF |
4784 | } |
4785 | } | |
4786 | ||
4787 | *indlen1 = len1; | |
4788 | *indlen2 = len2; | |
731ccdf9 BF |
4789 | |
4790 | return stolen; | |
01d1b786 BF |
4791 | } |
4792 | ||
ece930fa CH |
4793 | int |
4794 | xfs_bmap_del_extent_delay( | |
4795 | struct xfs_inode *ip, | |
4796 | int whichfork, | |
9788e059 | 4797 | struct xfs_iext_cursor *icur, |
ece930fa CH |
4798 | struct xfs_bmbt_irec *got, |
4799 | struct xfs_bmbt_irec *del) | |
4800 | { | |
4801 | struct xfs_mount *mp = ip->i_mount; | |
722e81c1 | 4802 | struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); |
ece930fa CH |
4803 | struct xfs_bmbt_irec new; |
4804 | int64_t da_old, da_new, da_diff = 0; | |
4805 | xfs_fileoff_t del_endoff, got_endoff; | |
4806 | xfs_filblks_t got_indlen, new_indlen, stolen; | |
7ce54306 | 4807 | uint32_t state = xfs_bmap_fork_to_state(whichfork); |
7bd43334 | 4808 | int error = 0; |
ece930fa CH |
4809 | bool isrt; |
4810 | ||
4811 | XFS_STATS_INC(mp, xs_del_exlist); | |
4812 | ||
4813 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); | |
4814 | del_endoff = del->br_startoff + del->br_blockcount; | |
4815 | got_endoff = got->br_startoff + got->br_blockcount; | |
4816 | da_old = startblockval(got->br_startblock); | |
4817 | da_new = 0; | |
4818 | ||
ece930fa CH |
4819 | ASSERT(del->br_blockcount > 0); |
4820 | ASSERT(got->br_startoff <= del->br_startoff); | |
4821 | ASSERT(got_endoff >= del_endoff); | |
4822 | ||
4823 | if (isrt) { | |
06d168b5 | 4824 | uint64_t rtexts = del->br_blockcount; |
ece930fa CH |
4825 | |
4826 | do_div(rtexts, mp->m_sb.sb_rextsize); | |
4827 | xfs_mod_frextents(mp, rtexts); | |
4828 | } | |
4829 | ||
4830 | /* | |
4831 | * Update the inode delalloc counter now and wait to update the | |
4832 | * sb counters as we might have to borrow some blocks for the | |
4833 | * indirect block accounting. | |
4834 | */ | |
9fcc3af9 DW |
4835 | ASSERT(!isrt); |
4836 | error = xfs_quota_unreserve_blkres(ip, del->br_blockcount); | |
6921638c DW |
4837 | if (error) |
4838 | return error; | |
ece930fa CH |
4839 | ip->i_delayed_blks -= del->br_blockcount; |
4840 | ||
ece930fa | 4841 | if (got->br_startoff == del->br_startoff) |
d0a03e5a | 4842 | state |= BMAP_LEFT_FILLING; |
ece930fa | 4843 | if (got_endoff == del_endoff) |
d0a03e5a | 4844 | state |= BMAP_RIGHT_FILLING; |
ece930fa | 4845 | |
d0a03e5a CH |
4846 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
4847 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: | |
ece930fa CH |
4848 | /* |
4849 | * Matches the whole extent. Delete the entry. | |
4850 | */ | |
cf455a62 | 4851 | xfs_iext_remove(ip, icur, state); |
9788e059 | 4852 | xfs_iext_prev(ifp, icur); |
ece930fa | 4853 | break; |
d0a03e5a | 4854 | case BMAP_LEFT_FILLING: |
ece930fa CH |
4855 | /* |
4856 | * Deleting the first part of the extent. | |
4857 | */ | |
ece930fa CH |
4858 | got->br_startoff = del_endoff; |
4859 | got->br_blockcount -= del->br_blockcount; | |
4860 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, | |
4861 | got->br_blockcount), da_old); | |
4862 | got->br_startblock = nullstartblock((int)da_new); | |
9788e059 | 4863 | xfs_iext_update_extent(ip, state, icur, got); |
ece930fa | 4864 | break; |
d0a03e5a | 4865 | case BMAP_RIGHT_FILLING: |
ece930fa CH |
4866 | /* |
4867 | * Deleting the last part of the extent. | |
4868 | */ | |
ece930fa CH |
4869 | got->br_blockcount = got->br_blockcount - del->br_blockcount; |
4870 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, | |
4871 | got->br_blockcount), da_old); | |
4872 | got->br_startblock = nullstartblock((int)da_new); | |
9788e059 | 4873 | xfs_iext_update_extent(ip, state, icur, got); |
ece930fa CH |
4874 | break; |
4875 | case 0: | |
4876 | /* | |
4877 | * Deleting the middle of the extent. | |
4878 | * | |
4879 | * Distribute the original indlen reservation across the two new | |
4880 | * extents. Steal blocks from the deleted extent if necessary. | |
4881 | * Stealing blocks simply fudges the fdblocks accounting below. | |
4882 | * Warn if either of the new indlen reservations is zero as this | |
4883 | * can lead to delalloc problems. | |
4884 | */ | |
ece930fa CH |
4885 | got->br_blockcount = del->br_startoff - got->br_startoff; |
4886 | got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); | |
4887 | ||
4888 | new.br_blockcount = got_endoff - del_endoff; | |
4889 | new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); | |
4890 | ||
4891 | WARN_ON_ONCE(!got_indlen || !new_indlen); | |
4892 | stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen, | |
4893 | del->br_blockcount); | |
4894 | ||
4895 | got->br_startblock = nullstartblock((int)got_indlen); | |
ece930fa CH |
4896 | |
4897 | new.br_startoff = del_endoff; | |
4898 | new.br_state = got->br_state; | |
4899 | new.br_startblock = nullstartblock((int)new_indlen); | |
4900 | ||
9788e059 CH |
4901 | xfs_iext_update_extent(ip, state, icur, got); |
4902 | xfs_iext_next(ifp, icur); | |
26a75f67 | 4903 | xfs_iext_insert(ip, icur, &new, state); |
ece930fa CH |
4904 | |
4905 | da_new = got_indlen + new_indlen - stolen; | |
4906 | del->br_blockcount -= stolen; | |
4907 | break; | |
4908 | } | |
4909 | ||
4910 | ASSERT(da_old >= da_new); | |
4911 | da_diff = da_old - da_new; | |
4912 | if (!isrt) | |
4913 | da_diff += del->br_blockcount; | |
f73690fe | 4914 | if (da_diff) { |
ece930fa | 4915 | xfs_mod_fdblocks(mp, da_diff, false); |
f73690fe DW |
4916 | xfs_mod_delalloc(mp, -da_diff); |
4917 | } | |
ece930fa CH |
4918 | return error; |
4919 | } | |
4920 | ||
4921 | void | |
4922 | xfs_bmap_del_extent_cow( | |
4923 | struct xfs_inode *ip, | |
9788e059 | 4924 | struct xfs_iext_cursor *icur, |
ece930fa CH |
4925 | struct xfs_bmbt_irec *got, |
4926 | struct xfs_bmbt_irec *del) | |
4927 | { | |
4928 | struct xfs_mount *mp = ip->i_mount; | |
722e81c1 | 4929 | struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_COW_FORK); |
ece930fa CH |
4930 | struct xfs_bmbt_irec new; |
4931 | xfs_fileoff_t del_endoff, got_endoff; | |
7ce54306 | 4932 | uint32_t state = BMAP_COWFORK; |
ece930fa CH |
4933 | |
4934 | XFS_STATS_INC(mp, xs_del_exlist); | |
4935 | ||
4936 | del_endoff = del->br_startoff + del->br_blockcount; | |
4937 | got_endoff = got->br_startoff + got->br_blockcount; | |
4938 | ||
ece930fa CH |
4939 | ASSERT(del->br_blockcount > 0); |
4940 | ASSERT(got->br_startoff <= del->br_startoff); | |
4941 | ASSERT(got_endoff >= del_endoff); | |
4942 | ASSERT(!isnullstartblock(got->br_startblock)); | |
4943 | ||
4944 | if (got->br_startoff == del->br_startoff) | |
d0a03e5a | 4945 | state |= BMAP_LEFT_FILLING; |
ece930fa | 4946 | if (got_endoff == del_endoff) |
d0a03e5a | 4947 | state |= BMAP_RIGHT_FILLING; |
ece930fa | 4948 | |
d0a03e5a CH |
4949 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
4950 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: | |
ece930fa CH |
4951 | /* |
4952 | * Matches the whole extent. Delete the entry. | |
4953 | */ | |
cf455a62 | 4954 | xfs_iext_remove(ip, icur, state); |
9788e059 | 4955 | xfs_iext_prev(ifp, icur); |
ece930fa | 4956 | break; |
d0a03e5a | 4957 | case BMAP_LEFT_FILLING: |
ece930fa CH |
4958 | /* |
4959 | * Deleting the first part of the extent. | |
4960 | */ | |
ece930fa CH |
4961 | got->br_startoff = del_endoff; |
4962 | got->br_blockcount -= del->br_blockcount; | |
4963 | got->br_startblock = del->br_startblock + del->br_blockcount; | |
9788e059 | 4964 | xfs_iext_update_extent(ip, state, icur, got); |
ece930fa | 4965 | break; |
d0a03e5a | 4966 | case BMAP_RIGHT_FILLING: |
ece930fa CH |
4967 | /* |
4968 | * Deleting the last part of the extent. | |
4969 | */ | |
ece930fa | 4970 | got->br_blockcount -= del->br_blockcount; |
9788e059 | 4971 | xfs_iext_update_extent(ip, state, icur, got); |
ece930fa CH |
4972 | break; |
4973 | case 0: | |
4974 | /* | |
4975 | * Deleting the middle of the extent. | |
4976 | */ | |
ece930fa | 4977 | got->br_blockcount = del->br_startoff - got->br_startoff; |
ece930fa CH |
4978 | |
4979 | new.br_startoff = del_endoff; | |
4980 | new.br_blockcount = got_endoff - del_endoff; | |
4981 | new.br_state = got->br_state; | |
4982 | new.br_startblock = del->br_startblock + del->br_blockcount; | |
4983 | ||
9788e059 CH |
4984 | xfs_iext_update_extent(ip, state, icur, got); |
4985 | xfs_iext_next(ifp, icur); | |
26a75f67 | 4986 | xfs_iext_insert(ip, icur, &new, state); |
ece930fa CH |
4987 | break; |
4988 | } | |
d07cc724 | 4989 | ip->i_delayed_blks -= del->br_blockcount; |
ece930fa CH |
4990 | } |
4991 | ||
2bd0ea18 | 4992 | /* |
49f693fa | 4993 | * Called by xfs_bmapi to update file extent records and the btree |
ad68fd19 | 4994 | * after removing space. |
2bd0ea18 | 4995 | */ |
49f693fa | 4996 | STATIC int /* error */ |
ad68fd19 | 4997 | xfs_bmap_del_extent_real( |
49f693fa DC |
4998 | xfs_inode_t *ip, /* incore inode pointer */ |
4999 | xfs_trans_t *tp, /* current transaction pointer */ | |
9788e059 | 5000 | struct xfs_iext_cursor *icur, |
ec924d04 | 5001 | struct xfs_btree_cur *cur, /* if null, not a btree */ |
49f693fa DC |
5002 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
5003 | int *logflagsp, /* inode logging flags */ | |
36b16da8 | 5004 | int whichfork, /* data or attr fork */ |
6e22af31 | 5005 | uint32_t bflags) /* bmapi flags */ |
2bd0ea18 | 5006 | { |
49f693fa DC |
5007 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
5008 | xfs_fileoff_t del_endoff; /* first offset past del */ | |
49f693fa | 5009 | int do_fx; /* free extent at end of routine */ |
49f693fa | 5010 | int error; /* error return value */ |
bd92a38b | 5011 | int flags = 0;/* inode logging flags */ |
70bf7533 | 5012 | struct xfs_bmbt_irec got; /* current extent entry */ |
49f693fa DC |
5013 | xfs_fileoff_t got_endoff; /* first offset past got */ |
5014 | int i; /* temp state */ | |
e07055b8 | 5015 | struct xfs_ifork *ifp; /* inode fork pointer */ |
49f693fa DC |
5016 | xfs_mount_t *mp; /* mount structure */ |
5017 | xfs_filblks_t nblks; /* quota/sb block count */ | |
5018 | xfs_bmbt_irec_t new; /* new record to be inserted */ | |
5019 | /* REFERENCED */ | |
5020 | uint qfield; /* quota field to update */ | |
7ce54306 | 5021 | uint32_t state = xfs_bmap_fork_to_state(whichfork); |
70bf7533 | 5022 | struct xfs_bmbt_irec old; |
a2ceac1f | 5023 | |
79896434 BD |
5024 | mp = ip->i_mount; |
5025 | XFS_STATS_INC(mp, xs_del_exlist); | |
a2ceac1f | 5026 | |
722e81c1 | 5027 | ifp = xfs_ifork_ptr(ip, whichfork); |
49f693fa | 5028 | ASSERT(del->br_blockcount > 0); |
9788e059 | 5029 | xfs_iext_get_extent(ifp, icur, &got); |
49f693fa DC |
5030 | ASSERT(got.br_startoff <= del->br_startoff); |
5031 | del_endoff = del->br_startoff + del->br_blockcount; | |
5032 | got_endoff = got.br_startoff + got.br_blockcount; | |
5033 | ASSERT(got_endoff >= del_endoff); | |
ad68fd19 | 5034 | ASSERT(!isnullstartblock(got.br_startblock)); |
49f693fa DC |
5035 | qfield = 0; |
5036 | error = 0; | |
ad68fd19 | 5037 | |
bd92a38b CH |
5038 | /* |
5039 | * If it's the case where the directory code is running with no block | |
5040 | * reservation, and the deleted block is in the middle of its extent, | |
5041 | * and the resulting insert of an extent would cause transformation to | |
5042 | * btree format, then reject it. The calling code will then swap blocks | |
5043 | * around instead. We have to do this now, rather than waiting for the | |
5044 | * conversion to btree format, since the transaction will be dirty then. | |
5045 | */ | |
5046 | if (tp->t_blk_res == 0 && | |
d967a68d | 5047 | ifp->if_format == XFS_DINODE_FMT_EXTENTS && |
87c472b7 | 5048 | ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) && |
bd92a38b CH |
5049 | del->br_startoff > got.br_startoff && del_endoff < got_endoff) |
5050 | return -ENOSPC; | |
5051 | ||
5052 | flags = XFS_ILOG_CORE; | |
ad68fd19 | 5053 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
d5fe08a6 | 5054 | if (!(bflags & XFS_BMAPI_REMAP)) { |
4dbd5762 DW |
5055 | error = xfs_rtfree_blocks(tp, del->br_startblock, |
5056 | del->br_blockcount); | |
d5fe08a6 DW |
5057 | if (error) |
5058 | goto done; | |
5059 | } | |
5060 | ||
49f693fa | 5061 | do_fx = 0; |
ad68fd19 CH |
5062 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
5063 | } else { | |
5064 | do_fx = 1; | |
ad68fd19 CH |
5065 | qfield = XFS_TRANS_DQ_BCOUNT; |
5066 | } | |
4dbd5762 | 5067 | nblks = del->br_blockcount; |
ad68fd19 CH |
5068 | |
5069 | del_endblock = del->br_startblock + del->br_blockcount; | |
5070 | if (cur) { | |
70a93110 | 5071 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
ad68fd19 CH |
5072 | if (error) |
5073 | goto done; | |
fbb4fa7f DW |
5074 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
5075 | error = -EFSCORRUPTED; | |
5076 | goto done; | |
5077 | } | |
49f693fa | 5078 | } |
85aec44f | 5079 | |
b039ac79 CH |
5080 | if (got.br_startoff == del->br_startoff) |
5081 | state |= BMAP_LEFT_FILLING; | |
5082 | if (got_endoff == del_endoff) | |
5083 | state |= BMAP_RIGHT_FILLING; | |
5084 | ||
5085 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { | |
5086 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: | |
49f693fa DC |
5087 | /* |
5088 | * Matches the whole extent. Delete the entry. | |
5089 | */ | |
cf455a62 | 5090 | xfs_iext_remove(ip, icur, state); |
9788e059 | 5091 | xfs_iext_prev(ifp, icur); |
87c472b7 CH |
5092 | ifp->if_nextents--; |
5093 | ||
49f693fa DC |
5094 | flags |= XFS_ILOG_CORE; |
5095 | if (!cur) { | |
5096 | flags |= xfs_ilog_fext(whichfork); | |
5097 | break; | |
2bd0ea18 | 5098 | } |
49f693fa DC |
5099 | if ((error = xfs_btree_delete(cur, &i))) |
5100 | goto done; | |
fbb4fa7f DW |
5101 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
5102 | error = -EFSCORRUPTED; | |
5103 | goto done; | |
5104 | } | |
49f693fa | 5105 | break; |
b039ac79 | 5106 | case BMAP_LEFT_FILLING: |
2bd0ea18 | 5107 | /* |
49f693fa | 5108 | * Deleting the first part of the extent. |
2bd0ea18 | 5109 | */ |
70bf7533 CH |
5110 | got.br_startoff = del_endoff; |
5111 | got.br_startblock = del_endblock; | |
5112 | got.br_blockcount -= del->br_blockcount; | |
9788e059 | 5113 | xfs_iext_update_extent(ip, state, icur, &got); |
49f693fa DC |
5114 | if (!cur) { |
5115 | flags |= xfs_ilog_fext(whichfork); | |
5116 | break; | |
5117 | } | |
d0e5f1ff | 5118 | error = xfs_bmbt_update(cur, &got); |
70bf7533 | 5119 | if (error) |
49f693fa DC |
5120 | goto done; |
5121 | break; | |
b039ac79 | 5122 | case BMAP_RIGHT_FILLING: |
2bd0ea18 | 5123 | /* |
49f693fa | 5124 | * Deleting the last part of the extent. |
2bd0ea18 | 5125 | */ |
70bf7533 | 5126 | got.br_blockcount -= del->br_blockcount; |
9788e059 | 5127 | xfs_iext_update_extent(ip, state, icur, &got); |
49f693fa DC |
5128 | if (!cur) { |
5129 | flags |= xfs_ilog_fext(whichfork); | |
5130 | break; | |
5131 | } | |
d0e5f1ff | 5132 | error = xfs_bmbt_update(cur, &got); |
70bf7533 | 5133 | if (error) |
49f693fa DC |
5134 | goto done; |
5135 | break; | |
49f693fa DC |
5136 | case 0: |
5137 | /* | |
5138 | * Deleting the middle of the extent. | |
5139 | */ | |
7428ec5b | 5140 | |
70bf7533 | 5141 | old = got; |
df926c07 | 5142 | |
70bf7533 | 5143 | got.br_blockcount = del->br_startoff - got.br_startoff; |
9788e059 | 5144 | xfs_iext_update_extent(ip, state, icur, &got); |
70bf7533 | 5145 | |
49f693fa | 5146 | new.br_startoff = del_endoff; |
70bf7533 | 5147 | new.br_blockcount = got_endoff - del_endoff; |
49f693fa | 5148 | new.br_state = got.br_state; |
ad68fd19 | 5149 | new.br_startblock = del_endblock; |
70bf7533 | 5150 | |
ad68fd19 CH |
5151 | flags |= XFS_ILOG_CORE; |
5152 | if (cur) { | |
d0e5f1ff | 5153 | error = xfs_bmbt_update(cur, &got); |
ad68fd19 CH |
5154 | if (error) |
5155 | goto done; | |
5156 | error = xfs_btree_increment(cur, 0, &i); | |
5157 | if (error) | |
5158 | goto done; | |
5159 | cur->bc_rec.b = new; | |
5160 | error = xfs_btree_insert(cur, &i); | |
5161 | if (error && error != -ENOSPC) | |
5162 | goto done; | |
5163 | /* | |
5164 | * If get no-space back from btree insert, it tried a | |
5165 | * split, and we have a zero block reservation. Fix up | |
5166 | * our state and return the error. | |
5167 | */ | |
5168 | if (error == -ENOSPC) { | |
49f693fa | 5169 | /* |
ad68fd19 CH |
5170 | * Reset the cursor, don't trust it after any |
5171 | * insert operation. | |
49f693fa | 5172 | */ |
70a93110 | 5173 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
ad68fd19 | 5174 | if (error) |
49f693fa | 5175 | goto done; |
fbb4fa7f DW |
5176 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
5177 | error = -EFSCORRUPTED; | |
5178 | goto done; | |
5179 | } | |
ad68fd19 CH |
5180 | /* |
5181 | * Update the btree record back | |
5182 | * to the original value. | |
5183 | */ | |
d0e5f1ff | 5184 | error = xfs_bmbt_update(cur, &old); |
ad68fd19 CH |
5185 | if (error) |
5186 | goto done; | |
5187 | /* | |
5188 | * Reset the extent record back | |
5189 | * to the original value. | |
5190 | */ | |
9788e059 | 5191 | xfs_iext_update_extent(ip, state, icur, &old); |
ad68fd19 CH |
5192 | flags = 0; |
5193 | error = -ENOSPC; | |
5194 | goto done; | |
5195 | } | |
fbb4fa7f DW |
5196 | if (XFS_IS_CORRUPT(mp, i != 1)) { |
5197 | error = -EFSCORRUPTED; | |
5198 | goto done; | |
5199 | } | |
ad68fd19 CH |
5200 | } else |
5201 | flags |= xfs_ilog_fext(whichfork); | |
87c472b7 CH |
5202 | |
5203 | ifp->if_nextents++; | |
9788e059 | 5204 | xfs_iext_next(ifp, icur); |
26a75f67 | 5205 | xfs_iext_insert(ip, icur, &new, state); |
49f693fa | 5206 | break; |
2bd0ea18 | 5207 | } |
d7f80320 DW |
5208 | |
5209 | /* remove reverse mapping */ | |
46d29bb9 | 5210 | xfs_rmap_unmap_extent(tp, ip, whichfork, del); |
d7f80320 | 5211 | |
2bd0ea18 | 5212 | /* |
49f693fa | 5213 | * If we need to, add to list of extents to delete. |
2bd0ea18 | 5214 | */ |
36b16da8 | 5215 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
cfe32f0d | 5216 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
5965a482 | 5217 | xfs_refcount_decrease_extent(tp, del); |
3a13f959 | 5218 | } else { |
cd3e5d3c | 5219 | error = __xfs_free_extent_later(tp, del->br_startblock, |
cc5af22a | 5220 | del->br_blockcount, NULL, |
ef16737e DC |
5221 | XFS_AG_RESV_NONE, |
5222 | ((bflags & XFS_BMAPI_NODISCARD) || | |
5223 | del->br_state == XFS_EXT_UNWRITTEN)); | |
cd3e5d3c DC |
5224 | if (error) |
5225 | goto done; | |
3a13f959 | 5226 | } |
cfe32f0d DW |
5227 | } |
5228 | ||
2bd0ea18 | 5229 | /* |
49f693fa | 5230 | * Adjust inode # blocks in the file. |
2bd0ea18 | 5231 | */ |
49f693fa | 5232 | if (nblks) |
aa00f286 | 5233 | ip->i_nblocks -= nblks; |
2bd0ea18 | 5234 | /* |
49f693fa | 5235 | * Adjust quota data. |
2bd0ea18 | 5236 | */ |
36b16da8 | 5237 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
49f693fa DC |
5238 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
5239 | ||
49f693fa DC |
5240 | done: |
5241 | *logflagsp = flags; | |
2bd0ea18 NS |
5242 | return error; |
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 | */ |
49f693fa | 5251 | int /* error */ |
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 */ | |
5273 | xfs_fsblock_t sum; | |
3d36acda | 5274 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
675b5a20 | 5275 | xfs_fileoff_t end; |
9788e059 CH |
5276 | struct xfs_iext_cursor icur; |
5277 | bool done = false; | |
2bd0ea18 | 5278 | |
675b5a20 | 5279 | trace_xfs_bunmap(ip, start, len, flags, _RET_IP_); |
a2ceac1f | 5280 | |
cb8a004a DW |
5281 | whichfork = xfs_bmapi_whichfork(flags); |
5282 | ASSERT(whichfork != XFS_COW_FORK); | |
722e81c1 | 5283 | ifp = xfs_ifork_ptr(ip, whichfork); |
d967a68d | 5284 | if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp))) |
12b53197 | 5285 | return -EFSCORRUPTED; |
93adb06a | 5286 | if (xfs_is_shutdown(mp)) |
12b53197 | 5287 | return -EIO; |
56b2de80 | 5288 | |
ff105f75 | 5289 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
49f693fa DC |
5290 | ASSERT(len > 0); |
5291 | ASSERT(nexts >= 0); | |
56b2de80 | 5292 | |
e00c57e7 CH |
5293 | error = xfs_iread_extents(tp, ip, whichfork); |
5294 | if (error) | |
49f693fa | 5295 | return error; |
e00c57e7 | 5296 | |
d09d4e5f | 5297 | if (xfs_iext_count(ifp) == 0) { |
3d36acda | 5298 | *rlen = 0; |
49f693fa | 5299 | return 0; |
56b2de80 | 5300 | } |
79896434 | 5301 | XFS_STATS_INC(mp, xs_blk_unmap); |
49f693fa | 5302 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
d6fbe8fe | 5303 | end = start + len; |
a2ceac1f | 5304 | |
9788e059 | 5305 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) { |
d6fbe8fe CH |
5306 | *rlen = 0; |
5307 | return 0; | |
49f693fa | 5308 | } |
d6fbe8fe | 5309 | end--; |
246eb90a | 5310 | |
49f693fa | 5311 | logflags = 0; |
84094546 | 5312 | if (ifp->if_format == XFS_DINODE_FMT_BTREE) { |
d967a68d | 5313 | ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE); |
49f693fa | 5314 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
116c6a88 | 5315 | cur->bc_ino.flags = 0; |
49f693fa DC |
5316 | } else |
5317 | cur = NULL; | |
a2ceac1f | 5318 | |
49f693fa | 5319 | if (isrt) { |
a2ceac1f | 5320 | /* |
49f693fa | 5321 | * Synchronize by locking the bitmap inode. |
a2ceac1f | 5322 | */ |
a62ed6d3 | 5323 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
49f693fa | 5324 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
a62ed6d3 DW |
5325 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
5326 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); | |
49f693fa | 5327 | } |
a2ceac1f | 5328 | |
49f693fa | 5329 | extno = 0; |
9788e059 | 5330 | while (end != (xfs_fileoff_t)-1 && end >= start && |
eb9a1cac | 5331 | (nexts == 0 || extno < nexts)) { |
a2ceac1f | 5332 | /* |
675b5a20 | 5333 | * Is the found extent after a hole in which end lives? |
49f693fa | 5334 | * Just back up to the previous extent, if so. |
a2ceac1f | 5335 | */ |
9788e059 CH |
5336 | if (got.br_startoff > end && |
5337 | !xfs_iext_prev_extent(ifp, &icur, &got)) { | |
5338 | done = true; | |
5339 | break; | |
a2ceac1f | 5340 | } |
49f693fa DC |
5341 | /* |
5342 | * Is the last block of this extent before the range | |
5343 | * we're supposed to delete? If so, we're done. | |
5344 | */ | |
675b5a20 | 5345 | end = XFS_FILEOFF_MIN(end, |
49f693fa | 5346 | got.br_startoff + got.br_blockcount - 1); |
675b5a20 | 5347 | if (end < start) |
49f693fa DC |
5348 | break; |
5349 | /* | |
5350 | * Then deal with the (possibly delayed) allocated space | |
5351 | * we found. | |
5352 | */ | |
49f693fa DC |
5353 | del = got; |
5354 | wasdel = isnullstartblock(del.br_startblock); | |
15a8bccc | 5355 | |
49f693fa DC |
5356 | if (got.br_startoff < start) { |
5357 | del.br_startoff = start; | |
5358 | del.br_blockcount -= start - got.br_startoff; | |
5359 | if (!wasdel) | |
5360 | del.br_startblock += start - got.br_startoff; | |
5361 | } | |
675b5a20 CH |
5362 | if (del.br_startoff + del.br_blockcount > end + 1) |
5363 | del.br_blockcount = end + 1 - del.br_startoff; | |
594956fa | 5364 | |
5a595099 DC |
5365 | if (!isrt) |
5366 | goto delete; | |
5367 | ||
49f693fa | 5368 | sum = del.br_startblock + del.br_blockcount; |
5a595099 DC |
5369 | div_u64_rem(sum, mp->m_sb.sb_rextsize, &mod); |
5370 | if (mod) { | |
49f693fa DC |
5371 | /* |
5372 | * Realtime extent not lined up at the end. | |
5373 | * The extent could have been split into written | |
5374 | * and unwritten pieces, or we could just be | |
5375 | * unmapping part of it. But we can't really | |
5376 | * get rid of part of a realtime extent. | |
5377 | */ | |
b7fdeb4d | 5378 | if (del.br_state == XFS_EXT_UNWRITTEN) { |
49f693fa DC |
5379 | /* |
5380 | * This piece is unwritten, or we're not | |
5381 | * using unwritten extents. Skip over it. | |
5382 | */ | |
675b5a20 CH |
5383 | ASSERT(end >= mod); |
5384 | end -= mod > del.br_blockcount ? | |
49f693fa | 5385 | del.br_blockcount : mod; |
9788e059 CH |
5386 | if (end < got.br_startoff && |
5387 | !xfs_iext_prev_extent(ifp, &icur, &got)) { | |
5388 | done = true; | |
5389 | break; | |
49f693fa DC |
5390 | } |
5391 | continue; | |
5392 | } | |
5393 | /* | |
5394 | * It's written, turn it unwritten. | |
5395 | * This is better than zeroing it. | |
5396 | */ | |
5397 | ASSERT(del.br_state == XFS_EXT_NORM); | |
0268fdc3 | 5398 | ASSERT(tp->t_blk_res > 0); |
49f693fa DC |
5399 | /* |
5400 | * If this spans a realtime extent boundary, | |
5401 | * chop it back to the start of the one we end at. | |
5402 | */ | |
5403 | if (del.br_blockcount > mod) { | |
5404 | del.br_startoff += del.br_blockcount - mod; | |
5405 | del.br_startblock += del.br_blockcount - mod; | |
5406 | del.br_blockcount = mod; | |
5407 | } | |
5408 | del.br_state = XFS_EXT_UNWRITTEN; | |
5409 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, | |
9788e059 | 5410 | whichfork, &icur, &cur, &del, |
64e8b4a7 | 5411 | &logflags); |
49f693fa DC |
5412 | if (error) |
5413 | goto error0; | |
5414 | goto nodelete; | |
a2ceac1f | 5415 | } |
5a595099 DC |
5416 | div_u64_rem(del.br_startblock, mp->m_sb.sb_rextsize, &mod); |
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); |
21375e5d | 6103 | xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list); |
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 DW |
6179 | |
6180 | /* Check that an inode's extent does not have invalid flags or bad ranges. */ | |
6181 | xfs_failaddr_t | |
6182 | xfs_bmap_validate_extent( | |
6183 | struct xfs_inode *ip, | |
6184 | int whichfork, | |
6185 | struct xfs_bmbt_irec *irec) | |
6186 | { | |
6187 | struct xfs_mount *mp = ip->i_mount; | |
0cf6a3a9 | 6188 | |
7626c690 | 6189 | if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount)) |
92415d01 DW |
6190 | return __this_address; |
6191 | ||
23e306ae | 6192 | if (XFS_IS_REALTIME_INODE(ip) && whichfork == XFS_DATA_FORK) { |
95bc0a4d DW |
6193 | if (!xfs_verify_rtbext(mp, irec->br_startblock, |
6194 | irec->br_blockcount)) | |
0cf6a3a9 DW |
6195 | return __this_address; |
6196 | } else { | |
f28f7e4a DW |
6197 | if (!xfs_verify_fsbext(mp, irec->br_startblock, |
6198 | irec->br_blockcount)) | |
0cf6a3a9 DW |
6199 | return __this_address; |
6200 | } | |
b7fdeb4d CH |
6201 | if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK) |
6202 | return __this_address; | |
0cf6a3a9 DW |
6203 | return NULL; |
6204 | } | |
1577541c DW |
6205 | |
6206 | int __init | |
6207 | xfs_bmap_intent_init_cache(void) | |
6208 | { | |
6209 | xfs_bmap_intent_cache = kmem_cache_create("xfs_bmap_intent", | |
6210 | sizeof(struct xfs_bmap_intent), | |
6211 | 0, 0, NULL); | |
6212 | ||
6213 | return xfs_bmap_intent_cache != NULL ? 0 : -ENOMEM; | |
6214 | } | |
6215 | ||
6216 | void | |
6217 | xfs_bmap_intent_destroy_cache(void) | |
6218 | { | |
6219 | kmem_cache_destroy(xfs_bmap_intent_cache); | |
6220 | xfs_bmap_intent_cache = NULL; | |
6221 | } |