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