2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir_sf.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
38 #include "xfs_btree.h"
39 #include "xfs_dmapi.h"
40 #include "xfs_mount.h"
41 #include "xfs_ialloc.h"
42 #include "xfs_itable.h"
43 #include "xfs_inode_item.h"
44 #include "xfs_extfree_item.h"
45 #include "xfs_alloc.h"
47 #include "xfs_rtalloc.h"
48 #include "xfs_error.h"
49 #include "xfs_dir_leaf.h"
50 #include "xfs_attr_leaf.h"
52 #include "xfs_quota.h"
53 #include "xfs_trans_space.h"
54 #include "xfs_buf_item.h"
59 xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
62 kmem_zone_t *xfs_bmap_free_item_zone;
65 * Prototypes for internal bmap routines.
70 * Called from xfs_bmap_add_attrfork to handle extents format files.
72 STATIC int /* error */
73 xfs_bmap_add_attrfork_extents(
74 xfs_trans_t *tp, /* transaction pointer */
75 xfs_inode_t *ip, /* incore inode pointer */
76 xfs_fsblock_t *firstblock, /* first block allocated */
77 xfs_bmap_free_t *flist, /* blocks to free at commit */
78 int *flags); /* inode logging flags */
81 * Called from xfs_bmap_add_attrfork to handle local format files.
83 STATIC int /* error */
84 xfs_bmap_add_attrfork_local(
85 xfs_trans_t *tp, /* transaction pointer */
86 xfs_inode_t *ip, /* incore inode pointer */
87 xfs_fsblock_t *firstblock, /* first block allocated */
88 xfs_bmap_free_t *flist, /* blocks to free at commit */
89 int *flags); /* inode logging flags */
92 * Called by xfs_bmapi to update file extent records and the btree
93 * after allocating space (or doing a delayed allocation).
95 STATIC int /* error */
97 xfs_inode_t *ip, /* incore inode pointer */
98 xfs_extnum_t idx, /* extent number to update/insert */
99 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
100 xfs_bmbt_irec_t *new, /* new data to add to file extents */
101 xfs_fsblock_t *first, /* pointer to firstblock variable */
102 xfs_bmap_free_t *flist, /* list of extents to be freed */
103 int *logflagsp, /* inode logging flags */
104 int whichfork, /* data or attr fork */
105 int rsvd); /* OK to allocate reserved blocks */
108 * Called by xfs_bmap_add_extent to handle cases converting a delayed
109 * allocation to a real allocation.
111 STATIC int /* error */
112 xfs_bmap_add_extent_delay_real(
113 xfs_inode_t *ip, /* incore inode pointer */
114 xfs_extnum_t idx, /* extent number to update/insert */
115 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
116 xfs_bmbt_irec_t *new, /* new data to add to file extents */
117 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
118 xfs_fsblock_t *first, /* pointer to firstblock variable */
119 xfs_bmap_free_t *flist, /* list of extents to be freed */
120 int *logflagsp, /* inode logging flags */
121 int rsvd); /* OK to allocate reserved blocks */
124 * Called by xfs_bmap_add_extent to handle cases converting a hole
125 * to a delayed allocation.
127 STATIC int /* error */
128 xfs_bmap_add_extent_hole_delay(
129 xfs_inode_t *ip, /* incore inode pointer */
130 xfs_extnum_t idx, /* extent number to update/insert */
131 xfs_btree_cur_t *cur, /* if null, not a btree */
132 xfs_bmbt_irec_t *new, /* new data to add to file extents */
133 int *logflagsp,/* inode logging flags */
134 int rsvd); /* OK to allocate reserved blocks */
137 * Called by xfs_bmap_add_extent to handle cases converting a hole
138 * to a real allocation.
140 STATIC int /* error */
141 xfs_bmap_add_extent_hole_real(
142 xfs_inode_t *ip, /* incore inode pointer */
143 xfs_extnum_t idx, /* extent number to update/insert */
144 xfs_btree_cur_t *cur, /* if null, not a btree */
145 xfs_bmbt_irec_t *new, /* new data to add to file extents */
146 int *logflagsp, /* inode logging flags */
147 int whichfork); /* data or attr fork */
150 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
151 * allocation to a real allocation or vice versa.
153 STATIC int /* error */
154 xfs_bmap_add_extent_unwritten_real(
155 xfs_inode_t *ip, /* incore inode pointer */
156 xfs_extnum_t idx, /* extent number to update/insert */
157 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
158 xfs_bmbt_irec_t *new, /* new data to add to file extents */
159 int *logflagsp); /* inode logging flags */
162 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
163 * It figures out where to ask the underlying allocator to put the new extent.
165 STATIC int /* error */
167 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
170 * Transform a btree format file with only one leaf node, where the
171 * extents list will fit in the inode, into an extents format file.
172 * Since the file extents are already in-core, all we have to do is
173 * give up the space for the btree root and pitch the leaf block.
175 STATIC int /* error */
176 xfs_bmap_btree_to_extents(
177 xfs_trans_t *tp, /* transaction pointer */
178 xfs_inode_t *ip, /* incore inode pointer */
179 xfs_btree_cur_t *cur, /* btree cursor */
180 int *logflagsp, /* inode logging flags */
181 int whichfork); /* data or attr fork */
185 * Check that the extents list for the inode ip is in the right order.
188 xfs_bmap_check_extents(
189 xfs_inode_t *ip, /* incore inode pointer */
190 int whichfork); /* data or attr fork */
194 * Called by xfs_bmapi to update file extent records and the btree
195 * after removing space (or undoing a delayed allocation).
197 STATIC int /* error */
199 xfs_inode_t *ip, /* incore inode pointer */
200 xfs_trans_t *tp, /* current trans pointer */
201 xfs_extnum_t idx, /* extent number to update/insert */
202 xfs_bmap_free_t *flist, /* list of extents to be freed */
203 xfs_btree_cur_t *cur, /* if null, not a btree */
204 xfs_bmbt_irec_t *new, /* new data to add to file extents */
205 int *logflagsp,/* inode logging flags */
206 int whichfork, /* data or attr fork */
207 int rsvd); /* OK to allocate reserved blocks */
210 * Remove the entry "free" from the free item list. Prev points to the
211 * previous entry, unless "free" is the head of the list.
215 xfs_bmap_free_t *flist, /* free item list header */
216 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
217 xfs_bmap_free_item_t *free); /* list item to be freed */
220 * Convert an extents-format file into a btree-format file.
221 * The new file will have a root block (in the inode) and a single child block.
223 STATIC int /* error */
224 xfs_bmap_extents_to_btree(
225 xfs_trans_t *tp, /* transaction pointer */
226 xfs_inode_t *ip, /* incore inode pointer */
227 xfs_fsblock_t *firstblock, /* first-block-allocated */
228 xfs_bmap_free_t *flist, /* blocks freed in xaction */
229 xfs_btree_cur_t **curp, /* cursor returned to caller */
230 int wasdel, /* converting a delayed alloc */
231 int *logflagsp, /* inode logging flags */
232 int whichfork); /* data or attr fork */
235 * Convert a local file to an extents file.
236 * This code is sort of bogus, since the file data needs to get
237 * logged so it won't be lost. The bmap-level manipulations are ok, though.
239 STATIC int /* error */
240 xfs_bmap_local_to_extents(
241 xfs_trans_t *tp, /* transaction pointer */
242 xfs_inode_t *ip, /* incore inode pointer */
243 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
244 xfs_extlen_t total, /* total blocks needed by transaction */
245 int *logflagsp, /* inode logging flags */
246 int whichfork); /* data or attr fork */
249 * Search the extents list for the inode, for the extent containing bno.
250 * If bno lies in a hole, point to the next entry. If bno lies past eof,
251 * *eofp will be set, and *prevp will contain the last entry (null if none).
252 * Else, *lastxp will be set to the index of the found
253 * entry; *gotp will contain the entry.
255 STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
256 xfs_bmap_search_extents(
257 xfs_inode_t *ip, /* incore inode pointer */
258 xfs_fileoff_t bno, /* block number searched for */
259 int whichfork, /* data or attr fork */
260 int *eofp, /* out: end of file found */
261 xfs_extnum_t *lastxp, /* out: last extent index */
262 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
263 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
266 * Check the last inode extent to determine whether this allocation will result
267 * in blocks being allocated at the end of the file. When we allocate new data
268 * blocks at the end of the file which do not start at the previous data block,
269 * we will try to align the new blocks at stripe unit boundaries.
271 STATIC int /* error */
273 xfs_inode_t *ip, /* incore inode pointer */
274 xfs_fileoff_t off, /* file offset in fsblocks */
275 int whichfork, /* data or attribute fork */
276 char *aeof); /* return value */
278 #ifdef XFS_BMAP_TRACE
280 * Add a bmap trace buffer entry. Base routine for the others.
283 xfs_bmap_trace_addentry(
284 int opcode, /* operation */
285 char *fname, /* function name */
286 char *desc, /* operation description */
287 xfs_inode_t *ip, /* incore inode pointer */
288 xfs_extnum_t idx, /* index of entry(ies) */
289 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
290 xfs_bmbt_rec_t *r1, /* first record */
291 xfs_bmbt_rec_t *r2, /* second record or null */
292 int whichfork); /* data or attr fork */
295 * Add bmap trace entry prior to a call to xfs_iext_remove.
298 xfs_bmap_trace_delete(
299 char *fname, /* function name */
300 char *desc, /* operation description */
301 xfs_inode_t *ip, /* incore inode pointer */
302 xfs_extnum_t idx, /* index of entry(entries) deleted */
303 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
304 int whichfork); /* data or attr fork */
307 * Add bmap trace entry prior to a call to xfs_iext_insert, or
308 * reading in the extents list from the disk (in the btree).
311 xfs_bmap_trace_insert(
312 char *fname, /* function name */
313 char *desc, /* operation description */
314 xfs_inode_t *ip, /* incore inode pointer */
315 xfs_extnum_t idx, /* index of entry(entries) inserted */
316 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
317 xfs_bmbt_irec_t *r1, /* inserted record 1 */
318 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
319 int whichfork); /* data or attr fork */
322 * Add bmap trace entry after updating an extent record in place.
325 xfs_bmap_trace_post_update(
326 char *fname, /* function name */
327 char *desc, /* operation description */
328 xfs_inode_t *ip, /* incore inode pointer */
329 xfs_extnum_t idx, /* index of entry updated */
330 int whichfork); /* data or attr fork */
333 * Add bmap trace entry prior to updating an extent record in place.
336 xfs_bmap_trace_pre_update(
337 char *fname, /* function name */
338 char *desc, /* operation description */
339 xfs_inode_t *ip, /* incore inode pointer */
340 xfs_extnum_t idx, /* index of entry to be updated */
341 int whichfork); /* data or attr fork */
344 #define xfs_bmap_trace_delete(f,d,ip,i,c,w)
345 #define xfs_bmap_trace_insert(f,d,ip,i,c,r1,r2,w)
346 #define xfs_bmap_trace_post_update(f,d,ip,i,w)
347 #define xfs_bmap_trace_pre_update(f,d,ip,i,w)
348 #endif /* XFS_BMAP_TRACE */
351 * Compute the worst-case number of indirect blocks that will be used
352 * for ip's delayed extent of length "len".
355 xfs_bmap_worst_indlen(
356 xfs_inode_t *ip, /* incore inode pointer */
357 xfs_filblks_t len); /* delayed extent length */
361 * Perform various validation checks on the values being returned
365 xfs_bmap_validate_ret(
369 xfs_bmbt_irec_t *mval,
373 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
376 #if defined(XFS_RW_TRACE)
385 #define xfs_bunmap_trace(ip, bno, len, flags, ra)
386 #endif /* XFS_RW_TRACE */
393 xfs_fsblock_t blockno,
398 xfs_bmap_count_leaves(
405 xfs_bmap_disk_count_leaves(
409 xfs_bmbt_block_t *block,
414 * Bmap internal routines.
418 * Called from xfs_bmap_add_attrfork to handle btree format files.
420 STATIC int /* error */
421 xfs_bmap_add_attrfork_btree(
422 xfs_trans_t *tp, /* transaction pointer */
423 xfs_inode_t *ip, /* incore inode pointer */
424 xfs_fsblock_t *firstblock, /* first block allocated */
425 xfs_bmap_free_t *flist, /* blocks to free at commit */
426 int *flags) /* inode logging flags */
428 xfs_btree_cur_t *cur; /* btree cursor */
429 int error; /* error return value */
430 xfs_mount_t *mp; /* file system mount struct */
431 int stat; /* newroot status */
434 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
435 *flags |= XFS_ILOG_DBROOT;
437 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
439 cur->bc_private.b.flist = flist;
440 cur->bc_private.b.firstblock = *firstblock;
441 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
443 ASSERT(stat == 1); /* must be at least one entry */
444 if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
447 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
448 return XFS_ERROR(ENOSPC);
450 *firstblock = cur->bc_private.b.firstblock;
451 cur->bc_private.b.allocated = 0;
452 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
456 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
461 * Called from xfs_bmap_add_attrfork to handle extents format files.
463 STATIC int /* error */
464 xfs_bmap_add_attrfork_extents(
465 xfs_trans_t *tp, /* transaction pointer */
466 xfs_inode_t *ip, /* incore inode pointer */
467 xfs_fsblock_t *firstblock, /* first block allocated */
468 xfs_bmap_free_t *flist, /* blocks to free at commit */
469 int *flags) /* inode logging flags */
471 xfs_btree_cur_t *cur; /* bmap btree cursor */
472 int error; /* error return value */
474 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
477 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
478 flags, XFS_DATA_FORK);
480 cur->bc_private.b.allocated = 0;
481 xfs_btree_del_cursor(cur,
482 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
488 * Called from xfs_bmap_add_attrfork to handle local format files.
490 STATIC int /* error */
491 xfs_bmap_add_attrfork_local(
492 xfs_trans_t *tp, /* transaction pointer */
493 xfs_inode_t *ip, /* incore inode pointer */
494 xfs_fsblock_t *firstblock, /* first block allocated */
495 xfs_bmap_free_t *flist, /* blocks to free at commit */
496 int *flags) /* inode logging flags */
498 xfs_da_args_t dargs; /* args for dir/attr code */
499 int error; /* error return value */
500 xfs_mount_t *mp; /* mount structure pointer */
502 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
504 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
506 memset(&dargs, 0, sizeof(dargs));
508 dargs.firstblock = firstblock;
510 dargs.total = mp->m_dirblkfsbs;
511 dargs.whichfork = XFS_DATA_FORK;
513 error = XFS_DIR_SHORTFORM_TO_SINGLE(mp, &dargs);
515 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
521 * Called by xfs_bmapi to update file extent records and the btree
522 * after allocating space (or doing a delayed allocation).
524 STATIC int /* error */
526 xfs_inode_t *ip, /* incore inode pointer */
527 xfs_extnum_t idx, /* extent number to update/insert */
528 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
529 xfs_bmbt_irec_t *new, /* new data to add to file extents */
530 xfs_fsblock_t *first, /* pointer to firstblock variable */
531 xfs_bmap_free_t *flist, /* list of extents to be freed */
532 int *logflagsp, /* inode logging flags */
533 int whichfork, /* data or attr fork */
534 int rsvd) /* OK to use reserved data blocks */
536 xfs_btree_cur_t *cur; /* btree cursor or null */
537 xfs_filblks_t da_new; /* new count del alloc blocks used */
538 xfs_filblks_t da_old; /* old count del alloc blocks used */
539 int error; /* error return value */
540 #ifdef XFS_BMAP_TRACE
541 static char fname[] = "xfs_bmap_add_extent";
543 xfs_ifork_t *ifp; /* inode fork ptr */
544 int logflags; /* returned value */
545 xfs_extnum_t nextents; /* number of extents in file now */
547 XFS_STATS_INC(xs_add_exlist);
549 ifp = XFS_IFORK_PTR(ip, whichfork);
550 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
551 ASSERT(idx <= nextents);
555 * This is the first extent added to a new/empty file.
556 * Special case this one, so other routines get to assume there are
557 * already extents in the list.
560 xfs_bmap_trace_insert(fname, "insert empty", ip, 0, 1, new,
562 xfs_iext_insert(ifp, 0, 1, new);
565 if (!ISNULLSTARTBLOCK(new->br_startblock)) {
566 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
567 logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
572 * Any kind of new delayed allocation goes here.
574 else if (ISNULLSTARTBLOCK(new->br_startblock)) {
576 ASSERT((cur->bc_private.b.flags &
577 XFS_BTCUR_BPRV_WASDEL) == 0);
578 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, cur, new,
583 * Real allocation off the end of the file.
585 else if (idx == nextents) {
587 ASSERT((cur->bc_private.b.flags &
588 XFS_BTCUR_BPRV_WASDEL) == 0);
589 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
590 &logflags, whichfork)))
593 xfs_bmbt_irec_t prev; /* old extent at offset idx */
596 * Get the record referred to by idx.
598 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
600 * If it's a real allocation record, and the new allocation ends
601 * after the start of the referred to record, then we're filling
602 * in a delayed or unwritten allocation with a real one, or
603 * converting real back to unwritten.
605 if (!ISNULLSTARTBLOCK(new->br_startblock) &&
606 new->br_startoff + new->br_blockcount > prev.br_startoff) {
607 if (prev.br_state != XFS_EXT_UNWRITTEN &&
608 ISNULLSTARTBLOCK(prev.br_startblock)) {
609 da_old = STARTBLOCKVAL(prev.br_startblock);
611 ASSERT(cur->bc_private.b.flags &
612 XFS_BTCUR_BPRV_WASDEL);
613 if ((error = xfs_bmap_add_extent_delay_real(ip,
614 idx, &cur, new, &da_new, first, flist,
617 } else if (new->br_state == XFS_EXT_NORM) {
618 ASSERT(new->br_state == XFS_EXT_NORM);
619 if ((error = xfs_bmap_add_extent_unwritten_real(
620 ip, idx, &cur, new, &logflags)))
623 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
624 if ((error = xfs_bmap_add_extent_unwritten_real(
625 ip, idx, &cur, new, &logflags)))
628 ASSERT(*curp == cur || *curp == NULL);
631 * Otherwise we're filling in a hole with an allocation.
635 ASSERT((cur->bc_private.b.flags &
636 XFS_BTCUR_BPRV_WASDEL) == 0);
637 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
638 new, &logflags, whichfork)))
643 ASSERT(*curp == cur || *curp == NULL);
645 * Convert to a btree if necessary.
647 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
648 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
649 int tmp_logflags; /* partial log flag return val */
652 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
653 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
654 logflags |= tmp_logflags;
659 * Adjust for changes in reserved delayed indirect blocks.
660 * Nothing to do for disk quotas here.
662 if (da_old || da_new) {
667 nblks += cur->bc_private.b.allocated;
668 ASSERT(nblks <= da_old);
670 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
671 (int)(da_old - nblks), rsvd);
674 * Clear out the allocated field, done with it now in any case.
677 cur->bc_private.b.allocated = 0;
683 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
685 *logflagsp = logflags;
690 * Called by xfs_bmap_add_extent to handle cases converting a delayed
691 * allocation to a real allocation.
693 STATIC int /* error */
694 xfs_bmap_add_extent_delay_real(
695 xfs_inode_t *ip, /* incore inode pointer */
696 xfs_extnum_t idx, /* extent number to update/insert */
697 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
698 xfs_bmbt_irec_t *new, /* new data to add to file extents */
699 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
700 xfs_fsblock_t *first, /* pointer to firstblock variable */
701 xfs_bmap_free_t *flist, /* list of extents to be freed */
702 int *logflagsp, /* inode logging flags */
703 int rsvd) /* OK to use reserved data block allocation */
705 xfs_btree_cur_t *cur; /* btree cursor */
706 int diff; /* temp value */
707 xfs_bmbt_rec_t *ep; /* extent entry for idx */
708 int error; /* error return value */
709 #ifdef XFS_BMAP_TRACE
710 static char fname[] = "xfs_bmap_add_extent_delay_real";
712 int i; /* temp state */
713 xfs_ifork_t *ifp; /* inode fork pointer */
714 xfs_fileoff_t new_endoff; /* end offset of new entry */
715 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
716 /* left is 0, right is 1, prev is 2 */
717 int rval=0; /* return value (logging flags) */
718 int state = 0;/* state bits, accessed thru macros */
719 xfs_filblks_t temp; /* value for dnew calculations */
720 xfs_filblks_t temp2; /* value for dnew calculations */
721 int tmp_rval; /* partial logging flags */
722 enum { /* bit number definitions for state */
723 LEFT_CONTIG, RIGHT_CONTIG,
724 LEFT_FILLING, RIGHT_FILLING,
725 LEFT_DELAY, RIGHT_DELAY,
726 LEFT_VALID, RIGHT_VALID
732 #define MASK(b) (1 << (b))
733 #define MASK2(a,b) (MASK(a) | MASK(b))
734 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
735 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
736 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
737 #define STATE_TEST(b) (state & MASK(b))
738 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
739 ((state &= ~MASK(b)), 0))
740 #define SWITCH_STATE \
741 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
744 * Set up a bunch of variables to make the tests simpler.
747 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
748 ep = xfs_iext_get_ext(ifp, idx);
749 xfs_bmbt_get_all(ep, &PREV);
750 new_endoff = new->br_startoff + new->br_blockcount;
751 ASSERT(PREV.br_startoff <= new->br_startoff);
752 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
754 * Set flags determining what part of the previous delayed allocation
755 * extent is being replaced by a real allocation.
757 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
758 STATE_SET(RIGHT_FILLING,
759 PREV.br_startoff + PREV.br_blockcount == new_endoff);
761 * Check and set flags if this segment has a left neighbor.
762 * Don't set contiguous if the combined extent would be too large.
764 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
765 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
766 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
768 STATE_SET(LEFT_CONTIG,
769 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
770 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
771 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
772 LEFT.br_state == new->br_state &&
773 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
775 * Check and set flags if this segment has a right neighbor.
776 * Don't set contiguous if the combined extent would be too large.
777 * Also check for all-three-contiguous being too large.
779 if (STATE_SET_TEST(RIGHT_VALID,
781 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
782 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
783 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
785 STATE_SET(RIGHT_CONTIG,
786 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
787 new_endoff == RIGHT.br_startoff &&
788 new->br_startblock + new->br_blockcount ==
789 RIGHT.br_startblock &&
790 new->br_state == RIGHT.br_state &&
791 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
792 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
793 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
794 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
798 * Switch out based on the FILLING and CONTIG state bits.
800 switch (SWITCH_STATE) {
802 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
804 * Filling in all of a previously delayed allocation extent.
805 * The left and right neighbors are both contiguous with new.
807 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
809 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
810 LEFT.br_blockcount + PREV.br_blockcount +
811 RIGHT.br_blockcount);
812 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
814 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
816 xfs_iext_remove(ifp, idx, 2);
817 ip->i_df.if_lastex = idx - 1;
818 ip->i_d.di_nextents--;
820 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
822 rval = XFS_ILOG_CORE;
823 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
825 RIGHT.br_blockcount, &i)))
828 if ((error = xfs_bmbt_delete(cur, &i)))
831 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
834 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
838 RIGHT.br_blockcount, LEFT.br_state)))
844 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
846 * Filling in all of a previously delayed allocation extent.
847 * The left neighbor is contiguous, the right is not.
849 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
851 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
852 LEFT.br_blockcount + PREV.br_blockcount);
853 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
855 ip->i_df.if_lastex = idx - 1;
856 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
858 xfs_iext_remove(ifp, idx, 1);
860 rval = XFS_ILOG_DEXT;
863 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
864 LEFT.br_startblock, LEFT.br_blockcount,
868 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
871 PREV.br_blockcount, LEFT.br_state)))
877 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
879 * Filling in all of a previously delayed allocation extent.
880 * The right neighbor is contiguous, the left is not.
882 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
884 xfs_bmbt_set_startblock(ep, new->br_startblock);
885 xfs_bmbt_set_blockcount(ep,
886 PREV.br_blockcount + RIGHT.br_blockcount);
887 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
889 ip->i_df.if_lastex = idx;
890 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
892 xfs_iext_remove(ifp, idx + 1, 1);
894 rval = XFS_ILOG_DEXT;
897 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
899 RIGHT.br_blockcount, &i)))
902 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
905 RIGHT.br_blockcount, PREV.br_state)))
911 case MASK2(LEFT_FILLING, RIGHT_FILLING):
913 * Filling in all of a previously delayed allocation extent.
914 * Neither the left nor right neighbors are contiguous with
917 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
919 xfs_bmbt_set_startblock(ep, new->br_startblock);
920 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
922 ip->i_df.if_lastex = idx;
923 ip->i_d.di_nextents++;
925 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
927 rval = XFS_ILOG_CORE;
928 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
929 new->br_startblock, new->br_blockcount,
933 cur->bc_rec.b.br_state = XFS_EXT_NORM;
934 if ((error = xfs_bmbt_insert(cur, &i)))
941 case MASK2(LEFT_FILLING, LEFT_CONTIG):
943 * Filling in the first part of a previous delayed allocation.
944 * The left neighbor is contiguous.
946 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
948 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
949 LEFT.br_blockcount + new->br_blockcount);
950 xfs_bmbt_set_startoff(ep,
951 PREV.br_startoff + new->br_blockcount);
952 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
954 temp = PREV.br_blockcount - new->br_blockcount;
955 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
957 xfs_bmbt_set_blockcount(ep, temp);
958 ip->i_df.if_lastex = idx - 1;
960 rval = XFS_ILOG_DEXT;
963 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
964 LEFT.br_startblock, LEFT.br_blockcount,
968 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
975 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
976 STARTBLOCKVAL(PREV.br_startblock));
977 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
978 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
983 case MASK(LEFT_FILLING):
985 * Filling in the first part of a previous delayed allocation.
986 * The left neighbor is not contiguous.
988 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
989 xfs_bmbt_set_startoff(ep, new_endoff);
990 temp = PREV.br_blockcount - new->br_blockcount;
991 xfs_bmbt_set_blockcount(ep, temp);
992 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
994 xfs_iext_insert(ifp, idx, 1, new);
995 ip->i_df.if_lastex = idx;
996 ip->i_d.di_nextents++;
998 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1000 rval = XFS_ILOG_CORE;
1001 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1002 new->br_startblock, new->br_blockcount,
1006 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1007 if ((error = xfs_bmbt_insert(cur, &i)))
1011 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1012 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1013 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1014 first, flist, &cur, 1, &tmp_rval,
1020 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1021 STARTBLOCKVAL(PREV.br_startblock) -
1022 (cur ? cur->bc_private.b.allocated : 0));
1023 ep = xfs_iext_get_ext(ifp, idx + 1);
1024 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1025 xfs_bmap_trace_post_update(fname, "LF", ip, idx + 1,
1030 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1032 * Filling in the last part of a previous delayed allocation.
1033 * The right neighbor is contiguous with the new allocation.
1035 temp = PREV.br_blockcount - new->br_blockcount;
1036 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1038 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1040 xfs_bmbt_set_blockcount(ep, temp);
1041 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1042 new->br_startoff, new->br_startblock,
1043 new->br_blockcount + RIGHT.br_blockcount,
1045 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1047 ip->i_df.if_lastex = idx + 1;
1049 rval = XFS_ILOG_DEXT;
1052 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1053 RIGHT.br_startblock,
1054 RIGHT.br_blockcount, &i)))
1057 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1059 new->br_blockcount +
1060 RIGHT.br_blockcount,
1064 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1065 STARTBLOCKVAL(PREV.br_startblock));
1066 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1067 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1072 case MASK(RIGHT_FILLING):
1074 * Filling in the last part of a previous delayed allocation.
1075 * The right neighbor is not contiguous.
1077 temp = PREV.br_blockcount - new->br_blockcount;
1078 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1079 xfs_bmbt_set_blockcount(ep, temp);
1080 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1081 new, NULL, XFS_DATA_FORK);
1082 xfs_iext_insert(ifp, idx + 1, 1, new);
1083 ip->i_df.if_lastex = idx + 1;
1084 ip->i_d.di_nextents++;
1086 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1088 rval = XFS_ILOG_CORE;
1089 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1090 new->br_startblock, new->br_blockcount,
1094 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1095 if ((error = xfs_bmbt_insert(cur, &i)))
1099 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1100 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1101 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1102 first, flist, &cur, 1, &tmp_rval,
1108 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1109 STARTBLOCKVAL(PREV.br_startblock) -
1110 (cur ? cur->bc_private.b.allocated : 0));
1111 ep = xfs_iext_get_ext(ifp, idx);
1112 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1113 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1119 * Filling in the middle part of a previous delayed allocation.
1120 * Contiguity is impossible here.
1121 * This case is avoided almost all the time.
1123 temp = new->br_startoff - PREV.br_startoff;
1124 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1125 xfs_bmbt_set_blockcount(ep, temp);
1127 r[1].br_startoff = new_endoff;
1128 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1129 r[1].br_blockcount = temp2;
1130 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1132 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1133 ip->i_df.if_lastex = idx + 1;
1134 ip->i_d.di_nextents++;
1136 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1138 rval = XFS_ILOG_CORE;
1139 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1140 new->br_startblock, new->br_blockcount,
1144 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1145 if ((error = xfs_bmbt_insert(cur, &i)))
1149 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1150 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1151 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1152 first, flist, &cur, 1, &tmp_rval,
1158 temp = xfs_bmap_worst_indlen(ip, temp);
1159 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1160 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
1161 (cur ? cur->bc_private.b.allocated : 0));
1163 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -diff, rsvd)) {
1165 * Ick gross gag me with a spoon.
1167 ASSERT(0); /* want to see if this ever happens! */
1173 !xfs_mod_incore_sb(ip->i_mount,
1174 XFS_SBS_FDBLOCKS, -diff, rsvd))
1181 !xfs_mod_incore_sb(ip->i_mount,
1182 XFS_SBS_FDBLOCKS, -diff, rsvd))
1187 ep = xfs_iext_get_ext(ifp, idx);
1188 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1189 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1190 xfs_bmap_trace_pre_update(fname, "0", ip, idx + 2,
1192 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1193 NULLSTARTBLOCK((int)temp2));
1194 xfs_bmap_trace_post_update(fname, "0", ip, idx + 2,
1196 *dnew = temp + temp2;
1199 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1200 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1201 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1202 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1203 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1204 case MASK(LEFT_CONTIG):
1205 case MASK(RIGHT_CONTIG):
1207 * These cases are all impossible.
1224 #undef STATE_SET_TEST
1229 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1230 * allocation to a real allocation or vice versa.
1232 STATIC int /* error */
1233 xfs_bmap_add_extent_unwritten_real(
1234 xfs_inode_t *ip, /* incore inode pointer */
1235 xfs_extnum_t idx, /* extent number to update/insert */
1236 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1237 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1238 int *logflagsp) /* inode logging flags */
1240 xfs_btree_cur_t *cur; /* btree cursor */
1241 xfs_bmbt_rec_t *ep; /* extent entry for idx */
1242 int error; /* error return value */
1243 #ifdef XFS_BMAP_TRACE
1244 static char fname[] = "xfs_bmap_add_extent_unwritten_real";
1246 int i; /* temp state */
1247 xfs_ifork_t *ifp; /* inode fork pointer */
1248 xfs_fileoff_t new_endoff; /* end offset of new entry */
1249 xfs_exntst_t newext; /* new extent state */
1250 xfs_exntst_t oldext; /* old extent state */
1251 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1252 /* left is 0, right is 1, prev is 2 */
1253 int rval=0; /* return value (logging flags) */
1254 int state = 0;/* state bits, accessed thru macros */
1255 enum { /* bit number definitions for state */
1256 LEFT_CONTIG, RIGHT_CONTIG,
1257 LEFT_FILLING, RIGHT_FILLING,
1258 LEFT_DELAY, RIGHT_DELAY,
1259 LEFT_VALID, RIGHT_VALID
1265 #define MASK(b) (1 << (b))
1266 #define MASK2(a,b) (MASK(a) | MASK(b))
1267 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1268 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1269 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1270 #define STATE_TEST(b) (state & MASK(b))
1271 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1272 ((state &= ~MASK(b)), 0))
1273 #define SWITCH_STATE \
1274 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1277 * Set up a bunch of variables to make the tests simpler.
1281 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1282 ep = xfs_iext_get_ext(ifp, idx);
1283 xfs_bmbt_get_all(ep, &PREV);
1284 newext = new->br_state;
1285 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1286 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1287 ASSERT(PREV.br_state == oldext);
1288 new_endoff = new->br_startoff + new->br_blockcount;
1289 ASSERT(PREV.br_startoff <= new->br_startoff);
1290 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1292 * Set flags determining what part of the previous oldext allocation
1293 * extent is being replaced by a newext allocation.
1295 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
1296 STATE_SET(RIGHT_FILLING,
1297 PREV.br_startoff + PREV.br_blockcount == new_endoff);
1299 * Check and set flags if this segment has a left neighbor.
1300 * Don't set contiguous if the combined extent would be too large.
1302 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1303 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1304 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
1306 STATE_SET(LEFT_CONTIG,
1307 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1308 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1309 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1310 LEFT.br_state == newext &&
1311 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1313 * Check and set flags if this segment has a right neighbor.
1314 * Don't set contiguous if the combined extent would be too large.
1315 * Also check for all-three-contiguous being too large.
1317 if (STATE_SET_TEST(RIGHT_VALID,
1319 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
1320 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1321 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
1323 STATE_SET(RIGHT_CONTIG,
1324 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1325 new_endoff == RIGHT.br_startoff &&
1326 new->br_startblock + new->br_blockcount ==
1327 RIGHT.br_startblock &&
1328 newext == RIGHT.br_state &&
1329 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1330 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
1331 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
1332 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1335 * Switch out based on the FILLING and CONTIG state bits.
1337 switch (SWITCH_STATE) {
1339 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1341 * Setting all of a previous oldext extent to newext.
1342 * The left and right neighbors are both contiguous with new.
1344 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1346 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1347 LEFT.br_blockcount + PREV.br_blockcount +
1348 RIGHT.br_blockcount);
1349 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1351 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
1353 xfs_iext_remove(ifp, idx, 2);
1354 ip->i_df.if_lastex = idx - 1;
1355 ip->i_d.di_nextents -= 2;
1357 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1359 rval = XFS_ILOG_CORE;
1360 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1361 RIGHT.br_startblock,
1362 RIGHT.br_blockcount, &i)))
1365 if ((error = xfs_bmbt_delete(cur, &i)))
1368 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1371 if ((error = xfs_bmbt_delete(cur, &i)))
1374 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1377 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1379 LEFT.br_blockcount + PREV.br_blockcount +
1380 RIGHT.br_blockcount, LEFT.br_state)))
1385 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
1387 * Setting all of a previous oldext extent to newext.
1388 * The left neighbor is contiguous, the right is not.
1390 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
1392 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1393 LEFT.br_blockcount + PREV.br_blockcount);
1394 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
1396 ip->i_df.if_lastex = idx - 1;
1397 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
1399 xfs_iext_remove(ifp, idx, 1);
1400 ip->i_d.di_nextents--;
1402 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1404 rval = XFS_ILOG_CORE;
1405 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1406 PREV.br_startblock, PREV.br_blockcount,
1410 if ((error = xfs_bmbt_delete(cur, &i)))
1413 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1416 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1418 LEFT.br_blockcount + PREV.br_blockcount,
1424 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
1426 * Setting all of a previous oldext extent to newext.
1427 * The right neighbor is contiguous, the left is not.
1429 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
1431 xfs_bmbt_set_blockcount(ep,
1432 PREV.br_blockcount + RIGHT.br_blockcount);
1433 xfs_bmbt_set_state(ep, newext);
1434 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
1436 ip->i_df.if_lastex = idx;
1437 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
1439 xfs_iext_remove(ifp, idx + 1, 1);
1440 ip->i_d.di_nextents--;
1442 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1444 rval = XFS_ILOG_CORE;
1445 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1446 RIGHT.br_startblock,
1447 RIGHT.br_blockcount, &i)))
1450 if ((error = xfs_bmbt_delete(cur, &i)))
1453 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1456 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1458 new->br_blockcount + RIGHT.br_blockcount,
1464 case MASK2(LEFT_FILLING, RIGHT_FILLING):
1466 * Setting all of a previous oldext extent to newext.
1467 * Neither the left nor right neighbors are contiguous with
1470 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
1472 xfs_bmbt_set_state(ep, newext);
1473 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
1475 ip->i_df.if_lastex = idx;
1477 rval = XFS_ILOG_DEXT;
1480 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1481 new->br_startblock, new->br_blockcount,
1485 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1486 new->br_startblock, new->br_blockcount,
1492 case MASK2(LEFT_FILLING, LEFT_CONTIG):
1494 * Setting the first part of a previous oldext extent to newext.
1495 * The left neighbor is contiguous.
1497 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
1499 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1500 LEFT.br_blockcount + new->br_blockcount);
1501 xfs_bmbt_set_startoff(ep,
1502 PREV.br_startoff + new->br_blockcount);
1503 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
1505 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
1507 xfs_bmbt_set_startblock(ep,
1508 new->br_startblock + new->br_blockcount);
1509 xfs_bmbt_set_blockcount(ep,
1510 PREV.br_blockcount - new->br_blockcount);
1511 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
1513 ip->i_df.if_lastex = idx - 1;
1515 rval = XFS_ILOG_DEXT;
1518 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1519 PREV.br_startblock, PREV.br_blockcount,
1523 if ((error = xfs_bmbt_update(cur,
1524 PREV.br_startoff + new->br_blockcount,
1525 PREV.br_startblock + new->br_blockcount,
1526 PREV.br_blockcount - new->br_blockcount,
1529 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1531 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1533 LEFT.br_blockcount + new->br_blockcount,
1539 case MASK(LEFT_FILLING):
1541 * Setting the first part of a previous oldext extent to newext.
1542 * The left neighbor is not contiguous.
1544 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1545 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1546 xfs_bmbt_set_startoff(ep, new_endoff);
1547 xfs_bmbt_set_blockcount(ep,
1548 PREV.br_blockcount - new->br_blockcount);
1549 xfs_bmbt_set_startblock(ep,
1550 new->br_startblock + new->br_blockcount);
1551 xfs_bmap_trace_post_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1552 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
1554 xfs_iext_insert(ifp, idx, 1, new);
1555 ip->i_df.if_lastex = idx;
1556 ip->i_d.di_nextents++;
1558 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1560 rval = XFS_ILOG_CORE;
1561 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1562 PREV.br_startblock, PREV.br_blockcount,
1566 if ((error = xfs_bmbt_update(cur,
1567 PREV.br_startoff + new->br_blockcount,
1568 PREV.br_startblock + new->br_blockcount,
1569 PREV.br_blockcount - new->br_blockcount,
1572 cur->bc_rec.b = *new;
1573 if ((error = xfs_bmbt_insert(cur, &i)))
1579 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1581 * Setting the last part of a previous oldext extent to newext.
1582 * The right neighbor is contiguous with the new allocation.
1584 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1586 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1588 xfs_bmbt_set_blockcount(ep,
1589 PREV.br_blockcount - new->br_blockcount);
1590 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1592 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1593 new->br_startoff, new->br_startblock,
1594 new->br_blockcount + RIGHT.br_blockcount, newext);
1595 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1597 ip->i_df.if_lastex = idx + 1;
1599 rval = XFS_ILOG_DEXT;
1602 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1604 PREV.br_blockcount, &i)))
1607 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1609 PREV.br_blockcount - new->br_blockcount,
1612 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1614 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1616 new->br_blockcount + RIGHT.br_blockcount,
1622 case MASK(RIGHT_FILLING):
1624 * Setting the last part of a previous oldext extent to newext.
1625 * The right neighbor is not contiguous.
1627 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1628 xfs_bmbt_set_blockcount(ep,
1629 PREV.br_blockcount - new->br_blockcount);
1630 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1631 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1632 new, NULL, XFS_DATA_FORK);
1633 xfs_iext_insert(ifp, idx + 1, 1, new);
1634 ip->i_df.if_lastex = idx + 1;
1635 ip->i_d.di_nextents++;
1637 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1639 rval = XFS_ILOG_CORE;
1640 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1641 PREV.br_startblock, PREV.br_blockcount,
1645 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1647 PREV.br_blockcount - new->br_blockcount,
1650 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1651 new->br_startblock, new->br_blockcount,
1655 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1656 if ((error = xfs_bmbt_insert(cur, &i)))
1664 * Setting the middle part of a previous oldext extent to
1665 * newext. Contiguity is impossible here.
1666 * One extent becomes three extents.
1668 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1669 xfs_bmbt_set_blockcount(ep,
1670 new->br_startoff - PREV.br_startoff);
1671 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1673 r[1].br_startoff = new_endoff;
1674 r[1].br_blockcount =
1675 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1676 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1677 r[1].br_state = oldext;
1678 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1680 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1681 ip->i_df.if_lastex = idx + 1;
1682 ip->i_d.di_nextents += 2;
1684 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1686 rval = XFS_ILOG_CORE;
1687 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1688 PREV.br_startblock, PREV.br_blockcount,
1692 /* new right extent - oldext */
1693 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1694 r[1].br_startblock, r[1].br_blockcount,
1697 /* new left extent - oldext */
1698 PREV.br_blockcount =
1699 new->br_startoff - PREV.br_startoff;
1700 cur->bc_rec.b = PREV;
1701 if ((error = xfs_bmbt_insert(cur, &i)))
1704 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1707 /* new middle extent - newext */
1708 cur->bc_rec.b = *new;
1709 if ((error = xfs_bmbt_insert(cur, &i)))
1715 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1716 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1717 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1718 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1719 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1720 case MASK(LEFT_CONTIG):
1721 case MASK(RIGHT_CONTIG):
1723 * These cases are all impossible.
1740 #undef STATE_SET_TEST
1745 * Called by xfs_bmap_add_extent to handle cases converting a hole
1746 * to a delayed allocation.
1749 STATIC int /* error */
1750 xfs_bmap_add_extent_hole_delay(
1751 xfs_inode_t *ip, /* incore inode pointer */
1752 xfs_extnum_t idx, /* extent number to update/insert */
1753 xfs_btree_cur_t *cur, /* if null, not a btree */
1754 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1755 int *logflagsp, /* inode logging flags */
1756 int rsvd) /* OK to allocate reserved blocks */
1758 xfs_bmbt_rec_t *ep; /* extent record for idx */
1759 #ifdef XFS_BMAP_TRACE
1760 static char fname[] = "xfs_bmap_add_extent_hole_delay";
1762 xfs_ifork_t *ifp; /* inode fork pointer */
1763 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1764 xfs_filblks_t newlen=0; /* new indirect size */
1765 xfs_filblks_t oldlen=0; /* old indirect size */
1766 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1767 int state; /* state bits, accessed thru macros */
1768 xfs_filblks_t temp; /* temp for indirect calculations */
1769 enum { /* bit number definitions for state */
1770 LEFT_CONTIG, RIGHT_CONTIG,
1771 LEFT_DELAY, RIGHT_DELAY,
1772 LEFT_VALID, RIGHT_VALID
1775 #define MASK(b) (1 << (b))
1776 #define MASK2(a,b) (MASK(a) | MASK(b))
1777 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1778 #define STATE_TEST(b) (state & MASK(b))
1779 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1780 ((state &= ~MASK(b)), 0))
1781 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1783 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1784 ep = xfs_iext_get_ext(ifp, idx);
1786 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1788 * Check and set flags if this segment has a left neighbor
1790 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1791 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1792 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1795 * Check and set flags if the current (right) segment exists.
1796 * If it doesn't exist, we're converting the hole at end-of-file.
1798 if (STATE_SET_TEST(RIGHT_VALID,
1800 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1801 xfs_bmbt_get_all(ep, &right);
1802 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1805 * Set contiguity flags on the left and right neighbors.
1806 * Don't let extents get too large, even if the pieces are contiguous.
1808 STATE_SET(LEFT_CONTIG,
1809 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1810 left.br_startoff + left.br_blockcount == new->br_startoff &&
1811 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1812 STATE_SET(RIGHT_CONTIG,
1813 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1814 new->br_startoff + new->br_blockcount == right.br_startoff &&
1815 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1816 (!STATE_TEST(LEFT_CONTIG) ||
1817 (left.br_blockcount + new->br_blockcount +
1818 right.br_blockcount <= MAXEXTLEN)));
1820 * Switch out based on the contiguity flags.
1822 switch (SWITCH_STATE) {
1824 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1826 * New allocation is contiguous with delayed allocations
1827 * on the left and on the right.
1828 * Merge all three into a single extent record.
1830 temp = left.br_blockcount + new->br_blockcount +
1831 right.br_blockcount;
1832 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
1834 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1835 oldlen = STARTBLOCKVAL(left.br_startblock) +
1836 STARTBLOCKVAL(new->br_startblock) +
1837 STARTBLOCKVAL(right.br_startblock);
1838 newlen = xfs_bmap_worst_indlen(ip, temp);
1839 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1840 NULLSTARTBLOCK((int)newlen));
1841 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
1843 xfs_bmap_trace_delete(fname, "LC|RC", ip, idx, 1,
1845 xfs_iext_remove(ifp, idx, 1);
1846 ip->i_df.if_lastex = idx - 1;
1849 case MASK(LEFT_CONTIG):
1851 * New allocation is contiguous with a delayed allocation
1853 * Merge the new allocation with the left neighbor.
1855 temp = left.br_blockcount + new->br_blockcount;
1856 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1,
1858 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1859 oldlen = STARTBLOCKVAL(left.br_startblock) +
1860 STARTBLOCKVAL(new->br_startblock);
1861 newlen = xfs_bmap_worst_indlen(ip, temp);
1862 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1863 NULLSTARTBLOCK((int)newlen));
1864 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1,
1866 ip->i_df.if_lastex = idx - 1;
1869 case MASK(RIGHT_CONTIG):
1871 * New allocation is contiguous with a delayed allocation
1873 * Merge the new allocation with the right neighbor.
1875 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1876 temp = new->br_blockcount + right.br_blockcount;
1877 oldlen = STARTBLOCKVAL(new->br_startblock) +
1878 STARTBLOCKVAL(right.br_startblock);
1879 newlen = xfs_bmap_worst_indlen(ip, temp);
1880 xfs_bmbt_set_allf(ep, new->br_startoff,
1881 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
1882 xfs_bmap_trace_post_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1883 ip->i_df.if_lastex = idx;
1888 * New allocation is not contiguous with another
1889 * delayed allocation.
1890 * Insert a new entry.
1892 oldlen = newlen = 0;
1893 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
1895 xfs_iext_insert(ifp, idx, 1, new);
1896 ip->i_df.if_lastex = idx;
1899 if (oldlen != newlen) {
1900 ASSERT(oldlen > newlen);
1901 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
1902 (int)(oldlen - newlen), rsvd);
1904 * Nothing to do for disk quota accounting here.
1913 #undef STATE_SET_TEST
1918 * Called by xfs_bmap_add_extent to handle cases converting a hole
1919 * to a real allocation.
1921 STATIC int /* error */
1922 xfs_bmap_add_extent_hole_real(
1923 xfs_inode_t *ip, /* incore inode pointer */
1924 xfs_extnum_t idx, /* extent number to update/insert */
1925 xfs_btree_cur_t *cur, /* if null, not a btree */
1926 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1927 int *logflagsp, /* inode logging flags */
1928 int whichfork) /* data or attr fork */
1930 xfs_bmbt_rec_t *ep; /* pointer to extent entry ins. point */
1931 int error; /* error return value */
1932 #ifdef XFS_BMAP_TRACE
1933 static char fname[] = "xfs_bmap_add_extent_hole_real";
1935 int i; /* temp state */
1936 xfs_ifork_t *ifp; /* inode fork pointer */
1937 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1938 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1939 int state; /* state bits, accessed thru macros */
1940 enum { /* bit number definitions for state */
1941 LEFT_CONTIG, RIGHT_CONTIG,
1942 LEFT_DELAY, RIGHT_DELAY,
1943 LEFT_VALID, RIGHT_VALID
1946 #define MASK(b) (1 << (b))
1947 #define MASK2(a,b) (MASK(a) | MASK(b))
1948 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1949 #define STATE_TEST(b) (state & MASK(b))
1950 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1951 ((state &= ~MASK(b)), 0))
1952 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1954 ifp = XFS_IFORK_PTR(ip, whichfork);
1955 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
1956 ep = xfs_iext_get_ext(ifp, idx);
1959 * Check and set flags if this segment has a left neighbor.
1961 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1962 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1963 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1966 * Check and set flags if this segment has a current value.
1967 * Not true if we're inserting into the "hole" at eof.
1969 if (STATE_SET_TEST(RIGHT_VALID,
1971 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1972 xfs_bmbt_get_all(ep, &right);
1973 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1976 * We're inserting a real allocation between "left" and "right".
1977 * Set the contiguity flags. Don't let extents get too large.
1979 STATE_SET(LEFT_CONTIG,
1980 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1981 left.br_startoff + left.br_blockcount == new->br_startoff &&
1982 left.br_startblock + left.br_blockcount == new->br_startblock &&
1983 left.br_state == new->br_state &&
1984 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1985 STATE_SET(RIGHT_CONTIG,
1986 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1987 new->br_startoff + new->br_blockcount == right.br_startoff &&
1988 new->br_startblock + new->br_blockcount ==
1989 right.br_startblock &&
1990 new->br_state == right.br_state &&
1991 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1992 (!STATE_TEST(LEFT_CONTIG) ||
1993 left.br_blockcount + new->br_blockcount +
1994 right.br_blockcount <= MAXEXTLEN));
1997 * Select which case we're in here, and implement it.
1999 switch (SWITCH_STATE) {
2001 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
2003 * New allocation is contiguous with real allocations on the
2004 * left and on the right.
2005 * Merge all three into a single extent record.
2007 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
2009 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2010 left.br_blockcount + new->br_blockcount +
2011 right.br_blockcount);
2012 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
2014 xfs_bmap_trace_delete(fname, "LC|RC", ip,
2016 xfs_iext_remove(ifp, idx, 1);
2017 ifp->if_lastex = idx - 1;
2018 XFS_IFORK_NEXT_SET(ip, whichfork,
2019 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2021 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2024 *logflagsp = XFS_ILOG_CORE;
2025 if ((error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
2026 right.br_startblock, right.br_blockcount, &i)))
2029 if ((error = xfs_bmbt_delete(cur, &i)))
2032 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2035 error = xfs_bmbt_update(cur, left.br_startoff,
2037 left.br_blockcount + new->br_blockcount +
2038 right.br_blockcount, left.br_state);
2041 case MASK(LEFT_CONTIG):
2043 * New allocation is contiguous with a real allocation
2045 * Merge the new allocation with the left neighbor.
2047 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1, whichfork);
2048 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2049 left.br_blockcount + new->br_blockcount);
2050 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1, whichfork);
2051 ifp->if_lastex = idx - 1;
2053 *logflagsp = XFS_ILOG_FEXT(whichfork);
2057 if ((error = xfs_bmbt_lookup_eq(cur, left.br_startoff,
2058 left.br_startblock, left.br_blockcount, &i)))
2061 error = xfs_bmbt_update(cur, left.br_startoff,
2063 left.br_blockcount + new->br_blockcount,
2067 case MASK(RIGHT_CONTIG):
2069 * New allocation is contiguous with a real allocation
2071 * Merge the new allocation with the right neighbor.
2073 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, whichfork);
2074 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2075 new->br_blockcount + right.br_blockcount,
2077 xfs_bmap_trace_post_update(fname, "RC", ip, idx, whichfork);
2078 ifp->if_lastex = idx;
2080 *logflagsp = XFS_ILOG_FEXT(whichfork);
2084 if ((error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
2085 right.br_startblock, right.br_blockcount, &i)))
2088 error = xfs_bmbt_update(cur, new->br_startoff,
2090 new->br_blockcount + right.br_blockcount,
2096 * New allocation is not contiguous with another
2098 * Insert a new entry.
2100 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
2102 xfs_iext_insert(ifp, idx, 1, new);
2103 ifp->if_lastex = idx;
2104 XFS_IFORK_NEXT_SET(ip, whichfork,
2105 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2107 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2110 *logflagsp = XFS_ILOG_CORE;
2111 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2112 new->br_startblock, new->br_blockcount, &i)))
2115 cur->bc_rec.b.br_state = new->br_state;
2116 if ((error = xfs_bmbt_insert(cur, &i)))
2125 #undef STATE_SET_TEST
2129 return 0; /* keep gcc quite */
2133 * Adjust the size of the new extent based on di_extsize and rt extsize.
2136 xfs_bmap_extsize_align(
2138 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2139 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2140 xfs_extlen_t extsz, /* align to this extent size */
2141 int rt, /* is this a realtime inode? */
2142 int eof, /* is extent at end-of-file? */
2143 int delay, /* creating delalloc extent? */
2144 int convert, /* overwriting unwritten extent? */
2145 xfs_fileoff_t *offp, /* in/out: aligned offset */
2146 xfs_extlen_t *lenp) /* in/out: aligned length */
2148 xfs_fileoff_t orig_off; /* original offset */
2149 xfs_extlen_t orig_alen; /* original length */
2150 xfs_fileoff_t orig_end; /* original off+len */
2151 xfs_fileoff_t nexto; /* next file offset */
2152 xfs_fileoff_t prevo; /* previous file offset */
2153 xfs_fileoff_t align_off; /* temp for offset */
2154 xfs_extlen_t align_alen; /* temp for length */
2155 xfs_extlen_t temp; /* temp for calculations */
2160 orig_off = align_off = *offp;
2161 orig_alen = align_alen = *lenp;
2162 orig_end = orig_off + orig_alen;
2165 * If this request overlaps an existing extent, then don't
2166 * attempt to perform any additional alignment.
2168 if (!delay && !eof &&
2169 (orig_off >= gotp->br_startoff) &&
2170 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2175 * If the file offset is unaligned vs. the extent size
2176 * we need to align it. This will be possible unless
2177 * the file was previously written with a kernel that didn't
2178 * perform this alignment, or if a truncate shot us in the
2181 temp = do_mod(orig_off, extsz);
2187 * Same adjustment for the end of the requested area.
2189 if ((temp = (align_alen % extsz))) {
2190 align_alen += extsz - temp;
2193 * If the previous block overlaps with this proposed allocation
2194 * then move the start forward without adjusting the length.
2196 if (prevp->br_startoff != NULLFILEOFF) {
2197 if (prevp->br_startblock == HOLESTARTBLOCK)
2198 prevo = prevp->br_startoff;
2200 prevo = prevp->br_startoff + prevp->br_blockcount;
2203 if (align_off != orig_off && align_off < prevo)
2206 * If the next block overlaps with this proposed allocation
2207 * then move the start back without adjusting the length,
2208 * but not before offset 0.
2209 * This may of course make the start overlap previous block,
2210 * and if we hit the offset 0 limit then the next block
2211 * can still overlap too.
2213 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2214 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2215 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2216 nexto = gotp->br_startoff + gotp->br_blockcount;
2218 nexto = gotp->br_startoff;
2220 nexto = NULLFILEOFF;
2222 align_off + align_alen != orig_end &&
2223 align_off + align_alen > nexto)
2224 align_off = nexto > align_alen ? nexto - align_alen : 0;
2226 * If we're now overlapping the next or previous extent that
2227 * means we can't fit an extsz piece in this hole. Just move
2228 * the start forward to the first valid spot and set
2229 * the length so we hit the end.
2231 if (align_off != orig_off && align_off < prevo)
2233 if (align_off + align_alen != orig_end &&
2234 align_off + align_alen > nexto &&
2235 nexto != NULLFILEOFF) {
2236 ASSERT(nexto > prevo);
2237 align_alen = nexto - align_off;
2241 * If realtime, and the result isn't a multiple of the realtime
2242 * extent size we need to remove blocks until it is.
2244 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2246 * We're not covering the original request, or
2247 * we won't be able to once we fix the length.
2249 if (orig_off < align_off ||
2250 orig_end > align_off + align_alen ||
2251 align_alen - temp < orig_alen)
2252 return XFS_ERROR(EINVAL);
2254 * Try to fix it by moving the start up.
2256 if (align_off + temp <= orig_off) {
2261 * Try to fix it by moving the end in.
2263 else if (align_off + align_alen - temp >= orig_end)
2266 * Set the start to the minimum then trim the length.
2269 align_alen -= orig_off - align_off;
2270 align_off = orig_off;
2271 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2274 * Result doesn't cover the request, fail it.
2276 if (orig_off < align_off || orig_end > align_off + align_alen)
2277 return XFS_ERROR(EINVAL);
2279 ASSERT(orig_off >= align_off);
2280 ASSERT(orig_end <= align_off + align_alen);
2284 if (!eof && gotp->br_startoff != NULLFILEOFF)
2285 ASSERT(align_off + align_alen <= gotp->br_startoff);
2286 if (prevp->br_startoff != NULLFILEOFF)
2287 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2295 #define XFS_ALLOC_GAP_UNITS 4
2299 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2301 xfs_fsblock_t adjust; /* adjustment to block numbers */
2302 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2303 xfs_mount_t *mp; /* mount point structure */
2304 int nullfb; /* true if ap->firstblock isn't set */
2305 int rt; /* true if inode is realtime */
2307 #define ISVALID(x,y) \
2309 (x) < mp->m_sb.sb_rblocks : \
2310 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2311 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2312 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2314 mp = ap->ip->i_mount;
2315 nullfb = ap->firstblock == NULLFSBLOCK;
2316 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2317 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2319 * If allocating at eof, and there's a previous real block,
2320 * try to use it's last block as our starting point.
2322 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2323 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2324 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2325 ap->prevp->br_startblock)) {
2326 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2328 * Adjust for the gap between prevp and us.
2331 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2333 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2337 * If not at eof, then compare the two neighbor blocks.
2338 * Figure out whether either one gives us a good starting point,
2339 * and pick the better one.
2341 else if (!ap->eof) {
2342 xfs_fsblock_t gotbno; /* right side block number */
2343 xfs_fsblock_t gotdiff=0; /* right side difference */
2344 xfs_fsblock_t prevbno; /* left side block number */
2345 xfs_fsblock_t prevdiff=0; /* left side difference */
2348 * If there's a previous (left) block, select a requested
2349 * start block based on it.
2351 if (ap->prevp->br_startoff != NULLFILEOFF &&
2352 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2353 (prevbno = ap->prevp->br_startblock +
2354 ap->prevp->br_blockcount) &&
2355 ISVALID(prevbno, ap->prevp->br_startblock)) {
2357 * Calculate gap to end of previous block.
2359 adjust = prevdiff = ap->off -
2360 (ap->prevp->br_startoff +
2361 ap->prevp->br_blockcount);
2363 * Figure the startblock based on the previous block's
2364 * end and the gap size.
2366 * If the gap is large relative to the piece we're
2367 * allocating, or using it gives us an invalid block
2368 * number, then just use the end of the previous block.
2370 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2371 ISVALID(prevbno + prevdiff,
2372 ap->prevp->br_startblock))
2377 * If the firstblock forbids it, can't use it,
2380 if (!rt && !nullfb &&
2381 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2382 prevbno = NULLFSBLOCK;
2385 * No previous block or can't follow it, just default.
2388 prevbno = NULLFSBLOCK;
2390 * If there's a following (right) block, select a requested
2391 * start block based on it.
2393 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
2395 * Calculate gap to start of next block.
2397 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2399 * Figure the startblock based on the next block's
2400 * start and the gap size.
2402 gotbno = ap->gotp->br_startblock;
2405 * If the gap is large relative to the piece we're
2406 * allocating, or using it gives us an invalid block
2407 * number, then just use the start of the next block
2408 * offset by our length.
2410 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2411 ISVALID(gotbno - gotdiff, gotbno))
2413 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2415 gotdiff += adjust - ap->alen;
2419 * If the firstblock forbids it, can't use it,
2422 if (!rt && !nullfb &&
2423 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2424 gotbno = NULLFSBLOCK;
2427 * No next block, just default.
2430 gotbno = NULLFSBLOCK;
2432 * If both valid, pick the better one, else the only good
2433 * one, else ap->rval is already set (to 0 or the inode block).
2435 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2436 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2437 else if (prevbno != NULLFSBLOCK)
2439 else if (gotbno != NULLFSBLOCK)
2448 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2450 xfs_alloctype_t atype = 0; /* type for allocation routines */
2451 int error; /* error return value */
2452 xfs_mount_t *mp; /* mount point structure */
2453 xfs_extlen_t prod = 0; /* product factor for allocators */
2454 xfs_extlen_t ralen = 0; /* realtime allocation length */
2455 xfs_extlen_t align; /* minimum allocation alignment */
2456 xfs_rtblock_t rtx; /* realtime extent number */
2459 mp = ap->ip->i_mount;
2460 align = ap->ip->i_d.di_extsize ?
2461 ap->ip->i_d.di_extsize : mp->m_sb.sb_rextsize;
2462 prod = align / mp->m_sb.sb_rextsize;
2463 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2464 align, 1, ap->eof, 0,
2465 ap->conv, &ap->off, &ap->alen);
2469 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2472 * If the offset & length are not perfectly aligned
2473 * then kill prod, it will just get us in trouble.
2475 if (do_mod(ap->off, align) || ap->alen % align)
2478 * Set ralen to be the actual requested length in rtextents.
2480 ralen = ap->alen / mp->m_sb.sb_rextsize;
2482 * If the old value was close enough to MAXEXTLEN that
2483 * we rounded up to it, cut it back so it's valid again.
2484 * Note that if it's a really large request (bigger than
2485 * MAXEXTLEN), we don't hear about that number, and can't
2486 * adjust the starting point to match it.
2488 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2489 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2491 * If it's an allocation to an empty file at offset 0,
2492 * pick an extent that will space things out in the rt area.
2494 if (ap->eof && ap->off == 0) {
2495 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2498 ap->rval = rtx * mp->m_sb.sb_rextsize;
2503 xfs_bmap_adjacent(ap);
2506 * Realtime allocation, done through xfs_rtallocate_extent.
2508 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2509 do_div(ap->rval, mp->m_sb.sb_rextsize);
2512 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2513 &ralen, atype, ap->wasdel, prod, &rtb)))
2515 if (rtb == NULLFSBLOCK && prod > 1 &&
2516 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2517 ap->alen, &ralen, atype,
2518 ap->wasdel, 1, &rtb)))
2521 if (ap->rval != NULLFSBLOCK) {
2522 ap->rval *= mp->m_sb.sb_rextsize;
2523 ralen *= mp->m_sb.sb_rextsize;
2525 ap->ip->i_d.di_nblocks += ralen;
2526 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2528 ap->ip->i_delayed_blks -= ralen;
2530 * Adjust the disk quota also. This was reserved
2533 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2534 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2535 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2544 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2546 xfs_mount_t *mp; /* mount point structure */
2547 xfs_alloctype_t atype = 0; /* type for allocation routines */
2548 xfs_extlen_t align; /* minimum allocation alignment */
2550 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2551 xfs_agnumber_t startag;
2552 xfs_alloc_arg_t args;
2555 xfs_extlen_t longest;
2557 xfs_extlen_t nextminlen = 0;
2559 int nullfb; /* true if ap->firstblock isn't set */
2565 mp = ap->ip->i_mount;
2566 align = (ap->userdata && ap->ip->i_d.di_extsize &&
2567 (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)) ?
2568 ap->ip->i_d.di_extsize : 0;
2569 if (unlikely(align)) {
2570 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2571 align, 0, ap->eof, 0, ap->conv,
2572 &ap->off, &ap->alen);
2576 nullfb = ap->firstblock == NULLFSBLOCK;
2577 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2579 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2581 ap->rval = ap->firstblock;
2583 xfs_bmap_adjacent(ap);
2586 * If allowed, use ap->rval; otherwise must use firstblock since
2587 * it's in the right allocation group.
2589 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2592 ap->rval = ap->firstblock;
2594 * Normal allocation, done through xfs_alloc_vextent.
2596 tryagain = isaligned = 0;
2599 args.fsbno = ap->rval;
2600 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2603 args.type = XFS_ALLOCTYPE_START_BNO;
2604 args.total = ap->total;
2606 * Find the longest available space.
2607 * We're going to try for the whole allocation at once.
2609 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2611 down_read(&mp->m_peraglock);
2612 while (blen < ap->alen) {
2613 pag = &mp->m_perag[ag];
2614 if (!pag->pagf_init &&
2615 (error = xfs_alloc_pagf_init(mp, args.tp,
2616 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2617 up_read(&mp->m_peraglock);
2621 * See xfs_alloc_fix_freelist...
2623 if (pag->pagf_init) {
2624 need = XFS_MIN_FREELIST_PAG(pag, mp);
2625 delta = need > pag->pagf_flcount ?
2626 need - pag->pagf_flcount : 0;
2627 longest = (pag->pagf_longest > delta) ?
2628 (pag->pagf_longest - delta) :
2629 (pag->pagf_flcount > 0 ||
2630 pag->pagf_longest > 0);
2635 if (++ag == mp->m_sb.sb_agcount)
2640 up_read(&mp->m_peraglock);
2642 * Since the above loop did a BUF_TRYLOCK, it is
2643 * possible that there is space for this request.
2645 if (notinit || blen < ap->minlen)
2646 args.minlen = ap->minlen;
2648 * If the best seen length is less than the request
2649 * length, use the best as the minimum.
2651 else if (blen < ap->alen)
2654 * Otherwise we've seen an extent as big as alen,
2655 * use that as the minimum.
2658 args.minlen = ap->alen;
2659 } else if (ap->low) {
2660 args.type = XFS_ALLOCTYPE_FIRST_AG;
2661 args.total = args.minlen = ap->minlen;
2663 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2664 args.total = ap->total;
2665 args.minlen = ap->minlen;
2667 if (unlikely(ap->userdata && ap->ip->i_d.di_extsize &&
2668 (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE))) {
2669 args.prod = ap->ip->i_d.di_extsize;
2670 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2671 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2672 } else if (unlikely(mp->m_sb.sb_blocksize >= NBPP)) {
2676 args.prod = NBPP >> mp->m_sb.sb_blocklog;
2677 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2678 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2681 * If we are not low on available data blocks, and the
2682 * underlying logical volume manager is a stripe, and
2683 * the file offset is zero then try to allocate data
2684 * blocks on stripe unit boundary.
2685 * NOTE: ap->aeof is only set if the allocation length
2686 * is >= the stripe unit and the allocation offset is
2687 * at the end of file.
2689 if (!ap->low && ap->aeof) {
2691 args.alignment = mp->m_dalign;
2695 * Adjust for alignment
2697 if (blen > args.alignment && blen <= ap->alen)
2698 args.minlen = blen - args.alignment;
2699 args.minalignslop = 0;
2702 * First try an exact bno allocation.
2703 * If it fails then do a near or start bno
2704 * allocation with alignment turned on.
2708 args.type = XFS_ALLOCTYPE_THIS_BNO;
2711 * Compute the minlen+alignment for the
2712 * next case. Set slop so that the value
2713 * of minlen+alignment+slop doesn't go up
2714 * between the calls.
2716 if (blen > mp->m_dalign && blen <= ap->alen)
2717 nextminlen = blen - mp->m_dalign;
2719 nextminlen = args.minlen;
2720 if (nextminlen + mp->m_dalign > args.minlen + 1)
2722 nextminlen + mp->m_dalign -
2725 args.minalignslop = 0;
2729 args.minalignslop = 0;
2731 args.minleft = ap->minleft;
2732 args.wasdel = ap->wasdel;
2734 args.userdata = ap->userdata;
2735 if ((error = xfs_alloc_vextent(&args)))
2737 if (tryagain && args.fsbno == NULLFSBLOCK) {
2739 * Exact allocation failed. Now try with alignment
2743 args.fsbno = ap->rval;
2744 args.alignment = mp->m_dalign;
2745 args.minlen = nextminlen;
2746 args.minalignslop = 0;
2748 if ((error = xfs_alloc_vextent(&args)))
2751 if (isaligned && args.fsbno == NULLFSBLOCK) {
2753 * allocation failed, so turn off alignment and
2757 args.fsbno = ap->rval;
2759 if ((error = xfs_alloc_vextent(&args)))
2762 if (args.fsbno == NULLFSBLOCK && nullfb &&
2763 args.minlen > ap->minlen) {
2764 args.minlen = ap->minlen;
2765 args.type = XFS_ALLOCTYPE_START_BNO;
2766 args.fsbno = ap->rval;
2767 if ((error = xfs_alloc_vextent(&args)))
2770 if (args.fsbno == NULLFSBLOCK && nullfb) {
2772 args.type = XFS_ALLOCTYPE_FIRST_AG;
2773 args.total = ap->minlen;
2775 if ((error = xfs_alloc_vextent(&args)))
2779 if (args.fsbno != NULLFSBLOCK) {
2780 ap->firstblock = ap->rval = args.fsbno;
2781 ASSERT(nullfb || fb_agno == args.agno ||
2782 (ap->low && fb_agno < args.agno));
2783 ap->alen = args.len;
2784 ap->ip->i_d.di_nblocks += args.len;
2785 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2787 ap->ip->i_delayed_blks -= args.len;
2789 * Adjust the disk quota also. This was reserved
2792 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2793 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2794 XFS_TRANS_DQ_BCOUNT,
2797 ap->rval = NULLFSBLOCK;
2804 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2805 * It figures out where to ask the underlying allocator to put the new extent.
2809 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2811 if ((ap->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) && ap->userdata)
2812 return xfs_bmap_rtalloc(ap);
2813 return xfs_bmap_btalloc(ap);
2817 * Transform a btree format file with only one leaf node, where the
2818 * extents list will fit in the inode, into an extents format file.
2819 * Since the file extents are already in-core, all we have to do is
2820 * give up the space for the btree root and pitch the leaf block.
2822 STATIC int /* error */
2823 xfs_bmap_btree_to_extents(
2824 xfs_trans_t *tp, /* transaction pointer */
2825 xfs_inode_t *ip, /* incore inode pointer */
2826 xfs_btree_cur_t *cur, /* btree cursor */
2827 int *logflagsp, /* inode logging flags */
2828 int whichfork) /* data or attr fork */
2831 xfs_bmbt_block_t *cblock;/* child btree block */
2832 xfs_fsblock_t cbno; /* child block number */
2833 xfs_buf_t *cbp; /* child block's buffer */
2834 int error; /* error return value */
2835 xfs_ifork_t *ifp; /* inode fork data */
2836 xfs_mount_t *mp; /* mount point structure */
2837 xfs_bmbt_ptr_t *pp; /* ptr to block address */
2838 xfs_bmbt_block_t *rblock;/* root btree block */
2840 ifp = XFS_IFORK_PTR(ip, whichfork);
2841 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2842 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2843 rblock = ifp->if_broot;
2844 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2845 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
2846 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
2848 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
2851 if ((error = xfs_btree_check_lptr(cur, INT_GET(*pp, ARCH_CONVERT), 1)))
2854 cbno = INT_GET(*pp, ARCH_CONVERT);
2855 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2856 XFS_BMAP_BTREE_REF)))
2858 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
2859 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
2861 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2862 ip->i_d.di_nblocks--;
2863 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
2864 xfs_trans_binval(tp, cbp);
2865 if (cur->bc_bufs[0] == cbp)
2866 cur->bc_bufs[0] = NULL;
2867 xfs_iroot_realloc(ip, -1, whichfork);
2868 ASSERT(ifp->if_broot == NULL);
2869 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2870 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2871 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2876 * Called by xfs_bmapi to update file extent records and the btree
2877 * after removing space (or undoing a delayed allocation).
2879 STATIC int /* error */
2880 xfs_bmap_del_extent(
2881 xfs_inode_t *ip, /* incore inode pointer */
2882 xfs_trans_t *tp, /* current transaction pointer */
2883 xfs_extnum_t idx, /* extent number to update/delete */
2884 xfs_bmap_free_t *flist, /* list of extents to be freed */
2885 xfs_btree_cur_t *cur, /* if null, not a btree */
2886 xfs_bmbt_irec_t *del, /* data to remove from extents */
2887 int *logflagsp, /* inode logging flags */
2888 int whichfork, /* data or attr fork */
2889 int rsvd) /* OK to allocate reserved blocks */
2891 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2892 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2893 xfs_fsblock_t del_endblock=0; /* first block past del */
2894 xfs_fileoff_t del_endoff; /* first offset past del */
2895 int delay; /* current block is delayed allocated */
2896 int do_fx; /* free extent at end of routine */
2897 xfs_bmbt_rec_t *ep; /* current extent entry pointer */
2898 int error; /* error return value */
2899 int flags; /* inode logging flags */
2900 #ifdef XFS_BMAP_TRACE
2901 static char fname[] = "xfs_bmap_del_extent";
2903 xfs_bmbt_irec_t got; /* current extent entry */
2904 xfs_fileoff_t got_endoff; /* first offset past got */
2905 int i; /* temp state */
2906 xfs_ifork_t *ifp; /* inode fork pointer */
2907 xfs_mount_t *mp; /* mount structure */
2908 xfs_filblks_t nblks; /* quota/sb block count */
2909 xfs_bmbt_irec_t new; /* new record to be inserted */
2911 uint qfield; /* quota field to update */
2912 xfs_filblks_t temp; /* for indirect length calculations */
2913 xfs_filblks_t temp2; /* for indirect length calculations */
2915 XFS_STATS_INC(xs_del_exlist);
2917 ifp = XFS_IFORK_PTR(ip, whichfork);
2918 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
2919 (uint)sizeof(xfs_bmbt_rec_t)));
2920 ASSERT(del->br_blockcount > 0);
2921 ep = xfs_iext_get_ext(ifp, idx);
2922 xfs_bmbt_get_all(ep, &got);
2923 ASSERT(got.br_startoff <= del->br_startoff);
2924 del_endoff = del->br_startoff + del->br_blockcount;
2925 got_endoff = got.br_startoff + got.br_blockcount;
2926 ASSERT(got_endoff >= del_endoff);
2927 delay = ISNULLSTARTBLOCK(got.br_startblock);
2928 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
2933 * If deleting a real allocation, must free up the disk space.
2936 flags = XFS_ILOG_CORE;
2938 * Realtime allocation. Free it and record di_nblocks update.
2940 if (whichfork == XFS_DATA_FORK &&
2941 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
2945 ASSERT(do_mod(del->br_blockcount,
2946 mp->m_sb.sb_rextsize) == 0);
2947 ASSERT(do_mod(del->br_startblock,
2948 mp->m_sb.sb_rextsize) == 0);
2949 bno = del->br_startblock;
2950 len = del->br_blockcount;
2951 do_div(bno, mp->m_sb.sb_rextsize);
2952 do_div(len, mp->m_sb.sb_rextsize);
2953 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
2954 (xfs_extlen_t)len)))
2957 nblks = len * mp->m_sb.sb_rextsize;
2958 qfield = XFS_TRANS_DQ_RTBCOUNT;
2961 * Ordinary allocation.
2965 nblks = del->br_blockcount;
2966 qfield = XFS_TRANS_DQ_BCOUNT;
2969 * Set up del_endblock and cur for later.
2971 del_endblock = del->br_startblock + del->br_blockcount;
2973 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2974 got.br_startblock, got.br_blockcount,
2979 da_old = da_new = 0;
2981 da_old = STARTBLOCKVAL(got.br_startblock);
2987 * Set flag value to use in switch statement.
2988 * Left-contig is 2, right-contig is 1.
2990 switch (((got.br_startoff == del->br_startoff) << 1) |
2991 (got_endoff == del_endoff)) {
2994 * Matches the whole extent. Delete the entry.
2996 xfs_bmap_trace_delete(fname, "3", ip, idx, 1, whichfork);
2997 xfs_iext_remove(ifp, idx, 1);
2998 ifp->if_lastex = idx;
3001 XFS_IFORK_NEXT_SET(ip, whichfork,
3002 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3003 flags |= XFS_ILOG_CORE;
3005 flags |= XFS_ILOG_FEXT(whichfork);
3008 if ((error = xfs_bmbt_delete(cur, &i)))
3015 * Deleting the first part of the extent.
3017 xfs_bmap_trace_pre_update(fname, "2", ip, idx, whichfork);
3018 xfs_bmbt_set_startoff(ep, del_endoff);
3019 temp = got.br_blockcount - del->br_blockcount;
3020 xfs_bmbt_set_blockcount(ep, temp);
3021 ifp->if_lastex = idx;
3023 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3025 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3026 xfs_bmap_trace_post_update(fname, "2", ip, idx,
3031 xfs_bmbt_set_startblock(ep, del_endblock);
3032 xfs_bmap_trace_post_update(fname, "2", ip, idx, whichfork);
3034 flags |= XFS_ILOG_FEXT(whichfork);
3037 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3038 got.br_blockcount - del->br_blockcount,
3045 * Deleting the last part of the extent.
3047 temp = got.br_blockcount - del->br_blockcount;
3048 xfs_bmap_trace_pre_update(fname, "1", ip, idx, whichfork);
3049 xfs_bmbt_set_blockcount(ep, temp);
3050 ifp->if_lastex = idx;
3052 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3054 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3055 xfs_bmap_trace_post_update(fname, "1", ip, idx,
3060 xfs_bmap_trace_post_update(fname, "1", ip, idx, whichfork);
3062 flags |= XFS_ILOG_FEXT(whichfork);
3065 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3067 got.br_blockcount - del->br_blockcount,
3074 * Deleting the middle of the extent.
3076 temp = del->br_startoff - got.br_startoff;
3077 xfs_bmap_trace_pre_update(fname, "0", ip, idx, whichfork);
3078 xfs_bmbt_set_blockcount(ep, temp);
3079 new.br_startoff = del_endoff;
3080 temp2 = got_endoff - del_endoff;
3081 new.br_blockcount = temp2;
3082 new.br_state = got.br_state;
3084 new.br_startblock = del_endblock;
3085 flags |= XFS_ILOG_CORE;
3087 if ((error = xfs_bmbt_update(cur,
3089 got.br_startblock, temp,
3092 if ((error = xfs_bmbt_increment(cur, 0, &i)))
3094 cur->bc_rec.b = new;
3095 error = xfs_bmbt_insert(cur, &i);
3096 if (error && error != ENOSPC)
3099 * If get no-space back from btree insert,
3100 * it tried a split, and we have a zero
3101 * block reservation.
3102 * Fix up our state and return the error.
3104 if (error == ENOSPC) {
3106 * Reset the cursor, don't trust
3107 * it after any insert operation.
3109 if ((error = xfs_bmbt_lookup_eq(cur,
3116 * Update the btree record back
3117 * to the original value.
3119 if ((error = xfs_bmbt_update(cur,
3126 * Reset the extent record back
3127 * to the original value.
3129 xfs_bmbt_set_blockcount(ep,
3132 error = XFS_ERROR(ENOSPC);
3137 flags |= XFS_ILOG_FEXT(whichfork);
3138 XFS_IFORK_NEXT_SET(ip, whichfork,
3139 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3141 ASSERT(whichfork == XFS_DATA_FORK);
3142 temp = xfs_bmap_worst_indlen(ip, temp);
3143 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3144 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3145 new.br_startblock = NULLSTARTBLOCK((int)temp2);
3146 da_new = temp + temp2;
3147 while (da_new > da_old) {
3151 xfs_bmbt_set_startblock(ep,
3152 NULLSTARTBLOCK((int)temp));
3154 if (da_new == da_old)
3160 NULLSTARTBLOCK((int)temp2);
3164 xfs_bmap_trace_post_update(fname, "0", ip, idx, whichfork);
3165 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 1, &new, NULL,
3167 xfs_iext_insert(ifp, idx + 1, 1, &new);
3168 ifp->if_lastex = idx + 1;
3172 * If we need to, add to list of extents to delete.
3175 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3178 * Adjust inode # blocks in the file.
3181 ip->i_d.di_nblocks -= nblks;
3183 * Adjust quota data.
3186 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
3189 * Account for change in delayed indirect blocks.
3190 * Nothing to do for disk quota accounting here.
3192 ASSERT(da_old >= da_new);
3193 if (da_old > da_new)
3194 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int)(da_old - da_new),
3202 * Remove the entry "free" from the free item list. Prev points to the
3203 * previous entry, unless "free" is the head of the list.
3207 xfs_bmap_free_t *flist, /* free item list header */
3208 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3209 xfs_bmap_free_item_t *free) /* list item to be freed */
3212 prev->xbfi_next = free->xbfi_next;
3214 flist->xbf_first = free->xbfi_next;
3216 kmem_zone_free(xfs_bmap_free_item_zone, free);
3220 * Convert an extents-format file into a btree-format file.
3221 * The new file will have a root block (in the inode) and a single child block.
3223 STATIC int /* error */
3224 xfs_bmap_extents_to_btree(
3225 xfs_trans_t *tp, /* transaction pointer */
3226 xfs_inode_t *ip, /* incore inode pointer */
3227 xfs_fsblock_t *firstblock, /* first-block-allocated */
3228 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3229 xfs_btree_cur_t **curp, /* cursor returned to caller */
3230 int wasdel, /* converting a delayed alloc */
3231 int *logflagsp, /* inode logging flags */
3232 int whichfork) /* data or attr fork */
3234 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
3235 xfs_buf_t *abp; /* buffer for ablock */
3236 xfs_alloc_arg_t args; /* allocation arguments */
3237 xfs_bmbt_rec_t *arp; /* child record pointer */
3238 xfs_bmbt_block_t *block; /* btree root block */
3239 xfs_btree_cur_t *cur; /* bmap btree cursor */
3240 xfs_bmbt_rec_t *ep; /* extent record pointer */
3241 int error; /* error return value */
3242 xfs_extnum_t i, cnt; /* extent record index */
3243 xfs_ifork_t *ifp; /* inode fork pointer */
3244 xfs_bmbt_key_t *kp; /* root block key pointer */
3245 xfs_mount_t *mp; /* mount structure */
3246 xfs_extnum_t nextents; /* number of file extents */
3247 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3249 ifp = XFS_IFORK_PTR(ip, whichfork);
3250 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3251 ASSERT(ifp->if_ext_max ==
3252 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3254 * Make space in the inode incore.
3256 xfs_iroot_realloc(ip, 1, whichfork);
3257 ifp->if_flags |= XFS_IFBROOT;
3261 block = ifp->if_broot;
3262 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3263 block->bb_level = cpu_to_be16(1);
3264 block->bb_numrecs = cpu_to_be16(1);
3265 block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3266 block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3268 * Need a cursor. Can't allocate until bb_level is filled in.
3271 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
3273 cur->bc_private.b.firstblock = *firstblock;
3274 cur->bc_private.b.flist = flist;
3275 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3277 * Convert to a btree with two levels, one record in root.
3279 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3282 if (*firstblock == NULLFSBLOCK) {
3283 args.type = XFS_ALLOCTYPE_START_BNO;
3284 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3285 } else if (flist->xbf_low) {
3286 args.type = XFS_ALLOCTYPE_START_BNO;
3287 args.fsbno = *firstblock;
3289 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3290 args.fsbno = *firstblock;
3292 args.minlen = args.maxlen = args.prod = 1;
3293 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3294 args.minalignslop = 0;
3295 args.wasdel = wasdel;
3297 if ((error = xfs_alloc_vextent(&args))) {
3298 xfs_iroot_realloc(ip, -1, whichfork);
3299 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3303 * Allocation can't fail, the space was reserved.
3305 ASSERT(args.fsbno != NULLFSBLOCK);
3306 ASSERT(*firstblock == NULLFSBLOCK ||
3307 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3309 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3310 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3311 cur->bc_private.b.allocated++;
3312 ip->i_d.di_nblocks++;
3313 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3314 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3316 * Fill in the child block.
3318 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
3319 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3320 ablock->bb_level = 0;
3321 ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3322 ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3323 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3324 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3325 for (cnt = i = 0; i < nextents; i++) {
3326 ep = xfs_iext_get_ext(ifp, i);
3327 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
3328 arp->l0 = INT_GET(ep->l0, ARCH_CONVERT);
3329 arp->l1 = INT_GET(ep->l1, ARCH_CONVERT);
3333 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3334 ablock->bb_numrecs = cpu_to_be16(cnt);
3336 * Fill in the root key and pointer.
3338 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
3339 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3340 INT_SET(kp->br_startoff, ARCH_CONVERT, xfs_bmbt_disk_get_startoff(arp));
3341 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
3342 INT_SET(*pp, ARCH_CONVERT, args.fsbno);
3344 * Do all this logging at the end so that
3345 * the root is at the right level.
3347 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
3348 xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3349 ASSERT(*curp == NULL);
3351 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
3356 * Helper routine to reset inode di_forkoff field when switching
3357 * attribute fork from local to extent format - we reset it where
3358 * possible to make space available for inline data fork extents.
3361 xfs_bmap_forkoff_reset(
3366 if (whichfork == XFS_ATTR_FORK &&
3367 (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
3368 (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
3369 ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
3370 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3371 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
3372 (uint)sizeof(xfs_bmbt_rec_t);
3373 ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
3374 (uint)sizeof(xfs_bmbt_rec_t);
3379 * Convert a local file to an extents file.
3380 * This code is out of bounds for data forks of regular files,
3381 * since the file data needs to get logged so things will stay consistent.
3382 * (The bmap-level manipulations are ok, though).
3384 STATIC int /* error */
3385 xfs_bmap_local_to_extents(
3386 xfs_trans_t *tp, /* transaction pointer */
3387 xfs_inode_t *ip, /* incore inode pointer */
3388 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3389 xfs_extlen_t total, /* total blocks needed by transaction */
3390 int *logflagsp, /* inode logging flags */
3391 int whichfork) /* data or attr fork */
3393 int error; /* error return value */
3394 int flags; /* logging flags returned */
3395 #ifdef XFS_BMAP_TRACE
3396 static char fname[] = "xfs_bmap_local_to_extents";
3398 xfs_ifork_t *ifp; /* inode fork pointer */
3401 * We don't want to deal with the case of keeping inode data inline yet.
3402 * So sending the data fork of a regular inode is invalid.
3404 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3405 whichfork == XFS_DATA_FORK));
3406 ifp = XFS_IFORK_PTR(ip, whichfork);
3407 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3410 if (ifp->if_bytes) {
3411 xfs_alloc_arg_t args; /* allocation arguments */
3412 xfs_buf_t *bp; /* buffer for extent block */
3413 xfs_bmbt_rec_t *ep; /* extent record pointer */
3416 args.mp = ip->i_mount;
3417 ASSERT(ifp->if_flags & XFS_IFINLINE);
3419 * Allocate a block. We know we need only one, since the
3420 * file currently fits in an inode.
3422 if (*firstblock == NULLFSBLOCK) {
3423 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3424 args.type = XFS_ALLOCTYPE_START_BNO;
3426 args.fsbno = *firstblock;
3427 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3430 args.mod = args.minleft = args.alignment = args.wasdel =
3431 args.isfl = args.minalignslop = 0;
3432 args.minlen = args.maxlen = args.prod = 1;
3433 if ((error = xfs_alloc_vextent(&args)))
3436 * Can't fail, the space was reserved.
3438 ASSERT(args.fsbno != NULLFSBLOCK);
3439 ASSERT(args.len == 1);
3440 *firstblock = args.fsbno;
3441 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3442 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3444 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3445 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3446 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3447 xfs_iext_add(ifp, 0, 1);
3448 ASSERT((ifp->if_flags & (XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFEXTENTS);
3449 ep = xfs_iext_get_ext(ifp, 0);
3450 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3451 xfs_bmap_trace_post_update(fname, "new", ip, 0, whichfork);
3452 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3453 ip->i_d.di_nblocks = 1;
3454 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
3455 XFS_TRANS_DQ_BCOUNT, 1L);
3456 flags |= XFS_ILOG_FEXT(whichfork);
3458 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3459 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3461 ifp->if_flags &= ~XFS_IFINLINE;
3462 ifp->if_flags |= XFS_IFEXTENTS;
3463 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3464 flags |= XFS_ILOG_CORE;
3470 xfs_bmbt_rec_t * /* pointer to found extent entry */
3471 xfs_bmap_do_search_extents(
3472 xfs_bmbt_rec_t *base, /* base of extent list */
3473 xfs_extnum_t lastx, /* last extent index used */
3474 xfs_extnum_t nextents, /* number of file extents */
3475 xfs_fileoff_t bno, /* block number searched for */
3476 int *eofp, /* out: end of file found */
3477 xfs_extnum_t *lastxp, /* out: last extent index */
3478 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3479 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3481 xfs_bmbt_rec_t *ep; /* extent list entry pointer */
3482 xfs_bmbt_irec_t got; /* extent list entry, decoded */
3483 int high; /* high index of binary search */
3484 int low; /* low index of binary search */
3487 * Initialize the extent entry structure to catch access to
3488 * uninitialized br_startblock field.
3490 got.br_startoff = 0xffa5a5a5a5a5a5a5LL;
3491 got.br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3492 got.br_state = XFS_EXT_INVALID;
3495 got.br_startblock = 0xffffa5a5a5a5a5a5LL;
3497 got.br_startblock = 0xffffa5a5;
3500 if (lastx != NULLEXTNUM && lastx < nextents)
3504 prevp->br_startoff = NULLFILEOFF;
3505 if (ep && bno >= (got.br_startoff = xfs_bmbt_get_startoff(ep)) &&
3506 bno < got.br_startoff +
3507 (got.br_blockcount = xfs_bmbt_get_blockcount(ep)))
3509 else if (ep && lastx < nextents - 1 &&
3510 bno >= (got.br_startoff = xfs_bmbt_get_startoff(ep + 1)) &&
3511 bno < got.br_startoff +
3512 (got.br_blockcount = xfs_bmbt_get_blockcount(ep + 1))) {
3516 } else if (nextents == 0)
3518 else if (bno == 0 &&
3519 (got.br_startoff = xfs_bmbt_get_startoff(base)) == 0) {
3522 got.br_blockcount = xfs_bmbt_get_blockcount(ep);
3526 high = nextents - 1;
3527 /* binary search the extents array */
3528 while (low <= high) {
3529 XFS_STATS_INC(xs_cmp_exlist);
3530 lastx = (low + high) >> 1;
3532 got.br_startoff = xfs_bmbt_get_startoff(ep);
3533 got.br_blockcount = xfs_bmbt_get_blockcount(ep);
3534 if (bno < got.br_startoff)
3536 else if (bno >= got.br_startoff + got.br_blockcount)
3539 got.br_startblock = xfs_bmbt_get_startblock(ep);
3540 got.br_state = xfs_bmbt_get_state(ep);
3547 if (bno >= got.br_startoff + got.br_blockcount) {
3549 if (lastx == nextents) {
3551 got.br_startblock = xfs_bmbt_get_startblock(ep);
3552 got.br_state = xfs_bmbt_get_state(ep);
3557 xfs_bmbt_get_all(ep, prevp);
3559 got.br_startoff = xfs_bmbt_get_startoff(ep);
3560 got.br_blockcount = xfs_bmbt_get_blockcount(ep);
3565 xfs_bmbt_get_all(ep - 1, prevp);
3569 got.br_startblock = xfs_bmbt_get_startblock(ep);
3570 got.br_state = xfs_bmbt_get_state(ep);
3578 * Call xfs_bmap_do_search_extents() to search for the extent
3579 * record containing block bno. If in multi-level in-core extent
3580 * allocation mode, find and extract the target extent buffer,
3581 * otherwise just use the direct extent list.
3583 xfs_bmbt_rec_t * /* pointer to found extent entry */
3584 xfs_bmap_search_multi_extents(
3585 xfs_ifork_t *ifp, /* inode fork pointer */
3586 xfs_fileoff_t bno, /* block number searched for */
3587 int *eofp, /* out: end of file found */
3588 xfs_extnum_t *lastxp, /* out: last extent index */
3589 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3590 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3592 xfs_bmbt_rec_t *base; /* base of extent records */
3593 xfs_bmbt_rec_t *ep; /* extent record pointer */
3594 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
3595 xfs_extnum_t lastx; /* last extent index */
3596 xfs_extnum_t nextents; /* number of file extents */
3599 * For multi-level extent allocation mode, find the
3600 * target extent list and pass only the contiguous
3601 * list to xfs_bmap_do_search_extents. Convert lastx
3602 * from a file extent index to an index within the
3603 * target extent list.
3605 if (ifp->if_flags & XFS_IFEXTIREC) {
3607 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
3608 base = erp->er_extbuf;
3609 nextents = erp->er_extcount;
3610 lastx = ifp->if_lastex - erp->er_extoff;
3612 base = &ifp->if_u1.if_extents[0];
3613 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3614 lastx = ifp->if_lastex;
3616 ep = xfs_bmap_do_search_extents(base, lastx, nextents, bno,
3617 eofp, lastxp, gotp, prevp);
3618 /* Convert lastx back to file-based index */
3619 if (ifp->if_flags & XFS_IFEXTIREC) {
3620 *lastxp += erp->er_extoff;
3626 * Search the extents list for the inode, for the extent containing bno.
3627 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3628 * *eofp will be set, and *prevp will contain the last entry (null if none).
3629 * Else, *lastxp will be set to the index of the found
3630 * entry; *gotp will contain the entry.
3632 STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
3633 xfs_bmap_search_extents(
3634 xfs_inode_t *ip, /* incore inode pointer */
3635 xfs_fileoff_t bno, /* block number searched for */
3636 int whichfork, /* data or attr fork */
3637 int *eofp, /* out: end of file found */
3638 xfs_extnum_t *lastxp, /* out: last extent index */
3639 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3640 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3642 xfs_ifork_t *ifp; /* inode fork pointer */
3643 xfs_bmbt_rec_t *ep; /* extent record pointer */
3644 int rt; /* realtime flag */
3646 XFS_STATS_INC(xs_look_exlist);
3647 ifp = XFS_IFORK_PTR(ip, whichfork);
3649 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3651 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
3652 if (unlikely(!rt && !gotp->br_startblock && (*lastxp != NULLEXTNUM))) {
3653 cmn_err(CE_PANIC,"Access to block zero: fs: <%s> inode: %lld "
3654 "start_block : %llx start_off : %llx blkcnt : %llx "
3655 "extent-state : %x \n",
3656 (ip->i_mount)->m_fsname, (long long)ip->i_ino,
3657 (unsigned long long)gotp->br_startblock,
3658 (unsigned long long)gotp->br_startoff,
3659 (unsigned long long)gotp->br_blockcount,
3666 #ifdef XFS_BMAP_TRACE
3667 ktrace_t *xfs_bmap_trace_buf;
3670 * Add a bmap trace buffer entry. Base routine for the others.
3673 xfs_bmap_trace_addentry(
3674 int opcode, /* operation */
3675 char *fname, /* function name */
3676 char *desc, /* operation description */
3677 xfs_inode_t *ip, /* incore inode pointer */
3678 xfs_extnum_t idx, /* index of entry(ies) */
3679 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
3680 xfs_bmbt_rec_t *r1, /* first record */
3681 xfs_bmbt_rec_t *r2, /* second record or null */
3682 int whichfork) /* data or attr fork */
3686 ASSERT(cnt == 1 || cnt == 2);
3691 memset(&tr2, 0, sizeof(tr2));
3694 ktrace_enter(xfs_bmap_trace_buf,
3695 (void *)(__psint_t)(opcode | (whichfork << 16)),
3696 (void *)fname, (void *)desc, (void *)ip,
3697 (void *)(__psint_t)idx,
3698 (void *)(__psint_t)cnt,
3699 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3700 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3701 (void *)(__psunsigned_t)(r1->l0 >> 32),
3702 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3703 (void *)(__psunsigned_t)(r1->l1 >> 32),
3704 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3705 (void *)(__psunsigned_t)(r2->l0 >> 32),
3706 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3707 (void *)(__psunsigned_t)(r2->l1 >> 32),
3708 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3710 ASSERT(ip->i_xtrace);
3711 ktrace_enter(ip->i_xtrace,
3712 (void *)(__psint_t)(opcode | (whichfork << 16)),
3713 (void *)fname, (void *)desc, (void *)ip,
3714 (void *)(__psint_t)idx,
3715 (void *)(__psint_t)cnt,
3716 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3717 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3718 (void *)(__psunsigned_t)(r1->l0 >> 32),
3719 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3720 (void *)(__psunsigned_t)(r1->l1 >> 32),
3721 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3722 (void *)(__psunsigned_t)(r2->l0 >> 32),
3723 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3724 (void *)(__psunsigned_t)(r2->l1 >> 32),
3725 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3730 * Add bmap trace entry prior to a call to xfs_iext_remove.
3733 xfs_bmap_trace_delete(
3734 char *fname, /* function name */
3735 char *desc, /* operation description */
3736 xfs_inode_t *ip, /* incore inode pointer */
3737 xfs_extnum_t idx, /* index of entry(entries) deleted */
3738 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3739 int whichfork) /* data or attr fork */
3741 xfs_ifork_t *ifp; /* inode fork pointer */
3743 ifp = XFS_IFORK_PTR(ip, whichfork);
3744 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
3745 cnt, xfs_iext_get_ext(ifp, idx),
3746 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
3751 * Add bmap trace entry prior to a call to xfs_iext_insert, or
3752 * reading in the extents list from the disk (in the btree).
3755 xfs_bmap_trace_insert(
3756 char *fname, /* function name */
3757 char *desc, /* operation description */
3758 xfs_inode_t *ip, /* incore inode pointer */
3759 xfs_extnum_t idx, /* index of entry(entries) inserted */
3760 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3761 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3762 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3763 int whichfork) /* data or attr fork */
3765 xfs_bmbt_rec_t tr1; /* compressed record 1 */
3766 xfs_bmbt_rec_t tr2; /* compressed record 2 if needed */
3768 xfs_bmbt_set_all(&tr1, r1);
3771 xfs_bmbt_set_all(&tr2, r2);
3776 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3777 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3781 * Add bmap trace entry after updating an extent record in place.
3784 xfs_bmap_trace_post_update(
3785 char *fname, /* function name */
3786 char *desc, /* operation description */
3787 xfs_inode_t *ip, /* incore inode pointer */
3788 xfs_extnum_t idx, /* index of entry updated */
3789 int whichfork) /* data or attr fork */
3791 xfs_ifork_t *ifp; /* inode fork pointer */
3793 ifp = XFS_IFORK_PTR(ip, whichfork);
3794 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
3795 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3799 * Add bmap trace entry prior to updating an extent record in place.
3802 xfs_bmap_trace_pre_update(
3803 char *fname, /* function name */
3804 char *desc, /* operation description */
3805 xfs_inode_t *ip, /* incore inode pointer */
3806 xfs_extnum_t idx, /* index of entry to be updated */
3807 int whichfork) /* data or attr fork */
3809 xfs_ifork_t *ifp; /* inode fork pointer */
3811 ifp = XFS_IFORK_PTR(ip, whichfork);
3812 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
3813 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3815 #endif /* XFS_BMAP_TRACE */
3818 * Compute the worst-case number of indirect blocks that will be used
3819 * for ip's delayed extent of length "len".
3821 STATIC xfs_filblks_t
3822 xfs_bmap_worst_indlen(
3823 xfs_inode_t *ip, /* incore inode pointer */
3824 xfs_filblks_t len) /* delayed extent length */
3826 int level; /* btree level number */
3827 int maxrecs; /* maximum record count at this level */
3828 xfs_mount_t *mp; /* mount structure */
3829 xfs_filblks_t rval; /* return value */
3832 maxrecs = mp->m_bmap_dmxr[0];
3833 for (level = 0, rval = 0;
3834 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3837 do_div(len, maxrecs);
3840 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3843 maxrecs = mp->m_bmap_dmxr[1];
3848 #if defined(XFS_RW_TRACE)
3857 if (ip->i_rwtrace == NULL)
3859 ktrace_enter(ip->i_rwtrace,
3860 (void *)(__psint_t)XFS_BUNMAPI,
3862 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3863 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3864 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3865 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3866 (void *)(__psint_t)len,
3867 (void *)(__psint_t)flags,
3868 (void *)(unsigned long)current_cpu(),
3880 * Convert inode from non-attributed to attributed.
3881 * Must not be in a transaction, ip must not be locked.
3883 int /* error code */
3884 xfs_bmap_add_attrfork(
3885 xfs_inode_t *ip, /* incore inode pointer */
3886 int size, /* space new attribute needs */
3887 int rsvd) /* xact may use reserved blks */
3889 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3890 xfs_bmap_free_t flist; /* freed extent records */
3891 xfs_mount_t *mp; /* mount structure */
3892 xfs_trans_t *tp; /* transaction pointer */
3893 unsigned long s; /* spinlock spl value */
3894 int blks; /* space reservation */
3895 int version = 1; /* superblock attr version */
3896 int committed; /* xaction was committed */
3897 int logflags; /* logging flags */
3898 int error; /* error return value */
3900 ASSERT(XFS_IFORK_Q(ip) == 0);
3901 ASSERT(ip->i_df.if_ext_max ==
3902 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3905 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3906 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3907 blks = XFS_ADDAFORK_SPACE_RES(mp);
3909 tp->t_flags |= XFS_TRANS_RESERVE;
3910 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3911 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3913 xfs_ilock(ip, XFS_ILOCK_EXCL);
3914 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
3915 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3916 XFS_QMOPT_RES_REGBLKS);
3918 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3919 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3922 if (XFS_IFORK_Q(ip))
3924 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3926 * For inodes coming from pre-6.2 filesystems.
3928 ASSERT(ip->i_d.di_aformat == 0);
3929 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3931 ASSERT(ip->i_d.di_anextents == 0);
3932 VN_HOLD(XFS_ITOV(ip));
3933 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3934 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3935 switch (ip->i_d.di_format) {
3936 case XFS_DINODE_FMT_DEV:
3937 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3939 case XFS_DINODE_FMT_UUID:
3940 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3942 case XFS_DINODE_FMT_LOCAL:
3943 case XFS_DINODE_FMT_EXTENTS:
3944 case XFS_DINODE_FMT_BTREE:
3945 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3946 if (!ip->i_d.di_forkoff)
3947 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3948 else if (mp->m_flags & XFS_MOUNT_ATTR2)
3953 error = XFS_ERROR(EINVAL);
3956 ip->i_df.if_ext_max =
3957 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3958 ASSERT(ip->i_afp == NULL);
3959 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3960 ip->i_afp->if_ext_max =
3961 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3962 ip->i_afp->if_flags = XFS_IFEXTENTS;
3964 XFS_BMAP_INIT(&flist, &firstblock);
3965 switch (ip->i_d.di_format) {
3966 case XFS_DINODE_FMT_LOCAL:
3967 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3970 case XFS_DINODE_FMT_EXTENTS:
3971 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3974 case XFS_DINODE_FMT_BTREE:
3975 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3983 xfs_trans_log_inode(tp, ip, logflags);
3986 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb) ||
3987 (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2)) {
3988 __int64_t sbfields = 0;
3990 s = XFS_SB_LOCK(mp);
3991 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) {
3992 XFS_SB_VERSION_ADDATTR(&mp->m_sb);
3993 sbfields |= XFS_SB_VERSIONNUM;
3995 if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2) {
3996 XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
3997 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
4000 XFS_SB_UNLOCK(mp, s);
4001 xfs_mod_sb(tp, sbfields);
4003 XFS_SB_UNLOCK(mp, s);
4005 if ((error = xfs_bmap_finish(&tp, &flist, firstblock, &committed)))
4007 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES, NULL);
4008 ASSERT(ip->i_df.if_ext_max ==
4009 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4012 xfs_bmap_cancel(&flist);
4014 ASSERT(ismrlocked(&ip->i_lock,MR_UPDATE));
4015 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4017 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4018 ASSERT(ip->i_df.if_ext_max ==
4019 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4024 * Add the extent to the list of extents to be free at transaction end.
4025 * The list is maintained sorted (by block number).
4030 xfs_fsblock_t bno, /* fs block number of extent */
4031 xfs_filblks_t len, /* length of extent */
4032 xfs_bmap_free_t *flist, /* list of extents */
4033 xfs_mount_t *mp) /* mount point structure */
4035 xfs_bmap_free_item_t *cur; /* current (next) element */
4036 xfs_bmap_free_item_t *new; /* new element */
4037 xfs_bmap_free_item_t *prev; /* previous element */
4039 xfs_agnumber_t agno;
4040 xfs_agblock_t agbno;
4042 ASSERT(bno != NULLFSBLOCK);
4044 ASSERT(len <= MAXEXTLEN);
4045 ASSERT(!ISNULLSTARTBLOCK(bno));
4046 agno = XFS_FSB_TO_AGNO(mp, bno);
4047 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4048 ASSERT(agno < mp->m_sb.sb_agcount);
4049 ASSERT(agbno < mp->m_sb.sb_agblocks);
4050 ASSERT(len < mp->m_sb.sb_agblocks);
4051 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4053 ASSERT(xfs_bmap_free_item_zone != NULL);
4054 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4055 new->xbfi_startblock = bno;
4056 new->xbfi_blockcount = (xfs_extlen_t)len;
4057 for (prev = NULL, cur = flist->xbf_first;
4059 prev = cur, cur = cur->xbfi_next) {
4060 if (cur->xbfi_startblock >= bno)
4064 prev->xbfi_next = new;
4066 flist->xbf_first = new;
4067 new->xbfi_next = cur;
4072 * Compute and fill in the value of the maximum depth of a bmap btree
4073 * in this filesystem. Done once, during mount.
4076 xfs_bmap_compute_maxlevels(
4077 xfs_mount_t *mp, /* file system mount structure */
4078 int whichfork) /* data or attr fork */
4080 int level; /* btree level */
4081 uint maxblocks; /* max blocks at this level */
4082 uint maxleafents; /* max leaf entries possible */
4083 int maxrootrecs; /* max records in root block */
4084 int minleafrecs; /* min records in leaf block */
4085 int minnoderecs; /* min records in node block */
4086 int sz; /* root block size */
4089 * The maximum number of extents in a file, hence the maximum
4090 * number of leaf entries, is controlled by the type of di_nextents
4091 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4092 * (a signed 16-bit number, xfs_aextnum_t).
4094 if (whichfork == XFS_DATA_FORK) {
4095 maxleafents = MAXEXTNUM;
4096 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4097 XFS_BMDR_SPACE_CALC(MINDBTPTRS) : mp->m_attroffset;
4099 maxleafents = MAXAEXTNUM;
4100 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4101 XFS_BMDR_SPACE_CALC(MINABTPTRS) :
4102 mp->m_sb.sb_inodesize - mp->m_attroffset;
4104 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
4105 minleafrecs = mp->m_bmap_dmnr[0];
4106 minnoderecs = mp->m_bmap_dmnr[1];
4107 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4108 for (level = 1; maxblocks > 1; level++) {
4109 if (maxblocks <= maxrootrecs)
4112 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4114 mp->m_bm_maxlevels[whichfork] = level;
4118 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4119 * caller. Frees all the extents that need freeing, which must be done
4120 * last due to locking considerations. We never free any extents in
4121 * the first transaction. This is to allow the caller to make the first
4122 * transaction a synchronous one so that the pointers to the data being
4123 * broken in this transaction will be permanent before the data is actually
4124 * freed. This is necessary to prevent blocks from being reallocated
4125 * and written to before the free and reallocation are actually permanent.
4126 * We do not just make the first transaction synchronous here, because
4127 * there are more efficient ways to gain the same protection in some cases
4128 * (see the file truncation code).
4130 * Return 1 if the given transaction was committed and a new one
4131 * started, and 0 otherwise in the committed parameter.
4136 xfs_trans_t **tp, /* transaction pointer addr */
4137 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4138 xfs_fsblock_t firstblock, /* controlled ag for allocs */
4139 int *committed) /* xact committed or not */
4141 xfs_efd_log_item_t *efd; /* extent free data */
4142 xfs_efi_log_item_t *efi; /* extent free intention */
4143 int error; /* error return value */
4144 xfs_bmap_free_item_t *free; /* free extent item */
4145 unsigned int logres; /* new log reservation */
4146 unsigned int logcount; /* new log count */
4147 xfs_mount_t *mp; /* filesystem mount structure */
4148 xfs_bmap_free_item_t *next; /* next item on free list */
4149 xfs_trans_t *ntp; /* new transaction pointer */
4151 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4152 if (flist->xbf_count == 0) {
4157 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4158 for (free = flist->xbf_first; free; free = free->xbfi_next)
4159 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4160 free->xbfi_blockcount);
4161 logres = ntp->t_log_res;
4162 logcount = ntp->t_log_count;
4163 ntp = xfs_trans_dup(*tp);
4164 error = xfs_trans_commit(*tp, 0, NULL);
4168 * We have a new transaction, so we should return committed=1,
4169 * even though we're returning an error.
4174 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4177 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4178 for (free = flist->xbf_first; free != NULL; free = next) {
4179 next = free->xbfi_next;
4180 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4181 free->xbfi_blockcount))) {
4183 * The bmap free list will be cleaned up at a
4184 * higher level. The EFI will be canceled when
4185 * this transaction is aborted.
4186 * Need to force shutdown here to make sure it
4187 * happens, since this transaction may not be
4191 if (!XFS_FORCED_SHUTDOWN(mp))
4192 xfs_force_shutdown(mp,
4193 (error == EFSCORRUPTED) ?
4194 XFS_CORRUPT_INCORE :
4195 XFS_METADATA_IO_ERROR);
4198 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4199 free->xbfi_blockcount);
4200 xfs_bmap_del_free(flist, NULL, free);
4206 * Free up any items left in the list.
4210 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4212 xfs_bmap_free_item_t *free; /* free list item */
4213 xfs_bmap_free_item_t *next;
4215 if (flist->xbf_count == 0)
4217 ASSERT(flist->xbf_first != NULL);
4218 for (free = flist->xbf_first; free; free = next) {
4219 next = free->xbfi_next;
4220 xfs_bmap_del_free(flist, NULL, free);
4222 ASSERT(flist->xbf_count == 0);
4226 * Returns the file-relative block number of the first unused block(s)
4227 * in the file with at least "len" logically contiguous blocks free.
4228 * This is the lowest-address hole if the file has holes, else the first block
4229 * past the end of file.
4230 * Return 0 if the file is currently local (in-inode).
4233 xfs_bmap_first_unused(
4234 xfs_trans_t *tp, /* transaction pointer */
4235 xfs_inode_t *ip, /* incore inode */
4236 xfs_extlen_t len, /* size of hole to find */
4237 xfs_fileoff_t *first_unused, /* unused block */
4238 int whichfork) /* data or attr fork */
4240 xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
4241 int error; /* error return value */
4242 int idx; /* extent record index */
4243 xfs_ifork_t *ifp; /* inode fork pointer */
4244 xfs_fileoff_t lastaddr; /* last block number seen */
4245 xfs_fileoff_t lowest; /* lowest useful block */
4246 xfs_fileoff_t max; /* starting useful block */
4247 xfs_fileoff_t off; /* offset for this block */
4248 xfs_extnum_t nextents; /* number of extent entries */
4250 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4251 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4252 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4253 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4257 ifp = XFS_IFORK_PTR(ip, whichfork);
4258 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4259 (error = xfs_iread_extents(tp, ip, whichfork)))
4261 lowest = *first_unused;
4262 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4263 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
4264 ep = xfs_iext_get_ext(ifp, idx);
4265 off = xfs_bmbt_get_startoff(ep);
4267 * See if the hole before this extent will work.
4269 if (off >= lowest + len && off - max >= len) {
4270 *first_unused = max;
4273 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4274 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4276 *first_unused = max;
4281 * Returns the file-relative block number of the last block + 1 before
4282 * last_block (input value) in the file.
4283 * This is not based on i_size, it is based on the extent records.
4284 * Returns 0 for local files, as they do not have extent records.
4287 xfs_bmap_last_before(
4288 xfs_trans_t *tp, /* transaction pointer */
4289 xfs_inode_t *ip, /* incore inode */
4290 xfs_fileoff_t *last_block, /* last block */
4291 int whichfork) /* data or attr fork */
4293 xfs_fileoff_t bno; /* input file offset */
4294 int eof; /* hit end of file */
4295 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4296 int error; /* error return value */
4297 xfs_bmbt_irec_t got; /* current extent value */
4298 xfs_ifork_t *ifp; /* inode fork pointer */
4299 xfs_extnum_t lastx; /* last extent used */
4300 xfs_bmbt_irec_t prev; /* previous extent value */
4302 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4303 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4304 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4305 return XFS_ERROR(EIO);
4306 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4310 ifp = XFS_IFORK_PTR(ip, whichfork);
4311 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4312 (error = xfs_iread_extents(tp, ip, whichfork)))
4314 bno = *last_block - 1;
4315 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4317 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4318 if (prev.br_startoff == NULLFILEOFF)
4321 *last_block = prev.br_startoff + prev.br_blockcount;
4324 * Otherwise *last_block is already the right answer.
4330 * Returns the file-relative block number of the first block past eof in
4331 * the file. This is not based on i_size, it is based on the extent records.
4332 * Returns 0 for local files, as they do not have extent records.
4335 xfs_bmap_last_offset(
4336 xfs_trans_t *tp, /* transaction pointer */
4337 xfs_inode_t *ip, /* incore inode */
4338 xfs_fileoff_t *last_block, /* last block */
4339 int whichfork) /* data or attr fork */
4341 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4342 int error; /* error return value */
4343 xfs_ifork_t *ifp; /* inode fork pointer */
4344 xfs_extnum_t nextents; /* number of extent entries */
4346 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4347 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4348 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4349 return XFS_ERROR(EIO);
4350 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4354 ifp = XFS_IFORK_PTR(ip, whichfork);
4355 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4356 (error = xfs_iread_extents(tp, ip, whichfork)))
4358 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4363 ep = xfs_iext_get_ext(ifp, nextents - 1);
4364 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4369 * Returns whether the selected fork of the inode has exactly one
4370 * block or not. For the data fork we check this matches di_size,
4371 * implying the file's range is 0..bsize-1.
4373 int /* 1=>1 block, 0=>otherwise */
4375 xfs_inode_t *ip, /* incore inode */
4376 int whichfork) /* data or attr fork */
4378 xfs_bmbt_rec_t *ep; /* ptr to fork's extent */
4379 xfs_ifork_t *ifp; /* inode fork pointer */
4380 int rval; /* return value */
4381 xfs_bmbt_irec_t s; /* internal version of extent */
4384 if (whichfork == XFS_DATA_FORK)
4385 return ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize;
4387 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4389 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4391 ifp = XFS_IFORK_PTR(ip, whichfork);
4392 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4393 ep = xfs_iext_get_ext(ifp, 0);
4394 xfs_bmbt_get_all(ep, &s);
4395 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4396 if (rval && whichfork == XFS_DATA_FORK)
4397 ASSERT(ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4402 * Read in the extents to if_extents.
4403 * All inode fields are set up by caller, we just traverse the btree
4404 * and copy the records in. If the file system cannot contain unwritten
4405 * extents, the records are checked for no "state" flags.
4408 xfs_bmap_read_extents(
4409 xfs_trans_t *tp, /* transaction pointer */
4410 xfs_inode_t *ip, /* incore inode */
4411 int whichfork) /* data or attr fork */
4413 xfs_bmbt_block_t *block; /* current btree block */
4414 xfs_fsblock_t bno; /* block # of "block" */
4415 xfs_buf_t *bp; /* buffer for "block" */
4416 int error; /* error return value */
4417 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4418 #ifdef XFS_BMAP_TRACE
4419 static char fname[] = "xfs_bmap_read_extents";
4421 xfs_extnum_t i, j; /* index into the extents list */
4422 xfs_ifork_t *ifp; /* fork structure */
4423 int level; /* btree level, for checking */
4424 xfs_mount_t *mp; /* file system mount structure */
4425 xfs_bmbt_ptr_t *pp; /* pointer to block address */
4427 xfs_extnum_t room; /* number of entries there's room for */
4431 ifp = XFS_IFORK_PTR(ip, whichfork);
4432 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4433 XFS_EXTFMT_INODE(ip);
4434 block = ifp->if_broot;
4436 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4438 level = be16_to_cpu(block->bb_level);
4440 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
4441 ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
4442 ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
4443 ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
4444 bno = INT_GET(*pp, ARCH_CONVERT);
4446 * Go down the tree until leaf level is reached, following the first
4447 * pointer (leftmost) at each level.
4449 while (level-- > 0) {
4450 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4451 XFS_BMAP_BTREE_REF)))
4453 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4454 XFS_WANT_CORRUPTED_GOTO(
4455 XFS_BMAP_SANITY_CHECK(mp, block, level),
4459 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
4460 1, mp->m_bmap_dmxr[1]);
4461 XFS_WANT_CORRUPTED_GOTO(
4462 XFS_FSB_SANITY_CHECK(mp, INT_GET(*pp, ARCH_CONVERT)),
4464 bno = INT_GET(*pp, ARCH_CONVERT);
4465 xfs_trans_brelse(tp, bp);
4468 * Here with bp and block set to the leftmost leaf node in the tree.
4470 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4473 * Loop over all leaf nodes. Copy information to the extent records.
4476 xfs_bmbt_rec_t *frp, *trp;
4477 xfs_fsblock_t nextbno;
4478 xfs_extnum_t num_recs;
4482 num_recs = be16_to_cpu(block->bb_numrecs);
4483 if (unlikely(i + num_recs > room)) {
4484 ASSERT(i + num_recs <= room);
4485 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4486 "corrupt dinode %Lu, (btree extents).",
4487 (unsigned long long) ip->i_ino);
4488 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4493 XFS_WANT_CORRUPTED_GOTO(
4494 XFS_BMAP_SANITY_CHECK(mp, block, 0),
4497 * Read-ahead the next leaf block, if any.
4499 nextbno = be64_to_cpu(block->bb_rightsib);
4500 if (nextbno != NULLFSBLOCK)
4501 xfs_btree_reada_bufl(mp, nextbno, 1);
4503 * Copy records into the extent records.
4505 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
4506 block, 1, mp->m_bmap_dmxr[0]);
4508 for (j = 0; j < num_recs; j++, i++, frp++) {
4509 trp = xfs_iext_get_ext(ifp, i);
4510 trp->l0 = INT_GET(frp->l0, ARCH_CONVERT);
4511 trp->l1 = INT_GET(frp->l1, ARCH_CONVERT);
4513 if (exntf == XFS_EXTFMT_NOSTATE) {
4515 * Check all attribute bmap btree records and
4516 * any "older" data bmap btree records for a
4517 * set bit in the "extent flag" position.
4519 if (unlikely(xfs_check_nostate_extents(ifp,
4520 start, num_recs))) {
4521 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4527 xfs_trans_brelse(tp, bp);
4530 * If we've reached the end, stop.
4532 if (bno == NULLFSBLOCK)
4534 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4535 XFS_BMAP_BTREE_REF)))
4537 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4539 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4540 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4541 xfs_bmap_trace_exlist(fname, ip, i, whichfork);
4544 xfs_trans_brelse(tp, bp);
4545 return XFS_ERROR(EFSCORRUPTED);
4548 #ifdef XFS_BMAP_TRACE
4550 * Add bmap trace insert entries for all the contents of the extent records.
4553 xfs_bmap_trace_exlist(
4554 char *fname, /* function name */
4555 xfs_inode_t *ip, /* incore inode pointer */
4556 xfs_extnum_t cnt, /* count of entries in the list */
4557 int whichfork) /* data or attr fork */
4559 xfs_bmbt_rec_t *ep; /* current extent record */
4560 xfs_extnum_t idx; /* extent record index */
4561 xfs_ifork_t *ifp; /* inode fork pointer */
4562 xfs_bmbt_irec_t s; /* file extent record */
4564 ifp = XFS_IFORK_PTR(ip, whichfork);
4565 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4566 for (idx = 0; idx < cnt; idx++) {
4567 ep = xfs_iext_get_ext(ifp, idx);
4568 xfs_bmbt_get_all(ep, &s);
4569 xfs_bmap_trace_insert(fname, "exlist", ip, idx, 1, &s, NULL,
4577 * Validate that the bmbt_irecs being returned from bmapi are valid
4578 * given the callers original parameters. Specifically check the
4579 * ranges of the returned irecs to ensure that they only extent beyond
4580 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4583 xfs_bmap_validate_ret(
4587 xfs_bmbt_irec_t *mval,
4591 int i; /* index to map values */
4593 ASSERT(ret_nmap <= nmap);
4595 for (i = 0; i < ret_nmap; i++) {
4596 ASSERT(mval[i].br_blockcount > 0);
4597 if (!(flags & XFS_BMAPI_ENTIRE)) {
4598 ASSERT(mval[i].br_startoff >= bno);
4599 ASSERT(mval[i].br_blockcount <= len);
4600 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4603 ASSERT(mval[i].br_startoff < bno + len);
4604 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4608 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4609 mval[i].br_startoff);
4610 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4611 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4612 mval[i].br_startblock != HOLESTARTBLOCK);
4613 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4614 mval[i].br_state == XFS_EXT_UNWRITTEN);
4621 * Map file blocks to filesystem blocks.
4622 * File range is given by the bno/len pair.
4623 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4624 * into a hole or past eof.
4625 * Only allocates blocks from a single allocation group,
4626 * to avoid locking problems.
4627 * The returned value in "firstblock" from the first call in a transaction
4628 * must be remembered and presented to subsequent calls in "firstblock".
4629 * An upper bound for the number of blocks to be allocated is supplied to
4630 * the first call in "total"; if no allocation group has that many free
4631 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4635 xfs_trans_t *tp, /* transaction pointer */
4636 xfs_inode_t *ip, /* incore inode */
4637 xfs_fileoff_t bno, /* starting file offs. mapped */
4638 xfs_filblks_t len, /* length to map in file */
4639 int flags, /* XFS_BMAPI_... */
4640 xfs_fsblock_t *firstblock, /* first allocated block
4641 controls a.g. for allocs */
4642 xfs_extlen_t total, /* total blocks needed */
4643 xfs_bmbt_irec_t *mval, /* output: map values */
4644 int *nmap, /* i/o: mval size/count */
4645 xfs_bmap_free_t *flist) /* i/o: list extents to free */
4647 xfs_fsblock_t abno; /* allocated block number */
4648 xfs_extlen_t alen; /* allocated extent length */
4649 xfs_fileoff_t aoff; /* allocated file offset */
4650 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
4651 xfs_btree_cur_t *cur; /* bmap btree cursor */
4652 xfs_fileoff_t end; /* end of mapped file region */
4653 int eof; /* we've hit the end of extents */
4654 xfs_bmbt_rec_t *ep; /* extent record pointer */
4655 int error; /* error return */
4656 xfs_bmbt_irec_t got; /* current file extent record */
4657 xfs_ifork_t *ifp; /* inode fork pointer */
4658 xfs_extlen_t indlen; /* indirect blocks length */
4659 xfs_extnum_t lastx; /* last useful extent number */
4660 int logflags; /* flags for transaction logging */
4661 xfs_extlen_t minleft; /* min blocks left after allocation */
4662 xfs_extlen_t minlen; /* min allocation size */
4663 xfs_mount_t *mp; /* xfs mount structure */
4664 int n; /* current extent index */
4665 int nallocs; /* number of extents alloc\'d */
4666 xfs_extnum_t nextents; /* number of extents in file */
4667 xfs_fileoff_t obno; /* old block number (offset) */
4668 xfs_bmbt_irec_t prev; /* previous file extent record */
4669 int tmp_logflags; /* temp flags holder */
4670 int whichfork; /* data or attr fork */
4671 char inhole; /* current location is hole in file */
4672 char wasdelay; /* old extent was delayed */
4673 char wr; /* this is a write request */
4674 char rt; /* this is a realtime file */
4676 xfs_fileoff_t orig_bno; /* original block number value */
4677 int orig_flags; /* original flags arg value */
4678 xfs_filblks_t orig_len; /* original value of len arg */
4679 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4680 int orig_nmap; /* original value of *nmap */
4689 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4690 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4691 XFS_ATTR_FORK : XFS_DATA_FORK;
4693 if (unlikely(XFS_TEST_ERROR(
4694 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4695 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4696 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4697 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4698 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4699 return XFS_ERROR(EFSCORRUPTED);
4701 if (XFS_FORCED_SHUTDOWN(mp))
4702 return XFS_ERROR(EIO);
4703 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4704 ifp = XFS_IFORK_PTR(ip, whichfork);
4705 ASSERT(ifp->if_ext_max ==
4706 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4707 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4708 XFS_STATS_INC(xs_blk_mapw);
4710 XFS_STATS_INC(xs_blk_mapr);
4712 * IGSTATE flag is used to combine extents which
4713 * differ only due to the state of the extents.
4714 * This technique is used from xfs_getbmap()
4715 * when the caller does not wish to see the
4716 * separation (which is the default).
4718 * This technique is also used when writing a
4719 * buffer which has been partially written,
4720 * (usually by being flushed during a chunkread),
4721 * to ensure one write takes place. This also
4722 * prevents a change in the xfs inode extents at
4723 * this time, intentionally. This change occurs
4724 * on completion of the write operation, in
4725 * xfs_strat_comp(), where the xfs_bmapi() call
4726 * is transactioned, and the extents combined.
4728 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4729 wr = 0; /* no allocations are allowed */
4730 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
4734 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4736 if ((error = xfs_bmap_local_to_extents(tp, ip,
4737 firstblock, total, &logflags, whichfork)))
4740 if (wr && *firstblock == NULLFSBLOCK) {
4741 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4742 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4747 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4748 (error = xfs_iread_extents(tp, ip, whichfork)))
4750 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4752 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4757 while (bno < end && n < *nmap) {
4759 * Reading past eof, act as though there's a hole
4763 got.br_startoff = end;
4764 inhole = eof || got.br_startoff > bno;
4765 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
4766 ISNULLSTARTBLOCK(got.br_startblock);
4768 * First, deal with the hole before the allocated space
4769 * that we found, if any.
4771 if (wr && (inhole || wasdelay)) {
4773 * For the wasdelay case, we could also just
4774 * allocate the stuff asked for in this bmap call
4775 * but that wouldn't be as good.
4777 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
4778 alen = (xfs_extlen_t)got.br_blockcount;
4779 aoff = got.br_startoff;
4780 if (lastx != NULLEXTNUM && lastx) {
4781 ep = xfs_iext_get_ext(ifp, lastx - 1);
4782 xfs_bmbt_get_all(ep, &prev);
4784 } else if (wasdelay) {
4785 alen = (xfs_extlen_t)
4786 XFS_FILBLKS_MIN(len,
4788 got.br_blockcount) - bno);
4791 alen = (xfs_extlen_t)
4792 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4794 alen = (xfs_extlen_t)
4795 XFS_FILBLKS_MIN(alen,
4796 got.br_startoff - bno);
4799 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4800 if (flags & XFS_BMAPI_DELAY) {
4803 /* Figure out the extent size, adjust alen */
4805 if (!(extsz = ip->i_d.di_extsize))
4806 extsz = mp->m_sb.sb_rextsize;
4808 extsz = ip->i_d.di_extsize;
4811 error = xfs_bmap_extsize_align(mp,
4814 flags&XFS_BMAPI_DELAY,
4815 flags&XFS_BMAPI_CONVERT,
4821 extsz = alen / mp->m_sb.sb_rextsize;
4824 * Make a transaction-less quota reservation for
4825 * delayed allocation blocks. This number gets
4827 * We return EDQUOT if we haven't allocated
4828 * blks already inside this loop;
4830 if (XFS_TRANS_RESERVE_QUOTA_NBLKS(
4831 mp, NULL, ip, (long)alen, 0,
4832 rt ? XFS_QMOPT_RES_RTBLKS :
4833 XFS_QMOPT_RES_REGBLKS)) {
4836 ASSERT(cur == NULL);
4837 return XFS_ERROR(EDQUOT);
4843 * Split changing sb for alen and indlen since
4844 * they could be coming from different places.
4846 indlen = (xfs_extlen_t)
4847 xfs_bmap_worst_indlen(ip, alen);
4851 error = xfs_mod_incore_sb(mp,
4854 XFS_BMAPI_RSVBLOCKS));
4856 error = xfs_mod_incore_sb(mp,
4859 XFS_BMAPI_RSVBLOCKS));
4862 error = xfs_mod_incore_sb(mp,
4865 XFS_BMAPI_RSVBLOCKS));
4867 xfs_mod_incore_sb(mp,
4870 XFS_BMAPI_RSVBLOCKS));
4872 xfs_mod_incore_sb(mp,
4875 XFS_BMAPI_RSVBLOCKS));
4879 if (XFS_IS_QUOTA_ON(mp))
4880 /* unreserve the blocks now */
4882 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4885 XFS_QMOPT_RES_RTBLKS :
4886 XFS_QMOPT_RES_REGBLKS);
4890 ip->i_delayed_blks += alen;
4891 abno = NULLSTARTBLOCK(indlen);
4894 * If first time, allocate and fill in
4895 * once-only bma fields.
4897 if (bma.ip == NULL) {
4905 /* Indicate if this is the first user data
4906 * in the file, or just any user data.
4908 if (!(flags & XFS_BMAPI_METADATA)) {
4909 bma.userdata = (aoff == 0) ?
4910 XFS_ALLOC_INITIAL_USER_DATA :
4914 * Fill in changeable bma fields.
4917 bma.firstblock = *firstblock;
4920 bma.conv = (flags & XFS_BMAPI_CONVERT);
4921 bma.wasdel = wasdelay;
4922 bma.minlen = minlen;
4923 bma.low = flist->xbf_low;
4924 bma.minleft = minleft;
4926 * Only want to do the alignment at the
4927 * eof if it is userdata and allocation length
4928 * is larger than a stripe unit.
4930 if (mp->m_dalign && alen >= mp->m_dalign &&
4931 (!(flags & XFS_BMAPI_METADATA)) &&
4932 (whichfork == XFS_DATA_FORK)) {
4933 if ((error = xfs_bmap_isaeof(ip, aoff,
4934 whichfork, &bma.aeof)))
4941 if ((error = xfs_bmap_alloc(&bma)))
4944 * Copy out result fields.
4947 if ((flist->xbf_low = bma.low))
4951 ASSERT(*firstblock == NULLFSBLOCK ||
4952 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4953 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4955 XFS_FSB_TO_AGNO(mp, *firstblock) <
4956 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4957 *firstblock = bma.firstblock;
4959 cur->bc_private.b.firstblock =
4961 if (abno == NULLFSBLOCK)
4963 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4964 cur = xfs_btree_init_cursor(mp,
4965 tp, NULL, 0, XFS_BTNUM_BMAP,
4967 cur->bc_private.b.firstblock =
4969 cur->bc_private.b.flist = flist;
4972 * Bump the number of extents we've allocated
4978 cur->bc_private.b.flags =
4979 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4980 got.br_startoff = aoff;
4981 got.br_startblock = abno;
4982 got.br_blockcount = alen;
4983 got.br_state = XFS_EXT_NORM; /* assume normal */
4985 * Determine state of extent, and the filesystem.
4986 * A wasdelay extent has been initialized, so
4987 * shouldn't be flagged as unwritten.
4989 if (wr && XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4990 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
4991 got.br_state = XFS_EXT_UNWRITTEN;
4993 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
4994 firstblock, flist, &tmp_logflags, whichfork,
4995 (flags & XFS_BMAPI_RSVBLOCKS));
4996 logflags |= tmp_logflags;
4999 lastx = ifp->if_lastex;
5000 ep = xfs_iext_get_ext(ifp, lastx);
5001 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5002 xfs_bmbt_get_all(ep, &got);
5003 ASSERT(got.br_startoff <= aoff);
5004 ASSERT(got.br_startoff + got.br_blockcount >=
5007 if (flags & XFS_BMAPI_DELAY) {
5008 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
5009 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
5011 ASSERT(got.br_state == XFS_EXT_NORM ||
5012 got.br_state == XFS_EXT_UNWRITTEN);
5015 * Fall down into the found allocated space case.
5017 } else if (inhole) {
5019 * Reading in a hole.
5021 mval->br_startoff = bno;
5022 mval->br_startblock = HOLESTARTBLOCK;
5023 mval->br_blockcount =
5024 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5025 mval->br_state = XFS_EXT_NORM;
5026 bno += mval->br_blockcount;
5027 len -= mval->br_blockcount;
5033 * Then deal with the allocated space we found.
5036 if (!(flags & XFS_BMAPI_ENTIRE) &&
5037 (got.br_startoff + got.br_blockcount > obno)) {
5040 ASSERT((bno >= obno) || (n == 0));
5042 mval->br_startoff = bno;
5043 if (ISNULLSTARTBLOCK(got.br_startblock)) {
5044 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5045 mval->br_startblock = DELAYSTARTBLOCK;
5047 mval->br_startblock =
5049 (bno - got.br_startoff);
5051 * Return the minimum of what we got and what we
5052 * asked for for the length. We can use the len
5053 * variable here because it is modified below
5054 * and we could have been there before coming
5055 * here if the first part of the allocation
5056 * didn't overlap what was asked for.
5058 mval->br_blockcount =
5059 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5060 (bno - got.br_startoff));
5061 mval->br_state = got.br_state;
5062 ASSERT(mval->br_blockcount <= len);
5065 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
5066 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5067 mval->br_startblock = DELAYSTARTBLOCK;
5072 * Check if writing previously allocated but
5073 * unwritten extents.
5075 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5076 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5078 * Modify (by adding) the state flag, if writing.
5080 ASSERT(mval->br_blockcount <= len);
5081 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5082 cur = xfs_btree_init_cursor(mp,
5083 tp, NULL, 0, XFS_BTNUM_BMAP,
5085 cur->bc_private.b.firstblock =
5087 cur->bc_private.b.flist = flist;
5089 mval->br_state = XFS_EXT_NORM;
5090 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
5091 firstblock, flist, &tmp_logflags, whichfork,
5092 (flags & XFS_BMAPI_RSVBLOCKS));
5093 logflags |= tmp_logflags;
5096 lastx = ifp->if_lastex;
5097 ep = xfs_iext_get_ext(ifp, lastx);
5098 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5099 xfs_bmbt_get_all(ep, &got);
5101 * We may have combined previously unwritten
5102 * space with written space, so generate
5105 if (mval->br_blockcount < len)
5109 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5110 ((mval->br_startoff + mval->br_blockcount) <= end));
5111 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5112 (mval->br_blockcount <= len) ||
5113 (mval->br_startoff < obno));
5114 bno = mval->br_startoff + mval->br_blockcount;
5116 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5117 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5118 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5119 ASSERT(mval->br_state == mval[-1].br_state);
5120 mval[-1].br_blockcount = mval->br_blockcount;
5121 mval[-1].br_state = mval->br_state;
5122 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5123 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5124 mval[-1].br_startblock != HOLESTARTBLOCK &&
5125 mval->br_startblock ==
5126 mval[-1].br_startblock + mval[-1].br_blockcount &&
5127 ((flags & XFS_BMAPI_IGSTATE) ||
5128 mval[-1].br_state == mval->br_state)) {
5129 ASSERT(mval->br_startoff ==
5130 mval[-1].br_startoff + mval[-1].br_blockcount);
5131 mval[-1].br_blockcount += mval->br_blockcount;
5133 mval->br_startblock == DELAYSTARTBLOCK &&
5134 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5135 mval->br_startoff ==
5136 mval[-1].br_startoff + mval[-1].br_blockcount) {
5137 mval[-1].br_blockcount += mval->br_blockcount;
5138 mval[-1].br_state = mval->br_state;
5139 } else if (!((n == 0) &&
5140 ((mval->br_startoff + mval->br_blockcount) <=
5146 * If we're done, stop now. Stop when we've allocated
5147 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5148 * the transaction may get too big.
5150 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5153 * Else go on to the next record.
5155 ep = xfs_iext_get_ext(ifp, ++lastx);
5156 if (lastx >= nextents) {
5160 xfs_bmbt_get_all(ep, &got);
5162 ifp->if_lastex = lastx;
5165 * Transform from btree to extents, give it cur.
5167 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5168 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5170 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5171 &tmp_logflags, whichfork);
5172 logflags |= tmp_logflags;
5176 ASSERT(ifp->if_ext_max ==
5177 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5178 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5179 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5184 * Log everything. Do this after conversion, there's no point in
5185 * logging the extent records if we've converted to btree format.
5187 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5188 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5189 logflags &= ~XFS_ILOG_FEXT(whichfork);
5190 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5191 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5192 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5194 * Log whatever the flags say, even if error. Otherwise we might miss
5195 * detecting a case where the data is changed, there's an error,
5196 * and it's not logged so we don't shutdown when we should.
5200 xfs_trans_log_inode(tp, ip, logflags);
5204 ASSERT(*firstblock == NULLFSBLOCK ||
5205 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5207 cur->bc_private.b.firstblock) ||
5209 XFS_FSB_TO_AGNO(mp, *firstblock) <
5211 cur->bc_private.b.firstblock)));
5212 *firstblock = cur->bc_private.b.firstblock;
5214 xfs_btree_del_cursor(cur,
5215 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5218 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5224 * Map file blocks to filesystem blocks, simple version.
5225 * One block (extent) only, read-only.
5226 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5227 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5228 * was set and all the others were clear.
5232 xfs_trans_t *tp, /* transaction pointer */
5233 xfs_inode_t *ip, /* incore inode */
5234 int whichfork, /* data or attr fork */
5235 xfs_fsblock_t *fsb, /* output: mapped block */
5236 xfs_fileoff_t bno) /* starting file offs. mapped */
5238 int eof; /* we've hit the end of extents */
5239 int error; /* error return */
5240 xfs_bmbt_irec_t got; /* current file extent record */
5241 xfs_ifork_t *ifp; /* inode fork pointer */
5242 xfs_extnum_t lastx; /* last useful extent number */
5243 xfs_bmbt_irec_t prev; /* previous file extent record */
5245 ifp = XFS_IFORK_PTR(ip, whichfork);
5247 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5248 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5249 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5251 return XFS_ERROR(EFSCORRUPTED);
5253 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5254 return XFS_ERROR(EIO);
5255 XFS_STATS_INC(xs_blk_mapr);
5256 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5257 (error = xfs_iread_extents(tp, ip, whichfork)))
5259 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5262 * Reading past eof, act as though there's a hole
5265 if (eof || got.br_startoff > bno) {
5269 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
5270 ASSERT(bno < got.br_startoff + got.br_blockcount);
5271 *fsb = got.br_startblock + (bno - got.br_startoff);
5272 ifp->if_lastex = lastx;
5277 * Unmap (remove) blocks from a file.
5278 * If nexts is nonzero then the number of extents to remove is limited to
5279 * that value. If not all extents in the block range can be removed then
5284 xfs_trans_t *tp, /* transaction pointer */
5285 struct xfs_inode *ip, /* incore inode */
5286 xfs_fileoff_t bno, /* starting offset to unmap */
5287 xfs_filblks_t len, /* length to unmap in file */
5288 int flags, /* misc flags */
5289 xfs_extnum_t nexts, /* number of extents max */
5290 xfs_fsblock_t *firstblock, /* first allocated block
5291 controls a.g. for allocs */
5292 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5293 int *done) /* set if not done yet */
5295 xfs_btree_cur_t *cur; /* bmap btree cursor */
5296 xfs_bmbt_irec_t del; /* extent being deleted */
5297 int eof; /* is deleting at eof */
5298 xfs_bmbt_rec_t *ep; /* extent record pointer */
5299 int error; /* error return value */
5300 xfs_extnum_t extno; /* extent number in list */
5301 xfs_bmbt_irec_t got; /* current extent record */
5302 xfs_ifork_t *ifp; /* inode fork pointer */
5303 int isrt; /* freeing in rt area */
5304 xfs_extnum_t lastx; /* last extent index used */
5305 int logflags; /* transaction logging flags */
5306 xfs_extlen_t mod; /* rt extent offset */
5307 xfs_mount_t *mp; /* mount structure */
5308 xfs_extnum_t nextents; /* number of file extents */
5309 xfs_bmbt_irec_t prev; /* previous extent record */
5310 xfs_fileoff_t start; /* first file offset deleted */
5311 int tmp_logflags; /* partial logging flags */
5312 int wasdel; /* was a delayed alloc extent */
5313 int whichfork; /* data or attribute fork */
5314 int rsvd; /* OK to allocate reserved blocks */
5317 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5318 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5319 XFS_ATTR_FORK : XFS_DATA_FORK;
5320 ifp = XFS_IFORK_PTR(ip, whichfork);
5322 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5323 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5324 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5326 return XFS_ERROR(EFSCORRUPTED);
5329 if (XFS_FORCED_SHUTDOWN(mp))
5330 return XFS_ERROR(EIO);
5331 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5334 ASSERT(ifp->if_ext_max ==
5335 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5336 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5337 (error = xfs_iread_extents(tp, ip, whichfork)))
5339 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5340 if (nextents == 0) {
5344 XFS_STATS_INC(xs_blk_unmap);
5345 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5347 bno = start + len - 1;
5348 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5351 * Check to see if the given block number is past the end of the
5352 * file, back up to the last block if so...
5355 ep = xfs_iext_get_ext(ifp, --lastx);
5356 xfs_bmbt_get_all(ep, &got);
5357 bno = got.br_startoff + got.br_blockcount - 1;
5360 if (ifp->if_flags & XFS_IFBROOT) {
5361 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5362 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
5364 cur->bc_private.b.firstblock = *firstblock;
5365 cur->bc_private.b.flist = flist;
5366 cur->bc_private.b.flags = 0;
5370 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5371 (nexts == 0 || extno < nexts)) {
5373 * Is the found extent after a hole in which bno lives?
5374 * Just back up to the previous extent, if so.
5376 if (got.br_startoff > bno) {
5379 ep = xfs_iext_get_ext(ifp, lastx);
5380 xfs_bmbt_get_all(ep, &got);
5383 * Is the last block of this extent before the range
5384 * we're supposed to delete? If so, we're done.
5386 bno = XFS_FILEOFF_MIN(bno,
5387 got.br_startoff + got.br_blockcount - 1);
5391 * Then deal with the (possibly delayed) allocated space
5396 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
5397 if (got.br_startoff < start) {
5398 del.br_startoff = start;
5399 del.br_blockcount -= start - got.br_startoff;
5401 del.br_startblock += start - got.br_startoff;
5403 if (del.br_startoff + del.br_blockcount > bno + 1)
5404 del.br_blockcount = bno + 1 - del.br_startoff;
5405 sum = del.br_startblock + del.br_blockcount;
5407 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5409 * Realtime extent not lined up at the end.
5410 * The extent could have been split into written
5411 * and unwritten pieces, or we could just be
5412 * unmapping part of it. But we can't really
5413 * get rid of part of a realtime extent.
5415 if (del.br_state == XFS_EXT_UNWRITTEN ||
5416 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5418 * This piece is unwritten, or we're not
5419 * using unwritten extents. Skip over it.
5422 bno -= mod > del.br_blockcount ?
5423 del.br_blockcount : mod;
5424 if (bno < got.br_startoff) {
5426 xfs_bmbt_get_all(xfs_iext_get_ext(
5432 * It's written, turn it unwritten.
5433 * This is better than zeroing it.
5435 ASSERT(del.br_state == XFS_EXT_NORM);
5436 ASSERT(xfs_trans_get_block_res(tp) > 0);
5438 * If this spans a realtime extent boundary,
5439 * chop it back to the start of the one we end at.
5441 if (del.br_blockcount > mod) {
5442 del.br_startoff += del.br_blockcount - mod;
5443 del.br_startblock += del.br_blockcount - mod;
5444 del.br_blockcount = mod;
5446 del.br_state = XFS_EXT_UNWRITTEN;
5447 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5448 firstblock, flist, &logflags, XFS_DATA_FORK, 0);
5453 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5455 * Realtime extent is lined up at the end but not
5456 * at the front. We'll get rid of full extents if
5459 mod = mp->m_sb.sb_rextsize - mod;
5460 if (del.br_blockcount > mod) {
5461 del.br_blockcount -= mod;
5462 del.br_startoff += mod;
5463 del.br_startblock += mod;
5464 } else if ((del.br_startoff == start &&
5465 (del.br_state == XFS_EXT_UNWRITTEN ||
5466 xfs_trans_get_block_res(tp) == 0)) ||
5467 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5469 * Can't make it unwritten. There isn't
5470 * a full extent here so just skip it.
5472 ASSERT(bno >= del.br_blockcount);
5473 bno -= del.br_blockcount;
5474 if (bno < got.br_startoff) {
5476 xfs_bmbt_get_all(--ep, &got);
5479 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5481 * This one is already unwritten.
5482 * It must have a written left neighbor.
5483 * Unwrite the killed part of that one and
5487 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5489 ASSERT(prev.br_state == XFS_EXT_NORM);
5490 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
5491 ASSERT(del.br_startblock ==
5492 prev.br_startblock + prev.br_blockcount);
5493 if (prev.br_startoff < start) {
5494 mod = start - prev.br_startoff;
5495 prev.br_blockcount -= mod;
5496 prev.br_startblock += mod;
5497 prev.br_startoff = start;
5499 prev.br_state = XFS_EXT_UNWRITTEN;
5500 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5501 &prev, firstblock, flist, &logflags,
5507 ASSERT(del.br_state == XFS_EXT_NORM);
5508 del.br_state = XFS_EXT_UNWRITTEN;
5509 error = xfs_bmap_add_extent(ip, lastx, &cur,
5510 &del, firstblock, flist, &logflags,
5518 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
5519 /* Update realtime/data freespace, unreserve quota */
5521 xfs_filblks_t rtexts;
5523 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5524 do_div(rtexts, mp->m_sb.sb_rextsize);
5525 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5527 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5528 NULL, ip, -((long)del.br_blockcount), 0,
5529 XFS_QMOPT_RES_RTBLKS);
5531 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
5532 (int)del.br_blockcount, rsvd);
5533 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5534 NULL, ip, -((long)del.br_blockcount), 0,
5535 XFS_QMOPT_RES_REGBLKS);
5537 ip->i_delayed_blks -= del.br_blockcount;
5539 cur->bc_private.b.flags |=
5540 XFS_BTCUR_BPRV_WASDEL;
5542 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5544 * If it's the case where the directory code is running
5545 * with no block reservation, and the deleted block is in
5546 * the middle of its extent, and the resulting insert
5547 * of an extent would cause transformation to btree format,
5548 * then reject it. The calling code will then swap
5549 * blocks around instead.
5550 * We have to do this now, rather than waiting for the
5551 * conversion to btree format, since the transaction
5554 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5555 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5556 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5557 del.br_startoff > got.br_startoff &&
5558 del.br_startoff + del.br_blockcount <
5559 got.br_startoff + got.br_blockcount) {
5560 error = XFS_ERROR(ENOSPC);
5563 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5564 &tmp_logflags, whichfork, rsvd);
5565 logflags |= tmp_logflags;
5568 bno = del.br_startoff - 1;
5570 lastx = ifp->if_lastex;
5572 * If not done go on to the next (previous) record.
5573 * Reset ep in case the extents array was re-alloced.
5575 ep = xfs_iext_get_ext(ifp, lastx);
5576 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5577 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5578 xfs_bmbt_get_startoff(ep) > bno) {
5580 ep = xfs_iext_get_ext(ifp, lastx);
5583 xfs_bmbt_get_all(ep, &got);
5587 ifp->if_lastex = lastx;
5588 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5589 ASSERT(ifp->if_ext_max ==
5590 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5592 * Convert to a btree if necessary.
5594 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5595 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5596 ASSERT(cur == NULL);
5597 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5598 &cur, 0, &tmp_logflags, whichfork);
5599 logflags |= tmp_logflags;
5604 * transform from btree to extents, give it cur
5606 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5607 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5608 ASSERT(cur != NULL);
5609 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5611 logflags |= tmp_logflags;
5616 * transform from extents to local?
5618 ASSERT(ifp->if_ext_max ==
5619 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5623 * Log everything. Do this after conversion, there's no point in
5624 * logging the extent records if we've converted to btree format.
5626 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5627 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5628 logflags &= ~XFS_ILOG_FEXT(whichfork);
5629 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5630 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5631 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5633 * Log inode even in the error case, if the transaction
5634 * is dirty we'll need to shut down the filesystem.
5637 xfs_trans_log_inode(tp, ip, logflags);
5640 *firstblock = cur->bc_private.b.firstblock;
5641 cur->bc_private.b.allocated = 0;
5643 xfs_btree_del_cursor(cur,
5644 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5650 * Fcntl interface to xfs_bmapi.
5652 int /* error code */
5654 bhv_desc_t *bdp, /* XFS behavior descriptor*/
5655 struct getbmap *bmv, /* user bmap structure */
5656 void __user *ap, /* pointer to user's array */
5657 int interface) /* interface flags */
5659 __int64_t bmvend; /* last block requested */
5660 int error; /* return value */
5661 __int64_t fixlen; /* length for -1 case */
5662 int i; /* extent number */
5663 xfs_inode_t *ip; /* xfs incore inode pointer */
5664 vnode_t *vp; /* corresponding vnode */
5665 int lock; /* lock state */
5666 xfs_bmbt_irec_t *map; /* buffer for user's data */
5667 xfs_mount_t *mp; /* file system mount point */
5668 int nex; /* # of user extents can do */
5669 int nexleft; /* # of user extents left */
5670 int subnex; /* # of bmapi's can do */
5671 int nmap; /* number of map entries */
5672 struct getbmap out; /* output structure */
5673 int whichfork; /* data or attr fork */
5674 int prealloced; /* this is a file with
5675 * preallocated data space */
5676 int sh_unwritten; /* true, if unwritten */
5677 /* extents listed separately */
5678 int bmapi_flags; /* flags for xfs_bmapi */
5679 __int32_t oflags; /* getbmapx bmv_oflags field */
5681 vp = BHV_TO_VNODE(bdp);
5682 ip = XFS_BHVTOI(bdp);
5685 whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5686 sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
5688 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5689 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5690 * bit is set for the file, generate a read event in order
5691 * that the DMAPI application may do its thing before we return
5692 * the extents. Usually this means restoring user file data to
5693 * regions of the file that look like holes.
5695 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5696 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5697 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5698 * could misinterpret holes in a DMAPI file as true holes,
5699 * when in fact they may represent offline user data.
5701 if ( (interface & BMV_IF_NO_DMAPI_READ) == 0
5702 && DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)
5703 && whichfork == XFS_DATA_FORK) {
5705 error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, 0, 0, 0, NULL);
5707 return XFS_ERROR(error);
5710 if (whichfork == XFS_ATTR_FORK) {
5711 if (XFS_IFORK_Q(ip)) {
5712 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5713 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5714 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5715 return XFS_ERROR(EINVAL);
5716 } else if (unlikely(
5717 ip->i_d.di_aformat != 0 &&
5718 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5719 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5721 return XFS_ERROR(EFSCORRUPTED);
5723 } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5724 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5725 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5726 return XFS_ERROR(EINVAL);
5727 if (whichfork == XFS_DATA_FORK) {
5728 if ((ip->i_d.di_extsize && (ip->i_d.di_flags &
5729 (XFS_DIFLAG_REALTIME|XFS_DIFLAG_EXTSIZE))) ||
5730 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5732 fixlen = XFS_MAXIOFFSET(mp);
5735 fixlen = ip->i_d.di_size;
5742 if (bmv->bmv_length == -1) {
5743 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5744 bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
5746 } else if (bmv->bmv_length < 0)
5747 return XFS_ERROR(EINVAL);
5748 if (bmv->bmv_length == 0) {
5749 bmv->bmv_entries = 0;
5752 nex = bmv->bmv_count - 1;
5754 return XFS_ERROR(EINVAL);
5755 bmvend = bmv->bmv_offset + bmv->bmv_length;
5757 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5759 if (whichfork == XFS_DATA_FORK && ip->i_delayed_blks) {
5760 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
5761 VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
5764 ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
5766 lock = xfs_ilock_map_shared(ip);
5769 * Don't let nex be bigger than the number of extents
5770 * we can have assuming alternating holes and real extents.
5772 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5773 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5775 bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
5776 ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
5779 * Allocate enough space to handle "subnex" maps at a time.
5782 map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
5784 bmv->bmv_entries = 0;
5786 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
5788 goto unlock_and_return;
5794 nmap = (nexleft > subnex) ? subnex : nexleft;
5795 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5796 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5797 bmapi_flags, NULL, 0, map, &nmap, NULL);
5799 goto unlock_and_return;
5800 ASSERT(nmap <= subnex);
5802 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5804 oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
5805 BMV_OF_PREALLOC : 0;
5806 out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
5807 out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5808 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
5809 if (map[i].br_startblock == HOLESTARTBLOCK &&
5810 ((prealloced && out.bmv_offset + out.bmv_length == bmvend) ||
5811 whichfork == XFS_ATTR_FORK )) {
5813 * came to hole at end of file or the end of
5816 goto unlock_and_return;
5819 (map[i].br_startblock == HOLESTARTBLOCK) ?
5821 XFS_FSB_TO_DB(ip, map[i].br_startblock);
5823 /* return either getbmap/getbmapx structure. */
5824 if (interface & BMV_IF_EXTENDED) {
5825 struct getbmapx outx;
5827 GETBMAP_CONVERT(out,outx);
5828 outx.bmv_oflags = oflags;
5829 outx.bmv_unused1 = outx.bmv_unused2 = 0;
5830 if (copy_to_user(ap, &outx,
5832 error = XFS_ERROR(EFAULT);
5833 goto unlock_and_return;
5836 if (copy_to_user(ap, &out,
5838 error = XFS_ERROR(EFAULT);
5839 goto unlock_and_return;
5843 out.bmv_offset + out.bmv_length;
5844 bmv->bmv_length = MAX((__int64_t)0,
5845 (__int64_t)(bmvend - bmv->bmv_offset));
5847 ap = (interface & BMV_IF_EXTENDED) ?
5849 ((struct getbmapx __user *)ap + 1) :
5851 ((struct getbmap __user *)ap + 1);
5854 } while (nmap && nexleft && bmv->bmv_length);
5857 xfs_iunlock_map_shared(ip, lock);
5858 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5860 kmem_free(map, subnex * sizeof(*map));
5866 * Check the last inode extent to determine whether this allocation will result
5867 * in blocks being allocated at the end of the file. When we allocate new data
5868 * blocks at the end of the file which do not start at the previous data block,
5869 * we will try to align the new blocks at stripe unit boundaries.
5871 STATIC int /* error */
5873 xfs_inode_t *ip, /* incore inode pointer */
5874 xfs_fileoff_t off, /* file offset in fsblocks */
5875 int whichfork, /* data or attribute fork */
5876 char *aeof) /* return value */
5878 int error; /* error return value */
5879 xfs_ifork_t *ifp; /* inode fork pointer */
5880 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
5881 xfs_extnum_t nextents; /* number of file extents */
5882 xfs_bmbt_irec_t s; /* expanded extent record */
5884 ASSERT(whichfork == XFS_DATA_FORK);
5885 ifp = XFS_IFORK_PTR(ip, whichfork);
5886 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5887 (error = xfs_iread_extents(NULL, ip, whichfork)))
5889 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5890 if (nextents == 0) {
5895 * Go to the last extent
5897 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5898 xfs_bmbt_get_all(lastrec, &s);
5900 * Check we are allocating in the last extent (for delayed allocations)
5901 * or past the last extent for non-delayed allocations.
5903 *aeof = (off >= s.br_startoff &&
5904 off < s.br_startoff + s.br_blockcount &&
5905 ISNULLSTARTBLOCK(s.br_startblock)) ||
5906 off >= s.br_startoff + s.br_blockcount;
5911 * Check if the endoff is outside the last extent. If so the caller will grow
5912 * the allocation to a stripe unit boundary.
5916 xfs_inode_t *ip, /* incore inode pointer */
5917 xfs_fileoff_t endoff, /* file offset in fsblocks */
5918 int whichfork, /* data or attribute fork */
5919 int *eof) /* result value */
5921 xfs_fsblock_t blockcount; /* extent block count */
5922 int error; /* error return value */
5923 xfs_ifork_t *ifp; /* inode fork pointer */
5924 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
5925 xfs_extnum_t nextents; /* number of file extents */
5926 xfs_fileoff_t startoff; /* extent starting file offset */
5928 ASSERT(whichfork == XFS_DATA_FORK);
5929 ifp = XFS_IFORK_PTR(ip, whichfork);
5930 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5931 (error = xfs_iread_extents(NULL, ip, whichfork)))
5933 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5934 if (nextents == 0) {
5939 * Go to the last extent
5941 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5942 startoff = xfs_bmbt_get_startoff(lastrec);
5943 blockcount = xfs_bmbt_get_blockcount(lastrec);
5944 *eof = endoff >= startoff + blockcount;
5950 * Check that the extents list for the inode ip is in the right order.
5953 xfs_bmap_check_extents(
5954 xfs_inode_t *ip, /* incore inode pointer */
5955 int whichfork) /* data or attr fork */
5957 xfs_bmbt_rec_t *ep; /* current extent entry */
5958 xfs_extnum_t idx; /* extent record index */
5959 xfs_ifork_t *ifp; /* inode fork pointer */
5960 xfs_extnum_t nextents; /* number of extents in list */
5961 xfs_bmbt_rec_t *nextp; /* next extent entry */
5963 ifp = XFS_IFORK_PTR(ip, whichfork);
5964 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
5965 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5966 ep = xfs_iext_get_ext(ifp, 0);
5967 for (idx = 0; idx < nextents - 1; idx++) {
5968 nextp = xfs_iext_get_ext(ifp, idx + 1);
5969 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
5978 xfs_btree_cur_t *cur,
5988 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5989 bp = cur->bc_bufs[i];
5991 if (XFS_BUF_ADDR(bp) == bno)
5992 break; /* Found it */
5994 if (i == XFS_BTREE_MAXLEVELS)
5997 if (!bp) { /* Chase down all the log items to see if the bp is there */
5998 xfs_log_item_chunk_t *licp;
6002 licp = &tp->t_items;
6003 while (!bp && licp != NULL) {
6004 if (XFS_LIC_ARE_ALL_FREE(licp)) {
6005 licp = licp->lic_next;
6008 for (i = 0; i < licp->lic_unused; i++) {
6009 xfs_log_item_desc_t *lidp;
6010 xfs_log_item_t *lip;
6011 xfs_buf_log_item_t *bip;
6014 if (XFS_LIC_ISFREE(licp, i)) {
6018 lidp = XFS_LIC_SLOT(licp, i);
6019 lip = lidp->lid_item;
6020 if (lip->li_type != XFS_LI_BUF)
6023 bip = (xfs_buf_log_item_t *)lip;
6026 if (XFS_BUF_ADDR(lbp) == bno) {
6028 break; /* Found it */
6031 licp = licp->lic_next;
6039 xfs_bmbt_block_t *block,
6045 xfs_bmbt_ptr_t *pp, *thispa; /* pointer to block address */
6046 xfs_bmbt_key_t *prevp, *keyp;
6048 ASSERT(be16_to_cpu(block->bb_level) > 0);
6051 for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
6052 dmxr = mp->m_bmap_dmxr[0];
6055 keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
6057 keyp = XFS_BTREE_KEY_ADDR(mp->m_sb.sb_blocksize,
6058 xfs_bmbt, block, i, dmxr);
6062 xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
6067 * Compare the block numbers to see if there are dups.
6071 pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
6073 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6074 xfs_bmbt, block, i, dmxr);
6076 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
6078 thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
6080 thispa = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6081 xfs_bmbt, block, j, dmxr);
6083 if (INT_GET(*thispa, ARCH_CONVERT) ==
6084 INT_GET(*pp, ARCH_CONVERT)) {
6085 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
6087 INT_GET(*thispa, ARCH_CONVERT));
6088 panic("%s: ptrs are equal in node\n",
6096 * Check that the extents for the inode ip are in the right order in all
6101 xfs_bmap_check_leaf_extents(
6102 xfs_btree_cur_t *cur, /* btree cursor or null */
6103 xfs_inode_t *ip, /* incore inode pointer */
6104 int whichfork) /* data or attr fork */
6106 xfs_bmbt_block_t *block; /* current btree block */
6107 xfs_fsblock_t bno; /* block # of "block" */
6108 xfs_buf_t *bp; /* buffer for "block" */
6109 int error; /* error return value */
6110 xfs_extnum_t i=0, j; /* index into the extents list */
6111 xfs_ifork_t *ifp; /* fork structure */
6112 int level; /* btree level, for checking */
6113 xfs_mount_t *mp; /* file system mount structure */
6114 xfs_bmbt_ptr_t *pp; /* pointer to block address */
6115 xfs_bmbt_rec_t *ep; /* pointer to current extent */
6116 xfs_bmbt_rec_t *lastp; /* pointer to previous extent */
6117 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
6120 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6126 ifp = XFS_IFORK_PTR(ip, whichfork);
6127 block = ifp->if_broot;
6129 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6131 level = be16_to_cpu(block->bb_level);
6133 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6134 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6135 ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
6136 ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
6137 ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
6138 bno = INT_GET(*pp, ARCH_CONVERT);
6140 * Go down the tree until leaf level is reached, following the first
6141 * pointer (leftmost) at each level.
6143 while (level-- > 0) {
6144 /* See if buf is in cur first */
6145 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6151 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6152 XFS_BMAP_BTREE_REF)))
6154 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6155 XFS_WANT_CORRUPTED_GOTO(
6156 XFS_BMAP_SANITY_CHECK(mp, block, level),
6162 * Check this block for basic sanity (increasing keys and
6163 * no duplicate blocks).
6166 xfs_check_block(block, mp, 0, 0);
6167 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
6168 1, mp->m_bmap_dmxr[1]);
6169 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, INT_GET(*pp, ARCH_CONVERT)), error0);
6170 bno = INT_GET(*pp, ARCH_CONVERT);
6173 xfs_trans_brelse(NULL, bp);
6178 * Here with bp and block set to the leftmost leaf node in the tree.
6183 * Loop over all leaf nodes checking that all extents are in the right order.
6187 xfs_fsblock_t nextbno;
6188 xfs_extnum_t num_recs;
6191 num_recs = be16_to_cpu(block->bb_numrecs);
6194 * Read-ahead the next leaf block, if any.
6197 nextbno = be64_to_cpu(block->bb_rightsib);
6200 * Check all the extents to make sure they are OK.
6201 * If we had a previous block, the last entry should
6202 * conform with the first entry in this one.
6205 ep = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
6206 block, 1, mp->m_bmap_dmxr[0]);
6207 for (j = 1; j < num_recs; j++) {
6208 nextp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
6209 block, j + 1, mp->m_bmap_dmxr[0]);
6211 xfs_btree_check_rec(XFS_BTNUM_BMAP,
6212 (void *)lastp, (void *)ep);
6214 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
6223 xfs_trans_brelse(NULL, bp);
6227 * If we've reached the end, stop.
6229 if (bno == NULLFSBLOCK)
6232 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6238 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6239 XFS_BMAP_BTREE_REF)))
6241 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6245 xfs_trans_brelse(NULL, bp);
6250 cmn_err(CE_WARN, "%s: at error0", __FUNCTION__);
6252 xfs_trans_brelse(NULL, bp);
6254 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
6256 panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__);
6262 * Count fsblocks of the given fork.
6265 xfs_bmap_count_blocks(
6266 xfs_trans_t *tp, /* transaction pointer */
6267 xfs_inode_t *ip, /* incore inode */
6268 int whichfork, /* data or attr fork */
6269 int *count) /* out: count of blocks */
6271 xfs_bmbt_block_t *block; /* current btree block */
6272 xfs_fsblock_t bno; /* block # of "block" */
6273 xfs_ifork_t *ifp; /* fork structure */
6274 int level; /* btree level, for checking */
6275 xfs_mount_t *mp; /* file system mount structure */
6276 xfs_bmbt_ptr_t *pp; /* pointer to block address */
6280 ifp = XFS_IFORK_PTR(ip, whichfork);
6281 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6282 if (unlikely(xfs_bmap_count_leaves(ifp, 0,
6283 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6285 XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)",
6286 XFS_ERRLEVEL_LOW, mp);
6287 return XFS_ERROR(EFSCORRUPTED);
6293 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6295 block = ifp->if_broot;
6296 level = be16_to_cpu(block->bb_level);
6298 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6299 ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
6300 ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
6301 ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
6302 bno = INT_GET(*pp, ARCH_CONVERT);
6304 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
6305 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6307 return XFS_ERROR(EFSCORRUPTED);
6314 * Recursively walks each level of a btree
6315 * to count total fsblocks is use.
6318 xfs_bmap_count_tree(
6319 xfs_mount_t *mp, /* file system mount point */
6320 xfs_trans_t *tp, /* transaction pointer */
6321 xfs_ifork_t *ifp, /* inode fork pointer */
6322 xfs_fsblock_t blockno, /* file system block number */
6323 int levelin, /* level in btree */
6324 int *count) /* Count of blocks */
6327 xfs_buf_t *bp, *nbp;
6328 int level = levelin;
6330 xfs_fsblock_t bno = blockno;
6331 xfs_fsblock_t nextbno;
6332 xfs_bmbt_block_t *block, *nextblock;
6335 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6338 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6341 /* Not at node above leafs, count this level of nodes */
6342 nextbno = be64_to_cpu(block->bb_rightsib);
6343 while (nextbno != NULLFSBLOCK) {
6344 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6345 0, &nbp, XFS_BMAP_BTREE_REF)))
6348 nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
6349 nextbno = be64_to_cpu(nextblock->bb_rightsib);
6350 xfs_trans_brelse(tp, nbp);
6353 /* Dive to the next level */
6354 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6355 xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
6356 bno = INT_GET(*pp, ARCH_CONVERT);
6357 if (unlikely((error =
6358 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6359 xfs_trans_brelse(tp, bp);
6360 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6361 XFS_ERRLEVEL_LOW, mp);
6362 return XFS_ERROR(EFSCORRUPTED);
6364 xfs_trans_brelse(tp, bp);
6366 /* count all level 1 nodes and their leaves */
6368 nextbno = be64_to_cpu(block->bb_rightsib);
6369 numrecs = be16_to_cpu(block->bb_numrecs);
6370 if (unlikely(xfs_bmap_disk_count_leaves(ifp, mp,
6371 0, block, numrecs, count) < 0)) {
6372 xfs_trans_brelse(tp, bp);
6373 XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
6374 XFS_ERRLEVEL_LOW, mp);
6375 return XFS_ERROR(EFSCORRUPTED);
6377 xfs_trans_brelse(tp, bp);
6378 if (nextbno == NULLFSBLOCK)
6381 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6382 XFS_BMAP_BTREE_REF)))
6385 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6392 * Count leaf blocks given a range of extent records.
6395 xfs_bmap_count_leaves(
6402 xfs_bmbt_rec_t *frp;
6404 for (b = 0; b < numrecs; b++) {
6405 frp = xfs_iext_get_ext(ifp, idx + b);
6406 *count += xfs_bmbt_get_blockcount(frp);
6412 * Count leaf blocks given a range of extent records originally
6416 xfs_bmap_disk_count_leaves(
6420 xfs_bmbt_block_t *block,
6425 xfs_bmbt_rec_t *frp;
6427 for (b = 1; b <= numrecs; b++) {
6428 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize,
6429 xfs_bmbt, block, idx + b, mp->m_bmap_dmxr[0]);
6430 *count += xfs_bmbt_disk_get_blockcount(frp);