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