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