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