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