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(
397 xfs_bmbt_block_t *block,
402 * Bmap internal routines.
406 * Called from xfs_bmap_add_attrfork to handle btree format files.
408 STATIC int /* error */
409 xfs_bmap_add_attrfork_btree(
410 xfs_trans_t *tp, /* transaction pointer */
411 xfs_inode_t *ip, /* incore inode pointer */
412 xfs_fsblock_t *firstblock, /* first block allocated */
413 xfs_bmap_free_t *flist, /* blocks to free at commit */
414 int *flags) /* inode logging flags */
416 xfs_btree_cur_t *cur; /* btree cursor */
417 int error; /* error return value */
418 xfs_mount_t *mp; /* file system mount struct */
419 int stat; /* newroot status */
422 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
423 *flags |= XFS_ILOG_DBROOT;
425 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
427 cur->bc_private.b.flist = flist;
428 cur->bc_private.b.firstblock = *firstblock;
429 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
431 ASSERT(stat == 1); /* must be at least one entry */
432 if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
435 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
436 return XFS_ERROR(ENOSPC);
438 *firstblock = cur->bc_private.b.firstblock;
439 cur->bc_private.b.allocated = 0;
440 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
444 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
449 * Called from xfs_bmap_add_attrfork to handle extents format files.
451 STATIC int /* error */
452 xfs_bmap_add_attrfork_extents(
453 xfs_trans_t *tp, /* transaction pointer */
454 xfs_inode_t *ip, /* incore inode pointer */
455 xfs_fsblock_t *firstblock, /* first block allocated */
456 xfs_bmap_free_t *flist, /* blocks to free at commit */
457 int *flags) /* inode logging flags */
459 xfs_btree_cur_t *cur; /* bmap btree cursor */
460 int error; /* error return value */
462 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
465 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
466 flags, XFS_DATA_FORK);
468 cur->bc_private.b.allocated = 0;
469 xfs_btree_del_cursor(cur,
470 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
476 * Called from xfs_bmap_add_attrfork to handle local format files.
478 STATIC int /* error */
479 xfs_bmap_add_attrfork_local(
480 xfs_trans_t *tp, /* transaction pointer */
481 xfs_inode_t *ip, /* incore inode pointer */
482 xfs_fsblock_t *firstblock, /* first block allocated */
483 xfs_bmap_free_t *flist, /* blocks to free at commit */
484 int *flags) /* inode logging flags */
486 xfs_da_args_t dargs; /* args for dir/attr code */
487 int error; /* error return value */
488 xfs_mount_t *mp; /* mount structure pointer */
490 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
492 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
494 memset(&dargs, 0, sizeof(dargs));
496 dargs.firstblock = firstblock;
498 dargs.total = mp->m_dirblkfsbs;
499 dargs.whichfork = XFS_DATA_FORK;
501 error = xfs_dir2_sf_to_block(&dargs);
503 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
509 * Called by xfs_bmapi to update file extent records and the btree
510 * after allocating space (or doing a delayed allocation).
512 STATIC int /* error */
514 xfs_inode_t *ip, /* incore inode pointer */
515 xfs_extnum_t idx, /* extent number to update/insert */
516 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
517 xfs_bmbt_irec_t *new, /* new data to add to file extents */
518 xfs_fsblock_t *first, /* pointer to firstblock variable */
519 xfs_bmap_free_t *flist, /* list of extents to be freed */
520 int *logflagsp, /* inode logging flags */
521 xfs_extdelta_t *delta, /* Change made to incore extents */
522 int whichfork, /* data or attr fork */
523 int rsvd) /* OK to use reserved data blocks */
525 xfs_btree_cur_t *cur; /* btree cursor or null */
526 xfs_filblks_t da_new; /* new count del alloc blocks used */
527 xfs_filblks_t da_old; /* old count del alloc blocks used */
528 int error; /* error return value */
529 xfs_ifork_t *ifp; /* inode fork ptr */
530 int logflags; /* returned value */
531 xfs_extnum_t nextents; /* number of extents in file now */
533 XFS_STATS_INC(xs_add_exlist);
535 ifp = XFS_IFORK_PTR(ip, whichfork);
536 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
537 ASSERT(idx <= nextents);
541 * This is the first extent added to a new/empty file.
542 * Special case this one, so other routines get to assume there are
543 * already extents in the list.
546 XFS_BMAP_TRACE_INSERT("insert empty", ip, 0, 1, new, NULL,
548 xfs_iext_insert(ifp, 0, 1, new);
551 if (!ISNULLSTARTBLOCK(new->br_startblock)) {
552 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
553 logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
556 /* DELTA: single new extent */
558 if (delta->xed_startoff > new->br_startoff)
559 delta->xed_startoff = new->br_startoff;
560 if (delta->xed_blockcount <
561 new->br_startoff + new->br_blockcount)
562 delta->xed_blockcount = new->br_startoff +
567 * Any kind of new delayed allocation goes here.
569 else if (ISNULLSTARTBLOCK(new->br_startblock)) {
571 ASSERT((cur->bc_private.b.flags &
572 XFS_BTCUR_BPRV_WASDEL) == 0);
573 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
574 &logflags, delta, rsvd)))
578 * Real allocation off the end of the file.
580 else if (idx == nextents) {
582 ASSERT((cur->bc_private.b.flags &
583 XFS_BTCUR_BPRV_WASDEL) == 0);
584 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
585 &logflags, delta, whichfork)))
588 xfs_bmbt_irec_t prev; /* old extent at offset idx */
591 * Get the record referred to by idx.
593 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
595 * If it's a real allocation record, and the new allocation ends
596 * after the start of the referred to record, then we're filling
597 * in a delayed or unwritten allocation with a real one, or
598 * converting real back to unwritten.
600 if (!ISNULLSTARTBLOCK(new->br_startblock) &&
601 new->br_startoff + new->br_blockcount > prev.br_startoff) {
602 if (prev.br_state != XFS_EXT_UNWRITTEN &&
603 ISNULLSTARTBLOCK(prev.br_startblock)) {
604 da_old = STARTBLOCKVAL(prev.br_startblock);
606 ASSERT(cur->bc_private.b.flags &
607 XFS_BTCUR_BPRV_WASDEL);
608 if ((error = xfs_bmap_add_extent_delay_real(ip,
609 idx, &cur, new, &da_new, first, flist,
610 &logflags, delta, rsvd)))
612 } else if (new->br_state == XFS_EXT_NORM) {
613 ASSERT(new->br_state == XFS_EXT_NORM);
614 if ((error = xfs_bmap_add_extent_unwritten_real(
615 ip, idx, &cur, new, &logflags, delta)))
618 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
619 if ((error = xfs_bmap_add_extent_unwritten_real(
620 ip, idx, &cur, new, &logflags, delta)))
623 ASSERT(*curp == cur || *curp == NULL);
626 * Otherwise we're filling in a hole with an allocation.
630 ASSERT((cur->bc_private.b.flags &
631 XFS_BTCUR_BPRV_WASDEL) == 0);
632 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
633 new, &logflags, delta, whichfork)))
638 ASSERT(*curp == cur || *curp == NULL);
640 * Convert to a btree if necessary.
642 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
643 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
644 int tmp_logflags; /* partial log flag return val */
647 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
648 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
649 logflags |= tmp_logflags;
654 * Adjust for changes in reserved delayed indirect blocks.
655 * Nothing to do for disk quotas here.
657 if (da_old || da_new) {
662 nblks += cur->bc_private.b.allocated;
663 ASSERT(nblks <= da_old);
665 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
666 (int64_t)(da_old - nblks), rsvd);
669 * Clear out the allocated field, done with it now in any case.
672 cur->bc_private.b.allocated = 0;
678 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
680 *logflagsp = logflags;
685 * Called by xfs_bmap_add_extent to handle cases converting a delayed
686 * allocation to a real allocation.
688 STATIC int /* error */
689 xfs_bmap_add_extent_delay_real(
690 xfs_inode_t *ip, /* incore inode pointer */
691 xfs_extnum_t idx, /* extent number to update/insert */
692 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
693 xfs_bmbt_irec_t *new, /* new data to add to file extents */
694 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
695 xfs_fsblock_t *first, /* pointer to firstblock variable */
696 xfs_bmap_free_t *flist, /* list of extents to be freed */
697 int *logflagsp, /* inode logging flags */
698 xfs_extdelta_t *delta, /* Change made to incore extents */
699 int rsvd) /* OK to use reserved data block allocation */
701 xfs_btree_cur_t *cur; /* btree cursor */
702 int diff; /* temp value */
703 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
704 int error; /* error return value */
705 int i; /* temp state */
706 xfs_ifork_t *ifp; /* inode fork pointer */
707 xfs_fileoff_t new_endoff; /* end offset of new entry */
708 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
709 /* left is 0, right is 1, prev is 2 */
710 int rval=0; /* return value (logging flags) */
711 int state = 0;/* state bits, accessed thru macros */
712 xfs_filblks_t temp=0; /* value for dnew calculations */
713 xfs_filblks_t temp2=0;/* value for dnew calculations */
714 int tmp_rval; /* partial logging flags */
715 enum { /* bit number definitions for state */
716 LEFT_CONTIG, RIGHT_CONTIG,
717 LEFT_FILLING, RIGHT_FILLING,
718 LEFT_DELAY, RIGHT_DELAY,
719 LEFT_VALID, RIGHT_VALID
725 #define MASK(b) (1 << (b))
726 #define MASK2(a,b) (MASK(a) | MASK(b))
727 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
728 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
729 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
730 #define STATE_TEST(b) (state & MASK(b))
731 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
732 ((state &= ~MASK(b)), 0))
733 #define SWITCH_STATE \
734 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
737 * Set up a bunch of variables to make the tests simpler.
740 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
741 ep = xfs_iext_get_ext(ifp, idx);
742 xfs_bmbt_get_all(ep, &PREV);
743 new_endoff = new->br_startoff + new->br_blockcount;
744 ASSERT(PREV.br_startoff <= new->br_startoff);
745 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
747 * Set flags determining what part of the previous delayed allocation
748 * extent is being replaced by a real allocation.
750 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
751 STATE_SET(RIGHT_FILLING,
752 PREV.br_startoff + PREV.br_blockcount == new_endoff);
754 * Check and set flags if this segment has a left neighbor.
755 * Don't set contiguous if the combined extent would be too large.
757 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
758 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
759 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
761 STATE_SET(LEFT_CONTIG,
762 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
763 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
764 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
765 LEFT.br_state == new->br_state &&
766 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
768 * Check and set flags if this segment has a right neighbor.
769 * Don't set contiguous if the combined extent would be too large.
770 * Also check for all-three-contiguous being too large.
772 if (STATE_SET_TEST(RIGHT_VALID,
774 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
775 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
776 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
778 STATE_SET(RIGHT_CONTIG,
779 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
780 new_endoff == RIGHT.br_startoff &&
781 new->br_startblock + new->br_blockcount ==
782 RIGHT.br_startblock &&
783 new->br_state == RIGHT.br_state &&
784 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
785 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
786 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
787 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
791 * Switch out based on the FILLING and CONTIG state bits.
793 switch (SWITCH_STATE) {
795 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
797 * Filling in all of a previously delayed allocation extent.
798 * The left and right neighbors are both contiguous with new.
800 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
802 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
803 LEFT.br_blockcount + PREV.br_blockcount +
804 RIGHT.br_blockcount);
805 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
807 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
808 xfs_iext_remove(ifp, idx, 2);
809 ip->i_df.if_lastex = idx - 1;
810 ip->i_d.di_nextents--;
812 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
814 rval = XFS_ILOG_CORE;
815 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
817 RIGHT.br_blockcount, &i)))
820 if ((error = xfs_bmbt_delete(cur, &i)))
823 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
826 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
830 RIGHT.br_blockcount, LEFT.br_state)))
834 /* DELTA: Three in-core extents are replaced by one. */
835 temp = LEFT.br_startoff;
836 temp2 = LEFT.br_blockcount +
841 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
843 * Filling in all of a previously delayed allocation extent.
844 * The left neighbor is contiguous, the right is not.
846 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
848 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
849 LEFT.br_blockcount + PREV.br_blockcount);
850 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
852 ip->i_df.if_lastex = idx - 1;
853 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
854 xfs_iext_remove(ifp, idx, 1);
856 rval = XFS_ILOG_DEXT;
859 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
860 LEFT.br_startblock, LEFT.br_blockcount,
864 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
867 PREV.br_blockcount, LEFT.br_state)))
871 /* DELTA: Two in-core extents are replaced by one. */
872 temp = LEFT.br_startoff;
873 temp2 = LEFT.br_blockcount +
877 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
879 * Filling in all of a previously delayed allocation extent.
880 * The right neighbor is contiguous, the left is not.
882 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
883 xfs_bmbt_set_startblock(ep, new->br_startblock);
884 xfs_bmbt_set_blockcount(ep,
885 PREV.br_blockcount + RIGHT.br_blockcount);
886 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
887 ip->i_df.if_lastex = idx;
888 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
889 xfs_iext_remove(ifp, idx + 1, 1);
891 rval = XFS_ILOG_DEXT;
894 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
896 RIGHT.br_blockcount, &i)))
899 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
902 RIGHT.br_blockcount, PREV.br_state)))
906 /* DELTA: Two in-core extents are replaced by one. */
907 temp = PREV.br_startoff;
908 temp2 = PREV.br_blockcount +
912 case MASK2(LEFT_FILLING, RIGHT_FILLING):
914 * Filling in all of a previously delayed allocation extent.
915 * Neither the left nor right neighbors are contiguous with
918 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
919 xfs_bmbt_set_startblock(ep, new->br_startblock);
920 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
921 ip->i_df.if_lastex = idx;
922 ip->i_d.di_nextents++;
924 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
926 rval = XFS_ILOG_CORE;
927 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
928 new->br_startblock, new->br_blockcount,
932 cur->bc_rec.b.br_state = XFS_EXT_NORM;
933 if ((error = xfs_bmbt_insert(cur, &i)))
938 /* DELTA: The in-core extent described by new changed type. */
939 temp = new->br_startoff;
940 temp2 = new->br_blockcount;
943 case MASK2(LEFT_FILLING, LEFT_CONTIG):
945 * Filling in the first part of a previous delayed allocation.
946 * The left neighbor is contiguous.
948 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
949 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
950 LEFT.br_blockcount + new->br_blockcount);
951 xfs_bmbt_set_startoff(ep,
952 PREV.br_startoff + new->br_blockcount);
953 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
954 temp = PREV.br_blockcount - new->br_blockcount;
955 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
956 xfs_bmbt_set_blockcount(ep, temp);
957 ip->i_df.if_lastex = idx - 1;
959 rval = XFS_ILOG_DEXT;
962 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
963 LEFT.br_startblock, LEFT.br_blockcount,
967 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
974 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
975 STARTBLOCKVAL(PREV.br_startblock));
976 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
977 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
979 /* DELTA: The boundary between two in-core extents moved. */
980 temp = LEFT.br_startoff;
981 temp2 = LEFT.br_blockcount +
985 case MASK(LEFT_FILLING):
987 * Filling in the first part of a previous delayed allocation.
988 * The left neighbor is not contiguous.
990 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
991 xfs_bmbt_set_startoff(ep, new_endoff);
992 temp = PREV.br_blockcount - new->br_blockcount;
993 xfs_bmbt_set_blockcount(ep, temp);
994 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
996 xfs_iext_insert(ifp, idx, 1, new);
997 ip->i_df.if_lastex = idx;
998 ip->i_d.di_nextents++;
1000 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1002 rval = XFS_ILOG_CORE;
1003 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1004 new->br_startblock, new->br_blockcount,
1008 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1009 if ((error = xfs_bmbt_insert(cur, &i)))
1013 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1014 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1015 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1016 first, flist, &cur, 1, &tmp_rval,
1022 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1023 STARTBLOCKVAL(PREV.br_startblock) -
1024 (cur ? cur->bc_private.b.allocated : 0));
1025 ep = xfs_iext_get_ext(ifp, idx + 1);
1026 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1027 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx + 1, XFS_DATA_FORK);
1029 /* DELTA: One in-core extent is split in two. */
1030 temp = PREV.br_startoff;
1031 temp2 = PREV.br_blockcount;
1034 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1036 * Filling in the last part of a previous delayed allocation.
1037 * The right neighbor is contiguous with the new allocation.
1039 temp = PREV.br_blockcount - new->br_blockcount;
1040 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
1041 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
1042 xfs_bmbt_set_blockcount(ep, temp);
1043 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1044 new->br_startoff, new->br_startblock,
1045 new->br_blockcount + RIGHT.br_blockcount,
1047 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
1048 ip->i_df.if_lastex = idx + 1;
1050 rval = XFS_ILOG_DEXT;
1053 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1054 RIGHT.br_startblock,
1055 RIGHT.br_blockcount, &i)))
1058 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1060 new->br_blockcount +
1061 RIGHT.br_blockcount,
1065 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1066 STARTBLOCKVAL(PREV.br_startblock));
1067 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1068 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
1070 /* DELTA: The boundary between two in-core extents moved. */
1071 temp = PREV.br_startoff;
1072 temp2 = PREV.br_blockcount +
1073 RIGHT.br_blockcount;
1076 case MASK(RIGHT_FILLING):
1078 * Filling in the last part of a previous delayed allocation.
1079 * The right neighbor is not contiguous.
1081 temp = PREV.br_blockcount - new->br_blockcount;
1082 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1083 xfs_bmbt_set_blockcount(ep, temp);
1084 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1086 xfs_iext_insert(ifp, idx + 1, 1, new);
1087 ip->i_df.if_lastex = idx + 1;
1088 ip->i_d.di_nextents++;
1090 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1092 rval = XFS_ILOG_CORE;
1093 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1094 new->br_startblock, new->br_blockcount,
1098 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1099 if ((error = xfs_bmbt_insert(cur, &i)))
1103 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1104 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1105 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1106 first, flist, &cur, 1, &tmp_rval,
1112 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1113 STARTBLOCKVAL(PREV.br_startblock) -
1114 (cur ? cur->bc_private.b.allocated : 0));
1115 ep = xfs_iext_get_ext(ifp, idx);
1116 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1117 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1119 /* DELTA: One in-core extent is split in two. */
1120 temp = PREV.br_startoff;
1121 temp2 = PREV.br_blockcount;
1126 * Filling in the middle part of a previous delayed allocation.
1127 * Contiguity is impossible here.
1128 * This case is avoided almost all the time.
1130 temp = new->br_startoff - PREV.br_startoff;
1131 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
1132 xfs_bmbt_set_blockcount(ep, temp);
1134 r[1].br_state = PREV.br_state;
1135 r[1].br_startblock = 0;
1136 r[1].br_startoff = new_endoff;
1137 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1138 r[1].br_blockcount = temp2;
1139 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
1141 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1142 ip->i_df.if_lastex = idx + 1;
1143 ip->i_d.di_nextents++;
1145 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1147 rval = XFS_ILOG_CORE;
1148 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1149 new->br_startblock, new->br_blockcount,
1153 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1154 if ((error = xfs_bmbt_insert(cur, &i)))
1158 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1159 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1160 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1161 first, flist, &cur, 1, &tmp_rval,
1167 temp = xfs_bmap_worst_indlen(ip, temp);
1168 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1169 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
1170 (cur ? cur->bc_private.b.allocated : 0));
1172 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
1174 * Ick gross gag me with a spoon.
1176 ASSERT(0); /* want to see if this ever happens! */
1182 !xfs_mod_incore_sb(ip->i_mount,
1183 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1190 !xfs_mod_incore_sb(ip->i_mount,
1191 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1196 ep = xfs_iext_get_ext(ifp, idx);
1197 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1198 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
1199 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
1200 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1201 NULLSTARTBLOCK((int)temp2));
1202 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
1203 *dnew = temp + temp2;
1204 /* DELTA: One in-core extent is split in three. */
1205 temp = PREV.br_startoff;
1206 temp2 = PREV.br_blockcount;
1209 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1210 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1211 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1212 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1213 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1214 case MASK(LEFT_CONTIG):
1215 case MASK(RIGHT_CONTIG):
1217 * These cases are all impossible.
1224 if (delta->xed_startoff > temp)
1225 delta->xed_startoff = temp;
1226 if (delta->xed_blockcount < temp2)
1227 delta->xed_blockcount = temp2;
1241 #undef STATE_SET_TEST
1246 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1247 * allocation to a real allocation or vice versa.
1249 STATIC int /* error */
1250 xfs_bmap_add_extent_unwritten_real(
1251 xfs_inode_t *ip, /* incore inode pointer */
1252 xfs_extnum_t idx, /* extent number to update/insert */
1253 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1254 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1255 int *logflagsp, /* inode logging flags */
1256 xfs_extdelta_t *delta) /* Change made to incore extents */
1258 xfs_btree_cur_t *cur; /* btree cursor */
1259 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1260 int error; /* error return value */
1261 int i; /* temp state */
1262 xfs_ifork_t *ifp; /* inode fork pointer */
1263 xfs_fileoff_t new_endoff; /* end offset of new entry */
1264 xfs_exntst_t newext; /* new extent state */
1265 xfs_exntst_t oldext; /* old extent state */
1266 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1267 /* left is 0, right is 1, prev is 2 */
1268 int rval=0; /* return value (logging flags) */
1269 int state = 0;/* state bits, accessed thru macros */
1270 xfs_filblks_t temp=0;
1271 xfs_filblks_t temp2=0;
1272 enum { /* bit number definitions for state */
1273 LEFT_CONTIG, RIGHT_CONTIG,
1274 LEFT_FILLING, RIGHT_FILLING,
1275 LEFT_DELAY, RIGHT_DELAY,
1276 LEFT_VALID, RIGHT_VALID
1282 #define MASK(b) (1 << (b))
1283 #define MASK2(a,b) (MASK(a) | MASK(b))
1284 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1285 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1286 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1287 #define STATE_TEST(b) (state & MASK(b))
1288 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1289 ((state &= ~MASK(b)), 0))
1290 #define SWITCH_STATE \
1291 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1294 * Set up a bunch of variables to make the tests simpler.
1298 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1299 ep = xfs_iext_get_ext(ifp, idx);
1300 xfs_bmbt_get_all(ep, &PREV);
1301 newext = new->br_state;
1302 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1303 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1304 ASSERT(PREV.br_state == oldext);
1305 new_endoff = new->br_startoff + new->br_blockcount;
1306 ASSERT(PREV.br_startoff <= new->br_startoff);
1307 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1309 * Set flags determining what part of the previous oldext allocation
1310 * extent is being replaced by a newext allocation.
1312 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
1313 STATE_SET(RIGHT_FILLING,
1314 PREV.br_startoff + PREV.br_blockcount == new_endoff);
1316 * Check and set flags if this segment has a left neighbor.
1317 * Don't set contiguous if the combined extent would be too large.
1319 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1320 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1321 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
1323 STATE_SET(LEFT_CONTIG,
1324 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1325 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1326 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1327 LEFT.br_state == newext &&
1328 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1330 * Check and set flags if this segment has a right neighbor.
1331 * Don't set contiguous if the combined extent would be too large.
1332 * Also check for all-three-contiguous being too large.
1334 if (STATE_SET_TEST(RIGHT_VALID,
1336 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
1337 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1338 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
1340 STATE_SET(RIGHT_CONTIG,
1341 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1342 new_endoff == RIGHT.br_startoff &&
1343 new->br_startblock + new->br_blockcount ==
1344 RIGHT.br_startblock &&
1345 newext == RIGHT.br_state &&
1346 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1347 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
1348 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
1349 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1352 * Switch out based on the FILLING and CONTIG state bits.
1354 switch (SWITCH_STATE) {
1356 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1358 * Setting all of a previous oldext extent to newext.
1359 * The left and right neighbors are both contiguous with new.
1361 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
1363 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1364 LEFT.br_blockcount + PREV.br_blockcount +
1365 RIGHT.br_blockcount);
1366 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
1368 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
1369 xfs_iext_remove(ifp, idx, 2);
1370 ip->i_df.if_lastex = idx - 1;
1371 ip->i_d.di_nextents -= 2;
1373 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1375 rval = XFS_ILOG_CORE;
1376 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1377 RIGHT.br_startblock,
1378 RIGHT.br_blockcount, &i)))
1381 if ((error = xfs_bmbt_delete(cur, &i)))
1384 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1387 if ((error = xfs_bmbt_delete(cur, &i)))
1390 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1393 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1395 LEFT.br_blockcount + PREV.br_blockcount +
1396 RIGHT.br_blockcount, LEFT.br_state)))
1399 /* DELTA: Three in-core extents are replaced by one. */
1400 temp = LEFT.br_startoff;
1401 temp2 = LEFT.br_blockcount +
1402 PREV.br_blockcount +
1403 RIGHT.br_blockcount;
1406 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
1408 * Setting all of a previous oldext extent to newext.
1409 * The left neighbor is contiguous, the right is not.
1411 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
1413 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1414 LEFT.br_blockcount + PREV.br_blockcount);
1415 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
1417 ip->i_df.if_lastex = idx - 1;
1418 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
1419 xfs_iext_remove(ifp, idx, 1);
1420 ip->i_d.di_nextents--;
1422 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1424 rval = XFS_ILOG_CORE;
1425 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1426 PREV.br_startblock, PREV.br_blockcount,
1430 if ((error = xfs_bmbt_delete(cur, &i)))
1433 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1436 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1438 LEFT.br_blockcount + PREV.br_blockcount,
1442 /* DELTA: Two in-core extents are replaced by one. */
1443 temp = LEFT.br_startoff;
1444 temp2 = LEFT.br_blockcount +
1448 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
1450 * Setting all of a previous oldext extent to newext.
1451 * The right neighbor is contiguous, the left is not.
1453 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx,
1455 xfs_bmbt_set_blockcount(ep,
1456 PREV.br_blockcount + RIGHT.br_blockcount);
1457 xfs_bmbt_set_state(ep, newext);
1458 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx,
1460 ip->i_df.if_lastex = idx;
1461 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
1462 xfs_iext_remove(ifp, idx + 1, 1);
1463 ip->i_d.di_nextents--;
1465 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1467 rval = XFS_ILOG_CORE;
1468 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1469 RIGHT.br_startblock,
1470 RIGHT.br_blockcount, &i)))
1473 if ((error = xfs_bmbt_delete(cur, &i)))
1476 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1479 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1481 new->br_blockcount + RIGHT.br_blockcount,
1485 /* DELTA: Two in-core extents are replaced by one. */
1486 temp = PREV.br_startoff;
1487 temp2 = PREV.br_blockcount +
1488 RIGHT.br_blockcount;
1491 case MASK2(LEFT_FILLING, RIGHT_FILLING):
1493 * Setting all of a previous oldext extent to newext.
1494 * Neither the left nor right neighbors are contiguous with
1497 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx,
1499 xfs_bmbt_set_state(ep, newext);
1500 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx,
1502 ip->i_df.if_lastex = idx;
1504 rval = XFS_ILOG_DEXT;
1507 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1508 new->br_startblock, new->br_blockcount,
1512 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1513 new->br_startblock, new->br_blockcount,
1517 /* DELTA: The in-core extent described by new changed type. */
1518 temp = new->br_startoff;
1519 temp2 = new->br_blockcount;
1522 case MASK2(LEFT_FILLING, LEFT_CONTIG):
1524 * Setting the first part of a previous oldext extent to newext.
1525 * The left neighbor is contiguous.
1527 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1,
1529 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1530 LEFT.br_blockcount + new->br_blockcount);
1531 xfs_bmbt_set_startoff(ep,
1532 PREV.br_startoff + new->br_blockcount);
1533 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1,
1535 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx,
1537 xfs_bmbt_set_startblock(ep,
1538 new->br_startblock + new->br_blockcount);
1539 xfs_bmbt_set_blockcount(ep,
1540 PREV.br_blockcount - new->br_blockcount);
1541 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx,
1543 ip->i_df.if_lastex = idx - 1;
1545 rval = XFS_ILOG_DEXT;
1548 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1549 PREV.br_startblock, PREV.br_blockcount,
1553 if ((error = xfs_bmbt_update(cur,
1554 PREV.br_startoff + new->br_blockcount,
1555 PREV.br_startblock + new->br_blockcount,
1556 PREV.br_blockcount - new->br_blockcount,
1559 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1561 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1563 LEFT.br_blockcount + new->br_blockcount,
1567 /* DELTA: The boundary between two in-core extents moved. */
1568 temp = LEFT.br_startoff;
1569 temp2 = LEFT.br_blockcount +
1573 case MASK(LEFT_FILLING):
1575 * Setting the first part of a previous oldext extent to newext.
1576 * The left neighbor is not contiguous.
1578 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1579 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1580 xfs_bmbt_set_startoff(ep, new_endoff);
1581 xfs_bmbt_set_blockcount(ep,
1582 PREV.br_blockcount - new->br_blockcount);
1583 xfs_bmbt_set_startblock(ep,
1584 new->br_startblock + new->br_blockcount);
1585 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1586 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
1588 xfs_iext_insert(ifp, idx, 1, new);
1589 ip->i_df.if_lastex = idx;
1590 ip->i_d.di_nextents++;
1592 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1594 rval = XFS_ILOG_CORE;
1595 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1596 PREV.br_startblock, PREV.br_blockcount,
1600 if ((error = xfs_bmbt_update(cur,
1601 PREV.br_startoff + new->br_blockcount,
1602 PREV.br_startblock + new->br_blockcount,
1603 PREV.br_blockcount - new->br_blockcount,
1606 cur->bc_rec.b = *new;
1607 if ((error = xfs_bmbt_insert(cur, &i)))
1611 /* DELTA: One in-core extent is split in two. */
1612 temp = PREV.br_startoff;
1613 temp2 = PREV.br_blockcount;
1616 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1618 * Setting the last part of a previous oldext extent to newext.
1619 * The right neighbor is contiguous with the new allocation.
1621 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx,
1623 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1,
1625 xfs_bmbt_set_blockcount(ep,
1626 PREV.br_blockcount - new->br_blockcount);
1627 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx,
1629 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1630 new->br_startoff, new->br_startblock,
1631 new->br_blockcount + RIGHT.br_blockcount, newext);
1632 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1,
1634 ip->i_df.if_lastex = idx + 1;
1636 rval = XFS_ILOG_DEXT;
1639 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1641 PREV.br_blockcount, &i)))
1644 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1646 PREV.br_blockcount - new->br_blockcount,
1649 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1651 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1653 new->br_blockcount + RIGHT.br_blockcount,
1657 /* DELTA: The boundary between two in-core extents moved. */
1658 temp = PREV.br_startoff;
1659 temp2 = PREV.br_blockcount +
1660 RIGHT.br_blockcount;
1663 case MASK(RIGHT_FILLING):
1665 * Setting the last part of a previous oldext extent to newext.
1666 * The right neighbor is not contiguous.
1668 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1669 xfs_bmbt_set_blockcount(ep,
1670 PREV.br_blockcount - new->br_blockcount);
1671 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1672 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1674 xfs_iext_insert(ifp, idx + 1, 1, new);
1675 ip->i_df.if_lastex = idx + 1;
1676 ip->i_d.di_nextents++;
1678 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1680 rval = XFS_ILOG_CORE;
1681 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1682 PREV.br_startblock, PREV.br_blockcount,
1686 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1688 PREV.br_blockcount - new->br_blockcount,
1691 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1692 new->br_startblock, new->br_blockcount,
1696 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1697 if ((error = xfs_bmbt_insert(cur, &i)))
1701 /* DELTA: One in-core extent is split in two. */
1702 temp = PREV.br_startoff;
1703 temp2 = PREV.br_blockcount;
1708 * Setting the middle part of a previous oldext extent to
1709 * newext. Contiguity is impossible here.
1710 * One extent becomes three extents.
1712 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
1713 xfs_bmbt_set_blockcount(ep,
1714 new->br_startoff - PREV.br_startoff);
1715 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
1717 r[1].br_startoff = new_endoff;
1718 r[1].br_blockcount =
1719 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1720 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1721 r[1].br_state = oldext;
1722 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
1724 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1725 ip->i_df.if_lastex = idx + 1;
1726 ip->i_d.di_nextents += 2;
1728 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1730 rval = XFS_ILOG_CORE;
1731 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1732 PREV.br_startblock, PREV.br_blockcount,
1736 /* new right extent - oldext */
1737 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1738 r[1].br_startblock, r[1].br_blockcount,
1741 /* new left extent - oldext */
1742 PREV.br_blockcount =
1743 new->br_startoff - PREV.br_startoff;
1744 cur->bc_rec.b = PREV;
1745 if ((error = xfs_bmbt_insert(cur, &i)))
1748 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1751 /* new middle extent - newext */
1752 cur->bc_rec.b = *new;
1753 if ((error = xfs_bmbt_insert(cur, &i)))
1757 /* DELTA: One in-core extent is split in three. */
1758 temp = PREV.br_startoff;
1759 temp2 = PREV.br_blockcount;
1762 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1763 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1764 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1765 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1766 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1767 case MASK(LEFT_CONTIG):
1768 case MASK(RIGHT_CONTIG):
1770 * These cases are all impossible.
1777 if (delta->xed_startoff > temp)
1778 delta->xed_startoff = temp;
1779 if (delta->xed_blockcount < temp2)
1780 delta->xed_blockcount = temp2;
1794 #undef STATE_SET_TEST
1799 * Called by xfs_bmap_add_extent to handle cases converting a hole
1800 * to a delayed allocation.
1803 STATIC int /* error */
1804 xfs_bmap_add_extent_hole_delay(
1805 xfs_inode_t *ip, /* incore inode pointer */
1806 xfs_extnum_t idx, /* extent number to update/insert */
1807 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1808 int *logflagsp, /* inode logging flags */
1809 xfs_extdelta_t *delta, /* Change made to incore extents */
1810 int rsvd) /* OK to allocate reserved blocks */
1812 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
1813 xfs_ifork_t *ifp; /* inode fork pointer */
1814 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1815 xfs_filblks_t newlen=0; /* new indirect size */
1816 xfs_filblks_t oldlen=0; /* old indirect size */
1817 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1818 int state; /* state bits, accessed thru macros */
1819 xfs_filblks_t temp=0; /* temp for indirect calculations */
1820 xfs_filblks_t temp2=0;
1821 enum { /* bit number definitions for state */
1822 LEFT_CONTIG, RIGHT_CONTIG,
1823 LEFT_DELAY, RIGHT_DELAY,
1824 LEFT_VALID, RIGHT_VALID
1827 #define MASK(b) (1 << (b))
1828 #define MASK2(a,b) (MASK(a) | MASK(b))
1829 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1830 #define STATE_TEST(b) (state & MASK(b))
1831 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1832 ((state &= ~MASK(b)), 0))
1833 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1835 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1836 ep = xfs_iext_get_ext(ifp, idx);
1838 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1840 * Check and set flags if this segment has a left neighbor
1842 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1843 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1844 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1847 * Check and set flags if the current (right) segment exists.
1848 * If it doesn't exist, we're converting the hole at end-of-file.
1850 if (STATE_SET_TEST(RIGHT_VALID,
1852 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1853 xfs_bmbt_get_all(ep, &right);
1854 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1857 * Set contiguity flags on the left and right neighbors.
1858 * Don't let extents get too large, even if the pieces are contiguous.
1860 STATE_SET(LEFT_CONTIG,
1861 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1862 left.br_startoff + left.br_blockcount == new->br_startoff &&
1863 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1864 STATE_SET(RIGHT_CONTIG,
1865 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1866 new->br_startoff + new->br_blockcount == right.br_startoff &&
1867 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1868 (!STATE_TEST(LEFT_CONTIG) ||
1869 (left.br_blockcount + new->br_blockcount +
1870 right.br_blockcount <= MAXEXTLEN)));
1872 * Switch out based on the contiguity flags.
1874 switch (SWITCH_STATE) {
1876 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1878 * New allocation is contiguous with delayed allocations
1879 * on the left and on the right.
1880 * Merge all three into a single extent record.
1882 temp = left.br_blockcount + new->br_blockcount +
1883 right.br_blockcount;
1884 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
1886 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1887 oldlen = STARTBLOCKVAL(left.br_startblock) +
1888 STARTBLOCKVAL(new->br_startblock) +
1889 STARTBLOCKVAL(right.br_startblock);
1890 newlen = xfs_bmap_worst_indlen(ip, temp);
1891 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1892 NULLSTARTBLOCK((int)newlen));
1893 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
1895 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, XFS_DATA_FORK);
1896 xfs_iext_remove(ifp, idx, 1);
1897 ip->i_df.if_lastex = idx - 1;
1898 /* DELTA: Two in-core extents were replaced by one. */
1900 temp = left.br_startoff;
1903 case MASK(LEFT_CONTIG):
1905 * New allocation is contiguous with a delayed allocation
1907 * Merge the new allocation with the left neighbor.
1909 temp = left.br_blockcount + new->br_blockcount;
1910 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1,
1912 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1913 oldlen = STARTBLOCKVAL(left.br_startblock) +
1914 STARTBLOCKVAL(new->br_startblock);
1915 newlen = xfs_bmap_worst_indlen(ip, temp);
1916 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1917 NULLSTARTBLOCK((int)newlen));
1918 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1,
1920 ip->i_df.if_lastex = idx - 1;
1921 /* DELTA: One in-core extent grew into a hole. */
1923 temp = left.br_startoff;
1926 case MASK(RIGHT_CONTIG):
1928 * New allocation is contiguous with a delayed allocation
1930 * Merge the new allocation with the right neighbor.
1932 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, XFS_DATA_FORK);
1933 temp = new->br_blockcount + right.br_blockcount;
1934 oldlen = STARTBLOCKVAL(new->br_startblock) +
1935 STARTBLOCKVAL(right.br_startblock);
1936 newlen = xfs_bmap_worst_indlen(ip, temp);
1937 xfs_bmbt_set_allf(ep, new->br_startoff,
1938 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
1939 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, XFS_DATA_FORK);
1940 ip->i_df.if_lastex = idx;
1941 /* DELTA: One in-core extent grew into a hole. */
1943 temp = new->br_startoff;
1948 * New allocation is not contiguous with another
1949 * delayed allocation.
1950 * Insert a new entry.
1952 oldlen = newlen = 0;
1953 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL,
1955 xfs_iext_insert(ifp, idx, 1, new);
1956 ip->i_df.if_lastex = idx;
1957 /* DELTA: A new in-core extent was added in a hole. */
1958 temp2 = new->br_blockcount;
1959 temp = new->br_startoff;
1962 if (oldlen != newlen) {
1963 ASSERT(oldlen > newlen);
1964 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
1965 (int64_t)(oldlen - newlen), rsvd);
1967 * Nothing to do for disk quota accounting here.
1972 if (delta->xed_startoff > temp)
1973 delta->xed_startoff = temp;
1974 if (delta->xed_blockcount < temp2)
1975 delta->xed_blockcount = temp2;
1983 #undef STATE_SET_TEST
1988 * Called by xfs_bmap_add_extent to handle cases converting a hole
1989 * to a real allocation.
1991 STATIC int /* error */
1992 xfs_bmap_add_extent_hole_real(
1993 xfs_inode_t *ip, /* incore inode pointer */
1994 xfs_extnum_t idx, /* extent number to update/insert */
1995 xfs_btree_cur_t *cur, /* if null, not a btree */
1996 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1997 int *logflagsp, /* inode logging flags */
1998 xfs_extdelta_t *delta, /* Change made to incore extents */
1999 int whichfork) /* data or attr fork */
2001 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
2002 int error; /* error return value */
2003 int i; /* temp state */
2004 xfs_ifork_t *ifp; /* inode fork pointer */
2005 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2006 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2007 int rval=0; /* return value (logging flags) */
2008 int state; /* state bits, accessed thru macros */
2009 xfs_filblks_t temp=0;
2010 xfs_filblks_t temp2=0;
2011 enum { /* bit number definitions for state */
2012 LEFT_CONTIG, RIGHT_CONTIG,
2013 LEFT_DELAY, RIGHT_DELAY,
2014 LEFT_VALID, RIGHT_VALID
2017 #define MASK(b) (1 << (b))
2018 #define MASK2(a,b) (MASK(a) | MASK(b))
2019 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
2020 #define STATE_TEST(b) (state & MASK(b))
2021 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
2022 ((state &= ~MASK(b)), 0))
2023 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
2025 ifp = XFS_IFORK_PTR(ip, whichfork);
2026 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
2027 ep = xfs_iext_get_ext(ifp, idx);
2030 * Check and set flags if this segment has a left neighbor.
2032 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
2033 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
2034 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
2037 * Check and set flags if this segment has a current value.
2038 * Not true if we're inserting into the "hole" at eof.
2040 if (STATE_SET_TEST(RIGHT_VALID,
2042 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
2043 xfs_bmbt_get_all(ep, &right);
2044 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
2047 * We're inserting a real allocation between "left" and "right".
2048 * Set the contiguity flags. Don't let extents get too large.
2050 STATE_SET(LEFT_CONTIG,
2051 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
2052 left.br_startoff + left.br_blockcount == new->br_startoff &&
2053 left.br_startblock + left.br_blockcount == new->br_startblock &&
2054 left.br_state == new->br_state &&
2055 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
2056 STATE_SET(RIGHT_CONTIG,
2057 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
2058 new->br_startoff + new->br_blockcount == right.br_startoff &&
2059 new->br_startblock + new->br_blockcount ==
2060 right.br_startblock &&
2061 new->br_state == right.br_state &&
2062 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2063 (!STATE_TEST(LEFT_CONTIG) ||
2064 left.br_blockcount + new->br_blockcount +
2065 right.br_blockcount <= MAXEXTLEN));
2069 * Select which case we're in here, and implement it.
2071 switch (SWITCH_STATE) {
2073 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
2075 * New allocation is contiguous with real allocations on the
2076 * left and on the right.
2077 * Merge all three into a single extent record.
2079 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
2081 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2082 left.br_blockcount + new->br_blockcount +
2083 right.br_blockcount);
2084 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
2086 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, whichfork);
2087 xfs_iext_remove(ifp, idx, 1);
2088 ifp->if_lastex = idx - 1;
2089 XFS_IFORK_NEXT_SET(ip, whichfork,
2090 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2092 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2094 rval = XFS_ILOG_CORE;
2095 if ((error = xfs_bmbt_lookup_eq(cur,
2097 right.br_startblock,
2098 right.br_blockcount, &i)))
2101 if ((error = xfs_bmbt_delete(cur, &i)))
2104 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2107 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2109 left.br_blockcount +
2110 new->br_blockcount +
2111 right.br_blockcount,
2115 /* DELTA: Two in-core extents were replaced by one. */
2116 temp = left.br_startoff;
2117 temp2 = left.br_blockcount +
2118 new->br_blockcount +
2119 right.br_blockcount;
2122 case MASK(LEFT_CONTIG):
2124 * New allocation is contiguous with a real allocation
2126 * Merge the new allocation with the left neighbor.
2128 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1, whichfork);
2129 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2130 left.br_blockcount + new->br_blockcount);
2131 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1, whichfork);
2132 ifp->if_lastex = idx - 1;
2134 rval = XFS_ILOG_FEXT(whichfork);
2137 if ((error = xfs_bmbt_lookup_eq(cur,
2140 left.br_blockcount, &i)))
2143 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2145 left.br_blockcount +
2150 /* DELTA: One in-core extent grew. */
2151 temp = left.br_startoff;
2152 temp2 = left.br_blockcount +
2156 case MASK(RIGHT_CONTIG):
2158 * New allocation is contiguous with a real allocation
2160 * Merge the new allocation with the right neighbor.
2162 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, whichfork);
2163 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2164 new->br_blockcount + right.br_blockcount,
2166 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, whichfork);
2167 ifp->if_lastex = idx;
2169 rval = XFS_ILOG_FEXT(whichfork);
2172 if ((error = xfs_bmbt_lookup_eq(cur,
2174 right.br_startblock,
2175 right.br_blockcount, &i)))
2178 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2180 new->br_blockcount +
2181 right.br_blockcount,
2185 /* DELTA: One in-core extent grew. */
2186 temp = new->br_startoff;
2187 temp2 = new->br_blockcount +
2188 right.br_blockcount;
2193 * New allocation is not contiguous with another
2195 * Insert a new entry.
2197 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL, whichfork);
2198 xfs_iext_insert(ifp, idx, 1, new);
2199 ifp->if_lastex = idx;
2200 XFS_IFORK_NEXT_SET(ip, whichfork,
2201 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2203 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2205 rval = XFS_ILOG_CORE;
2206 if ((error = xfs_bmbt_lookup_eq(cur,
2209 new->br_blockcount, &i)))
2212 cur->bc_rec.b.br_state = new->br_state;
2213 if ((error = xfs_bmbt_insert(cur, &i)))
2217 /* DELTA: A new extent was added in a hole. */
2218 temp = new->br_startoff;
2219 temp2 = new->br_blockcount;
2224 if (delta->xed_startoff > temp)
2225 delta->xed_startoff = temp;
2226 if (delta->xed_blockcount < temp2)
2227 delta->xed_blockcount = temp2;
2236 #undef STATE_SET_TEST
2241 * Adjust the size of the new extent based on di_extsize and rt extsize.
2244 xfs_bmap_extsize_align(
2246 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2247 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2248 xfs_extlen_t extsz, /* align to this extent size */
2249 int rt, /* is this a realtime inode? */
2250 int eof, /* is extent at end-of-file? */
2251 int delay, /* creating delalloc extent? */
2252 int convert, /* overwriting unwritten extent? */
2253 xfs_fileoff_t *offp, /* in/out: aligned offset */
2254 xfs_extlen_t *lenp) /* in/out: aligned length */
2256 xfs_fileoff_t orig_off; /* original offset */
2257 xfs_extlen_t orig_alen; /* original length */
2258 xfs_fileoff_t orig_end; /* original off+len */
2259 xfs_fileoff_t nexto; /* next file offset */
2260 xfs_fileoff_t prevo; /* previous file offset */
2261 xfs_fileoff_t align_off; /* temp for offset */
2262 xfs_extlen_t align_alen; /* temp for length */
2263 xfs_extlen_t temp; /* temp for calculations */
2268 orig_off = align_off = *offp;
2269 orig_alen = align_alen = *lenp;
2270 orig_end = orig_off + orig_alen;
2273 * If this request overlaps an existing extent, then don't
2274 * attempt to perform any additional alignment.
2276 if (!delay && !eof &&
2277 (orig_off >= gotp->br_startoff) &&
2278 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2283 * If the file offset is unaligned vs. the extent size
2284 * we need to align it. This will be possible unless
2285 * the file was previously written with a kernel that didn't
2286 * perform this alignment, or if a truncate shot us in the
2289 temp = do_mod(orig_off, extsz);
2295 * Same adjustment for the end of the requested area.
2297 if ((temp = (align_alen % extsz))) {
2298 align_alen += extsz - temp;
2301 * If the previous block overlaps with this proposed allocation
2302 * then move the start forward without adjusting the length.
2304 if (prevp->br_startoff != NULLFILEOFF) {
2305 if (prevp->br_startblock == HOLESTARTBLOCK)
2306 prevo = prevp->br_startoff;
2308 prevo = prevp->br_startoff + prevp->br_blockcount;
2311 if (align_off != orig_off && align_off < prevo)
2314 * If the next block overlaps with this proposed allocation
2315 * then move the start back without adjusting the length,
2316 * but not before offset 0.
2317 * This may of course make the start overlap previous block,
2318 * and if we hit the offset 0 limit then the next block
2319 * can still overlap too.
2321 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2322 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2323 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2324 nexto = gotp->br_startoff + gotp->br_blockcount;
2326 nexto = gotp->br_startoff;
2328 nexto = NULLFILEOFF;
2330 align_off + align_alen != orig_end &&
2331 align_off + align_alen > nexto)
2332 align_off = nexto > align_alen ? nexto - align_alen : 0;
2334 * If we're now overlapping the next or previous extent that
2335 * means we can't fit an extsz piece in this hole. Just move
2336 * the start forward to the first valid spot and set
2337 * the length so we hit the end.
2339 if (align_off != orig_off && align_off < prevo)
2341 if (align_off + align_alen != orig_end &&
2342 align_off + align_alen > nexto &&
2343 nexto != NULLFILEOFF) {
2344 ASSERT(nexto > prevo);
2345 align_alen = nexto - align_off;
2349 * If realtime, and the result isn't a multiple of the realtime
2350 * extent size we need to remove blocks until it is.
2352 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2354 * We're not covering the original request, or
2355 * we won't be able to once we fix the length.
2357 if (orig_off < align_off ||
2358 orig_end > align_off + align_alen ||
2359 align_alen - temp < orig_alen)
2360 return XFS_ERROR(EINVAL);
2362 * Try to fix it by moving the start up.
2364 if (align_off + temp <= orig_off) {
2369 * Try to fix it by moving the end in.
2371 else if (align_off + align_alen - temp >= orig_end)
2374 * Set the start to the minimum then trim the length.
2377 align_alen -= orig_off - align_off;
2378 align_off = orig_off;
2379 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2382 * Result doesn't cover the request, fail it.
2384 if (orig_off < align_off || orig_end > align_off + align_alen)
2385 return XFS_ERROR(EINVAL);
2387 ASSERT(orig_off >= align_off);
2388 ASSERT(orig_end <= align_off + align_alen);
2392 if (!eof && gotp->br_startoff != NULLFILEOFF)
2393 ASSERT(align_off + align_alen <= gotp->br_startoff);
2394 if (prevp->br_startoff != NULLFILEOFF)
2395 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2403 #define XFS_ALLOC_GAP_UNITS 4
2407 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2409 xfs_fsblock_t adjust; /* adjustment to block numbers */
2410 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2411 xfs_mount_t *mp; /* mount point structure */
2412 int nullfb; /* true if ap->firstblock isn't set */
2413 int rt; /* true if inode is realtime */
2415 #define ISVALID(x,y) \
2417 (x) < mp->m_sb.sb_rblocks : \
2418 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2419 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2420 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2422 mp = ap->ip->i_mount;
2423 nullfb = ap->firstblock == NULLFSBLOCK;
2424 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2425 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2427 * If allocating at eof, and there's a previous real block,
2428 * try to use it's last block as our starting point.
2430 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2431 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2432 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2433 ap->prevp->br_startblock)) {
2434 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2436 * Adjust for the gap between prevp and us.
2439 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2441 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2445 * If not at eof, then compare the two neighbor blocks.
2446 * Figure out whether either one gives us a good starting point,
2447 * and pick the better one.
2449 else if (!ap->eof) {
2450 xfs_fsblock_t gotbno; /* right side block number */
2451 xfs_fsblock_t gotdiff=0; /* right side difference */
2452 xfs_fsblock_t prevbno; /* left side block number */
2453 xfs_fsblock_t prevdiff=0; /* left side difference */
2456 * If there's a previous (left) block, select a requested
2457 * start block based on it.
2459 if (ap->prevp->br_startoff != NULLFILEOFF &&
2460 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2461 (prevbno = ap->prevp->br_startblock +
2462 ap->prevp->br_blockcount) &&
2463 ISVALID(prevbno, ap->prevp->br_startblock)) {
2465 * Calculate gap to end of previous block.
2467 adjust = prevdiff = ap->off -
2468 (ap->prevp->br_startoff +
2469 ap->prevp->br_blockcount);
2471 * Figure the startblock based on the previous block's
2472 * end and the gap size.
2474 * If the gap is large relative to the piece we're
2475 * allocating, or using it gives us an invalid block
2476 * number, then just use the end of the previous block.
2478 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2479 ISVALID(prevbno + prevdiff,
2480 ap->prevp->br_startblock))
2485 * If the firstblock forbids it, can't use it,
2488 if (!rt && !nullfb &&
2489 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2490 prevbno = NULLFSBLOCK;
2493 * No previous block or can't follow it, just default.
2496 prevbno = NULLFSBLOCK;
2498 * If there's a following (right) block, select a requested
2499 * start block based on it.
2501 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
2503 * Calculate gap to start of next block.
2505 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2507 * Figure the startblock based on the next block's
2508 * start and the gap size.
2510 gotbno = ap->gotp->br_startblock;
2513 * If the gap is large relative to the piece we're
2514 * allocating, or using it gives us an invalid block
2515 * number, then just use the start of the next block
2516 * offset by our length.
2518 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2519 ISVALID(gotbno - gotdiff, gotbno))
2521 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2523 gotdiff += adjust - ap->alen;
2527 * If the firstblock forbids it, can't use it,
2530 if (!rt && !nullfb &&
2531 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2532 gotbno = NULLFSBLOCK;
2535 * No next block, just default.
2538 gotbno = NULLFSBLOCK;
2540 * If both valid, pick the better one, else the only good
2541 * one, else ap->rval is already set (to 0 or the inode block).
2543 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2544 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2545 else if (prevbno != NULLFSBLOCK)
2547 else if (gotbno != NULLFSBLOCK)
2555 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2557 xfs_alloctype_t atype = 0; /* type for allocation routines */
2558 int error; /* error return value */
2559 xfs_mount_t *mp; /* mount point structure */
2560 xfs_extlen_t prod = 0; /* product factor for allocators */
2561 xfs_extlen_t ralen = 0; /* realtime allocation length */
2562 xfs_extlen_t align; /* minimum allocation alignment */
2565 mp = ap->ip->i_mount;
2566 align = xfs_get_extsz_hint(ap->ip);
2567 prod = align / mp->m_sb.sb_rextsize;
2568 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2569 align, 1, ap->eof, 0,
2570 ap->conv, &ap->off, &ap->alen);
2574 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2577 * If the offset & length are not perfectly aligned
2578 * then kill prod, it will just get us in trouble.
2580 if (do_mod(ap->off, align) || ap->alen % align)
2583 * Set ralen to be the actual requested length in rtextents.
2585 ralen = ap->alen / mp->m_sb.sb_rextsize;
2587 * If the old value was close enough to MAXEXTLEN that
2588 * we rounded up to it, cut it back so it's valid again.
2589 * Note that if it's a really large request (bigger than
2590 * MAXEXTLEN), we don't hear about that number, and can't
2591 * adjust the starting point to match it.
2593 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2594 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2596 * If it's an allocation to an empty file at offset 0,
2597 * pick an extent that will space things out in the rt area.
2599 if (ap->eof && ap->off == 0) {
2600 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2602 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2605 ap->rval = rtx * mp->m_sb.sb_rextsize;
2610 xfs_bmap_adjacent(ap);
2613 * Realtime allocation, done through xfs_rtallocate_extent.
2615 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2616 do_div(ap->rval, mp->m_sb.sb_rextsize);
2619 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2620 &ralen, atype, ap->wasdel, prod, &rtb)))
2622 if (rtb == NULLFSBLOCK && prod > 1 &&
2623 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2624 ap->alen, &ralen, atype,
2625 ap->wasdel, 1, &rtb)))
2628 if (ap->rval != NULLFSBLOCK) {
2629 ap->rval *= mp->m_sb.sb_rextsize;
2630 ralen *= mp->m_sb.sb_rextsize;
2632 ap->ip->i_d.di_nblocks += ralen;
2633 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2635 ap->ip->i_delayed_blks -= ralen;
2637 * Adjust the disk quota also. This was reserved
2640 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2641 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2642 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2651 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2653 xfs_mount_t *mp; /* mount point structure */
2654 xfs_alloctype_t atype = 0; /* type for allocation routines */
2655 xfs_extlen_t align; /* minimum allocation alignment */
2657 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2658 xfs_agnumber_t startag;
2659 xfs_alloc_arg_t args;
2662 xfs_extlen_t longest;
2664 xfs_extlen_t nextminlen = 0;
2666 int nullfb; /* true if ap->firstblock isn't set */
2672 mp = ap->ip->i_mount;
2673 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2674 if (unlikely(align)) {
2675 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2676 align, 0, ap->eof, 0, ap->conv,
2677 &ap->off, &ap->alen);
2681 nullfb = ap->firstblock == NULLFSBLOCK;
2682 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2684 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2685 ag = xfs_filestream_lookup_ag(ap->ip);
2686 ag = (ag != NULLAGNUMBER) ? ag : 0;
2687 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2689 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2692 ap->rval = ap->firstblock;
2694 xfs_bmap_adjacent(ap);
2697 * If allowed, use ap->rval; otherwise must use firstblock since
2698 * it's in the right allocation group.
2700 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2703 ap->rval = ap->firstblock;
2705 * Normal allocation, done through xfs_alloc_vextent.
2707 tryagain = isaligned = 0;
2710 args.fsbno = ap->rval;
2711 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2712 args.firstblock = ap->firstblock;
2715 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2716 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2718 args.type = XFS_ALLOCTYPE_START_BNO;
2719 args.total = ap->total;
2722 * Search for an allocation group with a single extent
2723 * large enough for the request.
2725 * If one isn't found, then adjust the minimum allocation
2726 * size to the largest space found.
2728 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2729 if (startag == NULLAGNUMBER)
2732 down_read(&mp->m_peraglock);
2733 while (blen < ap->alen) {
2734 pag = &mp->m_perag[ag];
2735 if (!pag->pagf_init &&
2736 (error = xfs_alloc_pagf_init(mp, args.tp,
2737 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2738 up_read(&mp->m_peraglock);
2742 * See xfs_alloc_fix_freelist...
2744 if (pag->pagf_init) {
2745 need = XFS_MIN_FREELIST_PAG(pag, mp);
2746 delta = need > pag->pagf_flcount ?
2747 need - pag->pagf_flcount : 0;
2748 longest = (pag->pagf_longest > delta) ?
2749 (pag->pagf_longest - delta) :
2750 (pag->pagf_flcount > 0 ||
2751 pag->pagf_longest > 0);
2757 if (xfs_inode_is_filestream(ap->ip)) {
2758 if (blen >= ap->alen)
2763 * If startag is an invalid AG, we've
2764 * come here once before and
2765 * xfs_filestream_new_ag picked the
2766 * best currently available.
2768 * Don't continue looping, since we
2769 * could loop forever.
2771 if (startag == NULLAGNUMBER)
2774 error = xfs_filestream_new_ag(ap, &ag);
2776 up_read(&mp->m_peraglock);
2780 /* loop again to set 'blen'*/
2781 startag = NULLAGNUMBER;
2785 if (++ag == mp->m_sb.sb_agcount)
2790 up_read(&mp->m_peraglock);
2792 * Since the above loop did a BUF_TRYLOCK, it is
2793 * possible that there is space for this request.
2795 if (notinit || blen < ap->minlen)
2796 args.minlen = ap->minlen;
2798 * If the best seen length is less than the request
2799 * length, use the best as the minimum.
2801 else if (blen < ap->alen)
2804 * Otherwise we've seen an extent as big as alen,
2805 * use that as the minimum.
2808 args.minlen = ap->alen;
2811 * set the failure fallback case to look in the selected
2812 * AG as the stream may have moved.
2814 if (xfs_inode_is_filestream(ap->ip))
2815 ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2816 } else if (ap->low) {
2817 if (xfs_inode_is_filestream(ap->ip))
2818 args.type = XFS_ALLOCTYPE_FIRST_AG;
2820 args.type = XFS_ALLOCTYPE_START_BNO;
2821 args.total = args.minlen = ap->minlen;
2823 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2824 args.total = ap->total;
2825 args.minlen = ap->minlen;
2827 /* apply extent size hints if obtained earlier */
2828 if (unlikely(align)) {
2830 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2831 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2832 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
2836 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
2837 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2838 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2841 * If we are not low on available data blocks, and the
2842 * underlying logical volume manager is a stripe, and
2843 * the file offset is zero then try to allocate data
2844 * blocks on stripe unit boundary.
2845 * NOTE: ap->aeof is only set if the allocation length
2846 * is >= the stripe unit and the allocation offset is
2847 * at the end of file.
2849 if (!ap->low && ap->aeof) {
2851 args.alignment = mp->m_dalign;
2855 * Adjust for alignment
2857 if (blen > args.alignment && blen <= ap->alen)
2858 args.minlen = blen - args.alignment;
2859 args.minalignslop = 0;
2862 * First try an exact bno allocation.
2863 * If it fails then do a near or start bno
2864 * allocation with alignment turned on.
2868 args.type = XFS_ALLOCTYPE_THIS_BNO;
2871 * Compute the minlen+alignment for the
2872 * next case. Set slop so that the value
2873 * of minlen+alignment+slop doesn't go up
2874 * between the calls.
2876 if (blen > mp->m_dalign && blen <= ap->alen)
2877 nextminlen = blen - mp->m_dalign;
2879 nextminlen = args.minlen;
2880 if (nextminlen + mp->m_dalign > args.minlen + 1)
2882 nextminlen + mp->m_dalign -
2885 args.minalignslop = 0;
2889 args.minalignslop = 0;
2891 args.minleft = ap->minleft;
2892 args.wasdel = ap->wasdel;
2894 args.userdata = ap->userdata;
2895 if ((error = xfs_alloc_vextent(&args)))
2897 if (tryagain && args.fsbno == NULLFSBLOCK) {
2899 * Exact allocation failed. Now try with alignment
2903 args.fsbno = ap->rval;
2904 args.alignment = mp->m_dalign;
2905 args.minlen = nextminlen;
2906 args.minalignslop = 0;
2908 if ((error = xfs_alloc_vextent(&args)))
2911 if (isaligned && args.fsbno == NULLFSBLOCK) {
2913 * allocation failed, so turn off alignment and
2917 args.fsbno = ap->rval;
2919 if ((error = xfs_alloc_vextent(&args)))
2922 if (args.fsbno == NULLFSBLOCK && nullfb &&
2923 args.minlen > ap->minlen) {
2924 args.minlen = ap->minlen;
2925 args.type = XFS_ALLOCTYPE_START_BNO;
2926 args.fsbno = ap->rval;
2927 if ((error = xfs_alloc_vextent(&args)))
2930 if (args.fsbno == NULLFSBLOCK && nullfb) {
2932 args.type = XFS_ALLOCTYPE_FIRST_AG;
2933 args.total = ap->minlen;
2935 if ((error = xfs_alloc_vextent(&args)))
2939 if (args.fsbno != NULLFSBLOCK) {
2940 ap->firstblock = ap->rval = args.fsbno;
2941 ASSERT(nullfb || fb_agno == args.agno ||
2942 (ap->low && fb_agno < args.agno));
2943 ap->alen = args.len;
2944 ap->ip->i_d.di_nblocks += args.len;
2945 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2947 ap->ip->i_delayed_blks -= args.len;
2949 * Adjust the disk quota also. This was reserved
2952 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2953 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2954 XFS_TRANS_DQ_BCOUNT,
2957 ap->rval = NULLFSBLOCK;
2964 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2965 * It figures out where to ask the underlying allocator to put the new extent.
2969 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2971 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
2972 return xfs_bmap_rtalloc(ap);
2973 return xfs_bmap_btalloc(ap);
2977 * Transform a btree format file with only one leaf node, where the
2978 * extents list will fit in the inode, into an extents format file.
2979 * Since the file extents are already in-core, all we have to do is
2980 * give up the space for the btree root and pitch the leaf block.
2982 STATIC int /* error */
2983 xfs_bmap_btree_to_extents(
2984 xfs_trans_t *tp, /* transaction pointer */
2985 xfs_inode_t *ip, /* incore inode pointer */
2986 xfs_btree_cur_t *cur, /* btree cursor */
2987 int *logflagsp, /* inode logging flags */
2988 int whichfork) /* data or attr fork */
2991 xfs_bmbt_block_t *cblock;/* child btree block */
2992 xfs_fsblock_t cbno; /* child block number */
2993 xfs_buf_t *cbp; /* child block's buffer */
2994 int error; /* error return value */
2995 xfs_ifork_t *ifp; /* inode fork data */
2996 xfs_mount_t *mp; /* mount point structure */
2997 __be64 *pp; /* ptr to block address */
2998 xfs_bmbt_block_t *rblock;/* root btree block */
3000 ifp = XFS_IFORK_PTR(ip, whichfork);
3001 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3002 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
3003 rblock = ifp->if_broot;
3004 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
3005 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
3006 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
3008 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
3009 cbno = be64_to_cpu(*pp);
3012 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
3015 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
3016 XFS_BMAP_BTREE_REF)))
3018 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
3019 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
3021 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
3022 ip->i_d.di_nblocks--;
3023 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
3024 xfs_trans_binval(tp, cbp);
3025 if (cur->bc_bufs[0] == cbp)
3026 cur->bc_bufs[0] = NULL;
3027 xfs_iroot_realloc(ip, -1, whichfork);
3028 ASSERT(ifp->if_broot == NULL);
3029 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
3030 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3031 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
3036 * Called by xfs_bmapi to update file extent records and the btree
3037 * after removing space (or undoing a delayed allocation).
3039 STATIC int /* error */
3040 xfs_bmap_del_extent(
3041 xfs_inode_t *ip, /* incore inode pointer */
3042 xfs_trans_t *tp, /* current transaction pointer */
3043 xfs_extnum_t idx, /* extent number to update/delete */
3044 xfs_bmap_free_t *flist, /* list of extents to be freed */
3045 xfs_btree_cur_t *cur, /* if null, not a btree */
3046 xfs_bmbt_irec_t *del, /* data to remove from extents */
3047 int *logflagsp, /* inode logging flags */
3048 xfs_extdelta_t *delta, /* Change made to incore extents */
3049 int whichfork, /* data or attr fork */
3050 int rsvd) /* OK to allocate reserved blocks */
3052 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
3053 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
3054 xfs_fsblock_t del_endblock=0; /* first block past del */
3055 xfs_fileoff_t del_endoff; /* first offset past del */
3056 int delay; /* current block is delayed allocated */
3057 int do_fx; /* free extent at end of routine */
3058 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
3059 int error; /* error return value */
3060 int flags; /* inode logging flags */
3061 xfs_bmbt_irec_t got; /* current extent entry */
3062 xfs_fileoff_t got_endoff; /* first offset past got */
3063 int i; /* temp state */
3064 xfs_ifork_t *ifp; /* inode fork pointer */
3065 xfs_mount_t *mp; /* mount structure */
3066 xfs_filblks_t nblks; /* quota/sb block count */
3067 xfs_bmbt_irec_t new; /* new record to be inserted */
3069 uint qfield; /* quota field to update */
3070 xfs_filblks_t temp; /* for indirect length calculations */
3071 xfs_filblks_t temp2; /* for indirect length calculations */
3073 XFS_STATS_INC(xs_del_exlist);
3075 ifp = XFS_IFORK_PTR(ip, whichfork);
3076 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
3077 (uint)sizeof(xfs_bmbt_rec_t)));
3078 ASSERT(del->br_blockcount > 0);
3079 ep = xfs_iext_get_ext(ifp, idx);
3080 xfs_bmbt_get_all(ep, &got);
3081 ASSERT(got.br_startoff <= del->br_startoff);
3082 del_endoff = del->br_startoff + del->br_blockcount;
3083 got_endoff = got.br_startoff + got.br_blockcount;
3084 ASSERT(got_endoff >= del_endoff);
3085 delay = ISNULLSTARTBLOCK(got.br_startblock);
3086 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
3091 * If deleting a real allocation, must free up the disk space.
3094 flags = XFS_ILOG_CORE;
3096 * Realtime allocation. Free it and record di_nblocks update.
3098 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
3102 ASSERT(do_mod(del->br_blockcount,
3103 mp->m_sb.sb_rextsize) == 0);
3104 ASSERT(do_mod(del->br_startblock,
3105 mp->m_sb.sb_rextsize) == 0);
3106 bno = del->br_startblock;
3107 len = del->br_blockcount;
3108 do_div(bno, mp->m_sb.sb_rextsize);
3109 do_div(len, mp->m_sb.sb_rextsize);
3110 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3111 (xfs_extlen_t)len)))
3114 nblks = len * mp->m_sb.sb_rextsize;
3115 qfield = XFS_TRANS_DQ_RTBCOUNT;
3118 * Ordinary allocation.
3122 nblks = del->br_blockcount;
3123 qfield = XFS_TRANS_DQ_BCOUNT;
3126 * Set up del_endblock and cur for later.
3128 del_endblock = del->br_startblock + del->br_blockcount;
3130 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3131 got.br_startblock, got.br_blockcount,
3136 da_old = da_new = 0;
3138 da_old = STARTBLOCKVAL(got.br_startblock);
3144 * Set flag value to use in switch statement.
3145 * Left-contig is 2, right-contig is 1.
3147 switch (((got.br_startoff == del->br_startoff) << 1) |
3148 (got_endoff == del_endoff)) {
3151 * Matches the whole extent. Delete the entry.
3153 XFS_BMAP_TRACE_DELETE("3", ip, idx, 1, whichfork);
3154 xfs_iext_remove(ifp, idx, 1);
3155 ifp->if_lastex = idx;
3158 XFS_IFORK_NEXT_SET(ip, whichfork,
3159 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3160 flags |= XFS_ILOG_CORE;
3162 flags |= XFS_ILOG_FEXT(whichfork);
3165 if ((error = xfs_bmbt_delete(cur, &i)))
3172 * Deleting the first part of the extent.
3174 XFS_BMAP_TRACE_PRE_UPDATE("2", ip, idx, whichfork);
3175 xfs_bmbt_set_startoff(ep, del_endoff);
3176 temp = got.br_blockcount - del->br_blockcount;
3177 xfs_bmbt_set_blockcount(ep, temp);
3178 ifp->if_lastex = idx;
3180 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3182 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3183 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx,
3188 xfs_bmbt_set_startblock(ep, del_endblock);
3189 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx, whichfork);
3191 flags |= XFS_ILOG_FEXT(whichfork);
3194 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3195 got.br_blockcount - del->br_blockcount,
3202 * Deleting the last part of the extent.
3204 temp = got.br_blockcount - del->br_blockcount;
3205 XFS_BMAP_TRACE_PRE_UPDATE("1", ip, idx, whichfork);
3206 xfs_bmbt_set_blockcount(ep, temp);
3207 ifp->if_lastex = idx;
3209 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3211 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3212 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx,
3217 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx, whichfork);
3219 flags |= XFS_ILOG_FEXT(whichfork);
3222 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3224 got.br_blockcount - del->br_blockcount,
3231 * Deleting the middle of the extent.
3233 temp = del->br_startoff - got.br_startoff;
3234 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, whichfork);
3235 xfs_bmbt_set_blockcount(ep, temp);
3236 new.br_startoff = del_endoff;
3237 temp2 = got_endoff - del_endoff;
3238 new.br_blockcount = temp2;
3239 new.br_state = got.br_state;
3241 new.br_startblock = del_endblock;
3242 flags |= XFS_ILOG_CORE;
3244 if ((error = xfs_bmbt_update(cur,
3246 got.br_startblock, temp,
3249 if ((error = xfs_bmbt_increment(cur, 0, &i)))
3251 cur->bc_rec.b = new;
3252 error = xfs_bmbt_insert(cur, &i);
3253 if (error && error != ENOSPC)
3256 * If get no-space back from btree insert,
3257 * it tried a split, and we have a zero
3258 * block reservation.
3259 * Fix up our state and return the error.
3261 if (error == ENOSPC) {
3263 * Reset the cursor, don't trust
3264 * it after any insert operation.
3266 if ((error = xfs_bmbt_lookup_eq(cur,
3273 * Update the btree record back
3274 * to the original value.
3276 if ((error = xfs_bmbt_update(cur,
3283 * Reset the extent record back
3284 * to the original value.
3286 xfs_bmbt_set_blockcount(ep,
3289 error = XFS_ERROR(ENOSPC);
3294 flags |= XFS_ILOG_FEXT(whichfork);
3295 XFS_IFORK_NEXT_SET(ip, whichfork,
3296 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3298 ASSERT(whichfork == XFS_DATA_FORK);
3299 temp = xfs_bmap_worst_indlen(ip, temp);
3300 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3301 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3302 new.br_startblock = NULLSTARTBLOCK((int)temp2);
3303 da_new = temp + temp2;
3304 while (da_new > da_old) {
3308 xfs_bmbt_set_startblock(ep,
3309 NULLSTARTBLOCK((int)temp));
3311 if (da_new == da_old)
3317 NULLSTARTBLOCK((int)temp2);
3321 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, whichfork);
3322 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 1, &new, NULL,
3324 xfs_iext_insert(ifp, idx + 1, 1, &new);
3325 ifp->if_lastex = idx + 1;
3329 * If we need to, add to list of extents to delete.
3332 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3335 * Adjust inode # blocks in the file.
3338 ip->i_d.di_nblocks -= nblks;
3340 * Adjust quota data.
3343 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
3346 * Account for change in delayed indirect blocks.
3347 * Nothing to do for disk quota accounting here.
3349 ASSERT(da_old >= da_new);
3350 if (da_old > da_new)
3351 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
3354 /* DELTA: report the original extent. */
3355 if (delta->xed_startoff > got.br_startoff)
3356 delta->xed_startoff = got.br_startoff;
3357 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3358 delta->xed_blockcount = got.br_startoff +
3367 * Remove the entry "free" from the free item list. Prev points to the
3368 * previous entry, unless "free" is the head of the list.
3372 xfs_bmap_free_t *flist, /* free item list header */
3373 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3374 xfs_bmap_free_item_t *free) /* list item to be freed */
3377 prev->xbfi_next = free->xbfi_next;
3379 flist->xbf_first = free->xbfi_next;
3381 kmem_zone_free(xfs_bmap_free_item_zone, free);
3385 * Convert an extents-format file into a btree-format file.
3386 * The new file will have a root block (in the inode) and a single child block.
3388 STATIC int /* error */
3389 xfs_bmap_extents_to_btree(
3390 xfs_trans_t *tp, /* transaction pointer */
3391 xfs_inode_t *ip, /* incore inode pointer */
3392 xfs_fsblock_t *firstblock, /* first-block-allocated */
3393 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3394 xfs_btree_cur_t **curp, /* cursor returned to caller */
3395 int wasdel, /* converting a delayed alloc */
3396 int *logflagsp, /* inode logging flags */
3397 int whichfork) /* data or attr fork */
3399 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
3400 xfs_buf_t *abp; /* buffer for ablock */
3401 xfs_alloc_arg_t args; /* allocation arguments */
3402 xfs_bmbt_rec_t *arp; /* child record pointer */
3403 xfs_bmbt_block_t *block; /* btree root block */
3404 xfs_btree_cur_t *cur; /* bmap btree cursor */
3405 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3406 int error; /* error return value */
3407 xfs_extnum_t i, cnt; /* extent record index */
3408 xfs_ifork_t *ifp; /* inode fork pointer */
3409 xfs_bmbt_key_t *kp; /* root block key pointer */
3410 xfs_mount_t *mp; /* mount structure */
3411 xfs_extnum_t nextents; /* number of file extents */
3412 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3414 ifp = XFS_IFORK_PTR(ip, whichfork);
3415 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3416 ASSERT(ifp->if_ext_max ==
3417 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3419 * Make space in the inode incore.
3421 xfs_iroot_realloc(ip, 1, whichfork);
3422 ifp->if_flags |= XFS_IFBROOT;
3426 block = ifp->if_broot;
3427 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3428 block->bb_level = cpu_to_be16(1);
3429 block->bb_numrecs = cpu_to_be16(1);
3430 block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3431 block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3433 * Need a cursor. Can't allocate until bb_level is filled in.
3436 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
3438 cur->bc_private.b.firstblock = *firstblock;
3439 cur->bc_private.b.flist = flist;
3440 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3442 * Convert to a btree with two levels, one record in root.
3444 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3447 args.firstblock = *firstblock;
3448 if (*firstblock == NULLFSBLOCK) {
3449 args.type = XFS_ALLOCTYPE_START_BNO;
3450 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3451 } else if (flist->xbf_low) {
3452 args.type = XFS_ALLOCTYPE_START_BNO;
3453 args.fsbno = *firstblock;
3455 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3456 args.fsbno = *firstblock;
3458 args.minlen = args.maxlen = args.prod = 1;
3459 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3460 args.minalignslop = 0;
3461 args.wasdel = wasdel;
3463 if ((error = xfs_alloc_vextent(&args))) {
3464 xfs_iroot_realloc(ip, -1, whichfork);
3465 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3469 * Allocation can't fail, the space was reserved.
3471 ASSERT(args.fsbno != NULLFSBLOCK);
3472 ASSERT(*firstblock == NULLFSBLOCK ||
3473 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3475 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3476 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3477 cur->bc_private.b.allocated++;
3478 ip->i_d.di_nblocks++;
3479 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3480 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3482 * Fill in the child block.
3484 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
3485 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3486 ablock->bb_level = 0;
3487 ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3488 ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3489 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3490 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3491 for (cnt = i = 0; i < nextents; i++) {
3492 ep = xfs_iext_get_ext(ifp, i);
3493 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
3494 arp->l0 = cpu_to_be64(ep->l0);
3495 arp->l1 = cpu_to_be64(ep->l1);
3499 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3500 ablock->bb_numrecs = cpu_to_be16(cnt);
3502 * Fill in the root key and pointer.
3504 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
3505 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3506 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3507 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
3508 *pp = cpu_to_be64(args.fsbno);
3510 * Do all this logging at the end so that
3511 * the root is at the right level.
3513 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
3514 xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3515 ASSERT(*curp == NULL);
3517 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
3522 * Helper routine to reset inode di_forkoff field when switching
3523 * attribute fork from local to extent format - we reset it where
3524 * possible to make space available for inline data fork extents.
3527 xfs_bmap_forkoff_reset(
3532 if (whichfork == XFS_ATTR_FORK &&
3533 (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
3534 (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
3535 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
3536 ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
3537 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3538 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
3539 (uint)sizeof(xfs_bmbt_rec_t);
3540 ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
3541 (uint)sizeof(xfs_bmbt_rec_t);
3546 * Convert a local file to an extents file.
3547 * This code is out of bounds for data forks of regular files,
3548 * since the file data needs to get logged so things will stay consistent.
3549 * (The bmap-level manipulations are ok, though).
3551 STATIC int /* error */
3552 xfs_bmap_local_to_extents(
3553 xfs_trans_t *tp, /* transaction pointer */
3554 xfs_inode_t *ip, /* incore inode pointer */
3555 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3556 xfs_extlen_t total, /* total blocks needed by transaction */
3557 int *logflagsp, /* inode logging flags */
3558 int whichfork) /* data or attr fork */
3560 int error; /* error return value */
3561 int flags; /* logging flags returned */
3562 xfs_ifork_t *ifp; /* inode fork pointer */
3565 * We don't want to deal with the case of keeping inode data inline yet.
3566 * So sending the data fork of a regular inode is invalid.
3568 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3569 whichfork == XFS_DATA_FORK));
3570 ifp = XFS_IFORK_PTR(ip, whichfork);
3571 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3574 if (ifp->if_bytes) {
3575 xfs_alloc_arg_t args; /* allocation arguments */
3576 xfs_buf_t *bp; /* buffer for extent block */
3577 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
3580 args.mp = ip->i_mount;
3581 args.firstblock = *firstblock;
3582 ASSERT((ifp->if_flags &
3583 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3585 * Allocate a block. We know we need only one, since the
3586 * file currently fits in an inode.
3588 if (*firstblock == NULLFSBLOCK) {
3589 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3590 args.type = XFS_ALLOCTYPE_START_BNO;
3592 args.fsbno = *firstblock;
3593 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3596 args.mod = args.minleft = args.alignment = args.wasdel =
3597 args.isfl = args.minalignslop = 0;
3598 args.minlen = args.maxlen = args.prod = 1;
3599 if ((error = xfs_alloc_vextent(&args)))
3602 * Can't fail, the space was reserved.
3604 ASSERT(args.fsbno != NULLFSBLOCK);
3605 ASSERT(args.len == 1);
3606 *firstblock = args.fsbno;
3607 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3608 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3610 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3611 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3612 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3613 xfs_iext_add(ifp, 0, 1);
3614 ep = xfs_iext_get_ext(ifp, 0);
3615 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3616 XFS_BMAP_TRACE_POST_UPDATE("new", ip, 0, whichfork);
3617 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3618 ip->i_d.di_nblocks = 1;
3619 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
3620 XFS_TRANS_DQ_BCOUNT, 1L);
3621 flags |= XFS_ILOG_FEXT(whichfork);
3623 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3624 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3626 ifp->if_flags &= ~XFS_IFINLINE;
3627 ifp->if_flags |= XFS_IFEXTENTS;
3628 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3629 flags |= XFS_ILOG_CORE;
3636 * Search the extent records for the entry containing block bno.
3637 * If bno lies in a hole, point to the next entry. If bno lies
3638 * past eof, *eofp will be set, and *prevp will contain the last
3639 * entry (null if none). Else, *lastxp will be set to the index
3640 * of the found entry; *gotp will contain the entry.
3642 xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3643 xfs_bmap_search_multi_extents(
3644 xfs_ifork_t *ifp, /* inode fork pointer */
3645 xfs_fileoff_t bno, /* block number searched for */
3646 int *eofp, /* out: end of file found */
3647 xfs_extnum_t *lastxp, /* out: last extent index */
3648 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3649 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3651 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3652 xfs_extnum_t lastx; /* last extent index */
3655 * Initialize the extent entry structure to catch access to
3656 * uninitialized br_startblock field.
3658 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3659 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3660 gotp->br_state = XFS_EXT_INVALID;
3662 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3664 gotp->br_startblock = 0xffffa5a5;
3666 prevp->br_startoff = NULLFILEOFF;
3668 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3670 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3672 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3673 xfs_bmbt_get_all(ep, gotp);
3687 * Search the extents list for the inode, for the extent containing bno.
3688 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3689 * *eofp will be set, and *prevp will contain the last entry (null if none).
3690 * Else, *lastxp will be set to the index of the found
3691 * entry; *gotp will contain the entry.
3693 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3694 xfs_bmap_search_extents(
3695 xfs_inode_t *ip, /* incore inode pointer */
3696 xfs_fileoff_t bno, /* block number searched for */
3697 int fork, /* data or attr fork */
3698 int *eofp, /* out: end of file found */
3699 xfs_extnum_t *lastxp, /* out: last extent index */
3700 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3701 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3703 xfs_ifork_t *ifp; /* inode fork pointer */
3704 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3706 XFS_STATS_INC(xs_look_exlist);
3707 ifp = XFS_IFORK_PTR(ip, fork);
3709 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3711 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3712 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3713 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3714 "Access to block zero in inode %llu "
3715 "start_block: %llx start_off: %llx "
3716 "blkcnt: %llx extent-state: %x lastx: %x\n",
3717 (unsigned long long)ip->i_ino,
3718 (unsigned long long)gotp->br_startblock,
3719 (unsigned long long)gotp->br_startoff,
3720 (unsigned long long)gotp->br_blockcount,
3721 gotp->br_state, *lastxp);
3722 *lastxp = NULLEXTNUM;
3730 #ifdef XFS_BMAP_TRACE
3731 ktrace_t *xfs_bmap_trace_buf;
3734 * Add a bmap trace buffer entry. Base routine for the others.
3737 xfs_bmap_trace_addentry(
3738 int opcode, /* operation */
3739 const char *fname, /* function name */
3740 char *desc, /* operation description */
3741 xfs_inode_t *ip, /* incore inode pointer */
3742 xfs_extnum_t idx, /* index of entry(ies) */
3743 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
3744 xfs_bmbt_rec_host_t *r1, /* first record */
3745 xfs_bmbt_rec_host_t *r2, /* second record or null */
3746 int whichfork) /* data or attr fork */
3748 xfs_bmbt_rec_host_t tr2;
3750 ASSERT(cnt == 1 || cnt == 2);
3755 memset(&tr2, 0, sizeof(tr2));
3758 ktrace_enter(xfs_bmap_trace_buf,
3759 (void *)(__psint_t)(opcode | (whichfork << 16)),
3760 (void *)fname, (void *)desc, (void *)ip,
3761 (void *)(__psint_t)idx,
3762 (void *)(__psint_t)cnt,
3763 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3764 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3765 (void *)(__psunsigned_t)(r1->l0 >> 32),
3766 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3767 (void *)(__psunsigned_t)(r1->l1 >> 32),
3768 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3769 (void *)(__psunsigned_t)(r2->l0 >> 32),
3770 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3771 (void *)(__psunsigned_t)(r2->l1 >> 32),
3772 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3774 ASSERT(ip->i_xtrace);
3775 ktrace_enter(ip->i_xtrace,
3776 (void *)(__psint_t)(opcode | (whichfork << 16)),
3777 (void *)fname, (void *)desc, (void *)ip,
3778 (void *)(__psint_t)idx,
3779 (void *)(__psint_t)cnt,
3780 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3781 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3782 (void *)(__psunsigned_t)(r1->l0 >> 32),
3783 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3784 (void *)(__psunsigned_t)(r1->l1 >> 32),
3785 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3786 (void *)(__psunsigned_t)(r2->l0 >> 32),
3787 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3788 (void *)(__psunsigned_t)(r2->l1 >> 32),
3789 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3794 * Add bmap trace entry prior to a call to xfs_iext_remove.
3797 xfs_bmap_trace_delete(
3798 const char *fname, /* function name */
3799 char *desc, /* operation description */
3800 xfs_inode_t *ip, /* incore inode pointer */
3801 xfs_extnum_t idx, /* index of entry(entries) deleted */
3802 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3803 int whichfork) /* data or attr fork */
3805 xfs_ifork_t *ifp; /* inode fork pointer */
3807 ifp = XFS_IFORK_PTR(ip, whichfork);
3808 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
3809 cnt, xfs_iext_get_ext(ifp, idx),
3810 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
3815 * Add bmap trace entry prior to a call to xfs_iext_insert, or
3816 * reading in the extents list from the disk (in the btree).
3819 xfs_bmap_trace_insert(
3820 const char *fname, /* function name */
3821 char *desc, /* operation description */
3822 xfs_inode_t *ip, /* incore inode pointer */
3823 xfs_extnum_t idx, /* index of entry(entries) inserted */
3824 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3825 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3826 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3827 int whichfork) /* data or attr fork */
3829 xfs_bmbt_rec_host_t tr1; /* compressed record 1 */
3830 xfs_bmbt_rec_host_t tr2; /* compressed record 2 if needed */
3832 xfs_bmbt_set_all(&tr1, r1);
3835 xfs_bmbt_set_all(&tr2, r2);
3840 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3841 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3845 * Add bmap trace entry after updating an extent record in place.
3848 xfs_bmap_trace_post_update(
3849 const char *fname, /* function name */
3850 char *desc, /* operation description */
3851 xfs_inode_t *ip, /* incore inode pointer */
3852 xfs_extnum_t idx, /* index of entry updated */
3853 int whichfork) /* data or attr fork */
3855 xfs_ifork_t *ifp; /* inode fork pointer */
3857 ifp = XFS_IFORK_PTR(ip, whichfork);
3858 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
3859 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3863 * Add bmap trace entry prior to updating an extent record in place.
3866 xfs_bmap_trace_pre_update(
3867 const char *fname, /* function name */
3868 char *desc, /* operation description */
3869 xfs_inode_t *ip, /* incore inode pointer */
3870 xfs_extnum_t idx, /* index of entry to be updated */
3871 int whichfork) /* data or attr fork */
3873 xfs_ifork_t *ifp; /* inode fork pointer */
3875 ifp = XFS_IFORK_PTR(ip, whichfork);
3876 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
3877 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3879 #endif /* XFS_BMAP_TRACE */
3882 * Compute the worst-case number of indirect blocks that will be used
3883 * for ip's delayed extent of length "len".
3885 STATIC xfs_filblks_t
3886 xfs_bmap_worst_indlen(
3887 xfs_inode_t *ip, /* incore inode pointer */
3888 xfs_filblks_t len) /* delayed extent length */
3890 int level; /* btree level number */
3891 int maxrecs; /* maximum record count at this level */
3892 xfs_mount_t *mp; /* mount structure */
3893 xfs_filblks_t rval; /* return value */
3896 maxrecs = mp->m_bmap_dmxr[0];
3897 for (level = 0, rval = 0;
3898 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3901 do_div(len, maxrecs);
3904 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3907 maxrecs = mp->m_bmap_dmxr[1];
3912 #if defined(XFS_RW_TRACE)
3921 if (ip->i_rwtrace == NULL)
3923 ktrace_enter(ip->i_rwtrace,
3924 (void *)(__psint_t)XFS_BUNMAP,
3926 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3927 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3928 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3929 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3930 (void *)(__psint_t)len,
3931 (void *)(__psint_t)flags,
3932 (void *)(unsigned long)current_cpu(),
3944 * Convert inode from non-attributed to attributed.
3945 * Must not be in a transaction, ip must not be locked.
3947 int /* error code */
3948 xfs_bmap_add_attrfork(
3949 xfs_inode_t *ip, /* incore inode pointer */
3950 int size, /* space new attribute needs */
3951 int rsvd) /* xact may use reserved blks */
3953 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3954 xfs_bmap_free_t flist; /* freed extent records */
3955 xfs_mount_t *mp; /* mount structure */
3956 xfs_trans_t *tp; /* transaction pointer */
3957 int blks; /* space reservation */
3958 int version = 1; /* superblock attr version */
3959 int committed; /* xaction was committed */
3960 int logflags; /* logging flags */
3961 int error; /* error return value */
3963 ASSERT(XFS_IFORK_Q(ip) == 0);
3964 ASSERT(ip->i_df.if_ext_max ==
3965 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3968 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3969 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3970 blks = XFS_ADDAFORK_SPACE_RES(mp);
3972 tp->t_flags |= XFS_TRANS_RESERVE;
3973 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3974 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3976 xfs_ilock(ip, XFS_ILOCK_EXCL);
3977 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
3978 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3979 XFS_QMOPT_RES_REGBLKS);
3981 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3982 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3985 if (XFS_IFORK_Q(ip))
3987 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3989 * For inodes coming from pre-6.2 filesystems.
3991 ASSERT(ip->i_d.di_aformat == 0);
3992 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3994 ASSERT(ip->i_d.di_anextents == 0);
3995 VN_HOLD(XFS_ITOV(ip));
3996 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3997 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3998 switch (ip->i_d.di_format) {
3999 case XFS_DINODE_FMT_DEV:
4000 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
4002 case XFS_DINODE_FMT_UUID:
4003 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
4005 case XFS_DINODE_FMT_LOCAL:
4006 case XFS_DINODE_FMT_EXTENTS:
4007 case XFS_DINODE_FMT_BTREE:
4008 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
4009 if (!ip->i_d.di_forkoff)
4010 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
4011 else if (mp->m_flags & XFS_MOUNT_ATTR2)
4016 error = XFS_ERROR(EINVAL);
4019 ip->i_df.if_ext_max =
4020 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4021 ASSERT(ip->i_afp == NULL);
4022 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
4023 ip->i_afp->if_ext_max =
4024 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4025 ip->i_afp->if_flags = XFS_IFEXTENTS;
4027 XFS_BMAP_INIT(&flist, &firstblock);
4028 switch (ip->i_d.di_format) {
4029 case XFS_DINODE_FMT_LOCAL:
4030 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
4033 case XFS_DINODE_FMT_EXTENTS:
4034 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
4037 case XFS_DINODE_FMT_BTREE:
4038 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
4046 xfs_trans_log_inode(tp, ip, logflags);
4049 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
4050 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
4051 __int64_t sbfields = 0;
4053 spin_lock(&mp->m_sb_lock);
4054 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
4055 xfs_sb_version_addattr(&mp->m_sb);
4056 sbfields |= XFS_SB_VERSIONNUM;
4058 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
4059 xfs_sb_version_addattr2(&mp->m_sb);
4060 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
4063 spin_unlock(&mp->m_sb_lock);
4064 xfs_mod_sb(tp, sbfields);
4066 spin_unlock(&mp->m_sb_lock);
4068 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
4070 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
4071 ASSERT(ip->i_df.if_ext_max ==
4072 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4075 xfs_bmap_cancel(&flist);
4077 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4079 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4080 ASSERT(ip->i_df.if_ext_max ==
4081 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4086 * Add the extent to the list of extents to be free at transaction end.
4087 * The list is maintained sorted (by block number).
4092 xfs_fsblock_t bno, /* fs block number of extent */
4093 xfs_filblks_t len, /* length of extent */
4094 xfs_bmap_free_t *flist, /* list of extents */
4095 xfs_mount_t *mp) /* mount point structure */
4097 xfs_bmap_free_item_t *cur; /* current (next) element */
4098 xfs_bmap_free_item_t *new; /* new element */
4099 xfs_bmap_free_item_t *prev; /* previous element */
4101 xfs_agnumber_t agno;
4102 xfs_agblock_t agbno;
4104 ASSERT(bno != NULLFSBLOCK);
4106 ASSERT(len <= MAXEXTLEN);
4107 ASSERT(!ISNULLSTARTBLOCK(bno));
4108 agno = XFS_FSB_TO_AGNO(mp, bno);
4109 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4110 ASSERT(agno < mp->m_sb.sb_agcount);
4111 ASSERT(agbno < mp->m_sb.sb_agblocks);
4112 ASSERT(len < mp->m_sb.sb_agblocks);
4113 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4115 ASSERT(xfs_bmap_free_item_zone != NULL);
4116 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4117 new->xbfi_startblock = bno;
4118 new->xbfi_blockcount = (xfs_extlen_t)len;
4119 for (prev = NULL, cur = flist->xbf_first;
4121 prev = cur, cur = cur->xbfi_next) {
4122 if (cur->xbfi_startblock >= bno)
4126 prev->xbfi_next = new;
4128 flist->xbf_first = new;
4129 new->xbfi_next = cur;
4134 * Compute and fill in the value of the maximum depth of a bmap btree
4135 * in this filesystem. Done once, during mount.
4138 xfs_bmap_compute_maxlevels(
4139 xfs_mount_t *mp, /* file system mount structure */
4140 int whichfork) /* data or attr fork */
4142 int level; /* btree level */
4143 uint maxblocks; /* max blocks at this level */
4144 uint maxleafents; /* max leaf entries possible */
4145 int maxrootrecs; /* max records in root block */
4146 int minleafrecs; /* min records in leaf block */
4147 int minnoderecs; /* min records in node block */
4148 int sz; /* root block size */
4151 * The maximum number of extents in a file, hence the maximum
4152 * number of leaf entries, is controlled by the type of di_nextents
4153 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4154 * (a signed 16-bit number, xfs_aextnum_t).
4156 * Note that we can no longer assume that if we are in ATTR1 that
4157 * the fork offset of all the inodes will be (m_attroffset >> 3)
4158 * because we could have mounted with ATTR2 and then mounted back
4159 * with ATTR1, keeping the di_forkoff's fixed but probably at
4160 * various positions. Therefore, for both ATTR1 and ATTR2
4161 * we have to assume the worst case scenario of a minimum size
4164 if (whichfork == XFS_DATA_FORK) {
4165 maxleafents = MAXEXTNUM;
4166 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
4168 maxleafents = MAXAEXTNUM;
4169 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
4171 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
4172 minleafrecs = mp->m_bmap_dmnr[0];
4173 minnoderecs = mp->m_bmap_dmnr[1];
4174 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4175 for (level = 1; maxblocks > 1; level++) {
4176 if (maxblocks <= maxrootrecs)
4179 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4181 mp->m_bm_maxlevels[whichfork] = level;
4185 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4186 * caller. Frees all the extents that need freeing, which must be done
4187 * last due to locking considerations. We never free any extents in
4188 * the first transaction. This is to allow the caller to make the first
4189 * transaction a synchronous one so that the pointers to the data being
4190 * broken in this transaction will be permanent before the data is actually
4191 * freed. This is necessary to prevent blocks from being reallocated
4192 * and written to before the free and reallocation are actually permanent.
4193 * We do not just make the first transaction synchronous here, because
4194 * there are more efficient ways to gain the same protection in some cases
4195 * (see the file truncation code).
4197 * Return 1 if the given transaction was committed and a new one
4198 * started, and 0 otherwise in the committed parameter.
4203 xfs_trans_t **tp, /* transaction pointer addr */
4204 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4205 int *committed) /* xact committed or not */
4207 xfs_efd_log_item_t *efd; /* extent free data */
4208 xfs_efi_log_item_t *efi; /* extent free intention */
4209 int error; /* error return value */
4210 xfs_bmap_free_item_t *free; /* free extent item */
4211 unsigned int logres; /* new log reservation */
4212 unsigned int logcount; /* new log count */
4213 xfs_mount_t *mp; /* filesystem mount structure */
4214 xfs_bmap_free_item_t *next; /* next item on free list */
4215 xfs_trans_t *ntp; /* new transaction pointer */
4217 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4218 if (flist->xbf_count == 0) {
4223 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4224 for (free = flist->xbf_first; free; free = free->xbfi_next)
4225 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4226 free->xbfi_blockcount);
4227 logres = ntp->t_log_res;
4228 logcount = ntp->t_log_count;
4229 ntp = xfs_trans_dup(*tp);
4230 error = xfs_trans_commit(*tp, 0);
4234 * We have a new transaction, so we should return committed=1,
4235 * even though we're returning an error.
4240 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4243 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4244 for (free = flist->xbf_first; free != NULL; free = next) {
4245 next = free->xbfi_next;
4246 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4247 free->xbfi_blockcount))) {
4249 * The bmap free list will be cleaned up at a
4250 * higher level. The EFI will be canceled when
4251 * this transaction is aborted.
4252 * Need to force shutdown here to make sure it
4253 * happens, since this transaction may not be
4257 if (!XFS_FORCED_SHUTDOWN(mp))
4258 xfs_force_shutdown(mp,
4259 (error == EFSCORRUPTED) ?
4260 SHUTDOWN_CORRUPT_INCORE :
4261 SHUTDOWN_META_IO_ERROR);
4264 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4265 free->xbfi_blockcount);
4266 xfs_bmap_del_free(flist, NULL, free);
4272 * Free up any items left in the list.
4276 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4278 xfs_bmap_free_item_t *free; /* free list item */
4279 xfs_bmap_free_item_t *next;
4281 if (flist->xbf_count == 0)
4283 ASSERT(flist->xbf_first != NULL);
4284 for (free = flist->xbf_first; free; free = next) {
4285 next = free->xbfi_next;
4286 xfs_bmap_del_free(flist, NULL, free);
4288 ASSERT(flist->xbf_count == 0);
4292 * Returns the file-relative block number of the first unused block(s)
4293 * in the file with at least "len" logically contiguous blocks free.
4294 * This is the lowest-address hole if the file has holes, else the first block
4295 * past the end of file.
4296 * Return 0 if the file is currently local (in-inode).
4299 xfs_bmap_first_unused(
4300 xfs_trans_t *tp, /* transaction pointer */
4301 xfs_inode_t *ip, /* incore inode */
4302 xfs_extlen_t len, /* size of hole to find */
4303 xfs_fileoff_t *first_unused, /* unused block */
4304 int whichfork) /* data or attr fork */
4306 int error; /* error return value */
4307 int idx; /* extent record index */
4308 xfs_ifork_t *ifp; /* inode fork pointer */
4309 xfs_fileoff_t lastaddr; /* last block number seen */
4310 xfs_fileoff_t lowest; /* lowest useful block */
4311 xfs_fileoff_t max; /* starting useful block */
4312 xfs_fileoff_t off; /* offset for this block */
4313 xfs_extnum_t nextents; /* number of extent entries */
4315 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4316 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4317 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4318 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4322 ifp = XFS_IFORK_PTR(ip, whichfork);
4323 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4324 (error = xfs_iread_extents(tp, ip, whichfork)))
4326 lowest = *first_unused;
4327 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4328 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
4329 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
4330 off = xfs_bmbt_get_startoff(ep);
4332 * See if the hole before this extent will work.
4334 if (off >= lowest + len && off - max >= len) {
4335 *first_unused = max;
4338 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4339 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4341 *first_unused = max;
4346 * Returns the file-relative block number of the last block + 1 before
4347 * last_block (input value) in the file.
4348 * This is not based on i_size, it is based on the extent records.
4349 * Returns 0 for local files, as they do not have extent records.
4352 xfs_bmap_last_before(
4353 xfs_trans_t *tp, /* transaction pointer */
4354 xfs_inode_t *ip, /* incore inode */
4355 xfs_fileoff_t *last_block, /* last block */
4356 int whichfork) /* data or attr fork */
4358 xfs_fileoff_t bno; /* input file offset */
4359 int eof; /* hit end of file */
4360 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
4361 int error; /* error return value */
4362 xfs_bmbt_irec_t got; /* current extent value */
4363 xfs_ifork_t *ifp; /* inode fork pointer */
4364 xfs_extnum_t lastx; /* last extent used */
4365 xfs_bmbt_irec_t prev; /* previous extent value */
4367 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4368 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4369 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4370 return XFS_ERROR(EIO);
4371 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4375 ifp = XFS_IFORK_PTR(ip, whichfork);
4376 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4377 (error = xfs_iread_extents(tp, ip, whichfork)))
4379 bno = *last_block - 1;
4380 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4382 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4383 if (prev.br_startoff == NULLFILEOFF)
4386 *last_block = prev.br_startoff + prev.br_blockcount;
4389 * Otherwise *last_block is already the right answer.
4395 * Returns the file-relative block number of the first block past eof in
4396 * the file. This is not based on i_size, it is based on the extent records.
4397 * Returns 0 for local files, as they do not have extent records.
4400 xfs_bmap_last_offset(
4401 xfs_trans_t *tp, /* transaction pointer */
4402 xfs_inode_t *ip, /* incore inode */
4403 xfs_fileoff_t *last_block, /* last block */
4404 int whichfork) /* data or attr fork */
4406 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
4407 int error; /* error return value */
4408 xfs_ifork_t *ifp; /* inode fork pointer */
4409 xfs_extnum_t nextents; /* number of extent entries */
4411 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4412 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4413 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4414 return XFS_ERROR(EIO);
4415 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4419 ifp = XFS_IFORK_PTR(ip, whichfork);
4420 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4421 (error = xfs_iread_extents(tp, ip, whichfork)))
4423 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4428 ep = xfs_iext_get_ext(ifp, nextents - 1);
4429 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4434 * Returns whether the selected fork of the inode has exactly one
4435 * block or not. For the data fork we check this matches di_size,
4436 * implying the file's range is 0..bsize-1.
4438 int /* 1=>1 block, 0=>otherwise */
4440 xfs_inode_t *ip, /* incore inode */
4441 int whichfork) /* data or attr fork */
4443 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
4444 xfs_ifork_t *ifp; /* inode fork pointer */
4445 int rval; /* return value */
4446 xfs_bmbt_irec_t s; /* internal version of extent */
4449 if (whichfork == XFS_DATA_FORK) {
4450 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4451 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4452 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4455 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4457 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4459 ifp = XFS_IFORK_PTR(ip, whichfork);
4460 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4461 ep = xfs_iext_get_ext(ifp, 0);
4462 xfs_bmbt_get_all(ep, &s);
4463 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4464 if (rval && whichfork == XFS_DATA_FORK)
4465 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
4470 * Read in the extents to if_extents.
4471 * All inode fields are set up by caller, we just traverse the btree
4472 * and copy the records in. If the file system cannot contain unwritten
4473 * extents, the records are checked for no "state" flags.
4476 xfs_bmap_read_extents(
4477 xfs_trans_t *tp, /* transaction pointer */
4478 xfs_inode_t *ip, /* incore inode */
4479 int whichfork) /* data or attr fork */
4481 xfs_bmbt_block_t *block; /* current btree block */
4482 xfs_fsblock_t bno; /* block # of "block" */
4483 xfs_buf_t *bp; /* buffer for "block" */
4484 int error; /* error return value */
4485 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4486 xfs_extnum_t i, j; /* index into the extents list */
4487 xfs_ifork_t *ifp; /* fork structure */
4488 int level; /* btree level, for checking */
4489 xfs_mount_t *mp; /* file system mount structure */
4490 __be64 *pp; /* pointer to block address */
4492 xfs_extnum_t room; /* number of entries there's room for */
4496 ifp = XFS_IFORK_PTR(ip, whichfork);
4497 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4498 XFS_EXTFMT_INODE(ip);
4499 block = ifp->if_broot;
4501 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4503 level = be16_to_cpu(block->bb_level);
4505 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
4506 bno = be64_to_cpu(*pp);
4507 ASSERT(bno != NULLDFSBNO);
4508 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4509 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4511 * Go down the tree until leaf level is reached, following the first
4512 * pointer (leftmost) at each level.
4514 while (level-- > 0) {
4515 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4516 XFS_BMAP_BTREE_REF)))
4518 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4519 XFS_WANT_CORRUPTED_GOTO(
4520 XFS_BMAP_SANITY_CHECK(mp, block, level),
4524 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
4525 bno = be64_to_cpu(*pp);
4526 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4527 xfs_trans_brelse(tp, bp);
4530 * Here with bp and block set to the leftmost leaf node in the tree.
4532 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4535 * Loop over all leaf nodes. Copy information to the extent records.
4538 xfs_bmbt_rec_t *frp;
4539 xfs_fsblock_t nextbno;
4540 xfs_extnum_t num_recs;
4544 num_recs = be16_to_cpu(block->bb_numrecs);
4545 if (unlikely(i + num_recs > room)) {
4546 ASSERT(i + num_recs <= room);
4547 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4548 "corrupt dinode %Lu, (btree extents).",
4549 (unsigned long long) ip->i_ino);
4550 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4555 XFS_WANT_CORRUPTED_GOTO(
4556 XFS_BMAP_SANITY_CHECK(mp, block, 0),
4559 * Read-ahead the next leaf block, if any.
4561 nextbno = be64_to_cpu(block->bb_rightsib);
4562 if (nextbno != NULLFSBLOCK)
4563 xfs_btree_reada_bufl(mp, nextbno, 1);
4565 * Copy records into the extent records.
4567 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
4569 for (j = 0; j < num_recs; j++, i++, frp++) {
4570 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
4571 trp->l0 = be64_to_cpu(frp->l0);
4572 trp->l1 = be64_to_cpu(frp->l1);
4574 if (exntf == XFS_EXTFMT_NOSTATE) {
4576 * Check all attribute bmap btree records and
4577 * any "older" data bmap btree records for a
4578 * set bit in the "extent flag" position.
4580 if (unlikely(xfs_check_nostate_extents(ifp,
4581 start, num_recs))) {
4582 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4588 xfs_trans_brelse(tp, bp);
4591 * If we've reached the end, stop.
4593 if (bno == NULLFSBLOCK)
4595 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4596 XFS_BMAP_BTREE_REF)))
4598 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4600 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4601 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4602 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
4605 xfs_trans_brelse(tp, bp);
4606 return XFS_ERROR(EFSCORRUPTED);
4609 #ifdef XFS_BMAP_TRACE
4611 * Add bmap trace insert entries for all the contents of the extent records.
4614 xfs_bmap_trace_exlist(
4615 const char *fname, /* function name */
4616 xfs_inode_t *ip, /* incore inode pointer */
4617 xfs_extnum_t cnt, /* count of entries in the list */
4618 int whichfork) /* data or attr fork */
4620 xfs_bmbt_rec_host_t *ep; /* current extent record */
4621 xfs_extnum_t idx; /* extent record index */
4622 xfs_ifork_t *ifp; /* inode fork pointer */
4623 xfs_bmbt_irec_t s; /* file extent record */
4625 ifp = XFS_IFORK_PTR(ip, whichfork);
4626 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4627 for (idx = 0; idx < cnt; idx++) {
4628 ep = xfs_iext_get_ext(ifp, idx);
4629 xfs_bmbt_get_all(ep, &s);
4630 XFS_BMAP_TRACE_INSERT("exlist", ip, idx, 1, &s, NULL,
4638 * Validate that the bmbt_irecs being returned from bmapi are valid
4639 * given the callers original parameters. Specifically check the
4640 * ranges of the returned irecs to ensure that they only extent beyond
4641 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4644 xfs_bmap_validate_ret(
4648 xfs_bmbt_irec_t *mval,
4652 int i; /* index to map values */
4654 ASSERT(ret_nmap <= nmap);
4656 for (i = 0; i < ret_nmap; i++) {
4657 ASSERT(mval[i].br_blockcount > 0);
4658 if (!(flags & XFS_BMAPI_ENTIRE)) {
4659 ASSERT(mval[i].br_startoff >= bno);
4660 ASSERT(mval[i].br_blockcount <= len);
4661 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4664 ASSERT(mval[i].br_startoff < bno + len);
4665 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4669 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4670 mval[i].br_startoff);
4671 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4672 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4673 mval[i].br_startblock != HOLESTARTBLOCK);
4674 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4675 mval[i].br_state == XFS_EXT_UNWRITTEN);
4682 * Map file blocks to filesystem blocks.
4683 * File range is given by the bno/len pair.
4684 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4685 * into a hole or past eof.
4686 * Only allocates blocks from a single allocation group,
4687 * to avoid locking problems.
4688 * The returned value in "firstblock" from the first call in a transaction
4689 * must be remembered and presented to subsequent calls in "firstblock".
4690 * An upper bound for the number of blocks to be allocated is supplied to
4691 * the first call in "total"; if no allocation group has that many free
4692 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4696 xfs_trans_t *tp, /* transaction pointer */
4697 xfs_inode_t *ip, /* incore inode */
4698 xfs_fileoff_t bno, /* starting file offs. mapped */
4699 xfs_filblks_t len, /* length to map in file */
4700 int flags, /* XFS_BMAPI_... */
4701 xfs_fsblock_t *firstblock, /* first allocated block
4702 controls a.g. for allocs */
4703 xfs_extlen_t total, /* total blocks needed */
4704 xfs_bmbt_irec_t *mval, /* output: map values */
4705 int *nmap, /* i/o: mval size/count */
4706 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4707 xfs_extdelta_t *delta) /* o: change made to incore extents */
4709 xfs_fsblock_t abno; /* allocated block number */
4710 xfs_extlen_t alen; /* allocated extent length */
4711 xfs_fileoff_t aoff; /* allocated file offset */
4712 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
4713 xfs_btree_cur_t *cur; /* bmap btree cursor */
4714 xfs_fileoff_t end; /* end of mapped file region */
4715 int eof; /* we've hit the end of extents */
4716 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
4717 int error; /* error return */
4718 xfs_bmbt_irec_t got; /* current file extent record */
4719 xfs_ifork_t *ifp; /* inode fork pointer */
4720 xfs_extlen_t indlen; /* indirect blocks length */
4721 xfs_extnum_t lastx; /* last useful extent number */
4722 int logflags; /* flags for transaction logging */
4723 xfs_extlen_t minleft; /* min blocks left after allocation */
4724 xfs_extlen_t minlen; /* min allocation size */
4725 xfs_mount_t *mp; /* xfs mount structure */
4726 int n; /* current extent index */
4727 int nallocs; /* number of extents alloc\'d */
4728 xfs_extnum_t nextents; /* number of extents in file */
4729 xfs_fileoff_t obno; /* old block number (offset) */
4730 xfs_bmbt_irec_t prev; /* previous file extent record */
4731 int tmp_logflags; /* temp flags holder */
4732 int whichfork; /* data or attr fork */
4733 char inhole; /* current location is hole in file */
4734 char wasdelay; /* old extent was delayed */
4735 char wr; /* this is a write request */
4736 char rt; /* this is a realtime file */
4738 xfs_fileoff_t orig_bno; /* original block number value */
4739 int orig_flags; /* original flags arg value */
4740 xfs_filblks_t orig_len; /* original value of len arg */
4741 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4742 int orig_nmap; /* original value of *nmap */
4751 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4752 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4753 XFS_ATTR_FORK : XFS_DATA_FORK;
4755 if (unlikely(XFS_TEST_ERROR(
4756 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4757 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4758 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4759 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4760 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4761 return XFS_ERROR(EFSCORRUPTED);
4763 if (XFS_FORCED_SHUTDOWN(mp))
4764 return XFS_ERROR(EIO);
4765 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4766 ifp = XFS_IFORK_PTR(ip, whichfork);
4767 ASSERT(ifp->if_ext_max ==
4768 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4769 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4770 XFS_STATS_INC(xs_blk_mapw);
4772 XFS_STATS_INC(xs_blk_mapr);
4774 * IGSTATE flag is used to combine extents which
4775 * differ only due to the state of the extents.
4776 * This technique is used from xfs_getbmap()
4777 * when the caller does not wish to see the
4778 * separation (which is the default).
4780 * This technique is also used when writing a
4781 * buffer which has been partially written,
4782 * (usually by being flushed during a chunkread),
4783 * to ensure one write takes place. This also
4784 * prevents a change in the xfs inode extents at
4785 * this time, intentionally. This change occurs
4786 * on completion of the write operation, in
4787 * xfs_strat_comp(), where the xfs_bmapi() call
4788 * is transactioned, and the extents combined.
4790 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4791 wr = 0; /* no allocations are allowed */
4792 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
4796 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4798 if ((error = xfs_bmap_local_to_extents(tp, ip,
4799 firstblock, total, &logflags, whichfork)))
4802 if (wr && *firstblock == NULLFSBLOCK) {
4803 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4804 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4809 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4810 (error = xfs_iread_extents(tp, ip, whichfork)))
4812 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4814 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4820 delta->xed_startoff = NULLFILEOFF;
4821 delta->xed_blockcount = 0;
4823 while (bno < end && n < *nmap) {
4825 * Reading past eof, act as though there's a hole
4829 got.br_startoff = end;
4830 inhole = eof || got.br_startoff > bno;
4831 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
4832 ISNULLSTARTBLOCK(got.br_startblock);
4834 * First, deal with the hole before the allocated space
4835 * that we found, if any.
4837 if (wr && (inhole || wasdelay)) {
4839 * For the wasdelay case, we could also just
4840 * allocate the stuff asked for in this bmap call
4841 * but that wouldn't be as good.
4843 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
4844 alen = (xfs_extlen_t)got.br_blockcount;
4845 aoff = got.br_startoff;
4846 if (lastx != NULLEXTNUM && lastx) {
4847 ep = xfs_iext_get_ext(ifp, lastx - 1);
4848 xfs_bmbt_get_all(ep, &prev);
4850 } else if (wasdelay) {
4851 alen = (xfs_extlen_t)
4852 XFS_FILBLKS_MIN(len,
4854 got.br_blockcount) - bno);
4857 alen = (xfs_extlen_t)
4858 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4860 alen = (xfs_extlen_t)
4861 XFS_FILBLKS_MIN(alen,
4862 got.br_startoff - bno);
4865 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4866 if (flags & XFS_BMAPI_DELAY) {
4869 /* Figure out the extent size, adjust alen */
4870 extsz = xfs_get_extsz_hint(ip);
4872 error = xfs_bmap_extsize_align(mp,
4875 flags&XFS_BMAPI_DELAY,
4876 flags&XFS_BMAPI_CONVERT,
4882 extsz = alen / mp->m_sb.sb_rextsize;
4885 * Make a transaction-less quota reservation for
4886 * delayed allocation blocks. This number gets
4887 * adjusted later. We return if we haven't
4888 * allocated blocks already inside this loop.
4890 if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS(
4891 mp, NULL, ip, (long)alen, 0,
4892 rt ? XFS_QMOPT_RES_RTBLKS :
4893 XFS_QMOPT_RES_REGBLKS))) {
4896 ASSERT(cur == NULL);
4903 * Split changing sb for alen and indlen since
4904 * they could be coming from different places.
4906 indlen = (xfs_extlen_t)
4907 xfs_bmap_worst_indlen(ip, alen);
4911 error = xfs_mod_incore_sb(mp,
4913 -((int64_t)extsz), (flags &
4914 XFS_BMAPI_RSVBLOCKS));
4916 error = xfs_mod_incore_sb(mp,
4918 -((int64_t)alen), (flags &
4919 XFS_BMAPI_RSVBLOCKS));
4922 error = xfs_mod_incore_sb(mp,
4924 -((int64_t)indlen), (flags &
4925 XFS_BMAPI_RSVBLOCKS));
4927 xfs_mod_incore_sb(mp,
4929 (int64_t)extsz, (flags &
4930 XFS_BMAPI_RSVBLOCKS));
4932 xfs_mod_incore_sb(mp,
4934 (int64_t)alen, (flags &
4935 XFS_BMAPI_RSVBLOCKS));
4939 if (XFS_IS_QUOTA_ON(mp))
4940 /* unreserve the blocks now */
4942 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4945 XFS_QMOPT_RES_RTBLKS :
4946 XFS_QMOPT_RES_REGBLKS);
4950 ip->i_delayed_blks += alen;
4951 abno = NULLSTARTBLOCK(indlen);
4954 * If first time, allocate and fill in
4955 * once-only bma fields.
4957 if (bma.ip == NULL) {
4965 /* Indicate if this is the first user data
4966 * in the file, or just any user data.
4968 if (!(flags & XFS_BMAPI_METADATA)) {
4969 bma.userdata = (aoff == 0) ?
4970 XFS_ALLOC_INITIAL_USER_DATA :
4974 * Fill in changeable bma fields.
4977 bma.firstblock = *firstblock;
4980 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4981 bma.wasdel = wasdelay;
4982 bma.minlen = minlen;
4983 bma.low = flist->xbf_low;
4984 bma.minleft = minleft;
4986 * Only want to do the alignment at the
4987 * eof if it is userdata and allocation length
4988 * is larger than a stripe unit.
4990 if (mp->m_dalign && alen >= mp->m_dalign &&
4991 (!(flags & XFS_BMAPI_METADATA)) &&
4992 (whichfork == XFS_DATA_FORK)) {
4993 if ((error = xfs_bmap_isaeof(ip, aoff,
4994 whichfork, &bma.aeof)))
5001 if ((error = xfs_bmap_alloc(&bma)))
5004 * Copy out result fields.
5007 if ((flist->xbf_low = bma.low))
5011 ASSERT(*firstblock == NULLFSBLOCK ||
5012 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5013 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
5015 XFS_FSB_TO_AGNO(mp, *firstblock) <
5016 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
5017 *firstblock = bma.firstblock;
5019 cur->bc_private.b.firstblock =
5021 if (abno == NULLFSBLOCK)
5023 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5024 cur = xfs_btree_init_cursor(mp,
5025 tp, NULL, 0, XFS_BTNUM_BMAP,
5027 cur->bc_private.b.firstblock =
5029 cur->bc_private.b.flist = flist;
5032 * Bump the number of extents we've allocated
5038 cur->bc_private.b.flags =
5039 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
5040 got.br_startoff = aoff;
5041 got.br_startblock = abno;
5042 got.br_blockcount = alen;
5043 got.br_state = XFS_EXT_NORM; /* assume normal */
5045 * Determine state of extent, and the filesystem.
5046 * A wasdelay extent has been initialized, so
5047 * shouldn't be flagged as unwritten.
5049 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5050 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5051 got.br_state = XFS_EXT_UNWRITTEN;
5053 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
5054 firstblock, flist, &tmp_logflags, delta,
5055 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5056 logflags |= tmp_logflags;
5059 lastx = ifp->if_lastex;
5060 ep = xfs_iext_get_ext(ifp, lastx);
5061 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5062 xfs_bmbt_get_all(ep, &got);
5063 ASSERT(got.br_startoff <= aoff);
5064 ASSERT(got.br_startoff + got.br_blockcount >=
5067 if (flags & XFS_BMAPI_DELAY) {
5068 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
5069 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
5071 ASSERT(got.br_state == XFS_EXT_NORM ||
5072 got.br_state == XFS_EXT_UNWRITTEN);
5075 * Fall down into the found allocated space case.
5077 } else if (inhole) {
5079 * Reading in a hole.
5081 mval->br_startoff = bno;
5082 mval->br_startblock = HOLESTARTBLOCK;
5083 mval->br_blockcount =
5084 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5085 mval->br_state = XFS_EXT_NORM;
5086 bno += mval->br_blockcount;
5087 len -= mval->br_blockcount;
5093 * Then deal with the allocated space we found.
5096 if (!(flags & XFS_BMAPI_ENTIRE) &&
5097 (got.br_startoff + got.br_blockcount > obno)) {
5100 ASSERT((bno >= obno) || (n == 0));
5102 mval->br_startoff = bno;
5103 if (ISNULLSTARTBLOCK(got.br_startblock)) {
5104 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5105 mval->br_startblock = DELAYSTARTBLOCK;
5107 mval->br_startblock =
5109 (bno - got.br_startoff);
5111 * Return the minimum of what we got and what we
5112 * asked for for the length. We can use the len
5113 * variable here because it is modified below
5114 * and we could have been there before coming
5115 * here if the first part of the allocation
5116 * didn't overlap what was asked for.
5118 mval->br_blockcount =
5119 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5120 (bno - got.br_startoff));
5121 mval->br_state = got.br_state;
5122 ASSERT(mval->br_blockcount <= len);
5125 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
5126 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5127 mval->br_startblock = DELAYSTARTBLOCK;
5132 * Check if writing previously allocated but
5133 * unwritten extents.
5135 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5136 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5138 * Modify (by adding) the state flag, if writing.
5140 ASSERT(mval->br_blockcount <= len);
5141 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5142 cur = xfs_btree_init_cursor(mp,
5143 tp, NULL, 0, XFS_BTNUM_BMAP,
5145 cur->bc_private.b.firstblock =
5147 cur->bc_private.b.flist = flist;
5149 mval->br_state = XFS_EXT_NORM;
5150 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
5151 firstblock, flist, &tmp_logflags, delta,
5152 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5153 logflags |= tmp_logflags;
5156 lastx = ifp->if_lastex;
5157 ep = xfs_iext_get_ext(ifp, lastx);
5158 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5159 xfs_bmbt_get_all(ep, &got);
5161 * We may have combined previously unwritten
5162 * space with written space, so generate
5165 if (mval->br_blockcount < len)
5169 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5170 ((mval->br_startoff + mval->br_blockcount) <= end));
5171 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5172 (mval->br_blockcount <= len) ||
5173 (mval->br_startoff < obno));
5174 bno = mval->br_startoff + mval->br_blockcount;
5176 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5177 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5178 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5179 ASSERT(mval->br_state == mval[-1].br_state);
5180 mval[-1].br_blockcount = mval->br_blockcount;
5181 mval[-1].br_state = mval->br_state;
5182 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5183 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5184 mval[-1].br_startblock != HOLESTARTBLOCK &&
5185 mval->br_startblock ==
5186 mval[-1].br_startblock + mval[-1].br_blockcount &&
5187 ((flags & XFS_BMAPI_IGSTATE) ||
5188 mval[-1].br_state == mval->br_state)) {
5189 ASSERT(mval->br_startoff ==
5190 mval[-1].br_startoff + mval[-1].br_blockcount);
5191 mval[-1].br_blockcount += mval->br_blockcount;
5193 mval->br_startblock == DELAYSTARTBLOCK &&
5194 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5195 mval->br_startoff ==
5196 mval[-1].br_startoff + mval[-1].br_blockcount) {
5197 mval[-1].br_blockcount += mval->br_blockcount;
5198 mval[-1].br_state = mval->br_state;
5199 } else if (!((n == 0) &&
5200 ((mval->br_startoff + mval->br_blockcount) <=
5206 * If we're done, stop now. Stop when we've allocated
5207 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5208 * the transaction may get too big.
5210 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5213 * Else go on to the next record.
5215 ep = xfs_iext_get_ext(ifp, ++lastx);
5217 if (lastx >= nextents)
5220 xfs_bmbt_get_all(ep, &got);
5222 ifp->if_lastex = lastx;
5225 * Transform from btree to extents, give it cur.
5227 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5228 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5230 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5231 &tmp_logflags, whichfork);
5232 logflags |= tmp_logflags;
5236 ASSERT(ifp->if_ext_max ==
5237 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5238 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5239 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5241 if (delta && delta->xed_startoff != NULLFILEOFF) {
5242 /* A change was actually made.
5243 * Note that delta->xed_blockount is an offset at this
5244 * point and needs to be converted to a block count.
5246 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5247 delta->xed_blockcount -= delta->xed_startoff;
5251 * Log everything. Do this after conversion, there's no point in
5252 * logging the extent records if we've converted to btree format.
5254 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5255 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5256 logflags &= ~XFS_ILOG_FEXT(whichfork);
5257 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5258 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5259 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5261 * Log whatever the flags say, even if error. Otherwise we might miss
5262 * detecting a case where the data is changed, there's an error,
5263 * and it's not logged so we don't shutdown when we should.
5267 xfs_trans_log_inode(tp, ip, logflags);
5271 ASSERT(*firstblock == NULLFSBLOCK ||
5272 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5274 cur->bc_private.b.firstblock) ||
5276 XFS_FSB_TO_AGNO(mp, *firstblock) <
5278 cur->bc_private.b.firstblock)));
5279 *firstblock = cur->bc_private.b.firstblock;
5281 xfs_btree_del_cursor(cur,
5282 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5285 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5291 * Map file blocks to filesystem blocks, simple version.
5292 * One block (extent) only, read-only.
5293 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5294 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5295 * was set and all the others were clear.
5299 xfs_trans_t *tp, /* transaction pointer */
5300 xfs_inode_t *ip, /* incore inode */
5301 int whichfork, /* data or attr fork */
5302 xfs_fsblock_t *fsb, /* output: mapped block */
5303 xfs_fileoff_t bno) /* starting file offs. mapped */
5305 int eof; /* we've hit the end of extents */
5306 int error; /* error return */
5307 xfs_bmbt_irec_t got; /* current file extent record */
5308 xfs_ifork_t *ifp; /* inode fork pointer */
5309 xfs_extnum_t lastx; /* last useful extent number */
5310 xfs_bmbt_irec_t prev; /* previous file extent record */
5312 ifp = XFS_IFORK_PTR(ip, whichfork);
5314 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5315 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5316 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5318 return XFS_ERROR(EFSCORRUPTED);
5320 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5321 return XFS_ERROR(EIO);
5322 XFS_STATS_INC(xs_blk_mapr);
5323 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5324 (error = xfs_iread_extents(tp, ip, whichfork)))
5326 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5329 * Reading past eof, act as though there's a hole
5332 if (eof || got.br_startoff > bno) {
5336 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
5337 ASSERT(bno < got.br_startoff + got.br_blockcount);
5338 *fsb = got.br_startblock + (bno - got.br_startoff);
5339 ifp->if_lastex = lastx;
5344 * Unmap (remove) blocks from a file.
5345 * If nexts is nonzero then the number of extents to remove is limited to
5346 * that value. If not all extents in the block range can be removed then
5351 xfs_trans_t *tp, /* transaction pointer */
5352 struct xfs_inode *ip, /* incore inode */
5353 xfs_fileoff_t bno, /* starting offset to unmap */
5354 xfs_filblks_t len, /* length to unmap in file */
5355 int flags, /* misc flags */
5356 xfs_extnum_t nexts, /* number of extents max */
5357 xfs_fsblock_t *firstblock, /* first allocated block
5358 controls a.g. for allocs */
5359 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5360 xfs_extdelta_t *delta, /* o: change made to incore
5362 int *done) /* set if not done yet */
5364 xfs_btree_cur_t *cur; /* bmap btree cursor */
5365 xfs_bmbt_irec_t del; /* extent being deleted */
5366 int eof; /* is deleting at eof */
5367 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
5368 int error; /* error return value */
5369 xfs_extnum_t extno; /* extent number in list */
5370 xfs_bmbt_irec_t got; /* current extent record */
5371 xfs_ifork_t *ifp; /* inode fork pointer */
5372 int isrt; /* freeing in rt area */
5373 xfs_extnum_t lastx; /* last extent index used */
5374 int logflags; /* transaction logging flags */
5375 xfs_extlen_t mod; /* rt extent offset */
5376 xfs_mount_t *mp; /* mount structure */
5377 xfs_extnum_t nextents; /* number of file extents */
5378 xfs_bmbt_irec_t prev; /* previous extent record */
5379 xfs_fileoff_t start; /* first file offset deleted */
5380 int tmp_logflags; /* partial logging flags */
5381 int wasdel; /* was a delayed alloc extent */
5382 int whichfork; /* data or attribute fork */
5383 int rsvd; /* OK to allocate reserved blocks */
5386 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5387 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5388 XFS_ATTR_FORK : XFS_DATA_FORK;
5389 ifp = XFS_IFORK_PTR(ip, whichfork);
5391 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5392 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5393 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5395 return XFS_ERROR(EFSCORRUPTED);
5398 if (XFS_FORCED_SHUTDOWN(mp))
5399 return XFS_ERROR(EIO);
5400 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5403 ASSERT(ifp->if_ext_max ==
5404 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5405 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5406 (error = xfs_iread_extents(tp, ip, whichfork)))
5408 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5409 if (nextents == 0) {
5413 XFS_STATS_INC(xs_blk_unmap);
5414 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5416 bno = start + len - 1;
5417 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5420 delta->xed_startoff = NULLFILEOFF;
5421 delta->xed_blockcount = 0;
5424 * Check to see if the given block number is past the end of the
5425 * file, back up to the last block if so...
5428 ep = xfs_iext_get_ext(ifp, --lastx);
5429 xfs_bmbt_get_all(ep, &got);
5430 bno = got.br_startoff + got.br_blockcount - 1;
5433 if (ifp->if_flags & XFS_IFBROOT) {
5434 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5435 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
5437 cur->bc_private.b.firstblock = *firstblock;
5438 cur->bc_private.b.flist = flist;
5439 cur->bc_private.b.flags = 0;
5443 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5444 (nexts == 0 || extno < nexts)) {
5446 * Is the found extent after a hole in which bno lives?
5447 * Just back up to the previous extent, if so.
5449 if (got.br_startoff > bno) {
5452 ep = xfs_iext_get_ext(ifp, lastx);
5453 xfs_bmbt_get_all(ep, &got);
5456 * Is the last block of this extent before the range
5457 * we're supposed to delete? If so, we're done.
5459 bno = XFS_FILEOFF_MIN(bno,
5460 got.br_startoff + got.br_blockcount - 1);
5464 * Then deal with the (possibly delayed) allocated space
5469 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
5470 if (got.br_startoff < start) {
5471 del.br_startoff = start;
5472 del.br_blockcount -= start - got.br_startoff;
5474 del.br_startblock += start - got.br_startoff;
5476 if (del.br_startoff + del.br_blockcount > bno + 1)
5477 del.br_blockcount = bno + 1 - del.br_startoff;
5478 sum = del.br_startblock + del.br_blockcount;
5480 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5482 * Realtime extent not lined up at the end.
5483 * The extent could have been split into written
5484 * and unwritten pieces, or we could just be
5485 * unmapping part of it. But we can't really
5486 * get rid of part of a realtime extent.
5488 if (del.br_state == XFS_EXT_UNWRITTEN ||
5489 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5491 * This piece is unwritten, or we're not
5492 * using unwritten extents. Skip over it.
5495 bno -= mod > del.br_blockcount ?
5496 del.br_blockcount : mod;
5497 if (bno < got.br_startoff) {
5499 xfs_bmbt_get_all(xfs_iext_get_ext(
5505 * It's written, turn it unwritten.
5506 * This is better than zeroing it.
5508 ASSERT(del.br_state == XFS_EXT_NORM);
5509 ASSERT(xfs_trans_get_block_res(tp) > 0);
5511 * If this spans a realtime extent boundary,
5512 * chop it back to the start of the one we end at.
5514 if (del.br_blockcount > mod) {
5515 del.br_startoff += del.br_blockcount - mod;
5516 del.br_startblock += del.br_blockcount - mod;
5517 del.br_blockcount = mod;
5519 del.br_state = XFS_EXT_UNWRITTEN;
5520 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5521 firstblock, flist, &logflags, delta,
5527 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5529 * Realtime extent is lined up at the end but not
5530 * at the front. We'll get rid of full extents if
5533 mod = mp->m_sb.sb_rextsize - mod;
5534 if (del.br_blockcount > mod) {
5535 del.br_blockcount -= mod;
5536 del.br_startoff += mod;
5537 del.br_startblock += mod;
5538 } else if ((del.br_startoff == start &&
5539 (del.br_state == XFS_EXT_UNWRITTEN ||
5540 xfs_trans_get_block_res(tp) == 0)) ||
5541 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5543 * Can't make it unwritten. There isn't
5544 * a full extent here so just skip it.
5546 ASSERT(bno >= del.br_blockcount);
5547 bno -= del.br_blockcount;
5548 if (bno < got.br_startoff) {
5550 xfs_bmbt_get_all(--ep, &got);
5553 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5555 * This one is already unwritten.
5556 * It must have a written left neighbor.
5557 * Unwrite the killed part of that one and
5561 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5563 ASSERT(prev.br_state == XFS_EXT_NORM);
5564 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
5565 ASSERT(del.br_startblock ==
5566 prev.br_startblock + prev.br_blockcount);
5567 if (prev.br_startoff < start) {
5568 mod = start - prev.br_startoff;
5569 prev.br_blockcount -= mod;
5570 prev.br_startblock += mod;
5571 prev.br_startoff = start;
5573 prev.br_state = XFS_EXT_UNWRITTEN;
5574 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5575 &prev, firstblock, flist, &logflags,
5576 delta, XFS_DATA_FORK, 0);
5581 ASSERT(del.br_state == XFS_EXT_NORM);
5582 del.br_state = XFS_EXT_UNWRITTEN;
5583 error = xfs_bmap_add_extent(ip, lastx, &cur,
5584 &del, firstblock, flist, &logflags,
5585 delta, XFS_DATA_FORK, 0);
5592 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
5593 /* Update realtime/data freespace, unreserve quota */
5595 xfs_filblks_t rtexts;
5597 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5598 do_div(rtexts, mp->m_sb.sb_rextsize);
5599 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5600 (int64_t)rtexts, rsvd);
5601 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5602 NULL, ip, -((long)del.br_blockcount), 0,
5603 XFS_QMOPT_RES_RTBLKS);
5605 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
5606 (int64_t)del.br_blockcount, rsvd);
5607 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5608 NULL, ip, -((long)del.br_blockcount), 0,
5609 XFS_QMOPT_RES_REGBLKS);
5611 ip->i_delayed_blks -= del.br_blockcount;
5613 cur->bc_private.b.flags |=
5614 XFS_BTCUR_BPRV_WASDEL;
5616 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5618 * If it's the case where the directory code is running
5619 * with no block reservation, and the deleted block is in
5620 * the middle of its extent, and the resulting insert
5621 * of an extent would cause transformation to btree format,
5622 * then reject it. The calling code will then swap
5623 * blocks around instead.
5624 * We have to do this now, rather than waiting for the
5625 * conversion to btree format, since the transaction
5628 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5629 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5630 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5631 del.br_startoff > got.br_startoff &&
5632 del.br_startoff + del.br_blockcount <
5633 got.br_startoff + got.br_blockcount) {
5634 error = XFS_ERROR(ENOSPC);
5637 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5638 &tmp_logflags, delta, whichfork, rsvd);
5639 logflags |= tmp_logflags;
5642 bno = del.br_startoff - 1;
5644 lastx = ifp->if_lastex;
5646 * If not done go on to the next (previous) record.
5647 * Reset ep in case the extents array was re-alloced.
5649 ep = xfs_iext_get_ext(ifp, lastx);
5650 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5651 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5652 xfs_bmbt_get_startoff(ep) > bno) {
5654 ep = xfs_iext_get_ext(ifp, lastx);
5657 xfs_bmbt_get_all(ep, &got);
5661 ifp->if_lastex = lastx;
5662 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5663 ASSERT(ifp->if_ext_max ==
5664 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5666 * Convert to a btree if necessary.
5668 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5669 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5670 ASSERT(cur == NULL);
5671 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5672 &cur, 0, &tmp_logflags, whichfork);
5673 logflags |= tmp_logflags;
5678 * transform from btree to extents, give it cur
5680 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5681 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5682 ASSERT(cur != NULL);
5683 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5685 logflags |= tmp_logflags;
5690 * transform from extents to local?
5692 ASSERT(ifp->if_ext_max ==
5693 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5695 if (delta && delta->xed_startoff != NULLFILEOFF) {
5696 /* A change was actually made.
5697 * Note that delta->xed_blockount is an offset at this
5698 * point and needs to be converted to a block count.
5700 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5701 delta->xed_blockcount -= delta->xed_startoff;
5705 * Log everything. Do this after conversion, there's no point in
5706 * logging the extent records if we've converted to btree format.
5708 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5709 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5710 logflags &= ~XFS_ILOG_FEXT(whichfork);
5711 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5712 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5713 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5715 * Log inode even in the error case, if the transaction
5716 * is dirty we'll need to shut down the filesystem.
5719 xfs_trans_log_inode(tp, ip, logflags);
5722 *firstblock = cur->bc_private.b.firstblock;
5723 cur->bc_private.b.allocated = 0;
5725 xfs_btree_del_cursor(cur,
5726 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5732 * returns 1 for success, 0 if we failed to map the extent.
5735 xfs_getbmapx_fix_eof_hole(
5736 xfs_inode_t *ip, /* xfs incore inode pointer */
5737 struct getbmap *out, /* output structure */
5738 int prealloced, /* this is a file with
5739 * preallocated data space */
5740 __int64_t end, /* last block requested */
5741 xfs_fsblock_t startblock)
5744 xfs_mount_t *mp; /* file system mount point */
5746 if (startblock == HOLESTARTBLOCK) {
5748 out->bmv_block = -1;
5749 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5750 fixlen -= out->bmv_offset;
5751 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5752 /* Came to hole at EOF. Trim it. */
5755 out->bmv_length = fixlen;
5758 out->bmv_block = XFS_FSB_TO_DB(ip, startblock);
5765 * Fcntl interface to xfs_bmapi.
5767 int /* error code */
5770 struct getbmap *bmv, /* user bmap structure */
5771 void __user *ap, /* pointer to user's array */
5772 int interface) /* interface flags */
5774 __int64_t bmvend; /* last block requested */
5775 int error; /* return value */
5776 __int64_t fixlen; /* length for -1 case */
5777 int i; /* extent number */
5778 int lock; /* lock state */
5779 xfs_bmbt_irec_t *map; /* buffer for user's data */
5780 xfs_mount_t *mp; /* file system mount point */
5781 int nex; /* # of user extents can do */
5782 int nexleft; /* # of user extents left */
5783 int subnex; /* # of bmapi's can do */
5784 int nmap; /* number of map entries */
5785 struct getbmap out; /* output structure */
5786 int whichfork; /* data or attr fork */
5787 int prealloced; /* this is a file with
5788 * preallocated data space */
5789 int sh_unwritten; /* true, if unwritten */
5790 /* extents listed separately */
5791 int bmapi_flags; /* flags for xfs_bmapi */
5792 __int32_t oflags; /* getbmapx bmv_oflags field */
5796 whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5797 sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
5799 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5800 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5801 * bit is set for the file, generate a read event in order
5802 * that the DMAPI application may do its thing before we return
5803 * the extents. Usually this means restoring user file data to
5804 * regions of the file that look like holes.
5806 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5807 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5808 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5809 * could misinterpret holes in a DMAPI file as true holes,
5810 * when in fact they may represent offline user data.
5812 if ((interface & BMV_IF_NO_DMAPI_READ) == 0 &&
5813 DM_EVENT_ENABLED(ip, DM_EVENT_READ) &&
5814 whichfork == XFS_DATA_FORK) {
5815 error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, 0, 0, 0, NULL);
5817 return XFS_ERROR(error);
5820 if (whichfork == XFS_ATTR_FORK) {
5821 if (XFS_IFORK_Q(ip)) {
5822 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5823 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5824 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5825 return XFS_ERROR(EINVAL);
5826 } else if (unlikely(
5827 ip->i_d.di_aformat != 0 &&
5828 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5829 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5831 return XFS_ERROR(EFSCORRUPTED);
5833 } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5834 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5835 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5836 return XFS_ERROR(EINVAL);
5837 if (whichfork == XFS_DATA_FORK) {
5838 if (xfs_get_extsz_hint(ip) ||
5839 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5841 fixlen = XFS_MAXIOFFSET(mp);
5844 fixlen = ip->i_size;
5851 if (bmv->bmv_length == -1) {
5852 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5853 bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
5855 } else if (bmv->bmv_length < 0)
5856 return XFS_ERROR(EINVAL);
5857 if (bmv->bmv_length == 0) {
5858 bmv->bmv_entries = 0;
5861 nex = bmv->bmv_count - 1;
5863 return XFS_ERROR(EINVAL);
5864 bmvend = bmv->bmv_offset + bmv->bmv_length;
5866 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5868 if (whichfork == XFS_DATA_FORK &&
5869 (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size)) {
5870 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
5871 error = xfs_flush_pages(ip, (xfs_off_t)0,
5874 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5879 ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
5881 lock = xfs_ilock_map_shared(ip);
5884 * Don't let nex be bigger than the number of extents
5885 * we can have assuming alternating holes and real extents.
5887 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5888 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5890 bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
5891 ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
5894 * Allocate enough space to handle "subnex" maps at a time.
5897 map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
5899 bmv->bmv_entries = 0;
5901 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
5903 goto unlock_and_return;
5909 nmap = (nexleft > subnex) ? subnex : nexleft;
5910 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5911 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5912 bmapi_flags, NULL, 0, map, &nmap,
5915 goto unlock_and_return;
5916 ASSERT(nmap <= subnex);
5918 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5920 oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
5921 BMV_OF_PREALLOC : 0;
5922 out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
5923 out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5924 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
5925 if (map[i].br_startblock == HOLESTARTBLOCK &&
5926 whichfork == XFS_ATTR_FORK) {
5927 /* came to the end of attribute fork */
5928 goto unlock_and_return;
5930 if (!xfs_getbmapx_fix_eof_hole(ip, &out,
5932 map[i].br_startblock)) {
5933 goto unlock_and_return;
5936 /* return either getbmap/getbmapx structure. */
5937 if (interface & BMV_IF_EXTENDED) {
5938 struct getbmapx outx;
5940 GETBMAP_CONVERT(out,outx);
5941 outx.bmv_oflags = oflags;
5942 outx.bmv_unused1 = outx.bmv_unused2 = 0;
5943 if (copy_to_user(ap, &outx,
5945 error = XFS_ERROR(EFAULT);
5946 goto unlock_and_return;
5949 if (copy_to_user(ap, &out,
5951 error = XFS_ERROR(EFAULT);
5952 goto unlock_and_return;
5956 out.bmv_offset + out.bmv_length;
5957 bmv->bmv_length = MAX((__int64_t)0,
5958 (__int64_t)(bmvend - bmv->bmv_offset));
5960 ap = (interface & BMV_IF_EXTENDED) ?
5962 ((struct getbmapx __user *)ap + 1) :
5964 ((struct getbmap __user *)ap + 1);
5967 } while (nmap && nexleft && bmv->bmv_length);
5970 xfs_iunlock_map_shared(ip, lock);
5971 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5979 * Check the last inode extent to determine whether this allocation will result
5980 * in blocks being allocated at the end of the file. When we allocate new data
5981 * blocks at the end of the file which do not start at the previous data block,
5982 * we will try to align the new blocks at stripe unit boundaries.
5984 STATIC int /* error */
5986 xfs_inode_t *ip, /* incore inode pointer */
5987 xfs_fileoff_t off, /* file offset in fsblocks */
5988 int whichfork, /* data or attribute fork */
5989 char *aeof) /* return value */
5991 int error; /* error return value */
5992 xfs_ifork_t *ifp; /* inode fork pointer */
5993 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
5994 xfs_extnum_t nextents; /* number of file extents */
5995 xfs_bmbt_irec_t s; /* expanded extent record */
5997 ASSERT(whichfork == XFS_DATA_FORK);
5998 ifp = XFS_IFORK_PTR(ip, whichfork);
5999 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6000 (error = xfs_iread_extents(NULL, ip, whichfork)))
6002 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6003 if (nextents == 0) {
6008 * Go to the last extent
6010 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6011 xfs_bmbt_get_all(lastrec, &s);
6013 * Check we are allocating in the last extent (for delayed allocations)
6014 * or past the last extent for non-delayed allocations.
6016 *aeof = (off >= s.br_startoff &&
6017 off < s.br_startoff + s.br_blockcount &&
6018 ISNULLSTARTBLOCK(s.br_startblock)) ||
6019 off >= s.br_startoff + s.br_blockcount;
6024 * Check if the endoff is outside the last extent. If so the caller will grow
6025 * the allocation to a stripe unit boundary.
6029 xfs_inode_t *ip, /* incore inode pointer */
6030 xfs_fileoff_t endoff, /* file offset in fsblocks */
6031 int whichfork, /* data or attribute fork */
6032 int *eof) /* result value */
6034 xfs_fsblock_t blockcount; /* extent block count */
6035 int error; /* error return value */
6036 xfs_ifork_t *ifp; /* inode fork pointer */
6037 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
6038 xfs_extnum_t nextents; /* number of file extents */
6039 xfs_fileoff_t startoff; /* extent starting file offset */
6041 ASSERT(whichfork == XFS_DATA_FORK);
6042 ifp = XFS_IFORK_PTR(ip, whichfork);
6043 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6044 (error = xfs_iread_extents(NULL, ip, whichfork)))
6046 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6047 if (nextents == 0) {
6052 * Go to the last extent
6054 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6055 startoff = xfs_bmbt_get_startoff(lastrec);
6056 blockcount = xfs_bmbt_get_blockcount(lastrec);
6057 *eof = endoff >= startoff + blockcount;
6065 xfs_btree_cur_t *cur,
6075 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6076 bp = cur->bc_bufs[i];
6078 if (XFS_BUF_ADDR(bp) == bno)
6079 break; /* Found it */
6081 if (i == XFS_BTREE_MAXLEVELS)
6084 if (!bp) { /* Chase down all the log items to see if the bp is there */
6085 xfs_log_item_chunk_t *licp;
6089 licp = &tp->t_items;
6090 while (!bp && licp != NULL) {
6091 if (XFS_LIC_ARE_ALL_FREE(licp)) {
6092 licp = licp->lic_next;
6095 for (i = 0; i < licp->lic_unused; i++) {
6096 xfs_log_item_desc_t *lidp;
6097 xfs_log_item_t *lip;
6098 xfs_buf_log_item_t *bip;
6101 if (XFS_LIC_ISFREE(licp, i)) {
6105 lidp = XFS_LIC_SLOT(licp, i);
6106 lip = lidp->lid_item;
6107 if (lip->li_type != XFS_LI_BUF)
6110 bip = (xfs_buf_log_item_t *)lip;
6113 if (XFS_BUF_ADDR(lbp) == bno) {
6115 break; /* Found it */
6118 licp = licp->lic_next;
6126 xfs_bmbt_block_t *block,
6132 __be64 *pp, *thispa; /* pointer to block address */
6133 xfs_bmbt_key_t *prevp, *keyp;
6135 ASSERT(be16_to_cpu(block->bb_level) > 0);
6138 for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
6139 dmxr = mp->m_bmap_dmxr[0];
6142 keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
6144 keyp = XFS_BTREE_KEY_ADDR(xfs_bmbt, block, i);
6148 xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
6153 * Compare the block numbers to see if there are dups.
6157 pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
6159 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, i, dmxr);
6161 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
6163 thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
6165 thispa = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, j,
6168 if (*thispa == *pp) {
6169 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
6171 (unsigned long long)be64_to_cpu(*thispa));
6172 panic("%s: ptrs are equal in node\n",
6180 * Check that the extents for the inode ip are in the right order in all
6185 xfs_bmap_check_leaf_extents(
6186 xfs_btree_cur_t *cur, /* btree cursor or null */
6187 xfs_inode_t *ip, /* incore inode pointer */
6188 int whichfork) /* data or attr fork */
6190 xfs_bmbt_block_t *block; /* current btree block */
6191 xfs_fsblock_t bno; /* block # of "block" */
6192 xfs_buf_t *bp; /* buffer for "block" */
6193 int error; /* error return value */
6194 xfs_extnum_t i=0, j; /* index into the extents list */
6195 xfs_ifork_t *ifp; /* fork structure */
6196 int level; /* btree level, for checking */
6197 xfs_mount_t *mp; /* file system mount structure */
6198 __be64 *pp; /* pointer to block address */
6199 xfs_bmbt_rec_t *ep; /* pointer to current extent */
6200 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
6201 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
6204 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6210 ifp = XFS_IFORK_PTR(ip, whichfork);
6211 block = ifp->if_broot;
6213 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6215 level = be16_to_cpu(block->bb_level);
6217 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6218 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6219 bno = be64_to_cpu(*pp);
6221 ASSERT(bno != NULLDFSBNO);
6222 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6223 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6226 * Go down the tree until leaf level is reached, following the first
6227 * pointer (leftmost) at each level.
6229 while (level-- > 0) {
6230 /* See if buf is in cur first */
6231 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6237 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6238 XFS_BMAP_BTREE_REF)))
6240 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6241 XFS_WANT_CORRUPTED_GOTO(
6242 XFS_BMAP_SANITY_CHECK(mp, block, level),
6248 * Check this block for basic sanity (increasing keys and
6249 * no duplicate blocks).
6252 xfs_check_block(block, mp, 0, 0);
6253 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
6254 bno = be64_to_cpu(*pp);
6255 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
6258 xfs_trans_brelse(NULL, bp);
6263 * Here with bp and block set to the leftmost leaf node in the tree.
6268 * Loop over all leaf nodes checking that all extents are in the right order.
6271 xfs_fsblock_t nextbno;
6272 xfs_extnum_t num_recs;
6275 num_recs = be16_to_cpu(block->bb_numrecs);
6278 * Read-ahead the next leaf block, if any.
6281 nextbno = be64_to_cpu(block->bb_rightsib);
6284 * Check all the extents to make sure they are OK.
6285 * If we had a previous block, the last entry should
6286 * conform with the first entry in this one.
6289 ep = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
6291 xfs_btree_check_rec(XFS_BTNUM_BMAP, &last, ep);
6293 for (j = 1; j < num_recs; j++) {
6294 nextp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, j + 1);
6295 xfs_btree_check_rec(XFS_BTNUM_BMAP, ep, nextp);
6303 xfs_trans_brelse(NULL, bp);
6307 * If we've reached the end, stop.
6309 if (bno == NULLFSBLOCK)
6312 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6318 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6319 XFS_BMAP_BTREE_REF)))
6321 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6325 xfs_trans_brelse(NULL, bp);
6330 cmn_err(CE_WARN, "%s: at error0", __func__);
6332 xfs_trans_brelse(NULL, bp);
6334 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
6336 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
6342 * Count fsblocks of the given fork.
6345 xfs_bmap_count_blocks(
6346 xfs_trans_t *tp, /* transaction pointer */
6347 xfs_inode_t *ip, /* incore inode */
6348 int whichfork, /* data or attr fork */
6349 int *count) /* out: count of blocks */
6351 xfs_bmbt_block_t *block; /* current btree block */
6352 xfs_fsblock_t bno; /* block # of "block" */
6353 xfs_ifork_t *ifp; /* fork structure */
6354 int level; /* btree level, for checking */
6355 xfs_mount_t *mp; /* file system mount structure */
6356 __be64 *pp; /* pointer to block address */
6360 ifp = XFS_IFORK_PTR(ip, whichfork);
6361 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6362 if (unlikely(xfs_bmap_count_leaves(ifp, 0,
6363 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6365 XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)",
6366 XFS_ERRLEVEL_LOW, mp);
6367 return XFS_ERROR(EFSCORRUPTED);
6373 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6375 block = ifp->if_broot;
6376 level = be16_to_cpu(block->bb_level);
6378 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6379 bno = be64_to_cpu(*pp);
6380 ASSERT(bno != NULLDFSBNO);
6381 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6382 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6384 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
6385 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6387 return XFS_ERROR(EFSCORRUPTED);
6394 * Recursively walks each level of a btree
6395 * to count total fsblocks is use.
6397 STATIC int /* error */
6398 xfs_bmap_count_tree(
6399 xfs_mount_t *mp, /* file system mount point */
6400 xfs_trans_t *tp, /* transaction pointer */
6401 xfs_ifork_t *ifp, /* inode fork pointer */
6402 xfs_fsblock_t blockno, /* file system block number */
6403 int levelin, /* level in btree */
6404 int *count) /* Count of blocks */
6407 xfs_buf_t *bp, *nbp;
6408 int level = levelin;
6410 xfs_fsblock_t bno = blockno;
6411 xfs_fsblock_t nextbno;
6412 xfs_bmbt_block_t *block, *nextblock;
6415 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6418 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6421 /* Not at node above leafs, count this level of nodes */
6422 nextbno = be64_to_cpu(block->bb_rightsib);
6423 while (nextbno != NULLFSBLOCK) {
6424 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6425 0, &nbp, XFS_BMAP_BTREE_REF)))
6428 nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
6429 nextbno = be64_to_cpu(nextblock->bb_rightsib);
6430 xfs_trans_brelse(tp, nbp);
6433 /* Dive to the next level */
6434 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
6435 bno = be64_to_cpu(*pp);
6436 if (unlikely((error =
6437 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6438 xfs_trans_brelse(tp, bp);
6439 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6440 XFS_ERRLEVEL_LOW, mp);
6441 return XFS_ERROR(EFSCORRUPTED);
6443 xfs_trans_brelse(tp, bp);
6445 /* count all level 1 nodes and their leaves */
6447 nextbno = be64_to_cpu(block->bb_rightsib);
6448 numrecs = be16_to_cpu(block->bb_numrecs);
6449 if (unlikely(xfs_bmap_disk_count_leaves(0,
6450 block, numrecs, count) < 0)) {
6451 xfs_trans_brelse(tp, bp);
6452 XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
6453 XFS_ERRLEVEL_LOW, mp);
6454 return XFS_ERROR(EFSCORRUPTED);
6456 xfs_trans_brelse(tp, bp);
6457 if (nextbno == NULLFSBLOCK)
6460 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6461 XFS_BMAP_BTREE_REF)))
6464 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6471 * Count leaf blocks given a range of extent records.
6474 xfs_bmap_count_leaves(
6482 for (b = 0; b < numrecs; b++) {
6483 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
6484 *count += xfs_bmbt_get_blockcount(frp);
6490 * Count leaf blocks given a range of extent records originally
6494 xfs_bmap_disk_count_leaves(
6496 xfs_bmbt_block_t *block,
6501 xfs_bmbt_rec_t *frp;
6503 for (b = 1; b <= numrecs; b++) {
6504 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, idx + b);
6505 *count += xfs_bmbt_disk_get_blockcount(frp);