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