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