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