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