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