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