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(ip, 0, 1, new,
596 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
600 if (!isnullstartblock(new->br_startblock)) {
601 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
602 logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
605 /* DELTA: single new extent */
607 if (delta->xed_startoff > new->br_startoff)
608 delta->xed_startoff = new->br_startoff;
609 if (delta->xed_blockcount <
610 new->br_startoff + new->br_blockcount)
611 delta->xed_blockcount = new->br_startoff +
616 * Any kind of new delayed allocation goes here.
618 else if (isnullstartblock(new->br_startblock)) {
620 ASSERT((cur->bc_private.b.flags &
621 XFS_BTCUR_BPRV_WASDEL) == 0);
622 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
623 &logflags, delta, rsvd)))
627 * Real allocation off the end of the file.
629 else if (idx == nextents) {
631 ASSERT((cur->bc_private.b.flags &
632 XFS_BTCUR_BPRV_WASDEL) == 0);
633 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
634 &logflags, delta, whichfork)))
637 xfs_bmbt_irec_t prev; /* old extent at offset idx */
640 * Get the record referred to by idx.
642 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
644 * If it's a real allocation record, and the new allocation ends
645 * after the start of the referred to record, then we're filling
646 * in a delayed or unwritten allocation with a real one, or
647 * converting real back to unwritten.
649 if (!isnullstartblock(new->br_startblock) &&
650 new->br_startoff + new->br_blockcount > prev.br_startoff) {
651 if (prev.br_state != XFS_EXT_UNWRITTEN &&
652 isnullstartblock(prev.br_startblock)) {
653 da_old = startblockval(prev.br_startblock);
655 ASSERT(cur->bc_private.b.flags &
656 XFS_BTCUR_BPRV_WASDEL);
657 if ((error = xfs_bmap_add_extent_delay_real(ip,
658 idx, &cur, new, &da_new, first, flist,
659 &logflags, delta, rsvd)))
661 } else if (new->br_state == XFS_EXT_NORM) {
662 ASSERT(new->br_state == XFS_EXT_NORM);
663 if ((error = xfs_bmap_add_extent_unwritten_real(
664 ip, idx, &cur, new, &logflags, delta)))
667 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
668 if ((error = xfs_bmap_add_extent_unwritten_real(
669 ip, idx, &cur, new, &logflags, delta)))
672 ASSERT(*curp == cur || *curp == NULL);
675 * Otherwise we're filling in a hole with an allocation.
679 ASSERT((cur->bc_private.b.flags &
680 XFS_BTCUR_BPRV_WASDEL) == 0);
681 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
682 new, &logflags, delta, whichfork)))
687 ASSERT(*curp == cur || *curp == NULL);
689 * Convert to a btree if necessary.
691 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
692 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
693 int tmp_logflags; /* partial log flag return val */
696 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
697 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
698 logflags |= tmp_logflags;
703 * Adjust for changes in reserved delayed indirect blocks.
704 * Nothing to do for disk quotas here.
706 if (da_old || da_new) {
711 nblks += cur->bc_private.b.allocated;
712 ASSERT(nblks <= da_old);
714 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
715 (int64_t)(da_old - nblks), rsvd);
718 * Clear out the allocated field, done with it now in any case.
721 cur->bc_private.b.allocated = 0;
727 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
729 *logflagsp = logflags;
734 * Called by xfs_bmap_add_extent to handle cases converting a delayed
735 * allocation to a real allocation.
737 STATIC int /* error */
738 xfs_bmap_add_extent_delay_real(
739 xfs_inode_t *ip, /* incore inode pointer */
740 xfs_extnum_t idx, /* extent number to update/insert */
741 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
742 xfs_bmbt_irec_t *new, /* new data to add to file extents */
743 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
744 xfs_fsblock_t *first, /* pointer to firstblock variable */
745 xfs_bmap_free_t *flist, /* list of extents to be freed */
746 int *logflagsp, /* inode logging flags */
747 xfs_extdelta_t *delta, /* Change made to incore extents */
748 int rsvd) /* OK to use reserved data block allocation */
750 xfs_btree_cur_t *cur; /* btree cursor */
751 int diff; /* temp value */
752 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
753 int error; /* error return value */
754 int i; /* temp state */
755 xfs_ifork_t *ifp; /* inode fork pointer */
756 xfs_fileoff_t new_endoff; /* end offset of new entry */
757 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
758 /* left is 0, right is 1, prev is 2 */
759 int rval=0; /* return value (logging flags) */
760 int state = 0;/* state bits, accessed thru macros */
761 xfs_filblks_t temp=0; /* value for dnew calculations */
762 xfs_filblks_t temp2=0;/* value for dnew calculations */
763 int tmp_rval; /* partial logging flags */
770 * Set up a bunch of variables to make the tests simpler.
773 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
774 ep = xfs_iext_get_ext(ifp, idx);
775 xfs_bmbt_get_all(ep, &PREV);
776 new_endoff = new->br_startoff + new->br_blockcount;
777 ASSERT(PREV.br_startoff <= new->br_startoff);
778 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
781 * Set flags determining what part of the previous delayed allocation
782 * extent is being replaced by a real allocation.
784 if (PREV.br_startoff == new->br_startoff)
785 state |= BMAP_LEFT_FILLING;
786 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
787 state |= BMAP_RIGHT_FILLING;
790 * Check and set flags if this segment has a left neighbor.
791 * Don't set contiguous if the combined extent would be too large.
794 state |= BMAP_LEFT_VALID;
795 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
797 if (isnullstartblock(LEFT.br_startblock))
798 state |= BMAP_LEFT_DELAY;
801 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
802 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
803 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
804 LEFT.br_state == new->br_state &&
805 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
806 state |= BMAP_LEFT_CONTIG;
809 * Check and set flags if this segment has a right neighbor.
810 * Don't set contiguous if the combined extent would be too large.
811 * Also check for all-three-contiguous being too large.
813 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
814 state |= BMAP_RIGHT_VALID;
815 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
817 if (isnullstartblock(RIGHT.br_startblock))
818 state |= BMAP_RIGHT_DELAY;
821 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
822 new_endoff == RIGHT.br_startoff &&
823 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
824 new->br_state == RIGHT.br_state &&
825 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
826 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
827 BMAP_RIGHT_FILLING)) !=
828 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
829 BMAP_RIGHT_FILLING) ||
830 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
832 state |= BMAP_RIGHT_CONTIG;
836 * Switch out based on the FILLING and CONTIG state bits.
838 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
839 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
840 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
841 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
843 * Filling in all of a previously delayed allocation extent.
844 * The left and right neighbors are both contiguous with new.
846 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
848 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
849 LEFT.br_blockcount + PREV.br_blockcount +
850 RIGHT.br_blockcount);
851 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
853 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
854 xfs_iext_remove(ip, idx, 2, state);
855 ip->i_df.if_lastex = idx - 1;
856 ip->i_d.di_nextents--;
858 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
860 rval = XFS_ILOG_CORE;
861 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
863 RIGHT.br_blockcount, &i)))
865 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
866 if ((error = xfs_btree_delete(cur, &i)))
868 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
869 if ((error = xfs_btree_decrement(cur, 0, &i)))
871 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
872 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
876 RIGHT.br_blockcount, LEFT.br_state)))
880 /* DELTA: Three in-core extents are replaced by one. */
881 temp = LEFT.br_startoff;
882 temp2 = LEFT.br_blockcount +
887 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
889 * Filling in all of a previously delayed allocation extent.
890 * The left neighbor is contiguous, the right is not.
892 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
894 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
895 LEFT.br_blockcount + PREV.br_blockcount);
896 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
898 ip->i_df.if_lastex = idx - 1;
899 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
900 xfs_iext_remove(ip, idx, 1, state);
902 rval = XFS_ILOG_DEXT;
905 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
906 LEFT.br_startblock, LEFT.br_blockcount,
909 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
910 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
913 PREV.br_blockcount, LEFT.br_state)))
917 /* DELTA: Two in-core extents are replaced by one. */
918 temp = LEFT.br_startoff;
919 temp2 = LEFT.br_blockcount +
923 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
925 * Filling in all of a previously delayed allocation extent.
926 * The right neighbor is contiguous, the left is not.
928 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
929 xfs_bmbt_set_startblock(ep, new->br_startblock);
930 xfs_bmbt_set_blockcount(ep,
931 PREV.br_blockcount + RIGHT.br_blockcount);
932 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
933 ip->i_df.if_lastex = idx;
934 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
935 xfs_iext_remove(ip, idx + 1, 1, state);
937 rval = XFS_ILOG_DEXT;
940 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
942 RIGHT.br_blockcount, &i)))
944 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
945 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
948 RIGHT.br_blockcount, PREV.br_state)))
952 /* DELTA: Two in-core extents are replaced by one. */
953 temp = PREV.br_startoff;
954 temp2 = PREV.br_blockcount +
958 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
960 * Filling in all of a previously delayed allocation extent.
961 * Neither the left nor right neighbors are contiguous with
964 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
965 xfs_bmbt_set_startblock(ep, new->br_startblock);
966 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
967 ip->i_df.if_lastex = idx;
968 ip->i_d.di_nextents++;
970 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
972 rval = XFS_ILOG_CORE;
973 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
974 new->br_startblock, new->br_blockcount,
977 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
978 cur->bc_rec.b.br_state = XFS_EXT_NORM;
979 if ((error = xfs_btree_insert(cur, &i)))
981 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
984 /* DELTA: The in-core extent described by new changed type. */
985 temp = new->br_startoff;
986 temp2 = new->br_blockcount;
989 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
991 * Filling in the first part of a previous delayed allocation.
992 * The left neighbor is contiguous.
994 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
995 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
996 LEFT.br_blockcount + new->br_blockcount);
997 xfs_bmbt_set_startoff(ep,
998 PREV.br_startoff + new->br_blockcount);
999 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
1000 temp = PREV.br_blockcount - new->br_blockcount;
1001 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
1002 xfs_bmbt_set_blockcount(ep, temp);
1003 ip->i_df.if_lastex = idx - 1;
1005 rval = XFS_ILOG_DEXT;
1008 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
1009 LEFT.br_startblock, LEFT.br_blockcount,
1012 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1013 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1015 LEFT.br_blockcount +
1020 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1021 startblockval(PREV.br_startblock));
1022 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1023 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
1025 /* DELTA: The boundary between two in-core extents moved. */
1026 temp = LEFT.br_startoff;
1027 temp2 = LEFT.br_blockcount +
1031 case BMAP_LEFT_FILLING:
1033 * Filling in the first part of a previous delayed allocation.
1034 * The left neighbor is not contiguous.
1036 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1037 xfs_bmbt_set_startoff(ep, new_endoff);
1038 temp = PREV.br_blockcount - new->br_blockcount;
1039 xfs_bmbt_set_blockcount(ep, temp);
1040 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
1042 xfs_iext_insert(ip, idx, 1, new, state);
1043 ip->i_df.if_lastex = idx;
1044 ip->i_d.di_nextents++;
1046 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1048 rval = XFS_ILOG_CORE;
1049 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1050 new->br_startblock, new->br_blockcount,
1053 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1054 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1055 if ((error = xfs_btree_insert(cur, &i)))
1057 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1059 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1060 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1061 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1062 first, flist, &cur, 1, &tmp_rval,
1068 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1069 startblockval(PREV.br_startblock) -
1070 (cur ? cur->bc_private.b.allocated : 0));
1071 ep = xfs_iext_get_ext(ifp, idx + 1);
1072 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1073 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx + 1, XFS_DATA_FORK);
1075 /* DELTA: One in-core extent is split in two. */
1076 temp = PREV.br_startoff;
1077 temp2 = PREV.br_blockcount;
1080 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1082 * Filling in the last part of a previous delayed allocation.
1083 * The right neighbor is contiguous with the new allocation.
1085 temp = PREV.br_blockcount - new->br_blockcount;
1086 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
1087 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
1088 xfs_bmbt_set_blockcount(ep, temp);
1089 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1090 new->br_startoff, new->br_startblock,
1091 new->br_blockcount + RIGHT.br_blockcount,
1093 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
1094 ip->i_df.if_lastex = idx + 1;
1096 rval = XFS_ILOG_DEXT;
1099 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1100 RIGHT.br_startblock,
1101 RIGHT.br_blockcount, &i)))
1103 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1104 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1106 new->br_blockcount +
1107 RIGHT.br_blockcount,
1111 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1112 startblockval(PREV.br_startblock));
1113 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1114 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
1116 /* DELTA: The boundary between two in-core extents moved. */
1117 temp = PREV.br_startoff;
1118 temp2 = PREV.br_blockcount +
1119 RIGHT.br_blockcount;
1122 case BMAP_RIGHT_FILLING:
1124 * Filling in the last part of a previous delayed allocation.
1125 * The right neighbor is not contiguous.
1127 temp = PREV.br_blockcount - new->br_blockcount;
1128 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1129 xfs_bmbt_set_blockcount(ep, temp);
1130 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1132 xfs_iext_insert(ip, idx + 1, 1, new, state);
1133 ip->i_df.if_lastex = idx + 1;
1134 ip->i_d.di_nextents++;
1136 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1138 rval = XFS_ILOG_CORE;
1139 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1140 new->br_startblock, new->br_blockcount,
1143 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1144 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1145 if ((error = xfs_btree_insert(cur, &i)))
1147 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1149 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1150 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1151 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1152 first, flist, &cur, 1, &tmp_rval,
1158 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1159 startblockval(PREV.br_startblock) -
1160 (cur ? cur->bc_private.b.allocated : 0));
1161 ep = xfs_iext_get_ext(ifp, idx);
1162 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1163 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1165 /* DELTA: One in-core extent is split in two. */
1166 temp = PREV.br_startoff;
1167 temp2 = PREV.br_blockcount;
1172 * Filling in the middle part of a previous delayed allocation.
1173 * Contiguity is impossible here.
1174 * This case is avoided almost all the time.
1176 temp = new->br_startoff - PREV.br_startoff;
1177 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
1178 xfs_bmbt_set_blockcount(ep, temp);
1180 r[1].br_state = PREV.br_state;
1181 r[1].br_startblock = 0;
1182 r[1].br_startoff = new_endoff;
1183 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1184 r[1].br_blockcount = temp2;
1185 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
1187 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
1188 ip->i_df.if_lastex = idx + 1;
1189 ip->i_d.di_nextents++;
1191 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1193 rval = XFS_ILOG_CORE;
1194 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1195 new->br_startblock, new->br_blockcount,
1198 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1199 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1200 if ((error = xfs_btree_insert(cur, &i)))
1202 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1204 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1205 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1206 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1207 first, flist, &cur, 1, &tmp_rval,
1213 temp = xfs_bmap_worst_indlen(ip, temp);
1214 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1215 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
1216 (cur ? cur->bc_private.b.allocated : 0));
1218 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
1220 * Ick gross gag me with a spoon.
1222 ASSERT(0); /* want to see if this ever happens! */
1228 !xfs_mod_incore_sb(ip->i_mount,
1229 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1236 !xfs_mod_incore_sb(ip->i_mount,
1237 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1242 ep = xfs_iext_get_ext(ifp, idx);
1243 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1244 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
1245 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
1246 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1247 nullstartblock((int)temp2));
1248 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
1249 *dnew = temp + temp2;
1250 /* DELTA: One in-core extent is split in three. */
1251 temp = PREV.br_startoff;
1252 temp2 = PREV.br_blockcount;
1255 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1256 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1257 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1258 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1259 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1260 case BMAP_LEFT_CONTIG:
1261 case BMAP_RIGHT_CONTIG:
1263 * These cases are all impossible.
1270 if (delta->xed_startoff > temp)
1271 delta->xed_startoff = temp;
1272 if (delta->xed_blockcount < temp2)
1273 delta->xed_blockcount = temp2;
1284 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1285 * allocation to a real allocation or vice versa.
1287 STATIC int /* error */
1288 xfs_bmap_add_extent_unwritten_real(
1289 xfs_inode_t *ip, /* incore inode pointer */
1290 xfs_extnum_t idx, /* extent number to update/insert */
1291 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1292 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1293 int *logflagsp, /* inode logging flags */
1294 xfs_extdelta_t *delta) /* Change made to incore extents */
1296 xfs_btree_cur_t *cur; /* btree cursor */
1297 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1298 int error; /* error return value */
1299 int i; /* temp state */
1300 xfs_ifork_t *ifp; /* inode fork pointer */
1301 xfs_fileoff_t new_endoff; /* end offset of new entry */
1302 xfs_exntst_t newext; /* new extent state */
1303 xfs_exntst_t oldext; /* old extent state */
1304 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1305 /* left is 0, right is 1, prev is 2 */
1306 int rval=0; /* return value (logging flags) */
1307 int state = 0;/* state bits, accessed thru macros */
1308 xfs_filblks_t temp=0;
1309 xfs_filblks_t temp2=0;
1315 * Set up a bunch of variables to make the tests simpler.
1319 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1320 ep = xfs_iext_get_ext(ifp, idx);
1321 xfs_bmbt_get_all(ep, &PREV);
1322 newext = new->br_state;
1323 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1324 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1325 ASSERT(PREV.br_state == oldext);
1326 new_endoff = new->br_startoff + new->br_blockcount;
1327 ASSERT(PREV.br_startoff <= new->br_startoff);
1328 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1331 * Set flags determining what part of the previous oldext allocation
1332 * extent is being replaced by a newext allocation.
1334 if (PREV.br_startoff == new->br_startoff)
1335 state |= BMAP_LEFT_FILLING;
1336 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1337 state |= BMAP_RIGHT_FILLING;
1340 * Check and set flags if this segment has a left neighbor.
1341 * Don't set contiguous if the combined extent would be too large.
1344 state |= BMAP_LEFT_VALID;
1345 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1347 if (isnullstartblock(LEFT.br_startblock))
1348 state |= BMAP_LEFT_DELAY;
1351 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1352 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1353 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1354 LEFT.br_state == newext &&
1355 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1356 state |= BMAP_LEFT_CONTIG;
1359 * Check and set flags if this segment has a right neighbor.
1360 * Don't set contiguous if the combined extent would be too large.
1361 * Also check for all-three-contiguous being too large.
1363 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1364 state |= BMAP_RIGHT_VALID;
1365 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1366 if (isnullstartblock(RIGHT.br_startblock))
1367 state |= BMAP_RIGHT_DELAY;
1370 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1371 new_endoff == RIGHT.br_startoff &&
1372 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1373 newext == RIGHT.br_state &&
1374 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1375 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1376 BMAP_RIGHT_FILLING)) !=
1377 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1378 BMAP_RIGHT_FILLING) ||
1379 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1381 state |= BMAP_RIGHT_CONTIG;
1384 * Switch out based on the FILLING and CONTIG state bits.
1386 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1387 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1388 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1389 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1391 * Setting all of a previous oldext extent to newext.
1392 * The left and right neighbors are both contiguous with new.
1394 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
1396 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1397 LEFT.br_blockcount + PREV.br_blockcount +
1398 RIGHT.br_blockcount);
1399 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
1401 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
1402 xfs_iext_remove(ip, idx, 2, state);
1403 ip->i_df.if_lastex = idx - 1;
1404 ip->i_d.di_nextents -= 2;
1406 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1408 rval = XFS_ILOG_CORE;
1409 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1410 RIGHT.br_startblock,
1411 RIGHT.br_blockcount, &i)))
1413 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1414 if ((error = xfs_btree_delete(cur, &i)))
1416 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1417 if ((error = xfs_btree_decrement(cur, 0, &i)))
1419 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1420 if ((error = xfs_btree_delete(cur, &i)))
1422 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1423 if ((error = xfs_btree_decrement(cur, 0, &i)))
1425 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1426 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1428 LEFT.br_blockcount + PREV.br_blockcount +
1429 RIGHT.br_blockcount, LEFT.br_state)))
1432 /* DELTA: Three in-core extents are replaced by one. */
1433 temp = LEFT.br_startoff;
1434 temp2 = LEFT.br_blockcount +
1435 PREV.br_blockcount +
1436 RIGHT.br_blockcount;
1439 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1441 * Setting all of a previous oldext extent to newext.
1442 * The left neighbor is contiguous, the right is not.
1444 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
1446 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1447 LEFT.br_blockcount + PREV.br_blockcount);
1448 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
1450 ip->i_df.if_lastex = idx - 1;
1451 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
1452 xfs_iext_remove(ip, idx, 1, state);
1453 ip->i_d.di_nextents--;
1455 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1457 rval = XFS_ILOG_CORE;
1458 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1459 PREV.br_startblock, PREV.br_blockcount,
1462 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1463 if ((error = xfs_btree_delete(cur, &i)))
1465 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1466 if ((error = xfs_btree_decrement(cur, 0, &i)))
1468 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1469 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1471 LEFT.br_blockcount + PREV.br_blockcount,
1475 /* DELTA: Two in-core extents are replaced by one. */
1476 temp = LEFT.br_startoff;
1477 temp2 = LEFT.br_blockcount +
1481 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1483 * Setting all of a previous oldext extent to newext.
1484 * The right neighbor is contiguous, the left is not.
1486 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx,
1488 xfs_bmbt_set_blockcount(ep,
1489 PREV.br_blockcount + RIGHT.br_blockcount);
1490 xfs_bmbt_set_state(ep, newext);
1491 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx,
1493 ip->i_df.if_lastex = idx;
1494 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
1495 xfs_iext_remove(ip, idx + 1, 1, state);
1496 ip->i_d.di_nextents--;
1498 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1500 rval = XFS_ILOG_CORE;
1501 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1502 RIGHT.br_startblock,
1503 RIGHT.br_blockcount, &i)))
1505 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1506 if ((error = xfs_btree_delete(cur, &i)))
1508 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1509 if ((error = xfs_btree_decrement(cur, 0, &i)))
1511 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1512 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1514 new->br_blockcount + RIGHT.br_blockcount,
1518 /* DELTA: Two in-core extents are replaced by one. */
1519 temp = PREV.br_startoff;
1520 temp2 = PREV.br_blockcount +
1521 RIGHT.br_blockcount;
1524 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1526 * Setting all of a previous oldext extent to newext.
1527 * Neither the left nor right neighbors are contiguous with
1530 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx,
1532 xfs_bmbt_set_state(ep, newext);
1533 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx,
1535 ip->i_df.if_lastex = idx;
1537 rval = XFS_ILOG_DEXT;
1540 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1541 new->br_startblock, new->br_blockcount,
1544 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1545 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1546 new->br_startblock, new->br_blockcount,
1550 /* DELTA: The in-core extent described by new changed type. */
1551 temp = new->br_startoff;
1552 temp2 = new->br_blockcount;
1555 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1557 * Setting the first part of a previous oldext extent to newext.
1558 * The left neighbor is contiguous.
1560 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1,
1562 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1563 LEFT.br_blockcount + new->br_blockcount);
1564 xfs_bmbt_set_startoff(ep,
1565 PREV.br_startoff + new->br_blockcount);
1566 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1,
1568 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx,
1570 xfs_bmbt_set_startblock(ep,
1571 new->br_startblock + new->br_blockcount);
1572 xfs_bmbt_set_blockcount(ep,
1573 PREV.br_blockcount - new->br_blockcount);
1574 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx,
1576 ip->i_df.if_lastex = idx - 1;
1578 rval = XFS_ILOG_DEXT;
1581 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1582 PREV.br_startblock, PREV.br_blockcount,
1585 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1586 if ((error = xfs_bmbt_update(cur,
1587 PREV.br_startoff + new->br_blockcount,
1588 PREV.br_startblock + new->br_blockcount,
1589 PREV.br_blockcount - new->br_blockcount,
1592 if ((error = xfs_btree_decrement(cur, 0, &i)))
1594 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1596 LEFT.br_blockcount + new->br_blockcount,
1600 /* DELTA: The boundary between two in-core extents moved. */
1601 temp = LEFT.br_startoff;
1602 temp2 = LEFT.br_blockcount +
1606 case BMAP_LEFT_FILLING:
1608 * Setting the first part of a previous oldext extent to newext.
1609 * The left neighbor is not contiguous.
1611 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1612 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1613 xfs_bmbt_set_startoff(ep, new_endoff);
1614 xfs_bmbt_set_blockcount(ep,
1615 PREV.br_blockcount - new->br_blockcount);
1616 xfs_bmbt_set_startblock(ep,
1617 new->br_startblock + new->br_blockcount);
1618 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1619 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
1621 xfs_iext_insert(ip, idx, 1, new, state);
1622 ip->i_df.if_lastex = idx;
1623 ip->i_d.di_nextents++;
1625 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1627 rval = XFS_ILOG_CORE;
1628 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1629 PREV.br_startblock, PREV.br_blockcount,
1632 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1633 if ((error = xfs_bmbt_update(cur,
1634 PREV.br_startoff + new->br_blockcount,
1635 PREV.br_startblock + new->br_blockcount,
1636 PREV.br_blockcount - new->br_blockcount,
1639 cur->bc_rec.b = *new;
1640 if ((error = xfs_btree_insert(cur, &i)))
1642 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1644 /* DELTA: One in-core extent is split in two. */
1645 temp = PREV.br_startoff;
1646 temp2 = PREV.br_blockcount;
1649 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1651 * Setting the last part of a previous oldext extent to newext.
1652 * The right neighbor is contiguous with the new allocation.
1654 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx,
1656 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1,
1658 xfs_bmbt_set_blockcount(ep,
1659 PREV.br_blockcount - new->br_blockcount);
1660 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx,
1662 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1663 new->br_startoff, new->br_startblock,
1664 new->br_blockcount + RIGHT.br_blockcount, newext);
1665 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1,
1667 ip->i_df.if_lastex = idx + 1;
1669 rval = XFS_ILOG_DEXT;
1672 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1674 PREV.br_blockcount, &i)))
1676 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1677 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1679 PREV.br_blockcount - new->br_blockcount,
1682 if ((error = xfs_btree_increment(cur, 0, &i)))
1684 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1686 new->br_blockcount + RIGHT.br_blockcount,
1690 /* DELTA: The boundary between two in-core extents moved. */
1691 temp = PREV.br_startoff;
1692 temp2 = PREV.br_blockcount +
1693 RIGHT.br_blockcount;
1696 case BMAP_RIGHT_FILLING:
1698 * Setting the last part of a previous oldext extent to newext.
1699 * The right neighbor is not contiguous.
1701 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1702 xfs_bmbt_set_blockcount(ep,
1703 PREV.br_blockcount - new->br_blockcount);
1704 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1705 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1707 xfs_iext_insert(ip, idx + 1, 1, new, state);
1708 ip->i_df.if_lastex = idx + 1;
1709 ip->i_d.di_nextents++;
1711 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1713 rval = XFS_ILOG_CORE;
1714 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1715 PREV.br_startblock, PREV.br_blockcount,
1718 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1719 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1721 PREV.br_blockcount - new->br_blockcount,
1724 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1725 new->br_startblock, new->br_blockcount,
1728 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1729 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1730 if ((error = xfs_btree_insert(cur, &i)))
1732 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1734 /* DELTA: One in-core extent is split in two. */
1735 temp = PREV.br_startoff;
1736 temp2 = PREV.br_blockcount;
1741 * Setting the middle part of a previous oldext extent to
1742 * newext. Contiguity is impossible here.
1743 * One extent becomes three extents.
1745 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
1746 xfs_bmbt_set_blockcount(ep,
1747 new->br_startoff - PREV.br_startoff);
1748 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
1750 r[1].br_startoff = new_endoff;
1751 r[1].br_blockcount =
1752 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1753 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1754 r[1].br_state = oldext;
1755 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
1757 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
1758 ip->i_df.if_lastex = idx + 1;
1759 ip->i_d.di_nextents += 2;
1761 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1763 rval = XFS_ILOG_CORE;
1764 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1765 PREV.br_startblock, PREV.br_blockcount,
1768 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1769 /* new right extent - oldext */
1770 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1771 r[1].br_startblock, r[1].br_blockcount,
1774 /* new left extent - oldext */
1775 cur->bc_rec.b = PREV;
1776 cur->bc_rec.b.br_blockcount =
1777 new->br_startoff - PREV.br_startoff;
1778 if ((error = xfs_btree_insert(cur, &i)))
1780 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1782 * Reset the cursor to the position of the new extent
1783 * we are about to insert as we can't trust it after
1784 * the previous insert.
1786 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1787 new->br_startblock, new->br_blockcount,
1790 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1791 /* new middle extent - newext */
1792 cur->bc_rec.b.br_state = new->br_state;
1793 if ((error = xfs_btree_insert(cur, &i)))
1795 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1797 /* DELTA: One in-core extent is split in three. */
1798 temp = PREV.br_startoff;
1799 temp2 = PREV.br_blockcount;
1802 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1803 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1804 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1805 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1806 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1807 case BMAP_LEFT_CONTIG:
1808 case BMAP_RIGHT_CONTIG:
1810 * These cases are all impossible.
1817 if (delta->xed_startoff > temp)
1818 delta->xed_startoff = temp;
1819 if (delta->xed_blockcount < temp2)
1820 delta->xed_blockcount = temp2;
1831 * Called by xfs_bmap_add_extent to handle cases converting a hole
1832 * to a delayed allocation.
1835 STATIC int /* error */
1836 xfs_bmap_add_extent_hole_delay(
1837 xfs_inode_t *ip, /* incore inode pointer */
1838 xfs_extnum_t idx, /* extent number to update/insert */
1839 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1840 int *logflagsp, /* inode logging flags */
1841 xfs_extdelta_t *delta, /* Change made to incore extents */
1842 int rsvd) /* OK to allocate reserved blocks */
1844 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
1845 xfs_ifork_t *ifp; /* inode fork pointer */
1846 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1847 xfs_filblks_t newlen=0; /* new indirect size */
1848 xfs_filblks_t oldlen=0; /* old indirect size */
1849 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1850 int state; /* state bits, accessed thru macros */
1851 xfs_filblks_t temp=0; /* temp for indirect calculations */
1852 xfs_filblks_t temp2=0;
1854 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1855 ep = xfs_iext_get_ext(ifp, idx);
1857 ASSERT(isnullstartblock(new->br_startblock));
1860 * Check and set flags if this segment has a left neighbor
1863 state |= BMAP_LEFT_VALID;
1864 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1866 if (isnullstartblock(left.br_startblock))
1867 state |= BMAP_LEFT_DELAY;
1871 * Check and set flags if the current (right) segment exists.
1872 * If it doesn't exist, we're converting the hole at end-of-file.
1874 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1875 state |= BMAP_RIGHT_VALID;
1876 xfs_bmbt_get_all(ep, &right);
1878 if (isnullstartblock(right.br_startblock))
1879 state |= BMAP_RIGHT_DELAY;
1883 * Set contiguity flags on the left and right neighbors.
1884 * Don't let extents get too large, even if the pieces are contiguous.
1886 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1887 left.br_startoff + left.br_blockcount == new->br_startoff &&
1888 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1889 state |= BMAP_LEFT_CONTIG;
1891 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1892 new->br_startoff + new->br_blockcount == right.br_startoff &&
1893 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1894 (!(state & BMAP_LEFT_CONTIG) ||
1895 (left.br_blockcount + new->br_blockcount +
1896 right.br_blockcount <= MAXEXTLEN)))
1897 state |= BMAP_RIGHT_CONTIG;
1900 * Switch out based on the contiguity flags.
1902 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1903 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1905 * New allocation is contiguous with delayed allocations
1906 * on the left and on the right.
1907 * Merge all three into a single extent record.
1909 temp = left.br_blockcount + new->br_blockcount +
1910 right.br_blockcount;
1911 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
1913 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1914 oldlen = startblockval(left.br_startblock) +
1915 startblockval(new->br_startblock) +
1916 startblockval(right.br_startblock);
1917 newlen = xfs_bmap_worst_indlen(ip, temp);
1918 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1919 nullstartblock((int)newlen));
1920 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
1922 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, XFS_DATA_FORK);
1923 xfs_iext_remove(ip, idx, 1, state);
1924 ip->i_df.if_lastex = idx - 1;
1925 /* DELTA: Two in-core extents were replaced by one. */
1927 temp = left.br_startoff;
1930 case BMAP_LEFT_CONTIG:
1932 * New allocation is contiguous with a delayed allocation
1934 * Merge the new allocation with the left neighbor.
1936 temp = left.br_blockcount + new->br_blockcount;
1937 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1,
1939 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1940 oldlen = startblockval(left.br_startblock) +
1941 startblockval(new->br_startblock);
1942 newlen = xfs_bmap_worst_indlen(ip, temp);
1943 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1944 nullstartblock((int)newlen));
1945 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1,
1947 ip->i_df.if_lastex = idx - 1;
1948 /* DELTA: One in-core extent grew into a hole. */
1950 temp = left.br_startoff;
1953 case BMAP_RIGHT_CONTIG:
1955 * New allocation is contiguous with a delayed allocation
1957 * Merge the new allocation with the right neighbor.
1959 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, XFS_DATA_FORK);
1960 temp = new->br_blockcount + right.br_blockcount;
1961 oldlen = startblockval(new->br_startblock) +
1962 startblockval(right.br_startblock);
1963 newlen = xfs_bmap_worst_indlen(ip, temp);
1964 xfs_bmbt_set_allf(ep, new->br_startoff,
1965 nullstartblock((int)newlen), temp, right.br_state);
1966 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, XFS_DATA_FORK);
1967 ip->i_df.if_lastex = idx;
1968 /* DELTA: One in-core extent grew into a hole. */
1970 temp = new->br_startoff;
1975 * New allocation is not contiguous with another
1976 * delayed allocation.
1977 * Insert a new entry.
1979 oldlen = newlen = 0;
1980 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL,
1982 xfs_iext_insert(ip, idx, 1, new, state);
1983 ip->i_df.if_lastex = idx;
1984 /* DELTA: A new in-core extent was added in a hole. */
1985 temp2 = new->br_blockcount;
1986 temp = new->br_startoff;
1989 if (oldlen != newlen) {
1990 ASSERT(oldlen > newlen);
1991 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
1992 (int64_t)(oldlen - newlen), rsvd);
1994 * Nothing to do for disk quota accounting here.
1999 if (delta->xed_startoff > temp)
2000 delta->xed_startoff = temp;
2001 if (delta->xed_blockcount < temp2)
2002 delta->xed_blockcount = temp2;
2009 * Called by xfs_bmap_add_extent to handle cases converting a hole
2010 * to a real allocation.
2012 STATIC int /* error */
2013 xfs_bmap_add_extent_hole_real(
2014 xfs_inode_t *ip, /* incore inode pointer */
2015 xfs_extnum_t idx, /* extent number to update/insert */
2016 xfs_btree_cur_t *cur, /* if null, not a btree */
2017 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2018 int *logflagsp, /* inode logging flags */
2019 xfs_extdelta_t *delta, /* Change made to incore extents */
2020 int whichfork) /* data or attr fork */
2022 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
2023 int error; /* error return value */
2024 int i; /* temp state */
2025 xfs_ifork_t *ifp; /* inode fork pointer */
2026 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2027 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2028 int rval=0; /* return value (logging flags) */
2029 int state; /* state bits, accessed thru macros */
2030 xfs_filblks_t temp=0;
2031 xfs_filblks_t temp2=0;
2033 ifp = XFS_IFORK_PTR(ip, whichfork);
2034 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
2035 ep = xfs_iext_get_ext(ifp, idx);
2038 if (whichfork == XFS_ATTR_FORK)
2039 state |= BMAP_ATTRFORK;
2042 * Check and set flags if this segment has a left neighbor.
2045 state |= BMAP_LEFT_VALID;
2046 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
2047 if (isnullstartblock(left.br_startblock))
2048 state |= BMAP_LEFT_DELAY;
2052 * Check and set flags if this segment has a current value.
2053 * Not true if we're inserting into the "hole" at eof.
2055 if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
2056 state |= BMAP_RIGHT_VALID;
2057 xfs_bmbt_get_all(ep, &right);
2058 if (isnullstartblock(right.br_startblock))
2059 state |= BMAP_RIGHT_DELAY;
2063 * We're inserting a real allocation between "left" and "right".
2064 * Set the contiguity flags. Don't let extents get too large.
2066 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2067 left.br_startoff + left.br_blockcount == new->br_startoff &&
2068 left.br_startblock + left.br_blockcount == new->br_startblock &&
2069 left.br_state == new->br_state &&
2070 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2071 state |= BMAP_LEFT_CONTIG;
2073 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2074 new->br_startoff + new->br_blockcount == right.br_startoff &&
2075 new->br_startblock + new->br_blockcount == right.br_startblock &&
2076 new->br_state == right.br_state &&
2077 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2078 (!(state & BMAP_LEFT_CONTIG) ||
2079 left.br_blockcount + new->br_blockcount +
2080 right.br_blockcount <= MAXEXTLEN))
2081 state |= BMAP_RIGHT_CONTIG;
2085 * Select which case we're in here, and implement it.
2087 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2088 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2090 * New allocation is contiguous with real allocations on the
2091 * left and on the right.
2092 * Merge all three into a single extent record.
2094 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
2096 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2097 left.br_blockcount + new->br_blockcount +
2098 right.br_blockcount);
2099 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
2101 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, whichfork);
2102 xfs_iext_remove(ip, idx, 1, state);
2103 ifp->if_lastex = idx - 1;
2104 XFS_IFORK_NEXT_SET(ip, whichfork,
2105 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2107 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2109 rval = XFS_ILOG_CORE;
2110 if ((error = xfs_bmbt_lookup_eq(cur,
2112 right.br_startblock,
2113 right.br_blockcount, &i)))
2115 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2116 if ((error = xfs_btree_delete(cur, &i)))
2118 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2119 if ((error = xfs_btree_decrement(cur, 0, &i)))
2121 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2122 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2124 left.br_blockcount +
2125 new->br_blockcount +
2126 right.br_blockcount,
2130 /* DELTA: Two in-core extents were replaced by one. */
2131 temp = left.br_startoff;
2132 temp2 = left.br_blockcount +
2133 new->br_blockcount +
2134 right.br_blockcount;
2137 case BMAP_LEFT_CONTIG:
2139 * New allocation is contiguous with a real allocation
2141 * Merge the new allocation with the left neighbor.
2143 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1, whichfork);
2144 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2145 left.br_blockcount + new->br_blockcount);
2146 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1, whichfork);
2147 ifp->if_lastex = idx - 1;
2149 rval = xfs_ilog_fext(whichfork);
2152 if ((error = xfs_bmbt_lookup_eq(cur,
2155 left.br_blockcount, &i)))
2157 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2158 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2160 left.br_blockcount +
2165 /* DELTA: One in-core extent grew. */
2166 temp = left.br_startoff;
2167 temp2 = left.br_blockcount +
2171 case BMAP_RIGHT_CONTIG:
2173 * New allocation is contiguous with a real allocation
2175 * Merge the new allocation with the right neighbor.
2177 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, whichfork);
2178 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2179 new->br_blockcount + right.br_blockcount,
2181 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, whichfork);
2182 ifp->if_lastex = idx;
2184 rval = xfs_ilog_fext(whichfork);
2187 if ((error = xfs_bmbt_lookup_eq(cur,
2189 right.br_startblock,
2190 right.br_blockcount, &i)))
2192 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2193 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2195 new->br_blockcount +
2196 right.br_blockcount,
2200 /* DELTA: One in-core extent grew. */
2201 temp = new->br_startoff;
2202 temp2 = new->br_blockcount +
2203 right.br_blockcount;
2208 * New allocation is not contiguous with another
2210 * Insert a new entry.
2212 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL, whichfork);
2213 xfs_iext_insert(ip, idx, 1, new, state);
2214 ifp->if_lastex = idx;
2215 XFS_IFORK_NEXT_SET(ip, whichfork,
2216 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2218 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2220 rval = XFS_ILOG_CORE;
2221 if ((error = xfs_bmbt_lookup_eq(cur,
2224 new->br_blockcount, &i)))
2226 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
2227 cur->bc_rec.b.br_state = new->br_state;
2228 if ((error = xfs_btree_insert(cur, &i)))
2230 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2232 /* DELTA: A new extent was added in a hole. */
2233 temp = new->br_startoff;
2234 temp2 = new->br_blockcount;
2239 if (delta->xed_startoff > temp)
2240 delta->xed_startoff = temp;
2241 if (delta->xed_blockcount < temp2)
2242 delta->xed_blockcount = temp2;
2250 * Adjust the size of the new extent based on di_extsize and rt extsize.
2253 xfs_bmap_extsize_align(
2255 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2256 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2257 xfs_extlen_t extsz, /* align to this extent size */
2258 int rt, /* is this a realtime inode? */
2259 int eof, /* is extent at end-of-file? */
2260 int delay, /* creating delalloc extent? */
2261 int convert, /* overwriting unwritten extent? */
2262 xfs_fileoff_t *offp, /* in/out: aligned offset */
2263 xfs_extlen_t *lenp) /* in/out: aligned length */
2265 xfs_fileoff_t orig_off; /* original offset */
2266 xfs_extlen_t orig_alen; /* original length */
2267 xfs_fileoff_t orig_end; /* original off+len */
2268 xfs_fileoff_t nexto; /* next file offset */
2269 xfs_fileoff_t prevo; /* previous file offset */
2270 xfs_fileoff_t align_off; /* temp for offset */
2271 xfs_extlen_t align_alen; /* temp for length */
2272 xfs_extlen_t temp; /* temp for calculations */
2277 orig_off = align_off = *offp;
2278 orig_alen = align_alen = *lenp;
2279 orig_end = orig_off + orig_alen;
2282 * If this request overlaps an existing extent, then don't
2283 * attempt to perform any additional alignment.
2285 if (!delay && !eof &&
2286 (orig_off >= gotp->br_startoff) &&
2287 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2292 * If the file offset is unaligned vs. the extent size
2293 * we need to align it. This will be possible unless
2294 * the file was previously written with a kernel that didn't
2295 * perform this alignment, or if a truncate shot us in the
2298 temp = do_mod(orig_off, extsz);
2304 * Same adjustment for the end of the requested area.
2306 if ((temp = (align_alen % extsz))) {
2307 align_alen += extsz - temp;
2310 * If the previous block overlaps with this proposed allocation
2311 * then move the start forward without adjusting the length.
2313 if (prevp->br_startoff != NULLFILEOFF) {
2314 if (prevp->br_startblock == HOLESTARTBLOCK)
2315 prevo = prevp->br_startoff;
2317 prevo = prevp->br_startoff + prevp->br_blockcount;
2320 if (align_off != orig_off && align_off < prevo)
2323 * If the next block overlaps with this proposed allocation
2324 * then move the start back without adjusting the length,
2325 * but not before offset 0.
2326 * This may of course make the start overlap previous block,
2327 * and if we hit the offset 0 limit then the next block
2328 * can still overlap too.
2330 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2331 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2332 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2333 nexto = gotp->br_startoff + gotp->br_blockcount;
2335 nexto = gotp->br_startoff;
2337 nexto = NULLFILEOFF;
2339 align_off + align_alen != orig_end &&
2340 align_off + align_alen > nexto)
2341 align_off = nexto > align_alen ? nexto - align_alen : 0;
2343 * If we're now overlapping the next or previous extent that
2344 * means we can't fit an extsz piece in this hole. Just move
2345 * the start forward to the first valid spot and set
2346 * the length so we hit the end.
2348 if (align_off != orig_off && align_off < prevo)
2350 if (align_off + align_alen != orig_end &&
2351 align_off + align_alen > nexto &&
2352 nexto != NULLFILEOFF) {
2353 ASSERT(nexto > prevo);
2354 align_alen = nexto - align_off;
2358 * If realtime, and the result isn't a multiple of the realtime
2359 * extent size we need to remove blocks until it is.
2361 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2363 * We're not covering the original request, or
2364 * we won't be able to once we fix the length.
2366 if (orig_off < align_off ||
2367 orig_end > align_off + align_alen ||
2368 align_alen - temp < orig_alen)
2369 return XFS_ERROR(EINVAL);
2371 * Try to fix it by moving the start up.
2373 if (align_off + temp <= orig_off) {
2378 * Try to fix it by moving the end in.
2380 else if (align_off + align_alen - temp >= orig_end)
2383 * Set the start to the minimum then trim the length.
2386 align_alen -= orig_off - align_off;
2387 align_off = orig_off;
2388 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2391 * Result doesn't cover the request, fail it.
2393 if (orig_off < align_off || orig_end > align_off + align_alen)
2394 return XFS_ERROR(EINVAL);
2396 ASSERT(orig_off >= align_off);
2397 ASSERT(orig_end <= align_off + align_alen);
2401 if (!eof && gotp->br_startoff != NULLFILEOFF)
2402 ASSERT(align_off + align_alen <= gotp->br_startoff);
2403 if (prevp->br_startoff != NULLFILEOFF)
2404 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2412 #define XFS_ALLOC_GAP_UNITS 4
2416 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2418 xfs_fsblock_t adjust; /* adjustment to block numbers */
2419 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2420 xfs_mount_t *mp; /* mount point structure */
2421 int nullfb; /* true if ap->firstblock isn't set */
2422 int rt; /* true if inode is realtime */
2424 #define ISVALID(x,y) \
2426 (x) < mp->m_sb.sb_rblocks : \
2427 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2428 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2429 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2431 mp = ap->ip->i_mount;
2432 nullfb = ap->firstblock == NULLFSBLOCK;
2433 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2434 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2436 * If allocating at eof, and there's a previous real block,
2437 * try to use its last block as our starting point.
2439 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2440 !isnullstartblock(ap->prevp->br_startblock) &&
2441 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2442 ap->prevp->br_startblock)) {
2443 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2445 * Adjust for the gap between prevp and us.
2448 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2450 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2454 * If not at eof, then compare the two neighbor blocks.
2455 * Figure out whether either one gives us a good starting point,
2456 * and pick the better one.
2458 else if (!ap->eof) {
2459 xfs_fsblock_t gotbno; /* right side block number */
2460 xfs_fsblock_t gotdiff=0; /* right side difference */
2461 xfs_fsblock_t prevbno; /* left side block number */
2462 xfs_fsblock_t prevdiff=0; /* left side difference */
2465 * If there's a previous (left) block, select a requested
2466 * start block based on it.
2468 if (ap->prevp->br_startoff != NULLFILEOFF &&
2469 !isnullstartblock(ap->prevp->br_startblock) &&
2470 (prevbno = ap->prevp->br_startblock +
2471 ap->prevp->br_blockcount) &&
2472 ISVALID(prevbno, ap->prevp->br_startblock)) {
2474 * Calculate gap to end of previous block.
2476 adjust = prevdiff = ap->off -
2477 (ap->prevp->br_startoff +
2478 ap->prevp->br_blockcount);
2480 * Figure the startblock based on the previous block's
2481 * end and the gap size.
2483 * If the gap is large relative to the piece we're
2484 * allocating, or using it gives us an invalid block
2485 * number, then just use the end of the previous block.
2487 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2488 ISVALID(prevbno + prevdiff,
2489 ap->prevp->br_startblock))
2494 * If the firstblock forbids it, can't use it,
2497 if (!rt && !nullfb &&
2498 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2499 prevbno = NULLFSBLOCK;
2502 * No previous block or can't follow it, just default.
2505 prevbno = NULLFSBLOCK;
2507 * If there's a following (right) block, select a requested
2508 * start block based on it.
2510 if (!isnullstartblock(ap->gotp->br_startblock)) {
2512 * Calculate gap to start of next block.
2514 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2516 * Figure the startblock based on the next block's
2517 * start and the gap size.
2519 gotbno = ap->gotp->br_startblock;
2522 * If the gap is large relative to the piece we're
2523 * allocating, or using it gives us an invalid block
2524 * number, then just use the start of the next block
2525 * offset by our length.
2527 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2528 ISVALID(gotbno - gotdiff, gotbno))
2530 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2532 gotdiff += adjust - ap->alen;
2536 * If the firstblock forbids it, can't use it,
2539 if (!rt && !nullfb &&
2540 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2541 gotbno = NULLFSBLOCK;
2544 * No next block, just default.
2547 gotbno = NULLFSBLOCK;
2549 * If both valid, pick the better one, else the only good
2550 * one, else ap->rval is already set (to 0 or the inode block).
2552 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2553 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2554 else if (prevbno != NULLFSBLOCK)
2556 else if (gotbno != NULLFSBLOCK)
2564 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2566 xfs_alloctype_t atype = 0; /* type for allocation routines */
2567 int error; /* error return value */
2568 xfs_mount_t *mp; /* mount point structure */
2569 xfs_extlen_t prod = 0; /* product factor for allocators */
2570 xfs_extlen_t ralen = 0; /* realtime allocation length */
2571 xfs_extlen_t align; /* minimum allocation alignment */
2574 mp = ap->ip->i_mount;
2575 align = xfs_get_extsz_hint(ap->ip);
2576 prod = align / mp->m_sb.sb_rextsize;
2577 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2578 align, 1, ap->eof, 0,
2579 ap->conv, &ap->off, &ap->alen);
2583 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2586 * If the offset & length are not perfectly aligned
2587 * then kill prod, it will just get us in trouble.
2589 if (do_mod(ap->off, align) || ap->alen % align)
2592 * Set ralen to be the actual requested length in rtextents.
2594 ralen = ap->alen / mp->m_sb.sb_rextsize;
2596 * If the old value was close enough to MAXEXTLEN that
2597 * we rounded up to it, cut it back so it's valid again.
2598 * Note that if it's a really large request (bigger than
2599 * MAXEXTLEN), we don't hear about that number, and can't
2600 * adjust the starting point to match it.
2602 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2603 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2605 * If it's an allocation to an empty file at offset 0,
2606 * pick an extent that will space things out in the rt area.
2608 if (ap->eof && ap->off == 0) {
2609 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2611 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2614 ap->rval = rtx * mp->m_sb.sb_rextsize;
2619 xfs_bmap_adjacent(ap);
2622 * Realtime allocation, done through xfs_rtallocate_extent.
2624 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2625 do_div(ap->rval, mp->m_sb.sb_rextsize);
2628 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2629 &ralen, atype, ap->wasdel, prod, &rtb)))
2631 if (rtb == NULLFSBLOCK && prod > 1 &&
2632 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2633 ap->alen, &ralen, atype,
2634 ap->wasdel, 1, &rtb)))
2637 if (ap->rval != NULLFSBLOCK) {
2638 ap->rval *= mp->m_sb.sb_rextsize;
2639 ralen *= mp->m_sb.sb_rextsize;
2641 ap->ip->i_d.di_nblocks += ralen;
2642 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2644 ap->ip->i_delayed_blks -= ralen;
2646 * Adjust the disk quota also. This was reserved
2649 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2650 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2651 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2660 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2662 xfs_mount_t *mp; /* mount point structure */
2663 xfs_alloctype_t atype = 0; /* type for allocation routines */
2664 xfs_extlen_t align; /* minimum allocation alignment */
2666 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2667 xfs_agnumber_t startag;
2668 xfs_alloc_arg_t args;
2670 xfs_extlen_t nextminlen = 0;
2672 int nullfb; /* true if ap->firstblock isn't set */
2678 mp = ap->ip->i_mount;
2679 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2680 if (unlikely(align)) {
2681 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2682 align, 0, ap->eof, 0, ap->conv,
2683 &ap->off, &ap->alen);
2687 nullfb = ap->firstblock == NULLFSBLOCK;
2688 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2690 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2691 ag = xfs_filestream_lookup_ag(ap->ip);
2692 ag = (ag != NULLAGNUMBER) ? ag : 0;
2693 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2695 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2698 ap->rval = ap->firstblock;
2700 xfs_bmap_adjacent(ap);
2703 * If allowed, use ap->rval; otherwise must use firstblock since
2704 * it's in the right allocation group.
2706 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2709 ap->rval = ap->firstblock;
2711 * Normal allocation, done through xfs_alloc_vextent.
2713 tryagain = isaligned = 0;
2716 args.fsbno = ap->rval;
2717 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2718 args.firstblock = ap->firstblock;
2721 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2722 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2724 args.type = XFS_ALLOCTYPE_START_BNO;
2725 args.total = ap->total;
2728 * Search for an allocation group with a single extent
2729 * large enough for the request.
2731 * If one isn't found, then adjust the minimum allocation
2732 * size to the largest space found.
2734 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2735 if (startag == NULLAGNUMBER)
2738 down_read(&mp->m_peraglock);
2739 while (blen < ap->alen) {
2740 pag = &mp->m_perag[ag];
2741 if (!pag->pagf_init &&
2742 (error = xfs_alloc_pagf_init(mp, args.tp,
2743 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2744 up_read(&mp->m_peraglock);
2748 * See xfs_alloc_fix_freelist...
2750 if (pag->pagf_init) {
2751 xfs_extlen_t longest;
2752 longest = xfs_alloc_longest_free_extent(mp, pag);
2758 if (xfs_inode_is_filestream(ap->ip)) {
2759 if (blen >= ap->alen)
2764 * If startag is an invalid AG, we've
2765 * come here once before and
2766 * xfs_filestream_new_ag picked the
2767 * best currently available.
2769 * Don't continue looping, since we
2770 * could loop forever.
2772 if (startag == NULLAGNUMBER)
2775 error = xfs_filestream_new_ag(ap, &ag);
2777 up_read(&mp->m_peraglock);
2781 /* loop again to set 'blen'*/
2782 startag = NULLAGNUMBER;
2786 if (++ag == mp->m_sb.sb_agcount)
2791 up_read(&mp->m_peraglock);
2793 * Since the above loop did a BUF_TRYLOCK, it is
2794 * possible that there is space for this request.
2796 if (notinit || blen < ap->minlen)
2797 args.minlen = ap->minlen;
2799 * If the best seen length is less than the request
2800 * length, use the best as the minimum.
2802 else if (blen < ap->alen)
2805 * Otherwise we've seen an extent as big as alen,
2806 * use that as the minimum.
2809 args.minlen = ap->alen;
2812 * set the failure fallback case to look in the selected
2813 * AG as the stream may have moved.
2815 if (xfs_inode_is_filestream(ap->ip))
2816 ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2817 } else if (ap->low) {
2818 if (xfs_inode_is_filestream(ap->ip))
2819 args.type = XFS_ALLOCTYPE_FIRST_AG;
2821 args.type = XFS_ALLOCTYPE_START_BNO;
2822 args.total = args.minlen = ap->minlen;
2824 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2825 args.total = ap->total;
2826 args.minlen = ap->minlen;
2828 /* apply extent size hints if obtained earlier */
2829 if (unlikely(align)) {
2831 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2832 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2833 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
2837 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
2838 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2839 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2842 * If we are not low on available data blocks, and the
2843 * underlying logical volume manager is a stripe, and
2844 * the file offset is zero then try to allocate data
2845 * blocks on stripe unit boundary.
2846 * NOTE: ap->aeof is only set if the allocation length
2847 * is >= the stripe unit and the allocation offset is
2848 * at the end of file.
2850 if (!ap->low && ap->aeof) {
2852 args.alignment = mp->m_dalign;
2856 * Adjust for alignment
2858 if (blen > args.alignment && blen <= ap->alen)
2859 args.minlen = blen - args.alignment;
2860 args.minalignslop = 0;
2863 * First try an exact bno allocation.
2864 * If it fails then do a near or start bno
2865 * allocation with alignment turned on.
2869 args.type = XFS_ALLOCTYPE_THIS_BNO;
2872 * Compute the minlen+alignment for the
2873 * next case. Set slop so that the value
2874 * of minlen+alignment+slop doesn't go up
2875 * between the calls.
2877 if (blen > mp->m_dalign && blen <= ap->alen)
2878 nextminlen = blen - mp->m_dalign;
2880 nextminlen = args.minlen;
2881 if (nextminlen + mp->m_dalign > args.minlen + 1)
2883 nextminlen + mp->m_dalign -
2886 args.minalignslop = 0;
2890 args.minalignslop = 0;
2892 args.minleft = ap->minleft;
2893 args.wasdel = ap->wasdel;
2895 args.userdata = ap->userdata;
2896 if ((error = xfs_alloc_vextent(&args)))
2898 if (tryagain && args.fsbno == NULLFSBLOCK) {
2900 * Exact allocation failed. Now try with alignment
2904 args.fsbno = ap->rval;
2905 args.alignment = mp->m_dalign;
2906 args.minlen = nextminlen;
2907 args.minalignslop = 0;
2909 if ((error = xfs_alloc_vextent(&args)))
2912 if (isaligned && args.fsbno == NULLFSBLOCK) {
2914 * allocation failed, so turn off alignment and
2918 args.fsbno = ap->rval;
2920 if ((error = xfs_alloc_vextent(&args)))
2923 if (args.fsbno == NULLFSBLOCK && nullfb &&
2924 args.minlen > ap->minlen) {
2925 args.minlen = ap->minlen;
2926 args.type = XFS_ALLOCTYPE_START_BNO;
2927 args.fsbno = ap->rval;
2928 if ((error = xfs_alloc_vextent(&args)))
2931 if (args.fsbno == NULLFSBLOCK && nullfb) {
2933 args.type = XFS_ALLOCTYPE_FIRST_AG;
2934 args.total = ap->minlen;
2936 if ((error = xfs_alloc_vextent(&args)))
2940 if (args.fsbno != NULLFSBLOCK) {
2941 ap->firstblock = ap->rval = args.fsbno;
2942 ASSERT(nullfb || fb_agno == args.agno ||
2943 (ap->low && fb_agno < args.agno));
2944 ap->alen = args.len;
2945 ap->ip->i_d.di_nblocks += args.len;
2946 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2948 ap->ip->i_delayed_blks -= args.len;
2950 * Adjust the disk quota also. This was reserved
2953 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2954 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2955 XFS_TRANS_DQ_BCOUNT,
2958 ap->rval = NULLFSBLOCK;
2965 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2966 * It figures out where to ask the underlying allocator to put the new extent.
2970 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2972 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
2973 return xfs_bmap_rtalloc(ap);
2974 return xfs_bmap_btalloc(ap);
2978 * Transform a btree format file with only one leaf node, where the
2979 * extents list will fit in the inode, into an extents format file.
2980 * Since the file extents are already in-core, all we have to do is
2981 * give up the space for the btree root and pitch the leaf block.
2983 STATIC int /* error */
2984 xfs_bmap_btree_to_extents(
2985 xfs_trans_t *tp, /* transaction pointer */
2986 xfs_inode_t *ip, /* incore inode pointer */
2987 xfs_btree_cur_t *cur, /* btree cursor */
2988 int *logflagsp, /* inode logging flags */
2989 int whichfork) /* data or attr fork */
2992 struct xfs_btree_block *cblock;/* child btree block */
2993 xfs_fsblock_t cbno; /* child block number */
2994 xfs_buf_t *cbp; /* child block's buffer */
2995 int error; /* error return value */
2996 xfs_ifork_t *ifp; /* inode fork data */
2997 xfs_mount_t *mp; /* mount point structure */
2998 __be64 *pp; /* ptr to block address */
2999 struct xfs_btree_block *rblock;/* root btree block */
3002 ifp = XFS_IFORK_PTR(ip, whichfork);
3003 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3004 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
3005 rblock = ifp->if_broot;
3006 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
3007 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
3008 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
3009 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
3010 cbno = be64_to_cpu(*pp);
3013 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
3016 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
3017 XFS_BMAP_BTREE_REF)))
3019 cblock = XFS_BUF_TO_BLOCK(cbp);
3020 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
3022 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
3023 ip->i_d.di_nblocks--;
3024 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
3025 xfs_trans_binval(tp, cbp);
3026 if (cur->bc_bufs[0] == cbp)
3027 cur->bc_bufs[0] = NULL;
3028 xfs_iroot_realloc(ip, -1, whichfork);
3029 ASSERT(ifp->if_broot == NULL);
3030 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
3031 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3032 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
3037 * Called by xfs_bmapi to update file extent records and the btree
3038 * after removing space (or undoing a delayed allocation).
3040 STATIC int /* error */
3041 xfs_bmap_del_extent(
3042 xfs_inode_t *ip, /* incore inode pointer */
3043 xfs_trans_t *tp, /* current transaction pointer */
3044 xfs_extnum_t idx, /* extent number to update/delete */
3045 xfs_bmap_free_t *flist, /* list of extents to be freed */
3046 xfs_btree_cur_t *cur, /* if null, not a btree */
3047 xfs_bmbt_irec_t *del, /* data to remove from extents */
3048 int *logflagsp, /* inode logging flags */
3049 xfs_extdelta_t *delta, /* Change made to incore extents */
3050 int whichfork, /* data or attr fork */
3051 int rsvd) /* OK to allocate reserved blocks */
3053 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
3054 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
3055 xfs_fsblock_t del_endblock=0; /* first block past del */
3056 xfs_fileoff_t del_endoff; /* first offset past del */
3057 int delay; /* current block is delayed allocated */
3058 int do_fx; /* free extent at end of routine */
3059 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
3060 int error; /* error return value */
3061 int flags; /* inode logging flags */
3062 xfs_bmbt_irec_t got; /* current extent entry */
3063 xfs_fileoff_t got_endoff; /* first offset past got */
3064 int i; /* temp state */
3065 xfs_ifork_t *ifp; /* inode fork pointer */
3066 xfs_mount_t *mp; /* mount structure */
3067 xfs_filblks_t nblks; /* quota/sb block count */
3068 xfs_bmbt_irec_t new; /* new record to be inserted */
3070 uint qfield; /* quota field to update */
3071 xfs_filblks_t temp; /* for indirect length calculations */
3072 xfs_filblks_t temp2; /* for indirect length calculations */
3074 XFS_STATS_INC(xs_del_exlist);
3076 ifp = XFS_IFORK_PTR(ip, whichfork);
3077 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
3078 (uint)sizeof(xfs_bmbt_rec_t)));
3079 ASSERT(del->br_blockcount > 0);
3080 ep = xfs_iext_get_ext(ifp, idx);
3081 xfs_bmbt_get_all(ep, &got);
3082 ASSERT(got.br_startoff <= del->br_startoff);
3083 del_endoff = del->br_startoff + del->br_blockcount;
3084 got_endoff = got.br_startoff + got.br_blockcount;
3085 ASSERT(got_endoff >= del_endoff);
3086 delay = isnullstartblock(got.br_startblock);
3087 ASSERT(isnullstartblock(del->br_startblock) == delay);
3092 * If deleting a real allocation, must free up the disk space.
3095 flags = XFS_ILOG_CORE;
3097 * Realtime allocation. Free it and record di_nblocks update.
3099 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
3103 ASSERT(do_mod(del->br_blockcount,
3104 mp->m_sb.sb_rextsize) == 0);
3105 ASSERT(do_mod(del->br_startblock,
3106 mp->m_sb.sb_rextsize) == 0);
3107 bno = del->br_startblock;
3108 len = del->br_blockcount;
3109 do_div(bno, mp->m_sb.sb_rextsize);
3110 do_div(len, mp->m_sb.sb_rextsize);
3111 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3112 (xfs_extlen_t)len)))
3115 nblks = len * mp->m_sb.sb_rextsize;
3116 qfield = XFS_TRANS_DQ_RTBCOUNT;
3119 * Ordinary allocation.
3123 nblks = del->br_blockcount;
3124 qfield = XFS_TRANS_DQ_BCOUNT;
3127 * Set up del_endblock and cur for later.
3129 del_endblock = del->br_startblock + del->br_blockcount;
3131 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3132 got.br_startblock, got.br_blockcount,
3135 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3137 da_old = da_new = 0;
3139 da_old = startblockval(got.br_startblock);
3145 * Set flag value to use in switch statement.
3146 * Left-contig is 2, right-contig is 1.
3148 switch (((got.br_startoff == del->br_startoff) << 1) |
3149 (got_endoff == del_endoff)) {
3152 * Matches the whole extent. Delete the entry.
3154 XFS_BMAP_TRACE_DELETE("3", ip, idx, 1, whichfork);
3155 xfs_iext_remove(ip, idx, 1,
3156 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
3157 ifp->if_lastex = idx;
3160 XFS_IFORK_NEXT_SET(ip, whichfork,
3161 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3162 flags |= XFS_ILOG_CORE;
3164 flags |= xfs_ilog_fext(whichfork);
3167 if ((error = xfs_btree_delete(cur, &i)))
3169 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3174 * Deleting the first part of the extent.
3176 XFS_BMAP_TRACE_PRE_UPDATE("2", ip, idx, whichfork);
3177 xfs_bmbt_set_startoff(ep, del_endoff);
3178 temp = got.br_blockcount - del->br_blockcount;
3179 xfs_bmbt_set_blockcount(ep, temp);
3180 ifp->if_lastex = idx;
3182 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3184 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
3185 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx,
3190 xfs_bmbt_set_startblock(ep, del_endblock);
3191 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx, whichfork);
3193 flags |= xfs_ilog_fext(whichfork);
3196 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3197 got.br_blockcount - del->br_blockcount,
3204 * Deleting the last part of the extent.
3206 temp = got.br_blockcount - del->br_blockcount;
3207 XFS_BMAP_TRACE_PRE_UPDATE("1", ip, idx, whichfork);
3208 xfs_bmbt_set_blockcount(ep, temp);
3209 ifp->if_lastex = idx;
3211 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3213 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
3214 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx,
3219 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx, whichfork);
3221 flags |= xfs_ilog_fext(whichfork);
3224 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3226 got.br_blockcount - del->br_blockcount,
3233 * Deleting the middle of the extent.
3235 temp = del->br_startoff - got.br_startoff;
3236 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, whichfork);
3237 xfs_bmbt_set_blockcount(ep, temp);
3238 new.br_startoff = del_endoff;
3239 temp2 = got_endoff - del_endoff;
3240 new.br_blockcount = temp2;
3241 new.br_state = got.br_state;
3243 new.br_startblock = del_endblock;
3244 flags |= XFS_ILOG_CORE;
3246 if ((error = xfs_bmbt_update(cur,
3248 got.br_startblock, temp,
3251 if ((error = xfs_btree_increment(cur, 0, &i)))
3253 cur->bc_rec.b = new;
3254 error = xfs_btree_insert(cur, &i);
3255 if (error && error != ENOSPC)
3258 * If get no-space back from btree insert,
3259 * it tried a split, and we have a zero
3260 * block reservation.
3261 * Fix up our state and return the error.
3263 if (error == ENOSPC) {
3265 * Reset the cursor, don't trust
3266 * it after any insert operation.
3268 if ((error = xfs_bmbt_lookup_eq(cur,
3273 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3275 * Update the btree record back
3276 * to the original value.
3278 if ((error = xfs_bmbt_update(cur,
3285 * Reset the extent record back
3286 * to the original value.
3288 xfs_bmbt_set_blockcount(ep,
3291 error = XFS_ERROR(ENOSPC);
3294 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3296 flags |= xfs_ilog_fext(whichfork);
3297 XFS_IFORK_NEXT_SET(ip, whichfork,
3298 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3300 ASSERT(whichfork == XFS_DATA_FORK);
3301 temp = xfs_bmap_worst_indlen(ip, temp);
3302 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
3303 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3304 new.br_startblock = nullstartblock((int)temp2);
3305 da_new = temp + temp2;
3306 while (da_new > da_old) {
3310 xfs_bmbt_set_startblock(ep,
3311 nullstartblock((int)temp));
3313 if (da_new == da_old)
3319 nullstartblock((int)temp2);
3323 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, whichfork);
3324 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 1, &new, NULL,
3326 xfs_iext_insert(ip, idx + 1, 1, &new,
3327 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
3328 ifp->if_lastex = idx + 1;
3332 * If we need to, add to list of extents to delete.
3335 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3338 * Adjust inode # blocks in the file.
3341 ip->i_d.di_nblocks -= nblks;
3343 * Adjust quota data.
3346 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
3349 * Account for change in delayed indirect blocks.
3350 * Nothing to do for disk quota accounting here.
3352 ASSERT(da_old >= da_new);
3353 if (da_old > da_new)
3354 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
3357 /* DELTA: report the original extent. */
3358 if (delta->xed_startoff > got.br_startoff)
3359 delta->xed_startoff = got.br_startoff;
3360 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3361 delta->xed_blockcount = got.br_startoff +
3370 * Remove the entry "free" from the free item list. Prev points to the
3371 * previous entry, unless "free" is the head of the list.
3375 xfs_bmap_free_t *flist, /* free item list header */
3376 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3377 xfs_bmap_free_item_t *free) /* list item to be freed */
3380 prev->xbfi_next = free->xbfi_next;
3382 flist->xbf_first = free->xbfi_next;
3384 kmem_zone_free(xfs_bmap_free_item_zone, free);
3388 * Convert an extents-format file into a btree-format file.
3389 * The new file will have a root block (in the inode) and a single child block.
3391 STATIC int /* error */
3392 xfs_bmap_extents_to_btree(
3393 xfs_trans_t *tp, /* transaction pointer */
3394 xfs_inode_t *ip, /* incore inode pointer */
3395 xfs_fsblock_t *firstblock, /* first-block-allocated */
3396 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3397 xfs_btree_cur_t **curp, /* cursor returned to caller */
3398 int wasdel, /* converting a delayed alloc */
3399 int *logflagsp, /* inode logging flags */
3400 int whichfork) /* data or attr fork */
3402 struct xfs_btree_block *ablock; /* allocated (child) bt block */
3403 xfs_buf_t *abp; /* buffer for ablock */
3404 xfs_alloc_arg_t args; /* allocation arguments */
3405 xfs_bmbt_rec_t *arp; /* child record pointer */
3406 struct xfs_btree_block *block; /* btree root block */
3407 xfs_btree_cur_t *cur; /* bmap btree cursor */
3408 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3409 int error; /* error return value */
3410 xfs_extnum_t i, cnt; /* extent record index */
3411 xfs_ifork_t *ifp; /* inode fork pointer */
3412 xfs_bmbt_key_t *kp; /* root block key pointer */
3413 xfs_mount_t *mp; /* mount structure */
3414 xfs_extnum_t nextents; /* number of file extents */
3415 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3417 ifp = XFS_IFORK_PTR(ip, whichfork);
3418 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3419 ASSERT(ifp->if_ext_max ==
3420 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3422 * Make space in the inode incore.
3424 xfs_iroot_realloc(ip, 1, whichfork);
3425 ifp->if_flags |= XFS_IFBROOT;
3430 block = ifp->if_broot;
3431 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3432 block->bb_level = cpu_to_be16(1);
3433 block->bb_numrecs = cpu_to_be16(1);
3434 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3435 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3438 * Need a cursor. Can't allocate until bb_level is filled in.
3441 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
3442 cur->bc_private.b.firstblock = *firstblock;
3443 cur->bc_private.b.flist = flist;
3444 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3446 * Convert to a btree with two levels, one record in root.
3448 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3451 args.firstblock = *firstblock;
3452 if (*firstblock == NULLFSBLOCK) {
3453 args.type = XFS_ALLOCTYPE_START_BNO;
3454 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3455 } else if (flist->xbf_low) {
3456 args.type = XFS_ALLOCTYPE_START_BNO;
3457 args.fsbno = *firstblock;
3459 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3460 args.fsbno = *firstblock;
3462 args.minlen = args.maxlen = args.prod = 1;
3463 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3464 args.minalignslop = 0;
3465 args.wasdel = wasdel;
3467 if ((error = xfs_alloc_vextent(&args))) {
3468 xfs_iroot_realloc(ip, -1, whichfork);
3469 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3473 * Allocation can't fail, the space was reserved.
3475 ASSERT(args.fsbno != NULLFSBLOCK);
3476 ASSERT(*firstblock == NULLFSBLOCK ||
3477 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3479 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3480 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3481 cur->bc_private.b.allocated++;
3482 ip->i_d.di_nblocks++;
3483 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3484 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3486 * Fill in the child block.
3488 ablock = XFS_BUF_TO_BLOCK(abp);
3489 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3490 ablock->bb_level = 0;
3491 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3492 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3493 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3494 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3495 for (cnt = i = 0; i < nextents; i++) {
3496 ep = xfs_iext_get_ext(ifp, i);
3497 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
3498 arp->l0 = cpu_to_be64(ep->l0);
3499 arp->l1 = cpu_to_be64(ep->l1);
3503 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3504 xfs_btree_set_numrecs(ablock, cnt);
3507 * Fill in the root key and pointer.
3509 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3510 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3511 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3512 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3513 be16_to_cpu(block->bb_level)));
3514 *pp = cpu_to_be64(args.fsbno);
3517 * Do all this logging at the end so that
3518 * the root is at the right level.
3520 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3521 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3522 ASSERT(*curp == NULL);
3524 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
3529 * Calculate the default attribute fork offset for newly created inodes.
3532 xfs_default_attroffset(
3533 struct xfs_inode *ip)
3535 struct xfs_mount *mp = ip->i_mount;
3538 if (mp->m_sb.sb_inodesize == 256) {
3539 offset = XFS_LITINO(mp) -
3540 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3542 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3545 ASSERT(offset < XFS_LITINO(mp));
3550 * Helper routine to reset inode di_forkoff field when switching
3551 * attribute fork from local to extent format - we reset it where
3552 * possible to make space available for inline data fork extents.
3555 xfs_bmap_forkoff_reset(
3560 if (whichfork == XFS_ATTR_FORK &&
3561 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3562 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3563 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3564 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3566 if (dfl_forkoff > ip->i_d.di_forkoff) {
3567 ip->i_d.di_forkoff = dfl_forkoff;
3568 ip->i_df.if_ext_max =
3569 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3570 ip->i_afp->if_ext_max =
3571 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3577 * Convert a local file to an extents file.
3578 * This code is out of bounds for data forks of regular files,
3579 * since the file data needs to get logged so things will stay consistent.
3580 * (The bmap-level manipulations are ok, though).
3582 STATIC int /* error */
3583 xfs_bmap_local_to_extents(
3584 xfs_trans_t *tp, /* transaction pointer */
3585 xfs_inode_t *ip, /* incore inode pointer */
3586 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3587 xfs_extlen_t total, /* total blocks needed by transaction */
3588 int *logflagsp, /* inode logging flags */
3589 int whichfork) /* data or attr fork */
3591 int error; /* error return value */
3592 int flags; /* logging flags returned */
3593 xfs_ifork_t *ifp; /* inode fork pointer */
3596 * We don't want to deal with the case of keeping inode data inline yet.
3597 * So sending the data fork of a regular inode is invalid.
3599 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3600 whichfork == XFS_DATA_FORK));
3601 ifp = XFS_IFORK_PTR(ip, whichfork);
3602 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3605 if (ifp->if_bytes) {
3606 xfs_alloc_arg_t args; /* allocation arguments */
3607 xfs_buf_t *bp; /* buffer for extent block */
3608 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
3611 args.mp = ip->i_mount;
3612 args.firstblock = *firstblock;
3613 ASSERT((ifp->if_flags &
3614 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3616 * Allocate a block. We know we need only one, since the
3617 * file currently fits in an inode.
3619 if (*firstblock == NULLFSBLOCK) {
3620 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3621 args.type = XFS_ALLOCTYPE_START_BNO;
3623 args.fsbno = *firstblock;
3624 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3627 args.mod = args.minleft = args.alignment = args.wasdel =
3628 args.isfl = args.minalignslop = 0;
3629 args.minlen = args.maxlen = args.prod = 1;
3630 if ((error = xfs_alloc_vextent(&args)))
3633 * Can't fail, the space was reserved.
3635 ASSERT(args.fsbno != NULLFSBLOCK);
3636 ASSERT(args.len == 1);
3637 *firstblock = args.fsbno;
3638 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3639 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3641 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3642 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3643 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3644 xfs_iext_add(ifp, 0, 1);
3645 ep = xfs_iext_get_ext(ifp, 0);
3646 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3647 XFS_BMAP_TRACE_POST_UPDATE("new", ip, 0, whichfork);
3648 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3649 ip->i_d.di_nblocks = 1;
3650 xfs_trans_mod_dquot_byino(tp, ip,
3651 XFS_TRANS_DQ_BCOUNT, 1L);
3652 flags |= xfs_ilog_fext(whichfork);
3654 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3655 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3657 ifp->if_flags &= ~XFS_IFINLINE;
3658 ifp->if_flags |= XFS_IFEXTENTS;
3659 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3660 flags |= XFS_ILOG_CORE;
3667 * Search the extent records for the entry containing block bno.
3668 * If bno lies in a hole, point to the next entry. If bno lies
3669 * past eof, *eofp will be set, and *prevp will contain the last
3670 * entry (null if none). Else, *lastxp will be set to the index
3671 * of the found entry; *gotp will contain the entry.
3673 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3674 xfs_bmap_search_multi_extents(
3675 xfs_ifork_t *ifp, /* inode fork pointer */
3676 xfs_fileoff_t bno, /* block number searched for */
3677 int *eofp, /* out: end of file found */
3678 xfs_extnum_t *lastxp, /* out: last extent index */
3679 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3680 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3682 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3683 xfs_extnum_t lastx; /* last extent index */
3686 * Initialize the extent entry structure to catch access to
3687 * uninitialized br_startblock field.
3689 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3690 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3691 gotp->br_state = XFS_EXT_INVALID;
3693 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3695 gotp->br_startblock = 0xffffa5a5;
3697 prevp->br_startoff = NULLFILEOFF;
3699 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3701 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3703 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3704 xfs_bmbt_get_all(ep, gotp);
3718 * Search the extents list for the inode, for the extent containing bno.
3719 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3720 * *eofp will be set, and *prevp will contain the last entry (null if none).
3721 * Else, *lastxp will be set to the index of the found
3722 * entry; *gotp will contain the entry.
3724 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3725 xfs_bmap_search_extents(
3726 xfs_inode_t *ip, /* incore inode pointer */
3727 xfs_fileoff_t bno, /* block number searched for */
3728 int fork, /* data or attr fork */
3729 int *eofp, /* out: end of file found */
3730 xfs_extnum_t *lastxp, /* out: last extent index */
3731 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3732 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3734 xfs_ifork_t *ifp; /* inode fork pointer */
3735 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3737 XFS_STATS_INC(xs_look_exlist);
3738 ifp = XFS_IFORK_PTR(ip, fork);
3740 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3742 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3743 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3744 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3745 "Access to block zero in inode %llu "
3746 "start_block: %llx start_off: %llx "
3747 "blkcnt: %llx extent-state: %x lastx: %x\n",
3748 (unsigned long long)ip->i_ino,
3749 (unsigned long long)gotp->br_startblock,
3750 (unsigned long long)gotp->br_startoff,
3751 (unsigned long long)gotp->br_blockcount,
3752 gotp->br_state, *lastxp);
3753 *lastxp = NULLEXTNUM;
3761 #ifdef XFS_BMAP_TRACE
3762 ktrace_t *xfs_bmap_trace_buf;
3765 * Add a bmap trace buffer entry. Base routine for the others.
3768 xfs_bmap_trace_addentry(
3769 int opcode, /* operation */
3770 const char *fname, /* function name */
3771 char *desc, /* operation description */
3772 xfs_inode_t *ip, /* incore inode pointer */
3773 xfs_extnum_t idx, /* index of entry(ies) */
3774 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
3775 xfs_bmbt_rec_host_t *r1, /* first record */
3776 xfs_bmbt_rec_host_t *r2, /* second record or null */
3777 int whichfork) /* data or attr fork */
3779 xfs_bmbt_rec_host_t tr2;
3781 ASSERT(cnt == 1 || cnt == 2);
3786 memset(&tr2, 0, sizeof(tr2));
3789 ktrace_enter(xfs_bmap_trace_buf,
3790 (void *)(__psint_t)(opcode | (whichfork << 16)),
3791 (void *)fname, (void *)desc, (void *)ip,
3792 (void *)(__psint_t)idx,
3793 (void *)(__psint_t)cnt,
3794 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3795 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3796 (void *)(__psunsigned_t)(r1->l0 >> 32),
3797 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3798 (void *)(__psunsigned_t)(r1->l1 >> 32),
3799 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3800 (void *)(__psunsigned_t)(r2->l0 >> 32),
3801 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3802 (void *)(__psunsigned_t)(r2->l1 >> 32),
3803 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3805 ASSERT(ip->i_xtrace);
3806 ktrace_enter(ip->i_xtrace,
3807 (void *)(__psint_t)(opcode | (whichfork << 16)),
3808 (void *)fname, (void *)desc, (void *)ip,
3809 (void *)(__psint_t)idx,
3810 (void *)(__psint_t)cnt,
3811 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3812 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3813 (void *)(__psunsigned_t)(r1->l0 >> 32),
3814 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3815 (void *)(__psunsigned_t)(r1->l1 >> 32),
3816 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3817 (void *)(__psunsigned_t)(r2->l0 >> 32),
3818 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3819 (void *)(__psunsigned_t)(r2->l1 >> 32),
3820 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3825 * Add bmap trace entry prior to a call to xfs_iext_remove.
3828 xfs_bmap_trace_delete(
3829 const char *fname, /* function name */
3830 char *desc, /* operation description */
3831 xfs_inode_t *ip, /* incore inode pointer */
3832 xfs_extnum_t idx, /* index of entry(entries) deleted */
3833 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3834 int whichfork) /* data or attr fork */
3836 xfs_ifork_t *ifp; /* inode fork pointer */
3838 ifp = XFS_IFORK_PTR(ip, whichfork);
3839 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
3840 cnt, xfs_iext_get_ext(ifp, idx),
3841 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
3846 * Add bmap trace entry prior to a call to xfs_iext_insert, or
3847 * reading in the extents list from the disk (in the btree).
3850 xfs_bmap_trace_insert(
3851 const char *fname, /* function name */
3852 char *desc, /* operation description */
3853 xfs_inode_t *ip, /* incore inode pointer */
3854 xfs_extnum_t idx, /* index of entry(entries) inserted */
3855 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3856 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3857 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3858 int whichfork) /* data or attr fork */
3860 xfs_bmbt_rec_host_t tr1; /* compressed record 1 */
3861 xfs_bmbt_rec_host_t tr2; /* compressed record 2 if needed */
3863 xfs_bmbt_set_all(&tr1, r1);
3866 xfs_bmbt_set_all(&tr2, r2);
3871 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3872 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3876 * Add bmap trace entry after updating an extent record in place.
3879 xfs_bmap_trace_post_update(
3880 const char *fname, /* function name */
3881 char *desc, /* operation description */
3882 xfs_inode_t *ip, /* incore inode pointer */
3883 xfs_extnum_t idx, /* index of entry updated */
3884 int whichfork) /* data or attr fork */
3886 xfs_ifork_t *ifp; /* inode fork pointer */
3888 ifp = XFS_IFORK_PTR(ip, whichfork);
3889 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
3890 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3894 * Add bmap trace entry prior to updating an extent record in place.
3897 xfs_bmap_trace_pre_update(
3898 const char *fname, /* function name */
3899 char *desc, /* operation description */
3900 xfs_inode_t *ip, /* incore inode pointer */
3901 xfs_extnum_t idx, /* index of entry to be updated */
3902 int whichfork) /* data or attr fork */
3904 xfs_ifork_t *ifp; /* inode fork pointer */
3906 ifp = XFS_IFORK_PTR(ip, whichfork);
3907 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
3908 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3910 #endif /* XFS_BMAP_TRACE */
3913 * Compute the worst-case number of indirect blocks that will be used
3914 * for ip's delayed extent of length "len".
3916 STATIC xfs_filblks_t
3917 xfs_bmap_worst_indlen(
3918 xfs_inode_t *ip, /* incore inode pointer */
3919 xfs_filblks_t len) /* delayed extent length */
3921 int level; /* btree level number */
3922 int maxrecs; /* maximum record count at this level */
3923 xfs_mount_t *mp; /* mount structure */
3924 xfs_filblks_t rval; /* return value */
3927 maxrecs = mp->m_bmap_dmxr[0];
3928 for (level = 0, rval = 0;
3929 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3932 do_div(len, maxrecs);
3935 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3938 maxrecs = mp->m_bmap_dmxr[1];
3943 #if defined(XFS_RW_TRACE)
3952 if (ip->i_rwtrace == NULL)
3954 ktrace_enter(ip->i_rwtrace,
3955 (void *)(__psint_t)XFS_BUNMAP,
3957 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3958 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3959 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3960 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3961 (void *)(__psint_t)len,
3962 (void *)(__psint_t)flags,
3963 (void *)(unsigned long)current_cpu(),
3975 * Convert inode from non-attributed to attributed.
3976 * Must not be in a transaction, ip must not be locked.
3978 int /* error code */
3979 xfs_bmap_add_attrfork(
3980 xfs_inode_t *ip, /* incore inode pointer */
3981 int size, /* space new attribute needs */
3982 int rsvd) /* xact may use reserved blks */
3984 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3985 xfs_bmap_free_t flist; /* freed extent records */
3986 xfs_mount_t *mp; /* mount structure */
3987 xfs_trans_t *tp; /* transaction pointer */
3988 int blks; /* space reservation */
3989 int version = 1; /* superblock attr version */
3990 int committed; /* xaction was committed */
3991 int logflags; /* logging flags */
3992 int error; /* error return value */
3994 ASSERT(XFS_IFORK_Q(ip) == 0);
3995 ASSERT(ip->i_df.if_ext_max ==
3996 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3999 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
4000 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
4001 blks = XFS_ADDAFORK_SPACE_RES(mp);
4003 tp->t_flags |= XFS_TRANS_RESERVE;
4004 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
4005 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
4007 xfs_ilock(ip, XFS_ILOCK_EXCL);
4008 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
4009 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
4010 XFS_QMOPT_RES_REGBLKS);
4012 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4013 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
4016 if (XFS_IFORK_Q(ip))
4018 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
4020 * For inodes coming from pre-6.2 filesystems.
4022 ASSERT(ip->i_d.di_aformat == 0);
4023 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
4025 ASSERT(ip->i_d.di_anextents == 0);
4027 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4028 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4029 switch (ip->i_d.di_format) {
4030 case XFS_DINODE_FMT_DEV:
4031 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
4033 case XFS_DINODE_FMT_UUID:
4034 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
4036 case XFS_DINODE_FMT_LOCAL:
4037 case XFS_DINODE_FMT_EXTENTS:
4038 case XFS_DINODE_FMT_BTREE:
4039 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
4040 if (!ip->i_d.di_forkoff)
4041 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
4042 else if (mp->m_flags & XFS_MOUNT_ATTR2)
4047 error = XFS_ERROR(EINVAL);
4050 ip->i_df.if_ext_max =
4051 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4052 ASSERT(ip->i_afp == NULL);
4053 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
4054 ip->i_afp->if_ext_max =
4055 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4056 ip->i_afp->if_flags = XFS_IFEXTENTS;
4058 xfs_bmap_init(&flist, &firstblock);
4059 switch (ip->i_d.di_format) {
4060 case XFS_DINODE_FMT_LOCAL:
4061 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
4064 case XFS_DINODE_FMT_EXTENTS:
4065 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
4068 case XFS_DINODE_FMT_BTREE:
4069 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
4077 xfs_trans_log_inode(tp, ip, logflags);
4080 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
4081 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
4082 __int64_t sbfields = 0;
4084 spin_lock(&mp->m_sb_lock);
4085 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
4086 xfs_sb_version_addattr(&mp->m_sb);
4087 sbfields |= XFS_SB_VERSIONNUM;
4089 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
4090 xfs_sb_version_addattr2(&mp->m_sb);
4091 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
4094 spin_unlock(&mp->m_sb_lock);
4095 xfs_mod_sb(tp, sbfields);
4097 spin_unlock(&mp->m_sb_lock);
4099 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
4101 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
4102 ASSERT(ip->i_df.if_ext_max ==
4103 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4106 xfs_bmap_cancel(&flist);
4108 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4110 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4111 ASSERT(ip->i_df.if_ext_max ==
4112 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4117 * Add the extent to the list of extents to be free at transaction end.
4118 * The list is maintained sorted (by block number).
4123 xfs_fsblock_t bno, /* fs block number of extent */
4124 xfs_filblks_t len, /* length of extent */
4125 xfs_bmap_free_t *flist, /* list of extents */
4126 xfs_mount_t *mp) /* mount point structure */
4128 xfs_bmap_free_item_t *cur; /* current (next) element */
4129 xfs_bmap_free_item_t *new; /* new element */
4130 xfs_bmap_free_item_t *prev; /* previous element */
4132 xfs_agnumber_t agno;
4133 xfs_agblock_t agbno;
4135 ASSERT(bno != NULLFSBLOCK);
4137 ASSERT(len <= MAXEXTLEN);
4138 ASSERT(!isnullstartblock(bno));
4139 agno = XFS_FSB_TO_AGNO(mp, bno);
4140 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4141 ASSERT(agno < mp->m_sb.sb_agcount);
4142 ASSERT(agbno < mp->m_sb.sb_agblocks);
4143 ASSERT(len < mp->m_sb.sb_agblocks);
4144 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4146 ASSERT(xfs_bmap_free_item_zone != NULL);
4147 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4148 new->xbfi_startblock = bno;
4149 new->xbfi_blockcount = (xfs_extlen_t)len;
4150 for (prev = NULL, cur = flist->xbf_first;
4152 prev = cur, cur = cur->xbfi_next) {
4153 if (cur->xbfi_startblock >= bno)
4157 prev->xbfi_next = new;
4159 flist->xbf_first = new;
4160 new->xbfi_next = cur;
4165 * Compute and fill in the value of the maximum depth of a bmap btree
4166 * in this filesystem. Done once, during mount.
4169 xfs_bmap_compute_maxlevels(
4170 xfs_mount_t *mp, /* file system mount structure */
4171 int whichfork) /* data or attr fork */
4173 int level; /* btree level */
4174 uint maxblocks; /* max blocks at this level */
4175 uint maxleafents; /* max leaf entries possible */
4176 int maxrootrecs; /* max records in root block */
4177 int minleafrecs; /* min records in leaf block */
4178 int minnoderecs; /* min records in node block */
4179 int sz; /* root block size */
4182 * The maximum number of extents in a file, hence the maximum
4183 * number of leaf entries, is controlled by the type of di_nextents
4184 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4185 * (a signed 16-bit number, xfs_aextnum_t).
4187 * Note that we can no longer assume that if we are in ATTR1 that
4188 * the fork offset of all the inodes will be
4189 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
4190 * with ATTR2 and then mounted back with ATTR1, keeping the
4191 * di_forkoff's fixed but probably at various positions. Therefore,
4192 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
4193 * of a minimum size available.
4195 if (whichfork == XFS_DATA_FORK) {
4196 maxleafents = MAXEXTNUM;
4197 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
4199 maxleafents = MAXAEXTNUM;
4200 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
4202 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
4203 minleafrecs = mp->m_bmap_dmnr[0];
4204 minnoderecs = mp->m_bmap_dmnr[1];
4205 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4206 for (level = 1; maxblocks > 1; level++) {
4207 if (maxblocks <= maxrootrecs)
4210 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4212 mp->m_bm_maxlevels[whichfork] = level;
4216 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4217 * caller. Frees all the extents that need freeing, which must be done
4218 * last due to locking considerations. We never free any extents in
4219 * the first transaction. This is to allow the caller to make the first
4220 * transaction a synchronous one so that the pointers to the data being
4221 * broken in this transaction will be permanent before the data is actually
4222 * freed. This is necessary to prevent blocks from being reallocated
4223 * and written to before the free and reallocation are actually permanent.
4224 * We do not just make the first transaction synchronous here, because
4225 * there are more efficient ways to gain the same protection in some cases
4226 * (see the file truncation code).
4228 * Return 1 if the given transaction was committed and a new one
4229 * started, and 0 otherwise in the committed parameter.
4234 xfs_trans_t **tp, /* transaction pointer addr */
4235 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4236 int *committed) /* xact committed or not */
4238 xfs_efd_log_item_t *efd; /* extent free data */
4239 xfs_efi_log_item_t *efi; /* extent free intention */
4240 int error; /* error return value */
4241 xfs_bmap_free_item_t *free; /* free extent item */
4242 unsigned int logres; /* new log reservation */
4243 unsigned int logcount; /* new log count */
4244 xfs_mount_t *mp; /* filesystem mount structure */
4245 xfs_bmap_free_item_t *next; /* next item on free list */
4246 xfs_trans_t *ntp; /* new transaction pointer */
4248 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4249 if (flist->xbf_count == 0) {
4254 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4255 for (free = flist->xbf_first; free; free = free->xbfi_next)
4256 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4257 free->xbfi_blockcount);
4258 logres = ntp->t_log_res;
4259 logcount = ntp->t_log_count;
4260 ntp = xfs_trans_dup(*tp);
4261 error = xfs_trans_commit(*tp, 0);
4265 * We have a new transaction, so we should return committed=1,
4266 * even though we're returning an error.
4272 * transaction commit worked ok so we can drop the extra ticket
4273 * reference that we gained in xfs_trans_dup()
4275 xfs_log_ticket_put(ntp->t_ticket);
4277 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4280 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4281 for (free = flist->xbf_first; free != NULL; free = next) {
4282 next = free->xbfi_next;
4283 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4284 free->xbfi_blockcount))) {
4286 * The bmap free list will be cleaned up at a
4287 * higher level. The EFI will be canceled when
4288 * this transaction is aborted.
4289 * Need to force shutdown here to make sure it
4290 * happens, since this transaction may not be
4294 if (!XFS_FORCED_SHUTDOWN(mp))
4295 xfs_force_shutdown(mp,
4296 (error == EFSCORRUPTED) ?
4297 SHUTDOWN_CORRUPT_INCORE :
4298 SHUTDOWN_META_IO_ERROR);
4301 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4302 free->xbfi_blockcount);
4303 xfs_bmap_del_free(flist, NULL, free);
4309 * Free up any items left in the list.
4313 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4315 xfs_bmap_free_item_t *free; /* free list item */
4316 xfs_bmap_free_item_t *next;
4318 if (flist->xbf_count == 0)
4320 ASSERT(flist->xbf_first != NULL);
4321 for (free = flist->xbf_first; free; free = next) {
4322 next = free->xbfi_next;
4323 xfs_bmap_del_free(flist, NULL, free);
4325 ASSERT(flist->xbf_count == 0);
4329 * Returns the file-relative block number of the first unused block(s)
4330 * in the file with at least "len" logically contiguous blocks free.
4331 * This is the lowest-address hole if the file has holes, else the first block
4332 * past the end of file.
4333 * Return 0 if the file is currently local (in-inode).
4336 xfs_bmap_first_unused(
4337 xfs_trans_t *tp, /* transaction pointer */
4338 xfs_inode_t *ip, /* incore inode */
4339 xfs_extlen_t len, /* size of hole to find */
4340 xfs_fileoff_t *first_unused, /* unused block */
4341 int whichfork) /* data or attr fork */
4343 int error; /* error return value */
4344 int idx; /* extent record index */
4345 xfs_ifork_t *ifp; /* inode fork pointer */
4346 xfs_fileoff_t lastaddr; /* last block number seen */
4347 xfs_fileoff_t lowest; /* lowest useful block */
4348 xfs_fileoff_t max; /* starting useful block */
4349 xfs_fileoff_t off; /* offset for this block */
4350 xfs_extnum_t nextents; /* number of extent entries */
4352 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4353 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4354 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4355 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4359 ifp = XFS_IFORK_PTR(ip, whichfork);
4360 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4361 (error = xfs_iread_extents(tp, ip, whichfork)))
4363 lowest = *first_unused;
4364 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4365 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
4366 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
4367 off = xfs_bmbt_get_startoff(ep);
4369 * See if the hole before this extent will work.
4371 if (off >= lowest + len && off - max >= len) {
4372 *first_unused = max;
4375 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4376 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4378 *first_unused = max;
4383 * Returns the file-relative block number of the last block + 1 before
4384 * last_block (input value) in the file.
4385 * This is not based on i_size, it is based on the extent records.
4386 * Returns 0 for local files, as they do not have extent records.
4389 xfs_bmap_last_before(
4390 xfs_trans_t *tp, /* transaction pointer */
4391 xfs_inode_t *ip, /* incore inode */
4392 xfs_fileoff_t *last_block, /* last block */
4393 int whichfork) /* data or attr fork */
4395 xfs_fileoff_t bno; /* input file offset */
4396 int eof; /* hit end of file */
4397 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
4398 int error; /* error return value */
4399 xfs_bmbt_irec_t got; /* current extent value */
4400 xfs_ifork_t *ifp; /* inode fork pointer */
4401 xfs_extnum_t lastx; /* last extent used */
4402 xfs_bmbt_irec_t prev; /* previous extent value */
4404 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4405 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4406 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4407 return XFS_ERROR(EIO);
4408 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4412 ifp = XFS_IFORK_PTR(ip, whichfork);
4413 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4414 (error = xfs_iread_extents(tp, ip, whichfork)))
4416 bno = *last_block - 1;
4417 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4419 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4420 if (prev.br_startoff == NULLFILEOFF)
4423 *last_block = prev.br_startoff + prev.br_blockcount;
4426 * Otherwise *last_block is already the right answer.
4432 * Returns the file-relative block number of the first block past eof in
4433 * the file. This is not based on i_size, it is based on the extent records.
4434 * Returns 0 for local files, as they do not have extent records.
4437 xfs_bmap_last_offset(
4438 xfs_trans_t *tp, /* transaction pointer */
4439 xfs_inode_t *ip, /* incore inode */
4440 xfs_fileoff_t *last_block, /* last block */
4441 int whichfork) /* data or attr fork */
4443 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
4444 int error; /* error return value */
4445 xfs_ifork_t *ifp; /* inode fork pointer */
4446 xfs_extnum_t nextents; /* number of extent entries */
4448 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4449 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4450 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4451 return XFS_ERROR(EIO);
4452 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4456 ifp = XFS_IFORK_PTR(ip, whichfork);
4457 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4458 (error = xfs_iread_extents(tp, ip, whichfork)))
4460 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4465 ep = xfs_iext_get_ext(ifp, nextents - 1);
4466 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4471 * Returns whether the selected fork of the inode has exactly one
4472 * block or not. For the data fork we check this matches di_size,
4473 * implying the file's range is 0..bsize-1.
4475 int /* 1=>1 block, 0=>otherwise */
4477 xfs_inode_t *ip, /* incore inode */
4478 int whichfork) /* data or attr fork */
4480 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
4481 xfs_ifork_t *ifp; /* inode fork pointer */
4482 int rval; /* return value */
4483 xfs_bmbt_irec_t s; /* internal version of extent */
4486 if (whichfork == XFS_DATA_FORK) {
4487 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4488 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4489 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4492 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4494 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4496 ifp = XFS_IFORK_PTR(ip, whichfork);
4497 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4498 ep = xfs_iext_get_ext(ifp, 0);
4499 xfs_bmbt_get_all(ep, &s);
4500 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4501 if (rval && whichfork == XFS_DATA_FORK)
4502 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
4507 xfs_bmap_sanity_check(
4508 struct xfs_mount *mp,
4512 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4514 if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
4515 be16_to_cpu(block->bb_level) != level ||
4516 be16_to_cpu(block->bb_numrecs) == 0 ||
4517 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4523 * Read in the extents to if_extents.
4524 * All inode fields are set up by caller, we just traverse the btree
4525 * and copy the records in. If the file system cannot contain unwritten
4526 * extents, the records are checked for no "state" flags.
4529 xfs_bmap_read_extents(
4530 xfs_trans_t *tp, /* transaction pointer */
4531 xfs_inode_t *ip, /* incore inode */
4532 int whichfork) /* data or attr fork */
4534 struct xfs_btree_block *block; /* current btree block */
4535 xfs_fsblock_t bno; /* block # of "block" */
4536 xfs_buf_t *bp; /* buffer for "block" */
4537 int error; /* error return value */
4538 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4539 xfs_extnum_t i, j; /* index into the extents list */
4540 xfs_ifork_t *ifp; /* fork structure */
4541 int level; /* btree level, for checking */
4542 xfs_mount_t *mp; /* file system mount structure */
4543 __be64 *pp; /* pointer to block address */
4545 xfs_extnum_t room; /* number of entries there's room for */
4549 ifp = XFS_IFORK_PTR(ip, whichfork);
4550 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4551 XFS_EXTFMT_INODE(ip);
4552 block = ifp->if_broot;
4554 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4556 level = be16_to_cpu(block->bb_level);
4558 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
4559 bno = be64_to_cpu(*pp);
4560 ASSERT(bno != NULLDFSBNO);
4561 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4562 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4564 * Go down the tree until leaf level is reached, following the first
4565 * pointer (leftmost) at each level.
4567 while (level-- > 0) {
4568 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4569 XFS_BMAP_BTREE_REF)))
4571 block = XFS_BUF_TO_BLOCK(bp);
4572 XFS_WANT_CORRUPTED_GOTO(
4573 xfs_bmap_sanity_check(mp, bp, level),
4577 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
4578 bno = be64_to_cpu(*pp);
4579 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4580 xfs_trans_brelse(tp, bp);
4583 * Here with bp and block set to the leftmost leaf node in the tree.
4585 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4588 * Loop over all leaf nodes. Copy information to the extent records.
4591 xfs_bmbt_rec_t *frp;
4592 xfs_fsblock_t nextbno;
4593 xfs_extnum_t num_recs;
4597 num_recs = xfs_btree_get_numrecs(block);
4598 if (unlikely(i + num_recs > room)) {
4599 ASSERT(i + num_recs <= room);
4600 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4601 "corrupt dinode %Lu, (btree extents).",
4602 (unsigned long long) ip->i_ino);
4603 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4608 XFS_WANT_CORRUPTED_GOTO(
4609 xfs_bmap_sanity_check(mp, bp, 0),
4612 * Read-ahead the next leaf block, if any.
4614 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
4615 if (nextbno != NULLFSBLOCK)
4616 xfs_btree_reada_bufl(mp, nextbno, 1);
4618 * Copy records into the extent records.
4620 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
4622 for (j = 0; j < num_recs; j++, i++, frp++) {
4623 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
4624 trp->l0 = be64_to_cpu(frp->l0);
4625 trp->l1 = be64_to_cpu(frp->l1);
4627 if (exntf == XFS_EXTFMT_NOSTATE) {
4629 * Check all attribute bmap btree records and
4630 * any "older" data bmap btree records for a
4631 * set bit in the "extent flag" position.
4633 if (unlikely(xfs_check_nostate_extents(ifp,
4634 start, num_recs))) {
4635 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4641 xfs_trans_brelse(tp, bp);
4644 * If we've reached the end, stop.
4646 if (bno == NULLFSBLOCK)
4648 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4649 XFS_BMAP_BTREE_REF)))
4651 block = XFS_BUF_TO_BLOCK(bp);
4653 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4654 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4655 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
4658 xfs_trans_brelse(tp, bp);
4659 return XFS_ERROR(EFSCORRUPTED);
4662 #ifdef XFS_BMAP_TRACE
4664 * Add bmap trace insert entries for all the contents of the extent records.
4667 xfs_bmap_trace_exlist(
4668 const char *fname, /* function name */
4669 xfs_inode_t *ip, /* incore inode pointer */
4670 xfs_extnum_t cnt, /* count of entries in the list */
4671 int whichfork) /* data or attr fork */
4673 xfs_bmbt_rec_host_t *ep; /* current extent record */
4674 xfs_extnum_t idx; /* extent record index */
4675 xfs_ifork_t *ifp; /* inode fork pointer */
4676 xfs_bmbt_irec_t s; /* file extent record */
4678 ifp = XFS_IFORK_PTR(ip, whichfork);
4679 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4680 for (idx = 0; idx < cnt; idx++) {
4681 ep = xfs_iext_get_ext(ifp, idx);
4682 xfs_bmbt_get_all(ep, &s);
4683 XFS_BMAP_TRACE_INSERT("exlist", ip, idx, 1, &s, NULL,
4691 * Validate that the bmbt_irecs being returned from bmapi are valid
4692 * given the callers original parameters. Specifically check the
4693 * ranges of the returned irecs to ensure that they only extent beyond
4694 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4697 xfs_bmap_validate_ret(
4701 xfs_bmbt_irec_t *mval,
4705 int i; /* index to map values */
4707 ASSERT(ret_nmap <= nmap);
4709 for (i = 0; i < ret_nmap; i++) {
4710 ASSERT(mval[i].br_blockcount > 0);
4711 if (!(flags & XFS_BMAPI_ENTIRE)) {
4712 ASSERT(mval[i].br_startoff >= bno);
4713 ASSERT(mval[i].br_blockcount <= len);
4714 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4717 ASSERT(mval[i].br_startoff < bno + len);
4718 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4722 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4723 mval[i].br_startoff);
4724 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4725 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4726 mval[i].br_startblock != HOLESTARTBLOCK);
4727 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4728 mval[i].br_state == XFS_EXT_UNWRITTEN);
4735 * Map file blocks to filesystem blocks.
4736 * File range is given by the bno/len pair.
4737 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4738 * into a hole or past eof.
4739 * Only allocates blocks from a single allocation group,
4740 * to avoid locking problems.
4741 * The returned value in "firstblock" from the first call in a transaction
4742 * must be remembered and presented to subsequent calls in "firstblock".
4743 * An upper bound for the number of blocks to be allocated is supplied to
4744 * the first call in "total"; if no allocation group has that many free
4745 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4749 xfs_trans_t *tp, /* transaction pointer */
4750 xfs_inode_t *ip, /* incore inode */
4751 xfs_fileoff_t bno, /* starting file offs. mapped */
4752 xfs_filblks_t len, /* length to map in file */
4753 int flags, /* XFS_BMAPI_... */
4754 xfs_fsblock_t *firstblock, /* first allocated block
4755 controls a.g. for allocs */
4756 xfs_extlen_t total, /* total blocks needed */
4757 xfs_bmbt_irec_t *mval, /* output: map values */
4758 int *nmap, /* i/o: mval size/count */
4759 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4760 xfs_extdelta_t *delta) /* o: change made to incore extents */
4762 xfs_fsblock_t abno; /* allocated block number */
4763 xfs_extlen_t alen; /* allocated extent length */
4764 xfs_fileoff_t aoff; /* allocated file offset */
4765 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
4766 xfs_btree_cur_t *cur; /* bmap btree cursor */
4767 xfs_fileoff_t end; /* end of mapped file region */
4768 int eof; /* we've hit the end of extents */
4769 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
4770 int error; /* error return */
4771 xfs_bmbt_irec_t got; /* current file extent record */
4772 xfs_ifork_t *ifp; /* inode fork pointer */
4773 xfs_extlen_t indlen; /* indirect blocks length */
4774 xfs_extnum_t lastx; /* last useful extent number */
4775 int logflags; /* flags for transaction logging */
4776 xfs_extlen_t minleft; /* min blocks left after allocation */
4777 xfs_extlen_t minlen; /* min allocation size */
4778 xfs_mount_t *mp; /* xfs mount structure */
4779 int n; /* current extent index */
4780 int nallocs; /* number of extents alloc'd */
4781 xfs_extnum_t nextents; /* number of extents in file */
4782 xfs_fileoff_t obno; /* old block number (offset) */
4783 xfs_bmbt_irec_t prev; /* previous file extent record */
4784 int tmp_logflags; /* temp flags holder */
4785 int whichfork; /* data or attr fork */
4786 char inhole; /* current location is hole in file */
4787 char wasdelay; /* old extent was delayed */
4788 char wr; /* this is a write request */
4789 char rt; /* this is a realtime file */
4791 xfs_fileoff_t orig_bno; /* original block number value */
4792 int orig_flags; /* original flags arg value */
4793 xfs_filblks_t orig_len; /* original value of len arg */
4794 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4795 int orig_nmap; /* original value of *nmap */
4804 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4805 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4806 XFS_ATTR_FORK : XFS_DATA_FORK;
4808 if (unlikely(XFS_TEST_ERROR(
4809 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4810 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4811 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4812 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4813 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4814 return XFS_ERROR(EFSCORRUPTED);
4816 if (XFS_FORCED_SHUTDOWN(mp))
4817 return XFS_ERROR(EIO);
4818 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4819 ifp = XFS_IFORK_PTR(ip, whichfork);
4820 ASSERT(ifp->if_ext_max ==
4821 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4822 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4823 XFS_STATS_INC(xs_blk_mapw);
4825 XFS_STATS_INC(xs_blk_mapr);
4827 * IGSTATE flag is used to combine extents which
4828 * differ only due to the state of the extents.
4829 * This technique is used from xfs_getbmap()
4830 * when the caller does not wish to see the
4831 * separation (which is the default).
4833 * This technique is also used when writing a
4834 * buffer which has been partially written,
4835 * (usually by being flushed during a chunkread),
4836 * to ensure one write takes place. This also
4837 * prevents a change in the xfs inode extents at
4838 * this time, intentionally. This change occurs
4839 * on completion of the write operation, in
4840 * xfs_strat_comp(), where the xfs_bmapi() call
4841 * is transactioned, and the extents combined.
4843 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4844 wr = 0; /* no allocations are allowed */
4845 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
4849 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4851 if ((error = xfs_bmap_local_to_extents(tp, ip,
4852 firstblock, total, &logflags, whichfork)))
4855 if (wr && *firstblock == NULLFSBLOCK) {
4856 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4857 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4862 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4863 (error = xfs_iread_extents(tp, ip, whichfork)))
4865 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4867 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4873 delta->xed_startoff = NULLFILEOFF;
4874 delta->xed_blockcount = 0;
4876 while (bno < end && n < *nmap) {
4878 * Reading past eof, act as though there's a hole
4882 got.br_startoff = end;
4883 inhole = eof || got.br_startoff > bno;
4884 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
4885 isnullstartblock(got.br_startblock);
4887 * First, deal with the hole before the allocated space
4888 * that we found, if any.
4890 if (wr && (inhole || wasdelay)) {
4892 * For the wasdelay case, we could also just
4893 * allocate the stuff asked for in this bmap call
4894 * but that wouldn't be as good.
4896 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
4897 alen = (xfs_extlen_t)got.br_blockcount;
4898 aoff = got.br_startoff;
4899 if (lastx != NULLEXTNUM && lastx) {
4900 ep = xfs_iext_get_ext(ifp, lastx - 1);
4901 xfs_bmbt_get_all(ep, &prev);
4903 } else if (wasdelay) {
4904 alen = (xfs_extlen_t)
4905 XFS_FILBLKS_MIN(len,
4907 got.br_blockcount) - bno);
4910 alen = (xfs_extlen_t)
4911 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4913 alen = (xfs_extlen_t)
4914 XFS_FILBLKS_MIN(alen,
4915 got.br_startoff - bno);
4918 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4919 if (flags & XFS_BMAPI_DELAY) {
4922 /* Figure out the extent size, adjust alen */
4923 extsz = xfs_get_extsz_hint(ip);
4925 error = xfs_bmap_extsize_align(mp,
4928 flags&XFS_BMAPI_DELAY,
4929 flags&XFS_BMAPI_CONVERT,
4935 extsz = alen / mp->m_sb.sb_rextsize;
4938 * Make a transaction-less quota reservation for
4939 * delayed allocation blocks. This number gets
4940 * adjusted later. We return if we haven't
4941 * allocated blocks already inside this loop.
4943 error = xfs_trans_reserve_quota_nblks(
4944 NULL, ip, (long)alen, 0,
4945 rt ? XFS_QMOPT_RES_RTBLKS :
4946 XFS_QMOPT_RES_REGBLKS);
4950 ASSERT(cur == NULL);
4957 * Split changing sb for alen and indlen since
4958 * they could be coming from different places.
4960 indlen = (xfs_extlen_t)
4961 xfs_bmap_worst_indlen(ip, alen);
4965 error = xfs_mod_incore_sb(mp,
4967 -((int64_t)extsz), (flags &
4968 XFS_BMAPI_RSVBLOCKS));
4970 error = xfs_mod_incore_sb(mp,
4972 -((int64_t)alen), (flags &
4973 XFS_BMAPI_RSVBLOCKS));
4976 error = xfs_mod_incore_sb(mp,
4978 -((int64_t)indlen), (flags &
4979 XFS_BMAPI_RSVBLOCKS));
4981 xfs_mod_incore_sb(mp,
4983 (int64_t)extsz, (flags &
4984 XFS_BMAPI_RSVBLOCKS));
4986 xfs_mod_incore_sb(mp,
4988 (int64_t)alen, (flags &
4989 XFS_BMAPI_RSVBLOCKS));
4993 if (XFS_IS_QUOTA_ON(mp))
4994 /* unreserve the blocks now */
4996 xfs_trans_unreserve_quota_nblks(
4999 XFS_QMOPT_RES_RTBLKS :
5000 XFS_QMOPT_RES_REGBLKS);
5004 ip->i_delayed_blks += alen;
5005 abno = nullstartblock(indlen);
5008 * If first time, allocate and fill in
5009 * once-only bma fields.
5011 if (bma.ip == NULL) {
5019 /* Indicate if this is the first user data
5020 * in the file, or just any user data.
5022 if (!(flags & XFS_BMAPI_METADATA)) {
5023 bma.userdata = (aoff == 0) ?
5024 XFS_ALLOC_INITIAL_USER_DATA :
5028 * Fill in changeable bma fields.
5031 bma.firstblock = *firstblock;
5034 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
5035 bma.wasdel = wasdelay;
5036 bma.minlen = minlen;
5037 bma.low = flist->xbf_low;
5038 bma.minleft = minleft;
5040 * Only want to do the alignment at the
5041 * eof if it is userdata and allocation length
5042 * is larger than a stripe unit.
5044 if (mp->m_dalign && alen >= mp->m_dalign &&
5045 (!(flags & XFS_BMAPI_METADATA)) &&
5046 (whichfork == XFS_DATA_FORK)) {
5047 if ((error = xfs_bmap_isaeof(ip, aoff,
5048 whichfork, &bma.aeof)))
5055 if ((error = xfs_bmap_alloc(&bma)))
5058 * Copy out result fields.
5061 if ((flist->xbf_low = bma.low))
5065 ASSERT(*firstblock == NULLFSBLOCK ||
5066 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5067 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
5069 XFS_FSB_TO_AGNO(mp, *firstblock) <
5070 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
5071 *firstblock = bma.firstblock;
5073 cur->bc_private.b.firstblock =
5075 if (abno == NULLFSBLOCK)
5077 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5078 cur = xfs_bmbt_init_cursor(mp, tp,
5080 cur->bc_private.b.firstblock =
5082 cur->bc_private.b.flist = flist;
5085 * Bump the number of extents we've allocated
5091 cur->bc_private.b.flags =
5092 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
5093 got.br_startoff = aoff;
5094 got.br_startblock = abno;
5095 got.br_blockcount = alen;
5096 got.br_state = XFS_EXT_NORM; /* assume normal */
5098 * Determine state of extent, and the filesystem.
5099 * A wasdelay extent has been initialized, so
5100 * shouldn't be flagged as unwritten.
5102 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5103 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5104 got.br_state = XFS_EXT_UNWRITTEN;
5106 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
5107 firstblock, flist, &tmp_logflags, delta,
5108 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5109 logflags |= tmp_logflags;
5112 lastx = ifp->if_lastex;
5113 ep = xfs_iext_get_ext(ifp, lastx);
5114 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5115 xfs_bmbt_get_all(ep, &got);
5116 ASSERT(got.br_startoff <= aoff);
5117 ASSERT(got.br_startoff + got.br_blockcount >=
5120 if (flags & XFS_BMAPI_DELAY) {
5121 ASSERT(isnullstartblock(got.br_startblock));
5122 ASSERT(startblockval(got.br_startblock) > 0);
5124 ASSERT(got.br_state == XFS_EXT_NORM ||
5125 got.br_state == XFS_EXT_UNWRITTEN);
5128 * Fall down into the found allocated space case.
5130 } else if (inhole) {
5132 * Reading in a hole.
5134 mval->br_startoff = bno;
5135 mval->br_startblock = HOLESTARTBLOCK;
5136 mval->br_blockcount =
5137 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5138 mval->br_state = XFS_EXT_NORM;
5139 bno += mval->br_blockcount;
5140 len -= mval->br_blockcount;
5146 * Then deal with the allocated space we found.
5149 if (!(flags & XFS_BMAPI_ENTIRE) &&
5150 (got.br_startoff + got.br_blockcount > obno)) {
5153 ASSERT((bno >= obno) || (n == 0));
5155 mval->br_startoff = bno;
5156 if (isnullstartblock(got.br_startblock)) {
5157 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5158 mval->br_startblock = DELAYSTARTBLOCK;
5160 mval->br_startblock =
5162 (bno - got.br_startoff);
5164 * Return the minimum of what we got and what we
5165 * asked for for the length. We can use the len
5166 * variable here because it is modified below
5167 * and we could have been there before coming
5168 * here if the first part of the allocation
5169 * didn't overlap what was asked for.
5171 mval->br_blockcount =
5172 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5173 (bno - got.br_startoff));
5174 mval->br_state = got.br_state;
5175 ASSERT(mval->br_blockcount <= len);
5178 if (isnullstartblock(mval->br_startblock)) {
5179 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5180 mval->br_startblock = DELAYSTARTBLOCK;
5185 * Check if writing previously allocated but
5186 * unwritten extents.
5188 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5189 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5191 * Modify (by adding) the state flag, if writing.
5193 ASSERT(mval->br_blockcount <= len);
5194 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5195 cur = xfs_bmbt_init_cursor(mp,
5197 cur->bc_private.b.firstblock =
5199 cur->bc_private.b.flist = flist;
5201 mval->br_state = XFS_EXT_NORM;
5202 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
5203 firstblock, flist, &tmp_logflags, delta,
5204 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5205 logflags |= tmp_logflags;
5208 lastx = ifp->if_lastex;
5209 ep = xfs_iext_get_ext(ifp, lastx);
5210 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5211 xfs_bmbt_get_all(ep, &got);
5213 * We may have combined previously unwritten
5214 * space with written space, so generate
5217 if (mval->br_blockcount < len)
5221 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5222 ((mval->br_startoff + mval->br_blockcount) <= end));
5223 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5224 (mval->br_blockcount <= len) ||
5225 (mval->br_startoff < obno));
5226 bno = mval->br_startoff + mval->br_blockcount;
5228 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5229 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5230 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5231 ASSERT(mval->br_state == mval[-1].br_state);
5232 mval[-1].br_blockcount = mval->br_blockcount;
5233 mval[-1].br_state = mval->br_state;
5234 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5235 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5236 mval[-1].br_startblock != HOLESTARTBLOCK &&
5237 mval->br_startblock ==
5238 mval[-1].br_startblock + mval[-1].br_blockcount &&
5239 ((flags & XFS_BMAPI_IGSTATE) ||
5240 mval[-1].br_state == mval->br_state)) {
5241 ASSERT(mval->br_startoff ==
5242 mval[-1].br_startoff + mval[-1].br_blockcount);
5243 mval[-1].br_blockcount += mval->br_blockcount;
5245 mval->br_startblock == DELAYSTARTBLOCK &&
5246 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5247 mval->br_startoff ==
5248 mval[-1].br_startoff + mval[-1].br_blockcount) {
5249 mval[-1].br_blockcount += mval->br_blockcount;
5250 mval[-1].br_state = mval->br_state;
5251 } else if (!((n == 0) &&
5252 ((mval->br_startoff + mval->br_blockcount) <=
5258 * If we're done, stop now. Stop when we've allocated
5259 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5260 * the transaction may get too big.
5262 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5265 * Else go on to the next record.
5267 ep = xfs_iext_get_ext(ifp, ++lastx);
5269 if (lastx >= nextents)
5272 xfs_bmbt_get_all(ep, &got);
5274 ifp->if_lastex = lastx;
5277 * Transform from btree to extents, give it cur.
5279 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5280 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5282 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5283 &tmp_logflags, whichfork);
5284 logflags |= tmp_logflags;
5288 ASSERT(ifp->if_ext_max ==
5289 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5290 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5291 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5293 if (delta && delta->xed_startoff != NULLFILEOFF) {
5294 /* A change was actually made.
5295 * Note that delta->xed_blockount is an offset at this
5296 * point and needs to be converted to a block count.
5298 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5299 delta->xed_blockcount -= delta->xed_startoff;
5303 * Log everything. Do this after conversion, there's no point in
5304 * logging the extent records if we've converted to btree format.
5306 if ((logflags & xfs_ilog_fext(whichfork)) &&
5307 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5308 logflags &= ~xfs_ilog_fext(whichfork);
5309 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5310 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5311 logflags &= ~xfs_ilog_fbroot(whichfork);
5313 * Log whatever the flags say, even if error. Otherwise we might miss
5314 * detecting a case where the data is changed, there's an error,
5315 * and it's not logged so we don't shutdown when we should.
5319 xfs_trans_log_inode(tp, ip, logflags);
5323 ASSERT(*firstblock == NULLFSBLOCK ||
5324 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5326 cur->bc_private.b.firstblock) ||
5328 XFS_FSB_TO_AGNO(mp, *firstblock) <
5330 cur->bc_private.b.firstblock)));
5331 *firstblock = cur->bc_private.b.firstblock;
5333 xfs_btree_del_cursor(cur,
5334 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5337 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5343 * Map file blocks to filesystem blocks, simple version.
5344 * One block (extent) only, read-only.
5345 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5346 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5347 * was set and all the others were clear.
5351 xfs_trans_t *tp, /* transaction pointer */
5352 xfs_inode_t *ip, /* incore inode */
5353 int whichfork, /* data or attr fork */
5354 xfs_fsblock_t *fsb, /* output: mapped block */
5355 xfs_fileoff_t bno) /* starting file offs. mapped */
5357 int eof; /* we've hit the end of extents */
5358 int error; /* error return */
5359 xfs_bmbt_irec_t got; /* current file extent record */
5360 xfs_ifork_t *ifp; /* inode fork pointer */
5361 xfs_extnum_t lastx; /* last useful extent number */
5362 xfs_bmbt_irec_t prev; /* previous file extent record */
5364 ifp = XFS_IFORK_PTR(ip, whichfork);
5366 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5367 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5368 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5370 return XFS_ERROR(EFSCORRUPTED);
5372 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5373 return XFS_ERROR(EIO);
5374 XFS_STATS_INC(xs_blk_mapr);
5375 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5376 (error = xfs_iread_extents(tp, ip, whichfork)))
5378 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5381 * Reading past eof, act as though there's a hole
5384 if (eof || got.br_startoff > bno) {
5388 ASSERT(!isnullstartblock(got.br_startblock));
5389 ASSERT(bno < got.br_startoff + got.br_blockcount);
5390 *fsb = got.br_startblock + (bno - got.br_startoff);
5391 ifp->if_lastex = lastx;
5396 * Unmap (remove) blocks from a file.
5397 * If nexts is nonzero then the number of extents to remove is limited to
5398 * that value. If not all extents in the block range can be removed then
5403 xfs_trans_t *tp, /* transaction pointer */
5404 struct xfs_inode *ip, /* incore inode */
5405 xfs_fileoff_t bno, /* starting offset to unmap */
5406 xfs_filblks_t len, /* length to unmap in file */
5407 int flags, /* misc flags */
5408 xfs_extnum_t nexts, /* number of extents max */
5409 xfs_fsblock_t *firstblock, /* first allocated block
5410 controls a.g. for allocs */
5411 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5412 xfs_extdelta_t *delta, /* o: change made to incore
5414 int *done) /* set if not done yet */
5416 xfs_btree_cur_t *cur; /* bmap btree cursor */
5417 xfs_bmbt_irec_t del; /* extent being deleted */
5418 int eof; /* is deleting at eof */
5419 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
5420 int error; /* error return value */
5421 xfs_extnum_t extno; /* extent number in list */
5422 xfs_bmbt_irec_t got; /* current extent record */
5423 xfs_ifork_t *ifp; /* inode fork pointer */
5424 int isrt; /* freeing in rt area */
5425 xfs_extnum_t lastx; /* last extent index used */
5426 int logflags; /* transaction logging flags */
5427 xfs_extlen_t mod; /* rt extent offset */
5428 xfs_mount_t *mp; /* mount structure */
5429 xfs_extnum_t nextents; /* number of file extents */
5430 xfs_bmbt_irec_t prev; /* previous extent record */
5431 xfs_fileoff_t start; /* first file offset deleted */
5432 int tmp_logflags; /* partial logging flags */
5433 int wasdel; /* was a delayed alloc extent */
5434 int whichfork; /* data or attribute fork */
5435 int rsvd; /* OK to allocate reserved blocks */
5438 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5439 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5440 XFS_ATTR_FORK : XFS_DATA_FORK;
5441 ifp = XFS_IFORK_PTR(ip, whichfork);
5443 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5444 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5445 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5447 return XFS_ERROR(EFSCORRUPTED);
5450 if (XFS_FORCED_SHUTDOWN(mp))
5451 return XFS_ERROR(EIO);
5452 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5455 ASSERT(ifp->if_ext_max ==
5456 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5457 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5458 (error = xfs_iread_extents(tp, ip, whichfork)))
5460 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5461 if (nextents == 0) {
5465 XFS_STATS_INC(xs_blk_unmap);
5466 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5468 bno = start + len - 1;
5469 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5472 delta->xed_startoff = NULLFILEOFF;
5473 delta->xed_blockcount = 0;
5476 * Check to see if the given block number is past the end of the
5477 * file, back up to the last block if so...
5480 ep = xfs_iext_get_ext(ifp, --lastx);
5481 xfs_bmbt_get_all(ep, &got);
5482 bno = got.br_startoff + got.br_blockcount - 1;
5485 if (ifp->if_flags & XFS_IFBROOT) {
5486 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5487 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5488 cur->bc_private.b.firstblock = *firstblock;
5489 cur->bc_private.b.flist = flist;
5490 cur->bc_private.b.flags = 0;
5494 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5495 (nexts == 0 || extno < nexts)) {
5497 * Is the found extent after a hole in which bno lives?
5498 * Just back up to the previous extent, if so.
5500 if (got.br_startoff > bno) {
5503 ep = xfs_iext_get_ext(ifp, lastx);
5504 xfs_bmbt_get_all(ep, &got);
5507 * Is the last block of this extent before the range
5508 * we're supposed to delete? If so, we're done.
5510 bno = XFS_FILEOFF_MIN(bno,
5511 got.br_startoff + got.br_blockcount - 1);
5515 * Then deal with the (possibly delayed) allocated space
5520 wasdel = isnullstartblock(del.br_startblock);
5521 if (got.br_startoff < start) {
5522 del.br_startoff = start;
5523 del.br_blockcount -= start - got.br_startoff;
5525 del.br_startblock += start - got.br_startoff;
5527 if (del.br_startoff + del.br_blockcount > bno + 1)
5528 del.br_blockcount = bno + 1 - del.br_startoff;
5529 sum = del.br_startblock + del.br_blockcount;
5531 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5533 * Realtime extent not lined up at the end.
5534 * The extent could have been split into written
5535 * and unwritten pieces, or we could just be
5536 * unmapping part of it. But we can't really
5537 * get rid of part of a realtime extent.
5539 if (del.br_state == XFS_EXT_UNWRITTEN ||
5540 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5542 * This piece is unwritten, or we're not
5543 * using unwritten extents. Skip over it.
5546 bno -= mod > del.br_blockcount ?
5547 del.br_blockcount : mod;
5548 if (bno < got.br_startoff) {
5550 xfs_bmbt_get_all(xfs_iext_get_ext(
5556 * It's written, turn it unwritten.
5557 * This is better than zeroing it.
5559 ASSERT(del.br_state == XFS_EXT_NORM);
5560 ASSERT(xfs_trans_get_block_res(tp) > 0);
5562 * If this spans a realtime extent boundary,
5563 * chop it back to the start of the one we end at.
5565 if (del.br_blockcount > mod) {
5566 del.br_startoff += del.br_blockcount - mod;
5567 del.br_startblock += del.br_blockcount - mod;
5568 del.br_blockcount = mod;
5570 del.br_state = XFS_EXT_UNWRITTEN;
5571 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5572 firstblock, flist, &logflags, delta,
5578 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5580 * Realtime extent is lined up at the end but not
5581 * at the front. We'll get rid of full extents if
5584 mod = mp->m_sb.sb_rextsize - mod;
5585 if (del.br_blockcount > mod) {
5586 del.br_blockcount -= mod;
5587 del.br_startoff += mod;
5588 del.br_startblock += mod;
5589 } else if ((del.br_startoff == start &&
5590 (del.br_state == XFS_EXT_UNWRITTEN ||
5591 xfs_trans_get_block_res(tp) == 0)) ||
5592 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5594 * Can't make it unwritten. There isn't
5595 * a full extent here so just skip it.
5597 ASSERT(bno >= del.br_blockcount);
5598 bno -= del.br_blockcount;
5599 if (bno < got.br_startoff) {
5601 xfs_bmbt_get_all(--ep, &got);
5604 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5606 * This one is already unwritten.
5607 * It must have a written left neighbor.
5608 * Unwrite the killed part of that one and
5612 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5614 ASSERT(prev.br_state == XFS_EXT_NORM);
5615 ASSERT(!isnullstartblock(prev.br_startblock));
5616 ASSERT(del.br_startblock ==
5617 prev.br_startblock + prev.br_blockcount);
5618 if (prev.br_startoff < start) {
5619 mod = start - prev.br_startoff;
5620 prev.br_blockcount -= mod;
5621 prev.br_startblock += mod;
5622 prev.br_startoff = start;
5624 prev.br_state = XFS_EXT_UNWRITTEN;
5625 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5626 &prev, firstblock, flist, &logflags,
5627 delta, XFS_DATA_FORK, 0);
5632 ASSERT(del.br_state == XFS_EXT_NORM);
5633 del.br_state = XFS_EXT_UNWRITTEN;
5634 error = xfs_bmap_add_extent(ip, lastx, &cur,
5635 &del, firstblock, flist, &logflags,
5636 delta, XFS_DATA_FORK, 0);
5643 ASSERT(startblockval(del.br_startblock) > 0);
5644 /* Update realtime/data freespace, unreserve quota */
5646 xfs_filblks_t rtexts;
5648 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5649 do_div(rtexts, mp->m_sb.sb_rextsize);
5650 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5651 (int64_t)rtexts, rsvd);
5652 (void)xfs_trans_reserve_quota_nblks(NULL,
5653 ip, -((long)del.br_blockcount), 0,
5654 XFS_QMOPT_RES_RTBLKS);
5656 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
5657 (int64_t)del.br_blockcount, rsvd);
5658 (void)xfs_trans_reserve_quota_nblks(NULL,
5659 ip, -((long)del.br_blockcount), 0,
5660 XFS_QMOPT_RES_REGBLKS);
5662 ip->i_delayed_blks -= del.br_blockcount;
5664 cur->bc_private.b.flags |=
5665 XFS_BTCUR_BPRV_WASDEL;
5667 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5669 * If it's the case where the directory code is running
5670 * with no block reservation, and the deleted block is in
5671 * the middle of its extent, and the resulting insert
5672 * of an extent would cause transformation to btree format,
5673 * then reject it. The calling code will then swap
5674 * blocks around instead.
5675 * We have to do this now, rather than waiting for the
5676 * conversion to btree format, since the transaction
5679 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5680 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5681 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5682 del.br_startoff > got.br_startoff &&
5683 del.br_startoff + del.br_blockcount <
5684 got.br_startoff + got.br_blockcount) {
5685 error = XFS_ERROR(ENOSPC);
5688 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5689 &tmp_logflags, delta, whichfork, rsvd);
5690 logflags |= tmp_logflags;
5693 bno = del.br_startoff - 1;
5695 lastx = ifp->if_lastex;
5697 * If not done go on to the next (previous) record.
5698 * Reset ep in case the extents array was re-alloced.
5700 ep = xfs_iext_get_ext(ifp, lastx);
5701 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5702 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5703 xfs_bmbt_get_startoff(ep) > bno) {
5705 ep = xfs_iext_get_ext(ifp, lastx);
5708 xfs_bmbt_get_all(ep, &got);
5712 ifp->if_lastex = lastx;
5713 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5714 ASSERT(ifp->if_ext_max ==
5715 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5717 * Convert to a btree if necessary.
5719 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5720 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5721 ASSERT(cur == NULL);
5722 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5723 &cur, 0, &tmp_logflags, whichfork);
5724 logflags |= tmp_logflags;
5729 * transform from btree to extents, give it cur
5731 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5732 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5733 ASSERT(cur != NULL);
5734 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5736 logflags |= tmp_logflags;
5741 * transform from extents to local?
5743 ASSERT(ifp->if_ext_max ==
5744 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5746 if (delta && delta->xed_startoff != NULLFILEOFF) {
5747 /* A change was actually made.
5748 * Note that delta->xed_blockount is an offset at this
5749 * point and needs to be converted to a block count.
5751 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5752 delta->xed_blockcount -= delta->xed_startoff;
5756 * Log everything. Do this after conversion, there's no point in
5757 * logging the extent records if we've converted to btree format.
5759 if ((logflags & xfs_ilog_fext(whichfork)) &&
5760 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5761 logflags &= ~xfs_ilog_fext(whichfork);
5762 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5763 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5764 logflags &= ~xfs_ilog_fbroot(whichfork);
5766 * Log inode even in the error case, if the transaction
5767 * is dirty we'll need to shut down the filesystem.
5770 xfs_trans_log_inode(tp, ip, logflags);
5773 *firstblock = cur->bc_private.b.firstblock;
5774 cur->bc_private.b.allocated = 0;
5776 xfs_btree_del_cursor(cur,
5777 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5783 * returns 1 for success, 0 if we failed to map the extent.
5786 xfs_getbmapx_fix_eof_hole(
5787 xfs_inode_t *ip, /* xfs incore inode pointer */
5788 struct getbmapx *out, /* output structure */
5789 int prealloced, /* this is a file with
5790 * preallocated data space */
5791 __int64_t end, /* last block requested */
5792 xfs_fsblock_t startblock)
5795 xfs_mount_t *mp; /* file system mount point */
5796 xfs_ifork_t *ifp; /* inode fork pointer */
5797 xfs_extnum_t lastx; /* last extent pointer */
5798 xfs_fileoff_t fileblock;
5800 if (startblock == HOLESTARTBLOCK) {
5802 out->bmv_block = -1;
5803 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5804 fixlen -= out->bmv_offset;
5805 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5806 /* Came to hole at EOF. Trim it. */
5809 out->bmv_length = fixlen;
5812 if (startblock == DELAYSTARTBLOCK)
5813 out->bmv_block = -2;
5815 out->bmv_block = xfs_fsb_to_db(ip, startblock);
5816 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5817 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5818 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5819 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5820 out->bmv_oflags |= BMV_OF_LAST;
5827 * Get inode's extents as described in bmv, and format for output.
5828 * Calls formatter to fill the user's buffer until all extents
5829 * are mapped, until the passed-in bmv->bmv_count slots have
5830 * been filled, or until the formatter short-circuits the loop,
5831 * if it is tracking filled-in extents on its own.
5833 int /* error code */
5836 struct getbmapx *bmv, /* user bmap structure */
5837 xfs_bmap_format_t formatter, /* format to user */
5838 void *arg) /* formatter arg */
5840 __int64_t bmvend; /* last block requested */
5841 int error = 0; /* return value */
5842 __int64_t fixlen; /* length for -1 case */
5843 int i; /* extent number */
5844 int lock; /* lock state */
5845 xfs_bmbt_irec_t *map; /* buffer for user's data */
5846 xfs_mount_t *mp; /* file system mount point */
5847 int nex; /* # of user extents can do */
5848 int nexleft; /* # of user extents left */
5849 int subnex; /* # of bmapi's can do */
5850 int nmap; /* number of map entries */
5851 struct getbmapx *out; /* output structure */
5852 int whichfork; /* data or attr fork */
5853 int prealloced; /* this is a file with
5854 * preallocated data space */
5855 int iflags; /* interface flags */
5856 int bmapi_flags; /* flags for xfs_bmapi */
5860 iflags = bmv->bmv_iflags;
5861 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5863 if (whichfork == XFS_ATTR_FORK) {
5864 if (XFS_IFORK_Q(ip)) {
5865 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5866 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5867 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5868 return XFS_ERROR(EINVAL);
5869 } else if (unlikely(
5870 ip->i_d.di_aformat != 0 &&
5871 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5872 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5874 return XFS_ERROR(EFSCORRUPTED);
5881 * If the BMV_IF_NO_DMAPI_READ interface bit specified, do
5882 * not generate a DMAPI read event. Otherwise, if the
5883 * DM_EVENT_READ bit is set for the file, generate a read
5884 * event in order that the DMAPI application may do its thing
5885 * before we return the extents. Usually this means restoring
5886 * user file data to regions of the file that look like holes.
5888 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5889 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5890 * If this were not true, callers of ioctl(XFS_IOC_GETBMAP)
5891 * could misinterpret holes in a DMAPI file as true holes,
5892 * when in fact they may represent offline user data.
5894 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) &&
5895 !(iflags & BMV_IF_NO_DMAPI_READ)) {
5896 error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip,
5899 return XFS_ERROR(error);
5902 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5903 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5904 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5905 return XFS_ERROR(EINVAL);
5907 if (xfs_get_extsz_hint(ip) ||
5908 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5910 fixlen = XFS_MAXIOFFSET(mp);
5913 fixlen = ip->i_size;
5917 if (bmv->bmv_length == -1) {
5918 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5920 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5921 } else if (bmv->bmv_length == 0) {
5922 bmv->bmv_entries = 0;
5924 } else if (bmv->bmv_length < 0) {
5925 return XFS_ERROR(EINVAL);
5928 nex = bmv->bmv_count - 1;
5930 return XFS_ERROR(EINVAL);
5931 bmvend = bmv->bmv_offset + bmv->bmv_length;
5934 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5935 return XFS_ERROR(ENOMEM);
5936 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5938 return XFS_ERROR(ENOMEM);
5940 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5941 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5942 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5943 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5945 goto out_unlock_iolock;
5948 ASSERT(ip->i_delayed_blks == 0);
5951 lock = xfs_ilock_map_shared(ip);
5954 * Don't let nex be bigger than the number of extents
5955 * we can have assuming alternating holes and real extents.
5957 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5958 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5960 bmapi_flags = xfs_bmapi_aflag(whichfork);
5961 if (!(iflags & BMV_IF_PREALLOC))
5962 bmapi_flags |= XFS_BMAPI_IGSTATE;
5965 * Allocate enough space to handle "subnex" maps at a time.
5969 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
5971 goto out_unlock_ilock;
5973 bmv->bmv_entries = 0;
5975 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5976 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5984 nmap = (nexleft > subnex) ? subnex : nexleft;
5985 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5986 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5987 bmapi_flags, NULL, 0, map, &nmap,
5991 ASSERT(nmap <= subnex);
5993 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5994 out[cur_ext].bmv_oflags = 0;
5995 if (map[i].br_state == XFS_EXT_UNWRITTEN)
5996 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
5997 else if (map[i].br_startblock == DELAYSTARTBLOCK)
5998 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5999 out[cur_ext].bmv_offset =
6000 XFS_FSB_TO_BB(mp, map[i].br_startoff);
6001 out[cur_ext].bmv_length =
6002 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
6003 out[cur_ext].bmv_unused1 = 0;
6004 out[cur_ext].bmv_unused2 = 0;
6005 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
6006 (map[i].br_startblock != DELAYSTARTBLOCK));
6007 if (map[i].br_startblock == HOLESTARTBLOCK &&
6008 whichfork == XFS_ATTR_FORK) {
6009 /* came to the end of attribute fork */
6010 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
6014 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
6016 map[i].br_startblock))
6021 out[cur_ext].bmv_offset +
6022 out[cur_ext].bmv_length;
6024 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
6028 } while (nmap && nexleft && bmv->bmv_length);
6033 xfs_iunlock_map_shared(ip, lock);
6035 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
6037 for (i = 0; i < cur_ext; i++) {
6038 int full = 0; /* user array is full */
6040 /* format results & advance arg */
6041 error = formatter(&arg, &out[i], &full);
6051 * Check the last inode extent to determine whether this allocation will result
6052 * in blocks being allocated at the end of the file. When we allocate new data
6053 * blocks at the end of the file which do not start at the previous data block,
6054 * we will try to align the new blocks at stripe unit boundaries.
6056 STATIC int /* error */
6058 xfs_inode_t *ip, /* incore inode pointer */
6059 xfs_fileoff_t off, /* file offset in fsblocks */
6060 int whichfork, /* data or attribute fork */
6061 char *aeof) /* return value */
6063 int error; /* error return value */
6064 xfs_ifork_t *ifp; /* inode fork pointer */
6065 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
6066 xfs_extnum_t nextents; /* number of file extents */
6067 xfs_bmbt_irec_t s; /* expanded extent record */
6069 ASSERT(whichfork == XFS_DATA_FORK);
6070 ifp = XFS_IFORK_PTR(ip, whichfork);
6071 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6072 (error = xfs_iread_extents(NULL, ip, whichfork)))
6074 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6075 if (nextents == 0) {
6080 * Go to the last extent
6082 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6083 xfs_bmbt_get_all(lastrec, &s);
6085 * Check we are allocating in the last extent (for delayed allocations)
6086 * or past the last extent for non-delayed allocations.
6088 *aeof = (off >= s.br_startoff &&
6089 off < s.br_startoff + s.br_blockcount &&
6090 isnullstartblock(s.br_startblock)) ||
6091 off >= s.br_startoff + s.br_blockcount;
6096 * Check if the endoff is outside the last extent. If so the caller will grow
6097 * the allocation to a stripe unit boundary.
6101 xfs_inode_t *ip, /* incore inode pointer */
6102 xfs_fileoff_t endoff, /* file offset in fsblocks */
6103 int whichfork, /* data or attribute fork */
6104 int *eof) /* result value */
6106 xfs_fsblock_t blockcount; /* extent block count */
6107 int error; /* error return value */
6108 xfs_ifork_t *ifp; /* inode fork pointer */
6109 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
6110 xfs_extnum_t nextents; /* number of file extents */
6111 xfs_fileoff_t startoff; /* extent starting file offset */
6113 ASSERT(whichfork == XFS_DATA_FORK);
6114 ifp = XFS_IFORK_PTR(ip, whichfork);
6115 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6116 (error = xfs_iread_extents(NULL, ip, whichfork)))
6118 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6119 if (nextents == 0) {
6124 * Go to the last extent
6126 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6127 startoff = xfs_bmbt_get_startoff(lastrec);
6128 blockcount = xfs_bmbt_get_blockcount(lastrec);
6129 *eof = endoff >= startoff + blockcount;
6137 xfs_btree_cur_t *cur,
6147 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6148 bp = cur->bc_bufs[i];
6150 if (XFS_BUF_ADDR(bp) == bno)
6151 break; /* Found it */
6153 if (i == XFS_BTREE_MAXLEVELS)
6156 if (!bp) { /* Chase down all the log items to see if the bp is there */
6157 xfs_log_item_chunk_t *licp;
6161 licp = &tp->t_items;
6162 while (!bp && licp != NULL) {
6163 if (xfs_lic_are_all_free(licp)) {
6164 licp = licp->lic_next;
6167 for (i = 0; i < licp->lic_unused; i++) {
6168 xfs_log_item_desc_t *lidp;
6169 xfs_log_item_t *lip;
6170 xfs_buf_log_item_t *bip;
6173 if (xfs_lic_isfree(licp, i)) {
6177 lidp = xfs_lic_slot(licp, i);
6178 lip = lidp->lid_item;
6179 if (lip->li_type != XFS_LI_BUF)
6182 bip = (xfs_buf_log_item_t *)lip;
6185 if (XFS_BUF_ADDR(lbp) == bno) {
6187 break; /* Found it */
6190 licp = licp->lic_next;
6198 struct xfs_btree_block *block,
6204 __be64 *pp, *thispa; /* pointer to block address */
6205 xfs_bmbt_key_t *prevp, *keyp;
6207 ASSERT(be16_to_cpu(block->bb_level) > 0);
6210 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
6211 dmxr = mp->m_bmap_dmxr[0];
6212 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
6215 ASSERT(be64_to_cpu(prevp->br_startoff) <
6216 be64_to_cpu(keyp->br_startoff));
6221 * Compare the block numbers to see if there are dups.
6224 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
6226 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
6228 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
6230 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
6232 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
6233 if (*thispa == *pp) {
6234 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
6236 (unsigned long long)be64_to_cpu(*thispa));
6237 panic("%s: ptrs are equal in node\n",
6245 * Check that the extents for the inode ip are in the right order in all
6250 xfs_bmap_check_leaf_extents(
6251 xfs_btree_cur_t *cur, /* btree cursor or null */
6252 xfs_inode_t *ip, /* incore inode pointer */
6253 int whichfork) /* data or attr fork */
6255 struct xfs_btree_block *block; /* current btree block */
6256 xfs_fsblock_t bno; /* block # of "block" */
6257 xfs_buf_t *bp; /* buffer for "block" */
6258 int error; /* error return value */
6259 xfs_extnum_t i=0, j; /* index into the extents list */
6260 xfs_ifork_t *ifp; /* fork structure */
6261 int level; /* btree level, for checking */
6262 xfs_mount_t *mp; /* file system mount structure */
6263 __be64 *pp; /* pointer to block address */
6264 xfs_bmbt_rec_t *ep; /* pointer to current extent */
6265 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
6266 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
6269 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6275 ifp = XFS_IFORK_PTR(ip, whichfork);
6276 block = ifp->if_broot;
6278 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6280 level = be16_to_cpu(block->bb_level);
6282 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6283 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
6284 bno = be64_to_cpu(*pp);
6286 ASSERT(bno != NULLDFSBNO);
6287 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6288 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6291 * Go down the tree until leaf level is reached, following the first
6292 * pointer (leftmost) at each level.
6294 while (level-- > 0) {
6295 /* See if buf is in cur first */
6296 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6302 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6303 XFS_BMAP_BTREE_REF)))
6305 block = XFS_BUF_TO_BLOCK(bp);
6306 XFS_WANT_CORRUPTED_GOTO(
6307 xfs_bmap_sanity_check(mp, bp, level),
6313 * Check this block for basic sanity (increasing keys and
6314 * no duplicate blocks).
6317 xfs_check_block(block, mp, 0, 0);
6318 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
6319 bno = be64_to_cpu(*pp);
6320 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
6323 xfs_trans_brelse(NULL, bp);
6328 * Here with bp and block set to the leftmost leaf node in the tree.
6333 * Loop over all leaf nodes checking that all extents are in the right order.
6336 xfs_fsblock_t nextbno;
6337 xfs_extnum_t num_recs;
6340 num_recs = xfs_btree_get_numrecs(block);
6343 * Read-ahead the next leaf block, if any.
6346 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6349 * Check all the extents to make sure they are OK.
6350 * If we had a previous block, the last entry should
6351 * conform with the first entry in this one.
6354 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
6356 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
6357 xfs_bmbt_disk_get_blockcount(&last) <=
6358 xfs_bmbt_disk_get_startoff(ep));
6360 for (j = 1; j < num_recs; j++) {
6361 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
6362 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
6363 xfs_bmbt_disk_get_blockcount(ep) <=
6364 xfs_bmbt_disk_get_startoff(nextp));
6372 xfs_trans_brelse(NULL, bp);
6376 * If we've reached the end, stop.
6378 if (bno == NULLFSBLOCK)
6381 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6387 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6388 XFS_BMAP_BTREE_REF)))
6390 block = XFS_BUF_TO_BLOCK(bp);
6394 xfs_trans_brelse(NULL, bp);
6399 cmn_err(CE_WARN, "%s: at error0", __func__);
6401 xfs_trans_brelse(NULL, bp);
6403 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
6405 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
6411 * Count fsblocks of the given fork.
6414 xfs_bmap_count_blocks(
6415 xfs_trans_t *tp, /* transaction pointer */
6416 xfs_inode_t *ip, /* incore inode */
6417 int whichfork, /* data or attr fork */
6418 int *count) /* out: count of blocks */
6420 struct xfs_btree_block *block; /* current btree block */
6421 xfs_fsblock_t bno; /* block # of "block" */
6422 xfs_ifork_t *ifp; /* fork structure */
6423 int level; /* btree level, for checking */
6424 xfs_mount_t *mp; /* file system mount structure */
6425 __be64 *pp; /* pointer to block address */
6429 ifp = XFS_IFORK_PTR(ip, whichfork);
6430 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6431 xfs_bmap_count_leaves(ifp, 0,
6432 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6438 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6440 block = ifp->if_broot;
6441 level = be16_to_cpu(block->bb_level);
6443 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
6444 bno = be64_to_cpu(*pp);
6445 ASSERT(bno != NULLDFSBNO);
6446 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6447 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6449 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
6450 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6452 return XFS_ERROR(EFSCORRUPTED);
6459 * Recursively walks each level of a btree
6460 * to count total fsblocks is use.
6462 STATIC int /* error */
6463 xfs_bmap_count_tree(
6464 xfs_mount_t *mp, /* file system mount point */
6465 xfs_trans_t *tp, /* transaction pointer */
6466 xfs_ifork_t *ifp, /* inode fork pointer */
6467 xfs_fsblock_t blockno, /* file system block number */
6468 int levelin, /* level in btree */
6469 int *count) /* Count of blocks */
6472 xfs_buf_t *bp, *nbp;
6473 int level = levelin;
6475 xfs_fsblock_t bno = blockno;
6476 xfs_fsblock_t nextbno;
6477 struct xfs_btree_block *block, *nextblock;
6480 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6483 block = XFS_BUF_TO_BLOCK(bp);
6486 /* Not at node above leaves, count this level of nodes */
6487 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6488 while (nextbno != NULLFSBLOCK) {
6489 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6490 0, &nbp, XFS_BMAP_BTREE_REF)))
6493 nextblock = XFS_BUF_TO_BLOCK(nbp);
6494 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
6495 xfs_trans_brelse(tp, nbp);
6498 /* Dive to the next level */
6499 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
6500 bno = be64_to_cpu(*pp);
6501 if (unlikely((error =
6502 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6503 xfs_trans_brelse(tp, bp);
6504 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6505 XFS_ERRLEVEL_LOW, mp);
6506 return XFS_ERROR(EFSCORRUPTED);
6508 xfs_trans_brelse(tp, bp);
6510 /* count all level 1 nodes and their leaves */
6512 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6513 numrecs = be16_to_cpu(block->bb_numrecs);
6514 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
6515 xfs_trans_brelse(tp, bp);
6516 if (nextbno == NULLFSBLOCK)
6519 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6520 XFS_BMAP_BTREE_REF)))
6523 block = XFS_BUF_TO_BLOCK(bp);
6530 * Count leaf blocks given a range of extent records.
6533 xfs_bmap_count_leaves(
6541 for (b = 0; b < numrecs; b++) {
6542 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
6543 *count += xfs_bmbt_get_blockcount(frp);
6548 * Count leaf blocks given a range of extent records originally
6552 xfs_bmap_disk_count_leaves(
6553 struct xfs_mount *mp,
6554 struct xfs_btree_block *block,
6559 xfs_bmbt_rec_t *frp;
6561 for (b = 1; b <= numrecs; b++) {
6562 frp = XFS_BMBT_REC_ADDR(mp, block, b);
6563 *count += xfs_bmbt_disk_get_blockcount(frp);