2 * Copyright (c) 2000-2006 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 xfs_extdelta_t *delta, /* Change made to incore extents */
105 int whichfork, /* data or attr fork */
106 int rsvd); /* OK to allocate reserved blocks */
109 * Called by xfs_bmap_add_extent to handle cases converting a delayed
110 * allocation to a real allocation.
112 STATIC int /* error */
113 xfs_bmap_add_extent_delay_real(
114 xfs_inode_t *ip, /* incore inode pointer */
115 xfs_extnum_t idx, /* extent number to update/insert */
116 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
117 xfs_bmbt_irec_t *new, /* new data to add to file extents */
118 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
119 xfs_fsblock_t *first, /* pointer to firstblock variable */
120 xfs_bmap_free_t *flist, /* list of extents to be freed */
121 int *logflagsp, /* inode logging flags */
122 xfs_extdelta_t *delta, /* Change made to incore extents */
123 int rsvd); /* OK to allocate reserved blocks */
126 * Called by xfs_bmap_add_extent to handle cases converting a hole
127 * to a delayed allocation.
129 STATIC int /* error */
130 xfs_bmap_add_extent_hole_delay(
131 xfs_inode_t *ip, /* incore inode pointer */
132 xfs_extnum_t idx, /* extent number to update/insert */
133 xfs_btree_cur_t *cur, /* if null, not a btree */
134 xfs_bmbt_irec_t *new, /* new data to add to file extents */
135 int *logflagsp,/* inode logging flags */
136 xfs_extdelta_t *delta, /* Change made to incore extents */
137 int rsvd); /* OK to allocate reserved blocks */
140 * Called by xfs_bmap_add_extent to handle cases converting a hole
141 * to a real allocation.
143 STATIC int /* error */
144 xfs_bmap_add_extent_hole_real(
145 xfs_inode_t *ip, /* incore inode pointer */
146 xfs_extnum_t idx, /* extent number to update/insert */
147 xfs_btree_cur_t *cur, /* if null, not a btree */
148 xfs_bmbt_irec_t *new, /* new data to add to file extents */
149 int *logflagsp, /* inode logging flags */
150 xfs_extdelta_t *delta, /* Change made to incore extents */
151 int whichfork); /* data or attr fork */
154 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
155 * allocation to a real allocation or vice versa.
157 STATIC int /* error */
158 xfs_bmap_add_extent_unwritten_real(
159 xfs_inode_t *ip, /* incore inode pointer */
160 xfs_extnum_t idx, /* extent number to update/insert */
161 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
162 xfs_bmbt_irec_t *new, /* new data to add to file extents */
163 int *logflagsp, /* inode logging flags */
164 xfs_extdelta_t *delta); /* Change made to incore extents */
167 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
168 * It figures out where to ask the underlying allocator to put the new extent.
170 STATIC int /* error */
172 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
175 * Transform a btree format file with only one leaf node, where the
176 * extents list will fit in the inode, into an extents format file.
177 * Since the file extents are already in-core, all we have to do is
178 * give up the space for the btree root and pitch the leaf block.
180 STATIC int /* error */
181 xfs_bmap_btree_to_extents(
182 xfs_trans_t *tp, /* transaction pointer */
183 xfs_inode_t *ip, /* incore inode pointer */
184 xfs_btree_cur_t *cur, /* btree cursor */
185 int *logflagsp, /* inode logging flags */
186 int whichfork); /* data or attr fork */
191 * Check that the extents list for the inode ip is in the right order.
194 xfs_bmap_check_extents(
195 xfs_inode_t *ip, /* incore inode pointer */
196 int whichfork); /* data or attr fork */
201 * Called by xfs_bmapi to update file extent records and the btree
202 * after removing space (or undoing a delayed allocation).
204 STATIC int /* error */
206 xfs_inode_t *ip, /* incore inode pointer */
207 xfs_trans_t *tp, /* current trans pointer */
208 xfs_extnum_t idx, /* extent number to update/insert */
209 xfs_bmap_free_t *flist, /* list of extents to be freed */
210 xfs_btree_cur_t *cur, /* if null, not a btree */
211 xfs_bmbt_irec_t *new, /* new data to add to file extents */
212 int *logflagsp,/* inode logging flags */
213 xfs_extdelta_t *delta, /* Change made to incore extents */
214 int whichfork, /* data or attr fork */
215 int rsvd); /* OK to allocate reserved blocks */
218 * Remove the entry "free" from the free item list. Prev points to the
219 * previous entry, unless "free" is the head of the list.
223 xfs_bmap_free_t *flist, /* free item list header */
224 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
225 xfs_bmap_free_item_t *free); /* list item to be freed */
228 * Convert an extents-format file into a btree-format file.
229 * The new file will have a root block (in the inode) and a single child block.
231 STATIC int /* error */
232 xfs_bmap_extents_to_btree(
233 xfs_trans_t *tp, /* transaction pointer */
234 xfs_inode_t *ip, /* incore inode pointer */
235 xfs_fsblock_t *firstblock, /* first-block-allocated */
236 xfs_bmap_free_t *flist, /* blocks freed in xaction */
237 xfs_btree_cur_t **curp, /* cursor returned to caller */
238 int wasdel, /* converting a delayed alloc */
239 int *logflagsp, /* inode logging flags */
240 int whichfork); /* data or attr fork */
243 * Convert a local file to an extents file.
244 * This code is sort of bogus, since the file data needs to get
245 * logged so it won't be lost. The bmap-level manipulations are ok, though.
247 STATIC int /* error */
248 xfs_bmap_local_to_extents(
249 xfs_trans_t *tp, /* transaction pointer */
250 xfs_inode_t *ip, /* incore inode pointer */
251 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
252 xfs_extlen_t total, /* total blocks needed by transaction */
253 int *logflagsp, /* inode logging flags */
254 int whichfork); /* data or attr fork */
257 * Search the extents list for the inode, for the extent containing bno.
258 * If bno lies in a hole, point to the next entry. If bno lies past eof,
259 * *eofp will be set, and *prevp will contain the last entry (null if none).
260 * Else, *lastxp will be set to the index of the found
261 * entry; *gotp will contain the entry.
263 STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
264 xfs_bmap_search_extents(
265 xfs_inode_t *ip, /* incore inode pointer */
266 xfs_fileoff_t bno, /* block number searched for */
267 int whichfork, /* data or attr fork */
268 int *eofp, /* out: end of file found */
269 xfs_extnum_t *lastxp, /* out: last extent index */
270 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
271 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
274 * Check the last inode extent to determine whether this allocation will result
275 * in blocks being allocated at the end of the file. When we allocate new data
276 * blocks at the end of the file which do not start at the previous data block,
277 * we will try to align the new blocks at stripe unit boundaries.
279 STATIC int /* error */
281 xfs_inode_t *ip, /* incore inode pointer */
282 xfs_fileoff_t off, /* file offset in fsblocks */
283 int whichfork, /* data or attribute fork */
284 char *aeof); /* return value */
286 #ifdef XFS_BMAP_TRACE
288 * Add a bmap trace buffer entry. Base routine for the others.
291 xfs_bmap_trace_addentry(
292 int opcode, /* operation */
293 char *fname, /* function name */
294 char *desc, /* operation description */
295 xfs_inode_t *ip, /* incore inode pointer */
296 xfs_extnum_t idx, /* index of entry(ies) */
297 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
298 xfs_bmbt_rec_t *r1, /* first record */
299 xfs_bmbt_rec_t *r2, /* second record or null */
300 int whichfork); /* data or attr fork */
303 * Add bmap trace entry prior to a call to xfs_iext_remove.
306 xfs_bmap_trace_delete(
307 char *fname, /* function name */
308 char *desc, /* operation description */
309 xfs_inode_t *ip, /* incore inode pointer */
310 xfs_extnum_t idx, /* index of entry(entries) deleted */
311 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
312 int whichfork); /* data or attr fork */
315 * Add bmap trace entry prior to a call to xfs_iext_insert, or
316 * reading in the extents list from the disk (in the btree).
319 xfs_bmap_trace_insert(
320 char *fname, /* function name */
321 char *desc, /* operation description */
322 xfs_inode_t *ip, /* incore inode pointer */
323 xfs_extnum_t idx, /* index of entry(entries) inserted */
324 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
325 xfs_bmbt_irec_t *r1, /* inserted record 1 */
326 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
327 int whichfork); /* data or attr fork */
330 * Add bmap trace entry after updating an extent record in place.
333 xfs_bmap_trace_post_update(
334 char *fname, /* function name */
335 char *desc, /* operation description */
336 xfs_inode_t *ip, /* incore inode pointer */
337 xfs_extnum_t idx, /* index of entry updated */
338 int whichfork); /* data or attr fork */
341 * Add bmap trace entry prior to updating an extent record in place.
344 xfs_bmap_trace_pre_update(
345 char *fname, /* function name */
346 char *desc, /* operation description */
347 xfs_inode_t *ip, /* incore inode pointer */
348 xfs_extnum_t idx, /* index of entry to be updated */
349 int whichfork); /* data or attr fork */
352 #define xfs_bmap_trace_delete(f,d,ip,i,c,w)
353 #define xfs_bmap_trace_insert(f,d,ip,i,c,r1,r2,w)
354 #define xfs_bmap_trace_post_update(f,d,ip,i,w)
355 #define xfs_bmap_trace_pre_update(f,d,ip,i,w)
356 #endif /* XFS_BMAP_TRACE */
359 * Compute the worst-case number of indirect blocks that will be used
360 * for ip's delayed extent of length "len".
363 xfs_bmap_worst_indlen(
364 xfs_inode_t *ip, /* incore inode pointer */
365 xfs_filblks_t len); /* delayed extent length */
369 * Perform various validation checks on the values being returned
373 xfs_bmap_validate_ret(
377 xfs_bmbt_irec_t *mval,
381 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
384 #if defined(XFS_RW_TRACE)
393 #define xfs_bunmap_trace(ip, bno, len, flags, ra)
394 #endif /* XFS_RW_TRACE */
401 xfs_fsblock_t blockno,
406 xfs_bmap_count_leaves(
413 xfs_bmap_disk_count_leaves(
417 xfs_bmbt_block_t *block,
422 * Bmap internal routines.
426 * Called from xfs_bmap_add_attrfork to handle btree format files.
428 STATIC int /* error */
429 xfs_bmap_add_attrfork_btree(
430 xfs_trans_t *tp, /* transaction pointer */
431 xfs_inode_t *ip, /* incore inode pointer */
432 xfs_fsblock_t *firstblock, /* first block allocated */
433 xfs_bmap_free_t *flist, /* blocks to free at commit */
434 int *flags) /* inode logging flags */
436 xfs_btree_cur_t *cur; /* btree cursor */
437 int error; /* error return value */
438 xfs_mount_t *mp; /* file system mount struct */
439 int stat; /* newroot status */
442 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
443 *flags |= XFS_ILOG_DBROOT;
445 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
447 cur->bc_private.b.flist = flist;
448 cur->bc_private.b.firstblock = *firstblock;
449 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
451 ASSERT(stat == 1); /* must be at least one entry */
452 if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
455 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
456 return XFS_ERROR(ENOSPC);
458 *firstblock = cur->bc_private.b.firstblock;
459 cur->bc_private.b.allocated = 0;
460 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
464 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
469 * Called from xfs_bmap_add_attrfork to handle extents format files.
471 STATIC int /* error */
472 xfs_bmap_add_attrfork_extents(
473 xfs_trans_t *tp, /* transaction pointer */
474 xfs_inode_t *ip, /* incore inode pointer */
475 xfs_fsblock_t *firstblock, /* first block allocated */
476 xfs_bmap_free_t *flist, /* blocks to free at commit */
477 int *flags) /* inode logging flags */
479 xfs_btree_cur_t *cur; /* bmap btree cursor */
480 int error; /* error return value */
482 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
485 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
486 flags, XFS_DATA_FORK);
488 cur->bc_private.b.allocated = 0;
489 xfs_btree_del_cursor(cur,
490 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
496 * Called from xfs_bmap_add_attrfork to handle local format files.
498 STATIC int /* error */
499 xfs_bmap_add_attrfork_local(
500 xfs_trans_t *tp, /* transaction pointer */
501 xfs_inode_t *ip, /* incore inode pointer */
502 xfs_fsblock_t *firstblock, /* first block allocated */
503 xfs_bmap_free_t *flist, /* blocks to free at commit */
504 int *flags) /* inode logging flags */
506 xfs_da_args_t dargs; /* args for dir/attr code */
507 int error; /* error return value */
508 xfs_mount_t *mp; /* mount structure pointer */
510 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
512 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
514 memset(&dargs, 0, sizeof(dargs));
516 dargs.firstblock = firstblock;
518 dargs.total = mp->m_dirblkfsbs;
519 dargs.whichfork = XFS_DATA_FORK;
521 error = XFS_DIR_SHORTFORM_TO_SINGLE(mp, &dargs);
523 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
529 * Called by xfs_bmapi to update file extent records and the btree
530 * after allocating space (or doing a delayed allocation).
532 STATIC int /* error */
534 xfs_inode_t *ip, /* incore inode pointer */
535 xfs_extnum_t idx, /* extent number to update/insert */
536 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
537 xfs_bmbt_irec_t *new, /* new data to add to file extents */
538 xfs_fsblock_t *first, /* pointer to firstblock variable */
539 xfs_bmap_free_t *flist, /* list of extents to be freed */
540 int *logflagsp, /* inode logging flags */
541 xfs_extdelta_t *delta, /* Change made to incore extents */
542 int whichfork, /* data or attr fork */
543 int rsvd) /* OK to use reserved data blocks */
545 xfs_btree_cur_t *cur; /* btree cursor or null */
546 xfs_filblks_t da_new; /* new count del alloc blocks used */
547 xfs_filblks_t da_old; /* old count del alloc blocks used */
548 int error; /* error return value */
549 #ifdef XFS_BMAP_TRACE
550 static char fname[] = "xfs_bmap_add_extent";
552 xfs_ifork_t *ifp; /* inode fork ptr */
553 int logflags; /* returned value */
554 xfs_extnum_t nextents; /* number of extents in file now */
556 XFS_STATS_INC(xs_add_exlist);
558 ifp = XFS_IFORK_PTR(ip, whichfork);
559 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
560 ASSERT(idx <= nextents);
564 * This is the first extent added to a new/empty file.
565 * Special case this one, so other routines get to assume there are
566 * already extents in the list.
569 xfs_bmap_trace_insert(fname, "insert empty", ip, 0, 1, new,
571 xfs_iext_insert(ifp, 0, 1, new);
574 if (!ISNULLSTARTBLOCK(new->br_startblock)) {
575 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
576 logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
579 /* DELTA: single new extent */
581 if (delta->xed_startoff > new->br_startoff)
582 delta->xed_startoff = new->br_startoff;
583 if (delta->xed_blockcount <
584 new->br_startoff + new->br_blockcount)
585 delta->xed_blockcount = new->br_startoff +
590 * Any kind of new delayed allocation goes here.
592 else if (ISNULLSTARTBLOCK(new->br_startblock)) {
594 ASSERT((cur->bc_private.b.flags &
595 XFS_BTCUR_BPRV_WASDEL) == 0);
596 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, cur, new,
597 &logflags, delta, rsvd)))
601 * Real allocation off the end of the file.
603 else if (idx == nextents) {
605 ASSERT((cur->bc_private.b.flags &
606 XFS_BTCUR_BPRV_WASDEL) == 0);
607 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
608 &logflags, delta, whichfork)))
611 xfs_bmbt_irec_t prev; /* old extent at offset idx */
614 * Get the record referred to by idx.
616 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
618 * If it's a real allocation record, and the new allocation ends
619 * after the start of the referred to record, then we're filling
620 * in a delayed or unwritten allocation with a real one, or
621 * converting real back to unwritten.
623 if (!ISNULLSTARTBLOCK(new->br_startblock) &&
624 new->br_startoff + new->br_blockcount > prev.br_startoff) {
625 if (prev.br_state != XFS_EXT_UNWRITTEN &&
626 ISNULLSTARTBLOCK(prev.br_startblock)) {
627 da_old = STARTBLOCKVAL(prev.br_startblock);
629 ASSERT(cur->bc_private.b.flags &
630 XFS_BTCUR_BPRV_WASDEL);
631 if ((error = xfs_bmap_add_extent_delay_real(ip,
632 idx, &cur, new, &da_new, first, flist,
633 &logflags, delta, rsvd)))
635 } else if (new->br_state == XFS_EXT_NORM) {
636 ASSERT(new->br_state == XFS_EXT_NORM);
637 if ((error = xfs_bmap_add_extent_unwritten_real(
638 ip, idx, &cur, new, &logflags, delta)))
641 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
642 if ((error = xfs_bmap_add_extent_unwritten_real(
643 ip, idx, &cur, new, &logflags, delta)))
646 ASSERT(*curp == cur || *curp == NULL);
649 * Otherwise we're filling in a hole with an allocation.
653 ASSERT((cur->bc_private.b.flags &
654 XFS_BTCUR_BPRV_WASDEL) == 0);
655 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
656 new, &logflags, delta, whichfork)))
661 ASSERT(*curp == cur || *curp == NULL);
663 * Convert to a btree if necessary.
665 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
666 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
667 int tmp_logflags; /* partial log flag return val */
670 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
671 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
672 logflags |= tmp_logflags;
677 * Adjust for changes in reserved delayed indirect blocks.
678 * Nothing to do for disk quotas here.
680 if (da_old || da_new) {
685 nblks += cur->bc_private.b.allocated;
686 ASSERT(nblks <= da_old);
688 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
689 (int)(da_old - nblks), rsvd);
692 * Clear out the allocated field, done with it now in any case.
695 cur->bc_private.b.allocated = 0;
701 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
703 *logflagsp = logflags;
708 * Called by xfs_bmap_add_extent to handle cases converting a delayed
709 * allocation to a real allocation.
711 STATIC int /* error */
712 xfs_bmap_add_extent_delay_real(
713 xfs_inode_t *ip, /* incore inode pointer */
714 xfs_extnum_t idx, /* extent number to update/insert */
715 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
716 xfs_bmbt_irec_t *new, /* new data to add to file extents */
717 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
718 xfs_fsblock_t *first, /* pointer to firstblock variable */
719 xfs_bmap_free_t *flist, /* list of extents to be freed */
720 int *logflagsp, /* inode logging flags */
721 xfs_extdelta_t *delta, /* Change made to incore extents */
722 int rsvd) /* OK to use reserved data block allocation */
724 xfs_btree_cur_t *cur; /* btree cursor */
725 int diff; /* temp value */
726 xfs_bmbt_rec_t *ep; /* extent entry for idx */
727 int error; /* error return value */
728 #ifdef XFS_BMAP_TRACE
729 static char fname[] = "xfs_bmap_add_extent_delay_real";
731 int i; /* temp state */
732 xfs_ifork_t *ifp; /* inode fork pointer */
733 xfs_fileoff_t new_endoff; /* end offset of new entry */
734 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
735 /* left is 0, right is 1, prev is 2 */
736 int rval=0; /* return value (logging flags) */
737 int state = 0;/* state bits, accessed thru macros */
738 xfs_filblks_t temp=0; /* value for dnew calculations */
739 xfs_filblks_t temp2=0;/* value for dnew calculations */
740 int tmp_rval; /* partial logging flags */
741 enum { /* bit number definitions for state */
742 LEFT_CONTIG, RIGHT_CONTIG,
743 LEFT_FILLING, RIGHT_FILLING,
744 LEFT_DELAY, RIGHT_DELAY,
745 LEFT_VALID, RIGHT_VALID
751 #define MASK(b) (1 << (b))
752 #define MASK2(a,b) (MASK(a) | MASK(b))
753 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
754 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
755 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
756 #define STATE_TEST(b) (state & MASK(b))
757 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
758 ((state &= ~MASK(b)), 0))
759 #define SWITCH_STATE \
760 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
763 * Set up a bunch of variables to make the tests simpler.
766 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
767 ep = xfs_iext_get_ext(ifp, idx);
768 xfs_bmbt_get_all(ep, &PREV);
769 new_endoff = new->br_startoff + new->br_blockcount;
770 ASSERT(PREV.br_startoff <= new->br_startoff);
771 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
773 * Set flags determining what part of the previous delayed allocation
774 * extent is being replaced by a real allocation.
776 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
777 STATE_SET(RIGHT_FILLING,
778 PREV.br_startoff + PREV.br_blockcount == new_endoff);
780 * Check and set flags if this segment has a left neighbor.
781 * Don't set contiguous if the combined extent would be too large.
783 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
784 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
785 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
787 STATE_SET(LEFT_CONTIG,
788 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
789 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
790 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
791 LEFT.br_state == new->br_state &&
792 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
794 * Check and set flags if this segment has a right neighbor.
795 * Don't set contiguous if the combined extent would be too large.
796 * Also check for all-three-contiguous being too large.
798 if (STATE_SET_TEST(RIGHT_VALID,
800 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
801 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
802 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
804 STATE_SET(RIGHT_CONTIG,
805 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
806 new_endoff == RIGHT.br_startoff &&
807 new->br_startblock + new->br_blockcount ==
808 RIGHT.br_startblock &&
809 new->br_state == RIGHT.br_state &&
810 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
811 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
812 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
813 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
817 * Switch out based on the FILLING and CONTIG state bits.
819 switch (SWITCH_STATE) {
821 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
823 * Filling in all of a previously delayed allocation extent.
824 * The left and right neighbors are both contiguous with new.
826 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
828 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
829 LEFT.br_blockcount + PREV.br_blockcount +
830 RIGHT.br_blockcount);
831 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
833 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
835 xfs_iext_remove(ifp, idx, 2);
836 ip->i_df.if_lastex = idx - 1;
837 ip->i_d.di_nextents--;
839 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
841 rval = XFS_ILOG_CORE;
842 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
844 RIGHT.br_blockcount, &i)))
847 if ((error = xfs_bmbt_delete(cur, &i)))
850 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
853 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
857 RIGHT.br_blockcount, LEFT.br_state)))
861 /* DELTA: Three in-core extents are replaced by one. */
862 temp = LEFT.br_startoff;
863 temp2 = LEFT.br_blockcount +
868 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
870 * Filling in all of a previously delayed allocation extent.
871 * The left neighbor is contiguous, the right is not.
873 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
875 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
876 LEFT.br_blockcount + PREV.br_blockcount);
877 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
879 ip->i_df.if_lastex = idx - 1;
880 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
882 xfs_iext_remove(ifp, idx, 1);
884 rval = XFS_ILOG_DEXT;
887 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
888 LEFT.br_startblock, LEFT.br_blockcount,
892 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
895 PREV.br_blockcount, LEFT.br_state)))
899 /* DELTA: Two in-core extents are replaced by one. */
900 temp = LEFT.br_startoff;
901 temp2 = LEFT.br_blockcount +
905 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
907 * Filling in all of a previously delayed allocation extent.
908 * The right neighbor is contiguous, the left is not.
910 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
912 xfs_bmbt_set_startblock(ep, new->br_startblock);
913 xfs_bmbt_set_blockcount(ep,
914 PREV.br_blockcount + RIGHT.br_blockcount);
915 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
917 ip->i_df.if_lastex = idx;
918 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
920 xfs_iext_remove(ifp, idx + 1, 1);
922 rval = XFS_ILOG_DEXT;
925 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
927 RIGHT.br_blockcount, &i)))
930 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
933 RIGHT.br_blockcount, PREV.br_state)))
937 /* DELTA: Two in-core extents are replaced by one. */
938 temp = PREV.br_startoff;
939 temp2 = PREV.br_blockcount +
943 case MASK2(LEFT_FILLING, RIGHT_FILLING):
945 * Filling in all of a previously delayed allocation extent.
946 * Neither the left nor right neighbors are contiguous with
949 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
951 xfs_bmbt_set_startblock(ep, new->br_startblock);
952 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
954 ip->i_df.if_lastex = idx;
955 ip->i_d.di_nextents++;
957 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
959 rval = XFS_ILOG_CORE;
960 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
961 new->br_startblock, new->br_blockcount,
965 cur->bc_rec.b.br_state = XFS_EXT_NORM;
966 if ((error = xfs_bmbt_insert(cur, &i)))
971 /* DELTA: The in-core extent described by new changed type. */
972 temp = new->br_startoff;
973 temp2 = new->br_blockcount;
976 case MASK2(LEFT_FILLING, LEFT_CONTIG):
978 * Filling in the first part of a previous delayed allocation.
979 * The left neighbor is contiguous.
981 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
983 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
984 LEFT.br_blockcount + new->br_blockcount);
985 xfs_bmbt_set_startoff(ep,
986 PREV.br_startoff + new->br_blockcount);
987 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
989 temp = PREV.br_blockcount - new->br_blockcount;
990 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
992 xfs_bmbt_set_blockcount(ep, temp);
993 ip->i_df.if_lastex = idx - 1;
995 rval = XFS_ILOG_DEXT;
998 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
999 LEFT.br_startblock, LEFT.br_blockcount,
1003 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1005 LEFT.br_blockcount +
1010 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1011 STARTBLOCKVAL(PREV.br_startblock));
1012 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1013 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
1016 /* DELTA: The boundary between two in-core extents moved. */
1017 temp = LEFT.br_startoff;
1018 temp2 = LEFT.br_blockcount +
1022 case MASK(LEFT_FILLING):
1024 * Filling in the first part of a previous delayed allocation.
1025 * The left neighbor is not contiguous.
1027 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1028 xfs_bmbt_set_startoff(ep, new_endoff);
1029 temp = PREV.br_blockcount - new->br_blockcount;
1030 xfs_bmbt_set_blockcount(ep, temp);
1031 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
1033 xfs_iext_insert(ifp, idx, 1, new);
1034 ip->i_df.if_lastex = idx;
1035 ip->i_d.di_nextents++;
1037 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1039 rval = XFS_ILOG_CORE;
1040 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1041 new->br_startblock, new->br_blockcount,
1045 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1046 if ((error = xfs_bmbt_insert(cur, &i)))
1050 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1051 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1052 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1053 first, flist, &cur, 1, &tmp_rval,
1059 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1060 STARTBLOCKVAL(PREV.br_startblock) -
1061 (cur ? cur->bc_private.b.allocated : 0));
1062 ep = xfs_iext_get_ext(ifp, idx + 1);
1063 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1064 xfs_bmap_trace_post_update(fname, "LF", ip, idx + 1,
1067 /* DELTA: One in-core extent is split in two. */
1068 temp = PREV.br_startoff;
1069 temp2 = PREV.br_blockcount;
1072 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1074 * Filling in the last part of a previous delayed allocation.
1075 * The right neighbor is contiguous with the new allocation.
1077 temp = PREV.br_blockcount - new->br_blockcount;
1078 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1080 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1082 xfs_bmbt_set_blockcount(ep, temp);
1083 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1084 new->br_startoff, new->br_startblock,
1085 new->br_blockcount + RIGHT.br_blockcount,
1087 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1089 ip->i_df.if_lastex = idx + 1;
1091 rval = XFS_ILOG_DEXT;
1094 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1095 RIGHT.br_startblock,
1096 RIGHT.br_blockcount, &i)))
1099 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1101 new->br_blockcount +
1102 RIGHT.br_blockcount,
1106 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1107 STARTBLOCKVAL(PREV.br_startblock));
1108 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1109 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1112 /* DELTA: The boundary between two in-core extents moved. */
1113 temp = PREV.br_startoff;
1114 temp2 = PREV.br_blockcount +
1115 RIGHT.br_blockcount;
1118 case MASK(RIGHT_FILLING):
1120 * Filling in the last part of a previous delayed allocation.
1121 * The right neighbor is not contiguous.
1123 temp = PREV.br_blockcount - new->br_blockcount;
1124 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1125 xfs_bmbt_set_blockcount(ep, temp);
1126 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1127 new, NULL, XFS_DATA_FORK);
1128 xfs_iext_insert(ifp, idx + 1, 1, new);
1129 ip->i_df.if_lastex = idx + 1;
1130 ip->i_d.di_nextents++;
1132 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1134 rval = XFS_ILOG_CORE;
1135 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1136 new->br_startblock, new->br_blockcount,
1140 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1141 if ((error = xfs_bmbt_insert(cur, &i)))
1145 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1146 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1147 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1148 first, flist, &cur, 1, &tmp_rval,
1154 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1155 STARTBLOCKVAL(PREV.br_startblock) -
1156 (cur ? cur->bc_private.b.allocated : 0));
1157 ep = xfs_iext_get_ext(ifp, idx);
1158 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1159 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1161 /* DELTA: One in-core extent is split in two. */
1162 temp = PREV.br_startoff;
1163 temp2 = PREV.br_blockcount;
1168 * Filling in the middle part of a previous delayed allocation.
1169 * Contiguity is impossible here.
1170 * This case is avoided almost all the time.
1172 temp = new->br_startoff - PREV.br_startoff;
1173 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1174 xfs_bmbt_set_blockcount(ep, temp);
1176 r[1].br_startoff = new_endoff;
1177 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1178 r[1].br_blockcount = temp2;
1179 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1181 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1182 ip->i_df.if_lastex = idx + 1;
1183 ip->i_d.di_nextents++;
1185 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1187 rval = XFS_ILOG_CORE;
1188 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1189 new->br_startblock, new->br_blockcount,
1193 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1194 if ((error = xfs_bmbt_insert(cur, &i)))
1198 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1199 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1200 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1201 first, flist, &cur, 1, &tmp_rval,
1207 temp = xfs_bmap_worst_indlen(ip, temp);
1208 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1209 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
1210 (cur ? cur->bc_private.b.allocated : 0));
1212 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -diff, rsvd)) {
1214 * Ick gross gag me with a spoon.
1216 ASSERT(0); /* want to see if this ever happens! */
1222 !xfs_mod_incore_sb(ip->i_mount,
1223 XFS_SBS_FDBLOCKS, -diff, rsvd))
1230 !xfs_mod_incore_sb(ip->i_mount,
1231 XFS_SBS_FDBLOCKS, -diff, rsvd))
1236 ep = xfs_iext_get_ext(ifp, idx);
1237 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1238 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1239 xfs_bmap_trace_pre_update(fname, "0", ip, idx + 2,
1241 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1242 NULLSTARTBLOCK((int)temp2));
1243 xfs_bmap_trace_post_update(fname, "0", ip, idx + 2,
1245 *dnew = temp + temp2;
1246 /* DELTA: One in-core extent is split in three. */
1247 temp = PREV.br_startoff;
1248 temp2 = PREV.br_blockcount;
1251 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1252 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1253 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1254 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1255 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1256 case MASK(LEFT_CONTIG):
1257 case MASK(RIGHT_CONTIG):
1259 * These cases are all impossible.
1266 if (delta->xed_startoff > temp)
1267 delta->xed_startoff = temp;
1268 if (delta->xed_blockcount < temp2)
1269 delta->xed_blockcount = temp2;
1283 #undef STATE_SET_TEST
1288 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1289 * allocation to a real allocation or vice versa.
1291 STATIC int /* error */
1292 xfs_bmap_add_extent_unwritten_real(
1293 xfs_inode_t *ip, /* incore inode pointer */
1294 xfs_extnum_t idx, /* extent number to update/insert */
1295 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1296 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1297 int *logflagsp, /* inode logging flags */
1298 xfs_extdelta_t *delta) /* Change made to incore extents */
1300 xfs_btree_cur_t *cur; /* btree cursor */
1301 xfs_bmbt_rec_t *ep; /* extent entry for idx */
1302 int error; /* error return value */
1303 #ifdef XFS_BMAP_TRACE
1304 static char fname[] = "xfs_bmap_add_extent_unwritten_real";
1306 int i; /* temp state */
1307 xfs_ifork_t *ifp; /* inode fork pointer */
1308 xfs_fileoff_t new_endoff; /* end offset of new entry */
1309 xfs_exntst_t newext; /* new extent state */
1310 xfs_exntst_t oldext; /* old extent state */
1311 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1312 /* left is 0, right is 1, prev is 2 */
1313 int rval=0; /* return value (logging flags) */
1314 int state = 0;/* state bits, accessed thru macros */
1315 xfs_filblks_t temp=0;
1316 xfs_filblks_t temp2=0;
1317 enum { /* bit number definitions for state */
1318 LEFT_CONTIG, RIGHT_CONTIG,
1319 LEFT_FILLING, RIGHT_FILLING,
1320 LEFT_DELAY, RIGHT_DELAY,
1321 LEFT_VALID, RIGHT_VALID
1327 #define MASK(b) (1 << (b))
1328 #define MASK2(a,b) (MASK(a) | MASK(b))
1329 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1330 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1331 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1332 #define STATE_TEST(b) (state & MASK(b))
1333 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1334 ((state &= ~MASK(b)), 0))
1335 #define SWITCH_STATE \
1336 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1339 * Set up a bunch of variables to make the tests simpler.
1343 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1344 ep = xfs_iext_get_ext(ifp, idx);
1345 xfs_bmbt_get_all(ep, &PREV);
1346 newext = new->br_state;
1347 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1348 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1349 ASSERT(PREV.br_state == oldext);
1350 new_endoff = new->br_startoff + new->br_blockcount;
1351 ASSERT(PREV.br_startoff <= new->br_startoff);
1352 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1354 * Set flags determining what part of the previous oldext allocation
1355 * extent is being replaced by a newext allocation.
1357 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
1358 STATE_SET(RIGHT_FILLING,
1359 PREV.br_startoff + PREV.br_blockcount == new_endoff);
1361 * Check and set flags if this segment has a left neighbor.
1362 * Don't set contiguous if the combined extent would be too large.
1364 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1365 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1366 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
1368 STATE_SET(LEFT_CONTIG,
1369 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1370 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1371 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1372 LEFT.br_state == newext &&
1373 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1375 * Check and set flags if this segment has a right neighbor.
1376 * Don't set contiguous if the combined extent would be too large.
1377 * Also check for all-three-contiguous being too large.
1379 if (STATE_SET_TEST(RIGHT_VALID,
1381 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
1382 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1383 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
1385 STATE_SET(RIGHT_CONTIG,
1386 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1387 new_endoff == RIGHT.br_startoff &&
1388 new->br_startblock + new->br_blockcount ==
1389 RIGHT.br_startblock &&
1390 newext == RIGHT.br_state &&
1391 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1392 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
1393 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
1394 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1397 * Switch out based on the FILLING and CONTIG state bits.
1399 switch (SWITCH_STATE) {
1401 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1403 * Setting all of a previous oldext extent to newext.
1404 * The left and right neighbors are both contiguous with new.
1406 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1408 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1409 LEFT.br_blockcount + PREV.br_blockcount +
1410 RIGHT.br_blockcount);
1411 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1413 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
1415 xfs_iext_remove(ifp, idx, 2);
1416 ip->i_df.if_lastex = idx - 1;
1417 ip->i_d.di_nextents -= 2;
1419 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1421 rval = XFS_ILOG_CORE;
1422 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1423 RIGHT.br_startblock,
1424 RIGHT.br_blockcount, &i)))
1427 if ((error = xfs_bmbt_delete(cur, &i)))
1430 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1433 if ((error = xfs_bmbt_delete(cur, &i)))
1436 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1439 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1441 LEFT.br_blockcount + PREV.br_blockcount +
1442 RIGHT.br_blockcount, LEFT.br_state)))
1445 /* DELTA: Three in-core extents are replaced by one. */
1446 temp = LEFT.br_startoff;
1447 temp2 = LEFT.br_blockcount +
1448 PREV.br_blockcount +
1449 RIGHT.br_blockcount;
1452 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
1454 * Setting all of a previous oldext extent to newext.
1455 * The left neighbor is contiguous, the right is not.
1457 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
1459 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1460 LEFT.br_blockcount + PREV.br_blockcount);
1461 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
1463 ip->i_df.if_lastex = idx - 1;
1464 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
1466 xfs_iext_remove(ifp, idx, 1);
1467 ip->i_d.di_nextents--;
1469 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1471 rval = XFS_ILOG_CORE;
1472 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1473 PREV.br_startblock, PREV.br_blockcount,
1477 if ((error = xfs_bmbt_delete(cur, &i)))
1480 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1483 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1485 LEFT.br_blockcount + PREV.br_blockcount,
1489 /* DELTA: Two in-core extents are replaced by one. */
1490 temp = LEFT.br_startoff;
1491 temp2 = LEFT.br_blockcount +
1495 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
1497 * Setting all of a previous oldext extent to newext.
1498 * The right neighbor is contiguous, the left is not.
1500 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
1502 xfs_bmbt_set_blockcount(ep,
1503 PREV.br_blockcount + RIGHT.br_blockcount);
1504 xfs_bmbt_set_state(ep, newext);
1505 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
1507 ip->i_df.if_lastex = idx;
1508 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
1510 xfs_iext_remove(ifp, idx + 1, 1);
1511 ip->i_d.di_nextents--;
1513 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1515 rval = XFS_ILOG_CORE;
1516 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1517 RIGHT.br_startblock,
1518 RIGHT.br_blockcount, &i)))
1521 if ((error = xfs_bmbt_delete(cur, &i)))
1524 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1527 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1529 new->br_blockcount + RIGHT.br_blockcount,
1533 /* DELTA: Two in-core extents are replaced by one. */
1534 temp = PREV.br_startoff;
1535 temp2 = PREV.br_blockcount +
1536 RIGHT.br_blockcount;
1539 case MASK2(LEFT_FILLING, RIGHT_FILLING):
1541 * Setting all of a previous oldext extent to newext.
1542 * Neither the left nor right neighbors are contiguous with
1545 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
1547 xfs_bmbt_set_state(ep, newext);
1548 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
1550 ip->i_df.if_lastex = idx;
1552 rval = XFS_ILOG_DEXT;
1555 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1556 new->br_startblock, new->br_blockcount,
1560 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1561 new->br_startblock, new->br_blockcount,
1565 /* DELTA: The in-core extent described by new changed type. */
1566 temp = new->br_startoff;
1567 temp2 = new->br_blockcount;
1570 case MASK2(LEFT_FILLING, LEFT_CONTIG):
1572 * Setting the first part of a previous oldext extent to newext.
1573 * The left neighbor is contiguous.
1575 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
1577 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1578 LEFT.br_blockcount + new->br_blockcount);
1579 xfs_bmbt_set_startoff(ep,
1580 PREV.br_startoff + new->br_blockcount);
1581 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
1583 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
1585 xfs_bmbt_set_startblock(ep,
1586 new->br_startblock + new->br_blockcount);
1587 xfs_bmbt_set_blockcount(ep,
1588 PREV.br_blockcount - new->br_blockcount);
1589 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
1591 ip->i_df.if_lastex = idx - 1;
1593 rval = XFS_ILOG_DEXT;
1596 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1597 PREV.br_startblock, PREV.br_blockcount,
1601 if ((error = xfs_bmbt_update(cur,
1602 PREV.br_startoff + new->br_blockcount,
1603 PREV.br_startblock + new->br_blockcount,
1604 PREV.br_blockcount - new->br_blockcount,
1607 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1609 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1611 LEFT.br_blockcount + new->br_blockcount,
1615 /* DELTA: The boundary between two in-core extents moved. */
1616 temp = LEFT.br_startoff;
1617 temp2 = LEFT.br_blockcount +
1621 case MASK(LEFT_FILLING):
1623 * Setting the first part of a previous oldext extent to newext.
1624 * The left neighbor is not contiguous.
1626 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1627 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1628 xfs_bmbt_set_startoff(ep, new_endoff);
1629 xfs_bmbt_set_blockcount(ep,
1630 PREV.br_blockcount - new->br_blockcount);
1631 xfs_bmbt_set_startblock(ep,
1632 new->br_startblock + new->br_blockcount);
1633 xfs_bmap_trace_post_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1634 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
1636 xfs_iext_insert(ifp, idx, 1, new);
1637 ip->i_df.if_lastex = idx;
1638 ip->i_d.di_nextents++;
1640 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1642 rval = XFS_ILOG_CORE;
1643 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1644 PREV.br_startblock, PREV.br_blockcount,
1648 if ((error = xfs_bmbt_update(cur,
1649 PREV.br_startoff + new->br_blockcount,
1650 PREV.br_startblock + new->br_blockcount,
1651 PREV.br_blockcount - new->br_blockcount,
1654 cur->bc_rec.b = *new;
1655 if ((error = xfs_bmbt_insert(cur, &i)))
1659 /* DELTA: One in-core extent is split in two. */
1660 temp = PREV.br_startoff;
1661 temp2 = PREV.br_blockcount;
1664 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1666 * Setting the last part of a previous oldext extent to newext.
1667 * The right neighbor is contiguous with the new allocation.
1669 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1671 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1673 xfs_bmbt_set_blockcount(ep,
1674 PREV.br_blockcount - new->br_blockcount);
1675 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1677 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1678 new->br_startoff, new->br_startblock,
1679 new->br_blockcount + RIGHT.br_blockcount, newext);
1680 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1682 ip->i_df.if_lastex = idx + 1;
1684 rval = XFS_ILOG_DEXT;
1687 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1689 PREV.br_blockcount, &i)))
1692 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1694 PREV.br_blockcount - new->br_blockcount,
1697 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1699 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1701 new->br_blockcount + RIGHT.br_blockcount,
1705 /* DELTA: The boundary between two in-core extents moved. */
1706 temp = PREV.br_startoff;
1707 temp2 = PREV.br_blockcount +
1708 RIGHT.br_blockcount;
1711 case MASK(RIGHT_FILLING):
1713 * Setting the last part of a previous oldext extent to newext.
1714 * The right neighbor is not contiguous.
1716 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1717 xfs_bmbt_set_blockcount(ep,
1718 PREV.br_blockcount - new->br_blockcount);
1719 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1720 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1721 new, NULL, XFS_DATA_FORK);
1722 xfs_iext_insert(ifp, idx + 1, 1, new);
1723 ip->i_df.if_lastex = idx + 1;
1724 ip->i_d.di_nextents++;
1726 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1728 rval = XFS_ILOG_CORE;
1729 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1730 PREV.br_startblock, PREV.br_blockcount,
1734 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1736 PREV.br_blockcount - new->br_blockcount,
1739 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1740 new->br_startblock, new->br_blockcount,
1744 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1745 if ((error = xfs_bmbt_insert(cur, &i)))
1749 /* DELTA: One in-core extent is split in two. */
1750 temp = PREV.br_startoff;
1751 temp2 = PREV.br_blockcount;
1756 * Setting the middle part of a previous oldext extent to
1757 * newext. Contiguity is impossible here.
1758 * One extent becomes three extents.
1760 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1761 xfs_bmbt_set_blockcount(ep,
1762 new->br_startoff - PREV.br_startoff);
1763 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1765 r[1].br_startoff = new_endoff;
1766 r[1].br_blockcount =
1767 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1768 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1769 r[1].br_state = oldext;
1770 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1772 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1773 ip->i_df.if_lastex = idx + 1;
1774 ip->i_d.di_nextents += 2;
1776 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1778 rval = XFS_ILOG_CORE;
1779 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1780 PREV.br_startblock, PREV.br_blockcount,
1784 /* new right extent - oldext */
1785 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1786 r[1].br_startblock, r[1].br_blockcount,
1789 /* new left extent - oldext */
1790 PREV.br_blockcount =
1791 new->br_startoff - PREV.br_startoff;
1792 cur->bc_rec.b = PREV;
1793 if ((error = xfs_bmbt_insert(cur, &i)))
1796 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1799 /* new middle extent - newext */
1800 cur->bc_rec.b = *new;
1801 if ((error = xfs_bmbt_insert(cur, &i)))
1805 /* DELTA: One in-core extent is split in three. */
1806 temp = PREV.br_startoff;
1807 temp2 = PREV.br_blockcount;
1810 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1811 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1812 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1813 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1814 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1815 case MASK(LEFT_CONTIG):
1816 case MASK(RIGHT_CONTIG):
1818 * These cases are all impossible.
1825 if (delta->xed_startoff > temp)
1826 delta->xed_startoff = temp;
1827 if (delta->xed_blockcount < temp2)
1828 delta->xed_blockcount = temp2;
1842 #undef STATE_SET_TEST
1847 * Called by xfs_bmap_add_extent to handle cases converting a hole
1848 * to a delayed allocation.
1851 STATIC int /* error */
1852 xfs_bmap_add_extent_hole_delay(
1853 xfs_inode_t *ip, /* incore inode pointer */
1854 xfs_extnum_t idx, /* extent number to update/insert */
1855 xfs_btree_cur_t *cur, /* if null, not a btree */
1856 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1857 int *logflagsp, /* inode logging flags */
1858 xfs_extdelta_t *delta, /* Change made to incore extents */
1859 int rsvd) /* OK to allocate reserved blocks */
1861 xfs_bmbt_rec_t *ep; /* extent record for idx */
1862 #ifdef XFS_BMAP_TRACE
1863 static char fname[] = "xfs_bmap_add_extent_hole_delay";
1865 xfs_ifork_t *ifp; /* inode fork pointer */
1866 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1867 xfs_filblks_t newlen=0; /* new indirect size */
1868 xfs_filblks_t oldlen=0; /* old indirect size */
1869 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1870 int state; /* state bits, accessed thru macros */
1871 xfs_filblks_t temp=0; /* temp for indirect calculations */
1872 xfs_filblks_t temp2=0;
1873 enum { /* bit number definitions for state */
1874 LEFT_CONTIG, RIGHT_CONTIG,
1875 LEFT_DELAY, RIGHT_DELAY,
1876 LEFT_VALID, RIGHT_VALID
1879 #define MASK(b) (1 << (b))
1880 #define MASK2(a,b) (MASK(a) | MASK(b))
1881 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1882 #define STATE_TEST(b) (state & MASK(b))
1883 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1884 ((state &= ~MASK(b)), 0))
1885 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1887 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1888 ep = xfs_iext_get_ext(ifp, idx);
1890 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1892 * Check and set flags if this segment has a left neighbor
1894 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1895 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1896 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1899 * Check and set flags if the current (right) segment exists.
1900 * If it doesn't exist, we're converting the hole at end-of-file.
1902 if (STATE_SET_TEST(RIGHT_VALID,
1904 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1905 xfs_bmbt_get_all(ep, &right);
1906 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1909 * Set contiguity flags on the left and right neighbors.
1910 * Don't let extents get too large, even if the pieces are contiguous.
1912 STATE_SET(LEFT_CONTIG,
1913 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1914 left.br_startoff + left.br_blockcount == new->br_startoff &&
1915 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1916 STATE_SET(RIGHT_CONTIG,
1917 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1918 new->br_startoff + new->br_blockcount == right.br_startoff &&
1919 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1920 (!STATE_TEST(LEFT_CONTIG) ||
1921 (left.br_blockcount + new->br_blockcount +
1922 right.br_blockcount <= MAXEXTLEN)));
1924 * Switch out based on the contiguity flags.
1926 switch (SWITCH_STATE) {
1928 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1930 * New allocation is contiguous with delayed allocations
1931 * on the left and on the right.
1932 * Merge all three into a single extent record.
1934 temp = left.br_blockcount + new->br_blockcount +
1935 right.br_blockcount;
1936 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
1938 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1939 oldlen = STARTBLOCKVAL(left.br_startblock) +
1940 STARTBLOCKVAL(new->br_startblock) +
1941 STARTBLOCKVAL(right.br_startblock);
1942 newlen = xfs_bmap_worst_indlen(ip, temp);
1943 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1944 NULLSTARTBLOCK((int)newlen));
1945 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
1947 xfs_bmap_trace_delete(fname, "LC|RC", ip, idx, 1,
1949 xfs_iext_remove(ifp, idx, 1);
1950 ip->i_df.if_lastex = idx - 1;
1951 /* DELTA: Two in-core extents were replaced by one. */
1953 temp = left.br_startoff;
1956 case MASK(LEFT_CONTIG):
1958 * New allocation is contiguous with a delayed allocation
1960 * Merge the new allocation with the left neighbor.
1962 temp = left.br_blockcount + new->br_blockcount;
1963 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1,
1965 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1966 oldlen = STARTBLOCKVAL(left.br_startblock) +
1967 STARTBLOCKVAL(new->br_startblock);
1968 newlen = xfs_bmap_worst_indlen(ip, temp);
1969 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1970 NULLSTARTBLOCK((int)newlen));
1971 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1,
1973 ip->i_df.if_lastex = idx - 1;
1974 /* DELTA: One in-core extent grew into a hole. */
1976 temp = left.br_startoff;
1979 case MASK(RIGHT_CONTIG):
1981 * New allocation is contiguous with a delayed allocation
1983 * Merge the new allocation with the right neighbor.
1985 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1986 temp = new->br_blockcount + right.br_blockcount;
1987 oldlen = STARTBLOCKVAL(new->br_startblock) +
1988 STARTBLOCKVAL(right.br_startblock);
1989 newlen = xfs_bmap_worst_indlen(ip, temp);
1990 xfs_bmbt_set_allf(ep, new->br_startoff,
1991 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
1992 xfs_bmap_trace_post_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1993 ip->i_df.if_lastex = idx;
1994 /* DELTA: One in-core extent grew into a hole. */
1996 temp = new->br_startoff;
2001 * New allocation is not contiguous with another
2002 * delayed allocation.
2003 * Insert a new entry.
2005 oldlen = newlen = 0;
2006 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
2008 xfs_iext_insert(ifp, idx, 1, new);
2009 ip->i_df.if_lastex = idx;
2010 /* DELTA: A new in-core extent was added in a hole. */
2011 temp2 = new->br_blockcount;
2012 temp = new->br_startoff;
2015 if (oldlen != newlen) {
2016 ASSERT(oldlen > newlen);
2017 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
2018 (int)(oldlen - newlen), rsvd);
2020 * Nothing to do for disk quota accounting here.
2025 if (delta->xed_startoff > temp)
2026 delta->xed_startoff = temp;
2027 if (delta->xed_blockcount < temp2)
2028 delta->xed_blockcount = temp2;
2036 #undef STATE_SET_TEST
2041 * Called by xfs_bmap_add_extent to handle cases converting a hole
2042 * to a real allocation.
2044 STATIC int /* error */
2045 xfs_bmap_add_extent_hole_real(
2046 xfs_inode_t *ip, /* incore inode pointer */
2047 xfs_extnum_t idx, /* extent number to update/insert */
2048 xfs_btree_cur_t *cur, /* if null, not a btree */
2049 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2050 int *logflagsp, /* inode logging flags */
2051 xfs_extdelta_t *delta, /* Change made to incore extents */
2052 int whichfork) /* data or attr fork */
2054 xfs_bmbt_rec_t *ep; /* pointer to extent entry ins. point */
2055 int error; /* error return value */
2056 #ifdef XFS_BMAP_TRACE
2057 static char fname[] = "xfs_bmap_add_extent_hole_real";
2059 int i; /* temp state */
2060 xfs_ifork_t *ifp; /* inode fork pointer */
2061 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2062 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2063 int rval=0; /* return value (logging flags) */
2064 int state; /* state bits, accessed thru macros */
2065 xfs_filblks_t temp=0;
2066 xfs_filblks_t temp2=0;
2067 enum { /* bit number definitions for state */
2068 LEFT_CONTIG, RIGHT_CONTIG,
2069 LEFT_DELAY, RIGHT_DELAY,
2070 LEFT_VALID, RIGHT_VALID
2073 #define MASK(b) (1 << (b))
2074 #define MASK2(a,b) (MASK(a) | MASK(b))
2075 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
2076 #define STATE_TEST(b) (state & MASK(b))
2077 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
2078 ((state &= ~MASK(b)), 0))
2079 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
2081 ifp = XFS_IFORK_PTR(ip, whichfork);
2082 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
2083 ep = xfs_iext_get_ext(ifp, idx);
2086 * Check and set flags if this segment has a left neighbor.
2088 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
2089 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
2090 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
2093 * Check and set flags if this segment has a current value.
2094 * Not true if we're inserting into the "hole" at eof.
2096 if (STATE_SET_TEST(RIGHT_VALID,
2098 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
2099 xfs_bmbt_get_all(ep, &right);
2100 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
2103 * We're inserting a real allocation between "left" and "right".
2104 * Set the contiguity flags. Don't let extents get too large.
2106 STATE_SET(LEFT_CONTIG,
2107 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
2108 left.br_startoff + left.br_blockcount == new->br_startoff &&
2109 left.br_startblock + left.br_blockcount == new->br_startblock &&
2110 left.br_state == new->br_state &&
2111 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
2112 STATE_SET(RIGHT_CONTIG,
2113 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
2114 new->br_startoff + new->br_blockcount == right.br_startoff &&
2115 new->br_startblock + new->br_blockcount ==
2116 right.br_startblock &&
2117 new->br_state == right.br_state &&
2118 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2119 (!STATE_TEST(LEFT_CONTIG) ||
2120 left.br_blockcount + new->br_blockcount +
2121 right.br_blockcount <= MAXEXTLEN));
2125 * Select which case we're in here, and implement it.
2127 switch (SWITCH_STATE) {
2129 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
2131 * New allocation is contiguous with real allocations on the
2132 * left and on the right.
2133 * Merge all three into a single extent record.
2135 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
2137 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2138 left.br_blockcount + new->br_blockcount +
2139 right.br_blockcount);
2140 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
2142 xfs_bmap_trace_delete(fname, "LC|RC", ip,
2144 xfs_iext_remove(ifp, idx, 1);
2145 ifp->if_lastex = idx - 1;
2146 XFS_IFORK_NEXT_SET(ip, whichfork,
2147 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2149 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2151 rval = XFS_ILOG_CORE;
2152 if ((error = xfs_bmbt_lookup_eq(cur,
2154 right.br_startblock,
2155 right.br_blockcount, &i)))
2158 if ((error = xfs_bmbt_delete(cur, &i)))
2161 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2164 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2166 left.br_blockcount +
2167 new->br_blockcount +
2168 right.br_blockcount,
2172 /* DELTA: Two in-core extents were replaced by one. */
2173 temp = left.br_startoff;
2174 temp2 = left.br_blockcount +
2175 new->br_blockcount +
2176 right.br_blockcount;
2179 case MASK(LEFT_CONTIG):
2181 * New allocation is contiguous with a real allocation
2183 * Merge the new allocation with the left neighbor.
2185 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1, whichfork);
2186 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2187 left.br_blockcount + new->br_blockcount);
2188 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1, whichfork);
2189 ifp->if_lastex = idx - 1;
2191 rval = XFS_ILOG_FEXT(whichfork);
2194 if ((error = xfs_bmbt_lookup_eq(cur,
2197 left.br_blockcount, &i)))
2200 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2202 left.br_blockcount +
2207 /* DELTA: One in-core extent grew. */
2208 temp = left.br_startoff;
2209 temp2 = left.br_blockcount +
2213 case MASK(RIGHT_CONTIG):
2215 * New allocation is contiguous with a real allocation
2217 * Merge the new allocation with the right neighbor.
2219 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, whichfork);
2220 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2221 new->br_blockcount + right.br_blockcount,
2223 xfs_bmap_trace_post_update(fname, "RC", ip, idx, whichfork);
2224 ifp->if_lastex = idx;
2226 rval = XFS_ILOG_FEXT(whichfork);
2229 if ((error = xfs_bmbt_lookup_eq(cur,
2231 right.br_startblock,
2232 right.br_blockcount, &i)))
2235 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2237 new->br_blockcount +
2238 right.br_blockcount,
2242 /* DELTA: One in-core extent grew. */
2243 temp = new->br_startoff;
2244 temp2 = new->br_blockcount +
2245 right.br_blockcount;
2250 * New allocation is not contiguous with another
2252 * Insert a new entry.
2254 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
2256 xfs_iext_insert(ifp, idx, 1, new);
2257 ifp->if_lastex = idx;
2258 XFS_IFORK_NEXT_SET(ip, whichfork,
2259 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2261 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2263 rval = XFS_ILOG_CORE;
2264 if ((error = xfs_bmbt_lookup_eq(cur,
2267 new->br_blockcount, &i)))
2270 cur->bc_rec.b.br_state = new->br_state;
2271 if ((error = xfs_bmbt_insert(cur, &i)))
2275 /* DELTA: A new extent was added in a hole. */
2276 temp = new->br_startoff;
2277 temp2 = new->br_blockcount;
2282 if (delta->xed_startoff > temp)
2283 delta->xed_startoff = temp;
2284 if (delta->xed_blockcount < temp2)
2285 delta->xed_blockcount = temp2;
2294 #undef STATE_SET_TEST
2299 * Adjust the size of the new extent based on di_extsize and rt extsize.
2302 xfs_bmap_extsize_align(
2304 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2305 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2306 xfs_extlen_t extsz, /* align to this extent size */
2307 int rt, /* is this a realtime inode? */
2308 int eof, /* is extent at end-of-file? */
2309 int delay, /* creating delalloc extent? */
2310 int convert, /* overwriting unwritten extent? */
2311 xfs_fileoff_t *offp, /* in/out: aligned offset */
2312 xfs_extlen_t *lenp) /* in/out: aligned length */
2314 xfs_fileoff_t orig_off; /* original offset */
2315 xfs_extlen_t orig_alen; /* original length */
2316 xfs_fileoff_t orig_end; /* original off+len */
2317 xfs_fileoff_t nexto; /* next file offset */
2318 xfs_fileoff_t prevo; /* previous file offset */
2319 xfs_fileoff_t align_off; /* temp for offset */
2320 xfs_extlen_t align_alen; /* temp for length */
2321 xfs_extlen_t temp; /* temp for calculations */
2326 orig_off = align_off = *offp;
2327 orig_alen = align_alen = *lenp;
2328 orig_end = orig_off + orig_alen;
2331 * If this request overlaps an existing extent, then don't
2332 * attempt to perform any additional alignment.
2334 if (!delay && !eof &&
2335 (orig_off >= gotp->br_startoff) &&
2336 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2341 * If the file offset is unaligned vs. the extent size
2342 * we need to align it. This will be possible unless
2343 * the file was previously written with a kernel that didn't
2344 * perform this alignment, or if a truncate shot us in the
2347 temp = do_mod(orig_off, extsz);
2353 * Same adjustment for the end of the requested area.
2355 if ((temp = (align_alen % extsz))) {
2356 align_alen += extsz - temp;
2359 * If the previous block overlaps with this proposed allocation
2360 * then move the start forward without adjusting the length.
2362 if (prevp->br_startoff != NULLFILEOFF) {
2363 if (prevp->br_startblock == HOLESTARTBLOCK)
2364 prevo = prevp->br_startoff;
2366 prevo = prevp->br_startoff + prevp->br_blockcount;
2369 if (align_off != orig_off && align_off < prevo)
2372 * If the next block overlaps with this proposed allocation
2373 * then move the start back without adjusting the length,
2374 * but not before offset 0.
2375 * This may of course make the start overlap previous block,
2376 * and if we hit the offset 0 limit then the next block
2377 * can still overlap too.
2379 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2380 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2381 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2382 nexto = gotp->br_startoff + gotp->br_blockcount;
2384 nexto = gotp->br_startoff;
2386 nexto = NULLFILEOFF;
2388 align_off + align_alen != orig_end &&
2389 align_off + align_alen > nexto)
2390 align_off = nexto > align_alen ? nexto - align_alen : 0;
2392 * If we're now overlapping the next or previous extent that
2393 * means we can't fit an extsz piece in this hole. Just move
2394 * the start forward to the first valid spot and set
2395 * the length so we hit the end.
2397 if (align_off != orig_off && align_off < prevo)
2399 if (align_off + align_alen != orig_end &&
2400 align_off + align_alen > nexto &&
2401 nexto != NULLFILEOFF) {
2402 ASSERT(nexto > prevo);
2403 align_alen = nexto - align_off;
2407 * If realtime, and the result isn't a multiple of the realtime
2408 * extent size we need to remove blocks until it is.
2410 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2412 * We're not covering the original request, or
2413 * we won't be able to once we fix the length.
2415 if (orig_off < align_off ||
2416 orig_end > align_off + align_alen ||
2417 align_alen - temp < orig_alen)
2418 return XFS_ERROR(EINVAL);
2420 * Try to fix it by moving the start up.
2422 if (align_off + temp <= orig_off) {
2427 * Try to fix it by moving the end in.
2429 else if (align_off + align_alen - temp >= orig_end)
2432 * Set the start to the minimum then trim the length.
2435 align_alen -= orig_off - align_off;
2436 align_off = orig_off;
2437 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2440 * Result doesn't cover the request, fail it.
2442 if (orig_off < align_off || orig_end > align_off + align_alen)
2443 return XFS_ERROR(EINVAL);
2445 ASSERT(orig_off >= align_off);
2446 ASSERT(orig_end <= align_off + align_alen);
2450 if (!eof && gotp->br_startoff != NULLFILEOFF)
2451 ASSERT(align_off + align_alen <= gotp->br_startoff);
2452 if (prevp->br_startoff != NULLFILEOFF)
2453 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2461 #define XFS_ALLOC_GAP_UNITS 4
2465 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2467 xfs_fsblock_t adjust; /* adjustment to block numbers */
2468 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2469 xfs_mount_t *mp; /* mount point structure */
2470 int nullfb; /* true if ap->firstblock isn't set */
2471 int rt; /* true if inode is realtime */
2473 #define ISVALID(x,y) \
2475 (x) < mp->m_sb.sb_rblocks : \
2476 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2477 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2478 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2480 mp = ap->ip->i_mount;
2481 nullfb = ap->firstblock == NULLFSBLOCK;
2482 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2483 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2485 * If allocating at eof, and there's a previous real block,
2486 * try to use it's last block as our starting point.
2488 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2489 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2490 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2491 ap->prevp->br_startblock)) {
2492 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2494 * Adjust for the gap between prevp and us.
2497 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2499 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2503 * If not at eof, then compare the two neighbor blocks.
2504 * Figure out whether either one gives us a good starting point,
2505 * and pick the better one.
2507 else if (!ap->eof) {
2508 xfs_fsblock_t gotbno; /* right side block number */
2509 xfs_fsblock_t gotdiff=0; /* right side difference */
2510 xfs_fsblock_t prevbno; /* left side block number */
2511 xfs_fsblock_t prevdiff=0; /* left side difference */
2514 * If there's a previous (left) block, select a requested
2515 * start block based on it.
2517 if (ap->prevp->br_startoff != NULLFILEOFF &&
2518 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2519 (prevbno = ap->prevp->br_startblock +
2520 ap->prevp->br_blockcount) &&
2521 ISVALID(prevbno, ap->prevp->br_startblock)) {
2523 * Calculate gap to end of previous block.
2525 adjust = prevdiff = ap->off -
2526 (ap->prevp->br_startoff +
2527 ap->prevp->br_blockcount);
2529 * Figure the startblock based on the previous block's
2530 * end and the gap size.
2532 * If the gap is large relative to the piece we're
2533 * allocating, or using it gives us an invalid block
2534 * number, then just use the end of the previous block.
2536 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2537 ISVALID(prevbno + prevdiff,
2538 ap->prevp->br_startblock))
2543 * If the firstblock forbids it, can't use it,
2546 if (!rt && !nullfb &&
2547 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2548 prevbno = NULLFSBLOCK;
2551 * No previous block or can't follow it, just default.
2554 prevbno = NULLFSBLOCK;
2556 * If there's a following (right) block, select a requested
2557 * start block based on it.
2559 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
2561 * Calculate gap to start of next block.
2563 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2565 * Figure the startblock based on the next block's
2566 * start and the gap size.
2568 gotbno = ap->gotp->br_startblock;
2571 * If the gap is large relative to the piece we're
2572 * allocating, or using it gives us an invalid block
2573 * number, then just use the start of the next block
2574 * offset by our length.
2576 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2577 ISVALID(gotbno - gotdiff, gotbno))
2579 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2581 gotdiff += adjust - ap->alen;
2585 * If the firstblock forbids it, can't use it,
2588 if (!rt && !nullfb &&
2589 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2590 gotbno = NULLFSBLOCK;
2593 * No next block, just default.
2596 gotbno = NULLFSBLOCK;
2598 * If both valid, pick the better one, else the only good
2599 * one, else ap->rval is already set (to 0 or the inode block).
2601 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2602 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2603 else if (prevbno != NULLFSBLOCK)
2605 else if (gotbno != NULLFSBLOCK)
2614 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2616 xfs_alloctype_t atype = 0; /* type for allocation routines */
2617 int error; /* error return value */
2618 xfs_mount_t *mp; /* mount point structure */
2619 xfs_extlen_t prod = 0; /* product factor for allocators */
2620 xfs_extlen_t ralen = 0; /* realtime allocation length */
2621 xfs_extlen_t align; /* minimum allocation alignment */
2622 xfs_rtblock_t rtx; /* realtime extent number */
2625 mp = ap->ip->i_mount;
2626 align = ap->ip->i_d.di_extsize ?
2627 ap->ip->i_d.di_extsize : mp->m_sb.sb_rextsize;
2628 prod = align / mp->m_sb.sb_rextsize;
2629 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2630 align, 1, ap->eof, 0,
2631 ap->conv, &ap->off, &ap->alen);
2635 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2638 * If the offset & length are not perfectly aligned
2639 * then kill prod, it will just get us in trouble.
2641 if (do_mod(ap->off, align) || ap->alen % align)
2644 * Set ralen to be the actual requested length in rtextents.
2646 ralen = ap->alen / mp->m_sb.sb_rextsize;
2648 * If the old value was close enough to MAXEXTLEN that
2649 * we rounded up to it, cut it back so it's valid again.
2650 * Note that if it's a really large request (bigger than
2651 * MAXEXTLEN), we don't hear about that number, and can't
2652 * adjust the starting point to match it.
2654 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2655 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2657 * If it's an allocation to an empty file at offset 0,
2658 * pick an extent that will space things out in the rt area.
2660 if (ap->eof && ap->off == 0) {
2661 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2664 ap->rval = rtx * mp->m_sb.sb_rextsize;
2669 xfs_bmap_adjacent(ap);
2672 * Realtime allocation, done through xfs_rtallocate_extent.
2674 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2675 do_div(ap->rval, mp->m_sb.sb_rextsize);
2678 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2679 &ralen, atype, ap->wasdel, prod, &rtb)))
2681 if (rtb == NULLFSBLOCK && prod > 1 &&
2682 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2683 ap->alen, &ralen, atype,
2684 ap->wasdel, 1, &rtb)))
2687 if (ap->rval != NULLFSBLOCK) {
2688 ap->rval *= mp->m_sb.sb_rextsize;
2689 ralen *= mp->m_sb.sb_rextsize;
2691 ap->ip->i_d.di_nblocks += ralen;
2692 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2694 ap->ip->i_delayed_blks -= ralen;
2696 * Adjust the disk quota also. This was reserved
2699 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2700 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2701 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2710 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2712 xfs_mount_t *mp; /* mount point structure */
2713 xfs_alloctype_t atype = 0; /* type for allocation routines */
2714 xfs_extlen_t align; /* minimum allocation alignment */
2716 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2717 xfs_agnumber_t startag;
2718 xfs_alloc_arg_t args;
2721 xfs_extlen_t longest;
2723 xfs_extlen_t nextminlen = 0;
2725 int nullfb; /* true if ap->firstblock isn't set */
2731 mp = ap->ip->i_mount;
2732 align = (ap->userdata && ap->ip->i_d.di_extsize &&
2733 (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)) ?
2734 ap->ip->i_d.di_extsize : 0;
2735 if (unlikely(align)) {
2736 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2737 align, 0, ap->eof, 0, ap->conv,
2738 &ap->off, &ap->alen);
2742 nullfb = ap->firstblock == NULLFSBLOCK;
2743 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2745 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2747 ap->rval = ap->firstblock;
2749 xfs_bmap_adjacent(ap);
2752 * If allowed, use ap->rval; otherwise must use firstblock since
2753 * it's in the right allocation group.
2755 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2758 ap->rval = ap->firstblock;
2760 * Normal allocation, done through xfs_alloc_vextent.
2762 tryagain = isaligned = 0;
2765 args.fsbno = ap->rval;
2766 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2769 args.type = XFS_ALLOCTYPE_START_BNO;
2770 args.total = ap->total;
2772 * Find the longest available space.
2773 * We're going to try for the whole allocation at once.
2775 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2777 down_read(&mp->m_peraglock);
2778 while (blen < ap->alen) {
2779 pag = &mp->m_perag[ag];
2780 if (!pag->pagf_init &&
2781 (error = xfs_alloc_pagf_init(mp, args.tp,
2782 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2783 up_read(&mp->m_peraglock);
2787 * See xfs_alloc_fix_freelist...
2789 if (pag->pagf_init) {
2790 need = XFS_MIN_FREELIST_PAG(pag, mp);
2791 delta = need > pag->pagf_flcount ?
2792 need - pag->pagf_flcount : 0;
2793 longest = (pag->pagf_longest > delta) ?
2794 (pag->pagf_longest - delta) :
2795 (pag->pagf_flcount > 0 ||
2796 pag->pagf_longest > 0);
2801 if (++ag == mp->m_sb.sb_agcount)
2806 up_read(&mp->m_peraglock);
2808 * Since the above loop did a BUF_TRYLOCK, it is
2809 * possible that there is space for this request.
2811 if (notinit || blen < ap->minlen)
2812 args.minlen = ap->minlen;
2814 * If the best seen length is less than the request
2815 * length, use the best as the minimum.
2817 else if (blen < ap->alen)
2820 * Otherwise we've seen an extent as big as alen,
2821 * use that as the minimum.
2824 args.minlen = ap->alen;
2825 } else if (ap->low) {
2826 args.type = XFS_ALLOCTYPE_FIRST_AG;
2827 args.total = args.minlen = ap->minlen;
2829 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2830 args.total = ap->total;
2831 args.minlen = ap->minlen;
2833 if (unlikely(ap->userdata && ap->ip->i_d.di_extsize &&
2834 (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE))) {
2835 args.prod = ap->ip->i_d.di_extsize;
2836 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2837 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2838 } else if (unlikely(mp->m_sb.sb_blocksize >= NBPP)) {
2842 args.prod = NBPP >> mp->m_sb.sb_blocklog;
2843 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2844 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2847 * If we are not low on available data blocks, and the
2848 * underlying logical volume manager is a stripe, and
2849 * the file offset is zero then try to allocate data
2850 * blocks on stripe unit boundary.
2851 * NOTE: ap->aeof is only set if the allocation length
2852 * is >= the stripe unit and the allocation offset is
2853 * at the end of file.
2855 if (!ap->low && ap->aeof) {
2857 args.alignment = mp->m_dalign;
2861 * Adjust for alignment
2863 if (blen > args.alignment && blen <= ap->alen)
2864 args.minlen = blen - args.alignment;
2865 args.minalignslop = 0;
2868 * First try an exact bno allocation.
2869 * If it fails then do a near or start bno
2870 * allocation with alignment turned on.
2874 args.type = XFS_ALLOCTYPE_THIS_BNO;
2877 * Compute the minlen+alignment for the
2878 * next case. Set slop so that the value
2879 * of minlen+alignment+slop doesn't go up
2880 * between the calls.
2882 if (blen > mp->m_dalign && blen <= ap->alen)
2883 nextminlen = blen - mp->m_dalign;
2885 nextminlen = args.minlen;
2886 if (nextminlen + mp->m_dalign > args.minlen + 1)
2888 nextminlen + mp->m_dalign -
2891 args.minalignslop = 0;
2895 args.minalignslop = 0;
2897 args.minleft = ap->minleft;
2898 args.wasdel = ap->wasdel;
2900 args.userdata = ap->userdata;
2901 if ((error = xfs_alloc_vextent(&args)))
2903 if (tryagain && args.fsbno == NULLFSBLOCK) {
2905 * Exact allocation failed. Now try with alignment
2909 args.fsbno = ap->rval;
2910 args.alignment = mp->m_dalign;
2911 args.minlen = nextminlen;
2912 args.minalignslop = 0;
2914 if ((error = xfs_alloc_vextent(&args)))
2917 if (isaligned && args.fsbno == NULLFSBLOCK) {
2919 * allocation failed, so turn off alignment and
2923 args.fsbno = ap->rval;
2925 if ((error = xfs_alloc_vextent(&args)))
2928 if (args.fsbno == NULLFSBLOCK && nullfb &&
2929 args.minlen > ap->minlen) {
2930 args.minlen = ap->minlen;
2931 args.type = XFS_ALLOCTYPE_START_BNO;
2932 args.fsbno = ap->rval;
2933 if ((error = xfs_alloc_vextent(&args)))
2936 if (args.fsbno == NULLFSBLOCK && nullfb) {
2938 args.type = XFS_ALLOCTYPE_FIRST_AG;
2939 args.total = ap->minlen;
2941 if ((error = xfs_alloc_vextent(&args)))
2945 if (args.fsbno != NULLFSBLOCK) {
2946 ap->firstblock = ap->rval = args.fsbno;
2947 ASSERT(nullfb || fb_agno == args.agno ||
2948 (ap->low && fb_agno < args.agno));
2949 ap->alen = args.len;
2950 ap->ip->i_d.di_nblocks += args.len;
2951 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2953 ap->ip->i_delayed_blks -= args.len;
2955 * Adjust the disk quota also. This was reserved
2958 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2959 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2960 XFS_TRANS_DQ_BCOUNT,
2963 ap->rval = NULLFSBLOCK;
2970 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2971 * It figures out where to ask the underlying allocator to put the new extent.
2975 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2977 if ((ap->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) && ap->userdata)
2978 return xfs_bmap_rtalloc(ap);
2979 return xfs_bmap_btalloc(ap);
2983 * Transform a btree format file with only one leaf node, where the
2984 * extents list will fit in the inode, into an extents format file.
2985 * Since the file extents are already in-core, all we have to do is
2986 * give up the space for the btree root and pitch the leaf block.
2988 STATIC int /* error */
2989 xfs_bmap_btree_to_extents(
2990 xfs_trans_t *tp, /* transaction pointer */
2991 xfs_inode_t *ip, /* incore inode pointer */
2992 xfs_btree_cur_t *cur, /* btree cursor */
2993 int *logflagsp, /* inode logging flags */
2994 int whichfork) /* data or attr fork */
2997 xfs_bmbt_block_t *cblock;/* child btree block */
2998 xfs_fsblock_t cbno; /* child block number */
2999 xfs_buf_t *cbp; /* child block's buffer */
3000 int error; /* error return value */
3001 xfs_ifork_t *ifp; /* inode fork data */
3002 xfs_mount_t *mp; /* mount point structure */
3003 xfs_bmbt_ptr_t *pp; /* ptr to block address */
3004 xfs_bmbt_block_t *rblock;/* root btree block */
3006 ifp = XFS_IFORK_PTR(ip, whichfork);
3007 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3008 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
3009 rblock = ifp->if_broot;
3010 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
3011 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
3012 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
3014 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
3017 if ((error = xfs_btree_check_lptr(cur, INT_GET(*pp, ARCH_CONVERT), 1)))
3020 cbno = INT_GET(*pp, ARCH_CONVERT);
3021 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
3022 XFS_BMAP_BTREE_REF)))
3024 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
3025 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
3027 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
3028 ip->i_d.di_nblocks--;
3029 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
3030 xfs_trans_binval(tp, cbp);
3031 if (cur->bc_bufs[0] == cbp)
3032 cur->bc_bufs[0] = NULL;
3033 xfs_iroot_realloc(ip, -1, whichfork);
3034 ASSERT(ifp->if_broot == NULL);
3035 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
3036 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3037 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
3042 * Called by xfs_bmapi to update file extent records and the btree
3043 * after removing space (or undoing a delayed allocation).
3045 STATIC int /* error */
3046 xfs_bmap_del_extent(
3047 xfs_inode_t *ip, /* incore inode pointer */
3048 xfs_trans_t *tp, /* current transaction pointer */
3049 xfs_extnum_t idx, /* extent number to update/delete */
3050 xfs_bmap_free_t *flist, /* list of extents to be freed */
3051 xfs_btree_cur_t *cur, /* if null, not a btree */
3052 xfs_bmbt_irec_t *del, /* data to remove from extents */
3053 int *logflagsp, /* inode logging flags */
3054 xfs_extdelta_t *delta, /* Change made to incore extents */
3055 int whichfork, /* data or attr fork */
3056 int rsvd) /* OK to allocate reserved blocks */
3058 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
3059 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
3060 xfs_fsblock_t del_endblock=0; /* first block past del */
3061 xfs_fileoff_t del_endoff; /* first offset past del */
3062 int delay; /* current block is delayed allocated */
3063 int do_fx; /* free extent at end of routine */
3064 xfs_bmbt_rec_t *ep; /* current extent entry pointer */
3065 int error; /* error return value */
3066 int flags; /* inode logging flags */
3067 #ifdef XFS_BMAP_TRACE
3068 static char fname[] = "xfs_bmap_del_extent";
3070 xfs_bmbt_irec_t got; /* current extent entry */
3071 xfs_fileoff_t got_endoff; /* first offset past got */
3072 int i; /* temp state */
3073 xfs_ifork_t *ifp; /* inode fork pointer */
3074 xfs_mount_t *mp; /* mount structure */
3075 xfs_filblks_t nblks; /* quota/sb block count */
3076 xfs_bmbt_irec_t new; /* new record to be inserted */
3078 uint qfield; /* quota field to update */
3079 xfs_filblks_t temp; /* for indirect length calculations */
3080 xfs_filblks_t temp2; /* for indirect length calculations */
3082 XFS_STATS_INC(xs_del_exlist);
3084 ifp = XFS_IFORK_PTR(ip, whichfork);
3085 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
3086 (uint)sizeof(xfs_bmbt_rec_t)));
3087 ASSERT(del->br_blockcount > 0);
3088 ep = xfs_iext_get_ext(ifp, idx);
3089 xfs_bmbt_get_all(ep, &got);
3090 ASSERT(got.br_startoff <= del->br_startoff);
3091 del_endoff = del->br_startoff + del->br_blockcount;
3092 got_endoff = got.br_startoff + got.br_blockcount;
3093 ASSERT(got_endoff >= del_endoff);
3094 delay = ISNULLSTARTBLOCK(got.br_startblock);
3095 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
3100 * If deleting a real allocation, must free up the disk space.
3103 flags = XFS_ILOG_CORE;
3105 * Realtime allocation. Free it and record di_nblocks update.
3107 if (whichfork == XFS_DATA_FORK &&
3108 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
3112 ASSERT(do_mod(del->br_blockcount,
3113 mp->m_sb.sb_rextsize) == 0);
3114 ASSERT(do_mod(del->br_startblock,
3115 mp->m_sb.sb_rextsize) == 0);
3116 bno = del->br_startblock;
3117 len = del->br_blockcount;
3118 do_div(bno, mp->m_sb.sb_rextsize);
3119 do_div(len, mp->m_sb.sb_rextsize);
3120 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3121 (xfs_extlen_t)len)))
3124 nblks = len * mp->m_sb.sb_rextsize;
3125 qfield = XFS_TRANS_DQ_RTBCOUNT;
3128 * Ordinary allocation.
3132 nblks = del->br_blockcount;
3133 qfield = XFS_TRANS_DQ_BCOUNT;
3136 * Set up del_endblock and cur for later.
3138 del_endblock = del->br_startblock + del->br_blockcount;
3140 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3141 got.br_startblock, got.br_blockcount,
3146 da_old = da_new = 0;
3148 da_old = STARTBLOCKVAL(got.br_startblock);
3154 * Set flag value to use in switch statement.
3155 * Left-contig is 2, right-contig is 1.
3157 switch (((got.br_startoff == del->br_startoff) << 1) |
3158 (got_endoff == del_endoff)) {
3161 * Matches the whole extent. Delete the entry.
3163 xfs_bmap_trace_delete(fname, "3", ip, idx, 1, whichfork);
3164 xfs_iext_remove(ifp, idx, 1);
3165 ifp->if_lastex = idx;
3168 XFS_IFORK_NEXT_SET(ip, whichfork,
3169 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3170 flags |= XFS_ILOG_CORE;
3172 flags |= XFS_ILOG_FEXT(whichfork);
3175 if ((error = xfs_bmbt_delete(cur, &i)))
3182 * Deleting the first part of the extent.
3184 xfs_bmap_trace_pre_update(fname, "2", ip, idx, whichfork);
3185 xfs_bmbt_set_startoff(ep, del_endoff);
3186 temp = got.br_blockcount - del->br_blockcount;
3187 xfs_bmbt_set_blockcount(ep, temp);
3188 ifp->if_lastex = idx;
3190 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3192 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3193 xfs_bmap_trace_post_update(fname, "2", ip, idx,
3198 xfs_bmbt_set_startblock(ep, del_endblock);
3199 xfs_bmap_trace_post_update(fname, "2", ip, idx, whichfork);
3201 flags |= XFS_ILOG_FEXT(whichfork);
3204 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3205 got.br_blockcount - del->br_blockcount,
3212 * Deleting the last part of the extent.
3214 temp = got.br_blockcount - del->br_blockcount;
3215 xfs_bmap_trace_pre_update(fname, "1", ip, idx, whichfork);
3216 xfs_bmbt_set_blockcount(ep, temp);
3217 ifp->if_lastex = idx;
3219 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3221 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3222 xfs_bmap_trace_post_update(fname, "1", ip, idx,
3227 xfs_bmap_trace_post_update(fname, "1", ip, idx, whichfork);
3229 flags |= XFS_ILOG_FEXT(whichfork);
3232 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3234 got.br_blockcount - del->br_blockcount,
3241 * Deleting the middle of the extent.
3243 temp = del->br_startoff - got.br_startoff;
3244 xfs_bmap_trace_pre_update(fname, "0", ip, idx, whichfork);
3245 xfs_bmbt_set_blockcount(ep, temp);
3246 new.br_startoff = del_endoff;
3247 temp2 = got_endoff - del_endoff;
3248 new.br_blockcount = temp2;
3249 new.br_state = got.br_state;
3251 new.br_startblock = del_endblock;
3252 flags |= XFS_ILOG_CORE;
3254 if ((error = xfs_bmbt_update(cur,
3256 got.br_startblock, temp,
3259 if ((error = xfs_bmbt_increment(cur, 0, &i)))
3261 cur->bc_rec.b = new;
3262 error = xfs_bmbt_insert(cur, &i);
3263 if (error && error != ENOSPC)
3266 * If get no-space back from btree insert,
3267 * it tried a split, and we have a zero
3268 * block reservation.
3269 * Fix up our state and return the error.
3271 if (error == ENOSPC) {
3273 * Reset the cursor, don't trust
3274 * it after any insert operation.
3276 if ((error = xfs_bmbt_lookup_eq(cur,
3283 * Update the btree record back
3284 * to the original value.
3286 if ((error = xfs_bmbt_update(cur,
3293 * Reset the extent record back
3294 * to the original value.
3296 xfs_bmbt_set_blockcount(ep,
3299 error = XFS_ERROR(ENOSPC);
3304 flags |= XFS_ILOG_FEXT(whichfork);
3305 XFS_IFORK_NEXT_SET(ip, whichfork,
3306 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3308 ASSERT(whichfork == XFS_DATA_FORK);
3309 temp = xfs_bmap_worst_indlen(ip, temp);
3310 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3311 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3312 new.br_startblock = NULLSTARTBLOCK((int)temp2);
3313 da_new = temp + temp2;
3314 while (da_new > da_old) {
3318 xfs_bmbt_set_startblock(ep,
3319 NULLSTARTBLOCK((int)temp));
3321 if (da_new == da_old)
3327 NULLSTARTBLOCK((int)temp2);
3331 xfs_bmap_trace_post_update(fname, "0", ip, idx, whichfork);
3332 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 1, &new, NULL,
3334 xfs_iext_insert(ifp, idx + 1, 1, &new);
3335 ifp->if_lastex = idx + 1;
3339 * If we need to, add to list of extents to delete.
3342 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3345 * Adjust inode # blocks in the file.
3348 ip->i_d.di_nblocks -= nblks;
3350 * Adjust quota data.
3353 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
3356 * Account for change in delayed indirect blocks.
3357 * Nothing to do for disk quota accounting here.
3359 ASSERT(da_old >= da_new);
3360 if (da_old > da_new)
3361 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int)(da_old - da_new),
3364 /* DELTA: report the original extent. */
3365 if (delta->xed_startoff > got.br_startoff)
3366 delta->xed_startoff = got.br_startoff;
3367 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3368 delta->xed_blockcount = got.br_startoff +
3377 * Remove the entry "free" from the free item list. Prev points to the
3378 * previous entry, unless "free" is the head of the list.
3382 xfs_bmap_free_t *flist, /* free item list header */
3383 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3384 xfs_bmap_free_item_t *free) /* list item to be freed */
3387 prev->xbfi_next = free->xbfi_next;
3389 flist->xbf_first = free->xbfi_next;
3391 kmem_zone_free(xfs_bmap_free_item_zone, free);
3395 * Convert an extents-format file into a btree-format file.
3396 * The new file will have a root block (in the inode) and a single child block.
3398 STATIC int /* error */
3399 xfs_bmap_extents_to_btree(
3400 xfs_trans_t *tp, /* transaction pointer */
3401 xfs_inode_t *ip, /* incore inode pointer */
3402 xfs_fsblock_t *firstblock, /* first-block-allocated */
3403 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3404 xfs_btree_cur_t **curp, /* cursor returned to caller */
3405 int wasdel, /* converting a delayed alloc */
3406 int *logflagsp, /* inode logging flags */
3407 int whichfork) /* data or attr fork */
3409 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
3410 xfs_buf_t *abp; /* buffer for ablock */
3411 xfs_alloc_arg_t args; /* allocation arguments */
3412 xfs_bmbt_rec_t *arp; /* child record pointer */
3413 xfs_bmbt_block_t *block; /* btree root block */
3414 xfs_btree_cur_t *cur; /* bmap btree cursor */
3415 xfs_bmbt_rec_t *ep; /* extent record pointer */
3416 int error; /* error return value */
3417 xfs_extnum_t i, cnt; /* extent record index */
3418 xfs_ifork_t *ifp; /* inode fork pointer */
3419 xfs_bmbt_key_t *kp; /* root block key pointer */
3420 xfs_mount_t *mp; /* mount structure */
3421 xfs_extnum_t nextents; /* number of file extents */
3422 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3424 ifp = XFS_IFORK_PTR(ip, whichfork);
3425 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3426 ASSERT(ifp->if_ext_max ==
3427 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3429 * Make space in the inode incore.
3431 xfs_iroot_realloc(ip, 1, whichfork);
3432 ifp->if_flags |= XFS_IFBROOT;
3436 block = ifp->if_broot;
3437 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3438 block->bb_level = cpu_to_be16(1);
3439 block->bb_numrecs = cpu_to_be16(1);
3440 block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3441 block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3443 * Need a cursor. Can't allocate until bb_level is filled in.
3446 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
3448 cur->bc_private.b.firstblock = *firstblock;
3449 cur->bc_private.b.flist = flist;
3450 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3452 * Convert to a btree with two levels, one record in root.
3454 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3457 if (*firstblock == NULLFSBLOCK) {
3458 args.type = XFS_ALLOCTYPE_START_BNO;
3459 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3460 } else if (flist->xbf_low) {
3461 args.type = XFS_ALLOCTYPE_START_BNO;
3462 args.fsbno = *firstblock;
3464 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3465 args.fsbno = *firstblock;
3467 args.minlen = args.maxlen = args.prod = 1;
3468 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3469 args.minalignslop = 0;
3470 args.wasdel = wasdel;
3472 if ((error = xfs_alloc_vextent(&args))) {
3473 xfs_iroot_realloc(ip, -1, whichfork);
3474 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3478 * Allocation can't fail, the space was reserved.
3480 ASSERT(args.fsbno != NULLFSBLOCK);
3481 ASSERT(*firstblock == NULLFSBLOCK ||
3482 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3484 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3485 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3486 cur->bc_private.b.allocated++;
3487 ip->i_d.di_nblocks++;
3488 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3489 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3491 * Fill in the child block.
3493 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
3494 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3495 ablock->bb_level = 0;
3496 ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3497 ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3498 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3499 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3500 for (cnt = i = 0; i < nextents; i++) {
3501 ep = xfs_iext_get_ext(ifp, i);
3502 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
3503 arp->l0 = INT_GET(ep->l0, ARCH_CONVERT);
3504 arp->l1 = INT_GET(ep->l1, ARCH_CONVERT);
3508 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3509 ablock->bb_numrecs = cpu_to_be16(cnt);
3511 * Fill in the root key and pointer.
3513 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
3514 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3515 INT_SET(kp->br_startoff, ARCH_CONVERT, xfs_bmbt_disk_get_startoff(arp));
3516 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
3517 INT_SET(*pp, ARCH_CONVERT, args.fsbno);
3519 * Do all this logging at the end so that
3520 * the root is at the right level.
3522 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
3523 xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3524 ASSERT(*curp == NULL);
3526 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
3531 * Helper routine to reset inode di_forkoff field when switching
3532 * attribute fork from local to extent format - we reset it where
3533 * possible to make space available for inline data fork extents.
3536 xfs_bmap_forkoff_reset(
3541 if (whichfork == XFS_ATTR_FORK &&
3542 (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
3543 (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
3544 ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
3545 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3546 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
3547 (uint)sizeof(xfs_bmbt_rec_t);
3548 ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
3549 (uint)sizeof(xfs_bmbt_rec_t);
3554 * Convert a local file to an extents file.
3555 * This code is out of bounds for data forks of regular files,
3556 * since the file data needs to get logged so things will stay consistent.
3557 * (The bmap-level manipulations are ok, though).
3559 STATIC int /* error */
3560 xfs_bmap_local_to_extents(
3561 xfs_trans_t *tp, /* transaction pointer */
3562 xfs_inode_t *ip, /* incore inode pointer */
3563 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3564 xfs_extlen_t total, /* total blocks needed by transaction */
3565 int *logflagsp, /* inode logging flags */
3566 int whichfork) /* data or attr fork */
3568 int error; /* error return value */
3569 int flags; /* logging flags returned */
3570 #ifdef XFS_BMAP_TRACE
3571 static char fname[] = "xfs_bmap_local_to_extents";
3573 xfs_ifork_t *ifp; /* inode fork pointer */
3576 * We don't want to deal with the case of keeping inode data inline yet.
3577 * So sending the data fork of a regular inode is invalid.
3579 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3580 whichfork == XFS_DATA_FORK));
3581 ifp = XFS_IFORK_PTR(ip, whichfork);
3582 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3585 if (ifp->if_bytes) {
3586 xfs_alloc_arg_t args; /* allocation arguments */
3587 xfs_buf_t *bp; /* buffer for extent block */
3588 xfs_bmbt_rec_t *ep; /* extent record pointer */
3591 args.mp = ip->i_mount;
3592 ASSERT((ifp->if_flags &
3593 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3595 * Allocate a block. We know we need only one, since the
3596 * file currently fits in an inode.
3598 if (*firstblock == NULLFSBLOCK) {
3599 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3600 args.type = XFS_ALLOCTYPE_START_BNO;
3602 args.fsbno = *firstblock;
3603 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3606 args.mod = args.minleft = args.alignment = args.wasdel =
3607 args.isfl = args.minalignslop = 0;
3608 args.minlen = args.maxlen = args.prod = 1;
3609 if ((error = xfs_alloc_vextent(&args)))
3612 * Can't fail, the space was reserved.
3614 ASSERT(args.fsbno != NULLFSBLOCK);
3615 ASSERT(args.len == 1);
3616 *firstblock = args.fsbno;
3617 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3618 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3620 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3621 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3622 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3623 xfs_iext_add(ifp, 0, 1);
3624 ep = xfs_iext_get_ext(ifp, 0);
3625 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3626 xfs_bmap_trace_post_update(fname, "new", ip, 0, whichfork);
3627 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3628 ip->i_d.di_nblocks = 1;
3629 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
3630 XFS_TRANS_DQ_BCOUNT, 1L);
3631 flags |= XFS_ILOG_FEXT(whichfork);
3633 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3634 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3636 ifp->if_flags &= ~XFS_IFINLINE;
3637 ifp->if_flags |= XFS_IFEXTENTS;
3638 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3639 flags |= XFS_ILOG_CORE;
3646 * Search the extent records for the entry containing block bno.
3647 * If bno lies in a hole, point to the next entry. If bno lies
3648 * past eof, *eofp will be set, and *prevp will contain the last
3649 * entry (null if none). Else, *lastxp will be set to the index
3650 * of the found entry; *gotp will contain the entry.
3652 xfs_bmbt_rec_t * /* pointer to found extent entry */
3653 xfs_bmap_search_multi_extents(
3654 xfs_ifork_t *ifp, /* inode fork pointer */
3655 xfs_fileoff_t bno, /* block number searched for */
3656 int *eofp, /* out: end of file found */
3657 xfs_extnum_t *lastxp, /* out: last extent index */
3658 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3659 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3661 xfs_bmbt_rec_t *ep; /* extent record pointer */
3662 xfs_extnum_t lastx; /* last extent index */
3665 * Initialize the extent entry structure to catch access to
3666 * uninitialized br_startblock field.
3668 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3669 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3670 gotp->br_state = XFS_EXT_INVALID;
3672 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3674 gotp->br_startblock = 0xffffa5a5;
3676 prevp->br_startoff = NULLFILEOFF;
3678 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3680 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3682 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3683 xfs_bmbt_get_all(ep, gotp);
3697 * Search the extents list for the inode, for the extent containing bno.
3698 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3699 * *eofp will be set, and *prevp will contain the last entry (null if none).
3700 * Else, *lastxp will be set to the index of the found
3701 * entry; *gotp will contain the entry.
3703 STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
3704 xfs_bmap_search_extents(
3705 xfs_inode_t *ip, /* incore inode pointer */
3706 xfs_fileoff_t bno, /* block number searched for */
3707 int whichfork, /* data or attr fork */
3708 int *eofp, /* out: end of file found */
3709 xfs_extnum_t *lastxp, /* out: last extent index */
3710 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3711 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3713 xfs_ifork_t *ifp; /* inode fork pointer */
3714 xfs_bmbt_rec_t *ep; /* extent record pointer */
3715 int rt; /* realtime flag */
3717 XFS_STATS_INC(xs_look_exlist);
3718 ifp = XFS_IFORK_PTR(ip, whichfork);
3720 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3722 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
3723 if (unlikely(!rt && !gotp->br_startblock && (*lastxp != NULLEXTNUM))) {
3724 cmn_err(CE_PANIC,"Access to block zero: fs: <%s> inode: %lld "
3725 "start_block : %llx start_off : %llx blkcnt : %llx "
3726 "extent-state : %x \n",
3727 (ip->i_mount)->m_fsname, (long long)ip->i_ino,
3728 (unsigned long long)gotp->br_startblock,
3729 (unsigned long long)gotp->br_startoff,
3730 (unsigned long long)gotp->br_blockcount,
3737 #ifdef XFS_BMAP_TRACE
3738 ktrace_t *xfs_bmap_trace_buf;
3741 * Add a bmap trace buffer entry. Base routine for the others.
3744 xfs_bmap_trace_addentry(
3745 int opcode, /* operation */
3746 char *fname, /* function name */
3747 char *desc, /* operation description */
3748 xfs_inode_t *ip, /* incore inode pointer */
3749 xfs_extnum_t idx, /* index of entry(ies) */
3750 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
3751 xfs_bmbt_rec_t *r1, /* first record */
3752 xfs_bmbt_rec_t *r2, /* second record or null */
3753 int whichfork) /* data or attr fork */
3757 ASSERT(cnt == 1 || cnt == 2);
3762 memset(&tr2, 0, sizeof(tr2));
3765 ktrace_enter(xfs_bmap_trace_buf,
3766 (void *)(__psint_t)(opcode | (whichfork << 16)),
3767 (void *)fname, (void *)desc, (void *)ip,
3768 (void *)(__psint_t)idx,
3769 (void *)(__psint_t)cnt,
3770 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3771 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3772 (void *)(__psunsigned_t)(r1->l0 >> 32),
3773 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3774 (void *)(__psunsigned_t)(r1->l1 >> 32),
3775 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3776 (void *)(__psunsigned_t)(r2->l0 >> 32),
3777 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3778 (void *)(__psunsigned_t)(r2->l1 >> 32),
3779 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3781 ASSERT(ip->i_xtrace);
3782 ktrace_enter(ip->i_xtrace,
3783 (void *)(__psint_t)(opcode | (whichfork << 16)),
3784 (void *)fname, (void *)desc, (void *)ip,
3785 (void *)(__psint_t)idx,
3786 (void *)(__psint_t)cnt,
3787 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3788 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3789 (void *)(__psunsigned_t)(r1->l0 >> 32),
3790 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3791 (void *)(__psunsigned_t)(r1->l1 >> 32),
3792 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3793 (void *)(__psunsigned_t)(r2->l0 >> 32),
3794 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3795 (void *)(__psunsigned_t)(r2->l1 >> 32),
3796 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3801 * Add bmap trace entry prior to a call to xfs_iext_remove.
3804 xfs_bmap_trace_delete(
3805 char *fname, /* function name */
3806 char *desc, /* operation description */
3807 xfs_inode_t *ip, /* incore inode pointer */
3808 xfs_extnum_t idx, /* index of entry(entries) deleted */
3809 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3810 int whichfork) /* data or attr fork */
3812 xfs_ifork_t *ifp; /* inode fork pointer */
3814 ifp = XFS_IFORK_PTR(ip, whichfork);
3815 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
3816 cnt, xfs_iext_get_ext(ifp, idx),
3817 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
3822 * Add bmap trace entry prior to a call to xfs_iext_insert, or
3823 * reading in the extents list from the disk (in the btree).
3826 xfs_bmap_trace_insert(
3827 char *fname, /* function name */
3828 char *desc, /* operation description */
3829 xfs_inode_t *ip, /* incore inode pointer */
3830 xfs_extnum_t idx, /* index of entry(entries) inserted */
3831 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3832 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3833 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3834 int whichfork) /* data or attr fork */
3836 xfs_bmbt_rec_t tr1; /* compressed record 1 */
3837 xfs_bmbt_rec_t tr2; /* compressed record 2 if needed */
3839 xfs_bmbt_set_all(&tr1, r1);
3842 xfs_bmbt_set_all(&tr2, r2);
3847 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3848 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3852 * Add bmap trace entry after updating an extent record in place.
3855 xfs_bmap_trace_post_update(
3856 char *fname, /* function name */
3857 char *desc, /* operation description */
3858 xfs_inode_t *ip, /* incore inode pointer */
3859 xfs_extnum_t idx, /* index of entry updated */
3860 int whichfork) /* data or attr fork */
3862 xfs_ifork_t *ifp; /* inode fork pointer */
3864 ifp = XFS_IFORK_PTR(ip, whichfork);
3865 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
3866 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3870 * Add bmap trace entry prior to updating an extent record in place.
3873 xfs_bmap_trace_pre_update(
3874 char *fname, /* function name */
3875 char *desc, /* operation description */
3876 xfs_inode_t *ip, /* incore inode pointer */
3877 xfs_extnum_t idx, /* index of entry to be updated */
3878 int whichfork) /* data or attr fork */
3880 xfs_ifork_t *ifp; /* inode fork pointer */
3882 ifp = XFS_IFORK_PTR(ip, whichfork);
3883 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
3884 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3886 #endif /* XFS_BMAP_TRACE */
3889 * Compute the worst-case number of indirect blocks that will be used
3890 * for ip's delayed extent of length "len".
3892 STATIC xfs_filblks_t
3893 xfs_bmap_worst_indlen(
3894 xfs_inode_t *ip, /* incore inode pointer */
3895 xfs_filblks_t len) /* delayed extent length */
3897 int level; /* btree level number */
3898 int maxrecs; /* maximum record count at this level */
3899 xfs_mount_t *mp; /* mount structure */
3900 xfs_filblks_t rval; /* return value */
3903 maxrecs = mp->m_bmap_dmxr[0];
3904 for (level = 0, rval = 0;
3905 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3908 do_div(len, maxrecs);
3911 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3914 maxrecs = mp->m_bmap_dmxr[1];
3919 #if defined(XFS_RW_TRACE)
3928 if (ip->i_rwtrace == NULL)
3930 ktrace_enter(ip->i_rwtrace,
3931 (void *)(__psint_t)XFS_BUNMAP,
3933 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3934 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3935 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3936 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3937 (void *)(__psint_t)len,
3938 (void *)(__psint_t)flags,
3939 (void *)current_cpu(),
3951 * Convert inode from non-attributed to attributed.
3952 * Must not be in a transaction, ip must not be locked.
3954 int /* error code */
3955 xfs_bmap_add_attrfork(
3956 xfs_inode_t *ip, /* incore inode pointer */
3957 int size, /* space new attribute needs */
3958 int rsvd) /* xact may use reserved blks */
3960 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3961 xfs_bmap_free_t flist; /* freed extent records */
3962 xfs_mount_t *mp; /* mount structure */
3963 xfs_trans_t *tp; /* transaction pointer */
3964 unsigned long s; /* spinlock spl value */
3965 int blks; /* space reservation */
3966 int version = 1; /* superblock attr version */
3967 int committed; /* xaction was committed */
3968 int logflags; /* logging flags */
3969 int error; /* error return value */
3971 ASSERT(XFS_IFORK_Q(ip) == 0);
3972 ASSERT(ip->i_df.if_ext_max ==
3973 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3976 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3977 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3978 blks = XFS_ADDAFORK_SPACE_RES(mp);
3980 tp->t_flags |= XFS_TRANS_RESERVE;
3981 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3982 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3984 xfs_ilock(ip, XFS_ILOCK_EXCL);
3985 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
3986 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3987 XFS_QMOPT_RES_REGBLKS);
3989 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3990 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3993 if (XFS_IFORK_Q(ip))
3995 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3997 * For inodes coming from pre-6.2 filesystems.
3999 ASSERT(ip->i_d.di_aformat == 0);
4000 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
4002 ASSERT(ip->i_d.di_anextents == 0);
4003 VN_HOLD(XFS_ITOV(ip));
4004 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4005 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4006 switch (ip->i_d.di_format) {
4007 case XFS_DINODE_FMT_DEV:
4008 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
4010 case XFS_DINODE_FMT_UUID:
4011 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
4013 case XFS_DINODE_FMT_LOCAL:
4014 case XFS_DINODE_FMT_EXTENTS:
4015 case XFS_DINODE_FMT_BTREE:
4016 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
4017 if (!ip->i_d.di_forkoff)
4018 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
4019 else if (mp->m_flags & XFS_MOUNT_ATTR2)
4024 error = XFS_ERROR(EINVAL);
4027 ip->i_df.if_ext_max =
4028 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4029 ASSERT(ip->i_afp == NULL);
4030 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
4031 ip->i_afp->if_ext_max =
4032 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4033 ip->i_afp->if_flags = XFS_IFEXTENTS;
4035 XFS_BMAP_INIT(&flist, &firstblock);
4036 switch (ip->i_d.di_format) {
4037 case XFS_DINODE_FMT_LOCAL:
4038 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
4041 case XFS_DINODE_FMT_EXTENTS:
4042 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
4045 case XFS_DINODE_FMT_BTREE:
4046 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
4054 xfs_trans_log_inode(tp, ip, logflags);
4057 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb) ||
4058 (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2)) {
4059 __int64_t sbfields = 0;
4061 s = XFS_SB_LOCK(mp);
4062 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) {
4063 XFS_SB_VERSION_ADDATTR(&mp->m_sb);
4064 sbfields |= XFS_SB_VERSIONNUM;
4066 if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2) {
4067 XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
4068 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
4071 XFS_SB_UNLOCK(mp, s);
4072 xfs_mod_sb(tp, sbfields);
4074 XFS_SB_UNLOCK(mp, s);
4076 if ((error = xfs_bmap_finish(&tp, &flist, firstblock, &committed)))
4078 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES, NULL);
4079 ASSERT(ip->i_df.if_ext_max ==
4080 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4083 xfs_bmap_cancel(&flist);
4085 ASSERT(ismrlocked(&ip->i_lock,MR_UPDATE));
4086 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4088 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4089 ASSERT(ip->i_df.if_ext_max ==
4090 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4095 * Add the extent to the list of extents to be free at transaction end.
4096 * The list is maintained sorted (by block number).
4101 xfs_fsblock_t bno, /* fs block number of extent */
4102 xfs_filblks_t len, /* length of extent */
4103 xfs_bmap_free_t *flist, /* list of extents */
4104 xfs_mount_t *mp) /* mount point structure */
4106 xfs_bmap_free_item_t *cur; /* current (next) element */
4107 xfs_bmap_free_item_t *new; /* new element */
4108 xfs_bmap_free_item_t *prev; /* previous element */
4110 xfs_agnumber_t agno;
4111 xfs_agblock_t agbno;
4113 ASSERT(bno != NULLFSBLOCK);
4115 ASSERT(len <= MAXEXTLEN);
4116 ASSERT(!ISNULLSTARTBLOCK(bno));
4117 agno = XFS_FSB_TO_AGNO(mp, bno);
4118 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4119 ASSERT(agno < mp->m_sb.sb_agcount);
4120 ASSERT(agbno < mp->m_sb.sb_agblocks);
4121 ASSERT(len < mp->m_sb.sb_agblocks);
4122 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4124 ASSERT(xfs_bmap_free_item_zone != NULL);
4125 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4126 new->xbfi_startblock = bno;
4127 new->xbfi_blockcount = (xfs_extlen_t)len;
4128 for (prev = NULL, cur = flist->xbf_first;
4130 prev = cur, cur = cur->xbfi_next) {
4131 if (cur->xbfi_startblock >= bno)
4135 prev->xbfi_next = new;
4137 flist->xbf_first = new;
4138 new->xbfi_next = cur;
4143 * Compute and fill in the value of the maximum depth of a bmap btree
4144 * in this filesystem. Done once, during mount.
4147 xfs_bmap_compute_maxlevels(
4148 xfs_mount_t *mp, /* file system mount structure */
4149 int whichfork) /* data or attr fork */
4151 int level; /* btree level */
4152 uint maxblocks; /* max blocks at this level */
4153 uint maxleafents; /* max leaf entries possible */
4154 int maxrootrecs; /* max records in root block */
4155 int minleafrecs; /* min records in leaf block */
4156 int minnoderecs; /* min records in node block */
4157 int sz; /* root block size */
4160 * The maximum number of extents in a file, hence the maximum
4161 * number of leaf entries, is controlled by the type of di_nextents
4162 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4163 * (a signed 16-bit number, xfs_aextnum_t).
4165 if (whichfork == XFS_DATA_FORK) {
4166 maxleafents = MAXEXTNUM;
4167 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4168 XFS_BMDR_SPACE_CALC(MINDBTPTRS) : mp->m_attroffset;
4170 maxleafents = MAXAEXTNUM;
4171 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4172 XFS_BMDR_SPACE_CALC(MINABTPTRS) :
4173 mp->m_sb.sb_inodesize - mp->m_attroffset;
4175 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
4176 minleafrecs = mp->m_bmap_dmnr[0];
4177 minnoderecs = mp->m_bmap_dmnr[1];
4178 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4179 for (level = 1; maxblocks > 1; level++) {
4180 if (maxblocks <= maxrootrecs)
4183 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4185 mp->m_bm_maxlevels[whichfork] = level;
4189 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4190 * caller. Frees all the extents that need freeing, which must be done
4191 * last due to locking considerations. We never free any extents in
4192 * the first transaction. This is to allow the caller to make the first
4193 * transaction a synchronous one so that the pointers to the data being
4194 * broken in this transaction will be permanent before the data is actually
4195 * freed. This is necessary to prevent blocks from being reallocated
4196 * and written to before the free and reallocation are actually permanent.
4197 * We do not just make the first transaction synchronous here, because
4198 * there are more efficient ways to gain the same protection in some cases
4199 * (see the file truncation code).
4201 * Return 1 if the given transaction was committed and a new one
4202 * started, and 0 otherwise in the committed parameter.
4207 xfs_trans_t **tp, /* transaction pointer addr */
4208 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4209 xfs_fsblock_t firstblock, /* controlled ag for allocs */
4210 int *committed) /* xact committed or not */
4212 xfs_efd_log_item_t *efd; /* extent free data */
4213 xfs_efi_log_item_t *efi; /* extent free intention */
4214 int error; /* error return value */
4215 xfs_bmap_free_item_t *free; /* free extent item */
4216 unsigned int logres; /* new log reservation */
4217 unsigned int logcount; /* new log count */
4218 xfs_mount_t *mp; /* filesystem mount structure */
4219 xfs_bmap_free_item_t *next; /* next item on free list */
4220 xfs_trans_t *ntp; /* new transaction pointer */
4222 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4223 if (flist->xbf_count == 0) {
4228 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4229 for (free = flist->xbf_first; free; free = free->xbfi_next)
4230 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4231 free->xbfi_blockcount);
4232 logres = ntp->t_log_res;
4233 logcount = ntp->t_log_count;
4234 ntp = xfs_trans_dup(*tp);
4235 error = xfs_trans_commit(*tp, 0, NULL);
4239 * We have a new transaction, so we should return committed=1,
4240 * even though we're returning an error.
4245 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4248 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4249 for (free = flist->xbf_first; free != NULL; free = next) {
4250 next = free->xbfi_next;
4251 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4252 free->xbfi_blockcount))) {
4254 * The bmap free list will be cleaned up at a
4255 * higher level. The EFI will be canceled when
4256 * this transaction is aborted.
4257 * Need to force shutdown here to make sure it
4258 * happens, since this transaction may not be
4262 if (!XFS_FORCED_SHUTDOWN(mp))
4263 xfs_force_shutdown(mp,
4264 (error == EFSCORRUPTED) ?
4265 XFS_CORRUPT_INCORE :
4266 XFS_METADATA_IO_ERROR);
4269 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4270 free->xbfi_blockcount);
4271 xfs_bmap_del_free(flist, NULL, free);
4277 * Free up any items left in the list.
4281 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4283 xfs_bmap_free_item_t *free; /* free list item */
4284 xfs_bmap_free_item_t *next;
4286 if (flist->xbf_count == 0)
4288 ASSERT(flist->xbf_first != NULL);
4289 for (free = flist->xbf_first; free; free = next) {
4290 next = free->xbfi_next;
4291 xfs_bmap_del_free(flist, NULL, free);
4293 ASSERT(flist->xbf_count == 0);
4297 * Returns the file-relative block number of the first unused block(s)
4298 * in the file with at least "len" logically contiguous blocks free.
4299 * This is the lowest-address hole if the file has holes, else the first block
4300 * past the end of file.
4301 * Return 0 if the file is currently local (in-inode).
4304 xfs_bmap_first_unused(
4305 xfs_trans_t *tp, /* transaction pointer */
4306 xfs_inode_t *ip, /* incore inode */
4307 xfs_extlen_t len, /* size of hole to find */
4308 xfs_fileoff_t *first_unused, /* unused block */
4309 int whichfork) /* data or attr fork */
4311 xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
4312 int error; /* error return value */
4313 int idx; /* extent record index */
4314 xfs_ifork_t *ifp; /* inode fork pointer */
4315 xfs_fileoff_t lastaddr; /* last block number seen */
4316 xfs_fileoff_t lowest; /* lowest useful block */
4317 xfs_fileoff_t max; /* starting useful block */
4318 xfs_fileoff_t off; /* offset for this block */
4319 xfs_extnum_t nextents; /* number of extent entries */
4321 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4322 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4323 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4324 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4328 ifp = XFS_IFORK_PTR(ip, whichfork);
4329 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4330 (error = xfs_iread_extents(tp, ip, whichfork)))
4332 lowest = *first_unused;
4333 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4334 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
4335 ep = xfs_iext_get_ext(ifp, idx);
4336 off = xfs_bmbt_get_startoff(ep);
4338 * See if the hole before this extent will work.
4340 if (off >= lowest + len && off - max >= len) {
4341 *first_unused = max;
4344 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4345 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4347 *first_unused = max;
4352 * Returns the file-relative block number of the last block + 1 before
4353 * last_block (input value) in the file.
4354 * This is not based on i_size, it is based on the extent records.
4355 * Returns 0 for local files, as they do not have extent records.
4358 xfs_bmap_last_before(
4359 xfs_trans_t *tp, /* transaction pointer */
4360 xfs_inode_t *ip, /* incore inode */
4361 xfs_fileoff_t *last_block, /* last block */
4362 int whichfork) /* data or attr fork */
4364 xfs_fileoff_t bno; /* input file offset */
4365 int eof; /* hit end of file */
4366 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4367 int error; /* error return value */
4368 xfs_bmbt_irec_t got; /* current extent value */
4369 xfs_ifork_t *ifp; /* inode fork pointer */
4370 xfs_extnum_t lastx; /* last extent used */
4371 xfs_bmbt_irec_t prev; /* previous extent value */
4373 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4374 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4375 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4376 return XFS_ERROR(EIO);
4377 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4381 ifp = XFS_IFORK_PTR(ip, whichfork);
4382 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4383 (error = xfs_iread_extents(tp, ip, whichfork)))
4385 bno = *last_block - 1;
4386 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4388 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4389 if (prev.br_startoff == NULLFILEOFF)
4392 *last_block = prev.br_startoff + prev.br_blockcount;
4395 * Otherwise *last_block is already the right answer.
4401 * Returns the file-relative block number of the first block past eof in
4402 * the file. This is not based on i_size, it is based on the extent records.
4403 * Returns 0 for local files, as they do not have extent records.
4406 xfs_bmap_last_offset(
4407 xfs_trans_t *tp, /* transaction pointer */
4408 xfs_inode_t *ip, /* incore inode */
4409 xfs_fileoff_t *last_block, /* last block */
4410 int whichfork) /* data or attr fork */
4412 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4413 int error; /* error return value */
4414 xfs_ifork_t *ifp; /* inode fork pointer */
4415 xfs_extnum_t nextents; /* number of extent entries */
4417 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4418 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4419 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4420 return XFS_ERROR(EIO);
4421 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4425 ifp = XFS_IFORK_PTR(ip, whichfork);
4426 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4427 (error = xfs_iread_extents(tp, ip, whichfork)))
4429 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4434 ep = xfs_iext_get_ext(ifp, nextents - 1);
4435 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4440 * Returns whether the selected fork of the inode has exactly one
4441 * block or not. For the data fork we check this matches di_size,
4442 * implying the file's range is 0..bsize-1.
4444 int /* 1=>1 block, 0=>otherwise */
4446 xfs_inode_t *ip, /* incore inode */
4447 int whichfork) /* data or attr fork */
4449 xfs_bmbt_rec_t *ep; /* ptr to fork's extent */
4450 xfs_ifork_t *ifp; /* inode fork pointer */
4451 int rval; /* return value */
4452 xfs_bmbt_irec_t s; /* internal version of extent */
4455 if (whichfork == XFS_DATA_FORK)
4456 return ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize;
4458 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4460 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4462 ifp = XFS_IFORK_PTR(ip, whichfork);
4463 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4464 ep = xfs_iext_get_ext(ifp, 0);
4465 xfs_bmbt_get_all(ep, &s);
4466 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4467 if (rval && whichfork == XFS_DATA_FORK)
4468 ASSERT(ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4473 * Read in the extents to if_extents.
4474 * All inode fields are set up by caller, we just traverse the btree
4475 * and copy the records in. If the file system cannot contain unwritten
4476 * extents, the records are checked for no "state" flags.
4479 xfs_bmap_read_extents(
4480 xfs_trans_t *tp, /* transaction pointer */
4481 xfs_inode_t *ip, /* incore inode */
4482 int whichfork) /* data or attr fork */
4484 xfs_bmbt_block_t *block; /* current btree block */
4485 xfs_fsblock_t bno; /* block # of "block" */
4486 xfs_buf_t *bp; /* buffer for "block" */
4487 int error; /* error return value */
4488 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4489 #ifdef XFS_BMAP_TRACE
4490 static char fname[] = "xfs_bmap_read_extents";
4492 xfs_extnum_t i, j; /* index into the extents list */
4493 xfs_ifork_t *ifp; /* fork structure */
4494 int level; /* btree level, for checking */
4495 xfs_mount_t *mp; /* file system mount structure */
4496 xfs_bmbt_ptr_t *pp; /* pointer to block address */
4498 xfs_extnum_t room; /* number of entries there's room for */
4502 ifp = XFS_IFORK_PTR(ip, whichfork);
4503 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4504 XFS_EXTFMT_INODE(ip);
4505 block = ifp->if_broot;
4507 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4509 level = be16_to_cpu(block->bb_level);
4511 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
4512 ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
4513 ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
4514 ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
4515 bno = INT_GET(*pp, ARCH_CONVERT);
4517 * Go down the tree until leaf level is reached, following the first
4518 * pointer (leftmost) at each level.
4520 while (level-- > 0) {
4521 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4522 XFS_BMAP_BTREE_REF)))
4524 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4525 XFS_WANT_CORRUPTED_GOTO(
4526 XFS_BMAP_SANITY_CHECK(mp, block, level),
4530 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
4531 1, mp->m_bmap_dmxr[1]);
4532 XFS_WANT_CORRUPTED_GOTO(
4533 XFS_FSB_SANITY_CHECK(mp, INT_GET(*pp, ARCH_CONVERT)),
4535 bno = INT_GET(*pp, ARCH_CONVERT);
4536 xfs_trans_brelse(tp, bp);
4539 * Here with bp and block set to the leftmost leaf node in the tree.
4541 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4544 * Loop over all leaf nodes. Copy information to the extent records.
4547 xfs_bmbt_rec_t *frp, *trp;
4548 xfs_fsblock_t nextbno;
4549 xfs_extnum_t num_recs;
4553 num_recs = be16_to_cpu(block->bb_numrecs);
4554 if (unlikely(i + num_recs > room)) {
4555 ASSERT(i + num_recs <= room);
4556 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4557 "corrupt dinode %Lu, (btree extents).",
4558 (unsigned long long) ip->i_ino);
4559 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4564 XFS_WANT_CORRUPTED_GOTO(
4565 XFS_BMAP_SANITY_CHECK(mp, block, 0),
4568 * Read-ahead the next leaf block, if any.
4570 nextbno = be64_to_cpu(block->bb_rightsib);
4571 if (nextbno != NULLFSBLOCK)
4572 xfs_btree_reada_bufl(mp, nextbno, 1);
4574 * Copy records into the extent records.
4576 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
4577 block, 1, mp->m_bmap_dmxr[0]);
4579 for (j = 0; j < num_recs; j++, i++, frp++) {
4580 trp = xfs_iext_get_ext(ifp, i);
4581 trp->l0 = INT_GET(frp->l0, ARCH_CONVERT);
4582 trp->l1 = INT_GET(frp->l1, ARCH_CONVERT);
4584 if (exntf == XFS_EXTFMT_NOSTATE) {
4586 * Check all attribute bmap btree records and
4587 * any "older" data bmap btree records for a
4588 * set bit in the "extent flag" position.
4590 if (unlikely(xfs_check_nostate_extents(ifp,
4591 start, num_recs))) {
4592 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4598 xfs_trans_brelse(tp, bp);
4601 * If we've reached the end, stop.
4603 if (bno == NULLFSBLOCK)
4605 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4606 XFS_BMAP_BTREE_REF)))
4608 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4610 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4611 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4612 xfs_bmap_trace_exlist(fname, ip, i, whichfork);
4615 xfs_trans_brelse(tp, bp);
4616 return XFS_ERROR(EFSCORRUPTED);
4619 #ifdef XFS_BMAP_TRACE
4621 * Add bmap trace insert entries for all the contents of the extent records.
4624 xfs_bmap_trace_exlist(
4625 char *fname, /* function name */
4626 xfs_inode_t *ip, /* incore inode pointer */
4627 xfs_extnum_t cnt, /* count of entries in the list */
4628 int whichfork) /* data or attr fork */
4630 xfs_bmbt_rec_t *ep; /* current extent record */
4631 xfs_extnum_t idx; /* extent record index */
4632 xfs_ifork_t *ifp; /* inode fork pointer */
4633 xfs_bmbt_irec_t s; /* file extent record */
4635 ifp = XFS_IFORK_PTR(ip, whichfork);
4636 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4637 for (idx = 0; idx < cnt; idx++) {
4638 ep = xfs_iext_get_ext(ifp, idx);
4639 xfs_bmbt_get_all(ep, &s);
4640 xfs_bmap_trace_insert(fname, "exlist", ip, idx, 1, &s, NULL,
4648 * Validate that the bmbt_irecs being returned from bmapi are valid
4649 * given the callers original parameters. Specifically check the
4650 * ranges of the returned irecs to ensure that they only extent beyond
4651 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4654 xfs_bmap_validate_ret(
4658 xfs_bmbt_irec_t *mval,
4662 int i; /* index to map values */
4664 ASSERT(ret_nmap <= nmap);
4666 for (i = 0; i < ret_nmap; i++) {
4667 ASSERT(mval[i].br_blockcount > 0);
4668 if (!(flags & XFS_BMAPI_ENTIRE)) {
4669 ASSERT(mval[i].br_startoff >= bno);
4670 ASSERT(mval[i].br_blockcount <= len);
4671 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4674 ASSERT(mval[i].br_startoff < bno + len);
4675 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4679 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4680 mval[i].br_startoff);
4681 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4682 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4683 mval[i].br_startblock != HOLESTARTBLOCK);
4684 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4685 mval[i].br_state == XFS_EXT_UNWRITTEN);
4692 * Map file blocks to filesystem blocks.
4693 * File range is given by the bno/len pair.
4694 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4695 * into a hole or past eof.
4696 * Only allocates blocks from a single allocation group,
4697 * to avoid locking problems.
4698 * The returned value in "firstblock" from the first call in a transaction
4699 * must be remembered and presented to subsequent calls in "firstblock".
4700 * An upper bound for the number of blocks to be allocated is supplied to
4701 * the first call in "total"; if no allocation group has that many free
4702 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4706 xfs_trans_t *tp, /* transaction pointer */
4707 xfs_inode_t *ip, /* incore inode */
4708 xfs_fileoff_t bno, /* starting file offs. mapped */
4709 xfs_filblks_t len, /* length to map in file */
4710 int flags, /* XFS_BMAPI_... */
4711 xfs_fsblock_t *firstblock, /* first allocated block
4712 controls a.g. for allocs */
4713 xfs_extlen_t total, /* total blocks needed */
4714 xfs_bmbt_irec_t *mval, /* output: map values */
4715 int *nmap, /* i/o: mval size/count */
4716 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4717 xfs_extdelta_t *delta) /* o: change made to incore extents */
4719 xfs_fsblock_t abno; /* allocated block number */
4720 xfs_extlen_t alen; /* allocated extent length */
4721 xfs_fileoff_t aoff; /* allocated file offset */
4722 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
4723 xfs_btree_cur_t *cur; /* bmap btree cursor */
4724 xfs_fileoff_t end; /* end of mapped file region */
4725 int eof; /* we've hit the end of extents */
4726 xfs_bmbt_rec_t *ep; /* extent record pointer */
4727 int error; /* error return */
4728 xfs_bmbt_irec_t got; /* current file extent record */
4729 xfs_ifork_t *ifp; /* inode fork pointer */
4730 xfs_extlen_t indlen; /* indirect blocks length */
4731 xfs_extnum_t lastx; /* last useful extent number */
4732 int logflags; /* flags for transaction logging */
4733 xfs_extlen_t minleft; /* min blocks left after allocation */
4734 xfs_extlen_t minlen; /* min allocation size */
4735 xfs_mount_t *mp; /* xfs mount structure */
4736 int n; /* current extent index */
4737 int nallocs; /* number of extents alloc\'d */
4738 xfs_extnum_t nextents; /* number of extents in file */
4739 xfs_fileoff_t obno; /* old block number (offset) */
4740 xfs_bmbt_irec_t prev; /* previous file extent record */
4741 int tmp_logflags; /* temp flags holder */
4742 int whichfork; /* data or attr fork */
4743 char inhole; /* current location is hole in file */
4744 char wasdelay; /* old extent was delayed */
4745 char wr; /* this is a write request */
4746 char rt; /* this is a realtime file */
4748 xfs_fileoff_t orig_bno; /* original block number value */
4749 int orig_flags; /* original flags arg value */
4750 xfs_filblks_t orig_len; /* original value of len arg */
4751 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4752 int orig_nmap; /* original value of *nmap */
4761 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4762 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4763 XFS_ATTR_FORK : XFS_DATA_FORK;
4765 if (unlikely(XFS_TEST_ERROR(
4766 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4767 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4768 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4769 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4770 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4771 return XFS_ERROR(EFSCORRUPTED);
4773 if (XFS_FORCED_SHUTDOWN(mp))
4774 return XFS_ERROR(EIO);
4775 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4776 ifp = XFS_IFORK_PTR(ip, whichfork);
4777 ASSERT(ifp->if_ext_max ==
4778 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4779 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4780 XFS_STATS_INC(xs_blk_mapw);
4782 XFS_STATS_INC(xs_blk_mapr);
4784 * IGSTATE flag is used to combine extents which
4785 * differ only due to the state of the extents.
4786 * This technique is used from xfs_getbmap()
4787 * when the caller does not wish to see the
4788 * separation (which is the default).
4790 * This technique is also used when writing a
4791 * buffer which has been partially written,
4792 * (usually by being flushed during a chunkread),
4793 * to ensure one write takes place. This also
4794 * prevents a change in the xfs inode extents at
4795 * this time, intentionally. This change occurs
4796 * on completion of the write operation, in
4797 * xfs_strat_comp(), where the xfs_bmapi() call
4798 * is transactioned, and the extents combined.
4800 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4801 wr = 0; /* no allocations are allowed */
4802 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
4806 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4808 if ((error = xfs_bmap_local_to_extents(tp, ip,
4809 firstblock, total, &logflags, whichfork)))
4812 if (wr && *firstblock == NULLFSBLOCK) {
4813 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4814 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4819 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4820 (error = xfs_iread_extents(tp, ip, whichfork)))
4822 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4824 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4830 delta->xed_startoff = NULLFILEOFF;
4831 delta->xed_blockcount = 0;
4833 while (bno < end && n < *nmap) {
4835 * Reading past eof, act as though there's a hole
4839 got.br_startoff = end;
4840 inhole = eof || got.br_startoff > bno;
4841 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
4842 ISNULLSTARTBLOCK(got.br_startblock);
4844 * First, deal with the hole before the allocated space
4845 * that we found, if any.
4847 if (wr && (inhole || wasdelay)) {
4849 * For the wasdelay case, we could also just
4850 * allocate the stuff asked for in this bmap call
4851 * but that wouldn't be as good.
4853 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
4854 alen = (xfs_extlen_t)got.br_blockcount;
4855 aoff = got.br_startoff;
4856 if (lastx != NULLEXTNUM && lastx) {
4857 ep = xfs_iext_get_ext(ifp, lastx - 1);
4858 xfs_bmbt_get_all(ep, &prev);
4860 } else if (wasdelay) {
4861 alen = (xfs_extlen_t)
4862 XFS_FILBLKS_MIN(len,
4864 got.br_blockcount) - bno);
4867 alen = (xfs_extlen_t)
4868 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4870 alen = (xfs_extlen_t)
4871 XFS_FILBLKS_MIN(alen,
4872 got.br_startoff - bno);
4875 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4876 if (flags & XFS_BMAPI_DELAY) {
4879 /* Figure out the extent size, adjust alen */
4881 if (!(extsz = ip->i_d.di_extsize))
4882 extsz = mp->m_sb.sb_rextsize;
4884 extsz = ip->i_d.di_extsize;
4887 error = xfs_bmap_extsize_align(mp,
4890 flags&XFS_BMAPI_DELAY,
4891 flags&XFS_BMAPI_CONVERT,
4897 extsz = alen / mp->m_sb.sb_rextsize;
4900 * Make a transaction-less quota reservation for
4901 * delayed allocation blocks. This number gets
4902 * adjusted later. We return if we haven't
4903 * allocated blocks already inside this loop.
4905 if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS(
4906 mp, NULL, ip, (long)alen, 0,
4907 rt ? XFS_QMOPT_RES_RTBLKS :
4908 XFS_QMOPT_RES_REGBLKS))) {
4911 ASSERT(cur == NULL);
4918 * Split changing sb for alen and indlen since
4919 * they could be coming from different places.
4921 indlen = (xfs_extlen_t)
4922 xfs_bmap_worst_indlen(ip, alen);
4926 error = xfs_mod_incore_sb(mp,
4929 XFS_BMAPI_RSVBLOCKS));
4931 error = xfs_mod_incore_sb(mp,
4934 XFS_BMAPI_RSVBLOCKS));
4937 error = xfs_mod_incore_sb(mp,
4940 XFS_BMAPI_RSVBLOCKS));
4942 xfs_mod_incore_sb(mp,
4945 XFS_BMAPI_RSVBLOCKS));
4947 xfs_mod_incore_sb(mp,
4950 XFS_BMAPI_RSVBLOCKS));
4954 if (XFS_IS_QUOTA_ON(mp))
4955 /* unreserve the blocks now */
4957 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4960 XFS_QMOPT_RES_RTBLKS :
4961 XFS_QMOPT_RES_REGBLKS);
4965 ip->i_delayed_blks += alen;
4966 abno = NULLSTARTBLOCK(indlen);
4969 * If first time, allocate and fill in
4970 * once-only bma fields.
4972 if (bma.ip == NULL) {
4980 /* Indicate if this is the first user data
4981 * in the file, or just any user data.
4983 if (!(flags & XFS_BMAPI_METADATA)) {
4984 bma.userdata = (aoff == 0) ?
4985 XFS_ALLOC_INITIAL_USER_DATA :
4989 * Fill in changeable bma fields.
4992 bma.firstblock = *firstblock;
4995 bma.conv = (flags & XFS_BMAPI_CONVERT) != 0;
4996 bma.wasdel = wasdelay;
4997 bma.minlen = minlen;
4998 bma.low = flist->xbf_low;
4999 bma.minleft = minleft;
5001 * Only want to do the alignment at the
5002 * eof if it is userdata and allocation length
5003 * is larger than a stripe unit.
5005 if (mp->m_dalign && alen >= mp->m_dalign &&
5006 (!(flags & XFS_BMAPI_METADATA)) &&
5007 (whichfork == XFS_DATA_FORK)) {
5008 if ((error = xfs_bmap_isaeof(ip, aoff,
5009 whichfork, &bma.aeof)))
5016 if ((error = xfs_bmap_alloc(&bma)))
5019 * Copy out result fields.
5022 if ((flist->xbf_low = bma.low))
5026 ASSERT(*firstblock == NULLFSBLOCK ||
5027 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5028 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
5030 XFS_FSB_TO_AGNO(mp, *firstblock) <
5031 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
5032 *firstblock = bma.firstblock;
5034 cur->bc_private.b.firstblock =
5036 if (abno == NULLFSBLOCK)
5038 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5039 cur = xfs_btree_init_cursor(mp,
5040 tp, NULL, 0, XFS_BTNUM_BMAP,
5042 cur->bc_private.b.firstblock =
5044 cur->bc_private.b.flist = flist;
5047 * Bump the number of extents we've allocated
5053 cur->bc_private.b.flags =
5054 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
5055 got.br_startoff = aoff;
5056 got.br_startblock = abno;
5057 got.br_blockcount = alen;
5058 got.br_state = XFS_EXT_NORM; /* assume normal */
5060 * Determine state of extent, and the filesystem.
5061 * A wasdelay extent has been initialized, so
5062 * shouldn't be flagged as unwritten.
5064 if (wr && XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5065 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5066 got.br_state = XFS_EXT_UNWRITTEN;
5068 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
5069 firstblock, flist, &tmp_logflags, delta,
5070 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5071 logflags |= tmp_logflags;
5074 lastx = ifp->if_lastex;
5075 ep = xfs_iext_get_ext(ifp, lastx);
5076 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5077 xfs_bmbt_get_all(ep, &got);
5078 ASSERT(got.br_startoff <= aoff);
5079 ASSERT(got.br_startoff + got.br_blockcount >=
5082 if (flags & XFS_BMAPI_DELAY) {
5083 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
5084 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
5086 ASSERT(got.br_state == XFS_EXT_NORM ||
5087 got.br_state == XFS_EXT_UNWRITTEN);
5090 * Fall down into the found allocated space case.
5092 } else if (inhole) {
5094 * Reading in a hole.
5096 mval->br_startoff = bno;
5097 mval->br_startblock = HOLESTARTBLOCK;
5098 mval->br_blockcount =
5099 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5100 mval->br_state = XFS_EXT_NORM;
5101 bno += mval->br_blockcount;
5102 len -= mval->br_blockcount;
5108 * Then deal with the allocated space we found.
5111 if (!(flags & XFS_BMAPI_ENTIRE) &&
5112 (got.br_startoff + got.br_blockcount > obno)) {
5115 ASSERT((bno >= obno) || (n == 0));
5117 mval->br_startoff = bno;
5118 if (ISNULLSTARTBLOCK(got.br_startblock)) {
5119 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5120 mval->br_startblock = DELAYSTARTBLOCK;
5122 mval->br_startblock =
5124 (bno - got.br_startoff);
5126 * Return the minimum of what we got and what we
5127 * asked for for the length. We can use the len
5128 * variable here because it is modified below
5129 * and we could have been there before coming
5130 * here if the first part of the allocation
5131 * didn't overlap what was asked for.
5133 mval->br_blockcount =
5134 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5135 (bno - got.br_startoff));
5136 mval->br_state = got.br_state;
5137 ASSERT(mval->br_blockcount <= len);
5140 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
5141 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5142 mval->br_startblock = DELAYSTARTBLOCK;
5147 * Check if writing previously allocated but
5148 * unwritten extents.
5150 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5151 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5153 * Modify (by adding) the state flag, if writing.
5155 ASSERT(mval->br_blockcount <= len);
5156 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5157 cur = xfs_btree_init_cursor(mp,
5158 tp, NULL, 0, XFS_BTNUM_BMAP,
5160 cur->bc_private.b.firstblock =
5162 cur->bc_private.b.flist = flist;
5164 mval->br_state = XFS_EXT_NORM;
5165 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
5166 firstblock, flist, &tmp_logflags, delta,
5167 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5168 logflags |= tmp_logflags;
5171 lastx = ifp->if_lastex;
5172 ep = xfs_iext_get_ext(ifp, lastx);
5173 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5174 xfs_bmbt_get_all(ep, &got);
5176 * We may have combined previously unwritten
5177 * space with written space, so generate
5180 if (mval->br_blockcount < len)
5184 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5185 ((mval->br_startoff + mval->br_blockcount) <= end));
5186 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5187 (mval->br_blockcount <= len) ||
5188 (mval->br_startoff < obno));
5189 bno = mval->br_startoff + mval->br_blockcount;
5191 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5192 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5193 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5194 ASSERT(mval->br_state == mval[-1].br_state);
5195 mval[-1].br_blockcount = mval->br_blockcount;
5196 mval[-1].br_state = mval->br_state;
5197 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5198 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5199 mval[-1].br_startblock != HOLESTARTBLOCK &&
5200 mval->br_startblock ==
5201 mval[-1].br_startblock + mval[-1].br_blockcount &&
5202 ((flags & XFS_BMAPI_IGSTATE) ||
5203 mval[-1].br_state == mval->br_state)) {
5204 ASSERT(mval->br_startoff ==
5205 mval[-1].br_startoff + mval[-1].br_blockcount);
5206 mval[-1].br_blockcount += mval->br_blockcount;
5208 mval->br_startblock == DELAYSTARTBLOCK &&
5209 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5210 mval->br_startoff ==
5211 mval[-1].br_startoff + mval[-1].br_blockcount) {
5212 mval[-1].br_blockcount += mval->br_blockcount;
5213 mval[-1].br_state = mval->br_state;
5214 } else if (!((n == 0) &&
5215 ((mval->br_startoff + mval->br_blockcount) <=
5221 * If we're done, stop now. Stop when we've allocated
5222 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5223 * the transaction may get too big.
5225 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5228 * Else go on to the next record.
5230 ep = xfs_iext_get_ext(ifp, ++lastx);
5231 if (lastx >= nextents) {
5235 xfs_bmbt_get_all(ep, &got);
5237 ifp->if_lastex = lastx;
5240 * Transform from btree to extents, give it cur.
5242 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5243 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5245 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5246 &tmp_logflags, whichfork);
5247 logflags |= tmp_logflags;
5251 ASSERT(ifp->if_ext_max ==
5252 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5253 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5254 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5256 if (delta && delta->xed_startoff != NULLFILEOFF) {
5257 /* A change was actually made.
5258 * Note that delta->xed_blockount is an offset at this
5259 * point and needs to be converted to a block count.
5261 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5262 delta->xed_blockcount -= delta->xed_startoff;
5266 * Log everything. Do this after conversion, there's no point in
5267 * logging the extent records if we've converted to btree format.
5269 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5270 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5271 logflags &= ~XFS_ILOG_FEXT(whichfork);
5272 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5273 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5274 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5276 * Log whatever the flags say, even if error. Otherwise we might miss
5277 * detecting a case where the data is changed, there's an error,
5278 * and it's not logged so we don't shutdown when we should.
5282 xfs_trans_log_inode(tp, ip, logflags);
5286 ASSERT(*firstblock == NULLFSBLOCK ||
5287 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5289 cur->bc_private.b.firstblock) ||
5291 XFS_FSB_TO_AGNO(mp, *firstblock) <
5293 cur->bc_private.b.firstblock)));
5294 *firstblock = cur->bc_private.b.firstblock;
5296 xfs_btree_del_cursor(cur,
5297 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5300 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5306 * Map file blocks to filesystem blocks, simple version.
5307 * One block (extent) only, read-only.
5308 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5309 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5310 * was set and all the others were clear.
5314 xfs_trans_t *tp, /* transaction pointer */
5315 xfs_inode_t *ip, /* incore inode */
5316 int whichfork, /* data or attr fork */
5317 xfs_fsblock_t *fsb, /* output: mapped block */
5318 xfs_fileoff_t bno) /* starting file offs. mapped */
5320 int eof; /* we've hit the end of extents */
5321 int error; /* error return */
5322 xfs_bmbt_irec_t got; /* current file extent record */
5323 xfs_ifork_t *ifp; /* inode fork pointer */
5324 xfs_extnum_t lastx; /* last useful extent number */
5325 xfs_bmbt_irec_t prev; /* previous file extent record */
5327 ifp = XFS_IFORK_PTR(ip, whichfork);
5329 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5330 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5331 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5333 return XFS_ERROR(EFSCORRUPTED);
5335 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5336 return XFS_ERROR(EIO);
5337 XFS_STATS_INC(xs_blk_mapr);
5338 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5339 (error = xfs_iread_extents(tp, ip, whichfork)))
5341 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5344 * Reading past eof, act as though there's a hole
5347 if (eof || got.br_startoff > bno) {
5351 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
5352 ASSERT(bno < got.br_startoff + got.br_blockcount);
5353 *fsb = got.br_startblock + (bno - got.br_startoff);
5354 ifp->if_lastex = lastx;
5359 * Unmap (remove) blocks from a file.
5360 * If nexts is nonzero then the number of extents to remove is limited to
5361 * that value. If not all extents in the block range can be removed then
5366 xfs_trans_t *tp, /* transaction pointer */
5367 struct xfs_inode *ip, /* incore inode */
5368 xfs_fileoff_t bno, /* starting offset to unmap */
5369 xfs_filblks_t len, /* length to unmap in file */
5370 int flags, /* misc flags */
5371 xfs_extnum_t nexts, /* number of extents max */
5372 xfs_fsblock_t *firstblock, /* first allocated block
5373 controls a.g. for allocs */
5374 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5375 xfs_extdelta_t *delta, /* o: change made to incore
5377 int *done) /* set if not done yet */
5379 xfs_btree_cur_t *cur; /* bmap btree cursor */
5380 xfs_bmbt_irec_t del; /* extent being deleted */
5381 int eof; /* is deleting at eof */
5382 xfs_bmbt_rec_t *ep; /* extent record pointer */
5383 int error; /* error return value */
5384 xfs_extnum_t extno; /* extent number in list */
5385 xfs_bmbt_irec_t got; /* current extent record */
5386 xfs_ifork_t *ifp; /* inode fork pointer */
5387 int isrt; /* freeing in rt area */
5388 xfs_extnum_t lastx; /* last extent index used */
5389 int logflags; /* transaction logging flags */
5390 xfs_extlen_t mod; /* rt extent offset */
5391 xfs_mount_t *mp; /* mount structure */
5392 xfs_extnum_t nextents; /* number of file extents */
5393 xfs_bmbt_irec_t prev; /* previous extent record */
5394 xfs_fileoff_t start; /* first file offset deleted */
5395 int tmp_logflags; /* partial logging flags */
5396 int wasdel; /* was a delayed alloc extent */
5397 int whichfork; /* data or attribute fork */
5398 int rsvd; /* OK to allocate reserved blocks */
5401 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5402 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5403 XFS_ATTR_FORK : XFS_DATA_FORK;
5404 ifp = XFS_IFORK_PTR(ip, whichfork);
5406 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5407 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5408 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5410 return XFS_ERROR(EFSCORRUPTED);
5413 if (XFS_FORCED_SHUTDOWN(mp))
5414 return XFS_ERROR(EIO);
5415 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5418 ASSERT(ifp->if_ext_max ==
5419 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5420 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5421 (error = xfs_iread_extents(tp, ip, whichfork)))
5423 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5424 if (nextents == 0) {
5428 XFS_STATS_INC(xs_blk_unmap);
5429 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5431 bno = start + len - 1;
5432 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5435 delta->xed_startoff = NULLFILEOFF;
5436 delta->xed_blockcount = 0;
5439 * Check to see if the given block number is past the end of the
5440 * file, back up to the last block if so...
5443 ep = xfs_iext_get_ext(ifp, --lastx);
5444 xfs_bmbt_get_all(ep, &got);
5445 bno = got.br_startoff + got.br_blockcount - 1;
5448 if (ifp->if_flags & XFS_IFBROOT) {
5449 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5450 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
5452 cur->bc_private.b.firstblock = *firstblock;
5453 cur->bc_private.b.flist = flist;
5454 cur->bc_private.b.flags = 0;
5458 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5459 (nexts == 0 || extno < nexts)) {
5461 * Is the found extent after a hole in which bno lives?
5462 * Just back up to the previous extent, if so.
5464 if (got.br_startoff > bno) {
5467 ep = xfs_iext_get_ext(ifp, lastx);
5468 xfs_bmbt_get_all(ep, &got);
5471 * Is the last block of this extent before the range
5472 * we're supposed to delete? If so, we're done.
5474 bno = XFS_FILEOFF_MIN(bno,
5475 got.br_startoff + got.br_blockcount - 1);
5479 * Then deal with the (possibly delayed) allocated space
5484 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
5485 if (got.br_startoff < start) {
5486 del.br_startoff = start;
5487 del.br_blockcount -= start - got.br_startoff;
5489 del.br_startblock += start - got.br_startoff;
5491 if (del.br_startoff + del.br_blockcount > bno + 1)
5492 del.br_blockcount = bno + 1 - del.br_startoff;
5493 sum = del.br_startblock + del.br_blockcount;
5495 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5497 * Realtime extent not lined up at the end.
5498 * The extent could have been split into written
5499 * and unwritten pieces, or we could just be
5500 * unmapping part of it. But we can't really
5501 * get rid of part of a realtime extent.
5503 if (del.br_state == XFS_EXT_UNWRITTEN ||
5504 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5506 * This piece is unwritten, or we're not
5507 * using unwritten extents. Skip over it.
5510 bno -= mod > del.br_blockcount ?
5511 del.br_blockcount : mod;
5512 if (bno < got.br_startoff) {
5514 xfs_bmbt_get_all(xfs_iext_get_ext(
5520 * It's written, turn it unwritten.
5521 * This is better than zeroing it.
5523 ASSERT(del.br_state == XFS_EXT_NORM);
5524 ASSERT(xfs_trans_get_block_res(tp) > 0);
5526 * If this spans a realtime extent boundary,
5527 * chop it back to the start of the one we end at.
5529 if (del.br_blockcount > mod) {
5530 del.br_startoff += del.br_blockcount - mod;
5531 del.br_startblock += del.br_blockcount - mod;
5532 del.br_blockcount = mod;
5534 del.br_state = XFS_EXT_UNWRITTEN;
5535 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5536 firstblock, flist, &logflags, delta,
5542 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5544 * Realtime extent is lined up at the end but not
5545 * at the front. We'll get rid of full extents if
5548 mod = mp->m_sb.sb_rextsize - mod;
5549 if (del.br_blockcount > mod) {
5550 del.br_blockcount -= mod;
5551 del.br_startoff += mod;
5552 del.br_startblock += mod;
5553 } else if ((del.br_startoff == start &&
5554 (del.br_state == XFS_EXT_UNWRITTEN ||
5555 xfs_trans_get_block_res(tp) == 0)) ||
5556 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5558 * Can't make it unwritten. There isn't
5559 * a full extent here so just skip it.
5561 ASSERT(bno >= del.br_blockcount);
5562 bno -= del.br_blockcount;
5563 if (bno < got.br_startoff) {
5565 xfs_bmbt_get_all(--ep, &got);
5568 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5570 * This one is already unwritten.
5571 * It must have a written left neighbor.
5572 * Unwrite the killed part of that one and
5576 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5578 ASSERT(prev.br_state == XFS_EXT_NORM);
5579 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
5580 ASSERT(del.br_startblock ==
5581 prev.br_startblock + prev.br_blockcount);
5582 if (prev.br_startoff < start) {
5583 mod = start - prev.br_startoff;
5584 prev.br_blockcount -= mod;
5585 prev.br_startblock += mod;
5586 prev.br_startoff = start;
5588 prev.br_state = XFS_EXT_UNWRITTEN;
5589 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5590 &prev, firstblock, flist, &logflags,
5591 delta, XFS_DATA_FORK, 0);
5596 ASSERT(del.br_state == XFS_EXT_NORM);
5597 del.br_state = XFS_EXT_UNWRITTEN;
5598 error = xfs_bmap_add_extent(ip, lastx, &cur,
5599 &del, firstblock, flist, &logflags,
5600 delta, XFS_DATA_FORK, 0);
5607 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
5608 /* Update realtime/data freespace, unreserve quota */
5610 xfs_filblks_t rtexts;
5612 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5613 do_div(rtexts, mp->m_sb.sb_rextsize);
5614 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5616 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5617 NULL, ip, -((long)del.br_blockcount), 0,
5618 XFS_QMOPT_RES_RTBLKS);
5620 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
5621 (int)del.br_blockcount, rsvd);
5622 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5623 NULL, ip, -((long)del.br_blockcount), 0,
5624 XFS_QMOPT_RES_REGBLKS);
5626 ip->i_delayed_blks -= del.br_blockcount;
5628 cur->bc_private.b.flags |=
5629 XFS_BTCUR_BPRV_WASDEL;
5631 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5633 * If it's the case where the directory code is running
5634 * with no block reservation, and the deleted block is in
5635 * the middle of its extent, and the resulting insert
5636 * of an extent would cause transformation to btree format,
5637 * then reject it. The calling code will then swap
5638 * blocks around instead.
5639 * We have to do this now, rather than waiting for the
5640 * conversion to btree format, since the transaction
5643 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5644 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5645 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5646 del.br_startoff > got.br_startoff &&
5647 del.br_startoff + del.br_blockcount <
5648 got.br_startoff + got.br_blockcount) {
5649 error = XFS_ERROR(ENOSPC);
5652 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5653 &tmp_logflags, delta, whichfork, rsvd);
5654 logflags |= tmp_logflags;
5657 bno = del.br_startoff - 1;
5659 lastx = ifp->if_lastex;
5661 * If not done go on to the next (previous) record.
5662 * Reset ep in case the extents array was re-alloced.
5664 ep = xfs_iext_get_ext(ifp, lastx);
5665 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5666 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5667 xfs_bmbt_get_startoff(ep) > bno) {
5669 ep = xfs_iext_get_ext(ifp, lastx);
5672 xfs_bmbt_get_all(ep, &got);
5676 ifp->if_lastex = lastx;
5677 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5678 ASSERT(ifp->if_ext_max ==
5679 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5681 * Convert to a btree if necessary.
5683 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5684 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5685 ASSERT(cur == NULL);
5686 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5687 &cur, 0, &tmp_logflags, whichfork);
5688 logflags |= tmp_logflags;
5693 * transform from btree to extents, give it cur
5695 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5696 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5697 ASSERT(cur != NULL);
5698 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5700 logflags |= tmp_logflags;
5705 * transform from extents to local?
5707 ASSERT(ifp->if_ext_max ==
5708 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5710 if (delta && delta->xed_startoff != NULLFILEOFF) {
5711 /* A change was actually made.
5712 * Note that delta->xed_blockount is an offset at this
5713 * point and needs to be converted to a block count.
5715 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5716 delta->xed_blockcount -= delta->xed_startoff;
5720 * Log everything. Do this after conversion, there's no point in
5721 * logging the extent records if we've converted to btree format.
5723 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5724 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5725 logflags &= ~XFS_ILOG_FEXT(whichfork);
5726 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5727 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5728 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5730 * Log inode even in the error case, if the transaction
5731 * is dirty we'll need to shut down the filesystem.
5734 xfs_trans_log_inode(tp, ip, logflags);
5737 *firstblock = cur->bc_private.b.firstblock;
5738 cur->bc_private.b.allocated = 0;
5740 xfs_btree_del_cursor(cur,
5741 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5747 static __inline__ int
5748 copy_to_user(void *dst, void *src, int len) {
5749 memcpy(dst,src,len);
5752 #undef copy_from_user
5753 static __inline__ int
5754 copy_from_user(void *dst, void *src, int len) {
5755 memcpy(dst,src,len);
5759 * Fcntl interface to xfs_bmapi.
5761 int /* error code */
5763 bhv_desc_t *bdp, /* XFS behavior descriptor*/
5764 struct getbmap *bmv, /* user bmap structure */
5765 void __user *ap, /* pointer to user's array */
5766 int interface) /* interface flags */
5768 __int64_t bmvend; /* last block requested */
5769 int error; /* return value */
5770 __int64_t fixlen; /* length for -1 case */
5771 int i; /* extent number */
5772 xfs_inode_t *ip; /* xfs incore inode pointer */
5773 xfs_vnode_t *vp; /* corresponding vnode */
5774 int lock; /* lock state */
5775 xfs_bmbt_irec_t *map; /* buffer for user's data */
5776 xfs_mount_t *mp; /* file system mount point */
5777 int nex; /* # of user extents can do */
5778 int nexleft; /* # of user extents left */
5779 int subnex; /* # of bmapi's can do */
5780 int nmap; /* number of map entries */
5781 struct getbmap out; /* output structure */
5782 int whichfork; /* data or attr fork */
5783 int prealloced; /* this is a file with
5784 * preallocated data space */
5785 int sh_unwritten; /* true, if unwritten */
5786 /* extents listed separately */
5787 int bmapi_flags; /* flags for xfs_bmapi */
5788 __int32_t oflags; /* getbmapx bmv_oflags field */
5790 vp = BHV_TO_VNODE(bdp);
5791 ip = XFS_BHVTOI(bdp);
5794 whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5795 sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
5797 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5798 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5799 * bit is set for the file, generate a read event in order
5800 * that the DMAPI application may do its thing before we return
5801 * the extents. Usually this means restoring user file data to
5802 * regions of the file that look like holes.
5804 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5805 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5806 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5807 * could misinterpret holes in a DMAPI file as true holes,
5808 * when in fact they may represent offline user data.
5810 if ( (interface & BMV_IF_NO_DMAPI_READ) == 0
5811 && DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)
5812 && whichfork == XFS_DATA_FORK) {
5814 error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, 0, 0, 0, NULL);
5816 return XFS_ERROR(error);
5819 if (whichfork == XFS_ATTR_FORK) {
5820 if (XFS_IFORK_Q(ip)) {
5821 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5822 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5823 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5824 return XFS_ERROR(EINVAL);
5825 } else if (unlikely(
5826 ip->i_d.di_aformat != 0 &&
5827 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5828 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5830 return XFS_ERROR(EFSCORRUPTED);
5832 } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5833 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5834 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5835 return XFS_ERROR(EINVAL);
5836 if (whichfork == XFS_DATA_FORK) {
5837 if ((ip->i_d.di_extsize && (ip->i_d.di_flags &
5838 (XFS_DIFLAG_REALTIME|XFS_DIFLAG_EXTSIZE))) ||
5839 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5841 fixlen = XFS_MAXIOFFSET(mp);
5844 fixlen = ip->i_d.di_size;
5851 if (bmv->bmv_length == -1) {
5852 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5853 bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
5855 } else if (bmv->bmv_length < 0)
5856 return XFS_ERROR(EINVAL);
5857 if (bmv->bmv_length == 0) {
5858 bmv->bmv_entries = 0;
5861 nex = bmv->bmv_count - 1;
5863 return XFS_ERROR(EINVAL);
5864 bmvend = bmv->bmv_offset + bmv->bmv_length;
5866 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5868 if (whichfork == XFS_DATA_FORK && ip->i_delayed_blks) {
5869 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
5870 XVOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
5873 ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
5875 lock = xfs_ilock_map_shared(ip);
5878 * Don't let nex be bigger than the number of extents
5879 * we can have assuming alternating holes and real extents.
5881 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5882 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5884 bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
5885 ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
5888 * Allocate enough space to handle "subnex" maps at a time.
5891 map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
5893 bmv->bmv_entries = 0;
5895 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
5897 goto unlock_and_return;
5903 nmap = (nexleft > subnex) ? subnex : nexleft;
5904 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5905 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5906 bmapi_flags, NULL, 0, map, &nmap,
5909 goto unlock_and_return;
5910 ASSERT(nmap <= subnex);
5912 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5914 oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
5915 BMV_OF_PREALLOC : 0;
5916 out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
5917 out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5918 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
5919 if (map[i].br_startblock == HOLESTARTBLOCK &&
5920 ((prealloced && out.bmv_offset + out.bmv_length == bmvend) ||
5921 whichfork == XFS_ATTR_FORK )) {
5923 * came to hole at end of file or the end of
5926 goto unlock_and_return;
5929 (map[i].br_startblock == HOLESTARTBLOCK) ?
5931 XFS_FSB_TO_DB(ip, map[i].br_startblock);
5933 /* return either getbmap/getbmapx structure. */
5934 if (interface & BMV_IF_EXTENDED) {
5935 struct getbmapx outx;
5937 GETBMAP_CONVERT(out,outx);
5938 outx.bmv_oflags = oflags;
5939 outx.bmv_unused1 = outx.bmv_unused2 = 0;
5940 if (copy_to_user(ap, &outx,
5942 error = XFS_ERROR(EFAULT);
5943 goto unlock_and_return;
5946 if (copy_to_user(ap, &out,
5948 error = XFS_ERROR(EFAULT);
5949 goto unlock_and_return;
5953 out.bmv_offset + out.bmv_length;
5954 bmv->bmv_length = MAX((__int64_t)0,
5955 (__int64_t)(bmvend - bmv->bmv_offset));
5957 ap = (interface & BMV_IF_EXTENDED) ?
5959 ((struct getbmapx __user *)ap + 1) :
5961 ((struct getbmap __user *)ap + 1);
5964 } while (nmap && nexleft && bmv->bmv_length);
5967 xfs_iunlock_map_shared(ip, lock);
5968 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5970 kmem_free(map, subnex * sizeof(*map));
5976 * Check the last inode extent to determine whether this allocation will result
5977 * in blocks being allocated at the end of the file. When we allocate new data
5978 * blocks at the end of the file which do not start at the previous data block,
5979 * we will try to align the new blocks at stripe unit boundaries.
5981 STATIC int /* error */
5983 xfs_inode_t *ip, /* incore inode pointer */
5984 xfs_fileoff_t off, /* file offset in fsblocks */
5985 int whichfork, /* data or attribute fork */
5986 char *aeof) /* return value */
5988 int error; /* error return value */
5989 xfs_ifork_t *ifp; /* inode fork pointer */
5990 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
5991 xfs_extnum_t nextents; /* number of file extents */
5992 xfs_bmbt_irec_t s; /* expanded extent record */
5994 ASSERT(whichfork == XFS_DATA_FORK);
5995 ifp = XFS_IFORK_PTR(ip, whichfork);
5996 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5997 (error = xfs_iread_extents(NULL, ip, whichfork)))
5999 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6000 if (nextents == 0) {
6005 * Go to the last extent
6007 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6008 xfs_bmbt_get_all(lastrec, &s);
6010 * Check we are allocating in the last extent (for delayed allocations)
6011 * or past the last extent for non-delayed allocations.
6013 *aeof = (off >= s.br_startoff &&
6014 off < s.br_startoff + s.br_blockcount &&
6015 ISNULLSTARTBLOCK(s.br_startblock)) ||
6016 off >= s.br_startoff + s.br_blockcount;
6021 * Check if the endoff is outside the last extent. If so the caller will grow
6022 * the allocation to a stripe unit boundary.
6026 xfs_inode_t *ip, /* incore inode pointer */
6027 xfs_fileoff_t endoff, /* file offset in fsblocks */
6028 int whichfork, /* data or attribute fork */
6029 int *eof) /* result value */
6031 xfs_fsblock_t blockcount; /* extent block count */
6032 int error; /* error return value */
6033 xfs_ifork_t *ifp; /* inode fork pointer */
6034 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
6035 xfs_extnum_t nextents; /* number of file extents */
6036 xfs_fileoff_t startoff; /* extent starting file offset */
6038 ASSERT(whichfork == XFS_DATA_FORK);
6039 ifp = XFS_IFORK_PTR(ip, whichfork);
6040 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6041 (error = xfs_iread_extents(NULL, ip, whichfork)))
6043 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6044 if (nextents == 0) {
6049 * Go to the last extent
6051 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6052 startoff = xfs_bmbt_get_startoff(lastrec);
6053 blockcount = xfs_bmbt_get_blockcount(lastrec);
6054 *eof = endoff >= startoff + blockcount;
6061 * Check that the extents list for the inode ip is in the right order.
6064 xfs_bmap_check_extents(
6065 xfs_inode_t *ip, /* incore inode pointer */
6066 int whichfork) /* data or attr fork */
6068 xfs_bmbt_rec_t *ep; /* current extent entry */
6069 xfs_extnum_t idx; /* extent record index */
6070 xfs_ifork_t *ifp; /* inode fork pointer */
6071 xfs_extnum_t nextents; /* number of extents in list */
6072 xfs_bmbt_rec_t *nextp; /* next extent entry */
6074 ifp = XFS_IFORK_PTR(ip, whichfork);
6075 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
6076 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6077 ep = xfs_iext_get_ext(ifp, 0);
6078 for (idx = 0; idx < nextents - 1; idx++) {
6079 nextp = xfs_iext_get_ext(ifp, idx + 1);
6080 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
6090 xfs_btree_cur_t *cur,
6100 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6101 bp = cur->bc_bufs[i];
6103 if (XFS_BUF_ADDR(bp) == bno)
6104 break; /* Found it */
6106 if (i == XFS_BTREE_MAXLEVELS)
6109 if (!bp) { /* Chase down all the log items to see if the bp is there */
6110 xfs_log_item_chunk_t *licp;
6114 licp = &tp->t_items;
6115 while (!bp && licp != NULL) {
6116 if (XFS_LIC_ARE_ALL_FREE(licp)) {
6117 licp = licp->lic_next;
6120 for (i = 0; i < licp->lic_unused; i++) {
6121 xfs_log_item_desc_t *lidp;
6122 xfs_log_item_t *lip;
6123 xfs_buf_log_item_t *bip;
6126 if (XFS_LIC_ISFREE(licp, i)) {
6130 lidp = XFS_LIC_SLOT(licp, i);
6131 lip = lidp->lid_item;
6132 if (lip->li_type != XFS_LI_BUF)
6135 bip = (xfs_buf_log_item_t *)lip;
6138 if (XFS_BUF_ADDR(lbp) == bno) {
6140 break; /* Found it */
6143 licp = licp->lic_next;
6151 xfs_bmbt_block_t *block,
6157 xfs_bmbt_ptr_t *pp, *thispa; /* pointer to block address */
6158 xfs_bmbt_key_t *prevp, *keyp;
6160 ASSERT(be16_to_cpu(block->bb_level) > 0);
6163 for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
6164 dmxr = mp->m_bmap_dmxr[0];
6167 keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
6169 keyp = XFS_BTREE_KEY_ADDR(mp->m_sb.sb_blocksize,
6170 xfs_bmbt, block, i, dmxr);
6174 xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
6179 * Compare the block numbers to see if there are dups.
6183 pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
6185 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6186 xfs_bmbt, block, i, dmxr);
6188 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
6190 thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
6192 thispa = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6193 xfs_bmbt, block, j, dmxr);
6195 if (INT_GET(*thispa, ARCH_CONVERT) ==
6196 INT_GET(*pp, ARCH_CONVERT)) {
6197 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
6199 INT_GET(*thispa, ARCH_CONVERT));
6200 panic("%s: ptrs are equal in node\n",
6208 * Check that the extents for the inode ip are in the right order in all
6213 xfs_bmap_check_leaf_extents(
6214 xfs_btree_cur_t *cur, /* btree cursor or null */
6215 xfs_inode_t *ip, /* incore inode pointer */
6216 int whichfork) /* data or attr fork */
6218 xfs_bmbt_block_t *block; /* current btree block */
6219 xfs_fsblock_t bno; /* block # of "block" */
6220 xfs_buf_t *bp; /* buffer for "block" */
6221 int error; /* error return value */
6222 xfs_extnum_t i=0, j; /* index into the extents list */
6223 xfs_ifork_t *ifp; /* fork structure */
6224 int level; /* btree level, for checking */
6225 xfs_mount_t *mp; /* file system mount structure */
6226 xfs_bmbt_ptr_t *pp; /* pointer to block address */
6227 xfs_bmbt_rec_t *ep; /* pointer to current extent */
6228 xfs_bmbt_rec_t *lastp; /* pointer to previous extent */
6229 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
6232 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6238 ifp = XFS_IFORK_PTR(ip, whichfork);
6239 block = ifp->if_broot;
6241 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6243 level = be16_to_cpu(block->bb_level);
6245 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6246 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6247 ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
6248 ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
6249 ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
6250 bno = INT_GET(*pp, ARCH_CONVERT);
6252 * Go down the tree until leaf level is reached, following the first
6253 * pointer (leftmost) at each level.
6255 while (level-- > 0) {
6256 /* See if buf is in cur first */
6257 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6263 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6264 XFS_BMAP_BTREE_REF)))
6266 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6267 XFS_WANT_CORRUPTED_GOTO(
6268 XFS_BMAP_SANITY_CHECK(mp, block, level),
6274 * Check this block for basic sanity (increasing keys and
6275 * no duplicate blocks).
6278 xfs_check_block(block, mp, 0, 0);
6279 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
6280 1, mp->m_bmap_dmxr[1]);
6281 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, INT_GET(*pp, ARCH_CONVERT)), error0);
6282 bno = INT_GET(*pp, ARCH_CONVERT);
6285 xfs_trans_brelse(NULL, bp);
6290 * Here with bp and block set to the leftmost leaf node in the tree.
6295 * Loop over all leaf nodes checking that all extents are in the right order.
6299 xfs_fsblock_t nextbno;
6300 xfs_extnum_t num_recs;
6303 num_recs = be16_to_cpu(block->bb_numrecs);
6306 * Read-ahead the next leaf block, if any.
6309 nextbno = be64_to_cpu(block->bb_rightsib);
6312 * Check all the extents to make sure they are OK.
6313 * If we had a previous block, the last entry should
6314 * conform with the first entry in this one.
6317 ep = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
6318 block, 1, mp->m_bmap_dmxr[0]);
6319 for (j = 1; j < num_recs; j++) {
6320 nextp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
6321 block, j + 1, mp->m_bmap_dmxr[0]);
6323 xfs_btree_check_rec(XFS_BTNUM_BMAP,
6324 (void *)lastp, (void *)ep);
6326 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
6335 xfs_trans_brelse(NULL, bp);
6339 * If we've reached the end, stop.
6341 if (bno == NULLFSBLOCK)
6344 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6350 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6351 XFS_BMAP_BTREE_REF)))
6353 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6357 xfs_trans_brelse(NULL, bp);
6362 cmn_err(CE_WARN, "%s: at error0", __FUNCTION__);
6364 xfs_trans_brelse(NULL, bp);
6366 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
6368 panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__);
6374 * Count fsblocks of the given fork.
6377 xfs_bmap_count_blocks(
6378 xfs_trans_t *tp, /* transaction pointer */
6379 xfs_inode_t *ip, /* incore inode */
6380 int whichfork, /* data or attr fork */
6381 int *count) /* out: count of blocks */
6383 xfs_bmbt_block_t *block; /* current btree block */
6384 xfs_fsblock_t bno; /* block # of "block" */
6385 xfs_ifork_t *ifp; /* fork structure */
6386 int level; /* btree level, for checking */
6387 xfs_mount_t *mp; /* file system mount structure */
6388 xfs_bmbt_ptr_t *pp; /* pointer to block address */
6392 ifp = XFS_IFORK_PTR(ip, whichfork);
6393 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6394 if (unlikely(xfs_bmap_count_leaves(ifp, 0,
6395 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6397 XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)",
6398 XFS_ERRLEVEL_LOW, mp);
6399 return XFS_ERROR(EFSCORRUPTED);
6405 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6407 block = ifp->if_broot;
6408 level = be16_to_cpu(block->bb_level);
6410 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6411 ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
6412 ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
6413 ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
6414 bno = INT_GET(*pp, ARCH_CONVERT);
6416 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
6417 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6419 return XFS_ERROR(EFSCORRUPTED);
6426 * Recursively walks each level of a btree
6427 * to count total fsblocks is use.
6430 xfs_bmap_count_tree(
6431 xfs_mount_t *mp, /* file system mount point */
6432 xfs_trans_t *tp, /* transaction pointer */
6433 xfs_ifork_t *ifp, /* inode fork pointer */
6434 xfs_fsblock_t blockno, /* file system block number */
6435 int levelin, /* level in btree */
6436 int *count) /* Count of blocks */
6439 xfs_buf_t *bp, *nbp;
6440 int level = levelin;
6442 xfs_fsblock_t bno = blockno;
6443 xfs_fsblock_t nextbno;
6444 xfs_bmbt_block_t *block, *nextblock;
6447 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6450 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6453 /* Not at node above leafs, count this level of nodes */
6454 nextbno = be64_to_cpu(block->bb_rightsib);
6455 while (nextbno != NULLFSBLOCK) {
6456 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6457 0, &nbp, XFS_BMAP_BTREE_REF)))
6460 nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
6461 nextbno = be64_to_cpu(nextblock->bb_rightsib);
6462 xfs_trans_brelse(tp, nbp);
6465 /* Dive to the next level */
6466 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6467 xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
6468 bno = INT_GET(*pp, ARCH_CONVERT);
6469 if (unlikely((error =
6470 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6471 xfs_trans_brelse(tp, bp);
6472 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6473 XFS_ERRLEVEL_LOW, mp);
6474 return XFS_ERROR(EFSCORRUPTED);
6476 xfs_trans_brelse(tp, bp);
6478 /* count all level 1 nodes and their leaves */
6480 nextbno = be64_to_cpu(block->bb_rightsib);
6481 numrecs = be16_to_cpu(block->bb_numrecs);
6482 if (unlikely(xfs_bmap_disk_count_leaves(ifp, mp,
6483 0, block, numrecs, count) < 0)) {
6484 xfs_trans_brelse(tp, bp);
6485 XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
6486 XFS_ERRLEVEL_LOW, mp);
6487 return XFS_ERROR(EFSCORRUPTED);
6489 xfs_trans_brelse(tp, bp);
6490 if (nextbno == NULLFSBLOCK)
6493 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6494 XFS_BMAP_BTREE_REF)))
6497 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6504 * Count leaf blocks given a range of extent records.
6507 xfs_bmap_count_leaves(
6514 xfs_bmbt_rec_t *frp;
6516 for (b = 0; b < numrecs; b++) {
6517 frp = xfs_iext_get_ext(ifp, idx + b);
6518 *count += xfs_bmbt_get_blockcount(frp);
6524 * Count leaf blocks given a range of extent records originally
6528 xfs_bmap_disk_count_leaves(
6532 xfs_bmbt_block_t *block,
6537 xfs_bmbt_rec_t *frp;
6539 for (b = 1; b <= numrecs; b++) {
6540 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize,
6541 xfs_bmbt, block, idx + b, mp->m_bmap_dmxr[0]);
6542 *count += xfs_bmbt_disk_get_blockcount(frp);