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"
59 xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
62 kmem_zone_t *xfs_bmap_free_item_zone;
65 * Prototypes for internal bmap routines.
70 * Called from xfs_bmap_add_attrfork to handle extents format files.
72 STATIC int /* error */
73 xfs_bmap_add_attrfork_extents(
74 xfs_trans_t *tp, /* transaction pointer */
75 xfs_inode_t *ip, /* incore inode pointer */
76 xfs_fsblock_t *firstblock, /* first block allocated */
77 xfs_bmap_free_t *flist, /* blocks to free at commit */
78 int *flags); /* inode logging flags */
81 * Called from xfs_bmap_add_attrfork to handle local format files.
83 STATIC int /* error */
84 xfs_bmap_add_attrfork_local(
85 xfs_trans_t *tp, /* transaction pointer */
86 xfs_inode_t *ip, /* incore inode pointer */
87 xfs_fsblock_t *firstblock, /* first block allocated */
88 xfs_bmap_free_t *flist, /* blocks to free at commit */
89 int *flags); /* inode logging flags */
92 * Called by xfs_bmapi to update file extent records and the btree
93 * after allocating space (or doing a delayed allocation).
95 STATIC int /* error */
97 xfs_inode_t *ip, /* incore inode pointer */
98 xfs_extnum_t idx, /* extent number to update/insert */
99 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
100 xfs_bmbt_irec_t *new, /* new data to add to file extents */
101 xfs_fsblock_t *first, /* pointer to firstblock variable */
102 xfs_bmap_free_t *flist, /* list of extents to be freed */
103 int *logflagsp, /* inode logging flags */
104 xfs_extdelta_t *delta, /* Change made to incore extents */
105 int whichfork, /* data or attr fork */
106 int rsvd); /* OK to allocate reserved blocks */
109 * Called by xfs_bmap_add_extent to handle cases converting a delayed
110 * allocation to a real allocation.
112 STATIC int /* error */
113 xfs_bmap_add_extent_delay_real(
114 xfs_inode_t *ip, /* incore inode pointer */
115 xfs_extnum_t idx, /* extent number to update/insert */
116 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
117 xfs_bmbt_irec_t *new, /* new data to add to file extents */
118 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
119 xfs_fsblock_t *first, /* pointer to firstblock variable */
120 xfs_bmap_free_t *flist, /* list of extents to be freed */
121 int *logflagsp, /* inode logging flags */
122 xfs_extdelta_t *delta, /* Change made to incore extents */
123 int rsvd); /* OK to allocate reserved blocks */
126 * Called by xfs_bmap_add_extent to handle cases converting a hole
127 * to a delayed allocation.
129 STATIC int /* error */
130 xfs_bmap_add_extent_hole_delay(
131 xfs_inode_t *ip, /* incore inode pointer */
132 xfs_extnum_t idx, /* extent number to update/insert */
133 xfs_btree_cur_t *cur, /* if null, not a btree */
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 */
190 * Check that the extents list for the inode ip is in the right order.
193 xfs_bmap_check_extents(
194 xfs_inode_t *ip, /* incore inode pointer */
195 int whichfork); /* data or attr fork */
199 * Called by xfs_bmapi to update file extent records and the btree
200 * after removing space (or undoing a delayed allocation).
202 STATIC int /* error */
204 xfs_inode_t *ip, /* incore inode pointer */
205 xfs_trans_t *tp, /* current trans pointer */
206 xfs_extnum_t idx, /* extent number to update/insert */
207 xfs_bmap_free_t *flist, /* list of extents to be freed */
208 xfs_btree_cur_t *cur, /* if null, not a btree */
209 xfs_bmbt_irec_t *new, /* new data to add to file extents */
210 int *logflagsp,/* inode logging flags */
211 xfs_extdelta_t *delta, /* Change made to incore extents */
212 int whichfork, /* data or attr fork */
213 int rsvd); /* OK to allocate reserved blocks */
216 * Remove the entry "free" from the free item list. Prev points to the
217 * previous entry, unless "free" is the head of the list.
221 xfs_bmap_free_t *flist, /* free item list header */
222 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
223 xfs_bmap_free_item_t *free); /* list item to be freed */
226 * Convert an extents-format file into a btree-format file.
227 * The new file will have a root block (in the inode) and a single child block.
229 STATIC int /* error */
230 xfs_bmap_extents_to_btree(
231 xfs_trans_t *tp, /* transaction pointer */
232 xfs_inode_t *ip, /* incore inode pointer */
233 xfs_fsblock_t *firstblock, /* first-block-allocated */
234 xfs_bmap_free_t *flist, /* blocks freed in xaction */
235 xfs_btree_cur_t **curp, /* cursor returned to caller */
236 int wasdel, /* converting a delayed alloc */
237 int *logflagsp, /* inode logging flags */
238 int whichfork); /* data or attr fork */
241 * Convert a local file to an extents file.
242 * This code is sort of bogus, since the file data needs to get
243 * logged so it won't be lost. The bmap-level manipulations are ok, though.
245 STATIC int /* error */
246 xfs_bmap_local_to_extents(
247 xfs_trans_t *tp, /* transaction pointer */
248 xfs_inode_t *ip, /* incore inode pointer */
249 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
250 xfs_extlen_t total, /* total blocks needed by transaction */
251 int *logflagsp, /* inode logging flags */
252 int whichfork); /* data or attr fork */
255 * Search the extents list for the inode, for the extent containing bno.
256 * If bno lies in a hole, point to the next entry. If bno lies past eof,
257 * *eofp will be set, and *prevp will contain the last entry (null if none).
258 * Else, *lastxp will be set to the index of the found
259 * entry; *gotp will contain the entry.
261 STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
262 xfs_bmap_search_extents(
263 xfs_inode_t *ip, /* incore inode pointer */
264 xfs_fileoff_t bno, /* block number searched for */
265 int whichfork, /* data or attr fork */
266 int *eofp, /* out: end of file found */
267 xfs_extnum_t *lastxp, /* out: last extent index */
268 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
269 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
272 * Check the last inode extent to determine whether this allocation will result
273 * in blocks being allocated at the end of the file. When we allocate new data
274 * blocks at the end of the file which do not start at the previous data block,
275 * we will try to align the new blocks at stripe unit boundaries.
277 STATIC int /* error */
279 xfs_inode_t *ip, /* incore inode pointer */
280 xfs_fileoff_t off, /* file offset in fsblocks */
281 int whichfork, /* data or attribute fork */
282 char *aeof); /* return value */
284 #ifdef XFS_BMAP_TRACE
286 * Add a bmap trace buffer entry. Base routine for the others.
289 xfs_bmap_trace_addentry(
290 int opcode, /* operation */
291 char *fname, /* function name */
292 char *desc, /* operation description */
293 xfs_inode_t *ip, /* incore inode pointer */
294 xfs_extnum_t idx, /* index of entry(ies) */
295 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
296 xfs_bmbt_rec_t *r1, /* first record */
297 xfs_bmbt_rec_t *r2, /* second record or null */
298 int whichfork); /* data or attr fork */
301 * Add bmap trace entry prior to a call to xfs_iext_remove.
304 xfs_bmap_trace_delete(
305 char *fname, /* function name */
306 char *desc, /* operation description */
307 xfs_inode_t *ip, /* incore inode pointer */
308 xfs_extnum_t idx, /* index of entry(entries) deleted */
309 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
310 int whichfork); /* data or attr fork */
313 * Add bmap trace entry prior to a call to xfs_iext_insert, or
314 * reading in the extents list from the disk (in the btree).
317 xfs_bmap_trace_insert(
318 char *fname, /* function name */
319 char *desc, /* operation description */
320 xfs_inode_t *ip, /* incore inode pointer */
321 xfs_extnum_t idx, /* index of entry(entries) inserted */
322 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
323 xfs_bmbt_irec_t *r1, /* inserted record 1 */
324 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
325 int whichfork); /* data or attr fork */
328 * Add bmap trace entry after updating an extent record in place.
331 xfs_bmap_trace_post_update(
332 char *fname, /* function name */
333 char *desc, /* operation description */
334 xfs_inode_t *ip, /* incore inode pointer */
335 xfs_extnum_t idx, /* index of entry updated */
336 int whichfork); /* data or attr fork */
339 * Add bmap trace entry prior to updating an extent record in place.
342 xfs_bmap_trace_pre_update(
343 char *fname, /* function name */
344 char *desc, /* operation description */
345 xfs_inode_t *ip, /* incore inode pointer */
346 xfs_extnum_t idx, /* index of entry to be updated */
347 int whichfork); /* data or attr fork */
350 #define xfs_bmap_trace_delete(f,d,ip,i,c,w)
351 #define xfs_bmap_trace_insert(f,d,ip,i,c,r1,r2,w)
352 #define xfs_bmap_trace_post_update(f,d,ip,i,w)
353 #define xfs_bmap_trace_pre_update(f,d,ip,i,w)
354 #endif /* XFS_BMAP_TRACE */
357 * Compute the worst-case number of indirect blocks that will be used
358 * for ip's delayed extent of length "len".
361 xfs_bmap_worst_indlen(
362 xfs_inode_t *ip, /* incore inode pointer */
363 xfs_filblks_t len); /* delayed extent length */
367 * Perform various validation checks on the values being returned
371 xfs_bmap_validate_ret(
375 xfs_bmbt_irec_t *mval,
379 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
382 #if defined(XFS_RW_TRACE)
391 #define xfs_bunmap_trace(ip, bno, len, flags, ra)
392 #endif /* XFS_RW_TRACE */
399 xfs_fsblock_t blockno,
404 xfs_bmap_count_leaves(
411 xfs_bmap_disk_count_leaves(
415 xfs_bmbt_block_t *block,
420 * Bmap internal routines.
424 * Called from xfs_bmap_add_attrfork to handle btree format files.
426 STATIC int /* error */
427 xfs_bmap_add_attrfork_btree(
428 xfs_trans_t *tp, /* transaction pointer */
429 xfs_inode_t *ip, /* incore inode pointer */
430 xfs_fsblock_t *firstblock, /* first block allocated */
431 xfs_bmap_free_t *flist, /* blocks to free at commit */
432 int *flags) /* inode logging flags */
434 xfs_btree_cur_t *cur; /* btree cursor */
435 int error; /* error return value */
436 xfs_mount_t *mp; /* file system mount struct */
437 int stat; /* newroot status */
440 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
441 *flags |= XFS_ILOG_DBROOT;
443 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
445 cur->bc_private.b.flist = flist;
446 cur->bc_private.b.firstblock = *firstblock;
447 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
449 ASSERT(stat == 1); /* must be at least one entry */
450 if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
453 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
454 return XFS_ERROR(ENOSPC);
456 *firstblock = cur->bc_private.b.firstblock;
457 cur->bc_private.b.allocated = 0;
458 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
462 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
467 * Called from xfs_bmap_add_attrfork to handle extents format files.
469 STATIC int /* error */
470 xfs_bmap_add_attrfork_extents(
471 xfs_trans_t *tp, /* transaction pointer */
472 xfs_inode_t *ip, /* incore inode pointer */
473 xfs_fsblock_t *firstblock, /* first block allocated */
474 xfs_bmap_free_t *flist, /* blocks to free at commit */
475 int *flags) /* inode logging flags */
477 xfs_btree_cur_t *cur; /* bmap btree cursor */
478 int error; /* error return value */
480 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
483 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
484 flags, XFS_DATA_FORK);
486 cur->bc_private.b.allocated = 0;
487 xfs_btree_del_cursor(cur,
488 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
494 * Called from xfs_bmap_add_attrfork to handle local format files.
496 STATIC int /* error */
497 xfs_bmap_add_attrfork_local(
498 xfs_trans_t *tp, /* transaction pointer */
499 xfs_inode_t *ip, /* incore inode pointer */
500 xfs_fsblock_t *firstblock, /* first block allocated */
501 xfs_bmap_free_t *flist, /* blocks to free at commit */
502 int *flags) /* inode logging flags */
504 xfs_da_args_t dargs; /* args for dir/attr code */
505 int error; /* error return value */
506 xfs_mount_t *mp; /* mount structure pointer */
508 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
510 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
512 memset(&dargs, 0, sizeof(dargs));
514 dargs.firstblock = firstblock;
516 dargs.total = mp->m_dirblkfsbs;
517 dargs.whichfork = XFS_DATA_FORK;
519 error = xfs_dir2_sf_to_block(&dargs);
521 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
527 * Called by xfs_bmapi to update file extent records and the btree
528 * after allocating space (or doing a delayed allocation).
530 STATIC int /* error */
532 xfs_inode_t *ip, /* incore inode pointer */
533 xfs_extnum_t idx, /* extent number to update/insert */
534 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
535 xfs_bmbt_irec_t *new, /* new data to add to file extents */
536 xfs_fsblock_t *first, /* pointer to firstblock variable */
537 xfs_bmap_free_t *flist, /* list of extents to be freed */
538 int *logflagsp, /* inode logging flags */
539 xfs_extdelta_t *delta, /* Change made to incore extents */
540 int whichfork, /* data or attr fork */
541 int rsvd) /* OK to use reserved data blocks */
543 xfs_btree_cur_t *cur; /* btree cursor or null */
544 xfs_filblks_t da_new; /* new count del alloc blocks used */
545 xfs_filblks_t da_old; /* old count del alloc blocks used */
546 int error; /* error return value */
547 #ifdef XFS_BMAP_TRACE
548 static char fname[] = "xfs_bmap_add_extent";
550 xfs_ifork_t *ifp; /* inode fork ptr */
551 int logflags; /* returned value */
552 xfs_extnum_t nextents; /* number of extents in file now */
554 XFS_STATS_INC(xs_add_exlist);
556 ifp = XFS_IFORK_PTR(ip, whichfork);
557 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
558 ASSERT(idx <= nextents);
562 * This is the first extent added to a new/empty file.
563 * Special case this one, so other routines get to assume there are
564 * already extents in the list.
567 xfs_bmap_trace_insert(fname, "insert empty", ip, 0, 1, new,
569 xfs_iext_insert(ifp, 0, 1, new);
572 if (!ISNULLSTARTBLOCK(new->br_startblock)) {
573 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
574 logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
577 /* DELTA: single new extent */
579 if (delta->xed_startoff > new->br_startoff)
580 delta->xed_startoff = new->br_startoff;
581 if (delta->xed_blockcount <
582 new->br_startoff + new->br_blockcount)
583 delta->xed_blockcount = new->br_startoff +
588 * Any kind of new delayed allocation goes here.
590 else if (ISNULLSTARTBLOCK(new->br_startblock)) {
592 ASSERT((cur->bc_private.b.flags &
593 XFS_BTCUR_BPRV_WASDEL) == 0);
594 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, cur, new,
595 &logflags, delta, rsvd)))
599 * Real allocation off the end of the file.
601 else if (idx == nextents) {
603 ASSERT((cur->bc_private.b.flags &
604 XFS_BTCUR_BPRV_WASDEL) == 0);
605 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
606 &logflags, delta, whichfork)))
609 xfs_bmbt_irec_t prev; /* old extent at offset idx */
612 * Get the record referred to by idx.
614 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
616 * If it's a real allocation record, and the new allocation ends
617 * after the start of the referred to record, then we're filling
618 * in a delayed or unwritten allocation with a real one, or
619 * converting real back to unwritten.
621 if (!ISNULLSTARTBLOCK(new->br_startblock) &&
622 new->br_startoff + new->br_blockcount > prev.br_startoff) {
623 if (prev.br_state != XFS_EXT_UNWRITTEN &&
624 ISNULLSTARTBLOCK(prev.br_startblock)) {
625 da_old = STARTBLOCKVAL(prev.br_startblock);
627 ASSERT(cur->bc_private.b.flags &
628 XFS_BTCUR_BPRV_WASDEL);
629 if ((error = xfs_bmap_add_extent_delay_real(ip,
630 idx, &cur, new, &da_new, first, flist,
631 &logflags, delta, rsvd)))
633 } else if (new->br_state == XFS_EXT_NORM) {
634 ASSERT(new->br_state == XFS_EXT_NORM);
635 if ((error = xfs_bmap_add_extent_unwritten_real(
636 ip, idx, &cur, new, &logflags, delta)))
639 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
640 if ((error = xfs_bmap_add_extent_unwritten_real(
641 ip, idx, &cur, new, &logflags, delta)))
644 ASSERT(*curp == cur || *curp == NULL);
647 * Otherwise we're filling in a hole with an allocation.
651 ASSERT((cur->bc_private.b.flags &
652 XFS_BTCUR_BPRV_WASDEL) == 0);
653 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
654 new, &logflags, delta, whichfork)))
659 ASSERT(*curp == cur || *curp == NULL);
661 * Convert to a btree if necessary.
663 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
664 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
665 int tmp_logflags; /* partial log flag return val */
668 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
669 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
670 logflags |= tmp_logflags;
675 * Adjust for changes in reserved delayed indirect blocks.
676 * Nothing to do for disk quotas here.
678 if (da_old || da_new) {
683 nblks += cur->bc_private.b.allocated;
684 ASSERT(nblks <= da_old);
686 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
687 (int)(da_old - nblks), rsvd);
690 * Clear out the allocated field, done with it now in any case.
693 cur->bc_private.b.allocated = 0;
699 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
701 *logflagsp = logflags;
706 * Called by xfs_bmap_add_extent to handle cases converting a delayed
707 * allocation to a real allocation.
709 STATIC int /* error */
710 xfs_bmap_add_extent_delay_real(
711 xfs_inode_t *ip, /* incore inode pointer */
712 xfs_extnum_t idx, /* extent number to update/insert */
713 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
714 xfs_bmbt_irec_t *new, /* new data to add to file extents */
715 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
716 xfs_fsblock_t *first, /* pointer to firstblock variable */
717 xfs_bmap_free_t *flist, /* list of extents to be freed */
718 int *logflagsp, /* inode logging flags */
719 xfs_extdelta_t *delta, /* Change made to incore extents */
720 int rsvd) /* OK to use reserved data block allocation */
722 xfs_btree_cur_t *cur; /* btree cursor */
723 int diff; /* temp value */
724 xfs_bmbt_rec_t *ep; /* extent entry for idx */
725 int error; /* error return value */
726 #ifdef XFS_BMAP_TRACE
727 static char fname[] = "xfs_bmap_add_extent_delay_real";
729 int i; /* temp state */
730 xfs_ifork_t *ifp; /* inode fork pointer */
731 xfs_fileoff_t new_endoff; /* end offset of new entry */
732 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
733 /* left is 0, right is 1, prev is 2 */
734 int rval=0; /* return value (logging flags) */
735 int state = 0;/* state bits, accessed thru macros */
736 xfs_filblks_t temp=0; /* value for dnew calculations */
737 xfs_filblks_t temp2=0;/* value for dnew calculations */
738 int tmp_rval; /* partial logging flags */
739 enum { /* bit number definitions for state */
740 LEFT_CONTIG, RIGHT_CONTIG,
741 LEFT_FILLING, RIGHT_FILLING,
742 LEFT_DELAY, RIGHT_DELAY,
743 LEFT_VALID, RIGHT_VALID
749 #define MASK(b) (1 << (b))
750 #define MASK2(a,b) (MASK(a) | MASK(b))
751 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
752 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
753 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
754 #define STATE_TEST(b) (state & MASK(b))
755 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
756 ((state &= ~MASK(b)), 0))
757 #define SWITCH_STATE \
758 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
761 * Set up a bunch of variables to make the tests simpler.
764 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
765 ep = xfs_iext_get_ext(ifp, idx);
766 xfs_bmbt_get_all(ep, &PREV);
767 new_endoff = new->br_startoff + new->br_blockcount;
768 ASSERT(PREV.br_startoff <= new->br_startoff);
769 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
771 * Set flags determining what part of the previous delayed allocation
772 * extent is being replaced by a real allocation.
774 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
775 STATE_SET(RIGHT_FILLING,
776 PREV.br_startoff + PREV.br_blockcount == new_endoff);
778 * Check and set flags if this segment has a left neighbor.
779 * Don't set contiguous if the combined extent would be too large.
781 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
782 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
783 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
785 STATE_SET(LEFT_CONTIG,
786 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
787 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
788 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
789 LEFT.br_state == new->br_state &&
790 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
792 * Check and set flags if this segment has a right neighbor.
793 * Don't set contiguous if the combined extent would be too large.
794 * Also check for all-three-contiguous being too large.
796 if (STATE_SET_TEST(RIGHT_VALID,
798 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
799 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
800 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
802 STATE_SET(RIGHT_CONTIG,
803 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
804 new_endoff == RIGHT.br_startoff &&
805 new->br_startblock + new->br_blockcount ==
806 RIGHT.br_startblock &&
807 new->br_state == RIGHT.br_state &&
808 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
809 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
810 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
811 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
815 * Switch out based on the FILLING and CONTIG state bits.
817 switch (SWITCH_STATE) {
819 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
821 * Filling in all of a previously delayed allocation extent.
822 * The left and right neighbors are both contiguous with new.
824 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
826 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
827 LEFT.br_blockcount + PREV.br_blockcount +
828 RIGHT.br_blockcount);
829 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
831 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
833 xfs_iext_remove(ifp, idx, 2);
834 ip->i_df.if_lastex = idx - 1;
835 ip->i_d.di_nextents--;
837 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
839 rval = XFS_ILOG_CORE;
840 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
842 RIGHT.br_blockcount, &i)))
845 if ((error = xfs_bmbt_delete(cur, &i)))
848 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
851 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
855 RIGHT.br_blockcount, LEFT.br_state)))
859 /* DELTA: Three in-core extents are replaced by one. */
860 temp = LEFT.br_startoff;
861 temp2 = LEFT.br_blockcount +
866 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
868 * Filling in all of a previously delayed allocation extent.
869 * The left neighbor is contiguous, the right is not.
871 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
873 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
874 LEFT.br_blockcount + PREV.br_blockcount);
875 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
877 ip->i_df.if_lastex = idx - 1;
878 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
880 xfs_iext_remove(ifp, idx, 1);
882 rval = XFS_ILOG_DEXT;
885 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
886 LEFT.br_startblock, LEFT.br_blockcount,
890 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
893 PREV.br_blockcount, LEFT.br_state)))
897 /* DELTA: Two in-core extents are replaced by one. */
898 temp = LEFT.br_startoff;
899 temp2 = LEFT.br_blockcount +
903 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
905 * Filling in all of a previously delayed allocation extent.
906 * The right neighbor is contiguous, the left is not.
908 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
910 xfs_bmbt_set_startblock(ep, new->br_startblock);
911 xfs_bmbt_set_blockcount(ep,
912 PREV.br_blockcount + RIGHT.br_blockcount);
913 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
915 ip->i_df.if_lastex = idx;
916 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
918 xfs_iext_remove(ifp, idx + 1, 1);
920 rval = XFS_ILOG_DEXT;
923 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
925 RIGHT.br_blockcount, &i)))
928 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
931 RIGHT.br_blockcount, PREV.br_state)))
935 /* DELTA: Two in-core extents are replaced by one. */
936 temp = PREV.br_startoff;
937 temp2 = PREV.br_blockcount +
941 case MASK2(LEFT_FILLING, RIGHT_FILLING):
943 * Filling in all of a previously delayed allocation extent.
944 * Neither the left nor right neighbors are contiguous with
947 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
949 xfs_bmbt_set_startblock(ep, new->br_startblock);
950 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
952 ip->i_df.if_lastex = idx;
953 ip->i_d.di_nextents++;
955 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
957 rval = XFS_ILOG_CORE;
958 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
959 new->br_startblock, new->br_blockcount,
963 cur->bc_rec.b.br_state = XFS_EXT_NORM;
964 if ((error = xfs_bmbt_insert(cur, &i)))
969 /* DELTA: The in-core extent described by new changed type. */
970 temp = new->br_startoff;
971 temp2 = new->br_blockcount;
974 case MASK2(LEFT_FILLING, LEFT_CONTIG):
976 * Filling in the first part of a previous delayed allocation.
977 * The left neighbor is contiguous.
979 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
981 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
982 LEFT.br_blockcount + new->br_blockcount);
983 xfs_bmbt_set_startoff(ep,
984 PREV.br_startoff + new->br_blockcount);
985 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
987 temp = PREV.br_blockcount - new->br_blockcount;
988 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
990 xfs_bmbt_set_blockcount(ep, temp);
991 ip->i_df.if_lastex = idx - 1;
993 rval = XFS_ILOG_DEXT;
996 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
997 LEFT.br_startblock, LEFT.br_blockcount,
1001 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1003 LEFT.br_blockcount +
1008 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1009 STARTBLOCKVAL(PREV.br_startblock));
1010 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1011 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
1014 /* DELTA: The boundary between two in-core extents moved. */
1015 temp = LEFT.br_startoff;
1016 temp2 = LEFT.br_blockcount +
1020 case MASK(LEFT_FILLING):
1022 * Filling in the first part of a previous delayed allocation.
1023 * The left neighbor is not contiguous.
1025 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1026 xfs_bmbt_set_startoff(ep, new_endoff);
1027 temp = PREV.br_blockcount - new->br_blockcount;
1028 xfs_bmbt_set_blockcount(ep, temp);
1029 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
1031 xfs_iext_insert(ifp, idx, 1, new);
1032 ip->i_df.if_lastex = idx;
1033 ip->i_d.di_nextents++;
1035 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1037 rval = XFS_ILOG_CORE;
1038 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1039 new->br_startblock, new->br_blockcount,
1043 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1044 if ((error = xfs_bmbt_insert(cur, &i)))
1048 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1049 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1050 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1051 first, flist, &cur, 1, &tmp_rval,
1057 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1058 STARTBLOCKVAL(PREV.br_startblock) -
1059 (cur ? cur->bc_private.b.allocated : 0));
1060 ep = xfs_iext_get_ext(ifp, idx + 1);
1061 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1062 xfs_bmap_trace_post_update(fname, "LF", ip, idx + 1,
1065 /* DELTA: One in-core extent is split in two. */
1066 temp = PREV.br_startoff;
1067 temp2 = PREV.br_blockcount;
1070 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1072 * Filling in the last part of a previous delayed allocation.
1073 * The right neighbor is contiguous with the new allocation.
1075 temp = PREV.br_blockcount - new->br_blockcount;
1076 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1078 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1080 xfs_bmbt_set_blockcount(ep, temp);
1081 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1082 new->br_startoff, new->br_startblock,
1083 new->br_blockcount + RIGHT.br_blockcount,
1085 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1087 ip->i_df.if_lastex = idx + 1;
1089 rval = XFS_ILOG_DEXT;
1092 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1093 RIGHT.br_startblock,
1094 RIGHT.br_blockcount, &i)))
1097 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1099 new->br_blockcount +
1100 RIGHT.br_blockcount,
1104 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1105 STARTBLOCKVAL(PREV.br_startblock));
1106 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1107 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1110 /* DELTA: The boundary between two in-core extents moved. */
1111 temp = PREV.br_startoff;
1112 temp2 = PREV.br_blockcount +
1113 RIGHT.br_blockcount;
1116 case MASK(RIGHT_FILLING):
1118 * Filling in the last part of a previous delayed allocation.
1119 * The right neighbor is not contiguous.
1121 temp = PREV.br_blockcount - new->br_blockcount;
1122 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1123 xfs_bmbt_set_blockcount(ep, temp);
1124 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1125 new, NULL, XFS_DATA_FORK);
1126 xfs_iext_insert(ifp, idx + 1, 1, new);
1127 ip->i_df.if_lastex = idx + 1;
1128 ip->i_d.di_nextents++;
1130 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1132 rval = XFS_ILOG_CORE;
1133 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1134 new->br_startblock, new->br_blockcount,
1138 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1139 if ((error = xfs_bmbt_insert(cur, &i)))
1143 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1144 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1145 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1146 first, flist, &cur, 1, &tmp_rval,
1152 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1153 STARTBLOCKVAL(PREV.br_startblock) -
1154 (cur ? cur->bc_private.b.allocated : 0));
1155 ep = xfs_iext_get_ext(ifp, idx);
1156 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1157 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1159 /* DELTA: One in-core extent is split in two. */
1160 temp = PREV.br_startoff;
1161 temp2 = PREV.br_blockcount;
1166 * Filling in the middle part of a previous delayed allocation.
1167 * Contiguity is impossible here.
1168 * This case is avoided almost all the time.
1170 temp = new->br_startoff - PREV.br_startoff;
1171 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1172 xfs_bmbt_set_blockcount(ep, temp);
1174 r[1].br_startoff = new_endoff;
1175 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1176 r[1].br_blockcount = temp2;
1177 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1179 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1180 ip->i_df.if_lastex = idx + 1;
1181 ip->i_d.di_nextents++;
1183 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1185 rval = XFS_ILOG_CORE;
1186 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1187 new->br_startblock, new->br_blockcount,
1191 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1192 if ((error = xfs_bmbt_insert(cur, &i)))
1196 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1197 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1198 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1199 first, flist, &cur, 1, &tmp_rval,
1205 temp = xfs_bmap_worst_indlen(ip, temp);
1206 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1207 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
1208 (cur ? cur->bc_private.b.allocated : 0));
1210 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -diff, rsvd)) {
1212 * Ick gross gag me with a spoon.
1214 ASSERT(0); /* want to see if this ever happens! */
1220 !xfs_mod_incore_sb(ip->i_mount,
1221 XFS_SBS_FDBLOCKS, -diff, rsvd))
1228 !xfs_mod_incore_sb(ip->i_mount,
1229 XFS_SBS_FDBLOCKS, -diff, rsvd))
1234 ep = xfs_iext_get_ext(ifp, idx);
1235 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1236 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1237 xfs_bmap_trace_pre_update(fname, "0", ip, idx + 2,
1239 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1240 NULLSTARTBLOCK((int)temp2));
1241 xfs_bmap_trace_post_update(fname, "0", ip, idx + 2,
1243 *dnew = temp + temp2;
1244 /* DELTA: One in-core extent is split in three. */
1245 temp = PREV.br_startoff;
1246 temp2 = PREV.br_blockcount;
1249 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1250 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1251 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1252 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1253 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1254 case MASK(LEFT_CONTIG):
1255 case MASK(RIGHT_CONTIG):
1257 * These cases are all impossible.
1264 if (delta->xed_startoff > temp)
1265 delta->xed_startoff = temp;
1266 if (delta->xed_blockcount < temp2)
1267 delta->xed_blockcount = temp2;
1281 #undef STATE_SET_TEST
1286 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1287 * allocation to a real allocation or vice versa.
1289 STATIC int /* error */
1290 xfs_bmap_add_extent_unwritten_real(
1291 xfs_inode_t *ip, /* incore inode pointer */
1292 xfs_extnum_t idx, /* extent number to update/insert */
1293 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1294 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1295 int *logflagsp, /* inode logging flags */
1296 xfs_extdelta_t *delta) /* Change made to incore extents */
1298 xfs_btree_cur_t *cur; /* btree cursor */
1299 xfs_bmbt_rec_t *ep; /* extent entry for idx */
1300 int error; /* error return value */
1301 #ifdef XFS_BMAP_TRACE
1302 static char fname[] = "xfs_bmap_add_extent_unwritten_real";
1304 int i; /* temp state */
1305 xfs_ifork_t *ifp; /* inode fork pointer */
1306 xfs_fileoff_t new_endoff; /* end offset of new entry */
1307 xfs_exntst_t newext; /* new extent state */
1308 xfs_exntst_t oldext; /* old extent state */
1309 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1310 /* left is 0, right is 1, prev is 2 */
1311 int rval=0; /* return value (logging flags) */
1312 int state = 0;/* state bits, accessed thru macros */
1313 xfs_filblks_t temp=0;
1314 xfs_filblks_t temp2=0;
1315 enum { /* bit number definitions for state */
1316 LEFT_CONTIG, RIGHT_CONTIG,
1317 LEFT_FILLING, RIGHT_FILLING,
1318 LEFT_DELAY, RIGHT_DELAY,
1319 LEFT_VALID, RIGHT_VALID
1325 #define MASK(b) (1 << (b))
1326 #define MASK2(a,b) (MASK(a) | MASK(b))
1327 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1328 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1329 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1330 #define STATE_TEST(b) (state & MASK(b))
1331 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1332 ((state &= ~MASK(b)), 0))
1333 #define SWITCH_STATE \
1334 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1337 * Set up a bunch of variables to make the tests simpler.
1341 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1342 ep = xfs_iext_get_ext(ifp, idx);
1343 xfs_bmbt_get_all(ep, &PREV);
1344 newext = new->br_state;
1345 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1346 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1347 ASSERT(PREV.br_state == oldext);
1348 new_endoff = new->br_startoff + new->br_blockcount;
1349 ASSERT(PREV.br_startoff <= new->br_startoff);
1350 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1352 * Set flags determining what part of the previous oldext allocation
1353 * extent is being replaced by a newext allocation.
1355 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
1356 STATE_SET(RIGHT_FILLING,
1357 PREV.br_startoff + PREV.br_blockcount == new_endoff);
1359 * Check and set flags if this segment has a left neighbor.
1360 * Don't set contiguous if the combined extent would be too large.
1362 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1363 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1364 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
1366 STATE_SET(LEFT_CONTIG,
1367 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1368 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1369 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1370 LEFT.br_state == newext &&
1371 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1373 * Check and set flags if this segment has a right neighbor.
1374 * Don't set contiguous if the combined extent would be too large.
1375 * Also check for all-three-contiguous being too large.
1377 if (STATE_SET_TEST(RIGHT_VALID,
1379 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
1380 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1381 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
1383 STATE_SET(RIGHT_CONTIG,
1384 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1385 new_endoff == RIGHT.br_startoff &&
1386 new->br_startblock + new->br_blockcount ==
1387 RIGHT.br_startblock &&
1388 newext == RIGHT.br_state &&
1389 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1390 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
1391 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
1392 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1395 * Switch out based on the FILLING and CONTIG state bits.
1397 switch (SWITCH_STATE) {
1399 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1401 * Setting all of a previous oldext extent to newext.
1402 * The left and right neighbors are both contiguous with new.
1404 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1406 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1407 LEFT.br_blockcount + PREV.br_blockcount +
1408 RIGHT.br_blockcount);
1409 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1411 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
1413 xfs_iext_remove(ifp, idx, 2);
1414 ip->i_df.if_lastex = idx - 1;
1415 ip->i_d.di_nextents -= 2;
1417 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1419 rval = XFS_ILOG_CORE;
1420 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1421 RIGHT.br_startblock,
1422 RIGHT.br_blockcount, &i)))
1425 if ((error = xfs_bmbt_delete(cur, &i)))
1428 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1431 if ((error = xfs_bmbt_delete(cur, &i)))
1434 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1437 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1439 LEFT.br_blockcount + PREV.br_blockcount +
1440 RIGHT.br_blockcount, LEFT.br_state)))
1443 /* DELTA: Three in-core extents are replaced by one. */
1444 temp = LEFT.br_startoff;
1445 temp2 = LEFT.br_blockcount +
1446 PREV.br_blockcount +
1447 RIGHT.br_blockcount;
1450 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
1452 * Setting all of a previous oldext extent to newext.
1453 * The left neighbor is contiguous, the right is not.
1455 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
1457 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1458 LEFT.br_blockcount + PREV.br_blockcount);
1459 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
1461 ip->i_df.if_lastex = idx - 1;
1462 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
1464 xfs_iext_remove(ifp, idx, 1);
1465 ip->i_d.di_nextents--;
1467 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1469 rval = XFS_ILOG_CORE;
1470 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1471 PREV.br_startblock, PREV.br_blockcount,
1475 if ((error = xfs_bmbt_delete(cur, &i)))
1478 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1481 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1483 LEFT.br_blockcount + PREV.br_blockcount,
1487 /* DELTA: Two in-core extents are replaced by one. */
1488 temp = LEFT.br_startoff;
1489 temp2 = LEFT.br_blockcount +
1493 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
1495 * Setting all of a previous oldext extent to newext.
1496 * The right neighbor is contiguous, the left is not.
1498 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
1500 xfs_bmbt_set_blockcount(ep,
1501 PREV.br_blockcount + RIGHT.br_blockcount);
1502 xfs_bmbt_set_state(ep, newext);
1503 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
1505 ip->i_df.if_lastex = idx;
1506 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
1508 xfs_iext_remove(ifp, idx + 1, 1);
1509 ip->i_d.di_nextents--;
1511 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1513 rval = XFS_ILOG_CORE;
1514 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1515 RIGHT.br_startblock,
1516 RIGHT.br_blockcount, &i)))
1519 if ((error = xfs_bmbt_delete(cur, &i)))
1522 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1525 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1527 new->br_blockcount + RIGHT.br_blockcount,
1531 /* DELTA: Two in-core extents are replaced by one. */
1532 temp = PREV.br_startoff;
1533 temp2 = PREV.br_blockcount +
1534 RIGHT.br_blockcount;
1537 case MASK2(LEFT_FILLING, RIGHT_FILLING):
1539 * Setting all of a previous oldext extent to newext.
1540 * Neither the left nor right neighbors are contiguous with
1543 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
1545 xfs_bmbt_set_state(ep, newext);
1546 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
1548 ip->i_df.if_lastex = idx;
1550 rval = XFS_ILOG_DEXT;
1553 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1554 new->br_startblock, new->br_blockcount,
1558 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1559 new->br_startblock, new->br_blockcount,
1563 /* DELTA: The in-core extent described by new changed type. */
1564 temp = new->br_startoff;
1565 temp2 = new->br_blockcount;
1568 case MASK2(LEFT_FILLING, LEFT_CONTIG):
1570 * Setting the first part of a previous oldext extent to newext.
1571 * The left neighbor is contiguous.
1573 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
1575 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1576 LEFT.br_blockcount + new->br_blockcount);
1577 xfs_bmbt_set_startoff(ep,
1578 PREV.br_startoff + new->br_blockcount);
1579 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
1581 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
1583 xfs_bmbt_set_startblock(ep,
1584 new->br_startblock + new->br_blockcount);
1585 xfs_bmbt_set_blockcount(ep,
1586 PREV.br_blockcount - new->br_blockcount);
1587 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
1589 ip->i_df.if_lastex = idx - 1;
1591 rval = XFS_ILOG_DEXT;
1594 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1595 PREV.br_startblock, PREV.br_blockcount,
1599 if ((error = xfs_bmbt_update(cur,
1600 PREV.br_startoff + new->br_blockcount,
1601 PREV.br_startblock + new->br_blockcount,
1602 PREV.br_blockcount - new->br_blockcount,
1605 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1607 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1609 LEFT.br_blockcount + new->br_blockcount,
1613 /* DELTA: The boundary between two in-core extents moved. */
1614 temp = LEFT.br_startoff;
1615 temp2 = LEFT.br_blockcount +
1619 case MASK(LEFT_FILLING):
1621 * Setting the first part of a previous oldext extent to newext.
1622 * The left neighbor is not contiguous.
1624 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1625 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1626 xfs_bmbt_set_startoff(ep, new_endoff);
1627 xfs_bmbt_set_blockcount(ep,
1628 PREV.br_blockcount - new->br_blockcount);
1629 xfs_bmbt_set_startblock(ep,
1630 new->br_startblock + new->br_blockcount);
1631 xfs_bmap_trace_post_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1632 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
1634 xfs_iext_insert(ifp, idx, 1, new);
1635 ip->i_df.if_lastex = idx;
1636 ip->i_d.di_nextents++;
1638 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1640 rval = XFS_ILOG_CORE;
1641 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1642 PREV.br_startblock, PREV.br_blockcount,
1646 if ((error = xfs_bmbt_update(cur,
1647 PREV.br_startoff + new->br_blockcount,
1648 PREV.br_startblock + new->br_blockcount,
1649 PREV.br_blockcount - new->br_blockcount,
1652 cur->bc_rec.b = *new;
1653 if ((error = xfs_bmbt_insert(cur, &i)))
1657 /* DELTA: One in-core extent is split in two. */
1658 temp = PREV.br_startoff;
1659 temp2 = PREV.br_blockcount;
1662 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1664 * Setting the last part of a previous oldext extent to newext.
1665 * The right neighbor is contiguous with the new allocation.
1667 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1669 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1671 xfs_bmbt_set_blockcount(ep,
1672 PREV.br_blockcount - new->br_blockcount);
1673 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1675 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1676 new->br_startoff, new->br_startblock,
1677 new->br_blockcount + RIGHT.br_blockcount, newext);
1678 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1680 ip->i_df.if_lastex = idx + 1;
1682 rval = XFS_ILOG_DEXT;
1685 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1687 PREV.br_blockcount, &i)))
1690 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1692 PREV.br_blockcount - new->br_blockcount,
1695 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1697 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1699 new->br_blockcount + RIGHT.br_blockcount,
1703 /* DELTA: The boundary between two in-core extents moved. */
1704 temp = PREV.br_startoff;
1705 temp2 = PREV.br_blockcount +
1706 RIGHT.br_blockcount;
1709 case MASK(RIGHT_FILLING):
1711 * Setting the last part of a previous oldext extent to newext.
1712 * The right neighbor is not contiguous.
1714 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1715 xfs_bmbt_set_blockcount(ep,
1716 PREV.br_blockcount - new->br_blockcount);
1717 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1718 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1719 new, NULL, XFS_DATA_FORK);
1720 xfs_iext_insert(ifp, idx + 1, 1, new);
1721 ip->i_df.if_lastex = idx + 1;
1722 ip->i_d.di_nextents++;
1724 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1726 rval = XFS_ILOG_CORE;
1727 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1728 PREV.br_startblock, PREV.br_blockcount,
1732 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1734 PREV.br_blockcount - new->br_blockcount,
1737 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1738 new->br_startblock, new->br_blockcount,
1742 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1743 if ((error = xfs_bmbt_insert(cur, &i)))
1747 /* DELTA: One in-core extent is split in two. */
1748 temp = PREV.br_startoff;
1749 temp2 = PREV.br_blockcount;
1754 * Setting the middle part of a previous oldext extent to
1755 * newext. Contiguity is impossible here.
1756 * One extent becomes three extents.
1758 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1759 xfs_bmbt_set_blockcount(ep,
1760 new->br_startoff - PREV.br_startoff);
1761 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1763 r[1].br_startoff = new_endoff;
1764 r[1].br_blockcount =
1765 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1766 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1767 r[1].br_state = oldext;
1768 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1770 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1771 ip->i_df.if_lastex = idx + 1;
1772 ip->i_d.di_nextents += 2;
1774 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1776 rval = XFS_ILOG_CORE;
1777 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1778 PREV.br_startblock, PREV.br_blockcount,
1782 /* new right extent - oldext */
1783 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1784 r[1].br_startblock, r[1].br_blockcount,
1787 /* new left extent - oldext */
1788 PREV.br_blockcount =
1789 new->br_startoff - PREV.br_startoff;
1790 cur->bc_rec.b = PREV;
1791 if ((error = xfs_bmbt_insert(cur, &i)))
1794 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1797 /* new middle extent - newext */
1798 cur->bc_rec.b = *new;
1799 if ((error = xfs_bmbt_insert(cur, &i)))
1803 /* DELTA: One in-core extent is split in three. */
1804 temp = PREV.br_startoff;
1805 temp2 = PREV.br_blockcount;
1808 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1809 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1810 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1811 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1812 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1813 case MASK(LEFT_CONTIG):
1814 case MASK(RIGHT_CONTIG):
1816 * These cases are all impossible.
1823 if (delta->xed_startoff > temp)
1824 delta->xed_startoff = temp;
1825 if (delta->xed_blockcount < temp2)
1826 delta->xed_blockcount = temp2;
1840 #undef STATE_SET_TEST
1845 * Called by xfs_bmap_add_extent to handle cases converting a hole
1846 * to a delayed allocation.
1849 STATIC int /* error */
1850 xfs_bmap_add_extent_hole_delay(
1851 xfs_inode_t *ip, /* incore inode pointer */
1852 xfs_extnum_t idx, /* extent number to update/insert */
1853 xfs_btree_cur_t *cur, /* if null, not a btree */
1854 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1855 int *logflagsp, /* inode logging flags */
1856 xfs_extdelta_t *delta, /* Change made to incore extents */
1857 int rsvd) /* OK to allocate reserved blocks */
1859 xfs_bmbt_rec_t *ep; /* extent record for idx */
1860 #ifdef XFS_BMAP_TRACE
1861 static char fname[] = "xfs_bmap_add_extent_hole_delay";
1863 xfs_ifork_t *ifp; /* inode fork pointer */
1864 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1865 xfs_filblks_t newlen=0; /* new indirect size */
1866 xfs_filblks_t oldlen=0; /* old indirect size */
1867 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1868 int state; /* state bits, accessed thru macros */
1869 xfs_filblks_t temp=0; /* temp for indirect calculations */
1870 xfs_filblks_t temp2=0;
1871 enum { /* bit number definitions for state */
1872 LEFT_CONTIG, RIGHT_CONTIG,
1873 LEFT_DELAY, RIGHT_DELAY,
1874 LEFT_VALID, RIGHT_VALID
1877 #define MASK(b) (1 << (b))
1878 #define MASK2(a,b) (MASK(a) | MASK(b))
1879 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1880 #define STATE_TEST(b) (state & MASK(b))
1881 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1882 ((state &= ~MASK(b)), 0))
1883 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1885 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1886 ep = xfs_iext_get_ext(ifp, idx);
1888 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1890 * Check and set flags if this segment has a left neighbor
1892 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1893 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1894 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1897 * Check and set flags if the current (right) segment exists.
1898 * If it doesn't exist, we're converting the hole at end-of-file.
1900 if (STATE_SET_TEST(RIGHT_VALID,
1902 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1903 xfs_bmbt_get_all(ep, &right);
1904 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1907 * Set contiguity flags on the left and right neighbors.
1908 * Don't let extents get too large, even if the pieces are contiguous.
1910 STATE_SET(LEFT_CONTIG,
1911 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1912 left.br_startoff + left.br_blockcount == new->br_startoff &&
1913 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1914 STATE_SET(RIGHT_CONTIG,
1915 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1916 new->br_startoff + new->br_blockcount == right.br_startoff &&
1917 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1918 (!STATE_TEST(LEFT_CONTIG) ||
1919 (left.br_blockcount + new->br_blockcount +
1920 right.br_blockcount <= MAXEXTLEN)));
1922 * Switch out based on the contiguity flags.
1924 switch (SWITCH_STATE) {
1926 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1928 * New allocation is contiguous with delayed allocations
1929 * on the left and on the right.
1930 * Merge all three into a single extent record.
1932 temp = left.br_blockcount + new->br_blockcount +
1933 right.br_blockcount;
1934 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
1936 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1937 oldlen = STARTBLOCKVAL(left.br_startblock) +
1938 STARTBLOCKVAL(new->br_startblock) +
1939 STARTBLOCKVAL(right.br_startblock);
1940 newlen = xfs_bmap_worst_indlen(ip, temp);
1941 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1942 NULLSTARTBLOCK((int)newlen));
1943 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
1945 xfs_bmap_trace_delete(fname, "LC|RC", ip, idx, 1,
1947 xfs_iext_remove(ifp, idx, 1);
1948 ip->i_df.if_lastex = idx - 1;
1949 /* DELTA: Two in-core extents were replaced by one. */
1951 temp = left.br_startoff;
1954 case MASK(LEFT_CONTIG):
1956 * New allocation is contiguous with a delayed allocation
1958 * Merge the new allocation with the left neighbor.
1960 temp = left.br_blockcount + new->br_blockcount;
1961 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1,
1963 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1964 oldlen = STARTBLOCKVAL(left.br_startblock) +
1965 STARTBLOCKVAL(new->br_startblock);
1966 newlen = xfs_bmap_worst_indlen(ip, temp);
1967 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1968 NULLSTARTBLOCK((int)newlen));
1969 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1,
1971 ip->i_df.if_lastex = idx - 1;
1972 /* DELTA: One in-core extent grew into a hole. */
1974 temp = left.br_startoff;
1977 case MASK(RIGHT_CONTIG):
1979 * New allocation is contiguous with a delayed allocation
1981 * Merge the new allocation with the right neighbor.
1983 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1984 temp = new->br_blockcount + right.br_blockcount;
1985 oldlen = STARTBLOCKVAL(new->br_startblock) +
1986 STARTBLOCKVAL(right.br_startblock);
1987 newlen = xfs_bmap_worst_indlen(ip, temp);
1988 xfs_bmbt_set_allf(ep, new->br_startoff,
1989 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
1990 xfs_bmap_trace_post_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1991 ip->i_df.if_lastex = idx;
1992 /* DELTA: One in-core extent grew into a hole. */
1994 temp = new->br_startoff;
1999 * New allocation is not contiguous with another
2000 * delayed allocation.
2001 * Insert a new entry.
2003 oldlen = newlen = 0;
2004 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
2006 xfs_iext_insert(ifp, idx, 1, new);
2007 ip->i_df.if_lastex = idx;
2008 /* DELTA: A new in-core extent was added in a hole. */
2009 temp2 = new->br_blockcount;
2010 temp = new->br_startoff;
2013 if (oldlen != newlen) {
2014 ASSERT(oldlen > newlen);
2015 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
2016 (int)(oldlen - newlen), rsvd);
2018 * Nothing to do for disk quota accounting here.
2023 if (delta->xed_startoff > temp)
2024 delta->xed_startoff = temp;
2025 if (delta->xed_blockcount < temp2)
2026 delta->xed_blockcount = temp2;
2034 #undef STATE_SET_TEST
2039 * Called by xfs_bmap_add_extent to handle cases converting a hole
2040 * to a real allocation.
2042 STATIC int /* error */
2043 xfs_bmap_add_extent_hole_real(
2044 xfs_inode_t *ip, /* incore inode pointer */
2045 xfs_extnum_t idx, /* extent number to update/insert */
2046 xfs_btree_cur_t *cur, /* if null, not a btree */
2047 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2048 int *logflagsp, /* inode logging flags */
2049 xfs_extdelta_t *delta, /* Change made to incore extents */
2050 int whichfork) /* data or attr fork */
2052 xfs_bmbt_rec_t *ep; /* pointer to extent entry ins. point */
2053 int error; /* error return value */
2054 #ifdef XFS_BMAP_TRACE
2055 static char fname[] = "xfs_bmap_add_extent_hole_real";
2057 int i; /* temp state */
2058 xfs_ifork_t *ifp; /* inode fork pointer */
2059 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2060 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2061 int rval=0; /* return value (logging flags) */
2062 int state; /* state bits, accessed thru macros */
2063 xfs_filblks_t temp=0;
2064 xfs_filblks_t temp2=0;
2065 enum { /* bit number definitions for state */
2066 LEFT_CONTIG, RIGHT_CONTIG,
2067 LEFT_DELAY, RIGHT_DELAY,
2068 LEFT_VALID, RIGHT_VALID
2071 #define MASK(b) (1 << (b))
2072 #define MASK2(a,b) (MASK(a) | MASK(b))
2073 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
2074 #define STATE_TEST(b) (state & MASK(b))
2075 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
2076 ((state &= ~MASK(b)), 0))
2077 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
2079 ifp = XFS_IFORK_PTR(ip, whichfork);
2080 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
2081 ep = xfs_iext_get_ext(ifp, idx);
2084 * Check and set flags if this segment has a left neighbor.
2086 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
2087 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
2088 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
2091 * Check and set flags if this segment has a current value.
2092 * Not true if we're inserting into the "hole" at eof.
2094 if (STATE_SET_TEST(RIGHT_VALID,
2096 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
2097 xfs_bmbt_get_all(ep, &right);
2098 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
2101 * We're inserting a real allocation between "left" and "right".
2102 * Set the contiguity flags. Don't let extents get too large.
2104 STATE_SET(LEFT_CONTIG,
2105 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
2106 left.br_startoff + left.br_blockcount == new->br_startoff &&
2107 left.br_startblock + left.br_blockcount == new->br_startblock &&
2108 left.br_state == new->br_state &&
2109 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
2110 STATE_SET(RIGHT_CONTIG,
2111 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
2112 new->br_startoff + new->br_blockcount == right.br_startoff &&
2113 new->br_startblock + new->br_blockcount ==
2114 right.br_startblock &&
2115 new->br_state == right.br_state &&
2116 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2117 (!STATE_TEST(LEFT_CONTIG) ||
2118 left.br_blockcount + new->br_blockcount +
2119 right.br_blockcount <= MAXEXTLEN));
2123 * Select which case we're in here, and implement it.
2125 switch (SWITCH_STATE) {
2127 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
2129 * New allocation is contiguous with real allocations on the
2130 * left and on the right.
2131 * Merge all three into a single extent record.
2133 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
2135 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2136 left.br_blockcount + new->br_blockcount +
2137 right.br_blockcount);
2138 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
2140 xfs_bmap_trace_delete(fname, "LC|RC", ip,
2142 xfs_iext_remove(ifp, idx, 1);
2143 ifp->if_lastex = idx - 1;
2144 XFS_IFORK_NEXT_SET(ip, whichfork,
2145 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2147 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2149 rval = XFS_ILOG_CORE;
2150 if ((error = xfs_bmbt_lookup_eq(cur,
2152 right.br_startblock,
2153 right.br_blockcount, &i)))
2156 if ((error = xfs_bmbt_delete(cur, &i)))
2159 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2162 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2164 left.br_blockcount +
2165 new->br_blockcount +
2166 right.br_blockcount,
2170 /* DELTA: Two in-core extents were replaced by one. */
2171 temp = left.br_startoff;
2172 temp2 = left.br_blockcount +
2173 new->br_blockcount +
2174 right.br_blockcount;
2177 case MASK(LEFT_CONTIG):
2179 * New allocation is contiguous with a real allocation
2181 * Merge the new allocation with the left neighbor.
2183 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1, whichfork);
2184 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2185 left.br_blockcount + new->br_blockcount);
2186 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1, whichfork);
2187 ifp->if_lastex = idx - 1;
2189 rval = XFS_ILOG_FEXT(whichfork);
2192 if ((error = xfs_bmbt_lookup_eq(cur,
2195 left.br_blockcount, &i)))
2198 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2200 left.br_blockcount +
2205 /* DELTA: One in-core extent grew. */
2206 temp = left.br_startoff;
2207 temp2 = left.br_blockcount +
2211 case MASK(RIGHT_CONTIG):
2213 * New allocation is contiguous with a real allocation
2215 * Merge the new allocation with the right neighbor.
2217 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, whichfork);
2218 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2219 new->br_blockcount + right.br_blockcount,
2221 xfs_bmap_trace_post_update(fname, "RC", ip, idx, whichfork);
2222 ifp->if_lastex = idx;
2224 rval = XFS_ILOG_FEXT(whichfork);
2227 if ((error = xfs_bmbt_lookup_eq(cur,
2229 right.br_startblock,
2230 right.br_blockcount, &i)))
2233 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2235 new->br_blockcount +
2236 right.br_blockcount,
2240 /* DELTA: One in-core extent grew. */
2241 temp = new->br_startoff;
2242 temp2 = new->br_blockcount +
2243 right.br_blockcount;
2248 * New allocation is not contiguous with another
2250 * Insert a new entry.
2252 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
2254 xfs_iext_insert(ifp, idx, 1, new);
2255 ifp->if_lastex = idx;
2256 XFS_IFORK_NEXT_SET(ip, whichfork,
2257 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2259 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2261 rval = XFS_ILOG_CORE;
2262 if ((error = xfs_bmbt_lookup_eq(cur,
2265 new->br_blockcount, &i)))
2268 cur->bc_rec.b.br_state = new->br_state;
2269 if ((error = xfs_bmbt_insert(cur, &i)))
2273 /* DELTA: A new extent was added in a hole. */
2274 temp = new->br_startoff;
2275 temp2 = new->br_blockcount;
2280 if (delta->xed_startoff > temp)
2281 delta->xed_startoff = temp;
2282 if (delta->xed_blockcount < temp2)
2283 delta->xed_blockcount = temp2;
2292 #undef STATE_SET_TEST
2297 * Adjust the size of the new extent based on di_extsize and rt extsize.
2300 xfs_bmap_extsize_align(
2302 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2303 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2304 xfs_extlen_t extsz, /* align to this extent size */
2305 int rt, /* is this a realtime inode? */
2306 int eof, /* is extent at end-of-file? */
2307 int delay, /* creating delalloc extent? */
2308 int convert, /* overwriting unwritten extent? */
2309 xfs_fileoff_t *offp, /* in/out: aligned offset */
2310 xfs_extlen_t *lenp) /* in/out: aligned length */
2312 xfs_fileoff_t orig_off; /* original offset */
2313 xfs_extlen_t orig_alen; /* original length */
2314 xfs_fileoff_t orig_end; /* original off+len */
2315 xfs_fileoff_t nexto; /* next file offset */
2316 xfs_fileoff_t prevo; /* previous file offset */
2317 xfs_fileoff_t align_off; /* temp for offset */
2318 xfs_extlen_t align_alen; /* temp for length */
2319 xfs_extlen_t temp; /* temp for calculations */
2324 orig_off = align_off = *offp;
2325 orig_alen = align_alen = *lenp;
2326 orig_end = orig_off + orig_alen;
2329 * If this request overlaps an existing extent, then don't
2330 * attempt to perform any additional alignment.
2332 if (!delay && !eof &&
2333 (orig_off >= gotp->br_startoff) &&
2334 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2339 * If the file offset is unaligned vs. the extent size
2340 * we need to align it. This will be possible unless
2341 * the file was previously written with a kernel that didn't
2342 * perform this alignment, or if a truncate shot us in the
2345 temp = do_mod(orig_off, extsz);
2351 * Same adjustment for the end of the requested area.
2353 if ((temp = (align_alen % extsz))) {
2354 align_alen += extsz - temp;
2357 * If the previous block overlaps with this proposed allocation
2358 * then move the start forward without adjusting the length.
2360 if (prevp->br_startoff != NULLFILEOFF) {
2361 if (prevp->br_startblock == HOLESTARTBLOCK)
2362 prevo = prevp->br_startoff;
2364 prevo = prevp->br_startoff + prevp->br_blockcount;
2367 if (align_off != orig_off && align_off < prevo)
2370 * If the next block overlaps with this proposed allocation
2371 * then move the start back without adjusting the length,
2372 * but not before offset 0.
2373 * This may of course make the start overlap previous block,
2374 * and if we hit the offset 0 limit then the next block
2375 * can still overlap too.
2377 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2378 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2379 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2380 nexto = gotp->br_startoff + gotp->br_blockcount;
2382 nexto = gotp->br_startoff;
2384 nexto = NULLFILEOFF;
2386 align_off + align_alen != orig_end &&
2387 align_off + align_alen > nexto)
2388 align_off = nexto > align_alen ? nexto - align_alen : 0;
2390 * If we're now overlapping the next or previous extent that
2391 * means we can't fit an extsz piece in this hole. Just move
2392 * the start forward to the first valid spot and set
2393 * the length so we hit the end.
2395 if (align_off != orig_off && align_off < prevo)
2397 if (align_off + align_alen != orig_end &&
2398 align_off + align_alen > nexto &&
2399 nexto != NULLFILEOFF) {
2400 ASSERT(nexto > prevo);
2401 align_alen = nexto - align_off;
2405 * If realtime, and the result isn't a multiple of the realtime
2406 * extent size we need to remove blocks until it is.
2408 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2410 * We're not covering the original request, or
2411 * we won't be able to once we fix the length.
2413 if (orig_off < align_off ||
2414 orig_end > align_off + align_alen ||
2415 align_alen - temp < orig_alen)
2416 return XFS_ERROR(EINVAL);
2418 * Try to fix it by moving the start up.
2420 if (align_off + temp <= orig_off) {
2425 * Try to fix it by moving the end in.
2427 else if (align_off + align_alen - temp >= orig_end)
2430 * Set the start to the minimum then trim the length.
2433 align_alen -= orig_off - align_off;
2434 align_off = orig_off;
2435 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2438 * Result doesn't cover the request, fail it.
2440 if (orig_off < align_off || orig_end > align_off + align_alen)
2441 return XFS_ERROR(EINVAL);
2443 ASSERT(orig_off >= align_off);
2444 ASSERT(orig_end <= align_off + align_alen);
2448 if (!eof && gotp->br_startoff != NULLFILEOFF)
2449 ASSERT(align_off + align_alen <= gotp->br_startoff);
2450 if (prevp->br_startoff != NULLFILEOFF)
2451 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2459 #define XFS_ALLOC_GAP_UNITS 4
2463 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2465 xfs_fsblock_t adjust; /* adjustment to block numbers */
2466 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2467 xfs_mount_t *mp; /* mount point structure */
2468 int nullfb; /* true if ap->firstblock isn't set */
2469 int rt; /* true if inode is realtime */
2471 #define ISVALID(x,y) \
2473 (x) < mp->m_sb.sb_rblocks : \
2474 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2475 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2476 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2478 mp = ap->ip->i_mount;
2479 nullfb = ap->firstblock == NULLFSBLOCK;
2480 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2481 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2483 * If allocating at eof, and there's a previous real block,
2484 * try to use it's last block as our starting point.
2486 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2487 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2488 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2489 ap->prevp->br_startblock)) {
2490 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2492 * Adjust for the gap between prevp and us.
2495 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2497 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2501 * If not at eof, then compare the two neighbor blocks.
2502 * Figure out whether either one gives us a good starting point,
2503 * and pick the better one.
2505 else if (!ap->eof) {
2506 xfs_fsblock_t gotbno; /* right side block number */
2507 xfs_fsblock_t gotdiff=0; /* right side difference */
2508 xfs_fsblock_t prevbno; /* left side block number */
2509 xfs_fsblock_t prevdiff=0; /* left side difference */
2512 * If there's a previous (left) block, select a requested
2513 * start block based on it.
2515 if (ap->prevp->br_startoff != NULLFILEOFF &&
2516 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2517 (prevbno = ap->prevp->br_startblock +
2518 ap->prevp->br_blockcount) &&
2519 ISVALID(prevbno, ap->prevp->br_startblock)) {
2521 * Calculate gap to end of previous block.
2523 adjust = prevdiff = ap->off -
2524 (ap->prevp->br_startoff +
2525 ap->prevp->br_blockcount);
2527 * Figure the startblock based on the previous block's
2528 * end and the gap size.
2530 * If the gap is large relative to the piece we're
2531 * allocating, or using it gives us an invalid block
2532 * number, then just use the end of the previous block.
2534 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2535 ISVALID(prevbno + prevdiff,
2536 ap->prevp->br_startblock))
2541 * If the firstblock forbids it, can't use it,
2544 if (!rt && !nullfb &&
2545 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2546 prevbno = NULLFSBLOCK;
2549 * No previous block or can't follow it, just default.
2552 prevbno = NULLFSBLOCK;
2554 * If there's a following (right) block, select a requested
2555 * start block based on it.
2557 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
2559 * Calculate gap to start of next block.
2561 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2563 * Figure the startblock based on the next block's
2564 * start and the gap size.
2566 gotbno = ap->gotp->br_startblock;
2569 * If the gap is large relative to the piece we're
2570 * allocating, or using it gives us an invalid block
2571 * number, then just use the start of the next block
2572 * offset by our length.
2574 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2575 ISVALID(gotbno - gotdiff, gotbno))
2577 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2579 gotdiff += adjust - ap->alen;
2583 * If the firstblock forbids it, can't use it,
2586 if (!rt && !nullfb &&
2587 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2588 gotbno = NULLFSBLOCK;
2591 * No next block, just default.
2594 gotbno = NULLFSBLOCK;
2596 * If both valid, pick the better one, else the only good
2597 * one, else ap->rval is already set (to 0 or the inode block).
2599 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2600 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2601 else if (prevbno != NULLFSBLOCK)
2603 else if (gotbno != NULLFSBLOCK)
2612 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2614 xfs_alloctype_t atype = 0; /* type for allocation routines */
2615 int error; /* error return value */
2616 xfs_mount_t *mp; /* mount point structure */
2617 xfs_extlen_t prod = 0; /* product factor for allocators */
2618 xfs_extlen_t ralen = 0; /* realtime allocation length */
2619 xfs_extlen_t align; /* minimum allocation alignment */
2620 xfs_rtblock_t rtx; /* realtime extent number */
2623 mp = ap->ip->i_mount;
2624 align = ap->ip->i_d.di_extsize ?
2625 ap->ip->i_d.di_extsize : mp->m_sb.sb_rextsize;
2626 prod = align / mp->m_sb.sb_rextsize;
2627 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2628 align, 1, ap->eof, 0,
2629 ap->conv, &ap->off, &ap->alen);
2633 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2636 * If the offset & length are not perfectly aligned
2637 * then kill prod, it will just get us in trouble.
2639 if (do_mod(ap->off, align) || ap->alen % align)
2642 * Set ralen to be the actual requested length in rtextents.
2644 ralen = ap->alen / mp->m_sb.sb_rextsize;
2646 * If the old value was close enough to MAXEXTLEN that
2647 * we rounded up to it, cut it back so it's valid again.
2648 * Note that if it's a really large request (bigger than
2649 * MAXEXTLEN), we don't hear about that number, and can't
2650 * adjust the starting point to match it.
2652 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2653 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2655 * If it's an allocation to an empty file at offset 0,
2656 * pick an extent that will space things out in the rt area.
2658 if (ap->eof && ap->off == 0) {
2659 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2662 ap->rval = rtx * mp->m_sb.sb_rextsize;
2667 xfs_bmap_adjacent(ap);
2670 * Realtime allocation, done through xfs_rtallocate_extent.
2672 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2673 do_div(ap->rval, mp->m_sb.sb_rextsize);
2676 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2677 &ralen, atype, ap->wasdel, prod, &rtb)))
2679 if (rtb == NULLFSBLOCK && prod > 1 &&
2680 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2681 ap->alen, &ralen, atype,
2682 ap->wasdel, 1, &rtb)))
2685 if (ap->rval != NULLFSBLOCK) {
2686 ap->rval *= mp->m_sb.sb_rextsize;
2687 ralen *= mp->m_sb.sb_rextsize;
2689 ap->ip->i_d.di_nblocks += ralen;
2690 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2692 ap->ip->i_delayed_blks -= ralen;
2694 * Adjust the disk quota also. This was reserved
2697 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2698 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2699 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2708 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2710 xfs_mount_t *mp; /* mount point structure */
2711 xfs_alloctype_t atype = 0; /* type for allocation routines */
2712 xfs_extlen_t align; /* minimum allocation alignment */
2714 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2715 xfs_agnumber_t startag;
2716 xfs_alloc_arg_t args;
2719 xfs_extlen_t longest;
2721 xfs_extlen_t nextminlen = 0;
2723 int nullfb; /* true if ap->firstblock isn't set */
2729 mp = ap->ip->i_mount;
2730 align = (ap->userdata && ap->ip->i_d.di_extsize &&
2731 (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)) ?
2732 ap->ip->i_d.di_extsize : 0;
2733 if (unlikely(align)) {
2734 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2735 align, 0, ap->eof, 0, ap->conv,
2736 &ap->off, &ap->alen);
2740 nullfb = ap->firstblock == NULLFSBLOCK;
2741 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2743 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2745 ap->rval = ap->firstblock;
2747 xfs_bmap_adjacent(ap);
2750 * If allowed, use ap->rval; otherwise must use firstblock since
2751 * it's in the right allocation group.
2753 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2756 ap->rval = ap->firstblock;
2758 * Normal allocation, done through xfs_alloc_vextent.
2760 tryagain = isaligned = 0;
2763 args.fsbno = ap->rval;
2764 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2765 args.firstblock = ap->firstblock;
2768 args.type = XFS_ALLOCTYPE_START_BNO;
2769 args.total = ap->total;
2771 * Find the longest available space.
2772 * We're going to try for the whole allocation at once.
2774 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2776 down_read(&mp->m_peraglock);
2777 while (blen < ap->alen) {
2778 pag = &mp->m_perag[ag];
2779 if (!pag->pagf_init &&
2780 (error = xfs_alloc_pagf_init(mp, args.tp,
2781 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2782 up_read(&mp->m_peraglock);
2786 * See xfs_alloc_fix_freelist...
2788 if (pag->pagf_init) {
2789 need = XFS_MIN_FREELIST_PAG(pag, mp);
2790 delta = need > pag->pagf_flcount ?
2791 need - pag->pagf_flcount : 0;
2792 longest = (pag->pagf_longest > delta) ?
2793 (pag->pagf_longest - delta) :
2794 (pag->pagf_flcount > 0 ||
2795 pag->pagf_longest > 0);
2800 if (++ag == mp->m_sb.sb_agcount)
2805 up_read(&mp->m_peraglock);
2807 * Since the above loop did a BUF_TRYLOCK, it is
2808 * possible that there is space for this request.
2810 if (notinit || blen < ap->minlen)
2811 args.minlen = ap->minlen;
2813 * If the best seen length is less than the request
2814 * length, use the best as the minimum.
2816 else if (blen < ap->alen)
2819 * Otherwise we've seen an extent as big as alen,
2820 * use that as the minimum.
2823 args.minlen = ap->alen;
2824 } else if (ap->low) {
2825 args.type = XFS_ALLOCTYPE_START_BNO;
2826 args.total = args.minlen = ap->minlen;
2828 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2829 args.total = ap->total;
2830 args.minlen = ap->minlen;
2832 if (unlikely(ap->userdata && ap->ip->i_d.di_extsize &&
2833 (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE))) {
2834 args.prod = ap->ip->i_d.di_extsize;
2835 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2836 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2837 } else if (mp->m_sb.sb_blocksize >= NBPP) {
2841 args.prod = NBPP >> mp->m_sb.sb_blocklog;
2842 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2843 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2846 * If we are not low on available data blocks, and the
2847 * underlying logical volume manager is a stripe, and
2848 * the file offset is zero then try to allocate data
2849 * blocks on stripe unit boundary.
2850 * NOTE: ap->aeof is only set if the allocation length
2851 * is >= the stripe unit and the allocation offset is
2852 * at the end of file.
2854 if (!ap->low && ap->aeof) {
2856 args.alignment = mp->m_dalign;
2860 * Adjust for alignment
2862 if (blen > args.alignment && blen <= ap->alen)
2863 args.minlen = blen - args.alignment;
2864 args.minalignslop = 0;
2867 * First try an exact bno allocation.
2868 * If it fails then do a near or start bno
2869 * allocation with alignment turned on.
2873 args.type = XFS_ALLOCTYPE_THIS_BNO;
2876 * Compute the minlen+alignment for the
2877 * next case. Set slop so that the value
2878 * of minlen+alignment+slop doesn't go up
2879 * between the calls.
2881 if (blen > mp->m_dalign && blen <= ap->alen)
2882 nextminlen = blen - mp->m_dalign;
2884 nextminlen = args.minlen;
2885 if (nextminlen + mp->m_dalign > args.minlen + 1)
2887 nextminlen + mp->m_dalign -
2890 args.minalignslop = 0;
2894 args.minalignslop = 0;
2896 args.minleft = ap->minleft;
2897 args.wasdel = ap->wasdel;
2899 args.userdata = ap->userdata;
2900 if ((error = xfs_alloc_vextent(&args)))
2902 if (tryagain && args.fsbno == NULLFSBLOCK) {
2904 * Exact allocation failed. Now try with alignment
2908 args.fsbno = ap->rval;
2909 args.alignment = mp->m_dalign;
2910 args.minlen = nextminlen;
2911 args.minalignslop = 0;
2913 if ((error = xfs_alloc_vextent(&args)))
2916 if (isaligned && args.fsbno == NULLFSBLOCK) {
2918 * allocation failed, so turn off alignment and
2922 args.fsbno = ap->rval;
2924 if ((error = xfs_alloc_vextent(&args)))
2927 if (args.fsbno == NULLFSBLOCK && nullfb &&
2928 args.minlen > ap->minlen) {
2929 args.minlen = ap->minlen;
2930 args.type = XFS_ALLOCTYPE_START_BNO;
2931 args.fsbno = ap->rval;
2932 if ((error = xfs_alloc_vextent(&args)))
2935 if (args.fsbno == NULLFSBLOCK && nullfb) {
2937 args.type = XFS_ALLOCTYPE_FIRST_AG;
2938 args.total = ap->minlen;
2940 if ((error = xfs_alloc_vextent(&args)))
2944 if (args.fsbno != NULLFSBLOCK) {
2945 ap->firstblock = ap->rval = args.fsbno;
2946 ASSERT(nullfb || fb_agno == args.agno ||
2947 (ap->low && fb_agno < args.agno));
2948 ap->alen = args.len;
2949 ap->ip->i_d.di_nblocks += args.len;
2950 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2952 ap->ip->i_delayed_blks -= args.len;
2954 * Adjust the disk quota also. This was reserved
2957 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2958 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2959 XFS_TRANS_DQ_BCOUNT,
2962 ap->rval = NULLFSBLOCK;
2969 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2970 * It figures out where to ask the underlying allocator to put the new extent.
2974 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2976 if ((ap->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) && ap->userdata)
2977 return xfs_bmap_rtalloc(ap);
2978 return xfs_bmap_btalloc(ap);
2982 * Transform a btree format file with only one leaf node, where the
2983 * extents list will fit in the inode, into an extents format file.
2984 * Since the file extents are already in-core, all we have to do is
2985 * give up the space for the btree root and pitch the leaf block.
2987 STATIC int /* error */
2988 xfs_bmap_btree_to_extents(
2989 xfs_trans_t *tp, /* transaction pointer */
2990 xfs_inode_t *ip, /* incore inode pointer */
2991 xfs_btree_cur_t *cur, /* btree cursor */
2992 int *logflagsp, /* inode logging flags */
2993 int whichfork) /* data or attr fork */
2996 xfs_bmbt_block_t *cblock;/* child btree block */
2997 xfs_fsblock_t cbno; /* child block number */
2998 xfs_buf_t *cbp; /* child block's buffer */
2999 int error; /* error return value */
3000 xfs_ifork_t *ifp; /* inode fork data */
3001 xfs_mount_t *mp; /* mount point structure */
3002 __be64 *pp; /* ptr to block address */
3003 xfs_bmbt_block_t *rblock;/* root btree block */
3005 ifp = XFS_IFORK_PTR(ip, whichfork);
3006 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3007 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
3008 rblock = ifp->if_broot;
3009 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
3010 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
3011 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
3013 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
3014 cbno = be64_to_cpu(*pp);
3017 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
3020 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
3021 XFS_BMAP_BTREE_REF)))
3023 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
3024 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
3026 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
3027 ip->i_d.di_nblocks--;
3028 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
3029 xfs_trans_binval(tp, cbp);
3030 if (cur->bc_bufs[0] == cbp)
3031 cur->bc_bufs[0] = NULL;
3032 xfs_iroot_realloc(ip, -1, whichfork);
3033 ASSERT(ifp->if_broot == NULL);
3034 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
3035 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3036 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
3041 * Called by xfs_bmapi to update file extent records and the btree
3042 * after removing space (or undoing a delayed allocation).
3044 STATIC int /* error */
3045 xfs_bmap_del_extent(
3046 xfs_inode_t *ip, /* incore inode pointer */
3047 xfs_trans_t *tp, /* current transaction pointer */
3048 xfs_extnum_t idx, /* extent number to update/delete */
3049 xfs_bmap_free_t *flist, /* list of extents to be freed */
3050 xfs_btree_cur_t *cur, /* if null, not a btree */
3051 xfs_bmbt_irec_t *del, /* data to remove from extents */
3052 int *logflagsp, /* inode logging flags */
3053 xfs_extdelta_t *delta, /* Change made to incore extents */
3054 int whichfork, /* data or attr fork */
3055 int rsvd) /* OK to allocate reserved blocks */
3057 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
3058 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
3059 xfs_fsblock_t del_endblock=0; /* first block past del */
3060 xfs_fileoff_t del_endoff; /* first offset past del */
3061 int delay; /* current block is delayed allocated */
3062 int do_fx; /* free extent at end of routine */
3063 xfs_bmbt_rec_t *ep; /* current extent entry pointer */
3064 int error; /* error return value */
3065 int flags; /* inode logging flags */
3066 #ifdef XFS_BMAP_TRACE
3067 static char fname[] = "xfs_bmap_del_extent";
3069 xfs_bmbt_irec_t got; /* current extent entry */
3070 xfs_fileoff_t got_endoff; /* first offset past got */
3071 int i; /* temp state */
3072 xfs_ifork_t *ifp; /* inode fork pointer */
3073 xfs_mount_t *mp; /* mount structure */
3074 xfs_filblks_t nblks; /* quota/sb block count */
3075 xfs_bmbt_irec_t new; /* new record to be inserted */
3077 uint qfield; /* quota field to update */
3078 xfs_filblks_t temp; /* for indirect length calculations */
3079 xfs_filblks_t temp2; /* for indirect length calculations */
3081 XFS_STATS_INC(xs_del_exlist);
3083 ifp = XFS_IFORK_PTR(ip, whichfork);
3084 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
3085 (uint)sizeof(xfs_bmbt_rec_t)));
3086 ASSERT(del->br_blockcount > 0);
3087 ep = xfs_iext_get_ext(ifp, idx);
3088 xfs_bmbt_get_all(ep, &got);
3089 ASSERT(got.br_startoff <= del->br_startoff);
3090 del_endoff = del->br_startoff + del->br_blockcount;
3091 got_endoff = got.br_startoff + got.br_blockcount;
3092 ASSERT(got_endoff >= del_endoff);
3093 delay = ISNULLSTARTBLOCK(got.br_startblock);
3094 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
3099 * If deleting a real allocation, must free up the disk space.
3102 flags = XFS_ILOG_CORE;
3104 * Realtime allocation. Free it and record di_nblocks update.
3106 if (whichfork == XFS_DATA_FORK &&
3107 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
3111 ASSERT(do_mod(del->br_blockcount,
3112 mp->m_sb.sb_rextsize) == 0);
3113 ASSERT(do_mod(del->br_startblock,
3114 mp->m_sb.sb_rextsize) == 0);
3115 bno = del->br_startblock;
3116 len = del->br_blockcount;
3117 do_div(bno, mp->m_sb.sb_rextsize);
3118 do_div(len, mp->m_sb.sb_rextsize);
3119 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3120 (xfs_extlen_t)len)))
3123 nblks = len * mp->m_sb.sb_rextsize;
3124 qfield = XFS_TRANS_DQ_RTBCOUNT;
3127 * Ordinary allocation.
3131 nblks = del->br_blockcount;
3132 qfield = XFS_TRANS_DQ_BCOUNT;
3135 * Set up del_endblock and cur for later.
3137 del_endblock = del->br_startblock + del->br_blockcount;
3139 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3140 got.br_startblock, got.br_blockcount,
3145 da_old = da_new = 0;
3147 da_old = STARTBLOCKVAL(got.br_startblock);
3153 * Set flag value to use in switch statement.
3154 * Left-contig is 2, right-contig is 1.
3156 switch (((got.br_startoff == del->br_startoff) << 1) |
3157 (got_endoff == del_endoff)) {
3160 * Matches the whole extent. Delete the entry.
3162 xfs_bmap_trace_delete(fname, "3", ip, idx, 1, whichfork);
3163 xfs_iext_remove(ifp, idx, 1);
3164 ifp->if_lastex = idx;
3167 XFS_IFORK_NEXT_SET(ip, whichfork,
3168 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3169 flags |= XFS_ILOG_CORE;
3171 flags |= XFS_ILOG_FEXT(whichfork);
3174 if ((error = xfs_bmbt_delete(cur, &i)))
3181 * Deleting the first part of the extent.
3183 xfs_bmap_trace_pre_update(fname, "2", ip, idx, whichfork);
3184 xfs_bmbt_set_startoff(ep, del_endoff);
3185 temp = got.br_blockcount - del->br_blockcount;
3186 xfs_bmbt_set_blockcount(ep, temp);
3187 ifp->if_lastex = idx;
3189 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3191 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3192 xfs_bmap_trace_post_update(fname, "2", ip, idx,
3197 xfs_bmbt_set_startblock(ep, del_endblock);
3198 xfs_bmap_trace_post_update(fname, "2", ip, idx, whichfork);
3200 flags |= XFS_ILOG_FEXT(whichfork);
3203 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3204 got.br_blockcount - del->br_blockcount,
3211 * Deleting the last part of the extent.
3213 temp = got.br_blockcount - del->br_blockcount;
3214 xfs_bmap_trace_pre_update(fname, "1", ip, idx, whichfork);
3215 xfs_bmbt_set_blockcount(ep, temp);
3216 ifp->if_lastex = idx;
3218 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3220 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3221 xfs_bmap_trace_post_update(fname, "1", ip, idx,
3226 xfs_bmap_trace_post_update(fname, "1", ip, idx, whichfork);
3228 flags |= XFS_ILOG_FEXT(whichfork);
3231 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3233 got.br_blockcount - del->br_blockcount,
3240 * Deleting the middle of the extent.
3242 temp = del->br_startoff - got.br_startoff;
3243 xfs_bmap_trace_pre_update(fname, "0", ip, idx, whichfork);
3244 xfs_bmbt_set_blockcount(ep, temp);
3245 new.br_startoff = del_endoff;
3246 temp2 = got_endoff - del_endoff;
3247 new.br_blockcount = temp2;
3248 new.br_state = got.br_state;
3250 new.br_startblock = del_endblock;
3251 flags |= XFS_ILOG_CORE;
3253 if ((error = xfs_bmbt_update(cur,
3255 got.br_startblock, temp,
3258 if ((error = xfs_bmbt_increment(cur, 0, &i)))
3260 cur->bc_rec.b = new;
3261 error = xfs_bmbt_insert(cur, &i);
3262 if (error && error != ENOSPC)
3265 * If get no-space back from btree insert,
3266 * it tried a split, and we have a zero
3267 * block reservation.
3268 * Fix up our state and return the error.
3270 if (error == ENOSPC) {
3272 * Reset the cursor, don't trust
3273 * it after any insert operation.
3275 if ((error = xfs_bmbt_lookup_eq(cur,
3282 * Update the btree record back
3283 * to the original value.
3285 if ((error = xfs_bmbt_update(cur,
3292 * Reset the extent record back
3293 * to the original value.
3295 xfs_bmbt_set_blockcount(ep,
3298 error = XFS_ERROR(ENOSPC);
3303 flags |= XFS_ILOG_FEXT(whichfork);
3304 XFS_IFORK_NEXT_SET(ip, whichfork,
3305 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3307 ASSERT(whichfork == XFS_DATA_FORK);
3308 temp = xfs_bmap_worst_indlen(ip, temp);
3309 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3310 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3311 new.br_startblock = NULLSTARTBLOCK((int)temp2);
3312 da_new = temp + temp2;
3313 while (da_new > da_old) {
3317 xfs_bmbt_set_startblock(ep,
3318 NULLSTARTBLOCK((int)temp));
3320 if (da_new == da_old)
3326 NULLSTARTBLOCK((int)temp2);
3330 xfs_bmap_trace_post_update(fname, "0", ip, idx, whichfork);
3331 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 1, &new, NULL,
3333 xfs_iext_insert(ifp, idx + 1, 1, &new);
3334 ifp->if_lastex = idx + 1;
3338 * If we need to, add to list of extents to delete.
3341 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3344 * Adjust inode # blocks in the file.
3347 ip->i_d.di_nblocks -= nblks;
3349 * Adjust quota data.
3352 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
3355 * Account for change in delayed indirect blocks.
3356 * Nothing to do for disk quota accounting here.
3358 ASSERT(da_old >= da_new);
3359 if (da_old > da_new)
3360 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int)(da_old - da_new),
3363 /* DELTA: report the original extent. */
3364 if (delta->xed_startoff > got.br_startoff)
3365 delta->xed_startoff = got.br_startoff;
3366 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3367 delta->xed_blockcount = got.br_startoff +
3376 * Remove the entry "free" from the free item list. Prev points to the
3377 * previous entry, unless "free" is the head of the list.
3381 xfs_bmap_free_t *flist, /* free item list header */
3382 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3383 xfs_bmap_free_item_t *free) /* list item to be freed */
3386 prev->xbfi_next = free->xbfi_next;
3388 flist->xbf_first = free->xbfi_next;
3390 kmem_zone_free(xfs_bmap_free_item_zone, free);
3394 * Convert an extents-format file into a btree-format file.
3395 * The new file will have a root block (in the inode) and a single child block.
3397 STATIC int /* error */
3398 xfs_bmap_extents_to_btree(
3399 xfs_trans_t *tp, /* transaction pointer */
3400 xfs_inode_t *ip, /* incore inode pointer */
3401 xfs_fsblock_t *firstblock, /* first-block-allocated */
3402 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3403 xfs_btree_cur_t **curp, /* cursor returned to caller */
3404 int wasdel, /* converting a delayed alloc */
3405 int *logflagsp, /* inode logging flags */
3406 int whichfork) /* data or attr fork */
3408 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
3409 xfs_buf_t *abp; /* buffer for ablock */
3410 xfs_alloc_arg_t args; /* allocation arguments */
3411 xfs_bmbt_rec_t *arp; /* child record pointer */
3412 xfs_bmbt_block_t *block; /* btree root block */
3413 xfs_btree_cur_t *cur; /* bmap btree cursor */
3414 xfs_bmbt_rec_t *ep; /* extent record pointer */
3415 int error; /* error return value */
3416 xfs_extnum_t i, cnt; /* extent record index */
3417 xfs_ifork_t *ifp; /* inode fork pointer */
3418 xfs_bmbt_key_t *kp; /* root block key pointer */
3419 xfs_mount_t *mp; /* mount structure */
3420 xfs_extnum_t nextents; /* number of file extents */
3421 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3423 ifp = XFS_IFORK_PTR(ip, whichfork);
3424 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3425 ASSERT(ifp->if_ext_max ==
3426 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3428 * Make space in the inode incore.
3430 xfs_iroot_realloc(ip, 1, whichfork);
3431 ifp->if_flags |= XFS_IFBROOT;
3435 block = ifp->if_broot;
3436 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3437 block->bb_level = cpu_to_be16(1);
3438 block->bb_numrecs = cpu_to_be16(1);
3439 block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3440 block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3442 * Need a cursor. Can't allocate until bb_level is filled in.
3445 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
3447 cur->bc_private.b.firstblock = *firstblock;
3448 cur->bc_private.b.flist = flist;
3449 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3451 * Convert to a btree with two levels, one record in root.
3453 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3456 args.firstblock = *firstblock;
3457 if (*firstblock == NULLFSBLOCK) {
3458 args.type = XFS_ALLOCTYPE_START_BNO;
3459 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3460 } else if (flist->xbf_low) {
3461 args.type = XFS_ALLOCTYPE_START_BNO;
3462 args.fsbno = *firstblock;
3464 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3465 args.fsbno = *firstblock;
3467 args.minlen = args.maxlen = args.prod = 1;
3468 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3469 args.minalignslop = 0;
3470 args.wasdel = wasdel;
3472 if ((error = xfs_alloc_vextent(&args))) {
3473 xfs_iroot_realloc(ip, -1, whichfork);
3474 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3478 * Allocation can't fail, the space was reserved.
3480 ASSERT(args.fsbno != NULLFSBLOCK);
3481 ASSERT(*firstblock == NULLFSBLOCK ||
3482 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3484 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3485 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3486 cur->bc_private.b.allocated++;
3487 ip->i_d.di_nblocks++;
3488 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3489 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3491 * Fill in the child block.
3493 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
3494 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3495 ablock->bb_level = 0;
3496 ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3497 ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3498 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3499 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3500 for (cnt = i = 0; i < nextents; i++) {
3501 ep = xfs_iext_get_ext(ifp, i);
3502 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
3503 arp->l0 = INT_GET(ep->l0, ARCH_CONVERT);
3504 arp->l1 = INT_GET(ep->l1, ARCH_CONVERT);
3508 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3509 ablock->bb_numrecs = cpu_to_be16(cnt);
3511 * Fill in the root key and pointer.
3513 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
3514 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3515 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3516 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
3517 *pp = cpu_to_be64(args.fsbno);
3519 * Do all this logging at the end so that
3520 * the root is at the right level.
3522 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
3523 xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3524 ASSERT(*curp == NULL);
3526 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
3531 * Helper routine to reset inode di_forkoff field when switching
3532 * attribute fork from local to extent format - we reset it where
3533 * possible to make space available for inline data fork extents.
3536 xfs_bmap_forkoff_reset(
3541 if (whichfork == XFS_ATTR_FORK &&
3542 (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
3543 (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
3544 ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
3545 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3546 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
3547 (uint)sizeof(xfs_bmbt_rec_t);
3548 ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
3549 (uint)sizeof(xfs_bmbt_rec_t);
3554 * Convert a local file to an extents file.
3555 * This code is out of bounds for data forks of regular files,
3556 * since the file data needs to get logged so things will stay consistent.
3557 * (The bmap-level manipulations are ok, though).
3559 STATIC int /* error */
3560 xfs_bmap_local_to_extents(
3561 xfs_trans_t *tp, /* transaction pointer */
3562 xfs_inode_t *ip, /* incore inode pointer */
3563 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3564 xfs_extlen_t total, /* total blocks needed by transaction */
3565 int *logflagsp, /* inode logging flags */
3566 int whichfork) /* data or attr fork */
3568 int error; /* error return value */
3569 int flags; /* logging flags returned */
3570 #ifdef XFS_BMAP_TRACE
3571 static char fname[] = "xfs_bmap_local_to_extents";
3573 xfs_ifork_t *ifp; /* inode fork pointer */
3576 * We don't want to deal with the case of keeping inode data inline yet.
3577 * So sending the data fork of a regular inode is invalid.
3579 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3580 whichfork == XFS_DATA_FORK));
3581 ifp = XFS_IFORK_PTR(ip, whichfork);
3582 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3585 if (ifp->if_bytes) {
3586 xfs_alloc_arg_t args; /* allocation arguments */
3587 xfs_buf_t *bp; /* buffer for extent block */
3588 xfs_bmbt_rec_t *ep; /* extent record pointer */
3591 args.mp = ip->i_mount;
3592 args.firstblock = *firstblock;
3593 ASSERT((ifp->if_flags &
3594 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3596 * Allocate a block. We know we need only one, since the
3597 * file currently fits in an inode.
3599 if (*firstblock == NULLFSBLOCK) {
3600 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3601 args.type = XFS_ALLOCTYPE_START_BNO;
3603 args.fsbno = *firstblock;
3604 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3607 args.mod = args.minleft = args.alignment = args.wasdel =
3608 args.isfl = args.minalignslop = 0;
3609 args.minlen = args.maxlen = args.prod = 1;
3610 if ((error = xfs_alloc_vextent(&args)))
3613 * Can't fail, the space was reserved.
3615 ASSERT(args.fsbno != NULLFSBLOCK);
3616 ASSERT(args.len == 1);
3617 *firstblock = args.fsbno;
3618 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3619 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3621 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3622 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3623 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3624 xfs_iext_add(ifp, 0, 1);
3625 ep = xfs_iext_get_ext(ifp, 0);
3626 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3627 xfs_bmap_trace_post_update(fname, "new", ip, 0, whichfork);
3628 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3629 ip->i_d.di_nblocks = 1;
3630 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
3631 XFS_TRANS_DQ_BCOUNT, 1L);
3632 flags |= XFS_ILOG_FEXT(whichfork);
3634 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3635 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3637 ifp->if_flags &= ~XFS_IFINLINE;
3638 ifp->if_flags |= XFS_IFEXTENTS;
3639 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3640 flags |= XFS_ILOG_CORE;
3647 * Search the extent records for the entry containing block bno.
3648 * If bno lies in a hole, point to the next entry. If bno lies
3649 * past eof, *eofp will be set, and *prevp will contain the last
3650 * entry (null if none). Else, *lastxp will be set to the index
3651 * of the found entry; *gotp will contain the entry.
3653 xfs_bmbt_rec_t * /* pointer to found extent entry */
3654 xfs_bmap_search_multi_extents(
3655 xfs_ifork_t *ifp, /* inode fork pointer */
3656 xfs_fileoff_t bno, /* block number searched for */
3657 int *eofp, /* out: end of file found */
3658 xfs_extnum_t *lastxp, /* out: last extent index */
3659 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3660 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3662 xfs_bmbt_rec_t *ep; /* extent record pointer */
3663 xfs_extnum_t lastx; /* last extent index */
3666 * Initialize the extent entry structure to catch access to
3667 * uninitialized br_startblock field.
3669 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3670 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3671 gotp->br_state = XFS_EXT_INVALID;
3673 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3675 gotp->br_startblock = 0xffffa5a5;
3677 prevp->br_startoff = NULLFILEOFF;
3679 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3681 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3683 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3684 xfs_bmbt_get_all(ep, gotp);
3698 * Search the extents list for the inode, for the extent containing bno.
3699 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3700 * *eofp will be set, and *prevp will contain the last entry (null if none).
3701 * Else, *lastxp will be set to the index of the found
3702 * entry; *gotp will contain the entry.
3704 STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
3705 xfs_bmap_search_extents(
3706 xfs_inode_t *ip, /* incore inode pointer */
3707 xfs_fileoff_t bno, /* block number searched for */
3708 int fork, /* data or attr fork */
3709 int *eofp, /* out: end of file found */
3710 xfs_extnum_t *lastxp, /* out: last extent index */
3711 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3712 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3714 xfs_ifork_t *ifp; /* inode fork pointer */
3715 xfs_bmbt_rec_t *ep; /* extent record pointer */
3717 XFS_STATS_INC(xs_look_exlist);
3718 ifp = XFS_IFORK_PTR(ip, fork);
3720 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3722 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3723 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3724 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3725 "Access to block zero in inode %llu "
3726 "start_block: %llx start_off: %llx "
3727 "blkcnt: %llx extent-state: %x lastx: %x\n",
3728 (unsigned long long)ip->i_ino,
3729 (unsigned long long)gotp->br_startblock,
3730 (unsigned long long)gotp->br_startoff,
3731 (unsigned long long)gotp->br_blockcount,
3732 gotp->br_state, *lastxp);
3733 *lastxp = NULLEXTNUM;
3741 #ifdef XFS_BMAP_TRACE
3742 ktrace_t *xfs_bmap_trace_buf;
3745 * Add a bmap trace buffer entry. Base routine for the others.
3748 xfs_bmap_trace_addentry(
3749 int opcode, /* operation */
3750 char *fname, /* function name */
3751 char *desc, /* operation description */
3752 xfs_inode_t *ip, /* incore inode pointer */
3753 xfs_extnum_t idx, /* index of entry(ies) */
3754 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
3755 xfs_bmbt_rec_t *r1, /* first record */
3756 xfs_bmbt_rec_t *r2, /* second record or null */
3757 int whichfork) /* data or attr fork */
3761 ASSERT(cnt == 1 || cnt == 2);
3766 memset(&tr2, 0, sizeof(tr2));
3769 ktrace_enter(xfs_bmap_trace_buf,
3770 (void *)(__psint_t)(opcode | (whichfork << 16)),
3771 (void *)fname, (void *)desc, (void *)ip,
3772 (void *)(__psint_t)idx,
3773 (void *)(__psint_t)cnt,
3774 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3775 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3776 (void *)(__psunsigned_t)(r1->l0 >> 32),
3777 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3778 (void *)(__psunsigned_t)(r1->l1 >> 32),
3779 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3780 (void *)(__psunsigned_t)(r2->l0 >> 32),
3781 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3782 (void *)(__psunsigned_t)(r2->l1 >> 32),
3783 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3785 ASSERT(ip->i_xtrace);
3786 ktrace_enter(ip->i_xtrace,
3787 (void *)(__psint_t)(opcode | (whichfork << 16)),
3788 (void *)fname, (void *)desc, (void *)ip,
3789 (void *)(__psint_t)idx,
3790 (void *)(__psint_t)cnt,
3791 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3792 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3793 (void *)(__psunsigned_t)(r1->l0 >> 32),
3794 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3795 (void *)(__psunsigned_t)(r1->l1 >> 32),
3796 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3797 (void *)(__psunsigned_t)(r2->l0 >> 32),
3798 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3799 (void *)(__psunsigned_t)(r2->l1 >> 32),
3800 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3805 * Add bmap trace entry prior to a call to xfs_iext_remove.
3808 xfs_bmap_trace_delete(
3809 char *fname, /* function name */
3810 char *desc, /* operation description */
3811 xfs_inode_t *ip, /* incore inode pointer */
3812 xfs_extnum_t idx, /* index of entry(entries) deleted */
3813 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3814 int whichfork) /* data or attr fork */
3816 xfs_ifork_t *ifp; /* inode fork pointer */
3818 ifp = XFS_IFORK_PTR(ip, whichfork);
3819 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
3820 cnt, xfs_iext_get_ext(ifp, idx),
3821 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
3826 * Add bmap trace entry prior to a call to xfs_iext_insert, or
3827 * reading in the extents list from the disk (in the btree).
3830 xfs_bmap_trace_insert(
3831 char *fname, /* function name */
3832 char *desc, /* operation description */
3833 xfs_inode_t *ip, /* incore inode pointer */
3834 xfs_extnum_t idx, /* index of entry(entries) inserted */
3835 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3836 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3837 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3838 int whichfork) /* data or attr fork */
3840 xfs_bmbt_rec_t tr1; /* compressed record 1 */
3841 xfs_bmbt_rec_t tr2; /* compressed record 2 if needed */
3843 xfs_bmbt_set_all(&tr1, r1);
3846 xfs_bmbt_set_all(&tr2, r2);
3851 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3852 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3856 * Add bmap trace entry after updating an extent record in place.
3859 xfs_bmap_trace_post_update(
3860 char *fname, /* function name */
3861 char *desc, /* operation description */
3862 xfs_inode_t *ip, /* incore inode pointer */
3863 xfs_extnum_t idx, /* index of entry updated */
3864 int whichfork) /* data or attr fork */
3866 xfs_ifork_t *ifp; /* inode fork pointer */
3868 ifp = XFS_IFORK_PTR(ip, whichfork);
3869 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
3870 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3874 * Add bmap trace entry prior to updating an extent record in place.
3877 xfs_bmap_trace_pre_update(
3878 char *fname, /* function name */
3879 char *desc, /* operation description */
3880 xfs_inode_t *ip, /* incore inode pointer */
3881 xfs_extnum_t idx, /* index of entry to be updated */
3882 int whichfork) /* data or attr fork */
3884 xfs_ifork_t *ifp; /* inode fork pointer */
3886 ifp = XFS_IFORK_PTR(ip, whichfork);
3887 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
3888 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3890 #endif /* XFS_BMAP_TRACE */
3893 * Compute the worst-case number of indirect blocks that will be used
3894 * for ip's delayed extent of length "len".
3896 STATIC xfs_filblks_t
3897 xfs_bmap_worst_indlen(
3898 xfs_inode_t *ip, /* incore inode pointer */
3899 xfs_filblks_t len) /* delayed extent length */
3901 int level; /* btree level number */
3902 int maxrecs; /* maximum record count at this level */
3903 xfs_mount_t *mp; /* mount structure */
3904 xfs_filblks_t rval; /* return value */
3907 maxrecs = mp->m_bmap_dmxr[0];
3908 for (level = 0, rval = 0;
3909 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3912 do_div(len, maxrecs);
3915 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3918 maxrecs = mp->m_bmap_dmxr[1];
3923 #if defined(XFS_RW_TRACE)
3932 if (ip->i_rwtrace == NULL)
3934 ktrace_enter(ip->i_rwtrace,
3935 (void *)(__psint_t)XFS_BUNMAP,
3937 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3938 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3939 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3940 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3941 (void *)(__psint_t)len,
3942 (void *)(__psint_t)flags,
3943 (void *)(unsigned long)current_cpu(),
3955 * Convert inode from non-attributed to attributed.
3956 * Must not be in a transaction, ip must not be locked.
3958 int /* error code */
3959 xfs_bmap_add_attrfork(
3960 xfs_inode_t *ip, /* incore inode pointer */
3961 int size, /* space new attribute needs */
3962 int rsvd) /* xact may use reserved blks */
3964 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3965 xfs_bmap_free_t flist; /* freed extent records */
3966 xfs_mount_t *mp; /* mount structure */
3967 xfs_trans_t *tp; /* transaction pointer */
3968 unsigned long s; /* spinlock spl value */
3969 int blks; /* space reservation */
3970 int version = 1; /* superblock attr version */
3971 int committed; /* xaction was committed */
3972 int logflags; /* logging flags */
3973 int error; /* error return value */
3975 ASSERT(XFS_IFORK_Q(ip) == 0);
3976 ASSERT(ip->i_df.if_ext_max ==
3977 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3980 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3981 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3982 blks = XFS_ADDAFORK_SPACE_RES(mp);
3984 tp->t_flags |= XFS_TRANS_RESERVE;
3985 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3986 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3988 xfs_ilock(ip, XFS_ILOCK_EXCL);
3989 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
3990 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3991 XFS_QMOPT_RES_REGBLKS);
3993 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3994 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3997 if (XFS_IFORK_Q(ip))
3999 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
4001 * For inodes coming from pre-6.2 filesystems.
4003 ASSERT(ip->i_d.di_aformat == 0);
4004 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
4006 ASSERT(ip->i_d.di_anextents == 0);
4007 VN_HOLD(XFS_ITOV(ip));
4008 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4009 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4010 switch (ip->i_d.di_format) {
4011 case XFS_DINODE_FMT_DEV:
4012 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
4014 case XFS_DINODE_FMT_UUID:
4015 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
4017 case XFS_DINODE_FMT_LOCAL:
4018 case XFS_DINODE_FMT_EXTENTS:
4019 case XFS_DINODE_FMT_BTREE:
4020 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
4021 if (!ip->i_d.di_forkoff)
4022 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
4023 else if (mp->m_flags & XFS_MOUNT_ATTR2)
4028 error = XFS_ERROR(EINVAL);
4031 ip->i_df.if_ext_max =
4032 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4033 ASSERT(ip->i_afp == NULL);
4034 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
4035 ip->i_afp->if_ext_max =
4036 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4037 ip->i_afp->if_flags = XFS_IFEXTENTS;
4039 XFS_BMAP_INIT(&flist, &firstblock);
4040 switch (ip->i_d.di_format) {
4041 case XFS_DINODE_FMT_LOCAL:
4042 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
4045 case XFS_DINODE_FMT_EXTENTS:
4046 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
4049 case XFS_DINODE_FMT_BTREE:
4050 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
4058 xfs_trans_log_inode(tp, ip, logflags);
4061 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb) ||
4062 (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2)) {
4063 __int64_t sbfields = 0;
4065 s = XFS_SB_LOCK(mp);
4066 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) {
4067 XFS_SB_VERSION_ADDATTR(&mp->m_sb);
4068 sbfields |= XFS_SB_VERSIONNUM;
4070 if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2) {
4071 XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
4072 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
4075 XFS_SB_UNLOCK(mp, s);
4076 xfs_mod_sb(tp, sbfields);
4078 XFS_SB_UNLOCK(mp, s);
4080 if ((error = xfs_bmap_finish(&tp, &flist, firstblock, &committed)))
4082 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES, NULL);
4083 ASSERT(ip->i_df.if_ext_max ==
4084 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4087 xfs_bmap_cancel(&flist);
4089 ASSERT(ismrlocked(&ip->i_lock,MR_UPDATE));
4090 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4092 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4093 ASSERT(ip->i_df.if_ext_max ==
4094 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4099 * Add the extent to the list of extents to be free at transaction end.
4100 * The list is maintained sorted (by block number).
4105 xfs_fsblock_t bno, /* fs block number of extent */
4106 xfs_filblks_t len, /* length of extent */
4107 xfs_bmap_free_t *flist, /* list of extents */
4108 xfs_mount_t *mp) /* mount point structure */
4110 xfs_bmap_free_item_t *cur; /* current (next) element */
4111 xfs_bmap_free_item_t *new; /* new element */
4112 xfs_bmap_free_item_t *prev; /* previous element */
4114 xfs_agnumber_t agno;
4115 xfs_agblock_t agbno;
4117 ASSERT(bno != NULLFSBLOCK);
4119 ASSERT(len <= MAXEXTLEN);
4120 ASSERT(!ISNULLSTARTBLOCK(bno));
4121 agno = XFS_FSB_TO_AGNO(mp, bno);
4122 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4123 ASSERT(agno < mp->m_sb.sb_agcount);
4124 ASSERT(agbno < mp->m_sb.sb_agblocks);
4125 ASSERT(len < mp->m_sb.sb_agblocks);
4126 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4128 ASSERT(xfs_bmap_free_item_zone != NULL);
4129 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4130 new->xbfi_startblock = bno;
4131 new->xbfi_blockcount = (xfs_extlen_t)len;
4132 for (prev = NULL, cur = flist->xbf_first;
4134 prev = cur, cur = cur->xbfi_next) {
4135 if (cur->xbfi_startblock >= bno)
4139 prev->xbfi_next = new;
4141 flist->xbf_first = new;
4142 new->xbfi_next = cur;
4147 * Compute and fill in the value of the maximum depth of a bmap btree
4148 * in this filesystem. Done once, during mount.
4151 xfs_bmap_compute_maxlevels(
4152 xfs_mount_t *mp, /* file system mount structure */
4153 int whichfork) /* data or attr fork */
4155 int level; /* btree level */
4156 uint maxblocks; /* max blocks at this level */
4157 uint maxleafents; /* max leaf entries possible */
4158 int maxrootrecs; /* max records in root block */
4159 int minleafrecs; /* min records in leaf block */
4160 int minnoderecs; /* min records in node block */
4161 int sz; /* root block size */
4164 * The maximum number of extents in a file, hence the maximum
4165 * number of leaf entries, is controlled by the type of di_nextents
4166 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4167 * (a signed 16-bit number, xfs_aextnum_t).
4169 if (whichfork == XFS_DATA_FORK) {
4170 maxleafents = MAXEXTNUM;
4171 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4172 XFS_BMDR_SPACE_CALC(MINDBTPTRS) : mp->m_attroffset;
4174 maxleafents = MAXAEXTNUM;
4175 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4176 XFS_BMDR_SPACE_CALC(MINABTPTRS) :
4177 mp->m_sb.sb_inodesize - mp->m_attroffset;
4179 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
4180 minleafrecs = mp->m_bmap_dmnr[0];
4181 minnoderecs = mp->m_bmap_dmnr[1];
4182 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4183 for (level = 1; maxblocks > 1; level++) {
4184 if (maxblocks <= maxrootrecs)
4187 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4189 mp->m_bm_maxlevels[whichfork] = level;
4193 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4194 * caller. Frees all the extents that need freeing, which must be done
4195 * last due to locking considerations. We never free any extents in
4196 * the first transaction. This is to allow the caller to make the first
4197 * transaction a synchronous one so that the pointers to the data being
4198 * broken in this transaction will be permanent before the data is actually
4199 * freed. This is necessary to prevent blocks from being reallocated
4200 * and written to before the free and reallocation are actually permanent.
4201 * We do not just make the first transaction synchronous here, because
4202 * there are more efficient ways to gain the same protection in some cases
4203 * (see the file truncation code).
4205 * Return 1 if the given transaction was committed and a new one
4206 * started, and 0 otherwise in the committed parameter.
4211 xfs_trans_t **tp, /* transaction pointer addr */
4212 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4213 xfs_fsblock_t firstblock, /* controlled ag for allocs */
4214 int *committed) /* xact committed or not */
4216 xfs_efd_log_item_t *efd; /* extent free data */
4217 xfs_efi_log_item_t *efi; /* extent free intention */
4218 int error; /* error return value */
4219 xfs_bmap_free_item_t *free; /* free extent item */
4220 unsigned int logres; /* new log reservation */
4221 unsigned int logcount; /* new log count */
4222 xfs_mount_t *mp; /* filesystem mount structure */
4223 xfs_bmap_free_item_t *next; /* next item on free list */
4224 xfs_trans_t *ntp; /* new transaction pointer */
4226 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4227 if (flist->xbf_count == 0) {
4232 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4233 for (free = flist->xbf_first; free; free = free->xbfi_next)
4234 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4235 free->xbfi_blockcount);
4236 logres = ntp->t_log_res;
4237 logcount = ntp->t_log_count;
4238 ntp = xfs_trans_dup(*tp);
4239 error = xfs_trans_commit(*tp, 0, NULL);
4243 * We have a new transaction, so we should return committed=1,
4244 * even though we're returning an error.
4249 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4252 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4253 for (free = flist->xbf_first; free != NULL; free = next) {
4254 next = free->xbfi_next;
4255 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4256 free->xbfi_blockcount))) {
4258 * The bmap free list will be cleaned up at a
4259 * higher level. The EFI will be canceled when
4260 * this transaction is aborted.
4261 * Need to force shutdown here to make sure it
4262 * happens, since this transaction may not be
4266 if (!XFS_FORCED_SHUTDOWN(mp))
4267 xfs_force_shutdown(mp,
4268 (error == EFSCORRUPTED) ?
4269 SHUTDOWN_CORRUPT_INCORE :
4270 SHUTDOWN_META_IO_ERROR);
4273 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4274 free->xbfi_blockcount);
4275 xfs_bmap_del_free(flist, NULL, free);
4281 * Free up any items left in the list.
4285 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4287 xfs_bmap_free_item_t *free; /* free list item */
4288 xfs_bmap_free_item_t *next;
4290 if (flist->xbf_count == 0)
4292 ASSERT(flist->xbf_first != NULL);
4293 for (free = flist->xbf_first; free; free = next) {
4294 next = free->xbfi_next;
4295 xfs_bmap_del_free(flist, NULL, free);
4297 ASSERT(flist->xbf_count == 0);
4301 * Returns the file-relative block number of the first unused block(s)
4302 * in the file with at least "len" logically contiguous blocks free.
4303 * This is the lowest-address hole if the file has holes, else the first block
4304 * past the end of file.
4305 * Return 0 if the file is currently local (in-inode).
4308 xfs_bmap_first_unused(
4309 xfs_trans_t *tp, /* transaction pointer */
4310 xfs_inode_t *ip, /* incore inode */
4311 xfs_extlen_t len, /* size of hole to find */
4312 xfs_fileoff_t *first_unused, /* unused block */
4313 int whichfork) /* data or attr fork */
4315 xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
4316 int error; /* error return value */
4317 int idx; /* extent record index */
4318 xfs_ifork_t *ifp; /* inode fork pointer */
4319 xfs_fileoff_t lastaddr; /* last block number seen */
4320 xfs_fileoff_t lowest; /* lowest useful block */
4321 xfs_fileoff_t max; /* starting useful block */
4322 xfs_fileoff_t off; /* offset for this block */
4323 xfs_extnum_t nextents; /* number of extent entries */
4325 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4326 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4327 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4328 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4332 ifp = XFS_IFORK_PTR(ip, whichfork);
4333 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4334 (error = xfs_iread_extents(tp, ip, whichfork)))
4336 lowest = *first_unused;
4337 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4338 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
4339 ep = xfs_iext_get_ext(ifp, idx);
4340 off = xfs_bmbt_get_startoff(ep);
4342 * See if the hole before this extent will work.
4344 if (off >= lowest + len && off - max >= len) {
4345 *first_unused = max;
4348 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4349 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4351 *first_unused = max;
4356 * Returns the file-relative block number of the last block + 1 before
4357 * last_block (input value) in the file.
4358 * This is not based on i_size, it is based on the extent records.
4359 * Returns 0 for local files, as they do not have extent records.
4362 xfs_bmap_last_before(
4363 xfs_trans_t *tp, /* transaction pointer */
4364 xfs_inode_t *ip, /* incore inode */
4365 xfs_fileoff_t *last_block, /* last block */
4366 int whichfork) /* data or attr fork */
4368 xfs_fileoff_t bno; /* input file offset */
4369 int eof; /* hit end of file */
4370 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4371 int error; /* error return value */
4372 xfs_bmbt_irec_t got; /* current extent value */
4373 xfs_ifork_t *ifp; /* inode fork pointer */
4374 xfs_extnum_t lastx; /* last extent used */
4375 xfs_bmbt_irec_t prev; /* previous extent value */
4377 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4378 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4379 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4380 return XFS_ERROR(EIO);
4381 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4385 ifp = XFS_IFORK_PTR(ip, whichfork);
4386 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4387 (error = xfs_iread_extents(tp, ip, whichfork)))
4389 bno = *last_block - 1;
4390 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4392 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4393 if (prev.br_startoff == NULLFILEOFF)
4396 *last_block = prev.br_startoff + prev.br_blockcount;
4399 * Otherwise *last_block is already the right answer.
4405 * Returns the file-relative block number of the first block past eof in
4406 * the file. This is not based on i_size, it is based on the extent records.
4407 * Returns 0 for local files, as they do not have extent records.
4410 xfs_bmap_last_offset(
4411 xfs_trans_t *tp, /* transaction pointer */
4412 xfs_inode_t *ip, /* incore inode */
4413 xfs_fileoff_t *last_block, /* last block */
4414 int whichfork) /* data or attr fork */
4416 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4417 int error; /* error return value */
4418 xfs_ifork_t *ifp; /* inode fork pointer */
4419 xfs_extnum_t nextents; /* number of extent entries */
4421 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4422 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4423 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4424 return XFS_ERROR(EIO);
4425 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4429 ifp = XFS_IFORK_PTR(ip, whichfork);
4430 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4431 (error = xfs_iread_extents(tp, ip, whichfork)))
4433 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4438 ep = xfs_iext_get_ext(ifp, nextents - 1);
4439 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4444 * Returns whether the selected fork of the inode has exactly one
4445 * block or not. For the data fork we check this matches di_size,
4446 * implying the file's range is 0..bsize-1.
4448 int /* 1=>1 block, 0=>otherwise */
4450 xfs_inode_t *ip, /* incore inode */
4451 int whichfork) /* data or attr fork */
4453 xfs_bmbt_rec_t *ep; /* ptr to fork's extent */
4454 xfs_ifork_t *ifp; /* inode fork pointer */
4455 int rval; /* return value */
4456 xfs_bmbt_irec_t s; /* internal version of extent */
4459 if (whichfork == XFS_DATA_FORK)
4460 return ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize;
4462 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4464 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4466 ifp = XFS_IFORK_PTR(ip, whichfork);
4467 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4468 ep = xfs_iext_get_ext(ifp, 0);
4469 xfs_bmbt_get_all(ep, &s);
4470 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4471 if (rval && whichfork == XFS_DATA_FORK)
4472 ASSERT(ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4477 * Read in the extents to if_extents.
4478 * All inode fields are set up by caller, we just traverse the btree
4479 * and copy the records in. If the file system cannot contain unwritten
4480 * extents, the records are checked for no "state" flags.
4483 xfs_bmap_read_extents(
4484 xfs_trans_t *tp, /* transaction pointer */
4485 xfs_inode_t *ip, /* incore inode */
4486 int whichfork) /* data or attr fork */
4488 xfs_bmbt_block_t *block; /* current btree block */
4489 xfs_fsblock_t bno; /* block # of "block" */
4490 xfs_buf_t *bp; /* buffer for "block" */
4491 int error; /* error return value */
4492 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4493 #ifdef XFS_BMAP_TRACE
4494 static char fname[] = "xfs_bmap_read_extents";
4496 xfs_extnum_t i, j; /* index into the extents list */
4497 xfs_ifork_t *ifp; /* fork structure */
4498 int level; /* btree level, for checking */
4499 xfs_mount_t *mp; /* file system mount structure */
4500 __be64 *pp; /* pointer to block address */
4502 xfs_extnum_t room; /* number of entries there's room for */
4506 ifp = XFS_IFORK_PTR(ip, whichfork);
4507 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4508 XFS_EXTFMT_INODE(ip);
4509 block = ifp->if_broot;
4511 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4513 level = be16_to_cpu(block->bb_level);
4515 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
4516 bno = be64_to_cpu(*pp);
4517 ASSERT(bno != NULLDFSBNO);
4518 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4519 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4521 * Go down the tree until leaf level is reached, following the first
4522 * pointer (leftmost) at each level.
4524 while (level-- > 0) {
4525 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4526 XFS_BMAP_BTREE_REF)))
4528 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4529 XFS_WANT_CORRUPTED_GOTO(
4530 XFS_BMAP_SANITY_CHECK(mp, block, level),
4534 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
4535 1, mp->m_bmap_dmxr[1]);
4536 bno = be64_to_cpu(*pp);
4537 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4538 xfs_trans_brelse(tp, bp);
4541 * Here with bp and block set to the leftmost leaf node in the tree.
4543 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4546 * Loop over all leaf nodes. Copy information to the extent records.
4549 xfs_bmbt_rec_t *frp, *trp;
4550 xfs_fsblock_t nextbno;
4551 xfs_extnum_t num_recs;
4555 num_recs = be16_to_cpu(block->bb_numrecs);
4556 if (unlikely(i + num_recs > room)) {
4557 ASSERT(i + num_recs <= room);
4558 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4559 "corrupt dinode %Lu, (btree extents).",
4560 (unsigned long long) ip->i_ino);
4561 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4566 XFS_WANT_CORRUPTED_GOTO(
4567 XFS_BMAP_SANITY_CHECK(mp, block, 0),
4570 * Read-ahead the next leaf block, if any.
4572 nextbno = be64_to_cpu(block->bb_rightsib);
4573 if (nextbno != NULLFSBLOCK)
4574 xfs_btree_reada_bufl(mp, nextbno, 1);
4576 * Copy records into the extent records.
4578 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
4579 block, 1, mp->m_bmap_dmxr[0]);
4581 for (j = 0; j < num_recs; j++, i++, frp++) {
4582 trp = xfs_iext_get_ext(ifp, i);
4583 trp->l0 = INT_GET(frp->l0, ARCH_CONVERT);
4584 trp->l1 = INT_GET(frp->l1, ARCH_CONVERT);
4586 if (exntf == XFS_EXTFMT_NOSTATE) {
4588 * Check all attribute bmap btree records and
4589 * any "older" data bmap btree records for a
4590 * set bit in the "extent flag" position.
4592 if (unlikely(xfs_check_nostate_extents(ifp,
4593 start, num_recs))) {
4594 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4600 xfs_trans_brelse(tp, bp);
4603 * If we've reached the end, stop.
4605 if (bno == NULLFSBLOCK)
4607 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4608 XFS_BMAP_BTREE_REF)))
4610 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4612 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4613 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4614 xfs_bmap_trace_exlist(fname, ip, i, whichfork);
4617 xfs_trans_brelse(tp, bp);
4618 return XFS_ERROR(EFSCORRUPTED);
4621 #ifdef XFS_BMAP_TRACE
4623 * Add bmap trace insert entries for all the contents of the extent records.
4626 xfs_bmap_trace_exlist(
4627 char *fname, /* function name */
4628 xfs_inode_t *ip, /* incore inode pointer */
4629 xfs_extnum_t cnt, /* count of entries in the list */
4630 int whichfork) /* data or attr fork */
4632 xfs_bmbt_rec_t *ep; /* current extent record */
4633 xfs_extnum_t idx; /* extent record index */
4634 xfs_ifork_t *ifp; /* inode fork pointer */
4635 xfs_bmbt_irec_t s; /* file extent record */
4637 ifp = XFS_IFORK_PTR(ip, whichfork);
4638 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4639 for (idx = 0; idx < cnt; idx++) {
4640 ep = xfs_iext_get_ext(ifp, idx);
4641 xfs_bmbt_get_all(ep, &s);
4642 xfs_bmap_trace_insert(fname, "exlist", ip, idx, 1, &s, NULL,
4650 * Validate that the bmbt_irecs being returned from bmapi are valid
4651 * given the callers original parameters. Specifically check the
4652 * ranges of the returned irecs to ensure that they only extent beyond
4653 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4656 xfs_bmap_validate_ret(
4660 xfs_bmbt_irec_t *mval,
4664 int i; /* index to map values */
4666 ASSERT(ret_nmap <= nmap);
4668 for (i = 0; i < ret_nmap; i++) {
4669 ASSERT(mval[i].br_blockcount > 0);
4670 if (!(flags & XFS_BMAPI_ENTIRE)) {
4671 ASSERT(mval[i].br_startoff >= bno);
4672 ASSERT(mval[i].br_blockcount <= len);
4673 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4676 ASSERT(mval[i].br_startoff < bno + len);
4677 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4681 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4682 mval[i].br_startoff);
4683 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4684 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4685 mval[i].br_startblock != HOLESTARTBLOCK);
4686 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4687 mval[i].br_state == XFS_EXT_UNWRITTEN);
4694 * Map file blocks to filesystem blocks.
4695 * File range is given by the bno/len pair.
4696 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4697 * into a hole or past eof.
4698 * Only allocates blocks from a single allocation group,
4699 * to avoid locking problems.
4700 * The returned value in "firstblock" from the first call in a transaction
4701 * must be remembered and presented to subsequent calls in "firstblock".
4702 * An upper bound for the number of blocks to be allocated is supplied to
4703 * the first call in "total"; if no allocation group has that many free
4704 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4708 xfs_trans_t *tp, /* transaction pointer */
4709 xfs_inode_t *ip, /* incore inode */
4710 xfs_fileoff_t bno, /* starting file offs. mapped */
4711 xfs_filblks_t len, /* length to map in file */
4712 int flags, /* XFS_BMAPI_... */
4713 xfs_fsblock_t *firstblock, /* first allocated block
4714 controls a.g. for allocs */
4715 xfs_extlen_t total, /* total blocks needed */
4716 xfs_bmbt_irec_t *mval, /* output: map values */
4717 int *nmap, /* i/o: mval size/count */
4718 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4719 xfs_extdelta_t *delta) /* o: change made to incore extents */
4721 xfs_fsblock_t abno; /* allocated block number */
4722 xfs_extlen_t alen; /* allocated extent length */
4723 xfs_fileoff_t aoff; /* allocated file offset */
4724 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
4725 xfs_btree_cur_t *cur; /* bmap btree cursor */
4726 xfs_fileoff_t end; /* end of mapped file region */
4727 int eof; /* we've hit the end of extents */
4728 xfs_bmbt_rec_t *ep; /* extent record pointer */
4729 int error; /* error return */
4730 xfs_bmbt_irec_t got; /* current file extent record */
4731 xfs_ifork_t *ifp; /* inode fork pointer */
4732 xfs_extlen_t indlen; /* indirect blocks length */
4733 xfs_extnum_t lastx; /* last useful extent number */
4734 int logflags; /* flags for transaction logging */
4735 xfs_extlen_t minleft; /* min blocks left after allocation */
4736 xfs_extlen_t minlen; /* min allocation size */
4737 xfs_mount_t *mp; /* xfs mount structure */
4738 int n; /* current extent index */
4739 int nallocs; /* number of extents alloc\'d */
4740 xfs_extnum_t nextents; /* number of extents in file */
4741 xfs_fileoff_t obno; /* old block number (offset) */
4742 xfs_bmbt_irec_t prev; /* previous file extent record */
4743 int tmp_logflags; /* temp flags holder */
4744 int whichfork; /* data or attr fork */
4745 char inhole; /* current location is hole in file */
4746 char wasdelay; /* old extent was delayed */
4747 char wr; /* this is a write request */
4748 char rt; /* this is a realtime file */
4750 xfs_fileoff_t orig_bno; /* original block number value */
4751 int orig_flags; /* original flags arg value */
4752 xfs_filblks_t orig_len; /* original value of len arg */
4753 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4754 int orig_nmap; /* original value of *nmap */
4763 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4764 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4765 XFS_ATTR_FORK : XFS_DATA_FORK;
4767 if (unlikely(XFS_TEST_ERROR(
4768 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4769 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4770 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4771 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4772 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4773 return XFS_ERROR(EFSCORRUPTED);
4775 if (XFS_FORCED_SHUTDOWN(mp))
4776 return XFS_ERROR(EIO);
4777 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4778 ifp = XFS_IFORK_PTR(ip, whichfork);
4779 ASSERT(ifp->if_ext_max ==
4780 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4781 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4782 XFS_STATS_INC(xs_blk_mapw);
4784 XFS_STATS_INC(xs_blk_mapr);
4786 * IGSTATE flag is used to combine extents which
4787 * differ only due to the state of the extents.
4788 * This technique is used from xfs_getbmap()
4789 * when the caller does not wish to see the
4790 * separation (which is the default).
4792 * This technique is also used when writing a
4793 * buffer which has been partially written,
4794 * (usually by being flushed during a chunkread),
4795 * to ensure one write takes place. This also
4796 * prevents a change in the xfs inode extents at
4797 * this time, intentionally. This change occurs
4798 * on completion of the write operation, in
4799 * xfs_strat_comp(), where the xfs_bmapi() call
4800 * is transactioned, and the extents combined.
4802 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4803 wr = 0; /* no allocations are allowed */
4804 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
4808 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4810 if ((error = xfs_bmap_local_to_extents(tp, ip,
4811 firstblock, total, &logflags, whichfork)))
4814 if (wr && *firstblock == NULLFSBLOCK) {
4815 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4816 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4821 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4822 (error = xfs_iread_extents(tp, ip, whichfork)))
4824 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4826 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4832 delta->xed_startoff = NULLFILEOFF;
4833 delta->xed_blockcount = 0;
4835 while (bno < end && n < *nmap) {
4837 * Reading past eof, act as though there's a hole
4841 got.br_startoff = end;
4842 inhole = eof || got.br_startoff > bno;
4843 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
4844 ISNULLSTARTBLOCK(got.br_startblock);
4846 * First, deal with the hole before the allocated space
4847 * that we found, if any.
4849 if (wr && (inhole || wasdelay)) {
4851 * For the wasdelay case, we could also just
4852 * allocate the stuff asked for in this bmap call
4853 * but that wouldn't be as good.
4855 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
4856 alen = (xfs_extlen_t)got.br_blockcount;
4857 aoff = got.br_startoff;
4858 if (lastx != NULLEXTNUM && lastx) {
4859 ep = xfs_iext_get_ext(ifp, lastx - 1);
4860 xfs_bmbt_get_all(ep, &prev);
4862 } else if (wasdelay) {
4863 alen = (xfs_extlen_t)
4864 XFS_FILBLKS_MIN(len,
4866 got.br_blockcount) - bno);
4869 alen = (xfs_extlen_t)
4870 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4872 alen = (xfs_extlen_t)
4873 XFS_FILBLKS_MIN(alen,
4874 got.br_startoff - bno);
4877 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4878 if (flags & XFS_BMAPI_DELAY) {
4881 /* Figure out the extent size, adjust alen */
4883 if (!(extsz = ip->i_d.di_extsize))
4884 extsz = mp->m_sb.sb_rextsize;
4886 extsz = ip->i_d.di_extsize;
4889 error = xfs_bmap_extsize_align(mp,
4892 flags&XFS_BMAPI_DELAY,
4893 flags&XFS_BMAPI_CONVERT,
4899 extsz = alen / mp->m_sb.sb_rextsize;
4902 * Make a transaction-less quota reservation for
4903 * delayed allocation blocks. This number gets
4904 * adjusted later. We return if we haven't
4905 * allocated blocks already inside this loop.
4907 if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS(
4908 mp, NULL, ip, (long)alen, 0,
4909 rt ? XFS_QMOPT_RES_RTBLKS :
4910 XFS_QMOPT_RES_REGBLKS))) {
4913 ASSERT(cur == NULL);
4920 * Split changing sb for alen and indlen since
4921 * they could be coming from different places.
4923 indlen = (xfs_extlen_t)
4924 xfs_bmap_worst_indlen(ip, alen);
4928 error = xfs_mod_incore_sb(mp,
4931 XFS_BMAPI_RSVBLOCKS));
4933 error = xfs_mod_incore_sb(mp,
4936 XFS_BMAPI_RSVBLOCKS));
4939 error = xfs_mod_incore_sb(mp,
4942 XFS_BMAPI_RSVBLOCKS));
4944 xfs_mod_incore_sb(mp,
4947 XFS_BMAPI_RSVBLOCKS));
4949 xfs_mod_incore_sb(mp,
4952 XFS_BMAPI_RSVBLOCKS));
4956 if (XFS_IS_QUOTA_ON(mp))
4957 /* unreserve the blocks now */
4959 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4962 XFS_QMOPT_RES_RTBLKS :
4963 XFS_QMOPT_RES_REGBLKS);
4967 ip->i_delayed_blks += alen;
4968 abno = NULLSTARTBLOCK(indlen);
4971 * If first time, allocate and fill in
4972 * once-only bma fields.
4974 if (bma.ip == NULL) {
4982 /* Indicate if this is the first user data
4983 * in the file, or just any user data.
4985 if (!(flags & XFS_BMAPI_METADATA)) {
4986 bma.userdata = (aoff == 0) ?
4987 XFS_ALLOC_INITIAL_USER_DATA :
4991 * Fill in changeable bma fields.
4994 bma.firstblock = *firstblock;
4997 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4998 bma.wasdel = wasdelay;
4999 bma.minlen = minlen;
5000 bma.low = flist->xbf_low;
5001 bma.minleft = minleft;
5003 * Only want to do the alignment at the
5004 * eof if it is userdata and allocation length
5005 * is larger than a stripe unit.
5007 if (mp->m_dalign && alen >= mp->m_dalign &&
5008 (!(flags & XFS_BMAPI_METADATA)) &&
5009 (whichfork == XFS_DATA_FORK)) {
5010 if ((error = xfs_bmap_isaeof(ip, aoff,
5011 whichfork, &bma.aeof)))
5018 if ((error = xfs_bmap_alloc(&bma)))
5021 * Copy out result fields.
5024 if ((flist->xbf_low = bma.low))
5028 ASSERT(*firstblock == NULLFSBLOCK ||
5029 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5030 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
5032 XFS_FSB_TO_AGNO(mp, *firstblock) <
5033 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
5034 *firstblock = bma.firstblock;
5036 cur->bc_private.b.firstblock =
5038 if (abno == NULLFSBLOCK)
5040 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5041 cur = xfs_btree_init_cursor(mp,
5042 tp, NULL, 0, XFS_BTNUM_BMAP,
5044 cur->bc_private.b.firstblock =
5046 cur->bc_private.b.flist = flist;
5049 * Bump the number of extents we've allocated
5055 cur->bc_private.b.flags =
5056 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
5057 got.br_startoff = aoff;
5058 got.br_startblock = abno;
5059 got.br_blockcount = alen;
5060 got.br_state = XFS_EXT_NORM; /* assume normal */
5062 * Determine state of extent, and the filesystem.
5063 * A wasdelay extent has been initialized, so
5064 * shouldn't be flagged as unwritten.
5066 if (wr && XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5067 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5068 got.br_state = XFS_EXT_UNWRITTEN;
5070 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
5071 firstblock, flist, &tmp_logflags, delta,
5072 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5073 logflags |= tmp_logflags;
5076 lastx = ifp->if_lastex;
5077 ep = xfs_iext_get_ext(ifp, lastx);
5078 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5079 xfs_bmbt_get_all(ep, &got);
5080 ASSERT(got.br_startoff <= aoff);
5081 ASSERT(got.br_startoff + got.br_blockcount >=
5084 if (flags & XFS_BMAPI_DELAY) {
5085 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
5086 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
5088 ASSERT(got.br_state == XFS_EXT_NORM ||
5089 got.br_state == XFS_EXT_UNWRITTEN);
5092 * Fall down into the found allocated space case.
5094 } else if (inhole) {
5096 * Reading in a hole.
5098 mval->br_startoff = bno;
5099 mval->br_startblock = HOLESTARTBLOCK;
5100 mval->br_blockcount =
5101 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5102 mval->br_state = XFS_EXT_NORM;
5103 bno += mval->br_blockcount;
5104 len -= mval->br_blockcount;
5110 * Then deal with the allocated space we found.
5113 if (!(flags & XFS_BMAPI_ENTIRE) &&
5114 (got.br_startoff + got.br_blockcount > obno)) {
5117 ASSERT((bno >= obno) || (n == 0));
5119 mval->br_startoff = bno;
5120 if (ISNULLSTARTBLOCK(got.br_startblock)) {
5121 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5122 mval->br_startblock = DELAYSTARTBLOCK;
5124 mval->br_startblock =
5126 (bno - got.br_startoff);
5128 * Return the minimum of what we got and what we
5129 * asked for for the length. We can use the len
5130 * variable here because it is modified below
5131 * and we could have been there before coming
5132 * here if the first part of the allocation
5133 * didn't overlap what was asked for.
5135 mval->br_blockcount =
5136 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5137 (bno - got.br_startoff));
5138 mval->br_state = got.br_state;
5139 ASSERT(mval->br_blockcount <= len);
5142 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
5143 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5144 mval->br_startblock = DELAYSTARTBLOCK;
5149 * Check if writing previously allocated but
5150 * unwritten extents.
5152 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5153 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5155 * Modify (by adding) the state flag, if writing.
5157 ASSERT(mval->br_blockcount <= len);
5158 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5159 cur = xfs_btree_init_cursor(mp,
5160 tp, NULL, 0, XFS_BTNUM_BMAP,
5162 cur->bc_private.b.firstblock =
5164 cur->bc_private.b.flist = flist;
5166 mval->br_state = XFS_EXT_NORM;
5167 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
5168 firstblock, flist, &tmp_logflags, delta,
5169 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5170 logflags |= tmp_logflags;
5173 lastx = ifp->if_lastex;
5174 ep = xfs_iext_get_ext(ifp, lastx);
5175 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5176 xfs_bmbt_get_all(ep, &got);
5178 * We may have combined previously unwritten
5179 * space with written space, so generate
5182 if (mval->br_blockcount < len)
5186 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5187 ((mval->br_startoff + mval->br_blockcount) <= end));
5188 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5189 (mval->br_blockcount <= len) ||
5190 (mval->br_startoff < obno));
5191 bno = mval->br_startoff + mval->br_blockcount;
5193 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5194 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5195 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5196 ASSERT(mval->br_state == mval[-1].br_state);
5197 mval[-1].br_blockcount = mval->br_blockcount;
5198 mval[-1].br_state = mval->br_state;
5199 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5200 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5201 mval[-1].br_startblock != HOLESTARTBLOCK &&
5202 mval->br_startblock ==
5203 mval[-1].br_startblock + mval[-1].br_blockcount &&
5204 ((flags & XFS_BMAPI_IGSTATE) ||
5205 mval[-1].br_state == mval->br_state)) {
5206 ASSERT(mval->br_startoff ==
5207 mval[-1].br_startoff + mval[-1].br_blockcount);
5208 mval[-1].br_blockcount += mval->br_blockcount;
5210 mval->br_startblock == DELAYSTARTBLOCK &&
5211 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5212 mval->br_startoff ==
5213 mval[-1].br_startoff + mval[-1].br_blockcount) {
5214 mval[-1].br_blockcount += mval->br_blockcount;
5215 mval[-1].br_state = mval->br_state;
5216 } else if (!((n == 0) &&
5217 ((mval->br_startoff + mval->br_blockcount) <=
5223 * If we're done, stop now. Stop when we've allocated
5224 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5225 * the transaction may get too big.
5227 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5230 * Else go on to the next record.
5232 ep = xfs_iext_get_ext(ifp, ++lastx);
5233 if (lastx >= nextents) {
5237 xfs_bmbt_get_all(ep, &got);
5239 ifp->if_lastex = lastx;
5242 * Transform from btree to extents, give it cur.
5244 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5245 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5247 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5248 &tmp_logflags, whichfork);
5249 logflags |= tmp_logflags;
5253 ASSERT(ifp->if_ext_max ==
5254 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5255 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5256 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5258 if (delta && delta->xed_startoff != NULLFILEOFF) {
5259 /* A change was actually made.
5260 * Note that delta->xed_blockount is an offset at this
5261 * point and needs to be converted to a block count.
5263 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5264 delta->xed_blockcount -= delta->xed_startoff;
5268 * Log everything. Do this after conversion, there's no point in
5269 * logging the extent records if we've converted to btree format.
5271 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5272 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5273 logflags &= ~XFS_ILOG_FEXT(whichfork);
5274 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5275 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5276 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5278 * Log whatever the flags say, even if error. Otherwise we might miss
5279 * detecting a case where the data is changed, there's an error,
5280 * and it's not logged so we don't shutdown when we should.
5284 xfs_trans_log_inode(tp, ip, logflags);
5288 ASSERT(*firstblock == NULLFSBLOCK ||
5289 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5291 cur->bc_private.b.firstblock) ||
5293 XFS_FSB_TO_AGNO(mp, *firstblock) <
5295 cur->bc_private.b.firstblock)));
5296 *firstblock = cur->bc_private.b.firstblock;
5298 xfs_btree_del_cursor(cur,
5299 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5302 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5308 * Map file blocks to filesystem blocks, simple version.
5309 * One block (extent) only, read-only.
5310 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5311 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5312 * was set and all the others were clear.
5316 xfs_trans_t *tp, /* transaction pointer */
5317 xfs_inode_t *ip, /* incore inode */
5318 int whichfork, /* data or attr fork */
5319 xfs_fsblock_t *fsb, /* output: mapped block */
5320 xfs_fileoff_t bno) /* starting file offs. mapped */
5322 int eof; /* we've hit the end of extents */
5323 int error; /* error return */
5324 xfs_bmbt_irec_t got; /* current file extent record */
5325 xfs_ifork_t *ifp; /* inode fork pointer */
5326 xfs_extnum_t lastx; /* last useful extent number */
5327 xfs_bmbt_irec_t prev; /* previous file extent record */
5329 ifp = XFS_IFORK_PTR(ip, whichfork);
5331 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5332 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5333 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5335 return XFS_ERROR(EFSCORRUPTED);
5337 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5338 return XFS_ERROR(EIO);
5339 XFS_STATS_INC(xs_blk_mapr);
5340 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5341 (error = xfs_iread_extents(tp, ip, whichfork)))
5343 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5346 * Reading past eof, act as though there's a hole
5349 if (eof || got.br_startoff > bno) {
5353 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
5354 ASSERT(bno < got.br_startoff + got.br_blockcount);
5355 *fsb = got.br_startblock + (bno - got.br_startoff);
5356 ifp->if_lastex = lastx;
5361 * Unmap (remove) blocks from a file.
5362 * If nexts is nonzero then the number of extents to remove is limited to
5363 * that value. If not all extents in the block range can be removed then
5368 xfs_trans_t *tp, /* transaction pointer */
5369 struct xfs_inode *ip, /* incore inode */
5370 xfs_fileoff_t bno, /* starting offset to unmap */
5371 xfs_filblks_t len, /* length to unmap in file */
5372 int flags, /* misc flags */
5373 xfs_extnum_t nexts, /* number of extents max */
5374 xfs_fsblock_t *firstblock, /* first allocated block
5375 controls a.g. for allocs */
5376 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5377 xfs_extdelta_t *delta, /* o: change made to incore
5379 int *done) /* set if not done yet */
5381 xfs_btree_cur_t *cur; /* bmap btree cursor */
5382 xfs_bmbt_irec_t del; /* extent being deleted */
5383 int eof; /* is deleting at eof */
5384 xfs_bmbt_rec_t *ep; /* extent record pointer */
5385 int error; /* error return value */
5386 xfs_extnum_t extno; /* extent number in list */
5387 xfs_bmbt_irec_t got; /* current extent record */
5388 xfs_ifork_t *ifp; /* inode fork pointer */
5389 int isrt; /* freeing in rt area */
5390 xfs_extnum_t lastx; /* last extent index used */
5391 int logflags; /* transaction logging flags */
5392 xfs_extlen_t mod; /* rt extent offset */
5393 xfs_mount_t *mp; /* mount structure */
5394 xfs_extnum_t nextents; /* number of file extents */
5395 xfs_bmbt_irec_t prev; /* previous extent record */
5396 xfs_fileoff_t start; /* first file offset deleted */
5397 int tmp_logflags; /* partial logging flags */
5398 int wasdel; /* was a delayed alloc extent */
5399 int whichfork; /* data or attribute fork */
5400 int rsvd; /* OK to allocate reserved blocks */
5403 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5404 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5405 XFS_ATTR_FORK : XFS_DATA_FORK;
5406 ifp = XFS_IFORK_PTR(ip, whichfork);
5408 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5409 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5410 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5412 return XFS_ERROR(EFSCORRUPTED);
5415 if (XFS_FORCED_SHUTDOWN(mp))
5416 return XFS_ERROR(EIO);
5417 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5420 ASSERT(ifp->if_ext_max ==
5421 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5422 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5423 (error = xfs_iread_extents(tp, ip, whichfork)))
5425 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5426 if (nextents == 0) {
5430 XFS_STATS_INC(xs_blk_unmap);
5431 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5433 bno = start + len - 1;
5434 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5437 delta->xed_startoff = NULLFILEOFF;
5438 delta->xed_blockcount = 0;
5441 * Check to see if the given block number is past the end of the
5442 * file, back up to the last block if so...
5445 ep = xfs_iext_get_ext(ifp, --lastx);
5446 xfs_bmbt_get_all(ep, &got);
5447 bno = got.br_startoff + got.br_blockcount - 1;
5450 if (ifp->if_flags & XFS_IFBROOT) {
5451 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5452 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
5454 cur->bc_private.b.firstblock = *firstblock;
5455 cur->bc_private.b.flist = flist;
5456 cur->bc_private.b.flags = 0;
5460 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5461 (nexts == 0 || extno < nexts)) {
5463 * Is the found extent after a hole in which bno lives?
5464 * Just back up to the previous extent, if so.
5466 if (got.br_startoff > bno) {
5469 ep = xfs_iext_get_ext(ifp, lastx);
5470 xfs_bmbt_get_all(ep, &got);
5473 * Is the last block of this extent before the range
5474 * we're supposed to delete? If so, we're done.
5476 bno = XFS_FILEOFF_MIN(bno,
5477 got.br_startoff + got.br_blockcount - 1);
5481 * Then deal with the (possibly delayed) allocated space
5486 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
5487 if (got.br_startoff < start) {
5488 del.br_startoff = start;
5489 del.br_blockcount -= start - got.br_startoff;
5491 del.br_startblock += start - got.br_startoff;
5493 if (del.br_startoff + del.br_blockcount > bno + 1)
5494 del.br_blockcount = bno + 1 - del.br_startoff;
5495 sum = del.br_startblock + del.br_blockcount;
5497 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5499 * Realtime extent not lined up at the end.
5500 * The extent could have been split into written
5501 * and unwritten pieces, or we could just be
5502 * unmapping part of it. But we can't really
5503 * get rid of part of a realtime extent.
5505 if (del.br_state == XFS_EXT_UNWRITTEN ||
5506 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5508 * This piece is unwritten, or we're not
5509 * using unwritten extents. Skip over it.
5512 bno -= mod > del.br_blockcount ?
5513 del.br_blockcount : mod;
5514 if (bno < got.br_startoff) {
5516 xfs_bmbt_get_all(xfs_iext_get_ext(
5522 * It's written, turn it unwritten.
5523 * This is better than zeroing it.
5525 ASSERT(del.br_state == XFS_EXT_NORM);
5526 ASSERT(xfs_trans_get_block_res(tp) > 0);
5528 * If this spans a realtime extent boundary,
5529 * chop it back to the start of the one we end at.
5531 if (del.br_blockcount > mod) {
5532 del.br_startoff += del.br_blockcount - mod;
5533 del.br_startblock += del.br_blockcount - mod;
5534 del.br_blockcount = mod;
5536 del.br_state = XFS_EXT_UNWRITTEN;
5537 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5538 firstblock, flist, &logflags, delta,
5544 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5546 * Realtime extent is lined up at the end but not
5547 * at the front. We'll get rid of full extents if
5550 mod = mp->m_sb.sb_rextsize - mod;
5551 if (del.br_blockcount > mod) {
5552 del.br_blockcount -= mod;
5553 del.br_startoff += mod;
5554 del.br_startblock += mod;
5555 } else if ((del.br_startoff == start &&
5556 (del.br_state == XFS_EXT_UNWRITTEN ||
5557 xfs_trans_get_block_res(tp) == 0)) ||
5558 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5560 * Can't make it unwritten. There isn't
5561 * a full extent here so just skip it.
5563 ASSERT(bno >= del.br_blockcount);
5564 bno -= del.br_blockcount;
5565 if (bno < got.br_startoff) {
5567 xfs_bmbt_get_all(--ep, &got);
5570 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5572 * This one is already unwritten.
5573 * It must have a written left neighbor.
5574 * Unwrite the killed part of that one and
5578 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5580 ASSERT(prev.br_state == XFS_EXT_NORM);
5581 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
5582 ASSERT(del.br_startblock ==
5583 prev.br_startblock + prev.br_blockcount);
5584 if (prev.br_startoff < start) {
5585 mod = start - prev.br_startoff;
5586 prev.br_blockcount -= mod;
5587 prev.br_startblock += mod;
5588 prev.br_startoff = start;
5590 prev.br_state = XFS_EXT_UNWRITTEN;
5591 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5592 &prev, firstblock, flist, &logflags,
5593 delta, XFS_DATA_FORK, 0);
5598 ASSERT(del.br_state == XFS_EXT_NORM);
5599 del.br_state = XFS_EXT_UNWRITTEN;
5600 error = xfs_bmap_add_extent(ip, lastx, &cur,
5601 &del, firstblock, flist, &logflags,
5602 delta, XFS_DATA_FORK, 0);
5609 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
5610 /* Update realtime/data freespace, unreserve quota */
5612 xfs_filblks_t rtexts;
5614 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5615 do_div(rtexts, mp->m_sb.sb_rextsize);
5616 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5618 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5619 NULL, ip, -((long)del.br_blockcount), 0,
5620 XFS_QMOPT_RES_RTBLKS);
5622 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
5623 (int)del.br_blockcount, rsvd);
5624 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5625 NULL, ip, -((long)del.br_blockcount), 0,
5626 XFS_QMOPT_RES_REGBLKS);
5628 ip->i_delayed_blks -= del.br_blockcount;
5630 cur->bc_private.b.flags |=
5631 XFS_BTCUR_BPRV_WASDEL;
5633 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5635 * If it's the case where the directory code is running
5636 * with no block reservation, and the deleted block is in
5637 * the middle of its extent, and the resulting insert
5638 * of an extent would cause transformation to btree format,
5639 * then reject it. The calling code will then swap
5640 * blocks around instead.
5641 * We have to do this now, rather than waiting for the
5642 * conversion to btree format, since the transaction
5645 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5646 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5647 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5648 del.br_startoff > got.br_startoff &&
5649 del.br_startoff + del.br_blockcount <
5650 got.br_startoff + got.br_blockcount) {
5651 error = XFS_ERROR(ENOSPC);
5654 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5655 &tmp_logflags, delta, whichfork, rsvd);
5656 logflags |= tmp_logflags;
5659 bno = del.br_startoff - 1;
5661 lastx = ifp->if_lastex;
5663 * If not done go on to the next (previous) record.
5664 * Reset ep in case the extents array was re-alloced.
5666 ep = xfs_iext_get_ext(ifp, lastx);
5667 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5668 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5669 xfs_bmbt_get_startoff(ep) > bno) {
5671 ep = xfs_iext_get_ext(ifp, lastx);
5674 xfs_bmbt_get_all(ep, &got);
5678 ifp->if_lastex = lastx;
5679 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5680 ASSERT(ifp->if_ext_max ==
5681 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5683 * Convert to a btree if necessary.
5685 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5686 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5687 ASSERT(cur == NULL);
5688 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5689 &cur, 0, &tmp_logflags, whichfork);
5690 logflags |= tmp_logflags;
5695 * transform from btree to extents, give it cur
5697 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5698 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5699 ASSERT(cur != NULL);
5700 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5702 logflags |= tmp_logflags;
5707 * transform from extents to local?
5709 ASSERT(ifp->if_ext_max ==
5710 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5712 if (delta && delta->xed_startoff != NULLFILEOFF) {
5713 /* A change was actually made.
5714 * Note that delta->xed_blockount is an offset at this
5715 * point and needs to be converted to a block count.
5717 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5718 delta->xed_blockcount -= delta->xed_startoff;
5722 * Log everything. Do this after conversion, there's no point in
5723 * logging the extent records if we've converted to btree format.
5725 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5726 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5727 logflags &= ~XFS_ILOG_FEXT(whichfork);
5728 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5729 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5730 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5732 * Log inode even in the error case, if the transaction
5733 * is dirty we'll need to shut down the filesystem.
5736 xfs_trans_log_inode(tp, ip, logflags);
5739 *firstblock = cur->bc_private.b.firstblock;
5740 cur->bc_private.b.allocated = 0;
5742 xfs_btree_del_cursor(cur,
5743 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5749 * Fcntl interface to xfs_bmapi.
5751 int /* error code */
5753 bhv_desc_t *bdp, /* XFS behavior descriptor*/
5754 struct getbmap *bmv, /* user bmap structure */
5755 void __user *ap, /* pointer to user's array */
5756 int interface) /* interface flags */
5758 __int64_t bmvend; /* last block requested */
5759 int error; /* return value */
5760 __int64_t fixlen; /* length for -1 case */
5761 int i; /* extent number */
5762 xfs_inode_t *ip; /* xfs incore inode pointer */
5763 bhv_vnode_t *vp; /* corresponding vnode */
5764 int lock; /* lock state */
5765 xfs_bmbt_irec_t *map; /* buffer for user's data */
5766 xfs_mount_t *mp; /* file system mount point */
5767 int nex; /* # of user extents can do */
5768 int nexleft; /* # of user extents left */
5769 int subnex; /* # of bmapi's can do */
5770 int nmap; /* number of map entries */
5771 struct getbmap out; /* output structure */
5772 int whichfork; /* data or attr fork */
5773 int prealloced; /* this is a file with
5774 * preallocated data space */
5775 int sh_unwritten; /* true, if unwritten */
5776 /* extents listed separately */
5777 int bmapi_flags; /* flags for xfs_bmapi */
5778 __int32_t oflags; /* getbmapx bmv_oflags field */
5780 vp = BHV_TO_VNODE(bdp);
5781 ip = XFS_BHVTOI(bdp);
5784 whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5785 sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
5787 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5788 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5789 * bit is set for the file, generate a read event in order
5790 * that the DMAPI application may do its thing before we return
5791 * the extents. Usually this means restoring user file data to
5792 * regions of the file that look like holes.
5794 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5795 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5796 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5797 * could misinterpret holes in a DMAPI file as true holes,
5798 * when in fact they may represent offline user data.
5800 if ( (interface & BMV_IF_NO_DMAPI_READ) == 0
5801 && DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)
5802 && whichfork == XFS_DATA_FORK) {
5804 error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, 0, 0, 0, NULL);
5806 return XFS_ERROR(error);
5809 if (whichfork == XFS_ATTR_FORK) {
5810 if (XFS_IFORK_Q(ip)) {
5811 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5812 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5813 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5814 return XFS_ERROR(EINVAL);
5815 } else if (unlikely(
5816 ip->i_d.di_aformat != 0 &&
5817 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5818 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5820 return XFS_ERROR(EFSCORRUPTED);
5822 } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5823 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5824 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5825 return XFS_ERROR(EINVAL);
5826 if (whichfork == XFS_DATA_FORK) {
5827 if ((ip->i_d.di_extsize && (ip->i_d.di_flags &
5828 (XFS_DIFLAG_REALTIME|XFS_DIFLAG_EXTSIZE))) ||
5829 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5831 fixlen = XFS_MAXIOFFSET(mp);
5834 fixlen = ip->i_d.di_size;
5841 if (bmv->bmv_length == -1) {
5842 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5843 bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
5845 } else if (bmv->bmv_length < 0)
5846 return XFS_ERROR(EINVAL);
5847 if (bmv->bmv_length == 0) {
5848 bmv->bmv_entries = 0;
5851 nex = bmv->bmv_count - 1;
5853 return XFS_ERROR(EINVAL);
5854 bmvend = bmv->bmv_offset + bmv->bmv_length;
5856 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5858 if (whichfork == XFS_DATA_FORK && ip->i_delayed_blks) {
5859 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
5860 error = bhv_vop_flush_pages(vp, (xfs_off_t)0, -1, 0, FI_REMAPF);
5863 ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
5865 lock = xfs_ilock_map_shared(ip);
5868 * Don't let nex be bigger than the number of extents
5869 * we can have assuming alternating holes and real extents.
5871 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5872 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5874 bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
5875 ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
5878 * Allocate enough space to handle "subnex" maps at a time.
5881 map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
5883 bmv->bmv_entries = 0;
5885 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
5887 goto unlock_and_return;
5893 nmap = (nexleft > subnex) ? subnex : nexleft;
5894 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5895 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5896 bmapi_flags, NULL, 0, map, &nmap,
5899 goto unlock_and_return;
5900 ASSERT(nmap <= subnex);
5902 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5904 oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
5905 BMV_OF_PREALLOC : 0;
5906 out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
5907 out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5908 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
5909 if (map[i].br_startblock == HOLESTARTBLOCK &&
5910 ((prealloced && out.bmv_offset + out.bmv_length == bmvend) ||
5911 whichfork == XFS_ATTR_FORK )) {
5913 * came to hole at end of file or the end of
5916 goto unlock_and_return;
5919 (map[i].br_startblock == HOLESTARTBLOCK) ?
5921 XFS_FSB_TO_DB(ip, map[i].br_startblock);
5923 /* return either getbmap/getbmapx structure. */
5924 if (interface & BMV_IF_EXTENDED) {
5925 struct getbmapx outx;
5927 GETBMAP_CONVERT(out,outx);
5928 outx.bmv_oflags = oflags;
5929 outx.bmv_unused1 = outx.bmv_unused2 = 0;
5930 if (copy_to_user(ap, &outx,
5932 error = XFS_ERROR(EFAULT);
5933 goto unlock_and_return;
5936 if (copy_to_user(ap, &out,
5938 error = XFS_ERROR(EFAULT);
5939 goto unlock_and_return;
5943 out.bmv_offset + out.bmv_length;
5944 bmv->bmv_length = MAX((__int64_t)0,
5945 (__int64_t)(bmvend - bmv->bmv_offset));
5947 ap = (interface & BMV_IF_EXTENDED) ?
5949 ((struct getbmapx __user *)ap + 1) :
5951 ((struct getbmap __user *)ap + 1);
5954 } while (nmap && nexleft && bmv->bmv_length);
5957 xfs_iunlock_map_shared(ip, lock);
5958 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5960 kmem_free(map, subnex * sizeof(*map));
5966 * Check the last inode extent to determine whether this allocation will result
5967 * in blocks being allocated at the end of the file. When we allocate new data
5968 * blocks at the end of the file which do not start at the previous data block,
5969 * we will try to align the new blocks at stripe unit boundaries.
5971 STATIC int /* error */
5973 xfs_inode_t *ip, /* incore inode pointer */
5974 xfs_fileoff_t off, /* file offset in fsblocks */
5975 int whichfork, /* data or attribute fork */
5976 char *aeof) /* return value */
5978 int error; /* error return value */
5979 xfs_ifork_t *ifp; /* inode fork pointer */
5980 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
5981 xfs_extnum_t nextents; /* number of file extents */
5982 xfs_bmbt_irec_t s; /* expanded extent record */
5984 ASSERT(whichfork == XFS_DATA_FORK);
5985 ifp = XFS_IFORK_PTR(ip, whichfork);
5986 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5987 (error = xfs_iread_extents(NULL, ip, whichfork)))
5989 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5990 if (nextents == 0) {
5995 * Go to the last extent
5997 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5998 xfs_bmbt_get_all(lastrec, &s);
6000 * Check we are allocating in the last extent (for delayed allocations)
6001 * or past the last extent for non-delayed allocations.
6003 *aeof = (off >= s.br_startoff &&
6004 off < s.br_startoff + s.br_blockcount &&
6005 ISNULLSTARTBLOCK(s.br_startblock)) ||
6006 off >= s.br_startoff + s.br_blockcount;
6011 * Check if the endoff is outside the last extent. If so the caller will grow
6012 * the allocation to a stripe unit boundary.
6016 xfs_inode_t *ip, /* incore inode pointer */
6017 xfs_fileoff_t endoff, /* file offset in fsblocks */
6018 int whichfork, /* data or attribute fork */
6019 int *eof) /* result value */
6021 xfs_fsblock_t blockcount; /* extent block count */
6022 int error; /* error return value */
6023 xfs_ifork_t *ifp; /* inode fork pointer */
6024 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
6025 xfs_extnum_t nextents; /* number of file extents */
6026 xfs_fileoff_t startoff; /* extent starting file offset */
6028 ASSERT(whichfork == XFS_DATA_FORK);
6029 ifp = XFS_IFORK_PTR(ip, whichfork);
6030 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6031 (error = xfs_iread_extents(NULL, ip, whichfork)))
6033 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6034 if (nextents == 0) {
6039 * Go to the last extent
6041 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6042 startoff = xfs_bmbt_get_startoff(lastrec);
6043 blockcount = xfs_bmbt_get_blockcount(lastrec);
6044 *eof = endoff >= startoff + blockcount;
6050 * Check that the extents list for the inode ip is in the right order.
6053 xfs_bmap_check_extents(
6054 xfs_inode_t *ip, /* incore inode pointer */
6055 int whichfork) /* data or attr fork */
6057 xfs_bmbt_rec_t *ep; /* current extent entry */
6058 xfs_extnum_t idx; /* extent record index */
6059 xfs_ifork_t *ifp; /* inode fork pointer */
6060 xfs_extnum_t nextents; /* number of extents in list */
6061 xfs_bmbt_rec_t *nextp; /* next extent entry */
6063 ifp = XFS_IFORK_PTR(ip, whichfork);
6064 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
6065 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6066 ep = xfs_iext_get_ext(ifp, 0);
6067 for (idx = 0; idx < nextents - 1; idx++) {
6068 nextp = xfs_iext_get_ext(ifp, idx + 1);
6069 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
6078 xfs_btree_cur_t *cur,
6088 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6089 bp = cur->bc_bufs[i];
6091 if (XFS_BUF_ADDR(bp) == bno)
6092 break; /* Found it */
6094 if (i == XFS_BTREE_MAXLEVELS)
6097 if (!bp) { /* Chase down all the log items to see if the bp is there */
6098 xfs_log_item_chunk_t *licp;
6102 licp = &tp->t_items;
6103 while (!bp && licp != NULL) {
6104 if (XFS_LIC_ARE_ALL_FREE(licp)) {
6105 licp = licp->lic_next;
6108 for (i = 0; i < licp->lic_unused; i++) {
6109 xfs_log_item_desc_t *lidp;
6110 xfs_log_item_t *lip;
6111 xfs_buf_log_item_t *bip;
6114 if (XFS_LIC_ISFREE(licp, i)) {
6118 lidp = XFS_LIC_SLOT(licp, i);
6119 lip = lidp->lid_item;
6120 if (lip->li_type != XFS_LI_BUF)
6123 bip = (xfs_buf_log_item_t *)lip;
6126 if (XFS_BUF_ADDR(lbp) == bno) {
6128 break; /* Found it */
6131 licp = licp->lic_next;
6139 xfs_bmbt_block_t *block,
6145 __be64 *pp, *thispa; /* pointer to block address */
6146 xfs_bmbt_key_t *prevp, *keyp;
6148 ASSERT(be16_to_cpu(block->bb_level) > 0);
6151 for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
6152 dmxr = mp->m_bmap_dmxr[0];
6155 keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
6157 keyp = XFS_BTREE_KEY_ADDR(mp->m_sb.sb_blocksize,
6158 xfs_bmbt, block, i, dmxr);
6162 xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
6167 * Compare the block numbers to see if there are dups.
6171 pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
6173 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6174 xfs_bmbt, block, i, dmxr);
6176 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
6178 thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
6180 thispa = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6181 xfs_bmbt, block, j, dmxr);
6183 if (*thispa == *pp) {
6184 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
6186 (unsigned long long)be64_to_cpu(*thispa));
6187 panic("%s: ptrs are equal in node\n",
6195 * Check that the extents for the inode ip are in the right order in all
6200 xfs_bmap_check_leaf_extents(
6201 xfs_btree_cur_t *cur, /* btree cursor or null */
6202 xfs_inode_t *ip, /* incore inode pointer */
6203 int whichfork) /* data or attr fork */
6205 xfs_bmbt_block_t *block; /* current btree block */
6206 xfs_fsblock_t bno; /* block # of "block" */
6207 xfs_buf_t *bp; /* buffer for "block" */
6208 int error; /* error return value */
6209 xfs_extnum_t i=0, j; /* index into the extents list */
6210 xfs_ifork_t *ifp; /* fork structure */
6211 int level; /* btree level, for checking */
6212 xfs_mount_t *mp; /* file system mount structure */
6213 __be64 *pp; /* pointer to block address */
6214 xfs_bmbt_rec_t *ep; /* pointer to current extent */
6215 xfs_bmbt_rec_t *lastp; /* pointer to previous extent */
6216 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
6219 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6225 ifp = XFS_IFORK_PTR(ip, whichfork);
6226 block = ifp->if_broot;
6228 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6230 level = be16_to_cpu(block->bb_level);
6232 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6233 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6234 bno = be64_to_cpu(*pp);
6236 ASSERT(bno != NULLDFSBNO);
6237 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6238 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6241 * Go down the tree until leaf level is reached, following the first
6242 * pointer (leftmost) at each level.
6244 while (level-- > 0) {
6245 /* See if buf is in cur first */
6246 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6252 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6253 XFS_BMAP_BTREE_REF)))
6255 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6256 XFS_WANT_CORRUPTED_GOTO(
6257 XFS_BMAP_SANITY_CHECK(mp, block, level),
6263 * Check this block for basic sanity (increasing keys and
6264 * no duplicate blocks).
6267 xfs_check_block(block, mp, 0, 0);
6268 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
6269 1, mp->m_bmap_dmxr[1]);
6270 bno = be64_to_cpu(*pp);
6271 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
6274 xfs_trans_brelse(NULL, bp);
6279 * Here with bp and block set to the leftmost leaf node in the tree.
6284 * Loop over all leaf nodes checking that all extents are in the right order.
6288 xfs_fsblock_t nextbno;
6289 xfs_extnum_t num_recs;
6292 num_recs = be16_to_cpu(block->bb_numrecs);
6295 * Read-ahead the next leaf block, if any.
6298 nextbno = be64_to_cpu(block->bb_rightsib);
6301 * Check all the extents to make sure they are OK.
6302 * If we had a previous block, the last entry should
6303 * conform with the first entry in this one.
6306 ep = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
6307 block, 1, mp->m_bmap_dmxr[0]);
6308 for (j = 1; j < num_recs; j++) {
6309 nextp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
6310 block, j + 1, mp->m_bmap_dmxr[0]);
6312 xfs_btree_check_rec(XFS_BTNUM_BMAP,
6313 (void *)lastp, (void *)ep);
6315 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
6324 xfs_trans_brelse(NULL, bp);
6328 * If we've reached the end, stop.
6330 if (bno == NULLFSBLOCK)
6333 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6339 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6340 XFS_BMAP_BTREE_REF)))
6342 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6346 xfs_trans_brelse(NULL, bp);
6351 cmn_err(CE_WARN, "%s: at error0", __FUNCTION__);
6353 xfs_trans_brelse(NULL, bp);
6355 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
6357 panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__);
6363 * Count fsblocks of the given fork.
6366 xfs_bmap_count_blocks(
6367 xfs_trans_t *tp, /* transaction pointer */
6368 xfs_inode_t *ip, /* incore inode */
6369 int whichfork, /* data or attr fork */
6370 int *count) /* out: count of blocks */
6372 xfs_bmbt_block_t *block; /* current btree block */
6373 xfs_fsblock_t bno; /* block # of "block" */
6374 xfs_ifork_t *ifp; /* fork structure */
6375 int level; /* btree level, for checking */
6376 xfs_mount_t *mp; /* file system mount structure */
6377 __be64 *pp; /* pointer to block address */
6381 ifp = XFS_IFORK_PTR(ip, whichfork);
6382 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6383 if (unlikely(xfs_bmap_count_leaves(ifp, 0,
6384 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6386 XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)",
6387 XFS_ERRLEVEL_LOW, mp);
6388 return XFS_ERROR(EFSCORRUPTED);
6394 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6396 block = ifp->if_broot;
6397 level = be16_to_cpu(block->bb_level);
6399 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6400 bno = be64_to_cpu(*pp);
6401 ASSERT(bno != NULLDFSBNO);
6402 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6403 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6405 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
6406 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6408 return XFS_ERROR(EFSCORRUPTED);
6415 * Recursively walks each level of a btree
6416 * to count total fsblocks is use.
6419 xfs_bmap_count_tree(
6420 xfs_mount_t *mp, /* file system mount point */
6421 xfs_trans_t *tp, /* transaction pointer */
6422 xfs_ifork_t *ifp, /* inode fork pointer */
6423 xfs_fsblock_t blockno, /* file system block number */
6424 int levelin, /* level in btree */
6425 int *count) /* Count of blocks */
6428 xfs_buf_t *bp, *nbp;
6429 int level = levelin;
6431 xfs_fsblock_t bno = blockno;
6432 xfs_fsblock_t nextbno;
6433 xfs_bmbt_block_t *block, *nextblock;
6436 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6439 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6442 /* Not at node above leafs, count this level of nodes */
6443 nextbno = be64_to_cpu(block->bb_rightsib);
6444 while (nextbno != NULLFSBLOCK) {
6445 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6446 0, &nbp, XFS_BMAP_BTREE_REF)))
6449 nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
6450 nextbno = be64_to_cpu(nextblock->bb_rightsib);
6451 xfs_trans_brelse(tp, nbp);
6454 /* Dive to the next level */
6455 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6456 xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
6457 bno = be64_to_cpu(*pp);
6458 if (unlikely((error =
6459 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6460 xfs_trans_brelse(tp, bp);
6461 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6462 XFS_ERRLEVEL_LOW, mp);
6463 return XFS_ERROR(EFSCORRUPTED);
6465 xfs_trans_brelse(tp, bp);
6467 /* count all level 1 nodes and their leaves */
6469 nextbno = be64_to_cpu(block->bb_rightsib);
6470 numrecs = be16_to_cpu(block->bb_numrecs);
6471 if (unlikely(xfs_bmap_disk_count_leaves(ifp, mp,
6472 0, block, numrecs, count) < 0)) {
6473 xfs_trans_brelse(tp, bp);
6474 XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
6475 XFS_ERRLEVEL_LOW, mp);
6476 return XFS_ERROR(EFSCORRUPTED);
6478 xfs_trans_brelse(tp, bp);
6479 if (nextbno == NULLFSBLOCK)
6482 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6483 XFS_BMAP_BTREE_REF)))
6486 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6493 * Count leaf blocks given a range of extent records.
6496 xfs_bmap_count_leaves(
6503 xfs_bmbt_rec_t *frp;
6505 for (b = 0; b < numrecs; b++) {
6506 frp = xfs_iext_get_ext(ifp, idx + b);
6507 *count += xfs_bmbt_get_blockcount(frp);
6513 * Count leaf blocks given a range of extent records originally
6517 xfs_bmap_disk_count_leaves(
6521 xfs_bmbt_block_t *block,
6526 xfs_bmbt_rec_t *frp;
6528 for (b = 1; b <= numrecs; b++) {
6529 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize,
6530 xfs_bmbt, block, idx + b, mp->m_bmap_dmxr[0]);
6531 *count += xfs_bmbt_disk_get_blockcount(frp);