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