GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / xfs / xfs_bmap.c
blobf90dadd5a96877ff9975ecaaf2fb260863f7d089
1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
7 * published by the Free Software Foundation.
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.
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
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_dir2_sf.h"
33 #include "xfs_dinode.h"
34 #include "xfs_inode.h"
35 #include "xfs_btree.h"
36 #include "xfs_mount.h"
37 #include "xfs_itable.h"
38 #include "xfs_dir2_data.h"
39 #include "xfs_dir2_leaf.h"
40 #include "xfs_dir2_block.h"
41 #include "xfs_inode_item.h"
42 #include "xfs_extfree_item.h"
43 #include "xfs_alloc.h"
44 #include "xfs_bmap.h"
45 #include "xfs_rtalloc.h"
46 #include "xfs_error.h"
47 #include "xfs_attr_leaf.h"
48 #include "xfs_rw.h"
49 #include "xfs_quota.h"
50 #include "xfs_trans_space.h"
51 #include "xfs_buf_item.h"
52 #include "xfs_filestream.h"
53 #include "xfs_vnodeops.h"
54 #include "xfs_trace.h"
57 #ifdef DEBUG
58 STATIC void
59 xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
60 #endif
62 kmem_zone_t *xfs_bmap_free_item_zone;
65 * Prototypes for internal bmap routines.
70 * Called from xfs_bmap_add_attrfork to handle extents format files.
72 STATIC int /* error */
73 xfs_bmap_add_attrfork_extents(
74 xfs_trans_t *tp, /* transaction pointer */
75 xfs_inode_t *ip, /* incore inode pointer */
76 xfs_fsblock_t *firstblock, /* first block allocated */
77 xfs_bmap_free_t *flist, /* blocks to free at commit */
78 int *flags); /* inode logging flags */
81 * Called from xfs_bmap_add_attrfork to handle local format files.
83 STATIC int /* error */
84 xfs_bmap_add_attrfork_local(
85 xfs_trans_t *tp, /* transaction pointer */
86 xfs_inode_t *ip, /* incore inode pointer */
87 xfs_fsblock_t *firstblock, /* first block allocated */
88 xfs_bmap_free_t *flist, /* blocks to free at commit */
89 int *flags); /* inode logging flags */
92 * Called by xfs_bmapi to update file extent records and the btree
93 * after allocating space (or doing a delayed allocation).
95 STATIC int /* error */
96 xfs_bmap_add_extent(
97 xfs_inode_t *ip, /* incore inode pointer */
98 xfs_extnum_t idx, /* extent number to update/insert */
99 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
100 xfs_bmbt_irec_t *new, /* new data to add to file extents */
101 xfs_fsblock_t *first, /* pointer to firstblock variable */
102 xfs_bmap_free_t *flist, /* list of extents to be freed */
103 int *logflagsp, /* inode logging flags */
104 int whichfork, /* data or attr fork */
105 int rsvd); /* OK to allocate reserved blocks */
108 * Called by xfs_bmap_add_extent to handle cases converting a delayed
109 * allocation to a real allocation.
111 STATIC int /* error */
112 xfs_bmap_add_extent_delay_real(
113 xfs_inode_t *ip, /* incore inode pointer */
114 xfs_extnum_t idx, /* extent number to update/insert */
115 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
116 xfs_bmbt_irec_t *new, /* new data to add to file extents */
117 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
118 xfs_fsblock_t *first, /* pointer to firstblock variable */
119 xfs_bmap_free_t *flist, /* list of extents to be freed */
120 int *logflagsp, /* inode logging flags */
121 int rsvd); /* OK to allocate reserved blocks */
124 * Called by xfs_bmap_add_extent to handle cases converting a hole
125 * to a delayed allocation.
127 STATIC int /* error */
128 xfs_bmap_add_extent_hole_delay(
129 xfs_inode_t *ip, /* incore inode pointer */
130 xfs_extnum_t idx, /* extent number to update/insert */
131 xfs_bmbt_irec_t *new, /* new data to add to file extents */
132 int *logflagsp,/* inode logging flags */
133 int rsvd); /* OK to allocate reserved blocks */
136 * Called by xfs_bmap_add_extent to handle cases converting a hole
137 * to a real allocation.
139 STATIC int /* error */
140 xfs_bmap_add_extent_hole_real(
141 xfs_inode_t *ip, /* incore inode pointer */
142 xfs_extnum_t idx, /* extent number to update/insert */
143 xfs_btree_cur_t *cur, /* if null, not a btree */
144 xfs_bmbt_irec_t *new, /* new data to add to file extents */
145 int *logflagsp, /* inode logging flags */
146 int whichfork); /* data or attr fork */
149 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
150 * allocation to a real allocation or vice versa.
152 STATIC int /* error */
153 xfs_bmap_add_extent_unwritten_real(
154 xfs_inode_t *ip, /* incore inode pointer */
155 xfs_extnum_t idx, /* extent number to update/insert */
156 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
157 xfs_bmbt_irec_t *new, /* new data to add to file extents */
158 int *logflagsp); /* inode logging flags */
161 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
162 * It figures out where to ask the underlying allocator to put the new extent.
164 STATIC int /* error */
165 xfs_bmap_alloc(
166 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
169 * Transform a btree format file with only one leaf node, where the
170 * extents list will fit in the inode, into an extents format file.
171 * Since the file extents are already in-core, all we have to do is
172 * give up the space for the btree root and pitch the leaf block.
174 STATIC int /* error */
175 xfs_bmap_btree_to_extents(
176 xfs_trans_t *tp, /* transaction pointer */
177 xfs_inode_t *ip, /* incore inode pointer */
178 xfs_btree_cur_t *cur, /* btree cursor */
179 int *logflagsp, /* inode logging flags */
180 int whichfork); /* data or attr fork */
183 * Called by xfs_bmapi to update file extent records and the btree
184 * after removing space (or undoing a delayed allocation).
186 STATIC int /* error */
187 xfs_bmap_del_extent(
188 xfs_inode_t *ip, /* incore inode pointer */
189 xfs_trans_t *tp, /* current trans pointer */
190 xfs_extnum_t idx, /* extent number to update/insert */
191 xfs_bmap_free_t *flist, /* list of extents to be freed */
192 xfs_btree_cur_t *cur, /* if null, not a btree */
193 xfs_bmbt_irec_t *new, /* new data to add to file extents */
194 int *logflagsp,/* inode logging flags */
195 int whichfork, /* data or attr fork */
196 int rsvd); /* OK to allocate reserved blocks */
199 * Remove the entry "free" from the free item list. Prev points to the
200 * previous entry, unless "free" is the head of the list.
202 STATIC void
203 xfs_bmap_del_free(
204 xfs_bmap_free_t *flist, /* free item list header */
205 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
206 xfs_bmap_free_item_t *free); /* list item to be freed */
209 * Convert an extents-format file into a btree-format file.
210 * The new file will have a root block (in the inode) and a single child block.
212 STATIC int /* error */
213 xfs_bmap_extents_to_btree(
214 xfs_trans_t *tp, /* transaction pointer */
215 xfs_inode_t *ip, /* incore inode pointer */
216 xfs_fsblock_t *firstblock, /* first-block-allocated */
217 xfs_bmap_free_t *flist, /* blocks freed in xaction */
218 xfs_btree_cur_t **curp, /* cursor returned to caller */
219 int wasdel, /* converting a delayed alloc */
220 int *logflagsp, /* inode logging flags */
221 int whichfork); /* data or attr fork */
224 * Convert a local file to an extents file.
225 * This code is sort of bogus, since the file data needs to get
226 * logged so it won't be lost. The bmap-level manipulations are ok, though.
228 STATIC int /* error */
229 xfs_bmap_local_to_extents(
230 xfs_trans_t *tp, /* transaction pointer */
231 xfs_inode_t *ip, /* incore inode pointer */
232 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
233 xfs_extlen_t total, /* total blocks needed by transaction */
234 int *logflagsp, /* inode logging flags */
235 int whichfork); /* data or attr fork */
238 * Search the extents list for the inode, for the extent containing bno.
239 * If bno lies in a hole, point to the next entry. If bno lies past eof,
240 * *eofp will be set, and *prevp will contain the last entry (null if none).
241 * Else, *lastxp will be set to the index of the found
242 * entry; *gotp will contain the entry.
244 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
245 xfs_bmap_search_extents(
246 xfs_inode_t *ip, /* incore inode pointer */
247 xfs_fileoff_t bno, /* block number searched for */
248 int whichfork, /* data or attr fork */
249 int *eofp, /* out: end of file found */
250 xfs_extnum_t *lastxp, /* out: last extent index */
251 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
252 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
255 * Check the last inode extent to determine whether this allocation will result
256 * in blocks being allocated at the end of the file. When we allocate new data
257 * blocks at the end of the file which do not start at the previous data block,
258 * we will try to align the new blocks at stripe unit boundaries.
260 STATIC int /* error */
261 xfs_bmap_isaeof(
262 xfs_inode_t *ip, /* incore inode pointer */
263 xfs_fileoff_t off, /* file offset in fsblocks */
264 int whichfork, /* data or attribute fork */
265 char *aeof); /* return value */
268 * Compute the worst-case number of indirect blocks that will be used
269 * for ip's delayed extent of length "len".
271 STATIC xfs_filblks_t
272 xfs_bmap_worst_indlen(
273 xfs_inode_t *ip, /* incore inode pointer */
274 xfs_filblks_t len); /* delayed extent length */
276 #ifdef DEBUG
278 * Perform various validation checks on the values being returned
279 * from xfs_bmapi().
281 STATIC void
282 xfs_bmap_validate_ret(
283 xfs_fileoff_t bno,
284 xfs_filblks_t len,
285 int flags,
286 xfs_bmbt_irec_t *mval,
287 int nmap,
288 int ret_nmap);
289 #else
290 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
291 #endif /* DEBUG */
293 STATIC int
294 xfs_bmap_count_tree(
295 xfs_mount_t *mp,
296 xfs_trans_t *tp,
297 xfs_ifork_t *ifp,
298 xfs_fsblock_t blockno,
299 int levelin,
300 int *count);
302 STATIC void
303 xfs_bmap_count_leaves(
304 xfs_ifork_t *ifp,
305 xfs_extnum_t idx,
306 int numrecs,
307 int *count);
309 STATIC void
310 xfs_bmap_disk_count_leaves(
311 struct xfs_mount *mp,
312 struct xfs_btree_block *block,
313 int numrecs,
314 int *count);
317 * Bmap internal routines.
320 STATIC int /* error */
321 xfs_bmbt_lookup_eq(
322 struct xfs_btree_cur *cur,
323 xfs_fileoff_t off,
324 xfs_fsblock_t bno,
325 xfs_filblks_t len,
326 int *stat) /* success/failure */
328 cur->bc_rec.b.br_startoff = off;
329 cur->bc_rec.b.br_startblock = bno;
330 cur->bc_rec.b.br_blockcount = len;
331 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
334 STATIC int /* error */
335 xfs_bmbt_lookup_ge(
336 struct xfs_btree_cur *cur,
337 xfs_fileoff_t off,
338 xfs_fsblock_t bno,
339 xfs_filblks_t len,
340 int *stat) /* success/failure */
342 cur->bc_rec.b.br_startoff = off;
343 cur->bc_rec.b.br_startblock = bno;
344 cur->bc_rec.b.br_blockcount = len;
345 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
349 * Update the record referred to by cur to the value given
350 * by [off, bno, len, state].
351 * This either works (return 0) or gets an EFSCORRUPTED error.
353 STATIC int
354 xfs_bmbt_update(
355 struct xfs_btree_cur *cur,
356 xfs_fileoff_t off,
357 xfs_fsblock_t bno,
358 xfs_filblks_t len,
359 xfs_exntst_t state)
361 union xfs_btree_rec rec;
363 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
364 return xfs_btree_update(cur, &rec);
368 * Called from xfs_bmap_add_attrfork to handle btree format files.
370 STATIC int /* error */
371 xfs_bmap_add_attrfork_btree(
372 xfs_trans_t *tp, /* transaction pointer */
373 xfs_inode_t *ip, /* incore inode pointer */
374 xfs_fsblock_t *firstblock, /* first block allocated */
375 xfs_bmap_free_t *flist, /* blocks to free at commit */
376 int *flags) /* inode logging flags */
378 xfs_btree_cur_t *cur; /* btree cursor */
379 int error; /* error return value */
380 xfs_mount_t *mp; /* file system mount struct */
381 int stat; /* newroot status */
383 mp = ip->i_mount;
384 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
385 *flags |= XFS_ILOG_DBROOT;
386 else {
387 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
388 cur->bc_private.b.flist = flist;
389 cur->bc_private.b.firstblock = *firstblock;
390 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
391 goto error0;
392 /* must be at least one entry */
393 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
394 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
395 goto error0;
396 if (stat == 0) {
397 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
398 return XFS_ERROR(ENOSPC);
400 *firstblock = cur->bc_private.b.firstblock;
401 cur->bc_private.b.allocated = 0;
402 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
404 return 0;
405 error0:
406 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
407 return error;
411 * Called from xfs_bmap_add_attrfork to handle extents format files.
413 STATIC int /* error */
414 xfs_bmap_add_attrfork_extents(
415 xfs_trans_t *tp, /* transaction pointer */
416 xfs_inode_t *ip, /* incore inode pointer */
417 xfs_fsblock_t *firstblock, /* first block allocated */
418 xfs_bmap_free_t *flist, /* blocks to free at commit */
419 int *flags) /* inode logging flags */
421 xfs_btree_cur_t *cur; /* bmap btree cursor */
422 int error; /* error return value */
424 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
425 return 0;
426 cur = NULL;
427 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
428 flags, XFS_DATA_FORK);
429 if (cur) {
430 cur->bc_private.b.allocated = 0;
431 xfs_btree_del_cursor(cur,
432 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
434 return error;
438 * Called from xfs_bmap_add_attrfork to handle local format files.
440 STATIC int /* error */
441 xfs_bmap_add_attrfork_local(
442 xfs_trans_t *tp, /* transaction pointer */
443 xfs_inode_t *ip, /* incore inode pointer */
444 xfs_fsblock_t *firstblock, /* first block allocated */
445 xfs_bmap_free_t *flist, /* blocks to free at commit */
446 int *flags) /* inode logging flags */
448 xfs_da_args_t dargs; /* args for dir/attr code */
449 int error; /* error return value */
450 xfs_mount_t *mp; /* mount structure pointer */
452 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
453 return 0;
454 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
455 mp = ip->i_mount;
456 memset(&dargs, 0, sizeof(dargs));
457 dargs.dp = ip;
458 dargs.firstblock = firstblock;
459 dargs.flist = flist;
460 dargs.total = mp->m_dirblkfsbs;
461 dargs.whichfork = XFS_DATA_FORK;
462 dargs.trans = tp;
463 error = xfs_dir2_sf_to_block(&dargs);
464 } else
465 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
466 XFS_DATA_FORK);
467 return error;
471 * Called by xfs_bmapi to update file extent records and the btree
472 * after allocating space (or doing a delayed allocation).
474 STATIC int /* error */
475 xfs_bmap_add_extent(
476 xfs_inode_t *ip, /* incore inode pointer */
477 xfs_extnum_t idx, /* extent number to update/insert */
478 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
479 xfs_bmbt_irec_t *new, /* new data to add to file extents */
480 xfs_fsblock_t *first, /* pointer to firstblock variable */
481 xfs_bmap_free_t *flist, /* list of extents to be freed */
482 int *logflagsp, /* inode logging flags */
483 int whichfork, /* data or attr fork */
484 int rsvd) /* OK to use reserved data blocks */
486 xfs_btree_cur_t *cur; /* btree cursor or null */
487 xfs_filblks_t da_new; /* new count del alloc blocks used */
488 xfs_filblks_t da_old; /* old count del alloc blocks used */
489 int error; /* error return value */
490 xfs_ifork_t *ifp; /* inode fork ptr */
491 int logflags; /* returned value */
492 xfs_extnum_t nextents; /* number of extents in file now */
494 XFS_STATS_INC(xs_add_exlist);
495 cur = *curp;
496 ifp = XFS_IFORK_PTR(ip, whichfork);
497 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
498 ASSERT(idx <= nextents);
499 da_old = da_new = 0;
500 error = 0;
502 * This is the first extent added to a new/empty file.
503 * Special case this one, so other routines get to assume there are
504 * already extents in the list.
506 if (nextents == 0) {
507 xfs_iext_insert(ip, 0, 1, new,
508 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
510 ASSERT(cur == NULL);
511 ifp->if_lastex = 0;
512 if (!isnullstartblock(new->br_startblock)) {
513 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
514 logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
515 } else
516 logflags = 0;
519 * Any kind of new delayed allocation goes here.
521 else if (isnullstartblock(new->br_startblock)) {
522 if (cur)
523 ASSERT((cur->bc_private.b.flags &
524 XFS_BTCUR_BPRV_WASDEL) == 0);
525 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
526 &logflags, rsvd)))
527 goto done;
530 * Real allocation off the end of the file.
532 else if (idx == nextents) {
533 if (cur)
534 ASSERT((cur->bc_private.b.flags &
535 XFS_BTCUR_BPRV_WASDEL) == 0);
536 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
537 &logflags, whichfork)))
538 goto done;
539 } else {
540 xfs_bmbt_irec_t prev; /* old extent at offset idx */
543 * Get the record referred to by idx.
545 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
547 * If it's a real allocation record, and the new allocation ends
548 * after the start of the referred to record, then we're filling
549 * in a delayed or unwritten allocation with a real one, or
550 * converting real back to unwritten.
552 if (!isnullstartblock(new->br_startblock) &&
553 new->br_startoff + new->br_blockcount > prev.br_startoff) {
554 if (prev.br_state != XFS_EXT_UNWRITTEN &&
555 isnullstartblock(prev.br_startblock)) {
556 da_old = startblockval(prev.br_startblock);
557 if (cur)
558 ASSERT(cur->bc_private.b.flags &
559 XFS_BTCUR_BPRV_WASDEL);
560 if ((error = xfs_bmap_add_extent_delay_real(ip,
561 idx, &cur, new, &da_new, first, flist,
562 &logflags, rsvd)))
563 goto done;
564 } else if (new->br_state == XFS_EXT_NORM) {
565 ASSERT(new->br_state == XFS_EXT_NORM);
566 if ((error = xfs_bmap_add_extent_unwritten_real(
567 ip, idx, &cur, new, &logflags)))
568 goto done;
569 } else {
570 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
571 if ((error = xfs_bmap_add_extent_unwritten_real(
572 ip, idx, &cur, new, &logflags)))
573 goto done;
575 ASSERT(*curp == cur || *curp == NULL);
578 * Otherwise we're filling in a hole with an allocation.
580 else {
581 if (cur)
582 ASSERT((cur->bc_private.b.flags &
583 XFS_BTCUR_BPRV_WASDEL) == 0);
584 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
585 new, &logflags, whichfork)))
586 goto done;
590 ASSERT(*curp == cur || *curp == NULL);
592 * Convert to a btree if necessary.
594 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
595 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
596 int tmp_logflags; /* partial log flag return val */
598 ASSERT(cur == NULL);
599 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
600 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
601 logflags |= tmp_logflags;
602 if (error)
603 goto done;
606 * Adjust for changes in reserved delayed indirect blocks.
607 * Nothing to do for disk quotas here.
609 if (da_old || da_new) {
610 xfs_filblks_t nblks;
612 nblks = da_new;
613 if (cur)
614 nblks += cur->bc_private.b.allocated;
615 ASSERT(nblks <= da_old);
616 if (nblks < da_old)
617 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
618 (int64_t)(da_old - nblks), rsvd);
621 * Clear out the allocated field, done with it now in any case.
623 if (cur) {
624 cur->bc_private.b.allocated = 0;
625 *curp = cur;
627 done:
628 #ifdef DEBUG
629 if (!error)
630 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
631 #endif
632 *logflagsp = logflags;
633 return error;
637 * Called by xfs_bmap_add_extent to handle cases converting a delayed
638 * allocation to a real allocation.
640 STATIC int /* error */
641 xfs_bmap_add_extent_delay_real(
642 xfs_inode_t *ip, /* incore inode pointer */
643 xfs_extnum_t idx, /* extent number to update/insert */
644 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
645 xfs_bmbt_irec_t *new, /* new data to add to file extents */
646 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
647 xfs_fsblock_t *first, /* pointer to firstblock variable */
648 xfs_bmap_free_t *flist, /* list of extents to be freed */
649 int *logflagsp, /* inode logging flags */
650 int rsvd) /* OK to use reserved data block allocation */
652 xfs_btree_cur_t *cur; /* btree cursor */
653 int diff; /* temp value */
654 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
655 int error; /* error return value */
656 int i; /* temp state */
657 xfs_ifork_t *ifp; /* inode fork pointer */
658 xfs_fileoff_t new_endoff; /* end offset of new entry */
659 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
660 /* left is 0, right is 1, prev is 2 */
661 int rval=0; /* return value (logging flags) */
662 int state = 0;/* state bits, accessed thru macros */
663 xfs_filblks_t temp=0; /* value for dnew calculations */
664 xfs_filblks_t temp2=0;/* value for dnew calculations */
665 int tmp_rval; /* partial logging flags */
667 #define LEFT r[0]
668 #define RIGHT r[1]
669 #define PREV r[2]
672 * Set up a bunch of variables to make the tests simpler.
674 cur = *curp;
675 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
676 ep = xfs_iext_get_ext(ifp, idx);
677 xfs_bmbt_get_all(ep, &PREV);
678 new_endoff = new->br_startoff + new->br_blockcount;
679 ASSERT(PREV.br_startoff <= new->br_startoff);
680 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
683 * Set flags determining what part of the previous delayed allocation
684 * extent is being replaced by a real allocation.
686 if (PREV.br_startoff == new->br_startoff)
687 state |= BMAP_LEFT_FILLING;
688 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
689 state |= BMAP_RIGHT_FILLING;
692 * Check and set flags if this segment has a left neighbor.
693 * Don't set contiguous if the combined extent would be too large.
695 if (idx > 0) {
696 state |= BMAP_LEFT_VALID;
697 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
699 if (isnullstartblock(LEFT.br_startblock))
700 state |= BMAP_LEFT_DELAY;
703 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
704 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
705 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
706 LEFT.br_state == new->br_state &&
707 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
708 state |= BMAP_LEFT_CONTIG;
711 * Check and set flags if this segment has a right neighbor.
712 * Don't set contiguous if the combined extent would be too large.
713 * Also check for all-three-contiguous being too large.
715 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
716 state |= BMAP_RIGHT_VALID;
717 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
719 if (isnullstartblock(RIGHT.br_startblock))
720 state |= BMAP_RIGHT_DELAY;
723 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
724 new_endoff == RIGHT.br_startoff &&
725 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
726 new->br_state == RIGHT.br_state &&
727 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
728 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
729 BMAP_RIGHT_FILLING)) !=
730 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
731 BMAP_RIGHT_FILLING) ||
732 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
733 <= MAXEXTLEN))
734 state |= BMAP_RIGHT_CONTIG;
736 error = 0;
738 * Switch out based on the FILLING and CONTIG state bits.
740 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
741 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
742 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
743 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
745 * Filling in all of a previously delayed allocation extent.
746 * The left and right neighbors are both contiguous with new.
748 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
749 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
750 LEFT.br_blockcount + PREV.br_blockcount +
751 RIGHT.br_blockcount);
752 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
754 xfs_iext_remove(ip, idx, 2, state);
755 ip->i_df.if_lastex = idx - 1;
756 ip->i_d.di_nextents--;
757 if (cur == NULL)
758 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
759 else {
760 rval = XFS_ILOG_CORE;
761 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
762 RIGHT.br_startblock,
763 RIGHT.br_blockcount, &i)))
764 goto done;
765 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
766 if ((error = xfs_btree_delete(cur, &i)))
767 goto done;
768 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
769 if ((error = xfs_btree_decrement(cur, 0, &i)))
770 goto done;
771 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
772 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
773 LEFT.br_startblock,
774 LEFT.br_blockcount +
775 PREV.br_blockcount +
776 RIGHT.br_blockcount, LEFT.br_state)))
777 goto done;
779 *dnew = 0;
780 break;
782 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
784 * Filling in all of a previously delayed allocation extent.
785 * The left neighbor is contiguous, the right is not.
787 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
788 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
789 LEFT.br_blockcount + PREV.br_blockcount);
790 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
792 ip->i_df.if_lastex = idx - 1;
793 xfs_iext_remove(ip, idx, 1, state);
794 if (cur == NULL)
795 rval = XFS_ILOG_DEXT;
796 else {
797 rval = 0;
798 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
799 LEFT.br_startblock, LEFT.br_blockcount,
800 &i)))
801 goto done;
802 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
803 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
804 LEFT.br_startblock,
805 LEFT.br_blockcount +
806 PREV.br_blockcount, LEFT.br_state)))
807 goto done;
809 *dnew = 0;
810 break;
812 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
814 * Filling in all of a previously delayed allocation extent.
815 * The right neighbor is contiguous, the left is not.
817 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
818 xfs_bmbt_set_startblock(ep, new->br_startblock);
819 xfs_bmbt_set_blockcount(ep,
820 PREV.br_blockcount + RIGHT.br_blockcount);
821 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
823 ip->i_df.if_lastex = idx;
824 xfs_iext_remove(ip, idx + 1, 1, state);
825 if (cur == NULL)
826 rval = XFS_ILOG_DEXT;
827 else {
828 rval = 0;
829 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
830 RIGHT.br_startblock,
831 RIGHT.br_blockcount, &i)))
832 goto done;
833 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
834 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
835 new->br_startblock,
836 PREV.br_blockcount +
837 RIGHT.br_blockcount, PREV.br_state)))
838 goto done;
840 *dnew = 0;
841 break;
843 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
845 * Filling in all of a previously delayed allocation extent.
846 * Neither the left nor right neighbors are contiguous with
847 * the new one.
849 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
850 xfs_bmbt_set_startblock(ep, new->br_startblock);
851 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
853 ip->i_df.if_lastex = idx;
854 ip->i_d.di_nextents++;
855 if (cur == NULL)
856 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
857 else {
858 rval = XFS_ILOG_CORE;
859 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
860 new->br_startblock, new->br_blockcount,
861 &i)))
862 goto done;
863 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
864 cur->bc_rec.b.br_state = XFS_EXT_NORM;
865 if ((error = xfs_btree_insert(cur, &i)))
866 goto done;
867 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
869 *dnew = 0;
870 break;
872 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
874 * Filling in the first part of a previous delayed allocation.
875 * The left neighbor is contiguous.
877 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
878 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
879 LEFT.br_blockcount + new->br_blockcount);
880 xfs_bmbt_set_startoff(ep,
881 PREV.br_startoff + new->br_blockcount);
882 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
884 temp = PREV.br_blockcount - new->br_blockcount;
885 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
886 xfs_bmbt_set_blockcount(ep, temp);
887 ip->i_df.if_lastex = idx - 1;
888 if (cur == NULL)
889 rval = XFS_ILOG_DEXT;
890 else {
891 rval = 0;
892 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
893 LEFT.br_startblock, LEFT.br_blockcount,
894 &i)))
895 goto done;
896 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
897 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
898 LEFT.br_startblock,
899 LEFT.br_blockcount +
900 new->br_blockcount,
901 LEFT.br_state)))
902 goto done;
904 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
905 startblockval(PREV.br_startblock));
906 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
907 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
908 *dnew = temp;
909 break;
911 case BMAP_LEFT_FILLING:
913 * Filling in the first part of a previous delayed allocation.
914 * The left neighbor is not contiguous.
916 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
917 xfs_bmbt_set_startoff(ep, new_endoff);
918 temp = PREV.br_blockcount - new->br_blockcount;
919 xfs_bmbt_set_blockcount(ep, temp);
920 xfs_iext_insert(ip, idx, 1, new, state);
921 ip->i_df.if_lastex = idx;
922 ip->i_d.di_nextents++;
923 if (cur == NULL)
924 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
925 else {
926 rval = XFS_ILOG_CORE;
927 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
928 new->br_startblock, new->br_blockcount,
929 &i)))
930 goto done;
931 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
932 cur->bc_rec.b.br_state = XFS_EXT_NORM;
933 if ((error = xfs_btree_insert(cur, &i)))
934 goto done;
935 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
937 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
938 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
939 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
940 first, flist, &cur, 1, &tmp_rval,
941 XFS_DATA_FORK);
942 rval |= tmp_rval;
943 if (error)
944 goto done;
946 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
947 startblockval(PREV.br_startblock) -
948 (cur ? cur->bc_private.b.allocated : 0));
949 ep = xfs_iext_get_ext(ifp, idx + 1);
950 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
951 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
952 *dnew = temp;
953 break;
955 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
957 * Filling in the last part of a previous delayed allocation.
958 * The right neighbor is contiguous with the new allocation.
960 temp = PREV.br_blockcount - new->br_blockcount;
961 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
962 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
963 xfs_bmbt_set_blockcount(ep, temp);
964 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
965 new->br_startoff, new->br_startblock,
966 new->br_blockcount + RIGHT.br_blockcount,
967 RIGHT.br_state);
968 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
969 ip->i_df.if_lastex = idx + 1;
970 if (cur == NULL)
971 rval = XFS_ILOG_DEXT;
972 else {
973 rval = 0;
974 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
975 RIGHT.br_startblock,
976 RIGHT.br_blockcount, &i)))
977 goto done;
978 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
979 if ((error = xfs_bmbt_update(cur, new->br_startoff,
980 new->br_startblock,
981 new->br_blockcount +
982 RIGHT.br_blockcount,
983 RIGHT.br_state)))
984 goto done;
986 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
987 startblockval(PREV.br_startblock));
988 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
989 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
990 *dnew = temp;
991 break;
993 case BMAP_RIGHT_FILLING:
995 * Filling in the last part of a previous delayed allocation.
996 * The right neighbor is not contiguous.
998 temp = PREV.br_blockcount - new->br_blockcount;
999 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1000 xfs_bmbt_set_blockcount(ep, temp);
1001 xfs_iext_insert(ip, idx + 1, 1, new, state);
1002 ip->i_df.if_lastex = idx + 1;
1003 ip->i_d.di_nextents++;
1004 if (cur == NULL)
1005 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1006 else {
1007 rval = XFS_ILOG_CORE;
1008 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1009 new->br_startblock, new->br_blockcount,
1010 &i)))
1011 goto done;
1012 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1013 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1014 if ((error = xfs_btree_insert(cur, &i)))
1015 goto done;
1016 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1018 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1019 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1020 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1021 first, flist, &cur, 1, &tmp_rval,
1022 XFS_DATA_FORK);
1023 rval |= tmp_rval;
1024 if (error)
1025 goto done;
1027 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1028 startblockval(PREV.br_startblock) -
1029 (cur ? cur->bc_private.b.allocated : 0));
1030 ep = xfs_iext_get_ext(ifp, idx);
1031 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1032 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1033 *dnew = temp;
1034 break;
1036 case 0:
1038 * Filling in the middle part of a previous delayed allocation.
1039 * Contiguity is impossible here.
1040 * This case is avoided almost all the time.
1042 temp = new->br_startoff - PREV.br_startoff;
1043 trace_xfs_bmap_pre_update(ip, idx, 0, _THIS_IP_);
1044 xfs_bmbt_set_blockcount(ep, temp);
1045 r[0] = *new;
1046 r[1].br_state = PREV.br_state;
1047 r[1].br_startblock = 0;
1048 r[1].br_startoff = new_endoff;
1049 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1050 r[1].br_blockcount = temp2;
1051 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
1052 ip->i_df.if_lastex = idx + 1;
1053 ip->i_d.di_nextents++;
1054 if (cur == NULL)
1055 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1056 else {
1057 rval = XFS_ILOG_CORE;
1058 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1059 new->br_startblock, new->br_blockcount,
1060 &i)))
1061 goto done;
1062 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1063 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1064 if ((error = xfs_btree_insert(cur, &i)))
1065 goto done;
1066 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1068 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1069 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1070 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1071 first, flist, &cur, 1, &tmp_rval,
1072 XFS_DATA_FORK);
1073 rval |= tmp_rval;
1074 if (error)
1075 goto done;
1077 temp = xfs_bmap_worst_indlen(ip, temp);
1078 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1079 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
1080 (cur ? cur->bc_private.b.allocated : 0));
1081 if (diff > 0 &&
1082 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
1084 * Ick gross gag me with a spoon.
1086 ASSERT(0); /* want to see if this ever happens! */
1087 while (diff > 0) {
1088 if (temp) {
1089 temp--;
1090 diff--;
1091 if (!diff ||
1092 !xfs_mod_incore_sb(ip->i_mount,
1093 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1094 break;
1096 if (temp2) {
1097 temp2--;
1098 diff--;
1099 if (!diff ||
1100 !xfs_mod_incore_sb(ip->i_mount,
1101 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1102 break;
1106 ep = xfs_iext_get_ext(ifp, idx);
1107 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1108 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1109 trace_xfs_bmap_pre_update(ip, idx + 2, state, _THIS_IP_);
1110 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1111 nullstartblock((int)temp2));
1112 trace_xfs_bmap_post_update(ip, idx + 2, state, _THIS_IP_);
1113 *dnew = temp + temp2;
1114 break;
1116 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1117 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1118 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1119 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1120 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1121 case BMAP_LEFT_CONTIG:
1122 case BMAP_RIGHT_CONTIG:
1124 * These cases are all impossible.
1126 ASSERT(0);
1128 *curp = cur;
1129 done:
1130 *logflagsp = rval;
1131 return error;
1132 #undef LEFT
1133 #undef RIGHT
1134 #undef PREV
1138 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1139 * allocation to a real allocation or vice versa.
1141 STATIC int /* error */
1142 xfs_bmap_add_extent_unwritten_real(
1143 xfs_inode_t *ip, /* incore inode pointer */
1144 xfs_extnum_t idx, /* extent number to update/insert */
1145 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1146 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1147 int *logflagsp) /* inode logging flags */
1149 xfs_btree_cur_t *cur; /* btree cursor */
1150 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1151 int error; /* error return value */
1152 int i; /* temp state */
1153 xfs_ifork_t *ifp; /* inode fork pointer */
1154 xfs_fileoff_t new_endoff; /* end offset of new entry */
1155 xfs_exntst_t newext; /* new extent state */
1156 xfs_exntst_t oldext; /* old extent state */
1157 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1158 /* left is 0, right is 1, prev is 2 */
1159 int rval=0; /* return value (logging flags) */
1160 int state = 0;/* state bits, accessed thru macros */
1162 #define LEFT r[0]
1163 #define RIGHT r[1]
1164 #define PREV r[2]
1166 * Set up a bunch of variables to make the tests simpler.
1168 error = 0;
1169 cur = *curp;
1170 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1171 ep = xfs_iext_get_ext(ifp, idx);
1172 xfs_bmbt_get_all(ep, &PREV);
1173 newext = new->br_state;
1174 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1175 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1176 ASSERT(PREV.br_state == oldext);
1177 new_endoff = new->br_startoff + new->br_blockcount;
1178 ASSERT(PREV.br_startoff <= new->br_startoff);
1179 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1182 * Set flags determining what part of the previous oldext allocation
1183 * extent is being replaced by a newext allocation.
1185 if (PREV.br_startoff == new->br_startoff)
1186 state |= BMAP_LEFT_FILLING;
1187 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1188 state |= BMAP_RIGHT_FILLING;
1191 * Check and set flags if this segment has a left neighbor.
1192 * Don't set contiguous if the combined extent would be too large.
1194 if (idx > 0) {
1195 state |= BMAP_LEFT_VALID;
1196 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1198 if (isnullstartblock(LEFT.br_startblock))
1199 state |= BMAP_LEFT_DELAY;
1202 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1203 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1204 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1205 LEFT.br_state == newext &&
1206 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1207 state |= BMAP_LEFT_CONTIG;
1210 * Check and set flags if this segment has a right neighbor.
1211 * Don't set contiguous if the combined extent would be too large.
1212 * Also check for all-three-contiguous being too large.
1214 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1215 state |= BMAP_RIGHT_VALID;
1216 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1217 if (isnullstartblock(RIGHT.br_startblock))
1218 state |= BMAP_RIGHT_DELAY;
1221 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1222 new_endoff == RIGHT.br_startoff &&
1223 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1224 newext == RIGHT.br_state &&
1225 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1226 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1227 BMAP_RIGHT_FILLING)) !=
1228 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1229 BMAP_RIGHT_FILLING) ||
1230 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1231 <= MAXEXTLEN))
1232 state |= BMAP_RIGHT_CONTIG;
1235 * Switch out based on the FILLING and CONTIG state bits.
1237 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1238 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1239 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1240 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1242 * Setting all of a previous oldext extent to newext.
1243 * The left and right neighbors are both contiguous with new.
1245 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1246 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1247 LEFT.br_blockcount + PREV.br_blockcount +
1248 RIGHT.br_blockcount);
1249 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1251 xfs_iext_remove(ip, idx, 2, state);
1252 ip->i_df.if_lastex = idx - 1;
1253 ip->i_d.di_nextents -= 2;
1254 if (cur == NULL)
1255 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1256 else {
1257 rval = XFS_ILOG_CORE;
1258 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1259 RIGHT.br_startblock,
1260 RIGHT.br_blockcount, &i)))
1261 goto done;
1262 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1263 if ((error = xfs_btree_delete(cur, &i)))
1264 goto done;
1265 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1266 if ((error = xfs_btree_decrement(cur, 0, &i)))
1267 goto done;
1268 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1269 if ((error = xfs_btree_delete(cur, &i)))
1270 goto done;
1271 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1272 if ((error = xfs_btree_decrement(cur, 0, &i)))
1273 goto done;
1274 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1275 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1276 LEFT.br_startblock,
1277 LEFT.br_blockcount + PREV.br_blockcount +
1278 RIGHT.br_blockcount, LEFT.br_state)))
1279 goto done;
1281 break;
1283 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1285 * Setting all of a previous oldext extent to newext.
1286 * The left neighbor is contiguous, the right is not.
1288 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1289 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1290 LEFT.br_blockcount + PREV.br_blockcount);
1291 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1293 ip->i_df.if_lastex = idx - 1;
1294 xfs_iext_remove(ip, idx, 1, state);
1295 ip->i_d.di_nextents--;
1296 if (cur == NULL)
1297 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1298 else {
1299 rval = XFS_ILOG_CORE;
1300 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1301 PREV.br_startblock, PREV.br_blockcount,
1302 &i)))
1303 goto done;
1304 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1305 if ((error = xfs_btree_delete(cur, &i)))
1306 goto done;
1307 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1308 if ((error = xfs_btree_decrement(cur, 0, &i)))
1309 goto done;
1310 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1311 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1312 LEFT.br_startblock,
1313 LEFT.br_blockcount + PREV.br_blockcount,
1314 LEFT.br_state)))
1315 goto done;
1317 break;
1319 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1321 * Setting all of a previous oldext extent to newext.
1322 * The right neighbor is contiguous, the left is not.
1324 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1325 xfs_bmbt_set_blockcount(ep,
1326 PREV.br_blockcount + RIGHT.br_blockcount);
1327 xfs_bmbt_set_state(ep, newext);
1328 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1329 ip->i_df.if_lastex = idx;
1330 xfs_iext_remove(ip, idx + 1, 1, state);
1331 ip->i_d.di_nextents--;
1332 if (cur == NULL)
1333 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1334 else {
1335 rval = XFS_ILOG_CORE;
1336 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1337 RIGHT.br_startblock,
1338 RIGHT.br_blockcount, &i)))
1339 goto done;
1340 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1341 if ((error = xfs_btree_delete(cur, &i)))
1342 goto done;
1343 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1344 if ((error = xfs_btree_decrement(cur, 0, &i)))
1345 goto done;
1346 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1347 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1348 new->br_startblock,
1349 new->br_blockcount + RIGHT.br_blockcount,
1350 newext)))
1351 goto done;
1353 break;
1355 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1357 * Setting all of a previous oldext extent to newext.
1358 * Neither the left nor right neighbors are contiguous with
1359 * the new one.
1361 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1362 xfs_bmbt_set_state(ep, newext);
1363 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1365 ip->i_df.if_lastex = idx;
1366 if (cur == NULL)
1367 rval = XFS_ILOG_DEXT;
1368 else {
1369 rval = 0;
1370 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1371 new->br_startblock, new->br_blockcount,
1372 &i)))
1373 goto done;
1374 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1375 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1376 new->br_startblock, new->br_blockcount,
1377 newext)))
1378 goto done;
1380 break;
1382 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1384 * Setting the first part of a previous oldext extent to newext.
1385 * The left neighbor is contiguous.
1387 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1388 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1389 LEFT.br_blockcount + new->br_blockcount);
1390 xfs_bmbt_set_startoff(ep,
1391 PREV.br_startoff + new->br_blockcount);
1392 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1394 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1395 xfs_bmbt_set_startblock(ep,
1396 new->br_startblock + new->br_blockcount);
1397 xfs_bmbt_set_blockcount(ep,
1398 PREV.br_blockcount - new->br_blockcount);
1399 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1401 ip->i_df.if_lastex = idx - 1;
1402 if (cur == NULL)
1403 rval = XFS_ILOG_DEXT;
1404 else {
1405 rval = 0;
1406 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1407 PREV.br_startblock, PREV.br_blockcount,
1408 &i)))
1409 goto done;
1410 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1411 if ((error = xfs_bmbt_update(cur,
1412 PREV.br_startoff + new->br_blockcount,
1413 PREV.br_startblock + new->br_blockcount,
1414 PREV.br_blockcount - new->br_blockcount,
1415 oldext)))
1416 goto done;
1417 if ((error = xfs_btree_decrement(cur, 0, &i)))
1418 goto done;
1419 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1420 LEFT.br_startblock,
1421 LEFT.br_blockcount + new->br_blockcount,
1422 LEFT.br_state))
1423 goto done;
1425 break;
1427 case BMAP_LEFT_FILLING:
1429 * Setting the first part of a previous oldext extent to newext.
1430 * The left neighbor is not contiguous.
1432 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1433 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1434 xfs_bmbt_set_startoff(ep, new_endoff);
1435 xfs_bmbt_set_blockcount(ep,
1436 PREV.br_blockcount - new->br_blockcount);
1437 xfs_bmbt_set_startblock(ep,
1438 new->br_startblock + new->br_blockcount);
1439 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1441 xfs_iext_insert(ip, idx, 1, new, state);
1442 ip->i_df.if_lastex = idx;
1443 ip->i_d.di_nextents++;
1444 if (cur == NULL)
1445 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1446 else {
1447 rval = XFS_ILOG_CORE;
1448 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1449 PREV.br_startblock, PREV.br_blockcount,
1450 &i)))
1451 goto done;
1452 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1453 if ((error = xfs_bmbt_update(cur,
1454 PREV.br_startoff + new->br_blockcount,
1455 PREV.br_startblock + new->br_blockcount,
1456 PREV.br_blockcount - new->br_blockcount,
1457 oldext)))
1458 goto done;
1459 cur->bc_rec.b = *new;
1460 if ((error = xfs_btree_insert(cur, &i)))
1461 goto done;
1462 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1464 break;
1466 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1468 * Setting the last part of a previous oldext extent to newext.
1469 * The right neighbor is contiguous with the new allocation.
1471 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1472 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
1473 xfs_bmbt_set_blockcount(ep,
1474 PREV.br_blockcount - new->br_blockcount);
1475 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1476 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1477 new->br_startoff, new->br_startblock,
1478 new->br_blockcount + RIGHT.br_blockcount, newext);
1479 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
1481 ip->i_df.if_lastex = idx + 1;
1482 if (cur == NULL)
1483 rval = XFS_ILOG_DEXT;
1484 else {
1485 rval = 0;
1486 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1487 PREV.br_startblock,
1488 PREV.br_blockcount, &i)))
1489 goto done;
1490 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1491 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1492 PREV.br_startblock,
1493 PREV.br_blockcount - new->br_blockcount,
1494 oldext)))
1495 goto done;
1496 if ((error = xfs_btree_increment(cur, 0, &i)))
1497 goto done;
1498 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1499 new->br_startblock,
1500 new->br_blockcount + RIGHT.br_blockcount,
1501 newext)))
1502 goto done;
1504 break;
1506 case BMAP_RIGHT_FILLING:
1508 * Setting the last part of a previous oldext extent to newext.
1509 * The right neighbor is not contiguous.
1511 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1512 xfs_bmbt_set_blockcount(ep,
1513 PREV.br_blockcount - new->br_blockcount);
1514 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1516 xfs_iext_insert(ip, idx + 1, 1, new, state);
1517 ip->i_df.if_lastex = idx + 1;
1518 ip->i_d.di_nextents++;
1519 if (cur == NULL)
1520 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1521 else {
1522 rval = XFS_ILOG_CORE;
1523 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1524 PREV.br_startblock, PREV.br_blockcount,
1525 &i)))
1526 goto done;
1527 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1528 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1529 PREV.br_startblock,
1530 PREV.br_blockcount - new->br_blockcount,
1531 oldext)))
1532 goto done;
1533 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1534 new->br_startblock, new->br_blockcount,
1535 &i)))
1536 goto done;
1537 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1538 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1539 if ((error = xfs_btree_insert(cur, &i)))
1540 goto done;
1541 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1543 break;
1545 case 0:
1547 * Setting the middle part of a previous oldext extent to
1548 * newext. Contiguity is impossible here.
1549 * One extent becomes three extents.
1551 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1552 xfs_bmbt_set_blockcount(ep,
1553 new->br_startoff - PREV.br_startoff);
1554 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1556 r[0] = *new;
1557 r[1].br_startoff = new_endoff;
1558 r[1].br_blockcount =
1559 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1560 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1561 r[1].br_state = oldext;
1562 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
1563 ip->i_df.if_lastex = idx + 1;
1564 ip->i_d.di_nextents += 2;
1565 if (cur == NULL)
1566 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1567 else {
1568 rval = XFS_ILOG_CORE;
1569 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1570 PREV.br_startblock, PREV.br_blockcount,
1571 &i)))
1572 goto done;
1573 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1574 /* new right extent - oldext */
1575 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1576 r[1].br_startblock, r[1].br_blockcount,
1577 r[1].br_state)))
1578 goto done;
1579 /* new left extent - oldext */
1580 cur->bc_rec.b = PREV;
1581 cur->bc_rec.b.br_blockcount =
1582 new->br_startoff - PREV.br_startoff;
1583 if ((error = xfs_btree_insert(cur, &i)))
1584 goto done;
1585 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1587 * Reset the cursor to the position of the new extent
1588 * we are about to insert as we can't trust it after
1589 * the previous insert.
1591 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1592 new->br_startblock, new->br_blockcount,
1593 &i)))
1594 goto done;
1595 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1596 /* new middle extent - newext */
1597 cur->bc_rec.b.br_state = new->br_state;
1598 if ((error = xfs_btree_insert(cur, &i)))
1599 goto done;
1600 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1602 break;
1604 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1605 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1606 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1607 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1608 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1609 case BMAP_LEFT_CONTIG:
1610 case BMAP_RIGHT_CONTIG:
1612 * These cases are all impossible.
1614 ASSERT(0);
1616 *curp = cur;
1617 done:
1618 *logflagsp = rval;
1619 return error;
1620 #undef LEFT
1621 #undef RIGHT
1622 #undef PREV
1626 * Called by xfs_bmap_add_extent to handle cases converting a hole
1627 * to a delayed allocation.
1629 /*ARGSUSED*/
1630 STATIC int /* error */
1631 xfs_bmap_add_extent_hole_delay(
1632 xfs_inode_t *ip, /* incore inode pointer */
1633 xfs_extnum_t idx, /* extent number to update/insert */
1634 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1635 int *logflagsp, /* inode logging flags */
1636 int rsvd) /* OK to allocate reserved blocks */
1638 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
1639 xfs_ifork_t *ifp; /* inode fork pointer */
1640 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1641 xfs_filblks_t newlen=0; /* new indirect size */
1642 xfs_filblks_t oldlen=0; /* old indirect size */
1643 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1644 int state; /* state bits, accessed thru macros */
1645 xfs_filblks_t temp=0; /* temp for indirect calculations */
1647 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1648 ep = xfs_iext_get_ext(ifp, idx);
1649 state = 0;
1650 ASSERT(isnullstartblock(new->br_startblock));
1653 * Check and set flags if this segment has a left neighbor
1655 if (idx > 0) {
1656 state |= BMAP_LEFT_VALID;
1657 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1659 if (isnullstartblock(left.br_startblock))
1660 state |= BMAP_LEFT_DELAY;
1664 * Check and set flags if the current (right) segment exists.
1665 * If it doesn't exist, we're converting the hole at end-of-file.
1667 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1668 state |= BMAP_RIGHT_VALID;
1669 xfs_bmbt_get_all(ep, &right);
1671 if (isnullstartblock(right.br_startblock))
1672 state |= BMAP_RIGHT_DELAY;
1676 * Set contiguity flags on the left and right neighbors.
1677 * Don't let extents get too large, even if the pieces are contiguous.
1679 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1680 left.br_startoff + left.br_blockcount == new->br_startoff &&
1681 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1682 state |= BMAP_LEFT_CONTIG;
1684 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1685 new->br_startoff + new->br_blockcount == right.br_startoff &&
1686 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1687 (!(state & BMAP_LEFT_CONTIG) ||
1688 (left.br_blockcount + new->br_blockcount +
1689 right.br_blockcount <= MAXEXTLEN)))
1690 state |= BMAP_RIGHT_CONTIG;
1693 * Switch out based on the contiguity flags.
1695 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1696 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1698 * New allocation is contiguous with delayed allocations
1699 * on the left and on the right.
1700 * Merge all three into a single extent record.
1702 temp = left.br_blockcount + new->br_blockcount +
1703 right.br_blockcount;
1705 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1706 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1707 oldlen = startblockval(left.br_startblock) +
1708 startblockval(new->br_startblock) +
1709 startblockval(right.br_startblock);
1710 newlen = xfs_bmap_worst_indlen(ip, temp);
1711 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1712 nullstartblock((int)newlen));
1713 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1715 xfs_iext_remove(ip, idx, 1, state);
1716 ip->i_df.if_lastex = idx - 1;
1717 break;
1719 case BMAP_LEFT_CONTIG:
1721 * New allocation is contiguous with a delayed allocation
1722 * on the left.
1723 * Merge the new allocation with the left neighbor.
1725 temp = left.br_blockcount + new->br_blockcount;
1726 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1727 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1728 oldlen = startblockval(left.br_startblock) +
1729 startblockval(new->br_startblock);
1730 newlen = xfs_bmap_worst_indlen(ip, temp);
1731 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1732 nullstartblock((int)newlen));
1733 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1735 ip->i_df.if_lastex = idx - 1;
1736 break;
1738 case BMAP_RIGHT_CONTIG:
1740 * New allocation is contiguous with a delayed allocation
1741 * on the right.
1742 * Merge the new allocation with the right neighbor.
1744 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1745 temp = new->br_blockcount + right.br_blockcount;
1746 oldlen = startblockval(new->br_startblock) +
1747 startblockval(right.br_startblock);
1748 newlen = xfs_bmap_worst_indlen(ip, temp);
1749 xfs_bmbt_set_allf(ep, new->br_startoff,
1750 nullstartblock((int)newlen), temp, right.br_state);
1751 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1753 ip->i_df.if_lastex = idx;
1754 break;
1756 case 0:
1758 * New allocation is not contiguous with another
1759 * delayed allocation.
1760 * Insert a new entry.
1762 oldlen = newlen = 0;
1763 xfs_iext_insert(ip, idx, 1, new, state);
1764 ip->i_df.if_lastex = idx;
1765 break;
1767 if (oldlen != newlen) {
1768 ASSERT(oldlen > newlen);
1769 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
1770 (int64_t)(oldlen - newlen), rsvd);
1772 * Nothing to do for disk quota accounting here.
1775 *logflagsp = 0;
1776 return 0;
1780 * Called by xfs_bmap_add_extent to handle cases converting a hole
1781 * to a real allocation.
1783 STATIC int /* error */
1784 xfs_bmap_add_extent_hole_real(
1785 xfs_inode_t *ip, /* incore inode pointer */
1786 xfs_extnum_t idx, /* extent number to update/insert */
1787 xfs_btree_cur_t *cur, /* if null, not a btree */
1788 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1789 int *logflagsp, /* inode logging flags */
1790 int whichfork) /* data or attr fork */
1792 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
1793 int error; /* error return value */
1794 int i; /* temp state */
1795 xfs_ifork_t *ifp; /* inode fork pointer */
1796 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1797 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1798 int rval=0; /* return value (logging flags) */
1799 int state; /* state bits, accessed thru macros */
1801 ifp = XFS_IFORK_PTR(ip, whichfork);
1802 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
1803 ep = xfs_iext_get_ext(ifp, idx);
1804 state = 0;
1806 if (whichfork == XFS_ATTR_FORK)
1807 state |= BMAP_ATTRFORK;
1810 * Check and set flags if this segment has a left neighbor.
1812 if (idx > 0) {
1813 state |= BMAP_LEFT_VALID;
1814 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1815 if (isnullstartblock(left.br_startblock))
1816 state |= BMAP_LEFT_DELAY;
1820 * Check and set flags if this segment has a current value.
1821 * Not true if we're inserting into the "hole" at eof.
1823 if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1824 state |= BMAP_RIGHT_VALID;
1825 xfs_bmbt_get_all(ep, &right);
1826 if (isnullstartblock(right.br_startblock))
1827 state |= BMAP_RIGHT_DELAY;
1831 * We're inserting a real allocation between "left" and "right".
1832 * Set the contiguity flags. Don't let extents get too large.
1834 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1835 left.br_startoff + left.br_blockcount == new->br_startoff &&
1836 left.br_startblock + left.br_blockcount == new->br_startblock &&
1837 left.br_state == new->br_state &&
1838 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1839 state |= BMAP_LEFT_CONTIG;
1841 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1842 new->br_startoff + new->br_blockcount == right.br_startoff &&
1843 new->br_startblock + new->br_blockcount == right.br_startblock &&
1844 new->br_state == right.br_state &&
1845 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1846 (!(state & BMAP_LEFT_CONTIG) ||
1847 left.br_blockcount + new->br_blockcount +
1848 right.br_blockcount <= MAXEXTLEN))
1849 state |= BMAP_RIGHT_CONTIG;
1851 error = 0;
1853 * Select which case we're in here, and implement it.
1855 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1856 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1858 * New allocation is contiguous with real allocations on the
1859 * left and on the right.
1860 * Merge all three into a single extent record.
1862 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1863 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1864 left.br_blockcount + new->br_blockcount +
1865 right.br_blockcount);
1866 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1868 xfs_iext_remove(ip, idx, 1, state);
1869 ifp->if_lastex = idx - 1;
1870 XFS_IFORK_NEXT_SET(ip, whichfork,
1871 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1872 if (cur == NULL) {
1873 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1874 } else {
1875 rval = XFS_ILOG_CORE;
1876 if ((error = xfs_bmbt_lookup_eq(cur,
1877 right.br_startoff,
1878 right.br_startblock,
1879 right.br_blockcount, &i)))
1880 goto done;
1881 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1882 if ((error = xfs_btree_delete(cur, &i)))
1883 goto done;
1884 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1885 if ((error = xfs_btree_decrement(cur, 0, &i)))
1886 goto done;
1887 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1888 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1889 left.br_startblock,
1890 left.br_blockcount +
1891 new->br_blockcount +
1892 right.br_blockcount,
1893 left.br_state)))
1894 goto done;
1896 break;
1898 case BMAP_LEFT_CONTIG:
1900 * New allocation is contiguous with a real allocation
1901 * on the left.
1902 * Merge the new allocation with the left neighbor.
1904 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1905 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1906 left.br_blockcount + new->br_blockcount);
1907 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1909 ifp->if_lastex = idx - 1;
1910 if (cur == NULL) {
1911 rval = xfs_ilog_fext(whichfork);
1912 } else {
1913 rval = 0;
1914 if ((error = xfs_bmbt_lookup_eq(cur,
1915 left.br_startoff,
1916 left.br_startblock,
1917 left.br_blockcount, &i)))
1918 goto done;
1919 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1920 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1921 left.br_startblock,
1922 left.br_blockcount +
1923 new->br_blockcount,
1924 left.br_state)))
1925 goto done;
1927 break;
1929 case BMAP_RIGHT_CONTIG:
1931 * New allocation is contiguous with a real allocation
1932 * on the right.
1933 * Merge the new allocation with the right neighbor.
1935 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1936 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
1937 new->br_blockcount + right.br_blockcount,
1938 right.br_state);
1939 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1941 ifp->if_lastex = idx;
1942 if (cur == NULL) {
1943 rval = xfs_ilog_fext(whichfork);
1944 } else {
1945 rval = 0;
1946 if ((error = xfs_bmbt_lookup_eq(cur,
1947 right.br_startoff,
1948 right.br_startblock,
1949 right.br_blockcount, &i)))
1950 goto done;
1951 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1952 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1953 new->br_startblock,
1954 new->br_blockcount +
1955 right.br_blockcount,
1956 right.br_state)))
1957 goto done;
1959 break;
1961 case 0:
1963 * New allocation is not contiguous with another
1964 * real allocation.
1965 * Insert a new entry.
1967 xfs_iext_insert(ip, idx, 1, new, state);
1968 ifp->if_lastex = idx;
1969 XFS_IFORK_NEXT_SET(ip, whichfork,
1970 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
1971 if (cur == NULL) {
1972 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1973 } else {
1974 rval = XFS_ILOG_CORE;
1975 if ((error = xfs_bmbt_lookup_eq(cur,
1976 new->br_startoff,
1977 new->br_startblock,
1978 new->br_blockcount, &i)))
1979 goto done;
1980 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1981 cur->bc_rec.b.br_state = new->br_state;
1982 if ((error = xfs_btree_insert(cur, &i)))
1983 goto done;
1984 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1986 break;
1988 done:
1989 *logflagsp = rval;
1990 return error;
1994 * Adjust the size of the new extent based on di_extsize and rt extsize.
1996 STATIC int
1997 xfs_bmap_extsize_align(
1998 xfs_mount_t *mp,
1999 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2000 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2001 xfs_extlen_t extsz, /* align to this extent size */
2002 int rt, /* is this a realtime inode? */
2003 int eof, /* is extent at end-of-file? */
2004 int delay, /* creating delalloc extent? */
2005 int convert, /* overwriting unwritten extent? */
2006 xfs_fileoff_t *offp, /* in/out: aligned offset */
2007 xfs_extlen_t *lenp) /* in/out: aligned length */
2009 xfs_fileoff_t orig_off; /* original offset */
2010 xfs_extlen_t orig_alen; /* original length */
2011 xfs_fileoff_t orig_end; /* original off+len */
2012 xfs_fileoff_t nexto; /* next file offset */
2013 xfs_fileoff_t prevo; /* previous file offset */
2014 xfs_fileoff_t align_off; /* temp for offset */
2015 xfs_extlen_t align_alen; /* temp for length */
2016 xfs_extlen_t temp; /* temp for calculations */
2018 if (convert)
2019 return 0;
2021 orig_off = align_off = *offp;
2022 orig_alen = align_alen = *lenp;
2023 orig_end = orig_off + orig_alen;
2026 * If this request overlaps an existing extent, then don't
2027 * attempt to perform any additional alignment.
2029 if (!delay && !eof &&
2030 (orig_off >= gotp->br_startoff) &&
2031 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2032 return 0;
2036 * If the file offset is unaligned vs. the extent size
2037 * we need to align it. This will be possible unless
2038 * the file was previously written with a kernel that didn't
2039 * perform this alignment, or if a truncate shot us in the
2040 * foot.
2042 temp = do_mod(orig_off, extsz);
2043 if (temp) {
2044 align_alen += temp;
2045 align_off -= temp;
2048 * Same adjustment for the end of the requested area.
2050 if ((temp = (align_alen % extsz))) {
2051 align_alen += extsz - temp;
2054 * If the previous block overlaps with this proposed allocation
2055 * then move the start forward without adjusting the length.
2057 if (prevp->br_startoff != NULLFILEOFF) {
2058 if (prevp->br_startblock == HOLESTARTBLOCK)
2059 prevo = prevp->br_startoff;
2060 else
2061 prevo = prevp->br_startoff + prevp->br_blockcount;
2062 } else
2063 prevo = 0;
2064 if (align_off != orig_off && align_off < prevo)
2065 align_off = prevo;
2067 * If the next block overlaps with this proposed allocation
2068 * then move the start back without adjusting the length,
2069 * but not before offset 0.
2070 * This may of course make the start overlap previous block,
2071 * and if we hit the offset 0 limit then the next block
2072 * can still overlap too.
2074 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2075 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2076 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2077 nexto = gotp->br_startoff + gotp->br_blockcount;
2078 else
2079 nexto = gotp->br_startoff;
2080 } else
2081 nexto = NULLFILEOFF;
2082 if (!eof &&
2083 align_off + align_alen != orig_end &&
2084 align_off + align_alen > nexto)
2085 align_off = nexto > align_alen ? nexto - align_alen : 0;
2087 * If we're now overlapping the next or previous extent that
2088 * means we can't fit an extsz piece in this hole. Just move
2089 * the start forward to the first valid spot and set
2090 * the length so we hit the end.
2092 if (align_off != orig_off && align_off < prevo)
2093 align_off = prevo;
2094 if (align_off + align_alen != orig_end &&
2095 align_off + align_alen > nexto &&
2096 nexto != NULLFILEOFF) {
2097 ASSERT(nexto > prevo);
2098 align_alen = nexto - align_off;
2102 * If realtime, and the result isn't a multiple of the realtime
2103 * extent size we need to remove blocks until it is.
2105 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2107 * We're not covering the original request, or
2108 * we won't be able to once we fix the length.
2110 if (orig_off < align_off ||
2111 orig_end > align_off + align_alen ||
2112 align_alen - temp < orig_alen)
2113 return XFS_ERROR(EINVAL);
2115 * Try to fix it by moving the start up.
2117 if (align_off + temp <= orig_off) {
2118 align_alen -= temp;
2119 align_off += temp;
2122 * Try to fix it by moving the end in.
2124 else if (align_off + align_alen - temp >= orig_end)
2125 align_alen -= temp;
2127 * Set the start to the minimum then trim the length.
2129 else {
2130 align_alen -= orig_off - align_off;
2131 align_off = orig_off;
2132 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2135 * Result doesn't cover the request, fail it.
2137 if (orig_off < align_off || orig_end > align_off + align_alen)
2138 return XFS_ERROR(EINVAL);
2139 } else {
2140 ASSERT(orig_off >= align_off);
2141 ASSERT(orig_end <= align_off + align_alen);
2144 #ifdef DEBUG
2145 if (!eof && gotp->br_startoff != NULLFILEOFF)
2146 ASSERT(align_off + align_alen <= gotp->br_startoff);
2147 if (prevp->br_startoff != NULLFILEOFF)
2148 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2149 #endif
2151 *lenp = align_alen;
2152 *offp = align_off;
2153 return 0;
2156 #define XFS_ALLOC_GAP_UNITS 4
2158 STATIC void
2159 xfs_bmap_adjacent(
2160 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2162 xfs_fsblock_t adjust; /* adjustment to block numbers */
2163 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2164 xfs_mount_t *mp; /* mount point structure */
2165 int nullfb; /* true if ap->firstblock isn't set */
2166 int rt; /* true if inode is realtime */
2168 #define ISVALID(x,y) \
2169 (rt ? \
2170 (x) < mp->m_sb.sb_rblocks : \
2171 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2172 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2173 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2175 mp = ap->ip->i_mount;
2176 nullfb = ap->firstblock == NULLFSBLOCK;
2177 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2178 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2180 * If allocating at eof, and there's a previous real block,
2181 * try to use its last block as our starting point.
2183 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2184 !isnullstartblock(ap->prevp->br_startblock) &&
2185 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2186 ap->prevp->br_startblock)) {
2187 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2189 * Adjust for the gap between prevp and us.
2191 adjust = ap->off -
2192 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2193 if (adjust &&
2194 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2195 ap->rval += adjust;
2198 * If not at eof, then compare the two neighbor blocks.
2199 * Figure out whether either one gives us a good starting point,
2200 * and pick the better one.
2202 else if (!ap->eof) {
2203 xfs_fsblock_t gotbno; /* right side block number */
2204 xfs_fsblock_t gotdiff=0; /* right side difference */
2205 xfs_fsblock_t prevbno; /* left side block number */
2206 xfs_fsblock_t prevdiff=0; /* left side difference */
2209 * If there's a previous (left) block, select a requested
2210 * start block based on it.
2212 if (ap->prevp->br_startoff != NULLFILEOFF &&
2213 !isnullstartblock(ap->prevp->br_startblock) &&
2214 (prevbno = ap->prevp->br_startblock +
2215 ap->prevp->br_blockcount) &&
2216 ISVALID(prevbno, ap->prevp->br_startblock)) {
2218 * Calculate gap to end of previous block.
2220 adjust = prevdiff = ap->off -
2221 (ap->prevp->br_startoff +
2222 ap->prevp->br_blockcount);
2224 * Figure the startblock based on the previous block's
2225 * end and the gap size.
2226 * Heuristic!
2227 * If the gap is large relative to the piece we're
2228 * allocating, or using it gives us an invalid block
2229 * number, then just use the end of the previous block.
2231 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2232 ISVALID(prevbno + prevdiff,
2233 ap->prevp->br_startblock))
2234 prevbno += adjust;
2235 else
2236 prevdiff += adjust;
2238 * If the firstblock forbids it, can't use it,
2239 * must use default.
2241 if (!rt && !nullfb &&
2242 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2243 prevbno = NULLFSBLOCK;
2246 * No previous block or can't follow it, just default.
2248 else
2249 prevbno = NULLFSBLOCK;
2251 * If there's a following (right) block, select a requested
2252 * start block based on it.
2254 if (!isnullstartblock(ap->gotp->br_startblock)) {
2256 * Calculate gap to start of next block.
2258 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2260 * Figure the startblock based on the next block's
2261 * start and the gap size.
2263 gotbno = ap->gotp->br_startblock;
2265 * Heuristic!
2266 * If the gap is large relative to the piece we're
2267 * allocating, or using it gives us an invalid block
2268 * number, then just use the start of the next block
2269 * offset by our length.
2271 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2272 ISVALID(gotbno - gotdiff, gotbno))
2273 gotbno -= adjust;
2274 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2275 gotbno -= ap->alen;
2276 gotdiff += adjust - ap->alen;
2277 } else
2278 gotdiff += adjust;
2280 * If the firstblock forbids it, can't use it,
2281 * must use default.
2283 if (!rt && !nullfb &&
2284 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2285 gotbno = NULLFSBLOCK;
2288 * No next block, just default.
2290 else
2291 gotbno = NULLFSBLOCK;
2293 * If both valid, pick the better one, else the only good
2294 * one, else ap->rval is already set (to 0 or the inode block).
2296 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2297 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2298 else if (prevbno != NULLFSBLOCK)
2299 ap->rval = prevbno;
2300 else if (gotbno != NULLFSBLOCK)
2301 ap->rval = gotbno;
2303 #undef ISVALID
2306 STATIC int
2307 xfs_bmap_rtalloc(
2308 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2310 xfs_alloctype_t atype = 0; /* type for allocation routines */
2311 int error; /* error return value */
2312 xfs_mount_t *mp; /* mount point structure */
2313 xfs_extlen_t prod = 0; /* product factor for allocators */
2314 xfs_extlen_t ralen = 0; /* realtime allocation length */
2315 xfs_extlen_t align; /* minimum allocation alignment */
2316 xfs_rtblock_t rtb;
2318 mp = ap->ip->i_mount;
2319 align = xfs_get_extsz_hint(ap->ip);
2320 prod = align / mp->m_sb.sb_rextsize;
2321 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2322 align, 1, ap->eof, 0,
2323 ap->conv, &ap->off, &ap->alen);
2324 if (error)
2325 return error;
2326 ASSERT(ap->alen);
2327 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2330 * If the offset & length are not perfectly aligned
2331 * then kill prod, it will just get us in trouble.
2333 if (do_mod(ap->off, align) || ap->alen % align)
2334 prod = 1;
2336 * Set ralen to be the actual requested length in rtextents.
2338 ralen = ap->alen / mp->m_sb.sb_rextsize;
2340 * If the old value was close enough to MAXEXTLEN that
2341 * we rounded up to it, cut it back so it's valid again.
2342 * Note that if it's a really large request (bigger than
2343 * MAXEXTLEN), we don't hear about that number, and can't
2344 * adjust the starting point to match it.
2346 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2347 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2349 * If it's an allocation to an empty file at offset 0,
2350 * pick an extent that will space things out in the rt area.
2352 if (ap->eof && ap->off == 0) {
2353 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2355 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2356 if (error)
2357 return error;
2358 ap->rval = rtx * mp->m_sb.sb_rextsize;
2359 } else {
2360 ap->rval = 0;
2363 xfs_bmap_adjacent(ap);
2366 * Realtime allocation, done through xfs_rtallocate_extent.
2368 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2369 do_div(ap->rval, mp->m_sb.sb_rextsize);
2370 rtb = ap->rval;
2371 ap->alen = ralen;
2372 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2373 &ralen, atype, ap->wasdel, prod, &rtb)))
2374 return error;
2375 if (rtb == NULLFSBLOCK && prod > 1 &&
2376 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2377 ap->alen, &ralen, atype,
2378 ap->wasdel, 1, &rtb)))
2379 return error;
2380 ap->rval = rtb;
2381 if (ap->rval != NULLFSBLOCK) {
2382 ap->rval *= mp->m_sb.sb_rextsize;
2383 ralen *= mp->m_sb.sb_rextsize;
2384 ap->alen = ralen;
2385 ap->ip->i_d.di_nblocks += ralen;
2386 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2387 if (ap->wasdel)
2388 ap->ip->i_delayed_blks -= ralen;
2390 * Adjust the disk quota also. This was reserved
2391 * earlier.
2393 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2394 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2395 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2396 } else {
2397 ap->alen = 0;
2399 return 0;
2402 STATIC int
2403 xfs_bmap_btalloc_nullfb(
2404 struct xfs_bmalloca *ap,
2405 struct xfs_alloc_arg *args,
2406 xfs_extlen_t *blen)
2408 struct xfs_mount *mp = ap->ip->i_mount;
2409 struct xfs_perag *pag;
2410 xfs_agnumber_t ag, startag;
2411 int notinit = 0;
2412 int error;
2414 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2415 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2416 else
2417 args->type = XFS_ALLOCTYPE_START_BNO;
2418 args->total = ap->total;
2421 * Search for an allocation group with a single extent large enough
2422 * for the request. If one isn't found, then adjust the minimum
2423 * allocation size to the largest space found.
2425 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2426 if (startag == NULLAGNUMBER)
2427 startag = ag = 0;
2429 pag = xfs_perag_get(mp, ag);
2430 while (*blen < ap->alen) {
2431 if (!pag->pagf_init) {
2432 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2433 XFS_ALLOC_FLAG_TRYLOCK);
2434 if (error) {
2435 xfs_perag_put(pag);
2436 return error;
2441 * See xfs_alloc_fix_freelist...
2443 if (pag->pagf_init) {
2444 xfs_extlen_t longest;
2445 longest = xfs_alloc_longest_free_extent(mp, pag);
2446 if (*blen < longest)
2447 *blen = longest;
2448 } else
2449 notinit = 1;
2451 if (xfs_inode_is_filestream(ap->ip)) {
2452 if (*blen >= ap->alen)
2453 break;
2455 if (ap->userdata) {
2457 * If startag is an invalid AG, we've
2458 * come here once before and
2459 * xfs_filestream_new_ag picked the
2460 * best currently available.
2462 * Don't continue looping, since we
2463 * could loop forever.
2465 if (startag == NULLAGNUMBER)
2466 break;
2468 error = xfs_filestream_new_ag(ap, &ag);
2469 xfs_perag_put(pag);
2470 if (error)
2471 return error;
2473 /* loop again to set 'blen'*/
2474 startag = NULLAGNUMBER;
2475 pag = xfs_perag_get(mp, ag);
2476 continue;
2479 if (++ag == mp->m_sb.sb_agcount)
2480 ag = 0;
2481 if (ag == startag)
2482 break;
2483 xfs_perag_put(pag);
2484 pag = xfs_perag_get(mp, ag);
2486 xfs_perag_put(pag);
2489 * Since the above loop did a BUF_TRYLOCK, it is
2490 * possible that there is space for this request.
2492 if (notinit || *blen < ap->minlen)
2493 args->minlen = ap->minlen;
2495 * If the best seen length is less than the request
2496 * length, use the best as the minimum.
2498 else if (*blen < ap->alen)
2499 args->minlen = *blen;
2501 * Otherwise we've seen an extent as big as alen,
2502 * use that as the minimum.
2504 else
2505 args->minlen = ap->alen;
2508 * set the failure fallback case to look in the selected
2509 * AG as the stream may have moved.
2511 if (xfs_inode_is_filestream(ap->ip))
2512 ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2514 return 0;
2517 STATIC int
2518 xfs_bmap_btalloc(
2519 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2521 xfs_mount_t *mp; /* mount point structure */
2522 xfs_alloctype_t atype = 0; /* type for allocation routines */
2523 xfs_extlen_t align; /* minimum allocation alignment */
2524 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2525 xfs_agnumber_t ag;
2526 xfs_alloc_arg_t args;
2527 xfs_extlen_t blen;
2528 xfs_extlen_t nextminlen = 0;
2529 int nullfb; /* true if ap->firstblock isn't set */
2530 int isaligned;
2531 int tryagain;
2532 int error;
2534 mp = ap->ip->i_mount;
2535 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2536 if (unlikely(align)) {
2537 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2538 align, 0, ap->eof, 0, ap->conv,
2539 &ap->off, &ap->alen);
2540 ASSERT(!error);
2541 ASSERT(ap->alen);
2543 nullfb = ap->firstblock == NULLFSBLOCK;
2544 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2545 if (nullfb) {
2546 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2547 ag = xfs_filestream_lookup_ag(ap->ip);
2548 ag = (ag != NULLAGNUMBER) ? ag : 0;
2549 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2550 } else {
2551 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2553 } else
2554 ap->rval = ap->firstblock;
2556 xfs_bmap_adjacent(ap);
2559 * If allowed, use ap->rval; otherwise must use firstblock since
2560 * it's in the right allocation group.
2562 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2564 else
2565 ap->rval = ap->firstblock;
2567 * Normal allocation, done through xfs_alloc_vextent.
2569 tryagain = isaligned = 0;
2570 args.tp = ap->tp;
2571 args.mp = mp;
2572 args.fsbno = ap->rval;
2573 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2574 args.firstblock = ap->firstblock;
2575 blen = 0;
2576 if (nullfb) {
2577 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2578 if (error)
2579 return error;
2580 } else if (ap->low) {
2581 if (xfs_inode_is_filestream(ap->ip))
2582 args.type = XFS_ALLOCTYPE_FIRST_AG;
2583 else
2584 args.type = XFS_ALLOCTYPE_START_BNO;
2585 args.total = args.minlen = ap->minlen;
2586 } else {
2587 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2588 args.total = ap->total;
2589 args.minlen = ap->minlen;
2591 /* apply extent size hints if obtained earlier */
2592 if (unlikely(align)) {
2593 args.prod = align;
2594 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2595 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2596 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
2597 args.prod = 1;
2598 args.mod = 0;
2599 } else {
2600 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
2601 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2602 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2605 * If we are not low on available data blocks, and the
2606 * underlying logical volume manager is a stripe, and
2607 * the file offset is zero then try to allocate data
2608 * blocks on stripe unit boundary.
2609 * NOTE: ap->aeof is only set if the allocation length
2610 * is >= the stripe unit and the allocation offset is
2611 * at the end of file.
2613 if (!ap->low && ap->aeof) {
2614 if (!ap->off) {
2615 args.alignment = mp->m_dalign;
2616 atype = args.type;
2617 isaligned = 1;
2619 * Adjust for alignment
2621 if (blen > args.alignment && blen <= ap->alen)
2622 args.minlen = blen - args.alignment;
2623 args.minalignslop = 0;
2624 } else {
2626 * First try an exact bno allocation.
2627 * If it fails then do a near or start bno
2628 * allocation with alignment turned on.
2630 atype = args.type;
2631 tryagain = 1;
2632 args.type = XFS_ALLOCTYPE_THIS_BNO;
2633 args.alignment = 1;
2635 * Compute the minlen+alignment for the
2636 * next case. Set slop so that the value
2637 * of minlen+alignment+slop doesn't go up
2638 * between the calls.
2640 if (blen > mp->m_dalign && blen <= ap->alen)
2641 nextminlen = blen - mp->m_dalign;
2642 else
2643 nextminlen = args.minlen;
2644 if (nextminlen + mp->m_dalign > args.minlen + 1)
2645 args.minalignslop =
2646 nextminlen + mp->m_dalign -
2647 args.minlen - 1;
2648 else
2649 args.minalignslop = 0;
2651 } else {
2652 args.alignment = 1;
2653 args.minalignslop = 0;
2655 args.minleft = ap->minleft;
2656 args.wasdel = ap->wasdel;
2657 args.isfl = 0;
2658 args.userdata = ap->userdata;
2659 if ((error = xfs_alloc_vextent(&args)))
2660 return error;
2661 if (tryagain && args.fsbno == NULLFSBLOCK) {
2663 * Exact allocation failed. Now try with alignment
2664 * turned on.
2666 args.type = atype;
2667 args.fsbno = ap->rval;
2668 args.alignment = mp->m_dalign;
2669 args.minlen = nextminlen;
2670 args.minalignslop = 0;
2671 isaligned = 1;
2672 if ((error = xfs_alloc_vextent(&args)))
2673 return error;
2675 if (isaligned && args.fsbno == NULLFSBLOCK) {
2677 * allocation failed, so turn off alignment and
2678 * try again.
2680 args.type = atype;
2681 args.fsbno = ap->rval;
2682 args.alignment = 0;
2683 if ((error = xfs_alloc_vextent(&args)))
2684 return error;
2686 if (args.fsbno == NULLFSBLOCK && nullfb &&
2687 args.minlen > ap->minlen) {
2688 args.minlen = ap->minlen;
2689 args.type = XFS_ALLOCTYPE_START_BNO;
2690 args.fsbno = ap->rval;
2691 if ((error = xfs_alloc_vextent(&args)))
2692 return error;
2694 if (args.fsbno == NULLFSBLOCK && nullfb) {
2695 args.fsbno = 0;
2696 args.type = XFS_ALLOCTYPE_FIRST_AG;
2697 args.total = ap->minlen;
2698 args.minleft = 0;
2699 if ((error = xfs_alloc_vextent(&args)))
2700 return error;
2701 ap->low = 1;
2703 if (args.fsbno != NULLFSBLOCK) {
2704 ap->firstblock = ap->rval = args.fsbno;
2705 ASSERT(nullfb || fb_agno == args.agno ||
2706 (ap->low && fb_agno < args.agno));
2707 ap->alen = args.len;
2708 ap->ip->i_d.di_nblocks += args.len;
2709 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2710 if (ap->wasdel)
2711 ap->ip->i_delayed_blks -= args.len;
2713 * Adjust the disk quota also. This was reserved
2714 * earlier.
2716 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2717 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2718 XFS_TRANS_DQ_BCOUNT,
2719 (long) args.len);
2720 } else {
2721 ap->rval = NULLFSBLOCK;
2722 ap->alen = 0;
2724 return 0;
2728 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2729 * It figures out where to ask the underlying allocator to put the new extent.
2731 STATIC int
2732 xfs_bmap_alloc(
2733 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2735 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
2736 return xfs_bmap_rtalloc(ap);
2737 return xfs_bmap_btalloc(ap);
2741 * Transform a btree format file with only one leaf node, where the
2742 * extents list will fit in the inode, into an extents format file.
2743 * Since the file extents are already in-core, all we have to do is
2744 * give up the space for the btree root and pitch the leaf block.
2746 STATIC int /* error */
2747 xfs_bmap_btree_to_extents(
2748 xfs_trans_t *tp, /* transaction pointer */
2749 xfs_inode_t *ip, /* incore inode pointer */
2750 xfs_btree_cur_t *cur, /* btree cursor */
2751 int *logflagsp, /* inode logging flags */
2752 int whichfork) /* data or attr fork */
2754 /* REFERENCED */
2755 struct xfs_btree_block *cblock;/* child btree block */
2756 xfs_fsblock_t cbno; /* child block number */
2757 xfs_buf_t *cbp; /* child block's buffer */
2758 int error; /* error return value */
2759 xfs_ifork_t *ifp; /* inode fork data */
2760 xfs_mount_t *mp; /* mount point structure */
2761 __be64 *pp; /* ptr to block address */
2762 struct xfs_btree_block *rblock;/* root btree block */
2764 mp = ip->i_mount;
2765 ifp = XFS_IFORK_PTR(ip, whichfork);
2766 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2767 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2768 rblock = ifp->if_broot;
2769 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2770 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
2771 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2772 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
2773 cbno = be64_to_cpu(*pp);
2774 *logflagsp = 0;
2775 #ifdef DEBUG
2776 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
2777 return error;
2778 #endif
2779 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2780 XFS_BMAP_BTREE_REF)))
2781 return error;
2782 cblock = XFS_BUF_TO_BLOCK(cbp);
2783 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
2784 return error;
2785 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2786 ip->i_d.di_nblocks--;
2787 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
2788 xfs_trans_binval(tp, cbp);
2789 if (cur->bc_bufs[0] == cbp)
2790 cur->bc_bufs[0] = NULL;
2791 xfs_iroot_realloc(ip, -1, whichfork);
2792 ASSERT(ifp->if_broot == NULL);
2793 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2794 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2795 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2796 return 0;
2800 * Called by xfs_bmapi to update file extent records and the btree
2801 * after removing space (or undoing a delayed allocation).
2803 STATIC int /* error */
2804 xfs_bmap_del_extent(
2805 xfs_inode_t *ip, /* incore inode pointer */
2806 xfs_trans_t *tp, /* current transaction pointer */
2807 xfs_extnum_t idx, /* extent number to update/delete */
2808 xfs_bmap_free_t *flist, /* list of extents to be freed */
2809 xfs_btree_cur_t *cur, /* if null, not a btree */
2810 xfs_bmbt_irec_t *del, /* data to remove from extents */
2811 int *logflagsp, /* inode logging flags */
2812 int whichfork, /* data or attr fork */
2813 int rsvd) /* OK to allocate reserved blocks */
2815 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2816 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2817 xfs_fsblock_t del_endblock=0; /* first block past del */
2818 xfs_fileoff_t del_endoff; /* first offset past del */
2819 int delay; /* current block is delayed allocated */
2820 int do_fx; /* free extent at end of routine */
2821 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
2822 int error; /* error return value */
2823 int flags; /* inode logging flags */
2824 xfs_bmbt_irec_t got; /* current extent entry */
2825 xfs_fileoff_t got_endoff; /* first offset past got */
2826 int i; /* temp state */
2827 xfs_ifork_t *ifp; /* inode fork pointer */
2828 xfs_mount_t *mp; /* mount structure */
2829 xfs_filblks_t nblks; /* quota/sb block count */
2830 xfs_bmbt_irec_t new; /* new record to be inserted */
2831 /* REFERENCED */
2832 uint qfield; /* quota field to update */
2833 xfs_filblks_t temp; /* for indirect length calculations */
2834 xfs_filblks_t temp2; /* for indirect length calculations */
2835 int state = 0;
2837 XFS_STATS_INC(xs_del_exlist);
2839 if (whichfork == XFS_ATTR_FORK)
2840 state |= BMAP_ATTRFORK;
2842 mp = ip->i_mount;
2843 ifp = XFS_IFORK_PTR(ip, whichfork);
2844 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
2845 (uint)sizeof(xfs_bmbt_rec_t)));
2846 ASSERT(del->br_blockcount > 0);
2847 ep = xfs_iext_get_ext(ifp, idx);
2848 xfs_bmbt_get_all(ep, &got);
2849 ASSERT(got.br_startoff <= del->br_startoff);
2850 del_endoff = del->br_startoff + del->br_blockcount;
2851 got_endoff = got.br_startoff + got.br_blockcount;
2852 ASSERT(got_endoff >= del_endoff);
2853 delay = isnullstartblock(got.br_startblock);
2854 ASSERT(isnullstartblock(del->br_startblock) == delay);
2855 flags = 0;
2856 qfield = 0;
2857 error = 0;
2859 * If deleting a real allocation, must free up the disk space.
2861 if (!delay) {
2862 flags = XFS_ILOG_CORE;
2864 * Realtime allocation. Free it and record di_nblocks update.
2866 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
2867 xfs_fsblock_t bno;
2868 xfs_filblks_t len;
2870 ASSERT(do_mod(del->br_blockcount,
2871 mp->m_sb.sb_rextsize) == 0);
2872 ASSERT(do_mod(del->br_startblock,
2873 mp->m_sb.sb_rextsize) == 0);
2874 bno = del->br_startblock;
2875 len = del->br_blockcount;
2876 do_div(bno, mp->m_sb.sb_rextsize);
2877 do_div(len, mp->m_sb.sb_rextsize);
2878 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
2879 (xfs_extlen_t)len)))
2880 goto done;
2881 do_fx = 0;
2882 nblks = len * mp->m_sb.sb_rextsize;
2883 qfield = XFS_TRANS_DQ_RTBCOUNT;
2886 * Ordinary allocation.
2888 else {
2889 do_fx = 1;
2890 nblks = del->br_blockcount;
2891 qfield = XFS_TRANS_DQ_BCOUNT;
2894 * Set up del_endblock and cur for later.
2896 del_endblock = del->br_startblock + del->br_blockcount;
2897 if (cur) {
2898 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2899 got.br_startblock, got.br_blockcount,
2900 &i)))
2901 goto done;
2902 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2904 da_old = da_new = 0;
2905 } else {
2906 da_old = startblockval(got.br_startblock);
2907 da_new = 0;
2908 nblks = 0;
2909 do_fx = 0;
2912 * Set flag value to use in switch statement.
2913 * Left-contig is 2, right-contig is 1.
2915 switch (((got.br_startoff == del->br_startoff) << 1) |
2916 (got_endoff == del_endoff)) {
2917 case 3:
2919 * Matches the whole extent. Delete the entry.
2921 xfs_iext_remove(ip, idx, 1,
2922 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
2923 ifp->if_lastex = idx;
2924 if (delay)
2925 break;
2926 XFS_IFORK_NEXT_SET(ip, whichfork,
2927 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2928 flags |= XFS_ILOG_CORE;
2929 if (!cur) {
2930 flags |= xfs_ilog_fext(whichfork);
2931 break;
2933 if ((error = xfs_btree_delete(cur, &i)))
2934 goto done;
2935 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2936 break;
2938 case 2:
2940 * Deleting the first part of the extent.
2942 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
2943 xfs_bmbt_set_startoff(ep, del_endoff);
2944 temp = got.br_blockcount - del->br_blockcount;
2945 xfs_bmbt_set_blockcount(ep, temp);
2946 ifp->if_lastex = idx;
2947 if (delay) {
2948 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2949 da_old);
2950 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2951 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2952 da_new = temp;
2953 break;
2955 xfs_bmbt_set_startblock(ep, del_endblock);
2956 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2957 if (!cur) {
2958 flags |= xfs_ilog_fext(whichfork);
2959 break;
2961 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2962 got.br_blockcount - del->br_blockcount,
2963 got.br_state)))
2964 goto done;
2965 break;
2967 case 1:
2969 * Deleting the last part of the extent.
2971 temp = got.br_blockcount - del->br_blockcount;
2972 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
2973 xfs_bmbt_set_blockcount(ep, temp);
2974 ifp->if_lastex = idx;
2975 if (delay) {
2976 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2977 da_old);
2978 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2979 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2980 da_new = temp;
2981 break;
2983 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2984 if (!cur) {
2985 flags |= xfs_ilog_fext(whichfork);
2986 break;
2988 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2989 got.br_startblock,
2990 got.br_blockcount - del->br_blockcount,
2991 got.br_state)))
2992 goto done;
2993 break;
2995 case 0:
2997 * Deleting the middle of the extent.
2999 temp = del->br_startoff - got.br_startoff;
3000 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
3001 xfs_bmbt_set_blockcount(ep, temp);
3002 new.br_startoff = del_endoff;
3003 temp2 = got_endoff - del_endoff;
3004 new.br_blockcount = temp2;
3005 new.br_state = got.br_state;
3006 if (!delay) {
3007 new.br_startblock = del_endblock;
3008 flags |= XFS_ILOG_CORE;
3009 if (cur) {
3010 if ((error = xfs_bmbt_update(cur,
3011 got.br_startoff,
3012 got.br_startblock, temp,
3013 got.br_state)))
3014 goto done;
3015 if ((error = xfs_btree_increment(cur, 0, &i)))
3016 goto done;
3017 cur->bc_rec.b = new;
3018 error = xfs_btree_insert(cur, &i);
3019 if (error && error != ENOSPC)
3020 goto done;
3022 * If get no-space back from btree insert,
3023 * it tried a split, and we have a zero
3024 * block reservation.
3025 * Fix up our state and return the error.
3027 if (error == ENOSPC) {
3029 * Reset the cursor, don't trust
3030 * it after any insert operation.
3032 if ((error = xfs_bmbt_lookup_eq(cur,
3033 got.br_startoff,
3034 got.br_startblock,
3035 temp, &i)))
3036 goto done;
3037 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3039 * Update the btree record back
3040 * to the original value.
3042 if ((error = xfs_bmbt_update(cur,
3043 got.br_startoff,
3044 got.br_startblock,
3045 got.br_blockcount,
3046 got.br_state)))
3047 goto done;
3049 * Reset the extent record back
3050 * to the original value.
3052 xfs_bmbt_set_blockcount(ep,
3053 got.br_blockcount);
3054 flags = 0;
3055 error = XFS_ERROR(ENOSPC);
3056 goto done;
3058 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3059 } else
3060 flags |= xfs_ilog_fext(whichfork);
3061 XFS_IFORK_NEXT_SET(ip, whichfork,
3062 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3063 } else {
3064 ASSERT(whichfork == XFS_DATA_FORK);
3065 temp = xfs_bmap_worst_indlen(ip, temp);
3066 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
3067 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3068 new.br_startblock = nullstartblock((int)temp2);
3069 da_new = temp + temp2;
3070 while (da_new > da_old) {
3071 if (temp) {
3072 temp--;
3073 da_new--;
3074 xfs_bmbt_set_startblock(ep,
3075 nullstartblock((int)temp));
3077 if (da_new == da_old)
3078 break;
3079 if (temp2) {
3080 temp2--;
3081 da_new--;
3082 new.br_startblock =
3083 nullstartblock((int)temp2);
3087 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3088 xfs_iext_insert(ip, idx + 1, 1, &new, state);
3089 ifp->if_lastex = idx + 1;
3090 break;
3093 * If we need to, add to list of extents to delete.
3095 if (do_fx)
3096 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3097 mp);
3099 * Adjust inode # blocks in the file.
3101 if (nblks)
3102 ip->i_d.di_nblocks -= nblks;
3104 * Adjust quota data.
3106 if (qfield)
3107 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
3110 * Account for change in delayed indirect blocks.
3111 * Nothing to do for disk quota accounting here.
3113 ASSERT(da_old >= da_new);
3114 if (da_old > da_new)
3115 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
3116 rsvd);
3117 done:
3118 *logflagsp = flags;
3119 return error;
3123 * Remove the entry "free" from the free item list. Prev points to the
3124 * previous entry, unless "free" is the head of the list.
3126 STATIC void
3127 xfs_bmap_del_free(
3128 xfs_bmap_free_t *flist, /* free item list header */
3129 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3130 xfs_bmap_free_item_t *free) /* list item to be freed */
3132 if (prev)
3133 prev->xbfi_next = free->xbfi_next;
3134 else
3135 flist->xbf_first = free->xbfi_next;
3136 flist->xbf_count--;
3137 kmem_zone_free(xfs_bmap_free_item_zone, free);
3141 * Convert an extents-format file into a btree-format file.
3142 * The new file will have a root block (in the inode) and a single child block.
3144 STATIC int /* error */
3145 xfs_bmap_extents_to_btree(
3146 xfs_trans_t *tp, /* transaction pointer */
3147 xfs_inode_t *ip, /* incore inode pointer */
3148 xfs_fsblock_t *firstblock, /* first-block-allocated */
3149 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3150 xfs_btree_cur_t **curp, /* cursor returned to caller */
3151 int wasdel, /* converting a delayed alloc */
3152 int *logflagsp, /* inode logging flags */
3153 int whichfork) /* data or attr fork */
3155 struct xfs_btree_block *ablock; /* allocated (child) bt block */
3156 xfs_buf_t *abp; /* buffer for ablock */
3157 xfs_alloc_arg_t args; /* allocation arguments */
3158 xfs_bmbt_rec_t *arp; /* child record pointer */
3159 struct xfs_btree_block *block; /* btree root block */
3160 xfs_btree_cur_t *cur; /* bmap btree cursor */
3161 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3162 int error; /* error return value */
3163 xfs_extnum_t i, cnt; /* extent record index */
3164 xfs_ifork_t *ifp; /* inode fork pointer */
3165 xfs_bmbt_key_t *kp; /* root block key pointer */
3166 xfs_mount_t *mp; /* mount structure */
3167 xfs_extnum_t nextents; /* number of file extents */
3168 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3170 ifp = XFS_IFORK_PTR(ip, whichfork);
3171 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3172 ASSERT(ifp->if_ext_max ==
3173 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3175 * Make space in the inode incore.
3177 xfs_iroot_realloc(ip, 1, whichfork);
3178 ifp->if_flags |= XFS_IFBROOT;
3181 * Fill in the root.
3183 block = ifp->if_broot;
3184 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3185 block->bb_level = cpu_to_be16(1);
3186 block->bb_numrecs = cpu_to_be16(1);
3187 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3188 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3191 * Need a cursor. Can't allocate until bb_level is filled in.
3193 mp = ip->i_mount;
3194 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
3195 cur->bc_private.b.firstblock = *firstblock;
3196 cur->bc_private.b.flist = flist;
3197 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3199 * Convert to a btree with two levels, one record in root.
3201 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3202 args.tp = tp;
3203 args.mp = mp;
3204 args.firstblock = *firstblock;
3205 if (*firstblock == NULLFSBLOCK) {
3206 args.type = XFS_ALLOCTYPE_START_BNO;
3207 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3208 } else if (flist->xbf_low) {
3209 args.type = XFS_ALLOCTYPE_START_BNO;
3210 args.fsbno = *firstblock;
3211 } else {
3212 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3213 args.fsbno = *firstblock;
3215 args.minlen = args.maxlen = args.prod = 1;
3216 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3217 args.minalignslop = 0;
3218 args.wasdel = wasdel;
3219 *logflagsp = 0;
3220 if ((error = xfs_alloc_vextent(&args))) {
3221 xfs_iroot_realloc(ip, -1, whichfork);
3222 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3223 return error;
3226 * Allocation can't fail, the space was reserved.
3228 ASSERT(args.fsbno != NULLFSBLOCK);
3229 ASSERT(*firstblock == NULLFSBLOCK ||
3230 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3231 (flist->xbf_low &&
3232 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3233 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3234 cur->bc_private.b.allocated++;
3235 ip->i_d.di_nblocks++;
3236 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3237 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3239 * Fill in the child block.
3241 ablock = XFS_BUF_TO_BLOCK(abp);
3242 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3243 ablock->bb_level = 0;
3244 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3245 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3246 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3247 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3248 for (cnt = i = 0; i < nextents; i++) {
3249 ep = xfs_iext_get_ext(ifp, i);
3250 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
3251 arp->l0 = cpu_to_be64(ep->l0);
3252 arp->l1 = cpu_to_be64(ep->l1);
3253 arp++; cnt++;
3256 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3257 xfs_btree_set_numrecs(ablock, cnt);
3260 * Fill in the root key and pointer.
3262 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3263 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3264 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3265 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3266 be16_to_cpu(block->bb_level)));
3267 *pp = cpu_to_be64(args.fsbno);
3270 * Do all this logging at the end so that
3271 * the root is at the right level.
3273 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3274 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3275 ASSERT(*curp == NULL);
3276 *curp = cur;
3277 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
3278 return 0;
3282 * Calculate the default attribute fork offset for newly created inodes.
3284 uint
3285 xfs_default_attroffset(
3286 struct xfs_inode *ip)
3288 struct xfs_mount *mp = ip->i_mount;
3289 uint offset;
3291 if (mp->m_sb.sb_inodesize == 256) {
3292 offset = XFS_LITINO(mp) -
3293 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3294 } else {
3295 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3298 ASSERT(offset < XFS_LITINO(mp));
3299 return offset;
3303 * Helper routine to reset inode di_forkoff field when switching
3304 * attribute fork from local to extent format - we reset it where
3305 * possible to make space available for inline data fork extents.
3307 STATIC void
3308 xfs_bmap_forkoff_reset(
3309 xfs_mount_t *mp,
3310 xfs_inode_t *ip,
3311 int whichfork)
3313 if (whichfork == XFS_ATTR_FORK &&
3314 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3315 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3316 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3317 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3319 if (dfl_forkoff > ip->i_d.di_forkoff) {
3320 ip->i_d.di_forkoff = dfl_forkoff;
3321 ip->i_df.if_ext_max =
3322 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3323 ip->i_afp->if_ext_max =
3324 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3330 * Convert a local file to an extents file.
3331 * This code is out of bounds for data forks of regular files,
3332 * since the file data needs to get logged so things will stay consistent.
3333 * (The bmap-level manipulations are ok, though).
3335 STATIC int /* error */
3336 xfs_bmap_local_to_extents(
3337 xfs_trans_t *tp, /* transaction pointer */
3338 xfs_inode_t *ip, /* incore inode pointer */
3339 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3340 xfs_extlen_t total, /* total blocks needed by transaction */
3341 int *logflagsp, /* inode logging flags */
3342 int whichfork) /* data or attr fork */
3344 int error; /* error return value */
3345 int flags; /* logging flags returned */
3346 xfs_ifork_t *ifp; /* inode fork pointer */
3349 * We don't want to deal with the case of keeping inode data inline yet.
3350 * So sending the data fork of a regular inode is invalid.
3352 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3353 whichfork == XFS_DATA_FORK));
3354 ifp = XFS_IFORK_PTR(ip, whichfork);
3355 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3356 flags = 0;
3357 error = 0;
3358 if (ifp->if_bytes) {
3359 xfs_alloc_arg_t args; /* allocation arguments */
3360 xfs_buf_t *bp; /* buffer for extent block */
3361 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
3363 args.tp = tp;
3364 args.mp = ip->i_mount;
3365 args.firstblock = *firstblock;
3366 ASSERT((ifp->if_flags &
3367 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3369 * Allocate a block. We know we need only one, since the
3370 * file currently fits in an inode.
3372 if (*firstblock == NULLFSBLOCK) {
3373 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3374 args.type = XFS_ALLOCTYPE_START_BNO;
3375 } else {
3376 args.fsbno = *firstblock;
3377 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3379 args.total = total;
3380 args.mod = args.minleft = args.alignment = args.wasdel =
3381 args.isfl = args.minalignslop = 0;
3382 args.minlen = args.maxlen = args.prod = 1;
3383 if ((error = xfs_alloc_vextent(&args)))
3384 goto done;
3386 * Can't fail, the space was reserved.
3388 ASSERT(args.fsbno != NULLFSBLOCK);
3389 ASSERT(args.len == 1);
3390 *firstblock = args.fsbno;
3391 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3392 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3393 ifp->if_bytes);
3394 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3395 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3396 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3397 xfs_iext_add(ifp, 0, 1);
3398 ep = xfs_iext_get_ext(ifp, 0);
3399 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3400 trace_xfs_bmap_post_update(ip, 0,
3401 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3402 _THIS_IP_);
3403 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3404 ip->i_d.di_nblocks = 1;
3405 xfs_trans_mod_dquot_byino(tp, ip,
3406 XFS_TRANS_DQ_BCOUNT, 1L);
3407 flags |= xfs_ilog_fext(whichfork);
3408 } else {
3409 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3410 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3412 ifp->if_flags &= ~XFS_IFINLINE;
3413 ifp->if_flags |= XFS_IFEXTENTS;
3414 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3415 flags |= XFS_ILOG_CORE;
3416 done:
3417 *logflagsp = flags;
3418 return error;
3422 * Search the extent records for the entry containing block bno.
3423 * If bno lies in a hole, point to the next entry. If bno lies
3424 * past eof, *eofp will be set, and *prevp will contain the last
3425 * entry (null if none). Else, *lastxp will be set to the index
3426 * of the found entry; *gotp will contain the entry.
3428 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3429 xfs_bmap_search_multi_extents(
3430 xfs_ifork_t *ifp, /* inode fork pointer */
3431 xfs_fileoff_t bno, /* block number searched for */
3432 int *eofp, /* out: end of file found */
3433 xfs_extnum_t *lastxp, /* out: last extent index */
3434 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3435 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3437 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3438 xfs_extnum_t lastx; /* last extent index */
3441 * Initialize the extent entry structure to catch access to
3442 * uninitialized br_startblock field.
3444 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3445 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3446 gotp->br_state = XFS_EXT_INVALID;
3447 #if XFS_BIG_BLKNOS
3448 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3449 #else
3450 gotp->br_startblock = 0xffffa5a5;
3451 #endif
3452 prevp->br_startoff = NULLFILEOFF;
3454 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3455 if (lastx > 0) {
3456 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3458 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3459 xfs_bmbt_get_all(ep, gotp);
3460 *eofp = 0;
3461 } else {
3462 if (lastx > 0) {
3463 *gotp = *prevp;
3465 *eofp = 1;
3466 ep = NULL;
3468 *lastxp = lastx;
3469 return ep;
3473 * Search the extents list for the inode, for the extent containing bno.
3474 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3475 * *eofp will be set, and *prevp will contain the last entry (null if none).
3476 * Else, *lastxp will be set to the index of the found
3477 * entry; *gotp will contain the entry.
3479 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3480 xfs_bmap_search_extents(
3481 xfs_inode_t *ip, /* incore inode pointer */
3482 xfs_fileoff_t bno, /* block number searched for */
3483 int fork, /* data or attr fork */
3484 int *eofp, /* out: end of file found */
3485 xfs_extnum_t *lastxp, /* out: last extent index */
3486 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3487 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3489 xfs_ifork_t *ifp; /* inode fork pointer */
3490 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3492 XFS_STATS_INC(xs_look_exlist);
3493 ifp = XFS_IFORK_PTR(ip, fork);
3495 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3497 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3498 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3499 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3500 "Access to block zero in inode %llu "
3501 "start_block: %llx start_off: %llx "
3502 "blkcnt: %llx extent-state: %x lastx: %x\n",
3503 (unsigned long long)ip->i_ino,
3504 (unsigned long long)gotp->br_startblock,
3505 (unsigned long long)gotp->br_startoff,
3506 (unsigned long long)gotp->br_blockcount,
3507 gotp->br_state, *lastxp);
3508 *lastxp = NULLEXTNUM;
3509 *eofp = 1;
3510 return NULL;
3512 return ep;
3516 * Compute the worst-case number of indirect blocks that will be used
3517 * for ip's delayed extent of length "len".
3519 STATIC xfs_filblks_t
3520 xfs_bmap_worst_indlen(
3521 xfs_inode_t *ip, /* incore inode pointer */
3522 xfs_filblks_t len) /* delayed extent length */
3524 int level; /* btree level number */
3525 int maxrecs; /* maximum record count at this level */
3526 xfs_mount_t *mp; /* mount structure */
3527 xfs_filblks_t rval; /* return value */
3529 mp = ip->i_mount;
3530 maxrecs = mp->m_bmap_dmxr[0];
3531 for (level = 0, rval = 0;
3532 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3533 level++) {
3534 len += maxrecs - 1;
3535 do_div(len, maxrecs);
3536 rval += len;
3537 if (len == 1)
3538 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3539 level - 1;
3540 if (level == 0)
3541 maxrecs = mp->m_bmap_dmxr[1];
3543 return rval;
3547 * Convert inode from non-attributed to attributed.
3548 * Must not be in a transaction, ip must not be locked.
3550 int /* error code */
3551 xfs_bmap_add_attrfork(
3552 xfs_inode_t *ip, /* incore inode pointer */
3553 int size, /* space new attribute needs */
3554 int rsvd) /* xact may use reserved blks */
3556 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3557 xfs_bmap_free_t flist; /* freed extent records */
3558 xfs_mount_t *mp; /* mount structure */
3559 xfs_trans_t *tp; /* transaction pointer */
3560 int blks; /* space reservation */
3561 int version = 1; /* superblock attr version */
3562 int committed; /* xaction was committed */
3563 int logflags; /* logging flags */
3564 int error; /* error return value */
3566 ASSERT(XFS_IFORK_Q(ip) == 0);
3567 ASSERT(ip->i_df.if_ext_max ==
3568 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3570 mp = ip->i_mount;
3571 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3572 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3573 blks = XFS_ADDAFORK_SPACE_RES(mp);
3574 if (rsvd)
3575 tp->t_flags |= XFS_TRANS_RESERVE;
3576 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3577 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3578 goto error0;
3579 xfs_ilock(ip, XFS_ILOCK_EXCL);
3580 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
3581 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3582 XFS_QMOPT_RES_REGBLKS);
3583 if (error) {
3584 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3585 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3586 return error;
3588 if (XFS_IFORK_Q(ip))
3589 goto error1;
3590 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3592 * For inodes coming from pre-6.2 filesystems.
3594 ASSERT(ip->i_d.di_aformat == 0);
3595 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3597 ASSERT(ip->i_d.di_anextents == 0);
3599 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
3600 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3602 switch (ip->i_d.di_format) {
3603 case XFS_DINODE_FMT_DEV:
3604 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3605 break;
3606 case XFS_DINODE_FMT_UUID:
3607 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3608 break;
3609 case XFS_DINODE_FMT_LOCAL:
3610 case XFS_DINODE_FMT_EXTENTS:
3611 case XFS_DINODE_FMT_BTREE:
3612 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3613 if (!ip->i_d.di_forkoff)
3614 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
3615 else if (mp->m_flags & XFS_MOUNT_ATTR2)
3616 version = 2;
3617 break;
3618 default:
3619 ASSERT(0);
3620 error = XFS_ERROR(EINVAL);
3621 goto error1;
3623 ip->i_df.if_ext_max =
3624 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3625 ASSERT(ip->i_afp == NULL);
3626 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3627 ip->i_afp->if_ext_max =
3628 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3629 ip->i_afp->if_flags = XFS_IFEXTENTS;
3630 logflags = 0;
3631 xfs_bmap_init(&flist, &firstblock);
3632 switch (ip->i_d.di_format) {
3633 case XFS_DINODE_FMT_LOCAL:
3634 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3635 &logflags);
3636 break;
3637 case XFS_DINODE_FMT_EXTENTS:
3638 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3639 &flist, &logflags);
3640 break;
3641 case XFS_DINODE_FMT_BTREE:
3642 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3643 &logflags);
3644 break;
3645 default:
3646 error = 0;
3647 break;
3649 if (logflags)
3650 xfs_trans_log_inode(tp, ip, logflags);
3651 if (error)
3652 goto error2;
3653 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3654 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
3655 __int64_t sbfields = 0;
3657 spin_lock(&mp->m_sb_lock);
3658 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3659 xfs_sb_version_addattr(&mp->m_sb);
3660 sbfields |= XFS_SB_VERSIONNUM;
3662 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3663 xfs_sb_version_addattr2(&mp->m_sb);
3664 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
3666 if (sbfields) {
3667 spin_unlock(&mp->m_sb_lock);
3668 xfs_mod_sb(tp, sbfields);
3669 } else
3670 spin_unlock(&mp->m_sb_lock);
3672 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
3673 goto error2;
3674 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3675 ASSERT(ip->i_df.if_ext_max ==
3676 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3677 return error;
3678 error2:
3679 xfs_bmap_cancel(&flist);
3680 error1:
3681 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3682 error0:
3683 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3684 ASSERT(ip->i_df.if_ext_max ==
3685 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3686 return error;
3690 * Add the extent to the list of extents to be free at transaction end.
3691 * The list is maintained sorted (by block number).
3693 /* ARGSUSED */
3694 void
3695 xfs_bmap_add_free(
3696 xfs_fsblock_t bno, /* fs block number of extent */
3697 xfs_filblks_t len, /* length of extent */
3698 xfs_bmap_free_t *flist, /* list of extents */
3699 xfs_mount_t *mp) /* mount point structure */
3701 xfs_bmap_free_item_t *cur; /* current (next) element */
3702 xfs_bmap_free_item_t *new; /* new element */
3703 xfs_bmap_free_item_t *prev; /* previous element */
3704 #ifdef DEBUG
3705 xfs_agnumber_t agno;
3706 xfs_agblock_t agbno;
3708 ASSERT(bno != NULLFSBLOCK);
3709 ASSERT(len > 0);
3710 ASSERT(len <= MAXEXTLEN);
3711 ASSERT(!isnullstartblock(bno));
3712 agno = XFS_FSB_TO_AGNO(mp, bno);
3713 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3714 ASSERT(agno < mp->m_sb.sb_agcount);
3715 ASSERT(agbno < mp->m_sb.sb_agblocks);
3716 ASSERT(len < mp->m_sb.sb_agblocks);
3717 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3718 #endif
3719 ASSERT(xfs_bmap_free_item_zone != NULL);
3720 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3721 new->xbfi_startblock = bno;
3722 new->xbfi_blockcount = (xfs_extlen_t)len;
3723 for (prev = NULL, cur = flist->xbf_first;
3724 cur != NULL;
3725 prev = cur, cur = cur->xbfi_next) {
3726 if (cur->xbfi_startblock >= bno)
3727 break;
3729 if (prev)
3730 prev->xbfi_next = new;
3731 else
3732 flist->xbf_first = new;
3733 new->xbfi_next = cur;
3734 flist->xbf_count++;
3738 * Compute and fill in the value of the maximum depth of a bmap btree
3739 * in this filesystem. Done once, during mount.
3741 void
3742 xfs_bmap_compute_maxlevels(
3743 xfs_mount_t *mp, /* file system mount structure */
3744 int whichfork) /* data or attr fork */
3746 int level; /* btree level */
3747 uint maxblocks; /* max blocks at this level */
3748 uint maxleafents; /* max leaf entries possible */
3749 int maxrootrecs; /* max records in root block */
3750 int minleafrecs; /* min records in leaf block */
3751 int minnoderecs; /* min records in node block */
3752 int sz; /* root block size */
3755 * The maximum number of extents in a file, hence the maximum
3756 * number of leaf entries, is controlled by the type of di_nextents
3757 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3758 * (a signed 16-bit number, xfs_aextnum_t).
3760 * Note that we can no longer assume that if we are in ATTR1 that
3761 * the fork offset of all the inodes will be
3762 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3763 * with ATTR2 and then mounted back with ATTR1, keeping the
3764 * di_forkoff's fixed but probably at various positions. Therefore,
3765 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3766 * of a minimum size available.
3768 if (whichfork == XFS_DATA_FORK) {
3769 maxleafents = MAXEXTNUM;
3770 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
3771 } else {
3772 maxleafents = MAXAEXTNUM;
3773 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
3775 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
3776 minleafrecs = mp->m_bmap_dmnr[0];
3777 minnoderecs = mp->m_bmap_dmnr[1];
3778 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3779 for (level = 1; maxblocks > 1; level++) {
3780 if (maxblocks <= maxrootrecs)
3781 maxblocks = 1;
3782 else
3783 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3785 mp->m_bm_maxlevels[whichfork] = level;
3789 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3790 * caller. Frees all the extents that need freeing, which must be done
3791 * last due to locking considerations. We never free any extents in
3792 * the first transaction. This is to allow the caller to make the first
3793 * transaction a synchronous one so that the pointers to the data being
3794 * broken in this transaction will be permanent before the data is actually
3795 * freed. This is necessary to prevent blocks from being reallocated
3796 * and written to before the free and reallocation are actually permanent.
3797 * We do not just make the first transaction synchronous here, because
3798 * there are more efficient ways to gain the same protection in some cases
3799 * (see the file truncation code).
3801 * Return 1 if the given transaction was committed and a new one
3802 * started, and 0 otherwise in the committed parameter.
3804 /*ARGSUSED*/
3805 int /* error */
3806 xfs_bmap_finish(
3807 xfs_trans_t **tp, /* transaction pointer addr */
3808 xfs_bmap_free_t *flist, /* i/o: list extents to free */
3809 int *committed) /* xact committed or not */
3811 xfs_efd_log_item_t *efd; /* extent free data */
3812 xfs_efi_log_item_t *efi; /* extent free intention */
3813 int error; /* error return value */
3814 xfs_bmap_free_item_t *free; /* free extent item */
3815 unsigned int logres; /* new log reservation */
3816 unsigned int logcount; /* new log count */
3817 xfs_mount_t *mp; /* filesystem mount structure */
3818 xfs_bmap_free_item_t *next; /* next item on free list */
3819 xfs_trans_t *ntp; /* new transaction pointer */
3821 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3822 if (flist->xbf_count == 0) {
3823 *committed = 0;
3824 return 0;
3826 ntp = *tp;
3827 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3828 for (free = flist->xbf_first; free; free = free->xbfi_next)
3829 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3830 free->xbfi_blockcount);
3831 logres = ntp->t_log_res;
3832 logcount = ntp->t_log_count;
3833 ntp = xfs_trans_dup(*tp);
3834 error = xfs_trans_commit(*tp, 0);
3835 *tp = ntp;
3836 *committed = 1;
3838 * We have a new transaction, so we should return committed=1,
3839 * even though we're returning an error.
3841 if (error)
3842 return error;
3845 * transaction commit worked ok so we can drop the extra ticket
3846 * reference that we gained in xfs_trans_dup()
3848 xfs_log_ticket_put(ntp->t_ticket);
3850 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3851 logcount)))
3852 return error;
3853 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3854 for (free = flist->xbf_first; free != NULL; free = next) {
3855 next = free->xbfi_next;
3856 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3857 free->xbfi_blockcount))) {
3859 * The bmap free list will be cleaned up at a
3860 * higher level. The EFI will be canceled when
3861 * this transaction is aborted.
3862 * Need to force shutdown here to make sure it
3863 * happens, since this transaction may not be
3864 * dirty yet.
3866 mp = ntp->t_mountp;
3867 if (!XFS_FORCED_SHUTDOWN(mp))
3868 xfs_force_shutdown(mp,
3869 (error == EFSCORRUPTED) ?
3870 SHUTDOWN_CORRUPT_INCORE :
3871 SHUTDOWN_META_IO_ERROR);
3872 return error;
3874 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3875 free->xbfi_blockcount);
3876 xfs_bmap_del_free(flist, NULL, free);
3878 return 0;
3882 * Free up any items left in the list.
3884 void
3885 xfs_bmap_cancel(
3886 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3888 xfs_bmap_free_item_t *free; /* free list item */
3889 xfs_bmap_free_item_t *next;
3891 if (flist->xbf_count == 0)
3892 return;
3893 ASSERT(flist->xbf_first != NULL);
3894 for (free = flist->xbf_first; free; free = next) {
3895 next = free->xbfi_next;
3896 xfs_bmap_del_free(flist, NULL, free);
3898 ASSERT(flist->xbf_count == 0);
3902 * Returns the file-relative block number of the first unused block(s)
3903 * in the file with at least "len" logically contiguous blocks free.
3904 * This is the lowest-address hole if the file has holes, else the first block
3905 * past the end of file.
3906 * Return 0 if the file is currently local (in-inode).
3908 int /* error */
3909 xfs_bmap_first_unused(
3910 xfs_trans_t *tp, /* transaction pointer */
3911 xfs_inode_t *ip, /* incore inode */
3912 xfs_extlen_t len, /* size of hole to find */
3913 xfs_fileoff_t *first_unused, /* unused block */
3914 int whichfork) /* data or attr fork */
3916 int error; /* error return value */
3917 int idx; /* extent record index */
3918 xfs_ifork_t *ifp; /* inode fork pointer */
3919 xfs_fileoff_t lastaddr; /* last block number seen */
3920 xfs_fileoff_t lowest; /* lowest useful block */
3921 xfs_fileoff_t max; /* starting useful block */
3922 xfs_fileoff_t off; /* offset for this block */
3923 xfs_extnum_t nextents; /* number of extent entries */
3925 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3926 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3927 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3928 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3929 *first_unused = 0;
3930 return 0;
3932 ifp = XFS_IFORK_PTR(ip, whichfork);
3933 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3934 (error = xfs_iread_extents(tp, ip, whichfork)))
3935 return error;
3936 lowest = *first_unused;
3937 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3938 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
3939 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
3940 off = xfs_bmbt_get_startoff(ep);
3942 * See if the hole before this extent will work.
3944 if (off >= lowest + len && off - max >= len) {
3945 *first_unused = max;
3946 return 0;
3948 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3949 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3951 *first_unused = max;
3952 return 0;
3956 * Returns the file-relative block number of the last block + 1 before
3957 * last_block (input value) in the file.
3958 * This is not based on i_size, it is based on the extent records.
3959 * Returns 0 for local files, as they do not have extent records.
3961 int /* error */
3962 xfs_bmap_last_before(
3963 xfs_trans_t *tp, /* transaction pointer */
3964 xfs_inode_t *ip, /* incore inode */
3965 xfs_fileoff_t *last_block, /* last block */
3966 int whichfork) /* data or attr fork */
3968 xfs_fileoff_t bno; /* input file offset */
3969 int eof; /* hit end of file */
3970 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
3971 int error; /* error return value */
3972 xfs_bmbt_irec_t got; /* current extent value */
3973 xfs_ifork_t *ifp; /* inode fork pointer */
3974 xfs_extnum_t lastx; /* last extent used */
3975 xfs_bmbt_irec_t prev; /* previous extent value */
3977 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3978 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3979 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3980 return XFS_ERROR(EIO);
3981 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3982 *last_block = 0;
3983 return 0;
3985 ifp = XFS_IFORK_PTR(ip, whichfork);
3986 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3987 (error = xfs_iread_extents(tp, ip, whichfork)))
3988 return error;
3989 bno = *last_block - 1;
3990 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3991 &prev);
3992 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3993 if (prev.br_startoff == NULLFILEOFF)
3994 *last_block = 0;
3995 else
3996 *last_block = prev.br_startoff + prev.br_blockcount;
3999 * Otherwise *last_block is already the right answer.
4001 return 0;
4005 * Returns the file-relative block number of the first block past eof in
4006 * the file. This is not based on i_size, it is based on the extent records.
4007 * Returns 0 for local files, as they do not have extent records.
4009 int /* error */
4010 xfs_bmap_last_offset(
4011 xfs_trans_t *tp, /* transaction pointer */
4012 xfs_inode_t *ip, /* incore inode */
4013 xfs_fileoff_t *last_block, /* last block */
4014 int whichfork) /* data or attr fork */
4016 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
4017 int error; /* error return value */
4018 xfs_ifork_t *ifp; /* inode fork pointer */
4019 xfs_extnum_t nextents; /* number of extent entries */
4021 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4022 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4023 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4024 return XFS_ERROR(EIO);
4025 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4026 *last_block = 0;
4027 return 0;
4029 ifp = XFS_IFORK_PTR(ip, whichfork);
4030 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4031 (error = xfs_iread_extents(tp, ip, whichfork)))
4032 return error;
4033 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4034 if (!nextents) {
4035 *last_block = 0;
4036 return 0;
4038 ep = xfs_iext_get_ext(ifp, nextents - 1);
4039 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4040 return 0;
4044 * Returns whether the selected fork of the inode has exactly one
4045 * block or not. For the data fork we check this matches di_size,
4046 * implying the file's range is 0..bsize-1.
4048 int /* 1=>1 block, 0=>otherwise */
4049 xfs_bmap_one_block(
4050 xfs_inode_t *ip, /* incore inode */
4051 int whichfork) /* data or attr fork */
4053 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
4054 xfs_ifork_t *ifp; /* inode fork pointer */
4055 int rval; /* return value */
4056 xfs_bmbt_irec_t s; /* internal version of extent */
4058 #ifndef DEBUG
4059 if (whichfork == XFS_DATA_FORK) {
4060 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4061 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4062 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4064 #endif /* !DEBUG */
4065 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4066 return 0;
4067 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4068 return 0;
4069 ifp = XFS_IFORK_PTR(ip, whichfork);
4070 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4071 ep = xfs_iext_get_ext(ifp, 0);
4072 xfs_bmbt_get_all(ep, &s);
4073 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4074 if (rval && whichfork == XFS_DATA_FORK)
4075 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
4076 return rval;
4079 STATIC int
4080 xfs_bmap_sanity_check(
4081 struct xfs_mount *mp,
4082 struct xfs_buf *bp,
4083 int level)
4085 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4087 if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
4088 be16_to_cpu(block->bb_level) != level ||
4089 be16_to_cpu(block->bb_numrecs) == 0 ||
4090 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4091 return 0;
4092 return 1;
4096 * Read in the extents to if_extents.
4097 * All inode fields are set up by caller, we just traverse the btree
4098 * and copy the records in. If the file system cannot contain unwritten
4099 * extents, the records are checked for no "state" flags.
4101 int /* error */
4102 xfs_bmap_read_extents(
4103 xfs_trans_t *tp, /* transaction pointer */
4104 xfs_inode_t *ip, /* incore inode */
4105 int whichfork) /* data or attr fork */
4107 struct xfs_btree_block *block; /* current btree block */
4108 xfs_fsblock_t bno; /* block # of "block" */
4109 xfs_buf_t *bp; /* buffer for "block" */
4110 int error; /* error return value */
4111 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4112 xfs_extnum_t i, j; /* index into the extents list */
4113 xfs_ifork_t *ifp; /* fork structure */
4114 int level; /* btree level, for checking */
4115 xfs_mount_t *mp; /* file system mount structure */
4116 __be64 *pp; /* pointer to block address */
4117 /* REFERENCED */
4118 xfs_extnum_t room; /* number of entries there's room for */
4120 bno = NULLFSBLOCK;
4121 mp = ip->i_mount;
4122 ifp = XFS_IFORK_PTR(ip, whichfork);
4123 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4124 XFS_EXTFMT_INODE(ip);
4125 block = ifp->if_broot;
4127 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4129 level = be16_to_cpu(block->bb_level);
4130 ASSERT(level > 0);
4131 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
4132 bno = be64_to_cpu(*pp);
4133 ASSERT(bno != NULLDFSBNO);
4134 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4135 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4137 * Go down the tree until leaf level is reached, following the first
4138 * pointer (leftmost) at each level.
4140 while (level-- > 0) {
4141 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4142 XFS_BMAP_BTREE_REF)))
4143 return error;
4144 block = XFS_BUF_TO_BLOCK(bp);
4145 XFS_WANT_CORRUPTED_GOTO(
4146 xfs_bmap_sanity_check(mp, bp, level),
4147 error0);
4148 if (level == 0)
4149 break;
4150 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
4151 bno = be64_to_cpu(*pp);
4152 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4153 xfs_trans_brelse(tp, bp);
4156 * Here with bp and block set to the leftmost leaf node in the tree.
4158 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4159 i = 0;
4161 * Loop over all leaf nodes. Copy information to the extent records.
4163 for (;;) {
4164 xfs_bmbt_rec_t *frp;
4165 xfs_fsblock_t nextbno;
4166 xfs_extnum_t num_recs;
4167 xfs_extnum_t start;
4170 num_recs = xfs_btree_get_numrecs(block);
4171 if (unlikely(i + num_recs > room)) {
4172 ASSERT(i + num_recs <= room);
4173 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4174 "corrupt dinode %Lu, (btree extents).",
4175 (unsigned long long) ip->i_ino);
4176 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4177 XFS_ERRLEVEL_LOW,
4178 ip->i_mount);
4179 goto error0;
4181 XFS_WANT_CORRUPTED_GOTO(
4182 xfs_bmap_sanity_check(mp, bp, 0),
4183 error0);
4185 * Read-ahead the next leaf block, if any.
4187 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
4188 if (nextbno != NULLFSBLOCK)
4189 xfs_btree_reada_bufl(mp, nextbno, 1);
4191 * Copy records into the extent records.
4193 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
4194 start = i;
4195 for (j = 0; j < num_recs; j++, i++, frp++) {
4196 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
4197 trp->l0 = be64_to_cpu(frp->l0);
4198 trp->l1 = be64_to_cpu(frp->l1);
4200 if (exntf == XFS_EXTFMT_NOSTATE) {
4202 * Check all attribute bmap btree records and
4203 * any "older" data bmap btree records for a
4204 * set bit in the "extent flag" position.
4206 if (unlikely(xfs_check_nostate_extents(ifp,
4207 start, num_recs))) {
4208 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4209 XFS_ERRLEVEL_LOW,
4210 ip->i_mount);
4211 goto error0;
4214 xfs_trans_brelse(tp, bp);
4215 bno = nextbno;
4217 * If we've reached the end, stop.
4219 if (bno == NULLFSBLOCK)
4220 break;
4221 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4222 XFS_BMAP_BTREE_REF)))
4223 return error;
4224 block = XFS_BUF_TO_BLOCK(bp);
4226 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4227 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4228 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
4229 return 0;
4230 error0:
4231 xfs_trans_brelse(tp, bp);
4232 return XFS_ERROR(EFSCORRUPTED);
4235 #ifdef DEBUG
4237 * Add bmap trace insert entries for all the contents of the extent records.
4239 void
4240 xfs_bmap_trace_exlist(
4241 xfs_inode_t *ip, /* incore inode pointer */
4242 xfs_extnum_t cnt, /* count of entries in the list */
4243 int whichfork, /* data or attr fork */
4244 unsigned long caller_ip)
4246 xfs_extnum_t idx; /* extent record index */
4247 xfs_ifork_t *ifp; /* inode fork pointer */
4248 int state = 0;
4250 if (whichfork == XFS_ATTR_FORK)
4251 state |= BMAP_ATTRFORK;
4253 ifp = XFS_IFORK_PTR(ip, whichfork);
4254 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4255 for (idx = 0; idx < cnt; idx++)
4256 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
4260 * Validate that the bmbt_irecs being returned from bmapi are valid
4261 * given the callers original parameters. Specifically check the
4262 * ranges of the returned irecs to ensure that they only extent beyond
4263 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4265 STATIC void
4266 xfs_bmap_validate_ret(
4267 xfs_fileoff_t bno,
4268 xfs_filblks_t len,
4269 int flags,
4270 xfs_bmbt_irec_t *mval,
4271 int nmap,
4272 int ret_nmap)
4274 int i; /* index to map values */
4276 ASSERT(ret_nmap <= nmap);
4278 for (i = 0; i < ret_nmap; i++) {
4279 ASSERT(mval[i].br_blockcount > 0);
4280 if (!(flags & XFS_BMAPI_ENTIRE)) {
4281 ASSERT(mval[i].br_startoff >= bno);
4282 ASSERT(mval[i].br_blockcount <= len);
4283 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4284 bno + len);
4285 } else {
4286 ASSERT(mval[i].br_startoff < bno + len);
4287 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4288 bno);
4290 ASSERT(i == 0 ||
4291 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4292 mval[i].br_startoff);
4293 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4294 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4295 mval[i].br_startblock != HOLESTARTBLOCK);
4296 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4297 mval[i].br_state == XFS_EXT_UNWRITTEN);
4300 #endif /* DEBUG */
4304 * Map file blocks to filesystem blocks.
4305 * File range is given by the bno/len pair.
4306 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4307 * into a hole or past eof.
4308 * Only allocates blocks from a single allocation group,
4309 * to avoid locking problems.
4310 * The returned value in "firstblock" from the first call in a transaction
4311 * must be remembered and presented to subsequent calls in "firstblock".
4312 * An upper bound for the number of blocks to be allocated is supplied to
4313 * the first call in "total"; if no allocation group has that many free
4314 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4316 int /* error */
4317 xfs_bmapi(
4318 xfs_trans_t *tp, /* transaction pointer */
4319 xfs_inode_t *ip, /* incore inode */
4320 xfs_fileoff_t bno, /* starting file offs. mapped */
4321 xfs_filblks_t len, /* length to map in file */
4322 int flags, /* XFS_BMAPI_... */
4323 xfs_fsblock_t *firstblock, /* first allocated block
4324 controls a.g. for allocs */
4325 xfs_extlen_t total, /* total blocks needed */
4326 xfs_bmbt_irec_t *mval, /* output: map values */
4327 int *nmap, /* i/o: mval size/count */
4328 xfs_bmap_free_t *flist) /* i/o: list extents to free */
4330 xfs_fsblock_t abno; /* allocated block number */
4331 xfs_extlen_t alen; /* allocated extent length */
4332 xfs_fileoff_t aoff; /* allocated file offset */
4333 xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */
4334 xfs_btree_cur_t *cur; /* bmap btree cursor */
4335 xfs_fileoff_t end; /* end of mapped file region */
4336 int eof; /* we've hit the end of extents */
4337 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
4338 int error; /* error return */
4339 xfs_bmbt_irec_t got; /* current file extent record */
4340 xfs_ifork_t *ifp; /* inode fork pointer */
4341 xfs_extlen_t indlen; /* indirect blocks length */
4342 xfs_extnum_t lastx; /* last useful extent number */
4343 int logflags; /* flags for transaction logging */
4344 xfs_extlen_t minleft; /* min blocks left after allocation */
4345 xfs_extlen_t minlen; /* min allocation size */
4346 xfs_mount_t *mp; /* xfs mount structure */
4347 int n; /* current extent index */
4348 int nallocs; /* number of extents alloc'd */
4349 xfs_extnum_t nextents; /* number of extents in file */
4350 xfs_fileoff_t obno; /* old block number (offset) */
4351 xfs_bmbt_irec_t prev; /* previous file extent record */
4352 int tmp_logflags; /* temp flags holder */
4353 int whichfork; /* data or attr fork */
4354 char inhole; /* current location is hole in file */
4355 char wasdelay; /* old extent was delayed */
4356 char wr; /* this is a write request */
4357 char rt; /* this is a realtime file */
4358 #ifdef DEBUG
4359 xfs_fileoff_t orig_bno; /* original block number value */
4360 int orig_flags; /* original flags arg value */
4361 xfs_filblks_t orig_len; /* original value of len arg */
4362 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4363 int orig_nmap; /* original value of *nmap */
4365 orig_bno = bno;
4366 orig_len = len;
4367 orig_flags = flags;
4368 orig_mval = mval;
4369 orig_nmap = *nmap;
4370 #endif
4371 ASSERT(*nmap >= 1);
4372 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4373 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4374 XFS_ATTR_FORK : XFS_DATA_FORK;
4375 mp = ip->i_mount;
4376 if (unlikely(XFS_TEST_ERROR(
4377 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4378 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4379 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4380 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4381 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4382 return XFS_ERROR(EFSCORRUPTED);
4384 if (XFS_FORCED_SHUTDOWN(mp))
4385 return XFS_ERROR(EIO);
4386 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4387 ifp = XFS_IFORK_PTR(ip, whichfork);
4388 ASSERT(ifp->if_ext_max ==
4389 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4390 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4391 XFS_STATS_INC(xs_blk_mapw);
4392 else
4393 XFS_STATS_INC(xs_blk_mapr);
4395 * IGSTATE flag is used to combine extents which
4396 * differ only due to the state of the extents.
4397 * This technique is used from xfs_getbmap()
4398 * when the caller does not wish to see the
4399 * separation (which is the default).
4401 * This technique is also used when writing a
4402 * buffer which has been partially written,
4403 * (usually by being flushed during a chunkread),
4404 * to ensure one write takes place. This also
4405 * prevents a change in the xfs inode extents at
4406 * this time, intentionally. This change occurs
4407 * on completion of the write operation, in
4408 * xfs_strat_comp(), where the xfs_bmapi() call
4409 * is transactioned, and the extents combined.
4411 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4412 wr = 0; /* no allocations are allowed */
4413 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
4414 logflags = 0;
4415 nallocs = 0;
4416 cur = NULL;
4417 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4418 ASSERT(wr && tp);
4419 if ((error = xfs_bmap_local_to_extents(tp, ip,
4420 firstblock, total, &logflags, whichfork)))
4421 goto error0;
4423 if (wr && *firstblock == NULLFSBLOCK) {
4424 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4425 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4426 else
4427 minleft = 1;
4428 } else
4429 minleft = 0;
4430 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4431 (error = xfs_iread_extents(tp, ip, whichfork)))
4432 goto error0;
4433 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4434 &prev);
4435 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4436 n = 0;
4437 end = bno + len;
4438 obno = bno;
4439 bma.ip = NULL;
4441 while (bno < end && n < *nmap) {
4443 * Reading past eof, act as though there's a hole
4444 * up to end.
4446 if (eof && !wr)
4447 got.br_startoff = end;
4448 inhole = eof || got.br_startoff > bno;
4449 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
4450 isnullstartblock(got.br_startblock);
4452 * First, deal with the hole before the allocated space
4453 * that we found, if any.
4455 if (wr && (inhole || wasdelay)) {
4457 * For the wasdelay case, we could also just
4458 * allocate the stuff asked for in this bmap call
4459 * but that wouldn't be as good.
4461 if (wasdelay) {
4462 alen = (xfs_extlen_t)got.br_blockcount;
4463 aoff = got.br_startoff;
4464 if (lastx != NULLEXTNUM && lastx) {
4465 ep = xfs_iext_get_ext(ifp, lastx - 1);
4466 xfs_bmbt_get_all(ep, &prev);
4468 } else {
4469 alen = (xfs_extlen_t)
4470 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4471 if (!eof)
4472 alen = (xfs_extlen_t)
4473 XFS_FILBLKS_MIN(alen,
4474 got.br_startoff - bno);
4475 aoff = bno;
4477 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4478 if (flags & XFS_BMAPI_DELAY) {
4479 xfs_extlen_t extsz;
4481 /* Figure out the extent size, adjust alen */
4482 extsz = xfs_get_extsz_hint(ip);
4483 if (extsz) {
4484 error = xfs_bmap_extsize_align(mp,
4485 &got, &prev, extsz,
4486 rt, eof,
4487 flags&XFS_BMAPI_DELAY,
4488 flags&XFS_BMAPI_CONVERT,
4489 &aoff, &alen);
4490 ASSERT(!error);
4493 if (rt)
4494 extsz = alen / mp->m_sb.sb_rextsize;
4497 * Make a transaction-less quota reservation for
4498 * delayed allocation blocks. This number gets
4499 * adjusted later. We return if we haven't
4500 * allocated blocks already inside this loop.
4502 error = xfs_trans_reserve_quota_nblks(
4503 NULL, ip, (long)alen, 0,
4504 rt ? XFS_QMOPT_RES_RTBLKS :
4505 XFS_QMOPT_RES_REGBLKS);
4506 if (error) {
4507 if (n == 0) {
4508 *nmap = 0;
4509 ASSERT(cur == NULL);
4510 return error;
4512 break;
4516 * Split changing sb for alen and indlen since
4517 * they could be coming from different places.
4519 indlen = (xfs_extlen_t)
4520 xfs_bmap_worst_indlen(ip, alen);
4521 ASSERT(indlen > 0);
4523 if (rt) {
4524 error = xfs_mod_incore_sb(mp,
4525 XFS_SBS_FREXTENTS,
4526 -((int64_t)extsz), (flags &
4527 XFS_BMAPI_RSVBLOCKS));
4528 } else {
4529 error = xfs_mod_incore_sb(mp,
4530 XFS_SBS_FDBLOCKS,
4531 -((int64_t)alen), (flags &
4532 XFS_BMAPI_RSVBLOCKS));
4534 if (!error) {
4535 error = xfs_mod_incore_sb(mp,
4536 XFS_SBS_FDBLOCKS,
4537 -((int64_t)indlen), (flags &
4538 XFS_BMAPI_RSVBLOCKS));
4539 if (error && rt)
4540 xfs_mod_incore_sb(mp,
4541 XFS_SBS_FREXTENTS,
4542 (int64_t)extsz, (flags &
4543 XFS_BMAPI_RSVBLOCKS));
4544 else if (error)
4545 xfs_mod_incore_sb(mp,
4546 XFS_SBS_FDBLOCKS,
4547 (int64_t)alen, (flags &
4548 XFS_BMAPI_RSVBLOCKS));
4551 if (error) {
4552 if (XFS_IS_QUOTA_ON(mp))
4553 /* unreserve the blocks now */
4554 (void)
4555 xfs_trans_unreserve_quota_nblks(
4556 NULL, ip,
4557 (long)alen, 0, rt ?
4558 XFS_QMOPT_RES_RTBLKS :
4559 XFS_QMOPT_RES_REGBLKS);
4560 break;
4563 ip->i_delayed_blks += alen;
4564 abno = nullstartblock(indlen);
4565 } else {
4567 * If first time, allocate and fill in
4568 * once-only bma fields.
4570 if (bma.ip == NULL) {
4571 bma.tp = tp;
4572 bma.ip = ip;
4573 bma.prevp = &prev;
4574 bma.gotp = &got;
4575 bma.total = total;
4576 bma.userdata = 0;
4578 /* Indicate if this is the first user data
4579 * in the file, or just any user data.
4581 if (!(flags & XFS_BMAPI_METADATA)) {
4582 bma.userdata = (aoff == 0) ?
4583 XFS_ALLOC_INITIAL_USER_DATA :
4584 XFS_ALLOC_USERDATA;
4587 * Fill in changeable bma fields.
4589 bma.eof = eof;
4590 bma.firstblock = *firstblock;
4591 bma.alen = alen;
4592 bma.off = aoff;
4593 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4594 bma.wasdel = wasdelay;
4595 bma.minlen = minlen;
4596 bma.low = flist->xbf_low;
4597 bma.minleft = minleft;
4599 * Only want to do the alignment at the
4600 * eof if it is userdata and allocation length
4601 * is larger than a stripe unit.
4603 if (mp->m_dalign && alen >= mp->m_dalign &&
4604 (!(flags & XFS_BMAPI_METADATA)) &&
4605 (whichfork == XFS_DATA_FORK)) {
4606 if ((error = xfs_bmap_isaeof(ip, aoff,
4607 whichfork, &bma.aeof)))
4608 goto error0;
4609 } else
4610 bma.aeof = 0;
4612 * Call allocator.
4614 if ((error = xfs_bmap_alloc(&bma)))
4615 goto error0;
4617 * Copy out result fields.
4619 abno = bma.rval;
4620 if ((flist->xbf_low = bma.low))
4621 minleft = 0;
4622 alen = bma.alen;
4623 aoff = bma.off;
4624 ASSERT(*firstblock == NULLFSBLOCK ||
4625 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4626 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4627 (flist->xbf_low &&
4628 XFS_FSB_TO_AGNO(mp, *firstblock) <
4629 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4630 *firstblock = bma.firstblock;
4631 if (cur)
4632 cur->bc_private.b.firstblock =
4633 *firstblock;
4634 if (abno == NULLFSBLOCK)
4635 break;
4636 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4637 cur = xfs_bmbt_init_cursor(mp, tp,
4638 ip, whichfork);
4639 cur->bc_private.b.firstblock =
4640 *firstblock;
4641 cur->bc_private.b.flist = flist;
4644 * Bump the number of extents we've allocated
4645 * in this call.
4647 nallocs++;
4649 if (cur)
4650 cur->bc_private.b.flags =
4651 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4652 got.br_startoff = aoff;
4653 got.br_startblock = abno;
4654 got.br_blockcount = alen;
4655 got.br_state = XFS_EXT_NORM; /* assume normal */
4657 * Determine state of extent, and the filesystem.
4658 * A wasdelay extent has been initialized, so
4659 * shouldn't be flagged as unwritten.
4661 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
4662 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
4663 got.br_state = XFS_EXT_UNWRITTEN;
4665 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
4666 firstblock, flist, &tmp_logflags,
4667 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
4668 logflags |= tmp_logflags;
4669 if (error)
4670 goto error0;
4671 lastx = ifp->if_lastex;
4672 ep = xfs_iext_get_ext(ifp, lastx);
4673 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4674 xfs_bmbt_get_all(ep, &got);
4675 ASSERT(got.br_startoff <= aoff);
4676 ASSERT(got.br_startoff + got.br_blockcount >=
4677 aoff + alen);
4678 #ifdef DEBUG
4679 if (flags & XFS_BMAPI_DELAY) {
4680 ASSERT(isnullstartblock(got.br_startblock));
4681 ASSERT(startblockval(got.br_startblock) > 0);
4683 ASSERT(got.br_state == XFS_EXT_NORM ||
4684 got.br_state == XFS_EXT_UNWRITTEN);
4685 #endif
4687 * Fall down into the found allocated space case.
4689 } else if (inhole) {
4691 * Reading in a hole.
4693 mval->br_startoff = bno;
4694 mval->br_startblock = HOLESTARTBLOCK;
4695 mval->br_blockcount =
4696 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4697 mval->br_state = XFS_EXT_NORM;
4698 bno += mval->br_blockcount;
4699 len -= mval->br_blockcount;
4700 mval++;
4701 n++;
4702 continue;
4705 * Then deal with the allocated space we found.
4707 ASSERT(ep != NULL);
4708 if (!(flags & XFS_BMAPI_ENTIRE) &&
4709 (got.br_startoff + got.br_blockcount > obno)) {
4710 if (obno > bno)
4711 bno = obno;
4712 ASSERT((bno >= obno) || (n == 0));
4713 ASSERT(bno < end);
4714 mval->br_startoff = bno;
4715 if (isnullstartblock(got.br_startblock)) {
4716 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
4717 mval->br_startblock = DELAYSTARTBLOCK;
4718 } else
4719 mval->br_startblock =
4720 got.br_startblock +
4721 (bno - got.br_startoff);
4723 * Return the minimum of what we got and what we
4724 * asked for for the length. We can use the len
4725 * variable here because it is modified below
4726 * and we could have been there before coming
4727 * here if the first part of the allocation
4728 * didn't overlap what was asked for.
4730 mval->br_blockcount =
4731 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
4732 (bno - got.br_startoff));
4733 mval->br_state = got.br_state;
4734 ASSERT(mval->br_blockcount <= len);
4735 } else {
4736 *mval = got;
4737 if (isnullstartblock(mval->br_startblock)) {
4738 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
4739 mval->br_startblock = DELAYSTARTBLOCK;
4744 * Check if writing previously allocated but
4745 * unwritten extents.
4747 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
4748 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
4750 * Modify (by adding) the state flag, if writing.
4752 ASSERT(mval->br_blockcount <= len);
4753 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4754 cur = xfs_bmbt_init_cursor(mp,
4755 tp, ip, whichfork);
4756 cur->bc_private.b.firstblock =
4757 *firstblock;
4758 cur->bc_private.b.flist = flist;
4760 mval->br_state = XFS_EXT_NORM;
4761 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
4762 firstblock, flist, &tmp_logflags,
4763 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
4764 logflags |= tmp_logflags;
4765 if (error)
4766 goto error0;
4767 lastx = ifp->if_lastex;
4768 ep = xfs_iext_get_ext(ifp, lastx);
4769 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4770 xfs_bmbt_get_all(ep, &got);
4772 * We may have combined previously unwritten
4773 * space with written space, so generate
4774 * another request.
4776 if (mval->br_blockcount < len)
4777 continue;
4780 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4781 ((mval->br_startoff + mval->br_blockcount) <= end));
4782 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4783 (mval->br_blockcount <= len) ||
4784 (mval->br_startoff < obno));
4785 bno = mval->br_startoff + mval->br_blockcount;
4786 len = end - bno;
4787 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4788 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4789 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4790 ASSERT(mval->br_state == mval[-1].br_state);
4791 mval[-1].br_blockcount = mval->br_blockcount;
4792 mval[-1].br_state = mval->br_state;
4793 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4794 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4795 mval[-1].br_startblock != HOLESTARTBLOCK &&
4796 mval->br_startblock ==
4797 mval[-1].br_startblock + mval[-1].br_blockcount &&
4798 ((flags & XFS_BMAPI_IGSTATE) ||
4799 mval[-1].br_state == mval->br_state)) {
4800 ASSERT(mval->br_startoff ==
4801 mval[-1].br_startoff + mval[-1].br_blockcount);
4802 mval[-1].br_blockcount += mval->br_blockcount;
4803 } else if (n > 0 &&
4804 mval->br_startblock == DELAYSTARTBLOCK &&
4805 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4806 mval->br_startoff ==
4807 mval[-1].br_startoff + mval[-1].br_blockcount) {
4808 mval[-1].br_blockcount += mval->br_blockcount;
4809 mval[-1].br_state = mval->br_state;
4810 } else if (!((n == 0) &&
4811 ((mval->br_startoff + mval->br_blockcount) <=
4812 obno))) {
4813 mval++;
4814 n++;
4817 * If we're done, stop now. Stop when we've allocated
4818 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4819 * the transaction may get too big.
4821 if (bno >= end || n >= *nmap || nallocs >= *nmap)
4822 break;
4824 * Else go on to the next record.
4826 ep = xfs_iext_get_ext(ifp, ++lastx);
4827 prev = got;
4828 if (lastx >= nextents)
4829 eof = 1;
4830 else
4831 xfs_bmbt_get_all(ep, &got);
4833 ifp->if_lastex = lastx;
4834 *nmap = n;
4836 * Transform from btree to extents, give it cur.
4838 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4839 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4840 ASSERT(wr && cur);
4841 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4842 &tmp_logflags, whichfork);
4843 logflags |= tmp_logflags;
4844 if (error)
4845 goto error0;
4847 ASSERT(ifp->if_ext_max ==
4848 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4849 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4850 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
4851 error = 0;
4852 error0:
4854 * Log everything. Do this after conversion, there's no point in
4855 * logging the extent records if we've converted to btree format.
4857 if ((logflags & xfs_ilog_fext(whichfork)) &&
4858 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4859 logflags &= ~xfs_ilog_fext(whichfork);
4860 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
4861 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4862 logflags &= ~xfs_ilog_fbroot(whichfork);
4864 * Log whatever the flags say, even if error. Otherwise we might miss
4865 * detecting a case where the data is changed, there's an error,
4866 * and it's not logged so we don't shutdown when we should.
4868 if (logflags) {
4869 ASSERT(tp && wr);
4870 xfs_trans_log_inode(tp, ip, logflags);
4872 if (cur) {
4873 if (!error) {
4874 ASSERT(*firstblock == NULLFSBLOCK ||
4875 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4876 XFS_FSB_TO_AGNO(mp,
4877 cur->bc_private.b.firstblock) ||
4878 (flist->xbf_low &&
4879 XFS_FSB_TO_AGNO(mp, *firstblock) <
4880 XFS_FSB_TO_AGNO(mp,
4881 cur->bc_private.b.firstblock)));
4882 *firstblock = cur->bc_private.b.firstblock;
4884 xfs_btree_del_cursor(cur,
4885 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4887 if (!error)
4888 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4889 orig_nmap, *nmap);
4890 return error;
4894 * Map file blocks to filesystem blocks, simple version.
4895 * One block (extent) only, read-only.
4896 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
4897 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
4898 * was set and all the others were clear.
4900 int /* error */
4901 xfs_bmapi_single(
4902 xfs_trans_t *tp, /* transaction pointer */
4903 xfs_inode_t *ip, /* incore inode */
4904 int whichfork, /* data or attr fork */
4905 xfs_fsblock_t *fsb, /* output: mapped block */
4906 xfs_fileoff_t bno) /* starting file offs. mapped */
4908 int eof; /* we've hit the end of extents */
4909 int error; /* error return */
4910 xfs_bmbt_irec_t got; /* current file extent record */
4911 xfs_ifork_t *ifp; /* inode fork pointer */
4912 xfs_extnum_t lastx; /* last useful extent number */
4913 xfs_bmbt_irec_t prev; /* previous file extent record */
4915 ifp = XFS_IFORK_PTR(ip, whichfork);
4916 if (unlikely(
4917 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4918 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
4919 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
4920 ip->i_mount);
4921 return XFS_ERROR(EFSCORRUPTED);
4923 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
4924 return XFS_ERROR(EIO);
4925 XFS_STATS_INC(xs_blk_mapr);
4926 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4927 (error = xfs_iread_extents(tp, ip, whichfork)))
4928 return error;
4929 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4930 &prev);
4932 * Reading past eof, act as though there's a hole
4933 * up to end.
4935 if (eof || got.br_startoff > bno) {
4936 *fsb = NULLFSBLOCK;
4937 return 0;
4939 ASSERT(!isnullstartblock(got.br_startblock));
4940 ASSERT(bno < got.br_startoff + got.br_blockcount);
4941 *fsb = got.br_startblock + (bno - got.br_startoff);
4942 ifp->if_lastex = lastx;
4943 return 0;
4947 * Unmap (remove) blocks from a file.
4948 * If nexts is nonzero then the number of extents to remove is limited to
4949 * that value. If not all extents in the block range can be removed then
4950 * *done is set.
4952 int /* error */
4953 xfs_bunmapi(
4954 xfs_trans_t *tp, /* transaction pointer */
4955 struct xfs_inode *ip, /* incore inode */
4956 xfs_fileoff_t bno, /* starting offset to unmap */
4957 xfs_filblks_t len, /* length to unmap in file */
4958 int flags, /* misc flags */
4959 xfs_extnum_t nexts, /* number of extents max */
4960 xfs_fsblock_t *firstblock, /* first allocated block
4961 controls a.g. for allocs */
4962 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4963 int *done) /* set if not done yet */
4965 xfs_btree_cur_t *cur; /* bmap btree cursor */
4966 xfs_bmbt_irec_t del; /* extent being deleted */
4967 int eof; /* is deleting at eof */
4968 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
4969 int error; /* error return value */
4970 xfs_extnum_t extno; /* extent number in list */
4971 xfs_bmbt_irec_t got; /* current extent record */
4972 xfs_ifork_t *ifp; /* inode fork pointer */
4973 int isrt; /* freeing in rt area */
4974 xfs_extnum_t lastx; /* last extent index used */
4975 int logflags; /* transaction logging flags */
4976 xfs_extlen_t mod; /* rt extent offset */
4977 xfs_mount_t *mp; /* mount structure */
4978 xfs_extnum_t nextents; /* number of file extents */
4979 xfs_bmbt_irec_t prev; /* previous extent record */
4980 xfs_fileoff_t start; /* first file offset deleted */
4981 int tmp_logflags; /* partial logging flags */
4982 int wasdel; /* was a delayed alloc extent */
4983 int whichfork; /* data or attribute fork */
4984 int rsvd; /* OK to allocate reserved blocks */
4985 xfs_fsblock_t sum;
4987 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
4989 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4990 XFS_ATTR_FORK : XFS_DATA_FORK;
4991 ifp = XFS_IFORK_PTR(ip, whichfork);
4992 if (unlikely(
4993 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4994 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
4995 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
4996 ip->i_mount);
4997 return XFS_ERROR(EFSCORRUPTED);
4999 mp = ip->i_mount;
5000 if (XFS_FORCED_SHUTDOWN(mp))
5001 return XFS_ERROR(EIO);
5002 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5003 ASSERT(len > 0);
5004 ASSERT(nexts >= 0);
5005 ASSERT(ifp->if_ext_max ==
5006 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5007 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5008 (error = xfs_iread_extents(tp, ip, whichfork)))
5009 return error;
5010 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5011 if (nextents == 0) {
5012 *done = 1;
5013 return 0;
5015 XFS_STATS_INC(xs_blk_unmap);
5016 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5017 start = bno;
5018 bno = start + len - 1;
5019 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5020 &prev);
5023 * Check to see if the given block number is past the end of the
5024 * file, back up to the last block if so...
5026 if (eof) {
5027 ep = xfs_iext_get_ext(ifp, --lastx);
5028 xfs_bmbt_get_all(ep, &got);
5029 bno = got.br_startoff + got.br_blockcount - 1;
5031 logflags = 0;
5032 if (ifp->if_flags & XFS_IFBROOT) {
5033 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5034 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5035 cur->bc_private.b.firstblock = *firstblock;
5036 cur->bc_private.b.flist = flist;
5037 cur->bc_private.b.flags = 0;
5038 } else
5039 cur = NULL;
5040 extno = 0;
5041 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5042 (nexts == 0 || extno < nexts)) {
5044 * Is the found extent after a hole in which bno lives?
5045 * Just back up to the previous extent, if so.
5047 if (got.br_startoff > bno) {
5048 if (--lastx < 0)
5049 break;
5050 ep = xfs_iext_get_ext(ifp, lastx);
5051 xfs_bmbt_get_all(ep, &got);
5054 * Is the last block of this extent before the range
5055 * we're supposed to delete? If so, we're done.
5057 bno = XFS_FILEOFF_MIN(bno,
5058 got.br_startoff + got.br_blockcount - 1);
5059 if (bno < start)
5060 break;
5062 * Then deal with the (possibly delayed) allocated space
5063 * we found.
5065 ASSERT(ep != NULL);
5066 del = got;
5067 wasdel = isnullstartblock(del.br_startblock);
5068 if (got.br_startoff < start) {
5069 del.br_startoff = start;
5070 del.br_blockcount -= start - got.br_startoff;
5071 if (!wasdel)
5072 del.br_startblock += start - got.br_startoff;
5074 if (del.br_startoff + del.br_blockcount > bno + 1)
5075 del.br_blockcount = bno + 1 - del.br_startoff;
5076 sum = del.br_startblock + del.br_blockcount;
5077 if (isrt &&
5078 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5080 * Realtime extent not lined up at the end.
5081 * The extent could have been split into written
5082 * and unwritten pieces, or we could just be
5083 * unmapping part of it. But we can't really
5084 * get rid of part of a realtime extent.
5086 if (del.br_state == XFS_EXT_UNWRITTEN ||
5087 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5089 * This piece is unwritten, or we're not
5090 * using unwritten extents. Skip over it.
5092 ASSERT(bno >= mod);
5093 bno -= mod > del.br_blockcount ?
5094 del.br_blockcount : mod;
5095 if (bno < got.br_startoff) {
5096 if (--lastx >= 0)
5097 xfs_bmbt_get_all(xfs_iext_get_ext(
5098 ifp, lastx), &got);
5100 continue;
5103 * It's written, turn it unwritten.
5104 * This is better than zeroing it.
5106 ASSERT(del.br_state == XFS_EXT_NORM);
5107 ASSERT(xfs_trans_get_block_res(tp) > 0);
5109 * If this spans a realtime extent boundary,
5110 * chop it back to the start of the one we end at.
5112 if (del.br_blockcount > mod) {
5113 del.br_startoff += del.br_blockcount - mod;
5114 del.br_startblock += del.br_blockcount - mod;
5115 del.br_blockcount = mod;
5117 del.br_state = XFS_EXT_UNWRITTEN;
5118 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5119 firstblock, flist, &logflags,
5120 XFS_DATA_FORK, 0);
5121 if (error)
5122 goto error0;
5123 goto nodelete;
5125 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5127 * Realtime extent is lined up at the end but not
5128 * at the front. We'll get rid of full extents if
5129 * we can.
5131 mod = mp->m_sb.sb_rextsize - mod;
5132 if (del.br_blockcount > mod) {
5133 del.br_blockcount -= mod;
5134 del.br_startoff += mod;
5135 del.br_startblock += mod;
5136 } else if ((del.br_startoff == start &&
5137 (del.br_state == XFS_EXT_UNWRITTEN ||
5138 xfs_trans_get_block_res(tp) == 0)) ||
5139 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5141 * Can't make it unwritten. There isn't
5142 * a full extent here so just skip it.
5144 ASSERT(bno >= del.br_blockcount);
5145 bno -= del.br_blockcount;
5146 if (bno < got.br_startoff) {
5147 if (--lastx >= 0)
5148 xfs_bmbt_get_all(--ep, &got);
5150 continue;
5151 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5153 * This one is already unwritten.
5154 * It must have a written left neighbor.
5155 * Unwrite the killed part of that one and
5156 * try again.
5158 ASSERT(lastx > 0);
5159 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5160 lastx - 1), &prev);
5161 ASSERT(prev.br_state == XFS_EXT_NORM);
5162 ASSERT(!isnullstartblock(prev.br_startblock));
5163 ASSERT(del.br_startblock ==
5164 prev.br_startblock + prev.br_blockcount);
5165 if (prev.br_startoff < start) {
5166 mod = start - prev.br_startoff;
5167 prev.br_blockcount -= mod;
5168 prev.br_startblock += mod;
5169 prev.br_startoff = start;
5171 prev.br_state = XFS_EXT_UNWRITTEN;
5172 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5173 &prev, firstblock, flist, &logflags,
5174 XFS_DATA_FORK, 0);
5175 if (error)
5176 goto error0;
5177 goto nodelete;
5178 } else {
5179 ASSERT(del.br_state == XFS_EXT_NORM);
5180 del.br_state = XFS_EXT_UNWRITTEN;
5181 error = xfs_bmap_add_extent(ip, lastx, &cur,
5182 &del, firstblock, flist, &logflags,
5183 XFS_DATA_FORK, 0);
5184 if (error)
5185 goto error0;
5186 goto nodelete;
5189 if (wasdel) {
5190 ASSERT(startblockval(del.br_startblock) > 0);
5191 /* Update realtime/data freespace, unreserve quota */
5192 if (isrt) {
5193 xfs_filblks_t rtexts;
5195 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5196 do_div(rtexts, mp->m_sb.sb_rextsize);
5197 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5198 (int64_t)rtexts, rsvd);
5199 (void)xfs_trans_reserve_quota_nblks(NULL,
5200 ip, -((long)del.br_blockcount), 0,
5201 XFS_QMOPT_RES_RTBLKS);
5202 } else {
5203 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
5204 (int64_t)del.br_blockcount, rsvd);
5205 (void)xfs_trans_reserve_quota_nblks(NULL,
5206 ip, -((long)del.br_blockcount), 0,
5207 XFS_QMOPT_RES_REGBLKS);
5209 ip->i_delayed_blks -= del.br_blockcount;
5210 if (cur)
5211 cur->bc_private.b.flags |=
5212 XFS_BTCUR_BPRV_WASDEL;
5213 } else if (cur)
5214 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5216 * If it's the case where the directory code is running
5217 * with no block reservation, and the deleted block is in
5218 * the middle of its extent, and the resulting insert
5219 * of an extent would cause transformation to btree format,
5220 * then reject it. The calling code will then swap
5221 * blocks around instead.
5222 * We have to do this now, rather than waiting for the
5223 * conversion to btree format, since the transaction
5224 * will be dirty.
5226 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5227 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5228 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5229 del.br_startoff > got.br_startoff &&
5230 del.br_startoff + del.br_blockcount <
5231 got.br_startoff + got.br_blockcount) {
5232 error = XFS_ERROR(ENOSPC);
5233 goto error0;
5235 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5236 &tmp_logflags, whichfork, rsvd);
5237 logflags |= tmp_logflags;
5238 if (error)
5239 goto error0;
5240 bno = del.br_startoff - 1;
5241 nodelete:
5242 lastx = ifp->if_lastex;
5244 * If not done go on to the next (previous) record.
5245 * Reset ep in case the extents array was re-alloced.
5247 ep = xfs_iext_get_ext(ifp, lastx);
5248 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5249 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5250 xfs_bmbt_get_startoff(ep) > bno) {
5251 if (--lastx >= 0)
5252 ep = xfs_iext_get_ext(ifp, lastx);
5254 if (lastx >= 0)
5255 xfs_bmbt_get_all(ep, &got);
5256 extno++;
5259 ifp->if_lastex = lastx;
5260 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5261 ASSERT(ifp->if_ext_max ==
5262 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5264 * Convert to a btree if necessary.
5266 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5267 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5268 ASSERT(cur == NULL);
5269 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5270 &cur, 0, &tmp_logflags, whichfork);
5271 logflags |= tmp_logflags;
5272 if (error)
5273 goto error0;
5276 * transform from btree to extents, give it cur
5278 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5279 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5280 ASSERT(cur != NULL);
5281 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5282 whichfork);
5283 logflags |= tmp_logflags;
5284 if (error)
5285 goto error0;
5288 * transform from extents to local?
5290 ASSERT(ifp->if_ext_max ==
5291 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5292 error = 0;
5293 error0:
5295 * Log everything. Do this after conversion, there's no point in
5296 * logging the extent records if we've converted to btree format.
5298 if ((logflags & xfs_ilog_fext(whichfork)) &&
5299 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5300 logflags &= ~xfs_ilog_fext(whichfork);
5301 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5302 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5303 logflags &= ~xfs_ilog_fbroot(whichfork);
5305 * Log inode even in the error case, if the transaction
5306 * is dirty we'll need to shut down the filesystem.
5308 if (logflags)
5309 xfs_trans_log_inode(tp, ip, logflags);
5310 if (cur) {
5311 if (!error) {
5312 *firstblock = cur->bc_private.b.firstblock;
5313 cur->bc_private.b.allocated = 0;
5315 xfs_btree_del_cursor(cur,
5316 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5318 return error;
5322 * returns 1 for success, 0 if we failed to map the extent.
5324 STATIC int
5325 xfs_getbmapx_fix_eof_hole(
5326 xfs_inode_t *ip, /* xfs incore inode pointer */
5327 struct getbmapx *out, /* output structure */
5328 int prealloced, /* this is a file with
5329 * preallocated data space */
5330 __int64_t end, /* last block requested */
5331 xfs_fsblock_t startblock)
5333 __int64_t fixlen;
5334 xfs_mount_t *mp; /* file system mount point */
5335 xfs_ifork_t *ifp; /* inode fork pointer */
5336 xfs_extnum_t lastx; /* last extent pointer */
5337 xfs_fileoff_t fileblock;
5339 if (startblock == HOLESTARTBLOCK) {
5340 mp = ip->i_mount;
5341 out->bmv_block = -1;
5342 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5343 fixlen -= out->bmv_offset;
5344 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5345 /* Came to hole at EOF. Trim it. */
5346 if (fixlen <= 0)
5347 return 0;
5348 out->bmv_length = fixlen;
5350 } else {
5351 if (startblock == DELAYSTARTBLOCK)
5352 out->bmv_block = -2;
5353 else
5354 out->bmv_block = xfs_fsb_to_db(ip, startblock);
5355 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5356 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5357 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5358 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5359 out->bmv_oflags |= BMV_OF_LAST;
5362 return 1;
5366 * Get inode's extents as described in bmv, and format for output.
5367 * Calls formatter to fill the user's buffer until all extents
5368 * are mapped, until the passed-in bmv->bmv_count slots have
5369 * been filled, or until the formatter short-circuits the loop,
5370 * if it is tracking filled-in extents on its own.
5372 int /* error code */
5373 xfs_getbmap(
5374 xfs_inode_t *ip,
5375 struct getbmapx *bmv, /* user bmap structure */
5376 xfs_bmap_format_t formatter, /* format to user */
5377 void *arg) /* formatter arg */
5379 __int64_t bmvend; /* last block requested */
5380 int error = 0; /* return value */
5381 __int64_t fixlen; /* length for -1 case */
5382 int i; /* extent number */
5383 int lock; /* lock state */
5384 xfs_bmbt_irec_t *map; /* buffer for user's data */
5385 xfs_mount_t *mp; /* file system mount point */
5386 int nex; /* # of user extents can do */
5387 int nexleft; /* # of user extents left */
5388 int subnex; /* # of bmapi's can do */
5389 int nmap; /* number of map entries */
5390 struct getbmapx *out; /* output structure */
5391 int whichfork; /* data or attr fork */
5392 int prealloced; /* this is a file with
5393 * preallocated data space */
5394 int iflags; /* interface flags */
5395 int bmapi_flags; /* flags for xfs_bmapi */
5396 int cur_ext = 0;
5398 mp = ip->i_mount;
5399 iflags = bmv->bmv_iflags;
5400 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5402 if (whichfork == XFS_ATTR_FORK) {
5403 if (XFS_IFORK_Q(ip)) {
5404 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5405 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5406 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5407 return XFS_ERROR(EINVAL);
5408 } else if (unlikely(
5409 ip->i_d.di_aformat != 0 &&
5410 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5411 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5412 ip->i_mount);
5413 return XFS_ERROR(EFSCORRUPTED);
5416 prealloced = 0;
5417 fixlen = 1LL << 32;
5418 } else {
5419 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5420 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5421 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5422 return XFS_ERROR(EINVAL);
5424 if (xfs_get_extsz_hint(ip) ||
5425 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5426 prealloced = 1;
5427 fixlen = XFS_MAXIOFFSET(mp);
5428 } else {
5429 prealloced = 0;
5430 fixlen = ip->i_size;
5434 if (bmv->bmv_length == -1) {
5435 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5436 bmv->bmv_length =
5437 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5438 } else if (bmv->bmv_length == 0) {
5439 bmv->bmv_entries = 0;
5440 return 0;
5441 } else if (bmv->bmv_length < 0) {
5442 return XFS_ERROR(EINVAL);
5445 nex = bmv->bmv_count - 1;
5446 if (nex <= 0)
5447 return XFS_ERROR(EINVAL);
5448 bmvend = bmv->bmv_offset + bmv->bmv_length;
5451 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5452 return XFS_ERROR(ENOMEM);
5453 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5454 if (!out)
5455 return XFS_ERROR(ENOMEM);
5457 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5458 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5459 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5460 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5461 if (error)
5462 goto out_unlock_iolock;
5465 ASSERT(ip->i_delayed_blks == 0);
5468 lock = xfs_ilock_map_shared(ip);
5471 * Don't let nex be bigger than the number of extents
5472 * we can have assuming alternating holes and real extents.
5474 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5475 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5477 bmapi_flags = xfs_bmapi_aflag(whichfork);
5478 if (!(iflags & BMV_IF_PREALLOC))
5479 bmapi_flags |= XFS_BMAPI_IGSTATE;
5482 * Allocate enough space to handle "subnex" maps at a time.
5484 error = ENOMEM;
5485 subnex = 16;
5486 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
5487 if (!map)
5488 goto out_unlock_ilock;
5490 bmv->bmv_entries = 0;
5492 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5493 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5494 error = 0;
5495 goto out_free_map;
5498 nexleft = nex;
5500 do {
5501 nmap = (nexleft > subnex) ? subnex : nexleft;
5502 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5503 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5504 bmapi_flags, NULL, 0, map, &nmap,
5505 NULL);
5506 if (error)
5507 goto out_free_map;
5508 ASSERT(nmap <= subnex);
5510 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5511 out[cur_ext].bmv_oflags = 0;
5512 if (map[i].br_state == XFS_EXT_UNWRITTEN)
5513 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
5514 else if (map[i].br_startblock == DELAYSTARTBLOCK)
5515 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5516 out[cur_ext].bmv_offset =
5517 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5518 out[cur_ext].bmv_length =
5519 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5520 out[cur_ext].bmv_unused1 = 0;
5521 out[cur_ext].bmv_unused2 = 0;
5522 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5523 (map[i].br_startblock != DELAYSTARTBLOCK));
5524 if (map[i].br_startblock == HOLESTARTBLOCK &&
5525 whichfork == XFS_ATTR_FORK) {
5526 /* came to the end of attribute fork */
5527 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
5528 goto out_free_map;
5531 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5532 prealloced, bmvend,
5533 map[i].br_startblock))
5534 goto out_free_map;
5536 bmv->bmv_offset =
5537 out[cur_ext].bmv_offset +
5538 out[cur_ext].bmv_length;
5539 bmv->bmv_length =
5540 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
5543 * In case we don't want to return the hole,
5544 * don't increase cur_ext so that we can reuse
5545 * it in the next loop.
5547 if ((iflags & BMV_IF_NO_HOLES) &&
5548 map[i].br_startblock == HOLESTARTBLOCK) {
5549 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5550 continue;
5553 nexleft--;
5554 bmv->bmv_entries++;
5555 cur_ext++;
5557 } while (nmap && nexleft && bmv->bmv_length);
5559 out_free_map:
5560 kmem_free(map);
5561 out_unlock_ilock:
5562 xfs_iunlock_map_shared(ip, lock);
5563 out_unlock_iolock:
5564 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5566 for (i = 0; i < cur_ext; i++) {
5567 int full = 0; /* user array is full */
5569 /* format results & advance arg */
5570 error = formatter(&arg, &out[i], &full);
5571 if (error || full)
5572 break;
5575 kmem_free(out);
5576 return error;
5580 * Check the last inode extent to determine whether this allocation will result
5581 * in blocks being allocated at the end of the file. When we allocate new data
5582 * blocks at the end of the file which do not start at the previous data block,
5583 * we will try to align the new blocks at stripe unit boundaries.
5585 STATIC int /* error */
5586 xfs_bmap_isaeof(
5587 xfs_inode_t *ip, /* incore inode pointer */
5588 xfs_fileoff_t off, /* file offset in fsblocks */
5589 int whichfork, /* data or attribute fork */
5590 char *aeof) /* return value */
5592 int error; /* error return value */
5593 xfs_ifork_t *ifp; /* inode fork pointer */
5594 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
5595 xfs_extnum_t nextents; /* number of file extents */
5596 xfs_bmbt_irec_t s; /* expanded extent record */
5598 ASSERT(whichfork == XFS_DATA_FORK);
5599 ifp = XFS_IFORK_PTR(ip, whichfork);
5600 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5601 (error = xfs_iread_extents(NULL, ip, whichfork)))
5602 return error;
5603 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5604 if (nextents == 0) {
5605 *aeof = 1;
5606 return 0;
5609 * Go to the last extent
5611 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5612 xfs_bmbt_get_all(lastrec, &s);
5614 * Check we are allocating in the last extent (for delayed allocations)
5615 * or past the last extent for non-delayed allocations.
5617 *aeof = (off >= s.br_startoff &&
5618 off < s.br_startoff + s.br_blockcount &&
5619 isnullstartblock(s.br_startblock)) ||
5620 off >= s.br_startoff + s.br_blockcount;
5621 return 0;
5625 * Check if the endoff is outside the last extent. If so the caller will grow
5626 * the allocation to a stripe unit boundary.
5628 int /* error */
5629 xfs_bmap_eof(
5630 xfs_inode_t *ip, /* incore inode pointer */
5631 xfs_fileoff_t endoff, /* file offset in fsblocks */
5632 int whichfork, /* data or attribute fork */
5633 int *eof) /* result value */
5635 xfs_fsblock_t blockcount; /* extent block count */
5636 int error; /* error return value */
5637 xfs_ifork_t *ifp; /* inode fork pointer */
5638 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
5639 xfs_extnum_t nextents; /* number of file extents */
5640 xfs_fileoff_t startoff; /* extent starting file offset */
5642 ASSERT(whichfork == XFS_DATA_FORK);
5643 ifp = XFS_IFORK_PTR(ip, whichfork);
5644 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5645 (error = xfs_iread_extents(NULL, ip, whichfork)))
5646 return error;
5647 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5648 if (nextents == 0) {
5649 *eof = 1;
5650 return 0;
5653 * Go to the last extent
5655 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5656 startoff = xfs_bmbt_get_startoff(lastrec);
5657 blockcount = xfs_bmbt_get_blockcount(lastrec);
5658 *eof = endoff >= startoff + blockcount;
5659 return 0;
5662 #ifdef DEBUG
5663 STATIC struct xfs_buf *
5664 xfs_bmap_get_bp(
5665 struct xfs_btree_cur *cur,
5666 xfs_fsblock_t bno)
5668 struct xfs_log_item_desc *lidp;
5669 int i;
5671 if (!cur)
5672 return NULL;
5674 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5675 if (!cur->bc_bufs[i])
5676 break;
5677 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5678 return cur->bc_bufs[i];
5681 /* Chase down all the log items to see if the bp is there */
5682 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
5683 struct xfs_buf_log_item *bip;
5684 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5685 if (bip->bli_item.li_type == XFS_LI_BUF &&
5686 XFS_BUF_ADDR(bip->bli_buf) == bno)
5687 return bip->bli_buf;
5690 return NULL;
5693 STATIC void
5694 xfs_check_block(
5695 struct xfs_btree_block *block,
5696 xfs_mount_t *mp,
5697 int root,
5698 short sz)
5700 int i, j, dmxr;
5701 __be64 *pp, *thispa; /* pointer to block address */
5702 xfs_bmbt_key_t *prevp, *keyp;
5704 ASSERT(be16_to_cpu(block->bb_level) > 0);
5706 prevp = NULL;
5707 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
5708 dmxr = mp->m_bmap_dmxr[0];
5709 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
5711 if (prevp) {
5712 ASSERT(be64_to_cpu(prevp->br_startoff) <
5713 be64_to_cpu(keyp->br_startoff));
5715 prevp = keyp;
5718 * Compare the block numbers to see if there are dups.
5720 if (root)
5721 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
5722 else
5723 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5725 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
5726 if (root)
5727 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
5728 else
5729 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
5730 if (*thispa == *pp) {
5731 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
5732 __func__, j, i,
5733 (unsigned long long)be64_to_cpu(*thispa));
5734 panic("%s: ptrs are equal in node\n",
5735 __func__);
5742 * Check that the extents for the inode ip are in the right order in all
5743 * btree leaves.
5746 STATIC void
5747 xfs_bmap_check_leaf_extents(
5748 xfs_btree_cur_t *cur, /* btree cursor or null */
5749 xfs_inode_t *ip, /* incore inode pointer */
5750 int whichfork) /* data or attr fork */
5752 struct xfs_btree_block *block; /* current btree block */
5753 xfs_fsblock_t bno; /* block # of "block" */
5754 xfs_buf_t *bp; /* buffer for "block" */
5755 int error; /* error return value */
5756 xfs_extnum_t i=0, j; /* index into the extents list */
5757 xfs_ifork_t *ifp; /* fork structure */
5758 int level; /* btree level, for checking */
5759 xfs_mount_t *mp; /* file system mount structure */
5760 __be64 *pp; /* pointer to block address */
5761 xfs_bmbt_rec_t *ep; /* pointer to current extent */
5762 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
5763 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
5764 int bp_release = 0;
5766 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5767 return;
5770 bno = NULLFSBLOCK;
5771 mp = ip->i_mount;
5772 ifp = XFS_IFORK_PTR(ip, whichfork);
5773 block = ifp->if_broot;
5775 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5777 level = be16_to_cpu(block->bb_level);
5778 ASSERT(level > 0);
5779 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
5780 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
5781 bno = be64_to_cpu(*pp);
5783 ASSERT(bno != NULLDFSBNO);
5784 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5785 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5788 * Go down the tree until leaf level is reached, following the first
5789 * pointer (leftmost) at each level.
5791 while (level-- > 0) {
5792 /* See if buf is in cur first */
5793 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5794 if (bp) {
5795 bp_release = 0;
5796 } else {
5797 bp_release = 1;
5799 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5800 XFS_BMAP_BTREE_REF)))
5801 goto error_norelse;
5802 block = XFS_BUF_TO_BLOCK(bp);
5803 XFS_WANT_CORRUPTED_GOTO(
5804 xfs_bmap_sanity_check(mp, bp, level),
5805 error0);
5806 if (level == 0)
5807 break;
5810 * Check this block for basic sanity (increasing keys and
5811 * no duplicate blocks).
5814 xfs_check_block(block, mp, 0, 0);
5815 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
5816 bno = be64_to_cpu(*pp);
5817 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
5818 if (bp_release) {
5819 bp_release = 0;
5820 xfs_trans_brelse(NULL, bp);
5825 * Here with bp and block set to the leftmost leaf node in the tree.
5827 i = 0;
5830 * Loop over all leaf nodes checking that all extents are in the right order.
5832 for (;;) {
5833 xfs_fsblock_t nextbno;
5834 xfs_extnum_t num_recs;
5837 num_recs = xfs_btree_get_numrecs(block);
5840 * Read-ahead the next leaf block, if any.
5843 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
5846 * Check all the extents to make sure they are OK.
5847 * If we had a previous block, the last entry should
5848 * conform with the first entry in this one.
5851 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
5852 if (i) {
5853 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
5854 xfs_bmbt_disk_get_blockcount(&last) <=
5855 xfs_bmbt_disk_get_startoff(ep));
5857 for (j = 1; j < num_recs; j++) {
5858 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
5859 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
5860 xfs_bmbt_disk_get_blockcount(ep) <=
5861 xfs_bmbt_disk_get_startoff(nextp));
5862 ep = nextp;
5865 last = *ep;
5866 i += num_recs;
5867 if (bp_release) {
5868 bp_release = 0;
5869 xfs_trans_brelse(NULL, bp);
5871 bno = nextbno;
5873 * If we've reached the end, stop.
5875 if (bno == NULLFSBLOCK)
5876 break;
5878 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5879 if (bp) {
5880 bp_release = 0;
5881 } else {
5882 bp_release = 1;
5884 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5885 XFS_BMAP_BTREE_REF)))
5886 goto error_norelse;
5887 block = XFS_BUF_TO_BLOCK(bp);
5889 if (bp_release) {
5890 bp_release = 0;
5891 xfs_trans_brelse(NULL, bp);
5893 return;
5895 error0:
5896 cmn_err(CE_WARN, "%s: at error0", __func__);
5897 if (bp_release)
5898 xfs_trans_brelse(NULL, bp);
5899 error_norelse:
5900 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
5901 __func__, i);
5902 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
5903 return;
5905 #endif
5908 * Count fsblocks of the given fork.
5910 int /* error */
5911 xfs_bmap_count_blocks(
5912 xfs_trans_t *tp, /* transaction pointer */
5913 xfs_inode_t *ip, /* incore inode */
5914 int whichfork, /* data or attr fork */
5915 int *count) /* out: count of blocks */
5917 struct xfs_btree_block *block; /* current btree block */
5918 xfs_fsblock_t bno; /* block # of "block" */
5919 xfs_ifork_t *ifp; /* fork structure */
5920 int level; /* btree level, for checking */
5921 xfs_mount_t *mp; /* file system mount structure */
5922 __be64 *pp; /* pointer to block address */
5924 bno = NULLFSBLOCK;
5925 mp = ip->i_mount;
5926 ifp = XFS_IFORK_PTR(ip, whichfork);
5927 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
5928 xfs_bmap_count_leaves(ifp, 0,
5929 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
5930 count);
5931 return 0;
5935 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5937 block = ifp->if_broot;
5938 level = be16_to_cpu(block->bb_level);
5939 ASSERT(level > 0);
5940 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
5941 bno = be64_to_cpu(*pp);
5942 ASSERT(bno != NULLDFSBNO);
5943 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5944 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5946 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
5947 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
5948 mp);
5949 return XFS_ERROR(EFSCORRUPTED);
5952 return 0;
5956 * Recursively walks each level of a btree
5957 * to count total fsblocks is use.
5959 STATIC int /* error */
5960 xfs_bmap_count_tree(
5961 xfs_mount_t *mp, /* file system mount point */
5962 xfs_trans_t *tp, /* transaction pointer */
5963 xfs_ifork_t *ifp, /* inode fork pointer */
5964 xfs_fsblock_t blockno, /* file system block number */
5965 int levelin, /* level in btree */
5966 int *count) /* Count of blocks */
5968 int error;
5969 xfs_buf_t *bp, *nbp;
5970 int level = levelin;
5971 __be64 *pp;
5972 xfs_fsblock_t bno = blockno;
5973 xfs_fsblock_t nextbno;
5974 struct xfs_btree_block *block, *nextblock;
5975 int numrecs;
5977 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
5978 return error;
5979 *count += 1;
5980 block = XFS_BUF_TO_BLOCK(bp);
5982 if (--level) {
5983 /* Not at node above leaves, count this level of nodes */
5984 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
5985 while (nextbno != NULLFSBLOCK) {
5986 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
5987 0, &nbp, XFS_BMAP_BTREE_REF)))
5988 return error;
5989 *count += 1;
5990 nextblock = XFS_BUF_TO_BLOCK(nbp);
5991 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
5992 xfs_trans_brelse(tp, nbp);
5995 /* Dive to the next level */
5996 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
5997 bno = be64_to_cpu(*pp);
5998 if (unlikely((error =
5999 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6000 xfs_trans_brelse(tp, bp);
6001 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6002 XFS_ERRLEVEL_LOW, mp);
6003 return XFS_ERROR(EFSCORRUPTED);
6005 xfs_trans_brelse(tp, bp);
6006 } else {
6007 /* count all level 1 nodes and their leaves */
6008 for (;;) {
6009 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6010 numrecs = be16_to_cpu(block->bb_numrecs);
6011 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
6012 xfs_trans_brelse(tp, bp);
6013 if (nextbno == NULLFSBLOCK)
6014 break;
6015 bno = nextbno;
6016 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6017 XFS_BMAP_BTREE_REF)))
6018 return error;
6019 *count += 1;
6020 block = XFS_BUF_TO_BLOCK(bp);
6023 return 0;
6027 * Count leaf blocks given a range of extent records.
6029 STATIC void
6030 xfs_bmap_count_leaves(
6031 xfs_ifork_t *ifp,
6032 xfs_extnum_t idx,
6033 int numrecs,
6034 int *count)
6036 int b;
6038 for (b = 0; b < numrecs; b++) {
6039 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
6040 *count += xfs_bmbt_get_blockcount(frp);
6045 * Count leaf blocks given a range of extent records originally
6046 * in btree format.
6048 STATIC void
6049 xfs_bmap_disk_count_leaves(
6050 struct xfs_mount *mp,
6051 struct xfs_btree_block *block,
6052 int numrecs,
6053 int *count)
6055 int b;
6056 xfs_bmbt_rec_t *frp;
6058 for (b = 1; b <= numrecs; b++) {
6059 frp = XFS_BMBT_REC_ADDR(mp, block, b);
6060 *count += xfs_bmbt_disk_get_blockcount(frp);