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