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