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