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"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_dir2_sf.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dinode.h"
35 #include "xfs_inode.h"
36 #include "xfs_btree.h"
37 #include "xfs_dmapi.h"
38 #include "xfs_mount.h"
39 #include "xfs_ialloc.h"
40 #include "xfs_itable.h"
41 #include "xfs_dir2_data.h"
42 #include "xfs_dir2_leaf.h"
43 #include "xfs_dir2_block.h"
44 #include "xfs_inode_item.h"
45 #include "xfs_extfree_item.h"
46 #include "xfs_alloc.h"
48 #include "xfs_rtalloc.h"
49 #include "xfs_error.h"
50 #include "xfs_attr_leaf.h"
52 #include "xfs_quota.h"
53 #include "xfs_trans_space.h"
54 #include "xfs_buf_item.h"
55 #include "xfs_filestream.h"
56 #include "xfs_vnodeops.h"
61 xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
64 kmem_zone_t *xfs_bmap_free_item_zone;
67 * Prototypes for internal bmap routines.
72 * Called from xfs_bmap_add_attrfork to handle extents format files.
74 STATIC int /* error */
75 xfs_bmap_add_attrfork_extents(
76 xfs_trans_t *tp, /* transaction pointer */
77 xfs_inode_t *ip, /* incore inode pointer */
78 xfs_fsblock_t *firstblock, /* first block allocated */
79 xfs_bmap_free_t *flist, /* blocks to free at commit */
80 int *flags); /* inode logging flags */
83 * Called from xfs_bmap_add_attrfork to handle local format files.
85 STATIC int /* error */
86 xfs_bmap_add_attrfork_local(
87 xfs_trans_t *tp, /* transaction pointer */
88 xfs_inode_t *ip, /* incore inode pointer */
89 xfs_fsblock_t *firstblock, /* first block allocated */
90 xfs_bmap_free_t *flist, /* blocks to free at commit */
91 int *flags); /* inode logging flags */
94 * Called by xfs_bmapi to update file extent records and the btree
95 * after allocating space (or doing a delayed allocation).
97 STATIC int /* error */
99 xfs_inode_t *ip, /* incore inode pointer */
100 xfs_extnum_t idx, /* extent number to update/insert */
101 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
102 xfs_bmbt_irec_t *new, /* new data to add to file extents */
103 xfs_fsblock_t *first, /* pointer to firstblock variable */
104 xfs_bmap_free_t *flist, /* list of extents to be freed */
105 int *logflagsp, /* inode logging flags */
106 xfs_extdelta_t *delta, /* Change made to incore extents */
107 int whichfork, /* data or attr fork */
108 int rsvd); /* OK to allocate reserved blocks */
111 * Called by xfs_bmap_add_extent to handle cases converting a delayed
112 * allocation to a real allocation.
114 STATIC int /* error */
115 xfs_bmap_add_extent_delay_real(
116 xfs_inode_t *ip, /* incore inode pointer */
117 xfs_extnum_t idx, /* extent number to update/insert */
118 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
119 xfs_bmbt_irec_t *new, /* new data to add to file extents */
120 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
121 xfs_fsblock_t *first, /* pointer to firstblock variable */
122 xfs_bmap_free_t *flist, /* list of extents to be freed */
123 int *logflagsp, /* inode logging flags */
124 xfs_extdelta_t *delta, /* Change made to incore extents */
125 int rsvd); /* OK to allocate reserved blocks */
128 * Called by xfs_bmap_add_extent to handle cases converting a hole
129 * to a delayed allocation.
131 STATIC int /* error */
132 xfs_bmap_add_extent_hole_delay(
133 xfs_inode_t *ip, /* incore inode pointer */
134 xfs_extnum_t idx, /* extent number to update/insert */
135 xfs_bmbt_irec_t *new, /* new data to add to file extents */
136 int *logflagsp,/* inode logging flags */
137 xfs_extdelta_t *delta, /* Change made to incore extents */
138 int rsvd); /* OK to allocate reserved blocks */
141 * Called by xfs_bmap_add_extent to handle cases converting a hole
142 * to a real allocation.
144 STATIC int /* error */
145 xfs_bmap_add_extent_hole_real(
146 xfs_inode_t *ip, /* incore inode pointer */
147 xfs_extnum_t idx, /* extent number to update/insert */
148 xfs_btree_cur_t *cur, /* if null, not a btree */
149 xfs_bmbt_irec_t *new, /* new data to add to file extents */
150 int *logflagsp, /* inode logging flags */
151 xfs_extdelta_t *delta, /* Change made to incore extents */
152 int whichfork); /* data or attr fork */
155 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
156 * allocation to a real allocation or vice versa.
158 STATIC int /* error */
159 xfs_bmap_add_extent_unwritten_real(
160 xfs_inode_t *ip, /* incore inode pointer */
161 xfs_extnum_t idx, /* extent number to update/insert */
162 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
163 xfs_bmbt_irec_t *new, /* new data to add to file extents */
164 int *logflagsp, /* inode logging flags */
165 xfs_extdelta_t *delta); /* Change made to incore extents */
168 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
169 * It figures out where to ask the underlying allocator to put the new extent.
171 STATIC int /* error */
173 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
176 * Transform a btree format file with only one leaf node, where the
177 * extents list will fit in the inode, into an extents format file.
178 * Since the file extents are already in-core, all we have to do is
179 * give up the space for the btree root and pitch the leaf block.
181 STATIC int /* error */
182 xfs_bmap_btree_to_extents(
183 xfs_trans_t *tp, /* transaction pointer */
184 xfs_inode_t *ip, /* incore inode pointer */
185 xfs_btree_cur_t *cur, /* btree cursor */
186 int *logflagsp, /* inode logging flags */
187 int whichfork); /* data or attr fork */
190 * Called by xfs_bmapi to update file extent records and the btree
191 * after removing space (or undoing a delayed allocation).
193 STATIC int /* error */
195 xfs_inode_t *ip, /* incore inode pointer */
196 xfs_trans_t *tp, /* current trans pointer */
197 xfs_extnum_t idx, /* extent number to update/insert */
198 xfs_bmap_free_t *flist, /* list of extents to be freed */
199 xfs_btree_cur_t *cur, /* if null, not a btree */
200 xfs_bmbt_irec_t *new, /* new data to add to file extents */
201 int *logflagsp,/* inode logging flags */
202 xfs_extdelta_t *delta, /* Change made to incore extents */
203 int whichfork, /* data or attr fork */
204 int rsvd); /* OK to allocate reserved blocks */
207 * Remove the entry "free" from the free item list. Prev points to the
208 * previous entry, unless "free" is the head of the list.
212 xfs_bmap_free_t *flist, /* free item list header */
213 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
214 xfs_bmap_free_item_t *free); /* list item to be freed */
217 * Convert an extents-format file into a btree-format file.
218 * The new file will have a root block (in the inode) and a single child block.
220 STATIC int /* error */
221 xfs_bmap_extents_to_btree(
222 xfs_trans_t *tp, /* transaction pointer */
223 xfs_inode_t *ip, /* incore inode pointer */
224 xfs_fsblock_t *firstblock, /* first-block-allocated */
225 xfs_bmap_free_t *flist, /* blocks freed in xaction */
226 xfs_btree_cur_t **curp, /* cursor returned to caller */
227 int wasdel, /* converting a delayed alloc */
228 int *logflagsp, /* inode logging flags */
229 int whichfork); /* data or attr fork */
232 * Convert a local file to an extents file.
233 * This code is sort of bogus, since the file data needs to get
234 * logged so it won't be lost. The bmap-level manipulations are ok, though.
236 STATIC int /* error */
237 xfs_bmap_local_to_extents(
238 xfs_trans_t *tp, /* transaction pointer */
239 xfs_inode_t *ip, /* incore inode pointer */
240 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
241 xfs_extlen_t total, /* total blocks needed by transaction */
242 int *logflagsp, /* inode logging flags */
243 int whichfork); /* data or attr fork */
246 * Search the extents list for the inode, for the extent containing bno.
247 * If bno lies in a hole, point to the next entry. If bno lies past eof,
248 * *eofp will be set, and *prevp will contain the last entry (null if none).
249 * Else, *lastxp will be set to the index of the found
250 * entry; *gotp will contain the entry.
252 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
253 xfs_bmap_search_extents(
254 xfs_inode_t *ip, /* incore inode pointer */
255 xfs_fileoff_t bno, /* block number searched for */
256 int whichfork, /* data or attr fork */
257 int *eofp, /* out: end of file found */
258 xfs_extnum_t *lastxp, /* out: last extent index */
259 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
260 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
263 * Check the last inode extent to determine whether this allocation will result
264 * in blocks being allocated at the end of the file. When we allocate new data
265 * blocks at the end of the file which do not start at the previous data block,
266 * we will try to align the new blocks at stripe unit boundaries.
268 STATIC int /* error */
270 xfs_inode_t *ip, /* incore inode pointer */
271 xfs_fileoff_t off, /* file offset in fsblocks */
272 int whichfork, /* data or attribute fork */
273 char *aeof); /* return value */
275 #ifdef XFS_BMAP_TRACE
277 * Add bmap trace entry prior to a call to xfs_iext_remove.
280 xfs_bmap_trace_delete(
281 const char *fname, /* function name */
282 char *desc, /* operation description */
283 xfs_inode_t *ip, /* incore inode pointer */
284 xfs_extnum_t idx, /* index of entry(entries) deleted */
285 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
286 int whichfork); /* data or attr fork */
289 * Add bmap trace entry prior to a call to xfs_iext_insert, or
290 * reading in the extents list from the disk (in the btree).
293 xfs_bmap_trace_insert(
294 const char *fname, /* function name */
295 char *desc, /* operation description */
296 xfs_inode_t *ip, /* incore inode pointer */
297 xfs_extnum_t idx, /* index of entry(entries) inserted */
298 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
299 xfs_bmbt_irec_t *r1, /* inserted record 1 */
300 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
301 int whichfork); /* data or attr fork */
304 * Add bmap trace entry after updating an extent record in place.
307 xfs_bmap_trace_post_update(
308 const char *fname, /* function name */
309 char *desc, /* operation description */
310 xfs_inode_t *ip, /* incore inode pointer */
311 xfs_extnum_t idx, /* index of entry updated */
312 int whichfork); /* data or attr fork */
315 * Add bmap trace entry prior to updating an extent record in place.
318 xfs_bmap_trace_pre_update(
319 const char *fname, /* function name */
320 char *desc, /* operation description */
321 xfs_inode_t *ip, /* incore inode pointer */
322 xfs_extnum_t idx, /* index of entry to be updated */
323 int whichfork); /* data or attr fork */
325 #define XFS_BMAP_TRACE_DELETE(d,ip,i,c,w) \
326 xfs_bmap_trace_delete(__func__,d,ip,i,c,w)
327 #define XFS_BMAP_TRACE_INSERT(d,ip,i,c,r1,r2,w) \
328 xfs_bmap_trace_insert(__func__,d,ip,i,c,r1,r2,w)
329 #define XFS_BMAP_TRACE_POST_UPDATE(d,ip,i,w) \
330 xfs_bmap_trace_post_update(__func__,d,ip,i,w)
331 #define XFS_BMAP_TRACE_PRE_UPDATE(d,ip,i,w) \
332 xfs_bmap_trace_pre_update(__func__,d,ip,i,w)
334 #define XFS_BMAP_TRACE_DELETE(d,ip,i,c,w)
335 #define XFS_BMAP_TRACE_INSERT(d,ip,i,c,r1,r2,w)
336 #define XFS_BMAP_TRACE_POST_UPDATE(d,ip,i,w)
337 #define XFS_BMAP_TRACE_PRE_UPDATE(d,ip,i,w)
338 #endif /* XFS_BMAP_TRACE */
341 * Compute the worst-case number of indirect blocks that will be used
342 * for ip's delayed extent of length "len".
345 xfs_bmap_worst_indlen(
346 xfs_inode_t *ip, /* incore inode pointer */
347 xfs_filblks_t len); /* delayed extent length */
351 * Perform various validation checks on the values being returned
355 xfs_bmap_validate_ret(
359 xfs_bmbt_irec_t *mval,
363 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
366 #if defined(XFS_RW_TRACE)
375 #define xfs_bunmap_trace(ip, bno, len, flags, ra)
376 #endif /* XFS_RW_TRACE */
383 xfs_fsblock_t blockno,
388 xfs_bmap_count_leaves(
395 xfs_bmap_disk_count_leaves(
396 struct xfs_mount *mp,
397 struct xfs_btree_block *block,
402 * Bmap internal routines.
405 STATIC int /* error */
407 struct xfs_btree_cur *cur,
411 int *stat) /* success/failure */
413 cur->bc_rec.b.br_startoff = off;
414 cur->bc_rec.b.br_startblock = bno;
415 cur->bc_rec.b.br_blockcount = len;
416 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
419 STATIC int /* error */
421 struct xfs_btree_cur *cur,
425 int *stat) /* success/failure */
427 cur->bc_rec.b.br_startoff = off;
428 cur->bc_rec.b.br_startblock = bno;
429 cur->bc_rec.b.br_blockcount = len;
430 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
434 * Update the record referred to by cur to the value given
435 * by [off, bno, len, state].
436 * This either works (return 0) or gets an EFSCORRUPTED error.
440 struct xfs_btree_cur *cur,
446 union xfs_btree_rec rec;
448 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
449 return xfs_btree_update(cur, &rec);
453 * Called from xfs_bmap_add_attrfork to handle btree format files.
455 STATIC int /* error */
456 xfs_bmap_add_attrfork_btree(
457 xfs_trans_t *tp, /* transaction pointer */
458 xfs_inode_t *ip, /* incore inode pointer */
459 xfs_fsblock_t *firstblock, /* first block allocated */
460 xfs_bmap_free_t *flist, /* blocks to free at commit */
461 int *flags) /* inode logging flags */
463 xfs_btree_cur_t *cur; /* btree cursor */
464 int error; /* error return value */
465 xfs_mount_t *mp; /* file system mount struct */
466 int stat; /* newroot status */
469 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
470 *flags |= XFS_ILOG_DBROOT;
472 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
473 cur->bc_private.b.flist = flist;
474 cur->bc_private.b.firstblock = *firstblock;
475 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
477 /* must be at least one entry */
478 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
479 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
482 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
483 return XFS_ERROR(ENOSPC);
485 *firstblock = cur->bc_private.b.firstblock;
486 cur->bc_private.b.allocated = 0;
487 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
491 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
496 * Called from xfs_bmap_add_attrfork to handle extents format files.
498 STATIC int /* error */
499 xfs_bmap_add_attrfork_extents(
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_btree_cur_t *cur; /* bmap btree cursor */
507 int error; /* error return value */
509 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
512 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
513 flags, XFS_DATA_FORK);
515 cur->bc_private.b.allocated = 0;
516 xfs_btree_del_cursor(cur,
517 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
523 * Called from xfs_bmap_add_attrfork to handle local format files.
525 STATIC int /* error */
526 xfs_bmap_add_attrfork_local(
527 xfs_trans_t *tp, /* transaction pointer */
528 xfs_inode_t *ip, /* incore inode pointer */
529 xfs_fsblock_t *firstblock, /* first block allocated */
530 xfs_bmap_free_t *flist, /* blocks to free at commit */
531 int *flags) /* inode logging flags */
533 xfs_da_args_t dargs; /* args for dir/attr code */
534 int error; /* error return value */
535 xfs_mount_t *mp; /* mount structure pointer */
537 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
539 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
541 memset(&dargs, 0, sizeof(dargs));
543 dargs.firstblock = firstblock;
545 dargs.total = mp->m_dirblkfsbs;
546 dargs.whichfork = XFS_DATA_FORK;
548 error = xfs_dir2_sf_to_block(&dargs);
550 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
556 * Called by xfs_bmapi to update file extent records and the btree
557 * after allocating space (or doing a delayed allocation).
559 STATIC int /* error */
561 xfs_inode_t *ip, /* incore inode pointer */
562 xfs_extnum_t idx, /* extent number to update/insert */
563 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
564 xfs_bmbt_irec_t *new, /* new data to add to file extents */
565 xfs_fsblock_t *first, /* pointer to firstblock variable */
566 xfs_bmap_free_t *flist, /* list of extents to be freed */
567 int *logflagsp, /* inode logging flags */
568 xfs_extdelta_t *delta, /* Change made to incore extents */
569 int whichfork, /* data or attr fork */
570 int rsvd) /* OK to use reserved data blocks */
572 xfs_btree_cur_t *cur; /* btree cursor or null */
573 xfs_filblks_t da_new; /* new count del alloc blocks used */
574 xfs_filblks_t da_old; /* old count del alloc blocks used */
575 int error; /* error return value */
576 xfs_ifork_t *ifp; /* inode fork ptr */
577 int logflags; /* returned value */
578 xfs_extnum_t nextents; /* number of extents in file now */
580 XFS_STATS_INC(xs_add_exlist);
582 ifp = XFS_IFORK_PTR(ip, whichfork);
583 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
584 ASSERT(idx <= nextents);
588 * This is the first extent added to a new/empty file.
589 * Special case this one, so other routines get to assume there are
590 * already extents in the list.
593 XFS_BMAP_TRACE_INSERT("insert empty", ip, 0, 1, new, NULL,
595 xfs_iext_insert(ifp, 0, 1, new);
598 if (!isnullstartblock(new->br_startblock)) {
599 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
600 logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
603 /* DELTA: single new extent */
605 if (delta->xed_startoff > new->br_startoff)
606 delta->xed_startoff = new->br_startoff;
607 if (delta->xed_blockcount <
608 new->br_startoff + new->br_blockcount)
609 delta->xed_blockcount = new->br_startoff +
614 * Any kind of new delayed allocation goes here.
616 else if (isnullstartblock(new->br_startblock)) {
618 ASSERT((cur->bc_private.b.flags &
619 XFS_BTCUR_BPRV_WASDEL) == 0);
620 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
621 &logflags, delta, rsvd)))
625 * Real allocation off the end of the file.
627 else if (idx == nextents) {
629 ASSERT((cur->bc_private.b.flags &
630 XFS_BTCUR_BPRV_WASDEL) == 0);
631 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
632 &logflags, delta, whichfork)))
635 xfs_bmbt_irec_t prev; /* old extent at offset idx */
638 * Get the record referred to by idx.
640 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
642 * If it's a real allocation record, and the new allocation ends
643 * after the start of the referred to record, then we're filling
644 * in a delayed or unwritten allocation with a real one, or
645 * converting real back to unwritten.
647 if (!isnullstartblock(new->br_startblock) &&
648 new->br_startoff + new->br_blockcount > prev.br_startoff) {
649 if (prev.br_state != XFS_EXT_UNWRITTEN &&
650 isnullstartblock(prev.br_startblock)) {
651 da_old = startblockval(prev.br_startblock);
653 ASSERT(cur->bc_private.b.flags &
654 XFS_BTCUR_BPRV_WASDEL);
655 if ((error = xfs_bmap_add_extent_delay_real(ip,
656 idx, &cur, new, &da_new, first, flist,
657 &logflags, delta, rsvd)))
659 } else if (new->br_state == XFS_EXT_NORM) {
660 ASSERT(new->br_state == XFS_EXT_NORM);
661 if ((error = xfs_bmap_add_extent_unwritten_real(
662 ip, idx, &cur, new, &logflags, delta)))
665 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
666 if ((error = xfs_bmap_add_extent_unwritten_real(
667 ip, idx, &cur, new, &logflags, delta)))
670 ASSERT(*curp == cur || *curp == NULL);
673 * Otherwise we're filling in a hole with an allocation.
677 ASSERT((cur->bc_private.b.flags &
678 XFS_BTCUR_BPRV_WASDEL) == 0);
679 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
680 new, &logflags, delta, whichfork)))
685 ASSERT(*curp == cur || *curp == NULL);
687 * Convert to a btree if necessary.
689 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
690 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
691 int tmp_logflags; /* partial log flag return val */
694 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
695 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
696 logflags |= tmp_logflags;
701 * Adjust for changes in reserved delayed indirect blocks.
702 * Nothing to do for disk quotas here.
704 if (da_old || da_new) {
709 nblks += cur->bc_private.b.allocated;
710 ASSERT(nblks <= da_old);
712 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
713 (int64_t)(da_old - nblks), rsvd);
716 * Clear out the allocated field, done with it now in any case.
719 cur->bc_private.b.allocated = 0;
725 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
727 *logflagsp = logflags;
732 * Called by xfs_bmap_add_extent to handle cases converting a delayed
733 * allocation to a real allocation.
735 STATIC int /* error */
736 xfs_bmap_add_extent_delay_real(
737 xfs_inode_t *ip, /* incore inode pointer */
738 xfs_extnum_t idx, /* extent number to update/insert */
739 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
740 xfs_bmbt_irec_t *new, /* new data to add to file extents */
741 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
742 xfs_fsblock_t *first, /* pointer to firstblock variable */
743 xfs_bmap_free_t *flist, /* list of extents to be freed */
744 int *logflagsp, /* inode logging flags */
745 xfs_extdelta_t *delta, /* Change made to incore extents */
746 int rsvd) /* OK to use reserved data block allocation */
748 xfs_btree_cur_t *cur; /* btree cursor */
749 int diff; /* temp value */
750 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
751 int error; /* error return value */
752 int i; /* temp state */
753 xfs_ifork_t *ifp; /* inode fork pointer */
754 xfs_fileoff_t new_endoff; /* end offset of new entry */
755 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
756 /* left is 0, right is 1, prev is 2 */
757 int rval=0; /* return value (logging flags) */
758 int state = 0;/* state bits, accessed thru macros */
759 xfs_filblks_t temp=0; /* value for dnew calculations */
760 xfs_filblks_t temp2=0;/* value for dnew calculations */
761 int tmp_rval; /* partial logging flags */
768 * Set up a bunch of variables to make the tests simpler.
771 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
772 ep = xfs_iext_get_ext(ifp, idx);
773 xfs_bmbt_get_all(ep, &PREV);
774 new_endoff = new->br_startoff + new->br_blockcount;
775 ASSERT(PREV.br_startoff <= new->br_startoff);
776 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
779 * Set flags determining what part of the previous delayed allocation
780 * extent is being replaced by a real allocation.
782 if (PREV.br_startoff == new->br_startoff)
783 state |= BMAP_LEFT_FILLING;
784 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
785 state |= BMAP_RIGHT_FILLING;
788 * Check and set flags if this segment has a left neighbor.
789 * Don't set contiguous if the combined extent would be too large.
792 state |= BMAP_LEFT_VALID;
793 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
795 if (isnullstartblock(LEFT.br_startblock))
796 state |= BMAP_LEFT_DELAY;
799 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
800 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
801 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
802 LEFT.br_state == new->br_state &&
803 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
804 state |= BMAP_LEFT_CONTIG;
807 * Check and set flags if this segment has a right neighbor.
808 * Don't set contiguous if the combined extent would be too large.
809 * Also check for all-three-contiguous being too large.
811 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
812 state |= BMAP_RIGHT_VALID;
813 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
815 if (isnullstartblock(RIGHT.br_startblock))
816 state |= BMAP_RIGHT_DELAY;
819 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
820 new_endoff == RIGHT.br_startoff &&
821 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
822 new->br_state == RIGHT.br_state &&
823 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
824 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
825 BMAP_RIGHT_FILLING)) !=
826 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
827 BMAP_RIGHT_FILLING) ||
828 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
830 state |= BMAP_RIGHT_CONTIG;
834 * Switch out based on the FILLING and CONTIG state bits.
836 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
837 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
838 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
839 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
841 * Filling in all of a previously delayed allocation extent.
842 * The left and right neighbors are both contiguous with new.
844 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
846 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
847 LEFT.br_blockcount + PREV.br_blockcount +
848 RIGHT.br_blockcount);
849 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
851 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
852 xfs_iext_remove(ifp, idx, 2);
853 ip->i_df.if_lastex = idx - 1;
854 ip->i_d.di_nextents--;
856 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
858 rval = XFS_ILOG_CORE;
859 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
861 RIGHT.br_blockcount, &i)))
863 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
864 if ((error = xfs_btree_delete(cur, &i)))
866 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
867 if ((error = xfs_btree_decrement(cur, 0, &i)))
869 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
870 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
874 RIGHT.br_blockcount, LEFT.br_state)))
878 /* DELTA: Three in-core extents are replaced by one. */
879 temp = LEFT.br_startoff;
880 temp2 = LEFT.br_blockcount +
885 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
887 * Filling in all of a previously delayed allocation extent.
888 * The left neighbor is contiguous, the right is not.
890 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
892 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
893 LEFT.br_blockcount + PREV.br_blockcount);
894 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
896 ip->i_df.if_lastex = idx - 1;
897 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
898 xfs_iext_remove(ifp, idx, 1);
900 rval = XFS_ILOG_DEXT;
903 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
904 LEFT.br_startblock, LEFT.br_blockcount,
907 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
908 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
911 PREV.br_blockcount, LEFT.br_state)))
915 /* DELTA: Two in-core extents are replaced by one. */
916 temp = LEFT.br_startoff;
917 temp2 = LEFT.br_blockcount +
921 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
923 * Filling in all of a previously delayed allocation extent.
924 * The right neighbor is contiguous, the left is not.
926 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
927 xfs_bmbt_set_startblock(ep, new->br_startblock);
928 xfs_bmbt_set_blockcount(ep,
929 PREV.br_blockcount + RIGHT.br_blockcount);
930 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
931 ip->i_df.if_lastex = idx;
932 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
933 xfs_iext_remove(ifp, idx + 1, 1);
935 rval = XFS_ILOG_DEXT;
938 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
940 RIGHT.br_blockcount, &i)))
942 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
943 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
946 RIGHT.br_blockcount, PREV.br_state)))
950 /* DELTA: Two in-core extents are replaced by one. */
951 temp = PREV.br_startoff;
952 temp2 = PREV.br_blockcount +
956 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
958 * Filling in all of a previously delayed allocation extent.
959 * Neither the left nor right neighbors are contiguous with
962 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
963 xfs_bmbt_set_startblock(ep, new->br_startblock);
964 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
965 ip->i_df.if_lastex = idx;
966 ip->i_d.di_nextents++;
968 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
970 rval = XFS_ILOG_CORE;
971 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
972 new->br_startblock, new->br_blockcount,
975 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
976 cur->bc_rec.b.br_state = XFS_EXT_NORM;
977 if ((error = xfs_btree_insert(cur, &i)))
979 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
982 /* DELTA: The in-core extent described by new changed type. */
983 temp = new->br_startoff;
984 temp2 = new->br_blockcount;
987 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
989 * Filling in the first part of a previous delayed allocation.
990 * The left neighbor is contiguous.
992 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
993 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
994 LEFT.br_blockcount + new->br_blockcount);
995 xfs_bmbt_set_startoff(ep,
996 PREV.br_startoff + new->br_blockcount);
997 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
998 temp = PREV.br_blockcount - new->br_blockcount;
999 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
1000 xfs_bmbt_set_blockcount(ep, temp);
1001 ip->i_df.if_lastex = idx - 1;
1003 rval = XFS_ILOG_DEXT;
1006 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
1007 LEFT.br_startblock, LEFT.br_blockcount,
1010 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1011 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1013 LEFT.br_blockcount +
1018 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1019 startblockval(PREV.br_startblock));
1020 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1021 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
1023 /* DELTA: The boundary between two in-core extents moved. */
1024 temp = LEFT.br_startoff;
1025 temp2 = LEFT.br_blockcount +
1029 case BMAP_LEFT_FILLING:
1031 * Filling in the first part of a previous delayed allocation.
1032 * The left neighbor is not contiguous.
1034 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1035 xfs_bmbt_set_startoff(ep, new_endoff);
1036 temp = PREV.br_blockcount - new->br_blockcount;
1037 xfs_bmbt_set_blockcount(ep, temp);
1038 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
1040 xfs_iext_insert(ifp, idx, 1, new);
1041 ip->i_df.if_lastex = idx;
1042 ip->i_d.di_nextents++;
1044 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1046 rval = XFS_ILOG_CORE;
1047 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1048 new->br_startblock, new->br_blockcount,
1051 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1052 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1053 if ((error = xfs_btree_insert(cur, &i)))
1055 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1057 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1058 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1059 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1060 first, flist, &cur, 1, &tmp_rval,
1066 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1067 startblockval(PREV.br_startblock) -
1068 (cur ? cur->bc_private.b.allocated : 0));
1069 ep = xfs_iext_get_ext(ifp, idx + 1);
1070 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1071 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx + 1, XFS_DATA_FORK);
1073 /* DELTA: One in-core extent is split in two. */
1074 temp = PREV.br_startoff;
1075 temp2 = PREV.br_blockcount;
1078 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1080 * Filling in the last part of a previous delayed allocation.
1081 * The right neighbor is contiguous with the new allocation.
1083 temp = PREV.br_blockcount - new->br_blockcount;
1084 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
1085 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
1086 xfs_bmbt_set_blockcount(ep, temp);
1087 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1088 new->br_startoff, new->br_startblock,
1089 new->br_blockcount + RIGHT.br_blockcount,
1091 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
1092 ip->i_df.if_lastex = idx + 1;
1094 rval = XFS_ILOG_DEXT;
1097 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1098 RIGHT.br_startblock,
1099 RIGHT.br_blockcount, &i)))
1101 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1102 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1104 new->br_blockcount +
1105 RIGHT.br_blockcount,
1109 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1110 startblockval(PREV.br_startblock));
1111 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1112 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
1114 /* DELTA: The boundary between two in-core extents moved. */
1115 temp = PREV.br_startoff;
1116 temp2 = PREV.br_blockcount +
1117 RIGHT.br_blockcount;
1120 case BMAP_RIGHT_FILLING:
1122 * Filling in the last part of a previous delayed allocation.
1123 * The right neighbor is not contiguous.
1125 temp = PREV.br_blockcount - new->br_blockcount;
1126 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1127 xfs_bmbt_set_blockcount(ep, temp);
1128 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1130 xfs_iext_insert(ifp, idx + 1, 1, new);
1131 ip->i_df.if_lastex = idx + 1;
1132 ip->i_d.di_nextents++;
1134 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1136 rval = XFS_ILOG_CORE;
1137 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1138 new->br_startblock, new->br_blockcount,
1141 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1142 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1143 if ((error = xfs_btree_insert(cur, &i)))
1145 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1147 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1148 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1149 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1150 first, flist, &cur, 1, &tmp_rval,
1156 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1157 startblockval(PREV.br_startblock) -
1158 (cur ? cur->bc_private.b.allocated : 0));
1159 ep = xfs_iext_get_ext(ifp, idx);
1160 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1161 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1163 /* DELTA: One in-core extent is split in two. */
1164 temp = PREV.br_startoff;
1165 temp2 = PREV.br_blockcount;
1170 * Filling in the middle part of a previous delayed allocation.
1171 * Contiguity is impossible here.
1172 * This case is avoided almost all the time.
1174 temp = new->br_startoff - PREV.br_startoff;
1175 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
1176 xfs_bmbt_set_blockcount(ep, temp);
1178 r[1].br_state = PREV.br_state;
1179 r[1].br_startblock = 0;
1180 r[1].br_startoff = new_endoff;
1181 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1182 r[1].br_blockcount = temp2;
1183 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
1185 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1186 ip->i_df.if_lastex = idx + 1;
1187 ip->i_d.di_nextents++;
1189 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1191 rval = XFS_ILOG_CORE;
1192 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1193 new->br_startblock, new->br_blockcount,
1196 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1197 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1198 if ((error = xfs_btree_insert(cur, &i)))
1200 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1202 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1203 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1204 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1205 first, flist, &cur, 1, &tmp_rval,
1211 temp = xfs_bmap_worst_indlen(ip, temp);
1212 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1213 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
1214 (cur ? cur->bc_private.b.allocated : 0));
1216 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
1218 * Ick gross gag me with a spoon.
1220 ASSERT(0); /* want to see if this ever happens! */
1226 !xfs_mod_incore_sb(ip->i_mount,
1227 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1234 !xfs_mod_incore_sb(ip->i_mount,
1235 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1240 ep = xfs_iext_get_ext(ifp, idx);
1241 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1242 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
1243 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
1244 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1245 nullstartblock((int)temp2));
1246 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
1247 *dnew = temp + temp2;
1248 /* DELTA: One in-core extent is split in three. */
1249 temp = PREV.br_startoff;
1250 temp2 = PREV.br_blockcount;
1253 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1254 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1255 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1256 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1257 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1258 case BMAP_LEFT_CONTIG:
1259 case BMAP_RIGHT_CONTIG:
1261 * These cases are all impossible.
1268 if (delta->xed_startoff > temp)
1269 delta->xed_startoff = temp;
1270 if (delta->xed_blockcount < temp2)
1271 delta->xed_blockcount = temp2;
1282 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1283 * allocation to a real allocation or vice versa.
1285 STATIC int /* error */
1286 xfs_bmap_add_extent_unwritten_real(
1287 xfs_inode_t *ip, /* incore inode pointer */
1288 xfs_extnum_t idx, /* extent number to update/insert */
1289 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1290 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1291 int *logflagsp, /* inode logging flags */
1292 xfs_extdelta_t *delta) /* Change made to incore extents */
1294 xfs_btree_cur_t *cur; /* btree cursor */
1295 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1296 int error; /* error return value */
1297 int i; /* temp state */
1298 xfs_ifork_t *ifp; /* inode fork pointer */
1299 xfs_fileoff_t new_endoff; /* end offset of new entry */
1300 xfs_exntst_t newext; /* new extent state */
1301 xfs_exntst_t oldext; /* old extent state */
1302 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1303 /* left is 0, right is 1, prev is 2 */
1304 int rval=0; /* return value (logging flags) */
1305 int state = 0;/* state bits, accessed thru macros */
1306 xfs_filblks_t temp=0;
1307 xfs_filblks_t temp2=0;
1313 * Set up a bunch of variables to make the tests simpler.
1317 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1318 ep = xfs_iext_get_ext(ifp, idx);
1319 xfs_bmbt_get_all(ep, &PREV);
1320 newext = new->br_state;
1321 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1322 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1323 ASSERT(PREV.br_state == oldext);
1324 new_endoff = new->br_startoff + new->br_blockcount;
1325 ASSERT(PREV.br_startoff <= new->br_startoff);
1326 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1329 * Set flags determining what part of the previous oldext allocation
1330 * extent is being replaced by a newext allocation.
1332 if (PREV.br_startoff == new->br_startoff)
1333 state |= BMAP_LEFT_FILLING;
1334 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1335 state |= BMAP_RIGHT_FILLING;
1338 * Check and set flags if this segment has a left neighbor.
1339 * Don't set contiguous if the combined extent would be too large.
1342 state |= BMAP_LEFT_VALID;
1343 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1345 if (isnullstartblock(LEFT.br_startblock))
1346 state |= BMAP_LEFT_DELAY;
1349 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1350 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1351 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1352 LEFT.br_state == newext &&
1353 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1354 state |= BMAP_LEFT_CONTIG;
1357 * Check and set flags if this segment has a right neighbor.
1358 * Don't set contiguous if the combined extent would be too large.
1359 * Also check for all-three-contiguous being too large.
1361 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1362 state |= BMAP_RIGHT_VALID;
1363 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1364 if (isnullstartblock(RIGHT.br_startblock))
1365 state |= BMAP_RIGHT_DELAY;
1368 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1369 new_endoff == RIGHT.br_startoff &&
1370 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1371 newext == RIGHT.br_state &&
1372 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1373 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1374 BMAP_RIGHT_FILLING)) !=
1375 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1376 BMAP_RIGHT_FILLING) ||
1377 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1379 state |= BMAP_RIGHT_CONTIG;
1382 * Switch out based on the FILLING and CONTIG state bits.
1384 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1385 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1386 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1387 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1389 * Setting all of a previous oldext extent to newext.
1390 * The left and right neighbors are both contiguous with new.
1392 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
1394 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1395 LEFT.br_blockcount + PREV.br_blockcount +
1396 RIGHT.br_blockcount);
1397 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
1399 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
1400 xfs_iext_remove(ifp, idx, 2);
1401 ip->i_df.if_lastex = idx - 1;
1402 ip->i_d.di_nextents -= 2;
1404 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1406 rval = XFS_ILOG_CORE;
1407 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1408 RIGHT.br_startblock,
1409 RIGHT.br_blockcount, &i)))
1411 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1412 if ((error = xfs_btree_delete(cur, &i)))
1414 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1415 if ((error = xfs_btree_decrement(cur, 0, &i)))
1417 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1418 if ((error = xfs_btree_delete(cur, &i)))
1420 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1421 if ((error = xfs_btree_decrement(cur, 0, &i)))
1423 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1424 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1426 LEFT.br_blockcount + PREV.br_blockcount +
1427 RIGHT.br_blockcount, LEFT.br_state)))
1430 /* DELTA: Three in-core extents are replaced by one. */
1431 temp = LEFT.br_startoff;
1432 temp2 = LEFT.br_blockcount +
1433 PREV.br_blockcount +
1434 RIGHT.br_blockcount;
1437 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1439 * Setting all of a previous oldext extent to newext.
1440 * The left neighbor is contiguous, the right is not.
1442 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
1444 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1445 LEFT.br_blockcount + PREV.br_blockcount);
1446 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
1448 ip->i_df.if_lastex = idx - 1;
1449 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
1450 xfs_iext_remove(ifp, idx, 1);
1451 ip->i_d.di_nextents--;
1453 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1455 rval = XFS_ILOG_CORE;
1456 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1457 PREV.br_startblock, PREV.br_blockcount,
1460 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1461 if ((error = xfs_btree_delete(cur, &i)))
1463 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1464 if ((error = xfs_btree_decrement(cur, 0, &i)))
1466 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1467 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1469 LEFT.br_blockcount + PREV.br_blockcount,
1473 /* DELTA: Two in-core extents are replaced by one. */
1474 temp = LEFT.br_startoff;
1475 temp2 = LEFT.br_blockcount +
1479 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1481 * Setting all of a previous oldext extent to newext.
1482 * The right neighbor is contiguous, the left is not.
1484 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx,
1486 xfs_bmbt_set_blockcount(ep,
1487 PREV.br_blockcount + RIGHT.br_blockcount);
1488 xfs_bmbt_set_state(ep, newext);
1489 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx,
1491 ip->i_df.if_lastex = idx;
1492 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
1493 xfs_iext_remove(ifp, idx + 1, 1);
1494 ip->i_d.di_nextents--;
1496 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1498 rval = XFS_ILOG_CORE;
1499 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1500 RIGHT.br_startblock,
1501 RIGHT.br_blockcount, &i)))
1503 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1504 if ((error = xfs_btree_delete(cur, &i)))
1506 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1507 if ((error = xfs_btree_decrement(cur, 0, &i)))
1509 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1510 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1512 new->br_blockcount + RIGHT.br_blockcount,
1516 /* DELTA: Two in-core extents are replaced by one. */
1517 temp = PREV.br_startoff;
1518 temp2 = PREV.br_blockcount +
1519 RIGHT.br_blockcount;
1522 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1524 * Setting all of a previous oldext extent to newext.
1525 * Neither the left nor right neighbors are contiguous with
1528 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx,
1530 xfs_bmbt_set_state(ep, newext);
1531 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx,
1533 ip->i_df.if_lastex = idx;
1535 rval = XFS_ILOG_DEXT;
1538 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1539 new->br_startblock, new->br_blockcount,
1542 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1543 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1544 new->br_startblock, new->br_blockcount,
1548 /* DELTA: The in-core extent described by new changed type. */
1549 temp = new->br_startoff;
1550 temp2 = new->br_blockcount;
1553 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1555 * Setting the first part of a previous oldext extent to newext.
1556 * The left neighbor is contiguous.
1558 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1,
1560 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1561 LEFT.br_blockcount + new->br_blockcount);
1562 xfs_bmbt_set_startoff(ep,
1563 PREV.br_startoff + new->br_blockcount);
1564 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1,
1566 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx,
1568 xfs_bmbt_set_startblock(ep,
1569 new->br_startblock + new->br_blockcount);
1570 xfs_bmbt_set_blockcount(ep,
1571 PREV.br_blockcount - new->br_blockcount);
1572 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx,
1574 ip->i_df.if_lastex = idx - 1;
1576 rval = XFS_ILOG_DEXT;
1579 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1580 PREV.br_startblock, PREV.br_blockcount,
1583 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1584 if ((error = xfs_bmbt_update(cur,
1585 PREV.br_startoff + new->br_blockcount,
1586 PREV.br_startblock + new->br_blockcount,
1587 PREV.br_blockcount - new->br_blockcount,
1590 if ((error = xfs_btree_decrement(cur, 0, &i)))
1592 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1594 LEFT.br_blockcount + new->br_blockcount,
1598 /* DELTA: The boundary between two in-core extents moved. */
1599 temp = LEFT.br_startoff;
1600 temp2 = LEFT.br_blockcount +
1604 case BMAP_LEFT_FILLING:
1606 * Setting the first part of a previous oldext extent to newext.
1607 * The left neighbor is not contiguous.
1609 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1610 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1611 xfs_bmbt_set_startoff(ep, new_endoff);
1612 xfs_bmbt_set_blockcount(ep,
1613 PREV.br_blockcount - new->br_blockcount);
1614 xfs_bmbt_set_startblock(ep,
1615 new->br_startblock + new->br_blockcount);
1616 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1617 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
1619 xfs_iext_insert(ifp, idx, 1, new);
1620 ip->i_df.if_lastex = idx;
1621 ip->i_d.di_nextents++;
1623 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1625 rval = XFS_ILOG_CORE;
1626 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1627 PREV.br_startblock, PREV.br_blockcount,
1630 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1631 if ((error = xfs_bmbt_update(cur,
1632 PREV.br_startoff + new->br_blockcount,
1633 PREV.br_startblock + new->br_blockcount,
1634 PREV.br_blockcount - new->br_blockcount,
1637 cur->bc_rec.b = *new;
1638 if ((error = xfs_btree_insert(cur, &i)))
1640 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1642 /* DELTA: One in-core extent is split in two. */
1643 temp = PREV.br_startoff;
1644 temp2 = PREV.br_blockcount;
1647 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1649 * Setting the last part of a previous oldext extent to newext.
1650 * The right neighbor is contiguous with the new allocation.
1652 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx,
1654 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1,
1656 xfs_bmbt_set_blockcount(ep,
1657 PREV.br_blockcount - new->br_blockcount);
1658 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx,
1660 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1661 new->br_startoff, new->br_startblock,
1662 new->br_blockcount + RIGHT.br_blockcount, newext);
1663 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1,
1665 ip->i_df.if_lastex = idx + 1;
1667 rval = XFS_ILOG_DEXT;
1670 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1672 PREV.br_blockcount, &i)))
1674 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1675 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1677 PREV.br_blockcount - new->br_blockcount,
1680 if ((error = xfs_btree_increment(cur, 0, &i)))
1682 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1684 new->br_blockcount + RIGHT.br_blockcount,
1688 /* DELTA: The boundary between two in-core extents moved. */
1689 temp = PREV.br_startoff;
1690 temp2 = PREV.br_blockcount +
1691 RIGHT.br_blockcount;
1694 case BMAP_RIGHT_FILLING:
1696 * Setting the last part of a previous oldext extent to newext.
1697 * The right neighbor is not contiguous.
1699 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1700 xfs_bmbt_set_blockcount(ep,
1701 PREV.br_blockcount - new->br_blockcount);
1702 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1703 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1705 xfs_iext_insert(ifp, idx + 1, 1, new);
1706 ip->i_df.if_lastex = idx + 1;
1707 ip->i_d.di_nextents++;
1709 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1711 rval = XFS_ILOG_CORE;
1712 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1713 PREV.br_startblock, PREV.br_blockcount,
1716 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1717 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1719 PREV.br_blockcount - new->br_blockcount,
1722 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1723 new->br_startblock, new->br_blockcount,
1726 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1727 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1728 if ((error = xfs_btree_insert(cur, &i)))
1730 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1732 /* DELTA: One in-core extent is split in two. */
1733 temp = PREV.br_startoff;
1734 temp2 = PREV.br_blockcount;
1739 * Setting the middle part of a previous oldext extent to
1740 * newext. Contiguity is impossible here.
1741 * One extent becomes three extents.
1743 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
1744 xfs_bmbt_set_blockcount(ep,
1745 new->br_startoff - PREV.br_startoff);
1746 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
1748 r[1].br_startoff = new_endoff;
1749 r[1].br_blockcount =
1750 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1751 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1752 r[1].br_state = oldext;
1753 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
1755 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1756 ip->i_df.if_lastex = idx + 1;
1757 ip->i_d.di_nextents += 2;
1759 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1761 rval = XFS_ILOG_CORE;
1762 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1763 PREV.br_startblock, PREV.br_blockcount,
1766 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1767 /* new right extent - oldext */
1768 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1769 r[1].br_startblock, r[1].br_blockcount,
1772 /* new left extent - oldext */
1773 cur->bc_rec.b = PREV;
1774 cur->bc_rec.b.br_blockcount =
1775 new->br_startoff - PREV.br_startoff;
1776 if ((error = xfs_btree_insert(cur, &i)))
1778 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1780 * Reset the cursor to the position of the new extent
1781 * we are about to insert as we can't trust it after
1782 * the previous insert.
1784 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1785 new->br_startblock, new->br_blockcount,
1788 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1789 /* new middle extent - newext */
1790 cur->bc_rec.b.br_state = new->br_state;
1791 if ((error = xfs_btree_insert(cur, &i)))
1793 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1795 /* DELTA: One in-core extent is split in three. */
1796 temp = PREV.br_startoff;
1797 temp2 = PREV.br_blockcount;
1800 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1801 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1802 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1803 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1804 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1805 case BMAP_LEFT_CONTIG:
1806 case BMAP_RIGHT_CONTIG:
1808 * These cases are all impossible.
1815 if (delta->xed_startoff > temp)
1816 delta->xed_startoff = temp;
1817 if (delta->xed_blockcount < temp2)
1818 delta->xed_blockcount = temp2;
1829 * Called by xfs_bmap_add_extent to handle cases converting a hole
1830 * to a delayed allocation.
1833 STATIC int /* error */
1834 xfs_bmap_add_extent_hole_delay(
1835 xfs_inode_t *ip, /* incore inode pointer */
1836 xfs_extnum_t idx, /* extent number to update/insert */
1837 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1838 int *logflagsp, /* inode logging flags */
1839 xfs_extdelta_t *delta, /* Change made to incore extents */
1840 int rsvd) /* OK to allocate reserved blocks */
1842 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
1843 xfs_ifork_t *ifp; /* inode fork pointer */
1844 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1845 xfs_filblks_t newlen=0; /* new indirect size */
1846 xfs_filblks_t oldlen=0; /* old indirect size */
1847 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1848 int state; /* state bits, accessed thru macros */
1849 xfs_filblks_t temp=0; /* temp for indirect calculations */
1850 xfs_filblks_t temp2=0;
1852 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1853 ep = xfs_iext_get_ext(ifp, idx);
1855 ASSERT(isnullstartblock(new->br_startblock));
1858 * Check and set flags if this segment has a left neighbor
1861 state |= BMAP_LEFT_VALID;
1862 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1864 if (isnullstartblock(left.br_startblock))
1865 state |= BMAP_LEFT_DELAY;
1869 * Check and set flags if the current (right) segment exists.
1870 * If it doesn't exist, we're converting the hole at end-of-file.
1872 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1873 state |= BMAP_RIGHT_VALID;
1874 xfs_bmbt_get_all(ep, &right);
1876 if (isnullstartblock(right.br_startblock))
1877 state |= BMAP_RIGHT_DELAY;
1881 * Set contiguity flags on the left and right neighbors.
1882 * Don't let extents get too large, even if the pieces are contiguous.
1884 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1885 left.br_startoff + left.br_blockcount == new->br_startoff &&
1886 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1887 state |= BMAP_LEFT_CONTIG;
1889 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1890 new->br_startoff + new->br_blockcount == right.br_startoff &&
1891 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1892 (!(state & BMAP_LEFT_CONTIG) ||
1893 (left.br_blockcount + new->br_blockcount +
1894 right.br_blockcount <= MAXEXTLEN)))
1895 state |= BMAP_RIGHT_CONTIG;
1898 * Switch out based on the contiguity flags.
1900 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1901 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1903 * New allocation is contiguous with delayed allocations
1904 * on the left and on the right.
1905 * Merge all three into a single extent record.
1907 temp = left.br_blockcount + new->br_blockcount +
1908 right.br_blockcount;
1909 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
1911 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1912 oldlen = startblockval(left.br_startblock) +
1913 startblockval(new->br_startblock) +
1914 startblockval(right.br_startblock);
1915 newlen = xfs_bmap_worst_indlen(ip, temp);
1916 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1917 nullstartblock((int)newlen));
1918 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
1920 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, XFS_DATA_FORK);
1921 xfs_iext_remove(ifp, idx, 1);
1922 ip->i_df.if_lastex = idx - 1;
1923 /* DELTA: Two in-core extents were replaced by one. */
1925 temp = left.br_startoff;
1928 case BMAP_LEFT_CONTIG:
1930 * New allocation is contiguous with a delayed allocation
1932 * Merge the new allocation with the left neighbor.
1934 temp = left.br_blockcount + new->br_blockcount;
1935 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1,
1937 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1938 oldlen = startblockval(left.br_startblock) +
1939 startblockval(new->br_startblock);
1940 newlen = xfs_bmap_worst_indlen(ip, temp);
1941 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1942 nullstartblock((int)newlen));
1943 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1,
1945 ip->i_df.if_lastex = idx - 1;
1946 /* DELTA: One in-core extent grew into a hole. */
1948 temp = left.br_startoff;
1951 case BMAP_RIGHT_CONTIG:
1953 * New allocation is contiguous with a delayed allocation
1955 * Merge the new allocation with the right neighbor.
1957 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, XFS_DATA_FORK);
1958 temp = new->br_blockcount + right.br_blockcount;
1959 oldlen = startblockval(new->br_startblock) +
1960 startblockval(right.br_startblock);
1961 newlen = xfs_bmap_worst_indlen(ip, temp);
1962 xfs_bmbt_set_allf(ep, new->br_startoff,
1963 nullstartblock((int)newlen), temp, right.br_state);
1964 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, XFS_DATA_FORK);
1965 ip->i_df.if_lastex = idx;
1966 /* DELTA: One in-core extent grew into a hole. */
1968 temp = new->br_startoff;
1973 * New allocation is not contiguous with another
1974 * delayed allocation.
1975 * Insert a new entry.
1977 oldlen = newlen = 0;
1978 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL,
1980 xfs_iext_insert(ifp, idx, 1, new);
1981 ip->i_df.if_lastex = idx;
1982 /* DELTA: A new in-core extent was added in a hole. */
1983 temp2 = new->br_blockcount;
1984 temp = new->br_startoff;
1987 if (oldlen != newlen) {
1988 ASSERT(oldlen > newlen);
1989 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
1990 (int64_t)(oldlen - newlen), rsvd);
1992 * Nothing to do for disk quota accounting here.
1997 if (delta->xed_startoff > temp)
1998 delta->xed_startoff = temp;
1999 if (delta->xed_blockcount < temp2)
2000 delta->xed_blockcount = temp2;
2007 * Called by xfs_bmap_add_extent to handle cases converting a hole
2008 * to a real allocation.
2010 STATIC int /* error */
2011 xfs_bmap_add_extent_hole_real(
2012 xfs_inode_t *ip, /* incore inode pointer */
2013 xfs_extnum_t idx, /* extent number to update/insert */
2014 xfs_btree_cur_t *cur, /* if null, not a btree */
2015 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2016 int *logflagsp, /* inode logging flags */
2017 xfs_extdelta_t *delta, /* Change made to incore extents */
2018 int whichfork) /* data or attr fork */
2020 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
2021 int error; /* error return value */
2022 int i; /* temp state */
2023 xfs_ifork_t *ifp; /* inode fork pointer */
2024 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2025 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2026 int rval=0; /* return value (logging flags) */
2027 int state; /* state bits, accessed thru macros */
2028 xfs_filblks_t temp=0;
2029 xfs_filblks_t temp2=0;
2031 ifp = XFS_IFORK_PTR(ip, whichfork);
2032 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
2033 ep = xfs_iext_get_ext(ifp, idx);
2037 * Check and set flags if this segment has a left neighbor.
2040 state |= BMAP_LEFT_VALID;
2041 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
2042 if (isnullstartblock(left.br_startblock))
2043 state |= BMAP_LEFT_DELAY;
2047 * Check and set flags if this segment has a current value.
2048 * Not true if we're inserting into the "hole" at eof.
2050 if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
2051 state |= BMAP_RIGHT_VALID;
2052 xfs_bmbt_get_all(ep, &right);
2053 if (isnullstartblock(right.br_startblock))
2054 state |= BMAP_RIGHT_DELAY;
2058 * We're inserting a real allocation between "left" and "right".
2059 * Set the contiguity flags. Don't let extents get too large.
2061 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2062 left.br_startoff + left.br_blockcount == new->br_startoff &&
2063 left.br_startblock + left.br_blockcount == new->br_startblock &&
2064 left.br_state == new->br_state &&
2065 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2066 state |= BMAP_LEFT_CONTIG;
2068 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2069 new->br_startoff + new->br_blockcount == right.br_startoff &&
2070 new->br_startblock + new->br_blockcount == right.br_startblock &&
2071 new->br_state == right.br_state &&
2072 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2073 (!(state & BMAP_LEFT_CONTIG) ||
2074 left.br_blockcount + new->br_blockcount +
2075 right.br_blockcount <= MAXEXTLEN))
2076 state |= BMAP_RIGHT_CONTIG;
2080 * Select which case we're in here, and implement it.
2082 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2083 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2085 * New allocation is contiguous with real allocations on the
2086 * left and on the right.
2087 * Merge all three into a single extent record.
2089 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
2091 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2092 left.br_blockcount + new->br_blockcount +
2093 right.br_blockcount);
2094 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
2096 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, whichfork);
2097 xfs_iext_remove(ifp, idx, 1);
2098 ifp->if_lastex = idx - 1;
2099 XFS_IFORK_NEXT_SET(ip, whichfork,
2100 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2102 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2104 rval = XFS_ILOG_CORE;
2105 if ((error = xfs_bmbt_lookup_eq(cur,
2107 right.br_startblock,
2108 right.br_blockcount, &i)))
2110 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2111 if ((error = xfs_btree_delete(cur, &i)))
2113 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2114 if ((error = xfs_btree_decrement(cur, 0, &i)))
2116 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2117 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2119 left.br_blockcount +
2120 new->br_blockcount +
2121 right.br_blockcount,
2125 /* DELTA: Two in-core extents were replaced by one. */
2126 temp = left.br_startoff;
2127 temp2 = left.br_blockcount +
2128 new->br_blockcount +
2129 right.br_blockcount;
2132 case BMAP_LEFT_CONTIG:
2134 * New allocation is contiguous with a real allocation
2136 * Merge the new allocation with the left neighbor.
2138 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1, whichfork);
2139 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2140 left.br_blockcount + new->br_blockcount);
2141 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1, whichfork);
2142 ifp->if_lastex = idx - 1;
2144 rval = xfs_ilog_fext(whichfork);
2147 if ((error = xfs_bmbt_lookup_eq(cur,
2150 left.br_blockcount, &i)))
2152 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2153 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2155 left.br_blockcount +
2160 /* DELTA: One in-core extent grew. */
2161 temp = left.br_startoff;
2162 temp2 = left.br_blockcount +
2166 case BMAP_RIGHT_CONTIG:
2168 * New allocation is contiguous with a real allocation
2170 * Merge the new allocation with the right neighbor.
2172 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, whichfork);
2173 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2174 new->br_blockcount + right.br_blockcount,
2176 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, whichfork);
2177 ifp->if_lastex = idx;
2179 rval = xfs_ilog_fext(whichfork);
2182 if ((error = xfs_bmbt_lookup_eq(cur,
2184 right.br_startblock,
2185 right.br_blockcount, &i)))
2187 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2188 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2190 new->br_blockcount +
2191 right.br_blockcount,
2195 /* DELTA: One in-core extent grew. */
2196 temp = new->br_startoff;
2197 temp2 = new->br_blockcount +
2198 right.br_blockcount;
2203 * New allocation is not contiguous with another
2205 * Insert a new entry.
2207 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL, whichfork);
2208 xfs_iext_insert(ifp, idx, 1, new);
2209 ifp->if_lastex = idx;
2210 XFS_IFORK_NEXT_SET(ip, whichfork,
2211 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2213 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2215 rval = XFS_ILOG_CORE;
2216 if ((error = xfs_bmbt_lookup_eq(cur,
2219 new->br_blockcount, &i)))
2221 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
2222 cur->bc_rec.b.br_state = new->br_state;
2223 if ((error = xfs_btree_insert(cur, &i)))
2225 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2227 /* DELTA: A new extent was added in a hole. */
2228 temp = new->br_startoff;
2229 temp2 = new->br_blockcount;
2234 if (delta->xed_startoff > temp)
2235 delta->xed_startoff = temp;
2236 if (delta->xed_blockcount < temp2)
2237 delta->xed_blockcount = temp2;
2245 * Adjust the size of the new extent based on di_extsize and rt extsize.
2248 xfs_bmap_extsize_align(
2250 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2251 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2252 xfs_extlen_t extsz, /* align to this extent size */
2253 int rt, /* is this a realtime inode? */
2254 int eof, /* is extent at end-of-file? */
2255 int delay, /* creating delalloc extent? */
2256 int convert, /* overwriting unwritten extent? */
2257 xfs_fileoff_t *offp, /* in/out: aligned offset */
2258 xfs_extlen_t *lenp) /* in/out: aligned length */
2260 xfs_fileoff_t orig_off; /* original offset */
2261 xfs_extlen_t orig_alen; /* original length */
2262 xfs_fileoff_t orig_end; /* original off+len */
2263 xfs_fileoff_t nexto; /* next file offset */
2264 xfs_fileoff_t prevo; /* previous file offset */
2265 xfs_fileoff_t align_off; /* temp for offset */
2266 xfs_extlen_t align_alen; /* temp for length */
2267 xfs_extlen_t temp; /* temp for calculations */
2272 orig_off = align_off = *offp;
2273 orig_alen = align_alen = *lenp;
2274 orig_end = orig_off + orig_alen;
2277 * If this request overlaps an existing extent, then don't
2278 * attempt to perform any additional alignment.
2280 if (!delay && !eof &&
2281 (orig_off >= gotp->br_startoff) &&
2282 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2287 * If the file offset is unaligned vs. the extent size
2288 * we need to align it. This will be possible unless
2289 * the file was previously written with a kernel that didn't
2290 * perform this alignment, or if a truncate shot us in the
2293 temp = do_mod(orig_off, extsz);
2299 * Same adjustment for the end of the requested area.
2301 if ((temp = (align_alen % extsz))) {
2302 align_alen += extsz - temp;
2305 * If the previous block overlaps with this proposed allocation
2306 * then move the start forward without adjusting the length.
2308 if (prevp->br_startoff != NULLFILEOFF) {
2309 if (prevp->br_startblock == HOLESTARTBLOCK)
2310 prevo = prevp->br_startoff;
2312 prevo = prevp->br_startoff + prevp->br_blockcount;
2315 if (align_off != orig_off && align_off < prevo)
2318 * If the next block overlaps with this proposed allocation
2319 * then move the start back without adjusting the length,
2320 * but not before offset 0.
2321 * This may of course make the start overlap previous block,
2322 * and if we hit the offset 0 limit then the next block
2323 * can still overlap too.
2325 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2326 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2327 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2328 nexto = gotp->br_startoff + gotp->br_blockcount;
2330 nexto = gotp->br_startoff;
2332 nexto = NULLFILEOFF;
2334 align_off + align_alen != orig_end &&
2335 align_off + align_alen > nexto)
2336 align_off = nexto > align_alen ? nexto - align_alen : 0;
2338 * If we're now overlapping the next or previous extent that
2339 * means we can't fit an extsz piece in this hole. Just move
2340 * the start forward to the first valid spot and set
2341 * the length so we hit the end.
2343 if (align_off != orig_off && align_off < prevo)
2345 if (align_off + align_alen != orig_end &&
2346 align_off + align_alen > nexto &&
2347 nexto != NULLFILEOFF) {
2348 ASSERT(nexto > prevo);
2349 align_alen = nexto - align_off;
2353 * If realtime, and the result isn't a multiple of the realtime
2354 * extent size we need to remove blocks until it is.
2356 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2358 * We're not covering the original request, or
2359 * we won't be able to once we fix the length.
2361 if (orig_off < align_off ||
2362 orig_end > align_off + align_alen ||
2363 align_alen - temp < orig_alen)
2364 return XFS_ERROR(EINVAL);
2366 * Try to fix it by moving the start up.
2368 if (align_off + temp <= orig_off) {
2373 * Try to fix it by moving the end in.
2375 else if (align_off + align_alen - temp >= orig_end)
2378 * Set the start to the minimum then trim the length.
2381 align_alen -= orig_off - align_off;
2382 align_off = orig_off;
2383 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2386 * Result doesn't cover the request, fail it.
2388 if (orig_off < align_off || orig_end > align_off + align_alen)
2389 return XFS_ERROR(EINVAL);
2391 ASSERT(orig_off >= align_off);
2392 ASSERT(orig_end <= align_off + align_alen);
2396 if (!eof && gotp->br_startoff != NULLFILEOFF)
2397 ASSERT(align_off + align_alen <= gotp->br_startoff);
2398 if (prevp->br_startoff != NULLFILEOFF)
2399 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2407 #define XFS_ALLOC_GAP_UNITS 4
2411 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2413 xfs_fsblock_t adjust; /* adjustment to block numbers */
2414 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2415 xfs_mount_t *mp; /* mount point structure */
2416 int nullfb; /* true if ap->firstblock isn't set */
2417 int rt; /* true if inode is realtime */
2419 #define ISVALID(x,y) \
2421 (x) < mp->m_sb.sb_rblocks : \
2422 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2423 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2424 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2426 mp = ap->ip->i_mount;
2427 nullfb = ap->firstblock == NULLFSBLOCK;
2428 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2429 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2431 * If allocating at eof, and there's a previous real block,
2432 * try to use its last block as our starting point.
2434 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2435 !isnullstartblock(ap->prevp->br_startblock) &&
2436 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2437 ap->prevp->br_startblock)) {
2438 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2440 * Adjust for the gap between prevp and us.
2443 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2445 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2449 * If not at eof, then compare the two neighbor blocks.
2450 * Figure out whether either one gives us a good starting point,
2451 * and pick the better one.
2453 else if (!ap->eof) {
2454 xfs_fsblock_t gotbno; /* right side block number */
2455 xfs_fsblock_t gotdiff=0; /* right side difference */
2456 xfs_fsblock_t prevbno; /* left side block number */
2457 xfs_fsblock_t prevdiff=0; /* left side difference */
2460 * If there's a previous (left) block, select a requested
2461 * start block based on it.
2463 if (ap->prevp->br_startoff != NULLFILEOFF &&
2464 !isnullstartblock(ap->prevp->br_startblock) &&
2465 (prevbno = ap->prevp->br_startblock +
2466 ap->prevp->br_blockcount) &&
2467 ISVALID(prevbno, ap->prevp->br_startblock)) {
2469 * Calculate gap to end of previous block.
2471 adjust = prevdiff = ap->off -
2472 (ap->prevp->br_startoff +
2473 ap->prevp->br_blockcount);
2475 * Figure the startblock based on the previous block's
2476 * end and the gap size.
2478 * If the gap is large relative to the piece we're
2479 * allocating, or using it gives us an invalid block
2480 * number, then just use the end of the previous block.
2482 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2483 ISVALID(prevbno + prevdiff,
2484 ap->prevp->br_startblock))
2489 * If the firstblock forbids it, can't use it,
2492 if (!rt && !nullfb &&
2493 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2494 prevbno = NULLFSBLOCK;
2497 * No previous block or can't follow it, just default.
2500 prevbno = NULLFSBLOCK;
2502 * If there's a following (right) block, select a requested
2503 * start block based on it.
2505 if (!isnullstartblock(ap->gotp->br_startblock)) {
2507 * Calculate gap to start of next block.
2509 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2511 * Figure the startblock based on the next block's
2512 * start and the gap size.
2514 gotbno = ap->gotp->br_startblock;
2517 * If the gap is large relative to the piece we're
2518 * allocating, or using it gives us an invalid block
2519 * number, then just use the start of the next block
2520 * offset by our length.
2522 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2523 ISVALID(gotbno - gotdiff, gotbno))
2525 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2527 gotdiff += adjust - ap->alen;
2531 * If the firstblock forbids it, can't use it,
2534 if (!rt && !nullfb &&
2535 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2536 gotbno = NULLFSBLOCK;
2539 * No next block, just default.
2542 gotbno = NULLFSBLOCK;
2544 * If both valid, pick the better one, else the only good
2545 * one, else ap->rval is already set (to 0 or the inode block).
2547 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2548 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2549 else if (prevbno != NULLFSBLOCK)
2551 else if (gotbno != NULLFSBLOCK)
2559 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2561 xfs_alloctype_t atype = 0; /* type for allocation routines */
2562 int error; /* error return value */
2563 xfs_mount_t *mp; /* mount point structure */
2564 xfs_extlen_t prod = 0; /* product factor for allocators */
2565 xfs_extlen_t ralen = 0; /* realtime allocation length */
2566 xfs_extlen_t align; /* minimum allocation alignment */
2569 mp = ap->ip->i_mount;
2570 align = xfs_get_extsz_hint(ap->ip);
2571 prod = align / mp->m_sb.sb_rextsize;
2572 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2573 align, 1, ap->eof, 0,
2574 ap->conv, &ap->off, &ap->alen);
2578 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2581 * If the offset & length are not perfectly aligned
2582 * then kill prod, it will just get us in trouble.
2584 if (do_mod(ap->off, align) || ap->alen % align)
2587 * Set ralen to be the actual requested length in rtextents.
2589 ralen = ap->alen / mp->m_sb.sb_rextsize;
2591 * If the old value was close enough to MAXEXTLEN that
2592 * we rounded up to it, cut it back so it's valid again.
2593 * Note that if it's a really large request (bigger than
2594 * MAXEXTLEN), we don't hear about that number, and can't
2595 * adjust the starting point to match it.
2597 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2598 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2600 * If it's an allocation to an empty file at offset 0,
2601 * pick an extent that will space things out in the rt area.
2603 if (ap->eof && ap->off == 0) {
2604 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2606 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2609 ap->rval = rtx * mp->m_sb.sb_rextsize;
2614 xfs_bmap_adjacent(ap);
2617 * Realtime allocation, done through xfs_rtallocate_extent.
2619 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2620 do_div(ap->rval, mp->m_sb.sb_rextsize);
2623 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2624 &ralen, atype, ap->wasdel, prod, &rtb)))
2626 if (rtb == NULLFSBLOCK && prod > 1 &&
2627 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2628 ap->alen, &ralen, atype,
2629 ap->wasdel, 1, &rtb)))
2632 if (ap->rval != NULLFSBLOCK) {
2633 ap->rval *= mp->m_sb.sb_rextsize;
2634 ralen *= mp->m_sb.sb_rextsize;
2636 ap->ip->i_d.di_nblocks += ralen;
2637 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2639 ap->ip->i_delayed_blks -= ralen;
2641 * Adjust the disk quota also. This was reserved
2644 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2645 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2646 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2655 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2657 xfs_mount_t *mp; /* mount point structure */
2658 xfs_alloctype_t atype = 0; /* type for allocation routines */
2659 xfs_extlen_t align; /* minimum allocation alignment */
2661 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2662 xfs_agnumber_t startag;
2663 xfs_alloc_arg_t args;
2665 xfs_extlen_t nextminlen = 0;
2667 int nullfb; /* true if ap->firstblock isn't set */
2673 mp = ap->ip->i_mount;
2674 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2675 if (unlikely(align)) {
2676 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2677 align, 0, ap->eof, 0, ap->conv,
2678 &ap->off, &ap->alen);
2682 nullfb = ap->firstblock == NULLFSBLOCK;
2683 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2685 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2686 ag = xfs_filestream_lookup_ag(ap->ip);
2687 ag = (ag != NULLAGNUMBER) ? ag : 0;
2688 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2690 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2693 ap->rval = ap->firstblock;
2695 xfs_bmap_adjacent(ap);
2698 * If allowed, use ap->rval; otherwise must use firstblock since
2699 * it's in the right allocation group.
2701 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2704 ap->rval = ap->firstblock;
2706 * Normal allocation, done through xfs_alloc_vextent.
2708 tryagain = isaligned = 0;
2711 args.fsbno = ap->rval;
2712 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2713 args.firstblock = ap->firstblock;
2716 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2717 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2719 args.type = XFS_ALLOCTYPE_START_BNO;
2720 args.total = ap->total;
2723 * Search for an allocation group with a single extent
2724 * large enough for the request.
2726 * If one isn't found, then adjust the minimum allocation
2727 * size to the largest space found.
2729 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2730 if (startag == NULLAGNUMBER)
2733 down_read(&mp->m_peraglock);
2734 while (blen < ap->alen) {
2735 pag = &mp->m_perag[ag];
2736 if (!pag->pagf_init &&
2737 (error = xfs_alloc_pagf_init(mp, args.tp,
2738 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2739 up_read(&mp->m_peraglock);
2743 * See xfs_alloc_fix_freelist...
2745 if (pag->pagf_init) {
2746 xfs_extlen_t longest;
2747 longest = xfs_alloc_longest_free_extent(mp, pag);
2753 if (xfs_inode_is_filestream(ap->ip)) {
2754 if (blen >= ap->alen)
2759 * If startag is an invalid AG, we've
2760 * come here once before and
2761 * xfs_filestream_new_ag picked the
2762 * best currently available.
2764 * Don't continue looping, since we
2765 * could loop forever.
2767 if (startag == NULLAGNUMBER)
2770 error = xfs_filestream_new_ag(ap, &ag);
2772 up_read(&mp->m_peraglock);
2776 /* loop again to set 'blen'*/
2777 startag = NULLAGNUMBER;
2781 if (++ag == mp->m_sb.sb_agcount)
2786 up_read(&mp->m_peraglock);
2788 * Since the above loop did a BUF_TRYLOCK, it is
2789 * possible that there is space for this request.
2791 if (notinit || blen < ap->minlen)
2792 args.minlen = ap->minlen;
2794 * If the best seen length is less than the request
2795 * length, use the best as the minimum.
2797 else if (blen < ap->alen)
2800 * Otherwise we've seen an extent as big as alen,
2801 * use that as the minimum.
2804 args.minlen = ap->alen;
2807 * set the failure fallback case to look in the selected
2808 * AG as the stream may have moved.
2810 if (xfs_inode_is_filestream(ap->ip))
2811 ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2812 } else if (ap->low) {
2813 if (xfs_inode_is_filestream(ap->ip))
2814 args.type = XFS_ALLOCTYPE_FIRST_AG;
2816 args.type = XFS_ALLOCTYPE_START_BNO;
2817 args.total = args.minlen = ap->minlen;
2819 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2820 args.total = ap->total;
2821 args.minlen = ap->minlen;
2823 /* apply extent size hints if obtained earlier */
2824 if (unlikely(align)) {
2826 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2827 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2828 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
2832 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
2833 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2834 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2837 * If we are not low on available data blocks, and the
2838 * underlying logical volume manager is a stripe, and
2839 * the file offset is zero then try to allocate data
2840 * blocks on stripe unit boundary.
2841 * NOTE: ap->aeof is only set if the allocation length
2842 * is >= the stripe unit and the allocation offset is
2843 * at the end of file.
2845 if (!ap->low && ap->aeof) {
2847 args.alignment = mp->m_dalign;
2851 * Adjust for alignment
2853 if (blen > args.alignment && blen <= ap->alen)
2854 args.minlen = blen - args.alignment;
2855 args.minalignslop = 0;
2858 * First try an exact bno allocation.
2859 * If it fails then do a near or start bno
2860 * allocation with alignment turned on.
2864 args.type = XFS_ALLOCTYPE_THIS_BNO;
2867 * Compute the minlen+alignment for the
2868 * next case. Set slop so that the value
2869 * of minlen+alignment+slop doesn't go up
2870 * between the calls.
2872 if (blen > mp->m_dalign && blen <= ap->alen)
2873 nextminlen = blen - mp->m_dalign;
2875 nextminlen = args.minlen;
2876 if (nextminlen + mp->m_dalign > args.minlen + 1)
2878 nextminlen + mp->m_dalign -
2881 args.minalignslop = 0;
2885 args.minalignslop = 0;
2887 args.minleft = ap->minleft;
2888 args.wasdel = ap->wasdel;
2890 args.userdata = ap->userdata;
2891 if ((error = xfs_alloc_vextent(&args)))
2893 if (tryagain && args.fsbno == NULLFSBLOCK) {
2895 * Exact allocation failed. Now try with alignment
2899 args.fsbno = ap->rval;
2900 args.alignment = mp->m_dalign;
2901 args.minlen = nextminlen;
2902 args.minalignslop = 0;
2904 if ((error = xfs_alloc_vextent(&args)))
2907 if (isaligned && args.fsbno == NULLFSBLOCK) {
2909 * allocation failed, so turn off alignment and
2913 args.fsbno = ap->rval;
2915 if ((error = xfs_alloc_vextent(&args)))
2918 if (args.fsbno == NULLFSBLOCK && nullfb &&
2919 args.minlen > ap->minlen) {
2920 args.minlen = ap->minlen;
2921 args.type = XFS_ALLOCTYPE_START_BNO;
2922 args.fsbno = ap->rval;
2923 if ((error = xfs_alloc_vextent(&args)))
2926 if (args.fsbno == NULLFSBLOCK && nullfb) {
2928 args.type = XFS_ALLOCTYPE_FIRST_AG;
2929 args.total = ap->minlen;
2931 if ((error = xfs_alloc_vextent(&args)))
2935 if (args.fsbno != NULLFSBLOCK) {
2936 ap->firstblock = ap->rval = args.fsbno;
2937 ASSERT(nullfb || fb_agno == args.agno ||
2938 (ap->low && fb_agno < args.agno));
2939 ap->alen = args.len;
2940 ap->ip->i_d.di_nblocks += args.len;
2941 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2943 ap->ip->i_delayed_blks -= args.len;
2945 * Adjust the disk quota also. This was reserved
2948 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2949 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2950 XFS_TRANS_DQ_BCOUNT,
2953 ap->rval = NULLFSBLOCK;
2960 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2961 * It figures out where to ask the underlying allocator to put the new extent.
2965 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2967 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
2968 return xfs_bmap_rtalloc(ap);
2969 return xfs_bmap_btalloc(ap);
2973 * Transform a btree format file with only one leaf node, where the
2974 * extents list will fit in the inode, into an extents format file.
2975 * Since the file extents are already in-core, all we have to do is
2976 * give up the space for the btree root and pitch the leaf block.
2978 STATIC int /* error */
2979 xfs_bmap_btree_to_extents(
2980 xfs_trans_t *tp, /* transaction pointer */
2981 xfs_inode_t *ip, /* incore inode pointer */
2982 xfs_btree_cur_t *cur, /* btree cursor */
2983 int *logflagsp, /* inode logging flags */
2984 int whichfork) /* data or attr fork */
2987 struct xfs_btree_block *cblock;/* child btree block */
2988 xfs_fsblock_t cbno; /* child block number */
2989 xfs_buf_t *cbp; /* child block's buffer */
2990 int error; /* error return value */
2991 xfs_ifork_t *ifp; /* inode fork data */
2992 xfs_mount_t *mp; /* mount point structure */
2993 __be64 *pp; /* ptr to block address */
2994 struct xfs_btree_block *rblock;/* root btree block */
2997 ifp = XFS_IFORK_PTR(ip, whichfork);
2998 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2999 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
3000 rblock = ifp->if_broot;
3001 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
3002 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
3003 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
3004 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
3005 cbno = be64_to_cpu(*pp);
3008 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
3011 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
3012 XFS_BMAP_BTREE_REF)))
3014 cblock = XFS_BUF_TO_BLOCK(cbp);
3015 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
3017 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
3018 ip->i_d.di_nblocks--;
3019 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
3020 xfs_trans_binval(tp, cbp);
3021 if (cur->bc_bufs[0] == cbp)
3022 cur->bc_bufs[0] = NULL;
3023 xfs_iroot_realloc(ip, -1, whichfork);
3024 ASSERT(ifp->if_broot == NULL);
3025 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
3026 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3027 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
3032 * Called by xfs_bmapi to update file extent records and the btree
3033 * after removing space (or undoing a delayed allocation).
3035 STATIC int /* error */
3036 xfs_bmap_del_extent(
3037 xfs_inode_t *ip, /* incore inode pointer */
3038 xfs_trans_t *tp, /* current transaction pointer */
3039 xfs_extnum_t idx, /* extent number to update/delete */
3040 xfs_bmap_free_t *flist, /* list of extents to be freed */
3041 xfs_btree_cur_t *cur, /* if null, not a btree */
3042 xfs_bmbt_irec_t *del, /* data to remove from extents */
3043 int *logflagsp, /* inode logging flags */
3044 xfs_extdelta_t *delta, /* Change made to incore extents */
3045 int whichfork, /* data or attr fork */
3046 int rsvd) /* OK to allocate reserved blocks */
3048 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
3049 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
3050 xfs_fsblock_t del_endblock=0; /* first block past del */
3051 xfs_fileoff_t del_endoff; /* first offset past del */
3052 int delay; /* current block is delayed allocated */
3053 int do_fx; /* free extent at end of routine */
3054 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
3055 int error; /* error return value */
3056 int flags; /* inode logging flags */
3057 xfs_bmbt_irec_t got; /* current extent entry */
3058 xfs_fileoff_t got_endoff; /* first offset past got */
3059 int i; /* temp state */
3060 xfs_ifork_t *ifp; /* inode fork pointer */
3061 xfs_mount_t *mp; /* mount structure */
3062 xfs_filblks_t nblks; /* quota/sb block count */
3063 xfs_bmbt_irec_t new; /* new record to be inserted */
3065 uint qfield; /* quota field to update */
3066 xfs_filblks_t temp; /* for indirect length calculations */
3067 xfs_filblks_t temp2; /* for indirect length calculations */
3069 XFS_STATS_INC(xs_del_exlist);
3071 ifp = XFS_IFORK_PTR(ip, whichfork);
3072 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
3073 (uint)sizeof(xfs_bmbt_rec_t)));
3074 ASSERT(del->br_blockcount > 0);
3075 ep = xfs_iext_get_ext(ifp, idx);
3076 xfs_bmbt_get_all(ep, &got);
3077 ASSERT(got.br_startoff <= del->br_startoff);
3078 del_endoff = del->br_startoff + del->br_blockcount;
3079 got_endoff = got.br_startoff + got.br_blockcount;
3080 ASSERT(got_endoff >= del_endoff);
3081 delay = isnullstartblock(got.br_startblock);
3082 ASSERT(isnullstartblock(del->br_startblock) == delay);
3087 * If deleting a real allocation, must free up the disk space.
3090 flags = XFS_ILOG_CORE;
3092 * Realtime allocation. Free it and record di_nblocks update.
3094 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
3098 ASSERT(do_mod(del->br_blockcount,
3099 mp->m_sb.sb_rextsize) == 0);
3100 ASSERT(do_mod(del->br_startblock,
3101 mp->m_sb.sb_rextsize) == 0);
3102 bno = del->br_startblock;
3103 len = del->br_blockcount;
3104 do_div(bno, mp->m_sb.sb_rextsize);
3105 do_div(len, mp->m_sb.sb_rextsize);
3106 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3107 (xfs_extlen_t)len)))
3110 nblks = len * mp->m_sb.sb_rextsize;
3111 qfield = XFS_TRANS_DQ_RTBCOUNT;
3114 * Ordinary allocation.
3118 nblks = del->br_blockcount;
3119 qfield = XFS_TRANS_DQ_BCOUNT;
3122 * Set up del_endblock and cur for later.
3124 del_endblock = del->br_startblock + del->br_blockcount;
3126 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3127 got.br_startblock, got.br_blockcount,
3130 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3132 da_old = da_new = 0;
3134 da_old = startblockval(got.br_startblock);
3140 * Set flag value to use in switch statement.
3141 * Left-contig is 2, right-contig is 1.
3143 switch (((got.br_startoff == del->br_startoff) << 1) |
3144 (got_endoff == del_endoff)) {
3147 * Matches the whole extent. Delete the entry.
3149 XFS_BMAP_TRACE_DELETE("3", ip, idx, 1, whichfork);
3150 xfs_iext_remove(ifp, idx, 1);
3151 ifp->if_lastex = idx;
3154 XFS_IFORK_NEXT_SET(ip, whichfork,
3155 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3156 flags |= XFS_ILOG_CORE;
3158 flags |= xfs_ilog_fext(whichfork);
3161 if ((error = xfs_btree_delete(cur, &i)))
3163 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3168 * Deleting the first part of the extent.
3170 XFS_BMAP_TRACE_PRE_UPDATE("2", ip, idx, whichfork);
3171 xfs_bmbt_set_startoff(ep, del_endoff);
3172 temp = got.br_blockcount - del->br_blockcount;
3173 xfs_bmbt_set_blockcount(ep, temp);
3174 ifp->if_lastex = idx;
3176 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3178 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
3179 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx,
3184 xfs_bmbt_set_startblock(ep, del_endblock);
3185 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx, whichfork);
3187 flags |= xfs_ilog_fext(whichfork);
3190 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3191 got.br_blockcount - del->br_blockcount,
3198 * Deleting the last part of the extent.
3200 temp = got.br_blockcount - del->br_blockcount;
3201 XFS_BMAP_TRACE_PRE_UPDATE("1", ip, idx, whichfork);
3202 xfs_bmbt_set_blockcount(ep, temp);
3203 ifp->if_lastex = idx;
3205 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3207 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
3208 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx,
3213 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx, whichfork);
3215 flags |= xfs_ilog_fext(whichfork);
3218 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3220 got.br_blockcount - del->br_blockcount,
3227 * Deleting the middle of the extent.
3229 temp = del->br_startoff - got.br_startoff;
3230 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, whichfork);
3231 xfs_bmbt_set_blockcount(ep, temp);
3232 new.br_startoff = del_endoff;
3233 temp2 = got_endoff - del_endoff;
3234 new.br_blockcount = temp2;
3235 new.br_state = got.br_state;
3237 new.br_startblock = del_endblock;
3238 flags |= XFS_ILOG_CORE;
3240 if ((error = xfs_bmbt_update(cur,
3242 got.br_startblock, temp,
3245 if ((error = xfs_btree_increment(cur, 0, &i)))
3247 cur->bc_rec.b = new;
3248 error = xfs_btree_insert(cur, &i);
3249 if (error && error != ENOSPC)
3252 * If get no-space back from btree insert,
3253 * it tried a split, and we have a zero
3254 * block reservation.
3255 * Fix up our state and return the error.
3257 if (error == ENOSPC) {
3259 * Reset the cursor, don't trust
3260 * it after any insert operation.
3262 if ((error = xfs_bmbt_lookup_eq(cur,
3267 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3269 * Update the btree record back
3270 * to the original value.
3272 if ((error = xfs_bmbt_update(cur,
3279 * Reset the extent record back
3280 * to the original value.
3282 xfs_bmbt_set_blockcount(ep,
3285 error = XFS_ERROR(ENOSPC);
3288 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3290 flags |= xfs_ilog_fext(whichfork);
3291 XFS_IFORK_NEXT_SET(ip, whichfork,
3292 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3294 ASSERT(whichfork == XFS_DATA_FORK);
3295 temp = xfs_bmap_worst_indlen(ip, temp);
3296 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
3297 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3298 new.br_startblock = nullstartblock((int)temp2);
3299 da_new = temp + temp2;
3300 while (da_new > da_old) {
3304 xfs_bmbt_set_startblock(ep,
3305 nullstartblock((int)temp));
3307 if (da_new == da_old)
3313 nullstartblock((int)temp2);
3317 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, whichfork);
3318 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 1, &new, NULL,
3320 xfs_iext_insert(ifp, idx + 1, 1, &new);
3321 ifp->if_lastex = idx + 1;
3325 * If we need to, add to list of extents to delete.
3328 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3331 * Adjust inode # blocks in the file.
3334 ip->i_d.di_nblocks -= nblks;
3336 * Adjust quota data.
3339 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
3342 * Account for change in delayed indirect blocks.
3343 * Nothing to do for disk quota accounting here.
3345 ASSERT(da_old >= da_new);
3346 if (da_old > da_new)
3347 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
3350 /* DELTA: report the original extent. */
3351 if (delta->xed_startoff > got.br_startoff)
3352 delta->xed_startoff = got.br_startoff;
3353 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3354 delta->xed_blockcount = got.br_startoff +
3363 * Remove the entry "free" from the free item list. Prev points to the
3364 * previous entry, unless "free" is the head of the list.
3368 xfs_bmap_free_t *flist, /* free item list header */
3369 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3370 xfs_bmap_free_item_t *free) /* list item to be freed */
3373 prev->xbfi_next = free->xbfi_next;
3375 flist->xbf_first = free->xbfi_next;
3377 kmem_zone_free(xfs_bmap_free_item_zone, free);
3381 * Convert an extents-format file into a btree-format file.
3382 * The new file will have a root block (in the inode) and a single child block.
3384 STATIC int /* error */
3385 xfs_bmap_extents_to_btree(
3386 xfs_trans_t *tp, /* transaction pointer */
3387 xfs_inode_t *ip, /* incore inode pointer */
3388 xfs_fsblock_t *firstblock, /* first-block-allocated */
3389 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3390 xfs_btree_cur_t **curp, /* cursor returned to caller */
3391 int wasdel, /* converting a delayed alloc */
3392 int *logflagsp, /* inode logging flags */
3393 int whichfork) /* data or attr fork */
3395 struct xfs_btree_block *ablock; /* allocated (child) bt block */
3396 xfs_buf_t *abp; /* buffer for ablock */
3397 xfs_alloc_arg_t args; /* allocation arguments */
3398 xfs_bmbt_rec_t *arp; /* child record pointer */
3399 struct xfs_btree_block *block; /* btree root block */
3400 xfs_btree_cur_t *cur; /* bmap btree cursor */
3401 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3402 int error; /* error return value */
3403 xfs_extnum_t i, cnt; /* extent record index */
3404 xfs_ifork_t *ifp; /* inode fork pointer */
3405 xfs_bmbt_key_t *kp; /* root block key pointer */
3406 xfs_mount_t *mp; /* mount structure */
3407 xfs_extnum_t nextents; /* number of file extents */
3408 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3410 ifp = XFS_IFORK_PTR(ip, whichfork);
3411 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3412 ASSERT(ifp->if_ext_max ==
3413 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3415 * Make space in the inode incore.
3417 xfs_iroot_realloc(ip, 1, whichfork);
3418 ifp->if_flags |= XFS_IFBROOT;
3423 block = ifp->if_broot;
3424 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3425 block->bb_level = cpu_to_be16(1);
3426 block->bb_numrecs = cpu_to_be16(1);
3427 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3428 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3431 * Need a cursor. Can't allocate until bb_level is filled in.
3434 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
3435 cur->bc_private.b.firstblock = *firstblock;
3436 cur->bc_private.b.flist = flist;
3437 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3439 * Convert to a btree with two levels, one record in root.
3441 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3444 args.firstblock = *firstblock;
3445 if (*firstblock == NULLFSBLOCK) {
3446 args.type = XFS_ALLOCTYPE_START_BNO;
3447 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3448 } else if (flist->xbf_low) {
3449 args.type = XFS_ALLOCTYPE_START_BNO;
3450 args.fsbno = *firstblock;
3452 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3453 args.fsbno = *firstblock;
3455 args.minlen = args.maxlen = args.prod = 1;
3456 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3457 args.minalignslop = 0;
3458 args.wasdel = wasdel;
3460 if ((error = xfs_alloc_vextent(&args))) {
3461 xfs_iroot_realloc(ip, -1, whichfork);
3462 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3466 * Allocation can't fail, the space was reserved.
3468 ASSERT(args.fsbno != NULLFSBLOCK);
3469 ASSERT(*firstblock == NULLFSBLOCK ||
3470 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3472 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3473 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3474 cur->bc_private.b.allocated++;
3475 ip->i_d.di_nblocks++;
3476 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3477 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3479 * Fill in the child block.
3481 ablock = XFS_BUF_TO_BLOCK(abp);
3482 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3483 ablock->bb_level = 0;
3484 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3485 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3486 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3487 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3488 for (cnt = i = 0; i < nextents; i++) {
3489 ep = xfs_iext_get_ext(ifp, i);
3490 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
3491 arp->l0 = cpu_to_be64(ep->l0);
3492 arp->l1 = cpu_to_be64(ep->l1);
3496 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3497 xfs_btree_set_numrecs(ablock, cnt);
3500 * Fill in the root key and pointer.
3502 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3503 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3504 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3505 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3506 be16_to_cpu(block->bb_level)));
3507 *pp = cpu_to_be64(args.fsbno);
3510 * Do all this logging at the end so that
3511 * the root is at the right level.
3513 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3514 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3515 ASSERT(*curp == NULL);
3517 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
3522 * Calculate the default attribute fork offset for newly created inodes.
3525 xfs_default_attroffset(
3526 struct xfs_inode *ip)
3528 struct xfs_mount *mp = ip->i_mount;
3531 if (mp->m_sb.sb_inodesize == 256) {
3532 offset = XFS_LITINO(mp) -
3533 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3535 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3538 ASSERT(offset < XFS_LITINO(mp));
3543 * Helper routine to reset inode di_forkoff field when switching
3544 * attribute fork from local to extent format - we reset it where
3545 * possible to make space available for inline data fork extents.
3548 xfs_bmap_forkoff_reset(
3553 if (whichfork == XFS_ATTR_FORK &&
3554 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3555 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3556 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3557 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3559 if (dfl_forkoff > ip->i_d.di_forkoff) {
3560 ip->i_d.di_forkoff = dfl_forkoff;
3561 ip->i_df.if_ext_max =
3562 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3563 ip->i_afp->if_ext_max =
3564 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3570 * Convert a local file to an extents file.
3571 * This code is out of bounds for data forks of regular files,
3572 * since the file data needs to get logged so things will stay consistent.
3573 * (The bmap-level manipulations are ok, though).
3575 STATIC int /* error */
3576 xfs_bmap_local_to_extents(
3577 xfs_trans_t *tp, /* transaction pointer */
3578 xfs_inode_t *ip, /* incore inode pointer */
3579 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3580 xfs_extlen_t total, /* total blocks needed by transaction */
3581 int *logflagsp, /* inode logging flags */
3582 int whichfork) /* data or attr fork */
3584 int error; /* error return value */
3585 int flags; /* logging flags returned */
3586 xfs_ifork_t *ifp; /* inode fork pointer */
3589 * We don't want to deal with the case of keeping inode data inline yet.
3590 * So sending the data fork of a regular inode is invalid.
3592 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3593 whichfork == XFS_DATA_FORK));
3594 ifp = XFS_IFORK_PTR(ip, whichfork);
3595 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3598 if (ifp->if_bytes) {
3599 xfs_alloc_arg_t args; /* allocation arguments */
3600 xfs_buf_t *bp; /* buffer for extent block */
3601 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
3604 args.mp = ip->i_mount;
3605 args.firstblock = *firstblock;
3606 ASSERT((ifp->if_flags &
3607 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3609 * Allocate a block. We know we need only one, since the
3610 * file currently fits in an inode.
3612 if (*firstblock == NULLFSBLOCK) {
3613 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3614 args.type = XFS_ALLOCTYPE_START_BNO;
3616 args.fsbno = *firstblock;
3617 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3620 args.mod = args.minleft = args.alignment = args.wasdel =
3621 args.isfl = args.minalignslop = 0;
3622 args.minlen = args.maxlen = args.prod = 1;
3623 if ((error = xfs_alloc_vextent(&args)))
3626 * Can't fail, the space was reserved.
3628 ASSERT(args.fsbno != NULLFSBLOCK);
3629 ASSERT(args.len == 1);
3630 *firstblock = args.fsbno;
3631 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3632 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3634 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3635 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3636 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3637 xfs_iext_add(ifp, 0, 1);
3638 ep = xfs_iext_get_ext(ifp, 0);
3639 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3640 XFS_BMAP_TRACE_POST_UPDATE("new", ip, 0, whichfork);
3641 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3642 ip->i_d.di_nblocks = 1;
3643 xfs_trans_mod_dquot_byino(tp, ip,
3644 XFS_TRANS_DQ_BCOUNT, 1L);
3645 flags |= xfs_ilog_fext(whichfork);
3647 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3648 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3650 ifp->if_flags &= ~XFS_IFINLINE;
3651 ifp->if_flags |= XFS_IFEXTENTS;
3652 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3653 flags |= XFS_ILOG_CORE;
3660 * Search the extent records for the entry containing block bno.
3661 * If bno lies in a hole, point to the next entry. If bno lies
3662 * past eof, *eofp will be set, and *prevp will contain the last
3663 * entry (null if none). Else, *lastxp will be set to the index
3664 * of the found entry; *gotp will contain the entry.
3666 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3667 xfs_bmap_search_multi_extents(
3668 xfs_ifork_t *ifp, /* inode fork pointer */
3669 xfs_fileoff_t bno, /* block number searched for */
3670 int *eofp, /* out: end of file found */
3671 xfs_extnum_t *lastxp, /* out: last extent index */
3672 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3673 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3675 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3676 xfs_extnum_t lastx; /* last extent index */
3679 * Initialize the extent entry structure to catch access to
3680 * uninitialized br_startblock field.
3682 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3683 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3684 gotp->br_state = XFS_EXT_INVALID;
3686 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3688 gotp->br_startblock = 0xffffa5a5;
3690 prevp->br_startoff = NULLFILEOFF;
3692 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3694 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3696 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3697 xfs_bmbt_get_all(ep, gotp);
3711 * Search the extents list for the inode, for the extent containing bno.
3712 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3713 * *eofp will be set, and *prevp will contain the last entry (null if none).
3714 * Else, *lastxp will be set to the index of the found
3715 * entry; *gotp will contain the entry.
3717 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3718 xfs_bmap_search_extents(
3719 xfs_inode_t *ip, /* incore inode pointer */
3720 xfs_fileoff_t bno, /* block number searched for */
3721 int fork, /* data or attr fork */
3722 int *eofp, /* out: end of file found */
3723 xfs_extnum_t *lastxp, /* out: last extent index */
3724 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3725 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3727 xfs_ifork_t *ifp; /* inode fork pointer */
3728 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3730 XFS_STATS_INC(xs_look_exlist);
3731 ifp = XFS_IFORK_PTR(ip, fork);
3733 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3735 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3736 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3737 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3738 "Access to block zero in inode %llu "
3739 "start_block: %llx start_off: %llx "
3740 "blkcnt: %llx extent-state: %x lastx: %x\n",
3741 (unsigned long long)ip->i_ino,
3742 (unsigned long long)gotp->br_startblock,
3743 (unsigned long long)gotp->br_startoff,
3744 (unsigned long long)gotp->br_blockcount,
3745 gotp->br_state, *lastxp);
3746 *lastxp = NULLEXTNUM;
3754 #ifdef XFS_BMAP_TRACE
3755 ktrace_t *xfs_bmap_trace_buf;
3758 * Add a bmap trace buffer entry. Base routine for the others.
3761 xfs_bmap_trace_addentry(
3762 int opcode, /* operation */
3763 const char *fname, /* function name */
3764 char *desc, /* operation description */
3765 xfs_inode_t *ip, /* incore inode pointer */
3766 xfs_extnum_t idx, /* index of entry(ies) */
3767 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
3768 xfs_bmbt_rec_host_t *r1, /* first record */
3769 xfs_bmbt_rec_host_t *r2, /* second record or null */
3770 int whichfork) /* data or attr fork */
3772 xfs_bmbt_rec_host_t tr2;
3774 ASSERT(cnt == 1 || cnt == 2);
3779 memset(&tr2, 0, sizeof(tr2));
3782 ktrace_enter(xfs_bmap_trace_buf,
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)
3798 ASSERT(ip->i_xtrace);
3799 ktrace_enter(ip->i_xtrace,
3800 (void *)(__psint_t)(opcode | (whichfork << 16)),
3801 (void *)fname, (void *)desc, (void *)ip,
3802 (void *)(__psint_t)idx,
3803 (void *)(__psint_t)cnt,
3804 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3805 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3806 (void *)(__psunsigned_t)(r1->l0 >> 32),
3807 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3808 (void *)(__psunsigned_t)(r1->l1 >> 32),
3809 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3810 (void *)(__psunsigned_t)(r2->l0 >> 32),
3811 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3812 (void *)(__psunsigned_t)(r2->l1 >> 32),
3813 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3818 * Add bmap trace entry prior to a call to xfs_iext_remove.
3821 xfs_bmap_trace_delete(
3822 const char *fname, /* function name */
3823 char *desc, /* operation description */
3824 xfs_inode_t *ip, /* incore inode pointer */
3825 xfs_extnum_t idx, /* index of entry(entries) deleted */
3826 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3827 int whichfork) /* data or attr fork */
3829 xfs_ifork_t *ifp; /* inode fork pointer */
3831 ifp = XFS_IFORK_PTR(ip, whichfork);
3832 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
3833 cnt, xfs_iext_get_ext(ifp, idx),
3834 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
3839 * Add bmap trace entry prior to a call to xfs_iext_insert, or
3840 * reading in the extents list from the disk (in the btree).
3843 xfs_bmap_trace_insert(
3844 const char *fname, /* function name */
3845 char *desc, /* operation description */
3846 xfs_inode_t *ip, /* incore inode pointer */
3847 xfs_extnum_t idx, /* index of entry(entries) inserted */
3848 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3849 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3850 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3851 int whichfork) /* data or attr fork */
3853 xfs_bmbt_rec_host_t tr1; /* compressed record 1 */
3854 xfs_bmbt_rec_host_t tr2; /* compressed record 2 if needed */
3856 xfs_bmbt_set_all(&tr1, r1);
3859 xfs_bmbt_set_all(&tr2, r2);
3864 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3865 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3869 * Add bmap trace entry after updating an extent record in place.
3872 xfs_bmap_trace_post_update(
3873 const char *fname, /* function name */
3874 char *desc, /* operation description */
3875 xfs_inode_t *ip, /* incore inode pointer */
3876 xfs_extnum_t idx, /* index of entry updated */
3877 int whichfork) /* data or attr fork */
3879 xfs_ifork_t *ifp; /* inode fork pointer */
3881 ifp = XFS_IFORK_PTR(ip, whichfork);
3882 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
3883 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3887 * Add bmap trace entry prior to updating an extent record in place.
3890 xfs_bmap_trace_pre_update(
3891 const char *fname, /* function name */
3892 char *desc, /* operation description */
3893 xfs_inode_t *ip, /* incore inode pointer */
3894 xfs_extnum_t idx, /* index of entry to be updated */
3895 int whichfork) /* data or attr fork */
3897 xfs_ifork_t *ifp; /* inode fork pointer */
3899 ifp = XFS_IFORK_PTR(ip, whichfork);
3900 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
3901 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3903 #endif /* XFS_BMAP_TRACE */
3906 * Compute the worst-case number of indirect blocks that will be used
3907 * for ip's delayed extent of length "len".
3909 STATIC xfs_filblks_t
3910 xfs_bmap_worst_indlen(
3911 xfs_inode_t *ip, /* incore inode pointer */
3912 xfs_filblks_t len) /* delayed extent length */
3914 int level; /* btree level number */
3915 int maxrecs; /* maximum record count at this level */
3916 xfs_mount_t *mp; /* mount structure */
3917 xfs_filblks_t rval; /* return value */
3920 maxrecs = mp->m_bmap_dmxr[0];
3921 for (level = 0, rval = 0;
3922 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3925 do_div(len, maxrecs);
3928 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3931 maxrecs = mp->m_bmap_dmxr[1];
3936 #if defined(XFS_RW_TRACE)
3945 if (ip->i_rwtrace == NULL)
3947 ktrace_enter(ip->i_rwtrace,
3948 (void *)(__psint_t)XFS_BUNMAP,
3950 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3951 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3952 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3953 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3954 (void *)(__psint_t)len,
3955 (void *)(__psint_t)flags,
3956 (void *)(unsigned long)current_cpu(),
3968 * Convert inode from non-attributed to attributed.
3969 * Must not be in a transaction, ip must not be locked.
3971 int /* error code */
3972 xfs_bmap_add_attrfork(
3973 xfs_inode_t *ip, /* incore inode pointer */
3974 int size, /* space new attribute needs */
3975 int rsvd) /* xact may use reserved blks */
3977 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3978 xfs_bmap_free_t flist; /* freed extent records */
3979 xfs_mount_t *mp; /* mount structure */
3980 xfs_trans_t *tp; /* transaction pointer */
3981 int blks; /* space reservation */
3982 int version = 1; /* superblock attr version */
3983 int committed; /* xaction was committed */
3984 int logflags; /* logging flags */
3985 int error; /* error return value */
3987 ASSERT(XFS_IFORK_Q(ip) == 0);
3988 ASSERT(ip->i_df.if_ext_max ==
3989 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3992 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3993 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3994 blks = XFS_ADDAFORK_SPACE_RES(mp);
3996 tp->t_flags |= XFS_TRANS_RESERVE;
3997 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3998 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
4000 xfs_ilock(ip, XFS_ILOCK_EXCL);
4001 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
4002 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
4003 XFS_QMOPT_RES_REGBLKS);
4005 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4006 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
4009 if (XFS_IFORK_Q(ip))
4011 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
4013 * For inodes coming from pre-6.2 filesystems.
4015 ASSERT(ip->i_d.di_aformat == 0);
4016 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
4018 ASSERT(ip->i_d.di_anextents == 0);
4020 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4021 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4022 switch (ip->i_d.di_format) {
4023 case XFS_DINODE_FMT_DEV:
4024 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
4026 case XFS_DINODE_FMT_UUID:
4027 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
4029 case XFS_DINODE_FMT_LOCAL:
4030 case XFS_DINODE_FMT_EXTENTS:
4031 case XFS_DINODE_FMT_BTREE:
4032 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
4033 if (!ip->i_d.di_forkoff)
4034 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
4035 else if (mp->m_flags & XFS_MOUNT_ATTR2)
4040 error = XFS_ERROR(EINVAL);
4043 ip->i_df.if_ext_max =
4044 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4045 ASSERT(ip->i_afp == NULL);
4046 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
4047 ip->i_afp->if_ext_max =
4048 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4049 ip->i_afp->if_flags = XFS_IFEXTENTS;
4051 xfs_bmap_init(&flist, &firstblock);
4052 switch (ip->i_d.di_format) {
4053 case XFS_DINODE_FMT_LOCAL:
4054 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
4057 case XFS_DINODE_FMT_EXTENTS:
4058 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
4061 case XFS_DINODE_FMT_BTREE:
4062 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
4070 xfs_trans_log_inode(tp, ip, logflags);
4073 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
4074 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
4075 __int64_t sbfields = 0;
4077 spin_lock(&mp->m_sb_lock);
4078 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
4079 xfs_sb_version_addattr(&mp->m_sb);
4080 sbfields |= XFS_SB_VERSIONNUM;
4082 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
4083 xfs_sb_version_addattr2(&mp->m_sb);
4084 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
4087 spin_unlock(&mp->m_sb_lock);
4088 xfs_mod_sb(tp, sbfields);
4090 spin_unlock(&mp->m_sb_lock);
4092 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
4094 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
4095 ASSERT(ip->i_df.if_ext_max ==
4096 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4099 xfs_bmap_cancel(&flist);
4101 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4103 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4104 ASSERT(ip->i_df.if_ext_max ==
4105 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4110 * Add the extent to the list of extents to be free at transaction end.
4111 * The list is maintained sorted (by block number).
4116 xfs_fsblock_t bno, /* fs block number of extent */
4117 xfs_filblks_t len, /* length of extent */
4118 xfs_bmap_free_t *flist, /* list of extents */
4119 xfs_mount_t *mp) /* mount point structure */
4121 xfs_bmap_free_item_t *cur; /* current (next) element */
4122 xfs_bmap_free_item_t *new; /* new element */
4123 xfs_bmap_free_item_t *prev; /* previous element */
4125 xfs_agnumber_t agno;
4126 xfs_agblock_t agbno;
4128 ASSERT(bno != NULLFSBLOCK);
4130 ASSERT(len <= MAXEXTLEN);
4131 ASSERT(!isnullstartblock(bno));
4132 agno = XFS_FSB_TO_AGNO(mp, bno);
4133 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4134 ASSERT(agno < mp->m_sb.sb_agcount);
4135 ASSERT(agbno < mp->m_sb.sb_agblocks);
4136 ASSERT(len < mp->m_sb.sb_agblocks);
4137 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4139 ASSERT(xfs_bmap_free_item_zone != NULL);
4140 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4141 new->xbfi_startblock = bno;
4142 new->xbfi_blockcount = (xfs_extlen_t)len;
4143 for (prev = NULL, cur = flist->xbf_first;
4145 prev = cur, cur = cur->xbfi_next) {
4146 if (cur->xbfi_startblock >= bno)
4150 prev->xbfi_next = new;
4152 flist->xbf_first = new;
4153 new->xbfi_next = cur;
4158 * Compute and fill in the value of the maximum depth of a bmap btree
4159 * in this filesystem. Done once, during mount.
4162 xfs_bmap_compute_maxlevels(
4163 xfs_mount_t *mp, /* file system mount structure */
4164 int whichfork) /* data or attr fork */
4166 int level; /* btree level */
4167 uint maxblocks; /* max blocks at this level */
4168 uint maxleafents; /* max leaf entries possible */
4169 int maxrootrecs; /* max records in root block */
4170 int minleafrecs; /* min records in leaf block */
4171 int minnoderecs; /* min records in node block */
4172 int sz; /* root block size */
4175 * The maximum number of extents in a file, hence the maximum
4176 * number of leaf entries, is controlled by the type of di_nextents
4177 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4178 * (a signed 16-bit number, xfs_aextnum_t).
4180 * Note that we can no longer assume that if we are in ATTR1 that
4181 * the fork offset of all the inodes will be
4182 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
4183 * with ATTR2 and then mounted back with ATTR1, keeping the
4184 * di_forkoff's fixed but probably at various positions. Therefore,
4185 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
4186 * of a minimum size available.
4188 if (whichfork == XFS_DATA_FORK) {
4189 maxleafents = MAXEXTNUM;
4190 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
4192 maxleafents = MAXAEXTNUM;
4193 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
4195 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
4196 minleafrecs = mp->m_bmap_dmnr[0];
4197 minnoderecs = mp->m_bmap_dmnr[1];
4198 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4199 for (level = 1; maxblocks > 1; level++) {
4200 if (maxblocks <= maxrootrecs)
4203 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4205 mp->m_bm_maxlevels[whichfork] = level;
4209 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4210 * caller. Frees all the extents that need freeing, which must be done
4211 * last due to locking considerations. We never free any extents in
4212 * the first transaction. This is to allow the caller to make the first
4213 * transaction a synchronous one so that the pointers to the data being
4214 * broken in this transaction will be permanent before the data is actually
4215 * freed. This is necessary to prevent blocks from being reallocated
4216 * and written to before the free and reallocation are actually permanent.
4217 * We do not just make the first transaction synchronous here, because
4218 * there are more efficient ways to gain the same protection in some cases
4219 * (see the file truncation code).
4221 * Return 1 if the given transaction was committed and a new one
4222 * started, and 0 otherwise in the committed parameter.
4227 xfs_trans_t **tp, /* transaction pointer addr */
4228 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4229 int *committed) /* xact committed or not */
4231 xfs_efd_log_item_t *efd; /* extent free data */
4232 xfs_efi_log_item_t *efi; /* extent free intention */
4233 int error; /* error return value */
4234 xfs_bmap_free_item_t *free; /* free extent item */
4235 unsigned int logres; /* new log reservation */
4236 unsigned int logcount; /* new log count */
4237 xfs_mount_t *mp; /* filesystem mount structure */
4238 xfs_bmap_free_item_t *next; /* next item on free list */
4239 xfs_trans_t *ntp; /* new transaction pointer */
4241 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4242 if (flist->xbf_count == 0) {
4247 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4248 for (free = flist->xbf_first; free; free = free->xbfi_next)
4249 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4250 free->xbfi_blockcount);
4251 logres = ntp->t_log_res;
4252 logcount = ntp->t_log_count;
4253 ntp = xfs_trans_dup(*tp);
4254 error = xfs_trans_commit(*tp, 0);
4258 * We have a new transaction, so we should return committed=1,
4259 * even though we're returning an error.
4265 * transaction commit worked ok so we can drop the extra ticket
4266 * reference that we gained in xfs_trans_dup()
4268 xfs_log_ticket_put(ntp->t_ticket);
4270 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4273 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4274 for (free = flist->xbf_first; free != NULL; free = next) {
4275 next = free->xbfi_next;
4276 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4277 free->xbfi_blockcount))) {
4279 * The bmap free list will be cleaned up at a
4280 * higher level. The EFI will be canceled when
4281 * this transaction is aborted.
4282 * Need to force shutdown here to make sure it
4283 * happens, since this transaction may not be
4287 if (!XFS_FORCED_SHUTDOWN(mp))
4288 xfs_force_shutdown(mp,
4289 (error == EFSCORRUPTED) ?
4290 SHUTDOWN_CORRUPT_INCORE :
4291 SHUTDOWN_META_IO_ERROR);
4294 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4295 free->xbfi_blockcount);
4296 xfs_bmap_del_free(flist, NULL, free);
4302 * Free up any items left in the list.
4306 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4308 xfs_bmap_free_item_t *free; /* free list item */
4309 xfs_bmap_free_item_t *next;
4311 if (flist->xbf_count == 0)
4313 ASSERT(flist->xbf_first != NULL);
4314 for (free = flist->xbf_first; free; free = next) {
4315 next = free->xbfi_next;
4316 xfs_bmap_del_free(flist, NULL, free);
4318 ASSERT(flist->xbf_count == 0);
4322 * Returns the file-relative block number of the first unused block(s)
4323 * in the file with at least "len" logically contiguous blocks free.
4324 * This is the lowest-address hole if the file has holes, else the first block
4325 * past the end of file.
4326 * Return 0 if the file is currently local (in-inode).
4329 xfs_bmap_first_unused(
4330 xfs_trans_t *tp, /* transaction pointer */
4331 xfs_inode_t *ip, /* incore inode */
4332 xfs_extlen_t len, /* size of hole to find */
4333 xfs_fileoff_t *first_unused, /* unused block */
4334 int whichfork) /* data or attr fork */
4336 int error; /* error return value */
4337 int idx; /* extent record index */
4338 xfs_ifork_t *ifp; /* inode fork pointer */
4339 xfs_fileoff_t lastaddr; /* last block number seen */
4340 xfs_fileoff_t lowest; /* lowest useful block */
4341 xfs_fileoff_t max; /* starting useful block */
4342 xfs_fileoff_t off; /* offset for this block */
4343 xfs_extnum_t nextents; /* number of extent entries */
4345 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4346 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4347 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4348 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4352 ifp = XFS_IFORK_PTR(ip, whichfork);
4353 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4354 (error = xfs_iread_extents(tp, ip, whichfork)))
4356 lowest = *first_unused;
4357 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4358 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
4359 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
4360 off = xfs_bmbt_get_startoff(ep);
4362 * See if the hole before this extent will work.
4364 if (off >= lowest + len && off - max >= len) {
4365 *first_unused = max;
4368 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4369 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4371 *first_unused = max;
4376 * Returns the file-relative block number of the last block + 1 before
4377 * last_block (input value) in the file.
4378 * This is not based on i_size, it is based on the extent records.
4379 * Returns 0 for local files, as they do not have extent records.
4382 xfs_bmap_last_before(
4383 xfs_trans_t *tp, /* transaction pointer */
4384 xfs_inode_t *ip, /* incore inode */
4385 xfs_fileoff_t *last_block, /* last block */
4386 int whichfork) /* data or attr fork */
4388 xfs_fileoff_t bno; /* input file offset */
4389 int eof; /* hit end of file */
4390 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
4391 int error; /* error return value */
4392 xfs_bmbt_irec_t got; /* current extent value */
4393 xfs_ifork_t *ifp; /* inode fork pointer */
4394 xfs_extnum_t lastx; /* last extent used */
4395 xfs_bmbt_irec_t prev; /* previous extent value */
4397 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4398 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4399 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4400 return XFS_ERROR(EIO);
4401 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4405 ifp = XFS_IFORK_PTR(ip, whichfork);
4406 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4407 (error = xfs_iread_extents(tp, ip, whichfork)))
4409 bno = *last_block - 1;
4410 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4412 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4413 if (prev.br_startoff == NULLFILEOFF)
4416 *last_block = prev.br_startoff + prev.br_blockcount;
4419 * Otherwise *last_block is already the right answer.
4425 * Returns the file-relative block number of the first block past eof in
4426 * the file. This is not based on i_size, it is based on the extent records.
4427 * Returns 0 for local files, as they do not have extent records.
4430 xfs_bmap_last_offset(
4431 xfs_trans_t *tp, /* transaction pointer */
4432 xfs_inode_t *ip, /* incore inode */
4433 xfs_fileoff_t *last_block, /* last block */
4434 int whichfork) /* data or attr fork */
4436 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
4437 int error; /* error return value */
4438 xfs_ifork_t *ifp; /* inode fork pointer */
4439 xfs_extnum_t nextents; /* number of extent entries */
4441 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4442 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4443 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4444 return XFS_ERROR(EIO);
4445 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4449 ifp = XFS_IFORK_PTR(ip, whichfork);
4450 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4451 (error = xfs_iread_extents(tp, ip, whichfork)))
4453 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4458 ep = xfs_iext_get_ext(ifp, nextents - 1);
4459 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4464 * Returns whether the selected fork of the inode has exactly one
4465 * block or not. For the data fork we check this matches di_size,
4466 * implying the file's range is 0..bsize-1.
4468 int /* 1=>1 block, 0=>otherwise */
4470 xfs_inode_t *ip, /* incore inode */
4471 int whichfork) /* data or attr fork */
4473 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
4474 xfs_ifork_t *ifp; /* inode fork pointer */
4475 int rval; /* return value */
4476 xfs_bmbt_irec_t s; /* internal version of extent */
4479 if (whichfork == XFS_DATA_FORK) {
4480 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4481 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4482 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4485 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4487 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4489 ifp = XFS_IFORK_PTR(ip, whichfork);
4490 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4491 ep = xfs_iext_get_ext(ifp, 0);
4492 xfs_bmbt_get_all(ep, &s);
4493 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4494 if (rval && whichfork == XFS_DATA_FORK)
4495 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
4500 xfs_bmap_sanity_check(
4501 struct xfs_mount *mp,
4505 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4507 if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
4508 be16_to_cpu(block->bb_level) != level ||
4509 be16_to_cpu(block->bb_numrecs) == 0 ||
4510 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4516 * Read in the extents to if_extents.
4517 * All inode fields are set up by caller, we just traverse the btree
4518 * and copy the records in. If the file system cannot contain unwritten
4519 * extents, the records are checked for no "state" flags.
4522 xfs_bmap_read_extents(
4523 xfs_trans_t *tp, /* transaction pointer */
4524 xfs_inode_t *ip, /* incore inode */
4525 int whichfork) /* data or attr fork */
4527 struct xfs_btree_block *block; /* current btree block */
4528 xfs_fsblock_t bno; /* block # of "block" */
4529 xfs_buf_t *bp; /* buffer for "block" */
4530 int error; /* error return value */
4531 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4532 xfs_extnum_t i, j; /* index into the extents list */
4533 xfs_ifork_t *ifp; /* fork structure */
4534 int level; /* btree level, for checking */
4535 xfs_mount_t *mp; /* file system mount structure */
4536 __be64 *pp; /* pointer to block address */
4538 xfs_extnum_t room; /* number of entries there's room for */
4542 ifp = XFS_IFORK_PTR(ip, whichfork);
4543 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4544 XFS_EXTFMT_INODE(ip);
4545 block = ifp->if_broot;
4547 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4549 level = be16_to_cpu(block->bb_level);
4551 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
4552 bno = be64_to_cpu(*pp);
4553 ASSERT(bno != NULLDFSBNO);
4554 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4555 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4557 * Go down the tree until leaf level is reached, following the first
4558 * pointer (leftmost) at each level.
4560 while (level-- > 0) {
4561 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4562 XFS_BMAP_BTREE_REF)))
4564 block = XFS_BUF_TO_BLOCK(bp);
4565 XFS_WANT_CORRUPTED_GOTO(
4566 xfs_bmap_sanity_check(mp, bp, level),
4570 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
4571 bno = be64_to_cpu(*pp);
4572 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4573 xfs_trans_brelse(tp, bp);
4576 * Here with bp and block set to the leftmost leaf node in the tree.
4578 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4581 * Loop over all leaf nodes. Copy information to the extent records.
4584 xfs_bmbt_rec_t *frp;
4585 xfs_fsblock_t nextbno;
4586 xfs_extnum_t num_recs;
4590 num_recs = xfs_btree_get_numrecs(block);
4591 if (unlikely(i + num_recs > room)) {
4592 ASSERT(i + num_recs <= room);
4593 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4594 "corrupt dinode %Lu, (btree extents).",
4595 (unsigned long long) ip->i_ino);
4596 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4601 XFS_WANT_CORRUPTED_GOTO(
4602 xfs_bmap_sanity_check(mp, bp, 0),
4605 * Read-ahead the next leaf block, if any.
4607 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
4608 if (nextbno != NULLFSBLOCK)
4609 xfs_btree_reada_bufl(mp, nextbno, 1);
4611 * Copy records into the extent records.
4613 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
4615 for (j = 0; j < num_recs; j++, i++, frp++) {
4616 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
4617 trp->l0 = be64_to_cpu(frp->l0);
4618 trp->l1 = be64_to_cpu(frp->l1);
4620 if (exntf == XFS_EXTFMT_NOSTATE) {
4622 * Check all attribute bmap btree records and
4623 * any "older" data bmap btree records for a
4624 * set bit in the "extent flag" position.
4626 if (unlikely(xfs_check_nostate_extents(ifp,
4627 start, num_recs))) {
4628 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4634 xfs_trans_brelse(tp, bp);
4637 * If we've reached the end, stop.
4639 if (bno == NULLFSBLOCK)
4641 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4642 XFS_BMAP_BTREE_REF)))
4644 block = XFS_BUF_TO_BLOCK(bp);
4646 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4647 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4648 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
4651 xfs_trans_brelse(tp, bp);
4652 return XFS_ERROR(EFSCORRUPTED);
4655 #ifdef XFS_BMAP_TRACE
4657 * Add bmap trace insert entries for all the contents of the extent records.
4660 xfs_bmap_trace_exlist(
4661 const char *fname, /* function name */
4662 xfs_inode_t *ip, /* incore inode pointer */
4663 xfs_extnum_t cnt, /* count of entries in the list */
4664 int whichfork) /* data or attr fork */
4666 xfs_bmbt_rec_host_t *ep; /* current extent record */
4667 xfs_extnum_t idx; /* extent record index */
4668 xfs_ifork_t *ifp; /* inode fork pointer */
4669 xfs_bmbt_irec_t s; /* file extent record */
4671 ifp = XFS_IFORK_PTR(ip, whichfork);
4672 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4673 for (idx = 0; idx < cnt; idx++) {
4674 ep = xfs_iext_get_ext(ifp, idx);
4675 xfs_bmbt_get_all(ep, &s);
4676 XFS_BMAP_TRACE_INSERT("exlist", ip, idx, 1, &s, NULL,
4684 * Validate that the bmbt_irecs being returned from bmapi are valid
4685 * given the callers original parameters. Specifically check the
4686 * ranges of the returned irecs to ensure that they only extent beyond
4687 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4690 xfs_bmap_validate_ret(
4694 xfs_bmbt_irec_t *mval,
4698 int i; /* index to map values */
4700 ASSERT(ret_nmap <= nmap);
4702 for (i = 0; i < ret_nmap; i++) {
4703 ASSERT(mval[i].br_blockcount > 0);
4704 if (!(flags & XFS_BMAPI_ENTIRE)) {
4705 ASSERT(mval[i].br_startoff >= bno);
4706 ASSERT(mval[i].br_blockcount <= len);
4707 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4710 ASSERT(mval[i].br_startoff < bno + len);
4711 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4715 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4716 mval[i].br_startoff);
4717 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4718 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4719 mval[i].br_startblock != HOLESTARTBLOCK);
4720 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4721 mval[i].br_state == XFS_EXT_UNWRITTEN);
4728 * Map file blocks to filesystem blocks.
4729 * File range is given by the bno/len pair.
4730 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4731 * into a hole or past eof.
4732 * Only allocates blocks from a single allocation group,
4733 * to avoid locking problems.
4734 * The returned value in "firstblock" from the first call in a transaction
4735 * must be remembered and presented to subsequent calls in "firstblock".
4736 * An upper bound for the number of blocks to be allocated is supplied to
4737 * the first call in "total"; if no allocation group has that many free
4738 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4742 xfs_trans_t *tp, /* transaction pointer */
4743 xfs_inode_t *ip, /* incore inode */
4744 xfs_fileoff_t bno, /* starting file offs. mapped */
4745 xfs_filblks_t len, /* length to map in file */
4746 int flags, /* XFS_BMAPI_... */
4747 xfs_fsblock_t *firstblock, /* first allocated block
4748 controls a.g. for allocs */
4749 xfs_extlen_t total, /* total blocks needed */
4750 xfs_bmbt_irec_t *mval, /* output: map values */
4751 int *nmap, /* i/o: mval size/count */
4752 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4753 xfs_extdelta_t *delta) /* o: change made to incore extents */
4755 xfs_fsblock_t abno; /* allocated block number */
4756 xfs_extlen_t alen; /* allocated extent length */
4757 xfs_fileoff_t aoff; /* allocated file offset */
4758 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
4759 xfs_btree_cur_t *cur; /* bmap btree cursor */
4760 xfs_fileoff_t end; /* end of mapped file region */
4761 int eof; /* we've hit the end of extents */
4762 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
4763 int error; /* error return */
4764 xfs_bmbt_irec_t got; /* current file extent record */
4765 xfs_ifork_t *ifp; /* inode fork pointer */
4766 xfs_extlen_t indlen; /* indirect blocks length */
4767 xfs_extnum_t lastx; /* last useful extent number */
4768 int logflags; /* flags for transaction logging */
4769 xfs_extlen_t minleft; /* min blocks left after allocation */
4770 xfs_extlen_t minlen; /* min allocation size */
4771 xfs_mount_t *mp; /* xfs mount structure */
4772 int n; /* current extent index */
4773 int nallocs; /* number of extents alloc'd */
4774 xfs_extnum_t nextents; /* number of extents in file */
4775 xfs_fileoff_t obno; /* old block number (offset) */
4776 xfs_bmbt_irec_t prev; /* previous file extent record */
4777 int tmp_logflags; /* temp flags holder */
4778 int whichfork; /* data or attr fork */
4779 char inhole; /* current location is hole in file */
4780 char wasdelay; /* old extent was delayed */
4781 char wr; /* this is a write request */
4782 char rt; /* this is a realtime file */
4784 xfs_fileoff_t orig_bno; /* original block number value */
4785 int orig_flags; /* original flags arg value */
4786 xfs_filblks_t orig_len; /* original value of len arg */
4787 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4788 int orig_nmap; /* original value of *nmap */
4797 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4798 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4799 XFS_ATTR_FORK : XFS_DATA_FORK;
4801 if (unlikely(XFS_TEST_ERROR(
4802 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4803 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4804 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4805 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4806 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4807 return XFS_ERROR(EFSCORRUPTED);
4809 if (XFS_FORCED_SHUTDOWN(mp))
4810 return XFS_ERROR(EIO);
4811 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4812 ifp = XFS_IFORK_PTR(ip, whichfork);
4813 ASSERT(ifp->if_ext_max ==
4814 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4815 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4816 XFS_STATS_INC(xs_blk_mapw);
4818 XFS_STATS_INC(xs_blk_mapr);
4820 * IGSTATE flag is used to combine extents which
4821 * differ only due to the state of the extents.
4822 * This technique is used from xfs_getbmap()
4823 * when the caller does not wish to see the
4824 * separation (which is the default).
4826 * This technique is also used when writing a
4827 * buffer which has been partially written,
4828 * (usually by being flushed during a chunkread),
4829 * to ensure one write takes place. This also
4830 * prevents a change in the xfs inode extents at
4831 * this time, intentionally. This change occurs
4832 * on completion of the write operation, in
4833 * xfs_strat_comp(), where the xfs_bmapi() call
4834 * is transactioned, and the extents combined.
4836 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4837 wr = 0; /* no allocations are allowed */
4838 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
4842 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4844 if ((error = xfs_bmap_local_to_extents(tp, ip,
4845 firstblock, total, &logflags, whichfork)))
4848 if (wr && *firstblock == NULLFSBLOCK) {
4849 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4850 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4855 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4856 (error = xfs_iread_extents(tp, ip, whichfork)))
4858 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4860 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4866 delta->xed_startoff = NULLFILEOFF;
4867 delta->xed_blockcount = 0;
4869 while (bno < end && n < *nmap) {
4871 * Reading past eof, act as though there's a hole
4875 got.br_startoff = end;
4876 inhole = eof || got.br_startoff > bno;
4877 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
4878 isnullstartblock(got.br_startblock);
4880 * First, deal with the hole before the allocated space
4881 * that we found, if any.
4883 if (wr && (inhole || wasdelay)) {
4885 * For the wasdelay case, we could also just
4886 * allocate the stuff asked for in this bmap call
4887 * but that wouldn't be as good.
4889 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
4890 alen = (xfs_extlen_t)got.br_blockcount;
4891 aoff = got.br_startoff;
4892 if (lastx != NULLEXTNUM && lastx) {
4893 ep = xfs_iext_get_ext(ifp, lastx - 1);
4894 xfs_bmbt_get_all(ep, &prev);
4896 } else if (wasdelay) {
4897 alen = (xfs_extlen_t)
4898 XFS_FILBLKS_MIN(len,
4900 got.br_blockcount) - bno);
4903 alen = (xfs_extlen_t)
4904 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4906 alen = (xfs_extlen_t)
4907 XFS_FILBLKS_MIN(alen,
4908 got.br_startoff - bno);
4911 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4912 if (flags & XFS_BMAPI_DELAY) {
4915 /* Figure out the extent size, adjust alen */
4916 extsz = xfs_get_extsz_hint(ip);
4918 error = xfs_bmap_extsize_align(mp,
4921 flags&XFS_BMAPI_DELAY,
4922 flags&XFS_BMAPI_CONVERT,
4928 extsz = alen / mp->m_sb.sb_rextsize;
4931 * Make a transaction-less quota reservation for
4932 * delayed allocation blocks. This number gets
4933 * adjusted later. We return if we haven't
4934 * allocated blocks already inside this loop.
4936 error = xfs_trans_reserve_quota_nblks(
4937 NULL, ip, (long)alen, 0,
4938 rt ? XFS_QMOPT_RES_RTBLKS :
4939 XFS_QMOPT_RES_REGBLKS);
4943 ASSERT(cur == NULL);
4950 * Split changing sb for alen and indlen since
4951 * they could be coming from different places.
4953 indlen = (xfs_extlen_t)
4954 xfs_bmap_worst_indlen(ip, alen);
4958 error = xfs_mod_incore_sb(mp,
4960 -((int64_t)extsz), (flags &
4961 XFS_BMAPI_RSVBLOCKS));
4963 error = xfs_mod_incore_sb(mp,
4965 -((int64_t)alen), (flags &
4966 XFS_BMAPI_RSVBLOCKS));
4969 error = xfs_mod_incore_sb(mp,
4971 -((int64_t)indlen), (flags &
4972 XFS_BMAPI_RSVBLOCKS));
4974 xfs_mod_incore_sb(mp,
4976 (int64_t)extsz, (flags &
4977 XFS_BMAPI_RSVBLOCKS));
4979 xfs_mod_incore_sb(mp,
4981 (int64_t)alen, (flags &
4982 XFS_BMAPI_RSVBLOCKS));
4986 if (XFS_IS_QUOTA_ON(mp))
4987 /* unreserve the blocks now */
4989 xfs_trans_unreserve_quota_nblks(
4992 XFS_QMOPT_RES_RTBLKS :
4993 XFS_QMOPT_RES_REGBLKS);
4997 ip->i_delayed_blks += alen;
4998 abno = nullstartblock(indlen);
5001 * If first time, allocate and fill in
5002 * once-only bma fields.
5004 if (bma.ip == NULL) {
5012 /* Indicate if this is the first user data
5013 * in the file, or just any user data.
5015 if (!(flags & XFS_BMAPI_METADATA)) {
5016 bma.userdata = (aoff == 0) ?
5017 XFS_ALLOC_INITIAL_USER_DATA :
5021 * Fill in changeable bma fields.
5024 bma.firstblock = *firstblock;
5027 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
5028 bma.wasdel = wasdelay;
5029 bma.minlen = minlen;
5030 bma.low = flist->xbf_low;
5031 bma.minleft = minleft;
5033 * Only want to do the alignment at the
5034 * eof if it is userdata and allocation length
5035 * is larger than a stripe unit.
5037 if (mp->m_dalign && alen >= mp->m_dalign &&
5038 (!(flags & XFS_BMAPI_METADATA)) &&
5039 (whichfork == XFS_DATA_FORK)) {
5040 if ((error = xfs_bmap_isaeof(ip, aoff,
5041 whichfork, &bma.aeof)))
5048 if ((error = xfs_bmap_alloc(&bma)))
5051 * Copy out result fields.
5054 if ((flist->xbf_low = bma.low))
5058 ASSERT(*firstblock == NULLFSBLOCK ||
5059 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5060 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
5062 XFS_FSB_TO_AGNO(mp, *firstblock) <
5063 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
5064 *firstblock = bma.firstblock;
5066 cur->bc_private.b.firstblock =
5068 if (abno == NULLFSBLOCK)
5070 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5071 cur = xfs_bmbt_init_cursor(mp, tp,
5073 cur->bc_private.b.firstblock =
5075 cur->bc_private.b.flist = flist;
5078 * Bump the number of extents we've allocated
5084 cur->bc_private.b.flags =
5085 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
5086 got.br_startoff = aoff;
5087 got.br_startblock = abno;
5088 got.br_blockcount = alen;
5089 got.br_state = XFS_EXT_NORM; /* assume normal */
5091 * Determine state of extent, and the filesystem.
5092 * A wasdelay extent has been initialized, so
5093 * shouldn't be flagged as unwritten.
5095 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5096 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5097 got.br_state = XFS_EXT_UNWRITTEN;
5099 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
5100 firstblock, flist, &tmp_logflags, delta,
5101 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5102 logflags |= tmp_logflags;
5105 lastx = ifp->if_lastex;
5106 ep = xfs_iext_get_ext(ifp, lastx);
5107 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5108 xfs_bmbt_get_all(ep, &got);
5109 ASSERT(got.br_startoff <= aoff);
5110 ASSERT(got.br_startoff + got.br_blockcount >=
5113 if (flags & XFS_BMAPI_DELAY) {
5114 ASSERT(isnullstartblock(got.br_startblock));
5115 ASSERT(startblockval(got.br_startblock) > 0);
5117 ASSERT(got.br_state == XFS_EXT_NORM ||
5118 got.br_state == XFS_EXT_UNWRITTEN);
5121 * Fall down into the found allocated space case.
5123 } else if (inhole) {
5125 * Reading in a hole.
5127 mval->br_startoff = bno;
5128 mval->br_startblock = HOLESTARTBLOCK;
5129 mval->br_blockcount =
5130 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5131 mval->br_state = XFS_EXT_NORM;
5132 bno += mval->br_blockcount;
5133 len -= mval->br_blockcount;
5139 * Then deal with the allocated space we found.
5142 if (!(flags & XFS_BMAPI_ENTIRE) &&
5143 (got.br_startoff + got.br_blockcount > obno)) {
5146 ASSERT((bno >= obno) || (n == 0));
5148 mval->br_startoff = bno;
5149 if (isnullstartblock(got.br_startblock)) {
5150 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5151 mval->br_startblock = DELAYSTARTBLOCK;
5153 mval->br_startblock =
5155 (bno - got.br_startoff);
5157 * Return the minimum of what we got and what we
5158 * asked for for the length. We can use the len
5159 * variable here because it is modified below
5160 * and we could have been there before coming
5161 * here if the first part of the allocation
5162 * didn't overlap what was asked for.
5164 mval->br_blockcount =
5165 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5166 (bno - got.br_startoff));
5167 mval->br_state = got.br_state;
5168 ASSERT(mval->br_blockcount <= len);
5171 if (isnullstartblock(mval->br_startblock)) {
5172 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5173 mval->br_startblock = DELAYSTARTBLOCK;
5178 * Check if writing previously allocated but
5179 * unwritten extents.
5181 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5182 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5184 * Modify (by adding) the state flag, if writing.
5186 ASSERT(mval->br_blockcount <= len);
5187 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5188 cur = xfs_bmbt_init_cursor(mp,
5190 cur->bc_private.b.firstblock =
5192 cur->bc_private.b.flist = flist;
5194 mval->br_state = XFS_EXT_NORM;
5195 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
5196 firstblock, flist, &tmp_logflags, delta,
5197 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5198 logflags |= tmp_logflags;
5201 lastx = ifp->if_lastex;
5202 ep = xfs_iext_get_ext(ifp, lastx);
5203 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5204 xfs_bmbt_get_all(ep, &got);
5206 * We may have combined previously unwritten
5207 * space with written space, so generate
5210 if (mval->br_blockcount < len)
5214 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5215 ((mval->br_startoff + mval->br_blockcount) <= end));
5216 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5217 (mval->br_blockcount <= len) ||
5218 (mval->br_startoff < obno));
5219 bno = mval->br_startoff + mval->br_blockcount;
5221 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5222 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5223 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5224 ASSERT(mval->br_state == mval[-1].br_state);
5225 mval[-1].br_blockcount = mval->br_blockcount;
5226 mval[-1].br_state = mval->br_state;
5227 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5228 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5229 mval[-1].br_startblock != HOLESTARTBLOCK &&
5230 mval->br_startblock ==
5231 mval[-1].br_startblock + mval[-1].br_blockcount &&
5232 ((flags & XFS_BMAPI_IGSTATE) ||
5233 mval[-1].br_state == mval->br_state)) {
5234 ASSERT(mval->br_startoff ==
5235 mval[-1].br_startoff + mval[-1].br_blockcount);
5236 mval[-1].br_blockcount += mval->br_blockcount;
5238 mval->br_startblock == DELAYSTARTBLOCK &&
5239 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5240 mval->br_startoff ==
5241 mval[-1].br_startoff + mval[-1].br_blockcount) {
5242 mval[-1].br_blockcount += mval->br_blockcount;
5243 mval[-1].br_state = mval->br_state;
5244 } else if (!((n == 0) &&
5245 ((mval->br_startoff + mval->br_blockcount) <=
5251 * If we're done, stop now. Stop when we've allocated
5252 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5253 * the transaction may get too big.
5255 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5258 * Else go on to the next record.
5260 ep = xfs_iext_get_ext(ifp, ++lastx);
5262 if (lastx >= nextents)
5265 xfs_bmbt_get_all(ep, &got);
5267 ifp->if_lastex = lastx;
5270 * Transform from btree to extents, give it cur.
5272 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5273 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5275 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5276 &tmp_logflags, whichfork);
5277 logflags |= tmp_logflags;
5281 ASSERT(ifp->if_ext_max ==
5282 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5283 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5284 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5286 if (delta && delta->xed_startoff != NULLFILEOFF) {
5287 /* A change was actually made.
5288 * Note that delta->xed_blockount is an offset at this
5289 * point and needs to be converted to a block count.
5291 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5292 delta->xed_blockcount -= delta->xed_startoff;
5296 * Log everything. Do this after conversion, there's no point in
5297 * logging the extent records if we've converted to btree format.
5299 if ((logflags & xfs_ilog_fext(whichfork)) &&
5300 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5301 logflags &= ~xfs_ilog_fext(whichfork);
5302 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5303 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5304 logflags &= ~xfs_ilog_fbroot(whichfork);
5306 * Log whatever the flags say, even if error. Otherwise we might miss
5307 * detecting a case where the data is changed, there's an error,
5308 * and it's not logged so we don't shutdown when we should.
5312 xfs_trans_log_inode(tp, ip, logflags);
5316 ASSERT(*firstblock == NULLFSBLOCK ||
5317 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5319 cur->bc_private.b.firstblock) ||
5321 XFS_FSB_TO_AGNO(mp, *firstblock) <
5323 cur->bc_private.b.firstblock)));
5324 *firstblock = cur->bc_private.b.firstblock;
5326 xfs_btree_del_cursor(cur,
5327 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5330 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5336 * Map file blocks to filesystem blocks, simple version.
5337 * One block (extent) only, read-only.
5338 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5339 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5340 * was set and all the others were clear.
5344 xfs_trans_t *tp, /* transaction pointer */
5345 xfs_inode_t *ip, /* incore inode */
5346 int whichfork, /* data or attr fork */
5347 xfs_fsblock_t *fsb, /* output: mapped block */
5348 xfs_fileoff_t bno) /* starting file offs. mapped */
5350 int eof; /* we've hit the end of extents */
5351 int error; /* error return */
5352 xfs_bmbt_irec_t got; /* current file extent record */
5353 xfs_ifork_t *ifp; /* inode fork pointer */
5354 xfs_extnum_t lastx; /* last useful extent number */
5355 xfs_bmbt_irec_t prev; /* previous file extent record */
5357 ifp = XFS_IFORK_PTR(ip, whichfork);
5359 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5360 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5361 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5363 return XFS_ERROR(EFSCORRUPTED);
5365 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5366 return XFS_ERROR(EIO);
5367 XFS_STATS_INC(xs_blk_mapr);
5368 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5369 (error = xfs_iread_extents(tp, ip, whichfork)))
5371 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5374 * Reading past eof, act as though there's a hole
5377 if (eof || got.br_startoff > bno) {
5381 ASSERT(!isnullstartblock(got.br_startblock));
5382 ASSERT(bno < got.br_startoff + got.br_blockcount);
5383 *fsb = got.br_startblock + (bno - got.br_startoff);
5384 ifp->if_lastex = lastx;
5389 * Unmap (remove) blocks from a file.
5390 * If nexts is nonzero then the number of extents to remove is limited to
5391 * that value. If not all extents in the block range can be removed then
5396 xfs_trans_t *tp, /* transaction pointer */
5397 struct xfs_inode *ip, /* incore inode */
5398 xfs_fileoff_t bno, /* starting offset to unmap */
5399 xfs_filblks_t len, /* length to unmap in file */
5400 int flags, /* misc flags */
5401 xfs_extnum_t nexts, /* number of extents max */
5402 xfs_fsblock_t *firstblock, /* first allocated block
5403 controls a.g. for allocs */
5404 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5405 xfs_extdelta_t *delta, /* o: change made to incore
5407 int *done) /* set if not done yet */
5409 xfs_btree_cur_t *cur; /* bmap btree cursor */
5410 xfs_bmbt_irec_t del; /* extent being deleted */
5411 int eof; /* is deleting at eof */
5412 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
5413 int error; /* error return value */
5414 xfs_extnum_t extno; /* extent number in list */
5415 xfs_bmbt_irec_t got; /* current extent record */
5416 xfs_ifork_t *ifp; /* inode fork pointer */
5417 int isrt; /* freeing in rt area */
5418 xfs_extnum_t lastx; /* last extent index used */
5419 int logflags; /* transaction logging flags */
5420 xfs_extlen_t mod; /* rt extent offset */
5421 xfs_mount_t *mp; /* mount structure */
5422 xfs_extnum_t nextents; /* number of file extents */
5423 xfs_bmbt_irec_t prev; /* previous extent record */
5424 xfs_fileoff_t start; /* first file offset deleted */
5425 int tmp_logflags; /* partial logging flags */
5426 int wasdel; /* was a delayed alloc extent */
5427 int whichfork; /* data or attribute fork */
5428 int rsvd; /* OK to allocate reserved blocks */
5431 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5432 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5433 XFS_ATTR_FORK : XFS_DATA_FORK;
5434 ifp = XFS_IFORK_PTR(ip, whichfork);
5436 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5437 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5438 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5440 return XFS_ERROR(EFSCORRUPTED);
5443 if (XFS_FORCED_SHUTDOWN(mp))
5444 return XFS_ERROR(EIO);
5445 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5448 ASSERT(ifp->if_ext_max ==
5449 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5450 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5451 (error = xfs_iread_extents(tp, ip, whichfork)))
5453 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5454 if (nextents == 0) {
5458 XFS_STATS_INC(xs_blk_unmap);
5459 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5461 bno = start + len - 1;
5462 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5465 delta->xed_startoff = NULLFILEOFF;
5466 delta->xed_blockcount = 0;
5469 * Check to see if the given block number is past the end of the
5470 * file, back up to the last block if so...
5473 ep = xfs_iext_get_ext(ifp, --lastx);
5474 xfs_bmbt_get_all(ep, &got);
5475 bno = got.br_startoff + got.br_blockcount - 1;
5478 if (ifp->if_flags & XFS_IFBROOT) {
5479 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5480 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5481 cur->bc_private.b.firstblock = *firstblock;
5482 cur->bc_private.b.flist = flist;
5483 cur->bc_private.b.flags = 0;
5487 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5488 (nexts == 0 || extno < nexts)) {
5490 * Is the found extent after a hole in which bno lives?
5491 * Just back up to the previous extent, if so.
5493 if (got.br_startoff > bno) {
5496 ep = xfs_iext_get_ext(ifp, lastx);
5497 xfs_bmbt_get_all(ep, &got);
5500 * Is the last block of this extent before the range
5501 * we're supposed to delete? If so, we're done.
5503 bno = XFS_FILEOFF_MIN(bno,
5504 got.br_startoff + got.br_blockcount - 1);
5508 * Then deal with the (possibly delayed) allocated space
5513 wasdel = isnullstartblock(del.br_startblock);
5514 if (got.br_startoff < start) {
5515 del.br_startoff = start;
5516 del.br_blockcount -= start - got.br_startoff;
5518 del.br_startblock += start - got.br_startoff;
5520 if (del.br_startoff + del.br_blockcount > bno + 1)
5521 del.br_blockcount = bno + 1 - del.br_startoff;
5522 sum = del.br_startblock + del.br_blockcount;
5524 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5526 * Realtime extent not lined up at the end.
5527 * The extent could have been split into written
5528 * and unwritten pieces, or we could just be
5529 * unmapping part of it. But we can't really
5530 * get rid of part of a realtime extent.
5532 if (del.br_state == XFS_EXT_UNWRITTEN ||
5533 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5535 * This piece is unwritten, or we're not
5536 * using unwritten extents. Skip over it.
5539 bno -= mod > del.br_blockcount ?
5540 del.br_blockcount : mod;
5541 if (bno < got.br_startoff) {
5543 xfs_bmbt_get_all(xfs_iext_get_ext(
5549 * It's written, turn it unwritten.
5550 * This is better than zeroing it.
5552 ASSERT(del.br_state == XFS_EXT_NORM);
5553 ASSERT(xfs_trans_get_block_res(tp) > 0);
5555 * If this spans a realtime extent boundary,
5556 * chop it back to the start of the one we end at.
5558 if (del.br_blockcount > mod) {
5559 del.br_startoff += del.br_blockcount - mod;
5560 del.br_startblock += del.br_blockcount - mod;
5561 del.br_blockcount = mod;
5563 del.br_state = XFS_EXT_UNWRITTEN;
5564 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5565 firstblock, flist, &logflags, delta,
5571 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5573 * Realtime extent is lined up at the end but not
5574 * at the front. We'll get rid of full extents if
5577 mod = mp->m_sb.sb_rextsize - mod;
5578 if (del.br_blockcount > mod) {
5579 del.br_blockcount -= mod;
5580 del.br_startoff += mod;
5581 del.br_startblock += mod;
5582 } else if ((del.br_startoff == start &&
5583 (del.br_state == XFS_EXT_UNWRITTEN ||
5584 xfs_trans_get_block_res(tp) == 0)) ||
5585 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5587 * Can't make it unwritten. There isn't
5588 * a full extent here so just skip it.
5590 ASSERT(bno >= del.br_blockcount);
5591 bno -= del.br_blockcount;
5592 if (bno < got.br_startoff) {
5594 xfs_bmbt_get_all(--ep, &got);
5597 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5599 * This one is already unwritten.
5600 * It must have a written left neighbor.
5601 * Unwrite the killed part of that one and
5605 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5607 ASSERT(prev.br_state == XFS_EXT_NORM);
5608 ASSERT(!isnullstartblock(prev.br_startblock));
5609 ASSERT(del.br_startblock ==
5610 prev.br_startblock + prev.br_blockcount);
5611 if (prev.br_startoff < start) {
5612 mod = start - prev.br_startoff;
5613 prev.br_blockcount -= mod;
5614 prev.br_startblock += mod;
5615 prev.br_startoff = start;
5617 prev.br_state = XFS_EXT_UNWRITTEN;
5618 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5619 &prev, firstblock, flist, &logflags,
5620 delta, XFS_DATA_FORK, 0);
5625 ASSERT(del.br_state == XFS_EXT_NORM);
5626 del.br_state = XFS_EXT_UNWRITTEN;
5627 error = xfs_bmap_add_extent(ip, lastx, &cur,
5628 &del, firstblock, flist, &logflags,
5629 delta, XFS_DATA_FORK, 0);
5636 ASSERT(startblockval(del.br_startblock) > 0);
5637 /* Update realtime/data freespace, unreserve quota */
5639 xfs_filblks_t rtexts;
5641 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5642 do_div(rtexts, mp->m_sb.sb_rextsize);
5643 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5644 (int64_t)rtexts, rsvd);
5645 (void)xfs_trans_reserve_quota_nblks(NULL,
5646 ip, -((long)del.br_blockcount), 0,
5647 XFS_QMOPT_RES_RTBLKS);
5649 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
5650 (int64_t)del.br_blockcount, rsvd);
5651 (void)xfs_trans_reserve_quota_nblks(NULL,
5652 ip, -((long)del.br_blockcount), 0,
5653 XFS_QMOPT_RES_REGBLKS);
5655 ip->i_delayed_blks -= del.br_blockcount;
5657 cur->bc_private.b.flags |=
5658 XFS_BTCUR_BPRV_WASDEL;
5660 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5662 * If it's the case where the directory code is running
5663 * with no block reservation, and the deleted block is in
5664 * the middle of its extent, and the resulting insert
5665 * of an extent would cause transformation to btree format,
5666 * then reject it. The calling code will then swap
5667 * blocks around instead.
5668 * We have to do this now, rather than waiting for the
5669 * conversion to btree format, since the transaction
5672 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5673 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5674 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5675 del.br_startoff > got.br_startoff &&
5676 del.br_startoff + del.br_blockcount <
5677 got.br_startoff + got.br_blockcount) {
5678 error = XFS_ERROR(ENOSPC);
5681 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5682 &tmp_logflags, delta, whichfork, rsvd);
5683 logflags |= tmp_logflags;
5686 bno = del.br_startoff - 1;
5688 lastx = ifp->if_lastex;
5690 * If not done go on to the next (previous) record.
5691 * Reset ep in case the extents array was re-alloced.
5693 ep = xfs_iext_get_ext(ifp, lastx);
5694 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5695 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5696 xfs_bmbt_get_startoff(ep) > bno) {
5698 ep = xfs_iext_get_ext(ifp, lastx);
5701 xfs_bmbt_get_all(ep, &got);
5705 ifp->if_lastex = lastx;
5706 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5707 ASSERT(ifp->if_ext_max ==
5708 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5710 * Convert to a btree if necessary.
5712 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5713 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5714 ASSERT(cur == NULL);
5715 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5716 &cur, 0, &tmp_logflags, whichfork);
5717 logflags |= tmp_logflags;
5722 * transform from btree to extents, give it cur
5724 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5725 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5726 ASSERT(cur != NULL);
5727 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5729 logflags |= tmp_logflags;
5734 * transform from extents to local?
5736 ASSERT(ifp->if_ext_max ==
5737 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5739 if (delta && delta->xed_startoff != NULLFILEOFF) {
5740 /* A change was actually made.
5741 * Note that delta->xed_blockount is an offset at this
5742 * point and needs to be converted to a block count.
5744 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5745 delta->xed_blockcount -= delta->xed_startoff;
5749 * Log everything. Do this after conversion, there's no point in
5750 * logging the extent records if we've converted to btree format.
5752 if ((logflags & xfs_ilog_fext(whichfork)) &&
5753 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5754 logflags &= ~xfs_ilog_fext(whichfork);
5755 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5756 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5757 logflags &= ~xfs_ilog_fbroot(whichfork);
5759 * Log inode even in the error case, if the transaction
5760 * is dirty we'll need to shut down the filesystem.
5763 xfs_trans_log_inode(tp, ip, logflags);
5766 *firstblock = cur->bc_private.b.firstblock;
5767 cur->bc_private.b.allocated = 0;
5769 xfs_btree_del_cursor(cur,
5770 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5776 * returns 1 for success, 0 if we failed to map the extent.
5779 xfs_getbmapx_fix_eof_hole(
5780 xfs_inode_t *ip, /* xfs incore inode pointer */
5781 struct getbmapx *out, /* output structure */
5782 int prealloced, /* this is a file with
5783 * preallocated data space */
5784 __int64_t end, /* last block requested */
5785 xfs_fsblock_t startblock)
5788 xfs_mount_t *mp; /* file system mount point */
5789 xfs_ifork_t *ifp; /* inode fork pointer */
5790 xfs_extnum_t lastx; /* last extent pointer */
5791 xfs_fileoff_t fileblock;
5793 if (startblock == HOLESTARTBLOCK) {
5795 out->bmv_block = -1;
5796 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5797 fixlen -= out->bmv_offset;
5798 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5799 /* Came to hole at EOF. Trim it. */
5802 out->bmv_length = fixlen;
5805 if (startblock == DELAYSTARTBLOCK)
5806 out->bmv_block = -2;
5808 out->bmv_block = xfs_fsb_to_db(ip, startblock);
5809 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5810 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5811 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5812 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5813 out->bmv_oflags |= BMV_OF_LAST;
5820 * Get inode's extents as described in bmv, and format for output.
5821 * Calls formatter to fill the user's buffer until all extents
5822 * are mapped, until the passed-in bmv->bmv_count slots have
5823 * been filled, or until the formatter short-circuits the loop,
5824 * if it is tracking filled-in extents on its own.
5826 int /* error code */
5829 struct getbmapx *bmv, /* user bmap structure */
5830 xfs_bmap_format_t formatter, /* format to user */
5831 void *arg) /* formatter arg */
5833 __int64_t bmvend; /* last block requested */
5834 int error = 0; /* return value */
5835 __int64_t fixlen; /* length for -1 case */
5836 int i; /* extent number */
5837 int lock; /* lock state */
5838 xfs_bmbt_irec_t *map; /* buffer for user's data */
5839 xfs_mount_t *mp; /* file system mount point */
5840 int nex; /* # of user extents can do */
5841 int nexleft; /* # of user extents left */
5842 int subnex; /* # of bmapi's can do */
5843 int nmap; /* number of map entries */
5844 struct getbmapx *out; /* output structure */
5845 int whichfork; /* data or attr fork */
5846 int prealloced; /* this is a file with
5847 * preallocated data space */
5848 int iflags; /* interface flags */
5849 int bmapi_flags; /* flags for xfs_bmapi */
5853 iflags = bmv->bmv_iflags;
5854 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5856 if (whichfork == XFS_ATTR_FORK) {
5857 if (XFS_IFORK_Q(ip)) {
5858 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5859 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5860 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5861 return XFS_ERROR(EINVAL);
5862 } else if (unlikely(
5863 ip->i_d.di_aformat != 0 &&
5864 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5865 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5867 return XFS_ERROR(EFSCORRUPTED);
5874 * If the BMV_IF_NO_DMAPI_READ interface bit specified, do
5875 * not generate a DMAPI read event. Otherwise, if the
5876 * DM_EVENT_READ bit is set for the file, generate a read
5877 * event in order that the DMAPI application may do its thing
5878 * before we return the extents. Usually this means restoring
5879 * user file data to regions of the file that look like holes.
5881 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5882 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5883 * If this were not true, callers of ioctl(XFS_IOC_GETBMAP)
5884 * could misinterpret holes in a DMAPI file as true holes,
5885 * when in fact they may represent offline user data.
5887 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) &&
5888 !(iflags & BMV_IF_NO_DMAPI_READ)) {
5889 error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip,
5892 return XFS_ERROR(error);
5895 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5896 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5897 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5898 return XFS_ERROR(EINVAL);
5900 if (xfs_get_extsz_hint(ip) ||
5901 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5903 fixlen = XFS_MAXIOFFSET(mp);
5906 fixlen = ip->i_size;
5910 if (bmv->bmv_length == -1) {
5911 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5913 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5914 } else if (bmv->bmv_length == 0) {
5915 bmv->bmv_entries = 0;
5917 } else if (bmv->bmv_length < 0) {
5918 return XFS_ERROR(EINVAL);
5921 nex = bmv->bmv_count - 1;
5923 return XFS_ERROR(EINVAL);
5924 bmvend = bmv->bmv_offset + bmv->bmv_length;
5927 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5928 return XFS_ERROR(ENOMEM);
5929 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5931 return XFS_ERROR(ENOMEM);
5933 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5934 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5935 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5936 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5938 goto out_unlock_iolock;
5941 ASSERT(ip->i_delayed_blks == 0);
5944 lock = xfs_ilock_map_shared(ip);
5947 * Don't let nex be bigger than the number of extents
5948 * we can have assuming alternating holes and real extents.
5950 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5951 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5953 bmapi_flags = xfs_bmapi_aflag(whichfork);
5954 if (!(iflags & BMV_IF_PREALLOC))
5955 bmapi_flags |= XFS_BMAPI_IGSTATE;
5958 * Allocate enough space to handle "subnex" maps at a time.
5962 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
5964 goto out_unlock_ilock;
5966 bmv->bmv_entries = 0;
5968 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5969 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5977 nmap = (nexleft > subnex) ? subnex : nexleft;
5978 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5979 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5980 bmapi_flags, NULL, 0, map, &nmap,
5984 ASSERT(nmap <= subnex);
5986 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5987 out[cur_ext].bmv_oflags = 0;
5988 if (map[i].br_state == XFS_EXT_UNWRITTEN)
5989 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
5990 else if (map[i].br_startblock == DELAYSTARTBLOCK)
5991 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5992 out[cur_ext].bmv_offset =
5993 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5994 out[cur_ext].bmv_length =
5995 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5996 out[cur_ext].bmv_unused1 = 0;
5997 out[cur_ext].bmv_unused2 = 0;
5998 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5999 (map[i].br_startblock != DELAYSTARTBLOCK));
6000 if (map[i].br_startblock == HOLESTARTBLOCK &&
6001 whichfork == XFS_ATTR_FORK) {
6002 /* came to the end of attribute fork */
6003 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
6007 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
6009 map[i].br_startblock))
6014 out[cur_ext].bmv_offset +
6015 out[cur_ext].bmv_length;
6017 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
6021 } while (nmap && nexleft && bmv->bmv_length);
6026 xfs_iunlock_map_shared(ip, lock);
6028 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
6030 for (i = 0; i < cur_ext; i++) {
6031 int full = 0; /* user array is full */
6033 /* format results & advance arg */
6034 error = formatter(&arg, &out[i], &full);
6044 * Check the last inode extent to determine whether this allocation will result
6045 * in blocks being allocated at the end of the file. When we allocate new data
6046 * blocks at the end of the file which do not start at the previous data block,
6047 * we will try to align the new blocks at stripe unit boundaries.
6049 STATIC int /* error */
6051 xfs_inode_t *ip, /* incore inode pointer */
6052 xfs_fileoff_t off, /* file offset in fsblocks */
6053 int whichfork, /* data or attribute fork */
6054 char *aeof) /* return value */
6056 int error; /* error return value */
6057 xfs_ifork_t *ifp; /* inode fork pointer */
6058 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
6059 xfs_extnum_t nextents; /* number of file extents */
6060 xfs_bmbt_irec_t s; /* expanded extent record */
6062 ASSERT(whichfork == XFS_DATA_FORK);
6063 ifp = XFS_IFORK_PTR(ip, whichfork);
6064 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6065 (error = xfs_iread_extents(NULL, ip, whichfork)))
6067 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6068 if (nextents == 0) {
6073 * Go to the last extent
6075 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6076 xfs_bmbt_get_all(lastrec, &s);
6078 * Check we are allocating in the last extent (for delayed allocations)
6079 * or past the last extent for non-delayed allocations.
6081 *aeof = (off >= s.br_startoff &&
6082 off < s.br_startoff + s.br_blockcount &&
6083 isnullstartblock(s.br_startblock)) ||
6084 off >= s.br_startoff + s.br_blockcount;
6089 * Check if the endoff is outside the last extent. If so the caller will grow
6090 * the allocation to a stripe unit boundary.
6094 xfs_inode_t *ip, /* incore inode pointer */
6095 xfs_fileoff_t endoff, /* file offset in fsblocks */
6096 int whichfork, /* data or attribute fork */
6097 int *eof) /* result value */
6099 xfs_fsblock_t blockcount; /* extent block count */
6100 int error; /* error return value */
6101 xfs_ifork_t *ifp; /* inode fork pointer */
6102 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
6103 xfs_extnum_t nextents; /* number of file extents */
6104 xfs_fileoff_t startoff; /* extent starting file offset */
6106 ASSERT(whichfork == XFS_DATA_FORK);
6107 ifp = XFS_IFORK_PTR(ip, whichfork);
6108 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6109 (error = xfs_iread_extents(NULL, ip, whichfork)))
6111 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6112 if (nextents == 0) {
6117 * Go to the last extent
6119 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6120 startoff = xfs_bmbt_get_startoff(lastrec);
6121 blockcount = xfs_bmbt_get_blockcount(lastrec);
6122 *eof = endoff >= startoff + blockcount;
6130 xfs_btree_cur_t *cur,
6140 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6141 bp = cur->bc_bufs[i];
6143 if (XFS_BUF_ADDR(bp) == bno)
6144 break; /* Found it */
6146 if (i == XFS_BTREE_MAXLEVELS)
6149 if (!bp) { /* Chase down all the log items to see if the bp is there */
6150 xfs_log_item_chunk_t *licp;
6154 licp = &tp->t_items;
6155 while (!bp && licp != NULL) {
6156 if (xfs_lic_are_all_free(licp)) {
6157 licp = licp->lic_next;
6160 for (i = 0; i < licp->lic_unused; i++) {
6161 xfs_log_item_desc_t *lidp;
6162 xfs_log_item_t *lip;
6163 xfs_buf_log_item_t *bip;
6166 if (xfs_lic_isfree(licp, i)) {
6170 lidp = xfs_lic_slot(licp, i);
6171 lip = lidp->lid_item;
6172 if (lip->li_type != XFS_LI_BUF)
6175 bip = (xfs_buf_log_item_t *)lip;
6178 if (XFS_BUF_ADDR(lbp) == bno) {
6180 break; /* Found it */
6183 licp = licp->lic_next;
6191 struct xfs_btree_block *block,
6197 __be64 *pp, *thispa; /* pointer to block address */
6198 xfs_bmbt_key_t *prevp, *keyp;
6200 ASSERT(be16_to_cpu(block->bb_level) > 0);
6203 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
6204 dmxr = mp->m_bmap_dmxr[0];
6205 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
6208 ASSERT(be64_to_cpu(prevp->br_startoff) <
6209 be64_to_cpu(keyp->br_startoff));
6214 * Compare the block numbers to see if there are dups.
6217 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
6219 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
6221 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
6223 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
6225 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
6226 if (*thispa == *pp) {
6227 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
6229 (unsigned long long)be64_to_cpu(*thispa));
6230 panic("%s: ptrs are equal in node\n",
6238 * Check that the extents for the inode ip are in the right order in all
6243 xfs_bmap_check_leaf_extents(
6244 xfs_btree_cur_t *cur, /* btree cursor or null */
6245 xfs_inode_t *ip, /* incore inode pointer */
6246 int whichfork) /* data or attr fork */
6248 struct xfs_btree_block *block; /* current btree block */
6249 xfs_fsblock_t bno; /* block # of "block" */
6250 xfs_buf_t *bp; /* buffer for "block" */
6251 int error; /* error return value */
6252 xfs_extnum_t i=0, j; /* index into the extents list */
6253 xfs_ifork_t *ifp; /* fork structure */
6254 int level; /* btree level, for checking */
6255 xfs_mount_t *mp; /* file system mount structure */
6256 __be64 *pp; /* pointer to block address */
6257 xfs_bmbt_rec_t *ep; /* pointer to current extent */
6258 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
6259 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
6262 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6268 ifp = XFS_IFORK_PTR(ip, whichfork);
6269 block = ifp->if_broot;
6271 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6273 level = be16_to_cpu(block->bb_level);
6275 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6276 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
6277 bno = be64_to_cpu(*pp);
6279 ASSERT(bno != NULLDFSBNO);
6280 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6281 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6284 * Go down the tree until leaf level is reached, following the first
6285 * pointer (leftmost) at each level.
6287 while (level-- > 0) {
6288 /* See if buf is in cur first */
6289 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6295 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6296 XFS_BMAP_BTREE_REF)))
6298 block = XFS_BUF_TO_BLOCK(bp);
6299 XFS_WANT_CORRUPTED_GOTO(
6300 xfs_bmap_sanity_check(mp, bp, level),
6306 * Check this block for basic sanity (increasing keys and
6307 * no duplicate blocks).
6310 xfs_check_block(block, mp, 0, 0);
6311 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
6312 bno = be64_to_cpu(*pp);
6313 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
6316 xfs_trans_brelse(NULL, bp);
6321 * Here with bp and block set to the leftmost leaf node in the tree.
6326 * Loop over all leaf nodes checking that all extents are in the right order.
6329 xfs_fsblock_t nextbno;
6330 xfs_extnum_t num_recs;
6333 num_recs = xfs_btree_get_numrecs(block);
6336 * Read-ahead the next leaf block, if any.
6339 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6342 * Check all the extents to make sure they are OK.
6343 * If we had a previous block, the last entry should
6344 * conform with the first entry in this one.
6347 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
6349 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
6350 xfs_bmbt_disk_get_blockcount(&last) <=
6351 xfs_bmbt_disk_get_startoff(ep));
6353 for (j = 1; j < num_recs; j++) {
6354 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
6355 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
6356 xfs_bmbt_disk_get_blockcount(ep) <=
6357 xfs_bmbt_disk_get_startoff(nextp));
6365 xfs_trans_brelse(NULL, bp);
6369 * If we've reached the end, stop.
6371 if (bno == NULLFSBLOCK)
6374 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6380 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6381 XFS_BMAP_BTREE_REF)))
6383 block = XFS_BUF_TO_BLOCK(bp);
6387 xfs_trans_brelse(NULL, bp);
6392 cmn_err(CE_WARN, "%s: at error0", __func__);
6394 xfs_trans_brelse(NULL, bp);
6396 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
6398 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
6404 * Count fsblocks of the given fork.
6407 xfs_bmap_count_blocks(
6408 xfs_trans_t *tp, /* transaction pointer */
6409 xfs_inode_t *ip, /* incore inode */
6410 int whichfork, /* data or attr fork */
6411 int *count) /* out: count of blocks */
6413 struct xfs_btree_block *block; /* current btree block */
6414 xfs_fsblock_t bno; /* block # of "block" */
6415 xfs_ifork_t *ifp; /* fork structure */
6416 int level; /* btree level, for checking */
6417 xfs_mount_t *mp; /* file system mount structure */
6418 __be64 *pp; /* pointer to block address */
6422 ifp = XFS_IFORK_PTR(ip, whichfork);
6423 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6424 xfs_bmap_count_leaves(ifp, 0,
6425 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6431 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6433 block = ifp->if_broot;
6434 level = be16_to_cpu(block->bb_level);
6436 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
6437 bno = be64_to_cpu(*pp);
6438 ASSERT(bno != NULLDFSBNO);
6439 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6440 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6442 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
6443 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6445 return XFS_ERROR(EFSCORRUPTED);
6452 * Recursively walks each level of a btree
6453 * to count total fsblocks is use.
6455 STATIC int /* error */
6456 xfs_bmap_count_tree(
6457 xfs_mount_t *mp, /* file system mount point */
6458 xfs_trans_t *tp, /* transaction pointer */
6459 xfs_ifork_t *ifp, /* inode fork pointer */
6460 xfs_fsblock_t blockno, /* file system block number */
6461 int levelin, /* level in btree */
6462 int *count) /* Count of blocks */
6465 xfs_buf_t *bp, *nbp;
6466 int level = levelin;
6468 xfs_fsblock_t bno = blockno;
6469 xfs_fsblock_t nextbno;
6470 struct xfs_btree_block *block, *nextblock;
6473 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6476 block = XFS_BUF_TO_BLOCK(bp);
6479 /* Not at node above leaves, count this level of nodes */
6480 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6481 while (nextbno != NULLFSBLOCK) {
6482 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6483 0, &nbp, XFS_BMAP_BTREE_REF)))
6486 nextblock = XFS_BUF_TO_BLOCK(nbp);
6487 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
6488 xfs_trans_brelse(tp, nbp);
6491 /* Dive to the next level */
6492 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
6493 bno = be64_to_cpu(*pp);
6494 if (unlikely((error =
6495 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6496 xfs_trans_brelse(tp, bp);
6497 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6498 XFS_ERRLEVEL_LOW, mp);
6499 return XFS_ERROR(EFSCORRUPTED);
6501 xfs_trans_brelse(tp, bp);
6503 /* count all level 1 nodes and their leaves */
6505 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6506 numrecs = be16_to_cpu(block->bb_numrecs);
6507 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
6508 xfs_trans_brelse(tp, bp);
6509 if (nextbno == NULLFSBLOCK)
6512 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6513 XFS_BMAP_BTREE_REF)))
6516 block = XFS_BUF_TO_BLOCK(bp);
6523 * Count leaf blocks given a range of extent records.
6526 xfs_bmap_count_leaves(
6534 for (b = 0; b < numrecs; b++) {
6535 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
6536 *count += xfs_bmbt_get_blockcount(frp);
6541 * Count leaf blocks given a range of extent records originally
6545 xfs_bmap_disk_count_leaves(
6546 struct xfs_mount *mp,
6547 struct xfs_btree_block *block,
6552 xfs_bmbt_rec_t *frp;
6554 for (b = 1; b <= numrecs; b++) {
6555 frp = XFS_BMBT_REC_ADDR(mp, block, b);
6556 *count += xfs_bmbt_disk_get_blockcount(frp);