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