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