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