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