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