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