2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
35 #include "xfs_macros.h"
36 #include "xfs_types.h"
39 #include "xfs_trans.h"
44 #include "xfs_dmapi.h"
45 #include "xfs_mount.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_bmap_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_btree.h"
50 #include "xfs_ialloc.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dir_sf.h"
53 #include "xfs_dir2_sf.h"
54 #include "xfs_dinode.h"
55 #include "xfs_inode_item.h"
56 #include "xfs_inode.h"
57 #include "xfs_itable.h"
58 #include "xfs_extfree_item.h"
59 #include "xfs_alloc.h"
61 #include "xfs_rtalloc.h"
62 #include "xfs_error.h"
63 #include "xfs_da_btree.h"
64 #include "xfs_dir_leaf.h"
67 #include "xfs_quota.h"
68 #include "xfs_trans_space.h"
69 #include "xfs_buf_item.h"
74 xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
77 kmem_zone_t *xfs_bmap_free_item_zone;
80 * Prototypes for internal bmap routines.
85 * Called from xfs_bmap_add_attrfork to handle extents format files.
87 STATIC int /* error */
88 xfs_bmap_add_attrfork_extents(
89 xfs_trans_t *tp, /* transaction pointer */
90 xfs_inode_t *ip, /* incore inode pointer */
91 xfs_fsblock_t *firstblock, /* first block allocated */
92 xfs_bmap_free_t *flist, /* blocks to free at commit */
93 int *flags); /* inode logging flags */
96 * Called from xfs_bmap_add_attrfork to handle local format files.
98 STATIC int /* error */
99 xfs_bmap_add_attrfork_local(
100 xfs_trans_t *tp, /* transaction pointer */
101 xfs_inode_t *ip, /* incore inode pointer */
102 xfs_fsblock_t *firstblock, /* first block allocated */
103 xfs_bmap_free_t *flist, /* blocks to free at commit */
104 int *flags); /* inode logging flags */
107 * Called by xfs_bmapi to update extent list structure and the btree
108 * after allocating space (or doing a delayed allocation).
110 STATIC int /* error */
112 xfs_inode_t *ip, /* incore inode pointer */
113 xfs_extnum_t idx, /* extent number to update/insert */
114 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
115 xfs_bmbt_irec_t *new, /* new data to put in extent list */
116 xfs_fsblock_t *first, /* pointer to firstblock variable */
117 xfs_bmap_free_t *flist, /* list of extents to be freed */
118 int *logflagsp, /* inode logging flags */
119 int whichfork, /* data or attr fork */
120 int rsvd); /* OK to allocate reserved blocks */
123 * Called by xfs_bmap_add_extent to handle cases converting a delayed
124 * allocation to a real allocation.
126 STATIC int /* error */
127 xfs_bmap_add_extent_delay_real(
128 xfs_inode_t *ip, /* incore inode pointer */
129 xfs_extnum_t idx, /* extent number to update/insert */
130 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
131 xfs_bmbt_irec_t *new, /* new data to put in extent list */
132 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
133 xfs_fsblock_t *first, /* pointer to firstblock variable */
134 xfs_bmap_free_t *flist, /* list of extents to be freed */
135 int *logflagsp, /* inode logging flags */
136 int rsvd); /* OK to allocate reserved blocks */
139 * Called by xfs_bmap_add_extent to handle cases converting a hole
140 * to a delayed allocation.
142 STATIC int /* error */
143 xfs_bmap_add_extent_hole_delay(
144 xfs_inode_t *ip, /* incore inode pointer */
145 xfs_extnum_t idx, /* extent number to update/insert */
146 xfs_btree_cur_t *cur, /* if null, not a btree */
147 xfs_bmbt_irec_t *new, /* new data to put in extent list */
148 int *logflagsp,/* inode logging flags */
149 int rsvd); /* OK to allocate reserved blocks */
152 * Called by xfs_bmap_add_extent to handle cases converting a hole
153 * to a real allocation.
155 STATIC int /* error */
156 xfs_bmap_add_extent_hole_real(
157 xfs_inode_t *ip, /* incore inode pointer */
158 xfs_extnum_t idx, /* extent number to update/insert */
159 xfs_btree_cur_t *cur, /* if null, not a btree */
160 xfs_bmbt_irec_t *new, /* new data to put in extent list */
161 int *logflagsp, /* inode logging flags */
162 int whichfork); /* data or attr fork */
165 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
166 * allocation to a real allocation or vice versa.
168 STATIC int /* error */
169 xfs_bmap_add_extent_unwritten_real(
170 xfs_inode_t *ip, /* incore inode pointer */
171 xfs_extnum_t idx, /* extent number to update/insert */
172 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
173 xfs_bmbt_irec_t *new, /* new data to put in extent list */
174 int *logflagsp); /* inode logging flags */
177 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
178 * It figures out where to ask the underlying allocator to put the new extent.
180 STATIC int /* error */
182 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
185 * Transform a btree format file with only one leaf node, where the
186 * extents list will fit in the inode, into an extents format file.
187 * Since the extent list is already in-core, all we have to do is
188 * give up the space for the btree root and pitch the leaf block.
190 STATIC int /* error */
191 xfs_bmap_btree_to_extents(
192 xfs_trans_t *tp, /* transaction pointer */
193 xfs_inode_t *ip, /* incore inode pointer */
194 xfs_btree_cur_t *cur, /* btree cursor */
195 int *logflagsp, /* inode logging flags */
196 int whichfork); /* data or attr fork */
200 * Check that the extents list for the inode ip is in the right order.
203 xfs_bmap_check_extents(
204 xfs_inode_t *ip, /* incore inode pointer */
205 int whichfork); /* data or attr fork */
207 #define xfs_bmap_check_extents(ip,w)
211 * Called by xfs_bmapi to update extent list structure and the btree
212 * after removing space (or undoing a delayed allocation).
214 STATIC int /* error */
216 xfs_inode_t *ip, /* incore inode pointer */
217 xfs_trans_t *tp, /* current trans pointer */
218 xfs_extnum_t idx, /* extent number to update/insert */
219 xfs_bmap_free_t *flist, /* list of extents to be freed */
220 xfs_btree_cur_t *cur, /* if null, not a btree */
221 xfs_bmbt_irec_t *new, /* new data to put in extent list */
222 int *logflagsp,/* inode logging flags */
223 int whichfork, /* data or attr fork */
224 int rsvd); /* OK to allocate reserved blocks */
227 * Remove the entry "free" from the free item list. Prev points to the
228 * previous entry, unless "free" is the head of the list.
232 xfs_bmap_free_t *flist, /* free item list header */
233 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
234 xfs_bmap_free_item_t *free); /* list item to be freed */
237 * Remove count entries from the extents array for inode "ip", starting
238 * at index "idx". Copies the remaining items down over the deleted ones,
239 * and gives back the excess memory.
242 xfs_bmap_delete_exlist(
243 xfs_inode_t *ip, /* incode inode pointer */
244 xfs_extnum_t idx, /* starting delete index */
245 xfs_extnum_t count, /* count of items to delete */
246 int whichfork); /* data or attr fork */
249 * Convert an extents-format file into a btree-format file.
250 * The new file will have a root block (in the inode) and a single child block.
252 STATIC int /* error */
253 xfs_bmap_extents_to_btree(
254 xfs_trans_t *tp, /* transaction pointer */
255 xfs_inode_t *ip, /* incore inode pointer */
256 xfs_fsblock_t *firstblock, /* first-block-allocated */
257 xfs_bmap_free_t *flist, /* blocks freed in xaction */
258 xfs_btree_cur_t **curp, /* cursor returned to caller */
259 int wasdel, /* converting a delayed alloc */
260 int *logflagsp, /* inode logging flags */
261 int whichfork); /* data or attr fork */
264 * Insert new item(s) in the extent list for inode "ip".
265 * Count new items are inserted at offset idx.
268 xfs_bmap_insert_exlist(
269 xfs_inode_t *ip, /* incore inode pointer */
270 xfs_extnum_t idx, /* starting index of new items */
271 xfs_extnum_t count, /* number of inserted items */
272 xfs_bmbt_irec_t *new, /* items to insert */
273 int whichfork); /* data or attr fork */
276 * Convert a local file to an extents file.
277 * This code is sort of bogus, since the file data needs to get
278 * logged so it won't be lost. The bmap-level manipulations are ok, though.
280 STATIC int /* error */
281 xfs_bmap_local_to_extents(
282 xfs_trans_t *tp, /* transaction pointer */
283 xfs_inode_t *ip, /* incore inode pointer */
284 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
285 xfs_extlen_t total, /* total blocks needed by transaction */
286 int *logflagsp, /* inode logging flags */
287 int whichfork); /* data or attr fork */
290 * Search the extents list for the inode, for the extent containing bno.
291 * If bno lies in a hole, point to the next entry. If bno lies past eof,
292 * *eofp will be set, and *prevp will contain the last entry (null if none).
293 * Else, *lastxp will be set to the index of the found
294 * entry; *gotp will contain the entry.
296 STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
297 xfs_bmap_search_extents(
298 xfs_inode_t *ip, /* incore inode pointer */
299 xfs_fileoff_t bno, /* block number searched for */
300 int whichfork, /* data or attr fork */
301 int *eofp, /* out: end of file found */
302 xfs_extnum_t *lastxp, /* out: last extent index */
303 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
304 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
306 #ifdef XFS_BMAP_TRACE
308 * Add a bmap trace buffer entry. Base routine for the others.
311 xfs_bmap_trace_addentry(
312 int opcode, /* operation */
313 char *fname, /* function name */
314 char *desc, /* operation description */
315 xfs_inode_t *ip, /* incore inode pointer */
316 xfs_extnum_t idx, /* index of entry(ies) */
317 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
318 xfs_bmbt_rec_t *r1, /* first record */
319 xfs_bmbt_rec_t *r2, /* second record or null */
320 int whichfork); /* data or attr fork */
323 * Add bmap trace entry prior to a call to xfs_bmap_delete_exlist.
326 xfs_bmap_trace_delete(
327 char *fname, /* function name */
328 char *desc, /* operation description */
329 xfs_inode_t *ip, /* incore inode pointer */
330 xfs_extnum_t idx, /* index of entry(entries) deleted */
331 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
332 int whichfork); /* data or attr fork */
335 * Add bmap trace entry prior to a call to xfs_bmap_insert_exlist, or
336 * reading in the extents list from the disk (in the btree).
339 xfs_bmap_trace_insert(
340 char *fname, /* function name */
341 char *desc, /* operation description */
342 xfs_inode_t *ip, /* incore inode pointer */
343 xfs_extnum_t idx, /* index of entry(entries) inserted */
344 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
345 xfs_bmbt_irec_t *r1, /* inserted record 1 */
346 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
347 int whichfork); /* data or attr fork */
350 * Add bmap trace entry after updating an extent list entry in place.
353 xfs_bmap_trace_post_update(
354 char *fname, /* function name */
355 char *desc, /* operation description */
356 xfs_inode_t *ip, /* incore inode pointer */
357 xfs_extnum_t idx, /* index of entry updated */
358 int whichfork); /* data or attr fork */
361 * Add bmap trace entry prior to updating an extent list entry in place.
364 xfs_bmap_trace_pre_update(
365 char *fname, /* function name */
366 char *desc, /* operation description */
367 xfs_inode_t *ip, /* incore inode pointer */
368 xfs_extnum_t idx, /* index of entry to be updated */
369 int whichfork); /* data or attr fork */
372 #define xfs_bmap_trace_delete(f,d,ip,i,c,w)
373 #define xfs_bmap_trace_insert(f,d,ip,i,c,r1,r2,w)
374 #define xfs_bmap_trace_post_update(f,d,ip,i,w)
375 #define xfs_bmap_trace_pre_update(f,d,ip,i,w)
376 #endif /* XFS_BMAP_TRACE */
379 * Compute the worst-case number of indirect blocks that will be used
380 * for ip's delayed extent of length "len".
383 xfs_bmap_worst_indlen(
384 xfs_inode_t *ip, /* incore inode pointer */
385 xfs_filblks_t len); /* delayed extent length */
389 * Perform various validation checks on the values being returned
393 xfs_bmap_validate_ret(
397 xfs_bmbt_irec_t *mval,
401 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
404 #if defined(XFS_RW_TRACE)
413 #define xfs_bunmap_trace(ip, bno, len, flags, ra)
414 #endif /* XFS_RW_TRACE */
420 xfs_fsblock_t blockno,
425 xfs_bmap_count_leaves(
431 * Bmap internal routines.
435 * Called from xfs_bmap_add_attrfork to handle btree format files.
437 STATIC int /* error */
438 xfs_bmap_add_attrfork_btree(
439 xfs_trans_t *tp, /* transaction pointer */
440 xfs_inode_t *ip, /* incore inode pointer */
441 xfs_fsblock_t *firstblock, /* first block allocated */
442 xfs_bmap_free_t *flist, /* blocks to free at commit */
443 int *flags) /* inode logging flags */
445 xfs_btree_cur_t *cur; /* btree cursor */
446 int error; /* error return value */
447 xfs_mount_t *mp; /* file system mount struct */
448 int stat; /* newroot status */
451 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
452 *flags |= XFS_ILOG_DBROOT;
454 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
456 cur->bc_private.b.flist = flist;
457 cur->bc_private.b.firstblock = *firstblock;
458 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
460 ASSERT(stat == 1); /* must be at least one entry */
461 if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
464 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
465 return XFS_ERROR(ENOSPC);
467 *firstblock = cur->bc_private.b.firstblock;
468 cur->bc_private.b.allocated = 0;
469 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
473 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
478 * Called from xfs_bmap_add_attrfork to handle extents format files.
480 STATIC int /* error */
481 xfs_bmap_add_attrfork_extents(
482 xfs_trans_t *tp, /* transaction pointer */
483 xfs_inode_t *ip, /* incore inode pointer */
484 xfs_fsblock_t *firstblock, /* first block allocated */
485 xfs_bmap_free_t *flist, /* blocks to free at commit */
486 int *flags) /* inode logging flags */
488 xfs_btree_cur_t *cur; /* bmap btree cursor */
489 int error; /* error return value */
491 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
494 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
495 flags, XFS_DATA_FORK);
497 cur->bc_private.b.allocated = 0;
498 xfs_btree_del_cursor(cur,
499 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
505 * Called from xfs_bmap_add_attrfork to handle local format files.
507 STATIC int /* error */
508 xfs_bmap_add_attrfork_local(
509 xfs_trans_t *tp, /* transaction pointer */
510 xfs_inode_t *ip, /* incore inode pointer */
511 xfs_fsblock_t *firstblock, /* first block allocated */
512 xfs_bmap_free_t *flist, /* blocks to free at commit */
513 int *flags) /* inode logging flags */
515 xfs_da_args_t dargs; /* args for dir/attr code */
516 int error; /* error return value */
517 xfs_mount_t *mp; /* mount structure pointer */
519 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
521 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
523 memset(&dargs, 0, sizeof(dargs));
525 dargs.firstblock = firstblock;
527 dargs.total = mp->m_dirblkfsbs;
528 dargs.whichfork = XFS_DATA_FORK;
530 error = XFS_DIR_SHORTFORM_TO_SINGLE(mp, &dargs);
532 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
538 * Called by xfs_bmapi to update extent list structure and the btree
539 * after allocating space (or doing a delayed allocation).
541 STATIC int /* error */
543 xfs_inode_t *ip, /* incore inode pointer */
544 xfs_extnum_t idx, /* extent number to update/insert */
545 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
546 xfs_bmbt_irec_t *new, /* new data to put in extent list */
547 xfs_fsblock_t *first, /* pointer to firstblock variable */
548 xfs_bmap_free_t *flist, /* list of extents to be freed */
549 int *logflagsp, /* inode logging flags */
550 int whichfork, /* data or attr fork */
551 int rsvd) /* OK to use reserved data blocks */
553 xfs_btree_cur_t *cur; /* btree cursor or null */
554 xfs_filblks_t da_new; /* new count del alloc blocks used */
555 xfs_filblks_t da_old; /* old count del alloc blocks used */
556 int error; /* error return value */
557 #ifdef XFS_BMAP_TRACE
558 static char fname[] = "xfs_bmap_add_extent";
560 xfs_ifork_t *ifp; /* inode fork ptr */
561 int logflags; /* returned value */
562 xfs_extnum_t nextents; /* number of extents in file now */
564 XFS_STATS_INC(xs_add_exlist);
566 ifp = XFS_IFORK_PTR(ip, whichfork);
567 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
568 ASSERT(idx <= nextents);
572 * This is the first extent added to a new/empty file.
573 * Special case this one, so other routines get to assume there are
574 * already extents in the list.
577 xfs_bmap_trace_insert(fname, "insert empty", ip, 0, 1, new,
579 xfs_bmap_insert_exlist(ip, 0, 1, new, whichfork);
582 if (!ISNULLSTARTBLOCK(new->br_startblock)) {
583 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
584 logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
589 * Any kind of new delayed allocation goes here.
591 else if (ISNULLSTARTBLOCK(new->br_startblock)) {
593 ASSERT((cur->bc_private.b.flags &
594 XFS_BTCUR_BPRV_WASDEL) == 0);
595 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, cur, new,
600 * Real allocation off the end of the file.
602 else if (idx == nextents) {
604 ASSERT((cur->bc_private.b.flags &
605 XFS_BTCUR_BPRV_WASDEL) == 0);
606 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
607 &logflags, whichfork)))
610 xfs_bmbt_irec_t prev; /* old extent at offset idx */
613 * Get the record referred to by idx.
615 xfs_bmbt_get_all(&ifp->if_u1.if_extents[idx], &prev);
617 * If it's a real allocation record, and the new allocation ends
618 * after the start of the referred to record, then we're filling
619 * in a delayed or unwritten allocation with a real one, or
620 * converting real back to unwritten.
622 if (!ISNULLSTARTBLOCK(new->br_startblock) &&
623 new->br_startoff + new->br_blockcount > prev.br_startoff) {
624 if (prev.br_state != XFS_EXT_UNWRITTEN &&
625 ISNULLSTARTBLOCK(prev.br_startblock)) {
626 da_old = STARTBLOCKVAL(prev.br_startblock);
628 ASSERT(cur->bc_private.b.flags &
629 XFS_BTCUR_BPRV_WASDEL);
630 if ((error = xfs_bmap_add_extent_delay_real(ip,
631 idx, &cur, new, &da_new, first, flist,
634 } else if (new->br_state == XFS_EXT_NORM) {
635 ASSERT(new->br_state == XFS_EXT_NORM);
636 if ((error = xfs_bmap_add_extent_unwritten_real(
637 ip, idx, &cur, new, &logflags)))
640 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
641 if ((error = xfs_bmap_add_extent_unwritten_real(
642 ip, idx, &cur, new, &logflags)))
645 ASSERT(*curp == cur || *curp == NULL);
648 * Otherwise we're filling in a hole with an allocation.
652 ASSERT((cur->bc_private.b.flags &
653 XFS_BTCUR_BPRV_WASDEL) == 0);
654 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
655 new, &logflags, whichfork)))
660 ASSERT(*curp == cur || *curp == NULL);
662 * Convert to a btree if necessary.
664 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
665 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
666 int tmp_logflags; /* partial log flag return val */
669 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
670 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
671 logflags |= tmp_logflags;
676 * Adjust for changes in reserved delayed indirect blocks.
677 * Nothing to do for disk quotas here.
679 if (da_old || da_new) {
684 nblks += cur->bc_private.b.allocated;
685 ASSERT(nblks <= da_old);
687 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
688 (int)(da_old - nblks), rsvd);
691 * Clear out the allocated field, done with it now in any case.
694 cur->bc_private.b.allocated = 0;
700 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
702 *logflagsp = logflags;
707 * Called by xfs_bmap_add_extent to handle cases converting a delayed
708 * allocation to a real allocation.
710 STATIC int /* error */
711 xfs_bmap_add_extent_delay_real(
712 xfs_inode_t *ip, /* incore inode pointer */
713 xfs_extnum_t idx, /* extent number to update/insert */
714 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
715 xfs_bmbt_irec_t *new, /* new data to put in extent list */
716 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
717 xfs_fsblock_t *first, /* pointer to firstblock variable */
718 xfs_bmap_free_t *flist, /* list of extents to be freed */
719 int *logflagsp, /* inode logging flags */
720 int rsvd) /* OK to use reserved data block allocation */
722 xfs_bmbt_rec_t *base; /* base of extent entry list */
723 xfs_btree_cur_t *cur; /* btree cursor */
724 int diff; /* temp value */
725 xfs_bmbt_rec_t *ep; /* extent entry for idx */
726 int error; /* error return value */
727 #ifdef XFS_BMAP_TRACE
728 static char fname[] = "xfs_bmap_add_extent_delay_real";
730 int i; /* temp state */
731 xfs_fileoff_t new_endoff; /* end offset of new entry */
732 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
733 /* left is 0, right is 1, prev is 2 */
734 int rval=0; /* return value (logging flags) */
735 int state = 0;/* state bits, accessed thru macros */
736 xfs_filblks_t temp; /* value for dnew calculations */
737 xfs_filblks_t temp2; /* value for dnew calculations */
738 int tmp_rval; /* partial logging flags */
739 enum { /* bit number definitions for state */
740 LEFT_CONTIG, RIGHT_CONTIG,
741 LEFT_FILLING, RIGHT_FILLING,
742 LEFT_DELAY, RIGHT_DELAY,
743 LEFT_VALID, RIGHT_VALID
749 #define MASK(b) (1 << (b))
750 #define MASK2(a,b) (MASK(a) | MASK(b))
751 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
752 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
753 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
754 #define STATE_TEST(b) (state & MASK(b))
755 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
756 ((state &= ~MASK(b)), 0))
757 #define SWITCH_STATE \
758 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
761 * Set up a bunch of variables to make the tests simpler.
764 base = ip->i_df.if_u1.if_extents;
766 xfs_bmbt_get_all(ep, &PREV);
767 new_endoff = new->br_startoff + new->br_blockcount;
768 ASSERT(PREV.br_startoff <= new->br_startoff);
769 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
771 * Set flags determining what part of the previous delayed allocation
772 * extent is being replaced by a real allocation.
774 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
775 STATE_SET(RIGHT_FILLING,
776 PREV.br_startoff + PREV.br_blockcount == new_endoff);
778 * Check and set flags if this segment has a left neighbor.
779 * Don't set contiguous if the combined extent would be too large.
781 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
782 xfs_bmbt_get_all(ep - 1, &LEFT);
783 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
785 STATE_SET(LEFT_CONTIG,
786 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
787 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
788 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
789 LEFT.br_state == new->br_state &&
790 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
792 * Check and set flags if this segment has a right neighbor.
793 * Don't set contiguous if the combined extent would be too large.
794 * Also check for all-three-contiguous being too large.
796 if (STATE_SET_TEST(RIGHT_VALID,
798 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
799 xfs_bmbt_get_all(ep + 1, &RIGHT);
800 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
802 STATE_SET(RIGHT_CONTIG,
803 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
804 new_endoff == RIGHT.br_startoff &&
805 new->br_startblock + new->br_blockcount ==
806 RIGHT.br_startblock &&
807 new->br_state == RIGHT.br_state &&
808 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
809 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
810 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
811 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
815 * Switch out based on the FILLING and CONTIG state bits.
817 switch (SWITCH_STATE) {
819 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
821 * Filling in all of a previously delayed allocation extent.
822 * The left and right neighbors are both contiguous with new.
824 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
826 xfs_bmbt_set_blockcount(ep - 1,
827 LEFT.br_blockcount + PREV.br_blockcount +
828 RIGHT.br_blockcount);
829 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
831 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
833 xfs_bmap_delete_exlist(ip, idx, 2, XFS_DATA_FORK);
834 ip->i_df.if_lastex = idx - 1;
835 ip->i_d.di_nextents--;
837 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
839 rval = XFS_ILOG_CORE;
840 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
842 RIGHT.br_blockcount, &i)))
845 if ((error = xfs_bmbt_delete(cur, &i)))
848 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
851 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
855 RIGHT.br_blockcount, LEFT.br_state)))
861 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
863 * Filling in all of a previously delayed allocation extent.
864 * The left neighbor is contiguous, the right is not.
866 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
868 xfs_bmbt_set_blockcount(ep - 1,
869 LEFT.br_blockcount + PREV.br_blockcount);
870 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
872 ip->i_df.if_lastex = idx - 1;
873 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
875 xfs_bmap_delete_exlist(ip, idx, 1, XFS_DATA_FORK);
877 rval = XFS_ILOG_DEXT;
880 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
881 LEFT.br_startblock, LEFT.br_blockcount,
885 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
888 PREV.br_blockcount, LEFT.br_state)))
894 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
896 * Filling in all of a previously delayed allocation extent.
897 * The right neighbor is contiguous, the left is not.
899 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
901 xfs_bmbt_set_startblock(ep, new->br_startblock);
902 xfs_bmbt_set_blockcount(ep,
903 PREV.br_blockcount + RIGHT.br_blockcount);
904 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
906 ip->i_df.if_lastex = idx;
907 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
909 xfs_bmap_delete_exlist(ip, idx + 1, 1, XFS_DATA_FORK);
911 rval = XFS_ILOG_DEXT;
914 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
916 RIGHT.br_blockcount, &i)))
919 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
922 RIGHT.br_blockcount, PREV.br_state)))
928 case MASK2(LEFT_FILLING, RIGHT_FILLING):
930 * Filling in all of a previously delayed allocation extent.
931 * Neither the left nor right neighbors are contiguous with
934 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
936 xfs_bmbt_set_startblock(ep, new->br_startblock);
937 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
939 ip->i_df.if_lastex = idx;
940 ip->i_d.di_nextents++;
942 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
944 rval = XFS_ILOG_CORE;
945 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
946 new->br_startblock, new->br_blockcount,
950 cur->bc_rec.b.br_state = XFS_EXT_NORM;
951 if ((error = xfs_bmbt_insert(cur, &i)))
958 case MASK2(LEFT_FILLING, LEFT_CONTIG):
960 * Filling in the first part of a previous delayed allocation.
961 * The left neighbor is contiguous.
963 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
965 xfs_bmbt_set_blockcount(ep - 1,
966 LEFT.br_blockcount + new->br_blockcount);
967 xfs_bmbt_set_startoff(ep,
968 PREV.br_startoff + new->br_blockcount);
969 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
971 temp = PREV.br_blockcount - new->br_blockcount;
972 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
974 xfs_bmbt_set_blockcount(ep, temp);
975 ip->i_df.if_lastex = idx - 1;
977 rval = XFS_ILOG_DEXT;
980 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
981 LEFT.br_startblock, LEFT.br_blockcount,
985 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
992 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
993 STARTBLOCKVAL(PREV.br_startblock));
994 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
995 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
1000 case MASK(LEFT_FILLING):
1002 * Filling in the first part of a previous delayed allocation.
1003 * The left neighbor is not contiguous.
1005 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1006 xfs_bmbt_set_startoff(ep, new_endoff);
1007 temp = PREV.br_blockcount - new->br_blockcount;
1008 xfs_bmbt_set_blockcount(ep, temp);
1009 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
1011 xfs_bmap_insert_exlist(ip, idx, 1, new, XFS_DATA_FORK);
1012 ip->i_df.if_lastex = idx;
1013 ip->i_d.di_nextents++;
1015 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1017 rval = XFS_ILOG_CORE;
1018 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1019 new->br_startblock, new->br_blockcount,
1023 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1024 if ((error = xfs_bmbt_insert(cur, &i)))
1028 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1029 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1030 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1031 first, flist, &cur, 1, &tmp_rval,
1037 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1038 STARTBLOCKVAL(PREV.br_startblock) -
1039 (cur ? cur->bc_private.b.allocated : 0));
1040 base = ip->i_df.if_u1.if_extents;
1041 ep = &base[idx + 1];
1042 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1043 xfs_bmap_trace_post_update(fname, "LF", ip, idx + 1,
1048 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1050 * Filling in the last part of a previous delayed allocation.
1051 * The right neighbor is contiguous with the new allocation.
1053 temp = PREV.br_blockcount - new->br_blockcount;
1054 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1056 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1058 xfs_bmbt_set_blockcount(ep, temp);
1059 xfs_bmbt_set_allf(ep + 1, new->br_startoff, new->br_startblock,
1060 new->br_blockcount + RIGHT.br_blockcount,
1062 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1064 ip->i_df.if_lastex = idx + 1;
1066 rval = XFS_ILOG_DEXT;
1069 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1070 RIGHT.br_startblock,
1071 RIGHT.br_blockcount, &i)))
1074 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1076 new->br_blockcount +
1077 RIGHT.br_blockcount,
1081 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1082 STARTBLOCKVAL(PREV.br_startblock));
1083 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1084 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1089 case MASK(RIGHT_FILLING):
1091 * Filling in the last part of a previous delayed allocation.
1092 * The right neighbor is not contiguous.
1094 temp = PREV.br_blockcount - new->br_blockcount;
1095 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1096 xfs_bmbt_set_blockcount(ep, temp);
1097 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1098 new, NULL, XFS_DATA_FORK);
1099 xfs_bmap_insert_exlist(ip, idx + 1, 1, new, XFS_DATA_FORK);
1100 ip->i_df.if_lastex = idx + 1;
1101 ip->i_d.di_nextents++;
1103 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1105 rval = XFS_ILOG_CORE;
1106 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1107 new->br_startblock, new->br_blockcount,
1111 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1112 if ((error = xfs_bmbt_insert(cur, &i)))
1116 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1117 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1118 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1119 first, flist, &cur, 1, &tmp_rval,
1125 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1126 STARTBLOCKVAL(PREV.br_startblock) -
1127 (cur ? cur->bc_private.b.allocated : 0));
1128 base = ip->i_df.if_u1.if_extents;
1130 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1131 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1137 * Filling in the middle part of a previous delayed allocation.
1138 * Contiguity is impossible here.
1139 * This case is avoided almost all the time.
1141 temp = new->br_startoff - PREV.br_startoff;
1142 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1143 xfs_bmbt_set_blockcount(ep, temp);
1145 r[1].br_startoff = new_endoff;
1146 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1147 r[1].br_blockcount = temp2;
1148 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1150 xfs_bmap_insert_exlist(ip, idx + 1, 2, &r[0], XFS_DATA_FORK);
1151 ip->i_df.if_lastex = idx + 1;
1152 ip->i_d.di_nextents++;
1154 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1156 rval = XFS_ILOG_CORE;
1157 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1158 new->br_startblock, new->br_blockcount,
1162 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1163 if ((error = xfs_bmbt_insert(cur, &i)))
1167 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1168 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1169 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1170 first, flist, &cur, 1, &tmp_rval,
1176 temp = xfs_bmap_worst_indlen(ip, temp);
1177 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1178 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
1179 (cur ? cur->bc_private.b.allocated : 0));
1181 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -diff, rsvd)) {
1183 * Ick gross gag me with a spoon.
1185 ASSERT(0); /* want to see if this ever happens! */
1191 !xfs_mod_incore_sb(ip->i_mount,
1192 XFS_SBS_FDBLOCKS, -diff, rsvd))
1199 !xfs_mod_incore_sb(ip->i_mount,
1200 XFS_SBS_FDBLOCKS, -diff, rsvd))
1205 base = ip->i_df.if_u1.if_extents;
1207 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1208 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1209 xfs_bmap_trace_pre_update(fname, "0", ip, idx + 2,
1211 xfs_bmbt_set_startblock(ep + 2, NULLSTARTBLOCK((int)temp2));
1212 xfs_bmap_trace_post_update(fname, "0", ip, idx + 2,
1214 *dnew = temp + temp2;
1217 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1218 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1219 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1220 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1221 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1222 case MASK(LEFT_CONTIG):
1223 case MASK(RIGHT_CONTIG):
1225 * These cases are all impossible.
1242 #undef STATE_SET_TEST
1247 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1248 * allocation to a real allocation or vice versa.
1250 STATIC int /* error */
1251 xfs_bmap_add_extent_unwritten_real(
1252 xfs_inode_t *ip, /* incore inode pointer */
1253 xfs_extnum_t idx, /* extent number to update/insert */
1254 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1255 xfs_bmbt_irec_t *new, /* new data to put in extent list */
1256 int *logflagsp) /* inode logging flags */
1258 xfs_bmbt_rec_t *base; /* base of extent entry list */
1259 xfs_btree_cur_t *cur; /* btree cursor */
1260 xfs_bmbt_rec_t *ep; /* extent entry for idx */
1261 int error; /* error return value */
1262 #ifdef XFS_BMAP_TRACE
1263 static char fname[] = "xfs_bmap_add_extent_unwritten_real";
1265 int i; /* temp state */
1266 xfs_fileoff_t new_endoff; /* end offset of new entry */
1267 xfs_exntst_t newext; /* new extent state */
1268 xfs_exntst_t oldext; /* old extent state */
1269 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1270 /* left is 0, right is 1, prev is 2 */
1271 int rval=0; /* return value (logging flags) */
1272 int state = 0;/* state bits, accessed thru macros */
1273 enum { /* bit number definitions for state */
1274 LEFT_CONTIG, RIGHT_CONTIG,
1275 LEFT_FILLING, RIGHT_FILLING,
1276 LEFT_DELAY, RIGHT_DELAY,
1277 LEFT_VALID, RIGHT_VALID
1283 #define MASK(b) (1 << (b))
1284 #define MASK2(a,b) (MASK(a) | MASK(b))
1285 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1286 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1287 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1288 #define STATE_TEST(b) (state & MASK(b))
1289 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1290 ((state &= ~MASK(b)), 0))
1291 #define SWITCH_STATE \
1292 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1295 * Set up a bunch of variables to make the tests simpler.
1299 base = ip->i_df.if_u1.if_extents;
1301 xfs_bmbt_get_all(ep, &PREV);
1302 newext = new->br_state;
1303 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1304 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1305 ASSERT(PREV.br_state == oldext);
1306 new_endoff = new->br_startoff + new->br_blockcount;
1307 ASSERT(PREV.br_startoff <= new->br_startoff);
1308 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1310 * Set flags determining what part of the previous oldext allocation
1311 * extent is being replaced by a newext allocation.
1313 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
1314 STATE_SET(RIGHT_FILLING,
1315 PREV.br_startoff + PREV.br_blockcount == new_endoff);
1317 * Check and set flags if this segment has a left neighbor.
1318 * Don't set contiguous if the combined extent would be too large.
1320 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1321 xfs_bmbt_get_all(ep - 1, &LEFT);
1322 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
1324 STATE_SET(LEFT_CONTIG,
1325 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1326 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1327 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1328 LEFT.br_state == newext &&
1329 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1331 * Check and set flags if this segment has a right neighbor.
1332 * Don't set contiguous if the combined extent would be too large.
1333 * Also check for all-three-contiguous being too large.
1335 if (STATE_SET_TEST(RIGHT_VALID,
1337 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
1338 xfs_bmbt_get_all(ep + 1, &RIGHT);
1339 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
1341 STATE_SET(RIGHT_CONTIG,
1342 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1343 new_endoff == RIGHT.br_startoff &&
1344 new->br_startblock + new->br_blockcount ==
1345 RIGHT.br_startblock &&
1346 newext == RIGHT.br_state &&
1347 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1348 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
1349 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
1350 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1353 * Switch out based on the FILLING and CONTIG state bits.
1355 switch (SWITCH_STATE) {
1357 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1359 * Setting all of a previous oldext extent to newext.
1360 * The left and right neighbors are both contiguous with new.
1362 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1364 xfs_bmbt_set_blockcount(ep - 1,
1365 LEFT.br_blockcount + PREV.br_blockcount +
1366 RIGHT.br_blockcount);
1367 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1369 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
1371 xfs_bmap_delete_exlist(ip, idx, 2, XFS_DATA_FORK);
1372 ip->i_df.if_lastex = idx - 1;
1373 ip->i_d.di_nextents -= 2;
1375 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1377 rval = XFS_ILOG_CORE;
1378 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1379 RIGHT.br_startblock,
1380 RIGHT.br_blockcount, &i)))
1383 if ((error = xfs_bmbt_delete(cur, &i)))
1386 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1389 if ((error = xfs_bmbt_delete(cur, &i)))
1392 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1395 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1397 LEFT.br_blockcount + PREV.br_blockcount +
1398 RIGHT.br_blockcount, LEFT.br_state)))
1403 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
1405 * Setting all of a previous oldext extent to newext.
1406 * The left neighbor is contiguous, the right is not.
1408 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
1410 xfs_bmbt_set_blockcount(ep - 1,
1411 LEFT.br_blockcount + PREV.br_blockcount);
1412 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
1414 ip->i_df.if_lastex = idx - 1;
1415 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
1417 xfs_bmap_delete_exlist(ip, idx, 1, XFS_DATA_FORK);
1418 ip->i_d.di_nextents--;
1420 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1422 rval = XFS_ILOG_CORE;
1423 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1424 PREV.br_startblock, PREV.br_blockcount,
1428 if ((error = xfs_bmbt_delete(cur, &i)))
1431 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1434 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1436 LEFT.br_blockcount + PREV.br_blockcount,
1442 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
1444 * Setting all of a previous oldext extent to newext.
1445 * The right neighbor is contiguous, the left is not.
1447 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
1449 xfs_bmbt_set_blockcount(ep,
1450 PREV.br_blockcount + RIGHT.br_blockcount);
1451 xfs_bmbt_set_state(ep, newext);
1452 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
1454 ip->i_df.if_lastex = idx;
1455 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
1457 xfs_bmap_delete_exlist(ip, idx + 1, 1, XFS_DATA_FORK);
1458 ip->i_d.di_nextents--;
1460 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1462 rval = XFS_ILOG_CORE;
1463 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1464 RIGHT.br_startblock,
1465 RIGHT.br_blockcount, &i)))
1468 if ((error = xfs_bmbt_delete(cur, &i)))
1471 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1474 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1476 new->br_blockcount + RIGHT.br_blockcount,
1482 case MASK2(LEFT_FILLING, RIGHT_FILLING):
1484 * Setting all of a previous oldext extent to newext.
1485 * Neither the left nor right neighbors are contiguous with
1488 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
1490 xfs_bmbt_set_state(ep, newext);
1491 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
1493 ip->i_df.if_lastex = idx;
1495 rval = XFS_ILOG_DEXT;
1498 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1499 new->br_startblock, new->br_blockcount,
1503 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1504 new->br_startblock, new->br_blockcount,
1510 case MASK2(LEFT_FILLING, LEFT_CONTIG):
1512 * Setting the first part of a previous oldext extent to newext.
1513 * The left neighbor is contiguous.
1515 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
1517 xfs_bmbt_set_blockcount(ep - 1,
1518 LEFT.br_blockcount + new->br_blockcount);
1519 xfs_bmbt_set_startoff(ep,
1520 PREV.br_startoff + new->br_blockcount);
1521 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
1523 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
1525 xfs_bmbt_set_startblock(ep,
1526 new->br_startblock + new->br_blockcount);
1527 xfs_bmbt_set_blockcount(ep,
1528 PREV.br_blockcount - new->br_blockcount);
1529 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
1531 ip->i_df.if_lastex = idx - 1;
1533 rval = XFS_ILOG_DEXT;
1536 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1537 PREV.br_startblock, PREV.br_blockcount,
1541 if ((error = xfs_bmbt_update(cur,
1542 PREV.br_startoff + new->br_blockcount,
1543 PREV.br_startblock + new->br_blockcount,
1544 PREV.br_blockcount - new->br_blockcount,
1547 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1549 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1551 LEFT.br_blockcount + new->br_blockcount,
1557 case MASK(LEFT_FILLING):
1559 * Setting the first part of a previous oldext extent to newext.
1560 * The left neighbor is not contiguous.
1562 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1563 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1564 xfs_bmbt_set_startoff(ep, new_endoff);
1565 xfs_bmbt_set_blockcount(ep,
1566 PREV.br_blockcount - new->br_blockcount);
1567 xfs_bmbt_set_startblock(ep,
1568 new->br_startblock + new->br_blockcount);
1569 xfs_bmap_trace_post_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1570 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
1572 xfs_bmap_insert_exlist(ip, idx, 1, new, XFS_DATA_FORK);
1573 ip->i_df.if_lastex = idx;
1574 ip->i_d.di_nextents++;
1576 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1578 rval = XFS_ILOG_CORE;
1579 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1580 PREV.br_startblock, PREV.br_blockcount,
1584 if ((error = xfs_bmbt_update(cur,
1585 PREV.br_startoff + new->br_blockcount,
1586 PREV.br_startblock + new->br_blockcount,
1587 PREV.br_blockcount - new->br_blockcount,
1590 cur->bc_rec.b = *new;
1591 if ((error = xfs_bmbt_insert(cur, &i)))
1597 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1599 * Setting the last part of a previous oldext extent to newext.
1600 * The right neighbor is contiguous with the new allocation.
1602 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1604 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1606 xfs_bmbt_set_blockcount(ep,
1607 PREV.br_blockcount - new->br_blockcount);
1608 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1610 xfs_bmbt_set_allf(ep + 1, new->br_startoff, new->br_startblock,
1611 new->br_blockcount + RIGHT.br_blockcount, newext);
1612 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1614 ip->i_df.if_lastex = idx + 1;
1616 rval = XFS_ILOG_DEXT;
1619 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1621 PREV.br_blockcount, &i)))
1624 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1626 PREV.br_blockcount - new->br_blockcount,
1629 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1631 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1633 new->br_blockcount + RIGHT.br_blockcount,
1639 case MASK(RIGHT_FILLING):
1641 * Setting the last part of a previous oldext extent to newext.
1642 * The right neighbor is not contiguous.
1644 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1645 xfs_bmbt_set_blockcount(ep,
1646 PREV.br_blockcount - new->br_blockcount);
1647 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1648 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1649 new, NULL, XFS_DATA_FORK);
1650 xfs_bmap_insert_exlist(ip, idx + 1, 1, new, XFS_DATA_FORK);
1651 ip->i_df.if_lastex = idx + 1;
1652 ip->i_d.di_nextents++;
1654 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1656 rval = XFS_ILOG_CORE;
1657 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1658 PREV.br_startblock, PREV.br_blockcount,
1662 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1664 PREV.br_blockcount - new->br_blockcount,
1667 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1668 new->br_startblock, new->br_blockcount,
1672 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1673 if ((error = xfs_bmbt_insert(cur, &i)))
1681 * Setting the middle part of a previous oldext extent to
1682 * newext. Contiguity is impossible here.
1683 * One extent becomes three extents.
1685 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1686 xfs_bmbt_set_blockcount(ep,
1687 new->br_startoff - PREV.br_startoff);
1688 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1690 r[1].br_startoff = new_endoff;
1691 r[1].br_blockcount =
1692 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1693 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1694 r[1].br_state = oldext;
1695 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1697 xfs_bmap_insert_exlist(ip, idx + 1, 2, &r[0], XFS_DATA_FORK);
1698 ip->i_df.if_lastex = idx + 1;
1699 ip->i_d.di_nextents += 2;
1701 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1703 rval = XFS_ILOG_CORE;
1704 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1705 PREV.br_startblock, PREV.br_blockcount,
1709 /* new right extent - oldext */
1710 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1711 r[1].br_startblock, r[1].br_blockcount,
1714 /* new left extent - oldext */
1715 PREV.br_blockcount =
1716 new->br_startoff - PREV.br_startoff;
1717 cur->bc_rec.b = PREV;
1718 if ((error = xfs_bmbt_insert(cur, &i)))
1721 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1724 /* new middle extent - newext */
1725 cur->bc_rec.b = *new;
1726 if ((error = xfs_bmbt_insert(cur, &i)))
1732 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1733 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1734 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1735 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1736 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1737 case MASK(LEFT_CONTIG):
1738 case MASK(RIGHT_CONTIG):
1740 * These cases are all impossible.
1757 #undef STATE_SET_TEST
1762 * Called by xfs_bmap_add_extent to handle cases converting a hole
1763 * to a delayed allocation.
1766 STATIC int /* error */
1767 xfs_bmap_add_extent_hole_delay(
1768 xfs_inode_t *ip, /* incore inode pointer */
1769 xfs_extnum_t idx, /* extent number to update/insert */
1770 xfs_btree_cur_t *cur, /* if null, not a btree */
1771 xfs_bmbt_irec_t *new, /* new data to put in extent list */
1772 int *logflagsp, /* inode logging flags */
1773 int rsvd) /* OK to allocate reserved blocks */
1775 xfs_bmbt_rec_t *base; /* base of extent entry list */
1776 xfs_bmbt_rec_t *ep; /* extent list entry for idx */
1777 #ifdef XFS_BMAP_TRACE
1778 static char fname[] = "xfs_bmap_add_extent_hole_delay";
1780 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1781 xfs_filblks_t newlen=0; /* new indirect size */
1782 xfs_filblks_t oldlen=0; /* old indirect size */
1783 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1784 int state; /* state bits, accessed thru macros */
1785 xfs_filblks_t temp; /* temp for indirect calculations */
1786 enum { /* bit number definitions for state */
1787 LEFT_CONTIG, RIGHT_CONTIG,
1788 LEFT_DELAY, RIGHT_DELAY,
1789 LEFT_VALID, RIGHT_VALID
1792 #define MASK(b) (1 << (b))
1793 #define MASK2(a,b) (MASK(a) | MASK(b))
1794 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1795 #define STATE_TEST(b) (state & MASK(b))
1796 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1797 ((state &= ~MASK(b)), 0))
1798 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1800 base = ip->i_df.if_u1.if_extents;
1803 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1805 * Check and set flags if this segment has a left neighbor
1807 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1808 xfs_bmbt_get_all(ep - 1, &left);
1809 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1812 * Check and set flags if the current (right) segment exists.
1813 * If it doesn't exist, we're converting the hole at end-of-file.
1815 if (STATE_SET_TEST(RIGHT_VALID,
1817 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1818 xfs_bmbt_get_all(ep, &right);
1819 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1822 * Set contiguity flags on the left and right neighbors.
1823 * Don't let extents get too large, even if the pieces are contiguous.
1825 STATE_SET(LEFT_CONTIG,
1826 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1827 left.br_startoff + left.br_blockcount == new->br_startoff &&
1828 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1829 STATE_SET(RIGHT_CONTIG,
1830 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1831 new->br_startoff + new->br_blockcount == right.br_startoff &&
1832 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1833 (!STATE_TEST(LEFT_CONTIG) ||
1834 (left.br_blockcount + new->br_blockcount +
1835 right.br_blockcount <= MAXEXTLEN)));
1837 * Switch out based on the contiguity flags.
1839 switch (SWITCH_STATE) {
1841 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1843 * New allocation is contiguous with delayed allocations
1844 * on the left and on the right.
1845 * Merge all three into a single extent list entry.
1847 temp = left.br_blockcount + new->br_blockcount +
1848 right.br_blockcount;
1849 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
1851 xfs_bmbt_set_blockcount(ep - 1, temp);
1852 oldlen = STARTBLOCKVAL(left.br_startblock) +
1853 STARTBLOCKVAL(new->br_startblock) +
1854 STARTBLOCKVAL(right.br_startblock);
1855 newlen = xfs_bmap_worst_indlen(ip, temp);
1856 xfs_bmbt_set_startblock(ep - 1, NULLSTARTBLOCK((int)newlen));
1857 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
1859 xfs_bmap_trace_delete(fname, "LC|RC", ip, idx, 1,
1861 xfs_bmap_delete_exlist(ip, idx, 1, XFS_DATA_FORK);
1862 ip->i_df.if_lastex = idx - 1;
1865 case MASK(LEFT_CONTIG):
1867 * New allocation is contiguous with a delayed allocation
1869 * Merge the new allocation with the left neighbor.
1871 temp = left.br_blockcount + new->br_blockcount;
1872 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1,
1874 xfs_bmbt_set_blockcount(ep - 1, temp);
1875 oldlen = STARTBLOCKVAL(left.br_startblock) +
1876 STARTBLOCKVAL(new->br_startblock);
1877 newlen = xfs_bmap_worst_indlen(ip, temp);
1878 xfs_bmbt_set_startblock(ep - 1, NULLSTARTBLOCK((int)newlen));
1879 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1,
1881 ip->i_df.if_lastex = idx - 1;
1884 case MASK(RIGHT_CONTIG):
1886 * New allocation is contiguous with a delayed allocation
1888 * Merge the new allocation with the right neighbor.
1890 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1891 temp = new->br_blockcount + right.br_blockcount;
1892 oldlen = STARTBLOCKVAL(new->br_startblock) +
1893 STARTBLOCKVAL(right.br_startblock);
1894 newlen = xfs_bmap_worst_indlen(ip, temp);
1895 xfs_bmbt_set_allf(ep, new->br_startoff,
1896 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
1897 xfs_bmap_trace_post_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1898 ip->i_df.if_lastex = idx;
1903 * New allocation is not contiguous with another
1904 * delayed allocation.
1905 * Insert a new entry.
1907 oldlen = newlen = 0;
1908 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
1910 xfs_bmap_insert_exlist(ip, idx, 1, new, XFS_DATA_FORK);
1911 ip->i_df.if_lastex = idx;
1914 if (oldlen != newlen) {
1915 ASSERT(oldlen > newlen);
1916 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
1917 (int)(oldlen - newlen), rsvd);
1919 * Nothing to do for disk quota accounting here.
1928 #undef STATE_SET_TEST
1933 * Called by xfs_bmap_add_extent to handle cases converting a hole
1934 * to a real allocation.
1936 STATIC int /* error */
1937 xfs_bmap_add_extent_hole_real(
1938 xfs_inode_t *ip, /* incore inode pointer */
1939 xfs_extnum_t idx, /* extent number to update/insert */
1940 xfs_btree_cur_t *cur, /* if null, not a btree */
1941 xfs_bmbt_irec_t *new, /* new data to put in extent list */
1942 int *logflagsp, /* inode logging flags */
1943 int whichfork) /* data or attr fork */
1945 xfs_bmbt_rec_t *ep; /* pointer to extent entry ins. point */
1946 int error; /* error return value */
1947 #ifdef XFS_BMAP_TRACE
1948 static char fname[] = "xfs_bmap_add_extent_hole_real";
1950 int i; /* temp state */
1951 xfs_ifork_t *ifp; /* inode fork pointer */
1952 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1953 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1954 int state; /* state bits, accessed thru macros */
1955 enum { /* bit number definitions for state */
1956 LEFT_CONTIG, RIGHT_CONTIG,
1957 LEFT_DELAY, RIGHT_DELAY,
1958 LEFT_VALID, RIGHT_VALID
1961 #define MASK(b) (1 << (b))
1962 #define MASK2(a,b) (MASK(a) | MASK(b))
1963 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1964 #define STATE_TEST(b) (state & MASK(b))
1965 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1966 ((state &= ~MASK(b)), 0))
1967 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1969 ifp = XFS_IFORK_PTR(ip, whichfork);
1970 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
1971 ep = &ifp->if_u1.if_extents[idx];
1974 * Check and set flags if this segment has a left neighbor.
1976 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1977 xfs_bmbt_get_all(ep - 1, &left);
1978 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1981 * Check and set flags if this segment has a current value.
1982 * Not true if we're inserting into the "hole" at eof.
1984 if (STATE_SET_TEST(RIGHT_VALID,
1986 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1987 xfs_bmbt_get_all(ep, &right);
1988 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1991 * We're inserting a real allocation between "left" and "right".
1992 * Set the contiguity flags. Don't let extents get too large.
1994 STATE_SET(LEFT_CONTIG,
1995 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1996 left.br_startoff + left.br_blockcount == new->br_startoff &&
1997 left.br_startblock + left.br_blockcount == new->br_startblock &&
1998 left.br_state == new->br_state &&
1999 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
2000 STATE_SET(RIGHT_CONTIG,
2001 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
2002 new->br_startoff + new->br_blockcount == right.br_startoff &&
2003 new->br_startblock + new->br_blockcount ==
2004 right.br_startblock &&
2005 new->br_state == right.br_state &&
2006 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2007 (!STATE_TEST(LEFT_CONTIG) ||
2008 left.br_blockcount + new->br_blockcount +
2009 right.br_blockcount <= MAXEXTLEN));
2012 * Select which case we're in here, and implement it.
2014 switch (SWITCH_STATE) {
2016 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
2018 * New allocation is contiguous with real allocations on the
2019 * left and on the right.
2020 * Merge all three into a single extent list entry.
2022 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
2024 xfs_bmbt_set_blockcount(ep - 1,
2025 left.br_blockcount + new->br_blockcount +
2026 right.br_blockcount);
2027 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
2029 xfs_bmap_trace_delete(fname, "LC|RC", ip,
2031 xfs_bmap_delete_exlist(ip, idx, 1, whichfork);
2032 ifp->if_lastex = idx - 1;
2033 XFS_IFORK_NEXT_SET(ip, whichfork,
2034 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2036 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2039 *logflagsp = XFS_ILOG_CORE;
2040 if ((error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
2041 right.br_startblock, right.br_blockcount, &i)))
2044 if ((error = xfs_bmbt_delete(cur, &i)))
2047 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2050 error = xfs_bmbt_update(cur, left.br_startoff,
2052 left.br_blockcount + new->br_blockcount +
2053 right.br_blockcount, left.br_state);
2056 case MASK(LEFT_CONTIG):
2058 * New allocation is contiguous with a real allocation
2060 * Merge the new allocation with the left neighbor.
2062 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1, whichfork);
2063 xfs_bmbt_set_blockcount(ep - 1,
2064 left.br_blockcount + new->br_blockcount);
2065 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1, whichfork);
2066 ifp->if_lastex = idx - 1;
2068 *logflagsp = XFS_ILOG_FEXT(whichfork);
2072 if ((error = xfs_bmbt_lookup_eq(cur, left.br_startoff,
2073 left.br_startblock, left.br_blockcount, &i)))
2076 error = xfs_bmbt_update(cur, left.br_startoff,
2078 left.br_blockcount + new->br_blockcount,
2082 case MASK(RIGHT_CONTIG):
2084 * New allocation is contiguous with a real allocation
2086 * Merge the new allocation with the right neighbor.
2088 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, whichfork);
2089 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2090 new->br_blockcount + right.br_blockcount,
2092 xfs_bmap_trace_post_update(fname, "RC", ip, idx, whichfork);
2093 ifp->if_lastex = idx;
2095 *logflagsp = XFS_ILOG_FEXT(whichfork);
2099 if ((error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
2100 right.br_startblock, right.br_blockcount, &i)))
2103 error = xfs_bmbt_update(cur, new->br_startoff,
2105 new->br_blockcount + right.br_blockcount,
2111 * New allocation is not contiguous with another
2113 * Insert a new entry.
2115 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
2117 xfs_bmap_insert_exlist(ip, idx, 1, new, whichfork);
2118 ifp->if_lastex = idx;
2119 XFS_IFORK_NEXT_SET(ip, whichfork,
2120 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2122 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2125 *logflagsp = XFS_ILOG_CORE;
2126 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2127 new->br_startblock, new->br_blockcount, &i)))
2130 cur->bc_rec.b.br_state = new->br_state;
2131 if ((error = xfs_bmbt_insert(cur, &i)))
2140 #undef STATE_SET_TEST
2144 return 0; /* keep gcc quite */
2147 #define XFS_ALLOC_GAP_UNITS 4
2150 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2151 * It figures out where to ask the underlying allocator to put the new extent.
2153 STATIC int /* error */
2155 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2157 xfs_fsblock_t adjust; /* adjustment to block numbers */
2158 xfs_alloctype_t atype=0; /* type for allocation routines */
2159 int error; /* error return value */
2160 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2161 xfs_mount_t *mp; /* mount point structure */
2162 int nullfb; /* true if ap->firstblock isn't set */
2163 int rt; /* true if inode is realtime */
2165 xfs_extlen_t prod=0; /* product factor for allocators */
2166 xfs_extlen_t ralen=0; /* realtime allocation length */
2169 #define ISVALID(x,y) \
2171 (x) < mp->m_sb.sb_rblocks : \
2172 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2173 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2174 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2179 mp = ap->ip->i_mount;
2180 nullfb = ap->firstblock == NULLFSBLOCK;
2181 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2182 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2185 xfs_extlen_t extsz; /* file extent size for rt */
2186 xfs_fileoff_t nexto; /* next file offset */
2187 xfs_extlen_t orig_alen; /* original ap->alen */
2188 xfs_fileoff_t orig_end; /* original off+len */
2189 xfs_fileoff_t orig_off; /* original ap->off */
2190 xfs_extlen_t mod_off; /* modulus calculations */
2191 xfs_fileoff_t prevo; /* previous file offset */
2192 xfs_rtblock_t rtx; /* realtime extent number */
2193 xfs_extlen_t temp; /* temp for rt calculations */
2196 * Set prod to match the realtime extent size.
2198 if (!(extsz = ap->ip->i_d.di_extsize))
2199 extsz = mp->m_sb.sb_rextsize;
2200 prod = extsz / mp->m_sb.sb_rextsize;
2202 orig_alen = ap->alen;
2203 orig_end = orig_off + orig_alen;
2205 * If the file offset is unaligned vs. the extent size
2206 * we need to align it. This will be possible unless
2207 * the file was previously written with a kernel that didn't
2208 * perform this alignment.
2210 mod_off = do_mod(orig_off, extsz);
2212 ap->alen += mod_off;
2216 * Same adjustment for the end of the requested area.
2218 if ((temp = (ap->alen % extsz)))
2219 ap->alen += extsz - temp;
2221 * If the previous block overlaps with this proposed allocation
2222 * then move the start forward without adjusting the length.
2225 ap->prevp->br_startoff == NULLFILEOFF ?
2227 (ap->prevp->br_startoff +
2228 ap->prevp->br_blockcount);
2229 if (ap->off != orig_off && ap->off < prevo)
2232 * If the next block overlaps with this proposed allocation
2233 * then move the start back without adjusting the length,
2234 * but not before offset 0.
2235 * This may of course make the start overlap previous block,
2236 * and if we hit the offset 0 limit then the next block
2237 * can still overlap too.
2239 nexto = (ap->eof || ap->gotp->br_startoff == NULLFILEOFF) ?
2240 NULLFILEOFF : ap->gotp->br_startoff;
2242 ap->off + ap->alen != orig_end &&
2243 ap->off + ap->alen > nexto)
2244 ap->off = nexto > ap->alen ? nexto - ap->alen : 0;
2246 * If we're now overlapping the next or previous extent that
2247 * means we can't fit an extsz piece in this hole. Just move
2248 * the start forward to the first valid spot and set
2249 * the length so we hit the end.
2251 if ((ap->off != orig_off && ap->off < prevo) ||
2252 (ap->off + ap->alen != orig_end &&
2253 ap->off + ap->alen > nexto)) {
2255 ap->alen = nexto - prevo;
2258 * If the result isn't a multiple of rtextents we need to
2259 * remove blocks until it is.
2261 if ((temp = (ap->alen % mp->m_sb.sb_rextsize))) {
2263 * We're not covering the original request, or
2264 * we won't be able to once we fix the length.
2266 if (orig_off < ap->off ||
2267 orig_end > ap->off + ap->alen ||
2268 ap->alen - temp < orig_alen)
2269 return XFS_ERROR(EINVAL);
2271 * Try to fix it by moving the start up.
2273 if (ap->off + temp <= orig_off) {
2278 * Try to fix it by moving the end in.
2280 else if (ap->off + ap->alen - temp >= orig_end)
2283 * Set the start to the minimum then trim the length.
2286 ap->alen -= orig_off - ap->off;
2288 ap->alen -= ap->alen % mp->m_sb.sb_rextsize;
2291 * Result doesn't cover the request, fail it.
2293 if (orig_off < ap->off || orig_end > ap->off + ap->alen)
2294 return XFS_ERROR(EINVAL);
2296 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2298 * If the offset & length are not perfectly aligned
2299 * then kill prod, it will just get us in trouble.
2301 if (do_mod(ap->off, extsz) || ap->alen % extsz)
2304 * Set ralen to be the actual requested length in rtextents.
2306 ralen = ap->alen / mp->m_sb.sb_rextsize;
2308 * If the old value was close enough to MAXEXTLEN that
2309 * we rounded up to it, cut it back so it's valid again.
2310 * Note that if it's a really large request (bigger than
2311 * MAXEXTLEN), we don't hear about that number, and can't
2312 * adjust the starting point to match it.
2314 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2315 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2317 * If it's an allocation to an empty file at offset 0,
2318 * pick an extent that will space things out in the rt area.
2320 if (ap->eof && ap->off == 0) {
2321 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2324 ap->rval = rtx * mp->m_sb.sb_rextsize;
2331 #endif /* __KERNEL__ */
2333 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2335 ap->rval = ap->firstblock;
2337 * If allocating at eof, and there's a previous real block,
2338 * try to use it's last block as our starting point.
2340 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2341 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2342 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2343 ap->prevp->br_startblock)) {
2344 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2346 * Adjust for the gap between prevp and us.
2349 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2351 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2355 * If not at eof, then compare the two neighbor blocks.
2356 * Figure out whether either one gives us a good starting point,
2357 * and pick the better one.
2359 else if (!ap->eof) {
2360 xfs_fsblock_t gotbno; /* right side block number */
2361 xfs_fsblock_t gotdiff=0; /* right side difference */
2362 xfs_fsblock_t prevbno; /* left side block number */
2363 xfs_fsblock_t prevdiff=0; /* left side difference */
2366 * If there's a previous (left) block, select a requested
2367 * start block based on it.
2369 if (ap->prevp->br_startoff != NULLFILEOFF &&
2370 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2371 (prevbno = ap->prevp->br_startblock +
2372 ap->prevp->br_blockcount) &&
2373 ISVALID(prevbno, ap->prevp->br_startblock)) {
2375 * Calculate gap to end of previous block.
2377 adjust = prevdiff = ap->off -
2378 (ap->prevp->br_startoff +
2379 ap->prevp->br_blockcount);
2381 * Figure the startblock based on the previous block's
2382 * end and the gap size.
2384 * If the gap is large relative to the piece we're
2385 * allocating, or using it gives us an invalid block
2386 * number, then just use the end of the previous block.
2388 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2389 ISVALID(prevbno + prevdiff,
2390 ap->prevp->br_startblock))
2395 * If the firstblock forbids it, can't use it,
2398 if (!rt && !nullfb &&
2399 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2400 prevbno = NULLFSBLOCK;
2403 * No previous block or can't follow it, just default.
2406 prevbno = NULLFSBLOCK;
2408 * If there's a following (right) block, select a requested
2409 * start block based on it.
2411 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
2413 * Calculate gap to start of next block.
2415 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2417 * Figure the startblock based on the next block's
2418 * start and the gap size.
2420 gotbno = ap->gotp->br_startblock;
2423 * If the gap is large relative to the piece we're
2424 * allocating, or using it gives us an invalid block
2425 * number, then just use the start of the next block
2426 * offset by our length.
2428 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2429 ISVALID(gotbno - gotdiff, gotbno))
2431 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2433 gotdiff += adjust - ap->alen;
2437 * If the firstblock forbids it, can't use it,
2440 if (!rt && !nullfb &&
2441 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2442 gotbno = NULLFSBLOCK;
2445 * No next block, just default.
2448 gotbno = NULLFSBLOCK;
2450 * If both valid, pick the better one, else the only good
2451 * one, else ap->rval is already set (to 0 or the inode block).
2453 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2454 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2455 else if (prevbno != NULLFSBLOCK)
2457 else if (gotbno != NULLFSBLOCK)
2461 * If allowed, use ap->rval; otherwise must use firstblock since
2462 * it's in the right allocation group.
2464 if (nullfb || rt || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2467 ap->rval = ap->firstblock;
2469 * Realtime allocation, done through xfs_rtallocate_extent.
2477 atype = ap->rval == 0 ?
2478 XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2479 do_div(ap->rval, mp->m_sb.sb_rextsize);
2482 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2483 &ralen, atype, ap->wasdel, prod, &rtb)))
2485 if (rtb == NULLFSBLOCK && prod > 1 &&
2486 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2487 ap->alen, &ralen, atype,
2488 ap->wasdel, 1, &rtb)))
2491 if (ap->rval != NULLFSBLOCK) {
2492 ap->rval *= mp->m_sb.sb_rextsize;
2493 ralen *= mp->m_sb.sb_rextsize;
2495 ap->ip->i_d.di_nblocks += ralen;
2496 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2498 ap->ip->i_delayed_blks -= ralen;
2500 * Adjust the disk quota also. This was reserved
2503 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2504 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2505 XFS_TRANS_DQ_RTBCOUNT,
2509 #endif /* __KERNEL__ */
2512 * Normal allocation, done through xfs_alloc_vextent.
2516 xfs_alloc_arg_t args;
2520 xfs_extlen_t longest;
2522 xfs_extlen_t nextminlen=0;
2525 xfs_agnumber_t startag;
2528 tryagain = isaligned = 0;
2531 args.fsbno = ap->rval;
2532 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2535 args.type = XFS_ALLOCTYPE_START_BNO;
2536 args.total = ap->total;
2538 * Find the longest available space.
2539 * We're going to try for the whole allocation at once.
2541 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2543 down_read(&mp->m_peraglock);
2544 while (blen < ap->alen) {
2545 pag = &mp->m_perag[ag];
2546 if (!pag->pagf_init &&
2547 (error = xfs_alloc_pagf_init(mp, args.tp,
2548 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2549 up_read(&mp->m_peraglock);
2553 * See xfs_alloc_fix_freelist...
2555 if (pag->pagf_init) {
2556 need = XFS_MIN_FREELIST_PAG(pag, mp);
2557 delta = need > pag->pagf_flcount ?
2558 need - pag->pagf_flcount : 0;
2559 longest = (pag->pagf_longest > delta) ?
2560 (pag->pagf_longest - delta) :
2561 (pag->pagf_flcount > 0 ||
2562 pag->pagf_longest > 0);
2567 if (++ag == mp->m_sb.sb_agcount)
2572 up_read(&mp->m_peraglock);
2574 * Since the above loop did a BUF_TRYLOCK, it is
2575 * possible that there is space for this request.
2577 if (notinit || blen < ap->minlen)
2578 args.minlen = ap->minlen;
2580 * If the best seen length is less than the request
2581 * length, use the best as the minimum.
2583 else if (blen < ap->alen)
2586 * Otherwise we've seen an extent as big as alen,
2587 * use that as the minimum.
2590 args.minlen = ap->alen;
2591 } else if (ap->low) {
2592 args.type = XFS_ALLOCTYPE_FIRST_AG;
2593 args.total = args.minlen = ap->minlen;
2595 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2596 args.total = ap->total;
2597 args.minlen = ap->minlen;
2599 if (ap->ip->i_d.di_extsize) {
2600 args.prod = ap->ip->i_d.di_extsize;
2601 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2602 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2603 } else if (mp->m_sb.sb_blocksize >= NBPP) {
2607 args.prod = NBPP >> mp->m_sb.sb_blocklog;
2608 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2609 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2612 * If we are not low on available data blocks, and the
2613 * underlying logical volume manager is a stripe, and
2614 * the file offset is zero then try to allocate data
2615 * blocks on stripe unit boundary.
2616 * NOTE: ap->aeof is only set if the allocation length
2617 * is >= the stripe unit and the allocation offset is
2618 * at the end of file.
2620 if (!ap->low && ap->aeof) {
2622 args.alignment = mp->m_dalign;
2626 * Adjust for alignment
2628 if (blen > args.alignment && blen <= ap->alen)
2629 args.minlen = blen - args.alignment;
2630 args.minalignslop = 0;
2633 * First try an exact bno allocation.
2634 * If it fails then do a near or start bno
2635 * allocation with alignment turned on.
2639 args.type = XFS_ALLOCTYPE_THIS_BNO;
2642 * Compute the minlen+alignment for the
2643 * next case. Set slop so that the value
2644 * of minlen+alignment+slop doesn't go up
2645 * between the calls.
2647 if (blen > mp->m_dalign && blen <= ap->alen)
2648 nextminlen = blen - mp->m_dalign;
2650 nextminlen = args.minlen;
2651 if (nextminlen + mp->m_dalign > args.minlen + 1)
2653 nextminlen + mp->m_dalign -
2656 args.minalignslop = 0;
2660 args.minalignslop = 0;
2662 args.minleft = ap->minleft;
2663 args.wasdel = ap->wasdel;
2665 args.userdata = ap->userdata;
2666 if ((error = xfs_alloc_vextent(&args)))
2668 if (tryagain && args.fsbno == NULLFSBLOCK) {
2670 * Exact allocation failed. Now try with alignment
2674 args.fsbno = ap->rval;
2675 args.alignment = mp->m_dalign;
2676 args.minlen = nextminlen;
2677 args.minalignslop = 0;
2679 if ((error = xfs_alloc_vextent(&args)))
2682 if (isaligned && args.fsbno == NULLFSBLOCK) {
2684 * allocation failed, so turn off alignment and
2688 args.fsbno = ap->rval;
2690 if ((error = xfs_alloc_vextent(&args)))
2693 if (args.fsbno == NULLFSBLOCK && nullfb &&
2694 args.minlen > ap->minlen) {
2695 args.minlen = ap->minlen;
2696 args.type = XFS_ALLOCTYPE_START_BNO;
2697 args.fsbno = ap->rval;
2698 if ((error = xfs_alloc_vextent(&args)))
2701 if (args.fsbno == NULLFSBLOCK && nullfb) {
2703 args.type = XFS_ALLOCTYPE_FIRST_AG;
2704 args.total = ap->minlen;
2706 if ((error = xfs_alloc_vextent(&args)))
2710 if (args.fsbno != NULLFSBLOCK) {
2711 ap->firstblock = ap->rval = args.fsbno;
2712 ASSERT(nullfb || fb_agno == args.agno ||
2713 (ap->low && fb_agno < args.agno));
2714 ap->alen = args.len;
2715 ap->ip->i_d.di_nblocks += args.len;
2716 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2718 ap->ip->i_delayed_blks -= args.len;
2720 * Adjust the disk quota also. This was reserved
2723 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2724 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2725 XFS_TRANS_DQ_BCOUNT,
2728 ap->rval = NULLFSBLOCK;
2737 * Transform a btree format file with only one leaf node, where the
2738 * extents list will fit in the inode, into an extents format file.
2739 * Since the extent list is already in-core, all we have to do is
2740 * give up the space for the btree root and pitch the leaf block.
2742 STATIC int /* error */
2743 xfs_bmap_btree_to_extents(
2744 xfs_trans_t *tp, /* transaction pointer */
2745 xfs_inode_t *ip, /* incore inode pointer */
2746 xfs_btree_cur_t *cur, /* btree cursor */
2747 int *logflagsp, /* inode logging flags */
2748 int whichfork) /* data or attr fork */
2751 xfs_bmbt_block_t *cblock;/* child btree block */
2752 xfs_fsblock_t cbno; /* child block number */
2753 xfs_buf_t *cbp; /* child block's buffer */
2754 int error; /* error return value */
2755 xfs_ifork_t *ifp; /* inode fork data */
2756 xfs_mount_t *mp; /* mount point structure */
2757 xfs_bmbt_ptr_t *pp; /* ptr to block address */
2758 xfs_bmbt_block_t *rblock;/* root btree block */
2760 ifp = XFS_IFORK_PTR(ip, whichfork);
2761 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2762 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2763 rblock = ifp->if_broot;
2764 ASSERT(INT_GET(rblock->bb_level, ARCH_CONVERT) == 1);
2765 ASSERT(INT_GET(rblock->bb_numrecs, ARCH_CONVERT) == 1);
2766 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
2768 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
2771 if ((error = xfs_btree_check_lptr(cur, INT_GET(*pp, ARCH_CONVERT), 1)))
2774 cbno = INT_GET(*pp, ARCH_CONVERT);
2775 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2776 XFS_BMAP_BTREE_REF)))
2778 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
2779 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
2781 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2782 ip->i_d.di_nblocks--;
2783 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
2784 xfs_trans_binval(tp, cbp);
2785 if (cur->bc_bufs[0] == cbp)
2786 cur->bc_bufs[0] = NULL;
2787 xfs_iroot_realloc(ip, -1, whichfork);
2788 ASSERT(ifp->if_broot == NULL);
2789 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2790 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2791 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2796 * Called by xfs_bmapi to update extent list structure and the btree
2797 * after removing space (or undoing a delayed allocation).
2799 STATIC int /* error */
2800 xfs_bmap_del_extent(
2801 xfs_inode_t *ip, /* incore inode pointer */
2802 xfs_trans_t *tp, /* current transaction pointer */
2803 xfs_extnum_t idx, /* extent number to update/delete */
2804 xfs_bmap_free_t *flist, /* list of extents to be freed */
2805 xfs_btree_cur_t *cur, /* if null, not a btree */
2806 xfs_bmbt_irec_t *del, /* data to remove from extent list */
2807 int *logflagsp, /* inode logging flags */
2808 int whichfork, /* data or attr fork */
2809 int rsvd) /* OK to allocate reserved blocks */
2811 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2812 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2813 xfs_fsblock_t del_endblock=0; /* first block past del */
2814 xfs_fileoff_t del_endoff; /* first offset past del */
2815 int delay; /* current block is delayed allocated */
2816 int do_fx; /* free extent at end of routine */
2817 xfs_bmbt_rec_t *ep; /* current extent entry pointer */
2818 int error; /* error return value */
2819 int flags; /* inode logging flags */
2820 #ifdef XFS_BMAP_TRACE
2821 static char fname[] = "xfs_bmap_del_extent";
2823 xfs_bmbt_irec_t got; /* current extent entry */
2824 xfs_fileoff_t got_endoff; /* first offset past got */
2825 int i; /* temp state */
2826 xfs_ifork_t *ifp; /* inode fork pointer */
2827 xfs_mount_t *mp; /* mount structure */
2828 xfs_filblks_t nblks; /* quota/sb block count */
2829 xfs_bmbt_irec_t new; /* new record to be inserted */
2831 xfs_extnum_t nextents; /* number of extents in list */
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 */
2836 XFS_STATS_INC(xs_del_exlist);
2838 ifp = XFS_IFORK_PTR(ip, whichfork);
2839 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2840 ASSERT(idx >= 0 && idx < nextents);
2841 ASSERT(del->br_blockcount > 0);
2842 ep = &ifp->if_u1.if_extents[idx];
2843 xfs_bmbt_get_all(ep, &got);
2844 ASSERT(got.br_startoff <= del->br_startoff);
2845 del_endoff = del->br_startoff + del->br_blockcount;
2846 got_endoff = got.br_startoff + got.br_blockcount;
2847 ASSERT(got_endoff >= del_endoff);
2848 delay = ISNULLSTARTBLOCK(got.br_startblock);
2849 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
2854 * If deleting a real allocation, must free up the disk space.
2857 flags = XFS_ILOG_CORE;
2859 * Realtime allocation. Free it and record di_nblocks update.
2861 if (whichfork == XFS_DATA_FORK &&
2862 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
2866 ASSERT(do_mod(del->br_blockcount,
2867 mp->m_sb.sb_rextsize) == 0);
2868 ASSERT(do_mod(del->br_startblock,
2869 mp->m_sb.sb_rextsize) == 0);
2870 bno = del->br_startblock;
2871 len = del->br_blockcount;
2872 do_div(bno, mp->m_sb.sb_rextsize);
2873 do_div(len, mp->m_sb.sb_rextsize);
2874 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
2875 (xfs_extlen_t)len)))
2878 nblks = len * mp->m_sb.sb_rextsize;
2879 qfield = XFS_TRANS_DQ_RTBCOUNT;
2882 * Ordinary allocation.
2886 nblks = del->br_blockcount;
2887 qfield = XFS_TRANS_DQ_BCOUNT;
2890 * Set up del_endblock and cur for later.
2892 del_endblock = del->br_startblock + del->br_blockcount;
2894 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2895 got.br_startblock, got.br_blockcount,
2900 da_old = da_new = 0;
2902 da_old = STARTBLOCKVAL(got.br_startblock);
2908 * Set flag value to use in switch statement.
2909 * Left-contig is 2, right-contig is 1.
2911 switch (((got.br_startoff == del->br_startoff) << 1) |
2912 (got_endoff == del_endoff)) {
2915 * Matches the whole extent. Delete the entry.
2917 xfs_bmap_trace_delete(fname, "3", ip, idx, 1, whichfork);
2918 xfs_bmap_delete_exlist(ip, idx, 1, whichfork);
2919 ifp->if_lastex = idx;
2922 XFS_IFORK_NEXT_SET(ip, whichfork,
2923 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2924 flags |= XFS_ILOG_CORE;
2926 flags |= XFS_ILOG_FEXT(whichfork);
2929 if ((error = xfs_bmbt_delete(cur, &i)))
2936 * Deleting the first part of the extent.
2938 xfs_bmap_trace_pre_update(fname, "2", ip, idx, whichfork);
2939 xfs_bmbt_set_startoff(ep, del_endoff);
2940 temp = got.br_blockcount - del->br_blockcount;
2941 xfs_bmbt_set_blockcount(ep, temp);
2942 ifp->if_lastex = idx;
2944 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2946 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
2947 xfs_bmap_trace_post_update(fname, "2", ip, idx,
2952 xfs_bmbt_set_startblock(ep, del_endblock);
2953 xfs_bmap_trace_post_update(fname, "2", ip, idx, whichfork);
2955 flags |= XFS_ILOG_FEXT(whichfork);
2958 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2959 got.br_blockcount - del->br_blockcount,
2966 * Deleting the last part of the extent.
2968 temp = got.br_blockcount - del->br_blockcount;
2969 xfs_bmap_trace_pre_update(fname, "1", ip, idx, whichfork);
2970 xfs_bmbt_set_blockcount(ep, temp);
2971 ifp->if_lastex = idx;
2973 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2975 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
2976 xfs_bmap_trace_post_update(fname, "1", ip, idx,
2981 xfs_bmap_trace_post_update(fname, "1", ip, idx, whichfork);
2983 flags |= XFS_ILOG_FEXT(whichfork);
2986 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2988 got.br_blockcount - del->br_blockcount,
2995 * Deleting the middle of the extent.
2997 temp = del->br_startoff - got.br_startoff;
2998 xfs_bmap_trace_pre_update(fname, "0", ip, idx, whichfork);
2999 xfs_bmbt_set_blockcount(ep, temp);
3000 new.br_startoff = del_endoff;
3001 temp2 = got_endoff - del_endoff;
3002 new.br_blockcount = temp2;
3003 new.br_state = got.br_state;
3005 new.br_startblock = del_endblock;
3006 flags |= XFS_ILOG_CORE;
3008 if ((error = xfs_bmbt_update(cur,
3010 got.br_startblock, temp,
3013 if ((error = xfs_bmbt_increment(cur, 0, &i)))
3015 cur->bc_rec.b = new;
3016 error = xfs_bmbt_insert(cur, &i);
3017 if (error && error != ENOSPC)
3020 * If get no-space back from btree insert,
3021 * it tried a split, and we have a zero
3022 * block reservation.
3023 * Fix up our state and return the error.
3025 if (error == ENOSPC) {
3027 * Reset the cursor, don't trust
3028 * it after any insert operation.
3030 if ((error = xfs_bmbt_lookup_eq(cur,
3037 * Update the btree record back
3038 * to the original value.
3040 if ((error = xfs_bmbt_update(cur,
3047 * Reset the extent record back
3048 * to the original value.
3050 xfs_bmbt_set_blockcount(ep,
3053 error = XFS_ERROR(ENOSPC);
3058 flags |= XFS_ILOG_FEXT(whichfork);
3059 XFS_IFORK_NEXT_SET(ip, whichfork,
3060 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3062 ASSERT(whichfork == XFS_DATA_FORK);
3063 temp = xfs_bmap_worst_indlen(ip, temp);
3064 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3065 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3066 new.br_startblock = NULLSTARTBLOCK((int)temp2);
3067 da_new = temp + temp2;
3068 while (da_new > da_old) {
3072 xfs_bmbt_set_startblock(ep,
3073 NULLSTARTBLOCK((int)temp));
3075 if (da_new == da_old)
3081 NULLSTARTBLOCK((int)temp2);
3085 xfs_bmap_trace_post_update(fname, "0", ip, idx, whichfork);
3086 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 1, &new, NULL,
3088 xfs_bmap_insert_exlist(ip, idx + 1, 1, &new, whichfork);
3089 ifp->if_lastex = idx + 1;
3093 * If we need to, add to list of extents to delete.
3096 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3099 * Adjust inode # blocks in the file.
3102 ip->i_d.di_nblocks -= nblks;
3104 * Adjust quota data.
3107 XFS_TRANS_MOD_DQUOT_BYINO(mp, 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, (int)(da_old - da_new),
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.
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 */
3133 prev->xbfi_next = free->xbfi_next;
3135 flist->xbf_first = free->xbfi_next;
3137 kmem_zone_free(xfs_bmap_free_item_zone, free);
3141 * Remove count entries from the extents array for inode "ip", starting
3142 * at index "idx". Copies the remaining items down over the deleted ones,
3143 * and gives back the excess memory.
3146 xfs_bmap_delete_exlist(
3147 xfs_inode_t *ip, /* incore inode pointer */
3148 xfs_extnum_t idx, /* starting delete index */
3149 xfs_extnum_t count, /* count of items to delete */
3150 int whichfork) /* data or attr fork */
3152 xfs_bmbt_rec_t *base; /* base of extent list */
3153 xfs_ifork_t *ifp; /* inode fork pointer */
3154 xfs_extnum_t nextents; /* number of extents in list after */
3156 ifp = XFS_IFORK_PTR(ip, whichfork);
3157 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3158 base = ifp->if_u1.if_extents;
3159 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - count;
3160 memmove(&base[idx], &base[idx + count],
3161 (nextents - idx) * sizeof(*base));
3162 xfs_iext_realloc(ip, -count, whichfork);
3166 * Convert an extents-format file into a btree-format file.
3167 * The new file will have a root block (in the inode) and a single child block.
3169 STATIC int /* error */
3170 xfs_bmap_extents_to_btree(
3171 xfs_trans_t *tp, /* transaction pointer */
3172 xfs_inode_t *ip, /* incore inode pointer */
3173 xfs_fsblock_t *firstblock, /* first-block-allocated */
3174 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3175 xfs_btree_cur_t **curp, /* cursor returned to caller */
3176 int wasdel, /* converting a delayed alloc */
3177 int *logflagsp, /* inode logging flags */
3178 int whichfork) /* data or attr fork */
3180 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
3181 xfs_buf_t *abp; /* buffer for ablock */
3182 xfs_alloc_arg_t args; /* allocation arguments */
3183 xfs_bmbt_rec_t *arp; /* child record pointer */
3184 xfs_bmbt_block_t *block; /* btree root block */
3185 xfs_btree_cur_t *cur; /* bmap btree cursor */
3186 xfs_bmbt_rec_t *ep; /* extent list pointer */
3187 int error; /* error return value */
3188 xfs_extnum_t i, cnt; /* extent list index */
3189 xfs_ifork_t *ifp; /* inode fork pointer */
3190 xfs_bmbt_key_t *kp; /* root block key pointer */
3191 xfs_mount_t *mp; /* mount structure */
3192 xfs_extnum_t nextents; /* extent list size */
3193 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3195 ifp = XFS_IFORK_PTR(ip, whichfork);
3196 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3197 ASSERT(ifp->if_ext_max ==
3198 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3200 * Make space in the inode incore.
3202 xfs_iroot_realloc(ip, 1, whichfork);
3203 ifp->if_flags |= XFS_IFBROOT;
3207 block = ifp->if_broot;
3208 INT_SET(block->bb_magic, ARCH_CONVERT, XFS_BMAP_MAGIC);
3209 INT_SET(block->bb_level, ARCH_CONVERT, 1);
3210 INT_SET(block->bb_numrecs, ARCH_CONVERT, 1);
3211 INT_SET(block->bb_leftsib, ARCH_CONVERT, NULLDFSBNO);
3212 INT_SET(block->bb_rightsib, ARCH_CONVERT, NULLDFSBNO);
3214 * Need a cursor. Can't allocate until bb_level is filled in.
3217 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
3219 cur->bc_private.b.firstblock = *firstblock;
3220 cur->bc_private.b.flist = flist;
3221 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3223 * Convert to a btree with two levels, one record in root.
3225 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3228 if (*firstblock == NULLFSBLOCK) {
3229 args.type = XFS_ALLOCTYPE_START_BNO;
3230 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3231 } else if (flist->xbf_low) {
3232 args.type = XFS_ALLOCTYPE_START_BNO;
3233 args.fsbno = *firstblock;
3235 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3236 args.fsbno = *firstblock;
3238 args.minlen = args.maxlen = args.prod = 1;
3239 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3240 args.minalignslop = 0;
3241 args.wasdel = wasdel;
3243 if ((error = xfs_alloc_vextent(&args))) {
3244 xfs_iroot_realloc(ip, -1, whichfork);
3245 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3249 * Allocation can't fail, the space was reserved.
3251 ASSERT(args.fsbno != NULLFSBLOCK);
3252 ASSERT(*firstblock == NULLFSBLOCK ||
3253 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3255 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3256 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3257 cur->bc_private.b.allocated++;
3258 ip->i_d.di_nblocks++;
3259 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3260 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3262 * Fill in the child block.
3264 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
3265 INT_SET(ablock->bb_magic, ARCH_CONVERT, XFS_BMAP_MAGIC);
3266 INT_ZERO(ablock->bb_level, ARCH_CONVERT);
3267 INT_SET(ablock->bb_leftsib, ARCH_CONVERT, NULLDFSBNO);
3268 INT_SET(ablock->bb_rightsib, ARCH_CONVERT, NULLDFSBNO);
3269 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3270 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3271 for (ep = ifp->if_u1.if_extents, cnt = i = 0; i < nextents; i++, ep++) {
3272 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
3273 arp->l0 = INT_GET(ep->l0, ARCH_CONVERT);
3274 arp->l1 = INT_GET(ep->l1, ARCH_CONVERT);
3278 INT_SET(ablock->bb_numrecs, ARCH_CONVERT, cnt);
3279 ASSERT(INT_GET(ablock->bb_numrecs, ARCH_CONVERT) == XFS_IFORK_NEXTENTS(ip, whichfork));
3281 * Fill in the root key and pointer.
3283 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
3284 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3285 INT_SET(kp->br_startoff, ARCH_CONVERT, xfs_bmbt_disk_get_startoff(arp));
3286 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
3287 INT_SET(*pp, ARCH_CONVERT, args.fsbno);
3289 * Do all this logging at the end so that
3290 * the root is at the right level.
3292 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
3293 xfs_bmbt_log_recs(cur, abp, 1, INT_GET(ablock->bb_numrecs, ARCH_CONVERT));
3294 ASSERT(*curp == NULL);
3296 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
3301 * Insert new item(s) in the extent list for inode "ip".
3302 * Count new items are inserted at offset idx.
3305 xfs_bmap_insert_exlist(
3306 xfs_inode_t *ip, /* incore inode pointer */
3307 xfs_extnum_t idx, /* starting index of new items */
3308 xfs_extnum_t count, /* number of inserted items */
3309 xfs_bmbt_irec_t *new, /* items to insert */
3310 int whichfork) /* data or attr fork */
3312 xfs_bmbt_rec_t *base; /* extent list base */
3313 xfs_ifork_t *ifp; /* inode fork pointer */
3314 xfs_extnum_t nextents; /* extent list size */
3315 xfs_extnum_t to; /* extent list index */
3317 ifp = XFS_IFORK_PTR(ip, whichfork);
3318 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3319 xfs_iext_realloc(ip, count, whichfork);
3320 base = ifp->if_u1.if_extents;
3321 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3322 memmove(&base[idx + count], &base[idx],
3323 (nextents - (idx + count)) * sizeof(*base));
3324 for (to = idx; to < idx + count; to++, new++)
3325 xfs_bmbt_set_all(&base[to], new);
3329 * Convert a local file to an extents file.
3330 * This code is out of bounds for data forks of regular files,
3331 * since the file data needs to get logged so things will stay consistent.
3332 * (The bmap-level manipulations are ok, though).
3334 STATIC int /* error */
3335 xfs_bmap_local_to_extents(
3336 xfs_trans_t *tp, /* transaction pointer */
3337 xfs_inode_t *ip, /* incore inode pointer */
3338 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3339 xfs_extlen_t total, /* total blocks needed by transaction */
3340 int *logflagsp, /* inode logging flags */
3341 int whichfork) /* data or attr fork */
3343 int error; /* error return value */
3344 int flags; /* logging flags returned */
3345 #ifdef XFS_BMAP_TRACE
3346 static char fname[] = "xfs_bmap_local_to_extents";
3348 xfs_ifork_t *ifp; /* inode fork pointer */
3351 * We don't want to deal with the case of keeping inode data inline yet.
3352 * So sending the data fork of a regular inode is invalid.
3354 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3355 whichfork == XFS_DATA_FORK));
3356 ifp = XFS_IFORK_PTR(ip, whichfork);
3357 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3360 if (ifp->if_bytes) {
3361 xfs_alloc_arg_t args; /* allocation arguments */
3362 xfs_buf_t *bp; /* buffer for extent list block */
3363 xfs_bmbt_rec_t *ep; /* extent list pointer */
3366 args.mp = ip->i_mount;
3367 ASSERT(ifp->if_flags & 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;
3376 args.fsbno = *firstblock;
3377 args.type = XFS_ALLOCTYPE_NEAR_BNO;
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)))
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,
3394 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3395 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3396 xfs_iext_realloc(ip, 1, whichfork);
3397 ep = ifp->if_u1.if_extents;
3398 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3399 xfs_bmap_trace_post_update(fname, "new", ip, 0, whichfork);
3400 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3401 ip->i_d.di_nblocks = 1;
3402 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
3403 XFS_TRANS_DQ_BCOUNT, 1L);
3404 flags |= XFS_ILOG_FEXT(whichfork);
3406 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3407 ifp->if_flags &= ~XFS_IFINLINE;
3408 ifp->if_flags |= XFS_IFEXTENTS;
3409 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3410 flags |= XFS_ILOG_CORE;
3416 xfs_bmbt_rec_t * /* pointer to found extent entry */
3417 xfs_bmap_do_search_extents(
3418 xfs_bmbt_rec_t *base, /* base of extent list */
3419 xfs_extnum_t lastx, /* last extent index used */
3420 xfs_extnum_t nextents, /* extent list size */
3421 xfs_fileoff_t bno, /* block number searched for */
3422 int *eofp, /* out: end of file found */
3423 xfs_extnum_t *lastxp, /* out: last extent index */
3424 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3425 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3427 xfs_bmbt_rec_t *ep; /* extent list entry pointer */
3428 xfs_bmbt_irec_t got; /* extent list entry, decoded */
3429 int high; /* high index of binary search */
3430 int low; /* low index of binary search */
3432 if (lastx != NULLEXTNUM && lastx < nextents)
3436 prevp->br_startoff = NULLFILEOFF;
3437 if (ep && bno >= (got.br_startoff = xfs_bmbt_get_startoff(ep)) &&
3438 bno < got.br_startoff +
3439 (got.br_blockcount = xfs_bmbt_get_blockcount(ep)))
3441 else if (ep && lastx < nextents - 1 &&
3442 bno >= (got.br_startoff = xfs_bmbt_get_startoff(ep + 1)) &&
3443 bno < got.br_startoff +
3444 (got.br_blockcount = xfs_bmbt_get_blockcount(ep + 1))) {
3448 } else if (nextents == 0)
3450 else if (bno == 0 &&
3451 (got.br_startoff = xfs_bmbt_get_startoff(base)) == 0) {
3454 got.br_blockcount = xfs_bmbt_get_blockcount(ep);
3457 /* binary search the extents array */
3459 high = nextents - 1;
3460 while (low <= high) {
3461 XFS_STATS_INC(xs_cmp_exlist);
3462 lastx = (low + high) >> 1;
3464 got.br_startoff = xfs_bmbt_get_startoff(ep);
3465 got.br_blockcount = xfs_bmbt_get_blockcount(ep);
3466 if (bno < got.br_startoff)
3468 else if (bno >= got.br_startoff + got.br_blockcount)
3471 got.br_startblock = xfs_bmbt_get_startblock(ep);
3472 got.br_state = xfs_bmbt_get_state(ep);
3479 if (bno >= got.br_startoff + got.br_blockcount) {
3481 if (lastx == nextents) {
3483 got.br_startblock = xfs_bmbt_get_startblock(ep);
3484 got.br_state = xfs_bmbt_get_state(ep);
3489 xfs_bmbt_get_all(ep, prevp);
3491 got.br_startoff = xfs_bmbt_get_startoff(ep);
3492 got.br_blockcount = xfs_bmbt_get_blockcount(ep);
3497 xfs_bmbt_get_all(ep - 1, prevp);
3501 got.br_startblock = xfs_bmbt_get_startblock(ep);
3502 got.br_state = xfs_bmbt_get_state(ep);
3510 * Search the extents list for the inode, for the extent containing bno.
3511 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3512 * *eofp will be set, and *prevp will contain the last entry (null if none).
3513 * Else, *lastxp will be set to the index of the found
3514 * entry; *gotp will contain the entry.
3516 STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
3517 xfs_bmap_search_extents(
3518 xfs_inode_t *ip, /* incore inode pointer */
3519 xfs_fileoff_t bno, /* block number searched for */
3520 int whichfork, /* data or attr fork */
3521 int *eofp, /* out: end of file found */
3522 xfs_extnum_t *lastxp, /* out: last extent index */
3523 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3524 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3526 xfs_ifork_t *ifp; /* inode fork pointer */
3527 xfs_bmbt_rec_t *base; /* base of extent list */
3528 xfs_extnum_t lastx; /* last extent index used */
3529 xfs_extnum_t nextents; /* extent list size */
3531 XFS_STATS_INC(xs_look_exlist);
3532 ifp = XFS_IFORK_PTR(ip, whichfork);
3533 lastx = ifp->if_lastex;
3534 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3535 base = &ifp->if_u1.if_extents[0];
3537 return xfs_bmap_do_search_extents(base, lastx, nextents, bno, eofp,
3538 lastxp, gotp, prevp);
3542 #ifdef XFS_BMAP_TRACE
3543 ktrace_t *xfs_bmap_trace_buf;
3546 * Add a bmap trace buffer entry. Base routine for the others.
3549 xfs_bmap_trace_addentry(
3550 int opcode, /* operation */
3551 char *fname, /* function name */
3552 char *desc, /* operation description */
3553 xfs_inode_t *ip, /* incore inode pointer */
3554 xfs_extnum_t idx, /* index of entry(ies) */
3555 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
3556 xfs_bmbt_rec_t *r1, /* first record */
3557 xfs_bmbt_rec_t *r2, /* second record or null */
3558 int whichfork) /* data or attr fork */
3562 ASSERT(cnt == 1 || cnt == 2);
3567 memset(&tr2, 0, sizeof(tr2));
3570 ktrace_enter(xfs_bmap_trace_buf,
3571 (void *)(__psint_t)(opcode | (whichfork << 16)),
3572 (void *)fname, (void *)desc, (void *)ip,
3573 (void *)(__psint_t)idx,
3574 (void *)(__psint_t)cnt,
3575 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3576 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3577 (void *)(__psunsigned_t)(r1->l0 >> 32),
3578 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3579 (void *)(__psunsigned_t)(r1->l1 >> 32),
3580 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3581 (void *)(__psunsigned_t)(r2->l0 >> 32),
3582 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3583 (void *)(__psunsigned_t)(r2->l1 >> 32),
3584 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3586 ASSERT(ip->i_xtrace);
3587 ktrace_enter(ip->i_xtrace,
3588 (void *)(__psint_t)(opcode | (whichfork << 16)),
3589 (void *)fname, (void *)desc, (void *)ip,
3590 (void *)(__psint_t)idx,
3591 (void *)(__psint_t)cnt,
3592 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3593 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3594 (void *)(__psunsigned_t)(r1->l0 >> 32),
3595 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3596 (void *)(__psunsigned_t)(r1->l1 >> 32),
3597 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3598 (void *)(__psunsigned_t)(r2->l0 >> 32),
3599 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3600 (void *)(__psunsigned_t)(r2->l1 >> 32),
3601 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3606 * Add bmap trace entry prior to a call to xfs_bmap_delete_exlist.
3609 xfs_bmap_trace_delete(
3610 char *fname, /* function name */
3611 char *desc, /* operation description */
3612 xfs_inode_t *ip, /* incore inode pointer */
3613 xfs_extnum_t idx, /* index of entry(entries) deleted */
3614 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3615 int whichfork) /* data or attr fork */
3617 xfs_ifork_t *ifp; /* inode fork pointer */
3619 ifp = XFS_IFORK_PTR(ip, whichfork);
3620 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
3621 cnt, &ifp->if_u1.if_extents[idx],
3622 cnt == 2 ? &ifp->if_u1.if_extents[idx + 1] : NULL,
3627 * Add bmap trace entry prior to a call to xfs_bmap_insert_exlist, or
3628 * reading in the extents list from the disk (in the btree).
3631 xfs_bmap_trace_insert(
3632 char *fname, /* function name */
3633 char *desc, /* operation description */
3634 xfs_inode_t *ip, /* incore inode pointer */
3635 xfs_extnum_t idx, /* index of entry(entries) inserted */
3636 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3637 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3638 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3639 int whichfork) /* data or attr fork */
3641 xfs_bmbt_rec_t tr1; /* compressed record 1 */
3642 xfs_bmbt_rec_t tr2; /* compressed record 2 if needed */
3644 xfs_bmbt_set_all(&tr1, r1);
3647 xfs_bmbt_set_all(&tr2, r2);
3652 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3653 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3657 * Add bmap trace entry after updating an extent list entry in place.
3660 xfs_bmap_trace_post_update(
3661 char *fname, /* function name */
3662 char *desc, /* operation description */
3663 xfs_inode_t *ip, /* incore inode pointer */
3664 xfs_extnum_t idx, /* index of entry updated */
3665 int whichfork) /* data or attr fork */
3667 xfs_ifork_t *ifp; /* inode fork pointer */
3669 ifp = XFS_IFORK_PTR(ip, whichfork);
3670 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
3671 1, &ifp->if_u1.if_extents[idx], NULL, whichfork);
3675 * Add bmap trace entry prior to updating an extent list entry in place.
3678 xfs_bmap_trace_pre_update(
3679 char *fname, /* function name */
3680 char *desc, /* operation description */
3681 xfs_inode_t *ip, /* incore inode pointer */
3682 xfs_extnum_t idx, /* index of entry to be updated */
3683 int whichfork) /* data or attr fork */
3685 xfs_ifork_t *ifp; /* inode fork pointer */
3687 ifp = XFS_IFORK_PTR(ip, whichfork);
3688 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
3689 &ifp->if_u1.if_extents[idx], NULL, whichfork);
3691 #endif /* XFS_BMAP_TRACE */
3694 * Compute the worst-case number of indirect blocks that will be used
3695 * for ip's delayed extent of length "len".
3697 STATIC xfs_filblks_t
3698 xfs_bmap_worst_indlen(
3699 xfs_inode_t *ip, /* incore inode pointer */
3700 xfs_filblks_t len) /* delayed extent length */
3702 int level; /* btree level number */
3703 int maxrecs; /* maximum record count at this level */
3704 xfs_mount_t *mp; /* mount structure */
3705 xfs_filblks_t rval; /* return value */
3708 maxrecs = mp->m_bmap_dmxr[0];
3709 for (level = 0, rval = 0;
3710 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3713 do_div(len, maxrecs);
3716 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3719 maxrecs = mp->m_bmap_dmxr[1];
3724 #if defined(XFS_RW_TRACE)
3733 if (ip->i_rwtrace == NULL)
3735 ktrace_enter(ip->i_rwtrace,
3736 (void *)(__psint_t)XFS_BUNMAPI,
3738 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3739 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3740 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3741 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3742 (void *)(__psint_t)len,
3743 (void *)(__psint_t)flags,
3744 (void *)(unsigned long)current_cpu(),
3756 * Convert inode from non-attributed to attributed.
3757 * Must not be in a transaction, ip must not be locked.
3759 int /* error code */
3760 xfs_bmap_add_attrfork(
3761 xfs_inode_t *ip, /* incore inode pointer */
3762 int rsvd) /* OK to allocated reserved blocks in trans */
3764 int blks; /* space reservation */
3765 int committed; /* xaction was committed */
3766 int error; /* error return value */
3767 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3768 xfs_bmap_free_t flist; /* freed extent list */
3769 int logflags; /* logging flags */
3770 xfs_mount_t *mp; /* mount structure */
3771 unsigned long s; /* spinlock spl value */
3772 xfs_trans_t *tp; /* transaction pointer */
3774 ASSERT(ip->i_df.if_ext_max ==
3775 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3776 if (XFS_IFORK_Q(ip))
3779 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3780 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3781 blks = XFS_ADDAFORK_SPACE_RES(mp);
3783 tp->t_flags |= XFS_TRANS_RESERVE;
3784 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3785 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3787 xfs_ilock(ip, XFS_ILOCK_EXCL);
3788 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
3789 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3790 XFS_QMOPT_RES_REGBLKS);
3792 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3793 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3796 if (XFS_IFORK_Q(ip))
3798 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3800 * For inodes coming from pre-6.2 filesystems.
3802 ASSERT(ip->i_d.di_aformat == 0);
3803 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3805 ASSERT(ip->i_d.di_anextents == 0);
3806 VN_HOLD(XFS_ITOV(ip));
3807 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3808 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3809 switch (ip->i_d.di_format) {
3810 case XFS_DINODE_FMT_DEV:
3811 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3813 case XFS_DINODE_FMT_UUID:
3814 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3816 case XFS_DINODE_FMT_LOCAL:
3817 case XFS_DINODE_FMT_EXTENTS:
3818 case XFS_DINODE_FMT_BTREE:
3819 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3823 error = XFS_ERROR(EINVAL);
3826 ip->i_df.if_ext_max =
3827 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3828 ASSERT(ip->i_afp == NULL);
3829 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3830 ip->i_afp->if_ext_max =
3831 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3832 ip->i_afp->if_flags = XFS_IFEXTENTS;
3834 XFS_BMAP_INIT(&flist, &firstblock);
3835 switch (ip->i_d.di_format) {
3836 case XFS_DINODE_FMT_LOCAL:
3837 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3840 case XFS_DINODE_FMT_EXTENTS:
3841 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3844 case XFS_DINODE_FMT_BTREE:
3845 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3853 xfs_trans_log_inode(tp, ip, logflags);
3856 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) {
3857 s = XFS_SB_LOCK(mp);
3858 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) {
3859 XFS_SB_VERSION_ADDATTR(&mp->m_sb);
3860 XFS_SB_UNLOCK(mp, s);
3861 xfs_mod_sb(tp, XFS_SB_VERSIONNUM);
3863 XFS_SB_UNLOCK(mp, s);
3865 if ((error = xfs_bmap_finish(&tp, &flist, firstblock, &committed)))
3867 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES, NULL);
3868 ASSERT(ip->i_df.if_ext_max ==
3869 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3872 xfs_bmap_cancel(&flist);
3874 ASSERT(ismrlocked(&ip->i_lock,MR_UPDATE));
3875 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3877 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3878 ASSERT(ip->i_df.if_ext_max ==
3879 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3884 * Add the extent to the list of extents to be free at transaction end.
3885 * The list is maintained sorted (by block number).
3890 xfs_fsblock_t bno, /* fs block number of extent */
3891 xfs_filblks_t len, /* length of extent */
3892 xfs_bmap_free_t *flist, /* list of extents */
3893 xfs_mount_t *mp) /* mount point structure */
3895 xfs_bmap_free_item_t *cur; /* current (next) element */
3896 xfs_bmap_free_item_t *new; /* new element */
3897 xfs_bmap_free_item_t *prev; /* previous element */
3899 xfs_agnumber_t agno;
3900 xfs_agblock_t agbno;
3902 ASSERT(bno != NULLFSBLOCK);
3904 ASSERT(len <= MAXEXTLEN);
3905 ASSERT(!ISNULLSTARTBLOCK(bno));
3906 agno = XFS_FSB_TO_AGNO(mp, bno);
3907 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3908 ASSERT(agno < mp->m_sb.sb_agcount);
3909 ASSERT(agbno < mp->m_sb.sb_agblocks);
3910 ASSERT(len < mp->m_sb.sb_agblocks);
3911 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3913 ASSERT(xfs_bmap_free_item_zone != NULL);
3914 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3915 new->xbfi_startblock = bno;
3916 new->xbfi_blockcount = (xfs_extlen_t)len;
3917 for (prev = NULL, cur = flist->xbf_first;
3919 prev = cur, cur = cur->xbfi_next) {
3920 if (cur->xbfi_startblock >= bno)
3924 prev->xbfi_next = new;
3926 flist->xbf_first = new;
3927 new->xbfi_next = cur;
3932 * Compute and fill in the value of the maximum depth of a bmap btree
3933 * in this filesystem. Done once, during mount.
3936 xfs_bmap_compute_maxlevels(
3937 xfs_mount_t *mp, /* file system mount structure */
3938 int whichfork) /* data or attr fork */
3940 int level; /* btree level */
3941 uint maxblocks; /* max blocks at this level */
3942 uint maxleafents; /* max leaf entries possible */
3943 int maxrootrecs; /* max records in root block */
3944 int minleafrecs; /* min records in leaf block */
3945 int minnoderecs; /* min records in node block */
3946 int sz; /* root block size */
3949 * The maximum number of extents in a file, hence the maximum
3950 * number of leaf entries, is controlled by the type of di_nextents
3951 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3952 * (a signed 16-bit number, xfs_aextnum_t).
3954 maxleafents = (whichfork == XFS_DATA_FORK) ? MAXEXTNUM : MAXAEXTNUM;
3955 minleafrecs = mp->m_bmap_dmnr[0];
3956 minnoderecs = mp->m_bmap_dmnr[1];
3957 sz = (whichfork == XFS_DATA_FORK) ?
3959 mp->m_sb.sb_inodesize - mp->m_attroffset;
3960 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
3961 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3962 for (level = 1; maxblocks > 1; level++) {
3963 if (maxblocks <= maxrootrecs)
3966 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3968 mp->m_bm_maxlevels[whichfork] = level;
3972 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3973 * caller. Frees all the extents that need freeing, which must be done
3974 * last due to locking considerations. We never free any extents in
3975 * the first transaction. This is to allow the caller to make the first
3976 * transaction a synchronous one so that the pointers to the data being
3977 * broken in this transaction will be permanent before the data is actually
3978 * freed. This is necessary to prevent blocks from being reallocated
3979 * and written to before the free and reallocation are actually permanent.
3980 * We do not just make the first transaction synchronous here, because
3981 * there are more efficient ways to gain the same protection in some cases
3982 * (see the file truncation code).
3984 * Return 1 if the given transaction was committed and a new one
3985 * started, and 0 otherwise in the committed parameter.
3990 xfs_trans_t **tp, /* transaction pointer addr */
3991 xfs_bmap_free_t *flist, /* i/o: list extents to free */
3992 xfs_fsblock_t firstblock, /* controlled ag for allocs */
3993 int *committed) /* xact committed or not */
3995 xfs_efd_log_item_t *efd; /* extent free data */
3996 xfs_efi_log_item_t *efi; /* extent free intention */
3997 int error; /* error return value */
3998 xfs_bmap_free_item_t *free; /* free extent list item */
3999 unsigned int logres; /* new log reservation */
4000 unsigned int logcount; /* new log count */
4001 xfs_mount_t *mp; /* filesystem mount structure */
4002 xfs_bmap_free_item_t *next; /* next item on free list */
4003 xfs_trans_t *ntp; /* new transaction pointer */
4005 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4006 if (flist->xbf_count == 0) {
4011 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4012 for (free = flist->xbf_first; free; free = free->xbfi_next)
4013 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4014 free->xbfi_blockcount);
4015 logres = ntp->t_log_res;
4016 logcount = ntp->t_log_count;
4017 ntp = xfs_trans_dup(*tp);
4018 error = xfs_trans_commit(*tp, 0, NULL);
4022 * We have a new transaction, so we should return committed=1,
4023 * even though we're returning an error.
4028 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4031 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4032 for (free = flist->xbf_first; free != NULL; free = next) {
4033 next = free->xbfi_next;
4034 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4035 free->xbfi_blockcount))) {
4037 * The bmap free list will be cleaned up at a
4038 * higher level. The EFI will be canceled when
4039 * this transaction is aborted.
4040 * Need to force shutdown here to make sure it
4041 * happens, since this transaction may not be
4045 if (!XFS_FORCED_SHUTDOWN(mp))
4046 xfs_force_shutdown(mp,
4047 (error == EFSCORRUPTED) ?
4048 XFS_CORRUPT_INCORE :
4049 XFS_METADATA_IO_ERROR);
4052 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4053 free->xbfi_blockcount);
4054 xfs_bmap_del_free(flist, NULL, free);
4060 * Free up any items left in the list.
4064 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4066 xfs_bmap_free_item_t *free; /* free list item */
4067 xfs_bmap_free_item_t *next;
4069 if (flist->xbf_count == 0)
4071 ASSERT(flist->xbf_first != NULL);
4072 for (free = flist->xbf_first; free; free = next) {
4073 next = free->xbfi_next;
4074 xfs_bmap_del_free(flist, NULL, free);
4076 ASSERT(flist->xbf_count == 0);
4080 * Returns EINVAL if the specified file is not swappable.
4083 xfs_bmap_check_swappable(
4084 xfs_inode_t *ip) /* incore inode */
4086 xfs_bmbt_rec_t *base; /* base of extent array */
4087 xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
4088 xfs_fileoff_t end_fsb; /* last block of file within size */
4089 xfs_bmbt_irec_t ext; /* extent list entry, decoded */
4090 xfs_ifork_t *ifp; /* inode fork pointer */
4091 xfs_fileoff_t lastaddr; /* last block number seen */
4092 xfs_extnum_t nextents; /* number of extent entries */
4093 int retval = 0; /* return value */
4095 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
4098 * Check for a zero length file.
4100 if (ip->i_d.di_size == 0)
4103 ASSERT(XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) == XFS_DINODE_FMT_BTREE ||
4104 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) == XFS_DINODE_FMT_EXTENTS);
4107 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4108 (retval = xfs_iread_extents(NULL, ip, XFS_DATA_FORK)))
4111 * Scan extents until the file size is reached. Look for
4112 * holes or unwritten extents, since I/O to these would cause
4115 end_fsb = XFS_B_TO_FSB(ip->i_mount, ip->i_d.di_size);
4116 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4117 base = &ifp->if_u1.if_extents[0];
4118 for (lastaddr = 0, ep = base; ep < &base[nextents]; ep++) {
4119 xfs_bmbt_get_all(ep, &ext);
4120 if (lastaddr < ext.br_startoff ||
4121 ext.br_state != XFS_EXT_NORM) {
4124 if (end_fsb <= (lastaddr = ext.br_startoff +
4129 retval = XFS_ERROR(EINVAL);
4133 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
4138 * Returns the file-relative block number of the first unused block(s)
4139 * in the file with at least "len" logically contiguous blocks free.
4140 * This is the lowest-address hole if the file has holes, else the first block
4141 * past the end of file.
4142 * Return 0 if the file is currently local (in-inode).
4145 xfs_bmap_first_unused(
4146 xfs_trans_t *tp, /* transaction pointer */
4147 xfs_inode_t *ip, /* incore inode */
4148 xfs_extlen_t len, /* size of hole to find */
4149 xfs_fileoff_t *first_unused, /* unused block */
4150 int whichfork) /* data or attr fork */
4152 xfs_bmbt_rec_t *base; /* base of extent array */
4153 xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
4154 int error; /* error return value */
4155 xfs_ifork_t *ifp; /* inode fork pointer */
4156 xfs_fileoff_t lastaddr; /* last block number seen */
4157 xfs_fileoff_t lowest; /* lowest useful block */
4158 xfs_fileoff_t max; /* starting useful block */
4159 xfs_fileoff_t off; /* offset for this block */
4160 xfs_extnum_t nextents; /* number of extent entries */
4162 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4163 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4164 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4165 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4169 ifp = XFS_IFORK_PTR(ip, whichfork);
4170 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4171 (error = xfs_iread_extents(tp, ip, whichfork)))
4173 lowest = *first_unused;
4174 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4175 base = &ifp->if_u1.if_extents[0];
4176 for (lastaddr = 0, max = lowest, ep = base;
4177 ep < &base[nextents];
4179 off = xfs_bmbt_get_startoff(ep);
4181 * See if the hole before this extent will work.
4183 if (off >= lowest + len && off - max >= len) {
4184 *first_unused = max;
4187 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4188 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4190 *first_unused = max;
4195 * Returns the file-relative block number of the last block + 1 before
4196 * last_block (input value) in the file.
4197 * This is not based on i_size, it is based on the extent list.
4198 * Returns 0 for local files, as they do not have an extent list.
4201 xfs_bmap_last_before(
4202 xfs_trans_t *tp, /* transaction pointer */
4203 xfs_inode_t *ip, /* incore inode */
4204 xfs_fileoff_t *last_block, /* last block */
4205 int whichfork) /* data or attr fork */
4207 xfs_fileoff_t bno; /* input file offset */
4208 int eof; /* hit end of file */
4209 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4210 int error; /* error return value */
4211 xfs_bmbt_irec_t got; /* current extent value */
4212 xfs_ifork_t *ifp; /* inode fork pointer */
4213 xfs_extnum_t lastx; /* last extent used */
4214 xfs_bmbt_irec_t prev; /* previous extent value */
4216 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4217 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4218 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4219 return XFS_ERROR(EIO);
4220 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4224 ifp = XFS_IFORK_PTR(ip, whichfork);
4225 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4226 (error = xfs_iread_extents(tp, ip, whichfork)))
4228 bno = *last_block - 1;
4229 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4231 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4232 if (prev.br_startoff == NULLFILEOFF)
4235 *last_block = prev.br_startoff + prev.br_blockcount;
4238 * Otherwise *last_block is already the right answer.
4244 * Returns the file-relative block number of the first block past eof in
4245 * the file. This is not based on i_size, it is based on the extent list.
4246 * Returns 0 for local files, as they do not have an extent list.
4249 xfs_bmap_last_offset(
4250 xfs_trans_t *tp, /* transaction pointer */
4251 xfs_inode_t *ip, /* incore inode */
4252 xfs_fileoff_t *last_block, /* last block */
4253 int whichfork) /* data or attr fork */
4255 xfs_bmbt_rec_t *base; /* base of extent array */
4256 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4257 int error; /* error return value */
4258 xfs_ifork_t *ifp; /* inode fork pointer */
4259 xfs_extnum_t nextents; /* number of extent entries */
4261 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4262 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4263 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4264 return XFS_ERROR(EIO);
4265 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4269 ifp = XFS_IFORK_PTR(ip, whichfork);
4270 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4271 (error = xfs_iread_extents(tp, ip, whichfork)))
4273 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4278 base = &ifp->if_u1.if_extents[0];
4279 ASSERT(base != NULL);
4280 ep = &base[nextents - 1];
4281 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4286 * Returns whether the selected fork of the inode has exactly one
4287 * block or not. For the data fork we check this matches di_size,
4288 * implying the file's range is 0..bsize-1.
4290 int /* 1=>1 block, 0=>otherwise */
4292 xfs_inode_t *ip, /* incore inode */
4293 int whichfork) /* data or attr fork */
4295 xfs_bmbt_rec_t *ep; /* ptr to fork's extent */
4296 xfs_ifork_t *ifp; /* inode fork pointer */
4297 int rval; /* return value */
4298 xfs_bmbt_irec_t s; /* internal version of extent */
4301 if (whichfork == XFS_DATA_FORK)
4302 return ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize;
4304 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4306 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4308 ifp = XFS_IFORK_PTR(ip, whichfork);
4309 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4310 ep = ifp->if_u1.if_extents;
4311 xfs_bmbt_get_all(ep, &s);
4312 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4313 if (rval && whichfork == XFS_DATA_FORK)
4314 ASSERT(ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4319 * Read in the extents to if_extents.
4320 * All inode fields are set up by caller, we just traverse the btree
4321 * and copy the records in. If the file system cannot contain unwritten
4322 * extents, the records are checked for no "state" flags.
4325 xfs_bmap_read_extents(
4326 xfs_trans_t *tp, /* transaction pointer */
4327 xfs_inode_t *ip, /* incore inode */
4328 int whichfork) /* data or attr fork */
4330 xfs_bmbt_block_t *block; /* current btree block */
4331 xfs_fsblock_t bno; /* block # of "block" */
4332 xfs_buf_t *bp; /* buffer for "block" */
4333 int error; /* error return value */
4334 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4335 #ifdef XFS_BMAP_TRACE
4336 static char fname[] = "xfs_bmap_read_extents";
4338 xfs_extnum_t i, j; /* index into the extents list */
4339 xfs_ifork_t *ifp; /* fork structure */
4340 int level; /* btree level, for checking */
4341 xfs_mount_t *mp; /* file system mount structure */
4342 xfs_bmbt_ptr_t *pp; /* pointer to block address */
4344 xfs_extnum_t room; /* number of entries there's room for */
4345 xfs_bmbt_rec_t *trp; /* target record pointer */
4349 ifp = XFS_IFORK_PTR(ip, whichfork);
4350 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4351 XFS_EXTFMT_INODE(ip);
4352 block = ifp->if_broot;
4354 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4356 ASSERT(INT_GET(block->bb_level, ARCH_CONVERT) > 0);
4357 level = INT_GET(block->bb_level, ARCH_CONVERT);
4358 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
4359 ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
4360 ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
4361 ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
4362 bno = INT_GET(*pp, ARCH_CONVERT);
4364 * Go down the tree until leaf level is reached, following the first
4365 * pointer (leftmost) at each level.
4367 while (level-- > 0) {
4368 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4369 XFS_BMAP_BTREE_REF)))
4371 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4372 XFS_WANT_CORRUPTED_GOTO(
4373 XFS_BMAP_SANITY_CHECK(mp, block, level),
4377 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
4378 1, mp->m_bmap_dmxr[1]);
4379 XFS_WANT_CORRUPTED_GOTO(
4380 XFS_FSB_SANITY_CHECK(mp, INT_GET(*pp, ARCH_CONVERT)),
4382 bno = INT_GET(*pp, ARCH_CONVERT);
4383 xfs_trans_brelse(tp, bp);
4386 * Here with bp and block set to the leftmost leaf node in the tree.
4388 room = ifp->if_bytes / (uint)sizeof(*trp);
4389 trp = ifp->if_u1.if_extents;
4392 * Loop over all leaf nodes. Copy information to the extent list.
4395 xfs_bmbt_rec_t *frp, *temp;
4396 xfs_fsblock_t nextbno;
4397 xfs_extnum_t num_recs;
4400 num_recs = INT_GET(block->bb_numrecs, ARCH_CONVERT);
4401 if (unlikely(i + num_recs > room)) {
4402 ASSERT(i + num_recs <= room);
4403 xfs_fs_cmn_err(CE_WARN, ip->i_mount,
4404 "corrupt dinode %Lu, (btree extents). Unmount and run xfs_repair.",
4405 (unsigned long long) ip->i_ino);
4406 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4411 XFS_WANT_CORRUPTED_GOTO(
4412 XFS_BMAP_SANITY_CHECK(mp, block, 0),
4415 * Read-ahead the next leaf block, if any.
4417 nextbno = INT_GET(block->bb_rightsib, ARCH_CONVERT);
4418 if (nextbno != NULLFSBLOCK)
4419 xfs_btree_reada_bufl(mp, nextbno, 1);
4421 * Copy records into the extent list.
4423 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
4424 block, 1, mp->m_bmap_dmxr[0]);
4426 for (j = 0; j < num_recs; j++, frp++, trp++) {
4427 trp->l0 = INT_GET(frp->l0, ARCH_CONVERT);
4428 trp->l1 = INT_GET(frp->l1, ARCH_CONVERT);
4430 if (exntf == XFS_EXTFMT_NOSTATE) {
4432 * Check all attribute bmap btree records and
4433 * any "older" data bmap btree records for a
4434 * set bit in the "extent flag" position.
4436 if (unlikely(xfs_check_nostate_extents(temp, num_recs))) {
4437 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4444 xfs_trans_brelse(tp, bp);
4447 * If we've reached the end, stop.
4449 if (bno == NULLFSBLOCK)
4451 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4452 XFS_BMAP_BTREE_REF)))
4454 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4456 ASSERT(i == ifp->if_bytes / (uint)sizeof(*trp));
4457 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4458 xfs_bmap_trace_exlist(fname, ip, i, whichfork);
4461 xfs_trans_brelse(tp, bp);
4462 return XFS_ERROR(EFSCORRUPTED);
4465 #ifdef XFS_BMAP_TRACE
4467 * Add bmap trace insert entries for all the contents of the extent list.
4470 xfs_bmap_trace_exlist(
4471 char *fname, /* function name */
4472 xfs_inode_t *ip, /* incore inode pointer */
4473 xfs_extnum_t cnt, /* count of entries in the list */
4474 int whichfork) /* data or attr fork */
4476 xfs_bmbt_rec_t *base; /* base of extent list */
4477 xfs_bmbt_rec_t *ep; /* current entry in extent list */
4478 xfs_extnum_t idx; /* extent list entry number */
4479 xfs_ifork_t *ifp; /* inode fork pointer */
4480 xfs_bmbt_irec_t s; /* extent list record */
4482 ifp = XFS_IFORK_PTR(ip, whichfork);
4483 ASSERT(cnt == ifp->if_bytes / (uint)sizeof(*base));
4484 base = ifp->if_u1.if_extents;
4485 for (idx = 0, ep = base; idx < cnt; idx++, ep++) {
4486 xfs_bmbt_get_all(ep, &s);
4487 xfs_bmap_trace_insert(fname, "exlist", ip, idx, 1, &s, NULL,
4495 * Validate that the bmbt_irecs being returned from bmapi are valid
4496 * given the callers original parameters. Specifically check the
4497 * ranges of the returned irecs to ensure that they only extent beyond
4498 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4501 xfs_bmap_validate_ret(
4505 xfs_bmbt_irec_t *mval,
4509 int i; /* index to map values */
4511 ASSERT(ret_nmap <= nmap);
4513 for (i = 0; i < ret_nmap; i++) {
4514 ASSERT(mval[i].br_blockcount > 0);
4515 if (!(flags & XFS_BMAPI_ENTIRE)) {
4516 ASSERT(mval[i].br_startoff >= bno);
4517 ASSERT(mval[i].br_blockcount <= len);
4518 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4521 ASSERT(mval[i].br_startoff < bno + len);
4522 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4526 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4527 mval[i].br_startoff);
4528 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4529 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4530 mval[i].br_startblock != HOLESTARTBLOCK);
4531 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4532 mval[i].br_state == XFS_EXT_UNWRITTEN);
4539 * Map file blocks to filesystem blocks.
4540 * File range is given by the bno/len pair.
4541 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4542 * into a hole or past eof.
4543 * Only allocates blocks from a single allocation group,
4544 * to avoid locking problems.
4545 * The returned value in "firstblock" from the first call in a transaction
4546 * must be remembered and presented to subsequent calls in "firstblock".
4547 * An upper bound for the number of blocks to be allocated is supplied to
4548 * the first call in "total"; if no allocation group has that many free
4549 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4553 xfs_trans_t *tp, /* transaction pointer */
4554 xfs_inode_t *ip, /* incore inode */
4555 xfs_fileoff_t bno, /* starting file offs. mapped */
4556 xfs_filblks_t len, /* length to map in file */
4557 int flags, /* XFS_BMAPI_... */
4558 xfs_fsblock_t *firstblock, /* first allocated block
4559 controls a.g. for allocs */
4560 xfs_extlen_t total, /* total blocks needed */
4561 xfs_bmbt_irec_t *mval, /* output: map values */
4562 int *nmap, /* i/o: mval size/count */
4563 xfs_bmap_free_t *flist) /* i/o: list extents to free */
4565 xfs_fsblock_t abno; /* allocated block number */
4566 xfs_extlen_t alen; /* allocated extent length */
4567 xfs_fileoff_t aoff; /* allocated file offset */
4568 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
4569 char contig; /* allocation must be one extent */
4570 xfs_btree_cur_t *cur; /* bmap btree cursor */
4571 char delay; /* this request is for delayed alloc */
4572 xfs_fileoff_t end; /* end of mapped file region */
4573 int eof; /* we've hit the end of extent list */
4574 xfs_bmbt_rec_t *ep; /* extent list entry pointer */
4575 int error; /* error return */
4576 char exact; /* don't do all of wasdelayed extent */
4577 xfs_bmbt_irec_t got; /* current extent list record */
4578 xfs_ifork_t *ifp; /* inode fork pointer */
4579 xfs_extlen_t indlen; /* indirect blocks length */
4580 char inhole; /* current location is hole in file */
4581 xfs_extnum_t lastx; /* last useful extent number */
4582 int logflags; /* flags for transaction logging */
4583 xfs_extlen_t minleft; /* min blocks left after allocation */
4584 xfs_extlen_t minlen; /* min allocation size */
4585 xfs_mount_t *mp; /* xfs mount structure */
4586 int n; /* current extent index */
4587 int nallocs; /* number of extents alloc\'d */
4588 xfs_extnum_t nextents; /* number of extents in file */
4589 xfs_fileoff_t obno; /* old block number (offset) */
4590 xfs_bmbt_irec_t prev; /* previous extent list record */
4591 char stateless; /* ignore state flag set */
4592 int tmp_logflags; /* temp flags holder */
4593 char trim; /* output trimmed to match range */
4594 char userdata; /* allocating non-metadata */
4595 char wasdelay; /* old extent was delayed */
4596 int whichfork; /* data or attr fork */
4597 char wr; /* this is a write request */
4598 char rsvd; /* OK to allocate reserved blocks */
4600 xfs_fileoff_t orig_bno; /* original block number value */
4601 int orig_flags; /* original flags arg value */
4602 xfs_filblks_t orig_len; /* original value of len arg */
4603 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4604 int orig_nmap; /* original value of *nmap */
4613 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4614 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4615 XFS_ATTR_FORK : XFS_DATA_FORK;
4617 if (unlikely(XFS_TEST_ERROR(
4618 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4619 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4620 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4621 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4622 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4623 return XFS_ERROR(EFSCORRUPTED);
4625 if (XFS_FORCED_SHUTDOWN(mp))
4626 return XFS_ERROR(EIO);
4627 ifp = XFS_IFORK_PTR(ip, whichfork);
4628 ASSERT(ifp->if_ext_max ==
4629 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4630 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4631 XFS_STATS_INC(xs_blk_mapw);
4633 XFS_STATS_INC(xs_blk_mapr);
4634 delay = (flags & XFS_BMAPI_DELAY) != 0;
4635 trim = (flags & XFS_BMAPI_ENTIRE) == 0;
4636 userdata = (flags & XFS_BMAPI_METADATA) == 0;
4637 exact = (flags & XFS_BMAPI_EXACT) != 0;
4638 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
4639 contig = (flags & XFS_BMAPI_CONTIG) != 0;
4641 * stateless is used to combine extents which
4642 * differ only due to the state of the extents.
4643 * This technique is used from xfs_getbmap()
4644 * when the caller does not wish to see the
4645 * separation (which is the default).
4647 * This technique is also used when writing a
4648 * buffer which has been partially written,
4649 * (usually by being flushed during a chunkread),
4650 * to ensure one write takes place. This also
4651 * prevents a change in the xfs inode extents at
4652 * this time, intentionally. This change occurs
4653 * on completion of the write operation, in
4654 * xfs_strat_comp(), where the xfs_bmapi() call
4655 * is transactioned, and the extents combined.
4657 stateless = (flags & XFS_BMAPI_IGSTATE) != 0;
4658 if (stateless && wr) /* if writing unwritten space, no */
4659 wr = 0; /* allocations are allowed */
4660 ASSERT(wr || !delay);
4664 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4666 if ((error = xfs_bmap_local_to_extents(tp, ip,
4667 firstblock, total, &logflags, whichfork)))
4670 if (wr && *firstblock == NULLFSBLOCK) {
4671 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4672 minleft = INT_GET(ifp->if_broot->bb_level, ARCH_CONVERT) + 1;
4677 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4678 (error = xfs_iread_extents(tp, ip, whichfork)))
4680 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4682 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4687 while (bno < end && n < *nmap) {
4689 * Reading past eof, act as though there's a hole
4693 got.br_startoff = end;
4694 inhole = eof || got.br_startoff > bno;
4695 wasdelay = wr && !inhole && !delay &&
4696 ISNULLSTARTBLOCK(got.br_startblock);
4698 * First, deal with the hole before the allocated space
4699 * that we found, if any.
4701 if (wr && (inhole || wasdelay)) {
4703 * For the wasdelay case, we could also just
4704 * allocate the stuff asked for in this bmap call
4705 * but that wouldn't be as good.
4707 if (wasdelay && !exact) {
4708 alen = (xfs_extlen_t)got.br_blockcount;
4709 aoff = got.br_startoff;
4710 if (lastx != NULLEXTNUM && lastx) {
4711 ep = &ifp->if_u1.if_extents[lastx - 1];
4712 xfs_bmbt_get_all(ep, &prev);
4714 } else if (wasdelay) {
4715 alen = (xfs_extlen_t)
4716 XFS_FILBLKS_MIN(len,
4718 got.br_blockcount) - bno);
4721 alen = (xfs_extlen_t)
4722 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4724 alen = (xfs_extlen_t)
4725 XFS_FILBLKS_MIN(alen,
4726 got.br_startoff - bno);
4729 minlen = contig ? alen : 1;
4731 indlen = (xfs_extlen_t)
4732 xfs_bmap_worst_indlen(ip, alen);
4735 * Make a transaction-less quota reservation for
4736 * delayed allocation blocks. This number gets
4738 * We return EDQUOT if we haven't allocated
4739 * blks already inside this loop;
4741 if (XFS_TRANS_RESERVE_BLKQUOTA(
4742 mp, NULL, ip, (long)alen)) {
4745 ASSERT(cur == NULL);
4746 return XFS_ERROR(EDQUOT);
4750 if (xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
4751 -(alen + indlen), rsvd)) {
4752 XFS_TRANS_UNRESERVE_BLKQUOTA(
4753 mp, NULL, ip, (long)alen);
4756 ip->i_delayed_blks += alen;
4757 abno = NULLSTARTBLOCK(indlen);
4760 * If first time, allocate and fill in
4761 * once-only bma fields.
4763 if (bma.ip == NULL) {
4771 /* Indicate if this is the first user data
4772 * in the file, or just any user data.
4775 bma.userdata = (aoff == 0) ?
4776 XFS_ALLOC_INITIAL_USER_DATA :
4780 * Fill in changeable bma fields.
4783 bma.firstblock = *firstblock;
4786 bma.wasdel = wasdelay;
4787 bma.minlen = minlen;
4788 bma.low = flist->xbf_low;
4789 bma.minleft = minleft;
4791 * Only want to do the alignment at the
4792 * eof if it is userdata and allocation length
4793 * is larger than a stripe unit.
4795 if (mp->m_dalign && alen >= mp->m_dalign &&
4796 userdata && whichfork == XFS_DATA_FORK) {
4797 if ((error = xfs_bmap_isaeof(ip, aoff,
4798 whichfork, &bma.aeof)))
4805 if ((error = xfs_bmap_alloc(&bma)))
4808 * Copy out result fields.
4811 if ((flist->xbf_low = bma.low))
4815 ASSERT(*firstblock == NULLFSBLOCK ||
4816 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4817 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4819 XFS_FSB_TO_AGNO(mp, *firstblock) <
4820 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4821 *firstblock = bma.firstblock;
4823 cur->bc_private.b.firstblock =
4825 if (abno == NULLFSBLOCK)
4827 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4828 cur = xfs_btree_init_cursor(mp,
4829 tp, NULL, 0, XFS_BTNUM_BMAP,
4831 cur->bc_private.b.firstblock =
4833 cur->bc_private.b.flist = flist;
4836 * Bump the number of extents we've allocated
4842 cur->bc_private.b.flags =
4843 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4844 got.br_startoff = aoff;
4845 got.br_startblock = abno;
4846 got.br_blockcount = alen;
4847 got.br_state = XFS_EXT_NORM; /* assume normal */
4849 * Determine state of extent, and the filesystem.
4850 * A wasdelay extent has been initialized, so
4851 * shouldn't be flagged as unwritten.
4853 if (wr && XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4854 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
4855 got.br_state = XFS_EXT_UNWRITTEN;
4857 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
4858 firstblock, flist, &tmp_logflags, whichfork,
4860 logflags |= tmp_logflags;
4863 lastx = ifp->if_lastex;
4864 ep = &ifp->if_u1.if_extents[lastx];
4865 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4866 xfs_bmbt_get_all(ep, &got);
4867 ASSERT(got.br_startoff <= aoff);
4868 ASSERT(got.br_startoff + got.br_blockcount >=
4872 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
4873 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
4875 ASSERT(got.br_state == XFS_EXT_NORM ||
4876 got.br_state == XFS_EXT_UNWRITTEN);
4879 * Fall down into the found allocated space case.
4881 } else if (inhole) {
4883 * Reading in a hole.
4885 mval->br_startoff = bno;
4886 mval->br_startblock = HOLESTARTBLOCK;
4887 mval->br_blockcount =
4888 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4889 mval->br_state = XFS_EXT_NORM;
4890 bno += mval->br_blockcount;
4891 len -= mval->br_blockcount;
4897 * Then deal with the allocated space we found.
4900 if (trim && (got.br_startoff + got.br_blockcount > obno)) {
4903 ASSERT((bno >= obno) || (n == 0));
4905 mval->br_startoff = bno;
4906 if (ISNULLSTARTBLOCK(got.br_startblock)) {
4907 ASSERT(!wr || delay);
4908 mval->br_startblock = DELAYSTARTBLOCK;
4910 mval->br_startblock =
4912 (bno - got.br_startoff);
4914 * Return the minimum of what we got and what we
4915 * asked for for the length. We can use the len
4916 * variable here because it is modified below
4917 * and we could have been there before coming
4918 * here if the first part of the allocation
4919 * didn't overlap what was asked for.
4921 mval->br_blockcount =
4922 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
4923 (bno - got.br_startoff));
4924 mval->br_state = got.br_state;
4925 ASSERT(mval->br_blockcount <= len);
4928 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
4929 ASSERT(!wr || delay);
4930 mval->br_startblock = DELAYSTARTBLOCK;
4935 * Check if writing previously allocated but
4936 * unwritten extents.
4938 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
4939 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
4941 * Modify (by adding) the state flag, if writing.
4943 ASSERT(mval->br_blockcount <= len);
4944 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4945 cur = xfs_btree_init_cursor(mp,
4946 tp, NULL, 0, XFS_BTNUM_BMAP,
4948 cur->bc_private.b.firstblock =
4950 cur->bc_private.b.flist = flist;
4952 mval->br_state = XFS_EXT_NORM;
4953 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
4954 firstblock, flist, &tmp_logflags, whichfork,
4956 logflags |= tmp_logflags;
4959 lastx = ifp->if_lastex;
4960 ep = &ifp->if_u1.if_extents[lastx];
4961 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4962 xfs_bmbt_get_all(ep, &got);
4964 * We may have combined previously unwritten
4965 * space with written space, so generate
4968 if (mval->br_blockcount < len)
4973 ((mval->br_startoff + mval->br_blockcount) <= end));
4974 ASSERT(!trim || (mval->br_blockcount <= len) ||
4975 (mval->br_startoff < obno));
4976 bno = mval->br_startoff + mval->br_blockcount;
4978 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4979 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4980 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4981 ASSERT(mval->br_state == mval[-1].br_state);
4982 mval[-1].br_blockcount = mval->br_blockcount;
4983 mval[-1].br_state = mval->br_state;
4984 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4985 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4986 mval[-1].br_startblock != HOLESTARTBLOCK &&
4987 mval->br_startblock ==
4988 mval[-1].br_startblock + mval[-1].br_blockcount &&
4989 (stateless || mval[-1].br_state == mval->br_state)) {
4990 ASSERT(mval->br_startoff ==
4991 mval[-1].br_startoff + mval[-1].br_blockcount);
4992 mval[-1].br_blockcount += mval->br_blockcount;
4994 mval->br_startblock == DELAYSTARTBLOCK &&
4995 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4996 mval->br_startoff ==
4997 mval[-1].br_startoff + mval[-1].br_blockcount) {
4998 mval[-1].br_blockcount += mval->br_blockcount;
4999 mval[-1].br_state = mval->br_state;
5000 } else if (!((n == 0) &&
5001 ((mval->br_startoff + mval->br_blockcount) <=
5007 * If we're done, stop now. Stop when we've allocated
5008 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5009 * the transaction may get too big.
5011 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5014 * Else go on to the next record.
5018 if (lastx >= nextents) {
5022 xfs_bmbt_get_all(ep, &got);
5024 ifp->if_lastex = lastx;
5027 * Transform from btree to extents, give it cur.
5029 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5030 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5032 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5033 &tmp_logflags, whichfork);
5034 logflags |= tmp_logflags;
5038 ASSERT(ifp->if_ext_max ==
5039 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5040 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5041 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5046 * Log everything. Do this after conversion, there's no point in
5047 * logging the extent list if we've converted to btree format.
5049 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5050 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5051 logflags &= ~XFS_ILOG_FEXT(whichfork);
5052 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5053 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5054 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5056 * Log whatever the flags say, even if error. Otherwise we might miss
5057 * detecting a case where the data is changed, there's an error,
5058 * and it's not logged so we don't shutdown when we should.
5062 xfs_trans_log_inode(tp, ip, logflags);
5066 ASSERT(*firstblock == NULLFSBLOCK ||
5067 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5069 cur->bc_private.b.firstblock) ||
5071 XFS_FSB_TO_AGNO(mp, *firstblock) <
5073 cur->bc_private.b.firstblock)));
5074 *firstblock = cur->bc_private.b.firstblock;
5076 xfs_btree_del_cursor(cur,
5077 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5080 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5086 * Map file blocks to filesystem blocks, simple version.
5087 * One block (extent) only, read-only.
5088 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5089 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5090 * was set and all the others were clear.
5094 xfs_trans_t *tp, /* transaction pointer */
5095 xfs_inode_t *ip, /* incore inode */
5096 int whichfork, /* data or attr fork */
5097 xfs_fsblock_t *fsb, /* output: mapped block */
5098 xfs_fileoff_t bno) /* starting file offs. mapped */
5100 int eof; /* we've hit the end of extent list */
5101 int error; /* error return */
5102 xfs_bmbt_irec_t got; /* current extent list record */
5103 xfs_ifork_t *ifp; /* inode fork pointer */
5104 xfs_extnum_t lastx; /* last useful extent number */
5105 xfs_bmbt_irec_t prev; /* previous extent list record */
5107 ifp = XFS_IFORK_PTR(ip, whichfork);
5109 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5110 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5111 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5113 return XFS_ERROR(EFSCORRUPTED);
5115 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5116 return XFS_ERROR(EIO);
5117 XFS_STATS_INC(xs_blk_mapr);
5118 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5119 (error = xfs_iread_extents(tp, ip, whichfork)))
5121 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5124 * Reading past eof, act as though there's a hole
5127 if (eof || got.br_startoff > bno) {
5131 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
5132 ASSERT(bno < got.br_startoff + got.br_blockcount);
5133 *fsb = got.br_startblock + (bno - got.br_startoff);
5134 ifp->if_lastex = lastx;
5139 * Unmap (remove) blocks from a file.
5140 * If nexts is nonzero then the number of extents to remove is limited to
5141 * that value. If not all extents in the block range can be removed then
5146 xfs_trans_t *tp, /* transaction pointer */
5147 struct xfs_inode *ip, /* incore inode */
5148 xfs_fileoff_t bno, /* starting offset to unmap */
5149 xfs_filblks_t len, /* length to unmap in file */
5150 int flags, /* misc flags */
5151 xfs_extnum_t nexts, /* number of extents max */
5152 xfs_fsblock_t *firstblock, /* first allocated block
5153 controls a.g. for allocs */
5154 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5155 int *done) /* set if not done yet */
5157 xfs_btree_cur_t *cur; /* bmap btree cursor */
5158 xfs_bmbt_irec_t del; /* extent being deleted */
5159 int eof; /* is deleting at eof */
5160 xfs_bmbt_rec_t *ep; /* extent list entry pointer */
5161 int error; /* error return value */
5162 xfs_extnum_t extno; /* extent number in list */
5163 xfs_bmbt_irec_t got; /* current extent list entry */
5164 xfs_ifork_t *ifp; /* inode fork pointer */
5165 int isrt; /* freeing in rt area */
5166 xfs_extnum_t lastx; /* last extent index used */
5167 int logflags; /* transaction logging flags */
5168 xfs_extlen_t mod; /* rt extent offset */
5169 xfs_mount_t *mp; /* mount structure */
5170 xfs_extnum_t nextents; /* size of extent list */
5171 xfs_bmbt_irec_t prev; /* previous extent list entry */
5172 xfs_fileoff_t start; /* first file offset deleted */
5173 int tmp_logflags; /* partial logging flags */
5174 int wasdel; /* was a delayed alloc extent */
5175 int whichfork; /* data or attribute fork */
5176 int rsvd; /* OK to allocate reserved blocks */
5179 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5180 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5181 XFS_ATTR_FORK : XFS_DATA_FORK;
5182 ifp = XFS_IFORK_PTR(ip, whichfork);
5184 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5185 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5186 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5188 return XFS_ERROR(EFSCORRUPTED);
5191 if (XFS_FORCED_SHUTDOWN(mp))
5192 return XFS_ERROR(EIO);
5193 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5196 ASSERT(ifp->if_ext_max ==
5197 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5198 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5199 (error = xfs_iread_extents(tp, ip, whichfork)))
5201 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5202 if (nextents == 0) {
5206 XFS_STATS_INC(xs_blk_unmap);
5207 isrt = (whichfork == XFS_DATA_FORK) &&
5208 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
5210 bno = start + len - 1;
5211 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5214 * Check to see if the given block number is past the end of the
5215 * file, back up to the last block if so...
5218 ep = &ifp->if_u1.if_extents[--lastx];
5219 xfs_bmbt_get_all(ep, &got);
5220 bno = got.br_startoff + got.br_blockcount - 1;
5223 if (ifp->if_flags & XFS_IFBROOT) {
5224 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5225 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
5227 cur->bc_private.b.firstblock = *firstblock;
5228 cur->bc_private.b.flist = flist;
5229 cur->bc_private.b.flags = 0;
5233 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5234 (nexts == 0 || extno < nexts)) {
5236 * Is the found extent after a hole in which bno lives?
5237 * Just back up to the previous extent, if so.
5239 if (got.br_startoff > bno) {
5243 xfs_bmbt_get_all(ep, &got);
5246 * Is the last block of this extent before the range
5247 * we're supposed to delete? If so, we're done.
5249 bno = XFS_FILEOFF_MIN(bno,
5250 got.br_startoff + got.br_blockcount - 1);
5254 * Then deal with the (possibly delayed) allocated space
5259 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
5260 if (got.br_startoff < start) {
5261 del.br_startoff = start;
5262 del.br_blockcount -= start - got.br_startoff;
5264 del.br_startblock += start - got.br_startoff;
5266 if (del.br_startoff + del.br_blockcount > bno + 1)
5267 del.br_blockcount = bno + 1 - del.br_startoff;
5268 sum = del.br_startblock + del.br_blockcount;
5270 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5272 * Realtime extent not lined up at the end.
5273 * The extent could have been split into written
5274 * and unwritten pieces, or we could just be
5275 * unmapping part of it. But we can't really
5276 * get rid of part of a realtime extent.
5278 if (del.br_state == XFS_EXT_UNWRITTEN ||
5279 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5281 * This piece is unwritten, or we're not
5282 * using unwritten extents. Skip over it.
5285 bno -= mod > del.br_blockcount ?
5286 del.br_blockcount : mod;
5287 if (bno < got.br_startoff) {
5289 xfs_bmbt_get_all(--ep, &got);
5294 * It's written, turn it unwritten.
5295 * This is better than zeroing it.
5297 ASSERT(del.br_state == XFS_EXT_NORM);
5298 ASSERT(xfs_trans_get_block_res(tp) > 0);
5300 * If this spans a realtime extent boundary,
5301 * chop it back to the start of the one we end at.
5303 if (del.br_blockcount > mod) {
5304 del.br_startoff += del.br_blockcount - mod;
5305 del.br_startblock += del.br_blockcount - mod;
5306 del.br_blockcount = mod;
5308 del.br_state = XFS_EXT_UNWRITTEN;
5309 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5310 firstblock, flist, &logflags, XFS_DATA_FORK, 0);
5315 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5317 * Realtime extent is lined up at the end but not
5318 * at the front. We'll get rid of full extents if
5321 mod = mp->m_sb.sb_rextsize - mod;
5322 if (del.br_blockcount > mod) {
5323 del.br_blockcount -= mod;
5324 del.br_startoff += mod;
5325 del.br_startblock += mod;
5326 } else if ((del.br_startoff == start &&
5327 (del.br_state == XFS_EXT_UNWRITTEN ||
5328 xfs_trans_get_block_res(tp) == 0)) ||
5329 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5331 * Can't make it unwritten. There isn't
5332 * a full extent here so just skip it.
5334 ASSERT(bno >= del.br_blockcount);
5335 bno -= del.br_blockcount;
5336 if (bno < got.br_startoff) {
5338 xfs_bmbt_get_all(--ep, &got);
5341 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5343 * This one is already unwritten.
5344 * It must have a written left neighbor.
5345 * Unwrite the killed part of that one and
5349 xfs_bmbt_get_all(ep - 1, &prev);
5350 ASSERT(prev.br_state == XFS_EXT_NORM);
5351 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
5352 ASSERT(del.br_startblock ==
5353 prev.br_startblock + prev.br_blockcount);
5354 if (prev.br_startoff < start) {
5355 mod = start - prev.br_startoff;
5356 prev.br_blockcount -= mod;
5357 prev.br_startblock += mod;
5358 prev.br_startoff = start;
5360 prev.br_state = XFS_EXT_UNWRITTEN;
5361 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5362 &prev, firstblock, flist, &logflags,
5368 ASSERT(del.br_state == XFS_EXT_NORM);
5369 del.br_state = XFS_EXT_UNWRITTEN;
5370 error = xfs_bmap_add_extent(ip, lastx, &cur,
5371 &del, firstblock, flist, &logflags,
5379 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
5380 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
5381 (int)del.br_blockcount, rsvd);
5382 /* Unreserve our quota space */
5383 XFS_TRANS_RESERVE_QUOTA_NBLKS(
5384 mp, NULL, ip, -((long)del.br_blockcount), 0,
5385 isrt ? XFS_QMOPT_RES_RTBLKS :
5386 XFS_QMOPT_RES_REGBLKS);
5387 ip->i_delayed_blks -= del.br_blockcount;
5389 cur->bc_private.b.flags |=
5390 XFS_BTCUR_BPRV_WASDEL;
5392 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5394 * If it's the case where the directory code is running
5395 * with no block reservation, and the deleted block is in
5396 * the middle of its extent, and the resulting insert
5397 * of an extent would cause transformation to btree format,
5398 * then reject it. The calling code will then swap
5399 * blocks around instead.
5400 * We have to do this now, rather than waiting for the
5401 * conversion to btree format, since the transaction
5404 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5405 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5406 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5407 del.br_startoff > got.br_startoff &&
5408 del.br_startoff + del.br_blockcount <
5409 got.br_startoff + got.br_blockcount) {
5410 error = XFS_ERROR(ENOSPC);
5413 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5414 &tmp_logflags, whichfork, rsvd);
5415 logflags |= tmp_logflags;
5418 bno = del.br_startoff - 1;
5420 lastx = ifp->if_lastex;
5422 * If not done go on to the next (previous) record.
5423 * Reset ep in case the extents array was re-alloced.
5425 ep = &ifp->if_u1.if_extents[lastx];
5426 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5427 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5428 xfs_bmbt_get_startoff(ep) > bno) {
5433 xfs_bmbt_get_all(ep, &got);
5437 ifp->if_lastex = lastx;
5438 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5439 ASSERT(ifp->if_ext_max ==
5440 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5442 * Convert to a btree if necessary.
5444 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5445 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5446 ASSERT(cur == NULL);
5447 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5448 &cur, 0, &tmp_logflags, whichfork);
5449 logflags |= tmp_logflags;
5454 * transform from btree to extents, give it cur
5456 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5457 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5458 ASSERT(cur != NULL);
5459 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5461 logflags |= tmp_logflags;
5466 * transform from extents to local?
5468 ASSERT(ifp->if_ext_max ==
5469 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5473 * Log everything. Do this after conversion, there's no point in
5474 * logging the extent list if we've converted to btree format.
5476 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5477 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5478 logflags &= ~XFS_ILOG_FEXT(whichfork);
5479 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5480 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5481 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5483 * Log inode even in the error case, if the transaction
5484 * is dirty we'll need to shut down the filesystem.
5487 xfs_trans_log_inode(tp, ip, logflags);
5490 *firstblock = cur->bc_private.b.firstblock;
5491 cur->bc_private.b.allocated = 0;
5493 xfs_btree_del_cursor(cur,
5494 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5500 * Fcntl interface to xfs_bmapi.
5502 int /* error code */
5504 bhv_desc_t *bdp, /* XFS behavior descriptor*/
5505 struct getbmap *bmv, /* user bmap structure */
5506 void *ap, /* pointer to user's array */
5507 int interface) /* interface flags */
5509 __int64_t bmvend; /* last block requested */
5510 int error; /* return value */
5511 __int64_t fixlen; /* length for -1 case */
5512 int i; /* extent number */
5513 xfs_inode_t *ip; /* xfs incore inode pointer */
5514 vnode_t *vp; /* corresponding vnode */
5515 int lock; /* lock state */
5516 xfs_bmbt_irec_t *map; /* buffer for user's data */
5517 xfs_mount_t *mp; /* file system mount point */
5518 int nex; /* # of user extents can do */
5519 int nexleft; /* # of user extents left */
5520 int subnex; /* # of bmapi's can do */
5521 int nmap; /* number of map entries */
5522 struct getbmap out; /* output structure */
5523 int whichfork; /* data or attr fork */
5524 int prealloced; /* this is a file with
5525 * preallocated data space */
5526 int sh_unwritten; /* true, if unwritten */
5527 /* extents listed separately */
5528 int bmapi_flags; /* flags for xfs_bmapi */
5529 __int32_t oflags; /* getbmapx bmv_oflags field */
5531 vp = BHV_TO_VNODE(bdp);
5532 ip = XFS_BHVTOI(bdp);
5535 whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5536 sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
5538 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5539 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5540 * bit is set for the file, generate a read event in order
5541 * that the DMAPI application may do its thing before we return
5542 * the extents. Usually this means restoring user file data to
5543 * regions of the file that look like holes.
5545 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5546 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5547 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5548 * could misinterpret holes in a DMAPI file as true holes,
5549 * when in fact they may represent offline user data.
5551 if ( (interface & BMV_IF_NO_DMAPI_READ) == 0
5552 && DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)
5553 && whichfork == XFS_DATA_FORK) {
5555 error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, 0, 0, 0, NULL);
5557 return XFS_ERROR(error);
5560 if (whichfork == XFS_ATTR_FORK) {
5561 if (XFS_IFORK_Q(ip)) {
5562 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5563 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5564 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5565 return XFS_ERROR(EINVAL);
5566 } else if (unlikely(
5567 ip->i_d.di_aformat != 0 &&
5568 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5569 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5571 return XFS_ERROR(EFSCORRUPTED);
5573 } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5574 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5575 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5576 return XFS_ERROR(EINVAL);
5577 if (whichfork == XFS_DATA_FORK) {
5578 if (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC) {
5580 fixlen = XFS_MAXIOFFSET(mp);
5583 fixlen = ip->i_d.di_size;
5590 if (bmv->bmv_length == -1) {
5591 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5592 bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
5594 } else if (bmv->bmv_length < 0)
5595 return XFS_ERROR(EINVAL);
5596 if (bmv->bmv_length == 0) {
5597 bmv->bmv_entries = 0;
5600 nex = bmv->bmv_count - 1;
5602 return XFS_ERROR(EINVAL);
5603 bmvend = bmv->bmv_offset + bmv->bmv_length;
5605 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5607 if (whichfork == XFS_DATA_FORK && ip->i_delayed_blks) {
5608 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
5609 VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
5612 ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
5614 lock = xfs_ilock_map_shared(ip);
5617 * Don't let nex be bigger than the number of extents
5618 * we can have assuming alternating holes and real extents.
5620 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5621 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5623 bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
5624 ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
5627 * Allocate enough space to handle "subnex" maps at a time.
5630 map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
5632 bmv->bmv_entries = 0;
5634 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
5636 goto unlock_and_return;
5642 nmap = (nexleft > subnex) ? subnex : nexleft;
5643 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5644 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5645 bmapi_flags, NULL, 0, map, &nmap, NULL);
5647 goto unlock_and_return;
5648 ASSERT(nmap <= subnex);
5650 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5652 oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
5653 BMV_OF_PREALLOC : 0;
5654 out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
5655 out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5656 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
5658 map[i].br_startblock == HOLESTARTBLOCK &&
5659 out.bmv_offset + out.bmv_length == bmvend) {
5661 * came to hole at end of file
5663 goto unlock_and_return;
5666 (map[i].br_startblock == HOLESTARTBLOCK) ?
5668 XFS_FSB_TO_DB(ip, map[i].br_startblock);
5670 /* return either getbmap/getbmapx structure. */
5671 if (interface & BMV_IF_EXTENDED) {
5672 struct getbmapx outx;
5674 GETBMAP_CONVERT(out,outx);
5675 outx.bmv_oflags = oflags;
5676 outx.bmv_unused1 = outx.bmv_unused2 = 0;
5677 if (copy_to_user(ap, &outx,
5679 error = XFS_ERROR(EFAULT);
5680 goto unlock_and_return;
5683 if (copy_to_user(ap, &out,
5685 error = XFS_ERROR(EFAULT);
5686 goto unlock_and_return;
5690 out.bmv_offset + out.bmv_length;
5691 bmv->bmv_length = MAX((__int64_t)0,
5692 (__int64_t)(bmvend - bmv->bmv_offset));
5694 ap = (interface & BMV_IF_EXTENDED) ?
5695 (void *)((struct getbmapx *)ap + 1) :
5696 (void *)((struct getbmap *)ap + 1);
5699 } while (nmap && nexleft && bmv->bmv_length);
5702 xfs_iunlock_map_shared(ip, lock);
5703 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5705 kmem_free(map, subnex * sizeof(*map));
5711 * Check the last inode extent to determine whether this allocation will result
5712 * in blocks being allocated at the end of the file. When we allocate new data
5713 * blocks at the end of the file which do not start at the previous data block,
5714 * we will try to align the new blocks at stripe unit boundaries.
5718 xfs_inode_t *ip, /* incore inode pointer */
5719 xfs_fileoff_t off, /* file offset in fsblocks */
5720 int whichfork, /* data or attribute fork */
5721 char *aeof) /* return value */
5723 int error; /* error return value */
5724 xfs_ifork_t *ifp; /* inode fork pointer */
5725 xfs_bmbt_rec_t *lastrec; /* extent list entry pointer */
5726 xfs_extnum_t nextents; /* size of extent list */
5727 xfs_bmbt_irec_t s; /* expanded extent list entry */
5729 ASSERT(whichfork == XFS_DATA_FORK);
5730 ifp = XFS_IFORK_PTR(ip, whichfork);
5731 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5732 (error = xfs_iread_extents(NULL, ip, whichfork)))
5734 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5735 if (nextents == 0) {
5740 * Go to the last extent
5742 lastrec = &ifp->if_u1.if_extents[nextents - 1];
5743 xfs_bmbt_get_all(lastrec, &s);
5745 * Check we are allocating in the last extent (for delayed allocations)
5746 * or past the last extent for non-delayed allocations.
5748 *aeof = (off >= s.br_startoff &&
5749 off < s.br_startoff + s.br_blockcount &&
5750 ISNULLSTARTBLOCK(s.br_startblock)) ||
5751 off >= s.br_startoff + s.br_blockcount;
5756 * Check if the endoff is outside the last extent. If so the caller will grow
5757 * the allocation to a stripe unit boundary.
5761 xfs_inode_t *ip, /* incore inode pointer */
5762 xfs_fileoff_t endoff, /* file offset in fsblocks */
5763 int whichfork, /* data or attribute fork */
5764 int *eof) /* result value */
5766 xfs_fsblock_t blockcount; /* extent block count */
5767 int error; /* error return value */
5768 xfs_ifork_t *ifp; /* inode fork pointer */
5769 xfs_bmbt_rec_t *lastrec; /* extent list entry pointer */
5770 xfs_extnum_t nextents; /* size of extent list */
5771 xfs_fileoff_t startoff; /* extent starting file offset */
5773 ASSERT(whichfork == XFS_DATA_FORK);
5774 ifp = XFS_IFORK_PTR(ip, whichfork);
5775 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5776 (error = xfs_iread_extents(NULL, ip, whichfork)))
5778 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5779 if (nextents == 0) {
5784 * Go to the last extent
5786 lastrec = &ifp->if_u1.if_extents[nextents - 1];
5787 startoff = xfs_bmbt_get_startoff(lastrec);
5788 blockcount = xfs_bmbt_get_blockcount(lastrec);
5789 *eof = endoff >= startoff + blockcount;
5795 * Check that the extents list for the inode ip is in the right order.
5798 xfs_bmap_check_extents(
5799 xfs_inode_t *ip, /* incore inode pointer */
5800 int whichfork) /* data or attr fork */
5802 xfs_bmbt_rec_t *base; /* base of extents list */
5803 xfs_bmbt_rec_t *ep; /* current extent entry */
5804 xfs_ifork_t *ifp; /* inode fork pointer */
5805 xfs_extnum_t nextents; /* number of extents in list */
5807 ifp = XFS_IFORK_PTR(ip, whichfork);
5808 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
5809 base = ifp->if_u1.if_extents;
5810 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5811 for (ep = base; ep < &base[nextents - 1]; ep++) {
5812 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
5820 xfs_btree_cur_t *cur,
5830 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5831 bp = cur->bc_bufs[i];
5833 if (XFS_BUF_ADDR(bp) == bno)
5834 break; /* Found it */
5836 if (i == XFS_BTREE_MAXLEVELS)
5839 if (!bp) { /* Chase down all the log items to see if the bp is there */
5840 xfs_log_item_chunk_t *licp;
5844 licp = &tp->t_items;
5845 while (!bp && licp != NULL) {
5846 if (XFS_LIC_ARE_ALL_FREE(licp)) {
5847 licp = licp->lic_next;
5850 for (i = 0; i < licp->lic_unused; i++) {
5851 xfs_log_item_desc_t *lidp;
5852 xfs_log_item_t *lip;
5853 xfs_buf_log_item_t *bip;
5856 if (XFS_LIC_ISFREE(licp, i)) {
5860 lidp = XFS_LIC_SLOT(licp, i);
5861 lip = lidp->lid_item;
5862 if (lip->li_type != XFS_LI_BUF)
5865 bip = (xfs_buf_log_item_t *)lip;
5868 if (XFS_BUF_ADDR(lbp) == bno) {
5870 break; /* Found it */
5873 licp = licp->lic_next;
5881 xfs_bmbt_block_t *block,
5887 xfs_bmbt_ptr_t *pp, *thispa; /* pointer to block address */
5888 xfs_bmbt_key_t *prevp, *keyp;
5890 ASSERT(INT_GET(block->bb_level, ARCH_CONVERT) > 0);
5893 for( i = 1; i <= INT_GET(block->bb_numrecs, ARCH_CONVERT);i++) {
5894 dmxr = mp->m_bmap_dmxr[0];
5897 keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
5899 keyp = XFS_BTREE_KEY_ADDR(mp->m_sb.sb_blocksize,
5900 xfs_bmbt, block, i, dmxr);
5904 xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
5909 * Compare the block numbers to see if there are dups.
5913 pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
5915 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
5916 xfs_bmbt, block, i, dmxr);
5918 for (j = i+1; j <= INT_GET(block->bb_numrecs, ARCH_CONVERT); j++) {
5920 thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
5922 thispa = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
5923 xfs_bmbt, block, j, dmxr);
5925 if (INT_GET(*thispa, ARCH_CONVERT) ==
5926 INT_GET(*pp, ARCH_CONVERT)) {
5927 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
5929 INT_GET(*thispa, ARCH_CONVERT));
5930 panic("%s: ptrs are equal in node\n",
5938 * Check that the extents for the inode ip are in the right order in all
5943 xfs_bmap_check_leaf_extents(
5944 xfs_btree_cur_t *cur, /* btree cursor or null */
5945 xfs_inode_t *ip, /* incore inode pointer */
5946 int whichfork) /* data or attr fork */
5948 xfs_bmbt_block_t *block; /* current btree block */
5949 xfs_fsblock_t bno; /* block # of "block" */
5950 xfs_buf_t *bp; /* buffer for "block" */
5951 int error; /* error return value */
5952 xfs_extnum_t i=0; /* index into the extents list */
5953 xfs_ifork_t *ifp; /* fork structure */
5954 int level; /* btree level, for checking */
5955 xfs_mount_t *mp; /* file system mount structure */
5956 xfs_bmbt_ptr_t *pp; /* pointer to block address */
5957 xfs_bmbt_rec_t *ep, *lastp; /* extent pointers in block entry */
5960 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5966 ifp = XFS_IFORK_PTR(ip, whichfork);
5967 block = ifp->if_broot;
5969 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5971 ASSERT(INT_GET(block->bb_level, ARCH_CONVERT) > 0);
5972 level = INT_GET(block->bb_level, ARCH_CONVERT);
5973 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
5974 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
5975 ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
5976 ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
5977 ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
5978 bno = INT_GET(*pp, ARCH_CONVERT);
5980 * Go down the tree until leaf level is reached, following the first
5981 * pointer (leftmost) at each level.
5983 while (level-- > 0) {
5984 /* See if buf is in cur first */
5985 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5991 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5992 XFS_BMAP_BTREE_REF)))
5994 block = XFS_BUF_TO_BMBT_BLOCK(bp);
5995 XFS_WANT_CORRUPTED_GOTO(
5996 XFS_BMAP_SANITY_CHECK(mp, block, level),
6002 * Check this block for basic sanity (increasing keys and
6003 * no duplicate blocks).
6006 xfs_check_block(block, mp, 0, 0);
6007 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
6008 1, mp->m_bmap_dmxr[1]);
6009 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, INT_GET(*pp, ARCH_CONVERT)), error0);
6010 bno = INT_GET(*pp, ARCH_CONVERT);
6013 xfs_trans_brelse(NULL, bp);
6018 * Here with bp and block set to the leftmost leaf node in the tree.
6023 * Loop over all leaf nodes checking that all extents are in the right order.
6027 xfs_bmbt_rec_t *frp;
6028 xfs_fsblock_t nextbno;
6029 xfs_extnum_t num_recs;
6032 num_recs = INT_GET(block->bb_numrecs, ARCH_CONVERT);
6035 * Read-ahead the next leaf block, if any.
6038 nextbno = INT_GET(block->bb_rightsib, ARCH_CONVERT);
6041 * Check all the extents to make sure they are OK.
6042 * If we had a previous block, the last entry should
6043 * conform with the first entry in this one.
6046 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
6047 block, 1, mp->m_bmap_dmxr[0]);
6049 for (ep = frp;ep < frp + (num_recs - 1); ep++) {
6051 xfs_btree_check_rec(XFS_BTNUM_BMAP,
6052 (void *)lastp, (void *)ep);
6054 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
6057 lastp = frp + num_recs - 1; /* For the next iteration */
6062 xfs_trans_brelse(NULL, bp);
6066 * If we've reached the end, stop.
6068 if (bno == NULLFSBLOCK)
6071 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6077 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6078 XFS_BMAP_BTREE_REF)))
6080 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6084 xfs_trans_brelse(NULL, bp);
6089 cmn_err(CE_WARN, "%s: at error0", __FUNCTION__);
6091 xfs_trans_brelse(NULL, bp);
6093 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
6095 panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__);
6101 * Count fsblocks of the given fork.
6104 xfs_bmap_count_blocks(
6105 xfs_trans_t *tp, /* transaction pointer */
6106 xfs_inode_t *ip, /* incore inode */
6107 int whichfork, /* data or attr fork */
6108 int *count) /* out: count of blocks */
6110 xfs_bmbt_block_t *block; /* current btree block */
6111 xfs_fsblock_t bno; /* block # of "block" */
6112 xfs_ifork_t *ifp; /* fork structure */
6113 int level; /* btree level, for checking */
6114 xfs_mount_t *mp; /* file system mount structure */
6115 xfs_bmbt_ptr_t *pp; /* pointer to block address */
6119 ifp = XFS_IFORK_PTR(ip, whichfork);
6120 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6121 if (unlikely(xfs_bmap_count_leaves(ifp->if_u1.if_extents,
6122 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6124 XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)",
6125 XFS_ERRLEVEL_LOW, mp);
6126 return XFS_ERROR(EFSCORRUPTED);
6132 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6134 block = ifp->if_broot;
6135 ASSERT(INT_GET(block->bb_level, ARCH_CONVERT) > 0);
6136 level = INT_GET(block->bb_level, ARCH_CONVERT);
6137 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6138 ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
6139 ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
6140 ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
6141 bno = INT_GET(*pp, ARCH_CONVERT);
6143 if (unlikely(xfs_bmap_count_tree(mp, tp, bno, level, count) < 0)) {
6144 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6146 return XFS_ERROR(EFSCORRUPTED);
6153 * Recursively walks each level of a btree
6154 * to count total fsblocks is use.
6157 xfs_bmap_count_tree(
6158 xfs_mount_t *mp, /* file system mount point */
6159 xfs_trans_t *tp, /* transaction pointer */
6160 xfs_fsblock_t blockno, /* file system block number */
6161 int levelin, /* level in btree */
6162 int *count) /* Count of blocks */
6165 xfs_buf_t *bp, *nbp;
6166 int level = levelin;
6168 xfs_fsblock_t bno = blockno;
6169 xfs_fsblock_t nextbno;
6170 xfs_bmbt_block_t *block, *nextblock;
6172 xfs_bmbt_rec_t *frp;
6174 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6177 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6180 /* Not at node above leafs, count this level of nodes */
6181 nextbno = INT_GET(block->bb_rightsib, ARCH_CONVERT);
6182 while (nextbno != NULLFSBLOCK) {
6183 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6184 0, &nbp, XFS_BMAP_BTREE_REF)))
6187 nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
6188 nextbno = INT_GET(nextblock->bb_rightsib, ARCH_CONVERT);
6189 xfs_trans_brelse(tp, nbp);
6192 /* Dive to the next level */
6193 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6194 xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
6195 bno = INT_GET(*pp, ARCH_CONVERT);
6196 if (unlikely((error =
6197 xfs_bmap_count_tree(mp, tp, bno, level, count)) < 0)) {
6198 xfs_trans_brelse(tp, bp);
6199 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6200 XFS_ERRLEVEL_LOW, mp);
6201 return XFS_ERROR(EFSCORRUPTED);
6203 xfs_trans_brelse(tp, bp);
6205 /* count all level 1 nodes and their leaves */
6207 nextbno = INT_GET(block->bb_rightsib, ARCH_CONVERT);
6208 numrecs = INT_GET(block->bb_numrecs, ARCH_CONVERT);
6209 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize,
6210 xfs_bmbt, block, 1, mp->m_bmap_dmxr[0]);
6211 if (unlikely(xfs_bmap_count_leaves(frp, numrecs, count) < 0)) {
6212 xfs_trans_brelse(tp, bp);
6213 XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
6214 XFS_ERRLEVEL_LOW, mp);
6215 return XFS_ERROR(EFSCORRUPTED);
6217 xfs_trans_brelse(tp, bp);
6218 if (nextbno == NULLFSBLOCK)
6221 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6222 XFS_BMAP_BTREE_REF)))
6225 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6232 * Count leaf blocks given a pointer to an extent list.
6235 xfs_bmap_count_leaves(
6236 xfs_bmbt_rec_t *frp,
6242 for ( b = 1; b <= numrecs; b++, frp++)
6243 *count += xfs_bmbt_disk_get_blockcount(frp);