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