2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_dir2_sf.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dinode.h"
35 #include "xfs_inode.h"
36 #include "xfs_btree.h"
37 #include "xfs_dmapi.h"
38 #include "xfs_mount.h"
39 #include "xfs_ialloc.h"
40 #include "xfs_itable.h"
41 #include "xfs_dir2_data.h"
42 #include "xfs_dir2_leaf.h"
43 #include "xfs_dir2_block.h"
44 #include "xfs_inode_item.h"
45 #include "xfs_extfree_item.h"
46 #include "xfs_alloc.h"
48 #include "xfs_rtalloc.h"
49 #include "xfs_error.h"
50 #include "xfs_attr_leaf.h"
52 #include "xfs_quota.h"
53 #include "xfs_trans_space.h"
54 #include "xfs_buf_item.h"
55 #include "xfs_filestream.h"
60 xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
63 kmem_zone_t *xfs_bmap_free_item_zone;
66 * Prototypes for internal bmap routines.
71 * Called from xfs_bmap_add_attrfork to handle extents format files.
73 STATIC int /* error */
74 xfs_bmap_add_attrfork_extents(
75 xfs_trans_t *tp, /* transaction pointer */
76 xfs_inode_t *ip, /* incore inode pointer */
77 xfs_fsblock_t *firstblock, /* first block allocated */
78 xfs_bmap_free_t *flist, /* blocks to free at commit */
79 int *flags); /* inode logging flags */
82 * Called from xfs_bmap_add_attrfork to handle local format files.
84 STATIC int /* error */
85 xfs_bmap_add_attrfork_local(
86 xfs_trans_t *tp, /* transaction pointer */
87 xfs_inode_t *ip, /* incore inode pointer */
88 xfs_fsblock_t *firstblock, /* first block allocated */
89 xfs_bmap_free_t *flist, /* blocks to free at commit */
90 int *flags); /* inode logging flags */
93 * Called by xfs_bmapi to update file extent records and the btree
94 * after allocating space (or doing a delayed allocation).
96 STATIC int /* error */
98 xfs_inode_t *ip, /* incore inode pointer */
99 xfs_extnum_t idx, /* extent number to update/insert */
100 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
101 xfs_bmbt_irec_t *new, /* new data to add to file extents */
102 xfs_fsblock_t *first, /* pointer to firstblock variable */
103 xfs_bmap_free_t *flist, /* list of extents to be freed */
104 int *logflagsp, /* inode logging flags */
105 xfs_extdelta_t *delta, /* Change made to incore extents */
106 int whichfork, /* data or attr fork */
107 int rsvd); /* OK to allocate reserved blocks */
110 * Called by xfs_bmap_add_extent to handle cases converting a delayed
111 * allocation to a real allocation.
113 STATIC int /* error */
114 xfs_bmap_add_extent_delay_real(
115 xfs_inode_t *ip, /* incore inode pointer */
116 xfs_extnum_t idx, /* extent number to update/insert */
117 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
118 xfs_bmbt_irec_t *new, /* new data to add to file extents */
119 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
120 xfs_fsblock_t *first, /* pointer to firstblock variable */
121 xfs_bmap_free_t *flist, /* list of extents to be freed */
122 int *logflagsp, /* inode logging flags */
123 xfs_extdelta_t *delta, /* Change made to incore extents */
124 int rsvd); /* OK to allocate reserved blocks */
127 * Called by xfs_bmap_add_extent to handle cases converting a hole
128 * to a delayed allocation.
130 STATIC int /* error */
131 xfs_bmap_add_extent_hole_delay(
132 xfs_inode_t *ip, /* incore inode pointer */
133 xfs_extnum_t idx, /* extent number to update/insert */
134 xfs_bmbt_irec_t *new, /* new data to add to file extents */
135 int *logflagsp,/* inode logging flags */
136 xfs_extdelta_t *delta, /* Change made to incore extents */
137 int rsvd); /* OK to allocate reserved blocks */
140 * Called by xfs_bmap_add_extent to handle cases converting a hole
141 * to a real allocation.
143 STATIC int /* error */
144 xfs_bmap_add_extent_hole_real(
145 xfs_inode_t *ip, /* incore inode pointer */
146 xfs_extnum_t idx, /* extent number to update/insert */
147 xfs_btree_cur_t *cur, /* if null, not a btree */
148 xfs_bmbt_irec_t *new, /* new data to add to file extents */
149 int *logflagsp, /* inode logging flags */
150 xfs_extdelta_t *delta, /* Change made to incore extents */
151 int whichfork); /* data or attr fork */
154 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
155 * allocation to a real allocation or vice versa.
157 STATIC int /* error */
158 xfs_bmap_add_extent_unwritten_real(
159 xfs_inode_t *ip, /* incore inode pointer */
160 xfs_extnum_t idx, /* extent number to update/insert */
161 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
162 xfs_bmbt_irec_t *new, /* new data to add to file extents */
163 int *logflagsp, /* inode logging flags */
164 xfs_extdelta_t *delta); /* Change made to incore extents */
167 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
168 * It figures out where to ask the underlying allocator to put the new extent.
170 STATIC int /* error */
172 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
175 * Transform a btree format file with only one leaf node, where the
176 * extents list will fit in the inode, into an extents format file.
177 * Since the file extents are already in-core, all we have to do is
178 * give up the space for the btree root and pitch the leaf block.
180 STATIC int /* error */
181 xfs_bmap_btree_to_extents(
182 xfs_trans_t *tp, /* transaction pointer */
183 xfs_inode_t *ip, /* incore inode pointer */
184 xfs_btree_cur_t *cur, /* btree cursor */
185 int *logflagsp, /* inode logging flags */
186 int whichfork); /* data or attr fork */
189 * Called by xfs_bmapi to update file extent records and the btree
190 * after removing space (or undoing a delayed allocation).
192 STATIC int /* error */
194 xfs_inode_t *ip, /* incore inode pointer */
195 xfs_trans_t *tp, /* current trans pointer */
196 xfs_extnum_t idx, /* extent number to update/insert */
197 xfs_bmap_free_t *flist, /* list of extents to be freed */
198 xfs_btree_cur_t *cur, /* if null, not a btree */
199 xfs_bmbt_irec_t *new, /* new data to add to file extents */
200 int *logflagsp,/* inode logging flags */
201 xfs_extdelta_t *delta, /* Change made to incore extents */
202 int whichfork, /* data or attr fork */
203 int rsvd); /* OK to allocate reserved blocks */
206 * Remove the entry "free" from the free item list. Prev points to the
207 * previous entry, unless "free" is the head of the list.
211 xfs_bmap_free_t *flist, /* free item list header */
212 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
213 xfs_bmap_free_item_t *free); /* list item to be freed */
216 * Convert an extents-format file into a btree-format file.
217 * The new file will have a root block (in the inode) and a single child block.
219 STATIC int /* error */
220 xfs_bmap_extents_to_btree(
221 xfs_trans_t *tp, /* transaction pointer */
222 xfs_inode_t *ip, /* incore inode pointer */
223 xfs_fsblock_t *firstblock, /* first-block-allocated */
224 xfs_bmap_free_t *flist, /* blocks freed in xaction */
225 xfs_btree_cur_t **curp, /* cursor returned to caller */
226 int wasdel, /* converting a delayed alloc */
227 int *logflagsp, /* inode logging flags */
228 int whichfork); /* data or attr fork */
231 * Convert a local file to an extents file.
232 * This code is sort of bogus, since the file data needs to get
233 * logged so it won't be lost. The bmap-level manipulations are ok, though.
235 STATIC int /* error */
236 xfs_bmap_local_to_extents(
237 xfs_trans_t *tp, /* transaction pointer */
238 xfs_inode_t *ip, /* incore inode pointer */
239 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
240 xfs_extlen_t total, /* total blocks needed by transaction */
241 int *logflagsp, /* inode logging flags */
242 int whichfork); /* data or attr fork */
245 * Search the extents list for the inode, for the extent containing bno.
246 * If bno lies in a hole, point to the next entry. If bno lies past eof,
247 * *eofp will be set, and *prevp will contain the last entry (null if none).
248 * Else, *lastxp will be set to the index of the found
249 * entry; *gotp will contain the entry.
251 STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
252 xfs_bmap_search_extents(
253 xfs_inode_t *ip, /* incore inode pointer */
254 xfs_fileoff_t bno, /* block number searched for */
255 int whichfork, /* data or attr fork */
256 int *eofp, /* out: end of file found */
257 xfs_extnum_t *lastxp, /* out: last extent index */
258 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
259 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
262 * Check the last inode extent to determine whether this allocation will result
263 * in blocks being allocated at the end of the file. When we allocate new data
264 * blocks at the end of the file which do not start at the previous data block,
265 * we will try to align the new blocks at stripe unit boundaries.
267 STATIC int /* error */
269 xfs_inode_t *ip, /* incore inode pointer */
270 xfs_fileoff_t off, /* file offset in fsblocks */
271 int whichfork, /* data or attribute fork */
272 char *aeof); /* return value */
274 #ifdef XFS_BMAP_TRACE
276 * Add a bmap trace buffer entry. Base routine for the others.
279 xfs_bmap_trace_addentry(
280 int opcode, /* operation */
281 char *fname, /* function name */
282 char *desc, /* operation description */
283 xfs_inode_t *ip, /* incore inode pointer */
284 xfs_extnum_t idx, /* index of entry(ies) */
285 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
286 xfs_bmbt_rec_t *r1, /* first record */
287 xfs_bmbt_rec_t *r2, /* second record or null */
288 int whichfork); /* data or attr fork */
291 * Add bmap trace entry prior to a call to xfs_iext_remove.
294 xfs_bmap_trace_delete(
295 char *fname, /* function name */
296 char *desc, /* operation description */
297 xfs_inode_t *ip, /* incore inode pointer */
298 xfs_extnum_t idx, /* index of entry(entries) deleted */
299 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
300 int whichfork); /* data or attr fork */
303 * Add bmap trace entry prior to a call to xfs_iext_insert, or
304 * reading in the extents list from the disk (in the btree).
307 xfs_bmap_trace_insert(
308 char *fname, /* function name */
309 char *desc, /* operation description */
310 xfs_inode_t *ip, /* incore inode pointer */
311 xfs_extnum_t idx, /* index of entry(entries) inserted */
312 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
313 xfs_bmbt_irec_t *r1, /* inserted record 1 */
314 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
315 int whichfork); /* data or attr fork */
318 * Add bmap trace entry after updating an extent record in place.
321 xfs_bmap_trace_post_update(
322 char *fname, /* function name */
323 char *desc, /* operation description */
324 xfs_inode_t *ip, /* incore inode pointer */
325 xfs_extnum_t idx, /* index of entry updated */
326 int whichfork); /* data or attr fork */
329 * Add bmap trace entry prior to updating an extent record in place.
332 xfs_bmap_trace_pre_update(
333 char *fname, /* function name */
334 char *desc, /* operation description */
335 xfs_inode_t *ip, /* incore inode pointer */
336 xfs_extnum_t idx, /* index of entry to be updated */
337 int whichfork); /* data or attr fork */
340 #define xfs_bmap_trace_delete(f,d,ip,i,c,w)
341 #define xfs_bmap_trace_insert(f,d,ip,i,c,r1,r2,w)
342 #define xfs_bmap_trace_post_update(f,d,ip,i,w)
343 #define xfs_bmap_trace_pre_update(f,d,ip,i,w)
344 #endif /* XFS_BMAP_TRACE */
347 * Compute the worst-case number of indirect blocks that will be used
348 * for ip's delayed extent of length "len".
351 xfs_bmap_worst_indlen(
352 xfs_inode_t *ip, /* incore inode pointer */
353 xfs_filblks_t len); /* delayed extent length */
357 * Perform various validation checks on the values being returned
361 xfs_bmap_validate_ret(
365 xfs_bmbt_irec_t *mval,
369 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
372 #if defined(XFS_RW_TRACE)
381 #define xfs_bunmap_trace(ip, bno, len, flags, ra)
382 #endif /* XFS_RW_TRACE */
389 xfs_fsblock_t blockno,
394 xfs_bmap_count_leaves(
401 xfs_bmap_disk_count_leaves(
403 xfs_bmbt_block_t *block,
408 * Bmap internal routines.
412 * Called from xfs_bmap_add_attrfork to handle btree format files.
414 STATIC int /* error */
415 xfs_bmap_add_attrfork_btree(
416 xfs_trans_t *tp, /* transaction pointer */
417 xfs_inode_t *ip, /* incore inode pointer */
418 xfs_fsblock_t *firstblock, /* first block allocated */
419 xfs_bmap_free_t *flist, /* blocks to free at commit */
420 int *flags) /* inode logging flags */
422 xfs_btree_cur_t *cur; /* btree cursor */
423 int error; /* error return value */
424 xfs_mount_t *mp; /* file system mount struct */
425 int stat; /* newroot status */
428 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
429 *flags |= XFS_ILOG_DBROOT;
431 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
433 cur->bc_private.b.flist = flist;
434 cur->bc_private.b.firstblock = *firstblock;
435 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
437 ASSERT(stat == 1); /* must be at least one entry */
438 if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
441 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
442 return XFS_ERROR(ENOSPC);
444 *firstblock = cur->bc_private.b.firstblock;
445 cur->bc_private.b.allocated = 0;
446 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
450 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
455 * Called from xfs_bmap_add_attrfork to handle extents format files.
457 STATIC int /* error */
458 xfs_bmap_add_attrfork_extents(
459 xfs_trans_t *tp, /* transaction pointer */
460 xfs_inode_t *ip, /* incore inode pointer */
461 xfs_fsblock_t *firstblock, /* first block allocated */
462 xfs_bmap_free_t *flist, /* blocks to free at commit */
463 int *flags) /* inode logging flags */
465 xfs_btree_cur_t *cur; /* bmap btree cursor */
466 int error; /* error return value */
468 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
471 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
472 flags, XFS_DATA_FORK);
474 cur->bc_private.b.allocated = 0;
475 xfs_btree_del_cursor(cur,
476 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
482 * Called from xfs_bmap_add_attrfork to handle local format files.
484 STATIC int /* error */
485 xfs_bmap_add_attrfork_local(
486 xfs_trans_t *tp, /* transaction pointer */
487 xfs_inode_t *ip, /* incore inode pointer */
488 xfs_fsblock_t *firstblock, /* first block allocated */
489 xfs_bmap_free_t *flist, /* blocks to free at commit */
490 int *flags) /* inode logging flags */
492 xfs_da_args_t dargs; /* args for dir/attr code */
493 int error; /* error return value */
494 xfs_mount_t *mp; /* mount structure pointer */
496 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
498 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
500 memset(&dargs, 0, sizeof(dargs));
502 dargs.firstblock = firstblock;
504 dargs.total = mp->m_dirblkfsbs;
505 dargs.whichfork = XFS_DATA_FORK;
507 error = xfs_dir2_sf_to_block(&dargs);
509 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
515 * Called by xfs_bmapi to update file extent records and the btree
516 * after allocating space (or doing a delayed allocation).
518 STATIC int /* error */
520 xfs_inode_t *ip, /* incore inode pointer */
521 xfs_extnum_t idx, /* extent number to update/insert */
522 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
523 xfs_bmbt_irec_t *new, /* new data to add to file extents */
524 xfs_fsblock_t *first, /* pointer to firstblock variable */
525 xfs_bmap_free_t *flist, /* list of extents to be freed */
526 int *logflagsp, /* inode logging flags */
527 xfs_extdelta_t *delta, /* Change made to incore extents */
528 int whichfork, /* data or attr fork */
529 int rsvd) /* OK to use reserved data blocks */
531 xfs_btree_cur_t *cur; /* btree cursor or null */
532 xfs_filblks_t da_new; /* new count del alloc blocks used */
533 xfs_filblks_t da_old; /* old count del alloc blocks used */
534 int error; /* error return value */
535 #ifdef XFS_BMAP_TRACE
536 static char fname[] = "xfs_bmap_add_extent";
538 xfs_ifork_t *ifp; /* inode fork ptr */
539 int logflags; /* returned value */
540 xfs_extnum_t nextents; /* number of extents in file now */
542 XFS_STATS_INC(xs_add_exlist);
544 ifp = XFS_IFORK_PTR(ip, whichfork);
545 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
546 ASSERT(idx <= nextents);
550 * This is the first extent added to a new/empty file.
551 * Special case this one, so other routines get to assume there are
552 * already extents in the list.
555 xfs_bmap_trace_insert(fname, "insert empty", ip, 0, 1, new,
557 xfs_iext_insert(ifp, 0, 1, new);
560 if (!ISNULLSTARTBLOCK(new->br_startblock)) {
561 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
562 logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
565 /* DELTA: single new extent */
567 if (delta->xed_startoff > new->br_startoff)
568 delta->xed_startoff = new->br_startoff;
569 if (delta->xed_blockcount <
570 new->br_startoff + new->br_blockcount)
571 delta->xed_blockcount = new->br_startoff +
576 * Any kind of new delayed allocation goes here.
578 else if (ISNULLSTARTBLOCK(new->br_startblock)) {
580 ASSERT((cur->bc_private.b.flags &
581 XFS_BTCUR_BPRV_WASDEL) == 0);
582 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
583 &logflags, delta, rsvd)))
587 * Real allocation off the end of the file.
589 else if (idx == nextents) {
591 ASSERT((cur->bc_private.b.flags &
592 XFS_BTCUR_BPRV_WASDEL) == 0);
593 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
594 &logflags, delta, whichfork)))
597 xfs_bmbt_irec_t prev; /* old extent at offset idx */
600 * Get the record referred to by idx.
602 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
604 * If it's a real allocation record, and the new allocation ends
605 * after the start of the referred to record, then we're filling
606 * in a delayed or unwritten allocation with a real one, or
607 * converting real back to unwritten.
609 if (!ISNULLSTARTBLOCK(new->br_startblock) &&
610 new->br_startoff + new->br_blockcount > prev.br_startoff) {
611 if (prev.br_state != XFS_EXT_UNWRITTEN &&
612 ISNULLSTARTBLOCK(prev.br_startblock)) {
613 da_old = STARTBLOCKVAL(prev.br_startblock);
615 ASSERT(cur->bc_private.b.flags &
616 XFS_BTCUR_BPRV_WASDEL);
617 if ((error = xfs_bmap_add_extent_delay_real(ip,
618 idx, &cur, new, &da_new, first, flist,
619 &logflags, delta, rsvd)))
621 } else if (new->br_state == XFS_EXT_NORM) {
622 ASSERT(new->br_state == XFS_EXT_NORM);
623 if ((error = xfs_bmap_add_extent_unwritten_real(
624 ip, idx, &cur, new, &logflags, delta)))
627 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
628 if ((error = xfs_bmap_add_extent_unwritten_real(
629 ip, idx, &cur, new, &logflags, delta)))
632 ASSERT(*curp == cur || *curp == NULL);
635 * Otherwise we're filling in a hole with an allocation.
639 ASSERT((cur->bc_private.b.flags &
640 XFS_BTCUR_BPRV_WASDEL) == 0);
641 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
642 new, &logflags, delta, whichfork)))
647 ASSERT(*curp == cur || *curp == NULL);
649 * Convert to a btree if necessary.
651 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
652 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
653 int tmp_logflags; /* partial log flag return val */
656 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
657 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
658 logflags |= tmp_logflags;
663 * Adjust for changes in reserved delayed indirect blocks.
664 * Nothing to do for disk quotas here.
666 if (da_old || da_new) {
671 nblks += cur->bc_private.b.allocated;
672 ASSERT(nblks <= da_old);
674 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
675 (int64_t)(da_old - nblks), rsvd);
678 * Clear out the allocated field, done with it now in any case.
681 cur->bc_private.b.allocated = 0;
687 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
689 *logflagsp = logflags;
694 * Called by xfs_bmap_add_extent to handle cases converting a delayed
695 * allocation to a real allocation.
697 STATIC int /* error */
698 xfs_bmap_add_extent_delay_real(
699 xfs_inode_t *ip, /* incore inode pointer */
700 xfs_extnum_t idx, /* extent number to update/insert */
701 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
702 xfs_bmbt_irec_t *new, /* new data to add to file extents */
703 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
704 xfs_fsblock_t *first, /* pointer to firstblock variable */
705 xfs_bmap_free_t *flist, /* list of extents to be freed */
706 int *logflagsp, /* inode logging flags */
707 xfs_extdelta_t *delta, /* Change made to incore extents */
708 int rsvd) /* OK to use reserved data block allocation */
710 xfs_btree_cur_t *cur; /* btree cursor */
711 int diff; /* temp value */
712 xfs_bmbt_rec_t *ep; /* extent entry for idx */
713 int error; /* error return value */
714 #ifdef XFS_BMAP_TRACE
715 static char fname[] = "xfs_bmap_add_extent_delay_real";
717 int i; /* temp state */
718 xfs_ifork_t *ifp; /* inode fork pointer */
719 xfs_fileoff_t new_endoff; /* end offset of new entry */
720 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
721 /* left is 0, right is 1, prev is 2 */
722 int rval=0; /* return value (logging flags) */
723 int state = 0;/* state bits, accessed thru macros */
724 xfs_filblks_t temp=0; /* value for dnew calculations */
725 xfs_filblks_t temp2=0;/* value for dnew calculations */
726 int tmp_rval; /* partial logging flags */
727 enum { /* bit number definitions for state */
728 LEFT_CONTIG, RIGHT_CONTIG,
729 LEFT_FILLING, RIGHT_FILLING,
730 LEFT_DELAY, RIGHT_DELAY,
731 LEFT_VALID, RIGHT_VALID
737 #define MASK(b) (1 << (b))
738 #define MASK2(a,b) (MASK(a) | MASK(b))
739 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
740 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
741 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
742 #define STATE_TEST(b) (state & MASK(b))
743 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
744 ((state &= ~MASK(b)), 0))
745 #define SWITCH_STATE \
746 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
749 * Set up a bunch of variables to make the tests simpler.
752 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
753 ep = xfs_iext_get_ext(ifp, idx);
754 xfs_bmbt_get_all(ep, &PREV);
755 new_endoff = new->br_startoff + new->br_blockcount;
756 ASSERT(PREV.br_startoff <= new->br_startoff);
757 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
759 * Set flags determining what part of the previous delayed allocation
760 * extent is being replaced by a real allocation.
762 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
763 STATE_SET(RIGHT_FILLING,
764 PREV.br_startoff + PREV.br_blockcount == new_endoff);
766 * Check and set flags if this segment has a left neighbor.
767 * Don't set contiguous if the combined extent would be too large.
769 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
770 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
771 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
773 STATE_SET(LEFT_CONTIG,
774 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
775 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
776 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
777 LEFT.br_state == new->br_state &&
778 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
780 * Check and set flags if this segment has a right neighbor.
781 * Don't set contiguous if the combined extent would be too large.
782 * Also check for all-three-contiguous being too large.
784 if (STATE_SET_TEST(RIGHT_VALID,
786 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
787 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
788 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
790 STATE_SET(RIGHT_CONTIG,
791 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
792 new_endoff == RIGHT.br_startoff &&
793 new->br_startblock + new->br_blockcount ==
794 RIGHT.br_startblock &&
795 new->br_state == RIGHT.br_state &&
796 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
797 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
798 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
799 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
803 * Switch out based on the FILLING and CONTIG state bits.
805 switch (SWITCH_STATE) {
807 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
809 * Filling in all of a previously delayed allocation extent.
810 * The left and right neighbors are both contiguous with new.
812 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
814 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
815 LEFT.br_blockcount + PREV.br_blockcount +
816 RIGHT.br_blockcount);
817 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
819 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
821 xfs_iext_remove(ifp, idx, 2);
822 ip->i_df.if_lastex = idx - 1;
823 ip->i_d.di_nextents--;
825 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
827 rval = XFS_ILOG_CORE;
828 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
830 RIGHT.br_blockcount, &i)))
833 if ((error = xfs_bmbt_delete(cur, &i)))
836 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
839 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
843 RIGHT.br_blockcount, LEFT.br_state)))
847 /* DELTA: Three in-core extents are replaced by one. */
848 temp = LEFT.br_startoff;
849 temp2 = LEFT.br_blockcount +
854 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
856 * Filling in all of a previously delayed allocation extent.
857 * The left neighbor is contiguous, the right is not.
859 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
861 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
862 LEFT.br_blockcount + PREV.br_blockcount);
863 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
865 ip->i_df.if_lastex = idx - 1;
866 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
868 xfs_iext_remove(ifp, idx, 1);
870 rval = XFS_ILOG_DEXT;
873 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
874 LEFT.br_startblock, LEFT.br_blockcount,
878 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
881 PREV.br_blockcount, LEFT.br_state)))
885 /* DELTA: Two in-core extents are replaced by one. */
886 temp = LEFT.br_startoff;
887 temp2 = LEFT.br_blockcount +
891 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
893 * Filling in all of a previously delayed allocation extent.
894 * The right neighbor is contiguous, the left is not.
896 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
898 xfs_bmbt_set_startblock(ep, new->br_startblock);
899 xfs_bmbt_set_blockcount(ep,
900 PREV.br_blockcount + RIGHT.br_blockcount);
901 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
903 ip->i_df.if_lastex = idx;
904 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
906 xfs_iext_remove(ifp, idx + 1, 1);
908 rval = XFS_ILOG_DEXT;
911 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
913 RIGHT.br_blockcount, &i)))
916 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
919 RIGHT.br_blockcount, PREV.br_state)))
923 /* DELTA: Two in-core extents are replaced by one. */
924 temp = PREV.br_startoff;
925 temp2 = PREV.br_blockcount +
929 case MASK2(LEFT_FILLING, RIGHT_FILLING):
931 * Filling in all of a previously delayed allocation extent.
932 * Neither the left nor right neighbors are contiguous with
935 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
937 xfs_bmbt_set_startblock(ep, new->br_startblock);
938 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
940 ip->i_df.if_lastex = idx;
941 ip->i_d.di_nextents++;
943 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
945 rval = XFS_ILOG_CORE;
946 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
947 new->br_startblock, new->br_blockcount,
951 cur->bc_rec.b.br_state = XFS_EXT_NORM;
952 if ((error = xfs_bmbt_insert(cur, &i)))
957 /* DELTA: The in-core extent described by new changed type. */
958 temp = new->br_startoff;
959 temp2 = new->br_blockcount;
962 case MASK2(LEFT_FILLING, LEFT_CONTIG):
964 * Filling in the first part of a previous delayed allocation.
965 * The left neighbor is contiguous.
967 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
969 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
970 LEFT.br_blockcount + new->br_blockcount);
971 xfs_bmbt_set_startoff(ep,
972 PREV.br_startoff + new->br_blockcount);
973 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
975 temp = PREV.br_blockcount - new->br_blockcount;
976 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
978 xfs_bmbt_set_blockcount(ep, temp);
979 ip->i_df.if_lastex = idx - 1;
981 rval = XFS_ILOG_DEXT;
984 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
985 LEFT.br_startblock, LEFT.br_blockcount,
989 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
996 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
997 STARTBLOCKVAL(PREV.br_startblock));
998 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
999 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
1002 /* DELTA: The boundary between two in-core extents moved. */
1003 temp = LEFT.br_startoff;
1004 temp2 = LEFT.br_blockcount +
1008 case MASK(LEFT_FILLING):
1010 * Filling in the first part of a previous delayed allocation.
1011 * The left neighbor is not contiguous.
1013 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1014 xfs_bmbt_set_startoff(ep, new_endoff);
1015 temp = PREV.br_blockcount - new->br_blockcount;
1016 xfs_bmbt_set_blockcount(ep, temp);
1017 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
1019 xfs_iext_insert(ifp, idx, 1, new);
1020 ip->i_df.if_lastex = idx;
1021 ip->i_d.di_nextents++;
1023 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1025 rval = XFS_ILOG_CORE;
1026 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1027 new->br_startblock, new->br_blockcount,
1031 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1032 if ((error = xfs_bmbt_insert(cur, &i)))
1036 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1037 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1038 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1039 first, flist, &cur, 1, &tmp_rval,
1045 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1046 STARTBLOCKVAL(PREV.br_startblock) -
1047 (cur ? cur->bc_private.b.allocated : 0));
1048 ep = xfs_iext_get_ext(ifp, idx + 1);
1049 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1050 xfs_bmap_trace_post_update(fname, "LF", ip, idx + 1,
1053 /* DELTA: One in-core extent is split in two. */
1054 temp = PREV.br_startoff;
1055 temp2 = PREV.br_blockcount;
1058 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1060 * Filling in the last part of a previous delayed allocation.
1061 * The right neighbor is contiguous with the new allocation.
1063 temp = PREV.br_blockcount - new->br_blockcount;
1064 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1066 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1068 xfs_bmbt_set_blockcount(ep, temp);
1069 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1070 new->br_startoff, new->br_startblock,
1071 new->br_blockcount + RIGHT.br_blockcount,
1073 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1075 ip->i_df.if_lastex = idx + 1;
1077 rval = XFS_ILOG_DEXT;
1080 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1081 RIGHT.br_startblock,
1082 RIGHT.br_blockcount, &i)))
1085 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1087 new->br_blockcount +
1088 RIGHT.br_blockcount,
1092 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1093 STARTBLOCKVAL(PREV.br_startblock));
1094 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1095 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1098 /* DELTA: The boundary between two in-core extents moved. */
1099 temp = PREV.br_startoff;
1100 temp2 = PREV.br_blockcount +
1101 RIGHT.br_blockcount;
1104 case MASK(RIGHT_FILLING):
1106 * Filling in the last part of a previous delayed allocation.
1107 * The right neighbor is not contiguous.
1109 temp = PREV.br_blockcount - new->br_blockcount;
1110 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1111 xfs_bmbt_set_blockcount(ep, temp);
1112 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1113 new, NULL, XFS_DATA_FORK);
1114 xfs_iext_insert(ifp, idx + 1, 1, new);
1115 ip->i_df.if_lastex = idx + 1;
1116 ip->i_d.di_nextents++;
1118 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1120 rval = XFS_ILOG_CORE;
1121 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1122 new->br_startblock, new->br_blockcount,
1126 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1127 if ((error = xfs_bmbt_insert(cur, &i)))
1131 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1132 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1133 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1134 first, flist, &cur, 1, &tmp_rval,
1140 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1141 STARTBLOCKVAL(PREV.br_startblock) -
1142 (cur ? cur->bc_private.b.allocated : 0));
1143 ep = xfs_iext_get_ext(ifp, idx);
1144 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1145 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1147 /* DELTA: One in-core extent is split in two. */
1148 temp = PREV.br_startoff;
1149 temp2 = PREV.br_blockcount;
1154 * Filling in the middle part of a previous delayed allocation.
1155 * Contiguity is impossible here.
1156 * This case is avoided almost all the time.
1158 temp = new->br_startoff - PREV.br_startoff;
1159 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1160 xfs_bmbt_set_blockcount(ep, temp);
1162 r[1].br_state = PREV.br_state;
1163 r[1].br_startblock = 0;
1164 r[1].br_startoff = new_endoff;
1165 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1166 r[1].br_blockcount = temp2;
1167 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1169 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1170 ip->i_df.if_lastex = idx + 1;
1171 ip->i_d.di_nextents++;
1173 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1175 rval = XFS_ILOG_CORE;
1176 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1177 new->br_startblock, new->br_blockcount,
1181 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1182 if ((error = xfs_bmbt_insert(cur, &i)))
1186 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1187 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1188 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1189 first, flist, &cur, 1, &tmp_rval,
1195 temp = xfs_bmap_worst_indlen(ip, temp);
1196 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1197 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
1198 (cur ? cur->bc_private.b.allocated : 0));
1200 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
1202 * Ick gross gag me with a spoon.
1204 ASSERT(0); /* want to see if this ever happens! */
1210 !xfs_mod_incore_sb(ip->i_mount,
1211 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1218 !xfs_mod_incore_sb(ip->i_mount,
1219 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1224 ep = xfs_iext_get_ext(ifp, idx);
1225 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1226 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1227 xfs_bmap_trace_pre_update(fname, "0", ip, idx + 2,
1229 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1230 NULLSTARTBLOCK((int)temp2));
1231 xfs_bmap_trace_post_update(fname, "0", ip, idx + 2,
1233 *dnew = temp + temp2;
1234 /* DELTA: One in-core extent is split in three. */
1235 temp = PREV.br_startoff;
1236 temp2 = PREV.br_blockcount;
1239 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1240 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1241 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1242 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1243 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1244 case MASK(LEFT_CONTIG):
1245 case MASK(RIGHT_CONTIG):
1247 * These cases are all impossible.
1254 if (delta->xed_startoff > temp)
1255 delta->xed_startoff = temp;
1256 if (delta->xed_blockcount < temp2)
1257 delta->xed_blockcount = temp2;
1271 #undef STATE_SET_TEST
1276 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1277 * allocation to a real allocation or vice versa.
1279 STATIC int /* error */
1280 xfs_bmap_add_extent_unwritten_real(
1281 xfs_inode_t *ip, /* incore inode pointer */
1282 xfs_extnum_t idx, /* extent number to update/insert */
1283 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1284 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1285 int *logflagsp, /* inode logging flags */
1286 xfs_extdelta_t *delta) /* Change made to incore extents */
1288 xfs_btree_cur_t *cur; /* btree cursor */
1289 xfs_bmbt_rec_t *ep; /* extent entry for idx */
1290 int error; /* error return value */
1291 #ifdef XFS_BMAP_TRACE
1292 static char fname[] = "xfs_bmap_add_extent_unwritten_real";
1294 int i; /* temp state */
1295 xfs_ifork_t *ifp; /* inode fork pointer */
1296 xfs_fileoff_t new_endoff; /* end offset of new entry */
1297 xfs_exntst_t newext; /* new extent state */
1298 xfs_exntst_t oldext; /* old extent state */
1299 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1300 /* left is 0, right is 1, prev is 2 */
1301 int rval=0; /* return value (logging flags) */
1302 int state = 0;/* state bits, accessed thru macros */
1303 xfs_filblks_t temp=0;
1304 xfs_filblks_t temp2=0;
1305 enum { /* bit number definitions for state */
1306 LEFT_CONTIG, RIGHT_CONTIG,
1307 LEFT_FILLING, RIGHT_FILLING,
1308 LEFT_DELAY, RIGHT_DELAY,
1309 LEFT_VALID, RIGHT_VALID
1315 #define MASK(b) (1 << (b))
1316 #define MASK2(a,b) (MASK(a) | MASK(b))
1317 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1318 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1319 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1320 #define STATE_TEST(b) (state & MASK(b))
1321 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1322 ((state &= ~MASK(b)), 0))
1323 #define SWITCH_STATE \
1324 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1327 * Set up a bunch of variables to make the tests simpler.
1331 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1332 ep = xfs_iext_get_ext(ifp, idx);
1333 xfs_bmbt_get_all(ep, &PREV);
1334 newext = new->br_state;
1335 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1336 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1337 ASSERT(PREV.br_state == oldext);
1338 new_endoff = new->br_startoff + new->br_blockcount;
1339 ASSERT(PREV.br_startoff <= new->br_startoff);
1340 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1342 * Set flags determining what part of the previous oldext allocation
1343 * extent is being replaced by a newext allocation.
1345 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
1346 STATE_SET(RIGHT_FILLING,
1347 PREV.br_startoff + PREV.br_blockcount == new_endoff);
1349 * Check and set flags if this segment has a left neighbor.
1350 * Don't set contiguous if the combined extent would be too large.
1352 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1353 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1354 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
1356 STATE_SET(LEFT_CONTIG,
1357 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1358 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1359 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1360 LEFT.br_state == newext &&
1361 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1363 * Check and set flags if this segment has a right neighbor.
1364 * Don't set contiguous if the combined extent would be too large.
1365 * Also check for all-three-contiguous being too large.
1367 if (STATE_SET_TEST(RIGHT_VALID,
1369 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
1370 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1371 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
1373 STATE_SET(RIGHT_CONTIG,
1374 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1375 new_endoff == RIGHT.br_startoff &&
1376 new->br_startblock + new->br_blockcount ==
1377 RIGHT.br_startblock &&
1378 newext == RIGHT.br_state &&
1379 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1380 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
1381 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
1382 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1385 * Switch out based on the FILLING and CONTIG state bits.
1387 switch (SWITCH_STATE) {
1389 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1391 * Setting all of a previous oldext extent to newext.
1392 * The left and right neighbors are both contiguous with new.
1394 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1396 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1397 LEFT.br_blockcount + PREV.br_blockcount +
1398 RIGHT.br_blockcount);
1399 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1401 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
1403 xfs_iext_remove(ifp, idx, 2);
1404 ip->i_df.if_lastex = idx - 1;
1405 ip->i_d.di_nextents -= 2;
1407 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1409 rval = XFS_ILOG_CORE;
1410 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1411 RIGHT.br_startblock,
1412 RIGHT.br_blockcount, &i)))
1415 if ((error = xfs_bmbt_delete(cur, &i)))
1418 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1421 if ((error = xfs_bmbt_delete(cur, &i)))
1424 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1427 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1429 LEFT.br_blockcount + PREV.br_blockcount +
1430 RIGHT.br_blockcount, LEFT.br_state)))
1433 /* DELTA: Three in-core extents are replaced by one. */
1434 temp = LEFT.br_startoff;
1435 temp2 = LEFT.br_blockcount +
1436 PREV.br_blockcount +
1437 RIGHT.br_blockcount;
1440 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
1442 * Setting all of a previous oldext extent to newext.
1443 * The left neighbor is contiguous, the right is not.
1445 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
1447 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1448 LEFT.br_blockcount + PREV.br_blockcount);
1449 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
1451 ip->i_df.if_lastex = idx - 1;
1452 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
1454 xfs_iext_remove(ifp, idx, 1);
1455 ip->i_d.di_nextents--;
1457 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1459 rval = XFS_ILOG_CORE;
1460 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1461 PREV.br_startblock, PREV.br_blockcount,
1465 if ((error = xfs_bmbt_delete(cur, &i)))
1468 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1471 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1473 LEFT.br_blockcount + PREV.br_blockcount,
1477 /* DELTA: Two in-core extents are replaced by one. */
1478 temp = LEFT.br_startoff;
1479 temp2 = LEFT.br_blockcount +
1483 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
1485 * Setting all of a previous oldext extent to newext.
1486 * The right neighbor is contiguous, the left is not.
1488 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
1490 xfs_bmbt_set_blockcount(ep,
1491 PREV.br_blockcount + RIGHT.br_blockcount);
1492 xfs_bmbt_set_state(ep, newext);
1493 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
1495 ip->i_df.if_lastex = idx;
1496 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
1498 xfs_iext_remove(ifp, idx + 1, 1);
1499 ip->i_d.di_nextents--;
1501 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1503 rval = XFS_ILOG_CORE;
1504 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1505 RIGHT.br_startblock,
1506 RIGHT.br_blockcount, &i)))
1509 if ((error = xfs_bmbt_delete(cur, &i)))
1512 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1515 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1517 new->br_blockcount + RIGHT.br_blockcount,
1521 /* DELTA: Two in-core extents are replaced by one. */
1522 temp = PREV.br_startoff;
1523 temp2 = PREV.br_blockcount +
1524 RIGHT.br_blockcount;
1527 case MASK2(LEFT_FILLING, RIGHT_FILLING):
1529 * Setting all of a previous oldext extent to newext.
1530 * Neither the left nor right neighbors are contiguous with
1533 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
1535 xfs_bmbt_set_state(ep, newext);
1536 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
1538 ip->i_df.if_lastex = idx;
1540 rval = XFS_ILOG_DEXT;
1543 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1544 new->br_startblock, new->br_blockcount,
1548 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1549 new->br_startblock, new->br_blockcount,
1553 /* DELTA: The in-core extent described by new changed type. */
1554 temp = new->br_startoff;
1555 temp2 = new->br_blockcount;
1558 case MASK2(LEFT_FILLING, LEFT_CONTIG):
1560 * Setting the first part of a previous oldext extent to newext.
1561 * The left neighbor is contiguous.
1563 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
1565 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1566 LEFT.br_blockcount + new->br_blockcount);
1567 xfs_bmbt_set_startoff(ep,
1568 PREV.br_startoff + new->br_blockcount);
1569 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
1571 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
1573 xfs_bmbt_set_startblock(ep,
1574 new->br_startblock + new->br_blockcount);
1575 xfs_bmbt_set_blockcount(ep,
1576 PREV.br_blockcount - new->br_blockcount);
1577 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
1579 ip->i_df.if_lastex = idx - 1;
1581 rval = XFS_ILOG_DEXT;
1584 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1585 PREV.br_startblock, PREV.br_blockcount,
1589 if ((error = xfs_bmbt_update(cur,
1590 PREV.br_startoff + new->br_blockcount,
1591 PREV.br_startblock + new->br_blockcount,
1592 PREV.br_blockcount - new->br_blockcount,
1595 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1597 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1599 LEFT.br_blockcount + new->br_blockcount,
1603 /* DELTA: The boundary between two in-core extents moved. */
1604 temp = LEFT.br_startoff;
1605 temp2 = LEFT.br_blockcount +
1609 case MASK(LEFT_FILLING):
1611 * Setting the first part of a previous oldext extent to newext.
1612 * The left neighbor is not contiguous.
1614 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1615 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1616 xfs_bmbt_set_startoff(ep, new_endoff);
1617 xfs_bmbt_set_blockcount(ep,
1618 PREV.br_blockcount - new->br_blockcount);
1619 xfs_bmbt_set_startblock(ep,
1620 new->br_startblock + new->br_blockcount);
1621 xfs_bmap_trace_post_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1622 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
1624 xfs_iext_insert(ifp, idx, 1, new);
1625 ip->i_df.if_lastex = idx;
1626 ip->i_d.di_nextents++;
1628 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1630 rval = XFS_ILOG_CORE;
1631 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1632 PREV.br_startblock, PREV.br_blockcount,
1636 if ((error = xfs_bmbt_update(cur,
1637 PREV.br_startoff + new->br_blockcount,
1638 PREV.br_startblock + new->br_blockcount,
1639 PREV.br_blockcount - new->br_blockcount,
1642 cur->bc_rec.b = *new;
1643 if ((error = xfs_bmbt_insert(cur, &i)))
1647 /* DELTA: One in-core extent is split in two. */
1648 temp = PREV.br_startoff;
1649 temp2 = PREV.br_blockcount;
1652 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1654 * Setting the last part of a previous oldext extent to newext.
1655 * The right neighbor is contiguous with the new allocation.
1657 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1659 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1661 xfs_bmbt_set_blockcount(ep,
1662 PREV.br_blockcount - new->br_blockcount);
1663 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1665 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1666 new->br_startoff, new->br_startblock,
1667 new->br_blockcount + RIGHT.br_blockcount, newext);
1668 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1670 ip->i_df.if_lastex = idx + 1;
1672 rval = XFS_ILOG_DEXT;
1675 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1677 PREV.br_blockcount, &i)))
1680 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1682 PREV.br_blockcount - new->br_blockcount,
1685 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1687 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1689 new->br_blockcount + RIGHT.br_blockcount,
1693 /* DELTA: The boundary between two in-core extents moved. */
1694 temp = PREV.br_startoff;
1695 temp2 = PREV.br_blockcount +
1696 RIGHT.br_blockcount;
1699 case MASK(RIGHT_FILLING):
1701 * Setting the last part of a previous oldext extent to newext.
1702 * The right neighbor is not contiguous.
1704 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1705 xfs_bmbt_set_blockcount(ep,
1706 PREV.br_blockcount - new->br_blockcount);
1707 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1708 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1709 new, NULL, XFS_DATA_FORK);
1710 xfs_iext_insert(ifp, idx + 1, 1, new);
1711 ip->i_df.if_lastex = idx + 1;
1712 ip->i_d.di_nextents++;
1714 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1716 rval = XFS_ILOG_CORE;
1717 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1718 PREV.br_startblock, PREV.br_blockcount,
1722 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1724 PREV.br_blockcount - new->br_blockcount,
1727 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1728 new->br_startblock, new->br_blockcount,
1732 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1733 if ((error = xfs_bmbt_insert(cur, &i)))
1737 /* DELTA: One in-core extent is split in two. */
1738 temp = PREV.br_startoff;
1739 temp2 = PREV.br_blockcount;
1744 * Setting the middle part of a previous oldext extent to
1745 * newext. Contiguity is impossible here.
1746 * One extent becomes three extents.
1748 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1749 xfs_bmbt_set_blockcount(ep,
1750 new->br_startoff - PREV.br_startoff);
1751 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1753 r[1].br_startoff = new_endoff;
1754 r[1].br_blockcount =
1755 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1756 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1757 r[1].br_state = oldext;
1758 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1760 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1761 ip->i_df.if_lastex = idx + 1;
1762 ip->i_d.di_nextents += 2;
1764 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1766 rval = XFS_ILOG_CORE;
1767 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1768 PREV.br_startblock, PREV.br_blockcount,
1772 /* new right extent - oldext */
1773 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1774 r[1].br_startblock, r[1].br_blockcount,
1777 /* new left extent - oldext */
1778 PREV.br_blockcount =
1779 new->br_startoff - PREV.br_startoff;
1780 cur->bc_rec.b = PREV;
1781 if ((error = xfs_bmbt_insert(cur, &i)))
1784 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1787 /* new middle extent - newext */
1788 cur->bc_rec.b = *new;
1789 if ((error = xfs_bmbt_insert(cur, &i)))
1793 /* DELTA: One in-core extent is split in three. */
1794 temp = PREV.br_startoff;
1795 temp2 = PREV.br_blockcount;
1798 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1799 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1800 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1801 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1802 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1803 case MASK(LEFT_CONTIG):
1804 case MASK(RIGHT_CONTIG):
1806 * These cases are all impossible.
1813 if (delta->xed_startoff > temp)
1814 delta->xed_startoff = temp;
1815 if (delta->xed_blockcount < temp2)
1816 delta->xed_blockcount = temp2;
1830 #undef STATE_SET_TEST
1835 * Called by xfs_bmap_add_extent to handle cases converting a hole
1836 * to a delayed allocation.
1839 STATIC int /* error */
1840 xfs_bmap_add_extent_hole_delay(
1841 xfs_inode_t *ip, /* incore inode pointer */
1842 xfs_extnum_t idx, /* extent number to update/insert */
1843 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1844 int *logflagsp, /* inode logging flags */
1845 xfs_extdelta_t *delta, /* Change made to incore extents */
1846 int rsvd) /* OK to allocate reserved blocks */
1848 xfs_bmbt_rec_t *ep; /* extent record for idx */
1849 #ifdef XFS_BMAP_TRACE
1850 static char fname[] = "xfs_bmap_add_extent_hole_delay";
1852 xfs_ifork_t *ifp; /* inode fork pointer */
1853 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1854 xfs_filblks_t newlen=0; /* new indirect size */
1855 xfs_filblks_t oldlen=0; /* old indirect size */
1856 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1857 int state; /* state bits, accessed thru macros */
1858 xfs_filblks_t temp=0; /* temp for indirect calculations */
1859 xfs_filblks_t temp2=0;
1860 enum { /* bit number definitions for state */
1861 LEFT_CONTIG, RIGHT_CONTIG,
1862 LEFT_DELAY, RIGHT_DELAY,
1863 LEFT_VALID, RIGHT_VALID
1866 #define MASK(b) (1 << (b))
1867 #define MASK2(a,b) (MASK(a) | MASK(b))
1868 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1869 #define STATE_TEST(b) (state & MASK(b))
1870 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1871 ((state &= ~MASK(b)), 0))
1872 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1874 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1875 ep = xfs_iext_get_ext(ifp, idx);
1877 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1879 * Check and set flags if this segment has a left neighbor
1881 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1882 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1883 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1886 * Check and set flags if the current (right) segment exists.
1887 * If it doesn't exist, we're converting the hole at end-of-file.
1889 if (STATE_SET_TEST(RIGHT_VALID,
1891 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1892 xfs_bmbt_get_all(ep, &right);
1893 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1896 * Set contiguity flags on the left and right neighbors.
1897 * Don't let extents get too large, even if the pieces are contiguous.
1899 STATE_SET(LEFT_CONTIG,
1900 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1901 left.br_startoff + left.br_blockcount == new->br_startoff &&
1902 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1903 STATE_SET(RIGHT_CONTIG,
1904 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1905 new->br_startoff + new->br_blockcount == right.br_startoff &&
1906 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1907 (!STATE_TEST(LEFT_CONTIG) ||
1908 (left.br_blockcount + new->br_blockcount +
1909 right.br_blockcount <= MAXEXTLEN)));
1911 * Switch out based on the contiguity flags.
1913 switch (SWITCH_STATE) {
1915 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1917 * New allocation is contiguous with delayed allocations
1918 * on the left and on the right.
1919 * Merge all three into a single extent record.
1921 temp = left.br_blockcount + new->br_blockcount +
1922 right.br_blockcount;
1923 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
1925 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1926 oldlen = STARTBLOCKVAL(left.br_startblock) +
1927 STARTBLOCKVAL(new->br_startblock) +
1928 STARTBLOCKVAL(right.br_startblock);
1929 newlen = xfs_bmap_worst_indlen(ip, temp);
1930 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1931 NULLSTARTBLOCK((int)newlen));
1932 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
1934 xfs_bmap_trace_delete(fname, "LC|RC", ip, idx, 1,
1936 xfs_iext_remove(ifp, idx, 1);
1937 ip->i_df.if_lastex = idx - 1;
1938 /* DELTA: Two in-core extents were replaced by one. */
1940 temp = left.br_startoff;
1943 case MASK(LEFT_CONTIG):
1945 * New allocation is contiguous with a delayed allocation
1947 * Merge the new allocation with the left neighbor.
1949 temp = left.br_blockcount + new->br_blockcount;
1950 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1,
1952 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1953 oldlen = STARTBLOCKVAL(left.br_startblock) +
1954 STARTBLOCKVAL(new->br_startblock);
1955 newlen = xfs_bmap_worst_indlen(ip, temp);
1956 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1957 NULLSTARTBLOCK((int)newlen));
1958 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1,
1960 ip->i_df.if_lastex = idx - 1;
1961 /* DELTA: One in-core extent grew into a hole. */
1963 temp = left.br_startoff;
1966 case MASK(RIGHT_CONTIG):
1968 * New allocation is contiguous with a delayed allocation
1970 * Merge the new allocation with the right neighbor.
1972 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1973 temp = new->br_blockcount + right.br_blockcount;
1974 oldlen = STARTBLOCKVAL(new->br_startblock) +
1975 STARTBLOCKVAL(right.br_startblock);
1976 newlen = xfs_bmap_worst_indlen(ip, temp);
1977 xfs_bmbt_set_allf(ep, new->br_startoff,
1978 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
1979 xfs_bmap_trace_post_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1980 ip->i_df.if_lastex = idx;
1981 /* DELTA: One in-core extent grew into a hole. */
1983 temp = new->br_startoff;
1988 * New allocation is not contiguous with another
1989 * delayed allocation.
1990 * Insert a new entry.
1992 oldlen = newlen = 0;
1993 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
1995 xfs_iext_insert(ifp, idx, 1, new);
1996 ip->i_df.if_lastex = idx;
1997 /* DELTA: A new in-core extent was added in a hole. */
1998 temp2 = new->br_blockcount;
1999 temp = new->br_startoff;
2002 if (oldlen != newlen) {
2003 ASSERT(oldlen > newlen);
2004 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
2005 (int64_t)(oldlen - newlen), rsvd);
2007 * Nothing to do for disk quota accounting here.
2012 if (delta->xed_startoff > temp)
2013 delta->xed_startoff = temp;
2014 if (delta->xed_blockcount < temp2)
2015 delta->xed_blockcount = temp2;
2023 #undef STATE_SET_TEST
2028 * Called by xfs_bmap_add_extent to handle cases converting a hole
2029 * to a real allocation.
2031 STATIC int /* error */
2032 xfs_bmap_add_extent_hole_real(
2033 xfs_inode_t *ip, /* incore inode pointer */
2034 xfs_extnum_t idx, /* extent number to update/insert */
2035 xfs_btree_cur_t *cur, /* if null, not a btree */
2036 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2037 int *logflagsp, /* inode logging flags */
2038 xfs_extdelta_t *delta, /* Change made to incore extents */
2039 int whichfork) /* data or attr fork */
2041 xfs_bmbt_rec_t *ep; /* pointer to extent entry ins. point */
2042 int error; /* error return value */
2043 #ifdef XFS_BMAP_TRACE
2044 static char fname[] = "xfs_bmap_add_extent_hole_real";
2046 int i; /* temp state */
2047 xfs_ifork_t *ifp; /* inode fork pointer */
2048 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2049 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2050 int rval=0; /* return value (logging flags) */
2051 int state; /* state bits, accessed thru macros */
2052 xfs_filblks_t temp=0;
2053 xfs_filblks_t temp2=0;
2054 enum { /* bit number definitions for state */
2055 LEFT_CONTIG, RIGHT_CONTIG,
2056 LEFT_DELAY, RIGHT_DELAY,
2057 LEFT_VALID, RIGHT_VALID
2060 #define MASK(b) (1 << (b))
2061 #define MASK2(a,b) (MASK(a) | MASK(b))
2062 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
2063 #define STATE_TEST(b) (state & MASK(b))
2064 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
2065 ((state &= ~MASK(b)), 0))
2066 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
2068 ifp = XFS_IFORK_PTR(ip, whichfork);
2069 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
2070 ep = xfs_iext_get_ext(ifp, idx);
2073 * Check and set flags if this segment has a left neighbor.
2075 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
2076 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
2077 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
2080 * Check and set flags if this segment has a current value.
2081 * Not true if we're inserting into the "hole" at eof.
2083 if (STATE_SET_TEST(RIGHT_VALID,
2085 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
2086 xfs_bmbt_get_all(ep, &right);
2087 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
2090 * We're inserting a real allocation between "left" and "right".
2091 * Set the contiguity flags. Don't let extents get too large.
2093 STATE_SET(LEFT_CONTIG,
2094 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
2095 left.br_startoff + left.br_blockcount == new->br_startoff &&
2096 left.br_startblock + left.br_blockcount == new->br_startblock &&
2097 left.br_state == new->br_state &&
2098 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
2099 STATE_SET(RIGHT_CONTIG,
2100 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
2101 new->br_startoff + new->br_blockcount == right.br_startoff &&
2102 new->br_startblock + new->br_blockcount ==
2103 right.br_startblock &&
2104 new->br_state == right.br_state &&
2105 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2106 (!STATE_TEST(LEFT_CONTIG) ||
2107 left.br_blockcount + new->br_blockcount +
2108 right.br_blockcount <= MAXEXTLEN));
2112 * Select which case we're in here, and implement it.
2114 switch (SWITCH_STATE) {
2116 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
2118 * New allocation is contiguous with real allocations on the
2119 * left and on the right.
2120 * Merge all three into a single extent record.
2122 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
2124 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2125 left.br_blockcount + new->br_blockcount +
2126 right.br_blockcount);
2127 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
2129 xfs_bmap_trace_delete(fname, "LC|RC", ip,
2131 xfs_iext_remove(ifp, idx, 1);
2132 ifp->if_lastex = idx - 1;
2133 XFS_IFORK_NEXT_SET(ip, whichfork,
2134 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2136 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2138 rval = XFS_ILOG_CORE;
2139 if ((error = xfs_bmbt_lookup_eq(cur,
2141 right.br_startblock,
2142 right.br_blockcount, &i)))
2145 if ((error = xfs_bmbt_delete(cur, &i)))
2148 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2151 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2153 left.br_blockcount +
2154 new->br_blockcount +
2155 right.br_blockcount,
2159 /* DELTA: Two in-core extents were replaced by one. */
2160 temp = left.br_startoff;
2161 temp2 = left.br_blockcount +
2162 new->br_blockcount +
2163 right.br_blockcount;
2166 case MASK(LEFT_CONTIG):
2168 * New allocation is contiguous with a real allocation
2170 * Merge the new allocation with the left neighbor.
2172 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1, whichfork);
2173 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2174 left.br_blockcount + new->br_blockcount);
2175 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1, whichfork);
2176 ifp->if_lastex = idx - 1;
2178 rval = XFS_ILOG_FEXT(whichfork);
2181 if ((error = xfs_bmbt_lookup_eq(cur,
2184 left.br_blockcount, &i)))
2187 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2189 left.br_blockcount +
2194 /* DELTA: One in-core extent grew. */
2195 temp = left.br_startoff;
2196 temp2 = left.br_blockcount +
2200 case MASK(RIGHT_CONTIG):
2202 * New allocation is contiguous with a real allocation
2204 * Merge the new allocation with the right neighbor.
2206 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, whichfork);
2207 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2208 new->br_blockcount + right.br_blockcount,
2210 xfs_bmap_trace_post_update(fname, "RC", ip, idx, whichfork);
2211 ifp->if_lastex = idx;
2213 rval = XFS_ILOG_FEXT(whichfork);
2216 if ((error = xfs_bmbt_lookup_eq(cur,
2218 right.br_startblock,
2219 right.br_blockcount, &i)))
2222 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2224 new->br_blockcount +
2225 right.br_blockcount,
2229 /* DELTA: One in-core extent grew. */
2230 temp = new->br_startoff;
2231 temp2 = new->br_blockcount +
2232 right.br_blockcount;
2237 * New allocation is not contiguous with another
2239 * Insert a new entry.
2241 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
2243 xfs_iext_insert(ifp, idx, 1, new);
2244 ifp->if_lastex = idx;
2245 XFS_IFORK_NEXT_SET(ip, whichfork,
2246 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2248 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2250 rval = XFS_ILOG_CORE;
2251 if ((error = xfs_bmbt_lookup_eq(cur,
2254 new->br_blockcount, &i)))
2257 cur->bc_rec.b.br_state = new->br_state;
2258 if ((error = xfs_bmbt_insert(cur, &i)))
2262 /* DELTA: A new extent was added in a hole. */
2263 temp = new->br_startoff;
2264 temp2 = new->br_blockcount;
2269 if (delta->xed_startoff > temp)
2270 delta->xed_startoff = temp;
2271 if (delta->xed_blockcount < temp2)
2272 delta->xed_blockcount = temp2;
2281 #undef STATE_SET_TEST
2286 * Adjust the size of the new extent based on di_extsize and rt extsize.
2289 xfs_bmap_extsize_align(
2291 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2292 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2293 xfs_extlen_t extsz, /* align to this extent size */
2294 int rt, /* is this a realtime inode? */
2295 int eof, /* is extent at end-of-file? */
2296 int delay, /* creating delalloc extent? */
2297 int convert, /* overwriting unwritten extent? */
2298 xfs_fileoff_t *offp, /* in/out: aligned offset */
2299 xfs_extlen_t *lenp) /* in/out: aligned length */
2301 xfs_fileoff_t orig_off; /* original offset */
2302 xfs_extlen_t orig_alen; /* original length */
2303 xfs_fileoff_t orig_end; /* original off+len */
2304 xfs_fileoff_t nexto; /* next file offset */
2305 xfs_fileoff_t prevo; /* previous file offset */
2306 xfs_fileoff_t align_off; /* temp for offset */
2307 xfs_extlen_t align_alen; /* temp for length */
2308 xfs_extlen_t temp; /* temp for calculations */
2313 orig_off = align_off = *offp;
2314 orig_alen = align_alen = *lenp;
2315 orig_end = orig_off + orig_alen;
2318 * If this request overlaps an existing extent, then don't
2319 * attempt to perform any additional alignment.
2321 if (!delay && !eof &&
2322 (orig_off >= gotp->br_startoff) &&
2323 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2328 * If the file offset is unaligned vs. the extent size
2329 * we need to align it. This will be possible unless
2330 * the file was previously written with a kernel that didn't
2331 * perform this alignment, or if a truncate shot us in the
2334 temp = do_mod(orig_off, extsz);
2340 * Same adjustment for the end of the requested area.
2342 if ((temp = (align_alen % extsz))) {
2343 align_alen += extsz - temp;
2346 * If the previous block overlaps with this proposed allocation
2347 * then move the start forward without adjusting the length.
2349 if (prevp->br_startoff != NULLFILEOFF) {
2350 if (prevp->br_startblock == HOLESTARTBLOCK)
2351 prevo = prevp->br_startoff;
2353 prevo = prevp->br_startoff + prevp->br_blockcount;
2356 if (align_off != orig_off && align_off < prevo)
2359 * If the next block overlaps with this proposed allocation
2360 * then move the start back without adjusting the length,
2361 * but not before offset 0.
2362 * This may of course make the start overlap previous block,
2363 * and if we hit the offset 0 limit then the next block
2364 * can still overlap too.
2366 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2367 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2368 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2369 nexto = gotp->br_startoff + gotp->br_blockcount;
2371 nexto = gotp->br_startoff;
2373 nexto = NULLFILEOFF;
2375 align_off + align_alen != orig_end &&
2376 align_off + align_alen > nexto)
2377 align_off = nexto > align_alen ? nexto - align_alen : 0;
2379 * If we're now overlapping the next or previous extent that
2380 * means we can't fit an extsz piece in this hole. Just move
2381 * the start forward to the first valid spot and set
2382 * the length so we hit the end.
2384 if (align_off != orig_off && align_off < prevo)
2386 if (align_off + align_alen != orig_end &&
2387 align_off + align_alen > nexto &&
2388 nexto != NULLFILEOFF) {
2389 ASSERT(nexto > prevo);
2390 align_alen = nexto - align_off;
2394 * If realtime, and the result isn't a multiple of the realtime
2395 * extent size we need to remove blocks until it is.
2397 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2399 * We're not covering the original request, or
2400 * we won't be able to once we fix the length.
2402 if (orig_off < align_off ||
2403 orig_end > align_off + align_alen ||
2404 align_alen - temp < orig_alen)
2405 return XFS_ERROR(EINVAL);
2407 * Try to fix it by moving the start up.
2409 if (align_off + temp <= orig_off) {
2414 * Try to fix it by moving the end in.
2416 else if (align_off + align_alen - temp >= orig_end)
2419 * Set the start to the minimum then trim the length.
2422 align_alen -= orig_off - align_off;
2423 align_off = orig_off;
2424 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2427 * Result doesn't cover the request, fail it.
2429 if (orig_off < align_off || orig_end > align_off + align_alen)
2430 return XFS_ERROR(EINVAL);
2432 ASSERT(orig_off >= align_off);
2433 ASSERT(orig_end <= align_off + align_alen);
2437 if (!eof && gotp->br_startoff != NULLFILEOFF)
2438 ASSERT(align_off + align_alen <= gotp->br_startoff);
2439 if (prevp->br_startoff != NULLFILEOFF)
2440 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2448 #define XFS_ALLOC_GAP_UNITS 4
2452 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2454 xfs_fsblock_t adjust; /* adjustment to block numbers */
2455 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2456 xfs_mount_t *mp; /* mount point structure */
2457 int nullfb; /* true if ap->firstblock isn't set */
2458 int rt; /* true if inode is realtime */
2460 #define ISVALID(x,y) \
2462 (x) < mp->m_sb.sb_rblocks : \
2463 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2464 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2465 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2467 mp = ap->ip->i_mount;
2468 nullfb = ap->firstblock == NULLFSBLOCK;
2469 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2470 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2472 * If allocating at eof, and there's a previous real block,
2473 * try to use it's last block as our starting point.
2475 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2476 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2477 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2478 ap->prevp->br_startblock)) {
2479 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2481 * Adjust for the gap between prevp and us.
2484 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2486 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2490 * If not at eof, then compare the two neighbor blocks.
2491 * Figure out whether either one gives us a good starting point,
2492 * and pick the better one.
2494 else if (!ap->eof) {
2495 xfs_fsblock_t gotbno; /* right side block number */
2496 xfs_fsblock_t gotdiff=0; /* right side difference */
2497 xfs_fsblock_t prevbno; /* left side block number */
2498 xfs_fsblock_t prevdiff=0; /* left side difference */
2501 * If there's a previous (left) block, select a requested
2502 * start block based on it.
2504 if (ap->prevp->br_startoff != NULLFILEOFF &&
2505 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2506 (prevbno = ap->prevp->br_startblock +
2507 ap->prevp->br_blockcount) &&
2508 ISVALID(prevbno, ap->prevp->br_startblock)) {
2510 * Calculate gap to end of previous block.
2512 adjust = prevdiff = ap->off -
2513 (ap->prevp->br_startoff +
2514 ap->prevp->br_blockcount);
2516 * Figure the startblock based on the previous block's
2517 * end and the gap size.
2519 * If the gap is large relative to the piece we're
2520 * allocating, or using it gives us an invalid block
2521 * number, then just use the end of the previous block.
2523 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2524 ISVALID(prevbno + prevdiff,
2525 ap->prevp->br_startblock))
2530 * If the firstblock forbids it, can't use it,
2533 if (!rt && !nullfb &&
2534 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2535 prevbno = NULLFSBLOCK;
2538 * No previous block or can't follow it, just default.
2541 prevbno = NULLFSBLOCK;
2543 * If there's a following (right) block, select a requested
2544 * start block based on it.
2546 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
2548 * Calculate gap to start of next block.
2550 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2552 * Figure the startblock based on the next block's
2553 * start and the gap size.
2555 gotbno = ap->gotp->br_startblock;
2558 * If the gap is large relative to the piece we're
2559 * allocating, or using it gives us an invalid block
2560 * number, then just use the start of the next block
2561 * offset by our length.
2563 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2564 ISVALID(gotbno - gotdiff, gotbno))
2566 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2568 gotdiff += adjust - ap->alen;
2572 * If the firstblock forbids it, can't use it,
2575 if (!rt && !nullfb &&
2576 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2577 gotbno = NULLFSBLOCK;
2580 * No next block, just default.
2583 gotbno = NULLFSBLOCK;
2585 * If both valid, pick the better one, else the only good
2586 * one, else ap->rval is already set (to 0 or the inode block).
2588 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2589 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2590 else if (prevbno != NULLFSBLOCK)
2592 else if (gotbno != NULLFSBLOCK)
2601 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2603 xfs_alloctype_t atype = 0; /* type for allocation routines */
2604 int error; /* error return value */
2605 xfs_mount_t *mp; /* mount point structure */
2606 xfs_extlen_t prod = 0; /* product factor for allocators */
2607 xfs_extlen_t ralen = 0; /* realtime allocation length */
2608 xfs_extlen_t align; /* minimum allocation alignment */
2611 mp = ap->ip->i_mount;
2612 align = xfs_get_extsz_hint(ap->ip);
2613 prod = align / mp->m_sb.sb_rextsize;
2614 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2615 align, 1, ap->eof, 0,
2616 ap->conv, &ap->off, &ap->alen);
2620 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2623 * If the offset & length are not perfectly aligned
2624 * then kill prod, it will just get us in trouble.
2626 if (do_mod(ap->off, align) || ap->alen % align)
2629 * Set ralen to be the actual requested length in rtextents.
2631 ralen = ap->alen / mp->m_sb.sb_rextsize;
2633 * If the old value was close enough to MAXEXTLEN that
2634 * we rounded up to it, cut it back so it's valid again.
2635 * Note that if it's a really large request (bigger than
2636 * MAXEXTLEN), we don't hear about that number, and can't
2637 * adjust the starting point to match it.
2639 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2640 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2642 * If it's an allocation to an empty file at offset 0,
2643 * pick an extent that will space things out in the rt area.
2645 if (ap->eof && ap->off == 0) {
2646 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2648 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2651 ap->rval = rtx * mp->m_sb.sb_rextsize;
2656 xfs_bmap_adjacent(ap);
2659 * Realtime allocation, done through xfs_rtallocate_extent.
2661 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2662 do_div(ap->rval, mp->m_sb.sb_rextsize);
2665 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2666 &ralen, atype, ap->wasdel, prod, &rtb)))
2668 if (rtb == NULLFSBLOCK && prod > 1 &&
2669 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2670 ap->alen, &ralen, atype,
2671 ap->wasdel, 1, &rtb)))
2674 if (ap->rval != NULLFSBLOCK) {
2675 ap->rval *= mp->m_sb.sb_rextsize;
2676 ralen *= mp->m_sb.sb_rextsize;
2678 ap->ip->i_d.di_nblocks += ralen;
2679 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2681 ap->ip->i_delayed_blks -= ralen;
2683 * Adjust the disk quota also. This was reserved
2686 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2687 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2688 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2697 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2699 xfs_mount_t *mp; /* mount point structure */
2700 xfs_alloctype_t atype = 0; /* type for allocation routines */
2701 xfs_extlen_t align; /* minimum allocation alignment */
2703 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2704 xfs_agnumber_t startag;
2705 xfs_alloc_arg_t args;
2708 xfs_extlen_t longest;
2710 xfs_extlen_t nextminlen = 0;
2712 int nullfb; /* true if ap->firstblock isn't set */
2718 mp = ap->ip->i_mount;
2719 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2720 if (unlikely(align)) {
2721 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2722 align, 0, ap->eof, 0, ap->conv,
2723 &ap->off, &ap->alen);
2727 nullfb = ap->firstblock == NULLFSBLOCK;
2728 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2730 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2731 ag = xfs_filestream_lookup_ag(ap->ip);
2732 ag = (ag != NULLAGNUMBER) ? ag : 0;
2733 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2735 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2738 ap->rval = ap->firstblock;
2740 xfs_bmap_adjacent(ap);
2743 * If allowed, use ap->rval; otherwise must use firstblock since
2744 * it's in the right allocation group.
2746 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2749 ap->rval = ap->firstblock;
2751 * Normal allocation, done through xfs_alloc_vextent.
2753 tryagain = isaligned = 0;
2756 args.fsbno = ap->rval;
2757 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2758 args.firstblock = ap->firstblock;
2761 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2762 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2764 args.type = XFS_ALLOCTYPE_START_BNO;
2765 args.total = ap->total;
2768 * Search for an allocation group with a single extent
2769 * large enough for the request.
2771 * If one isn't found, then adjust the minimum allocation
2772 * size to the largest space found.
2774 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2775 if (startag == NULLAGNUMBER)
2778 down_read(&mp->m_peraglock);
2779 while (blen < ap->alen) {
2780 pag = &mp->m_perag[ag];
2781 if (!pag->pagf_init &&
2782 (error = xfs_alloc_pagf_init(mp, args.tp,
2783 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2784 up_read(&mp->m_peraglock);
2788 * See xfs_alloc_fix_freelist...
2790 if (pag->pagf_init) {
2791 need = XFS_MIN_FREELIST_PAG(pag, mp);
2792 delta = need > pag->pagf_flcount ?
2793 need - pag->pagf_flcount : 0;
2794 longest = (pag->pagf_longest > delta) ?
2795 (pag->pagf_longest - delta) :
2796 (pag->pagf_flcount > 0 ||
2797 pag->pagf_longest > 0);
2803 if (xfs_inode_is_filestream(ap->ip)) {
2804 if (blen >= ap->alen)
2809 * If startag is an invalid AG, we've
2810 * come here once before and
2811 * xfs_filestream_new_ag picked the
2812 * best currently available.
2814 * Don't continue looping, since we
2815 * could loop forever.
2817 if (startag == NULLAGNUMBER)
2820 error = xfs_filestream_new_ag(ap, &ag);
2822 up_read(&mp->m_peraglock);
2826 /* loop again to set 'blen'*/
2827 startag = NULLAGNUMBER;
2831 if (++ag == mp->m_sb.sb_agcount)
2836 up_read(&mp->m_peraglock);
2838 * Since the above loop did a BUF_TRYLOCK, it is
2839 * possible that there is space for this request.
2841 if (notinit || blen < ap->minlen)
2842 args.minlen = ap->minlen;
2844 * If the best seen length is less than the request
2845 * length, use the best as the minimum.
2847 else if (blen < ap->alen)
2850 * Otherwise we've seen an extent as big as alen,
2851 * use that as the minimum.
2854 args.minlen = ap->alen;
2857 * set the failure fallback case to look in the selected
2858 * AG as the stream may have moved.
2860 if (xfs_inode_is_filestream(ap->ip))
2861 ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2862 } else if (ap->low) {
2863 if (xfs_inode_is_filestream(ap->ip))
2864 args.type = XFS_ALLOCTYPE_FIRST_AG;
2866 args.type = XFS_ALLOCTYPE_START_BNO;
2867 args.total = args.minlen = ap->minlen;
2869 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2870 args.total = ap->total;
2871 args.minlen = ap->minlen;
2873 /* apply extent size hints if obtained earlier */
2874 if (unlikely(align)) {
2876 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2877 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2878 } else if (mp->m_sb.sb_blocksize >= NBPP) {
2882 args.prod = NBPP >> mp->m_sb.sb_blocklog;
2883 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2884 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2887 * If we are not low on available data blocks, and the
2888 * underlying logical volume manager is a stripe, and
2889 * the file offset is zero then try to allocate data
2890 * blocks on stripe unit boundary.
2891 * NOTE: ap->aeof is only set if the allocation length
2892 * is >= the stripe unit and the allocation offset is
2893 * at the end of file.
2895 if (!ap->low && ap->aeof) {
2897 args.alignment = mp->m_dalign;
2901 * Adjust for alignment
2903 if (blen > args.alignment && blen <= ap->alen)
2904 args.minlen = blen - args.alignment;
2905 args.minalignslop = 0;
2908 * First try an exact bno allocation.
2909 * If it fails then do a near or start bno
2910 * allocation with alignment turned on.
2914 args.type = XFS_ALLOCTYPE_THIS_BNO;
2917 * Compute the minlen+alignment for the
2918 * next case. Set slop so that the value
2919 * of minlen+alignment+slop doesn't go up
2920 * between the calls.
2922 if (blen > mp->m_dalign && blen <= ap->alen)
2923 nextminlen = blen - mp->m_dalign;
2925 nextminlen = args.minlen;
2926 if (nextminlen + mp->m_dalign > args.minlen + 1)
2928 nextminlen + mp->m_dalign -
2931 args.minalignslop = 0;
2935 args.minalignslop = 0;
2937 args.minleft = ap->minleft;
2938 args.wasdel = ap->wasdel;
2940 args.userdata = ap->userdata;
2941 if ((error = xfs_alloc_vextent(&args)))
2943 if (tryagain && args.fsbno == NULLFSBLOCK) {
2945 * Exact allocation failed. Now try with alignment
2949 args.fsbno = ap->rval;
2950 args.alignment = mp->m_dalign;
2951 args.minlen = nextminlen;
2952 args.minalignslop = 0;
2954 if ((error = xfs_alloc_vextent(&args)))
2957 if (isaligned && args.fsbno == NULLFSBLOCK) {
2959 * allocation failed, so turn off alignment and
2963 args.fsbno = ap->rval;
2965 if ((error = xfs_alloc_vextent(&args)))
2968 if (args.fsbno == NULLFSBLOCK && nullfb &&
2969 args.minlen > ap->minlen) {
2970 args.minlen = ap->minlen;
2971 args.type = XFS_ALLOCTYPE_START_BNO;
2972 args.fsbno = ap->rval;
2973 if ((error = xfs_alloc_vextent(&args)))
2976 if (args.fsbno == NULLFSBLOCK && nullfb) {
2978 args.type = XFS_ALLOCTYPE_FIRST_AG;
2979 args.total = ap->minlen;
2981 if ((error = xfs_alloc_vextent(&args)))
2985 if (args.fsbno != NULLFSBLOCK) {
2986 ap->firstblock = ap->rval = args.fsbno;
2987 ASSERT(nullfb || fb_agno == args.agno ||
2988 (ap->low && fb_agno < args.agno));
2989 ap->alen = args.len;
2990 ap->ip->i_d.di_nblocks += args.len;
2991 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2993 ap->ip->i_delayed_blks -= args.len;
2995 * Adjust the disk quota also. This was reserved
2998 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2999 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3000 XFS_TRANS_DQ_BCOUNT,
3003 ap->rval = NULLFSBLOCK;
3010 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3011 * It figures out where to ask the underlying allocator to put the new extent.
3015 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
3017 if ((ap->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) && ap->userdata)
3018 return xfs_bmap_rtalloc(ap);
3019 return xfs_bmap_btalloc(ap);
3023 * Transform a btree format file with only one leaf node, where the
3024 * extents list will fit in the inode, into an extents format file.
3025 * Since the file extents are already in-core, all we have to do is
3026 * give up the space for the btree root and pitch the leaf block.
3028 STATIC int /* error */
3029 xfs_bmap_btree_to_extents(
3030 xfs_trans_t *tp, /* transaction pointer */
3031 xfs_inode_t *ip, /* incore inode pointer */
3032 xfs_btree_cur_t *cur, /* btree cursor */
3033 int *logflagsp, /* inode logging flags */
3034 int whichfork) /* data or attr fork */
3037 xfs_bmbt_block_t *cblock;/* child btree block */
3038 xfs_fsblock_t cbno; /* child block number */
3039 xfs_buf_t *cbp; /* child block's buffer */
3040 int error; /* error return value */
3041 xfs_ifork_t *ifp; /* inode fork data */
3042 xfs_mount_t *mp; /* mount point structure */
3043 __be64 *pp; /* ptr to block address */
3044 xfs_bmbt_block_t *rblock;/* root btree block */
3046 ifp = XFS_IFORK_PTR(ip, whichfork);
3047 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3048 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
3049 rblock = ifp->if_broot;
3050 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
3051 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
3052 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
3054 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
3055 cbno = be64_to_cpu(*pp);
3058 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
3061 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
3062 XFS_BMAP_BTREE_REF)))
3064 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
3065 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
3067 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
3068 ip->i_d.di_nblocks--;
3069 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
3070 xfs_trans_binval(tp, cbp);
3071 if (cur->bc_bufs[0] == cbp)
3072 cur->bc_bufs[0] = NULL;
3073 xfs_iroot_realloc(ip, -1, whichfork);
3074 ASSERT(ifp->if_broot == NULL);
3075 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
3076 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3077 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
3082 * Called by xfs_bmapi to update file extent records and the btree
3083 * after removing space (or undoing a delayed allocation).
3085 STATIC int /* error */
3086 xfs_bmap_del_extent(
3087 xfs_inode_t *ip, /* incore inode pointer */
3088 xfs_trans_t *tp, /* current transaction pointer */
3089 xfs_extnum_t idx, /* extent number to update/delete */
3090 xfs_bmap_free_t *flist, /* list of extents to be freed */
3091 xfs_btree_cur_t *cur, /* if null, not a btree */
3092 xfs_bmbt_irec_t *del, /* data to remove from extents */
3093 int *logflagsp, /* inode logging flags */
3094 xfs_extdelta_t *delta, /* Change made to incore extents */
3095 int whichfork, /* data or attr fork */
3096 int rsvd) /* OK to allocate reserved blocks */
3098 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
3099 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
3100 xfs_fsblock_t del_endblock=0; /* first block past del */
3101 xfs_fileoff_t del_endoff; /* first offset past del */
3102 int delay; /* current block is delayed allocated */
3103 int do_fx; /* free extent at end of routine */
3104 xfs_bmbt_rec_t *ep; /* current extent entry pointer */
3105 int error; /* error return value */
3106 int flags; /* inode logging flags */
3107 #ifdef XFS_BMAP_TRACE
3108 static char fname[] = "xfs_bmap_del_extent";
3110 xfs_bmbt_irec_t got; /* current extent entry */
3111 xfs_fileoff_t got_endoff; /* first offset past got */
3112 int i; /* temp state */
3113 xfs_ifork_t *ifp; /* inode fork pointer */
3114 xfs_mount_t *mp; /* mount structure */
3115 xfs_filblks_t nblks; /* quota/sb block count */
3116 xfs_bmbt_irec_t new; /* new record to be inserted */
3118 uint qfield; /* quota field to update */
3119 xfs_filblks_t temp; /* for indirect length calculations */
3120 xfs_filblks_t temp2; /* for indirect length calculations */
3122 XFS_STATS_INC(xs_del_exlist);
3124 ifp = XFS_IFORK_PTR(ip, whichfork);
3125 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
3126 (uint)sizeof(xfs_bmbt_rec_t)));
3127 ASSERT(del->br_blockcount > 0);
3128 ep = xfs_iext_get_ext(ifp, idx);
3129 xfs_bmbt_get_all(ep, &got);
3130 ASSERT(got.br_startoff <= del->br_startoff);
3131 del_endoff = del->br_startoff + del->br_blockcount;
3132 got_endoff = got.br_startoff + got.br_blockcount;
3133 ASSERT(got_endoff >= del_endoff);
3134 delay = ISNULLSTARTBLOCK(got.br_startblock);
3135 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
3140 * If deleting a real allocation, must free up the disk space.
3143 flags = XFS_ILOG_CORE;
3145 * Realtime allocation. Free it and record di_nblocks update.
3147 if (whichfork == XFS_DATA_FORK &&
3148 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
3152 ASSERT(do_mod(del->br_blockcount,
3153 mp->m_sb.sb_rextsize) == 0);
3154 ASSERT(do_mod(del->br_startblock,
3155 mp->m_sb.sb_rextsize) == 0);
3156 bno = del->br_startblock;
3157 len = del->br_blockcount;
3158 do_div(bno, mp->m_sb.sb_rextsize);
3159 do_div(len, mp->m_sb.sb_rextsize);
3160 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3161 (xfs_extlen_t)len)))
3164 nblks = len * mp->m_sb.sb_rextsize;
3165 qfield = XFS_TRANS_DQ_RTBCOUNT;
3168 * Ordinary allocation.
3172 nblks = del->br_blockcount;
3173 qfield = XFS_TRANS_DQ_BCOUNT;
3176 * Set up del_endblock and cur for later.
3178 del_endblock = del->br_startblock + del->br_blockcount;
3180 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3181 got.br_startblock, got.br_blockcount,
3186 da_old = da_new = 0;
3188 da_old = STARTBLOCKVAL(got.br_startblock);
3194 * Set flag value to use in switch statement.
3195 * Left-contig is 2, right-contig is 1.
3197 switch (((got.br_startoff == del->br_startoff) << 1) |
3198 (got_endoff == del_endoff)) {
3201 * Matches the whole extent. Delete the entry.
3203 xfs_bmap_trace_delete(fname, "3", ip, idx, 1, whichfork);
3204 xfs_iext_remove(ifp, idx, 1);
3205 ifp->if_lastex = idx;
3208 XFS_IFORK_NEXT_SET(ip, whichfork,
3209 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3210 flags |= XFS_ILOG_CORE;
3212 flags |= XFS_ILOG_FEXT(whichfork);
3215 if ((error = xfs_bmbt_delete(cur, &i)))
3222 * Deleting the first part of the extent.
3224 xfs_bmap_trace_pre_update(fname, "2", ip, idx, whichfork);
3225 xfs_bmbt_set_startoff(ep, del_endoff);
3226 temp = got.br_blockcount - del->br_blockcount;
3227 xfs_bmbt_set_blockcount(ep, temp);
3228 ifp->if_lastex = idx;
3230 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3232 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3233 xfs_bmap_trace_post_update(fname, "2", ip, idx,
3238 xfs_bmbt_set_startblock(ep, del_endblock);
3239 xfs_bmap_trace_post_update(fname, "2", ip, idx, whichfork);
3241 flags |= XFS_ILOG_FEXT(whichfork);
3244 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3245 got.br_blockcount - del->br_blockcount,
3252 * Deleting the last part of the extent.
3254 temp = got.br_blockcount - del->br_blockcount;
3255 xfs_bmap_trace_pre_update(fname, "1", ip, idx, whichfork);
3256 xfs_bmbt_set_blockcount(ep, temp);
3257 ifp->if_lastex = idx;
3259 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3261 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3262 xfs_bmap_trace_post_update(fname, "1", ip, idx,
3267 xfs_bmap_trace_post_update(fname, "1", ip, idx, whichfork);
3269 flags |= XFS_ILOG_FEXT(whichfork);
3272 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3274 got.br_blockcount - del->br_blockcount,
3281 * Deleting the middle of the extent.
3283 temp = del->br_startoff - got.br_startoff;
3284 xfs_bmap_trace_pre_update(fname, "0", ip, idx, whichfork);
3285 xfs_bmbt_set_blockcount(ep, temp);
3286 new.br_startoff = del_endoff;
3287 temp2 = got_endoff - del_endoff;
3288 new.br_blockcount = temp2;
3289 new.br_state = got.br_state;
3291 new.br_startblock = del_endblock;
3292 flags |= XFS_ILOG_CORE;
3294 if ((error = xfs_bmbt_update(cur,
3296 got.br_startblock, temp,
3299 if ((error = xfs_bmbt_increment(cur, 0, &i)))
3301 cur->bc_rec.b = new;
3302 error = xfs_bmbt_insert(cur, &i);
3303 if (error && error != ENOSPC)
3306 * If get no-space back from btree insert,
3307 * it tried a split, and we have a zero
3308 * block reservation.
3309 * Fix up our state and return the error.
3311 if (error == ENOSPC) {
3313 * Reset the cursor, don't trust
3314 * it after any insert operation.
3316 if ((error = xfs_bmbt_lookup_eq(cur,
3323 * Update the btree record back
3324 * to the original value.
3326 if ((error = xfs_bmbt_update(cur,
3333 * Reset the extent record back
3334 * to the original value.
3336 xfs_bmbt_set_blockcount(ep,
3339 error = XFS_ERROR(ENOSPC);
3344 flags |= XFS_ILOG_FEXT(whichfork);
3345 XFS_IFORK_NEXT_SET(ip, whichfork,
3346 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3348 ASSERT(whichfork == XFS_DATA_FORK);
3349 temp = xfs_bmap_worst_indlen(ip, temp);
3350 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3351 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3352 new.br_startblock = NULLSTARTBLOCK((int)temp2);
3353 da_new = temp + temp2;
3354 while (da_new > da_old) {
3358 xfs_bmbt_set_startblock(ep,
3359 NULLSTARTBLOCK((int)temp));
3361 if (da_new == da_old)
3367 NULLSTARTBLOCK((int)temp2);
3371 xfs_bmap_trace_post_update(fname, "0", ip, idx, whichfork);
3372 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 1, &new, NULL,
3374 xfs_iext_insert(ifp, idx + 1, 1, &new);
3375 ifp->if_lastex = idx + 1;
3379 * If we need to, add to list of extents to delete.
3382 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3385 * Adjust inode # blocks in the file.
3388 ip->i_d.di_nblocks -= nblks;
3390 * Adjust quota data.
3393 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
3396 * Account for change in delayed indirect blocks.
3397 * Nothing to do for disk quota accounting here.
3399 ASSERT(da_old >= da_new);
3400 if (da_old > da_new)
3401 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
3404 /* DELTA: report the original extent. */
3405 if (delta->xed_startoff > got.br_startoff)
3406 delta->xed_startoff = got.br_startoff;
3407 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3408 delta->xed_blockcount = got.br_startoff +
3417 * Remove the entry "free" from the free item list. Prev points to the
3418 * previous entry, unless "free" is the head of the list.
3422 xfs_bmap_free_t *flist, /* free item list header */
3423 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3424 xfs_bmap_free_item_t *free) /* list item to be freed */
3427 prev->xbfi_next = free->xbfi_next;
3429 flist->xbf_first = free->xbfi_next;
3431 kmem_zone_free(xfs_bmap_free_item_zone, free);
3435 * Convert an extents-format file into a btree-format file.
3436 * The new file will have a root block (in the inode) and a single child block.
3438 STATIC int /* error */
3439 xfs_bmap_extents_to_btree(
3440 xfs_trans_t *tp, /* transaction pointer */
3441 xfs_inode_t *ip, /* incore inode pointer */
3442 xfs_fsblock_t *firstblock, /* first-block-allocated */
3443 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3444 xfs_btree_cur_t **curp, /* cursor returned to caller */
3445 int wasdel, /* converting a delayed alloc */
3446 int *logflagsp, /* inode logging flags */
3447 int whichfork) /* data or attr fork */
3449 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
3450 xfs_buf_t *abp; /* buffer for ablock */
3451 xfs_alloc_arg_t args; /* allocation arguments */
3452 xfs_bmbt_rec_t *arp; /* child record pointer */
3453 xfs_bmbt_block_t *block; /* btree root block */
3454 xfs_btree_cur_t *cur; /* bmap btree cursor */
3455 xfs_bmbt_rec_t *ep; /* extent record pointer */
3456 int error; /* error return value */
3457 xfs_extnum_t i, cnt; /* extent record index */
3458 xfs_ifork_t *ifp; /* inode fork pointer */
3459 xfs_bmbt_key_t *kp; /* root block key pointer */
3460 xfs_mount_t *mp; /* mount structure */
3461 xfs_extnum_t nextents; /* number of file extents */
3462 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3464 ifp = XFS_IFORK_PTR(ip, whichfork);
3465 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3466 ASSERT(ifp->if_ext_max ==
3467 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3469 * Make space in the inode incore.
3471 xfs_iroot_realloc(ip, 1, whichfork);
3472 ifp->if_flags |= XFS_IFBROOT;
3476 block = ifp->if_broot;
3477 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3478 block->bb_level = cpu_to_be16(1);
3479 block->bb_numrecs = cpu_to_be16(1);
3480 block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3481 block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3483 * Need a cursor. Can't allocate until bb_level is filled in.
3486 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
3488 cur->bc_private.b.firstblock = *firstblock;
3489 cur->bc_private.b.flist = flist;
3490 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3492 * Convert to a btree with two levels, one record in root.
3494 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3497 args.firstblock = *firstblock;
3498 if (*firstblock == NULLFSBLOCK) {
3499 args.type = XFS_ALLOCTYPE_START_BNO;
3500 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3501 } else if (flist->xbf_low) {
3502 args.type = XFS_ALLOCTYPE_START_BNO;
3503 args.fsbno = *firstblock;
3505 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3506 args.fsbno = *firstblock;
3508 args.minlen = args.maxlen = args.prod = 1;
3509 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3510 args.minalignslop = 0;
3511 args.wasdel = wasdel;
3513 if ((error = xfs_alloc_vextent(&args))) {
3514 xfs_iroot_realloc(ip, -1, whichfork);
3515 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3519 * Allocation can't fail, the space was reserved.
3521 ASSERT(args.fsbno != NULLFSBLOCK);
3522 ASSERT(*firstblock == NULLFSBLOCK ||
3523 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3525 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3526 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3527 cur->bc_private.b.allocated++;
3528 ip->i_d.di_nblocks++;
3529 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3530 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3532 * Fill in the child block.
3534 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
3535 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3536 ablock->bb_level = 0;
3537 ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3538 ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3539 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3540 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3541 for (cnt = i = 0; i < nextents; i++) {
3542 ep = xfs_iext_get_ext(ifp, i);
3543 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
3544 arp->l0 = INT_GET(ep->l0, ARCH_CONVERT);
3545 arp->l1 = INT_GET(ep->l1, ARCH_CONVERT);
3549 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3550 ablock->bb_numrecs = cpu_to_be16(cnt);
3552 * Fill in the root key and pointer.
3554 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
3555 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3556 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3557 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
3558 *pp = cpu_to_be64(args.fsbno);
3560 * Do all this logging at the end so that
3561 * the root is at the right level.
3563 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
3564 xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3565 ASSERT(*curp == NULL);
3567 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
3572 * Helper routine to reset inode di_forkoff field when switching
3573 * attribute fork from local to extent format - we reset it where
3574 * possible to make space available for inline data fork extents.
3577 xfs_bmap_forkoff_reset(
3582 if (whichfork == XFS_ATTR_FORK &&
3583 (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
3584 (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
3585 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
3586 ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
3587 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3588 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
3589 (uint)sizeof(xfs_bmbt_rec_t);
3590 ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
3591 (uint)sizeof(xfs_bmbt_rec_t);
3596 * Convert a local file to an extents file.
3597 * This code is out of bounds for data forks of regular files,
3598 * since the file data needs to get logged so things will stay consistent.
3599 * (The bmap-level manipulations are ok, though).
3601 STATIC int /* error */
3602 xfs_bmap_local_to_extents(
3603 xfs_trans_t *tp, /* transaction pointer */
3604 xfs_inode_t *ip, /* incore inode pointer */
3605 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3606 xfs_extlen_t total, /* total blocks needed by transaction */
3607 int *logflagsp, /* inode logging flags */
3608 int whichfork) /* data or attr fork */
3610 int error; /* error return value */
3611 int flags; /* logging flags returned */
3612 #ifdef XFS_BMAP_TRACE
3613 static char fname[] = "xfs_bmap_local_to_extents";
3615 xfs_ifork_t *ifp; /* inode fork pointer */
3618 * We don't want to deal with the case of keeping inode data inline yet.
3619 * So sending the data fork of a regular inode is invalid.
3621 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3622 whichfork == XFS_DATA_FORK));
3623 ifp = XFS_IFORK_PTR(ip, whichfork);
3624 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3627 if (ifp->if_bytes) {
3628 xfs_alloc_arg_t args; /* allocation arguments */
3629 xfs_buf_t *bp; /* buffer for extent block */
3630 xfs_bmbt_rec_t *ep; /* extent record pointer */
3633 args.mp = ip->i_mount;
3634 args.firstblock = *firstblock;
3635 ASSERT((ifp->if_flags &
3636 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3638 * Allocate a block. We know we need only one, since the
3639 * file currently fits in an inode.
3641 if (*firstblock == NULLFSBLOCK) {
3642 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3643 args.type = XFS_ALLOCTYPE_START_BNO;
3645 args.fsbno = *firstblock;
3646 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3649 args.mod = args.minleft = args.alignment = args.wasdel =
3650 args.isfl = args.minalignslop = 0;
3651 args.minlen = args.maxlen = args.prod = 1;
3652 if ((error = xfs_alloc_vextent(&args)))
3655 * Can't fail, the space was reserved.
3657 ASSERT(args.fsbno != NULLFSBLOCK);
3658 ASSERT(args.len == 1);
3659 *firstblock = args.fsbno;
3660 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3661 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3663 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3664 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3665 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3666 xfs_iext_add(ifp, 0, 1);
3667 ep = xfs_iext_get_ext(ifp, 0);
3668 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3669 xfs_bmap_trace_post_update(fname, "new", ip, 0, whichfork);
3670 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3671 ip->i_d.di_nblocks = 1;
3672 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
3673 XFS_TRANS_DQ_BCOUNT, 1L);
3674 flags |= XFS_ILOG_FEXT(whichfork);
3676 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3677 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3679 ifp->if_flags &= ~XFS_IFINLINE;
3680 ifp->if_flags |= XFS_IFEXTENTS;
3681 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3682 flags |= XFS_ILOG_CORE;
3689 * Search the extent records for the entry containing block bno.
3690 * If bno lies in a hole, point to the next entry. If bno lies
3691 * past eof, *eofp will be set, and *prevp will contain the last
3692 * entry (null if none). Else, *lastxp will be set to the index
3693 * of the found entry; *gotp will contain the entry.
3695 xfs_bmbt_rec_t * /* pointer to found extent entry */
3696 xfs_bmap_search_multi_extents(
3697 xfs_ifork_t *ifp, /* inode fork pointer */
3698 xfs_fileoff_t bno, /* block number searched for */
3699 int *eofp, /* out: end of file found */
3700 xfs_extnum_t *lastxp, /* out: last extent index */
3701 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3702 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3704 xfs_bmbt_rec_t *ep; /* extent record pointer */
3705 xfs_extnum_t lastx; /* last extent index */
3708 * Initialize the extent entry structure to catch access to
3709 * uninitialized br_startblock field.
3711 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3712 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3713 gotp->br_state = XFS_EXT_INVALID;
3715 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3717 gotp->br_startblock = 0xffffa5a5;
3719 prevp->br_startoff = NULLFILEOFF;
3721 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3723 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3725 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3726 xfs_bmbt_get_all(ep, gotp);
3740 * Search the extents list for the inode, for the extent containing bno.
3741 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3742 * *eofp will be set, and *prevp will contain the last entry (null if none).
3743 * Else, *lastxp will be set to the index of the found
3744 * entry; *gotp will contain the entry.
3746 STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
3747 xfs_bmap_search_extents(
3748 xfs_inode_t *ip, /* incore inode pointer */
3749 xfs_fileoff_t bno, /* block number searched for */
3750 int fork, /* data or attr fork */
3751 int *eofp, /* out: end of file found */
3752 xfs_extnum_t *lastxp, /* out: last extent index */
3753 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3754 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3756 xfs_ifork_t *ifp; /* inode fork pointer */
3757 xfs_bmbt_rec_t *ep; /* extent record pointer */
3759 XFS_STATS_INC(xs_look_exlist);
3760 ifp = XFS_IFORK_PTR(ip, fork);
3762 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3764 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3765 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3766 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3767 "Access to block zero in inode %llu "
3768 "start_block: %llx start_off: %llx "
3769 "blkcnt: %llx extent-state: %x lastx: %x\n",
3770 (unsigned long long)ip->i_ino,
3771 (unsigned long long)gotp->br_startblock,
3772 (unsigned long long)gotp->br_startoff,
3773 (unsigned long long)gotp->br_blockcount,
3774 gotp->br_state, *lastxp);
3775 *lastxp = NULLEXTNUM;
3783 #ifdef XFS_BMAP_TRACE
3784 ktrace_t *xfs_bmap_trace_buf;
3787 * Add a bmap trace buffer entry. Base routine for the others.
3790 xfs_bmap_trace_addentry(
3791 int opcode, /* operation */
3792 char *fname, /* function name */
3793 char *desc, /* operation description */
3794 xfs_inode_t *ip, /* incore inode pointer */
3795 xfs_extnum_t idx, /* index of entry(ies) */
3796 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
3797 xfs_bmbt_rec_t *r1, /* first record */
3798 xfs_bmbt_rec_t *r2, /* second record or null */
3799 int whichfork) /* data or attr fork */
3803 ASSERT(cnt == 1 || cnt == 2);
3808 memset(&tr2, 0, sizeof(tr2));
3811 ktrace_enter(xfs_bmap_trace_buf,
3812 (void *)(__psint_t)(opcode | (whichfork << 16)),
3813 (void *)fname, (void *)desc, (void *)ip,
3814 (void *)(__psint_t)idx,
3815 (void *)(__psint_t)cnt,
3816 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3817 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3818 (void *)(__psunsigned_t)(r1->l0 >> 32),
3819 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3820 (void *)(__psunsigned_t)(r1->l1 >> 32),
3821 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3822 (void *)(__psunsigned_t)(r2->l0 >> 32),
3823 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3824 (void *)(__psunsigned_t)(r2->l1 >> 32),
3825 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3827 ASSERT(ip->i_xtrace);
3828 ktrace_enter(ip->i_xtrace,
3829 (void *)(__psint_t)(opcode | (whichfork << 16)),
3830 (void *)fname, (void *)desc, (void *)ip,
3831 (void *)(__psint_t)idx,
3832 (void *)(__psint_t)cnt,
3833 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3834 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3835 (void *)(__psunsigned_t)(r1->l0 >> 32),
3836 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3837 (void *)(__psunsigned_t)(r1->l1 >> 32),
3838 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3839 (void *)(__psunsigned_t)(r2->l0 >> 32),
3840 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3841 (void *)(__psunsigned_t)(r2->l1 >> 32),
3842 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3847 * Add bmap trace entry prior to a call to xfs_iext_remove.
3850 xfs_bmap_trace_delete(
3851 char *fname, /* function name */
3852 char *desc, /* operation description */
3853 xfs_inode_t *ip, /* incore inode pointer */
3854 xfs_extnum_t idx, /* index of entry(entries) deleted */
3855 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3856 int whichfork) /* data or attr fork */
3858 xfs_ifork_t *ifp; /* inode fork pointer */
3860 ifp = XFS_IFORK_PTR(ip, whichfork);
3861 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
3862 cnt, xfs_iext_get_ext(ifp, idx),
3863 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
3868 * Add bmap trace entry prior to a call to xfs_iext_insert, or
3869 * reading in the extents list from the disk (in the btree).
3872 xfs_bmap_trace_insert(
3873 char *fname, /* function name */
3874 char *desc, /* operation description */
3875 xfs_inode_t *ip, /* incore inode pointer */
3876 xfs_extnum_t idx, /* index of entry(entries) inserted */
3877 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3878 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3879 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3880 int whichfork) /* data or attr fork */
3882 xfs_bmbt_rec_t tr1; /* compressed record 1 */
3883 xfs_bmbt_rec_t tr2; /* compressed record 2 if needed */
3885 xfs_bmbt_set_all(&tr1, r1);
3888 xfs_bmbt_set_all(&tr2, r2);
3893 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3894 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3898 * Add bmap trace entry after updating an extent record in place.
3901 xfs_bmap_trace_post_update(
3902 char *fname, /* function name */
3903 char *desc, /* operation description */
3904 xfs_inode_t *ip, /* incore inode pointer */
3905 xfs_extnum_t idx, /* index of entry updated */
3906 int whichfork) /* data or attr fork */
3908 xfs_ifork_t *ifp; /* inode fork pointer */
3910 ifp = XFS_IFORK_PTR(ip, whichfork);
3911 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
3912 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3916 * Add bmap trace entry prior to updating an extent record in place.
3919 xfs_bmap_trace_pre_update(
3920 char *fname, /* function name */
3921 char *desc, /* operation description */
3922 xfs_inode_t *ip, /* incore inode pointer */
3923 xfs_extnum_t idx, /* index of entry to be updated */
3924 int whichfork) /* data or attr fork */
3926 xfs_ifork_t *ifp; /* inode fork pointer */
3928 ifp = XFS_IFORK_PTR(ip, whichfork);
3929 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
3930 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3932 #endif /* XFS_BMAP_TRACE */
3935 * Compute the worst-case number of indirect blocks that will be used
3936 * for ip's delayed extent of length "len".
3938 STATIC xfs_filblks_t
3939 xfs_bmap_worst_indlen(
3940 xfs_inode_t *ip, /* incore inode pointer */
3941 xfs_filblks_t len) /* delayed extent length */
3943 int level; /* btree level number */
3944 int maxrecs; /* maximum record count at this level */
3945 xfs_mount_t *mp; /* mount structure */
3946 xfs_filblks_t rval; /* return value */
3949 maxrecs = mp->m_bmap_dmxr[0];
3950 for (level = 0, rval = 0;
3951 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3954 do_div(len, maxrecs);
3957 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3960 maxrecs = mp->m_bmap_dmxr[1];
3965 #if defined(XFS_RW_TRACE)
3974 if (ip->i_rwtrace == NULL)
3976 ktrace_enter(ip->i_rwtrace,
3977 (void *)(__psint_t)XFS_BUNMAP,
3979 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3980 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3981 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3982 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3983 (void *)(__psint_t)len,
3984 (void *)(__psint_t)flags,
3985 (void *)(unsigned long)current_cpu(),
3997 * Convert inode from non-attributed to attributed.
3998 * Must not be in a transaction, ip must not be locked.
4000 int /* error code */
4001 xfs_bmap_add_attrfork(
4002 xfs_inode_t *ip, /* incore inode pointer */
4003 int size, /* space new attribute needs */
4004 int rsvd) /* xact may use reserved blks */
4006 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
4007 xfs_bmap_free_t flist; /* freed extent records */
4008 xfs_mount_t *mp; /* mount structure */
4009 xfs_trans_t *tp; /* transaction pointer */
4010 unsigned long s; /* spinlock spl value */
4011 int blks; /* space reservation */
4012 int version = 1; /* superblock attr version */
4013 int committed; /* xaction was committed */
4014 int logflags; /* logging flags */
4015 int error; /* error return value */
4017 ASSERT(XFS_IFORK_Q(ip) == 0);
4018 ASSERT(ip->i_df.if_ext_max ==
4019 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4022 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
4023 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
4024 blks = XFS_ADDAFORK_SPACE_RES(mp);
4026 tp->t_flags |= XFS_TRANS_RESERVE;
4027 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
4028 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
4030 xfs_ilock(ip, XFS_ILOCK_EXCL);
4031 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
4032 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
4033 XFS_QMOPT_RES_REGBLKS);
4035 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4036 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
4039 if (XFS_IFORK_Q(ip))
4041 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
4043 * For inodes coming from pre-6.2 filesystems.
4045 ASSERT(ip->i_d.di_aformat == 0);
4046 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
4048 ASSERT(ip->i_d.di_anextents == 0);
4049 VN_HOLD(XFS_ITOV(ip));
4050 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4051 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4052 switch (ip->i_d.di_format) {
4053 case XFS_DINODE_FMT_DEV:
4054 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
4056 case XFS_DINODE_FMT_UUID:
4057 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
4059 case XFS_DINODE_FMT_LOCAL:
4060 case XFS_DINODE_FMT_EXTENTS:
4061 case XFS_DINODE_FMT_BTREE:
4062 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
4063 if (!ip->i_d.di_forkoff)
4064 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
4065 else if (mp->m_flags & XFS_MOUNT_ATTR2)
4070 error = XFS_ERROR(EINVAL);
4073 ip->i_df.if_ext_max =
4074 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4075 ASSERT(ip->i_afp == NULL);
4076 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
4077 ip->i_afp->if_ext_max =
4078 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4079 ip->i_afp->if_flags = XFS_IFEXTENTS;
4081 XFS_BMAP_INIT(&flist, &firstblock);
4082 switch (ip->i_d.di_format) {
4083 case XFS_DINODE_FMT_LOCAL:
4084 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
4087 case XFS_DINODE_FMT_EXTENTS:
4088 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
4091 case XFS_DINODE_FMT_BTREE:
4092 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
4100 xfs_trans_log_inode(tp, ip, logflags);
4103 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb) ||
4104 (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2)) {
4105 __int64_t sbfields = 0;
4107 s = XFS_SB_LOCK(mp);
4108 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) {
4109 XFS_SB_VERSION_ADDATTR(&mp->m_sb);
4110 sbfields |= XFS_SB_VERSIONNUM;
4112 if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2) {
4113 XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
4114 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
4117 XFS_SB_UNLOCK(mp, s);
4118 xfs_mod_sb(tp, sbfields);
4120 XFS_SB_UNLOCK(mp, s);
4122 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
4124 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
4125 ASSERT(ip->i_df.if_ext_max ==
4126 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4129 xfs_bmap_cancel(&flist);
4131 ASSERT(ismrlocked(&ip->i_lock,MR_UPDATE));
4132 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4134 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4135 ASSERT(ip->i_df.if_ext_max ==
4136 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4141 * Add the extent to the list of extents to be free at transaction end.
4142 * The list is maintained sorted (by block number).
4147 xfs_fsblock_t bno, /* fs block number of extent */
4148 xfs_filblks_t len, /* length of extent */
4149 xfs_bmap_free_t *flist, /* list of extents */
4150 xfs_mount_t *mp) /* mount point structure */
4152 xfs_bmap_free_item_t *cur; /* current (next) element */
4153 xfs_bmap_free_item_t *new; /* new element */
4154 xfs_bmap_free_item_t *prev; /* previous element */
4156 xfs_agnumber_t agno;
4157 xfs_agblock_t agbno;
4159 ASSERT(bno != NULLFSBLOCK);
4161 ASSERT(len <= MAXEXTLEN);
4162 ASSERT(!ISNULLSTARTBLOCK(bno));
4163 agno = XFS_FSB_TO_AGNO(mp, bno);
4164 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4165 ASSERT(agno < mp->m_sb.sb_agcount);
4166 ASSERT(agbno < mp->m_sb.sb_agblocks);
4167 ASSERT(len < mp->m_sb.sb_agblocks);
4168 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4170 ASSERT(xfs_bmap_free_item_zone != NULL);
4171 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4172 new->xbfi_startblock = bno;
4173 new->xbfi_blockcount = (xfs_extlen_t)len;
4174 for (prev = NULL, cur = flist->xbf_first;
4176 prev = cur, cur = cur->xbfi_next) {
4177 if (cur->xbfi_startblock >= bno)
4181 prev->xbfi_next = new;
4183 flist->xbf_first = new;
4184 new->xbfi_next = cur;
4189 * Compute and fill in the value of the maximum depth of a bmap btree
4190 * in this filesystem. Done once, during mount.
4193 xfs_bmap_compute_maxlevels(
4194 xfs_mount_t *mp, /* file system mount structure */
4195 int whichfork) /* data or attr fork */
4197 int level; /* btree level */
4198 uint maxblocks; /* max blocks at this level */
4199 uint maxleafents; /* max leaf entries possible */
4200 int maxrootrecs; /* max records in root block */
4201 int minleafrecs; /* min records in leaf block */
4202 int minnoderecs; /* min records in node block */
4203 int sz; /* root block size */
4206 * The maximum number of extents in a file, hence the maximum
4207 * number of leaf entries, is controlled by the type of di_nextents
4208 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4209 * (a signed 16-bit number, xfs_aextnum_t).
4211 if (whichfork == XFS_DATA_FORK) {
4212 maxleafents = MAXEXTNUM;
4213 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4214 XFS_BMDR_SPACE_CALC(MINDBTPTRS) : mp->m_attroffset;
4216 maxleafents = MAXAEXTNUM;
4217 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4218 XFS_BMDR_SPACE_CALC(MINABTPTRS) :
4219 mp->m_sb.sb_inodesize - mp->m_attroffset;
4221 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
4222 minleafrecs = mp->m_bmap_dmnr[0];
4223 minnoderecs = mp->m_bmap_dmnr[1];
4224 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4225 for (level = 1; maxblocks > 1; level++) {
4226 if (maxblocks <= maxrootrecs)
4229 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4231 mp->m_bm_maxlevels[whichfork] = level;
4235 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4236 * caller. Frees all the extents that need freeing, which must be done
4237 * last due to locking considerations. We never free any extents in
4238 * the first transaction. This is to allow the caller to make the first
4239 * transaction a synchronous one so that the pointers to the data being
4240 * broken in this transaction will be permanent before the data is actually
4241 * freed. This is necessary to prevent blocks from being reallocated
4242 * and written to before the free and reallocation are actually permanent.
4243 * We do not just make the first transaction synchronous here, because
4244 * there are more efficient ways to gain the same protection in some cases
4245 * (see the file truncation code).
4247 * Return 1 if the given transaction was committed and a new one
4248 * started, and 0 otherwise in the committed parameter.
4253 xfs_trans_t **tp, /* transaction pointer addr */
4254 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4255 int *committed) /* xact committed or not */
4257 xfs_efd_log_item_t *efd; /* extent free data */
4258 xfs_efi_log_item_t *efi; /* extent free intention */
4259 int error; /* error return value */
4260 xfs_bmap_free_item_t *free; /* free extent item */
4261 unsigned int logres; /* new log reservation */
4262 unsigned int logcount; /* new log count */
4263 xfs_mount_t *mp; /* filesystem mount structure */
4264 xfs_bmap_free_item_t *next; /* next item on free list */
4265 xfs_trans_t *ntp; /* new transaction pointer */
4267 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4268 if (flist->xbf_count == 0) {
4273 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4274 for (free = flist->xbf_first; free; free = free->xbfi_next)
4275 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4276 free->xbfi_blockcount);
4277 logres = ntp->t_log_res;
4278 logcount = ntp->t_log_count;
4279 ntp = xfs_trans_dup(*tp);
4280 error = xfs_trans_commit(*tp, 0);
4284 * We have a new transaction, so we should return committed=1,
4285 * even though we're returning an error.
4290 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4293 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4294 for (free = flist->xbf_first; free != NULL; free = next) {
4295 next = free->xbfi_next;
4296 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4297 free->xbfi_blockcount))) {
4299 * The bmap free list will be cleaned up at a
4300 * higher level. The EFI will be canceled when
4301 * this transaction is aborted.
4302 * Need to force shutdown here to make sure it
4303 * happens, since this transaction may not be
4307 if (!XFS_FORCED_SHUTDOWN(mp))
4308 xfs_force_shutdown(mp,
4309 (error == EFSCORRUPTED) ?
4310 SHUTDOWN_CORRUPT_INCORE :
4311 SHUTDOWN_META_IO_ERROR);
4314 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4315 free->xbfi_blockcount);
4316 xfs_bmap_del_free(flist, NULL, free);
4322 * Free up any items left in the list.
4326 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4328 xfs_bmap_free_item_t *free; /* free list item */
4329 xfs_bmap_free_item_t *next;
4331 if (flist->xbf_count == 0)
4333 ASSERT(flist->xbf_first != NULL);
4334 for (free = flist->xbf_first; free; free = next) {
4335 next = free->xbfi_next;
4336 xfs_bmap_del_free(flist, NULL, free);
4338 ASSERT(flist->xbf_count == 0);
4342 * Returns the file-relative block number of the first unused block(s)
4343 * in the file with at least "len" logically contiguous blocks free.
4344 * This is the lowest-address hole if the file has holes, else the first block
4345 * past the end of file.
4346 * Return 0 if the file is currently local (in-inode).
4349 xfs_bmap_first_unused(
4350 xfs_trans_t *tp, /* transaction pointer */
4351 xfs_inode_t *ip, /* incore inode */
4352 xfs_extlen_t len, /* size of hole to find */
4353 xfs_fileoff_t *first_unused, /* unused block */
4354 int whichfork) /* data or attr fork */
4356 xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
4357 int error; /* error return value */
4358 int idx; /* extent record index */
4359 xfs_ifork_t *ifp; /* inode fork pointer */
4360 xfs_fileoff_t lastaddr; /* last block number seen */
4361 xfs_fileoff_t lowest; /* lowest useful block */
4362 xfs_fileoff_t max; /* starting useful block */
4363 xfs_fileoff_t off; /* offset for this block */
4364 xfs_extnum_t nextents; /* number of extent entries */
4366 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4367 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4368 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4369 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4373 ifp = XFS_IFORK_PTR(ip, whichfork);
4374 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4375 (error = xfs_iread_extents(tp, ip, whichfork)))
4377 lowest = *first_unused;
4378 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4379 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
4380 ep = xfs_iext_get_ext(ifp, idx);
4381 off = xfs_bmbt_get_startoff(ep);
4383 * See if the hole before this extent will work.
4385 if (off >= lowest + len && off - max >= len) {
4386 *first_unused = max;
4389 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4390 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4392 *first_unused = max;
4397 * Returns the file-relative block number of the last block + 1 before
4398 * last_block (input value) in the file.
4399 * This is not based on i_size, it is based on the extent records.
4400 * Returns 0 for local files, as they do not have extent records.
4403 xfs_bmap_last_before(
4404 xfs_trans_t *tp, /* transaction pointer */
4405 xfs_inode_t *ip, /* incore inode */
4406 xfs_fileoff_t *last_block, /* last block */
4407 int whichfork) /* data or attr fork */
4409 xfs_fileoff_t bno; /* input file offset */
4410 int eof; /* hit end of file */
4411 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4412 int error; /* error return value */
4413 xfs_bmbt_irec_t got; /* current extent value */
4414 xfs_ifork_t *ifp; /* inode fork pointer */
4415 xfs_extnum_t lastx; /* last extent used */
4416 xfs_bmbt_irec_t prev; /* previous extent value */
4418 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4419 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4420 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4421 return XFS_ERROR(EIO);
4422 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4426 ifp = XFS_IFORK_PTR(ip, whichfork);
4427 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4428 (error = xfs_iread_extents(tp, ip, whichfork)))
4430 bno = *last_block - 1;
4431 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4433 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4434 if (prev.br_startoff == NULLFILEOFF)
4437 *last_block = prev.br_startoff + prev.br_blockcount;
4440 * Otherwise *last_block is already the right answer.
4446 * Returns the file-relative block number of the first block past eof in
4447 * the file. This is not based on i_size, it is based on the extent records.
4448 * Returns 0 for local files, as they do not have extent records.
4451 xfs_bmap_last_offset(
4452 xfs_trans_t *tp, /* transaction pointer */
4453 xfs_inode_t *ip, /* incore inode */
4454 xfs_fileoff_t *last_block, /* last block */
4455 int whichfork) /* data or attr fork */
4457 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4458 int error; /* error return value */
4459 xfs_ifork_t *ifp; /* inode fork pointer */
4460 xfs_extnum_t nextents; /* number of extent entries */
4462 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4463 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4464 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4465 return XFS_ERROR(EIO);
4466 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4470 ifp = XFS_IFORK_PTR(ip, whichfork);
4471 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4472 (error = xfs_iread_extents(tp, ip, whichfork)))
4474 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4479 ep = xfs_iext_get_ext(ifp, nextents - 1);
4480 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4485 * Returns whether the selected fork of the inode has exactly one
4486 * block or not. For the data fork we check this matches di_size,
4487 * implying the file's range is 0..bsize-1.
4489 int /* 1=>1 block, 0=>otherwise */
4491 xfs_inode_t *ip, /* incore inode */
4492 int whichfork) /* data or attr fork */
4494 xfs_bmbt_rec_t *ep; /* ptr to fork's extent */
4495 xfs_ifork_t *ifp; /* inode fork pointer */
4496 int rval; /* return value */
4497 xfs_bmbt_irec_t s; /* internal version of extent */
4500 if (whichfork == XFS_DATA_FORK) {
4501 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4502 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4503 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4506 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4508 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4510 ifp = XFS_IFORK_PTR(ip, whichfork);
4511 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4512 ep = xfs_iext_get_ext(ifp, 0);
4513 xfs_bmbt_get_all(ep, &s);
4514 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4515 if (rval && whichfork == XFS_DATA_FORK)
4516 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
4521 * Read in the extents to if_extents.
4522 * All inode fields are set up by caller, we just traverse the btree
4523 * and copy the records in. If the file system cannot contain unwritten
4524 * extents, the records are checked for no "state" flags.
4527 xfs_bmap_read_extents(
4528 xfs_trans_t *tp, /* transaction pointer */
4529 xfs_inode_t *ip, /* incore inode */
4530 int whichfork) /* data or attr fork */
4532 xfs_bmbt_block_t *block; /* current btree block */
4533 xfs_fsblock_t bno; /* block # of "block" */
4534 xfs_buf_t *bp; /* buffer for "block" */
4535 int error; /* error return value */
4536 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4537 #ifdef XFS_BMAP_TRACE
4538 static char fname[] = "xfs_bmap_read_extents";
4540 xfs_extnum_t i, j; /* index into the extents list */
4541 xfs_ifork_t *ifp; /* fork structure */
4542 int level; /* btree level, for checking */
4543 xfs_mount_t *mp; /* file system mount structure */
4544 __be64 *pp; /* pointer to block address */
4546 xfs_extnum_t room; /* number of entries there's room for */
4550 ifp = XFS_IFORK_PTR(ip, whichfork);
4551 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4552 XFS_EXTFMT_INODE(ip);
4553 block = ifp->if_broot;
4555 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4557 level = be16_to_cpu(block->bb_level);
4559 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
4560 bno = be64_to_cpu(*pp);
4561 ASSERT(bno != NULLDFSBNO);
4562 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4563 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4565 * Go down the tree until leaf level is reached, following the first
4566 * pointer (leftmost) at each level.
4568 while (level-- > 0) {
4569 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4570 XFS_BMAP_BTREE_REF)))
4572 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4573 XFS_WANT_CORRUPTED_GOTO(
4574 XFS_BMAP_SANITY_CHECK(mp, block, level),
4578 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
4579 bno = be64_to_cpu(*pp);
4580 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4581 xfs_trans_brelse(tp, bp);
4584 * Here with bp and block set to the leftmost leaf node in the tree.
4586 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4589 * Loop over all leaf nodes. Copy information to the extent records.
4592 xfs_bmbt_rec_t *frp, *trp;
4593 xfs_fsblock_t nextbno;
4594 xfs_extnum_t num_recs;
4598 num_recs = be16_to_cpu(block->bb_numrecs);
4599 if (unlikely(i + num_recs > room)) {
4600 ASSERT(i + num_recs <= room);
4601 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4602 "corrupt dinode %Lu, (btree extents).",
4603 (unsigned long long) ip->i_ino);
4604 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4609 XFS_WANT_CORRUPTED_GOTO(
4610 XFS_BMAP_SANITY_CHECK(mp, block, 0),
4613 * Read-ahead the next leaf block, if any.
4615 nextbno = be64_to_cpu(block->bb_rightsib);
4616 if (nextbno != NULLFSBLOCK)
4617 xfs_btree_reada_bufl(mp, nextbno, 1);
4619 * Copy records into the extent records.
4621 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
4623 for (j = 0; j < num_recs; j++, i++, frp++) {
4624 trp = xfs_iext_get_ext(ifp, i);
4625 trp->l0 = INT_GET(frp->l0, ARCH_CONVERT);
4626 trp->l1 = INT_GET(frp->l1, ARCH_CONVERT);
4628 if (exntf == XFS_EXTFMT_NOSTATE) {
4630 * Check all attribute bmap btree records and
4631 * any "older" data bmap btree records for a
4632 * set bit in the "extent flag" position.
4634 if (unlikely(xfs_check_nostate_extents(ifp,
4635 start, num_recs))) {
4636 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4642 xfs_trans_brelse(tp, bp);
4645 * If we've reached the end, stop.
4647 if (bno == NULLFSBLOCK)
4649 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4650 XFS_BMAP_BTREE_REF)))
4652 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4654 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4655 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4656 xfs_bmap_trace_exlist(fname, ip, i, whichfork);
4659 xfs_trans_brelse(tp, bp);
4660 return XFS_ERROR(EFSCORRUPTED);
4663 #ifdef XFS_BMAP_TRACE
4665 * Add bmap trace insert entries for all the contents of the extent records.
4668 xfs_bmap_trace_exlist(
4669 char *fname, /* function name */
4670 xfs_inode_t *ip, /* incore inode pointer */
4671 xfs_extnum_t cnt, /* count of entries in the list */
4672 int whichfork) /* data or attr fork */
4674 xfs_bmbt_rec_t *ep; /* current extent record */
4675 xfs_extnum_t idx; /* extent record index */
4676 xfs_ifork_t *ifp; /* inode fork pointer */
4677 xfs_bmbt_irec_t s; /* file extent record */
4679 ifp = XFS_IFORK_PTR(ip, whichfork);
4680 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4681 for (idx = 0; idx < cnt; idx++) {
4682 ep = xfs_iext_get_ext(ifp, idx);
4683 xfs_bmbt_get_all(ep, &s);
4684 xfs_bmap_trace_insert(fname, "exlist", ip, idx, 1, &s, NULL,
4692 * Validate that the bmbt_irecs being returned from bmapi are valid
4693 * given the callers original parameters. Specifically check the
4694 * ranges of the returned irecs to ensure that they only extent beyond
4695 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4698 xfs_bmap_validate_ret(
4702 xfs_bmbt_irec_t *mval,
4706 int i; /* index to map values */
4708 ASSERT(ret_nmap <= nmap);
4710 for (i = 0; i < ret_nmap; i++) {
4711 ASSERT(mval[i].br_blockcount > 0);
4712 if (!(flags & XFS_BMAPI_ENTIRE)) {
4713 ASSERT(mval[i].br_startoff >= bno);
4714 ASSERT(mval[i].br_blockcount <= len);
4715 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4718 ASSERT(mval[i].br_startoff < bno + len);
4719 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4723 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4724 mval[i].br_startoff);
4725 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4726 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4727 mval[i].br_startblock != HOLESTARTBLOCK);
4728 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4729 mval[i].br_state == XFS_EXT_UNWRITTEN);
4736 * Map file blocks to filesystem blocks.
4737 * File range is given by the bno/len pair.
4738 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4739 * into a hole or past eof.
4740 * Only allocates blocks from a single allocation group,
4741 * to avoid locking problems.
4742 * The returned value in "firstblock" from the first call in a transaction
4743 * must be remembered and presented to subsequent calls in "firstblock".
4744 * An upper bound for the number of blocks to be allocated is supplied to
4745 * the first call in "total"; if no allocation group has that many free
4746 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4750 xfs_trans_t *tp, /* transaction pointer */
4751 xfs_inode_t *ip, /* incore inode */
4752 xfs_fileoff_t bno, /* starting file offs. mapped */
4753 xfs_filblks_t len, /* length to map in file */
4754 int flags, /* XFS_BMAPI_... */
4755 xfs_fsblock_t *firstblock, /* first allocated block
4756 controls a.g. for allocs */
4757 xfs_extlen_t total, /* total blocks needed */
4758 xfs_bmbt_irec_t *mval, /* output: map values */
4759 int *nmap, /* i/o: mval size/count */
4760 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4761 xfs_extdelta_t *delta) /* o: change made to incore extents */
4763 xfs_fsblock_t abno; /* allocated block number */
4764 xfs_extlen_t alen; /* allocated extent length */
4765 xfs_fileoff_t aoff; /* allocated file offset */
4766 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
4767 xfs_btree_cur_t *cur; /* bmap btree cursor */
4768 xfs_fileoff_t end; /* end of mapped file region */
4769 int eof; /* we've hit the end of extents */
4770 xfs_bmbt_rec_t *ep; /* extent record pointer */
4771 int error; /* error return */
4772 xfs_bmbt_irec_t got; /* current file extent record */
4773 xfs_ifork_t *ifp; /* inode fork pointer */
4774 xfs_extlen_t indlen; /* indirect blocks length */
4775 xfs_extnum_t lastx; /* last useful extent number */
4776 int logflags; /* flags for transaction logging */
4777 xfs_extlen_t minleft; /* min blocks left after allocation */
4778 xfs_extlen_t minlen; /* min allocation size */
4779 xfs_mount_t *mp; /* xfs mount structure */
4780 int n; /* current extent index */
4781 int nallocs; /* number of extents alloc\'d */
4782 xfs_extnum_t nextents; /* number of extents in file */
4783 xfs_fileoff_t obno; /* old block number (offset) */
4784 xfs_bmbt_irec_t prev; /* previous file extent record */
4785 int tmp_logflags; /* temp flags holder */
4786 int whichfork; /* data or attr fork */
4787 char inhole; /* current location is hole in file */
4788 char wasdelay; /* old extent was delayed */
4789 char wr; /* this is a write request */
4790 char rt; /* this is a realtime file */
4792 xfs_fileoff_t orig_bno; /* original block number value */
4793 int orig_flags; /* original flags arg value */
4794 xfs_filblks_t orig_len; /* original value of len arg */
4795 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4796 int orig_nmap; /* original value of *nmap */
4805 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4806 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4807 XFS_ATTR_FORK : XFS_DATA_FORK;
4809 if (unlikely(XFS_TEST_ERROR(
4810 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4811 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4812 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4813 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4814 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4815 return XFS_ERROR(EFSCORRUPTED);
4817 if (XFS_FORCED_SHUTDOWN(mp))
4818 return XFS_ERROR(EIO);
4819 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4820 ifp = XFS_IFORK_PTR(ip, whichfork);
4821 ASSERT(ifp->if_ext_max ==
4822 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4823 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4824 XFS_STATS_INC(xs_blk_mapw);
4826 XFS_STATS_INC(xs_blk_mapr);
4828 * IGSTATE flag is used to combine extents which
4829 * differ only due to the state of the extents.
4830 * This technique is used from xfs_getbmap()
4831 * when the caller does not wish to see the
4832 * separation (which is the default).
4834 * This technique is also used when writing a
4835 * buffer which has been partially written,
4836 * (usually by being flushed during a chunkread),
4837 * to ensure one write takes place. This also
4838 * prevents a change in the xfs inode extents at
4839 * this time, intentionally. This change occurs
4840 * on completion of the write operation, in
4841 * xfs_strat_comp(), where the xfs_bmapi() call
4842 * is transactioned, and the extents combined.
4844 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4845 wr = 0; /* no allocations are allowed */
4846 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
4850 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4852 if ((error = xfs_bmap_local_to_extents(tp, ip,
4853 firstblock, total, &logflags, whichfork)))
4856 if (wr && *firstblock == NULLFSBLOCK) {
4857 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4858 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4863 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4864 (error = xfs_iread_extents(tp, ip, whichfork)))
4866 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4868 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4874 delta->xed_startoff = NULLFILEOFF;
4875 delta->xed_blockcount = 0;
4877 while (bno < end && n < *nmap) {
4879 * Reading past eof, act as though there's a hole
4883 got.br_startoff = end;
4884 inhole = eof || got.br_startoff > bno;
4885 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
4886 ISNULLSTARTBLOCK(got.br_startblock);
4888 * First, deal with the hole before the allocated space
4889 * that we found, if any.
4891 if (wr && (inhole || wasdelay)) {
4893 * For the wasdelay case, we could also just
4894 * allocate the stuff asked for in this bmap call
4895 * but that wouldn't be as good.
4897 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
4898 alen = (xfs_extlen_t)got.br_blockcount;
4899 aoff = got.br_startoff;
4900 if (lastx != NULLEXTNUM && lastx) {
4901 ep = xfs_iext_get_ext(ifp, lastx - 1);
4902 xfs_bmbt_get_all(ep, &prev);
4904 } else if (wasdelay) {
4905 alen = (xfs_extlen_t)
4906 XFS_FILBLKS_MIN(len,
4908 got.br_blockcount) - bno);
4911 alen = (xfs_extlen_t)
4912 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4914 alen = (xfs_extlen_t)
4915 XFS_FILBLKS_MIN(alen,
4916 got.br_startoff - bno);
4919 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4920 if (flags & XFS_BMAPI_DELAY) {
4923 /* Figure out the extent size, adjust alen */
4924 extsz = xfs_get_extsz_hint(ip);
4926 error = xfs_bmap_extsize_align(mp,
4929 flags&XFS_BMAPI_DELAY,
4930 flags&XFS_BMAPI_CONVERT,
4936 extsz = alen / mp->m_sb.sb_rextsize;
4939 * Make a transaction-less quota reservation for
4940 * delayed allocation blocks. This number gets
4941 * adjusted later. We return if we haven't
4942 * allocated blocks already inside this loop.
4944 if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS(
4945 mp, NULL, ip, (long)alen, 0,
4946 rt ? XFS_QMOPT_RES_RTBLKS :
4947 XFS_QMOPT_RES_REGBLKS))) {
4950 ASSERT(cur == NULL);
4957 * Split changing sb for alen and indlen since
4958 * they could be coming from different places.
4960 indlen = (xfs_extlen_t)
4961 xfs_bmap_worst_indlen(ip, alen);
4965 error = xfs_mod_incore_sb(mp,
4967 -((int64_t)extsz), (flags &
4968 XFS_BMAPI_RSVBLOCKS));
4970 error = xfs_mod_incore_sb(mp,
4972 -((int64_t)alen), (flags &
4973 XFS_BMAPI_RSVBLOCKS));
4976 error = xfs_mod_incore_sb(mp,
4978 -((int64_t)indlen), (flags &
4979 XFS_BMAPI_RSVBLOCKS));
4981 xfs_mod_incore_sb(mp,
4983 (int64_t)extsz, (flags &
4984 XFS_BMAPI_RSVBLOCKS));
4986 xfs_mod_incore_sb(mp,
4988 (int64_t)alen, (flags &
4989 XFS_BMAPI_RSVBLOCKS));
4993 if (XFS_IS_QUOTA_ON(mp))
4994 /* unreserve the blocks now */
4996 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4999 XFS_QMOPT_RES_RTBLKS :
5000 XFS_QMOPT_RES_REGBLKS);
5004 ip->i_delayed_blks += alen;
5005 abno = NULLSTARTBLOCK(indlen);
5008 * If first time, allocate and fill in
5009 * once-only bma fields.
5011 if (bma.ip == NULL) {
5019 /* Indicate if this is the first user data
5020 * in the file, or just any user data.
5022 if (!(flags & XFS_BMAPI_METADATA)) {
5023 bma.userdata = (aoff == 0) ?
5024 XFS_ALLOC_INITIAL_USER_DATA :
5028 * Fill in changeable bma fields.
5031 bma.firstblock = *firstblock;
5034 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
5035 bma.wasdel = wasdelay;
5036 bma.minlen = minlen;
5037 bma.low = flist->xbf_low;
5038 bma.minleft = minleft;
5040 * Only want to do the alignment at the
5041 * eof if it is userdata and allocation length
5042 * is larger than a stripe unit.
5044 if (mp->m_dalign && alen >= mp->m_dalign &&
5045 (!(flags & XFS_BMAPI_METADATA)) &&
5046 (whichfork == XFS_DATA_FORK)) {
5047 if ((error = xfs_bmap_isaeof(ip, aoff,
5048 whichfork, &bma.aeof)))
5055 if ((error = xfs_bmap_alloc(&bma)))
5058 * Copy out result fields.
5061 if ((flist->xbf_low = bma.low))
5065 ASSERT(*firstblock == NULLFSBLOCK ||
5066 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5067 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
5069 XFS_FSB_TO_AGNO(mp, *firstblock) <
5070 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
5071 *firstblock = bma.firstblock;
5073 cur->bc_private.b.firstblock =
5075 if (abno == NULLFSBLOCK)
5077 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5078 cur = xfs_btree_init_cursor(mp,
5079 tp, NULL, 0, XFS_BTNUM_BMAP,
5081 cur->bc_private.b.firstblock =
5083 cur->bc_private.b.flist = flist;
5086 * Bump the number of extents we've allocated
5092 cur->bc_private.b.flags =
5093 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
5094 got.br_startoff = aoff;
5095 got.br_startblock = abno;
5096 got.br_blockcount = alen;
5097 got.br_state = XFS_EXT_NORM; /* assume normal */
5099 * Determine state of extent, and the filesystem.
5100 * A wasdelay extent has been initialized, so
5101 * shouldn't be flagged as unwritten.
5103 if (wr && XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5104 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5105 got.br_state = XFS_EXT_UNWRITTEN;
5107 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
5108 firstblock, flist, &tmp_logflags, delta,
5109 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5110 logflags |= tmp_logflags;
5113 lastx = ifp->if_lastex;
5114 ep = xfs_iext_get_ext(ifp, lastx);
5115 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5116 xfs_bmbt_get_all(ep, &got);
5117 ASSERT(got.br_startoff <= aoff);
5118 ASSERT(got.br_startoff + got.br_blockcount >=
5121 if (flags & XFS_BMAPI_DELAY) {
5122 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
5123 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
5125 ASSERT(got.br_state == XFS_EXT_NORM ||
5126 got.br_state == XFS_EXT_UNWRITTEN);
5129 * Fall down into the found allocated space case.
5131 } else if (inhole) {
5133 * Reading in a hole.
5135 mval->br_startoff = bno;
5136 mval->br_startblock = HOLESTARTBLOCK;
5137 mval->br_blockcount =
5138 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5139 mval->br_state = XFS_EXT_NORM;
5140 bno += mval->br_blockcount;
5141 len -= mval->br_blockcount;
5147 * Then deal with the allocated space we found.
5150 if (!(flags & XFS_BMAPI_ENTIRE) &&
5151 (got.br_startoff + got.br_blockcount > obno)) {
5154 ASSERT((bno >= obno) || (n == 0));
5156 mval->br_startoff = bno;
5157 if (ISNULLSTARTBLOCK(got.br_startblock)) {
5158 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5159 mval->br_startblock = DELAYSTARTBLOCK;
5161 mval->br_startblock =
5163 (bno - got.br_startoff);
5165 * Return the minimum of what we got and what we
5166 * asked for for the length. We can use the len
5167 * variable here because it is modified below
5168 * and we could have been there before coming
5169 * here if the first part of the allocation
5170 * didn't overlap what was asked for.
5172 mval->br_blockcount =
5173 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5174 (bno - got.br_startoff));
5175 mval->br_state = got.br_state;
5176 ASSERT(mval->br_blockcount <= len);
5179 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
5180 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5181 mval->br_startblock = DELAYSTARTBLOCK;
5186 * Check if writing previously allocated but
5187 * unwritten extents.
5189 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5190 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5192 * Modify (by adding) the state flag, if writing.
5194 ASSERT(mval->br_blockcount <= len);
5195 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5196 cur = xfs_btree_init_cursor(mp,
5197 tp, NULL, 0, XFS_BTNUM_BMAP,
5199 cur->bc_private.b.firstblock =
5201 cur->bc_private.b.flist = flist;
5203 mval->br_state = XFS_EXT_NORM;
5204 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
5205 firstblock, flist, &tmp_logflags, delta,
5206 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5207 logflags |= tmp_logflags;
5210 lastx = ifp->if_lastex;
5211 ep = xfs_iext_get_ext(ifp, lastx);
5212 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5213 xfs_bmbt_get_all(ep, &got);
5215 * We may have combined previously unwritten
5216 * space with written space, so generate
5219 if (mval->br_blockcount < len)
5223 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5224 ((mval->br_startoff + mval->br_blockcount) <= end));
5225 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5226 (mval->br_blockcount <= len) ||
5227 (mval->br_startoff < obno));
5228 bno = mval->br_startoff + mval->br_blockcount;
5230 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5231 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5232 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5233 ASSERT(mval->br_state == mval[-1].br_state);
5234 mval[-1].br_blockcount = mval->br_blockcount;
5235 mval[-1].br_state = mval->br_state;
5236 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5237 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5238 mval[-1].br_startblock != HOLESTARTBLOCK &&
5239 mval->br_startblock ==
5240 mval[-1].br_startblock + mval[-1].br_blockcount &&
5241 ((flags & XFS_BMAPI_IGSTATE) ||
5242 mval[-1].br_state == mval->br_state)) {
5243 ASSERT(mval->br_startoff ==
5244 mval[-1].br_startoff + mval[-1].br_blockcount);
5245 mval[-1].br_blockcount += mval->br_blockcount;
5247 mval->br_startblock == DELAYSTARTBLOCK &&
5248 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5249 mval->br_startoff ==
5250 mval[-1].br_startoff + mval[-1].br_blockcount) {
5251 mval[-1].br_blockcount += mval->br_blockcount;
5252 mval[-1].br_state = mval->br_state;
5253 } else if (!((n == 0) &&
5254 ((mval->br_startoff + mval->br_blockcount) <=
5260 * If we're done, stop now. Stop when we've allocated
5261 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5262 * the transaction may get too big.
5264 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5267 * Else go on to the next record.
5269 ep = xfs_iext_get_ext(ifp, ++lastx);
5271 if (lastx >= nextents)
5274 xfs_bmbt_get_all(ep, &got);
5276 ifp->if_lastex = lastx;
5279 * Transform from btree to extents, give it cur.
5281 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5282 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5284 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5285 &tmp_logflags, whichfork);
5286 logflags |= tmp_logflags;
5290 ASSERT(ifp->if_ext_max ==
5291 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5292 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5293 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5295 if (delta && delta->xed_startoff != NULLFILEOFF) {
5296 /* A change was actually made.
5297 * Note that delta->xed_blockount is an offset at this
5298 * point and needs to be converted to a block count.
5300 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5301 delta->xed_blockcount -= delta->xed_startoff;
5305 * Log everything. Do this after conversion, there's no point in
5306 * logging the extent records if we've converted to btree format.
5308 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5309 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5310 logflags &= ~XFS_ILOG_FEXT(whichfork);
5311 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5312 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5313 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5315 * Log whatever the flags say, even if error. Otherwise we might miss
5316 * detecting a case where the data is changed, there's an error,
5317 * and it's not logged so we don't shutdown when we should.
5321 xfs_trans_log_inode(tp, ip, logflags);
5325 ASSERT(*firstblock == NULLFSBLOCK ||
5326 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5328 cur->bc_private.b.firstblock) ||
5330 XFS_FSB_TO_AGNO(mp, *firstblock) <
5332 cur->bc_private.b.firstblock)));
5333 *firstblock = cur->bc_private.b.firstblock;
5335 xfs_btree_del_cursor(cur,
5336 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5339 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5345 * Map file blocks to filesystem blocks, simple version.
5346 * One block (extent) only, read-only.
5347 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5348 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5349 * was set and all the others were clear.
5353 xfs_trans_t *tp, /* transaction pointer */
5354 xfs_inode_t *ip, /* incore inode */
5355 int whichfork, /* data or attr fork */
5356 xfs_fsblock_t *fsb, /* output: mapped block */
5357 xfs_fileoff_t bno) /* starting file offs. mapped */
5359 int eof; /* we've hit the end of extents */
5360 int error; /* error return */
5361 xfs_bmbt_irec_t got; /* current file extent record */
5362 xfs_ifork_t *ifp; /* inode fork pointer */
5363 xfs_extnum_t lastx; /* last useful extent number */
5364 xfs_bmbt_irec_t prev; /* previous file extent record */
5366 ifp = XFS_IFORK_PTR(ip, whichfork);
5368 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5369 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5370 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5372 return XFS_ERROR(EFSCORRUPTED);
5374 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5375 return XFS_ERROR(EIO);
5376 XFS_STATS_INC(xs_blk_mapr);
5377 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5378 (error = xfs_iread_extents(tp, ip, whichfork)))
5380 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5383 * Reading past eof, act as though there's a hole
5386 if (eof || got.br_startoff > bno) {
5390 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
5391 ASSERT(bno < got.br_startoff + got.br_blockcount);
5392 *fsb = got.br_startblock + (bno - got.br_startoff);
5393 ifp->if_lastex = lastx;
5398 * Unmap (remove) blocks from a file.
5399 * If nexts is nonzero then the number of extents to remove is limited to
5400 * that value. If not all extents in the block range can be removed then
5405 xfs_trans_t *tp, /* transaction pointer */
5406 struct xfs_inode *ip, /* incore inode */
5407 xfs_fileoff_t bno, /* starting offset to unmap */
5408 xfs_filblks_t len, /* length to unmap in file */
5409 int flags, /* misc flags */
5410 xfs_extnum_t nexts, /* number of extents max */
5411 xfs_fsblock_t *firstblock, /* first allocated block
5412 controls a.g. for allocs */
5413 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5414 xfs_extdelta_t *delta, /* o: change made to incore
5416 int *done) /* set if not done yet */
5418 xfs_btree_cur_t *cur; /* bmap btree cursor */
5419 xfs_bmbt_irec_t del; /* extent being deleted */
5420 int eof; /* is deleting at eof */
5421 xfs_bmbt_rec_t *ep; /* extent record pointer */
5422 int error; /* error return value */
5423 xfs_extnum_t extno; /* extent number in list */
5424 xfs_bmbt_irec_t got; /* current extent record */
5425 xfs_ifork_t *ifp; /* inode fork pointer */
5426 int isrt; /* freeing in rt area */
5427 xfs_extnum_t lastx; /* last extent index used */
5428 int logflags; /* transaction logging flags */
5429 xfs_extlen_t mod; /* rt extent offset */
5430 xfs_mount_t *mp; /* mount structure */
5431 xfs_extnum_t nextents; /* number of file extents */
5432 xfs_bmbt_irec_t prev; /* previous extent record */
5433 xfs_fileoff_t start; /* first file offset deleted */
5434 int tmp_logflags; /* partial logging flags */
5435 int wasdel; /* was a delayed alloc extent */
5436 int whichfork; /* data or attribute fork */
5437 int rsvd; /* OK to allocate reserved blocks */
5440 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5441 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5442 XFS_ATTR_FORK : XFS_DATA_FORK;
5443 ifp = XFS_IFORK_PTR(ip, whichfork);
5445 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5446 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5447 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5449 return XFS_ERROR(EFSCORRUPTED);
5452 if (XFS_FORCED_SHUTDOWN(mp))
5453 return XFS_ERROR(EIO);
5454 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5457 ASSERT(ifp->if_ext_max ==
5458 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5459 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5460 (error = xfs_iread_extents(tp, ip, whichfork)))
5462 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5463 if (nextents == 0) {
5467 XFS_STATS_INC(xs_blk_unmap);
5468 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5470 bno = start + len - 1;
5471 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5474 delta->xed_startoff = NULLFILEOFF;
5475 delta->xed_blockcount = 0;
5478 * Check to see if the given block number is past the end of the
5479 * file, back up to the last block if so...
5482 ep = xfs_iext_get_ext(ifp, --lastx);
5483 xfs_bmbt_get_all(ep, &got);
5484 bno = got.br_startoff + got.br_blockcount - 1;
5487 if (ifp->if_flags & XFS_IFBROOT) {
5488 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5489 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
5491 cur->bc_private.b.firstblock = *firstblock;
5492 cur->bc_private.b.flist = flist;
5493 cur->bc_private.b.flags = 0;
5497 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5498 (nexts == 0 || extno < nexts)) {
5500 * Is the found extent after a hole in which bno lives?
5501 * Just back up to the previous extent, if so.
5503 if (got.br_startoff > bno) {
5506 ep = xfs_iext_get_ext(ifp, lastx);
5507 xfs_bmbt_get_all(ep, &got);
5510 * Is the last block of this extent before the range
5511 * we're supposed to delete? If so, we're done.
5513 bno = XFS_FILEOFF_MIN(bno,
5514 got.br_startoff + got.br_blockcount - 1);
5518 * Then deal with the (possibly delayed) allocated space
5523 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
5524 if (got.br_startoff < start) {
5525 del.br_startoff = start;
5526 del.br_blockcount -= start - got.br_startoff;
5528 del.br_startblock += start - got.br_startoff;
5530 if (del.br_startoff + del.br_blockcount > bno + 1)
5531 del.br_blockcount = bno + 1 - del.br_startoff;
5532 sum = del.br_startblock + del.br_blockcount;
5534 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5536 * Realtime extent not lined up at the end.
5537 * The extent could have been split into written
5538 * and unwritten pieces, or we could just be
5539 * unmapping part of it. But we can't really
5540 * get rid of part of a realtime extent.
5542 if (del.br_state == XFS_EXT_UNWRITTEN ||
5543 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5545 * This piece is unwritten, or we're not
5546 * using unwritten extents. Skip over it.
5549 bno -= mod > del.br_blockcount ?
5550 del.br_blockcount : mod;
5551 if (bno < got.br_startoff) {
5553 xfs_bmbt_get_all(xfs_iext_get_ext(
5559 * It's written, turn it unwritten.
5560 * This is better than zeroing it.
5562 ASSERT(del.br_state == XFS_EXT_NORM);
5563 ASSERT(xfs_trans_get_block_res(tp) > 0);
5565 * If this spans a realtime extent boundary,
5566 * chop it back to the start of the one we end at.
5568 if (del.br_blockcount > mod) {
5569 del.br_startoff += del.br_blockcount - mod;
5570 del.br_startblock += del.br_blockcount - mod;
5571 del.br_blockcount = mod;
5573 del.br_state = XFS_EXT_UNWRITTEN;
5574 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5575 firstblock, flist, &logflags, delta,
5581 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5583 * Realtime extent is lined up at the end but not
5584 * at the front. We'll get rid of full extents if
5587 mod = mp->m_sb.sb_rextsize - mod;
5588 if (del.br_blockcount > mod) {
5589 del.br_blockcount -= mod;
5590 del.br_startoff += mod;
5591 del.br_startblock += mod;
5592 } else if ((del.br_startoff == start &&
5593 (del.br_state == XFS_EXT_UNWRITTEN ||
5594 xfs_trans_get_block_res(tp) == 0)) ||
5595 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5597 * Can't make it unwritten. There isn't
5598 * a full extent here so just skip it.
5600 ASSERT(bno >= del.br_blockcount);
5601 bno -= del.br_blockcount;
5602 if (bno < got.br_startoff) {
5604 xfs_bmbt_get_all(--ep, &got);
5607 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5609 * This one is already unwritten.
5610 * It must have a written left neighbor.
5611 * Unwrite the killed part of that one and
5615 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5617 ASSERT(prev.br_state == XFS_EXT_NORM);
5618 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
5619 ASSERT(del.br_startblock ==
5620 prev.br_startblock + prev.br_blockcount);
5621 if (prev.br_startoff < start) {
5622 mod = start - prev.br_startoff;
5623 prev.br_blockcount -= mod;
5624 prev.br_startblock += mod;
5625 prev.br_startoff = start;
5627 prev.br_state = XFS_EXT_UNWRITTEN;
5628 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5629 &prev, firstblock, flist, &logflags,
5630 delta, XFS_DATA_FORK, 0);
5635 ASSERT(del.br_state == XFS_EXT_NORM);
5636 del.br_state = XFS_EXT_UNWRITTEN;
5637 error = xfs_bmap_add_extent(ip, lastx, &cur,
5638 &del, firstblock, flist, &logflags,
5639 delta, XFS_DATA_FORK, 0);
5646 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
5647 /* Update realtime/data freespace, unreserve quota */
5649 xfs_filblks_t rtexts;
5651 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5652 do_div(rtexts, mp->m_sb.sb_rextsize);
5653 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5654 (int64_t)rtexts, rsvd);
5655 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5656 NULL, ip, -((long)del.br_blockcount), 0,
5657 XFS_QMOPT_RES_RTBLKS);
5659 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
5660 (int64_t)del.br_blockcount, rsvd);
5661 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5662 NULL, ip, -((long)del.br_blockcount), 0,
5663 XFS_QMOPT_RES_REGBLKS);
5665 ip->i_delayed_blks -= del.br_blockcount;
5667 cur->bc_private.b.flags |=
5668 XFS_BTCUR_BPRV_WASDEL;
5670 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5672 * If it's the case where the directory code is running
5673 * with no block reservation, and the deleted block is in
5674 * the middle of its extent, and the resulting insert
5675 * of an extent would cause transformation to btree format,
5676 * then reject it. The calling code will then swap
5677 * blocks around instead.
5678 * We have to do this now, rather than waiting for the
5679 * conversion to btree format, since the transaction
5682 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5683 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5684 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5685 del.br_startoff > got.br_startoff &&
5686 del.br_startoff + del.br_blockcount <
5687 got.br_startoff + got.br_blockcount) {
5688 error = XFS_ERROR(ENOSPC);
5691 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5692 &tmp_logflags, delta, whichfork, rsvd);
5693 logflags |= tmp_logflags;
5696 bno = del.br_startoff - 1;
5698 lastx = ifp->if_lastex;
5700 * If not done go on to the next (previous) record.
5701 * Reset ep in case the extents array was re-alloced.
5703 ep = xfs_iext_get_ext(ifp, lastx);
5704 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5705 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5706 xfs_bmbt_get_startoff(ep) > bno) {
5708 ep = xfs_iext_get_ext(ifp, lastx);
5711 xfs_bmbt_get_all(ep, &got);
5715 ifp->if_lastex = lastx;
5716 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5717 ASSERT(ifp->if_ext_max ==
5718 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5720 * Convert to a btree if necessary.
5722 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5723 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5724 ASSERT(cur == NULL);
5725 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5726 &cur, 0, &tmp_logflags, whichfork);
5727 logflags |= tmp_logflags;
5732 * transform from btree to extents, give it cur
5734 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5735 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5736 ASSERT(cur != NULL);
5737 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5739 logflags |= tmp_logflags;
5744 * transform from extents to local?
5746 ASSERT(ifp->if_ext_max ==
5747 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5749 if (delta && delta->xed_startoff != NULLFILEOFF) {
5750 /* A change was actually made.
5751 * Note that delta->xed_blockount is an offset at this
5752 * point and needs to be converted to a block count.
5754 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5755 delta->xed_blockcount -= delta->xed_startoff;
5759 * Log everything. Do this after conversion, there's no point in
5760 * logging the extent records if we've converted to btree format.
5762 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5763 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5764 logflags &= ~XFS_ILOG_FEXT(whichfork);
5765 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5766 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5767 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5769 * Log inode even in the error case, if the transaction
5770 * is dirty we'll need to shut down the filesystem.
5773 xfs_trans_log_inode(tp, ip, logflags);
5776 *firstblock = cur->bc_private.b.firstblock;
5777 cur->bc_private.b.allocated = 0;
5779 xfs_btree_del_cursor(cur,
5780 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5786 * Fcntl interface to xfs_bmapi.
5788 int /* error code */
5790 bhv_desc_t *bdp, /* XFS behavior descriptor*/
5791 struct getbmap *bmv, /* user bmap structure */
5792 void __user *ap, /* pointer to user's array */
5793 int interface) /* interface flags */
5795 __int64_t bmvend; /* last block requested */
5796 int error; /* return value */
5797 __int64_t fixlen; /* length for -1 case */
5798 int i; /* extent number */
5799 xfs_inode_t *ip; /* xfs incore inode pointer */
5800 bhv_vnode_t *vp; /* corresponding vnode */
5801 int lock; /* lock state */
5802 xfs_bmbt_irec_t *map; /* buffer for user's data */
5803 xfs_mount_t *mp; /* file system mount point */
5804 int nex; /* # of user extents can do */
5805 int nexleft; /* # of user extents left */
5806 int subnex; /* # of bmapi's can do */
5807 int nmap; /* number of map entries */
5808 struct getbmap out; /* output structure */
5809 int whichfork; /* data or attr fork */
5810 int prealloced; /* this is a file with
5811 * preallocated data space */
5812 int sh_unwritten; /* true, if unwritten */
5813 /* extents listed separately */
5814 int bmapi_flags; /* flags for xfs_bmapi */
5815 __int32_t oflags; /* getbmapx bmv_oflags field */
5817 vp = BHV_TO_VNODE(bdp);
5818 ip = XFS_BHVTOI(bdp);
5821 whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5822 sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
5824 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5825 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5826 * bit is set for the file, generate a read event in order
5827 * that the DMAPI application may do its thing before we return
5828 * the extents. Usually this means restoring user file data to
5829 * regions of the file that look like holes.
5831 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5832 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5833 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5834 * could misinterpret holes in a DMAPI file as true holes,
5835 * when in fact they may represent offline user data.
5837 if ( (interface & BMV_IF_NO_DMAPI_READ) == 0
5838 && DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)
5839 && whichfork == XFS_DATA_FORK) {
5841 error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, 0, 0, 0, NULL);
5843 return XFS_ERROR(error);
5846 if (whichfork == XFS_ATTR_FORK) {
5847 if (XFS_IFORK_Q(ip)) {
5848 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5849 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5850 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5851 return XFS_ERROR(EINVAL);
5852 } else if (unlikely(
5853 ip->i_d.di_aformat != 0 &&
5854 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5855 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5857 return XFS_ERROR(EFSCORRUPTED);
5859 } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5860 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5861 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5862 return XFS_ERROR(EINVAL);
5863 if (whichfork == XFS_DATA_FORK) {
5864 if (xfs_get_extsz_hint(ip) ||
5865 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5867 fixlen = XFS_MAXIOFFSET(mp);
5870 fixlen = ip->i_size;
5877 if (bmv->bmv_length == -1) {
5878 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5879 bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
5881 } else if (bmv->bmv_length < 0)
5882 return XFS_ERROR(EINVAL);
5883 if (bmv->bmv_length == 0) {
5884 bmv->bmv_entries = 0;
5887 nex = bmv->bmv_count - 1;
5889 return XFS_ERROR(EINVAL);
5890 bmvend = bmv->bmv_offset + bmv->bmv_length;
5892 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5894 if (whichfork == XFS_DATA_FORK &&
5895 (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size)) {
5896 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
5897 error = bhv_vop_flush_pages(vp, (xfs_off_t)0, -1, 0, FI_REMAPF);
5900 ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
5902 lock = xfs_ilock_map_shared(ip);
5905 * Don't let nex be bigger than the number of extents
5906 * we can have assuming alternating holes and real extents.
5908 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5909 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5911 bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
5912 ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
5915 * Allocate enough space to handle "subnex" maps at a time.
5918 map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
5920 bmv->bmv_entries = 0;
5922 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
5924 goto unlock_and_return;
5930 nmap = (nexleft > subnex) ? subnex : nexleft;
5931 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5932 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5933 bmapi_flags, NULL, 0, map, &nmap,
5936 goto unlock_and_return;
5937 ASSERT(nmap <= subnex);
5939 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5941 oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
5942 BMV_OF_PREALLOC : 0;
5943 out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
5944 out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5945 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
5946 if (map[i].br_startblock == HOLESTARTBLOCK &&
5947 ((prealloced && out.bmv_offset + out.bmv_length == bmvend) ||
5948 whichfork == XFS_ATTR_FORK )) {
5950 * came to hole at end of file or the end of
5953 goto unlock_and_return;
5956 (map[i].br_startblock == HOLESTARTBLOCK) ?
5958 XFS_FSB_TO_DB(ip, map[i].br_startblock);
5960 /* return either getbmap/getbmapx structure. */
5961 if (interface & BMV_IF_EXTENDED) {
5962 struct getbmapx outx;
5964 GETBMAP_CONVERT(out,outx);
5965 outx.bmv_oflags = oflags;
5966 outx.bmv_unused1 = outx.bmv_unused2 = 0;
5967 if (copy_to_user(ap, &outx,
5969 error = XFS_ERROR(EFAULT);
5970 goto unlock_and_return;
5973 if (copy_to_user(ap, &out,
5975 error = XFS_ERROR(EFAULT);
5976 goto unlock_and_return;
5980 out.bmv_offset + out.bmv_length;
5981 bmv->bmv_length = MAX((__int64_t)0,
5982 (__int64_t)(bmvend - bmv->bmv_offset));
5984 ap = (interface & BMV_IF_EXTENDED) ?
5986 ((struct getbmapx __user *)ap + 1) :
5988 ((struct getbmap __user *)ap + 1);
5991 } while (nmap && nexleft && bmv->bmv_length);
5994 xfs_iunlock_map_shared(ip, lock);
5995 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5997 kmem_free(map, subnex * sizeof(*map));
6003 * Check the last inode extent to determine whether this allocation will result
6004 * in blocks being allocated at the end of the file. When we allocate new data
6005 * blocks at the end of the file which do not start at the previous data block,
6006 * we will try to align the new blocks at stripe unit boundaries.
6008 STATIC int /* error */
6010 xfs_inode_t *ip, /* incore inode pointer */
6011 xfs_fileoff_t off, /* file offset in fsblocks */
6012 int whichfork, /* data or attribute fork */
6013 char *aeof) /* return value */
6015 int error; /* error return value */
6016 xfs_ifork_t *ifp; /* inode fork pointer */
6017 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
6018 xfs_extnum_t nextents; /* number of file extents */
6019 xfs_bmbt_irec_t s; /* expanded extent record */
6021 ASSERT(whichfork == XFS_DATA_FORK);
6022 ifp = XFS_IFORK_PTR(ip, whichfork);
6023 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6024 (error = xfs_iread_extents(NULL, ip, whichfork)))
6026 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6027 if (nextents == 0) {
6032 * Go to the last extent
6034 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6035 xfs_bmbt_get_all(lastrec, &s);
6037 * Check we are allocating in the last extent (for delayed allocations)
6038 * or past the last extent for non-delayed allocations.
6040 *aeof = (off >= s.br_startoff &&
6041 off < s.br_startoff + s.br_blockcount &&
6042 ISNULLSTARTBLOCK(s.br_startblock)) ||
6043 off >= s.br_startoff + s.br_blockcount;
6048 * Check if the endoff is outside the last extent. If so the caller will grow
6049 * the allocation to a stripe unit boundary.
6053 xfs_inode_t *ip, /* incore inode pointer */
6054 xfs_fileoff_t endoff, /* file offset in fsblocks */
6055 int whichfork, /* data or attribute fork */
6056 int *eof) /* result value */
6058 xfs_fsblock_t blockcount; /* extent block count */
6059 int error; /* error return value */
6060 xfs_ifork_t *ifp; /* inode fork pointer */
6061 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
6062 xfs_extnum_t nextents; /* number of file extents */
6063 xfs_fileoff_t startoff; /* extent starting file offset */
6065 ASSERT(whichfork == XFS_DATA_FORK);
6066 ifp = XFS_IFORK_PTR(ip, whichfork);
6067 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6068 (error = xfs_iread_extents(NULL, ip, whichfork)))
6070 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6071 if (nextents == 0) {
6076 * Go to the last extent
6078 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6079 startoff = xfs_bmbt_get_startoff(lastrec);
6080 blockcount = xfs_bmbt_get_blockcount(lastrec);
6081 *eof = endoff >= startoff + blockcount;
6089 xfs_btree_cur_t *cur,
6099 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6100 bp = cur->bc_bufs[i];
6102 if (XFS_BUF_ADDR(bp) == bno)
6103 break; /* Found it */
6105 if (i == XFS_BTREE_MAXLEVELS)
6108 if (!bp) { /* Chase down all the log items to see if the bp is there */
6109 xfs_log_item_chunk_t *licp;
6113 licp = &tp->t_items;
6114 while (!bp && licp != NULL) {
6115 if (XFS_LIC_ARE_ALL_FREE(licp)) {
6116 licp = licp->lic_next;
6119 for (i = 0; i < licp->lic_unused; i++) {
6120 xfs_log_item_desc_t *lidp;
6121 xfs_log_item_t *lip;
6122 xfs_buf_log_item_t *bip;
6125 if (XFS_LIC_ISFREE(licp, i)) {
6129 lidp = XFS_LIC_SLOT(licp, i);
6130 lip = lidp->lid_item;
6131 if (lip->li_type != XFS_LI_BUF)
6134 bip = (xfs_buf_log_item_t *)lip;
6137 if (XFS_BUF_ADDR(lbp) == bno) {
6139 break; /* Found it */
6142 licp = licp->lic_next;
6150 xfs_bmbt_block_t *block,
6156 __be64 *pp, *thispa; /* pointer to block address */
6157 xfs_bmbt_key_t *prevp, *keyp;
6159 ASSERT(be16_to_cpu(block->bb_level) > 0);
6162 for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
6163 dmxr = mp->m_bmap_dmxr[0];
6166 keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
6168 keyp = XFS_BTREE_KEY_ADDR(xfs_bmbt, block, i);
6172 xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
6177 * Compare the block numbers to see if there are dups.
6181 pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
6183 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, i, dmxr);
6185 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
6187 thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
6189 thispa = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, j,
6192 if (*thispa == *pp) {
6193 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
6195 (unsigned long long)be64_to_cpu(*thispa));
6196 panic("%s: ptrs are equal in node\n",
6204 * Check that the extents for the inode ip are in the right order in all
6209 xfs_bmap_check_leaf_extents(
6210 xfs_btree_cur_t *cur, /* btree cursor or null */
6211 xfs_inode_t *ip, /* incore inode pointer */
6212 int whichfork) /* data or attr fork */
6214 xfs_bmbt_block_t *block; /* current btree block */
6215 xfs_fsblock_t bno; /* block # of "block" */
6216 xfs_buf_t *bp; /* buffer for "block" */
6217 int error; /* error return value */
6218 xfs_extnum_t i=0, j; /* index into the extents list */
6219 xfs_ifork_t *ifp; /* fork structure */
6220 int level; /* btree level, for checking */
6221 xfs_mount_t *mp; /* file system mount structure */
6222 __be64 *pp; /* pointer to block address */
6223 xfs_bmbt_rec_t *ep; /* pointer to current extent */
6224 xfs_bmbt_rec_t *lastp; /* pointer to previous extent */
6225 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
6228 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6234 ifp = XFS_IFORK_PTR(ip, whichfork);
6235 block = ifp->if_broot;
6237 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6239 level = be16_to_cpu(block->bb_level);
6241 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6242 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6243 bno = be64_to_cpu(*pp);
6245 ASSERT(bno != NULLDFSBNO);
6246 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6247 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6250 * Go down the tree until leaf level is reached, following the first
6251 * pointer (leftmost) at each level.
6253 while (level-- > 0) {
6254 /* See if buf is in cur first */
6255 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6261 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6262 XFS_BMAP_BTREE_REF)))
6264 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6265 XFS_WANT_CORRUPTED_GOTO(
6266 XFS_BMAP_SANITY_CHECK(mp, block, level),
6272 * Check this block for basic sanity (increasing keys and
6273 * no duplicate blocks).
6276 xfs_check_block(block, mp, 0, 0);
6277 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
6278 bno = be64_to_cpu(*pp);
6279 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
6282 xfs_trans_brelse(NULL, bp);
6287 * Here with bp and block set to the leftmost leaf node in the tree.
6292 * Loop over all leaf nodes checking that all extents are in the right order.
6296 xfs_fsblock_t nextbno;
6297 xfs_extnum_t num_recs;
6300 num_recs = be16_to_cpu(block->bb_numrecs);
6303 * Read-ahead the next leaf block, if any.
6306 nextbno = be64_to_cpu(block->bb_rightsib);
6309 * Check all the extents to make sure they are OK.
6310 * If we had a previous block, the last entry should
6311 * conform with the first entry in this one.
6314 ep = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
6315 for (j = 1; j < num_recs; j++) {
6316 nextp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, j + 1);
6318 xfs_btree_check_rec(XFS_BTNUM_BMAP,
6319 (void *)lastp, (void *)ep);
6321 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
6330 xfs_trans_brelse(NULL, bp);
6334 * If we've reached the end, stop.
6336 if (bno == NULLFSBLOCK)
6339 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6345 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6346 XFS_BMAP_BTREE_REF)))
6348 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6352 xfs_trans_brelse(NULL, bp);
6357 cmn_err(CE_WARN, "%s: at error0", __FUNCTION__);
6359 xfs_trans_brelse(NULL, bp);
6361 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
6363 panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__);
6369 * Count fsblocks of the given fork.
6372 xfs_bmap_count_blocks(
6373 xfs_trans_t *tp, /* transaction pointer */
6374 xfs_inode_t *ip, /* incore inode */
6375 int whichfork, /* data or attr fork */
6376 int *count) /* out: count of blocks */
6378 xfs_bmbt_block_t *block; /* current btree block */
6379 xfs_fsblock_t bno; /* block # of "block" */
6380 xfs_ifork_t *ifp; /* fork structure */
6381 int level; /* btree level, for checking */
6382 xfs_mount_t *mp; /* file system mount structure */
6383 __be64 *pp; /* pointer to block address */
6387 ifp = XFS_IFORK_PTR(ip, whichfork);
6388 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6389 if (unlikely(xfs_bmap_count_leaves(ifp, 0,
6390 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6392 XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)",
6393 XFS_ERRLEVEL_LOW, mp);
6394 return XFS_ERROR(EFSCORRUPTED);
6400 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6402 block = ifp->if_broot;
6403 level = be16_to_cpu(block->bb_level);
6405 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6406 bno = be64_to_cpu(*pp);
6407 ASSERT(bno != NULLDFSBNO);
6408 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6409 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6411 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
6412 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6414 return XFS_ERROR(EFSCORRUPTED);
6421 * Recursively walks each level of a btree
6422 * to count total fsblocks is use.
6425 xfs_bmap_count_tree(
6426 xfs_mount_t *mp, /* file system mount point */
6427 xfs_trans_t *tp, /* transaction pointer */
6428 xfs_ifork_t *ifp, /* inode fork pointer */
6429 xfs_fsblock_t blockno, /* file system block number */
6430 int levelin, /* level in btree */
6431 int *count) /* Count of blocks */
6434 xfs_buf_t *bp, *nbp;
6435 int level = levelin;
6437 xfs_fsblock_t bno = blockno;
6438 xfs_fsblock_t nextbno;
6439 xfs_bmbt_block_t *block, *nextblock;
6442 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6445 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6448 /* Not at node above leafs, count this level of nodes */
6449 nextbno = be64_to_cpu(block->bb_rightsib);
6450 while (nextbno != NULLFSBLOCK) {
6451 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6452 0, &nbp, XFS_BMAP_BTREE_REF)))
6455 nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
6456 nextbno = be64_to_cpu(nextblock->bb_rightsib);
6457 xfs_trans_brelse(tp, nbp);
6460 /* Dive to the next level */
6461 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
6462 bno = be64_to_cpu(*pp);
6463 if (unlikely((error =
6464 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6465 xfs_trans_brelse(tp, bp);
6466 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6467 XFS_ERRLEVEL_LOW, mp);
6468 return XFS_ERROR(EFSCORRUPTED);
6470 xfs_trans_brelse(tp, bp);
6472 /* count all level 1 nodes and their leaves */
6474 nextbno = be64_to_cpu(block->bb_rightsib);
6475 numrecs = be16_to_cpu(block->bb_numrecs);
6476 if (unlikely(xfs_bmap_disk_count_leaves(0,
6477 block, numrecs, count) < 0)) {
6478 xfs_trans_brelse(tp, bp);
6479 XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
6480 XFS_ERRLEVEL_LOW, mp);
6481 return XFS_ERROR(EFSCORRUPTED);
6483 xfs_trans_brelse(tp, bp);
6484 if (nextbno == NULLFSBLOCK)
6487 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6488 XFS_BMAP_BTREE_REF)))
6491 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6498 * Count leaf blocks given a range of extent records.
6501 xfs_bmap_count_leaves(
6508 xfs_bmbt_rec_t *frp;
6510 for (b = 0; b < numrecs; b++) {
6511 frp = xfs_iext_get_ext(ifp, idx + b);
6512 *count += xfs_bmbt_get_blockcount(frp);
6518 * Count leaf blocks given a range of extent records originally
6522 xfs_bmap_disk_count_leaves(
6524 xfs_bmbt_block_t *block,
6529 xfs_bmbt_rec_t *frp;
6531 for (b = 1; b <= numrecs; b++) {
6532 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, idx + b);
6533 *count += xfs_bmbt_disk_get_blockcount(frp);