2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir_sf.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
38 #include "xfs_btree.h"
39 #include "xfs_dmapi.h"
40 #include "xfs_mount.h"
41 #include "xfs_ialloc.h"
42 #include "xfs_itable.h"
43 #include "xfs_inode_item.h"
44 #include "xfs_extfree_item.h"
45 #include "xfs_alloc.h"
47 #include "xfs_rtalloc.h"
48 #include "xfs_error.h"
49 #include "xfs_dir_leaf.h"
50 #include "xfs_attr_leaf.h"
52 #include "xfs_quota.h"
53 #include "xfs_trans_space.h"
54 #include "xfs_buf_item.h"
59 xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
62 kmem_zone_t *xfs_bmap_free_item_zone;
65 * Prototypes for internal bmap routines.
70 * Called from xfs_bmap_add_attrfork to handle extents format files.
72 STATIC int /* error */
73 xfs_bmap_add_attrfork_extents(
74 xfs_trans_t *tp, /* transaction pointer */
75 xfs_inode_t *ip, /* incore inode pointer */
76 xfs_fsblock_t *firstblock, /* first block allocated */
77 xfs_bmap_free_t *flist, /* blocks to free at commit */
78 int *flags); /* inode logging flags */
81 * Called from xfs_bmap_add_attrfork to handle local format files.
83 STATIC int /* error */
84 xfs_bmap_add_attrfork_local(
85 xfs_trans_t *tp, /* transaction pointer */
86 xfs_inode_t *ip, /* incore inode pointer */
87 xfs_fsblock_t *firstblock, /* first block allocated */
88 xfs_bmap_free_t *flist, /* blocks to free at commit */
89 int *flags); /* inode logging flags */
92 * Called by xfs_bmapi to update extent list structure and the btree
93 * after allocating space (or doing a delayed allocation).
95 STATIC int /* error */
97 xfs_inode_t *ip, /* incore inode pointer */
98 xfs_extnum_t idx, /* extent number to update/insert */
99 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
100 xfs_bmbt_irec_t *new, /* new data to put in extent list */
101 xfs_fsblock_t *first, /* pointer to firstblock variable */
102 xfs_bmap_free_t *flist, /* list of extents to be freed */
103 int *logflagsp, /* inode logging flags */
104 int whichfork, /* data or attr fork */
105 int rsvd); /* OK to allocate reserved blocks */
108 * Called by xfs_bmap_add_extent to handle cases converting a delayed
109 * allocation to a real allocation.
111 STATIC int /* error */
112 xfs_bmap_add_extent_delay_real(
113 xfs_inode_t *ip, /* incore inode pointer */
114 xfs_extnum_t idx, /* extent number to update/insert */
115 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
116 xfs_bmbt_irec_t *new, /* new data to put in extent list */
117 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
118 xfs_fsblock_t *first, /* pointer to firstblock variable */
119 xfs_bmap_free_t *flist, /* list of extents to be freed */
120 int *logflagsp, /* inode logging flags */
121 int rsvd); /* OK to allocate reserved blocks */
124 * Called by xfs_bmap_add_extent to handle cases converting a hole
125 * to a delayed allocation.
127 STATIC int /* error */
128 xfs_bmap_add_extent_hole_delay(
129 xfs_inode_t *ip, /* incore inode pointer */
130 xfs_extnum_t idx, /* extent number to update/insert */
131 xfs_btree_cur_t *cur, /* if null, not a btree */
132 xfs_bmbt_irec_t *new, /* new data to put in extent list */
133 int *logflagsp,/* inode logging flags */
134 int rsvd); /* OK to allocate reserved blocks */
137 * Called by xfs_bmap_add_extent to handle cases converting a hole
138 * to a real allocation.
140 STATIC int /* error */
141 xfs_bmap_add_extent_hole_real(
142 xfs_inode_t *ip, /* incore inode pointer */
143 xfs_extnum_t idx, /* extent number to update/insert */
144 xfs_btree_cur_t *cur, /* if null, not a btree */
145 xfs_bmbt_irec_t *new, /* new data to put in extent list */
146 int *logflagsp, /* inode logging flags */
147 int whichfork); /* data or attr fork */
150 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
151 * allocation to a real allocation or vice versa.
153 STATIC int /* error */
154 xfs_bmap_add_extent_unwritten_real(
155 xfs_inode_t *ip, /* incore inode pointer */
156 xfs_extnum_t idx, /* extent number to update/insert */
157 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
158 xfs_bmbt_irec_t *new, /* new data to put in extent list */
159 int *logflagsp); /* inode logging flags */
162 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
163 * It figures out where to ask the underlying allocator to put the new extent.
165 STATIC int /* error */
167 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
170 * Transform a btree format file with only one leaf node, where the
171 * extents list will fit in the inode, into an extents format file.
172 * Since the extent list is already in-core, all we have to do is
173 * give up the space for the btree root and pitch the leaf block.
175 STATIC int /* error */
176 xfs_bmap_btree_to_extents(
177 xfs_trans_t *tp, /* transaction pointer */
178 xfs_inode_t *ip, /* incore inode pointer */
179 xfs_btree_cur_t *cur, /* btree cursor */
180 int *logflagsp, /* inode logging flags */
181 int whichfork); /* data or attr fork */
185 * Check that the extents list for the inode ip is in the right order.
188 xfs_bmap_check_extents(
189 xfs_inode_t *ip, /* incore inode pointer */
190 int whichfork); /* data or attr fork */
194 * Called by xfs_bmapi to update extent list structure and the btree
195 * after removing space (or undoing a delayed allocation).
197 STATIC int /* error */
199 xfs_inode_t *ip, /* incore inode pointer */
200 xfs_trans_t *tp, /* current trans pointer */
201 xfs_extnum_t idx, /* extent number to update/insert */
202 xfs_bmap_free_t *flist, /* list of extents to be freed */
203 xfs_btree_cur_t *cur, /* if null, not a btree */
204 xfs_bmbt_irec_t *new, /* new data to put in extent list */
205 int *logflagsp,/* inode logging flags */
206 int whichfork, /* data or attr fork */
207 int rsvd); /* OK to allocate reserved blocks */
210 * Remove the entry "free" from the free item list. Prev points to the
211 * previous entry, unless "free" is the head of the list.
215 xfs_bmap_free_t *flist, /* free item list header */
216 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
217 xfs_bmap_free_item_t *free); /* list item to be freed */
220 * Remove count entries from the extents array for inode "ip", starting
221 * at index "idx". Copies the remaining items down over the deleted ones,
222 * and gives back the excess memory.
225 xfs_bmap_delete_exlist(
226 xfs_inode_t *ip, /* incode inode pointer */
227 xfs_extnum_t idx, /* starting delete index */
228 xfs_extnum_t count, /* count of items to delete */
229 int whichfork); /* data or attr fork */
232 * Convert an extents-format file into a btree-format file.
233 * The new file will have a root block (in the inode) and a single child block.
235 STATIC int /* error */
236 xfs_bmap_extents_to_btree(
237 xfs_trans_t *tp, /* transaction pointer */
238 xfs_inode_t *ip, /* incore inode pointer */
239 xfs_fsblock_t *firstblock, /* first-block-allocated */
240 xfs_bmap_free_t *flist, /* blocks freed in xaction */
241 xfs_btree_cur_t **curp, /* cursor returned to caller */
242 int wasdel, /* converting a delayed alloc */
243 int *logflagsp, /* inode logging flags */
244 int whichfork); /* data or attr fork */
247 * Insert new item(s) in the extent list for inode "ip".
248 * Count new items are inserted at offset idx.
251 xfs_bmap_insert_exlist(
252 xfs_inode_t *ip, /* incore inode pointer */
253 xfs_extnum_t idx, /* starting index of new items */
254 xfs_extnum_t count, /* number of inserted items */
255 xfs_bmbt_irec_t *new, /* items to insert */
256 int whichfork); /* data or attr fork */
259 * Convert a local file to an extents file.
260 * This code is sort of bogus, since the file data needs to get
261 * logged so it won't be lost. The bmap-level manipulations are ok, though.
263 STATIC int /* error */
264 xfs_bmap_local_to_extents(
265 xfs_trans_t *tp, /* transaction pointer */
266 xfs_inode_t *ip, /* incore inode pointer */
267 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
268 xfs_extlen_t total, /* total blocks needed by transaction */
269 int *logflagsp, /* inode logging flags */
270 int whichfork); /* data or attr fork */
273 * Search the extents list for the inode, for the extent containing bno.
274 * If bno lies in a hole, point to the next entry. If bno lies past eof,
275 * *eofp will be set, and *prevp will contain the last entry (null if none).
276 * Else, *lastxp will be set to the index of the found
277 * entry; *gotp will contain the entry.
279 STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
280 xfs_bmap_search_extents(
281 xfs_inode_t *ip, /* incore inode pointer */
282 xfs_fileoff_t bno, /* block number searched for */
283 int whichfork, /* data or attr fork */
284 int *eofp, /* out: end of file found */
285 xfs_extnum_t *lastxp, /* out: last extent index */
286 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
287 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
290 * Check the last inode extent to determine whether this allocation will result
291 * in blocks being allocated at the end of the file. When we allocate new data
292 * blocks at the end of the file which do not start at the previous data block,
293 * we will try to align the new blocks at stripe unit boundaries.
295 STATIC int /* error */
297 xfs_inode_t *ip, /* incore inode pointer */
298 xfs_fileoff_t off, /* file offset in fsblocks */
299 int whichfork, /* data or attribute fork */
300 char *aeof); /* return value */
302 #ifdef XFS_BMAP_TRACE
304 * Add a bmap trace buffer entry. Base routine for the others.
307 xfs_bmap_trace_addentry(
308 int opcode, /* operation */
309 char *fname, /* function name */
310 char *desc, /* operation description */
311 xfs_inode_t *ip, /* incore inode pointer */
312 xfs_extnum_t idx, /* index of entry(ies) */
313 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
314 xfs_bmbt_rec_t *r1, /* first record */
315 xfs_bmbt_rec_t *r2, /* second record or null */
316 int whichfork); /* data or attr fork */
319 * Add bmap trace entry prior to a call to xfs_bmap_delete_exlist.
322 xfs_bmap_trace_delete(
323 char *fname, /* function name */
324 char *desc, /* operation description */
325 xfs_inode_t *ip, /* incore inode pointer */
326 xfs_extnum_t idx, /* index of entry(entries) deleted */
327 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
328 int whichfork); /* data or attr fork */
331 * Add bmap trace entry prior to a call to xfs_bmap_insert_exlist, or
332 * reading in the extents list from the disk (in the btree).
335 xfs_bmap_trace_insert(
336 char *fname, /* function name */
337 char *desc, /* operation description */
338 xfs_inode_t *ip, /* incore inode pointer */
339 xfs_extnum_t idx, /* index of entry(entries) inserted */
340 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
341 xfs_bmbt_irec_t *r1, /* inserted record 1 */
342 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
343 int whichfork); /* data or attr fork */
346 * Add bmap trace entry after updating an extent list entry in place.
349 xfs_bmap_trace_post_update(
350 char *fname, /* function name */
351 char *desc, /* operation description */
352 xfs_inode_t *ip, /* incore inode pointer */
353 xfs_extnum_t idx, /* index of entry updated */
354 int whichfork); /* data or attr fork */
357 * Add bmap trace entry prior to updating an extent list entry in place.
360 xfs_bmap_trace_pre_update(
361 char *fname, /* function name */
362 char *desc, /* operation description */
363 xfs_inode_t *ip, /* incore inode pointer */
364 xfs_extnum_t idx, /* index of entry to be updated */
365 int whichfork); /* data or attr fork */
368 #define xfs_bmap_trace_delete(f,d,ip,i,c,w)
369 #define xfs_bmap_trace_insert(f,d,ip,i,c,r1,r2,w)
370 #define xfs_bmap_trace_post_update(f,d,ip,i,w)
371 #define xfs_bmap_trace_pre_update(f,d,ip,i,w)
372 #endif /* XFS_BMAP_TRACE */
375 * Compute the worst-case number of indirect blocks that will be used
376 * for ip's delayed extent of length "len".
379 xfs_bmap_worst_indlen(
380 xfs_inode_t *ip, /* incore inode pointer */
381 xfs_filblks_t len); /* delayed extent length */
385 * Perform various validation checks on the values being returned
389 xfs_bmap_validate_ret(
393 xfs_bmbt_irec_t *mval,
397 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
400 #if defined(XFS_RW_TRACE)
409 #define xfs_bunmap_trace(ip, bno, len, flags, ra)
410 #endif /* XFS_RW_TRACE */
416 xfs_fsblock_t blockno,
421 xfs_bmap_count_leaves(
427 xfs_bmap_disk_count_leaves(
433 * Bmap internal routines.
437 * Called from xfs_bmap_add_attrfork to handle btree format files.
439 STATIC int /* error */
440 xfs_bmap_add_attrfork_btree(
441 xfs_trans_t *tp, /* transaction pointer */
442 xfs_inode_t *ip, /* incore inode pointer */
443 xfs_fsblock_t *firstblock, /* first block allocated */
444 xfs_bmap_free_t *flist, /* blocks to free at commit */
445 int *flags) /* inode logging flags */
447 xfs_btree_cur_t *cur; /* btree cursor */
448 int error; /* error return value */
449 xfs_mount_t *mp; /* file system mount struct */
450 int stat; /* newroot status */
453 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
454 *flags |= XFS_ILOG_DBROOT;
456 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
458 cur->bc_private.b.flist = flist;
459 cur->bc_private.b.firstblock = *firstblock;
460 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
462 ASSERT(stat == 1); /* must be at least one entry */
463 if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
466 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
467 return XFS_ERROR(ENOSPC);
469 *firstblock = cur->bc_private.b.firstblock;
470 cur->bc_private.b.allocated = 0;
471 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
475 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
480 * Called from xfs_bmap_add_attrfork to handle extents format files.
482 STATIC int /* error */
483 xfs_bmap_add_attrfork_extents(
484 xfs_trans_t *tp, /* transaction pointer */
485 xfs_inode_t *ip, /* incore inode pointer */
486 xfs_fsblock_t *firstblock, /* first block allocated */
487 xfs_bmap_free_t *flist, /* blocks to free at commit */
488 int *flags) /* inode logging flags */
490 xfs_btree_cur_t *cur; /* bmap btree cursor */
491 int error; /* error return value */
493 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
496 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
497 flags, XFS_DATA_FORK);
499 cur->bc_private.b.allocated = 0;
500 xfs_btree_del_cursor(cur,
501 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
507 * Called from xfs_bmap_add_attrfork to handle local format files.
509 STATIC int /* error */
510 xfs_bmap_add_attrfork_local(
511 xfs_trans_t *tp, /* transaction pointer */
512 xfs_inode_t *ip, /* incore inode pointer */
513 xfs_fsblock_t *firstblock, /* first block allocated */
514 xfs_bmap_free_t *flist, /* blocks to free at commit */
515 int *flags) /* inode logging flags */
517 xfs_da_args_t dargs; /* args for dir/attr code */
518 int error; /* error return value */
519 xfs_mount_t *mp; /* mount structure pointer */
521 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
523 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
525 memset(&dargs, 0, sizeof(dargs));
527 dargs.firstblock = firstblock;
529 dargs.total = mp->m_dirblkfsbs;
530 dargs.whichfork = XFS_DATA_FORK;
532 error = XFS_DIR_SHORTFORM_TO_SINGLE(mp, &dargs);
534 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
540 * Called by xfs_bmapi to update extent list structure and the btree
541 * after allocating space (or doing a delayed allocation).
543 STATIC int /* error */
545 xfs_inode_t *ip, /* incore inode pointer */
546 xfs_extnum_t idx, /* extent number to update/insert */
547 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
548 xfs_bmbt_irec_t *new, /* new data to put in extent list */
549 xfs_fsblock_t *first, /* pointer to firstblock variable */
550 xfs_bmap_free_t *flist, /* list of extents to be freed */
551 int *logflagsp, /* inode logging flags */
552 int whichfork, /* data or attr fork */
553 int rsvd) /* OK to use reserved data blocks */
555 xfs_btree_cur_t *cur; /* btree cursor or null */
556 xfs_filblks_t da_new; /* new count del alloc blocks used */
557 xfs_filblks_t da_old; /* old count del alloc blocks used */
558 int error; /* error return value */
559 #ifdef XFS_BMAP_TRACE
560 static char fname[] = "xfs_bmap_add_extent";
562 xfs_ifork_t *ifp; /* inode fork ptr */
563 int logflags; /* returned value */
564 xfs_extnum_t nextents; /* number of extents in file now */
566 XFS_STATS_INC(xs_add_exlist);
568 ifp = XFS_IFORK_PTR(ip, whichfork);
569 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
570 ASSERT(idx <= nextents);
574 * This is the first extent added to a new/empty file.
575 * Special case this one, so other routines get to assume there are
576 * already extents in the list.
579 xfs_bmap_trace_insert(fname, "insert empty", ip, 0, 1, new,
581 xfs_bmap_insert_exlist(ip, 0, 1, new, whichfork);
584 if (!ISNULLSTARTBLOCK(new->br_startblock)) {
585 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
586 logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
591 * Any kind of new delayed allocation goes here.
593 else if (ISNULLSTARTBLOCK(new->br_startblock)) {
595 ASSERT((cur->bc_private.b.flags &
596 XFS_BTCUR_BPRV_WASDEL) == 0);
597 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, cur, new,
602 * Real allocation off the end of the file.
604 else if (idx == nextents) {
606 ASSERT((cur->bc_private.b.flags &
607 XFS_BTCUR_BPRV_WASDEL) == 0);
608 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
609 &logflags, whichfork)))
612 xfs_bmbt_irec_t prev; /* old extent at offset idx */
615 * Get the record referred to by idx.
617 xfs_bmbt_get_all(&ifp->if_u1.if_extents[idx], &prev);
619 * If it's a real allocation record, and the new allocation ends
620 * after the start of the referred to record, then we're filling
621 * in a delayed or unwritten allocation with a real one, or
622 * converting real back to unwritten.
624 if (!ISNULLSTARTBLOCK(new->br_startblock) &&
625 new->br_startoff + new->br_blockcount > prev.br_startoff) {
626 if (prev.br_state != XFS_EXT_UNWRITTEN &&
627 ISNULLSTARTBLOCK(prev.br_startblock)) {
628 da_old = STARTBLOCKVAL(prev.br_startblock);
630 ASSERT(cur->bc_private.b.flags &
631 XFS_BTCUR_BPRV_WASDEL);
632 if ((error = xfs_bmap_add_extent_delay_real(ip,
633 idx, &cur, new, &da_new, first, flist,
636 } else if (new->br_state == XFS_EXT_NORM) {
637 ASSERT(new->br_state == XFS_EXT_NORM);
638 if ((error = xfs_bmap_add_extent_unwritten_real(
639 ip, idx, &cur, new, &logflags)))
642 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
643 if ((error = xfs_bmap_add_extent_unwritten_real(
644 ip, idx, &cur, new, &logflags)))
647 ASSERT(*curp == cur || *curp == NULL);
650 * Otherwise we're filling in a hole with an allocation.
654 ASSERT((cur->bc_private.b.flags &
655 XFS_BTCUR_BPRV_WASDEL) == 0);
656 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
657 new, &logflags, whichfork)))
662 ASSERT(*curp == cur || *curp == NULL);
664 * Convert to a btree if necessary.
666 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
667 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
668 int tmp_logflags; /* partial log flag return val */
671 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
672 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
673 logflags |= tmp_logflags;
678 * Adjust for changes in reserved delayed indirect blocks.
679 * Nothing to do for disk quotas here.
681 if (da_old || da_new) {
686 nblks += cur->bc_private.b.allocated;
687 ASSERT(nblks <= da_old);
689 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
690 (int)(da_old - nblks), rsvd);
693 * Clear out the allocated field, done with it now in any case.
696 cur->bc_private.b.allocated = 0;
702 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
704 *logflagsp = logflags;
709 * Called by xfs_bmap_add_extent to handle cases converting a delayed
710 * allocation to a real allocation.
712 STATIC int /* error */
713 xfs_bmap_add_extent_delay_real(
714 xfs_inode_t *ip, /* incore inode pointer */
715 xfs_extnum_t idx, /* extent number to update/insert */
716 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
717 xfs_bmbt_irec_t *new, /* new data to put in extent list */
718 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
719 xfs_fsblock_t *first, /* pointer to firstblock variable */
720 xfs_bmap_free_t *flist, /* list of extents to be freed */
721 int *logflagsp, /* inode logging flags */
722 int rsvd) /* OK to use reserved data block allocation */
724 xfs_bmbt_rec_t *base; /* base of extent entry list */
725 xfs_btree_cur_t *cur; /* btree cursor */
726 int diff; /* temp value */
727 xfs_bmbt_rec_t *ep; /* extent entry for idx */
728 int error; /* error return value */
729 #ifdef XFS_BMAP_TRACE
730 static char fname[] = "xfs_bmap_add_extent_delay_real";
732 int i; /* temp state */
733 xfs_fileoff_t new_endoff; /* end offset of new entry */
734 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
735 /* left is 0, right is 1, prev is 2 */
736 int rval=0; /* return value (logging flags) */
737 int state = 0;/* state bits, accessed thru macros */
738 xfs_filblks_t temp; /* value for dnew calculations */
739 xfs_filblks_t temp2; /* value for dnew calculations */
740 int tmp_rval; /* partial logging flags */
741 enum { /* bit number definitions for state */
742 LEFT_CONTIG, RIGHT_CONTIG,
743 LEFT_FILLING, RIGHT_FILLING,
744 LEFT_DELAY, RIGHT_DELAY,
745 LEFT_VALID, RIGHT_VALID
751 #define MASK(b) (1 << (b))
752 #define MASK2(a,b) (MASK(a) | MASK(b))
753 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
754 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
755 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
756 #define STATE_TEST(b) (state & MASK(b))
757 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
758 ((state &= ~MASK(b)), 0))
759 #define SWITCH_STATE \
760 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
763 * Set up a bunch of variables to make the tests simpler.
766 base = ip->i_df.if_u1.if_extents;
768 xfs_bmbt_get_all(ep, &PREV);
769 new_endoff = new->br_startoff + new->br_blockcount;
770 ASSERT(PREV.br_startoff <= new->br_startoff);
771 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
773 * Set flags determining what part of the previous delayed allocation
774 * extent is being replaced by a real allocation.
776 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
777 STATE_SET(RIGHT_FILLING,
778 PREV.br_startoff + PREV.br_blockcount == new_endoff);
780 * Check and set flags if this segment has a left neighbor.
781 * Don't set contiguous if the combined extent would be too large.
783 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
784 xfs_bmbt_get_all(ep - 1, &LEFT);
785 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
787 STATE_SET(LEFT_CONTIG,
788 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
789 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
790 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
791 LEFT.br_state == new->br_state &&
792 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
794 * Check and set flags if this segment has a right neighbor.
795 * Don't set contiguous if the combined extent would be too large.
796 * Also check for all-three-contiguous being too large.
798 if (STATE_SET_TEST(RIGHT_VALID,
800 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
801 xfs_bmbt_get_all(ep + 1, &RIGHT);
802 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
804 STATE_SET(RIGHT_CONTIG,
805 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
806 new_endoff == RIGHT.br_startoff &&
807 new->br_startblock + new->br_blockcount ==
808 RIGHT.br_startblock &&
809 new->br_state == RIGHT.br_state &&
810 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
811 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
812 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
813 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
817 * Switch out based on the FILLING and CONTIG state bits.
819 switch (SWITCH_STATE) {
821 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
823 * Filling in all of a previously delayed allocation extent.
824 * The left and right neighbors are both contiguous with new.
826 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
828 xfs_bmbt_set_blockcount(ep - 1,
829 LEFT.br_blockcount + PREV.br_blockcount +
830 RIGHT.br_blockcount);
831 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
833 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
835 xfs_bmap_delete_exlist(ip, idx, 2, XFS_DATA_FORK);
836 ip->i_df.if_lastex = idx - 1;
837 ip->i_d.di_nextents--;
839 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
841 rval = XFS_ILOG_CORE;
842 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
844 RIGHT.br_blockcount, &i)))
847 if ((error = xfs_bmbt_delete(cur, &i)))
850 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
853 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
857 RIGHT.br_blockcount, LEFT.br_state)))
863 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
865 * Filling in all of a previously delayed allocation extent.
866 * The left neighbor is contiguous, the right is not.
868 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
870 xfs_bmbt_set_blockcount(ep - 1,
871 LEFT.br_blockcount + PREV.br_blockcount);
872 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
874 ip->i_df.if_lastex = idx - 1;
875 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
877 xfs_bmap_delete_exlist(ip, idx, 1, XFS_DATA_FORK);
879 rval = XFS_ILOG_DEXT;
882 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
883 LEFT.br_startblock, LEFT.br_blockcount,
887 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
890 PREV.br_blockcount, LEFT.br_state)))
896 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
898 * Filling in all of a previously delayed allocation extent.
899 * The right neighbor is contiguous, the left is not.
901 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
903 xfs_bmbt_set_startblock(ep, new->br_startblock);
904 xfs_bmbt_set_blockcount(ep,
905 PREV.br_blockcount + RIGHT.br_blockcount);
906 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
908 ip->i_df.if_lastex = idx;
909 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
911 xfs_bmap_delete_exlist(ip, idx + 1, 1, XFS_DATA_FORK);
913 rval = XFS_ILOG_DEXT;
916 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
918 RIGHT.br_blockcount, &i)))
921 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
924 RIGHT.br_blockcount, PREV.br_state)))
930 case MASK2(LEFT_FILLING, RIGHT_FILLING):
932 * Filling in all of a previously delayed allocation extent.
933 * Neither the left nor right neighbors are contiguous with
936 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
938 xfs_bmbt_set_startblock(ep, new->br_startblock);
939 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
941 ip->i_df.if_lastex = idx;
942 ip->i_d.di_nextents++;
944 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
946 rval = XFS_ILOG_CORE;
947 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
948 new->br_startblock, new->br_blockcount,
952 cur->bc_rec.b.br_state = XFS_EXT_NORM;
953 if ((error = xfs_bmbt_insert(cur, &i)))
960 case MASK2(LEFT_FILLING, LEFT_CONTIG):
962 * Filling in the first part of a previous delayed allocation.
963 * The left neighbor is contiguous.
965 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
967 xfs_bmbt_set_blockcount(ep - 1,
968 LEFT.br_blockcount + new->br_blockcount);
969 xfs_bmbt_set_startoff(ep,
970 PREV.br_startoff + new->br_blockcount);
971 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
973 temp = PREV.br_blockcount - new->br_blockcount;
974 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
976 xfs_bmbt_set_blockcount(ep, temp);
977 ip->i_df.if_lastex = idx - 1;
979 rval = XFS_ILOG_DEXT;
982 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
983 LEFT.br_startblock, LEFT.br_blockcount,
987 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
994 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
995 STARTBLOCKVAL(PREV.br_startblock));
996 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
997 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
1002 case MASK(LEFT_FILLING):
1004 * Filling in the first part of a previous delayed allocation.
1005 * The left neighbor is not contiguous.
1007 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1008 xfs_bmbt_set_startoff(ep, new_endoff);
1009 temp = PREV.br_blockcount - new->br_blockcount;
1010 xfs_bmbt_set_blockcount(ep, temp);
1011 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
1013 xfs_bmap_insert_exlist(ip, idx, 1, new, XFS_DATA_FORK);
1014 ip->i_df.if_lastex = idx;
1015 ip->i_d.di_nextents++;
1017 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1019 rval = XFS_ILOG_CORE;
1020 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1021 new->br_startblock, new->br_blockcount,
1025 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1026 if ((error = xfs_bmbt_insert(cur, &i)))
1030 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1031 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1032 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1033 first, flist, &cur, 1, &tmp_rval,
1039 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1040 STARTBLOCKVAL(PREV.br_startblock) -
1041 (cur ? cur->bc_private.b.allocated : 0));
1042 base = ip->i_df.if_u1.if_extents;
1043 ep = &base[idx + 1];
1044 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1045 xfs_bmap_trace_post_update(fname, "LF", ip, idx + 1,
1050 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1052 * Filling in the last part of a previous delayed allocation.
1053 * The right neighbor is contiguous with the new allocation.
1055 temp = PREV.br_blockcount - new->br_blockcount;
1056 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1058 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1060 xfs_bmbt_set_blockcount(ep, temp);
1061 xfs_bmbt_set_allf(ep + 1, new->br_startoff, new->br_startblock,
1062 new->br_blockcount + RIGHT.br_blockcount,
1064 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1066 ip->i_df.if_lastex = idx + 1;
1068 rval = XFS_ILOG_DEXT;
1071 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1072 RIGHT.br_startblock,
1073 RIGHT.br_blockcount, &i)))
1076 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1078 new->br_blockcount +
1079 RIGHT.br_blockcount,
1083 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1084 STARTBLOCKVAL(PREV.br_startblock));
1085 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1086 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1091 case MASK(RIGHT_FILLING):
1093 * Filling in the last part of a previous delayed allocation.
1094 * The right neighbor is not contiguous.
1096 temp = PREV.br_blockcount - new->br_blockcount;
1097 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1098 xfs_bmbt_set_blockcount(ep, temp);
1099 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1100 new, NULL, XFS_DATA_FORK);
1101 xfs_bmap_insert_exlist(ip, idx + 1, 1, new, XFS_DATA_FORK);
1102 ip->i_df.if_lastex = idx + 1;
1103 ip->i_d.di_nextents++;
1105 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1107 rval = XFS_ILOG_CORE;
1108 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1109 new->br_startblock, new->br_blockcount,
1113 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1114 if ((error = xfs_bmbt_insert(cur, &i)))
1118 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1119 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1120 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1121 first, flist, &cur, 1, &tmp_rval,
1127 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1128 STARTBLOCKVAL(PREV.br_startblock) -
1129 (cur ? cur->bc_private.b.allocated : 0));
1130 base = ip->i_df.if_u1.if_extents;
1132 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1133 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1139 * Filling in the middle part of a previous delayed allocation.
1140 * Contiguity is impossible here.
1141 * This case is avoided almost all the time.
1143 temp = new->br_startoff - PREV.br_startoff;
1144 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1145 xfs_bmbt_set_blockcount(ep, temp);
1147 r[1].br_startoff = new_endoff;
1148 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1149 r[1].br_blockcount = temp2;
1150 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1152 xfs_bmap_insert_exlist(ip, idx + 1, 2, &r[0], XFS_DATA_FORK);
1153 ip->i_df.if_lastex = idx + 1;
1154 ip->i_d.di_nextents++;
1156 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1158 rval = XFS_ILOG_CORE;
1159 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1160 new->br_startblock, new->br_blockcount,
1164 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1165 if ((error = xfs_bmbt_insert(cur, &i)))
1169 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1170 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1171 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1172 first, flist, &cur, 1, &tmp_rval,
1178 temp = xfs_bmap_worst_indlen(ip, temp);
1179 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1180 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
1181 (cur ? cur->bc_private.b.allocated : 0));
1183 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -diff, rsvd)) {
1185 * Ick gross gag me with a spoon.
1187 ASSERT(0); /* want to see if this ever happens! */
1193 !xfs_mod_incore_sb(ip->i_mount,
1194 XFS_SBS_FDBLOCKS, -diff, rsvd))
1201 !xfs_mod_incore_sb(ip->i_mount,
1202 XFS_SBS_FDBLOCKS, -diff, rsvd))
1207 base = ip->i_df.if_u1.if_extents;
1209 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1210 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1211 xfs_bmap_trace_pre_update(fname, "0", ip, idx + 2,
1213 xfs_bmbt_set_startblock(ep + 2, NULLSTARTBLOCK((int)temp2));
1214 xfs_bmap_trace_post_update(fname, "0", ip, idx + 2,
1216 *dnew = temp + temp2;
1219 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1220 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1221 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1222 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1223 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1224 case MASK(LEFT_CONTIG):
1225 case MASK(RIGHT_CONTIG):
1227 * These cases are all impossible.
1244 #undef STATE_SET_TEST
1249 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1250 * allocation to a real allocation or vice versa.
1252 STATIC int /* error */
1253 xfs_bmap_add_extent_unwritten_real(
1254 xfs_inode_t *ip, /* incore inode pointer */
1255 xfs_extnum_t idx, /* extent number to update/insert */
1256 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1257 xfs_bmbt_irec_t *new, /* new data to put in extent list */
1258 int *logflagsp) /* inode logging flags */
1260 xfs_bmbt_rec_t *base; /* base of extent entry list */
1261 xfs_btree_cur_t *cur; /* btree cursor */
1262 xfs_bmbt_rec_t *ep; /* extent entry for idx */
1263 int error; /* error return value */
1264 #ifdef XFS_BMAP_TRACE
1265 static char fname[] = "xfs_bmap_add_extent_unwritten_real";
1267 int i; /* temp state */
1268 xfs_fileoff_t new_endoff; /* end offset of new entry */
1269 xfs_exntst_t newext; /* new extent state */
1270 xfs_exntst_t oldext; /* old extent state */
1271 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1272 /* left is 0, right is 1, prev is 2 */
1273 int rval=0; /* return value (logging flags) */
1274 int state = 0;/* state bits, accessed thru macros */
1275 enum { /* bit number definitions for state */
1276 LEFT_CONTIG, RIGHT_CONTIG,
1277 LEFT_FILLING, RIGHT_FILLING,
1278 LEFT_DELAY, RIGHT_DELAY,
1279 LEFT_VALID, RIGHT_VALID
1285 #define MASK(b) (1 << (b))
1286 #define MASK2(a,b) (MASK(a) | MASK(b))
1287 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1288 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1289 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1290 #define STATE_TEST(b) (state & MASK(b))
1291 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1292 ((state &= ~MASK(b)), 0))
1293 #define SWITCH_STATE \
1294 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1297 * Set up a bunch of variables to make the tests simpler.
1301 base = ip->i_df.if_u1.if_extents;
1303 xfs_bmbt_get_all(ep, &PREV);
1304 newext = new->br_state;
1305 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1306 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1307 ASSERT(PREV.br_state == oldext);
1308 new_endoff = new->br_startoff + new->br_blockcount;
1309 ASSERT(PREV.br_startoff <= new->br_startoff);
1310 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1312 * Set flags determining what part of the previous oldext allocation
1313 * extent is being replaced by a newext allocation.
1315 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
1316 STATE_SET(RIGHT_FILLING,
1317 PREV.br_startoff + PREV.br_blockcount == new_endoff);
1319 * Check and set flags if this segment has a left neighbor.
1320 * Don't set contiguous if the combined extent would be too large.
1322 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1323 xfs_bmbt_get_all(ep - 1, &LEFT);
1324 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
1326 STATE_SET(LEFT_CONTIG,
1327 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1328 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1329 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1330 LEFT.br_state == newext &&
1331 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1333 * Check and set flags if this segment has a right neighbor.
1334 * Don't set contiguous if the combined extent would be too large.
1335 * Also check for all-three-contiguous being too large.
1337 if (STATE_SET_TEST(RIGHT_VALID,
1339 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
1340 xfs_bmbt_get_all(ep + 1, &RIGHT);
1341 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
1343 STATE_SET(RIGHT_CONTIG,
1344 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1345 new_endoff == RIGHT.br_startoff &&
1346 new->br_startblock + new->br_blockcount ==
1347 RIGHT.br_startblock &&
1348 newext == RIGHT.br_state &&
1349 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1350 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
1351 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
1352 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1355 * Switch out based on the FILLING and CONTIG state bits.
1357 switch (SWITCH_STATE) {
1359 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1361 * Setting all of a previous oldext extent to newext.
1362 * The left and right neighbors are both contiguous with new.
1364 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1366 xfs_bmbt_set_blockcount(ep - 1,
1367 LEFT.br_blockcount + PREV.br_blockcount +
1368 RIGHT.br_blockcount);
1369 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1371 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
1373 xfs_bmap_delete_exlist(ip, idx, 2, XFS_DATA_FORK);
1374 ip->i_df.if_lastex = idx - 1;
1375 ip->i_d.di_nextents -= 2;
1377 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1379 rval = XFS_ILOG_CORE;
1380 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1381 RIGHT.br_startblock,
1382 RIGHT.br_blockcount, &i)))
1385 if ((error = xfs_bmbt_delete(cur, &i)))
1388 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1391 if ((error = xfs_bmbt_delete(cur, &i)))
1394 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1397 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1399 LEFT.br_blockcount + PREV.br_blockcount +
1400 RIGHT.br_blockcount, LEFT.br_state)))
1405 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
1407 * Setting all of a previous oldext extent to newext.
1408 * The left neighbor is contiguous, the right is not.
1410 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
1412 xfs_bmbt_set_blockcount(ep - 1,
1413 LEFT.br_blockcount + PREV.br_blockcount);
1414 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
1416 ip->i_df.if_lastex = idx - 1;
1417 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
1419 xfs_bmap_delete_exlist(ip, idx, 1, XFS_DATA_FORK);
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,
1444 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
1446 * Setting all of a previous oldext extent to newext.
1447 * The right neighbor is contiguous, the left is not.
1449 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
1451 xfs_bmbt_set_blockcount(ep,
1452 PREV.br_blockcount + RIGHT.br_blockcount);
1453 xfs_bmbt_set_state(ep, newext);
1454 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
1456 ip->i_df.if_lastex = idx;
1457 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
1459 xfs_bmap_delete_exlist(ip, idx + 1, 1, XFS_DATA_FORK);
1460 ip->i_d.di_nextents--;
1462 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1464 rval = XFS_ILOG_CORE;
1465 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1466 RIGHT.br_startblock,
1467 RIGHT.br_blockcount, &i)))
1470 if ((error = xfs_bmbt_delete(cur, &i)))
1473 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1476 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1478 new->br_blockcount + RIGHT.br_blockcount,
1484 case MASK2(LEFT_FILLING, RIGHT_FILLING):
1486 * Setting all of a previous oldext extent to newext.
1487 * Neither the left nor right neighbors are contiguous with
1490 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
1492 xfs_bmbt_set_state(ep, newext);
1493 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
1495 ip->i_df.if_lastex = idx;
1497 rval = XFS_ILOG_DEXT;
1500 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1501 new->br_startblock, new->br_blockcount,
1505 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1506 new->br_startblock, new->br_blockcount,
1512 case MASK2(LEFT_FILLING, LEFT_CONTIG):
1514 * Setting the first part of a previous oldext extent to newext.
1515 * The left neighbor is contiguous.
1517 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
1519 xfs_bmbt_set_blockcount(ep - 1,
1520 LEFT.br_blockcount + new->br_blockcount);
1521 xfs_bmbt_set_startoff(ep,
1522 PREV.br_startoff + new->br_blockcount);
1523 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
1525 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
1527 xfs_bmbt_set_startblock(ep,
1528 new->br_startblock + new->br_blockcount);
1529 xfs_bmbt_set_blockcount(ep,
1530 PREV.br_blockcount - new->br_blockcount);
1531 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
1533 ip->i_df.if_lastex = idx - 1;
1535 rval = XFS_ILOG_DEXT;
1538 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1539 PREV.br_startblock, PREV.br_blockcount,
1543 if ((error = xfs_bmbt_update(cur,
1544 PREV.br_startoff + new->br_blockcount,
1545 PREV.br_startblock + new->br_blockcount,
1546 PREV.br_blockcount - new->br_blockcount,
1549 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1551 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1553 LEFT.br_blockcount + new->br_blockcount,
1559 case MASK(LEFT_FILLING):
1561 * Setting the first part of a previous oldext extent to newext.
1562 * The left neighbor is not contiguous.
1564 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1565 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1566 xfs_bmbt_set_startoff(ep, new_endoff);
1567 xfs_bmbt_set_blockcount(ep,
1568 PREV.br_blockcount - new->br_blockcount);
1569 xfs_bmbt_set_startblock(ep,
1570 new->br_startblock + new->br_blockcount);
1571 xfs_bmap_trace_post_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1572 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
1574 xfs_bmap_insert_exlist(ip, idx, 1, new, XFS_DATA_FORK);
1575 ip->i_df.if_lastex = idx;
1576 ip->i_d.di_nextents++;
1578 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1580 rval = XFS_ILOG_CORE;
1581 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1582 PREV.br_startblock, PREV.br_blockcount,
1586 if ((error = xfs_bmbt_update(cur,
1587 PREV.br_startoff + new->br_blockcount,
1588 PREV.br_startblock + new->br_blockcount,
1589 PREV.br_blockcount - new->br_blockcount,
1592 cur->bc_rec.b = *new;
1593 if ((error = xfs_bmbt_insert(cur, &i)))
1599 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1601 * Setting the last part of a previous oldext extent to newext.
1602 * The right neighbor is contiguous with the new allocation.
1604 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1606 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1608 xfs_bmbt_set_blockcount(ep,
1609 PREV.br_blockcount - new->br_blockcount);
1610 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1612 xfs_bmbt_set_allf(ep + 1, new->br_startoff, new->br_startblock,
1613 new->br_blockcount + RIGHT.br_blockcount, newext);
1614 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1616 ip->i_df.if_lastex = idx + 1;
1618 rval = XFS_ILOG_DEXT;
1621 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1623 PREV.br_blockcount, &i)))
1626 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1628 PREV.br_blockcount - new->br_blockcount,
1631 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1633 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1635 new->br_blockcount + RIGHT.br_blockcount,
1641 case MASK(RIGHT_FILLING):
1643 * Setting the last part of a previous oldext extent to newext.
1644 * The right neighbor is not contiguous.
1646 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1647 xfs_bmbt_set_blockcount(ep,
1648 PREV.br_blockcount - new->br_blockcount);
1649 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1650 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1651 new, NULL, XFS_DATA_FORK);
1652 xfs_bmap_insert_exlist(ip, idx + 1, 1, new, XFS_DATA_FORK);
1653 ip->i_df.if_lastex = idx + 1;
1654 ip->i_d.di_nextents++;
1656 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1658 rval = XFS_ILOG_CORE;
1659 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1660 PREV.br_startblock, PREV.br_blockcount,
1664 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1666 PREV.br_blockcount - new->br_blockcount,
1669 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1670 new->br_startblock, new->br_blockcount,
1674 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1675 if ((error = xfs_bmbt_insert(cur, &i)))
1683 * Setting the middle part of a previous oldext extent to
1684 * newext. Contiguity is impossible here.
1685 * One extent becomes three extents.
1687 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1688 xfs_bmbt_set_blockcount(ep,
1689 new->br_startoff - PREV.br_startoff);
1690 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1692 r[1].br_startoff = new_endoff;
1693 r[1].br_blockcount =
1694 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1695 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1696 r[1].br_state = oldext;
1697 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1699 xfs_bmap_insert_exlist(ip, idx + 1, 2, &r[0], XFS_DATA_FORK);
1700 ip->i_df.if_lastex = idx + 1;
1701 ip->i_d.di_nextents += 2;
1703 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1705 rval = XFS_ILOG_CORE;
1706 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1707 PREV.br_startblock, PREV.br_blockcount,
1711 /* new right extent - oldext */
1712 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1713 r[1].br_startblock, r[1].br_blockcount,
1716 /* new left extent - oldext */
1717 PREV.br_blockcount =
1718 new->br_startoff - PREV.br_startoff;
1719 cur->bc_rec.b = PREV;
1720 if ((error = xfs_bmbt_insert(cur, &i)))
1723 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1726 /* new middle extent - newext */
1727 cur->bc_rec.b = *new;
1728 if ((error = xfs_bmbt_insert(cur, &i)))
1734 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1735 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1736 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1737 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1738 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1739 case MASK(LEFT_CONTIG):
1740 case MASK(RIGHT_CONTIG):
1742 * These cases are all impossible.
1759 #undef STATE_SET_TEST
1764 * Called by xfs_bmap_add_extent to handle cases converting a hole
1765 * to a delayed allocation.
1768 STATIC int /* error */
1769 xfs_bmap_add_extent_hole_delay(
1770 xfs_inode_t *ip, /* incore inode pointer */
1771 xfs_extnum_t idx, /* extent number to update/insert */
1772 xfs_btree_cur_t *cur, /* if null, not a btree */
1773 xfs_bmbt_irec_t *new, /* new data to put in extent list */
1774 int *logflagsp, /* inode logging flags */
1775 int rsvd) /* OK to allocate reserved blocks */
1777 xfs_bmbt_rec_t *base; /* base of extent entry list */
1778 xfs_bmbt_rec_t *ep; /* extent list entry for idx */
1779 #ifdef XFS_BMAP_TRACE
1780 static char fname[] = "xfs_bmap_add_extent_hole_delay";
1782 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1783 xfs_filblks_t newlen=0; /* new indirect size */
1784 xfs_filblks_t oldlen=0; /* old indirect size */
1785 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1786 int state; /* state bits, accessed thru macros */
1787 xfs_filblks_t temp; /* temp for indirect calculations */
1788 enum { /* bit number definitions for state */
1789 LEFT_CONTIG, RIGHT_CONTIG,
1790 LEFT_DELAY, RIGHT_DELAY,
1791 LEFT_VALID, RIGHT_VALID
1794 #define MASK(b) (1 << (b))
1795 #define MASK2(a,b) (MASK(a) | MASK(b))
1796 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1797 #define STATE_TEST(b) (state & MASK(b))
1798 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1799 ((state &= ~MASK(b)), 0))
1800 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1802 base = ip->i_df.if_u1.if_extents;
1805 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1807 * Check and set flags if this segment has a left neighbor
1809 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1810 xfs_bmbt_get_all(ep - 1, &left);
1811 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1814 * Check and set flags if the current (right) segment exists.
1815 * If it doesn't exist, we're converting the hole at end-of-file.
1817 if (STATE_SET_TEST(RIGHT_VALID,
1819 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1820 xfs_bmbt_get_all(ep, &right);
1821 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1824 * Set contiguity flags on the left and right neighbors.
1825 * Don't let extents get too large, even if the pieces are contiguous.
1827 STATE_SET(LEFT_CONTIG,
1828 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1829 left.br_startoff + left.br_blockcount == new->br_startoff &&
1830 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1831 STATE_SET(RIGHT_CONTIG,
1832 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1833 new->br_startoff + new->br_blockcount == right.br_startoff &&
1834 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1835 (!STATE_TEST(LEFT_CONTIG) ||
1836 (left.br_blockcount + new->br_blockcount +
1837 right.br_blockcount <= MAXEXTLEN)));
1839 * Switch out based on the contiguity flags.
1841 switch (SWITCH_STATE) {
1843 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1845 * New allocation is contiguous with delayed allocations
1846 * on the left and on the right.
1847 * Merge all three into a single extent list entry.
1849 temp = left.br_blockcount + new->br_blockcount +
1850 right.br_blockcount;
1851 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
1853 xfs_bmbt_set_blockcount(ep - 1, temp);
1854 oldlen = STARTBLOCKVAL(left.br_startblock) +
1855 STARTBLOCKVAL(new->br_startblock) +
1856 STARTBLOCKVAL(right.br_startblock);
1857 newlen = xfs_bmap_worst_indlen(ip, temp);
1858 xfs_bmbt_set_startblock(ep - 1, NULLSTARTBLOCK((int)newlen));
1859 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
1861 xfs_bmap_trace_delete(fname, "LC|RC", ip, idx, 1,
1863 xfs_bmap_delete_exlist(ip, idx, 1, XFS_DATA_FORK);
1864 ip->i_df.if_lastex = idx - 1;
1867 case MASK(LEFT_CONTIG):
1869 * New allocation is contiguous with a delayed allocation
1871 * Merge the new allocation with the left neighbor.
1873 temp = left.br_blockcount + new->br_blockcount;
1874 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1,
1876 xfs_bmbt_set_blockcount(ep - 1, temp);
1877 oldlen = STARTBLOCKVAL(left.br_startblock) +
1878 STARTBLOCKVAL(new->br_startblock);
1879 newlen = xfs_bmap_worst_indlen(ip, temp);
1880 xfs_bmbt_set_startblock(ep - 1, NULLSTARTBLOCK((int)newlen));
1881 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1,
1883 ip->i_df.if_lastex = idx - 1;
1886 case MASK(RIGHT_CONTIG):
1888 * New allocation is contiguous with a delayed allocation
1890 * Merge the new allocation with the right neighbor.
1892 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1893 temp = new->br_blockcount + right.br_blockcount;
1894 oldlen = STARTBLOCKVAL(new->br_startblock) +
1895 STARTBLOCKVAL(right.br_startblock);
1896 newlen = xfs_bmap_worst_indlen(ip, temp);
1897 xfs_bmbt_set_allf(ep, new->br_startoff,
1898 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
1899 xfs_bmap_trace_post_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1900 ip->i_df.if_lastex = idx;
1905 * New allocation is not contiguous with another
1906 * delayed allocation.
1907 * Insert a new entry.
1909 oldlen = newlen = 0;
1910 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
1912 xfs_bmap_insert_exlist(ip, idx, 1, new, XFS_DATA_FORK);
1913 ip->i_df.if_lastex = idx;
1916 if (oldlen != newlen) {
1917 ASSERT(oldlen > newlen);
1918 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
1919 (int)(oldlen - newlen), rsvd);
1921 * Nothing to do for disk quota accounting here.
1930 #undef STATE_SET_TEST
1935 * Called by xfs_bmap_add_extent to handle cases converting a hole
1936 * to a real allocation.
1938 STATIC int /* error */
1939 xfs_bmap_add_extent_hole_real(
1940 xfs_inode_t *ip, /* incore inode pointer */
1941 xfs_extnum_t idx, /* extent number to update/insert */
1942 xfs_btree_cur_t *cur, /* if null, not a btree */
1943 xfs_bmbt_irec_t *new, /* new data to put in extent list */
1944 int *logflagsp, /* inode logging flags */
1945 int whichfork) /* data or attr fork */
1947 xfs_bmbt_rec_t *ep; /* pointer to extent entry ins. point */
1948 int error; /* error return value */
1949 #ifdef XFS_BMAP_TRACE
1950 static char fname[] = "xfs_bmap_add_extent_hole_real";
1952 int i; /* temp state */
1953 xfs_ifork_t *ifp; /* inode fork pointer */
1954 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1955 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1956 int state; /* state bits, accessed thru macros */
1957 enum { /* bit number definitions for state */
1958 LEFT_CONTIG, RIGHT_CONTIG,
1959 LEFT_DELAY, RIGHT_DELAY,
1960 LEFT_VALID, RIGHT_VALID
1963 #define MASK(b) (1 << (b))
1964 #define MASK2(a,b) (MASK(a) | MASK(b))
1965 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1966 #define STATE_TEST(b) (state & MASK(b))
1967 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1968 ((state &= ~MASK(b)), 0))
1969 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1971 ifp = XFS_IFORK_PTR(ip, whichfork);
1972 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
1973 ep = &ifp->if_u1.if_extents[idx];
1976 * Check and set flags if this segment has a left neighbor.
1978 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1979 xfs_bmbt_get_all(ep - 1, &left);
1980 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1983 * Check and set flags if this segment has a current value.
1984 * Not true if we're inserting into the "hole" at eof.
1986 if (STATE_SET_TEST(RIGHT_VALID,
1988 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1989 xfs_bmbt_get_all(ep, &right);
1990 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1993 * We're inserting a real allocation between "left" and "right".
1994 * Set the contiguity flags. Don't let extents get too large.
1996 STATE_SET(LEFT_CONTIG,
1997 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1998 left.br_startoff + left.br_blockcount == new->br_startoff &&
1999 left.br_startblock + left.br_blockcount == new->br_startblock &&
2000 left.br_state == new->br_state &&
2001 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
2002 STATE_SET(RIGHT_CONTIG,
2003 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
2004 new->br_startoff + new->br_blockcount == right.br_startoff &&
2005 new->br_startblock + new->br_blockcount ==
2006 right.br_startblock &&
2007 new->br_state == right.br_state &&
2008 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2009 (!STATE_TEST(LEFT_CONTIG) ||
2010 left.br_blockcount + new->br_blockcount +
2011 right.br_blockcount <= MAXEXTLEN));
2014 * Select which case we're in here, and implement it.
2016 switch (SWITCH_STATE) {
2018 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
2020 * New allocation is contiguous with real allocations on the
2021 * left and on the right.
2022 * Merge all three into a single extent list entry.
2024 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
2026 xfs_bmbt_set_blockcount(ep - 1,
2027 left.br_blockcount + new->br_blockcount +
2028 right.br_blockcount);
2029 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
2031 xfs_bmap_trace_delete(fname, "LC|RC", ip,
2033 xfs_bmap_delete_exlist(ip, idx, 1, whichfork);
2034 ifp->if_lastex = idx - 1;
2035 XFS_IFORK_NEXT_SET(ip, whichfork,
2036 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2038 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2041 *logflagsp = XFS_ILOG_CORE;
2042 if ((error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
2043 right.br_startblock, right.br_blockcount, &i)))
2046 if ((error = xfs_bmbt_delete(cur, &i)))
2049 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2052 error = xfs_bmbt_update(cur, left.br_startoff,
2054 left.br_blockcount + new->br_blockcount +
2055 right.br_blockcount, left.br_state);
2058 case MASK(LEFT_CONTIG):
2060 * New allocation is contiguous with a real allocation
2062 * Merge the new allocation with the left neighbor.
2064 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1, whichfork);
2065 xfs_bmbt_set_blockcount(ep - 1,
2066 left.br_blockcount + new->br_blockcount);
2067 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1, whichfork);
2068 ifp->if_lastex = idx - 1;
2070 *logflagsp = XFS_ILOG_FEXT(whichfork);
2074 if ((error = xfs_bmbt_lookup_eq(cur, left.br_startoff,
2075 left.br_startblock, left.br_blockcount, &i)))
2078 error = xfs_bmbt_update(cur, left.br_startoff,
2080 left.br_blockcount + new->br_blockcount,
2084 case MASK(RIGHT_CONTIG):
2086 * New allocation is contiguous with a real allocation
2088 * Merge the new allocation with the right neighbor.
2090 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, whichfork);
2091 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2092 new->br_blockcount + right.br_blockcount,
2094 xfs_bmap_trace_post_update(fname, "RC", ip, idx, whichfork);
2095 ifp->if_lastex = idx;
2097 *logflagsp = XFS_ILOG_FEXT(whichfork);
2101 if ((error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
2102 right.br_startblock, right.br_blockcount, &i)))
2105 error = xfs_bmbt_update(cur, new->br_startoff,
2107 new->br_blockcount + right.br_blockcount,
2113 * New allocation is not contiguous with another
2115 * Insert a new entry.
2117 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
2119 xfs_bmap_insert_exlist(ip, idx, 1, new, whichfork);
2120 ifp->if_lastex = idx;
2121 XFS_IFORK_NEXT_SET(ip, whichfork,
2122 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2124 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2127 *logflagsp = XFS_ILOG_CORE;
2128 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2129 new->br_startblock, new->br_blockcount, &i)))
2132 cur->bc_rec.b.br_state = new->br_state;
2133 if ((error = xfs_bmbt_insert(cur, &i)))
2142 #undef STATE_SET_TEST
2146 return 0; /* keep gcc quite */
2150 * Adjust the size of the new extent based on di_extsize and rt extsize.
2153 xfs_bmap_extsize_align(
2155 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2156 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2157 xfs_extlen_t extsz, /* align to this extent size */
2158 int rt, /* is this a realtime inode? */
2159 int eof, /* is extent at end-of-file? */
2160 int delay, /* creating delalloc extent? */
2161 int convert, /* overwriting unwritten extent? */
2162 xfs_fileoff_t *offp, /* in/out: aligned offset */
2163 xfs_extlen_t *lenp) /* in/out: aligned length */
2165 xfs_fileoff_t orig_off; /* original offset */
2166 xfs_extlen_t orig_alen; /* original length */
2167 xfs_fileoff_t orig_end; /* original off+len */
2168 xfs_fileoff_t nexto; /* next file offset */
2169 xfs_fileoff_t prevo; /* previous file offset */
2170 xfs_fileoff_t align_off; /* temp for offset */
2171 xfs_extlen_t align_alen; /* temp for length */
2172 xfs_extlen_t temp; /* temp for calculations */
2177 orig_off = align_off = *offp;
2178 orig_alen = align_alen = *lenp;
2179 orig_end = orig_off + orig_alen;
2182 * If this request overlaps an existing extent, then don't
2183 * attempt to perform any additional alignment.
2185 if (!delay && !eof &&
2186 (orig_off >= gotp->br_startoff) &&
2187 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2192 * If the file offset is unaligned vs. the extent size
2193 * we need to align it. This will be possible unless
2194 * the file was previously written with a kernel that didn't
2195 * perform this alignment, or if a truncate shot us in the
2198 temp = do_mod(orig_off, extsz);
2204 * Same adjustment for the end of the requested area.
2206 if ((temp = (align_alen % extsz))) {
2207 align_alen += extsz - temp;
2210 * If the previous block overlaps with this proposed allocation
2211 * then move the start forward without adjusting the length.
2213 if (prevp->br_startoff != NULLFILEOFF) {
2214 if (prevp->br_startblock == HOLESTARTBLOCK)
2215 prevo = prevp->br_startoff;
2217 prevo = prevp->br_startoff + prevp->br_blockcount;
2220 if (align_off != orig_off && align_off < prevo)
2223 * If the next block overlaps with this proposed allocation
2224 * then move the start back without adjusting the length,
2225 * but not before offset 0.
2226 * This may of course make the start overlap previous block,
2227 * and if we hit the offset 0 limit then the next block
2228 * can still overlap too.
2230 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2231 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2232 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2233 nexto = gotp->br_startoff + gotp->br_blockcount;
2235 nexto = gotp->br_startoff;
2237 nexto = NULLFILEOFF;
2239 align_off + align_alen != orig_end &&
2240 align_off + align_alen > nexto)
2241 align_off = nexto > align_alen ? nexto - align_alen : 0;
2243 * If we're now overlapping the next or previous extent that
2244 * means we can't fit an extsz piece in this hole. Just move
2245 * the start forward to the first valid spot and set
2246 * the length so we hit the end.
2248 if (align_off != orig_off && align_off < prevo)
2250 if (align_off + align_alen != orig_end &&
2251 align_off + align_alen > nexto &&
2252 nexto != NULLFILEOFF) {
2253 ASSERT(nexto > prevo);
2254 align_alen = nexto - align_off;
2258 * If realtime, and the result isn't a multiple of the realtime
2259 * extent size we need to remove blocks until it is.
2261 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2263 * We're not covering the original request, or
2264 * we won't be able to once we fix the length.
2266 if (orig_off < align_off ||
2267 orig_end > align_off + align_alen ||
2268 align_alen - temp < orig_alen)
2269 return XFS_ERROR(EINVAL);
2271 * Try to fix it by moving the start up.
2273 if (align_off + temp <= orig_off) {
2278 * Try to fix it by moving the end in.
2280 else if (align_off + align_alen - temp >= orig_end)
2283 * Set the start to the minimum then trim the length.
2286 align_alen -= orig_off - align_off;
2287 align_off = orig_off;
2288 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2291 * Result doesn't cover the request, fail it.
2293 if (orig_off < align_off || orig_end > align_off + align_alen)
2294 return XFS_ERROR(EINVAL);
2296 ASSERT(orig_off >= align_off);
2297 ASSERT(orig_end <= align_off + align_alen);
2301 if (!eof && gotp->br_startoff != NULLFILEOFF)
2302 ASSERT(align_off + align_alen <= gotp->br_startoff);
2303 if (prevp->br_startoff != NULLFILEOFF)
2304 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2312 #define XFS_ALLOC_GAP_UNITS 4
2315 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2316 * It figures out where to ask the underlying allocator to put the new extent.
2320 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2322 xfs_fsblock_t adjust; /* adjustment to block numbers */
2323 xfs_alloctype_t atype=0; /* type for allocation routines */
2324 int error; /* error return value */
2325 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2326 xfs_mount_t *mp; /* mount point structure */
2327 int nullfb; /* true if ap->firstblock isn't set */
2328 int rt; /* true if inode is realtime */
2329 xfs_extlen_t prod = 0; /* product factor for allocators */
2330 xfs_extlen_t ralen = 0; /* realtime allocation length */
2331 xfs_extlen_t align; /* minimum allocation alignment */
2334 #define ISVALID(x,y) \
2336 (x) < mp->m_sb.sb_rblocks : \
2337 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2338 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2339 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2344 mp = ap->ip->i_mount;
2345 nullfb = ap->firstblock == NULLFSBLOCK;
2346 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2347 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2349 align = ap->ip->i_d.di_extsize ?
2350 ap->ip->i_d.di_extsize : mp->m_sb.sb_rextsize;
2351 /* Set prod to match the extent size */
2352 prod = align / mp->m_sb.sb_rextsize;
2354 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2355 align, rt, ap->eof, 0,
2356 ap->conv, &ap->off, &ap->alen);
2360 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2363 * If the offset & length are not perfectly aligned
2364 * then kill prod, it will just get us in trouble.
2366 if (do_mod(ap->off, align) || ap->alen % align)
2369 * Set ralen to be the actual requested length in rtextents.
2371 ralen = ap->alen / mp->m_sb.sb_rextsize;
2373 * If the old value was close enough to MAXEXTLEN that
2374 * we rounded up to it, cut it back so it's valid again.
2375 * Note that if it's a really large request (bigger than
2376 * MAXEXTLEN), we don't hear about that number, and can't
2377 * adjust the starting point to match it.
2379 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2380 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2382 * If it's an allocation to an empty file at offset 0,
2383 * pick an extent that will space things out in the rt area.
2385 if (ap->eof && ap->off == 0) {
2386 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2389 ap->rval = rtx * mp->m_sb.sb_rextsize;
2393 align = (ap->userdata && ap->ip->i_d.di_extsize &&
2394 (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)) ?
2395 ap->ip->i_d.di_extsize : 0;
2396 if (unlikely(align)) {
2397 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2399 ap->eof, 0, ap->conv,
2400 &ap->off, &ap->alen);
2405 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2407 ap->rval = ap->firstblock;
2411 * If allocating at eof, and there's a previous real block,
2412 * try to use it's last block as our starting point.
2414 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2415 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2416 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2417 ap->prevp->br_startblock)) {
2418 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2420 * Adjust for the gap between prevp and us.
2423 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2425 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2429 * If not at eof, then compare the two neighbor blocks.
2430 * Figure out whether either one gives us a good starting point,
2431 * and pick the better one.
2433 else if (!ap->eof) {
2434 xfs_fsblock_t gotbno; /* right side block number */
2435 xfs_fsblock_t gotdiff=0; /* right side difference */
2436 xfs_fsblock_t prevbno; /* left side block number */
2437 xfs_fsblock_t prevdiff=0; /* left side difference */
2440 * If there's a previous (left) block, select a requested
2441 * start block based on it.
2443 if (ap->prevp->br_startoff != NULLFILEOFF &&
2444 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2445 (prevbno = ap->prevp->br_startblock +
2446 ap->prevp->br_blockcount) &&
2447 ISVALID(prevbno, ap->prevp->br_startblock)) {
2449 * Calculate gap to end of previous block.
2451 adjust = prevdiff = ap->off -
2452 (ap->prevp->br_startoff +
2453 ap->prevp->br_blockcount);
2455 * Figure the startblock based on the previous block's
2456 * end and the gap size.
2458 * If the gap is large relative to the piece we're
2459 * allocating, or using it gives us an invalid block
2460 * number, then just use the end of the previous block.
2462 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2463 ISVALID(prevbno + prevdiff,
2464 ap->prevp->br_startblock))
2469 * If the firstblock forbids it, can't use it,
2472 if (!rt && !nullfb &&
2473 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2474 prevbno = NULLFSBLOCK;
2477 * No previous block or can't follow it, just default.
2480 prevbno = NULLFSBLOCK;
2482 * If there's a following (right) block, select a requested
2483 * start block based on it.
2485 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
2487 * Calculate gap to start of next block.
2489 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2491 * Figure the startblock based on the next block's
2492 * start and the gap size.
2494 gotbno = ap->gotp->br_startblock;
2497 * If the gap is large relative to the piece we're
2498 * allocating, or using it gives us an invalid block
2499 * number, then just use the start of the next block
2500 * offset by our length.
2502 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2503 ISVALID(gotbno - gotdiff, gotbno))
2505 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2507 gotdiff += adjust - ap->alen;
2511 * If the firstblock forbids it, can't use it,
2514 if (!rt && !nullfb &&
2515 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2516 gotbno = NULLFSBLOCK;
2519 * No next block, just default.
2522 gotbno = NULLFSBLOCK;
2524 * If both valid, pick the better one, else the only good
2525 * one, else ap->rval is already set (to 0 or the inode block).
2527 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2528 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2529 else if (prevbno != NULLFSBLOCK)
2531 else if (gotbno != NULLFSBLOCK)
2535 * If allowed, use ap->rval; otherwise must use firstblock since
2536 * it's in the right allocation group.
2538 if (nullfb || rt || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2541 ap->rval = ap->firstblock;
2543 * Realtime allocation, done through xfs_rtallocate_extent.
2551 atype = ap->rval == 0 ?
2552 XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2553 do_div(ap->rval, mp->m_sb.sb_rextsize);
2556 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2557 &ralen, atype, ap->wasdel, prod, &rtb)))
2559 if (rtb == NULLFSBLOCK && prod > 1 &&
2560 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2561 ap->alen, &ralen, atype,
2562 ap->wasdel, 1, &rtb)))
2565 if (ap->rval != NULLFSBLOCK) {
2566 ap->rval *= mp->m_sb.sb_rextsize;
2567 ralen *= mp->m_sb.sb_rextsize;
2569 ap->ip->i_d.di_nblocks += ralen;
2570 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2572 ap->ip->i_delayed_blks -= ralen;
2574 * Adjust the disk quota also. This was reserved
2577 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2578 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2579 XFS_TRANS_DQ_RTBCOUNT,
2583 #endif /* __KERNEL__ */
2586 * Normal allocation, done through xfs_alloc_vextent.
2590 xfs_alloc_arg_t args;
2594 xfs_extlen_t longest;
2596 xfs_extlen_t nextminlen=0;
2599 xfs_agnumber_t startag;
2602 tryagain = isaligned = 0;
2605 args.fsbno = ap->rval;
2606 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2609 args.type = XFS_ALLOCTYPE_START_BNO;
2610 args.total = ap->total;
2612 * Find the longest available space.
2613 * We're going to try for the whole allocation at once.
2615 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2617 down_read(&mp->m_peraglock);
2618 while (blen < ap->alen) {
2619 pag = &mp->m_perag[ag];
2620 if (!pag->pagf_init &&
2621 (error = xfs_alloc_pagf_init(mp, args.tp,
2622 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2623 up_read(&mp->m_peraglock);
2627 * See xfs_alloc_fix_freelist...
2629 if (pag->pagf_init) {
2630 need = XFS_MIN_FREELIST_PAG(pag, mp);
2631 delta = need > pag->pagf_flcount ?
2632 need - pag->pagf_flcount : 0;
2633 longest = (pag->pagf_longest > delta) ?
2634 (pag->pagf_longest - delta) :
2635 (pag->pagf_flcount > 0 ||
2636 pag->pagf_longest > 0);
2641 if (++ag == mp->m_sb.sb_agcount)
2646 up_read(&mp->m_peraglock);
2648 * Since the above loop did a BUF_TRYLOCK, it is
2649 * possible that there is space for this request.
2651 if (notinit || blen < ap->minlen)
2652 args.minlen = ap->minlen;
2654 * If the best seen length is less than the request
2655 * length, use the best as the minimum.
2657 else if (blen < ap->alen)
2660 * Otherwise we've seen an extent as big as alen,
2661 * use that as the minimum.
2664 args.minlen = ap->alen;
2665 } else if (ap->low) {
2666 args.type = XFS_ALLOCTYPE_FIRST_AG;
2667 args.total = args.minlen = ap->minlen;
2669 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2670 args.total = ap->total;
2671 args.minlen = ap->minlen;
2673 if (unlikely(ap->userdata && ap->ip->i_d.di_extsize &&
2674 (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE))) {
2675 args.prod = ap->ip->i_d.di_extsize;
2676 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2677 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2678 } else if (unlikely(mp->m_sb.sb_blocksize >= NBPP)) {
2682 args.prod = NBPP >> mp->m_sb.sb_blocklog;
2683 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2684 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2687 * If we are not low on available data blocks, and the
2688 * underlying logical volume manager is a stripe, and
2689 * the file offset is zero then try to allocate data
2690 * blocks on stripe unit boundary.
2691 * NOTE: ap->aeof is only set if the allocation length
2692 * is >= the stripe unit and the allocation offset is
2693 * at the end of file.
2695 if (!ap->low && ap->aeof) {
2697 args.alignment = mp->m_dalign;
2701 * Adjust for alignment
2703 if (blen > args.alignment && blen <= ap->alen)
2704 args.minlen = blen - args.alignment;
2705 args.minalignslop = 0;
2708 * First try an exact bno allocation.
2709 * If it fails then do a near or start bno
2710 * allocation with alignment turned on.
2714 args.type = XFS_ALLOCTYPE_THIS_BNO;
2717 * Compute the minlen+alignment for the
2718 * next case. Set slop so that the value
2719 * of minlen+alignment+slop doesn't go up
2720 * between the calls.
2722 if (blen > mp->m_dalign && blen <= ap->alen)
2723 nextminlen = blen - mp->m_dalign;
2725 nextminlen = args.minlen;
2726 if (nextminlen + mp->m_dalign > args.minlen + 1)
2728 nextminlen + mp->m_dalign -
2731 args.minalignslop = 0;
2735 args.minalignslop = 0;
2737 args.minleft = ap->minleft;
2738 args.wasdel = ap->wasdel;
2740 args.userdata = ap->userdata;
2741 if ((error = xfs_alloc_vextent(&args)))
2743 if (tryagain && args.fsbno == NULLFSBLOCK) {
2745 * Exact allocation failed. Now try with alignment
2749 args.fsbno = ap->rval;
2750 args.alignment = mp->m_dalign;
2751 args.minlen = nextminlen;
2752 args.minalignslop = 0;
2754 if ((error = xfs_alloc_vextent(&args)))
2757 if (isaligned && args.fsbno == NULLFSBLOCK) {
2759 * allocation failed, so turn off alignment and
2763 args.fsbno = ap->rval;
2765 if ((error = xfs_alloc_vextent(&args)))
2768 if (args.fsbno == NULLFSBLOCK && nullfb &&
2769 args.minlen > ap->minlen) {
2770 args.minlen = ap->minlen;
2771 args.type = XFS_ALLOCTYPE_START_BNO;
2772 args.fsbno = ap->rval;
2773 if ((error = xfs_alloc_vextent(&args)))
2776 if (args.fsbno == NULLFSBLOCK && nullfb) {
2778 args.type = XFS_ALLOCTYPE_FIRST_AG;
2779 args.total = ap->minlen;
2781 if ((error = xfs_alloc_vextent(&args)))
2785 if (args.fsbno != NULLFSBLOCK) {
2786 ap->firstblock = ap->rval = args.fsbno;
2787 ASSERT(nullfb || fb_agno == args.agno ||
2788 (ap->low && fb_agno < args.agno));
2789 ap->alen = args.len;
2790 ap->ip->i_d.di_nblocks += args.len;
2791 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2793 ap->ip->i_delayed_blks -= args.len;
2795 * Adjust the disk quota also. This was reserved
2798 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2799 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2800 XFS_TRANS_DQ_BCOUNT,
2803 ap->rval = NULLFSBLOCK;
2812 * Transform a btree format file with only one leaf node, where the
2813 * extents list will fit in the inode, into an extents format file.
2814 * Since the extent list is already in-core, all we have to do is
2815 * give up the space for the btree root and pitch the leaf block.
2817 STATIC int /* error */
2818 xfs_bmap_btree_to_extents(
2819 xfs_trans_t *tp, /* transaction pointer */
2820 xfs_inode_t *ip, /* incore inode pointer */
2821 xfs_btree_cur_t *cur, /* btree cursor */
2822 int *logflagsp, /* inode logging flags */
2823 int whichfork) /* data or attr fork */
2826 xfs_bmbt_block_t *cblock;/* child btree block */
2827 xfs_fsblock_t cbno; /* child block number */
2828 xfs_buf_t *cbp; /* child block's buffer */
2829 int error; /* error return value */
2830 xfs_ifork_t *ifp; /* inode fork data */
2831 xfs_mount_t *mp; /* mount point structure */
2832 xfs_bmbt_ptr_t *pp; /* ptr to block address */
2833 xfs_bmbt_block_t *rblock;/* root btree block */
2835 ifp = XFS_IFORK_PTR(ip, whichfork);
2836 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2837 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2838 rblock = ifp->if_broot;
2839 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2840 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
2841 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
2843 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
2846 if ((error = xfs_btree_check_lptr(cur, INT_GET(*pp, ARCH_CONVERT), 1)))
2849 cbno = INT_GET(*pp, ARCH_CONVERT);
2850 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2851 XFS_BMAP_BTREE_REF)))
2853 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
2854 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
2856 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2857 ip->i_d.di_nblocks--;
2858 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
2859 xfs_trans_binval(tp, cbp);
2860 if (cur->bc_bufs[0] == cbp)
2861 cur->bc_bufs[0] = NULL;
2862 xfs_iroot_realloc(ip, -1, whichfork);
2863 ASSERT(ifp->if_broot == NULL);
2864 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2865 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2866 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2871 * Called by xfs_bmapi to update extent list structure and the btree
2872 * after removing space (or undoing a delayed allocation).
2874 STATIC int /* error */
2875 xfs_bmap_del_extent(
2876 xfs_inode_t *ip, /* incore inode pointer */
2877 xfs_trans_t *tp, /* current transaction pointer */
2878 xfs_extnum_t idx, /* extent number to update/delete */
2879 xfs_bmap_free_t *flist, /* list of extents to be freed */
2880 xfs_btree_cur_t *cur, /* if null, not a btree */
2881 xfs_bmbt_irec_t *del, /* data to remove from extent list */
2882 int *logflagsp, /* inode logging flags */
2883 int whichfork, /* data or attr fork */
2884 int rsvd) /* OK to allocate reserved blocks */
2886 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2887 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2888 xfs_fsblock_t del_endblock=0; /* first block past del */
2889 xfs_fileoff_t del_endoff; /* first offset past del */
2890 int delay; /* current block is delayed allocated */
2891 int do_fx; /* free extent at end of routine */
2892 xfs_bmbt_rec_t *ep; /* current extent entry pointer */
2893 int error; /* error return value */
2894 int flags; /* inode logging flags */
2895 #ifdef XFS_BMAP_TRACE
2896 static char fname[] = "xfs_bmap_del_extent";
2898 xfs_bmbt_irec_t got; /* current extent entry */
2899 xfs_fileoff_t got_endoff; /* first offset past got */
2900 int i; /* temp state */
2901 xfs_ifork_t *ifp; /* inode fork pointer */
2902 xfs_mount_t *mp; /* mount structure */
2903 xfs_filblks_t nblks; /* quota/sb block count */
2904 xfs_bmbt_irec_t new; /* new record to be inserted */
2906 xfs_extnum_t nextents; /* number of extents in list */
2907 uint qfield; /* quota field to update */
2908 xfs_filblks_t temp; /* for indirect length calculations */
2909 xfs_filblks_t temp2; /* for indirect length calculations */
2911 XFS_STATS_INC(xs_del_exlist);
2913 ifp = XFS_IFORK_PTR(ip, whichfork);
2914 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2915 ASSERT(idx >= 0 && idx < nextents);
2916 ASSERT(del->br_blockcount > 0);
2917 ep = &ifp->if_u1.if_extents[idx];
2918 xfs_bmbt_get_all(ep, &got);
2919 ASSERT(got.br_startoff <= del->br_startoff);
2920 del_endoff = del->br_startoff + del->br_blockcount;
2921 got_endoff = got.br_startoff + got.br_blockcount;
2922 ASSERT(got_endoff >= del_endoff);
2923 delay = ISNULLSTARTBLOCK(got.br_startblock);
2924 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
2929 * If deleting a real allocation, must free up the disk space.
2932 flags = XFS_ILOG_CORE;
2934 * Realtime allocation. Free it and record di_nblocks update.
2936 if (whichfork == XFS_DATA_FORK &&
2937 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
2941 ASSERT(do_mod(del->br_blockcount,
2942 mp->m_sb.sb_rextsize) == 0);
2943 ASSERT(do_mod(del->br_startblock,
2944 mp->m_sb.sb_rextsize) == 0);
2945 bno = del->br_startblock;
2946 len = del->br_blockcount;
2947 do_div(bno, mp->m_sb.sb_rextsize);
2948 do_div(len, mp->m_sb.sb_rextsize);
2949 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
2950 (xfs_extlen_t)len)))
2953 nblks = len * mp->m_sb.sb_rextsize;
2954 qfield = XFS_TRANS_DQ_RTBCOUNT;
2957 * Ordinary allocation.
2961 nblks = del->br_blockcount;
2962 qfield = XFS_TRANS_DQ_BCOUNT;
2965 * Set up del_endblock and cur for later.
2967 del_endblock = del->br_startblock + del->br_blockcount;
2969 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2970 got.br_startblock, got.br_blockcount,
2975 da_old = da_new = 0;
2977 da_old = STARTBLOCKVAL(got.br_startblock);
2983 * Set flag value to use in switch statement.
2984 * Left-contig is 2, right-contig is 1.
2986 switch (((got.br_startoff == del->br_startoff) << 1) |
2987 (got_endoff == del_endoff)) {
2990 * Matches the whole extent. Delete the entry.
2992 xfs_bmap_trace_delete(fname, "3", ip, idx, 1, whichfork);
2993 xfs_bmap_delete_exlist(ip, idx, 1, whichfork);
2994 ifp->if_lastex = idx;
2997 XFS_IFORK_NEXT_SET(ip, whichfork,
2998 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2999 flags |= XFS_ILOG_CORE;
3001 flags |= XFS_ILOG_FEXT(whichfork);
3004 if ((error = xfs_bmbt_delete(cur, &i)))
3011 * Deleting the first part of the extent.
3013 xfs_bmap_trace_pre_update(fname, "2", ip, idx, whichfork);
3014 xfs_bmbt_set_startoff(ep, del_endoff);
3015 temp = got.br_blockcount - del->br_blockcount;
3016 xfs_bmbt_set_blockcount(ep, temp);
3017 ifp->if_lastex = idx;
3019 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3021 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3022 xfs_bmap_trace_post_update(fname, "2", ip, idx,
3027 xfs_bmbt_set_startblock(ep, del_endblock);
3028 xfs_bmap_trace_post_update(fname, "2", ip, idx, whichfork);
3030 flags |= XFS_ILOG_FEXT(whichfork);
3033 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3034 got.br_blockcount - del->br_blockcount,
3041 * Deleting the last part of the extent.
3043 temp = got.br_blockcount - del->br_blockcount;
3044 xfs_bmap_trace_pre_update(fname, "1", ip, idx, whichfork);
3045 xfs_bmbt_set_blockcount(ep, temp);
3046 ifp->if_lastex = idx;
3048 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3050 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3051 xfs_bmap_trace_post_update(fname, "1", ip, idx,
3056 xfs_bmap_trace_post_update(fname, "1", ip, idx, whichfork);
3058 flags |= XFS_ILOG_FEXT(whichfork);
3061 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3063 got.br_blockcount - del->br_blockcount,
3070 * Deleting the middle of the extent.
3072 temp = del->br_startoff - got.br_startoff;
3073 xfs_bmap_trace_pre_update(fname, "0", ip, idx, whichfork);
3074 xfs_bmbt_set_blockcount(ep, temp);
3075 new.br_startoff = del_endoff;
3076 temp2 = got_endoff - del_endoff;
3077 new.br_blockcount = temp2;
3078 new.br_state = got.br_state;
3080 new.br_startblock = del_endblock;
3081 flags |= XFS_ILOG_CORE;
3083 if ((error = xfs_bmbt_update(cur,
3085 got.br_startblock, temp,
3088 if ((error = xfs_bmbt_increment(cur, 0, &i)))
3090 cur->bc_rec.b = new;
3091 error = xfs_bmbt_insert(cur, &i);
3092 if (error && error != ENOSPC)
3095 * If get no-space back from btree insert,
3096 * it tried a split, and we have a zero
3097 * block reservation.
3098 * Fix up our state and return the error.
3100 if (error == ENOSPC) {
3102 * Reset the cursor, don't trust
3103 * it after any insert operation.
3105 if ((error = xfs_bmbt_lookup_eq(cur,
3112 * Update the btree record back
3113 * to the original value.
3115 if ((error = xfs_bmbt_update(cur,
3122 * Reset the extent record back
3123 * to the original value.
3125 xfs_bmbt_set_blockcount(ep,
3128 error = XFS_ERROR(ENOSPC);
3133 flags |= XFS_ILOG_FEXT(whichfork);
3134 XFS_IFORK_NEXT_SET(ip, whichfork,
3135 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3137 ASSERT(whichfork == XFS_DATA_FORK);
3138 temp = xfs_bmap_worst_indlen(ip, temp);
3139 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3140 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3141 new.br_startblock = NULLSTARTBLOCK((int)temp2);
3142 da_new = temp + temp2;
3143 while (da_new > da_old) {
3147 xfs_bmbt_set_startblock(ep,
3148 NULLSTARTBLOCK((int)temp));
3150 if (da_new == da_old)
3156 NULLSTARTBLOCK((int)temp2);
3160 xfs_bmap_trace_post_update(fname, "0", ip, idx, whichfork);
3161 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 1, &new, NULL,
3163 xfs_bmap_insert_exlist(ip, idx + 1, 1, &new, whichfork);
3164 ifp->if_lastex = idx + 1;
3168 * If we need to, add to list of extents to delete.
3171 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3174 * Adjust inode # blocks in the file.
3177 ip->i_d.di_nblocks -= nblks;
3179 * Adjust quota data.
3182 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
3185 * Account for change in delayed indirect blocks.
3186 * Nothing to do for disk quota accounting here.
3188 ASSERT(da_old >= da_new);
3189 if (da_old > da_new)
3190 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int)(da_old - da_new),
3198 * Remove the entry "free" from the free item list. Prev points to the
3199 * previous entry, unless "free" is the head of the list.
3203 xfs_bmap_free_t *flist, /* free item list header */
3204 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3205 xfs_bmap_free_item_t *free) /* list item to be freed */
3208 prev->xbfi_next = free->xbfi_next;
3210 flist->xbf_first = free->xbfi_next;
3212 kmem_zone_free(xfs_bmap_free_item_zone, free);
3216 * Remove count entries from the extents array for inode "ip", starting
3217 * at index "idx". Copies the remaining items down over the deleted ones,
3218 * and gives back the excess memory.
3221 xfs_bmap_delete_exlist(
3222 xfs_inode_t *ip, /* incore inode pointer */
3223 xfs_extnum_t idx, /* starting delete index */
3224 xfs_extnum_t count, /* count of items to delete */
3225 int whichfork) /* data or attr fork */
3227 xfs_bmbt_rec_t *base; /* base of extent list */
3228 xfs_ifork_t *ifp; /* inode fork pointer */
3229 xfs_extnum_t nextents; /* number of extents in list after */
3231 ifp = XFS_IFORK_PTR(ip, whichfork);
3232 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3233 base = ifp->if_u1.if_extents;
3234 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - count;
3235 memmove(&base[idx], &base[idx + count],
3236 (nextents - idx) * sizeof(*base));
3237 xfs_iext_realloc(ip, -count, whichfork);
3241 * Convert an extents-format file into a btree-format file.
3242 * The new file will have a root block (in the inode) and a single child block.
3244 STATIC int /* error */
3245 xfs_bmap_extents_to_btree(
3246 xfs_trans_t *tp, /* transaction pointer */
3247 xfs_inode_t *ip, /* incore inode pointer */
3248 xfs_fsblock_t *firstblock, /* first-block-allocated */
3249 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3250 xfs_btree_cur_t **curp, /* cursor returned to caller */
3251 int wasdel, /* converting a delayed alloc */
3252 int *logflagsp, /* inode logging flags */
3253 int whichfork) /* data or attr fork */
3255 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
3256 xfs_buf_t *abp; /* buffer for ablock */
3257 xfs_alloc_arg_t args; /* allocation arguments */
3258 xfs_bmbt_rec_t *arp; /* child record pointer */
3259 xfs_bmbt_block_t *block; /* btree root block */
3260 xfs_btree_cur_t *cur; /* bmap btree cursor */
3261 xfs_bmbt_rec_t *ep; /* extent list pointer */
3262 int error; /* error return value */
3263 xfs_extnum_t i, cnt; /* extent list index */
3264 xfs_ifork_t *ifp; /* inode fork pointer */
3265 xfs_bmbt_key_t *kp; /* root block key pointer */
3266 xfs_mount_t *mp; /* mount structure */
3267 xfs_extnum_t nextents; /* extent list size */
3268 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3270 ifp = XFS_IFORK_PTR(ip, whichfork);
3271 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3272 ASSERT(ifp->if_ext_max ==
3273 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3275 * Make space in the inode incore.
3277 xfs_iroot_realloc(ip, 1, whichfork);
3278 ifp->if_flags |= XFS_IFBROOT;
3282 block = ifp->if_broot;
3283 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3284 block->bb_level = cpu_to_be16(1);
3285 block->bb_numrecs = cpu_to_be16(1);
3286 block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3287 block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3289 * Need a cursor. Can't allocate until bb_level is filled in.
3292 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
3294 cur->bc_private.b.firstblock = *firstblock;
3295 cur->bc_private.b.flist = flist;
3296 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3298 * Convert to a btree with two levels, one record in root.
3300 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3303 if (*firstblock == NULLFSBLOCK) {
3304 args.type = XFS_ALLOCTYPE_START_BNO;
3305 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3306 } else if (flist->xbf_low) {
3307 args.type = XFS_ALLOCTYPE_START_BNO;
3308 args.fsbno = *firstblock;
3310 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3311 args.fsbno = *firstblock;
3313 args.minlen = args.maxlen = args.prod = 1;
3314 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3315 args.minalignslop = 0;
3316 args.wasdel = wasdel;
3318 if ((error = xfs_alloc_vextent(&args))) {
3319 xfs_iroot_realloc(ip, -1, whichfork);
3320 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3324 * Allocation can't fail, the space was reserved.
3326 ASSERT(args.fsbno != NULLFSBLOCK);
3327 ASSERT(*firstblock == NULLFSBLOCK ||
3328 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3330 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3331 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3332 cur->bc_private.b.allocated++;
3333 ip->i_d.di_nblocks++;
3334 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3335 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3337 * Fill in the child block.
3339 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
3340 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3341 ablock->bb_level = 0;
3342 ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3343 ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3344 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3345 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3346 for (ep = ifp->if_u1.if_extents, cnt = i = 0; i < nextents; i++, ep++) {
3347 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
3348 arp->l0 = INT_GET(ep->l0, ARCH_CONVERT);
3349 arp->l1 = INT_GET(ep->l1, ARCH_CONVERT);
3353 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3354 ablock->bb_numrecs = cpu_to_be16(cnt);
3356 * Fill in the root key and pointer.
3358 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
3359 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3360 INT_SET(kp->br_startoff, ARCH_CONVERT, xfs_bmbt_disk_get_startoff(arp));
3361 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
3362 INT_SET(*pp, ARCH_CONVERT, args.fsbno);
3364 * Do all this logging at the end so that
3365 * the root is at the right level.
3367 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
3368 xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3369 ASSERT(*curp == NULL);
3371 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
3376 * Insert new item(s) in the extent list for inode "ip".
3377 * Count new items are inserted at offset idx.
3380 xfs_bmap_insert_exlist(
3381 xfs_inode_t *ip, /* incore inode pointer */
3382 xfs_extnum_t idx, /* starting index of new items */
3383 xfs_extnum_t count, /* number of inserted items */
3384 xfs_bmbt_irec_t *new, /* items to insert */
3385 int whichfork) /* data or attr fork */
3387 xfs_bmbt_rec_t *base; /* extent list base */
3388 xfs_ifork_t *ifp; /* inode fork pointer */
3389 xfs_extnum_t nextents; /* extent list size */
3390 xfs_extnum_t to; /* extent list index */
3392 ifp = XFS_IFORK_PTR(ip, whichfork);
3393 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3394 xfs_iext_realloc(ip, count, whichfork);
3395 base = ifp->if_u1.if_extents;
3396 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3397 memmove(&base[idx + count], &base[idx],
3398 (nextents - (idx + count)) * sizeof(*base));
3399 for (to = idx; to < idx + count; to++, new++)
3400 xfs_bmbt_set_all(&base[to], new);
3404 * Helper routine to reset inode di_forkoff field when switching
3405 * attribute fork from local to extent format - we reset it where
3406 * possible to make space available for inline data fork extents.
3409 xfs_bmap_forkoff_reset(
3414 if (whichfork == XFS_ATTR_FORK &&
3415 (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
3416 (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
3417 ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
3418 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3419 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
3420 (uint)sizeof(xfs_bmbt_rec_t);
3421 ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
3422 (uint)sizeof(xfs_bmbt_rec_t);
3427 * Convert a local file to an extents file.
3428 * This code is out of bounds for data forks of regular files,
3429 * since the file data needs to get logged so things will stay consistent.
3430 * (The bmap-level manipulations are ok, though).
3432 STATIC int /* error */
3433 xfs_bmap_local_to_extents(
3434 xfs_trans_t *tp, /* transaction pointer */
3435 xfs_inode_t *ip, /* incore inode pointer */
3436 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3437 xfs_extlen_t total, /* total blocks needed by transaction */
3438 int *logflagsp, /* inode logging flags */
3439 int whichfork) /* data or attr fork */
3441 int error; /* error return value */
3442 int flags; /* logging flags returned */
3443 #ifdef XFS_BMAP_TRACE
3444 static char fname[] = "xfs_bmap_local_to_extents";
3446 xfs_ifork_t *ifp; /* inode fork pointer */
3449 * We don't want to deal with the case of keeping inode data inline yet.
3450 * So sending the data fork of a regular inode is invalid.
3452 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3453 whichfork == XFS_DATA_FORK));
3454 ifp = XFS_IFORK_PTR(ip, whichfork);
3455 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3458 if (ifp->if_bytes) {
3459 xfs_alloc_arg_t args; /* allocation arguments */
3460 xfs_buf_t *bp; /* buffer for extent list block */
3461 xfs_bmbt_rec_t *ep; /* extent list pointer */
3464 args.mp = ip->i_mount;
3465 ASSERT(ifp->if_flags & XFS_IFINLINE);
3467 * Allocate a block. We know we need only one, since the
3468 * file currently fits in an inode.
3470 if (*firstblock == NULLFSBLOCK) {
3471 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3472 args.type = XFS_ALLOCTYPE_START_BNO;
3474 args.fsbno = *firstblock;
3475 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3478 args.mod = args.minleft = args.alignment = args.wasdel =
3479 args.isfl = args.minalignslop = 0;
3480 args.minlen = args.maxlen = args.prod = 1;
3481 if ((error = xfs_alloc_vextent(&args)))
3484 * Can't fail, the space was reserved.
3486 ASSERT(args.fsbno != NULLFSBLOCK);
3487 ASSERT(args.len == 1);
3488 *firstblock = args.fsbno;
3489 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3490 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3492 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3493 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3494 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3495 xfs_iext_realloc(ip, 1, whichfork);
3496 ep = ifp->if_u1.if_extents;
3497 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3498 xfs_bmap_trace_post_update(fname, "new", ip, 0, whichfork);
3499 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3500 ip->i_d.di_nblocks = 1;
3501 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
3502 XFS_TRANS_DQ_BCOUNT, 1L);
3503 flags |= XFS_ILOG_FEXT(whichfork);
3505 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3506 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3508 ifp->if_flags &= ~XFS_IFINLINE;
3509 ifp->if_flags |= XFS_IFEXTENTS;
3510 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3511 flags |= XFS_ILOG_CORE;
3517 xfs_bmbt_rec_t * /* pointer to found extent entry */
3518 xfs_bmap_do_search_extents(
3519 xfs_bmbt_rec_t *base, /* base of extent list */
3520 xfs_extnum_t lastx, /* last extent index used */
3521 xfs_extnum_t nextents, /* extent list size */
3522 xfs_fileoff_t bno, /* block number searched for */
3523 int *eofp, /* out: end of file found */
3524 xfs_extnum_t *lastxp, /* out: last extent index */
3525 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3526 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3528 xfs_bmbt_rec_t *ep; /* extent list entry pointer */
3529 xfs_bmbt_irec_t got; /* extent list entry, decoded */
3530 int high; /* high index of binary search */
3531 int low; /* low index of binary search */
3534 * Initialize the extent entry structure to catch access to
3535 * uninitialized br_startblock field.
3537 got.br_startoff = 0xffa5a5a5a5a5a5a5LL;
3538 got.br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3539 got.br_state = XFS_EXT_INVALID;
3542 got.br_startblock = 0xffffa5a5a5a5a5a5LL;
3544 got.br_startblock = 0xffffa5a5;
3547 if (lastx != NULLEXTNUM && lastx < nextents)
3551 prevp->br_startoff = NULLFILEOFF;
3552 if (ep && bno >= (got.br_startoff = xfs_bmbt_get_startoff(ep)) &&
3553 bno < got.br_startoff +
3554 (got.br_blockcount = xfs_bmbt_get_blockcount(ep)))
3556 else if (ep && lastx < nextents - 1 &&
3557 bno >= (got.br_startoff = xfs_bmbt_get_startoff(ep + 1)) &&
3558 bno < got.br_startoff +
3559 (got.br_blockcount = xfs_bmbt_get_blockcount(ep + 1))) {
3563 } else if (nextents == 0)
3565 else if (bno == 0 &&
3566 (got.br_startoff = xfs_bmbt_get_startoff(base)) == 0) {
3569 got.br_blockcount = xfs_bmbt_get_blockcount(ep);
3572 /* binary search the extents array */
3574 high = nextents - 1;
3575 while (low <= high) {
3576 XFS_STATS_INC(xs_cmp_exlist);
3577 lastx = (low + high) >> 1;
3579 got.br_startoff = xfs_bmbt_get_startoff(ep);
3580 got.br_blockcount = xfs_bmbt_get_blockcount(ep);
3581 if (bno < got.br_startoff)
3583 else if (bno >= got.br_startoff + got.br_blockcount)
3586 got.br_startblock = xfs_bmbt_get_startblock(ep);
3587 got.br_state = xfs_bmbt_get_state(ep);
3594 if (bno >= got.br_startoff + got.br_blockcount) {
3596 if (lastx == nextents) {
3598 got.br_startblock = xfs_bmbt_get_startblock(ep);
3599 got.br_state = xfs_bmbt_get_state(ep);
3604 xfs_bmbt_get_all(ep, prevp);
3606 got.br_startoff = xfs_bmbt_get_startoff(ep);
3607 got.br_blockcount = xfs_bmbt_get_blockcount(ep);
3612 xfs_bmbt_get_all(ep - 1, prevp);
3616 got.br_startblock = xfs_bmbt_get_startblock(ep);
3617 got.br_state = xfs_bmbt_get_state(ep);
3625 * Search the extents list for the inode, for the extent containing bno.
3626 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3627 * *eofp will be set, and *prevp will contain the last entry (null if none).
3628 * Else, *lastxp will be set to the index of the found
3629 * entry; *gotp will contain the entry.
3631 STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
3632 xfs_bmap_search_extents(
3633 xfs_inode_t *ip, /* incore inode pointer */
3634 xfs_fileoff_t bno, /* block number searched for */
3635 int whichfork, /* data or attr fork */
3636 int *eofp, /* out: end of file found */
3637 xfs_extnum_t *lastxp, /* out: last extent index */
3638 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3639 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3641 xfs_ifork_t *ifp; /* inode fork pointer */
3642 xfs_bmbt_rec_t *base; /* base of extent list */
3643 xfs_extnum_t lastx; /* last extent index used */
3644 xfs_extnum_t nextents; /* extent list size */
3645 xfs_bmbt_rec_t *ep; /* extent list entry pointer */
3646 int rt; /* realtime flag */
3648 XFS_STATS_INC(xs_look_exlist);
3649 ifp = XFS_IFORK_PTR(ip, whichfork);
3650 lastx = ifp->if_lastex;
3651 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3652 base = &ifp->if_u1.if_extents[0];
3654 ep = xfs_bmap_do_search_extents(base, lastx, nextents, bno, eofp,
3655 lastxp, gotp, prevp);
3656 rt = ip->i_d.di_flags & XFS_DIFLAG_REALTIME;
3657 if(!rt && !gotp->br_startblock && (*lastxp != NULLEXTNUM)) {
3658 cmn_err(CE_PANIC,"Access to block zero: fs: <%s> inode: %lld "
3659 "start_block : %llx start_off : %llx blkcnt : %llx "
3660 "extent-state : %x \n",
3661 (ip->i_mount)->m_fsname,(long long)ip->i_ino,
3662 gotp->br_startblock, gotp->br_startoff,
3663 gotp->br_blockcount,gotp->br_state);
3669 #ifdef XFS_BMAP_TRACE
3670 ktrace_t *xfs_bmap_trace_buf;
3673 * Add a bmap trace buffer entry. Base routine for the others.
3676 xfs_bmap_trace_addentry(
3677 int opcode, /* operation */
3678 char *fname, /* function name */
3679 char *desc, /* operation description */
3680 xfs_inode_t *ip, /* incore inode pointer */
3681 xfs_extnum_t idx, /* index of entry(ies) */
3682 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
3683 xfs_bmbt_rec_t *r1, /* first record */
3684 xfs_bmbt_rec_t *r2, /* second record or null */
3685 int whichfork) /* data or attr fork */
3689 ASSERT(cnt == 1 || cnt == 2);
3694 memset(&tr2, 0, sizeof(tr2));
3697 ktrace_enter(xfs_bmap_trace_buf,
3698 (void *)(__psint_t)(opcode | (whichfork << 16)),
3699 (void *)fname, (void *)desc, (void *)ip,
3700 (void *)(__psint_t)idx,
3701 (void *)(__psint_t)cnt,
3702 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3703 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3704 (void *)(__psunsigned_t)(r1->l0 >> 32),
3705 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3706 (void *)(__psunsigned_t)(r1->l1 >> 32),
3707 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3708 (void *)(__psunsigned_t)(r2->l0 >> 32),
3709 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3710 (void *)(__psunsigned_t)(r2->l1 >> 32),
3711 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3713 ASSERT(ip->i_xtrace);
3714 ktrace_enter(ip->i_xtrace,
3715 (void *)(__psint_t)(opcode | (whichfork << 16)),
3716 (void *)fname, (void *)desc, (void *)ip,
3717 (void *)(__psint_t)idx,
3718 (void *)(__psint_t)cnt,
3719 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3720 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3721 (void *)(__psunsigned_t)(r1->l0 >> 32),
3722 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3723 (void *)(__psunsigned_t)(r1->l1 >> 32),
3724 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3725 (void *)(__psunsigned_t)(r2->l0 >> 32),
3726 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3727 (void *)(__psunsigned_t)(r2->l1 >> 32),
3728 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3733 * Add bmap trace entry prior to a call to xfs_bmap_delete_exlist.
3736 xfs_bmap_trace_delete(
3737 char *fname, /* function name */
3738 char *desc, /* operation description */
3739 xfs_inode_t *ip, /* incore inode pointer */
3740 xfs_extnum_t idx, /* index of entry(entries) deleted */
3741 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3742 int whichfork) /* data or attr fork */
3744 xfs_ifork_t *ifp; /* inode fork pointer */
3746 ifp = XFS_IFORK_PTR(ip, whichfork);
3747 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
3748 cnt, &ifp->if_u1.if_extents[idx],
3749 cnt == 2 ? &ifp->if_u1.if_extents[idx + 1] : NULL,
3754 * Add bmap trace entry prior to a call to xfs_bmap_insert_exlist, or
3755 * reading in the extents list from the disk (in the btree).
3758 xfs_bmap_trace_insert(
3759 char *fname, /* function name */
3760 char *desc, /* operation description */
3761 xfs_inode_t *ip, /* incore inode pointer */
3762 xfs_extnum_t idx, /* index of entry(entries) inserted */
3763 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3764 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3765 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3766 int whichfork) /* data or attr fork */
3768 xfs_bmbt_rec_t tr1; /* compressed record 1 */
3769 xfs_bmbt_rec_t tr2; /* compressed record 2 if needed */
3771 xfs_bmbt_set_all(&tr1, r1);
3774 xfs_bmbt_set_all(&tr2, r2);
3779 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3780 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3784 * Add bmap trace entry after updating an extent list entry in place.
3787 xfs_bmap_trace_post_update(
3788 char *fname, /* function name */
3789 char *desc, /* operation description */
3790 xfs_inode_t *ip, /* incore inode pointer */
3791 xfs_extnum_t idx, /* index of entry updated */
3792 int whichfork) /* data or attr fork */
3794 xfs_ifork_t *ifp; /* inode fork pointer */
3796 ifp = XFS_IFORK_PTR(ip, whichfork);
3797 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
3798 1, &ifp->if_u1.if_extents[idx], NULL, whichfork);
3802 * Add bmap trace entry prior to updating an extent list entry in place.
3805 xfs_bmap_trace_pre_update(
3806 char *fname, /* function name */
3807 char *desc, /* operation description */
3808 xfs_inode_t *ip, /* incore inode pointer */
3809 xfs_extnum_t idx, /* index of entry to be updated */
3810 int whichfork) /* data or attr fork */
3812 xfs_ifork_t *ifp; /* inode fork pointer */
3814 ifp = XFS_IFORK_PTR(ip, whichfork);
3815 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
3816 &ifp->if_u1.if_extents[idx], NULL, whichfork);
3818 #endif /* XFS_BMAP_TRACE */
3821 * Compute the worst-case number of indirect blocks that will be used
3822 * for ip's delayed extent of length "len".
3824 STATIC xfs_filblks_t
3825 xfs_bmap_worst_indlen(
3826 xfs_inode_t *ip, /* incore inode pointer */
3827 xfs_filblks_t len) /* delayed extent length */
3829 int level; /* btree level number */
3830 int maxrecs; /* maximum record count at this level */
3831 xfs_mount_t *mp; /* mount structure */
3832 xfs_filblks_t rval; /* return value */
3835 maxrecs = mp->m_bmap_dmxr[0];
3836 for (level = 0, rval = 0;
3837 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3840 do_div(len, maxrecs);
3843 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3846 maxrecs = mp->m_bmap_dmxr[1];
3851 #if defined(XFS_RW_TRACE)
3860 if (ip->i_rwtrace == NULL)
3862 ktrace_enter(ip->i_rwtrace,
3863 (void *)(__psint_t)XFS_BUNMAPI,
3865 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3866 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3867 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3868 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3869 (void *)(__psint_t)len,
3870 (void *)(__psint_t)flags,
3871 (void *)(unsigned long)current_cpu(),
3883 * Convert inode from non-attributed to attributed.
3884 * Must not be in a transaction, ip must not be locked.
3886 int /* error code */
3887 xfs_bmap_add_attrfork(
3888 xfs_inode_t *ip, /* incore inode pointer */
3889 int size, /* space new attribute needs */
3890 int rsvd) /* xact may use reserved blks */
3892 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3893 xfs_bmap_free_t flist; /* freed extent list */
3894 xfs_mount_t *mp; /* mount structure */
3895 xfs_trans_t *tp; /* transaction pointer */
3896 unsigned long s; /* spinlock spl value */
3897 int blks; /* space reservation */
3898 int version = 1; /* superblock attr version */
3899 int committed; /* xaction was committed */
3900 int logflags; /* logging flags */
3901 int error; /* error return value */
3903 ASSERT(XFS_IFORK_Q(ip) == 0);
3904 ASSERT(ip->i_df.if_ext_max ==
3905 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3908 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3909 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3910 blks = XFS_ADDAFORK_SPACE_RES(mp);
3912 tp->t_flags |= XFS_TRANS_RESERVE;
3913 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3914 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3916 xfs_ilock(ip, XFS_ILOCK_EXCL);
3917 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
3918 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3919 XFS_QMOPT_RES_REGBLKS);
3921 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3922 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3925 if (XFS_IFORK_Q(ip))
3927 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3929 * For inodes coming from pre-6.2 filesystems.
3931 ASSERT(ip->i_d.di_aformat == 0);
3932 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3934 ASSERT(ip->i_d.di_anextents == 0);
3935 VN_HOLD(XFS_ITOV(ip));
3936 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3937 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3938 switch (ip->i_d.di_format) {
3939 case XFS_DINODE_FMT_DEV:
3940 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3942 case XFS_DINODE_FMT_UUID:
3943 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3945 case XFS_DINODE_FMT_LOCAL:
3946 case XFS_DINODE_FMT_EXTENTS:
3947 case XFS_DINODE_FMT_BTREE:
3948 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3949 if (!ip->i_d.di_forkoff)
3950 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3951 else if (mp->m_flags & XFS_MOUNT_ATTR2)
3956 error = XFS_ERROR(EINVAL);
3959 ip->i_df.if_ext_max =
3960 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3961 ASSERT(ip->i_afp == NULL);
3962 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3963 ip->i_afp->if_ext_max =
3964 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3965 ip->i_afp->if_flags = XFS_IFEXTENTS;
3967 XFS_BMAP_INIT(&flist, &firstblock);
3968 switch (ip->i_d.di_format) {
3969 case XFS_DINODE_FMT_LOCAL:
3970 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3973 case XFS_DINODE_FMT_EXTENTS:
3974 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3977 case XFS_DINODE_FMT_BTREE:
3978 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3986 xfs_trans_log_inode(tp, ip, logflags);
3989 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb) ||
3990 (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2)) {
3991 __int64_t sbfields = 0;
3993 s = XFS_SB_LOCK(mp);
3994 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) {
3995 XFS_SB_VERSION_ADDATTR(&mp->m_sb);
3996 sbfields |= XFS_SB_VERSIONNUM;
3998 if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2) {
3999 XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
4000 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
4003 XFS_SB_UNLOCK(mp, s);
4004 xfs_mod_sb(tp, sbfields);
4006 XFS_SB_UNLOCK(mp, s);
4008 if ((error = xfs_bmap_finish(&tp, &flist, firstblock, &committed)))
4010 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES, NULL);
4011 ASSERT(ip->i_df.if_ext_max ==
4012 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4015 xfs_bmap_cancel(&flist);
4017 ASSERT(ismrlocked(&ip->i_lock,MR_UPDATE));
4018 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4020 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4021 ASSERT(ip->i_df.if_ext_max ==
4022 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4027 * Add the extent to the list of extents to be free at transaction end.
4028 * The list is maintained sorted (by block number).
4033 xfs_fsblock_t bno, /* fs block number of extent */
4034 xfs_filblks_t len, /* length of extent */
4035 xfs_bmap_free_t *flist, /* list of extents */
4036 xfs_mount_t *mp) /* mount point structure */
4038 xfs_bmap_free_item_t *cur; /* current (next) element */
4039 xfs_bmap_free_item_t *new; /* new element */
4040 xfs_bmap_free_item_t *prev; /* previous element */
4042 xfs_agnumber_t agno;
4043 xfs_agblock_t agbno;
4045 ASSERT(bno != NULLFSBLOCK);
4047 ASSERT(len <= MAXEXTLEN);
4048 ASSERT(!ISNULLSTARTBLOCK(bno));
4049 agno = XFS_FSB_TO_AGNO(mp, bno);
4050 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4051 ASSERT(agno < mp->m_sb.sb_agcount);
4052 ASSERT(agbno < mp->m_sb.sb_agblocks);
4053 ASSERT(len < mp->m_sb.sb_agblocks);
4054 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4056 ASSERT(xfs_bmap_free_item_zone != NULL);
4057 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4058 new->xbfi_startblock = bno;
4059 new->xbfi_blockcount = (xfs_extlen_t)len;
4060 for (prev = NULL, cur = flist->xbf_first;
4062 prev = cur, cur = cur->xbfi_next) {
4063 if (cur->xbfi_startblock >= bno)
4067 prev->xbfi_next = new;
4069 flist->xbf_first = new;
4070 new->xbfi_next = cur;
4075 * Compute and fill in the value of the maximum depth of a bmap btree
4076 * in this filesystem. Done once, during mount.
4079 xfs_bmap_compute_maxlevels(
4080 xfs_mount_t *mp, /* file system mount structure */
4081 int whichfork) /* data or attr fork */
4083 int level; /* btree level */
4084 uint maxblocks; /* max blocks at this level */
4085 uint maxleafents; /* max leaf entries possible */
4086 int maxrootrecs; /* max records in root block */
4087 int minleafrecs; /* min records in leaf block */
4088 int minnoderecs; /* min records in node block */
4089 int sz; /* root block size */
4092 * The maximum number of extents in a file, hence the maximum
4093 * number of leaf entries, is controlled by the type of di_nextents
4094 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4095 * (a signed 16-bit number, xfs_aextnum_t).
4097 if (whichfork == XFS_DATA_FORK) {
4098 maxleafents = MAXEXTNUM;
4099 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4100 XFS_BMDR_SPACE_CALC(MINDBTPTRS) : mp->m_attroffset;
4102 maxleafents = MAXAEXTNUM;
4103 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4104 XFS_BMDR_SPACE_CALC(MINABTPTRS) :
4105 mp->m_sb.sb_inodesize - mp->m_attroffset;
4107 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
4108 minleafrecs = mp->m_bmap_dmnr[0];
4109 minnoderecs = mp->m_bmap_dmnr[1];
4110 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4111 for (level = 1; maxblocks > 1; level++) {
4112 if (maxblocks <= maxrootrecs)
4115 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4117 mp->m_bm_maxlevels[whichfork] = level;
4121 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4122 * caller. Frees all the extents that need freeing, which must be done
4123 * last due to locking considerations. We never free any extents in
4124 * the first transaction. This is to allow the caller to make the first
4125 * transaction a synchronous one so that the pointers to the data being
4126 * broken in this transaction will be permanent before the data is actually
4127 * freed. This is necessary to prevent blocks from being reallocated
4128 * and written to before the free and reallocation are actually permanent.
4129 * We do not just make the first transaction synchronous here, because
4130 * there are more efficient ways to gain the same protection in some cases
4131 * (see the file truncation code).
4133 * Return 1 if the given transaction was committed and a new one
4134 * started, and 0 otherwise in the committed parameter.
4139 xfs_trans_t **tp, /* transaction pointer addr */
4140 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4141 xfs_fsblock_t firstblock, /* controlled ag for allocs */
4142 int *committed) /* xact committed or not */
4144 xfs_efd_log_item_t *efd; /* extent free data */
4145 xfs_efi_log_item_t *efi; /* extent free intention */
4146 int error; /* error return value */
4147 xfs_bmap_free_item_t *free; /* free extent list item */
4148 unsigned int logres; /* new log reservation */
4149 unsigned int logcount; /* new log count */
4150 xfs_mount_t *mp; /* filesystem mount structure */
4151 xfs_bmap_free_item_t *next; /* next item on free list */
4152 xfs_trans_t *ntp; /* new transaction pointer */
4154 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4155 if (flist->xbf_count == 0) {
4160 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4161 for (free = flist->xbf_first; free; free = free->xbfi_next)
4162 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4163 free->xbfi_blockcount);
4164 logres = ntp->t_log_res;
4165 logcount = ntp->t_log_count;
4166 ntp = xfs_trans_dup(*tp);
4167 error = xfs_trans_commit(*tp, 0, NULL);
4171 * We have a new transaction, so we should return committed=1,
4172 * even though we're returning an error.
4177 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4180 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4181 for (free = flist->xbf_first; free != NULL; free = next) {
4182 next = free->xbfi_next;
4183 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4184 free->xbfi_blockcount))) {
4186 * The bmap free list will be cleaned up at a
4187 * higher level. The EFI will be canceled when
4188 * this transaction is aborted.
4189 * Need to force shutdown here to make sure it
4190 * happens, since this transaction may not be
4194 if (!XFS_FORCED_SHUTDOWN(mp))
4195 xfs_force_shutdown(mp,
4196 (error == EFSCORRUPTED) ?
4197 XFS_CORRUPT_INCORE :
4198 XFS_METADATA_IO_ERROR);
4201 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4202 free->xbfi_blockcount);
4203 xfs_bmap_del_free(flist, NULL, free);
4209 * Free up any items left in the list.
4213 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4215 xfs_bmap_free_item_t *free; /* free list item */
4216 xfs_bmap_free_item_t *next;
4218 if (flist->xbf_count == 0)
4220 ASSERT(flist->xbf_first != NULL);
4221 for (free = flist->xbf_first; free; free = next) {
4222 next = free->xbfi_next;
4223 xfs_bmap_del_free(flist, NULL, free);
4225 ASSERT(flist->xbf_count == 0);
4229 * Returns the file-relative block number of the first unused block(s)
4230 * in the file with at least "len" logically contiguous blocks free.
4231 * This is the lowest-address hole if the file has holes, else the first block
4232 * past the end of file.
4233 * Return 0 if the file is currently local (in-inode).
4236 xfs_bmap_first_unused(
4237 xfs_trans_t *tp, /* transaction pointer */
4238 xfs_inode_t *ip, /* incore inode */
4239 xfs_extlen_t len, /* size of hole to find */
4240 xfs_fileoff_t *first_unused, /* unused block */
4241 int whichfork) /* data or attr fork */
4243 xfs_bmbt_rec_t *base; /* base of extent array */
4244 xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
4245 int error; /* error return value */
4246 xfs_ifork_t *ifp; /* inode fork pointer */
4247 xfs_fileoff_t lastaddr; /* last block number seen */
4248 xfs_fileoff_t lowest; /* lowest useful block */
4249 xfs_fileoff_t max; /* starting useful block */
4250 xfs_fileoff_t off; /* offset for this block */
4251 xfs_extnum_t nextents; /* number of extent entries */
4253 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4254 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4255 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4256 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4260 ifp = XFS_IFORK_PTR(ip, whichfork);
4261 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4262 (error = xfs_iread_extents(tp, ip, whichfork)))
4264 lowest = *first_unused;
4265 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4266 base = &ifp->if_u1.if_extents[0];
4267 for (lastaddr = 0, max = lowest, ep = base;
4268 ep < &base[nextents];
4270 off = xfs_bmbt_get_startoff(ep);
4272 * See if the hole before this extent will work.
4274 if (off >= lowest + len && off - max >= len) {
4275 *first_unused = max;
4278 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4279 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4281 *first_unused = max;
4286 * Returns the file-relative block number of the last block + 1 before
4287 * last_block (input value) in the file.
4288 * This is not based on i_size, it is based on the extent list.
4289 * Returns 0 for local files, as they do not have an extent list.
4292 xfs_bmap_last_before(
4293 xfs_trans_t *tp, /* transaction pointer */
4294 xfs_inode_t *ip, /* incore inode */
4295 xfs_fileoff_t *last_block, /* last block */
4296 int whichfork) /* data or attr fork */
4298 xfs_fileoff_t bno; /* input file offset */
4299 int eof; /* hit end of file */
4300 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4301 int error; /* error return value */
4302 xfs_bmbt_irec_t got; /* current extent value */
4303 xfs_ifork_t *ifp; /* inode fork pointer */
4304 xfs_extnum_t lastx; /* last extent used */
4305 xfs_bmbt_irec_t prev; /* previous extent value */
4307 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4308 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4309 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4310 return XFS_ERROR(EIO);
4311 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4315 ifp = XFS_IFORK_PTR(ip, whichfork);
4316 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4317 (error = xfs_iread_extents(tp, ip, whichfork)))
4319 bno = *last_block - 1;
4320 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4322 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4323 if (prev.br_startoff == NULLFILEOFF)
4326 *last_block = prev.br_startoff + prev.br_blockcount;
4329 * Otherwise *last_block is already the right answer.
4335 * Returns the file-relative block number of the first block past eof in
4336 * the file. This is not based on i_size, it is based on the extent list.
4337 * Returns 0 for local files, as they do not have an extent list.
4340 xfs_bmap_last_offset(
4341 xfs_trans_t *tp, /* transaction pointer */
4342 xfs_inode_t *ip, /* incore inode */
4343 xfs_fileoff_t *last_block, /* last block */
4344 int whichfork) /* data or attr fork */
4346 xfs_bmbt_rec_t *base; /* base of extent array */
4347 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4348 int error; /* error return value */
4349 xfs_ifork_t *ifp; /* inode fork pointer */
4350 xfs_extnum_t nextents; /* number of extent entries */
4352 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4353 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4354 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4355 return XFS_ERROR(EIO);
4356 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4360 ifp = XFS_IFORK_PTR(ip, whichfork);
4361 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4362 (error = xfs_iread_extents(tp, ip, whichfork)))
4364 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4369 base = &ifp->if_u1.if_extents[0];
4370 ASSERT(base != NULL);
4371 ep = &base[nextents - 1];
4372 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4377 * Returns whether the selected fork of the inode has exactly one
4378 * block or not. For the data fork we check this matches di_size,
4379 * implying the file's range is 0..bsize-1.
4381 int /* 1=>1 block, 0=>otherwise */
4383 xfs_inode_t *ip, /* incore inode */
4384 int whichfork) /* data or attr fork */
4386 xfs_bmbt_rec_t *ep; /* ptr to fork's extent */
4387 xfs_ifork_t *ifp; /* inode fork pointer */
4388 int rval; /* return value */
4389 xfs_bmbt_irec_t s; /* internal version of extent */
4392 if (whichfork == XFS_DATA_FORK)
4393 return ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize;
4395 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4397 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4399 ifp = XFS_IFORK_PTR(ip, whichfork);
4400 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4401 ep = ifp->if_u1.if_extents;
4402 xfs_bmbt_get_all(ep, &s);
4403 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4404 if (rval && whichfork == XFS_DATA_FORK)
4405 ASSERT(ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4410 * Read in the extents to if_extents.
4411 * All inode fields are set up by caller, we just traverse the btree
4412 * and copy the records in. If the file system cannot contain unwritten
4413 * extents, the records are checked for no "state" flags.
4416 xfs_bmap_read_extents(
4417 xfs_trans_t *tp, /* transaction pointer */
4418 xfs_inode_t *ip, /* incore inode */
4419 int whichfork) /* data or attr fork */
4421 xfs_bmbt_block_t *block; /* current btree block */
4422 xfs_fsblock_t bno; /* block # of "block" */
4423 xfs_buf_t *bp; /* buffer for "block" */
4424 int error; /* error return value */
4425 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4426 #ifdef XFS_BMAP_TRACE
4427 static char fname[] = "xfs_bmap_read_extents";
4429 xfs_extnum_t i, j; /* index into the extents list */
4430 xfs_ifork_t *ifp; /* fork structure */
4431 int level; /* btree level, for checking */
4432 xfs_mount_t *mp; /* file system mount structure */
4433 xfs_bmbt_ptr_t *pp; /* pointer to block address */
4435 xfs_extnum_t room; /* number of entries there's room for */
4436 xfs_bmbt_rec_t *trp; /* target record pointer */
4440 ifp = XFS_IFORK_PTR(ip, whichfork);
4441 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4442 XFS_EXTFMT_INODE(ip);
4443 block = ifp->if_broot;
4445 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4447 level = be16_to_cpu(block->bb_level);
4449 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
4450 ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
4451 ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
4452 ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
4453 bno = INT_GET(*pp, ARCH_CONVERT);
4455 * Go down the tree until leaf level is reached, following the first
4456 * pointer (leftmost) at each level.
4458 while (level-- > 0) {
4459 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4460 XFS_BMAP_BTREE_REF)))
4462 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4463 XFS_WANT_CORRUPTED_GOTO(
4464 XFS_BMAP_SANITY_CHECK(mp, block, level),
4468 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
4469 1, mp->m_bmap_dmxr[1]);
4470 XFS_WANT_CORRUPTED_GOTO(
4471 XFS_FSB_SANITY_CHECK(mp, INT_GET(*pp, ARCH_CONVERT)),
4473 bno = INT_GET(*pp, ARCH_CONVERT);
4474 xfs_trans_brelse(tp, bp);
4477 * Here with bp and block set to the leftmost leaf node in the tree.
4479 room = ifp->if_bytes / (uint)sizeof(*trp);
4480 trp = ifp->if_u1.if_extents;
4483 * Loop over all leaf nodes. Copy information to the extent list.
4486 xfs_bmbt_rec_t *frp, *temp;
4487 xfs_fsblock_t nextbno;
4488 xfs_extnum_t num_recs;
4491 num_recs = be16_to_cpu(block->bb_numrecs);
4492 if (unlikely(i + num_recs > room)) {
4493 ASSERT(i + num_recs <= room);
4494 xfs_fs_cmn_err(CE_WARN, ip->i_mount,
4495 "corrupt dinode %Lu, (btree extents). Unmount and run xfs_repair.",
4496 (unsigned long long) ip->i_ino);
4497 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4502 XFS_WANT_CORRUPTED_GOTO(
4503 XFS_BMAP_SANITY_CHECK(mp, block, 0),
4506 * Read-ahead the next leaf block, if any.
4508 nextbno = be64_to_cpu(block->bb_rightsib);
4509 if (nextbno != NULLFSBLOCK)
4510 xfs_btree_reada_bufl(mp, nextbno, 1);
4512 * Copy records into the extent list.
4514 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
4515 block, 1, mp->m_bmap_dmxr[0]);
4517 for (j = 0; j < num_recs; j++, frp++, trp++) {
4518 trp->l0 = INT_GET(frp->l0, ARCH_CONVERT);
4519 trp->l1 = INT_GET(frp->l1, ARCH_CONVERT);
4521 if (exntf == XFS_EXTFMT_NOSTATE) {
4523 * Check all attribute bmap btree records and
4524 * any "older" data bmap btree records for a
4525 * set bit in the "extent flag" position.
4527 if (unlikely(xfs_check_nostate_extents(temp, num_recs))) {
4528 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4535 xfs_trans_brelse(tp, bp);
4538 * If we've reached the end, stop.
4540 if (bno == NULLFSBLOCK)
4542 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4543 XFS_BMAP_BTREE_REF)))
4545 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4547 ASSERT(i == ifp->if_bytes / (uint)sizeof(*trp));
4548 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4549 xfs_bmap_trace_exlist(fname, ip, i, whichfork);
4552 xfs_trans_brelse(tp, bp);
4553 return XFS_ERROR(EFSCORRUPTED);
4556 #ifdef XFS_BMAP_TRACE
4558 * Add bmap trace insert entries for all the contents of the extent list.
4561 xfs_bmap_trace_exlist(
4562 char *fname, /* function name */
4563 xfs_inode_t *ip, /* incore inode pointer */
4564 xfs_extnum_t cnt, /* count of entries in the list */
4565 int whichfork) /* data or attr fork */
4567 xfs_bmbt_rec_t *base; /* base of extent list */
4568 xfs_bmbt_rec_t *ep; /* current entry in extent list */
4569 xfs_extnum_t idx; /* extent list entry number */
4570 xfs_ifork_t *ifp; /* inode fork pointer */
4571 xfs_bmbt_irec_t s; /* extent list record */
4573 ifp = XFS_IFORK_PTR(ip, whichfork);
4574 ASSERT(cnt == ifp->if_bytes / (uint)sizeof(*base));
4575 base = ifp->if_u1.if_extents;
4576 for (idx = 0, ep = base; idx < cnt; idx++, ep++) {
4577 xfs_bmbt_get_all(ep, &s);
4578 xfs_bmap_trace_insert(fname, "exlist", ip, idx, 1, &s, NULL,
4586 * Validate that the bmbt_irecs being returned from bmapi are valid
4587 * given the callers original parameters. Specifically check the
4588 * ranges of the returned irecs to ensure that they only extent beyond
4589 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4592 xfs_bmap_validate_ret(
4596 xfs_bmbt_irec_t *mval,
4600 int i; /* index to map values */
4602 ASSERT(ret_nmap <= nmap);
4604 for (i = 0; i < ret_nmap; i++) {
4605 ASSERT(mval[i].br_blockcount > 0);
4606 if (!(flags & XFS_BMAPI_ENTIRE)) {
4607 ASSERT(mval[i].br_startoff >= bno);
4608 ASSERT(mval[i].br_blockcount <= len);
4609 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4612 ASSERT(mval[i].br_startoff < bno + len);
4613 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4617 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4618 mval[i].br_startoff);
4619 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4620 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4621 mval[i].br_startblock != HOLESTARTBLOCK);
4622 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4623 mval[i].br_state == XFS_EXT_UNWRITTEN);
4630 * Map file blocks to filesystem blocks.
4631 * File range is given by the bno/len pair.
4632 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4633 * into a hole or past eof.
4634 * Only allocates blocks from a single allocation group,
4635 * to avoid locking problems.
4636 * The returned value in "firstblock" from the first call in a transaction
4637 * must be remembered and presented to subsequent calls in "firstblock".
4638 * An upper bound for the number of blocks to be allocated is supplied to
4639 * the first call in "total"; if no allocation group has that many free
4640 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4644 xfs_trans_t *tp, /* transaction pointer */
4645 xfs_inode_t *ip, /* incore inode */
4646 xfs_fileoff_t bno, /* starting file offs. mapped */
4647 xfs_filblks_t len, /* length to map in file */
4648 int flags, /* XFS_BMAPI_... */
4649 xfs_fsblock_t *firstblock, /* first allocated block
4650 controls a.g. for allocs */
4651 xfs_extlen_t total, /* total blocks needed */
4652 xfs_bmbt_irec_t *mval, /* output: map values */
4653 int *nmap, /* i/o: mval size/count */
4654 xfs_bmap_free_t *flist) /* i/o: list extents to free */
4656 xfs_fsblock_t abno; /* allocated block number */
4657 xfs_extlen_t alen; /* allocated extent length */
4658 xfs_fileoff_t aoff; /* allocated file offset */
4659 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
4660 xfs_btree_cur_t *cur; /* bmap btree cursor */
4661 xfs_fileoff_t end; /* end of mapped file region */
4662 int eof; /* we've hit the end of extent list */
4663 char contig; /* allocation must be one extent */
4664 char delay; /* this request is for delayed alloc */
4665 char exact; /* don't do all of wasdelayed extent */
4666 char convert; /* unwritten extent I/O completion */
4667 xfs_bmbt_rec_t *ep; /* extent list entry pointer */
4668 int error; /* error return */
4669 xfs_bmbt_irec_t got; /* current extent list record */
4670 xfs_ifork_t *ifp; /* inode fork pointer */
4671 xfs_extlen_t indlen; /* indirect blocks length */
4672 xfs_extnum_t lastx; /* last useful extent number */
4673 int logflags; /* flags for transaction logging */
4674 xfs_extlen_t minleft; /* min blocks left after allocation */
4675 xfs_extlen_t minlen; /* min allocation size */
4676 xfs_mount_t *mp; /* xfs mount structure */
4677 int n; /* current extent index */
4678 int nallocs; /* number of extents alloc\'d */
4679 xfs_extnum_t nextents; /* number of extents in file */
4680 xfs_fileoff_t obno; /* old block number (offset) */
4681 xfs_bmbt_irec_t prev; /* previous extent list record */
4682 int tmp_logflags; /* temp flags holder */
4683 int whichfork; /* data or attr fork */
4684 char inhole; /* current location is hole in file */
4685 char stateless; /* ignore state flag set */
4686 char trim; /* output trimmed to match range */
4687 char userdata; /* allocating non-metadata */
4688 char wasdelay; /* old extent was delayed */
4689 char wr; /* this is a write request */
4690 char rt; /* this is a realtime file */
4691 char rsvd; /* OK to allocate reserved blocks */
4693 xfs_fileoff_t orig_bno; /* original block number value */
4694 int orig_flags; /* original flags arg value */
4695 xfs_filblks_t orig_len; /* original value of len arg */
4696 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4697 int orig_nmap; /* original value of *nmap */
4706 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4707 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4708 XFS_ATTR_FORK : XFS_DATA_FORK;
4710 if (unlikely(XFS_TEST_ERROR(
4711 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4712 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4713 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4714 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4715 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4716 return XFS_ERROR(EFSCORRUPTED);
4718 if (XFS_FORCED_SHUTDOWN(mp))
4719 return XFS_ERROR(EIO);
4720 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4721 ifp = XFS_IFORK_PTR(ip, whichfork);
4722 ASSERT(ifp->if_ext_max ==
4723 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4724 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4725 XFS_STATS_INC(xs_blk_mapw);
4727 XFS_STATS_INC(xs_blk_mapr);
4728 delay = (flags & XFS_BMAPI_DELAY) != 0;
4729 trim = (flags & XFS_BMAPI_ENTIRE) == 0;
4730 userdata = (flags & XFS_BMAPI_METADATA) == 0;
4731 convert = (flags & XFS_BMAPI_CONVERT) != 0;
4732 exact = (flags & XFS_BMAPI_EXACT) != 0;
4733 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
4734 contig = (flags & XFS_BMAPI_CONTIG) != 0;
4736 * stateless is used to combine extents which
4737 * differ only due to the state of the extents.
4738 * This technique is used from xfs_getbmap()
4739 * when the caller does not wish to see the
4740 * separation (which is the default).
4742 * This technique is also used when writing a
4743 * buffer which has been partially written,
4744 * (usually by being flushed during a chunkread),
4745 * to ensure one write takes place. This also
4746 * prevents a change in the xfs inode extents at
4747 * this time, intentionally. This change occurs
4748 * on completion of the write operation, in
4749 * xfs_strat_comp(), where the xfs_bmapi() call
4750 * is transactioned, and the extents combined.
4752 stateless = (flags & XFS_BMAPI_IGSTATE) != 0;
4753 if (stateless && wr) /* if writing unwritten space, no */
4754 wr = 0; /* allocations are allowed */
4755 ASSERT(wr || !delay);
4759 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4761 if ((error = xfs_bmap_local_to_extents(tp, ip,
4762 firstblock, total, &logflags, whichfork)))
4765 if (wr && *firstblock == NULLFSBLOCK) {
4766 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4767 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4772 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4773 (error = xfs_iread_extents(tp, ip, whichfork)))
4775 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4777 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4782 while (bno < end && n < *nmap) {
4784 * Reading past eof, act as though there's a hole
4788 got.br_startoff = end;
4789 inhole = eof || got.br_startoff > bno;
4790 wasdelay = wr && !inhole && !delay &&
4791 ISNULLSTARTBLOCK(got.br_startblock);
4793 * First, deal with the hole before the allocated space
4794 * that we found, if any.
4796 if (wr && (inhole || wasdelay)) {
4798 * For the wasdelay case, we could also just
4799 * allocate the stuff asked for in this bmap call
4800 * but that wouldn't be as good.
4802 if (wasdelay && !exact) {
4803 alen = (xfs_extlen_t)got.br_blockcount;
4804 aoff = got.br_startoff;
4805 if (lastx != NULLEXTNUM && lastx) {
4806 ep = &ifp->if_u1.if_extents[lastx - 1];
4807 xfs_bmbt_get_all(ep, &prev);
4809 } else if (wasdelay) {
4810 alen = (xfs_extlen_t)
4811 XFS_FILBLKS_MIN(len,
4813 got.br_blockcount) - bno);
4816 alen = (xfs_extlen_t)
4817 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4819 alen = (xfs_extlen_t)
4820 XFS_FILBLKS_MIN(alen,
4821 got.br_startoff - bno);
4824 minlen = contig ? alen : 1;
4828 /* Figure out the extent size, adjust alen */
4830 if (!(extsz = ip->i_d.di_extsize))
4831 extsz = mp->m_sb.sb_rextsize;
4833 extsz = ip->i_d.di_extsize;
4836 error = xfs_bmap_extsize_align(mp,
4838 rt, eof, delay, convert,
4844 extsz = alen / mp->m_sb.sb_rextsize;
4847 * Make a transaction-less quota reservation for
4848 * delayed allocation blocks. This number gets
4850 * We return EDQUOT if we haven't allocated
4851 * blks already inside this loop;
4853 if (XFS_TRANS_RESERVE_QUOTA_NBLKS(
4854 mp, NULL, ip, (long)alen, 0,
4855 rt ? XFS_QMOPT_RES_RTBLKS :
4856 XFS_QMOPT_RES_REGBLKS)) {
4859 ASSERT(cur == NULL);
4860 return XFS_ERROR(EDQUOT);
4866 * Split changing sb for alen and indlen since
4867 * they could be coming from different places.
4869 indlen = (xfs_extlen_t)
4870 xfs_bmap_worst_indlen(ip, alen);
4874 error = xfs_mod_incore_sb(mp,
4878 error = xfs_mod_incore_sb(mp,
4883 error = xfs_mod_incore_sb(mp,
4887 xfs_mod_incore_sb(ip->i_mount,
4891 xfs_mod_incore_sb(ip->i_mount,
4898 if (XFS_IS_QUOTA_ON(ip->i_mount))
4899 /* unreserve the blocks now */
4901 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4904 XFS_QMOPT_RES_RTBLKS :
4905 XFS_QMOPT_RES_REGBLKS);
4909 ip->i_delayed_blks += alen;
4910 abno = NULLSTARTBLOCK(indlen);
4913 * If first time, allocate and fill in
4914 * once-only bma fields.
4916 if (bma.ip == NULL) {
4924 /* Indicate if this is the first user data
4925 * in the file, or just any user data.
4928 bma.userdata = (aoff == 0) ?
4929 XFS_ALLOC_INITIAL_USER_DATA :
4933 * Fill in changeable bma fields.
4936 bma.firstblock = *firstblock;
4940 bma.wasdel = wasdelay;
4941 bma.minlen = minlen;
4942 bma.low = flist->xbf_low;
4943 bma.minleft = minleft;
4945 * Only want to do the alignment at the
4946 * eof if it is userdata and allocation length
4947 * is larger than a stripe unit.
4949 if (mp->m_dalign && alen >= mp->m_dalign &&
4950 userdata && whichfork == XFS_DATA_FORK) {
4951 if ((error = xfs_bmap_isaeof(ip, aoff,
4952 whichfork, &bma.aeof)))
4959 if ((error = xfs_bmap_alloc(&bma)))
4962 * Copy out result fields.
4965 if ((flist->xbf_low = bma.low))
4969 ASSERT(*firstblock == NULLFSBLOCK ||
4970 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4971 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4973 XFS_FSB_TO_AGNO(mp, *firstblock) <
4974 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4975 *firstblock = bma.firstblock;
4977 cur->bc_private.b.firstblock =
4979 if (abno == NULLFSBLOCK)
4981 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4982 cur = xfs_btree_init_cursor(mp,
4983 tp, NULL, 0, XFS_BTNUM_BMAP,
4985 cur->bc_private.b.firstblock =
4987 cur->bc_private.b.flist = flist;
4990 * Bump the number of extents we've allocated
4996 cur->bc_private.b.flags =
4997 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4998 got.br_startoff = aoff;
4999 got.br_startblock = abno;
5000 got.br_blockcount = alen;
5001 got.br_state = XFS_EXT_NORM; /* assume normal */
5003 * Determine state of extent, and the filesystem.
5004 * A wasdelay extent has been initialized, so
5005 * shouldn't be flagged as unwritten.
5007 if (wr && XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5008 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5009 got.br_state = XFS_EXT_UNWRITTEN;
5011 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
5012 firstblock, flist, &tmp_logflags, whichfork,
5014 logflags |= tmp_logflags;
5017 lastx = ifp->if_lastex;
5018 ep = &ifp->if_u1.if_extents[lastx];
5019 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5020 xfs_bmbt_get_all(ep, &got);
5021 ASSERT(got.br_startoff <= aoff);
5022 ASSERT(got.br_startoff + got.br_blockcount >=
5026 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
5027 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
5029 ASSERT(got.br_state == XFS_EXT_NORM ||
5030 got.br_state == XFS_EXT_UNWRITTEN);
5033 * Fall down into the found allocated space case.
5035 } else if (inhole) {
5037 * Reading in a hole.
5039 mval->br_startoff = bno;
5040 mval->br_startblock = HOLESTARTBLOCK;
5041 mval->br_blockcount =
5042 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5043 mval->br_state = XFS_EXT_NORM;
5044 bno += mval->br_blockcount;
5045 len -= mval->br_blockcount;
5051 * Then deal with the allocated space we found.
5054 if (trim && (got.br_startoff + got.br_blockcount > obno)) {
5057 ASSERT((bno >= obno) || (n == 0));
5059 mval->br_startoff = bno;
5060 if (ISNULLSTARTBLOCK(got.br_startblock)) {
5061 ASSERT(!wr || delay);
5062 mval->br_startblock = DELAYSTARTBLOCK;
5064 mval->br_startblock =
5066 (bno - got.br_startoff);
5068 * Return the minimum of what we got and what we
5069 * asked for for the length. We can use the len
5070 * variable here because it is modified below
5071 * and we could have been there before coming
5072 * here if the first part of the allocation
5073 * didn't overlap what was asked for.
5075 mval->br_blockcount =
5076 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5077 (bno - got.br_startoff));
5078 mval->br_state = got.br_state;
5079 ASSERT(mval->br_blockcount <= len);
5082 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
5083 ASSERT(!wr || delay);
5084 mval->br_startblock = DELAYSTARTBLOCK;
5089 * Check if writing previously allocated but
5090 * unwritten extents.
5092 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5093 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5095 * Modify (by adding) the state flag, if writing.
5097 ASSERT(mval->br_blockcount <= len);
5098 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5099 cur = xfs_btree_init_cursor(mp,
5100 tp, NULL, 0, XFS_BTNUM_BMAP,
5102 cur->bc_private.b.firstblock =
5104 cur->bc_private.b.flist = flist;
5106 mval->br_state = XFS_EXT_NORM;
5107 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
5108 firstblock, flist, &tmp_logflags, whichfork,
5110 logflags |= tmp_logflags;
5113 lastx = ifp->if_lastex;
5114 ep = &ifp->if_u1.if_extents[lastx];
5115 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5116 xfs_bmbt_get_all(ep, &got);
5118 * We may have combined previously unwritten
5119 * space with written space, so generate
5122 if (mval->br_blockcount < len)
5127 ((mval->br_startoff + mval->br_blockcount) <= end));
5128 ASSERT(!trim || (mval->br_blockcount <= len) ||
5129 (mval->br_startoff < obno));
5130 bno = mval->br_startoff + mval->br_blockcount;
5132 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5133 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5134 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5135 ASSERT(mval->br_state == mval[-1].br_state);
5136 mval[-1].br_blockcount = mval->br_blockcount;
5137 mval[-1].br_state = mval->br_state;
5138 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5139 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5140 mval[-1].br_startblock != HOLESTARTBLOCK &&
5141 mval->br_startblock ==
5142 mval[-1].br_startblock + mval[-1].br_blockcount &&
5143 (stateless || mval[-1].br_state == mval->br_state)) {
5144 ASSERT(mval->br_startoff ==
5145 mval[-1].br_startoff + mval[-1].br_blockcount);
5146 mval[-1].br_blockcount += mval->br_blockcount;
5148 mval->br_startblock == DELAYSTARTBLOCK &&
5149 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5150 mval->br_startoff ==
5151 mval[-1].br_startoff + mval[-1].br_blockcount) {
5152 mval[-1].br_blockcount += mval->br_blockcount;
5153 mval[-1].br_state = mval->br_state;
5154 } else if (!((n == 0) &&
5155 ((mval->br_startoff + mval->br_blockcount) <=
5161 * If we're done, stop now. Stop when we've allocated
5162 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5163 * the transaction may get too big.
5165 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5168 * Else go on to the next record.
5172 if (lastx >= nextents) {
5176 xfs_bmbt_get_all(ep, &got);
5178 ifp->if_lastex = lastx;
5181 * Transform from btree to extents, give it cur.
5183 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5184 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5186 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5187 &tmp_logflags, whichfork);
5188 logflags |= tmp_logflags;
5192 ASSERT(ifp->if_ext_max ==
5193 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5194 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5195 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5200 * Log everything. Do this after conversion, there's no point in
5201 * logging the extent list if we've converted to btree format.
5203 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5204 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5205 logflags &= ~XFS_ILOG_FEXT(whichfork);
5206 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5207 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5208 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5210 * Log whatever the flags say, even if error. Otherwise we might miss
5211 * detecting a case where the data is changed, there's an error,
5212 * and it's not logged so we don't shutdown when we should.
5216 xfs_trans_log_inode(tp, ip, logflags);
5220 ASSERT(*firstblock == NULLFSBLOCK ||
5221 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5223 cur->bc_private.b.firstblock) ||
5225 XFS_FSB_TO_AGNO(mp, *firstblock) <
5227 cur->bc_private.b.firstblock)));
5228 *firstblock = cur->bc_private.b.firstblock;
5230 xfs_btree_del_cursor(cur,
5231 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5234 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5240 * Map file blocks to filesystem blocks, simple version.
5241 * One block (extent) only, read-only.
5242 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5243 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5244 * was set and all the others were clear.
5248 xfs_trans_t *tp, /* transaction pointer */
5249 xfs_inode_t *ip, /* incore inode */
5250 int whichfork, /* data or attr fork */
5251 xfs_fsblock_t *fsb, /* output: mapped block */
5252 xfs_fileoff_t bno) /* starting file offs. mapped */
5254 int eof; /* we've hit the end of extent list */
5255 int error; /* error return */
5256 xfs_bmbt_irec_t got; /* current extent list record */
5257 xfs_ifork_t *ifp; /* inode fork pointer */
5258 xfs_extnum_t lastx; /* last useful extent number */
5259 xfs_bmbt_irec_t prev; /* previous extent list record */
5261 ifp = XFS_IFORK_PTR(ip, whichfork);
5263 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5264 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5265 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5267 return XFS_ERROR(EFSCORRUPTED);
5269 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5270 return XFS_ERROR(EIO);
5271 XFS_STATS_INC(xs_blk_mapr);
5272 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5273 (error = xfs_iread_extents(tp, ip, whichfork)))
5275 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5278 * Reading past eof, act as though there's a hole
5281 if (eof || got.br_startoff > bno) {
5285 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
5286 ASSERT(bno < got.br_startoff + got.br_blockcount);
5287 *fsb = got.br_startblock + (bno - got.br_startoff);
5288 ifp->if_lastex = lastx;
5293 * Unmap (remove) blocks from a file.
5294 * If nexts is nonzero then the number of extents to remove is limited to
5295 * that value. If not all extents in the block range can be removed then
5300 xfs_trans_t *tp, /* transaction pointer */
5301 struct xfs_inode *ip, /* incore inode */
5302 xfs_fileoff_t bno, /* starting offset to unmap */
5303 xfs_filblks_t len, /* length to unmap in file */
5304 int flags, /* misc flags */
5305 xfs_extnum_t nexts, /* number of extents max */
5306 xfs_fsblock_t *firstblock, /* first allocated block
5307 controls a.g. for allocs */
5308 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5309 int *done) /* set if not done yet */
5311 xfs_btree_cur_t *cur; /* bmap btree cursor */
5312 xfs_bmbt_irec_t del; /* extent being deleted */
5313 int eof; /* is deleting at eof */
5314 xfs_bmbt_rec_t *ep; /* extent list entry pointer */
5315 int error; /* error return value */
5316 xfs_extnum_t extno; /* extent number in list */
5317 xfs_bmbt_irec_t got; /* current extent list entry */
5318 xfs_ifork_t *ifp; /* inode fork pointer */
5319 int isrt; /* freeing in rt area */
5320 xfs_extnum_t lastx; /* last extent index used */
5321 int logflags; /* transaction logging flags */
5322 xfs_extlen_t mod; /* rt extent offset */
5323 xfs_mount_t *mp; /* mount structure */
5324 xfs_extnum_t nextents; /* size of extent list */
5325 xfs_bmbt_irec_t prev; /* previous extent list entry */
5326 xfs_fileoff_t start; /* first file offset deleted */
5327 int tmp_logflags; /* partial logging flags */
5328 int wasdel; /* was a delayed alloc extent */
5329 int whichfork; /* data or attribute fork */
5330 int rsvd; /* OK to allocate reserved blocks */
5333 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5334 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5335 XFS_ATTR_FORK : XFS_DATA_FORK;
5336 ifp = XFS_IFORK_PTR(ip, whichfork);
5338 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5339 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5340 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5342 return XFS_ERROR(EFSCORRUPTED);
5345 if (XFS_FORCED_SHUTDOWN(mp))
5346 return XFS_ERROR(EIO);
5347 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5350 ASSERT(ifp->if_ext_max ==
5351 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5352 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5353 (error = xfs_iread_extents(tp, ip, whichfork)))
5355 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5356 if (nextents == 0) {
5360 XFS_STATS_INC(xs_blk_unmap);
5361 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5363 bno = start + len - 1;
5364 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5367 * Check to see if the given block number is past the end of the
5368 * file, back up to the last block if so...
5371 ep = &ifp->if_u1.if_extents[--lastx];
5372 xfs_bmbt_get_all(ep, &got);
5373 bno = got.br_startoff + got.br_blockcount - 1;
5376 if (ifp->if_flags & XFS_IFBROOT) {
5377 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5378 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
5380 cur->bc_private.b.firstblock = *firstblock;
5381 cur->bc_private.b.flist = flist;
5382 cur->bc_private.b.flags = 0;
5386 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5387 (nexts == 0 || extno < nexts)) {
5389 * Is the found extent after a hole in which bno lives?
5390 * Just back up to the previous extent, if so.
5392 if (got.br_startoff > bno) {
5396 xfs_bmbt_get_all(ep, &got);
5399 * Is the last block of this extent before the range
5400 * we're supposed to delete? If so, we're done.
5402 bno = XFS_FILEOFF_MIN(bno,
5403 got.br_startoff + got.br_blockcount - 1);
5407 * Then deal with the (possibly delayed) allocated space
5412 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
5413 if (got.br_startoff < start) {
5414 del.br_startoff = start;
5415 del.br_blockcount -= start - got.br_startoff;
5417 del.br_startblock += start - got.br_startoff;
5419 if (del.br_startoff + del.br_blockcount > bno + 1)
5420 del.br_blockcount = bno + 1 - del.br_startoff;
5421 sum = del.br_startblock + del.br_blockcount;
5423 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5425 * Realtime extent not lined up at the end.
5426 * The extent could have been split into written
5427 * and unwritten pieces, or we could just be
5428 * unmapping part of it. But we can't really
5429 * get rid of part of a realtime extent.
5431 if (del.br_state == XFS_EXT_UNWRITTEN ||
5432 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5434 * This piece is unwritten, or we're not
5435 * using unwritten extents. Skip over it.
5438 bno -= mod > del.br_blockcount ?
5439 del.br_blockcount : mod;
5440 if (bno < got.br_startoff) {
5442 xfs_bmbt_get_all(--ep, &got);
5447 * It's written, turn it unwritten.
5448 * This is better than zeroing it.
5450 ASSERT(del.br_state == XFS_EXT_NORM);
5451 ASSERT(xfs_trans_get_block_res(tp) > 0);
5453 * If this spans a realtime extent boundary,
5454 * chop it back to the start of the one we end at.
5456 if (del.br_blockcount > mod) {
5457 del.br_startoff += del.br_blockcount - mod;
5458 del.br_startblock += del.br_blockcount - mod;
5459 del.br_blockcount = mod;
5461 del.br_state = XFS_EXT_UNWRITTEN;
5462 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5463 firstblock, flist, &logflags, XFS_DATA_FORK, 0);
5468 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5470 * Realtime extent is lined up at the end but not
5471 * at the front. We'll get rid of full extents if
5474 mod = mp->m_sb.sb_rextsize - mod;
5475 if (del.br_blockcount > mod) {
5476 del.br_blockcount -= mod;
5477 del.br_startoff += mod;
5478 del.br_startblock += mod;
5479 } else if ((del.br_startoff == start &&
5480 (del.br_state == XFS_EXT_UNWRITTEN ||
5481 xfs_trans_get_block_res(tp) == 0)) ||
5482 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5484 * Can't make it unwritten. There isn't
5485 * a full extent here so just skip it.
5487 ASSERT(bno >= del.br_blockcount);
5488 bno -= del.br_blockcount;
5489 if (bno < got.br_startoff) {
5491 xfs_bmbt_get_all(--ep, &got);
5494 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5496 * This one is already unwritten.
5497 * It must have a written left neighbor.
5498 * Unwrite the killed part of that one and
5502 xfs_bmbt_get_all(ep - 1, &prev);
5503 ASSERT(prev.br_state == XFS_EXT_NORM);
5504 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
5505 ASSERT(del.br_startblock ==
5506 prev.br_startblock + prev.br_blockcount);
5507 if (prev.br_startoff < start) {
5508 mod = start - prev.br_startoff;
5509 prev.br_blockcount -= mod;
5510 prev.br_startblock += mod;
5511 prev.br_startoff = start;
5513 prev.br_state = XFS_EXT_UNWRITTEN;
5514 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5515 &prev, firstblock, flist, &logflags,
5521 ASSERT(del.br_state == XFS_EXT_NORM);
5522 del.br_state = XFS_EXT_UNWRITTEN;
5523 error = xfs_bmap_add_extent(ip, lastx, &cur,
5524 &del, firstblock, flist, &logflags,
5532 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
5533 /* Update realtime/data freespace, unreserve quota */
5535 xfs_filblks_t rtexts;
5537 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5538 do_div(rtexts, mp->m_sb.sb_rextsize);
5539 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5541 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5542 NULL, ip, -((long)del.br_blockcount), 0,
5543 XFS_QMOPT_RES_RTBLKS);
5545 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
5546 (int)del.br_blockcount, rsvd);
5547 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5548 NULL, ip, -((long)del.br_blockcount), 0,
5549 XFS_QMOPT_RES_REGBLKS);
5551 ip->i_delayed_blks -= del.br_blockcount;
5553 cur->bc_private.b.flags |=
5554 XFS_BTCUR_BPRV_WASDEL;
5556 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5558 * If it's the case where the directory code is running
5559 * with no block reservation, and the deleted block is in
5560 * the middle of its extent, and the resulting insert
5561 * of an extent would cause transformation to btree format,
5562 * then reject it. The calling code will then swap
5563 * blocks around instead.
5564 * We have to do this now, rather than waiting for the
5565 * conversion to btree format, since the transaction
5568 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5569 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5570 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5571 del.br_startoff > got.br_startoff &&
5572 del.br_startoff + del.br_blockcount <
5573 got.br_startoff + got.br_blockcount) {
5574 error = XFS_ERROR(ENOSPC);
5577 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5578 &tmp_logflags, whichfork, rsvd);
5579 logflags |= tmp_logflags;
5582 bno = del.br_startoff - 1;
5584 lastx = ifp->if_lastex;
5586 * If not done go on to the next (previous) record.
5587 * Reset ep in case the extents array was re-alloced.
5589 ep = &ifp->if_u1.if_extents[lastx];
5590 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5591 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5592 xfs_bmbt_get_startoff(ep) > bno) {
5597 xfs_bmbt_get_all(ep, &got);
5601 ifp->if_lastex = lastx;
5602 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5603 ASSERT(ifp->if_ext_max ==
5604 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5606 * Convert to a btree if necessary.
5608 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5609 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5610 ASSERT(cur == NULL);
5611 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5612 &cur, 0, &tmp_logflags, whichfork);
5613 logflags |= tmp_logflags;
5618 * transform from btree to extents, give it cur
5620 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5621 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5622 ASSERT(cur != NULL);
5623 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5625 logflags |= tmp_logflags;
5630 * transform from extents to local?
5632 ASSERT(ifp->if_ext_max ==
5633 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5637 * Log everything. Do this after conversion, there's no point in
5638 * logging the extent list if we've converted to btree format.
5640 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5641 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5642 logflags &= ~XFS_ILOG_FEXT(whichfork);
5643 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5644 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5645 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5647 * Log inode even in the error case, if the transaction
5648 * is dirty we'll need to shut down the filesystem.
5651 xfs_trans_log_inode(tp, ip, logflags);
5654 *firstblock = cur->bc_private.b.firstblock;
5655 cur->bc_private.b.allocated = 0;
5657 xfs_btree_del_cursor(cur,
5658 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5664 * Fcntl interface to xfs_bmapi.
5666 int /* error code */
5668 bhv_desc_t *bdp, /* XFS behavior descriptor*/
5669 struct getbmap *bmv, /* user bmap structure */
5670 void __user *ap, /* pointer to user's array */
5671 int interface) /* interface flags */
5673 __int64_t bmvend; /* last block requested */
5674 int error; /* return value */
5675 __int64_t fixlen; /* length for -1 case */
5676 int i; /* extent number */
5677 xfs_inode_t *ip; /* xfs incore inode pointer */
5678 vnode_t *vp; /* corresponding vnode */
5679 int lock; /* lock state */
5680 xfs_bmbt_irec_t *map; /* buffer for user's data */
5681 xfs_mount_t *mp; /* file system mount point */
5682 int nex; /* # of user extents can do */
5683 int nexleft; /* # of user extents left */
5684 int subnex; /* # of bmapi's can do */
5685 int nmap; /* number of map entries */
5686 struct getbmap out; /* output structure */
5687 int whichfork; /* data or attr fork */
5688 int prealloced; /* this is a file with
5689 * preallocated data space */
5690 int sh_unwritten; /* true, if unwritten */
5691 /* extents listed separately */
5692 int bmapi_flags; /* flags for xfs_bmapi */
5693 __int32_t oflags; /* getbmapx bmv_oflags field */
5695 vp = BHV_TO_VNODE(bdp);
5696 ip = XFS_BHVTOI(bdp);
5699 whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5700 sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
5702 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5703 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5704 * bit is set for the file, generate a read event in order
5705 * that the DMAPI application may do its thing before we return
5706 * the extents. Usually this means restoring user file data to
5707 * regions of the file that look like holes.
5709 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5710 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5711 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5712 * could misinterpret holes in a DMAPI file as true holes,
5713 * when in fact they may represent offline user data.
5715 if ( (interface & BMV_IF_NO_DMAPI_READ) == 0
5716 && DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)
5717 && whichfork == XFS_DATA_FORK) {
5719 error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, 0, 0, 0, NULL);
5721 return XFS_ERROR(error);
5724 if (whichfork == XFS_ATTR_FORK) {
5725 if (XFS_IFORK_Q(ip)) {
5726 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5727 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5728 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5729 return XFS_ERROR(EINVAL);
5730 } else if (unlikely(
5731 ip->i_d.di_aformat != 0 &&
5732 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5733 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5735 return XFS_ERROR(EFSCORRUPTED);
5737 } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5738 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5739 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5740 return XFS_ERROR(EINVAL);
5741 if (whichfork == XFS_DATA_FORK) {
5742 if ((ip->i_d.di_extsize && (ip->i_d.di_flags &
5743 (XFS_DIFLAG_REALTIME|XFS_DIFLAG_EXTSIZE))) ||
5744 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5746 fixlen = XFS_MAXIOFFSET(mp);
5749 fixlen = ip->i_d.di_size;
5756 if (bmv->bmv_length == -1) {
5757 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5758 bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
5760 } else if (bmv->bmv_length < 0)
5761 return XFS_ERROR(EINVAL);
5762 if (bmv->bmv_length == 0) {
5763 bmv->bmv_entries = 0;
5766 nex = bmv->bmv_count - 1;
5768 return XFS_ERROR(EINVAL);
5769 bmvend = bmv->bmv_offset + bmv->bmv_length;
5771 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5773 if (whichfork == XFS_DATA_FORK && ip->i_delayed_blks) {
5774 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
5775 VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
5778 ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
5780 lock = xfs_ilock_map_shared(ip);
5783 * Don't let nex be bigger than the number of extents
5784 * we can have assuming alternating holes and real extents.
5786 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5787 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5789 bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
5790 ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
5793 * Allocate enough space to handle "subnex" maps at a time.
5796 map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
5798 bmv->bmv_entries = 0;
5800 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
5802 goto unlock_and_return;
5808 nmap = (nexleft > subnex) ? subnex : nexleft;
5809 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5810 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5811 bmapi_flags, NULL, 0, map, &nmap, NULL);
5813 goto unlock_and_return;
5814 ASSERT(nmap <= subnex);
5816 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5818 oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
5819 BMV_OF_PREALLOC : 0;
5820 out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
5821 out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5822 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
5823 if (map[i].br_startblock == HOLESTARTBLOCK &&
5824 ((prealloced && out.bmv_offset + out.bmv_length == bmvend) ||
5825 whichfork == XFS_ATTR_FORK )) {
5827 * came to hole at end of file or the end of
5830 goto unlock_and_return;
5833 (map[i].br_startblock == HOLESTARTBLOCK) ?
5835 XFS_FSB_TO_DB(ip, map[i].br_startblock);
5837 /* return either getbmap/getbmapx structure. */
5838 if (interface & BMV_IF_EXTENDED) {
5839 struct getbmapx outx;
5841 GETBMAP_CONVERT(out,outx);
5842 outx.bmv_oflags = oflags;
5843 outx.bmv_unused1 = outx.bmv_unused2 = 0;
5844 if (copy_to_user(ap, &outx,
5846 error = XFS_ERROR(EFAULT);
5847 goto unlock_and_return;
5850 if (copy_to_user(ap, &out,
5852 error = XFS_ERROR(EFAULT);
5853 goto unlock_and_return;
5857 out.bmv_offset + out.bmv_length;
5858 bmv->bmv_length = MAX((__int64_t)0,
5859 (__int64_t)(bmvend - bmv->bmv_offset));
5861 ap = (interface & BMV_IF_EXTENDED) ?
5863 ((struct getbmapx __user *)ap + 1) :
5865 ((struct getbmap __user *)ap + 1);
5868 } while (nmap && nexleft && bmv->bmv_length);
5871 xfs_iunlock_map_shared(ip, lock);
5872 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5874 kmem_free(map, subnex * sizeof(*map));
5880 * Check the last inode extent to determine whether this allocation will result
5881 * in blocks being allocated at the end of the file. When we allocate new data
5882 * blocks at the end of the file which do not start at the previous data block,
5883 * we will try to align the new blocks at stripe unit boundaries.
5885 STATIC int /* error */
5887 xfs_inode_t *ip, /* incore inode pointer */
5888 xfs_fileoff_t off, /* file offset in fsblocks */
5889 int whichfork, /* data or attribute fork */
5890 char *aeof) /* return value */
5892 int error; /* error return value */
5893 xfs_ifork_t *ifp; /* inode fork pointer */
5894 xfs_bmbt_rec_t *lastrec; /* extent list entry pointer */
5895 xfs_extnum_t nextents; /* size of extent list */
5896 xfs_bmbt_irec_t s; /* expanded extent list entry */
5898 ASSERT(whichfork == XFS_DATA_FORK);
5899 ifp = XFS_IFORK_PTR(ip, whichfork);
5900 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5901 (error = xfs_iread_extents(NULL, ip, whichfork)))
5903 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5904 if (nextents == 0) {
5909 * Go to the last extent
5911 lastrec = &ifp->if_u1.if_extents[nextents - 1];
5912 xfs_bmbt_get_all(lastrec, &s);
5914 * Check we are allocating in the last extent (for delayed allocations)
5915 * or past the last extent for non-delayed allocations.
5917 *aeof = (off >= s.br_startoff &&
5918 off < s.br_startoff + s.br_blockcount &&
5919 ISNULLSTARTBLOCK(s.br_startblock)) ||
5920 off >= s.br_startoff + s.br_blockcount;
5925 * Check if the endoff is outside the last extent. If so the caller will grow
5926 * the allocation to a stripe unit boundary.
5930 xfs_inode_t *ip, /* incore inode pointer */
5931 xfs_fileoff_t endoff, /* file offset in fsblocks */
5932 int whichfork, /* data or attribute fork */
5933 int *eof) /* result value */
5935 xfs_fsblock_t blockcount; /* extent block count */
5936 int error; /* error return value */
5937 xfs_ifork_t *ifp; /* inode fork pointer */
5938 xfs_bmbt_rec_t *lastrec; /* extent list entry pointer */
5939 xfs_extnum_t nextents; /* size of extent list */
5940 xfs_fileoff_t startoff; /* extent starting file offset */
5942 ASSERT(whichfork == XFS_DATA_FORK);
5943 ifp = XFS_IFORK_PTR(ip, whichfork);
5944 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5945 (error = xfs_iread_extents(NULL, ip, whichfork)))
5947 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5948 if (nextents == 0) {
5953 * Go to the last extent
5955 lastrec = &ifp->if_u1.if_extents[nextents - 1];
5956 startoff = xfs_bmbt_get_startoff(lastrec);
5957 blockcount = xfs_bmbt_get_blockcount(lastrec);
5958 *eof = endoff >= startoff + blockcount;
5964 * Check that the extents list for the inode ip is in the right order.
5967 xfs_bmap_check_extents(
5968 xfs_inode_t *ip, /* incore inode pointer */
5969 int whichfork) /* data or attr fork */
5971 xfs_bmbt_rec_t *base; /* base of extents list */
5972 xfs_bmbt_rec_t *ep; /* current extent entry */
5973 xfs_ifork_t *ifp; /* inode fork pointer */
5974 xfs_extnum_t nextents; /* number of extents in list */
5976 ifp = XFS_IFORK_PTR(ip, whichfork);
5977 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
5978 base = ifp->if_u1.if_extents;
5979 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5980 for (ep = base; ep < &base[nextents - 1]; ep++) {
5981 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
5989 xfs_btree_cur_t *cur,
5999 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6000 bp = cur->bc_bufs[i];
6002 if (XFS_BUF_ADDR(bp) == bno)
6003 break; /* Found it */
6005 if (i == XFS_BTREE_MAXLEVELS)
6008 if (!bp) { /* Chase down all the log items to see if the bp is there */
6009 xfs_log_item_chunk_t *licp;
6013 licp = &tp->t_items;
6014 while (!bp && licp != NULL) {
6015 if (XFS_LIC_ARE_ALL_FREE(licp)) {
6016 licp = licp->lic_next;
6019 for (i = 0; i < licp->lic_unused; i++) {
6020 xfs_log_item_desc_t *lidp;
6021 xfs_log_item_t *lip;
6022 xfs_buf_log_item_t *bip;
6025 if (XFS_LIC_ISFREE(licp, i)) {
6029 lidp = XFS_LIC_SLOT(licp, i);
6030 lip = lidp->lid_item;
6031 if (lip->li_type != XFS_LI_BUF)
6034 bip = (xfs_buf_log_item_t *)lip;
6037 if (XFS_BUF_ADDR(lbp) == bno) {
6039 break; /* Found it */
6042 licp = licp->lic_next;
6050 xfs_bmbt_block_t *block,
6056 xfs_bmbt_ptr_t *pp, *thispa; /* pointer to block address */
6057 xfs_bmbt_key_t *prevp, *keyp;
6059 ASSERT(be16_to_cpu(block->bb_level) > 0);
6062 for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
6063 dmxr = mp->m_bmap_dmxr[0];
6066 keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
6068 keyp = XFS_BTREE_KEY_ADDR(mp->m_sb.sb_blocksize,
6069 xfs_bmbt, block, i, dmxr);
6073 xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
6078 * Compare the block numbers to see if there are dups.
6082 pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
6084 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6085 xfs_bmbt, block, i, dmxr);
6087 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
6089 thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
6091 thispa = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6092 xfs_bmbt, block, j, dmxr);
6094 if (INT_GET(*thispa, ARCH_CONVERT) ==
6095 INT_GET(*pp, ARCH_CONVERT)) {
6096 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
6098 INT_GET(*thispa, ARCH_CONVERT));
6099 panic("%s: ptrs are equal in node\n",
6107 * Check that the extents for the inode ip are in the right order in all
6112 xfs_bmap_check_leaf_extents(
6113 xfs_btree_cur_t *cur, /* btree cursor or null */
6114 xfs_inode_t *ip, /* incore inode pointer */
6115 int whichfork) /* data or attr fork */
6117 xfs_bmbt_block_t *block; /* current btree block */
6118 xfs_fsblock_t bno; /* block # of "block" */
6119 xfs_buf_t *bp; /* buffer for "block" */
6120 int error; /* error return value */
6121 xfs_extnum_t i=0; /* index into the extents list */
6122 xfs_ifork_t *ifp; /* fork structure */
6123 int level; /* btree level, for checking */
6124 xfs_mount_t *mp; /* file system mount structure */
6125 xfs_bmbt_ptr_t *pp; /* pointer to block address */
6126 xfs_bmbt_rec_t *ep, *lastp; /* extent pointers in block entry */
6129 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6135 ifp = XFS_IFORK_PTR(ip, whichfork);
6136 block = ifp->if_broot;
6138 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6140 level = be16_to_cpu(block->bb_level);
6142 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6143 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6144 ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
6145 ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
6146 ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
6147 bno = INT_GET(*pp, ARCH_CONVERT);
6149 * Go down the tree until leaf level is reached, following the first
6150 * pointer (leftmost) at each level.
6152 while (level-- > 0) {
6153 /* See if buf is in cur first */
6154 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6160 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6161 XFS_BMAP_BTREE_REF)))
6163 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6164 XFS_WANT_CORRUPTED_GOTO(
6165 XFS_BMAP_SANITY_CHECK(mp, block, level),
6171 * Check this block for basic sanity (increasing keys and
6172 * no duplicate blocks).
6175 xfs_check_block(block, mp, 0, 0);
6176 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
6177 1, mp->m_bmap_dmxr[1]);
6178 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, INT_GET(*pp, ARCH_CONVERT)), error0);
6179 bno = INT_GET(*pp, ARCH_CONVERT);
6182 xfs_trans_brelse(NULL, bp);
6187 * Here with bp and block set to the leftmost leaf node in the tree.
6192 * Loop over all leaf nodes checking that all extents are in the right order.
6196 xfs_bmbt_rec_t *frp;
6197 xfs_fsblock_t nextbno;
6198 xfs_extnum_t num_recs;
6201 num_recs = be16_to_cpu(block->bb_numrecs);
6204 * Read-ahead the next leaf block, if any.
6207 nextbno = be64_to_cpu(block->bb_rightsib);
6210 * Check all the extents to make sure they are OK.
6211 * If we had a previous block, the last entry should
6212 * conform with the first entry in this one.
6215 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
6216 block, 1, mp->m_bmap_dmxr[0]);
6218 for (ep = frp;ep < frp + (num_recs - 1); ep++) {
6220 xfs_btree_check_rec(XFS_BTNUM_BMAP,
6221 (void *)lastp, (void *)ep);
6223 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
6226 lastp = frp + num_recs - 1; /* For the next iteration */
6231 xfs_trans_brelse(NULL, bp);
6235 * If we've reached the end, stop.
6237 if (bno == NULLFSBLOCK)
6240 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6246 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6247 XFS_BMAP_BTREE_REF)))
6249 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6253 xfs_trans_brelse(NULL, bp);
6258 cmn_err(CE_WARN, "%s: at error0", __FUNCTION__);
6260 xfs_trans_brelse(NULL, bp);
6262 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
6264 panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__);
6270 * Count fsblocks of the given fork.
6273 xfs_bmap_count_blocks(
6274 xfs_trans_t *tp, /* transaction pointer */
6275 xfs_inode_t *ip, /* incore inode */
6276 int whichfork, /* data or attr fork */
6277 int *count) /* out: count of blocks */
6279 xfs_bmbt_block_t *block; /* current btree block */
6280 xfs_fsblock_t bno; /* block # of "block" */
6281 xfs_ifork_t *ifp; /* fork structure */
6282 int level; /* btree level, for checking */
6283 xfs_mount_t *mp; /* file system mount structure */
6284 xfs_bmbt_ptr_t *pp; /* pointer to block address */
6288 ifp = XFS_IFORK_PTR(ip, whichfork);
6289 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6290 if (unlikely(xfs_bmap_count_leaves(ifp->if_u1.if_extents,
6291 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6293 XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)",
6294 XFS_ERRLEVEL_LOW, mp);
6295 return XFS_ERROR(EFSCORRUPTED);
6301 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6303 block = ifp->if_broot;
6304 level = be16_to_cpu(block->bb_level);
6306 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6307 ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
6308 ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
6309 ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
6310 bno = INT_GET(*pp, ARCH_CONVERT);
6312 if (unlikely(xfs_bmap_count_tree(mp, tp, bno, level, count) < 0)) {
6313 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6315 return XFS_ERROR(EFSCORRUPTED);
6322 * Recursively walks each level of a btree
6323 * to count total fsblocks is use.
6326 xfs_bmap_count_tree(
6327 xfs_mount_t *mp, /* file system mount point */
6328 xfs_trans_t *tp, /* transaction pointer */
6329 xfs_fsblock_t blockno, /* file system block number */
6330 int levelin, /* level in btree */
6331 int *count) /* Count of blocks */
6334 xfs_buf_t *bp, *nbp;
6335 int level = levelin;
6337 xfs_fsblock_t bno = blockno;
6338 xfs_fsblock_t nextbno;
6339 xfs_bmbt_block_t *block, *nextblock;
6341 xfs_bmbt_rec_t *frp;
6343 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6346 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6349 /* Not at node above leafs, count this level of nodes */
6350 nextbno = be64_to_cpu(block->bb_rightsib);
6351 while (nextbno != NULLFSBLOCK) {
6352 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6353 0, &nbp, XFS_BMAP_BTREE_REF)))
6356 nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
6357 nextbno = be64_to_cpu(nextblock->bb_rightsib);
6358 xfs_trans_brelse(tp, nbp);
6361 /* Dive to the next level */
6362 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6363 xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
6364 bno = INT_GET(*pp, ARCH_CONVERT);
6365 if (unlikely((error =
6366 xfs_bmap_count_tree(mp, tp, bno, level, count)) < 0)) {
6367 xfs_trans_brelse(tp, bp);
6368 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6369 XFS_ERRLEVEL_LOW, mp);
6370 return XFS_ERROR(EFSCORRUPTED);
6372 xfs_trans_brelse(tp, bp);
6374 /* count all level 1 nodes and their leaves */
6376 nextbno = be64_to_cpu(block->bb_rightsib);
6377 numrecs = be16_to_cpu(block->bb_numrecs);
6378 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize,
6379 xfs_bmbt, block, 1, mp->m_bmap_dmxr[0]);
6380 if (unlikely(xfs_bmap_disk_count_leaves(frp, numrecs, count) < 0)) {
6381 xfs_trans_brelse(tp, bp);
6382 XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
6383 XFS_ERRLEVEL_LOW, mp);
6384 return XFS_ERROR(EFSCORRUPTED);
6386 xfs_trans_brelse(tp, bp);
6387 if (nextbno == NULLFSBLOCK)
6390 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6391 XFS_BMAP_BTREE_REF)))
6394 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6401 * Count leaf blocks given a pointer to an extent list.
6404 xfs_bmap_count_leaves(
6405 xfs_bmbt_rec_t *frp,
6411 for ( b = 1; b <= numrecs; b++, frp++)
6412 *count += xfs_bmbt_get_blockcount(frp);
6417 * Count leaf blocks given a pointer to an extent list originally in btree format.
6420 xfs_bmap_disk_count_leaves(
6421 xfs_bmbt_rec_t *frp,
6427 for ( b = 1; b <= numrecs; b++, frp++)
6428 *count += xfs_bmbt_disk_get_blockcount(frp);