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_state = PREV.br_state;
1175 r[1].br_startblock = 0;
1176 r[1].br_startoff = new_endoff;
1177 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1178 r[1].br_blockcount = temp2;
1179 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1181 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1182 ip->i_df.if_lastex = idx + 1;
1183 ip->i_d.di_nextents++;
1185 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1187 rval = XFS_ILOG_CORE;
1188 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1189 new->br_startblock, new->br_blockcount,
1193 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1194 if ((error = xfs_bmbt_insert(cur, &i)))
1198 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1199 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1200 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1201 first, flist, &cur, 1, &tmp_rval,
1207 temp = xfs_bmap_worst_indlen(ip, temp);
1208 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1209 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
1210 (cur ? cur->bc_private.b.allocated : 0));
1212 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -diff, rsvd)) {
1214 * Ick gross gag me with a spoon.
1216 ASSERT(0); /* want to see if this ever happens! */
1222 !xfs_mod_incore_sb(ip->i_mount,
1223 XFS_SBS_FDBLOCKS, -diff, rsvd))
1230 !xfs_mod_incore_sb(ip->i_mount,
1231 XFS_SBS_FDBLOCKS, -diff, rsvd))
1236 ep = xfs_iext_get_ext(ifp, idx);
1237 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1238 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1239 xfs_bmap_trace_pre_update(fname, "0", ip, idx + 2,
1241 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1242 NULLSTARTBLOCK((int)temp2));
1243 xfs_bmap_trace_post_update(fname, "0", ip, idx + 2,
1245 *dnew = temp + temp2;
1246 /* DELTA: One in-core extent is split in three. */
1247 temp = PREV.br_startoff;
1248 temp2 = PREV.br_blockcount;
1251 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1252 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1253 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1254 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1255 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1256 case MASK(LEFT_CONTIG):
1257 case MASK(RIGHT_CONTIG):
1259 * These cases are all impossible.
1266 if (delta->xed_startoff > temp)
1267 delta->xed_startoff = temp;
1268 if (delta->xed_blockcount < temp2)
1269 delta->xed_blockcount = temp2;
1283 #undef STATE_SET_TEST
1288 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1289 * allocation to a real allocation or vice versa.
1291 STATIC int /* error */
1292 xfs_bmap_add_extent_unwritten_real(
1293 xfs_inode_t *ip, /* incore inode pointer */
1294 xfs_extnum_t idx, /* extent number to update/insert */
1295 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1296 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1297 int *logflagsp, /* inode logging flags */
1298 xfs_extdelta_t *delta) /* Change made to incore extents */
1300 xfs_btree_cur_t *cur; /* btree cursor */
1301 xfs_bmbt_rec_t *ep; /* extent entry for idx */
1302 int error; /* error return value */
1303 #ifdef XFS_BMAP_TRACE
1304 static char fname[] = "xfs_bmap_add_extent_unwritten_real";
1306 int i; /* temp state */
1307 xfs_ifork_t *ifp; /* inode fork pointer */
1308 xfs_fileoff_t new_endoff; /* end offset of new entry */
1309 xfs_exntst_t newext; /* new extent state */
1310 xfs_exntst_t oldext; /* old extent state */
1311 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1312 /* left is 0, right is 1, prev is 2 */
1313 int rval=0; /* return value (logging flags) */
1314 int state = 0;/* state bits, accessed thru macros */
1315 xfs_filblks_t temp=0;
1316 xfs_filblks_t temp2=0;
1317 enum { /* bit number definitions for state */
1318 LEFT_CONTIG, RIGHT_CONTIG,
1319 LEFT_FILLING, RIGHT_FILLING,
1320 LEFT_DELAY, RIGHT_DELAY,
1321 LEFT_VALID, RIGHT_VALID
1327 #define MASK(b) (1 << (b))
1328 #define MASK2(a,b) (MASK(a) | MASK(b))
1329 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1330 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1331 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1332 #define STATE_TEST(b) (state & MASK(b))
1333 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1334 ((state &= ~MASK(b)), 0))
1335 #define SWITCH_STATE \
1336 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1339 * Set up a bunch of variables to make the tests simpler.
1343 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1344 ep = xfs_iext_get_ext(ifp, idx);
1345 xfs_bmbt_get_all(ep, &PREV);
1346 newext = new->br_state;
1347 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1348 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1349 ASSERT(PREV.br_state == oldext);
1350 new_endoff = new->br_startoff + new->br_blockcount;
1351 ASSERT(PREV.br_startoff <= new->br_startoff);
1352 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1354 * Set flags determining what part of the previous oldext allocation
1355 * extent is being replaced by a newext allocation.
1357 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
1358 STATE_SET(RIGHT_FILLING,
1359 PREV.br_startoff + PREV.br_blockcount == new_endoff);
1361 * Check and set flags if this segment has a left neighbor.
1362 * Don't set contiguous if the combined extent would be too large.
1364 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1365 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1366 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
1368 STATE_SET(LEFT_CONTIG,
1369 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1370 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1371 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1372 LEFT.br_state == newext &&
1373 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1375 * Check and set flags if this segment has a right neighbor.
1376 * Don't set contiguous if the combined extent would be too large.
1377 * Also check for all-three-contiguous being too large.
1379 if (STATE_SET_TEST(RIGHT_VALID,
1381 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
1382 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1383 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
1385 STATE_SET(RIGHT_CONTIG,
1386 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1387 new_endoff == RIGHT.br_startoff &&
1388 new->br_startblock + new->br_blockcount ==
1389 RIGHT.br_startblock &&
1390 newext == RIGHT.br_state &&
1391 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1392 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
1393 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
1394 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1397 * Switch out based on the FILLING and CONTIG state bits.
1399 switch (SWITCH_STATE) {
1401 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1403 * Setting all of a previous oldext extent to newext.
1404 * The left and right neighbors are both contiguous with new.
1406 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1408 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1409 LEFT.br_blockcount + PREV.br_blockcount +
1410 RIGHT.br_blockcount);
1411 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1413 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
1415 xfs_iext_remove(ifp, idx, 2);
1416 ip->i_df.if_lastex = idx - 1;
1417 ip->i_d.di_nextents -= 2;
1419 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1421 rval = XFS_ILOG_CORE;
1422 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1423 RIGHT.br_startblock,
1424 RIGHT.br_blockcount, &i)))
1427 if ((error = xfs_bmbt_delete(cur, &i)))
1430 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1433 if ((error = xfs_bmbt_delete(cur, &i)))
1436 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1439 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1441 LEFT.br_blockcount + PREV.br_blockcount +
1442 RIGHT.br_blockcount, LEFT.br_state)))
1445 /* DELTA: Three in-core extents are replaced by one. */
1446 temp = LEFT.br_startoff;
1447 temp2 = LEFT.br_blockcount +
1448 PREV.br_blockcount +
1449 RIGHT.br_blockcount;
1452 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
1454 * Setting all of a previous oldext extent to newext.
1455 * The left neighbor is contiguous, the right is not.
1457 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
1459 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1460 LEFT.br_blockcount + PREV.br_blockcount);
1461 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
1463 ip->i_df.if_lastex = idx - 1;
1464 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
1466 xfs_iext_remove(ifp, idx, 1);
1467 ip->i_d.di_nextents--;
1469 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1471 rval = XFS_ILOG_CORE;
1472 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1473 PREV.br_startblock, PREV.br_blockcount,
1477 if ((error = xfs_bmbt_delete(cur, &i)))
1480 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1483 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1485 LEFT.br_blockcount + PREV.br_blockcount,
1489 /* DELTA: Two in-core extents are replaced by one. */
1490 temp = LEFT.br_startoff;
1491 temp2 = LEFT.br_blockcount +
1495 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
1497 * Setting all of a previous oldext extent to newext.
1498 * The right neighbor is contiguous, the left is not.
1500 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
1502 xfs_bmbt_set_blockcount(ep,
1503 PREV.br_blockcount + RIGHT.br_blockcount);
1504 xfs_bmbt_set_state(ep, newext);
1505 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
1507 ip->i_df.if_lastex = idx;
1508 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
1510 xfs_iext_remove(ifp, idx + 1, 1);
1511 ip->i_d.di_nextents--;
1513 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1515 rval = XFS_ILOG_CORE;
1516 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1517 RIGHT.br_startblock,
1518 RIGHT.br_blockcount, &i)))
1521 if ((error = xfs_bmbt_delete(cur, &i)))
1524 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1527 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1529 new->br_blockcount + RIGHT.br_blockcount,
1533 /* DELTA: Two in-core extents are replaced by one. */
1534 temp = PREV.br_startoff;
1535 temp2 = PREV.br_blockcount +
1536 RIGHT.br_blockcount;
1539 case MASK2(LEFT_FILLING, RIGHT_FILLING):
1541 * Setting all of a previous oldext extent to newext.
1542 * Neither the left nor right neighbors are contiguous with
1545 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
1547 xfs_bmbt_set_state(ep, newext);
1548 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
1550 ip->i_df.if_lastex = idx;
1552 rval = XFS_ILOG_DEXT;
1555 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1556 new->br_startblock, new->br_blockcount,
1560 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1561 new->br_startblock, new->br_blockcount,
1565 /* DELTA: The in-core extent described by new changed type. */
1566 temp = new->br_startoff;
1567 temp2 = new->br_blockcount;
1570 case MASK2(LEFT_FILLING, LEFT_CONTIG):
1572 * Setting the first part of a previous oldext extent to newext.
1573 * The left neighbor is contiguous.
1575 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
1577 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1578 LEFT.br_blockcount + new->br_blockcount);
1579 xfs_bmbt_set_startoff(ep,
1580 PREV.br_startoff + new->br_blockcount);
1581 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
1583 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
1585 xfs_bmbt_set_startblock(ep,
1586 new->br_startblock + new->br_blockcount);
1587 xfs_bmbt_set_blockcount(ep,
1588 PREV.br_blockcount - new->br_blockcount);
1589 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
1591 ip->i_df.if_lastex = idx - 1;
1593 rval = XFS_ILOG_DEXT;
1596 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1597 PREV.br_startblock, PREV.br_blockcount,
1601 if ((error = xfs_bmbt_update(cur,
1602 PREV.br_startoff + new->br_blockcount,
1603 PREV.br_startblock + new->br_blockcount,
1604 PREV.br_blockcount - new->br_blockcount,
1607 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1609 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1611 LEFT.br_blockcount + new->br_blockcount,
1615 /* DELTA: The boundary between two in-core extents moved. */
1616 temp = LEFT.br_startoff;
1617 temp2 = LEFT.br_blockcount +
1621 case MASK(LEFT_FILLING):
1623 * Setting the first part of a previous oldext extent to newext.
1624 * The left neighbor is not contiguous.
1626 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1627 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1628 xfs_bmbt_set_startoff(ep, new_endoff);
1629 xfs_bmbt_set_blockcount(ep,
1630 PREV.br_blockcount - new->br_blockcount);
1631 xfs_bmbt_set_startblock(ep,
1632 new->br_startblock + new->br_blockcount);
1633 xfs_bmap_trace_post_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1634 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
1636 xfs_iext_insert(ifp, idx, 1, new);
1637 ip->i_df.if_lastex = idx;
1638 ip->i_d.di_nextents++;
1640 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1642 rval = XFS_ILOG_CORE;
1643 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1644 PREV.br_startblock, PREV.br_blockcount,
1648 if ((error = xfs_bmbt_update(cur,
1649 PREV.br_startoff + new->br_blockcount,
1650 PREV.br_startblock + new->br_blockcount,
1651 PREV.br_blockcount - new->br_blockcount,
1654 cur->bc_rec.b = *new;
1655 if ((error = xfs_bmbt_insert(cur, &i)))
1659 /* DELTA: One in-core extent is split in two. */
1660 temp = PREV.br_startoff;
1661 temp2 = PREV.br_blockcount;
1664 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1666 * Setting the last part of a previous oldext extent to newext.
1667 * The right neighbor is contiguous with the new allocation.
1669 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1671 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1673 xfs_bmbt_set_blockcount(ep,
1674 PREV.br_blockcount - new->br_blockcount);
1675 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1677 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1678 new->br_startoff, new->br_startblock,
1679 new->br_blockcount + RIGHT.br_blockcount, newext);
1680 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1682 ip->i_df.if_lastex = idx + 1;
1684 rval = XFS_ILOG_DEXT;
1687 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1689 PREV.br_blockcount, &i)))
1692 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1694 PREV.br_blockcount - new->br_blockcount,
1697 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1699 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1701 new->br_blockcount + RIGHT.br_blockcount,
1705 /* DELTA: The boundary between two in-core extents moved. */
1706 temp = PREV.br_startoff;
1707 temp2 = PREV.br_blockcount +
1708 RIGHT.br_blockcount;
1711 case MASK(RIGHT_FILLING):
1713 * Setting the last part of a previous oldext extent to newext.
1714 * The right neighbor is not contiguous.
1716 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1717 xfs_bmbt_set_blockcount(ep,
1718 PREV.br_blockcount - new->br_blockcount);
1719 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1720 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1721 new, NULL, XFS_DATA_FORK);
1722 xfs_iext_insert(ifp, idx + 1, 1, new);
1723 ip->i_df.if_lastex = idx + 1;
1724 ip->i_d.di_nextents++;
1726 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1728 rval = XFS_ILOG_CORE;
1729 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1730 PREV.br_startblock, PREV.br_blockcount,
1734 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1736 PREV.br_blockcount - new->br_blockcount,
1739 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1740 new->br_startblock, new->br_blockcount,
1744 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1745 if ((error = xfs_bmbt_insert(cur, &i)))
1749 /* DELTA: One in-core extent is split in two. */
1750 temp = PREV.br_startoff;
1751 temp2 = PREV.br_blockcount;
1756 * Setting the middle part of a previous oldext extent to
1757 * newext. Contiguity is impossible here.
1758 * One extent becomes three extents.
1760 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1761 xfs_bmbt_set_blockcount(ep,
1762 new->br_startoff - PREV.br_startoff);
1763 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1765 r[1].br_startoff = new_endoff;
1766 r[1].br_blockcount =
1767 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1768 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1769 r[1].br_state = oldext;
1770 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1772 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1773 ip->i_df.if_lastex = idx + 1;
1774 ip->i_d.di_nextents += 2;
1776 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1778 rval = XFS_ILOG_CORE;
1779 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1780 PREV.br_startblock, PREV.br_blockcount,
1784 /* new right extent - oldext */
1785 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1786 r[1].br_startblock, r[1].br_blockcount,
1789 /* new left extent - oldext */
1790 PREV.br_blockcount =
1791 new->br_startoff - PREV.br_startoff;
1792 cur->bc_rec.b = PREV;
1793 if ((error = xfs_bmbt_insert(cur, &i)))
1796 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1799 /* new middle extent - newext */
1800 cur->bc_rec.b = *new;
1801 if ((error = xfs_bmbt_insert(cur, &i)))
1805 /* DELTA: One in-core extent is split in three. */
1806 temp = PREV.br_startoff;
1807 temp2 = PREV.br_blockcount;
1810 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1811 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1812 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1813 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1814 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1815 case MASK(LEFT_CONTIG):
1816 case MASK(RIGHT_CONTIG):
1818 * These cases are all impossible.
1825 if (delta->xed_startoff > temp)
1826 delta->xed_startoff = temp;
1827 if (delta->xed_blockcount < temp2)
1828 delta->xed_blockcount = temp2;
1842 #undef STATE_SET_TEST
1847 * Called by xfs_bmap_add_extent to handle cases converting a hole
1848 * to a delayed allocation.
1851 STATIC int /* error */
1852 xfs_bmap_add_extent_hole_delay(
1853 xfs_inode_t *ip, /* incore inode pointer */
1854 xfs_extnum_t idx, /* extent number to update/insert */
1855 xfs_btree_cur_t *cur, /* if null, not a btree */
1856 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1857 int *logflagsp, /* inode logging flags */
1858 xfs_extdelta_t *delta, /* Change made to incore extents */
1859 int rsvd) /* OK to allocate reserved blocks */
1861 xfs_bmbt_rec_t *ep; /* extent record for idx */
1862 #ifdef XFS_BMAP_TRACE
1863 static char fname[] = "xfs_bmap_add_extent_hole_delay";
1865 xfs_ifork_t *ifp; /* inode fork pointer */
1866 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1867 xfs_filblks_t newlen=0; /* new indirect size */
1868 xfs_filblks_t oldlen=0; /* old indirect size */
1869 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1870 int state; /* state bits, accessed thru macros */
1871 xfs_filblks_t temp=0; /* temp for indirect calculations */
1872 xfs_filblks_t temp2=0;
1873 enum { /* bit number definitions for state */
1874 LEFT_CONTIG, RIGHT_CONTIG,
1875 LEFT_DELAY, RIGHT_DELAY,
1876 LEFT_VALID, RIGHT_VALID
1879 #define MASK(b) (1 << (b))
1880 #define MASK2(a,b) (MASK(a) | MASK(b))
1881 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1882 #define STATE_TEST(b) (state & MASK(b))
1883 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1884 ((state &= ~MASK(b)), 0))
1885 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1887 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1888 ep = xfs_iext_get_ext(ifp, idx);
1890 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1892 * Check and set flags if this segment has a left neighbor
1894 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1895 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1896 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1899 * Check and set flags if the current (right) segment exists.
1900 * If it doesn't exist, we're converting the hole at end-of-file.
1902 if (STATE_SET_TEST(RIGHT_VALID,
1904 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1905 xfs_bmbt_get_all(ep, &right);
1906 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1909 * Set contiguity flags on the left and right neighbors.
1910 * Don't let extents get too large, even if the pieces are contiguous.
1912 STATE_SET(LEFT_CONTIG,
1913 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1914 left.br_startoff + left.br_blockcount == new->br_startoff &&
1915 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1916 STATE_SET(RIGHT_CONTIG,
1917 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1918 new->br_startoff + new->br_blockcount == right.br_startoff &&
1919 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1920 (!STATE_TEST(LEFT_CONTIG) ||
1921 (left.br_blockcount + new->br_blockcount +
1922 right.br_blockcount <= MAXEXTLEN)));
1924 * Switch out based on the contiguity flags.
1926 switch (SWITCH_STATE) {
1928 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1930 * New allocation is contiguous with delayed allocations
1931 * on the left and on the right.
1932 * Merge all three into a single extent record.
1934 temp = left.br_blockcount + new->br_blockcount +
1935 right.br_blockcount;
1936 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
1938 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1939 oldlen = STARTBLOCKVAL(left.br_startblock) +
1940 STARTBLOCKVAL(new->br_startblock) +
1941 STARTBLOCKVAL(right.br_startblock);
1942 newlen = xfs_bmap_worst_indlen(ip, temp);
1943 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1944 NULLSTARTBLOCK((int)newlen));
1945 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
1947 xfs_bmap_trace_delete(fname, "LC|RC", ip, idx, 1,
1949 xfs_iext_remove(ifp, idx, 1);
1950 ip->i_df.if_lastex = idx - 1;
1951 /* DELTA: Two in-core extents were replaced by one. */
1953 temp = left.br_startoff;
1956 case MASK(LEFT_CONTIG):
1958 * New allocation is contiguous with a delayed allocation
1960 * Merge the new allocation with the left neighbor.
1962 temp = left.br_blockcount + new->br_blockcount;
1963 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1,
1965 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1966 oldlen = STARTBLOCKVAL(left.br_startblock) +
1967 STARTBLOCKVAL(new->br_startblock);
1968 newlen = xfs_bmap_worst_indlen(ip, temp);
1969 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1970 NULLSTARTBLOCK((int)newlen));
1971 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1,
1973 ip->i_df.if_lastex = idx - 1;
1974 /* DELTA: One in-core extent grew into a hole. */
1976 temp = left.br_startoff;
1979 case MASK(RIGHT_CONTIG):
1981 * New allocation is contiguous with a delayed allocation
1983 * Merge the new allocation with the right neighbor.
1985 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1986 temp = new->br_blockcount + right.br_blockcount;
1987 oldlen = STARTBLOCKVAL(new->br_startblock) +
1988 STARTBLOCKVAL(right.br_startblock);
1989 newlen = xfs_bmap_worst_indlen(ip, temp);
1990 xfs_bmbt_set_allf(ep, new->br_startoff,
1991 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
1992 xfs_bmap_trace_post_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1993 ip->i_df.if_lastex = idx;
1994 /* DELTA: One in-core extent grew into a hole. */
1996 temp = new->br_startoff;
2001 * New allocation is not contiguous with another
2002 * delayed allocation.
2003 * Insert a new entry.
2005 oldlen = newlen = 0;
2006 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
2008 xfs_iext_insert(ifp, idx, 1, new);
2009 ip->i_df.if_lastex = idx;
2010 /* DELTA: A new in-core extent was added in a hole. */
2011 temp2 = new->br_blockcount;
2012 temp = new->br_startoff;
2015 if (oldlen != newlen) {
2016 ASSERT(oldlen > newlen);
2017 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
2018 (int)(oldlen - newlen), rsvd);
2020 * Nothing to do for disk quota accounting here.
2025 if (delta->xed_startoff > temp)
2026 delta->xed_startoff = temp;
2027 if (delta->xed_blockcount < temp2)
2028 delta->xed_blockcount = temp2;
2036 #undef STATE_SET_TEST
2041 * Called by xfs_bmap_add_extent to handle cases converting a hole
2042 * to a real allocation.
2044 STATIC int /* error */
2045 xfs_bmap_add_extent_hole_real(
2046 xfs_inode_t *ip, /* incore inode pointer */
2047 xfs_extnum_t idx, /* extent number to update/insert */
2048 xfs_btree_cur_t *cur, /* if null, not a btree */
2049 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2050 int *logflagsp, /* inode logging flags */
2051 xfs_extdelta_t *delta, /* Change made to incore extents */
2052 int whichfork) /* data or attr fork */
2054 xfs_bmbt_rec_t *ep; /* pointer to extent entry ins. point */
2055 int error; /* error return value */
2056 #ifdef XFS_BMAP_TRACE
2057 static char fname[] = "xfs_bmap_add_extent_hole_real";
2059 int i; /* temp state */
2060 xfs_ifork_t *ifp; /* inode fork pointer */
2061 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2062 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2063 int rval=0; /* return value (logging flags) */
2064 int state; /* state bits, accessed thru macros */
2065 xfs_filblks_t temp=0;
2066 xfs_filblks_t temp2=0;
2067 enum { /* bit number definitions for state */
2068 LEFT_CONTIG, RIGHT_CONTIG,
2069 LEFT_DELAY, RIGHT_DELAY,
2070 LEFT_VALID, RIGHT_VALID
2073 #define MASK(b) (1 << (b))
2074 #define MASK2(a,b) (MASK(a) | MASK(b))
2075 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
2076 #define STATE_TEST(b) (state & MASK(b))
2077 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
2078 ((state &= ~MASK(b)), 0))
2079 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
2081 ifp = XFS_IFORK_PTR(ip, whichfork);
2082 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
2083 ep = xfs_iext_get_ext(ifp, idx);
2086 * Check and set flags if this segment has a left neighbor.
2088 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
2089 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
2090 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
2093 * Check and set flags if this segment has a current value.
2094 * Not true if we're inserting into the "hole" at eof.
2096 if (STATE_SET_TEST(RIGHT_VALID,
2098 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
2099 xfs_bmbt_get_all(ep, &right);
2100 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
2103 * We're inserting a real allocation between "left" and "right".
2104 * Set the contiguity flags. Don't let extents get too large.
2106 STATE_SET(LEFT_CONTIG,
2107 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
2108 left.br_startoff + left.br_blockcount == new->br_startoff &&
2109 left.br_startblock + left.br_blockcount == new->br_startblock &&
2110 left.br_state == new->br_state &&
2111 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
2112 STATE_SET(RIGHT_CONTIG,
2113 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
2114 new->br_startoff + new->br_blockcount == right.br_startoff &&
2115 new->br_startblock + new->br_blockcount ==
2116 right.br_startblock &&
2117 new->br_state == right.br_state &&
2118 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2119 (!STATE_TEST(LEFT_CONTIG) ||
2120 left.br_blockcount + new->br_blockcount +
2121 right.br_blockcount <= MAXEXTLEN));
2125 * Select which case we're in here, and implement it.
2127 switch (SWITCH_STATE) {
2129 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
2131 * New allocation is contiguous with real allocations on the
2132 * left and on the right.
2133 * Merge all three into a single extent record.
2135 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
2137 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2138 left.br_blockcount + new->br_blockcount +
2139 right.br_blockcount);
2140 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
2142 xfs_bmap_trace_delete(fname, "LC|RC", ip,
2144 xfs_iext_remove(ifp, idx, 1);
2145 ifp->if_lastex = idx - 1;
2146 XFS_IFORK_NEXT_SET(ip, whichfork,
2147 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2149 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2151 rval = XFS_ILOG_CORE;
2152 if ((error = xfs_bmbt_lookup_eq(cur,
2154 right.br_startblock,
2155 right.br_blockcount, &i)))
2158 if ((error = xfs_bmbt_delete(cur, &i)))
2161 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2164 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2166 left.br_blockcount +
2167 new->br_blockcount +
2168 right.br_blockcount,
2172 /* DELTA: Two in-core extents were replaced by one. */
2173 temp = left.br_startoff;
2174 temp2 = left.br_blockcount +
2175 new->br_blockcount +
2176 right.br_blockcount;
2179 case MASK(LEFT_CONTIG):
2181 * New allocation is contiguous with a real allocation
2183 * Merge the new allocation with the left neighbor.
2185 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1, whichfork);
2186 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2187 left.br_blockcount + new->br_blockcount);
2188 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1, whichfork);
2189 ifp->if_lastex = idx - 1;
2191 rval = XFS_ILOG_FEXT(whichfork);
2194 if ((error = xfs_bmbt_lookup_eq(cur,
2197 left.br_blockcount, &i)))
2200 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2202 left.br_blockcount +
2207 /* DELTA: One in-core extent grew. */
2208 temp = left.br_startoff;
2209 temp2 = left.br_blockcount +
2213 case MASK(RIGHT_CONTIG):
2215 * New allocation is contiguous with a real allocation
2217 * Merge the new allocation with the right neighbor.
2219 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, whichfork);
2220 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2221 new->br_blockcount + right.br_blockcount,
2223 xfs_bmap_trace_post_update(fname, "RC", ip, idx, whichfork);
2224 ifp->if_lastex = idx;
2226 rval = XFS_ILOG_FEXT(whichfork);
2229 if ((error = xfs_bmbt_lookup_eq(cur,
2231 right.br_startblock,
2232 right.br_blockcount, &i)))
2235 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2237 new->br_blockcount +
2238 right.br_blockcount,
2242 /* DELTA: One in-core extent grew. */
2243 temp = new->br_startoff;
2244 temp2 = new->br_blockcount +
2245 right.br_blockcount;
2250 * New allocation is not contiguous with another
2252 * Insert a new entry.
2254 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
2256 xfs_iext_insert(ifp, idx, 1, new);
2257 ifp->if_lastex = idx;
2258 XFS_IFORK_NEXT_SET(ip, whichfork,
2259 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2261 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2263 rval = XFS_ILOG_CORE;
2264 if ((error = xfs_bmbt_lookup_eq(cur,
2267 new->br_blockcount, &i)))
2270 cur->bc_rec.b.br_state = new->br_state;
2271 if ((error = xfs_bmbt_insert(cur, &i)))
2275 /* DELTA: A new extent was added in a hole. */
2276 temp = new->br_startoff;
2277 temp2 = new->br_blockcount;
2282 if (delta->xed_startoff > temp)
2283 delta->xed_startoff = temp;
2284 if (delta->xed_blockcount < temp2)
2285 delta->xed_blockcount = temp2;
2294 #undef STATE_SET_TEST
2299 * Adjust the size of the new extent based on di_extsize and rt extsize.
2302 xfs_bmap_extsize_align(
2304 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2305 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2306 xfs_extlen_t extsz, /* align to this extent size */
2307 int rt, /* is this a realtime inode? */
2308 int eof, /* is extent at end-of-file? */
2309 int delay, /* creating delalloc extent? */
2310 int convert, /* overwriting unwritten extent? */
2311 xfs_fileoff_t *offp, /* in/out: aligned offset */
2312 xfs_extlen_t *lenp) /* in/out: aligned length */
2314 xfs_fileoff_t orig_off; /* original offset */
2315 xfs_extlen_t orig_alen; /* original length */
2316 xfs_fileoff_t orig_end; /* original off+len */
2317 xfs_fileoff_t nexto; /* next file offset */
2318 xfs_fileoff_t prevo; /* previous file offset */
2319 xfs_fileoff_t align_off; /* temp for offset */
2320 xfs_extlen_t align_alen; /* temp for length */
2321 xfs_extlen_t temp; /* temp for calculations */
2326 orig_off = align_off = *offp;
2327 orig_alen = align_alen = *lenp;
2328 orig_end = orig_off + orig_alen;
2331 * If this request overlaps an existing extent, then don't
2332 * attempt to perform any additional alignment.
2334 if (!delay && !eof &&
2335 (orig_off >= gotp->br_startoff) &&
2336 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2341 * If the file offset is unaligned vs. the extent size
2342 * we need to align it. This will be possible unless
2343 * the file was previously written with a kernel that didn't
2344 * perform this alignment, or if a truncate shot us in the
2347 temp = do_mod(orig_off, extsz);
2353 * Same adjustment for the end of the requested area.
2355 if ((temp = (align_alen % extsz))) {
2356 align_alen += extsz - temp;
2359 * If the previous block overlaps with this proposed allocation
2360 * then move the start forward without adjusting the length.
2362 if (prevp->br_startoff != NULLFILEOFF) {
2363 if (prevp->br_startblock == HOLESTARTBLOCK)
2364 prevo = prevp->br_startoff;
2366 prevo = prevp->br_startoff + prevp->br_blockcount;
2369 if (align_off != orig_off && align_off < prevo)
2372 * If the next block overlaps with this proposed allocation
2373 * then move the start back without adjusting the length,
2374 * but not before offset 0.
2375 * This may of course make the start overlap previous block,
2376 * and if we hit the offset 0 limit then the next block
2377 * can still overlap too.
2379 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2380 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2381 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2382 nexto = gotp->br_startoff + gotp->br_blockcount;
2384 nexto = gotp->br_startoff;
2386 nexto = NULLFILEOFF;
2388 align_off + align_alen != orig_end &&
2389 align_off + align_alen > nexto)
2390 align_off = nexto > align_alen ? nexto - align_alen : 0;
2392 * If we're now overlapping the next or previous extent that
2393 * means we can't fit an extsz piece in this hole. Just move
2394 * the start forward to the first valid spot and set
2395 * the length so we hit the end.
2397 if (align_off != orig_off && align_off < prevo)
2399 if (align_off + align_alen != orig_end &&
2400 align_off + align_alen > nexto &&
2401 nexto != NULLFILEOFF) {
2402 ASSERT(nexto > prevo);
2403 align_alen = nexto - align_off;
2407 * If realtime, and the result isn't a multiple of the realtime
2408 * extent size we need to remove blocks until it is.
2410 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2412 * We're not covering the original request, or
2413 * we won't be able to once we fix the length.
2415 if (orig_off < align_off ||
2416 orig_end > align_off + align_alen ||
2417 align_alen - temp < orig_alen)
2418 return XFS_ERROR(EINVAL);
2420 * Try to fix it by moving the start up.
2422 if (align_off + temp <= orig_off) {
2427 * Try to fix it by moving the end in.
2429 else if (align_off + align_alen - temp >= orig_end)
2432 * Set the start to the minimum then trim the length.
2435 align_alen -= orig_off - align_off;
2436 align_off = orig_off;
2437 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2440 * Result doesn't cover the request, fail it.
2442 if (orig_off < align_off || orig_end > align_off + align_alen)
2443 return XFS_ERROR(EINVAL);
2445 ASSERT(orig_off >= align_off);
2446 ASSERT(orig_end <= align_off + align_alen);
2450 if (!eof && gotp->br_startoff != NULLFILEOFF)
2451 ASSERT(align_off + align_alen <= gotp->br_startoff);
2452 if (prevp->br_startoff != NULLFILEOFF)
2453 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2461 #define XFS_ALLOC_GAP_UNITS 4
2465 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2467 xfs_fsblock_t adjust; /* adjustment to block numbers */
2468 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2469 xfs_mount_t *mp; /* mount point structure */
2470 int nullfb; /* true if ap->firstblock isn't set */
2471 int rt; /* true if inode is realtime */
2473 #define ISVALID(x,y) \
2475 (x) < mp->m_sb.sb_rblocks : \
2476 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2477 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2478 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2480 mp = ap->ip->i_mount;
2481 nullfb = ap->firstblock == NULLFSBLOCK;
2482 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2483 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2485 * If allocating at eof, and there's a previous real block,
2486 * try to use it's last block as our starting point.
2488 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2489 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2490 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2491 ap->prevp->br_startblock)) {
2492 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2494 * Adjust for the gap between prevp and us.
2497 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2499 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2503 * If not at eof, then compare the two neighbor blocks.
2504 * Figure out whether either one gives us a good starting point,
2505 * and pick the better one.
2507 else if (!ap->eof) {
2508 xfs_fsblock_t gotbno; /* right side block number */
2509 xfs_fsblock_t gotdiff=0; /* right side difference */
2510 xfs_fsblock_t prevbno; /* left side block number */
2511 xfs_fsblock_t prevdiff=0; /* left side difference */
2514 * If there's a previous (left) block, select a requested
2515 * start block based on it.
2517 if (ap->prevp->br_startoff != NULLFILEOFF &&
2518 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2519 (prevbno = ap->prevp->br_startblock +
2520 ap->prevp->br_blockcount) &&
2521 ISVALID(prevbno, ap->prevp->br_startblock)) {
2523 * Calculate gap to end of previous block.
2525 adjust = prevdiff = ap->off -
2526 (ap->prevp->br_startoff +
2527 ap->prevp->br_blockcount);
2529 * Figure the startblock based on the previous block's
2530 * end and the gap size.
2532 * If the gap is large relative to the piece we're
2533 * allocating, or using it gives us an invalid block
2534 * number, then just use the end of the previous block.
2536 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2537 ISVALID(prevbno + prevdiff,
2538 ap->prevp->br_startblock))
2543 * If the firstblock forbids it, can't use it,
2546 if (!rt && !nullfb &&
2547 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2548 prevbno = NULLFSBLOCK;
2551 * No previous block or can't follow it, just default.
2554 prevbno = NULLFSBLOCK;
2556 * If there's a following (right) block, select a requested
2557 * start block based on it.
2559 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
2561 * Calculate gap to start of next block.
2563 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2565 * Figure the startblock based on the next block's
2566 * start and the gap size.
2568 gotbno = ap->gotp->br_startblock;
2571 * If the gap is large relative to the piece we're
2572 * allocating, or using it gives us an invalid block
2573 * number, then just use the start of the next block
2574 * offset by our length.
2576 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2577 ISVALID(gotbno - gotdiff, gotbno))
2579 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2581 gotdiff += adjust - ap->alen;
2585 * If the firstblock forbids it, can't use it,
2588 if (!rt && !nullfb &&
2589 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2590 gotbno = NULLFSBLOCK;
2593 * No next block, just default.
2596 gotbno = NULLFSBLOCK;
2598 * If both valid, pick the better one, else the only good
2599 * one, else ap->rval is already set (to 0 or the inode block).
2601 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2602 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2603 else if (prevbno != NULLFSBLOCK)
2605 else if (gotbno != NULLFSBLOCK)
2614 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2616 xfs_alloctype_t atype = 0; /* type for allocation routines */
2617 int error; /* error return value */
2618 xfs_mount_t *mp; /* mount point structure */
2619 xfs_extlen_t prod = 0; /* product factor for allocators */
2620 xfs_extlen_t ralen = 0; /* realtime allocation length */
2621 xfs_extlen_t align; /* minimum allocation alignment */
2622 xfs_rtblock_t rtx; /* realtime extent number */
2625 mp = ap->ip->i_mount;
2626 align = ap->ip->i_d.di_extsize ?
2627 ap->ip->i_d.di_extsize : mp->m_sb.sb_rextsize;
2628 prod = align / mp->m_sb.sb_rextsize;
2629 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2630 align, 1, ap->eof, 0,
2631 ap->conv, &ap->off, &ap->alen);
2635 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2638 * If the offset & length are not perfectly aligned
2639 * then kill prod, it will just get us in trouble.
2641 if (do_mod(ap->off, align) || ap->alen % align)
2644 * Set ralen to be the actual requested length in rtextents.
2646 ralen = ap->alen / mp->m_sb.sb_rextsize;
2648 * If the old value was close enough to MAXEXTLEN that
2649 * we rounded up to it, cut it back so it's valid again.
2650 * Note that if it's a really large request (bigger than
2651 * MAXEXTLEN), we don't hear about that number, and can't
2652 * adjust the starting point to match it.
2654 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2655 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2657 * If it's an allocation to an empty file at offset 0,
2658 * pick an extent that will space things out in the rt area.
2660 if (ap->eof && ap->off == 0) {
2661 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2664 ap->rval = rtx * mp->m_sb.sb_rextsize;
2669 xfs_bmap_adjacent(ap);
2672 * Realtime allocation, done through xfs_rtallocate_extent.
2674 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2675 do_div(ap->rval, mp->m_sb.sb_rextsize);
2678 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2679 &ralen, atype, ap->wasdel, prod, &rtb)))
2681 if (rtb == NULLFSBLOCK && prod > 1 &&
2682 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2683 ap->alen, &ralen, atype,
2684 ap->wasdel, 1, &rtb)))
2687 if (ap->rval != NULLFSBLOCK) {
2688 ap->rval *= mp->m_sb.sb_rextsize;
2689 ralen *= mp->m_sb.sb_rextsize;
2691 ap->ip->i_d.di_nblocks += ralen;
2692 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2694 ap->ip->i_delayed_blks -= ralen;
2696 * Adjust the disk quota also. This was reserved
2699 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2700 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2701 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2710 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2712 xfs_mount_t *mp; /* mount point structure */
2713 xfs_alloctype_t atype = 0; /* type for allocation routines */
2714 xfs_extlen_t align; /* minimum allocation alignment */
2716 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2717 xfs_agnumber_t startag;
2718 xfs_alloc_arg_t args;
2721 xfs_extlen_t longest;
2723 xfs_extlen_t nextminlen = 0;
2725 int nullfb; /* true if ap->firstblock isn't set */
2731 mp = ap->ip->i_mount;
2732 align = (ap->userdata && ap->ip->i_d.di_extsize &&
2733 (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)) ?
2734 ap->ip->i_d.di_extsize : 0;
2735 if (unlikely(align)) {
2736 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2737 align, 0, ap->eof, 0, ap->conv,
2738 &ap->off, &ap->alen);
2742 nullfb = ap->firstblock == NULLFSBLOCK;
2743 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2745 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2747 ap->rval = ap->firstblock;
2749 xfs_bmap_adjacent(ap);
2752 * If allowed, use ap->rval; otherwise must use firstblock since
2753 * it's in the right allocation group.
2755 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2758 ap->rval = ap->firstblock;
2760 * Normal allocation, done through xfs_alloc_vextent.
2762 tryagain = isaligned = 0;
2765 args.fsbno = ap->rval;
2766 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2767 args.firstblock = ap->firstblock;
2770 args.type = XFS_ALLOCTYPE_START_BNO;
2771 args.total = ap->total;
2773 * Find the longest available space.
2774 * We're going to try for the whole allocation at once.
2776 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2778 down_read(&mp->m_peraglock);
2779 while (blen < ap->alen) {
2780 pag = &mp->m_perag[ag];
2781 if (!pag->pagf_init &&
2782 (error = xfs_alloc_pagf_init(mp, args.tp,
2783 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2784 up_read(&mp->m_peraglock);
2788 * See xfs_alloc_fix_freelist...
2790 if (pag->pagf_init) {
2791 need = XFS_MIN_FREELIST_PAG(pag, mp);
2792 delta = need > pag->pagf_flcount ?
2793 need - pag->pagf_flcount : 0;
2794 longest = (pag->pagf_longest > delta) ?
2795 (pag->pagf_longest - delta) :
2796 (pag->pagf_flcount > 0 ||
2797 pag->pagf_longest > 0);
2802 if (++ag == mp->m_sb.sb_agcount)
2807 up_read(&mp->m_peraglock);
2809 * Since the above loop did a BUF_TRYLOCK, it is
2810 * possible that there is space for this request.
2812 if (notinit || blen < ap->minlen)
2813 args.minlen = ap->minlen;
2815 * If the best seen length is less than the request
2816 * length, use the best as the minimum.
2818 else if (blen < ap->alen)
2821 * Otherwise we've seen an extent as big as alen,
2822 * use that as the minimum.
2825 args.minlen = ap->alen;
2826 } else if (ap->low) {
2827 args.type = XFS_ALLOCTYPE_START_BNO;
2828 args.total = args.minlen = ap->minlen;
2830 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2831 args.total = ap->total;
2832 args.minlen = ap->minlen;
2834 if (unlikely(ap->userdata && ap->ip->i_d.di_extsize &&
2835 (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE))) {
2836 args.prod = ap->ip->i_d.di_extsize;
2837 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2838 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2839 } else if (mp->m_sb.sb_blocksize >= NBPP) {
2843 args.prod = NBPP >> mp->m_sb.sb_blocklog;
2844 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2845 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2848 * If we are not low on available data blocks, and the
2849 * underlying logical volume manager is a stripe, and
2850 * the file offset is zero then try to allocate data
2851 * blocks on stripe unit boundary.
2852 * NOTE: ap->aeof is only set if the allocation length
2853 * is >= the stripe unit and the allocation offset is
2854 * at the end of file.
2856 if (!ap->low && ap->aeof) {
2858 args.alignment = mp->m_dalign;
2862 * Adjust for alignment
2864 if (blen > args.alignment && blen <= ap->alen)
2865 args.minlen = blen - args.alignment;
2866 args.minalignslop = 0;
2869 * First try an exact bno allocation.
2870 * If it fails then do a near or start bno
2871 * allocation with alignment turned on.
2875 args.type = XFS_ALLOCTYPE_THIS_BNO;
2878 * Compute the minlen+alignment for the
2879 * next case. Set slop so that the value
2880 * of minlen+alignment+slop doesn't go up
2881 * between the calls.
2883 if (blen > mp->m_dalign && blen <= ap->alen)
2884 nextminlen = blen - mp->m_dalign;
2886 nextminlen = args.minlen;
2887 if (nextminlen + mp->m_dalign > args.minlen + 1)
2889 nextminlen + mp->m_dalign -
2892 args.minalignslop = 0;
2896 args.minalignslop = 0;
2898 args.minleft = ap->minleft;
2899 args.wasdel = ap->wasdel;
2901 args.userdata = ap->userdata;
2902 if ((error = xfs_alloc_vextent(&args)))
2904 if (tryagain && args.fsbno == NULLFSBLOCK) {
2906 * Exact allocation failed. Now try with alignment
2910 args.fsbno = ap->rval;
2911 args.alignment = mp->m_dalign;
2912 args.minlen = nextminlen;
2913 args.minalignslop = 0;
2915 if ((error = xfs_alloc_vextent(&args)))
2918 if (isaligned && args.fsbno == NULLFSBLOCK) {
2920 * allocation failed, so turn off alignment and
2924 args.fsbno = ap->rval;
2926 if ((error = xfs_alloc_vextent(&args)))
2929 if (args.fsbno == NULLFSBLOCK && nullfb &&
2930 args.minlen > ap->minlen) {
2931 args.minlen = ap->minlen;
2932 args.type = XFS_ALLOCTYPE_START_BNO;
2933 args.fsbno = ap->rval;
2934 if ((error = xfs_alloc_vextent(&args)))
2937 if (args.fsbno == NULLFSBLOCK && nullfb) {
2939 args.type = XFS_ALLOCTYPE_FIRST_AG;
2940 args.total = ap->minlen;
2942 if ((error = xfs_alloc_vextent(&args)))
2946 if (args.fsbno != NULLFSBLOCK) {
2947 ap->firstblock = ap->rval = args.fsbno;
2948 ASSERT(nullfb || fb_agno == args.agno ||
2949 (ap->low && fb_agno < args.agno));
2950 ap->alen = args.len;
2951 ap->ip->i_d.di_nblocks += args.len;
2952 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2954 ap->ip->i_delayed_blks -= args.len;
2956 * Adjust the disk quota also. This was reserved
2959 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2960 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2961 XFS_TRANS_DQ_BCOUNT,
2964 ap->rval = NULLFSBLOCK;
2971 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2972 * It figures out where to ask the underlying allocator to put the new extent.
2976 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2978 if ((ap->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) && ap->userdata)
2979 return xfs_bmap_rtalloc(ap);
2980 return xfs_bmap_btalloc(ap);
2984 * Transform a btree format file with only one leaf node, where the
2985 * extents list will fit in the inode, into an extents format file.
2986 * Since the file extents are already in-core, all we have to do is
2987 * give up the space for the btree root and pitch the leaf block.
2989 STATIC int /* error */
2990 xfs_bmap_btree_to_extents(
2991 xfs_trans_t *tp, /* transaction pointer */
2992 xfs_inode_t *ip, /* incore inode pointer */
2993 xfs_btree_cur_t *cur, /* btree cursor */
2994 int *logflagsp, /* inode logging flags */
2995 int whichfork) /* data or attr fork */
2998 xfs_bmbt_block_t *cblock;/* child btree block */
2999 xfs_fsblock_t cbno; /* child block number */
3000 xfs_buf_t *cbp; /* child block's buffer */
3001 int error; /* error return value */
3002 xfs_ifork_t *ifp; /* inode fork data */
3003 xfs_mount_t *mp; /* mount point structure */
3004 __be64 *pp; /* ptr to block address */
3005 xfs_bmbt_block_t *rblock;/* root btree block */
3007 ifp = XFS_IFORK_PTR(ip, whichfork);
3008 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3009 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
3010 rblock = ifp->if_broot;
3011 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
3012 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
3013 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
3015 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
3016 cbno = be64_to_cpu(*pp);
3019 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
3022 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
3023 XFS_BMAP_BTREE_REF)))
3025 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
3026 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
3028 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
3029 ip->i_d.di_nblocks--;
3030 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
3031 xfs_trans_binval(tp, cbp);
3032 if (cur->bc_bufs[0] == cbp)
3033 cur->bc_bufs[0] = NULL;
3034 xfs_iroot_realloc(ip, -1, whichfork);
3035 ASSERT(ifp->if_broot == NULL);
3036 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
3037 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3038 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
3043 * Called by xfs_bmapi to update file extent records and the btree
3044 * after removing space (or undoing a delayed allocation).
3046 STATIC int /* error */
3047 xfs_bmap_del_extent(
3048 xfs_inode_t *ip, /* incore inode pointer */
3049 xfs_trans_t *tp, /* current transaction pointer */
3050 xfs_extnum_t idx, /* extent number to update/delete */
3051 xfs_bmap_free_t *flist, /* list of extents to be freed */
3052 xfs_btree_cur_t *cur, /* if null, not a btree */
3053 xfs_bmbt_irec_t *del, /* data to remove from extents */
3054 int *logflagsp, /* inode logging flags */
3055 xfs_extdelta_t *delta, /* Change made to incore extents */
3056 int whichfork, /* data or attr fork */
3057 int rsvd) /* OK to allocate reserved blocks */
3059 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
3060 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
3061 xfs_fsblock_t del_endblock=0; /* first block past del */
3062 xfs_fileoff_t del_endoff; /* first offset past del */
3063 int delay; /* current block is delayed allocated */
3064 int do_fx; /* free extent at end of routine */
3065 xfs_bmbt_rec_t *ep; /* current extent entry pointer */
3066 int error; /* error return value */
3067 int flags; /* inode logging flags */
3068 #ifdef XFS_BMAP_TRACE
3069 static char fname[] = "xfs_bmap_del_extent";
3071 xfs_bmbt_irec_t got; /* current extent entry */
3072 xfs_fileoff_t got_endoff; /* first offset past got */
3073 int i; /* temp state */
3074 xfs_ifork_t *ifp; /* inode fork pointer */
3075 xfs_mount_t *mp; /* mount structure */
3076 xfs_filblks_t nblks; /* quota/sb block count */
3077 xfs_bmbt_irec_t new; /* new record to be inserted */
3079 uint qfield; /* quota field to update */
3080 xfs_filblks_t temp; /* for indirect length calculations */
3081 xfs_filblks_t temp2; /* for indirect length calculations */
3083 XFS_STATS_INC(xs_del_exlist);
3085 ifp = XFS_IFORK_PTR(ip, whichfork);
3086 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
3087 (uint)sizeof(xfs_bmbt_rec_t)));
3088 ASSERT(del->br_blockcount > 0);
3089 ep = xfs_iext_get_ext(ifp, idx);
3090 xfs_bmbt_get_all(ep, &got);
3091 ASSERT(got.br_startoff <= del->br_startoff);
3092 del_endoff = del->br_startoff + del->br_blockcount;
3093 got_endoff = got.br_startoff + got.br_blockcount;
3094 ASSERT(got_endoff >= del_endoff);
3095 delay = ISNULLSTARTBLOCK(got.br_startblock);
3096 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
3101 * If deleting a real allocation, must free up the disk space.
3104 flags = XFS_ILOG_CORE;
3106 * Realtime allocation. Free it and record di_nblocks update.
3108 if (whichfork == XFS_DATA_FORK &&
3109 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
3113 ASSERT(do_mod(del->br_blockcount,
3114 mp->m_sb.sb_rextsize) == 0);
3115 ASSERT(do_mod(del->br_startblock,
3116 mp->m_sb.sb_rextsize) == 0);
3117 bno = del->br_startblock;
3118 len = del->br_blockcount;
3119 do_div(bno, mp->m_sb.sb_rextsize);
3120 do_div(len, mp->m_sb.sb_rextsize);
3121 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3122 (xfs_extlen_t)len)))
3125 nblks = len * mp->m_sb.sb_rextsize;
3126 qfield = XFS_TRANS_DQ_RTBCOUNT;
3129 * Ordinary allocation.
3133 nblks = del->br_blockcount;
3134 qfield = XFS_TRANS_DQ_BCOUNT;
3137 * Set up del_endblock and cur for later.
3139 del_endblock = del->br_startblock + del->br_blockcount;
3141 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3142 got.br_startblock, got.br_blockcount,
3147 da_old = da_new = 0;
3149 da_old = STARTBLOCKVAL(got.br_startblock);
3155 * Set flag value to use in switch statement.
3156 * Left-contig is 2, right-contig is 1.
3158 switch (((got.br_startoff == del->br_startoff) << 1) |
3159 (got_endoff == del_endoff)) {
3162 * Matches the whole extent. Delete the entry.
3164 xfs_bmap_trace_delete(fname, "3", ip, idx, 1, whichfork);
3165 xfs_iext_remove(ifp, idx, 1);
3166 ifp->if_lastex = idx;
3169 XFS_IFORK_NEXT_SET(ip, whichfork,
3170 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3171 flags |= XFS_ILOG_CORE;
3173 flags |= XFS_ILOG_FEXT(whichfork);
3176 if ((error = xfs_bmbt_delete(cur, &i)))
3183 * Deleting the first part of the extent.
3185 xfs_bmap_trace_pre_update(fname, "2", ip, idx, whichfork);
3186 xfs_bmbt_set_startoff(ep, del_endoff);
3187 temp = got.br_blockcount - del->br_blockcount;
3188 xfs_bmbt_set_blockcount(ep, temp);
3189 ifp->if_lastex = idx;
3191 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3193 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3194 xfs_bmap_trace_post_update(fname, "2", ip, idx,
3199 xfs_bmbt_set_startblock(ep, del_endblock);
3200 xfs_bmap_trace_post_update(fname, "2", ip, idx, whichfork);
3202 flags |= XFS_ILOG_FEXT(whichfork);
3205 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3206 got.br_blockcount - del->br_blockcount,
3213 * Deleting the last part of the extent.
3215 temp = got.br_blockcount - del->br_blockcount;
3216 xfs_bmap_trace_pre_update(fname, "1", ip, idx, whichfork);
3217 xfs_bmbt_set_blockcount(ep, temp);
3218 ifp->if_lastex = idx;
3220 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3222 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3223 xfs_bmap_trace_post_update(fname, "1", ip, idx,
3228 xfs_bmap_trace_post_update(fname, "1", ip, idx, whichfork);
3230 flags |= XFS_ILOG_FEXT(whichfork);
3233 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3235 got.br_blockcount - del->br_blockcount,
3242 * Deleting the middle of the extent.
3244 temp = del->br_startoff - got.br_startoff;
3245 xfs_bmap_trace_pre_update(fname, "0", ip, idx, whichfork);
3246 xfs_bmbt_set_blockcount(ep, temp);
3247 new.br_startoff = del_endoff;
3248 temp2 = got_endoff - del_endoff;
3249 new.br_blockcount = temp2;
3250 new.br_state = got.br_state;
3252 new.br_startblock = del_endblock;
3253 flags |= XFS_ILOG_CORE;
3255 if ((error = xfs_bmbt_update(cur,
3257 got.br_startblock, temp,
3260 if ((error = xfs_bmbt_increment(cur, 0, &i)))
3262 cur->bc_rec.b = new;
3263 error = xfs_bmbt_insert(cur, &i);
3264 if (error && error != ENOSPC)
3267 * If get no-space back from btree insert,
3268 * it tried a split, and we have a zero
3269 * block reservation.
3270 * Fix up our state and return the error.
3272 if (error == ENOSPC) {
3274 * Reset the cursor, don't trust
3275 * it after any insert operation.
3277 if ((error = xfs_bmbt_lookup_eq(cur,
3284 * Update the btree record back
3285 * to the original value.
3287 if ((error = xfs_bmbt_update(cur,
3294 * Reset the extent record back
3295 * to the original value.
3297 xfs_bmbt_set_blockcount(ep,
3300 error = XFS_ERROR(ENOSPC);
3305 flags |= XFS_ILOG_FEXT(whichfork);
3306 XFS_IFORK_NEXT_SET(ip, whichfork,
3307 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3309 ASSERT(whichfork == XFS_DATA_FORK);
3310 temp = xfs_bmap_worst_indlen(ip, temp);
3311 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3312 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3313 new.br_startblock = NULLSTARTBLOCK((int)temp2);
3314 da_new = temp + temp2;
3315 while (da_new > da_old) {
3319 xfs_bmbt_set_startblock(ep,
3320 NULLSTARTBLOCK((int)temp));
3322 if (da_new == da_old)
3328 NULLSTARTBLOCK((int)temp2);
3332 xfs_bmap_trace_post_update(fname, "0", ip, idx, whichfork);
3333 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 1, &new, NULL,
3335 xfs_iext_insert(ifp, idx + 1, 1, &new);
3336 ifp->if_lastex = idx + 1;
3340 * If we need to, add to list of extents to delete.
3343 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3346 * Adjust inode # blocks in the file.
3349 ip->i_d.di_nblocks -= nblks;
3351 * Adjust quota data.
3354 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
3357 * Account for change in delayed indirect blocks.
3358 * Nothing to do for disk quota accounting here.
3360 ASSERT(da_old >= da_new);
3361 if (da_old > da_new)
3362 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int)(da_old - da_new),
3365 /* DELTA: report the original extent. */
3366 if (delta->xed_startoff > got.br_startoff)
3367 delta->xed_startoff = got.br_startoff;
3368 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3369 delta->xed_blockcount = got.br_startoff +
3378 * Remove the entry "free" from the free item list. Prev points to the
3379 * previous entry, unless "free" is the head of the list.
3383 xfs_bmap_free_t *flist, /* free item list header */
3384 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3385 xfs_bmap_free_item_t *free) /* list item to be freed */
3388 prev->xbfi_next = free->xbfi_next;
3390 flist->xbf_first = free->xbfi_next;
3392 kmem_zone_free(xfs_bmap_free_item_zone, free);
3396 * Convert an extents-format file into a btree-format file.
3397 * The new file will have a root block (in the inode) and a single child block.
3399 STATIC int /* error */
3400 xfs_bmap_extents_to_btree(
3401 xfs_trans_t *tp, /* transaction pointer */
3402 xfs_inode_t *ip, /* incore inode pointer */
3403 xfs_fsblock_t *firstblock, /* first-block-allocated */
3404 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3405 xfs_btree_cur_t **curp, /* cursor returned to caller */
3406 int wasdel, /* converting a delayed alloc */
3407 int *logflagsp, /* inode logging flags */
3408 int whichfork) /* data or attr fork */
3410 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
3411 xfs_buf_t *abp; /* buffer for ablock */
3412 xfs_alloc_arg_t args; /* allocation arguments */
3413 xfs_bmbt_rec_t *arp; /* child record pointer */
3414 xfs_bmbt_block_t *block; /* btree root block */
3415 xfs_btree_cur_t *cur; /* bmap btree cursor */
3416 xfs_bmbt_rec_t *ep; /* extent record pointer */
3417 int error; /* error return value */
3418 xfs_extnum_t i, cnt; /* extent record index */
3419 xfs_ifork_t *ifp; /* inode fork pointer */
3420 xfs_bmbt_key_t *kp; /* root block key pointer */
3421 xfs_mount_t *mp; /* mount structure */
3422 xfs_extnum_t nextents; /* number of file extents */
3423 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3425 ifp = XFS_IFORK_PTR(ip, whichfork);
3426 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3427 ASSERT(ifp->if_ext_max ==
3428 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3430 * Make space in the inode incore.
3432 xfs_iroot_realloc(ip, 1, whichfork);
3433 ifp->if_flags |= XFS_IFBROOT;
3437 block = ifp->if_broot;
3438 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3439 block->bb_level = cpu_to_be16(1);
3440 block->bb_numrecs = cpu_to_be16(1);
3441 block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3442 block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3444 * Need a cursor. Can't allocate until bb_level is filled in.
3447 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
3449 cur->bc_private.b.firstblock = *firstblock;
3450 cur->bc_private.b.flist = flist;
3451 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3453 * Convert to a btree with two levels, one record in root.
3455 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3458 args.firstblock = *firstblock;
3459 if (*firstblock == NULLFSBLOCK) {
3460 args.type = XFS_ALLOCTYPE_START_BNO;
3461 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3462 } else if (flist->xbf_low) {
3463 args.type = XFS_ALLOCTYPE_START_BNO;
3464 args.fsbno = *firstblock;
3466 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3467 args.fsbno = *firstblock;
3469 args.minlen = args.maxlen = args.prod = 1;
3470 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3471 args.minalignslop = 0;
3472 args.wasdel = wasdel;
3474 if ((error = xfs_alloc_vextent(&args))) {
3475 xfs_iroot_realloc(ip, -1, whichfork);
3476 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3480 * Allocation can't fail, the space was reserved.
3482 ASSERT(args.fsbno != NULLFSBLOCK);
3483 ASSERT(*firstblock == NULLFSBLOCK ||
3484 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3486 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3487 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3488 cur->bc_private.b.allocated++;
3489 ip->i_d.di_nblocks++;
3490 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3491 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3493 * Fill in the child block.
3495 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
3496 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3497 ablock->bb_level = 0;
3498 ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3499 ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3500 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3501 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3502 for (cnt = i = 0; i < nextents; i++) {
3503 ep = xfs_iext_get_ext(ifp, i);
3504 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
3505 arp->l0 = INT_GET(ep->l0, ARCH_CONVERT);
3506 arp->l1 = INT_GET(ep->l1, ARCH_CONVERT);
3510 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3511 ablock->bb_numrecs = cpu_to_be16(cnt);
3513 * Fill in the root key and pointer.
3515 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
3516 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3517 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3518 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
3519 *pp = cpu_to_be64(args.fsbno);
3521 * Do all this logging at the end so that
3522 * the root is at the right level.
3524 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
3525 xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3526 ASSERT(*curp == NULL);
3528 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
3533 * Helper routine to reset inode di_forkoff field when switching
3534 * attribute fork from local to extent format - we reset it where
3535 * possible to make space available for inline data fork extents.
3538 xfs_bmap_forkoff_reset(
3543 if (whichfork == XFS_ATTR_FORK &&
3544 (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
3545 (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
3546 ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
3547 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3548 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
3549 (uint)sizeof(xfs_bmbt_rec_t);
3550 ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
3551 (uint)sizeof(xfs_bmbt_rec_t);
3556 * Convert a local file to an extents file.
3557 * This code is out of bounds for data forks of regular files,
3558 * since the file data needs to get logged so things will stay consistent.
3559 * (The bmap-level manipulations are ok, though).
3561 STATIC int /* error */
3562 xfs_bmap_local_to_extents(
3563 xfs_trans_t *tp, /* transaction pointer */
3564 xfs_inode_t *ip, /* incore inode pointer */
3565 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3566 xfs_extlen_t total, /* total blocks needed by transaction */
3567 int *logflagsp, /* inode logging flags */
3568 int whichfork) /* data or attr fork */
3570 int error; /* error return value */
3571 int flags; /* logging flags returned */
3572 #ifdef XFS_BMAP_TRACE
3573 static char fname[] = "xfs_bmap_local_to_extents";
3575 xfs_ifork_t *ifp; /* inode fork pointer */
3578 * We don't want to deal with the case of keeping inode data inline yet.
3579 * So sending the data fork of a regular inode is invalid.
3581 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3582 whichfork == XFS_DATA_FORK));
3583 ifp = XFS_IFORK_PTR(ip, whichfork);
3584 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3587 if (ifp->if_bytes) {
3588 xfs_alloc_arg_t args; /* allocation arguments */
3589 xfs_buf_t *bp; /* buffer for extent block */
3590 xfs_bmbt_rec_t *ep; /* extent record pointer */
3593 args.mp = ip->i_mount;
3594 args.firstblock = *firstblock;
3595 ASSERT((ifp->if_flags &
3596 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3598 * Allocate a block. We know we need only one, since the
3599 * file currently fits in an inode.
3601 if (*firstblock == NULLFSBLOCK) {
3602 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3603 args.type = XFS_ALLOCTYPE_START_BNO;
3605 args.fsbno = *firstblock;
3606 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3609 args.mod = args.minleft = args.alignment = args.wasdel =
3610 args.isfl = args.minalignslop = 0;
3611 args.minlen = args.maxlen = args.prod = 1;
3612 if ((error = xfs_alloc_vextent(&args)))
3615 * Can't fail, the space was reserved.
3617 ASSERT(args.fsbno != NULLFSBLOCK);
3618 ASSERT(args.len == 1);
3619 *firstblock = args.fsbno;
3620 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3621 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3623 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3624 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3625 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3626 xfs_iext_add(ifp, 0, 1);
3627 ep = xfs_iext_get_ext(ifp, 0);
3628 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3629 xfs_bmap_trace_post_update(fname, "new", ip, 0, whichfork);
3630 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3631 ip->i_d.di_nblocks = 1;
3632 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
3633 XFS_TRANS_DQ_BCOUNT, 1L);
3634 flags |= XFS_ILOG_FEXT(whichfork);
3636 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3637 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3639 ifp->if_flags &= ~XFS_IFINLINE;
3640 ifp->if_flags |= XFS_IFEXTENTS;
3641 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3642 flags |= XFS_ILOG_CORE;
3649 * Search the extent records for the entry containing block bno.
3650 * If bno lies in a hole, point to the next entry. If bno lies
3651 * past eof, *eofp will be set, and *prevp will contain the last
3652 * entry (null if none). Else, *lastxp will be set to the index
3653 * of the found entry; *gotp will contain the entry.
3655 xfs_bmbt_rec_t * /* pointer to found extent entry */
3656 xfs_bmap_search_multi_extents(
3657 xfs_ifork_t *ifp, /* inode fork pointer */
3658 xfs_fileoff_t bno, /* block number searched for */
3659 int *eofp, /* out: end of file found */
3660 xfs_extnum_t *lastxp, /* out: last extent index */
3661 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3662 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3664 xfs_bmbt_rec_t *ep; /* extent record pointer */
3665 xfs_extnum_t lastx; /* last extent index */
3668 * Initialize the extent entry structure to catch access to
3669 * uninitialized br_startblock field.
3671 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3672 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3673 gotp->br_state = XFS_EXT_INVALID;
3675 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3677 gotp->br_startblock = 0xffffa5a5;
3679 prevp->br_startoff = NULLFILEOFF;
3681 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3683 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3685 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3686 xfs_bmbt_get_all(ep, gotp);
3700 * Search the extents list for the inode, for the extent containing bno.
3701 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3702 * *eofp will be set, and *prevp will contain the last entry (null if none).
3703 * Else, *lastxp will be set to the index of the found
3704 * entry; *gotp will contain the entry.
3706 STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
3707 xfs_bmap_search_extents(
3708 xfs_inode_t *ip, /* incore inode pointer */
3709 xfs_fileoff_t bno, /* block number searched for */
3710 int fork, /* data or attr fork */
3711 int *eofp, /* out: end of file found */
3712 xfs_extnum_t *lastxp, /* out: last extent index */
3713 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3714 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3716 xfs_ifork_t *ifp; /* inode fork pointer */
3717 xfs_bmbt_rec_t *ep; /* extent record pointer */
3719 XFS_STATS_INC(xs_look_exlist);
3720 ifp = XFS_IFORK_PTR(ip, fork);
3722 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3724 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3725 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3726 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3727 "Access to block zero in inode %llu "
3728 "start_block: %llx start_off: %llx "
3729 "blkcnt: %llx extent-state: %x lastx: %x\n",
3730 (unsigned long long)ip->i_ino,
3731 (unsigned long long)gotp->br_startblock,
3732 (unsigned long long)gotp->br_startoff,
3733 (unsigned long long)gotp->br_blockcount,
3734 gotp->br_state, *lastxp);
3735 *lastxp = NULLEXTNUM;
3743 #ifdef XFS_BMAP_TRACE
3744 ktrace_t *xfs_bmap_trace_buf;
3747 * Add a bmap trace buffer entry. Base routine for the others.
3750 xfs_bmap_trace_addentry(
3751 int opcode, /* operation */
3752 char *fname, /* function name */
3753 char *desc, /* operation description */
3754 xfs_inode_t *ip, /* incore inode pointer */
3755 xfs_extnum_t idx, /* index of entry(ies) */
3756 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
3757 xfs_bmbt_rec_t *r1, /* first record */
3758 xfs_bmbt_rec_t *r2, /* second record or null */
3759 int whichfork) /* data or attr fork */
3763 ASSERT(cnt == 1 || cnt == 2);
3768 memset(&tr2, 0, sizeof(tr2));
3771 ktrace_enter(xfs_bmap_trace_buf,
3772 (void *)(__psint_t)(opcode | (whichfork << 16)),
3773 (void *)fname, (void *)desc, (void *)ip,
3774 (void *)(__psint_t)idx,
3775 (void *)(__psint_t)cnt,
3776 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3777 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3778 (void *)(__psunsigned_t)(r1->l0 >> 32),
3779 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3780 (void *)(__psunsigned_t)(r1->l1 >> 32),
3781 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3782 (void *)(__psunsigned_t)(r2->l0 >> 32),
3783 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3784 (void *)(__psunsigned_t)(r2->l1 >> 32),
3785 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3787 ASSERT(ip->i_xtrace);
3788 ktrace_enter(ip->i_xtrace,
3789 (void *)(__psint_t)(opcode | (whichfork << 16)),
3790 (void *)fname, (void *)desc, (void *)ip,
3791 (void *)(__psint_t)idx,
3792 (void *)(__psint_t)cnt,
3793 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3794 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3795 (void *)(__psunsigned_t)(r1->l0 >> 32),
3796 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3797 (void *)(__psunsigned_t)(r1->l1 >> 32),
3798 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3799 (void *)(__psunsigned_t)(r2->l0 >> 32),
3800 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3801 (void *)(__psunsigned_t)(r2->l1 >> 32),
3802 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3807 * Add bmap trace entry prior to a call to xfs_iext_remove.
3810 xfs_bmap_trace_delete(
3811 char *fname, /* function name */
3812 char *desc, /* operation description */
3813 xfs_inode_t *ip, /* incore inode pointer */
3814 xfs_extnum_t idx, /* index of entry(entries) deleted */
3815 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3816 int whichfork) /* data or attr fork */
3818 xfs_ifork_t *ifp; /* inode fork pointer */
3820 ifp = XFS_IFORK_PTR(ip, whichfork);
3821 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
3822 cnt, xfs_iext_get_ext(ifp, idx),
3823 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
3828 * Add bmap trace entry prior to a call to xfs_iext_insert, or
3829 * reading in the extents list from the disk (in the btree).
3832 xfs_bmap_trace_insert(
3833 char *fname, /* function name */
3834 char *desc, /* operation description */
3835 xfs_inode_t *ip, /* incore inode pointer */
3836 xfs_extnum_t idx, /* index of entry(entries) inserted */
3837 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3838 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3839 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3840 int whichfork) /* data or attr fork */
3842 xfs_bmbt_rec_t tr1; /* compressed record 1 */
3843 xfs_bmbt_rec_t tr2; /* compressed record 2 if needed */
3845 xfs_bmbt_set_all(&tr1, r1);
3848 xfs_bmbt_set_all(&tr2, r2);
3853 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3854 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3858 * Add bmap trace entry after updating an extent record in place.
3861 xfs_bmap_trace_post_update(
3862 char *fname, /* function name */
3863 char *desc, /* operation description */
3864 xfs_inode_t *ip, /* incore inode pointer */
3865 xfs_extnum_t idx, /* index of entry updated */
3866 int whichfork) /* data or attr fork */
3868 xfs_ifork_t *ifp; /* inode fork pointer */
3870 ifp = XFS_IFORK_PTR(ip, whichfork);
3871 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
3872 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3876 * Add bmap trace entry prior to updating an extent record in place.
3879 xfs_bmap_trace_pre_update(
3880 char *fname, /* function name */
3881 char *desc, /* operation description */
3882 xfs_inode_t *ip, /* incore inode pointer */
3883 xfs_extnum_t idx, /* index of entry to be updated */
3884 int whichfork) /* data or attr fork */
3886 xfs_ifork_t *ifp; /* inode fork pointer */
3888 ifp = XFS_IFORK_PTR(ip, whichfork);
3889 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
3890 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3892 #endif /* XFS_BMAP_TRACE */
3895 * Compute the worst-case number of indirect blocks that will be used
3896 * for ip's delayed extent of length "len".
3898 STATIC xfs_filblks_t
3899 xfs_bmap_worst_indlen(
3900 xfs_inode_t *ip, /* incore inode pointer */
3901 xfs_filblks_t len) /* delayed extent length */
3903 int level; /* btree level number */
3904 int maxrecs; /* maximum record count at this level */
3905 xfs_mount_t *mp; /* mount structure */
3906 xfs_filblks_t rval; /* return value */
3909 maxrecs = mp->m_bmap_dmxr[0];
3910 for (level = 0, rval = 0;
3911 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3914 do_div(len, maxrecs);
3917 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3920 maxrecs = mp->m_bmap_dmxr[1];
3925 #if defined(XFS_RW_TRACE)
3934 if (ip->i_rwtrace == NULL)
3936 ktrace_enter(ip->i_rwtrace,
3937 (void *)(__psint_t)XFS_BUNMAP,
3939 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3940 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3941 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3942 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3943 (void *)(__psint_t)len,
3944 (void *)(__psint_t)flags,
3945 (void *)(unsigned long)current_cpu(),
3957 * Convert inode from non-attributed to attributed.
3958 * Must not be in a transaction, ip must not be locked.
3960 int /* error code */
3961 xfs_bmap_add_attrfork(
3962 xfs_inode_t *ip, /* incore inode pointer */
3963 int size, /* space new attribute needs */
3964 int rsvd) /* xact may use reserved blks */
3966 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3967 xfs_bmap_free_t flist; /* freed extent records */
3968 xfs_mount_t *mp; /* mount structure */
3969 xfs_trans_t *tp; /* transaction pointer */
3970 unsigned long s; /* spinlock spl value */
3971 int blks; /* space reservation */
3972 int version = 1; /* superblock attr version */
3973 int committed; /* xaction was committed */
3974 int logflags; /* logging flags */
3975 int error; /* error return value */
3977 ASSERT(XFS_IFORK_Q(ip) == 0);
3978 ASSERT(ip->i_df.if_ext_max ==
3979 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3982 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3983 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3984 blks = XFS_ADDAFORK_SPACE_RES(mp);
3986 tp->t_flags |= XFS_TRANS_RESERVE;
3987 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3988 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3990 xfs_ilock(ip, XFS_ILOCK_EXCL);
3991 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
3992 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3993 XFS_QMOPT_RES_REGBLKS);
3995 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3996 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3999 if (XFS_IFORK_Q(ip))
4001 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
4003 * For inodes coming from pre-6.2 filesystems.
4005 ASSERT(ip->i_d.di_aformat == 0);
4006 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
4008 ASSERT(ip->i_d.di_anextents == 0);
4009 VN_HOLD(XFS_ITOV(ip));
4010 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4011 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4012 switch (ip->i_d.di_format) {
4013 case XFS_DINODE_FMT_DEV:
4014 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
4016 case XFS_DINODE_FMT_UUID:
4017 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
4019 case XFS_DINODE_FMT_LOCAL:
4020 case XFS_DINODE_FMT_EXTENTS:
4021 case XFS_DINODE_FMT_BTREE:
4022 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
4023 if (!ip->i_d.di_forkoff)
4024 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
4025 else if (mp->m_flags & XFS_MOUNT_ATTR2)
4030 error = XFS_ERROR(EINVAL);
4033 ip->i_df.if_ext_max =
4034 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4035 ASSERT(ip->i_afp == NULL);
4036 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
4037 ip->i_afp->if_ext_max =
4038 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4039 ip->i_afp->if_flags = XFS_IFEXTENTS;
4041 XFS_BMAP_INIT(&flist, &firstblock);
4042 switch (ip->i_d.di_format) {
4043 case XFS_DINODE_FMT_LOCAL:
4044 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
4047 case XFS_DINODE_FMT_EXTENTS:
4048 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
4051 case XFS_DINODE_FMT_BTREE:
4052 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
4060 xfs_trans_log_inode(tp, ip, logflags);
4063 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb) ||
4064 (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2)) {
4065 __int64_t sbfields = 0;
4067 s = XFS_SB_LOCK(mp);
4068 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) {
4069 XFS_SB_VERSION_ADDATTR(&mp->m_sb);
4070 sbfields |= XFS_SB_VERSIONNUM;
4072 if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2) {
4073 XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
4074 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
4077 XFS_SB_UNLOCK(mp, s);
4078 xfs_mod_sb(tp, sbfields);
4080 XFS_SB_UNLOCK(mp, s);
4082 if ((error = xfs_bmap_finish(&tp, &flist, firstblock, &committed)))
4084 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES, NULL);
4085 ASSERT(ip->i_df.if_ext_max ==
4086 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4089 xfs_bmap_cancel(&flist);
4091 ASSERT(ismrlocked(&ip->i_lock,MR_UPDATE));
4092 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4094 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4095 ASSERT(ip->i_df.if_ext_max ==
4096 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4101 * Add the extent to the list of extents to be free at transaction end.
4102 * The list is maintained sorted (by block number).
4107 xfs_fsblock_t bno, /* fs block number of extent */
4108 xfs_filblks_t len, /* length of extent */
4109 xfs_bmap_free_t *flist, /* list of extents */
4110 xfs_mount_t *mp) /* mount point structure */
4112 xfs_bmap_free_item_t *cur; /* current (next) element */
4113 xfs_bmap_free_item_t *new; /* new element */
4114 xfs_bmap_free_item_t *prev; /* previous element */
4116 xfs_agnumber_t agno;
4117 xfs_agblock_t agbno;
4119 ASSERT(bno != NULLFSBLOCK);
4121 ASSERT(len <= MAXEXTLEN);
4122 ASSERT(!ISNULLSTARTBLOCK(bno));
4123 agno = XFS_FSB_TO_AGNO(mp, bno);
4124 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4125 ASSERT(agno < mp->m_sb.sb_agcount);
4126 ASSERT(agbno < mp->m_sb.sb_agblocks);
4127 ASSERT(len < mp->m_sb.sb_agblocks);
4128 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4130 ASSERT(xfs_bmap_free_item_zone != NULL);
4131 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4132 new->xbfi_startblock = bno;
4133 new->xbfi_blockcount = (xfs_extlen_t)len;
4134 for (prev = NULL, cur = flist->xbf_first;
4136 prev = cur, cur = cur->xbfi_next) {
4137 if (cur->xbfi_startblock >= bno)
4141 prev->xbfi_next = new;
4143 flist->xbf_first = new;
4144 new->xbfi_next = cur;
4149 * Compute and fill in the value of the maximum depth of a bmap btree
4150 * in this filesystem. Done once, during mount.
4153 xfs_bmap_compute_maxlevels(
4154 xfs_mount_t *mp, /* file system mount structure */
4155 int whichfork) /* data or attr fork */
4157 int level; /* btree level */
4158 uint maxblocks; /* max blocks at this level */
4159 uint maxleafents; /* max leaf entries possible */
4160 int maxrootrecs; /* max records in root block */
4161 int minleafrecs; /* min records in leaf block */
4162 int minnoderecs; /* min records in node block */
4163 int sz; /* root block size */
4166 * The maximum number of extents in a file, hence the maximum
4167 * number of leaf entries, is controlled by the type of di_nextents
4168 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4169 * (a signed 16-bit number, xfs_aextnum_t).
4171 if (whichfork == XFS_DATA_FORK) {
4172 maxleafents = MAXEXTNUM;
4173 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4174 XFS_BMDR_SPACE_CALC(MINDBTPTRS) : mp->m_attroffset;
4176 maxleafents = MAXAEXTNUM;
4177 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4178 XFS_BMDR_SPACE_CALC(MINABTPTRS) :
4179 mp->m_sb.sb_inodesize - mp->m_attroffset;
4181 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
4182 minleafrecs = mp->m_bmap_dmnr[0];
4183 minnoderecs = mp->m_bmap_dmnr[1];
4184 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4185 for (level = 1; maxblocks > 1; level++) {
4186 if (maxblocks <= maxrootrecs)
4189 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4191 mp->m_bm_maxlevels[whichfork] = level;
4195 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4196 * caller. Frees all the extents that need freeing, which must be done
4197 * last due to locking considerations. We never free any extents in
4198 * the first transaction. This is to allow the caller to make the first
4199 * transaction a synchronous one so that the pointers to the data being
4200 * broken in this transaction will be permanent before the data is actually
4201 * freed. This is necessary to prevent blocks from being reallocated
4202 * and written to before the free and reallocation are actually permanent.
4203 * We do not just make the first transaction synchronous here, because
4204 * there are more efficient ways to gain the same protection in some cases
4205 * (see the file truncation code).
4207 * Return 1 if the given transaction was committed and a new one
4208 * started, and 0 otherwise in the committed parameter.
4213 xfs_trans_t **tp, /* transaction pointer addr */
4214 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4215 xfs_fsblock_t firstblock, /* controlled ag for allocs */
4216 int *committed) /* xact committed or not */
4218 xfs_efd_log_item_t *efd; /* extent free data */
4219 xfs_efi_log_item_t *efi; /* extent free intention */
4220 int error; /* error return value */
4221 xfs_bmap_free_item_t *free; /* free extent item */
4222 unsigned int logres; /* new log reservation */
4223 unsigned int logcount; /* new log count */
4224 xfs_mount_t *mp; /* filesystem mount structure */
4225 xfs_bmap_free_item_t *next; /* next item on free list */
4226 xfs_trans_t *ntp; /* new transaction pointer */
4228 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4229 if (flist->xbf_count == 0) {
4234 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4235 for (free = flist->xbf_first; free; free = free->xbfi_next)
4236 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4237 free->xbfi_blockcount);
4238 logres = ntp->t_log_res;
4239 logcount = ntp->t_log_count;
4240 ntp = xfs_trans_dup(*tp);
4241 error = xfs_trans_commit(*tp, 0, NULL);
4245 * We have a new transaction, so we should return committed=1,
4246 * even though we're returning an error.
4251 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4254 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4255 for (free = flist->xbf_first; free != NULL; free = next) {
4256 next = free->xbfi_next;
4257 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4258 free->xbfi_blockcount))) {
4260 * The bmap free list will be cleaned up at a
4261 * higher level. The EFI will be canceled when
4262 * this transaction is aborted.
4263 * Need to force shutdown here to make sure it
4264 * happens, since this transaction may not be
4268 if (!XFS_FORCED_SHUTDOWN(mp))
4269 xfs_force_shutdown(mp,
4270 (error == EFSCORRUPTED) ?
4271 SHUTDOWN_CORRUPT_INCORE :
4272 SHUTDOWN_META_IO_ERROR);
4275 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4276 free->xbfi_blockcount);
4277 xfs_bmap_del_free(flist, NULL, free);
4283 * Free up any items left in the list.
4287 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4289 xfs_bmap_free_item_t *free; /* free list item */
4290 xfs_bmap_free_item_t *next;
4292 if (flist->xbf_count == 0)
4294 ASSERT(flist->xbf_first != NULL);
4295 for (free = flist->xbf_first; free; free = next) {
4296 next = free->xbfi_next;
4297 xfs_bmap_del_free(flist, NULL, free);
4299 ASSERT(flist->xbf_count == 0);
4303 * Returns the file-relative block number of the first unused block(s)
4304 * in the file with at least "len" logically contiguous blocks free.
4305 * This is the lowest-address hole if the file has holes, else the first block
4306 * past the end of file.
4307 * Return 0 if the file is currently local (in-inode).
4310 xfs_bmap_first_unused(
4311 xfs_trans_t *tp, /* transaction pointer */
4312 xfs_inode_t *ip, /* incore inode */
4313 xfs_extlen_t len, /* size of hole to find */
4314 xfs_fileoff_t *first_unused, /* unused block */
4315 int whichfork) /* data or attr fork */
4317 xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
4318 int error; /* error return value */
4319 int idx; /* extent record index */
4320 xfs_ifork_t *ifp; /* inode fork pointer */
4321 xfs_fileoff_t lastaddr; /* last block number seen */
4322 xfs_fileoff_t lowest; /* lowest useful block */
4323 xfs_fileoff_t max; /* starting useful block */
4324 xfs_fileoff_t off; /* offset for this block */
4325 xfs_extnum_t nextents; /* number of extent entries */
4327 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4328 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4329 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4330 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4334 ifp = XFS_IFORK_PTR(ip, whichfork);
4335 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4336 (error = xfs_iread_extents(tp, ip, whichfork)))
4338 lowest = *first_unused;
4339 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4340 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
4341 ep = xfs_iext_get_ext(ifp, idx);
4342 off = xfs_bmbt_get_startoff(ep);
4344 * See if the hole before this extent will work.
4346 if (off >= lowest + len && off - max >= len) {
4347 *first_unused = max;
4350 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4351 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4353 *first_unused = max;
4358 * Returns the file-relative block number of the last block + 1 before
4359 * last_block (input value) in the file.
4360 * This is not based on i_size, it is based on the extent records.
4361 * Returns 0 for local files, as they do not have extent records.
4364 xfs_bmap_last_before(
4365 xfs_trans_t *tp, /* transaction pointer */
4366 xfs_inode_t *ip, /* incore inode */
4367 xfs_fileoff_t *last_block, /* last block */
4368 int whichfork) /* data or attr fork */
4370 xfs_fileoff_t bno; /* input file offset */
4371 int eof; /* hit end of file */
4372 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4373 int error; /* error return value */
4374 xfs_bmbt_irec_t got; /* current extent value */
4375 xfs_ifork_t *ifp; /* inode fork pointer */
4376 xfs_extnum_t lastx; /* last extent used */
4377 xfs_bmbt_irec_t prev; /* previous extent value */
4379 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4380 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4381 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4382 return XFS_ERROR(EIO);
4383 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4387 ifp = XFS_IFORK_PTR(ip, whichfork);
4388 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4389 (error = xfs_iread_extents(tp, ip, whichfork)))
4391 bno = *last_block - 1;
4392 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4394 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4395 if (prev.br_startoff == NULLFILEOFF)
4398 *last_block = prev.br_startoff + prev.br_blockcount;
4401 * Otherwise *last_block is already the right answer.
4407 * Returns the file-relative block number of the first block past eof in
4408 * the file. This is not based on i_size, it is based on the extent records.
4409 * Returns 0 for local files, as they do not have extent records.
4412 xfs_bmap_last_offset(
4413 xfs_trans_t *tp, /* transaction pointer */
4414 xfs_inode_t *ip, /* incore inode */
4415 xfs_fileoff_t *last_block, /* last block */
4416 int whichfork) /* data or attr fork */
4418 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4419 int error; /* error return value */
4420 xfs_ifork_t *ifp; /* inode fork pointer */
4421 xfs_extnum_t nextents; /* number of extent entries */
4423 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4424 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4425 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4426 return XFS_ERROR(EIO);
4427 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4431 ifp = XFS_IFORK_PTR(ip, whichfork);
4432 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4433 (error = xfs_iread_extents(tp, ip, whichfork)))
4435 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4440 ep = xfs_iext_get_ext(ifp, nextents - 1);
4441 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4446 * Returns whether the selected fork of the inode has exactly one
4447 * block or not. For the data fork we check this matches di_size,
4448 * implying the file's range is 0..bsize-1.
4450 int /* 1=>1 block, 0=>otherwise */
4452 xfs_inode_t *ip, /* incore inode */
4453 int whichfork) /* data or attr fork */
4455 xfs_bmbt_rec_t *ep; /* ptr to fork's extent */
4456 xfs_ifork_t *ifp; /* inode fork pointer */
4457 int rval; /* return value */
4458 xfs_bmbt_irec_t s; /* internal version of extent */
4461 if (whichfork == XFS_DATA_FORK)
4462 return ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize;
4464 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4466 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4468 ifp = XFS_IFORK_PTR(ip, whichfork);
4469 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4470 ep = xfs_iext_get_ext(ifp, 0);
4471 xfs_bmbt_get_all(ep, &s);
4472 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4473 if (rval && whichfork == XFS_DATA_FORK)
4474 ASSERT(ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4479 * Read in the extents to if_extents.
4480 * All inode fields are set up by caller, we just traverse the btree
4481 * and copy the records in. If the file system cannot contain unwritten
4482 * extents, the records are checked for no "state" flags.
4485 xfs_bmap_read_extents(
4486 xfs_trans_t *tp, /* transaction pointer */
4487 xfs_inode_t *ip, /* incore inode */
4488 int whichfork) /* data or attr fork */
4490 xfs_bmbt_block_t *block; /* current btree block */
4491 xfs_fsblock_t bno; /* block # of "block" */
4492 xfs_buf_t *bp; /* buffer for "block" */
4493 int error; /* error return value */
4494 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4495 #ifdef XFS_BMAP_TRACE
4496 static char fname[] = "xfs_bmap_read_extents";
4498 xfs_extnum_t i, j; /* index into the extents list */
4499 xfs_ifork_t *ifp; /* fork structure */
4500 int level; /* btree level, for checking */
4501 xfs_mount_t *mp; /* file system mount structure */
4502 __be64 *pp; /* pointer to block address */
4504 xfs_extnum_t room; /* number of entries there's room for */
4508 ifp = XFS_IFORK_PTR(ip, whichfork);
4509 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4510 XFS_EXTFMT_INODE(ip);
4511 block = ifp->if_broot;
4513 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4515 level = be16_to_cpu(block->bb_level);
4517 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
4518 bno = be64_to_cpu(*pp);
4519 ASSERT(bno != NULLDFSBNO);
4520 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4521 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4523 * Go down the tree until leaf level is reached, following the first
4524 * pointer (leftmost) at each level.
4526 while (level-- > 0) {
4527 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4528 XFS_BMAP_BTREE_REF)))
4530 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4531 XFS_WANT_CORRUPTED_GOTO(
4532 XFS_BMAP_SANITY_CHECK(mp, block, level),
4536 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
4537 1, mp->m_bmap_dmxr[1]);
4538 bno = be64_to_cpu(*pp);
4539 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4540 xfs_trans_brelse(tp, bp);
4543 * Here with bp and block set to the leftmost leaf node in the tree.
4545 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4548 * Loop over all leaf nodes. Copy information to the extent records.
4551 xfs_bmbt_rec_t *frp, *trp;
4552 xfs_fsblock_t nextbno;
4553 xfs_extnum_t num_recs;
4557 num_recs = be16_to_cpu(block->bb_numrecs);
4558 if (unlikely(i + num_recs > room)) {
4559 ASSERT(i + num_recs <= room);
4560 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4561 "corrupt dinode %Lu, (btree extents).",
4562 (unsigned long long) ip->i_ino);
4563 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4568 XFS_WANT_CORRUPTED_GOTO(
4569 XFS_BMAP_SANITY_CHECK(mp, block, 0),
4572 * Read-ahead the next leaf block, if any.
4574 nextbno = be64_to_cpu(block->bb_rightsib);
4575 if (nextbno != NULLFSBLOCK)
4576 xfs_btree_reada_bufl(mp, nextbno, 1);
4578 * Copy records into the extent records.
4580 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
4581 block, 1, mp->m_bmap_dmxr[0]);
4583 for (j = 0; j < num_recs; j++, i++, frp++) {
4584 trp = xfs_iext_get_ext(ifp, i);
4585 trp->l0 = INT_GET(frp->l0, ARCH_CONVERT);
4586 trp->l1 = INT_GET(frp->l1, ARCH_CONVERT);
4588 if (exntf == XFS_EXTFMT_NOSTATE) {
4590 * Check all attribute bmap btree records and
4591 * any "older" data bmap btree records for a
4592 * set bit in the "extent flag" position.
4594 if (unlikely(xfs_check_nostate_extents(ifp,
4595 start, num_recs))) {
4596 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4602 xfs_trans_brelse(tp, bp);
4605 * If we've reached the end, stop.
4607 if (bno == NULLFSBLOCK)
4609 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4610 XFS_BMAP_BTREE_REF)))
4612 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4614 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4615 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4616 xfs_bmap_trace_exlist(fname, ip, i, whichfork);
4619 xfs_trans_brelse(tp, bp);
4620 return XFS_ERROR(EFSCORRUPTED);
4623 #ifdef XFS_BMAP_TRACE
4625 * Add bmap trace insert entries for all the contents of the extent records.
4628 xfs_bmap_trace_exlist(
4629 char *fname, /* function name */
4630 xfs_inode_t *ip, /* incore inode pointer */
4631 xfs_extnum_t cnt, /* count of entries in the list */
4632 int whichfork) /* data or attr fork */
4634 xfs_bmbt_rec_t *ep; /* current extent record */
4635 xfs_extnum_t idx; /* extent record index */
4636 xfs_ifork_t *ifp; /* inode fork pointer */
4637 xfs_bmbt_irec_t s; /* file extent record */
4639 ifp = XFS_IFORK_PTR(ip, whichfork);
4640 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4641 for (idx = 0; idx < cnt; idx++) {
4642 ep = xfs_iext_get_ext(ifp, idx);
4643 xfs_bmbt_get_all(ep, &s);
4644 xfs_bmap_trace_insert(fname, "exlist", ip, idx, 1, &s, NULL,
4652 * Validate that the bmbt_irecs being returned from bmapi are valid
4653 * given the callers original parameters. Specifically check the
4654 * ranges of the returned irecs to ensure that they only extent beyond
4655 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4658 xfs_bmap_validate_ret(
4662 xfs_bmbt_irec_t *mval,
4666 int i; /* index to map values */
4668 ASSERT(ret_nmap <= nmap);
4670 for (i = 0; i < ret_nmap; i++) {
4671 ASSERT(mval[i].br_blockcount > 0);
4672 if (!(flags & XFS_BMAPI_ENTIRE)) {
4673 ASSERT(mval[i].br_startoff >= bno);
4674 ASSERT(mval[i].br_blockcount <= len);
4675 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4678 ASSERT(mval[i].br_startoff < bno + len);
4679 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4683 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4684 mval[i].br_startoff);
4685 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4686 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4687 mval[i].br_startblock != HOLESTARTBLOCK);
4688 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4689 mval[i].br_state == XFS_EXT_UNWRITTEN);
4696 * Map file blocks to filesystem blocks.
4697 * File range is given by the bno/len pair.
4698 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4699 * into a hole or past eof.
4700 * Only allocates blocks from a single allocation group,
4701 * to avoid locking problems.
4702 * The returned value in "firstblock" from the first call in a transaction
4703 * must be remembered and presented to subsequent calls in "firstblock".
4704 * An upper bound for the number of blocks to be allocated is supplied to
4705 * the first call in "total"; if no allocation group has that many free
4706 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4710 xfs_trans_t *tp, /* transaction pointer */
4711 xfs_inode_t *ip, /* incore inode */
4712 xfs_fileoff_t bno, /* starting file offs. mapped */
4713 xfs_filblks_t len, /* length to map in file */
4714 int flags, /* XFS_BMAPI_... */
4715 xfs_fsblock_t *firstblock, /* first allocated block
4716 controls a.g. for allocs */
4717 xfs_extlen_t total, /* total blocks needed */
4718 xfs_bmbt_irec_t *mval, /* output: map values */
4719 int *nmap, /* i/o: mval size/count */
4720 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4721 xfs_extdelta_t *delta) /* o: change made to incore extents */
4723 xfs_fsblock_t abno; /* allocated block number */
4724 xfs_extlen_t alen; /* allocated extent length */
4725 xfs_fileoff_t aoff; /* allocated file offset */
4726 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
4727 xfs_btree_cur_t *cur; /* bmap btree cursor */
4728 xfs_fileoff_t end; /* end of mapped file region */
4729 int eof; /* we've hit the end of extents */
4730 xfs_bmbt_rec_t *ep; /* extent record pointer */
4731 int error; /* error return */
4732 xfs_bmbt_irec_t got; /* current file extent record */
4733 xfs_ifork_t *ifp; /* inode fork pointer */
4734 xfs_extlen_t indlen; /* indirect blocks length */
4735 xfs_extnum_t lastx; /* last useful extent number */
4736 int logflags; /* flags for transaction logging */
4737 xfs_extlen_t minleft; /* min blocks left after allocation */
4738 xfs_extlen_t minlen; /* min allocation size */
4739 xfs_mount_t *mp; /* xfs mount structure */
4740 int n; /* current extent index */
4741 int nallocs; /* number of extents alloc\'d */
4742 xfs_extnum_t nextents; /* number of extents in file */
4743 xfs_fileoff_t obno; /* old block number (offset) */
4744 xfs_bmbt_irec_t prev; /* previous file extent record */
4745 int tmp_logflags; /* temp flags holder */
4746 int whichfork; /* data or attr fork */
4747 char inhole; /* current location is hole in file */
4748 char wasdelay; /* old extent was delayed */
4749 char wr; /* this is a write request */
4750 char rt; /* this is a realtime file */
4752 xfs_fileoff_t orig_bno; /* original block number value */
4753 int orig_flags; /* original flags arg value */
4754 xfs_filblks_t orig_len; /* original value of len arg */
4755 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4756 int orig_nmap; /* original value of *nmap */
4765 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4766 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4767 XFS_ATTR_FORK : XFS_DATA_FORK;
4769 if (unlikely(XFS_TEST_ERROR(
4770 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4771 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4772 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4773 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4774 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4775 return XFS_ERROR(EFSCORRUPTED);
4777 if (XFS_FORCED_SHUTDOWN(mp))
4778 return XFS_ERROR(EIO);
4779 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4780 ifp = XFS_IFORK_PTR(ip, whichfork);
4781 ASSERT(ifp->if_ext_max ==
4782 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4783 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4784 XFS_STATS_INC(xs_blk_mapw);
4786 XFS_STATS_INC(xs_blk_mapr);
4788 * IGSTATE flag is used to combine extents which
4789 * differ only due to the state of the extents.
4790 * This technique is used from xfs_getbmap()
4791 * when the caller does not wish to see the
4792 * separation (which is the default).
4794 * This technique is also used when writing a
4795 * buffer which has been partially written,
4796 * (usually by being flushed during a chunkread),
4797 * to ensure one write takes place. This also
4798 * prevents a change in the xfs inode extents at
4799 * this time, intentionally. This change occurs
4800 * on completion of the write operation, in
4801 * xfs_strat_comp(), where the xfs_bmapi() call
4802 * is transactioned, and the extents combined.
4804 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4805 wr = 0; /* no allocations are allowed */
4806 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
4810 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4812 if ((error = xfs_bmap_local_to_extents(tp, ip,
4813 firstblock, total, &logflags, whichfork)))
4816 if (wr && *firstblock == NULLFSBLOCK) {
4817 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4818 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4823 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4824 (error = xfs_iread_extents(tp, ip, whichfork)))
4826 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4828 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4834 delta->xed_startoff = NULLFILEOFF;
4835 delta->xed_blockcount = 0;
4837 while (bno < end && n < *nmap) {
4839 * Reading past eof, act as though there's a hole
4843 got.br_startoff = end;
4844 inhole = eof || got.br_startoff > bno;
4845 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
4846 ISNULLSTARTBLOCK(got.br_startblock);
4848 * First, deal with the hole before the allocated space
4849 * that we found, if any.
4851 if (wr && (inhole || wasdelay)) {
4853 * For the wasdelay case, we could also just
4854 * allocate the stuff asked for in this bmap call
4855 * but that wouldn't be as good.
4857 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
4858 alen = (xfs_extlen_t)got.br_blockcount;
4859 aoff = got.br_startoff;
4860 if (lastx != NULLEXTNUM && lastx) {
4861 ep = xfs_iext_get_ext(ifp, lastx - 1);
4862 xfs_bmbt_get_all(ep, &prev);
4864 } else if (wasdelay) {
4865 alen = (xfs_extlen_t)
4866 XFS_FILBLKS_MIN(len,
4868 got.br_blockcount) - bno);
4871 alen = (xfs_extlen_t)
4872 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4874 alen = (xfs_extlen_t)
4875 XFS_FILBLKS_MIN(alen,
4876 got.br_startoff - bno);
4879 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4880 if (flags & XFS_BMAPI_DELAY) {
4883 /* Figure out the extent size, adjust alen */
4885 if (!(extsz = ip->i_d.di_extsize))
4886 extsz = mp->m_sb.sb_rextsize;
4888 extsz = ip->i_d.di_extsize;
4891 error = xfs_bmap_extsize_align(mp,
4894 flags&XFS_BMAPI_DELAY,
4895 flags&XFS_BMAPI_CONVERT,
4901 extsz = alen / mp->m_sb.sb_rextsize;
4904 * Make a transaction-less quota reservation for
4905 * delayed allocation blocks. This number gets
4906 * adjusted later. We return if we haven't
4907 * allocated blocks already inside this loop.
4909 if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS(
4910 mp, NULL, ip, (long)alen, 0,
4911 rt ? XFS_QMOPT_RES_RTBLKS :
4912 XFS_QMOPT_RES_REGBLKS))) {
4915 ASSERT(cur == NULL);
4922 * Split changing sb for alen and indlen since
4923 * they could be coming from different places.
4925 indlen = (xfs_extlen_t)
4926 xfs_bmap_worst_indlen(ip, alen);
4930 error = xfs_mod_incore_sb(mp,
4933 XFS_BMAPI_RSVBLOCKS));
4935 error = xfs_mod_incore_sb(mp,
4938 XFS_BMAPI_RSVBLOCKS));
4941 error = xfs_mod_incore_sb(mp,
4944 XFS_BMAPI_RSVBLOCKS));
4946 xfs_mod_incore_sb(mp,
4949 XFS_BMAPI_RSVBLOCKS));
4951 xfs_mod_incore_sb(mp,
4954 XFS_BMAPI_RSVBLOCKS));
4958 if (XFS_IS_QUOTA_ON(mp))
4959 /* unreserve the blocks now */
4961 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4964 XFS_QMOPT_RES_RTBLKS :
4965 XFS_QMOPT_RES_REGBLKS);
4969 ip->i_delayed_blks += alen;
4970 abno = NULLSTARTBLOCK(indlen);
4973 * If first time, allocate and fill in
4974 * once-only bma fields.
4976 if (bma.ip == NULL) {
4984 /* Indicate if this is the first user data
4985 * in the file, or just any user data.
4987 if (!(flags & XFS_BMAPI_METADATA)) {
4988 bma.userdata = (aoff == 0) ?
4989 XFS_ALLOC_INITIAL_USER_DATA :
4993 * Fill in changeable bma fields.
4996 bma.firstblock = *firstblock;
4999 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
5000 bma.wasdel = wasdelay;
5001 bma.minlen = minlen;
5002 bma.low = flist->xbf_low;
5003 bma.minleft = minleft;
5005 * Only want to do the alignment at the
5006 * eof if it is userdata and allocation length
5007 * is larger than a stripe unit.
5009 if (mp->m_dalign && alen >= mp->m_dalign &&
5010 (!(flags & XFS_BMAPI_METADATA)) &&
5011 (whichfork == XFS_DATA_FORK)) {
5012 if ((error = xfs_bmap_isaeof(ip, aoff,
5013 whichfork, &bma.aeof)))
5020 if ((error = xfs_bmap_alloc(&bma)))
5023 * Copy out result fields.
5026 if ((flist->xbf_low = bma.low))
5030 ASSERT(*firstblock == NULLFSBLOCK ||
5031 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5032 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
5034 XFS_FSB_TO_AGNO(mp, *firstblock) <
5035 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
5036 *firstblock = bma.firstblock;
5038 cur->bc_private.b.firstblock =
5040 if (abno == NULLFSBLOCK)
5042 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5043 cur = xfs_btree_init_cursor(mp,
5044 tp, NULL, 0, XFS_BTNUM_BMAP,
5046 cur->bc_private.b.firstblock =
5048 cur->bc_private.b.flist = flist;
5051 * Bump the number of extents we've allocated
5057 cur->bc_private.b.flags =
5058 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
5059 got.br_startoff = aoff;
5060 got.br_startblock = abno;
5061 got.br_blockcount = alen;
5062 got.br_state = XFS_EXT_NORM; /* assume normal */
5064 * Determine state of extent, and the filesystem.
5065 * A wasdelay extent has been initialized, so
5066 * shouldn't be flagged as unwritten.
5068 if (wr && XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5069 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5070 got.br_state = XFS_EXT_UNWRITTEN;
5072 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
5073 firstblock, flist, &tmp_logflags, delta,
5074 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5075 logflags |= tmp_logflags;
5078 lastx = ifp->if_lastex;
5079 ep = xfs_iext_get_ext(ifp, lastx);
5080 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5081 xfs_bmbt_get_all(ep, &got);
5082 ASSERT(got.br_startoff <= aoff);
5083 ASSERT(got.br_startoff + got.br_blockcount >=
5086 if (flags & XFS_BMAPI_DELAY) {
5087 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
5088 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
5090 ASSERT(got.br_state == XFS_EXT_NORM ||
5091 got.br_state == XFS_EXT_UNWRITTEN);
5094 * Fall down into the found allocated space case.
5096 } else if (inhole) {
5098 * Reading in a hole.
5100 mval->br_startoff = bno;
5101 mval->br_startblock = HOLESTARTBLOCK;
5102 mval->br_blockcount =
5103 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5104 mval->br_state = XFS_EXT_NORM;
5105 bno += mval->br_blockcount;
5106 len -= mval->br_blockcount;
5112 * Then deal with the allocated space we found.
5115 if (!(flags & XFS_BMAPI_ENTIRE) &&
5116 (got.br_startoff + got.br_blockcount > obno)) {
5119 ASSERT((bno >= obno) || (n == 0));
5121 mval->br_startoff = bno;
5122 if (ISNULLSTARTBLOCK(got.br_startblock)) {
5123 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5124 mval->br_startblock = DELAYSTARTBLOCK;
5126 mval->br_startblock =
5128 (bno - got.br_startoff);
5130 * Return the minimum of what we got and what we
5131 * asked for for the length. We can use the len
5132 * variable here because it is modified below
5133 * and we could have been there before coming
5134 * here if the first part of the allocation
5135 * didn't overlap what was asked for.
5137 mval->br_blockcount =
5138 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5139 (bno - got.br_startoff));
5140 mval->br_state = got.br_state;
5141 ASSERT(mval->br_blockcount <= len);
5144 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
5145 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5146 mval->br_startblock = DELAYSTARTBLOCK;
5151 * Check if writing previously allocated but
5152 * unwritten extents.
5154 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5155 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5157 * Modify (by adding) the state flag, if writing.
5159 ASSERT(mval->br_blockcount <= len);
5160 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5161 cur = xfs_btree_init_cursor(mp,
5162 tp, NULL, 0, XFS_BTNUM_BMAP,
5164 cur->bc_private.b.firstblock =
5166 cur->bc_private.b.flist = flist;
5168 mval->br_state = XFS_EXT_NORM;
5169 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
5170 firstblock, flist, &tmp_logflags, delta,
5171 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5172 logflags |= tmp_logflags;
5175 lastx = ifp->if_lastex;
5176 ep = xfs_iext_get_ext(ifp, lastx);
5177 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5178 xfs_bmbt_get_all(ep, &got);
5180 * We may have combined previously unwritten
5181 * space with written space, so generate
5184 if (mval->br_blockcount < len)
5188 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5189 ((mval->br_startoff + mval->br_blockcount) <= end));
5190 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5191 (mval->br_blockcount <= len) ||
5192 (mval->br_startoff < obno));
5193 bno = mval->br_startoff + mval->br_blockcount;
5195 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5196 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5197 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5198 ASSERT(mval->br_state == mval[-1].br_state);
5199 mval[-1].br_blockcount = mval->br_blockcount;
5200 mval[-1].br_state = mval->br_state;
5201 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5202 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5203 mval[-1].br_startblock != HOLESTARTBLOCK &&
5204 mval->br_startblock ==
5205 mval[-1].br_startblock + mval[-1].br_blockcount &&
5206 ((flags & XFS_BMAPI_IGSTATE) ||
5207 mval[-1].br_state == mval->br_state)) {
5208 ASSERT(mval->br_startoff ==
5209 mval[-1].br_startoff + mval[-1].br_blockcount);
5210 mval[-1].br_blockcount += mval->br_blockcount;
5212 mval->br_startblock == DELAYSTARTBLOCK &&
5213 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5214 mval->br_startoff ==
5215 mval[-1].br_startoff + mval[-1].br_blockcount) {
5216 mval[-1].br_blockcount += mval->br_blockcount;
5217 mval[-1].br_state = mval->br_state;
5218 } else if (!((n == 0) &&
5219 ((mval->br_startoff + mval->br_blockcount) <=
5225 * If we're done, stop now. Stop when we've allocated
5226 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5227 * the transaction may get too big.
5229 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5232 * Else go on to the next record.
5234 ep = xfs_iext_get_ext(ifp, ++lastx);
5235 if (lastx >= nextents) {
5239 xfs_bmbt_get_all(ep, &got);
5241 ifp->if_lastex = lastx;
5244 * Transform from btree to extents, give it cur.
5246 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5247 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5249 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5250 &tmp_logflags, whichfork);
5251 logflags |= tmp_logflags;
5255 ASSERT(ifp->if_ext_max ==
5256 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5257 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5258 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5260 if (delta && delta->xed_startoff != NULLFILEOFF) {
5261 /* A change was actually made.
5262 * Note that delta->xed_blockount is an offset at this
5263 * point and needs to be converted to a block count.
5265 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5266 delta->xed_blockcount -= delta->xed_startoff;
5270 * Log everything. Do this after conversion, there's no point in
5271 * logging the extent records if we've converted to btree format.
5273 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5274 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5275 logflags &= ~XFS_ILOG_FEXT(whichfork);
5276 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5277 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5278 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5280 * Log whatever the flags say, even if error. Otherwise we might miss
5281 * detecting a case where the data is changed, there's an error,
5282 * and it's not logged so we don't shutdown when we should.
5286 xfs_trans_log_inode(tp, ip, logflags);
5290 ASSERT(*firstblock == NULLFSBLOCK ||
5291 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5293 cur->bc_private.b.firstblock) ||
5295 XFS_FSB_TO_AGNO(mp, *firstblock) <
5297 cur->bc_private.b.firstblock)));
5298 *firstblock = cur->bc_private.b.firstblock;
5300 xfs_btree_del_cursor(cur,
5301 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5304 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5310 * Map file blocks to filesystem blocks, simple version.
5311 * One block (extent) only, read-only.
5312 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5313 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5314 * was set and all the others were clear.
5318 xfs_trans_t *tp, /* transaction pointer */
5319 xfs_inode_t *ip, /* incore inode */
5320 int whichfork, /* data or attr fork */
5321 xfs_fsblock_t *fsb, /* output: mapped block */
5322 xfs_fileoff_t bno) /* starting file offs. mapped */
5324 int eof; /* we've hit the end of extents */
5325 int error; /* error return */
5326 xfs_bmbt_irec_t got; /* current file extent record */
5327 xfs_ifork_t *ifp; /* inode fork pointer */
5328 xfs_extnum_t lastx; /* last useful extent number */
5329 xfs_bmbt_irec_t prev; /* previous file extent record */
5331 ifp = XFS_IFORK_PTR(ip, whichfork);
5333 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5334 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5335 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5337 return XFS_ERROR(EFSCORRUPTED);
5339 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5340 return XFS_ERROR(EIO);
5341 XFS_STATS_INC(xs_blk_mapr);
5342 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5343 (error = xfs_iread_extents(tp, ip, whichfork)))
5345 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5348 * Reading past eof, act as though there's a hole
5351 if (eof || got.br_startoff > bno) {
5355 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
5356 ASSERT(bno < got.br_startoff + got.br_blockcount);
5357 *fsb = got.br_startblock + (bno - got.br_startoff);
5358 ifp->if_lastex = lastx;
5363 * Unmap (remove) blocks from a file.
5364 * If nexts is nonzero then the number of extents to remove is limited to
5365 * that value. If not all extents in the block range can be removed then
5370 xfs_trans_t *tp, /* transaction pointer */
5371 struct xfs_inode *ip, /* incore inode */
5372 xfs_fileoff_t bno, /* starting offset to unmap */
5373 xfs_filblks_t len, /* length to unmap in file */
5374 int flags, /* misc flags */
5375 xfs_extnum_t nexts, /* number of extents max */
5376 xfs_fsblock_t *firstblock, /* first allocated block
5377 controls a.g. for allocs */
5378 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5379 xfs_extdelta_t *delta, /* o: change made to incore
5381 int *done) /* set if not done yet */
5383 xfs_btree_cur_t *cur; /* bmap btree cursor */
5384 xfs_bmbt_irec_t del; /* extent being deleted */
5385 int eof; /* is deleting at eof */
5386 xfs_bmbt_rec_t *ep; /* extent record pointer */
5387 int error; /* error return value */
5388 xfs_extnum_t extno; /* extent number in list */
5389 xfs_bmbt_irec_t got; /* current extent record */
5390 xfs_ifork_t *ifp; /* inode fork pointer */
5391 int isrt; /* freeing in rt area */
5392 xfs_extnum_t lastx; /* last extent index used */
5393 int logflags; /* transaction logging flags */
5394 xfs_extlen_t mod; /* rt extent offset */
5395 xfs_mount_t *mp; /* mount structure */
5396 xfs_extnum_t nextents; /* number of file extents */
5397 xfs_bmbt_irec_t prev; /* previous extent record */
5398 xfs_fileoff_t start; /* first file offset deleted */
5399 int tmp_logflags; /* partial logging flags */
5400 int wasdel; /* was a delayed alloc extent */
5401 int whichfork; /* data or attribute fork */
5402 int rsvd; /* OK to allocate reserved blocks */
5405 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5406 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5407 XFS_ATTR_FORK : XFS_DATA_FORK;
5408 ifp = XFS_IFORK_PTR(ip, whichfork);
5410 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5411 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5412 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5414 return XFS_ERROR(EFSCORRUPTED);
5417 if (XFS_FORCED_SHUTDOWN(mp))
5418 return XFS_ERROR(EIO);
5419 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5422 ASSERT(ifp->if_ext_max ==
5423 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5424 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5425 (error = xfs_iread_extents(tp, ip, whichfork)))
5427 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5428 if (nextents == 0) {
5432 XFS_STATS_INC(xs_blk_unmap);
5433 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5435 bno = start + len - 1;
5436 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5439 delta->xed_startoff = NULLFILEOFF;
5440 delta->xed_blockcount = 0;
5443 * Check to see if the given block number is past the end of the
5444 * file, back up to the last block if so...
5447 ep = xfs_iext_get_ext(ifp, --lastx);
5448 xfs_bmbt_get_all(ep, &got);
5449 bno = got.br_startoff + got.br_blockcount - 1;
5452 if (ifp->if_flags & XFS_IFBROOT) {
5453 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5454 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
5456 cur->bc_private.b.firstblock = *firstblock;
5457 cur->bc_private.b.flist = flist;
5458 cur->bc_private.b.flags = 0;
5462 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5463 (nexts == 0 || extno < nexts)) {
5465 * Is the found extent after a hole in which bno lives?
5466 * Just back up to the previous extent, if so.
5468 if (got.br_startoff > bno) {
5471 ep = xfs_iext_get_ext(ifp, lastx);
5472 xfs_bmbt_get_all(ep, &got);
5475 * Is the last block of this extent before the range
5476 * we're supposed to delete? If so, we're done.
5478 bno = XFS_FILEOFF_MIN(bno,
5479 got.br_startoff + got.br_blockcount - 1);
5483 * Then deal with the (possibly delayed) allocated space
5488 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
5489 if (got.br_startoff < start) {
5490 del.br_startoff = start;
5491 del.br_blockcount -= start - got.br_startoff;
5493 del.br_startblock += start - got.br_startoff;
5495 if (del.br_startoff + del.br_blockcount > bno + 1)
5496 del.br_blockcount = bno + 1 - del.br_startoff;
5497 sum = del.br_startblock + del.br_blockcount;
5499 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5501 * Realtime extent not lined up at the end.
5502 * The extent could have been split into written
5503 * and unwritten pieces, or we could just be
5504 * unmapping part of it. But we can't really
5505 * get rid of part of a realtime extent.
5507 if (del.br_state == XFS_EXT_UNWRITTEN ||
5508 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5510 * This piece is unwritten, or we're not
5511 * using unwritten extents. Skip over it.
5514 bno -= mod > del.br_blockcount ?
5515 del.br_blockcount : mod;
5516 if (bno < got.br_startoff) {
5518 xfs_bmbt_get_all(xfs_iext_get_ext(
5524 * It's written, turn it unwritten.
5525 * This is better than zeroing it.
5527 ASSERT(del.br_state == XFS_EXT_NORM);
5528 ASSERT(xfs_trans_get_block_res(tp) > 0);
5530 * If this spans a realtime extent boundary,
5531 * chop it back to the start of the one we end at.
5533 if (del.br_blockcount > mod) {
5534 del.br_startoff += del.br_blockcount - mod;
5535 del.br_startblock += del.br_blockcount - mod;
5536 del.br_blockcount = mod;
5538 del.br_state = XFS_EXT_UNWRITTEN;
5539 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5540 firstblock, flist, &logflags, delta,
5546 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5548 * Realtime extent is lined up at the end but not
5549 * at the front. We'll get rid of full extents if
5552 mod = mp->m_sb.sb_rextsize - mod;
5553 if (del.br_blockcount > mod) {
5554 del.br_blockcount -= mod;
5555 del.br_startoff += mod;
5556 del.br_startblock += mod;
5557 } else if ((del.br_startoff == start &&
5558 (del.br_state == XFS_EXT_UNWRITTEN ||
5559 xfs_trans_get_block_res(tp) == 0)) ||
5560 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5562 * Can't make it unwritten. There isn't
5563 * a full extent here so just skip it.
5565 ASSERT(bno >= del.br_blockcount);
5566 bno -= del.br_blockcount;
5567 if (bno < got.br_startoff) {
5569 xfs_bmbt_get_all(--ep, &got);
5572 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5574 * This one is already unwritten.
5575 * It must have a written left neighbor.
5576 * Unwrite the killed part of that one and
5580 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5582 ASSERT(prev.br_state == XFS_EXT_NORM);
5583 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
5584 ASSERT(del.br_startblock ==
5585 prev.br_startblock + prev.br_blockcount);
5586 if (prev.br_startoff < start) {
5587 mod = start - prev.br_startoff;
5588 prev.br_blockcount -= mod;
5589 prev.br_startblock += mod;
5590 prev.br_startoff = start;
5592 prev.br_state = XFS_EXT_UNWRITTEN;
5593 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5594 &prev, firstblock, flist, &logflags,
5595 delta, XFS_DATA_FORK, 0);
5600 ASSERT(del.br_state == XFS_EXT_NORM);
5601 del.br_state = XFS_EXT_UNWRITTEN;
5602 error = xfs_bmap_add_extent(ip, lastx, &cur,
5603 &del, firstblock, flist, &logflags,
5604 delta, XFS_DATA_FORK, 0);
5611 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
5612 /* Update realtime/data freespace, unreserve quota */
5614 xfs_filblks_t rtexts;
5616 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5617 do_div(rtexts, mp->m_sb.sb_rextsize);
5618 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5620 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5621 NULL, ip, -((long)del.br_blockcount), 0,
5622 XFS_QMOPT_RES_RTBLKS);
5624 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
5625 (int)del.br_blockcount, rsvd);
5626 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5627 NULL, ip, -((long)del.br_blockcount), 0,
5628 XFS_QMOPT_RES_REGBLKS);
5630 ip->i_delayed_blks -= del.br_blockcount;
5632 cur->bc_private.b.flags |=
5633 XFS_BTCUR_BPRV_WASDEL;
5635 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5637 * If it's the case where the directory code is running
5638 * with no block reservation, and the deleted block is in
5639 * the middle of its extent, and the resulting insert
5640 * of an extent would cause transformation to btree format,
5641 * then reject it. The calling code will then swap
5642 * blocks around instead.
5643 * We have to do this now, rather than waiting for the
5644 * conversion to btree format, since the transaction
5647 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5648 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5649 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5650 del.br_startoff > got.br_startoff &&
5651 del.br_startoff + del.br_blockcount <
5652 got.br_startoff + got.br_blockcount) {
5653 error = XFS_ERROR(ENOSPC);
5656 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5657 &tmp_logflags, delta, whichfork, rsvd);
5658 logflags |= tmp_logflags;
5661 bno = del.br_startoff - 1;
5663 lastx = ifp->if_lastex;
5665 * If not done go on to the next (previous) record.
5666 * Reset ep in case the extents array was re-alloced.
5668 ep = xfs_iext_get_ext(ifp, lastx);
5669 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5670 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5671 xfs_bmbt_get_startoff(ep) > bno) {
5673 ep = xfs_iext_get_ext(ifp, lastx);
5676 xfs_bmbt_get_all(ep, &got);
5680 ifp->if_lastex = lastx;
5681 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5682 ASSERT(ifp->if_ext_max ==
5683 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5685 * Convert to a btree if necessary.
5687 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5688 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5689 ASSERT(cur == NULL);
5690 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5691 &cur, 0, &tmp_logflags, whichfork);
5692 logflags |= tmp_logflags;
5697 * transform from btree to extents, give it cur
5699 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5700 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5701 ASSERT(cur != NULL);
5702 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5704 logflags |= tmp_logflags;
5709 * transform from extents to local?
5711 ASSERT(ifp->if_ext_max ==
5712 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5714 if (delta && delta->xed_startoff != NULLFILEOFF) {
5715 /* A change was actually made.
5716 * Note that delta->xed_blockount is an offset at this
5717 * point and needs to be converted to a block count.
5719 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5720 delta->xed_blockcount -= delta->xed_startoff;
5724 * Log everything. Do this after conversion, there's no point in
5725 * logging the extent records if we've converted to btree format.
5727 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5728 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5729 logflags &= ~XFS_ILOG_FEXT(whichfork);
5730 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5731 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5732 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5734 * Log inode even in the error case, if the transaction
5735 * is dirty we'll need to shut down the filesystem.
5738 xfs_trans_log_inode(tp, ip, logflags);
5741 *firstblock = cur->bc_private.b.firstblock;
5742 cur->bc_private.b.allocated = 0;
5744 xfs_btree_del_cursor(cur,
5745 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5751 * Fcntl interface to xfs_bmapi.
5753 int /* error code */
5755 bhv_desc_t *bdp, /* XFS behavior descriptor*/
5756 struct getbmap *bmv, /* user bmap structure */
5757 void __user *ap, /* pointer to user's array */
5758 int interface) /* interface flags */
5760 __int64_t bmvend; /* last block requested */
5761 int error; /* return value */
5762 __int64_t fixlen; /* length for -1 case */
5763 int i; /* extent number */
5764 xfs_inode_t *ip; /* xfs incore inode pointer */
5765 bhv_vnode_t *vp; /* corresponding vnode */
5766 int lock; /* lock state */
5767 xfs_bmbt_irec_t *map; /* buffer for user's data */
5768 xfs_mount_t *mp; /* file system mount point */
5769 int nex; /* # of user extents can do */
5770 int nexleft; /* # of user extents left */
5771 int subnex; /* # of bmapi's can do */
5772 int nmap; /* number of map entries */
5773 struct getbmap out; /* output structure */
5774 int whichfork; /* data or attr fork */
5775 int prealloced; /* this is a file with
5776 * preallocated data space */
5777 int sh_unwritten; /* true, if unwritten */
5778 /* extents listed separately */
5779 int bmapi_flags; /* flags for xfs_bmapi */
5780 __int32_t oflags; /* getbmapx bmv_oflags field */
5782 vp = BHV_TO_VNODE(bdp);
5783 ip = XFS_BHVTOI(bdp);
5786 whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5787 sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
5789 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5790 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5791 * bit is set for the file, generate a read event in order
5792 * that the DMAPI application may do its thing before we return
5793 * the extents. Usually this means restoring user file data to
5794 * regions of the file that look like holes.
5796 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5797 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5798 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5799 * could misinterpret holes in a DMAPI file as true holes,
5800 * when in fact they may represent offline user data.
5802 if ( (interface & BMV_IF_NO_DMAPI_READ) == 0
5803 && DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)
5804 && whichfork == XFS_DATA_FORK) {
5806 error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, 0, 0, 0, NULL);
5808 return XFS_ERROR(error);
5811 if (whichfork == XFS_ATTR_FORK) {
5812 if (XFS_IFORK_Q(ip)) {
5813 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5814 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5815 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5816 return XFS_ERROR(EINVAL);
5817 } else if (unlikely(
5818 ip->i_d.di_aformat != 0 &&
5819 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5820 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5822 return XFS_ERROR(EFSCORRUPTED);
5824 } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5825 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5826 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5827 return XFS_ERROR(EINVAL);
5828 if (whichfork == XFS_DATA_FORK) {
5829 if ((ip->i_d.di_extsize && (ip->i_d.di_flags &
5830 (XFS_DIFLAG_REALTIME|XFS_DIFLAG_EXTSIZE))) ||
5831 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5833 fixlen = XFS_MAXIOFFSET(mp);
5836 fixlen = ip->i_d.di_size;
5843 if (bmv->bmv_length == -1) {
5844 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5845 bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
5847 } else if (bmv->bmv_length < 0)
5848 return XFS_ERROR(EINVAL);
5849 if (bmv->bmv_length == 0) {
5850 bmv->bmv_entries = 0;
5853 nex = bmv->bmv_count - 1;
5855 return XFS_ERROR(EINVAL);
5856 bmvend = bmv->bmv_offset + bmv->bmv_length;
5858 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5860 if (whichfork == XFS_DATA_FORK && ip->i_delayed_blks) {
5861 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
5862 error = bhv_vop_flush_pages(vp, (xfs_off_t)0, -1, 0, FI_REMAPF);
5865 ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
5867 lock = xfs_ilock_map_shared(ip);
5870 * Don't let nex be bigger than the number of extents
5871 * we can have assuming alternating holes and real extents.
5873 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5874 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5876 bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
5877 ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
5880 * Allocate enough space to handle "subnex" maps at a time.
5883 map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
5885 bmv->bmv_entries = 0;
5887 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
5889 goto unlock_and_return;
5895 nmap = (nexleft > subnex) ? subnex : nexleft;
5896 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5897 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5898 bmapi_flags, NULL, 0, map, &nmap,
5901 goto unlock_and_return;
5902 ASSERT(nmap <= subnex);
5904 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5906 oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
5907 BMV_OF_PREALLOC : 0;
5908 out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
5909 out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5910 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
5911 if (map[i].br_startblock == HOLESTARTBLOCK &&
5912 ((prealloced && out.bmv_offset + out.bmv_length == bmvend) ||
5913 whichfork == XFS_ATTR_FORK )) {
5915 * came to hole at end of file or the end of
5918 goto unlock_and_return;
5921 (map[i].br_startblock == HOLESTARTBLOCK) ?
5923 XFS_FSB_TO_DB(ip, map[i].br_startblock);
5925 /* return either getbmap/getbmapx structure. */
5926 if (interface & BMV_IF_EXTENDED) {
5927 struct getbmapx outx;
5929 GETBMAP_CONVERT(out,outx);
5930 outx.bmv_oflags = oflags;
5931 outx.bmv_unused1 = outx.bmv_unused2 = 0;
5932 if (copy_to_user(ap, &outx,
5934 error = XFS_ERROR(EFAULT);
5935 goto unlock_and_return;
5938 if (copy_to_user(ap, &out,
5940 error = XFS_ERROR(EFAULT);
5941 goto unlock_and_return;
5945 out.bmv_offset + out.bmv_length;
5946 bmv->bmv_length = MAX((__int64_t)0,
5947 (__int64_t)(bmvend - bmv->bmv_offset));
5949 ap = (interface & BMV_IF_EXTENDED) ?
5951 ((struct getbmapx __user *)ap + 1) :
5953 ((struct getbmap __user *)ap + 1);
5956 } while (nmap && nexleft && bmv->bmv_length);
5959 xfs_iunlock_map_shared(ip, lock);
5960 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5962 kmem_free(map, subnex * sizeof(*map));
5968 * Check the last inode extent to determine whether this allocation will result
5969 * in blocks being allocated at the end of the file. When we allocate new data
5970 * blocks at the end of the file which do not start at the previous data block,
5971 * we will try to align the new blocks at stripe unit boundaries.
5973 STATIC int /* error */
5975 xfs_inode_t *ip, /* incore inode pointer */
5976 xfs_fileoff_t off, /* file offset in fsblocks */
5977 int whichfork, /* data or attribute fork */
5978 char *aeof) /* return value */
5980 int error; /* error return value */
5981 xfs_ifork_t *ifp; /* inode fork pointer */
5982 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
5983 xfs_extnum_t nextents; /* number of file extents */
5984 xfs_bmbt_irec_t s; /* expanded extent record */
5986 ASSERT(whichfork == XFS_DATA_FORK);
5987 ifp = XFS_IFORK_PTR(ip, whichfork);
5988 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5989 (error = xfs_iread_extents(NULL, ip, whichfork)))
5991 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5992 if (nextents == 0) {
5997 * Go to the last extent
5999 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6000 xfs_bmbt_get_all(lastrec, &s);
6002 * Check we are allocating in the last extent (for delayed allocations)
6003 * or past the last extent for non-delayed allocations.
6005 *aeof = (off >= s.br_startoff &&
6006 off < s.br_startoff + s.br_blockcount &&
6007 ISNULLSTARTBLOCK(s.br_startblock)) ||
6008 off >= s.br_startoff + s.br_blockcount;
6013 * Check if the endoff is outside the last extent. If so the caller will grow
6014 * the allocation to a stripe unit boundary.
6018 xfs_inode_t *ip, /* incore inode pointer */
6019 xfs_fileoff_t endoff, /* file offset in fsblocks */
6020 int whichfork, /* data or attribute fork */
6021 int *eof) /* result value */
6023 xfs_fsblock_t blockcount; /* extent block count */
6024 int error; /* error return value */
6025 xfs_ifork_t *ifp; /* inode fork pointer */
6026 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
6027 xfs_extnum_t nextents; /* number of file extents */
6028 xfs_fileoff_t startoff; /* extent starting file offset */
6030 ASSERT(whichfork == XFS_DATA_FORK);
6031 ifp = XFS_IFORK_PTR(ip, whichfork);
6032 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6033 (error = xfs_iread_extents(NULL, ip, whichfork)))
6035 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6036 if (nextents == 0) {
6041 * Go to the last extent
6043 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6044 startoff = xfs_bmbt_get_startoff(lastrec);
6045 blockcount = xfs_bmbt_get_blockcount(lastrec);
6046 *eof = endoff >= startoff + blockcount;
6052 * Check that the extents list for the inode ip is in the right order.
6055 xfs_bmap_check_extents(
6056 xfs_inode_t *ip, /* incore inode pointer */
6057 int whichfork) /* data or attr fork */
6059 xfs_bmbt_rec_t *ep; /* current extent entry */
6060 xfs_extnum_t idx; /* extent record index */
6061 xfs_ifork_t *ifp; /* inode fork pointer */
6062 xfs_extnum_t nextents; /* number of extents in list */
6063 xfs_bmbt_rec_t *nextp; /* next extent entry */
6065 ifp = XFS_IFORK_PTR(ip, whichfork);
6066 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
6067 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6068 ep = xfs_iext_get_ext(ifp, 0);
6069 for (idx = 0; idx < nextents - 1; idx++) {
6070 nextp = xfs_iext_get_ext(ifp, idx + 1);
6071 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
6080 xfs_btree_cur_t *cur,
6090 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6091 bp = cur->bc_bufs[i];
6093 if (XFS_BUF_ADDR(bp) == bno)
6094 break; /* Found it */
6096 if (i == XFS_BTREE_MAXLEVELS)
6099 if (!bp) { /* Chase down all the log items to see if the bp is there */
6100 xfs_log_item_chunk_t *licp;
6104 licp = &tp->t_items;
6105 while (!bp && licp != NULL) {
6106 if (XFS_LIC_ARE_ALL_FREE(licp)) {
6107 licp = licp->lic_next;
6110 for (i = 0; i < licp->lic_unused; i++) {
6111 xfs_log_item_desc_t *lidp;
6112 xfs_log_item_t *lip;
6113 xfs_buf_log_item_t *bip;
6116 if (XFS_LIC_ISFREE(licp, i)) {
6120 lidp = XFS_LIC_SLOT(licp, i);
6121 lip = lidp->lid_item;
6122 if (lip->li_type != XFS_LI_BUF)
6125 bip = (xfs_buf_log_item_t *)lip;
6128 if (XFS_BUF_ADDR(lbp) == bno) {
6130 break; /* Found it */
6133 licp = licp->lic_next;
6141 xfs_bmbt_block_t *block,
6147 __be64 *pp, *thispa; /* pointer to block address */
6148 xfs_bmbt_key_t *prevp, *keyp;
6150 ASSERT(be16_to_cpu(block->bb_level) > 0);
6153 for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
6154 dmxr = mp->m_bmap_dmxr[0];
6157 keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
6159 keyp = XFS_BTREE_KEY_ADDR(mp->m_sb.sb_blocksize,
6160 xfs_bmbt, block, i, dmxr);
6164 xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
6169 * Compare the block numbers to see if there are dups.
6173 pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
6175 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6176 xfs_bmbt, block, i, dmxr);
6178 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
6180 thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
6182 thispa = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6183 xfs_bmbt, block, j, dmxr);
6185 if (*thispa == *pp) {
6186 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
6188 (unsigned long long)be64_to_cpu(*thispa));
6189 panic("%s: ptrs are equal in node\n",
6197 * Check that the extents for the inode ip are in the right order in all
6202 xfs_bmap_check_leaf_extents(
6203 xfs_btree_cur_t *cur, /* btree cursor or null */
6204 xfs_inode_t *ip, /* incore inode pointer */
6205 int whichfork) /* data or attr fork */
6207 xfs_bmbt_block_t *block; /* current btree block */
6208 xfs_fsblock_t bno; /* block # of "block" */
6209 xfs_buf_t *bp; /* buffer for "block" */
6210 int error; /* error return value */
6211 xfs_extnum_t i=0, j; /* index into the extents list */
6212 xfs_ifork_t *ifp; /* fork structure */
6213 int level; /* btree level, for checking */
6214 xfs_mount_t *mp; /* file system mount structure */
6215 __be64 *pp; /* pointer to block address */
6216 xfs_bmbt_rec_t *ep; /* pointer to current extent */
6217 xfs_bmbt_rec_t *lastp; /* pointer to previous extent */
6218 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
6221 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6227 ifp = XFS_IFORK_PTR(ip, whichfork);
6228 block = ifp->if_broot;
6230 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6232 level = be16_to_cpu(block->bb_level);
6234 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6235 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6236 bno = be64_to_cpu(*pp);
6238 ASSERT(bno != NULLDFSBNO);
6239 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6240 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6243 * Go down the tree until leaf level is reached, following the first
6244 * pointer (leftmost) at each level.
6246 while (level-- > 0) {
6247 /* See if buf is in cur first */
6248 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6254 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6255 XFS_BMAP_BTREE_REF)))
6257 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6258 XFS_WANT_CORRUPTED_GOTO(
6259 XFS_BMAP_SANITY_CHECK(mp, block, level),
6265 * Check this block for basic sanity (increasing keys and
6266 * no duplicate blocks).
6269 xfs_check_block(block, mp, 0, 0);
6270 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
6271 1, mp->m_bmap_dmxr[1]);
6272 bno = be64_to_cpu(*pp);
6273 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
6276 xfs_trans_brelse(NULL, bp);
6281 * Here with bp and block set to the leftmost leaf node in the tree.
6286 * Loop over all leaf nodes checking that all extents are in the right order.
6290 xfs_fsblock_t nextbno;
6291 xfs_extnum_t num_recs;
6294 num_recs = be16_to_cpu(block->bb_numrecs);
6297 * Read-ahead the next leaf block, if any.
6300 nextbno = be64_to_cpu(block->bb_rightsib);
6303 * Check all the extents to make sure they are OK.
6304 * If we had a previous block, the last entry should
6305 * conform with the first entry in this one.
6308 ep = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
6309 block, 1, mp->m_bmap_dmxr[0]);
6310 for (j = 1; j < num_recs; j++) {
6311 nextp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
6312 block, j + 1, mp->m_bmap_dmxr[0]);
6314 xfs_btree_check_rec(XFS_BTNUM_BMAP,
6315 (void *)lastp, (void *)ep);
6317 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
6326 xfs_trans_brelse(NULL, bp);
6330 * If we've reached the end, stop.
6332 if (bno == NULLFSBLOCK)
6335 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6341 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6342 XFS_BMAP_BTREE_REF)))
6344 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6348 xfs_trans_brelse(NULL, bp);
6353 cmn_err(CE_WARN, "%s: at error0", __FUNCTION__);
6355 xfs_trans_brelse(NULL, bp);
6357 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
6359 panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__);
6365 * Count fsblocks of the given fork.
6368 xfs_bmap_count_blocks(
6369 xfs_trans_t *tp, /* transaction pointer */
6370 xfs_inode_t *ip, /* incore inode */
6371 int whichfork, /* data or attr fork */
6372 int *count) /* out: count of blocks */
6374 xfs_bmbt_block_t *block; /* current btree block */
6375 xfs_fsblock_t bno; /* block # of "block" */
6376 xfs_ifork_t *ifp; /* fork structure */
6377 int level; /* btree level, for checking */
6378 xfs_mount_t *mp; /* file system mount structure */
6379 __be64 *pp; /* pointer to block address */
6383 ifp = XFS_IFORK_PTR(ip, whichfork);
6384 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6385 if (unlikely(xfs_bmap_count_leaves(ifp, 0,
6386 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6388 XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)",
6389 XFS_ERRLEVEL_LOW, mp);
6390 return XFS_ERROR(EFSCORRUPTED);
6396 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6398 block = ifp->if_broot;
6399 level = be16_to_cpu(block->bb_level);
6401 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6402 bno = be64_to_cpu(*pp);
6403 ASSERT(bno != NULLDFSBNO);
6404 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6405 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6407 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
6408 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6410 return XFS_ERROR(EFSCORRUPTED);
6417 * Recursively walks each level of a btree
6418 * to count total fsblocks is use.
6421 xfs_bmap_count_tree(
6422 xfs_mount_t *mp, /* file system mount point */
6423 xfs_trans_t *tp, /* transaction pointer */
6424 xfs_ifork_t *ifp, /* inode fork pointer */
6425 xfs_fsblock_t blockno, /* file system block number */
6426 int levelin, /* level in btree */
6427 int *count) /* Count of blocks */
6430 xfs_buf_t *bp, *nbp;
6431 int level = levelin;
6433 xfs_fsblock_t bno = blockno;
6434 xfs_fsblock_t nextbno;
6435 xfs_bmbt_block_t *block, *nextblock;
6438 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6441 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6444 /* Not at node above leafs, count this level of nodes */
6445 nextbno = be64_to_cpu(block->bb_rightsib);
6446 while (nextbno != NULLFSBLOCK) {
6447 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6448 0, &nbp, XFS_BMAP_BTREE_REF)))
6451 nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
6452 nextbno = be64_to_cpu(nextblock->bb_rightsib);
6453 xfs_trans_brelse(tp, nbp);
6456 /* Dive to the next level */
6457 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6458 xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
6459 bno = be64_to_cpu(*pp);
6460 if (unlikely((error =
6461 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6462 xfs_trans_brelse(tp, bp);
6463 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6464 XFS_ERRLEVEL_LOW, mp);
6465 return XFS_ERROR(EFSCORRUPTED);
6467 xfs_trans_brelse(tp, bp);
6469 /* count all level 1 nodes and their leaves */
6471 nextbno = be64_to_cpu(block->bb_rightsib);
6472 numrecs = be16_to_cpu(block->bb_numrecs);
6473 if (unlikely(xfs_bmap_disk_count_leaves(ifp, mp,
6474 0, block, numrecs, count) < 0)) {
6475 xfs_trans_brelse(tp, bp);
6476 XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
6477 XFS_ERRLEVEL_LOW, mp);
6478 return XFS_ERROR(EFSCORRUPTED);
6480 xfs_trans_brelse(tp, bp);
6481 if (nextbno == NULLFSBLOCK)
6484 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6485 XFS_BMAP_BTREE_REF)))
6488 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6495 * Count leaf blocks given a range of extent records.
6498 xfs_bmap_count_leaves(
6505 xfs_bmbt_rec_t *frp;
6507 for (b = 0; b < numrecs; b++) {
6508 frp = xfs_iext_get_ext(ifp, idx + b);
6509 *count += xfs_bmbt_get_blockcount(frp);
6515 * Count leaf blocks given a range of extent records originally
6519 xfs_bmap_disk_count_leaves(
6523 xfs_bmbt_block_t *block,
6528 xfs_bmbt_rec_t *frp;
6530 for (b = 1; b <= numrecs; b++) {
6531 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize,
6532 xfs_bmbt, block, idx + b, mp->m_bmap_dmxr[0]);
6533 *count += xfs_bmbt_disk_get_blockcount(frp);