2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_alloc_btree.h"
31 #include "xfs_ialloc_btree.h"
32 #include "xfs_dir2_sf.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_dinode.h"
35 #include "xfs_inode.h"
36 #include "xfs_btree.h"
37 #include "xfs_dmapi.h"
38 #include "xfs_mount.h"
39 #include "xfs_ialloc.h"
40 #include "xfs_itable.h"
41 #include "xfs_dir2_data.h"
42 #include "xfs_dir2_leaf.h"
43 #include "xfs_dir2_block.h"
44 #include "xfs_inode_item.h"
45 #include "xfs_extfree_item.h"
46 #include "xfs_alloc.h"
48 #include "xfs_rtalloc.h"
49 #include "xfs_error.h"
50 #include "xfs_attr_leaf.h"
52 #include "xfs_quota.h"
53 #include "xfs_trans_space.h"
54 #include "xfs_buf_item.h"
55 #include "xfs_filestream.h"
60 xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
63 kmem_zone_t *xfs_bmap_free_item_zone;
66 * Prototypes for internal bmap routines.
71 * Called from xfs_bmap_add_attrfork to handle extents format files.
73 STATIC int /* error */
74 xfs_bmap_add_attrfork_extents(
75 xfs_trans_t *tp, /* transaction pointer */
76 xfs_inode_t *ip, /* incore inode pointer */
77 xfs_fsblock_t *firstblock, /* first block allocated */
78 xfs_bmap_free_t *flist, /* blocks to free at commit */
79 int *flags); /* inode logging flags */
82 * Called from xfs_bmap_add_attrfork to handle local format files.
84 STATIC int /* error */
85 xfs_bmap_add_attrfork_local(
86 xfs_trans_t *tp, /* transaction pointer */
87 xfs_inode_t *ip, /* incore inode pointer */
88 xfs_fsblock_t *firstblock, /* first block allocated */
89 xfs_bmap_free_t *flist, /* blocks to free at commit */
90 int *flags); /* inode logging flags */
93 * Called by xfs_bmapi to update file extent records and the btree
94 * after allocating space (or doing a delayed allocation).
96 STATIC int /* error */
98 xfs_inode_t *ip, /* incore inode pointer */
99 xfs_extnum_t idx, /* extent number to update/insert */
100 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
101 xfs_bmbt_irec_t *new, /* new data to add to file extents */
102 xfs_fsblock_t *first, /* pointer to firstblock variable */
103 xfs_bmap_free_t *flist, /* list of extents to be freed */
104 int *logflagsp, /* inode logging flags */
105 xfs_extdelta_t *delta, /* Change made to incore extents */
106 int whichfork, /* data or attr fork */
107 int rsvd); /* OK to allocate reserved blocks */
110 * Called by xfs_bmap_add_extent to handle cases converting a delayed
111 * allocation to a real allocation.
113 STATIC int /* error */
114 xfs_bmap_add_extent_delay_real(
115 xfs_inode_t *ip, /* incore inode pointer */
116 xfs_extnum_t idx, /* extent number to update/insert */
117 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
118 xfs_bmbt_irec_t *new, /* new data to add to file extents */
119 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
120 xfs_fsblock_t *first, /* pointer to firstblock variable */
121 xfs_bmap_free_t *flist, /* list of extents to be freed */
122 int *logflagsp, /* inode logging flags */
123 xfs_extdelta_t *delta, /* Change made to incore extents */
124 int rsvd); /* OK to allocate reserved blocks */
127 * Called by xfs_bmap_add_extent to handle cases converting a hole
128 * to a delayed allocation.
130 STATIC int /* error */
131 xfs_bmap_add_extent_hole_delay(
132 xfs_inode_t *ip, /* incore inode pointer */
133 xfs_extnum_t idx, /* extent number to update/insert */
134 xfs_bmbt_irec_t *new, /* new data to add to file extents */
135 int *logflagsp,/* inode logging flags */
136 xfs_extdelta_t *delta, /* Change made to incore extents */
137 int rsvd); /* OK to allocate reserved blocks */
140 * Called by xfs_bmap_add_extent to handle cases converting a hole
141 * to a real allocation.
143 STATIC int /* error */
144 xfs_bmap_add_extent_hole_real(
145 xfs_inode_t *ip, /* incore inode pointer */
146 xfs_extnum_t idx, /* extent number to update/insert */
147 xfs_btree_cur_t *cur, /* if null, not a btree */
148 xfs_bmbt_irec_t *new, /* new data to add to file extents */
149 int *logflagsp, /* inode logging flags */
150 xfs_extdelta_t *delta, /* Change made to incore extents */
151 int whichfork); /* data or attr fork */
154 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
155 * allocation to a real allocation or vice versa.
157 STATIC int /* error */
158 xfs_bmap_add_extent_unwritten_real(
159 xfs_inode_t *ip, /* incore inode pointer */
160 xfs_extnum_t idx, /* extent number to update/insert */
161 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
162 xfs_bmbt_irec_t *new, /* new data to add to file extents */
163 int *logflagsp, /* inode logging flags */
164 xfs_extdelta_t *delta); /* Change made to incore extents */
167 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
168 * It figures out where to ask the underlying allocator to put the new extent.
170 STATIC int /* error */
172 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
175 * Transform a btree format file with only one leaf node, where the
176 * extents list will fit in the inode, into an extents format file.
177 * Since the file extents are already in-core, all we have to do is
178 * give up the space for the btree root and pitch the leaf block.
180 STATIC int /* error */
181 xfs_bmap_btree_to_extents(
182 xfs_trans_t *tp, /* transaction pointer */
183 xfs_inode_t *ip, /* incore inode pointer */
184 xfs_btree_cur_t *cur, /* btree cursor */
185 int *logflagsp, /* inode logging flags */
186 int whichfork); /* data or attr fork */
189 * Called by xfs_bmapi to update file extent records and the btree
190 * after removing space (or undoing a delayed allocation).
192 STATIC int /* error */
194 xfs_inode_t *ip, /* incore inode pointer */
195 xfs_trans_t *tp, /* current trans pointer */
196 xfs_extnum_t idx, /* extent number to update/insert */
197 xfs_bmap_free_t *flist, /* list of extents to be freed */
198 xfs_btree_cur_t *cur, /* if null, not a btree */
199 xfs_bmbt_irec_t *new, /* new data to add to file extents */
200 int *logflagsp,/* inode logging flags */
201 xfs_extdelta_t *delta, /* Change made to incore extents */
202 int whichfork, /* data or attr fork */
203 int rsvd); /* OK to allocate reserved blocks */
206 * Remove the entry "free" from the free item list. Prev points to the
207 * previous entry, unless "free" is the head of the list.
211 xfs_bmap_free_t *flist, /* free item list header */
212 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
213 xfs_bmap_free_item_t *free); /* list item to be freed */
216 * Convert an extents-format file into a btree-format file.
217 * The new file will have a root block (in the inode) and a single child block.
219 STATIC int /* error */
220 xfs_bmap_extents_to_btree(
221 xfs_trans_t *tp, /* transaction pointer */
222 xfs_inode_t *ip, /* incore inode pointer */
223 xfs_fsblock_t *firstblock, /* first-block-allocated */
224 xfs_bmap_free_t *flist, /* blocks freed in xaction */
225 xfs_btree_cur_t **curp, /* cursor returned to caller */
226 int wasdel, /* converting a delayed alloc */
227 int *logflagsp, /* inode logging flags */
228 int whichfork); /* data or attr fork */
231 * Convert a local file to an extents file.
232 * This code is sort of bogus, since the file data needs to get
233 * logged so it won't be lost. The bmap-level manipulations are ok, though.
235 STATIC int /* error */
236 xfs_bmap_local_to_extents(
237 xfs_trans_t *tp, /* transaction pointer */
238 xfs_inode_t *ip, /* incore inode pointer */
239 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
240 xfs_extlen_t total, /* total blocks needed by transaction */
241 int *logflagsp, /* inode logging flags */
242 int whichfork); /* data or attr fork */
245 * Search the extents list for the inode, for the extent containing bno.
246 * If bno lies in a hole, point to the next entry. If bno lies past eof,
247 * *eofp will be set, and *prevp will contain the last entry (null if none).
248 * Else, *lastxp will be set to the index of the found
249 * entry; *gotp will contain the entry.
251 STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
252 xfs_bmap_search_extents(
253 xfs_inode_t *ip, /* incore inode pointer */
254 xfs_fileoff_t bno, /* block number searched for */
255 int whichfork, /* data or attr fork */
256 int *eofp, /* out: end of file found */
257 xfs_extnum_t *lastxp, /* out: last extent index */
258 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
259 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
262 * Check the last inode extent to determine whether this allocation will result
263 * in blocks being allocated at the end of the file. When we allocate new data
264 * blocks at the end of the file which do not start at the previous data block,
265 * we will try to align the new blocks at stripe unit boundaries.
267 STATIC int /* error */
269 xfs_inode_t *ip, /* incore inode pointer */
270 xfs_fileoff_t off, /* file offset in fsblocks */
271 int whichfork, /* data or attribute fork */
272 char *aeof); /* return value */
274 #ifdef XFS_BMAP_TRACE
276 * Add a bmap trace buffer entry. Base routine for the others.
279 xfs_bmap_trace_addentry(
280 int opcode, /* operation */
281 const char *fname, /* function name */
282 char *desc, /* operation description */
283 xfs_inode_t *ip, /* incore inode pointer */
284 xfs_extnum_t idx, /* index of entry(ies) */
285 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
286 xfs_bmbt_rec_t *r1, /* first record */
287 xfs_bmbt_rec_t *r2, /* second record or null */
288 int whichfork); /* data or attr fork */
291 * Add bmap trace entry prior to a call to xfs_iext_remove.
294 xfs_bmap_trace_delete(
295 const char *fname, /* function name */
296 char *desc, /* operation description */
297 xfs_inode_t *ip, /* incore inode pointer */
298 xfs_extnum_t idx, /* index of entry(entries) deleted */
299 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
300 int whichfork); /* data or attr fork */
303 * Add bmap trace entry prior to a call to xfs_iext_insert, or
304 * reading in the extents list from the disk (in the btree).
307 xfs_bmap_trace_insert(
308 const char *fname, /* function name */
309 char *desc, /* operation description */
310 xfs_inode_t *ip, /* incore inode pointer */
311 xfs_extnum_t idx, /* index of entry(entries) inserted */
312 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
313 xfs_bmbt_irec_t *r1, /* inserted record 1 */
314 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
315 int whichfork); /* data or attr fork */
318 * Add bmap trace entry after updating an extent record in place.
321 xfs_bmap_trace_post_update(
322 const char *fname, /* function name */
323 char *desc, /* operation description */
324 xfs_inode_t *ip, /* incore inode pointer */
325 xfs_extnum_t idx, /* index of entry updated */
326 int whichfork); /* data or attr fork */
329 * Add bmap trace entry prior to updating an extent record in place.
332 xfs_bmap_trace_pre_update(
333 const char *fname, /* function name */
334 char *desc, /* operation description */
335 xfs_inode_t *ip, /* incore inode pointer */
336 xfs_extnum_t idx, /* index of entry to be updated */
337 int whichfork); /* data or attr fork */
339 #define XFS_BMAP_TRACE_DELETE(d,ip,i,c,w) \
340 xfs_bmap_trace_delete(__FUNCTION__,d,ip,i,c,w)
341 #define XFS_BMAP_TRACE_INSERT(d,ip,i,c,r1,r2,w) \
342 xfs_bmap_trace_insert(__FUNCTION__,d,ip,i,c,r1,r2,w)
343 #define XFS_BMAP_TRACE_POST_UPDATE(d,ip,i,w) \
344 xfs_bmap_trace_post_update(__FUNCTION__,d,ip,i,w)
345 #define XFS_BMAP_TRACE_PRE_UPDATE(d,ip,i,w) \
346 xfs_bmap_trace_pre_update(__FUNCTION__,d,ip,i,w)
348 #define XFS_BMAP_TRACE_DELETE(d,ip,i,c,w)
349 #define XFS_BMAP_TRACE_INSERT(d,ip,i,c,r1,r2,w)
350 #define XFS_BMAP_TRACE_POST_UPDATE(d,ip,i,w)
351 #define XFS_BMAP_TRACE_PRE_UPDATE(d,ip,i,w)
352 #endif /* XFS_BMAP_TRACE */
355 * Compute the worst-case number of indirect blocks that will be used
356 * for ip's delayed extent of length "len".
359 xfs_bmap_worst_indlen(
360 xfs_inode_t *ip, /* incore inode pointer */
361 xfs_filblks_t len); /* delayed extent length */
365 * Perform various validation checks on the values being returned
369 xfs_bmap_validate_ret(
373 xfs_bmbt_irec_t *mval,
377 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
380 #if defined(XFS_RW_TRACE)
389 #define xfs_bunmap_trace(ip, bno, len, flags, ra)
390 #endif /* XFS_RW_TRACE */
397 xfs_fsblock_t blockno,
402 xfs_bmap_count_leaves(
409 xfs_bmap_disk_count_leaves(
411 xfs_bmbt_block_t *block,
416 * Bmap internal routines.
420 * Called from xfs_bmap_add_attrfork to handle btree format files.
422 STATIC int /* error */
423 xfs_bmap_add_attrfork_btree(
424 xfs_trans_t *tp, /* transaction pointer */
425 xfs_inode_t *ip, /* incore inode pointer */
426 xfs_fsblock_t *firstblock, /* first block allocated */
427 xfs_bmap_free_t *flist, /* blocks to free at commit */
428 int *flags) /* inode logging flags */
430 xfs_btree_cur_t *cur; /* btree cursor */
431 int error; /* error return value */
432 xfs_mount_t *mp; /* file system mount struct */
433 int stat; /* newroot status */
436 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
437 *flags |= XFS_ILOG_DBROOT;
439 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
441 cur->bc_private.b.flist = flist;
442 cur->bc_private.b.firstblock = *firstblock;
443 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
445 ASSERT(stat == 1); /* must be at least one entry */
446 if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
449 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
450 return XFS_ERROR(ENOSPC);
452 *firstblock = cur->bc_private.b.firstblock;
453 cur->bc_private.b.allocated = 0;
454 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
458 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
463 * Called from xfs_bmap_add_attrfork to handle extents format files.
465 STATIC int /* error */
466 xfs_bmap_add_attrfork_extents(
467 xfs_trans_t *tp, /* transaction pointer */
468 xfs_inode_t *ip, /* incore inode pointer */
469 xfs_fsblock_t *firstblock, /* first block allocated */
470 xfs_bmap_free_t *flist, /* blocks to free at commit */
471 int *flags) /* inode logging flags */
473 xfs_btree_cur_t *cur; /* bmap btree cursor */
474 int error; /* error return value */
476 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
479 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
480 flags, XFS_DATA_FORK);
482 cur->bc_private.b.allocated = 0;
483 xfs_btree_del_cursor(cur,
484 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
490 * Called from xfs_bmap_add_attrfork to handle local format files.
492 STATIC int /* error */
493 xfs_bmap_add_attrfork_local(
494 xfs_trans_t *tp, /* transaction pointer */
495 xfs_inode_t *ip, /* incore inode pointer */
496 xfs_fsblock_t *firstblock, /* first block allocated */
497 xfs_bmap_free_t *flist, /* blocks to free at commit */
498 int *flags) /* inode logging flags */
500 xfs_da_args_t dargs; /* args for dir/attr code */
501 int error; /* error return value */
502 xfs_mount_t *mp; /* mount structure pointer */
504 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
506 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
508 memset(&dargs, 0, sizeof(dargs));
510 dargs.firstblock = firstblock;
512 dargs.total = mp->m_dirblkfsbs;
513 dargs.whichfork = XFS_DATA_FORK;
515 error = xfs_dir2_sf_to_block(&dargs);
517 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
523 * Called by xfs_bmapi to update file extent records and the btree
524 * after allocating space (or doing a delayed allocation).
526 STATIC int /* error */
528 xfs_inode_t *ip, /* incore inode pointer */
529 xfs_extnum_t idx, /* extent number to update/insert */
530 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
531 xfs_bmbt_irec_t *new, /* new data to add to file extents */
532 xfs_fsblock_t *first, /* pointer to firstblock variable */
533 xfs_bmap_free_t *flist, /* list of extents to be freed */
534 int *logflagsp, /* inode logging flags */
535 xfs_extdelta_t *delta, /* Change made to incore extents */
536 int whichfork, /* data or attr fork */
537 int rsvd) /* OK to use reserved data blocks */
539 xfs_btree_cur_t *cur; /* btree cursor or null */
540 xfs_filblks_t da_new; /* new count del alloc blocks used */
541 xfs_filblks_t da_old; /* old count del alloc blocks used */
542 int error; /* error return value */
543 xfs_ifork_t *ifp; /* inode fork ptr */
544 int logflags; /* returned value */
545 xfs_extnum_t nextents; /* number of extents in file now */
547 XFS_STATS_INC(xs_add_exlist);
549 ifp = XFS_IFORK_PTR(ip, whichfork);
550 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
551 ASSERT(idx <= nextents);
555 * This is the first extent added to a new/empty file.
556 * Special case this one, so other routines get to assume there are
557 * already extents in the list.
560 XFS_BMAP_TRACE_INSERT("insert empty", ip, 0, 1, new, NULL,
562 xfs_iext_insert(ifp, 0, 1, new);
565 if (!ISNULLSTARTBLOCK(new->br_startblock)) {
566 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
567 logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
570 /* DELTA: single new extent */
572 if (delta->xed_startoff > new->br_startoff)
573 delta->xed_startoff = new->br_startoff;
574 if (delta->xed_blockcount <
575 new->br_startoff + new->br_blockcount)
576 delta->xed_blockcount = new->br_startoff +
581 * Any kind of new delayed allocation goes here.
583 else if (ISNULLSTARTBLOCK(new->br_startblock)) {
585 ASSERT((cur->bc_private.b.flags &
586 XFS_BTCUR_BPRV_WASDEL) == 0);
587 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
588 &logflags, delta, rsvd)))
592 * Real allocation off the end of the file.
594 else if (idx == nextents) {
596 ASSERT((cur->bc_private.b.flags &
597 XFS_BTCUR_BPRV_WASDEL) == 0);
598 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
599 &logflags, delta, whichfork)))
602 xfs_bmbt_irec_t prev; /* old extent at offset idx */
605 * Get the record referred to by idx.
607 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
609 * If it's a real allocation record, and the new allocation ends
610 * after the start of the referred to record, then we're filling
611 * in a delayed or unwritten allocation with a real one, or
612 * converting real back to unwritten.
614 if (!ISNULLSTARTBLOCK(new->br_startblock) &&
615 new->br_startoff + new->br_blockcount > prev.br_startoff) {
616 if (prev.br_state != XFS_EXT_UNWRITTEN &&
617 ISNULLSTARTBLOCK(prev.br_startblock)) {
618 da_old = STARTBLOCKVAL(prev.br_startblock);
620 ASSERT(cur->bc_private.b.flags &
621 XFS_BTCUR_BPRV_WASDEL);
622 if ((error = xfs_bmap_add_extent_delay_real(ip,
623 idx, &cur, new, &da_new, first, flist,
624 &logflags, delta, rsvd)))
626 } else if (new->br_state == XFS_EXT_NORM) {
627 ASSERT(new->br_state == XFS_EXT_NORM);
628 if ((error = xfs_bmap_add_extent_unwritten_real(
629 ip, idx, &cur, new, &logflags, delta)))
632 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
633 if ((error = xfs_bmap_add_extent_unwritten_real(
634 ip, idx, &cur, new, &logflags, delta)))
637 ASSERT(*curp == cur || *curp == NULL);
640 * Otherwise we're filling in a hole with an allocation.
644 ASSERT((cur->bc_private.b.flags &
645 XFS_BTCUR_BPRV_WASDEL) == 0);
646 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
647 new, &logflags, delta, whichfork)))
652 ASSERT(*curp == cur || *curp == NULL);
654 * Convert to a btree if necessary.
656 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
657 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
658 int tmp_logflags; /* partial log flag return val */
661 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
662 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
663 logflags |= tmp_logflags;
668 * Adjust for changes in reserved delayed indirect blocks.
669 * Nothing to do for disk quotas here.
671 if (da_old || da_new) {
676 nblks += cur->bc_private.b.allocated;
677 ASSERT(nblks <= da_old);
679 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
680 (int64_t)(da_old - nblks), rsvd);
683 * Clear out the allocated field, done with it now in any case.
686 cur->bc_private.b.allocated = 0;
692 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
694 *logflagsp = logflags;
699 * Called by xfs_bmap_add_extent to handle cases converting a delayed
700 * allocation to a real allocation.
702 STATIC int /* error */
703 xfs_bmap_add_extent_delay_real(
704 xfs_inode_t *ip, /* incore inode pointer */
705 xfs_extnum_t idx, /* extent number to update/insert */
706 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
707 xfs_bmbt_irec_t *new, /* new data to add to file extents */
708 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
709 xfs_fsblock_t *first, /* pointer to firstblock variable */
710 xfs_bmap_free_t *flist, /* list of extents to be freed */
711 int *logflagsp, /* inode logging flags */
712 xfs_extdelta_t *delta, /* Change made to incore extents */
713 int rsvd) /* OK to use reserved data block allocation */
715 xfs_btree_cur_t *cur; /* btree cursor */
716 int diff; /* temp value */
717 xfs_bmbt_rec_t *ep; /* extent entry for idx */
718 int error; /* error return value */
719 int i; /* temp state */
720 xfs_ifork_t *ifp; /* inode fork pointer */
721 xfs_fileoff_t new_endoff; /* end offset of new entry */
722 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
723 /* left is 0, right is 1, prev is 2 */
724 int rval=0; /* return value (logging flags) */
725 int state = 0;/* state bits, accessed thru macros */
726 xfs_filblks_t temp=0; /* value for dnew calculations */
727 xfs_filblks_t temp2=0;/* value for dnew calculations */
728 int tmp_rval; /* partial logging flags */
729 enum { /* bit number definitions for state */
730 LEFT_CONTIG, RIGHT_CONTIG,
731 LEFT_FILLING, RIGHT_FILLING,
732 LEFT_DELAY, RIGHT_DELAY,
733 LEFT_VALID, RIGHT_VALID
739 #define MASK(b) (1 << (b))
740 #define MASK2(a,b) (MASK(a) | MASK(b))
741 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
742 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
743 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
744 #define STATE_TEST(b) (state & MASK(b))
745 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
746 ((state &= ~MASK(b)), 0))
747 #define SWITCH_STATE \
748 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
751 * Set up a bunch of variables to make the tests simpler.
754 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
755 ep = xfs_iext_get_ext(ifp, idx);
756 xfs_bmbt_get_all(ep, &PREV);
757 new_endoff = new->br_startoff + new->br_blockcount;
758 ASSERT(PREV.br_startoff <= new->br_startoff);
759 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
761 * Set flags determining what part of the previous delayed allocation
762 * extent is being replaced by a real allocation.
764 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
765 STATE_SET(RIGHT_FILLING,
766 PREV.br_startoff + PREV.br_blockcount == new_endoff);
768 * Check and set flags if this segment has a left neighbor.
769 * Don't set contiguous if the combined extent would be too large.
771 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
772 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
773 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
775 STATE_SET(LEFT_CONTIG,
776 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
777 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
778 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
779 LEFT.br_state == new->br_state &&
780 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
782 * Check and set flags if this segment has a right neighbor.
783 * Don't set contiguous if the combined extent would be too large.
784 * Also check for all-three-contiguous being too large.
786 if (STATE_SET_TEST(RIGHT_VALID,
788 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
789 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
790 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
792 STATE_SET(RIGHT_CONTIG,
793 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
794 new_endoff == RIGHT.br_startoff &&
795 new->br_startblock + new->br_blockcount ==
796 RIGHT.br_startblock &&
797 new->br_state == RIGHT.br_state &&
798 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
799 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
800 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
801 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
805 * Switch out based on the FILLING and CONTIG state bits.
807 switch (SWITCH_STATE) {
809 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
811 * Filling in all of a previously delayed allocation extent.
812 * The left and right neighbors are both contiguous with new.
814 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
816 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
817 LEFT.br_blockcount + PREV.br_blockcount +
818 RIGHT.br_blockcount);
819 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
821 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
822 xfs_iext_remove(ifp, idx, 2);
823 ip->i_df.if_lastex = idx - 1;
824 ip->i_d.di_nextents--;
826 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
828 rval = XFS_ILOG_CORE;
829 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
831 RIGHT.br_blockcount, &i)))
834 if ((error = xfs_bmbt_delete(cur, &i)))
837 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
840 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
844 RIGHT.br_blockcount, LEFT.br_state)))
848 /* DELTA: Three in-core extents are replaced by one. */
849 temp = LEFT.br_startoff;
850 temp2 = LEFT.br_blockcount +
855 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
857 * Filling in all of a previously delayed allocation extent.
858 * The left neighbor is contiguous, the right is not.
860 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
862 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
863 LEFT.br_blockcount + PREV.br_blockcount);
864 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
866 ip->i_df.if_lastex = idx - 1;
867 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
868 xfs_iext_remove(ifp, idx, 1);
870 rval = XFS_ILOG_DEXT;
873 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
874 LEFT.br_startblock, LEFT.br_blockcount,
878 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
881 PREV.br_blockcount, LEFT.br_state)))
885 /* DELTA: Two in-core extents are replaced by one. */
886 temp = LEFT.br_startoff;
887 temp2 = LEFT.br_blockcount +
891 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
893 * Filling in all of a previously delayed allocation extent.
894 * The right neighbor is contiguous, the left is not.
896 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
897 xfs_bmbt_set_startblock(ep, new->br_startblock);
898 xfs_bmbt_set_blockcount(ep,
899 PREV.br_blockcount + RIGHT.br_blockcount);
900 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
901 ip->i_df.if_lastex = idx;
902 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
903 xfs_iext_remove(ifp, idx + 1, 1);
905 rval = XFS_ILOG_DEXT;
908 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
910 RIGHT.br_blockcount, &i)))
913 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
916 RIGHT.br_blockcount, PREV.br_state)))
920 /* DELTA: Two in-core extents are replaced by one. */
921 temp = PREV.br_startoff;
922 temp2 = PREV.br_blockcount +
926 case MASK2(LEFT_FILLING, RIGHT_FILLING):
928 * Filling in all of a previously delayed allocation extent.
929 * Neither the left nor right neighbors are contiguous with
932 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
933 xfs_bmbt_set_startblock(ep, new->br_startblock);
934 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
935 ip->i_df.if_lastex = idx;
936 ip->i_d.di_nextents++;
938 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
940 rval = XFS_ILOG_CORE;
941 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
942 new->br_startblock, new->br_blockcount,
946 cur->bc_rec.b.br_state = XFS_EXT_NORM;
947 if ((error = xfs_bmbt_insert(cur, &i)))
952 /* DELTA: The in-core extent described by new changed type. */
953 temp = new->br_startoff;
954 temp2 = new->br_blockcount;
957 case MASK2(LEFT_FILLING, LEFT_CONTIG):
959 * Filling in the first part of a previous delayed allocation.
960 * The left neighbor is contiguous.
962 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
963 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
964 LEFT.br_blockcount + new->br_blockcount);
965 xfs_bmbt_set_startoff(ep,
966 PREV.br_startoff + new->br_blockcount);
967 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
968 temp = PREV.br_blockcount - new->br_blockcount;
969 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
970 xfs_bmbt_set_blockcount(ep, temp);
971 ip->i_df.if_lastex = idx - 1;
973 rval = XFS_ILOG_DEXT;
976 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
977 LEFT.br_startblock, LEFT.br_blockcount,
981 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
988 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
989 STARTBLOCKVAL(PREV.br_startblock));
990 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
991 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
993 /* DELTA: The boundary between two in-core extents moved. */
994 temp = LEFT.br_startoff;
995 temp2 = LEFT.br_blockcount +
999 case MASK(LEFT_FILLING):
1001 * Filling in the first part of a previous delayed allocation.
1002 * The left neighbor is not contiguous.
1004 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1005 xfs_bmbt_set_startoff(ep, new_endoff);
1006 temp = PREV.br_blockcount - new->br_blockcount;
1007 xfs_bmbt_set_blockcount(ep, temp);
1008 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
1010 xfs_iext_insert(ifp, idx, 1, new);
1011 ip->i_df.if_lastex = idx;
1012 ip->i_d.di_nextents++;
1014 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1016 rval = XFS_ILOG_CORE;
1017 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1018 new->br_startblock, new->br_blockcount,
1022 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1023 if ((error = xfs_bmbt_insert(cur, &i)))
1027 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1028 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1029 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1030 first, flist, &cur, 1, &tmp_rval,
1036 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1037 STARTBLOCKVAL(PREV.br_startblock) -
1038 (cur ? cur->bc_private.b.allocated : 0));
1039 ep = xfs_iext_get_ext(ifp, idx + 1);
1040 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1041 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx + 1, XFS_DATA_FORK);
1043 /* DELTA: One in-core extent is split in two. */
1044 temp = PREV.br_startoff;
1045 temp2 = PREV.br_blockcount;
1048 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1050 * Filling in the last part of a previous delayed allocation.
1051 * The right neighbor is contiguous with the new allocation.
1053 temp = PREV.br_blockcount - new->br_blockcount;
1054 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
1055 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
1056 xfs_bmbt_set_blockcount(ep, temp);
1057 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1058 new->br_startoff, new->br_startblock,
1059 new->br_blockcount + RIGHT.br_blockcount,
1061 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
1062 ip->i_df.if_lastex = idx + 1;
1064 rval = XFS_ILOG_DEXT;
1067 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1068 RIGHT.br_startblock,
1069 RIGHT.br_blockcount, &i)))
1072 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1074 new->br_blockcount +
1075 RIGHT.br_blockcount,
1079 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1080 STARTBLOCKVAL(PREV.br_startblock));
1081 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1082 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
1084 /* DELTA: The boundary between two in-core extents moved. */
1085 temp = PREV.br_startoff;
1086 temp2 = PREV.br_blockcount +
1087 RIGHT.br_blockcount;
1090 case MASK(RIGHT_FILLING):
1092 * Filling in the last part of a previous delayed allocation.
1093 * The right neighbor is not contiguous.
1095 temp = PREV.br_blockcount - new->br_blockcount;
1096 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1097 xfs_bmbt_set_blockcount(ep, temp);
1098 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1100 xfs_iext_insert(ifp, idx + 1, 1, new);
1101 ip->i_df.if_lastex = idx + 1;
1102 ip->i_d.di_nextents++;
1104 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1106 rval = XFS_ILOG_CORE;
1107 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1108 new->br_startblock, new->br_blockcount,
1112 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1113 if ((error = xfs_bmbt_insert(cur, &i)))
1117 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1118 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1119 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1120 first, flist, &cur, 1, &tmp_rval,
1126 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1127 STARTBLOCKVAL(PREV.br_startblock) -
1128 (cur ? cur->bc_private.b.allocated : 0));
1129 ep = xfs_iext_get_ext(ifp, idx);
1130 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1131 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1133 /* DELTA: One in-core extent is split in two. */
1134 temp = PREV.br_startoff;
1135 temp2 = PREV.br_blockcount;
1140 * Filling in the middle part of a previous delayed allocation.
1141 * Contiguity is impossible here.
1142 * This case is avoided almost all the time.
1144 temp = new->br_startoff - PREV.br_startoff;
1145 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
1146 xfs_bmbt_set_blockcount(ep, temp);
1148 r[1].br_state = PREV.br_state;
1149 r[1].br_startblock = 0;
1150 r[1].br_startoff = new_endoff;
1151 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1152 r[1].br_blockcount = temp2;
1153 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
1155 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1156 ip->i_df.if_lastex = idx + 1;
1157 ip->i_d.di_nextents++;
1159 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1161 rval = XFS_ILOG_CORE;
1162 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1163 new->br_startblock, new->br_blockcount,
1167 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1168 if ((error = xfs_bmbt_insert(cur, &i)))
1172 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1173 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1174 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1175 first, flist, &cur, 1, &tmp_rval,
1181 temp = xfs_bmap_worst_indlen(ip, temp);
1182 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1183 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
1184 (cur ? cur->bc_private.b.allocated : 0));
1186 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
1188 * Ick gross gag me with a spoon.
1190 ASSERT(0); /* want to see if this ever happens! */
1196 !xfs_mod_incore_sb(ip->i_mount,
1197 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1204 !xfs_mod_incore_sb(ip->i_mount,
1205 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1210 ep = xfs_iext_get_ext(ifp, idx);
1211 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1212 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
1213 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
1214 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1215 NULLSTARTBLOCK((int)temp2));
1216 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
1217 *dnew = temp + temp2;
1218 /* DELTA: One in-core extent is split in three. */
1219 temp = PREV.br_startoff;
1220 temp2 = PREV.br_blockcount;
1223 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1224 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1225 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1226 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1227 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1228 case MASK(LEFT_CONTIG):
1229 case MASK(RIGHT_CONTIG):
1231 * These cases are all impossible.
1238 if (delta->xed_startoff > temp)
1239 delta->xed_startoff = temp;
1240 if (delta->xed_blockcount < temp2)
1241 delta->xed_blockcount = temp2;
1255 #undef STATE_SET_TEST
1260 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1261 * allocation to a real allocation or vice versa.
1263 STATIC int /* error */
1264 xfs_bmap_add_extent_unwritten_real(
1265 xfs_inode_t *ip, /* incore inode pointer */
1266 xfs_extnum_t idx, /* extent number to update/insert */
1267 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1268 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1269 int *logflagsp, /* inode logging flags */
1270 xfs_extdelta_t *delta) /* Change made to incore extents */
1272 xfs_btree_cur_t *cur; /* btree cursor */
1273 xfs_bmbt_rec_t *ep; /* extent entry for idx */
1274 int error; /* error return value */
1275 int i; /* temp state */
1276 xfs_ifork_t *ifp; /* inode fork pointer */
1277 xfs_fileoff_t new_endoff; /* end offset of new entry */
1278 xfs_exntst_t newext; /* new extent state */
1279 xfs_exntst_t oldext; /* old extent state */
1280 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1281 /* left is 0, right is 1, prev is 2 */
1282 int rval=0; /* return value (logging flags) */
1283 int state = 0;/* state bits, accessed thru macros */
1284 xfs_filblks_t temp=0;
1285 xfs_filblks_t temp2=0;
1286 enum { /* bit number definitions for state */
1287 LEFT_CONTIG, RIGHT_CONTIG,
1288 LEFT_FILLING, RIGHT_FILLING,
1289 LEFT_DELAY, RIGHT_DELAY,
1290 LEFT_VALID, RIGHT_VALID
1296 #define MASK(b) (1 << (b))
1297 #define MASK2(a,b) (MASK(a) | MASK(b))
1298 #define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1299 #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1300 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1301 #define STATE_TEST(b) (state & MASK(b))
1302 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1303 ((state &= ~MASK(b)), 0))
1304 #define SWITCH_STATE \
1305 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1308 * Set up a bunch of variables to make the tests simpler.
1312 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1313 ep = xfs_iext_get_ext(ifp, idx);
1314 xfs_bmbt_get_all(ep, &PREV);
1315 newext = new->br_state;
1316 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1317 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1318 ASSERT(PREV.br_state == oldext);
1319 new_endoff = new->br_startoff + new->br_blockcount;
1320 ASSERT(PREV.br_startoff <= new->br_startoff);
1321 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1323 * Set flags determining what part of the previous oldext allocation
1324 * extent is being replaced by a newext allocation.
1326 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
1327 STATE_SET(RIGHT_FILLING,
1328 PREV.br_startoff + PREV.br_blockcount == new_endoff);
1330 * Check and set flags if this segment has a left neighbor.
1331 * Don't set contiguous if the combined extent would be too large.
1333 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1334 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1335 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
1337 STATE_SET(LEFT_CONTIG,
1338 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1339 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1340 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1341 LEFT.br_state == newext &&
1342 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1344 * Check and set flags if this segment has a right neighbor.
1345 * Don't set contiguous if the combined extent would be too large.
1346 * Also check for all-three-contiguous being too large.
1348 if (STATE_SET_TEST(RIGHT_VALID,
1350 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
1351 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1352 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
1354 STATE_SET(RIGHT_CONTIG,
1355 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1356 new_endoff == RIGHT.br_startoff &&
1357 new->br_startblock + new->br_blockcount ==
1358 RIGHT.br_startblock &&
1359 newext == RIGHT.br_state &&
1360 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1361 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
1362 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
1363 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1366 * Switch out based on the FILLING and CONTIG state bits.
1368 switch (SWITCH_STATE) {
1370 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1372 * Setting all of a previous oldext extent to newext.
1373 * The left and right neighbors are both contiguous with new.
1375 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
1377 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1378 LEFT.br_blockcount + PREV.br_blockcount +
1379 RIGHT.br_blockcount);
1380 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
1382 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
1383 xfs_iext_remove(ifp, idx, 2);
1384 ip->i_df.if_lastex = idx - 1;
1385 ip->i_d.di_nextents -= 2;
1387 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1389 rval = XFS_ILOG_CORE;
1390 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1391 RIGHT.br_startblock,
1392 RIGHT.br_blockcount, &i)))
1395 if ((error = xfs_bmbt_delete(cur, &i)))
1398 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1401 if ((error = xfs_bmbt_delete(cur, &i)))
1404 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1407 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1409 LEFT.br_blockcount + PREV.br_blockcount +
1410 RIGHT.br_blockcount, LEFT.br_state)))
1413 /* DELTA: Three in-core extents are replaced by one. */
1414 temp = LEFT.br_startoff;
1415 temp2 = LEFT.br_blockcount +
1416 PREV.br_blockcount +
1417 RIGHT.br_blockcount;
1420 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
1422 * Setting all of a previous oldext extent to newext.
1423 * The left neighbor is contiguous, the right is not.
1425 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
1427 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1428 LEFT.br_blockcount + PREV.br_blockcount);
1429 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
1431 ip->i_df.if_lastex = idx - 1;
1432 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
1433 xfs_iext_remove(ifp, idx, 1);
1434 ip->i_d.di_nextents--;
1436 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1438 rval = XFS_ILOG_CORE;
1439 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1440 PREV.br_startblock, PREV.br_blockcount,
1444 if ((error = xfs_bmbt_delete(cur, &i)))
1447 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1450 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1452 LEFT.br_blockcount + PREV.br_blockcount,
1456 /* DELTA: Two in-core extents are replaced by one. */
1457 temp = LEFT.br_startoff;
1458 temp2 = LEFT.br_blockcount +
1462 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
1464 * Setting all of a previous oldext extent to newext.
1465 * The right neighbor is contiguous, the left is not.
1467 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx,
1469 xfs_bmbt_set_blockcount(ep,
1470 PREV.br_blockcount + RIGHT.br_blockcount);
1471 xfs_bmbt_set_state(ep, newext);
1472 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx,
1474 ip->i_df.if_lastex = idx;
1475 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
1476 xfs_iext_remove(ifp, idx + 1, 1);
1477 ip->i_d.di_nextents--;
1479 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1481 rval = XFS_ILOG_CORE;
1482 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1483 RIGHT.br_startblock,
1484 RIGHT.br_blockcount, &i)))
1487 if ((error = xfs_bmbt_delete(cur, &i)))
1490 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1493 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1495 new->br_blockcount + RIGHT.br_blockcount,
1499 /* DELTA: Two in-core extents are replaced by one. */
1500 temp = PREV.br_startoff;
1501 temp2 = PREV.br_blockcount +
1502 RIGHT.br_blockcount;
1505 case MASK2(LEFT_FILLING, RIGHT_FILLING):
1507 * Setting all of a previous oldext extent to newext.
1508 * Neither the left nor right neighbors are contiguous with
1511 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx,
1513 xfs_bmbt_set_state(ep, newext);
1514 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx,
1516 ip->i_df.if_lastex = idx;
1518 rval = XFS_ILOG_DEXT;
1521 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1522 new->br_startblock, new->br_blockcount,
1526 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1527 new->br_startblock, new->br_blockcount,
1531 /* DELTA: The in-core extent described by new changed type. */
1532 temp = new->br_startoff;
1533 temp2 = new->br_blockcount;
1536 case MASK2(LEFT_FILLING, LEFT_CONTIG):
1538 * Setting the first part of a previous oldext extent to newext.
1539 * The left neighbor is contiguous.
1541 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1,
1543 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1544 LEFT.br_blockcount + new->br_blockcount);
1545 xfs_bmbt_set_startoff(ep,
1546 PREV.br_startoff + new->br_blockcount);
1547 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1,
1549 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx,
1551 xfs_bmbt_set_startblock(ep,
1552 new->br_startblock + new->br_blockcount);
1553 xfs_bmbt_set_blockcount(ep,
1554 PREV.br_blockcount - new->br_blockcount);
1555 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx,
1557 ip->i_df.if_lastex = idx - 1;
1559 rval = XFS_ILOG_DEXT;
1562 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1563 PREV.br_startblock, PREV.br_blockcount,
1567 if ((error = xfs_bmbt_update(cur,
1568 PREV.br_startoff + new->br_blockcount,
1569 PREV.br_startblock + new->br_blockcount,
1570 PREV.br_blockcount - new->br_blockcount,
1573 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1575 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1577 LEFT.br_blockcount + new->br_blockcount,
1581 /* DELTA: The boundary between two in-core extents moved. */
1582 temp = LEFT.br_startoff;
1583 temp2 = LEFT.br_blockcount +
1587 case MASK(LEFT_FILLING):
1589 * Setting the first part of a previous oldext extent to newext.
1590 * The left neighbor is not contiguous.
1592 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1593 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1594 xfs_bmbt_set_startoff(ep, new_endoff);
1595 xfs_bmbt_set_blockcount(ep,
1596 PREV.br_blockcount - new->br_blockcount);
1597 xfs_bmbt_set_startblock(ep,
1598 new->br_startblock + new->br_blockcount);
1599 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1600 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
1602 xfs_iext_insert(ifp, idx, 1, new);
1603 ip->i_df.if_lastex = idx;
1604 ip->i_d.di_nextents++;
1606 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1608 rval = XFS_ILOG_CORE;
1609 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1610 PREV.br_startblock, PREV.br_blockcount,
1614 if ((error = xfs_bmbt_update(cur,
1615 PREV.br_startoff + new->br_blockcount,
1616 PREV.br_startblock + new->br_blockcount,
1617 PREV.br_blockcount - new->br_blockcount,
1620 cur->bc_rec.b = *new;
1621 if ((error = xfs_bmbt_insert(cur, &i)))
1625 /* DELTA: One in-core extent is split in two. */
1626 temp = PREV.br_startoff;
1627 temp2 = PREV.br_blockcount;
1630 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1632 * Setting the last part of a previous oldext extent to newext.
1633 * The right neighbor is contiguous with the new allocation.
1635 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx,
1637 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1,
1639 xfs_bmbt_set_blockcount(ep,
1640 PREV.br_blockcount - new->br_blockcount);
1641 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx,
1643 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1644 new->br_startoff, new->br_startblock,
1645 new->br_blockcount + RIGHT.br_blockcount, newext);
1646 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1,
1648 ip->i_df.if_lastex = idx + 1;
1650 rval = XFS_ILOG_DEXT;
1653 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1655 PREV.br_blockcount, &i)))
1658 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1660 PREV.br_blockcount - new->br_blockcount,
1663 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1665 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1667 new->br_blockcount + RIGHT.br_blockcount,
1671 /* DELTA: The boundary between two in-core extents moved. */
1672 temp = PREV.br_startoff;
1673 temp2 = PREV.br_blockcount +
1674 RIGHT.br_blockcount;
1677 case MASK(RIGHT_FILLING):
1679 * Setting the last part of a previous oldext extent to newext.
1680 * The right neighbor is not contiguous.
1682 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1683 xfs_bmbt_set_blockcount(ep,
1684 PREV.br_blockcount - new->br_blockcount);
1685 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1686 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1688 xfs_iext_insert(ifp, idx + 1, 1, new);
1689 ip->i_df.if_lastex = idx + 1;
1690 ip->i_d.di_nextents++;
1692 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1694 rval = XFS_ILOG_CORE;
1695 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1696 PREV.br_startblock, PREV.br_blockcount,
1700 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1702 PREV.br_blockcount - new->br_blockcount,
1705 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1706 new->br_startblock, new->br_blockcount,
1710 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1711 if ((error = xfs_bmbt_insert(cur, &i)))
1715 /* DELTA: One in-core extent is split in two. */
1716 temp = PREV.br_startoff;
1717 temp2 = PREV.br_blockcount;
1722 * Setting the middle part of a previous oldext extent to
1723 * newext. Contiguity is impossible here.
1724 * One extent becomes three extents.
1726 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
1727 xfs_bmbt_set_blockcount(ep,
1728 new->br_startoff - PREV.br_startoff);
1729 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
1731 r[1].br_startoff = new_endoff;
1732 r[1].br_blockcount =
1733 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1734 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1735 r[1].br_state = oldext;
1736 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
1738 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
1739 ip->i_df.if_lastex = idx + 1;
1740 ip->i_d.di_nextents += 2;
1742 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1744 rval = XFS_ILOG_CORE;
1745 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1746 PREV.br_startblock, PREV.br_blockcount,
1750 /* new right extent - oldext */
1751 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1752 r[1].br_startblock, r[1].br_blockcount,
1755 /* new left extent - oldext */
1756 PREV.br_blockcount =
1757 new->br_startoff - PREV.br_startoff;
1758 cur->bc_rec.b = PREV;
1759 if ((error = xfs_bmbt_insert(cur, &i)))
1762 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1765 /* new middle extent - newext */
1766 cur->bc_rec.b = *new;
1767 if ((error = xfs_bmbt_insert(cur, &i)))
1771 /* DELTA: One in-core extent is split in three. */
1772 temp = PREV.br_startoff;
1773 temp2 = PREV.br_blockcount;
1776 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1777 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1778 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1779 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1780 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1781 case MASK(LEFT_CONTIG):
1782 case MASK(RIGHT_CONTIG):
1784 * These cases are all impossible.
1791 if (delta->xed_startoff > temp)
1792 delta->xed_startoff = temp;
1793 if (delta->xed_blockcount < temp2)
1794 delta->xed_blockcount = temp2;
1808 #undef STATE_SET_TEST
1813 * Called by xfs_bmap_add_extent to handle cases converting a hole
1814 * to a delayed allocation.
1817 STATIC int /* error */
1818 xfs_bmap_add_extent_hole_delay(
1819 xfs_inode_t *ip, /* incore inode pointer */
1820 xfs_extnum_t idx, /* extent number to update/insert */
1821 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1822 int *logflagsp, /* inode logging flags */
1823 xfs_extdelta_t *delta, /* Change made to incore extents */
1824 int rsvd) /* OK to allocate reserved blocks */
1826 xfs_bmbt_rec_t *ep; /* extent record for idx */
1827 xfs_ifork_t *ifp; /* inode fork pointer */
1828 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1829 xfs_filblks_t newlen=0; /* new indirect size */
1830 xfs_filblks_t oldlen=0; /* old indirect size */
1831 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1832 int state; /* state bits, accessed thru macros */
1833 xfs_filblks_t temp=0; /* temp for indirect calculations */
1834 xfs_filblks_t temp2=0;
1835 enum { /* bit number definitions for state */
1836 LEFT_CONTIG, RIGHT_CONTIG,
1837 LEFT_DELAY, RIGHT_DELAY,
1838 LEFT_VALID, RIGHT_VALID
1841 #define MASK(b) (1 << (b))
1842 #define MASK2(a,b) (MASK(a) | MASK(b))
1843 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1844 #define STATE_TEST(b) (state & MASK(b))
1845 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1846 ((state &= ~MASK(b)), 0))
1847 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1849 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1850 ep = xfs_iext_get_ext(ifp, idx);
1852 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1854 * Check and set flags if this segment has a left neighbor
1856 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1857 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1858 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1861 * Check and set flags if the current (right) segment exists.
1862 * If it doesn't exist, we're converting the hole at end-of-file.
1864 if (STATE_SET_TEST(RIGHT_VALID,
1866 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1867 xfs_bmbt_get_all(ep, &right);
1868 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1871 * Set contiguity flags on the left and right neighbors.
1872 * Don't let extents get too large, even if the pieces are contiguous.
1874 STATE_SET(LEFT_CONTIG,
1875 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1876 left.br_startoff + left.br_blockcount == new->br_startoff &&
1877 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1878 STATE_SET(RIGHT_CONTIG,
1879 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1880 new->br_startoff + new->br_blockcount == right.br_startoff &&
1881 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1882 (!STATE_TEST(LEFT_CONTIG) ||
1883 (left.br_blockcount + new->br_blockcount +
1884 right.br_blockcount <= MAXEXTLEN)));
1886 * Switch out based on the contiguity flags.
1888 switch (SWITCH_STATE) {
1890 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1892 * New allocation is contiguous with delayed allocations
1893 * on the left and on the right.
1894 * Merge all three into a single extent record.
1896 temp = left.br_blockcount + new->br_blockcount +
1897 right.br_blockcount;
1898 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
1900 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1901 oldlen = STARTBLOCKVAL(left.br_startblock) +
1902 STARTBLOCKVAL(new->br_startblock) +
1903 STARTBLOCKVAL(right.br_startblock);
1904 newlen = xfs_bmap_worst_indlen(ip, temp);
1905 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1906 NULLSTARTBLOCK((int)newlen));
1907 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
1909 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, XFS_DATA_FORK);
1910 xfs_iext_remove(ifp, idx, 1);
1911 ip->i_df.if_lastex = idx - 1;
1912 /* DELTA: Two in-core extents were replaced by one. */
1914 temp = left.br_startoff;
1917 case MASK(LEFT_CONTIG):
1919 * New allocation is contiguous with a delayed allocation
1921 * Merge the new allocation with the left neighbor.
1923 temp = left.br_blockcount + new->br_blockcount;
1924 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1,
1926 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1927 oldlen = STARTBLOCKVAL(left.br_startblock) +
1928 STARTBLOCKVAL(new->br_startblock);
1929 newlen = xfs_bmap_worst_indlen(ip, temp);
1930 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1931 NULLSTARTBLOCK((int)newlen));
1932 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1,
1934 ip->i_df.if_lastex = idx - 1;
1935 /* DELTA: One in-core extent grew into a hole. */
1937 temp = left.br_startoff;
1940 case MASK(RIGHT_CONTIG):
1942 * New allocation is contiguous with a delayed allocation
1944 * Merge the new allocation with the right neighbor.
1946 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, XFS_DATA_FORK);
1947 temp = new->br_blockcount + right.br_blockcount;
1948 oldlen = STARTBLOCKVAL(new->br_startblock) +
1949 STARTBLOCKVAL(right.br_startblock);
1950 newlen = xfs_bmap_worst_indlen(ip, temp);
1951 xfs_bmbt_set_allf(ep, new->br_startoff,
1952 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
1953 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, XFS_DATA_FORK);
1954 ip->i_df.if_lastex = idx;
1955 /* DELTA: One in-core extent grew into a hole. */
1957 temp = new->br_startoff;
1962 * New allocation is not contiguous with another
1963 * delayed allocation.
1964 * Insert a new entry.
1966 oldlen = newlen = 0;
1967 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL,
1969 xfs_iext_insert(ifp, idx, 1, new);
1970 ip->i_df.if_lastex = idx;
1971 /* DELTA: A new in-core extent was added in a hole. */
1972 temp2 = new->br_blockcount;
1973 temp = new->br_startoff;
1976 if (oldlen != newlen) {
1977 ASSERT(oldlen > newlen);
1978 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
1979 (int64_t)(oldlen - newlen), rsvd);
1981 * Nothing to do for disk quota accounting here.
1986 if (delta->xed_startoff > temp)
1987 delta->xed_startoff = temp;
1988 if (delta->xed_blockcount < temp2)
1989 delta->xed_blockcount = temp2;
1997 #undef STATE_SET_TEST
2002 * Called by xfs_bmap_add_extent to handle cases converting a hole
2003 * to a real allocation.
2005 STATIC int /* error */
2006 xfs_bmap_add_extent_hole_real(
2007 xfs_inode_t *ip, /* incore inode pointer */
2008 xfs_extnum_t idx, /* extent number to update/insert */
2009 xfs_btree_cur_t *cur, /* if null, not a btree */
2010 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2011 int *logflagsp, /* inode logging flags */
2012 xfs_extdelta_t *delta, /* Change made to incore extents */
2013 int whichfork) /* data or attr fork */
2015 xfs_bmbt_rec_t *ep; /* pointer to extent entry ins. point */
2016 int error; /* error return value */
2017 int i; /* temp state */
2018 xfs_ifork_t *ifp; /* inode fork pointer */
2019 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2020 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2021 int rval=0; /* return value (logging flags) */
2022 int state; /* state bits, accessed thru macros */
2023 xfs_filblks_t temp=0;
2024 xfs_filblks_t temp2=0;
2025 enum { /* bit number definitions for state */
2026 LEFT_CONTIG, RIGHT_CONTIG,
2027 LEFT_DELAY, RIGHT_DELAY,
2028 LEFT_VALID, RIGHT_VALID
2031 #define MASK(b) (1 << (b))
2032 #define MASK2(a,b) (MASK(a) | MASK(b))
2033 #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
2034 #define STATE_TEST(b) (state & MASK(b))
2035 #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
2036 ((state &= ~MASK(b)), 0))
2037 #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
2039 ifp = XFS_IFORK_PTR(ip, whichfork);
2040 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
2041 ep = xfs_iext_get_ext(ifp, idx);
2044 * Check and set flags if this segment has a left neighbor.
2046 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
2047 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
2048 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
2051 * Check and set flags if this segment has a current value.
2052 * Not true if we're inserting into the "hole" at eof.
2054 if (STATE_SET_TEST(RIGHT_VALID,
2056 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
2057 xfs_bmbt_get_all(ep, &right);
2058 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
2061 * We're inserting a real allocation between "left" and "right".
2062 * Set the contiguity flags. Don't let extents get too large.
2064 STATE_SET(LEFT_CONTIG,
2065 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
2066 left.br_startoff + left.br_blockcount == new->br_startoff &&
2067 left.br_startblock + left.br_blockcount == new->br_startblock &&
2068 left.br_state == new->br_state &&
2069 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
2070 STATE_SET(RIGHT_CONTIG,
2071 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
2072 new->br_startoff + new->br_blockcount == right.br_startoff &&
2073 new->br_startblock + new->br_blockcount ==
2074 right.br_startblock &&
2075 new->br_state == right.br_state &&
2076 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2077 (!STATE_TEST(LEFT_CONTIG) ||
2078 left.br_blockcount + new->br_blockcount +
2079 right.br_blockcount <= MAXEXTLEN));
2083 * Select which case we're in here, and implement it.
2085 switch (SWITCH_STATE) {
2087 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
2089 * New allocation is contiguous with real allocations on the
2090 * left and on the right.
2091 * Merge all three into a single extent record.
2093 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
2095 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2096 left.br_blockcount + new->br_blockcount +
2097 right.br_blockcount);
2098 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
2100 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, whichfork);
2101 xfs_iext_remove(ifp, idx, 1);
2102 ifp->if_lastex = idx - 1;
2103 XFS_IFORK_NEXT_SET(ip, whichfork,
2104 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2106 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2108 rval = XFS_ILOG_CORE;
2109 if ((error = xfs_bmbt_lookup_eq(cur,
2111 right.br_startblock,
2112 right.br_blockcount, &i)))
2115 if ((error = xfs_bmbt_delete(cur, &i)))
2118 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2121 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2123 left.br_blockcount +
2124 new->br_blockcount +
2125 right.br_blockcount,
2129 /* DELTA: Two in-core extents were replaced by one. */
2130 temp = left.br_startoff;
2131 temp2 = left.br_blockcount +
2132 new->br_blockcount +
2133 right.br_blockcount;
2136 case MASK(LEFT_CONTIG):
2138 * New allocation is contiguous with a real allocation
2140 * Merge the new allocation with the left neighbor.
2142 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1, whichfork);
2143 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
2144 left.br_blockcount + new->br_blockcount);
2145 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1, whichfork);
2146 ifp->if_lastex = idx - 1;
2148 rval = XFS_ILOG_FEXT(whichfork);
2151 if ((error = xfs_bmbt_lookup_eq(cur,
2154 left.br_blockcount, &i)))
2157 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2159 left.br_blockcount +
2164 /* DELTA: One in-core extent grew. */
2165 temp = left.br_startoff;
2166 temp2 = left.br_blockcount +
2170 case MASK(RIGHT_CONTIG):
2172 * New allocation is contiguous with a real allocation
2174 * Merge the new allocation with the right neighbor.
2176 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, whichfork);
2177 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2178 new->br_blockcount + right.br_blockcount,
2180 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, whichfork);
2181 ifp->if_lastex = idx;
2183 rval = XFS_ILOG_FEXT(whichfork);
2186 if ((error = xfs_bmbt_lookup_eq(cur,
2188 right.br_startblock,
2189 right.br_blockcount, &i)))
2192 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2194 new->br_blockcount +
2195 right.br_blockcount,
2199 /* DELTA: One in-core extent grew. */
2200 temp = new->br_startoff;
2201 temp2 = new->br_blockcount +
2202 right.br_blockcount;
2207 * New allocation is not contiguous with another
2209 * Insert a new entry.
2211 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL, whichfork);
2212 xfs_iext_insert(ifp, idx, 1, new);
2213 ifp->if_lastex = idx;
2214 XFS_IFORK_NEXT_SET(ip, whichfork,
2215 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2217 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2219 rval = XFS_ILOG_CORE;
2220 if ((error = xfs_bmbt_lookup_eq(cur,
2223 new->br_blockcount, &i)))
2226 cur->bc_rec.b.br_state = new->br_state;
2227 if ((error = xfs_bmbt_insert(cur, &i)))
2231 /* DELTA: A new extent was added in a hole. */
2232 temp = new->br_startoff;
2233 temp2 = new->br_blockcount;
2238 if (delta->xed_startoff > temp)
2239 delta->xed_startoff = temp;
2240 if (delta->xed_blockcount < temp2)
2241 delta->xed_blockcount = temp2;
2250 #undef STATE_SET_TEST
2255 * Adjust the size of the new extent based on di_extsize and rt extsize.
2258 xfs_bmap_extsize_align(
2260 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2261 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2262 xfs_extlen_t extsz, /* align to this extent size */
2263 int rt, /* is this a realtime inode? */
2264 int eof, /* is extent at end-of-file? */
2265 int delay, /* creating delalloc extent? */
2266 int convert, /* overwriting unwritten extent? */
2267 xfs_fileoff_t *offp, /* in/out: aligned offset */
2268 xfs_extlen_t *lenp) /* in/out: aligned length */
2270 xfs_fileoff_t orig_off; /* original offset */
2271 xfs_extlen_t orig_alen; /* original length */
2272 xfs_fileoff_t orig_end; /* original off+len */
2273 xfs_fileoff_t nexto; /* next file offset */
2274 xfs_fileoff_t prevo; /* previous file offset */
2275 xfs_fileoff_t align_off; /* temp for offset */
2276 xfs_extlen_t align_alen; /* temp for length */
2277 xfs_extlen_t temp; /* temp for calculations */
2282 orig_off = align_off = *offp;
2283 orig_alen = align_alen = *lenp;
2284 orig_end = orig_off + orig_alen;
2287 * If this request overlaps an existing extent, then don't
2288 * attempt to perform any additional alignment.
2290 if (!delay && !eof &&
2291 (orig_off >= gotp->br_startoff) &&
2292 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2297 * If the file offset is unaligned vs. the extent size
2298 * we need to align it. This will be possible unless
2299 * the file was previously written with a kernel that didn't
2300 * perform this alignment, or if a truncate shot us in the
2303 temp = do_mod(orig_off, extsz);
2309 * Same adjustment for the end of the requested area.
2311 if ((temp = (align_alen % extsz))) {
2312 align_alen += extsz - temp;
2315 * If the previous block overlaps with this proposed allocation
2316 * then move the start forward without adjusting the length.
2318 if (prevp->br_startoff != NULLFILEOFF) {
2319 if (prevp->br_startblock == HOLESTARTBLOCK)
2320 prevo = prevp->br_startoff;
2322 prevo = prevp->br_startoff + prevp->br_blockcount;
2325 if (align_off != orig_off && align_off < prevo)
2328 * If the next block overlaps with this proposed allocation
2329 * then move the start back without adjusting the length,
2330 * but not before offset 0.
2331 * This may of course make the start overlap previous block,
2332 * and if we hit the offset 0 limit then the next block
2333 * can still overlap too.
2335 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2336 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2337 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2338 nexto = gotp->br_startoff + gotp->br_blockcount;
2340 nexto = gotp->br_startoff;
2342 nexto = NULLFILEOFF;
2344 align_off + align_alen != orig_end &&
2345 align_off + align_alen > nexto)
2346 align_off = nexto > align_alen ? nexto - align_alen : 0;
2348 * If we're now overlapping the next or previous extent that
2349 * means we can't fit an extsz piece in this hole. Just move
2350 * the start forward to the first valid spot and set
2351 * the length so we hit the end.
2353 if (align_off != orig_off && align_off < prevo)
2355 if (align_off + align_alen != orig_end &&
2356 align_off + align_alen > nexto &&
2357 nexto != NULLFILEOFF) {
2358 ASSERT(nexto > prevo);
2359 align_alen = nexto - align_off;
2363 * If realtime, and the result isn't a multiple of the realtime
2364 * extent size we need to remove blocks until it is.
2366 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2368 * We're not covering the original request, or
2369 * we won't be able to once we fix the length.
2371 if (orig_off < align_off ||
2372 orig_end > align_off + align_alen ||
2373 align_alen - temp < orig_alen)
2374 return XFS_ERROR(EINVAL);
2376 * Try to fix it by moving the start up.
2378 if (align_off + temp <= orig_off) {
2383 * Try to fix it by moving the end in.
2385 else if (align_off + align_alen - temp >= orig_end)
2388 * Set the start to the minimum then trim the length.
2391 align_alen -= orig_off - align_off;
2392 align_off = orig_off;
2393 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2396 * Result doesn't cover the request, fail it.
2398 if (orig_off < align_off || orig_end > align_off + align_alen)
2399 return XFS_ERROR(EINVAL);
2401 ASSERT(orig_off >= align_off);
2402 ASSERT(orig_end <= align_off + align_alen);
2406 if (!eof && gotp->br_startoff != NULLFILEOFF)
2407 ASSERT(align_off + align_alen <= gotp->br_startoff);
2408 if (prevp->br_startoff != NULLFILEOFF)
2409 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2417 #define XFS_ALLOC_GAP_UNITS 4
2421 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2423 xfs_fsblock_t adjust; /* adjustment to block numbers */
2424 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2425 xfs_mount_t *mp; /* mount point structure */
2426 int nullfb; /* true if ap->firstblock isn't set */
2427 int rt; /* true if inode is realtime */
2429 #define ISVALID(x,y) \
2431 (x) < mp->m_sb.sb_rblocks : \
2432 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2433 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2434 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2436 mp = ap->ip->i_mount;
2437 nullfb = ap->firstblock == NULLFSBLOCK;
2438 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2439 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2441 * If allocating at eof, and there's a previous real block,
2442 * try to use it's last block as our starting point.
2444 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2445 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2446 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2447 ap->prevp->br_startblock)) {
2448 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2450 * Adjust for the gap between prevp and us.
2453 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2455 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2459 * If not at eof, then compare the two neighbor blocks.
2460 * Figure out whether either one gives us a good starting point,
2461 * and pick the better one.
2463 else if (!ap->eof) {
2464 xfs_fsblock_t gotbno; /* right side block number */
2465 xfs_fsblock_t gotdiff=0; /* right side difference */
2466 xfs_fsblock_t prevbno; /* left side block number */
2467 xfs_fsblock_t prevdiff=0; /* left side difference */
2470 * If there's a previous (left) block, select a requested
2471 * start block based on it.
2473 if (ap->prevp->br_startoff != NULLFILEOFF &&
2474 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2475 (prevbno = ap->prevp->br_startblock +
2476 ap->prevp->br_blockcount) &&
2477 ISVALID(prevbno, ap->prevp->br_startblock)) {
2479 * Calculate gap to end of previous block.
2481 adjust = prevdiff = ap->off -
2482 (ap->prevp->br_startoff +
2483 ap->prevp->br_blockcount);
2485 * Figure the startblock based on the previous block's
2486 * end and the gap size.
2488 * If the gap is large relative to the piece we're
2489 * allocating, or using it gives us an invalid block
2490 * number, then just use the end of the previous block.
2492 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2493 ISVALID(prevbno + prevdiff,
2494 ap->prevp->br_startblock))
2499 * If the firstblock forbids it, can't use it,
2502 if (!rt && !nullfb &&
2503 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2504 prevbno = NULLFSBLOCK;
2507 * No previous block or can't follow it, just default.
2510 prevbno = NULLFSBLOCK;
2512 * If there's a following (right) block, select a requested
2513 * start block based on it.
2515 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
2517 * Calculate gap to start of next block.
2519 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2521 * Figure the startblock based on the next block's
2522 * start and the gap size.
2524 gotbno = ap->gotp->br_startblock;
2527 * If the gap is large relative to the piece we're
2528 * allocating, or using it gives us an invalid block
2529 * number, then just use the start of the next block
2530 * offset by our length.
2532 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2533 ISVALID(gotbno - gotdiff, gotbno))
2535 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2537 gotdiff += adjust - ap->alen;
2541 * If the firstblock forbids it, can't use it,
2544 if (!rt && !nullfb &&
2545 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2546 gotbno = NULLFSBLOCK;
2549 * No next block, just default.
2552 gotbno = NULLFSBLOCK;
2554 * If both valid, pick the better one, else the only good
2555 * one, else ap->rval is already set (to 0 or the inode block).
2557 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2558 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2559 else if (prevbno != NULLFSBLOCK)
2561 else if (gotbno != NULLFSBLOCK)
2570 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2572 xfs_alloctype_t atype = 0; /* type for allocation routines */
2573 int error; /* error return value */
2574 xfs_mount_t *mp; /* mount point structure */
2575 xfs_extlen_t prod = 0; /* product factor for allocators */
2576 xfs_extlen_t ralen = 0; /* realtime allocation length */
2577 xfs_extlen_t align; /* minimum allocation alignment */
2580 mp = ap->ip->i_mount;
2581 align = xfs_get_extsz_hint(ap->ip);
2582 prod = align / mp->m_sb.sb_rextsize;
2583 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2584 align, 1, ap->eof, 0,
2585 ap->conv, &ap->off, &ap->alen);
2589 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2592 * If the offset & length are not perfectly aligned
2593 * then kill prod, it will just get us in trouble.
2595 if (do_mod(ap->off, align) || ap->alen % align)
2598 * Set ralen to be the actual requested length in rtextents.
2600 ralen = ap->alen / mp->m_sb.sb_rextsize;
2602 * If the old value was close enough to MAXEXTLEN that
2603 * we rounded up to it, cut it back so it's valid again.
2604 * Note that if it's a really large request (bigger than
2605 * MAXEXTLEN), we don't hear about that number, and can't
2606 * adjust the starting point to match it.
2608 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2609 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2611 * If it's an allocation to an empty file at offset 0,
2612 * pick an extent that will space things out in the rt area.
2614 if (ap->eof && ap->off == 0) {
2615 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2617 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2620 ap->rval = rtx * mp->m_sb.sb_rextsize;
2625 xfs_bmap_adjacent(ap);
2628 * Realtime allocation, done through xfs_rtallocate_extent.
2630 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2631 do_div(ap->rval, mp->m_sb.sb_rextsize);
2634 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2635 &ralen, atype, ap->wasdel, prod, &rtb)))
2637 if (rtb == NULLFSBLOCK && prod > 1 &&
2638 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2639 ap->alen, &ralen, atype,
2640 ap->wasdel, 1, &rtb)))
2643 if (ap->rval != NULLFSBLOCK) {
2644 ap->rval *= mp->m_sb.sb_rextsize;
2645 ralen *= mp->m_sb.sb_rextsize;
2647 ap->ip->i_d.di_nblocks += ralen;
2648 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2650 ap->ip->i_delayed_blks -= ralen;
2652 * Adjust the disk quota also. This was reserved
2655 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2656 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2657 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2666 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2668 xfs_mount_t *mp; /* mount point structure */
2669 xfs_alloctype_t atype = 0; /* type for allocation routines */
2670 xfs_extlen_t align; /* minimum allocation alignment */
2672 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2673 xfs_agnumber_t startag;
2674 xfs_alloc_arg_t args;
2677 xfs_extlen_t longest;
2679 xfs_extlen_t nextminlen = 0;
2681 int nullfb; /* true if ap->firstblock isn't set */
2687 mp = ap->ip->i_mount;
2688 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2689 if (unlikely(align)) {
2690 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2691 align, 0, ap->eof, 0, ap->conv,
2692 &ap->off, &ap->alen);
2696 nullfb = ap->firstblock == NULLFSBLOCK;
2697 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2699 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2700 ag = xfs_filestream_lookup_ag(ap->ip);
2701 ag = (ag != NULLAGNUMBER) ? ag : 0;
2702 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2704 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2707 ap->rval = ap->firstblock;
2709 xfs_bmap_adjacent(ap);
2712 * If allowed, use ap->rval; otherwise must use firstblock since
2713 * it's in the right allocation group.
2715 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2718 ap->rval = ap->firstblock;
2720 * Normal allocation, done through xfs_alloc_vextent.
2722 tryagain = isaligned = 0;
2725 args.fsbno = ap->rval;
2726 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2727 args.firstblock = ap->firstblock;
2730 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2731 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2733 args.type = XFS_ALLOCTYPE_START_BNO;
2734 args.total = ap->total;
2737 * Search for an allocation group with a single extent
2738 * large enough for the request.
2740 * If one isn't found, then adjust the minimum allocation
2741 * size to the largest space found.
2743 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2744 if (startag == NULLAGNUMBER)
2747 down_read(&mp->m_peraglock);
2748 while (blen < ap->alen) {
2749 pag = &mp->m_perag[ag];
2750 if (!pag->pagf_init &&
2751 (error = xfs_alloc_pagf_init(mp, args.tp,
2752 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2753 up_read(&mp->m_peraglock);
2757 * See xfs_alloc_fix_freelist...
2759 if (pag->pagf_init) {
2760 need = XFS_MIN_FREELIST_PAG(pag, mp);
2761 delta = need > pag->pagf_flcount ?
2762 need - pag->pagf_flcount : 0;
2763 longest = (pag->pagf_longest > delta) ?
2764 (pag->pagf_longest - delta) :
2765 (pag->pagf_flcount > 0 ||
2766 pag->pagf_longest > 0);
2772 if (xfs_inode_is_filestream(ap->ip)) {
2773 if (blen >= ap->alen)
2778 * If startag is an invalid AG, we've
2779 * come here once before and
2780 * xfs_filestream_new_ag picked the
2781 * best currently available.
2783 * Don't continue looping, since we
2784 * could loop forever.
2786 if (startag == NULLAGNUMBER)
2789 error = xfs_filestream_new_ag(ap, &ag);
2791 up_read(&mp->m_peraglock);
2795 /* loop again to set 'blen'*/
2796 startag = NULLAGNUMBER;
2800 if (++ag == mp->m_sb.sb_agcount)
2805 up_read(&mp->m_peraglock);
2807 * Since the above loop did a BUF_TRYLOCK, it is
2808 * possible that there is space for this request.
2810 if (notinit || blen < ap->minlen)
2811 args.minlen = ap->minlen;
2813 * If the best seen length is less than the request
2814 * length, use the best as the minimum.
2816 else if (blen < ap->alen)
2819 * Otherwise we've seen an extent as big as alen,
2820 * use that as the minimum.
2823 args.minlen = ap->alen;
2826 * set the failure fallback case to look in the selected
2827 * AG as the stream may have moved.
2829 if (xfs_inode_is_filestream(ap->ip))
2830 ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2831 } else if (ap->low) {
2832 if (xfs_inode_is_filestream(ap->ip))
2833 args.type = XFS_ALLOCTYPE_FIRST_AG;
2835 args.type = XFS_ALLOCTYPE_START_BNO;
2836 args.total = args.minlen = ap->minlen;
2838 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2839 args.total = ap->total;
2840 args.minlen = ap->minlen;
2842 /* apply extent size hints if obtained earlier */
2843 if (unlikely(align)) {
2845 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2846 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2847 } else if (mp->m_sb.sb_blocksize >= NBPP) {
2851 args.prod = NBPP >> mp->m_sb.sb_blocklog;
2852 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2853 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2856 * If we are not low on available data blocks, and the
2857 * underlying logical volume manager is a stripe, and
2858 * the file offset is zero then try to allocate data
2859 * blocks on stripe unit boundary.
2860 * NOTE: ap->aeof is only set if the allocation length
2861 * is >= the stripe unit and the allocation offset is
2862 * at the end of file.
2864 if (!ap->low && ap->aeof) {
2866 args.alignment = mp->m_dalign;
2870 * Adjust for alignment
2872 if (blen > args.alignment && blen <= ap->alen)
2873 args.minlen = blen - args.alignment;
2874 args.minalignslop = 0;
2877 * First try an exact bno allocation.
2878 * If it fails then do a near or start bno
2879 * allocation with alignment turned on.
2883 args.type = XFS_ALLOCTYPE_THIS_BNO;
2886 * Compute the minlen+alignment for the
2887 * next case. Set slop so that the value
2888 * of minlen+alignment+slop doesn't go up
2889 * between the calls.
2891 if (blen > mp->m_dalign && blen <= ap->alen)
2892 nextminlen = blen - mp->m_dalign;
2894 nextminlen = args.minlen;
2895 if (nextminlen + mp->m_dalign > args.minlen + 1)
2897 nextminlen + mp->m_dalign -
2900 args.minalignslop = 0;
2904 args.minalignslop = 0;
2906 args.minleft = ap->minleft;
2907 args.wasdel = ap->wasdel;
2909 args.userdata = ap->userdata;
2910 if ((error = xfs_alloc_vextent(&args)))
2912 if (tryagain && args.fsbno == NULLFSBLOCK) {
2914 * Exact allocation failed. Now try with alignment
2918 args.fsbno = ap->rval;
2919 args.alignment = mp->m_dalign;
2920 args.minlen = nextminlen;
2921 args.minalignslop = 0;
2923 if ((error = xfs_alloc_vextent(&args)))
2926 if (isaligned && args.fsbno == NULLFSBLOCK) {
2928 * allocation failed, so turn off alignment and
2932 args.fsbno = ap->rval;
2934 if ((error = xfs_alloc_vextent(&args)))
2937 if (args.fsbno == NULLFSBLOCK && nullfb &&
2938 args.minlen > ap->minlen) {
2939 args.minlen = ap->minlen;
2940 args.type = XFS_ALLOCTYPE_START_BNO;
2941 args.fsbno = ap->rval;
2942 if ((error = xfs_alloc_vextent(&args)))
2945 if (args.fsbno == NULLFSBLOCK && nullfb) {
2947 args.type = XFS_ALLOCTYPE_FIRST_AG;
2948 args.total = ap->minlen;
2950 if ((error = xfs_alloc_vextent(&args)))
2954 if (args.fsbno != NULLFSBLOCK) {
2955 ap->firstblock = ap->rval = args.fsbno;
2956 ASSERT(nullfb || fb_agno == args.agno ||
2957 (ap->low && fb_agno < args.agno));
2958 ap->alen = args.len;
2959 ap->ip->i_d.di_nblocks += args.len;
2960 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2962 ap->ip->i_delayed_blks -= args.len;
2964 * Adjust the disk quota also. This was reserved
2967 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2968 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2969 XFS_TRANS_DQ_BCOUNT,
2972 ap->rval = NULLFSBLOCK;
2979 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2980 * It figures out where to ask the underlying allocator to put the new extent.
2984 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2986 if ((ap->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) && ap->userdata)
2987 return xfs_bmap_rtalloc(ap);
2988 return xfs_bmap_btalloc(ap);
2992 * Transform a btree format file with only one leaf node, where the
2993 * extents list will fit in the inode, into an extents format file.
2994 * Since the file extents are already in-core, all we have to do is
2995 * give up the space for the btree root and pitch the leaf block.
2997 STATIC int /* error */
2998 xfs_bmap_btree_to_extents(
2999 xfs_trans_t *tp, /* transaction pointer */
3000 xfs_inode_t *ip, /* incore inode pointer */
3001 xfs_btree_cur_t *cur, /* btree cursor */
3002 int *logflagsp, /* inode logging flags */
3003 int whichfork) /* data or attr fork */
3006 xfs_bmbt_block_t *cblock;/* child btree block */
3007 xfs_fsblock_t cbno; /* child block number */
3008 xfs_buf_t *cbp; /* child block's buffer */
3009 int error; /* error return value */
3010 xfs_ifork_t *ifp; /* inode fork data */
3011 xfs_mount_t *mp; /* mount point structure */
3012 __be64 *pp; /* ptr to block address */
3013 xfs_bmbt_block_t *rblock;/* root btree block */
3015 ifp = XFS_IFORK_PTR(ip, whichfork);
3016 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3017 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
3018 rblock = ifp->if_broot;
3019 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
3020 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
3021 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
3023 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
3024 cbno = be64_to_cpu(*pp);
3027 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
3030 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
3031 XFS_BMAP_BTREE_REF)))
3033 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
3034 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
3036 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
3037 ip->i_d.di_nblocks--;
3038 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
3039 xfs_trans_binval(tp, cbp);
3040 if (cur->bc_bufs[0] == cbp)
3041 cur->bc_bufs[0] = NULL;
3042 xfs_iroot_realloc(ip, -1, whichfork);
3043 ASSERT(ifp->if_broot == NULL);
3044 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
3045 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3046 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
3051 * Called by xfs_bmapi to update file extent records and the btree
3052 * after removing space (or undoing a delayed allocation).
3054 STATIC int /* error */
3055 xfs_bmap_del_extent(
3056 xfs_inode_t *ip, /* incore inode pointer */
3057 xfs_trans_t *tp, /* current transaction pointer */
3058 xfs_extnum_t idx, /* extent number to update/delete */
3059 xfs_bmap_free_t *flist, /* list of extents to be freed */
3060 xfs_btree_cur_t *cur, /* if null, not a btree */
3061 xfs_bmbt_irec_t *del, /* data to remove from extents */
3062 int *logflagsp, /* inode logging flags */
3063 xfs_extdelta_t *delta, /* Change made to incore extents */
3064 int whichfork, /* data or attr fork */
3065 int rsvd) /* OK to allocate reserved blocks */
3067 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
3068 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
3069 xfs_fsblock_t del_endblock=0; /* first block past del */
3070 xfs_fileoff_t del_endoff; /* first offset past del */
3071 int delay; /* current block is delayed allocated */
3072 int do_fx; /* free extent at end of routine */
3073 xfs_bmbt_rec_t *ep; /* current extent entry pointer */
3074 int error; /* error return value */
3075 int flags; /* inode logging flags */
3076 xfs_bmbt_irec_t got; /* current extent entry */
3077 xfs_fileoff_t got_endoff; /* first offset past got */
3078 int i; /* temp state */
3079 xfs_ifork_t *ifp; /* inode fork pointer */
3080 xfs_mount_t *mp; /* mount structure */
3081 xfs_filblks_t nblks; /* quota/sb block count */
3082 xfs_bmbt_irec_t new; /* new record to be inserted */
3084 uint qfield; /* quota field to update */
3085 xfs_filblks_t temp; /* for indirect length calculations */
3086 xfs_filblks_t temp2; /* for indirect length calculations */
3088 XFS_STATS_INC(xs_del_exlist);
3090 ifp = XFS_IFORK_PTR(ip, whichfork);
3091 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
3092 (uint)sizeof(xfs_bmbt_rec_t)));
3093 ASSERT(del->br_blockcount > 0);
3094 ep = xfs_iext_get_ext(ifp, idx);
3095 xfs_bmbt_get_all(ep, &got);
3096 ASSERT(got.br_startoff <= del->br_startoff);
3097 del_endoff = del->br_startoff + del->br_blockcount;
3098 got_endoff = got.br_startoff + got.br_blockcount;
3099 ASSERT(got_endoff >= del_endoff);
3100 delay = ISNULLSTARTBLOCK(got.br_startblock);
3101 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
3106 * If deleting a real allocation, must free up the disk space.
3109 flags = XFS_ILOG_CORE;
3111 * Realtime allocation. Free it and record di_nblocks update.
3113 if (whichfork == XFS_DATA_FORK &&
3114 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
3118 ASSERT(do_mod(del->br_blockcount,
3119 mp->m_sb.sb_rextsize) == 0);
3120 ASSERT(do_mod(del->br_startblock,
3121 mp->m_sb.sb_rextsize) == 0);
3122 bno = del->br_startblock;
3123 len = del->br_blockcount;
3124 do_div(bno, mp->m_sb.sb_rextsize);
3125 do_div(len, mp->m_sb.sb_rextsize);
3126 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3127 (xfs_extlen_t)len)))
3130 nblks = len * mp->m_sb.sb_rextsize;
3131 qfield = XFS_TRANS_DQ_RTBCOUNT;
3134 * Ordinary allocation.
3138 nblks = del->br_blockcount;
3139 qfield = XFS_TRANS_DQ_BCOUNT;
3142 * Set up del_endblock and cur for later.
3144 del_endblock = del->br_startblock + del->br_blockcount;
3146 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3147 got.br_startblock, got.br_blockcount,
3152 da_old = da_new = 0;
3154 da_old = STARTBLOCKVAL(got.br_startblock);
3160 * Set flag value to use in switch statement.
3161 * Left-contig is 2, right-contig is 1.
3163 switch (((got.br_startoff == del->br_startoff) << 1) |
3164 (got_endoff == del_endoff)) {
3167 * Matches the whole extent. Delete the entry.
3169 XFS_BMAP_TRACE_DELETE("3", ip, idx, 1, whichfork);
3170 xfs_iext_remove(ifp, idx, 1);
3171 ifp->if_lastex = idx;
3174 XFS_IFORK_NEXT_SET(ip, whichfork,
3175 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3176 flags |= XFS_ILOG_CORE;
3178 flags |= XFS_ILOG_FEXT(whichfork);
3181 if ((error = xfs_bmbt_delete(cur, &i)))
3188 * Deleting the first part of the extent.
3190 XFS_BMAP_TRACE_PRE_UPDATE("2", ip, idx, whichfork);
3191 xfs_bmbt_set_startoff(ep, del_endoff);
3192 temp = got.br_blockcount - del->br_blockcount;
3193 xfs_bmbt_set_blockcount(ep, temp);
3194 ifp->if_lastex = idx;
3196 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3198 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3199 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx,
3204 xfs_bmbt_set_startblock(ep, del_endblock);
3205 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx, whichfork);
3207 flags |= XFS_ILOG_FEXT(whichfork);
3210 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3211 got.br_blockcount - del->br_blockcount,
3218 * Deleting the last part of the extent.
3220 temp = got.br_blockcount - del->br_blockcount;
3221 XFS_BMAP_TRACE_PRE_UPDATE("1", ip, idx, whichfork);
3222 xfs_bmbt_set_blockcount(ep, temp);
3223 ifp->if_lastex = idx;
3225 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3227 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3228 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx,
3233 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx, whichfork);
3235 flags |= XFS_ILOG_FEXT(whichfork);
3238 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3240 got.br_blockcount - del->br_blockcount,
3247 * Deleting the middle of the extent.
3249 temp = del->br_startoff - got.br_startoff;
3250 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, whichfork);
3251 xfs_bmbt_set_blockcount(ep, temp);
3252 new.br_startoff = del_endoff;
3253 temp2 = got_endoff - del_endoff;
3254 new.br_blockcount = temp2;
3255 new.br_state = got.br_state;
3257 new.br_startblock = del_endblock;
3258 flags |= XFS_ILOG_CORE;
3260 if ((error = xfs_bmbt_update(cur,
3262 got.br_startblock, temp,
3265 if ((error = xfs_bmbt_increment(cur, 0, &i)))
3267 cur->bc_rec.b = new;
3268 error = xfs_bmbt_insert(cur, &i);
3269 if (error && error != ENOSPC)
3272 * If get no-space back from btree insert,
3273 * it tried a split, and we have a zero
3274 * block reservation.
3275 * Fix up our state and return the error.
3277 if (error == ENOSPC) {
3279 * Reset the cursor, don't trust
3280 * it after any insert operation.
3282 if ((error = xfs_bmbt_lookup_eq(cur,
3289 * Update the btree record back
3290 * to the original value.
3292 if ((error = xfs_bmbt_update(cur,
3299 * Reset the extent record back
3300 * to the original value.
3302 xfs_bmbt_set_blockcount(ep,
3305 error = XFS_ERROR(ENOSPC);
3310 flags |= XFS_ILOG_FEXT(whichfork);
3311 XFS_IFORK_NEXT_SET(ip, whichfork,
3312 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3314 ASSERT(whichfork == XFS_DATA_FORK);
3315 temp = xfs_bmap_worst_indlen(ip, temp);
3316 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3317 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3318 new.br_startblock = NULLSTARTBLOCK((int)temp2);
3319 da_new = temp + temp2;
3320 while (da_new > da_old) {
3324 xfs_bmbt_set_startblock(ep,
3325 NULLSTARTBLOCK((int)temp));
3327 if (da_new == da_old)
3333 NULLSTARTBLOCK((int)temp2);
3337 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, whichfork);
3338 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 1, &new, NULL,
3340 xfs_iext_insert(ifp, idx + 1, 1, &new);
3341 ifp->if_lastex = idx + 1;
3345 * If we need to, add to list of extents to delete.
3348 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3351 * Adjust inode # blocks in the file.
3354 ip->i_d.di_nblocks -= nblks;
3356 * Adjust quota data.
3359 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
3362 * Account for change in delayed indirect blocks.
3363 * Nothing to do for disk quota accounting here.
3365 ASSERT(da_old >= da_new);
3366 if (da_old > da_new)
3367 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
3370 /* DELTA: report the original extent. */
3371 if (delta->xed_startoff > got.br_startoff)
3372 delta->xed_startoff = got.br_startoff;
3373 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3374 delta->xed_blockcount = got.br_startoff +
3383 * Remove the entry "free" from the free item list. Prev points to the
3384 * previous entry, unless "free" is the head of the list.
3388 xfs_bmap_free_t *flist, /* free item list header */
3389 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3390 xfs_bmap_free_item_t *free) /* list item to be freed */
3393 prev->xbfi_next = free->xbfi_next;
3395 flist->xbf_first = free->xbfi_next;
3397 kmem_zone_free(xfs_bmap_free_item_zone, free);
3401 * Convert an extents-format file into a btree-format file.
3402 * The new file will have a root block (in the inode) and a single child block.
3404 STATIC int /* error */
3405 xfs_bmap_extents_to_btree(
3406 xfs_trans_t *tp, /* transaction pointer */
3407 xfs_inode_t *ip, /* incore inode pointer */
3408 xfs_fsblock_t *firstblock, /* first-block-allocated */
3409 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3410 xfs_btree_cur_t **curp, /* cursor returned to caller */
3411 int wasdel, /* converting a delayed alloc */
3412 int *logflagsp, /* inode logging flags */
3413 int whichfork) /* data or attr fork */
3415 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
3416 xfs_buf_t *abp; /* buffer for ablock */
3417 xfs_alloc_arg_t args; /* allocation arguments */
3418 xfs_bmbt_rec_t *arp; /* child record pointer */
3419 xfs_bmbt_block_t *block; /* btree root block */
3420 xfs_btree_cur_t *cur; /* bmap btree cursor */
3421 xfs_bmbt_rec_t *ep; /* extent record pointer */
3422 int error; /* error return value */
3423 xfs_extnum_t i, cnt; /* extent record index */
3424 xfs_ifork_t *ifp; /* inode fork pointer */
3425 xfs_bmbt_key_t *kp; /* root block key pointer */
3426 xfs_mount_t *mp; /* mount structure */
3427 xfs_extnum_t nextents; /* number of file extents */
3428 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3430 ifp = XFS_IFORK_PTR(ip, whichfork);
3431 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3432 ASSERT(ifp->if_ext_max ==
3433 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3435 * Make space in the inode incore.
3437 xfs_iroot_realloc(ip, 1, whichfork);
3438 ifp->if_flags |= XFS_IFBROOT;
3442 block = ifp->if_broot;
3443 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3444 block->bb_level = cpu_to_be16(1);
3445 block->bb_numrecs = cpu_to_be16(1);
3446 block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3447 block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3449 * Need a cursor. Can't allocate until bb_level is filled in.
3452 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
3454 cur->bc_private.b.firstblock = *firstblock;
3455 cur->bc_private.b.flist = flist;
3456 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3458 * Convert to a btree with two levels, one record in root.
3460 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3463 args.firstblock = *firstblock;
3464 if (*firstblock == NULLFSBLOCK) {
3465 args.type = XFS_ALLOCTYPE_START_BNO;
3466 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3467 } else if (flist->xbf_low) {
3468 args.type = XFS_ALLOCTYPE_START_BNO;
3469 args.fsbno = *firstblock;
3471 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3472 args.fsbno = *firstblock;
3474 args.minlen = args.maxlen = args.prod = 1;
3475 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3476 args.minalignslop = 0;
3477 args.wasdel = wasdel;
3479 if ((error = xfs_alloc_vextent(&args))) {
3480 xfs_iroot_realloc(ip, -1, whichfork);
3481 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3485 * Allocation can't fail, the space was reserved.
3487 ASSERT(args.fsbno != NULLFSBLOCK);
3488 ASSERT(*firstblock == NULLFSBLOCK ||
3489 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3491 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3492 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3493 cur->bc_private.b.allocated++;
3494 ip->i_d.di_nblocks++;
3495 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3496 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3498 * Fill in the child block.
3500 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
3501 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3502 ablock->bb_level = 0;
3503 ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3504 ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
3505 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3506 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3507 for (cnt = i = 0; i < nextents; i++) {
3508 ep = xfs_iext_get_ext(ifp, i);
3509 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
3510 arp->l0 = INT_GET(ep->l0, ARCH_CONVERT);
3511 arp->l1 = INT_GET(ep->l1, ARCH_CONVERT);
3515 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3516 ablock->bb_numrecs = cpu_to_be16(cnt);
3518 * Fill in the root key and pointer.
3520 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
3521 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3522 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3523 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
3524 *pp = cpu_to_be64(args.fsbno);
3526 * Do all this logging at the end so that
3527 * the root is at the right level.
3529 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
3530 xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3531 ASSERT(*curp == NULL);
3533 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
3538 * Helper routine to reset inode di_forkoff field when switching
3539 * attribute fork from local to extent format - we reset it where
3540 * possible to make space available for inline data fork extents.
3543 xfs_bmap_forkoff_reset(
3548 if (whichfork == XFS_ATTR_FORK &&
3549 (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
3550 (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
3551 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
3552 ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
3553 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3554 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
3555 (uint)sizeof(xfs_bmbt_rec_t);
3556 ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
3557 (uint)sizeof(xfs_bmbt_rec_t);
3562 * Convert a local file to an extents file.
3563 * This code is out of bounds for data forks of regular files,
3564 * since the file data needs to get logged so things will stay consistent.
3565 * (The bmap-level manipulations are ok, though).
3567 STATIC int /* error */
3568 xfs_bmap_local_to_extents(
3569 xfs_trans_t *tp, /* transaction pointer */
3570 xfs_inode_t *ip, /* incore inode pointer */
3571 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3572 xfs_extlen_t total, /* total blocks needed by transaction */
3573 int *logflagsp, /* inode logging flags */
3574 int whichfork) /* data or attr fork */
3576 int error; /* error return value */
3577 int flags; /* logging flags returned */
3578 xfs_ifork_t *ifp; /* inode fork pointer */
3581 * We don't want to deal with the case of keeping inode data inline yet.
3582 * So sending the data fork of a regular inode is invalid.
3584 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3585 whichfork == XFS_DATA_FORK));
3586 ifp = XFS_IFORK_PTR(ip, whichfork);
3587 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3590 if (ifp->if_bytes) {
3591 xfs_alloc_arg_t args; /* allocation arguments */
3592 xfs_buf_t *bp; /* buffer for extent block */
3593 xfs_bmbt_rec_t *ep; /* extent record pointer */
3596 args.mp = ip->i_mount;
3597 args.firstblock = *firstblock;
3598 ASSERT((ifp->if_flags &
3599 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3601 * Allocate a block. We know we need only one, since the
3602 * file currently fits in an inode.
3604 if (*firstblock == NULLFSBLOCK) {
3605 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3606 args.type = XFS_ALLOCTYPE_START_BNO;
3608 args.fsbno = *firstblock;
3609 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3612 args.mod = args.minleft = args.alignment = args.wasdel =
3613 args.isfl = args.minalignslop = 0;
3614 args.minlen = args.maxlen = args.prod = 1;
3615 if ((error = xfs_alloc_vextent(&args)))
3618 * Can't fail, the space was reserved.
3620 ASSERT(args.fsbno != NULLFSBLOCK);
3621 ASSERT(args.len == 1);
3622 *firstblock = args.fsbno;
3623 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3624 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3626 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3627 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3628 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3629 xfs_iext_add(ifp, 0, 1);
3630 ep = xfs_iext_get_ext(ifp, 0);
3631 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3632 XFS_BMAP_TRACE_POST_UPDATE("new", ip, 0, whichfork);
3633 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3634 ip->i_d.di_nblocks = 1;
3635 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
3636 XFS_TRANS_DQ_BCOUNT, 1L);
3637 flags |= XFS_ILOG_FEXT(whichfork);
3639 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3640 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3642 ifp->if_flags &= ~XFS_IFINLINE;
3643 ifp->if_flags |= XFS_IFEXTENTS;
3644 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3645 flags |= XFS_ILOG_CORE;
3652 * Search the extent records for the entry containing block bno.
3653 * If bno lies in a hole, point to the next entry. If bno lies
3654 * past eof, *eofp will be set, and *prevp will contain the last
3655 * entry (null if none). Else, *lastxp will be set to the index
3656 * of the found entry; *gotp will contain the entry.
3658 xfs_bmbt_rec_t * /* pointer to found extent entry */
3659 xfs_bmap_search_multi_extents(
3660 xfs_ifork_t *ifp, /* inode fork pointer */
3661 xfs_fileoff_t bno, /* block number searched for */
3662 int *eofp, /* out: end of file found */
3663 xfs_extnum_t *lastxp, /* out: last extent index */
3664 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3665 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3667 xfs_bmbt_rec_t *ep; /* extent record pointer */
3668 xfs_extnum_t lastx; /* last extent index */
3671 * Initialize the extent entry structure to catch access to
3672 * uninitialized br_startblock field.
3674 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3675 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3676 gotp->br_state = XFS_EXT_INVALID;
3678 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3680 gotp->br_startblock = 0xffffa5a5;
3682 prevp->br_startoff = NULLFILEOFF;
3684 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3686 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3688 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3689 xfs_bmbt_get_all(ep, gotp);
3703 * Search the extents list for the inode, for the extent containing bno.
3704 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3705 * *eofp will be set, and *prevp will contain the last entry (null if none).
3706 * Else, *lastxp will be set to the index of the found
3707 * entry; *gotp will contain the entry.
3709 STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
3710 xfs_bmap_search_extents(
3711 xfs_inode_t *ip, /* incore inode pointer */
3712 xfs_fileoff_t bno, /* block number searched for */
3713 int fork, /* data or attr fork */
3714 int *eofp, /* out: end of file found */
3715 xfs_extnum_t *lastxp, /* out: last extent index */
3716 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3717 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3719 xfs_ifork_t *ifp; /* inode fork pointer */
3720 xfs_bmbt_rec_t *ep; /* extent record pointer */
3722 XFS_STATS_INC(xs_look_exlist);
3723 ifp = XFS_IFORK_PTR(ip, fork);
3725 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3727 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3728 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3729 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3730 "Access to block zero in inode %llu "
3731 "start_block: %llx start_off: %llx "
3732 "blkcnt: %llx extent-state: %x lastx: %x\n",
3733 (unsigned long long)ip->i_ino,
3734 (unsigned long long)gotp->br_startblock,
3735 (unsigned long long)gotp->br_startoff,
3736 (unsigned long long)gotp->br_blockcount,
3737 gotp->br_state, *lastxp);
3738 *lastxp = NULLEXTNUM;
3746 #ifdef XFS_BMAP_TRACE
3747 ktrace_t *xfs_bmap_trace_buf;
3750 * Add a bmap trace buffer entry. Base routine for the others.
3753 xfs_bmap_trace_addentry(
3754 int opcode, /* operation */
3755 const char *fname, /* function name */
3756 char *desc, /* operation description */
3757 xfs_inode_t *ip, /* incore inode pointer */
3758 xfs_extnum_t idx, /* index of entry(ies) */
3759 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
3760 xfs_bmbt_rec_t *r1, /* first record */
3761 xfs_bmbt_rec_t *r2, /* second record or null */
3762 int whichfork) /* data or attr fork */
3766 ASSERT(cnt == 1 || cnt == 2);
3771 memset(&tr2, 0, sizeof(tr2));
3774 ktrace_enter(xfs_bmap_trace_buf,
3775 (void *)(__psint_t)(opcode | (whichfork << 16)),
3776 (void *)fname, (void *)desc, (void *)ip,
3777 (void *)(__psint_t)idx,
3778 (void *)(__psint_t)cnt,
3779 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3780 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3781 (void *)(__psunsigned_t)(r1->l0 >> 32),
3782 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3783 (void *)(__psunsigned_t)(r1->l1 >> 32),
3784 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3785 (void *)(__psunsigned_t)(r2->l0 >> 32),
3786 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3787 (void *)(__psunsigned_t)(r2->l1 >> 32),
3788 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3790 ASSERT(ip->i_xtrace);
3791 ktrace_enter(ip->i_xtrace,
3792 (void *)(__psint_t)(opcode | (whichfork << 16)),
3793 (void *)fname, (void *)desc, (void *)ip,
3794 (void *)(__psint_t)idx,
3795 (void *)(__psint_t)cnt,
3796 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3797 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3798 (void *)(__psunsigned_t)(r1->l0 >> 32),
3799 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3800 (void *)(__psunsigned_t)(r1->l1 >> 32),
3801 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3802 (void *)(__psunsigned_t)(r2->l0 >> 32),
3803 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3804 (void *)(__psunsigned_t)(r2->l1 >> 32),
3805 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3810 * Add bmap trace entry prior to a call to xfs_iext_remove.
3813 xfs_bmap_trace_delete(
3814 const char *fname, /* function name */
3815 char *desc, /* operation description */
3816 xfs_inode_t *ip, /* incore inode pointer */
3817 xfs_extnum_t idx, /* index of entry(entries) deleted */
3818 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3819 int whichfork) /* data or attr fork */
3821 xfs_ifork_t *ifp; /* inode fork pointer */
3823 ifp = XFS_IFORK_PTR(ip, whichfork);
3824 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
3825 cnt, xfs_iext_get_ext(ifp, idx),
3826 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
3831 * Add bmap trace entry prior to a call to xfs_iext_insert, or
3832 * reading in the extents list from the disk (in the btree).
3835 xfs_bmap_trace_insert(
3836 const char *fname, /* function name */
3837 char *desc, /* operation description */
3838 xfs_inode_t *ip, /* incore inode pointer */
3839 xfs_extnum_t idx, /* index of entry(entries) inserted */
3840 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3841 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3842 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3843 int whichfork) /* data or attr fork */
3845 xfs_bmbt_rec_t tr1; /* compressed record 1 */
3846 xfs_bmbt_rec_t tr2; /* compressed record 2 if needed */
3848 xfs_bmbt_set_all(&tr1, r1);
3851 xfs_bmbt_set_all(&tr2, r2);
3856 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3857 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3861 * Add bmap trace entry after updating an extent record in place.
3864 xfs_bmap_trace_post_update(
3865 const char *fname, /* function name */
3866 char *desc, /* operation description */
3867 xfs_inode_t *ip, /* incore inode pointer */
3868 xfs_extnum_t idx, /* index of entry updated */
3869 int whichfork) /* data or attr fork */
3871 xfs_ifork_t *ifp; /* inode fork pointer */
3873 ifp = XFS_IFORK_PTR(ip, whichfork);
3874 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
3875 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3879 * Add bmap trace entry prior to updating an extent record in place.
3882 xfs_bmap_trace_pre_update(
3883 const char *fname, /* function name */
3884 char *desc, /* operation description */
3885 xfs_inode_t *ip, /* incore inode pointer */
3886 xfs_extnum_t idx, /* index of entry to be updated */
3887 int whichfork) /* data or attr fork */
3889 xfs_ifork_t *ifp; /* inode fork pointer */
3891 ifp = XFS_IFORK_PTR(ip, whichfork);
3892 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
3893 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
3895 #endif /* XFS_BMAP_TRACE */
3898 * Compute the worst-case number of indirect blocks that will be used
3899 * for ip's delayed extent of length "len".
3901 STATIC xfs_filblks_t
3902 xfs_bmap_worst_indlen(
3903 xfs_inode_t *ip, /* incore inode pointer */
3904 xfs_filblks_t len) /* delayed extent length */
3906 int level; /* btree level number */
3907 int maxrecs; /* maximum record count at this level */
3908 xfs_mount_t *mp; /* mount structure */
3909 xfs_filblks_t rval; /* return value */
3912 maxrecs = mp->m_bmap_dmxr[0];
3913 for (level = 0, rval = 0;
3914 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3917 do_div(len, maxrecs);
3920 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3923 maxrecs = mp->m_bmap_dmxr[1];
3928 #if defined(XFS_RW_TRACE)
3937 if (ip->i_rwtrace == NULL)
3939 ktrace_enter(ip->i_rwtrace,
3940 (void *)(__psint_t)XFS_BUNMAP,
3942 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3943 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3944 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3945 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3946 (void *)(__psint_t)len,
3947 (void *)(__psint_t)flags,
3948 (void *)(unsigned long)current_cpu(),
3960 * Convert inode from non-attributed to attributed.
3961 * Must not be in a transaction, ip must not be locked.
3963 int /* error code */
3964 xfs_bmap_add_attrfork(
3965 xfs_inode_t *ip, /* incore inode pointer */
3966 int size, /* space new attribute needs */
3967 int rsvd) /* xact may use reserved blks */
3969 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3970 xfs_bmap_free_t flist; /* freed extent records */
3971 xfs_mount_t *mp; /* mount structure */
3972 xfs_trans_t *tp; /* transaction pointer */
3973 unsigned long s; /* spinlock spl value */
3974 int blks; /* space reservation */
3975 int version = 1; /* superblock attr version */
3976 int committed; /* xaction was committed */
3977 int logflags; /* logging flags */
3978 int error; /* error return value */
3980 ASSERT(XFS_IFORK_Q(ip) == 0);
3981 ASSERT(ip->i_df.if_ext_max ==
3982 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3985 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3986 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3987 blks = XFS_ADDAFORK_SPACE_RES(mp);
3989 tp->t_flags |= XFS_TRANS_RESERVE;
3990 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3991 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3993 xfs_ilock(ip, XFS_ILOCK_EXCL);
3994 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
3995 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3996 XFS_QMOPT_RES_REGBLKS);
3998 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3999 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
4002 if (XFS_IFORK_Q(ip))
4004 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
4006 * For inodes coming from pre-6.2 filesystems.
4008 ASSERT(ip->i_d.di_aformat == 0);
4009 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
4011 ASSERT(ip->i_d.di_anextents == 0);
4012 VN_HOLD(XFS_ITOV(ip));
4013 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4014 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4015 switch (ip->i_d.di_format) {
4016 case XFS_DINODE_FMT_DEV:
4017 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
4019 case XFS_DINODE_FMT_UUID:
4020 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
4022 case XFS_DINODE_FMT_LOCAL:
4023 case XFS_DINODE_FMT_EXTENTS:
4024 case XFS_DINODE_FMT_BTREE:
4025 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
4026 if (!ip->i_d.di_forkoff)
4027 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
4028 else if (mp->m_flags & XFS_MOUNT_ATTR2)
4033 error = XFS_ERROR(EINVAL);
4036 ip->i_df.if_ext_max =
4037 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4038 ASSERT(ip->i_afp == NULL);
4039 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
4040 ip->i_afp->if_ext_max =
4041 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4042 ip->i_afp->if_flags = XFS_IFEXTENTS;
4044 XFS_BMAP_INIT(&flist, &firstblock);
4045 switch (ip->i_d.di_format) {
4046 case XFS_DINODE_FMT_LOCAL:
4047 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
4050 case XFS_DINODE_FMT_EXTENTS:
4051 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
4054 case XFS_DINODE_FMT_BTREE:
4055 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
4063 xfs_trans_log_inode(tp, ip, logflags);
4066 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb) ||
4067 (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2)) {
4068 __int64_t sbfields = 0;
4070 s = XFS_SB_LOCK(mp);
4071 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) {
4072 XFS_SB_VERSION_ADDATTR(&mp->m_sb);
4073 sbfields |= XFS_SB_VERSIONNUM;
4075 if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2) {
4076 XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
4077 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
4080 XFS_SB_UNLOCK(mp, s);
4081 xfs_mod_sb(tp, sbfields);
4083 XFS_SB_UNLOCK(mp, s);
4085 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
4087 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
4088 ASSERT(ip->i_df.if_ext_max ==
4089 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4092 xfs_bmap_cancel(&flist);
4094 ASSERT(ismrlocked(&ip->i_lock,MR_UPDATE));
4095 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4097 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4098 ASSERT(ip->i_df.if_ext_max ==
4099 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4104 * Add the extent to the list of extents to be free at transaction end.
4105 * The list is maintained sorted (by block number).
4110 xfs_fsblock_t bno, /* fs block number of extent */
4111 xfs_filblks_t len, /* length of extent */
4112 xfs_bmap_free_t *flist, /* list of extents */
4113 xfs_mount_t *mp) /* mount point structure */
4115 xfs_bmap_free_item_t *cur; /* current (next) element */
4116 xfs_bmap_free_item_t *new; /* new element */
4117 xfs_bmap_free_item_t *prev; /* previous element */
4119 xfs_agnumber_t agno;
4120 xfs_agblock_t agbno;
4122 ASSERT(bno != NULLFSBLOCK);
4124 ASSERT(len <= MAXEXTLEN);
4125 ASSERT(!ISNULLSTARTBLOCK(bno));
4126 agno = XFS_FSB_TO_AGNO(mp, bno);
4127 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4128 ASSERT(agno < mp->m_sb.sb_agcount);
4129 ASSERT(agbno < mp->m_sb.sb_agblocks);
4130 ASSERT(len < mp->m_sb.sb_agblocks);
4131 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4133 ASSERT(xfs_bmap_free_item_zone != NULL);
4134 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4135 new->xbfi_startblock = bno;
4136 new->xbfi_blockcount = (xfs_extlen_t)len;
4137 for (prev = NULL, cur = flist->xbf_first;
4139 prev = cur, cur = cur->xbfi_next) {
4140 if (cur->xbfi_startblock >= bno)
4144 prev->xbfi_next = new;
4146 flist->xbf_first = new;
4147 new->xbfi_next = cur;
4152 * Compute and fill in the value of the maximum depth of a bmap btree
4153 * in this filesystem. Done once, during mount.
4156 xfs_bmap_compute_maxlevels(
4157 xfs_mount_t *mp, /* file system mount structure */
4158 int whichfork) /* data or attr fork */
4160 int level; /* btree level */
4161 uint maxblocks; /* max blocks at this level */
4162 uint maxleafents; /* max leaf entries possible */
4163 int maxrootrecs; /* max records in root block */
4164 int minleafrecs; /* min records in leaf block */
4165 int minnoderecs; /* min records in node block */
4166 int sz; /* root block size */
4169 * The maximum number of extents in a file, hence the maximum
4170 * number of leaf entries, is controlled by the type of di_nextents
4171 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4172 * (a signed 16-bit number, xfs_aextnum_t).
4174 if (whichfork == XFS_DATA_FORK) {
4175 maxleafents = MAXEXTNUM;
4176 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4177 XFS_BMDR_SPACE_CALC(MINDBTPTRS) : mp->m_attroffset;
4179 maxleafents = MAXAEXTNUM;
4180 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4181 XFS_BMDR_SPACE_CALC(MINABTPTRS) :
4182 mp->m_sb.sb_inodesize - mp->m_attroffset;
4184 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
4185 minleafrecs = mp->m_bmap_dmnr[0];
4186 minnoderecs = mp->m_bmap_dmnr[1];
4187 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4188 for (level = 1; maxblocks > 1; level++) {
4189 if (maxblocks <= maxrootrecs)
4192 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4194 mp->m_bm_maxlevels[whichfork] = level;
4198 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4199 * caller. Frees all the extents that need freeing, which must be done
4200 * last due to locking considerations. We never free any extents in
4201 * the first transaction. This is to allow the caller to make the first
4202 * transaction a synchronous one so that the pointers to the data being
4203 * broken in this transaction will be permanent before the data is actually
4204 * freed. This is necessary to prevent blocks from being reallocated
4205 * and written to before the free and reallocation are actually permanent.
4206 * We do not just make the first transaction synchronous here, because
4207 * there are more efficient ways to gain the same protection in some cases
4208 * (see the file truncation code).
4210 * Return 1 if the given transaction was committed and a new one
4211 * started, and 0 otherwise in the committed parameter.
4216 xfs_trans_t **tp, /* transaction pointer addr */
4217 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4218 int *committed) /* xact committed or not */
4220 xfs_efd_log_item_t *efd; /* extent free data */
4221 xfs_efi_log_item_t *efi; /* extent free intention */
4222 int error; /* error return value */
4223 xfs_bmap_free_item_t *free; /* free extent item */
4224 unsigned int logres; /* new log reservation */
4225 unsigned int logcount; /* new log count */
4226 xfs_mount_t *mp; /* filesystem mount structure */
4227 xfs_bmap_free_item_t *next; /* next item on free list */
4228 xfs_trans_t *ntp; /* new transaction pointer */
4230 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4231 if (flist->xbf_count == 0) {
4236 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4237 for (free = flist->xbf_first; free; free = free->xbfi_next)
4238 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4239 free->xbfi_blockcount);
4240 logres = ntp->t_log_res;
4241 logcount = ntp->t_log_count;
4242 ntp = xfs_trans_dup(*tp);
4243 error = xfs_trans_commit(*tp, 0);
4247 * We have a new transaction, so we should return committed=1,
4248 * even though we're returning an error.
4253 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4256 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4257 for (free = flist->xbf_first; free != NULL; free = next) {
4258 next = free->xbfi_next;
4259 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4260 free->xbfi_blockcount))) {
4262 * The bmap free list will be cleaned up at a
4263 * higher level. The EFI will be canceled when
4264 * this transaction is aborted.
4265 * Need to force shutdown here to make sure it
4266 * happens, since this transaction may not be
4270 if (!XFS_FORCED_SHUTDOWN(mp))
4271 xfs_force_shutdown(mp,
4272 (error == EFSCORRUPTED) ?
4273 SHUTDOWN_CORRUPT_INCORE :
4274 SHUTDOWN_META_IO_ERROR);
4277 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4278 free->xbfi_blockcount);
4279 xfs_bmap_del_free(flist, NULL, free);
4285 * Free up any items left in the list.
4289 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4291 xfs_bmap_free_item_t *free; /* free list item */
4292 xfs_bmap_free_item_t *next;
4294 if (flist->xbf_count == 0)
4296 ASSERT(flist->xbf_first != NULL);
4297 for (free = flist->xbf_first; free; free = next) {
4298 next = free->xbfi_next;
4299 xfs_bmap_del_free(flist, NULL, free);
4301 ASSERT(flist->xbf_count == 0);
4305 * Returns the file-relative block number of the first unused block(s)
4306 * in the file with at least "len" logically contiguous blocks free.
4307 * This is the lowest-address hole if the file has holes, else the first block
4308 * past the end of file.
4309 * Return 0 if the file is currently local (in-inode).
4312 xfs_bmap_first_unused(
4313 xfs_trans_t *tp, /* transaction pointer */
4314 xfs_inode_t *ip, /* incore inode */
4315 xfs_extlen_t len, /* size of hole to find */
4316 xfs_fileoff_t *first_unused, /* unused block */
4317 int whichfork) /* data or attr fork */
4319 xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
4320 int error; /* error return value */
4321 int idx; /* extent record index */
4322 xfs_ifork_t *ifp; /* inode fork pointer */
4323 xfs_fileoff_t lastaddr; /* last block number seen */
4324 xfs_fileoff_t lowest; /* lowest useful block */
4325 xfs_fileoff_t max; /* starting useful block */
4326 xfs_fileoff_t off; /* offset for this block */
4327 xfs_extnum_t nextents; /* number of extent entries */
4329 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4330 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4331 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4332 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4336 ifp = XFS_IFORK_PTR(ip, whichfork);
4337 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4338 (error = xfs_iread_extents(tp, ip, whichfork)))
4340 lowest = *first_unused;
4341 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4342 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
4343 ep = xfs_iext_get_ext(ifp, idx);
4344 off = xfs_bmbt_get_startoff(ep);
4346 * See if the hole before this extent will work.
4348 if (off >= lowest + len && off - max >= len) {
4349 *first_unused = max;
4352 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4353 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4355 *first_unused = max;
4360 * Returns the file-relative block number of the last block + 1 before
4361 * last_block (input value) in the file.
4362 * This is not based on i_size, it is based on the extent records.
4363 * Returns 0 for local files, as they do not have extent records.
4366 xfs_bmap_last_before(
4367 xfs_trans_t *tp, /* transaction pointer */
4368 xfs_inode_t *ip, /* incore inode */
4369 xfs_fileoff_t *last_block, /* last block */
4370 int whichfork) /* data or attr fork */
4372 xfs_fileoff_t bno; /* input file offset */
4373 int eof; /* hit end of file */
4374 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4375 int error; /* error return value */
4376 xfs_bmbt_irec_t got; /* current extent value */
4377 xfs_ifork_t *ifp; /* inode fork pointer */
4378 xfs_extnum_t lastx; /* last extent used */
4379 xfs_bmbt_irec_t prev; /* previous extent value */
4381 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4382 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4383 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4384 return XFS_ERROR(EIO);
4385 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4389 ifp = XFS_IFORK_PTR(ip, whichfork);
4390 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4391 (error = xfs_iread_extents(tp, ip, whichfork)))
4393 bno = *last_block - 1;
4394 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4396 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4397 if (prev.br_startoff == NULLFILEOFF)
4400 *last_block = prev.br_startoff + prev.br_blockcount;
4403 * Otherwise *last_block is already the right answer.
4409 * Returns the file-relative block number of the first block past eof in
4410 * the file. This is not based on i_size, it is based on the extent records.
4411 * Returns 0 for local files, as they do not have extent records.
4414 xfs_bmap_last_offset(
4415 xfs_trans_t *tp, /* transaction pointer */
4416 xfs_inode_t *ip, /* incore inode */
4417 xfs_fileoff_t *last_block, /* last block */
4418 int whichfork) /* data or attr fork */
4420 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4421 int error; /* error return value */
4422 xfs_ifork_t *ifp; /* inode fork pointer */
4423 xfs_extnum_t nextents; /* number of extent entries */
4425 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4426 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4427 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4428 return XFS_ERROR(EIO);
4429 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4433 ifp = XFS_IFORK_PTR(ip, whichfork);
4434 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4435 (error = xfs_iread_extents(tp, ip, whichfork)))
4437 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4442 ep = xfs_iext_get_ext(ifp, nextents - 1);
4443 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4448 * Returns whether the selected fork of the inode has exactly one
4449 * block or not. For the data fork we check this matches di_size,
4450 * implying the file's range is 0..bsize-1.
4452 int /* 1=>1 block, 0=>otherwise */
4454 xfs_inode_t *ip, /* incore inode */
4455 int whichfork) /* data or attr fork */
4457 xfs_bmbt_rec_t *ep; /* ptr to fork's extent */
4458 xfs_ifork_t *ifp; /* inode fork pointer */
4459 int rval; /* return value */
4460 xfs_bmbt_irec_t s; /* internal version of extent */
4463 if (whichfork == XFS_DATA_FORK) {
4464 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4465 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4466 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4469 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4471 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4473 ifp = XFS_IFORK_PTR(ip, whichfork);
4474 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4475 ep = xfs_iext_get_ext(ifp, 0);
4476 xfs_bmbt_get_all(ep, &s);
4477 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4478 if (rval && whichfork == XFS_DATA_FORK)
4479 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
4484 * Read in the extents to if_extents.
4485 * All inode fields are set up by caller, we just traverse the btree
4486 * and copy the records in. If the file system cannot contain unwritten
4487 * extents, the records are checked for no "state" flags.
4490 xfs_bmap_read_extents(
4491 xfs_trans_t *tp, /* transaction pointer */
4492 xfs_inode_t *ip, /* incore inode */
4493 int whichfork) /* data or attr fork */
4495 xfs_bmbt_block_t *block; /* current btree block */
4496 xfs_fsblock_t bno; /* block # of "block" */
4497 xfs_buf_t *bp; /* buffer for "block" */
4498 int error; /* error return value */
4499 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4500 xfs_extnum_t i, j; /* index into the extents list */
4501 xfs_ifork_t *ifp; /* fork structure */
4502 int level; /* btree level, for checking */
4503 xfs_mount_t *mp; /* file system mount structure */
4504 __be64 *pp; /* pointer to block address */
4506 xfs_extnum_t room; /* number of entries there's room for */
4510 ifp = XFS_IFORK_PTR(ip, whichfork);
4511 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4512 XFS_EXTFMT_INODE(ip);
4513 block = ifp->if_broot;
4515 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4517 level = be16_to_cpu(block->bb_level);
4519 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
4520 bno = be64_to_cpu(*pp);
4521 ASSERT(bno != NULLDFSBNO);
4522 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4523 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4525 * Go down the tree until leaf level is reached, following the first
4526 * pointer (leftmost) at each level.
4528 while (level-- > 0) {
4529 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4530 XFS_BMAP_BTREE_REF)))
4532 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4533 XFS_WANT_CORRUPTED_GOTO(
4534 XFS_BMAP_SANITY_CHECK(mp, block, level),
4538 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
4539 bno = be64_to_cpu(*pp);
4540 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4541 xfs_trans_brelse(tp, bp);
4544 * Here with bp and block set to the leftmost leaf node in the tree.
4546 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4549 * Loop over all leaf nodes. Copy information to the extent records.
4552 xfs_bmbt_rec_t *frp, *trp;
4553 xfs_fsblock_t nextbno;
4554 xfs_extnum_t num_recs;
4558 num_recs = be16_to_cpu(block->bb_numrecs);
4559 if (unlikely(i + num_recs > room)) {
4560 ASSERT(i + num_recs <= room);
4561 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4562 "corrupt dinode %Lu, (btree extents).",
4563 (unsigned long long) ip->i_ino);
4564 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4569 XFS_WANT_CORRUPTED_GOTO(
4570 XFS_BMAP_SANITY_CHECK(mp, block, 0),
4573 * Read-ahead the next leaf block, if any.
4575 nextbno = be64_to_cpu(block->bb_rightsib);
4576 if (nextbno != NULLFSBLOCK)
4577 xfs_btree_reada_bufl(mp, nextbno, 1);
4579 * Copy records into the extent records.
4581 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
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(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 const 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("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 */
4884 extsz = xfs_get_extsz_hint(ip);
4886 error = xfs_bmap_extsize_align(mp,
4889 flags&XFS_BMAPI_DELAY,
4890 flags&XFS_BMAPI_CONVERT,
4896 extsz = alen / mp->m_sb.sb_rextsize;
4899 * Make a transaction-less quota reservation for
4900 * delayed allocation blocks. This number gets
4901 * adjusted later. We return if we haven't
4902 * allocated blocks already inside this loop.
4904 if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS(
4905 mp, NULL, ip, (long)alen, 0,
4906 rt ? XFS_QMOPT_RES_RTBLKS :
4907 XFS_QMOPT_RES_REGBLKS))) {
4910 ASSERT(cur == NULL);
4917 * Split changing sb for alen and indlen since
4918 * they could be coming from different places.
4920 indlen = (xfs_extlen_t)
4921 xfs_bmap_worst_indlen(ip, alen);
4925 error = xfs_mod_incore_sb(mp,
4927 -((int64_t)extsz), (flags &
4928 XFS_BMAPI_RSVBLOCKS));
4930 error = xfs_mod_incore_sb(mp,
4932 -((int64_t)alen), (flags &
4933 XFS_BMAPI_RSVBLOCKS));
4936 error = xfs_mod_incore_sb(mp,
4938 -((int64_t)indlen), (flags &
4939 XFS_BMAPI_RSVBLOCKS));
4941 xfs_mod_incore_sb(mp,
4943 (int64_t)extsz, (flags &
4944 XFS_BMAPI_RSVBLOCKS));
4946 xfs_mod_incore_sb(mp,
4948 (int64_t)alen, (flags &
4949 XFS_BMAPI_RSVBLOCKS));
4953 if (XFS_IS_QUOTA_ON(mp))
4954 /* unreserve the blocks now */
4956 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4959 XFS_QMOPT_RES_RTBLKS :
4960 XFS_QMOPT_RES_REGBLKS);
4964 ip->i_delayed_blks += alen;
4965 abno = NULLSTARTBLOCK(indlen);
4968 * If first time, allocate and fill in
4969 * once-only bma fields.
4971 if (bma.ip == NULL) {
4979 /* Indicate if this is the first user data
4980 * in the file, or just any user data.
4982 if (!(flags & XFS_BMAPI_METADATA)) {
4983 bma.userdata = (aoff == 0) ?
4984 XFS_ALLOC_INITIAL_USER_DATA :
4988 * Fill in changeable bma fields.
4991 bma.firstblock = *firstblock;
4994 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4995 bma.wasdel = wasdelay;
4996 bma.minlen = minlen;
4997 bma.low = flist->xbf_low;
4998 bma.minleft = minleft;
5000 * Only want to do the alignment at the
5001 * eof if it is userdata and allocation length
5002 * is larger than a stripe unit.
5004 if (mp->m_dalign && alen >= mp->m_dalign &&
5005 (!(flags & XFS_BMAPI_METADATA)) &&
5006 (whichfork == XFS_DATA_FORK)) {
5007 if ((error = xfs_bmap_isaeof(ip, aoff,
5008 whichfork, &bma.aeof)))
5015 if ((error = xfs_bmap_alloc(&bma)))
5018 * Copy out result fields.
5021 if ((flist->xbf_low = bma.low))
5025 ASSERT(*firstblock == NULLFSBLOCK ||
5026 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5027 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
5029 XFS_FSB_TO_AGNO(mp, *firstblock) <
5030 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
5031 *firstblock = bma.firstblock;
5033 cur->bc_private.b.firstblock =
5035 if (abno == NULLFSBLOCK)
5037 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5038 cur = xfs_btree_init_cursor(mp,
5039 tp, NULL, 0, XFS_BTNUM_BMAP,
5041 cur->bc_private.b.firstblock =
5043 cur->bc_private.b.flist = flist;
5046 * Bump the number of extents we've allocated
5052 cur->bc_private.b.flags =
5053 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
5054 got.br_startoff = aoff;
5055 got.br_startblock = abno;
5056 got.br_blockcount = alen;
5057 got.br_state = XFS_EXT_NORM; /* assume normal */
5059 * Determine state of extent, and the filesystem.
5060 * A wasdelay extent has been initialized, so
5061 * shouldn't be flagged as unwritten.
5063 if (wr && XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5064 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5065 got.br_state = XFS_EXT_UNWRITTEN;
5067 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
5068 firstblock, flist, &tmp_logflags, delta,
5069 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5070 logflags |= tmp_logflags;
5073 lastx = ifp->if_lastex;
5074 ep = xfs_iext_get_ext(ifp, lastx);
5075 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5076 xfs_bmbt_get_all(ep, &got);
5077 ASSERT(got.br_startoff <= aoff);
5078 ASSERT(got.br_startoff + got.br_blockcount >=
5081 if (flags & XFS_BMAPI_DELAY) {
5082 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
5083 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
5085 ASSERT(got.br_state == XFS_EXT_NORM ||
5086 got.br_state == XFS_EXT_UNWRITTEN);
5089 * Fall down into the found allocated space case.
5091 } else if (inhole) {
5093 * Reading in a hole.
5095 mval->br_startoff = bno;
5096 mval->br_startblock = HOLESTARTBLOCK;
5097 mval->br_blockcount =
5098 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5099 mval->br_state = XFS_EXT_NORM;
5100 bno += mval->br_blockcount;
5101 len -= mval->br_blockcount;
5107 * Then deal with the allocated space we found.
5110 if (!(flags & XFS_BMAPI_ENTIRE) &&
5111 (got.br_startoff + got.br_blockcount > obno)) {
5114 ASSERT((bno >= obno) || (n == 0));
5116 mval->br_startoff = bno;
5117 if (ISNULLSTARTBLOCK(got.br_startblock)) {
5118 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5119 mval->br_startblock = DELAYSTARTBLOCK;
5121 mval->br_startblock =
5123 (bno - got.br_startoff);
5125 * Return the minimum of what we got and what we
5126 * asked for for the length. We can use the len
5127 * variable here because it is modified below
5128 * and we could have been there before coming
5129 * here if the first part of the allocation
5130 * didn't overlap what was asked for.
5132 mval->br_blockcount =
5133 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5134 (bno - got.br_startoff));
5135 mval->br_state = got.br_state;
5136 ASSERT(mval->br_blockcount <= len);
5139 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
5140 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
5141 mval->br_startblock = DELAYSTARTBLOCK;
5146 * Check if writing previously allocated but
5147 * unwritten extents.
5149 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5150 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5152 * Modify (by adding) the state flag, if writing.
5154 ASSERT(mval->br_blockcount <= len);
5155 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5156 cur = xfs_btree_init_cursor(mp,
5157 tp, NULL, 0, XFS_BTNUM_BMAP,
5159 cur->bc_private.b.firstblock =
5161 cur->bc_private.b.flist = flist;
5163 mval->br_state = XFS_EXT_NORM;
5164 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
5165 firstblock, flist, &tmp_logflags, delta,
5166 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
5167 logflags |= tmp_logflags;
5170 lastx = ifp->if_lastex;
5171 ep = xfs_iext_get_ext(ifp, lastx);
5172 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5173 xfs_bmbt_get_all(ep, &got);
5175 * We may have combined previously unwritten
5176 * space with written space, so generate
5179 if (mval->br_blockcount < len)
5183 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5184 ((mval->br_startoff + mval->br_blockcount) <= end));
5185 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5186 (mval->br_blockcount <= len) ||
5187 (mval->br_startoff < obno));
5188 bno = mval->br_startoff + mval->br_blockcount;
5190 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5191 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5192 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5193 ASSERT(mval->br_state == mval[-1].br_state);
5194 mval[-1].br_blockcount = mval->br_blockcount;
5195 mval[-1].br_state = mval->br_state;
5196 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5197 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5198 mval[-1].br_startblock != HOLESTARTBLOCK &&
5199 mval->br_startblock ==
5200 mval[-1].br_startblock + mval[-1].br_blockcount &&
5201 ((flags & XFS_BMAPI_IGSTATE) ||
5202 mval[-1].br_state == mval->br_state)) {
5203 ASSERT(mval->br_startoff ==
5204 mval[-1].br_startoff + mval[-1].br_blockcount);
5205 mval[-1].br_blockcount += mval->br_blockcount;
5207 mval->br_startblock == DELAYSTARTBLOCK &&
5208 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5209 mval->br_startoff ==
5210 mval[-1].br_startoff + mval[-1].br_blockcount) {
5211 mval[-1].br_blockcount += mval->br_blockcount;
5212 mval[-1].br_state = mval->br_state;
5213 } else if (!((n == 0) &&
5214 ((mval->br_startoff + mval->br_blockcount) <=
5220 * If we're done, stop now. Stop when we've allocated
5221 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5222 * the transaction may get too big.
5224 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5227 * Else go on to the next record.
5229 ep = xfs_iext_get_ext(ifp, ++lastx);
5231 if (lastx >= nextents)
5234 xfs_bmbt_get_all(ep, &got);
5236 ifp->if_lastex = lastx;
5239 * Transform from btree to extents, give it cur.
5241 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5242 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5244 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5245 &tmp_logflags, whichfork);
5246 logflags |= tmp_logflags;
5250 ASSERT(ifp->if_ext_max ==
5251 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5252 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5253 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5255 if (delta && delta->xed_startoff != NULLFILEOFF) {
5256 /* A change was actually made.
5257 * Note that delta->xed_blockount is an offset at this
5258 * point and needs to be converted to a block count.
5260 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5261 delta->xed_blockcount -= delta->xed_startoff;
5265 * Log everything. Do this after conversion, there's no point in
5266 * logging the extent records if we've converted to btree format.
5268 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5269 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5270 logflags &= ~XFS_ILOG_FEXT(whichfork);
5271 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5272 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5273 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5275 * Log whatever the flags say, even if error. Otherwise we might miss
5276 * detecting a case where the data is changed, there's an error,
5277 * and it's not logged so we don't shutdown when we should.
5281 xfs_trans_log_inode(tp, ip, logflags);
5285 ASSERT(*firstblock == NULLFSBLOCK ||
5286 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5288 cur->bc_private.b.firstblock) ||
5290 XFS_FSB_TO_AGNO(mp, *firstblock) <
5292 cur->bc_private.b.firstblock)));
5293 *firstblock = cur->bc_private.b.firstblock;
5295 xfs_btree_del_cursor(cur,
5296 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5299 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5305 * Map file blocks to filesystem blocks, simple version.
5306 * One block (extent) only, read-only.
5307 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5308 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5309 * was set and all the others were clear.
5313 xfs_trans_t *tp, /* transaction pointer */
5314 xfs_inode_t *ip, /* incore inode */
5315 int whichfork, /* data or attr fork */
5316 xfs_fsblock_t *fsb, /* output: mapped block */
5317 xfs_fileoff_t bno) /* starting file offs. mapped */
5319 int eof; /* we've hit the end of extents */
5320 int error; /* error return */
5321 xfs_bmbt_irec_t got; /* current file extent record */
5322 xfs_ifork_t *ifp; /* inode fork pointer */
5323 xfs_extnum_t lastx; /* last useful extent number */
5324 xfs_bmbt_irec_t prev; /* previous file extent record */
5326 ifp = XFS_IFORK_PTR(ip, whichfork);
5328 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5329 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5330 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5332 return XFS_ERROR(EFSCORRUPTED);
5334 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5335 return XFS_ERROR(EIO);
5336 XFS_STATS_INC(xs_blk_mapr);
5337 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5338 (error = xfs_iread_extents(tp, ip, whichfork)))
5340 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5343 * Reading past eof, act as though there's a hole
5346 if (eof || got.br_startoff > bno) {
5350 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
5351 ASSERT(bno < got.br_startoff + got.br_blockcount);
5352 *fsb = got.br_startblock + (bno - got.br_startoff);
5353 ifp->if_lastex = lastx;
5358 * Unmap (remove) blocks from a file.
5359 * If nexts is nonzero then the number of extents to remove is limited to
5360 * that value. If not all extents in the block range can be removed then
5365 xfs_trans_t *tp, /* transaction pointer */
5366 struct xfs_inode *ip, /* incore inode */
5367 xfs_fileoff_t bno, /* starting offset to unmap */
5368 xfs_filblks_t len, /* length to unmap in file */
5369 int flags, /* misc flags */
5370 xfs_extnum_t nexts, /* number of extents max */
5371 xfs_fsblock_t *firstblock, /* first allocated block
5372 controls a.g. for allocs */
5373 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5374 xfs_extdelta_t *delta, /* o: change made to incore
5376 int *done) /* set if not done yet */
5378 xfs_btree_cur_t *cur; /* bmap btree cursor */
5379 xfs_bmbt_irec_t del; /* extent being deleted */
5380 int eof; /* is deleting at eof */
5381 xfs_bmbt_rec_t *ep; /* extent record pointer */
5382 int error; /* error return value */
5383 xfs_extnum_t extno; /* extent number in list */
5384 xfs_bmbt_irec_t got; /* current extent record */
5385 xfs_ifork_t *ifp; /* inode fork pointer */
5386 int isrt; /* freeing in rt area */
5387 xfs_extnum_t lastx; /* last extent index used */
5388 int logflags; /* transaction logging flags */
5389 xfs_extlen_t mod; /* rt extent offset */
5390 xfs_mount_t *mp; /* mount structure */
5391 xfs_extnum_t nextents; /* number of file extents */
5392 xfs_bmbt_irec_t prev; /* previous extent record */
5393 xfs_fileoff_t start; /* first file offset deleted */
5394 int tmp_logflags; /* partial logging flags */
5395 int wasdel; /* was a delayed alloc extent */
5396 int whichfork; /* data or attribute fork */
5397 int rsvd; /* OK to allocate reserved blocks */
5400 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5401 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5402 XFS_ATTR_FORK : XFS_DATA_FORK;
5403 ifp = XFS_IFORK_PTR(ip, whichfork);
5405 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5406 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5407 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5409 return XFS_ERROR(EFSCORRUPTED);
5412 if (XFS_FORCED_SHUTDOWN(mp))
5413 return XFS_ERROR(EIO);
5414 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5417 ASSERT(ifp->if_ext_max ==
5418 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5419 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5420 (error = xfs_iread_extents(tp, ip, whichfork)))
5422 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5423 if (nextents == 0) {
5427 XFS_STATS_INC(xs_blk_unmap);
5428 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5430 bno = start + len - 1;
5431 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5434 delta->xed_startoff = NULLFILEOFF;
5435 delta->xed_blockcount = 0;
5438 * Check to see if the given block number is past the end of the
5439 * file, back up to the last block if so...
5442 ep = xfs_iext_get_ext(ifp, --lastx);
5443 xfs_bmbt_get_all(ep, &got);
5444 bno = got.br_startoff + got.br_blockcount - 1;
5447 if (ifp->if_flags & XFS_IFBROOT) {
5448 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5449 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
5451 cur->bc_private.b.firstblock = *firstblock;
5452 cur->bc_private.b.flist = flist;
5453 cur->bc_private.b.flags = 0;
5457 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5458 (nexts == 0 || extno < nexts)) {
5460 * Is the found extent after a hole in which bno lives?
5461 * Just back up to the previous extent, if so.
5463 if (got.br_startoff > bno) {
5466 ep = xfs_iext_get_ext(ifp, lastx);
5467 xfs_bmbt_get_all(ep, &got);
5470 * Is the last block of this extent before the range
5471 * we're supposed to delete? If so, we're done.
5473 bno = XFS_FILEOFF_MIN(bno,
5474 got.br_startoff + got.br_blockcount - 1);
5478 * Then deal with the (possibly delayed) allocated space
5483 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
5484 if (got.br_startoff < start) {
5485 del.br_startoff = start;
5486 del.br_blockcount -= start - got.br_startoff;
5488 del.br_startblock += start - got.br_startoff;
5490 if (del.br_startoff + del.br_blockcount > bno + 1)
5491 del.br_blockcount = bno + 1 - del.br_startoff;
5492 sum = del.br_startblock + del.br_blockcount;
5494 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5496 * Realtime extent not lined up at the end.
5497 * The extent could have been split into written
5498 * and unwritten pieces, or we could just be
5499 * unmapping part of it. But we can't really
5500 * get rid of part of a realtime extent.
5502 if (del.br_state == XFS_EXT_UNWRITTEN ||
5503 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5505 * This piece is unwritten, or we're not
5506 * using unwritten extents. Skip over it.
5509 bno -= mod > del.br_blockcount ?
5510 del.br_blockcount : mod;
5511 if (bno < got.br_startoff) {
5513 xfs_bmbt_get_all(xfs_iext_get_ext(
5519 * It's written, turn it unwritten.
5520 * This is better than zeroing it.
5522 ASSERT(del.br_state == XFS_EXT_NORM);
5523 ASSERT(xfs_trans_get_block_res(tp) > 0);
5525 * If this spans a realtime extent boundary,
5526 * chop it back to the start of the one we end at.
5528 if (del.br_blockcount > mod) {
5529 del.br_startoff += del.br_blockcount - mod;
5530 del.br_startblock += del.br_blockcount - mod;
5531 del.br_blockcount = mod;
5533 del.br_state = XFS_EXT_UNWRITTEN;
5534 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5535 firstblock, flist, &logflags, delta,
5541 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5543 * Realtime extent is lined up at the end but not
5544 * at the front. We'll get rid of full extents if
5547 mod = mp->m_sb.sb_rextsize - mod;
5548 if (del.br_blockcount > mod) {
5549 del.br_blockcount -= mod;
5550 del.br_startoff += mod;
5551 del.br_startblock += mod;
5552 } else if ((del.br_startoff == start &&
5553 (del.br_state == XFS_EXT_UNWRITTEN ||
5554 xfs_trans_get_block_res(tp) == 0)) ||
5555 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5557 * Can't make it unwritten. There isn't
5558 * a full extent here so just skip it.
5560 ASSERT(bno >= del.br_blockcount);
5561 bno -= del.br_blockcount;
5562 if (bno < got.br_startoff) {
5564 xfs_bmbt_get_all(--ep, &got);
5567 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5569 * This one is already unwritten.
5570 * It must have a written left neighbor.
5571 * Unwrite the killed part of that one and
5575 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5577 ASSERT(prev.br_state == XFS_EXT_NORM);
5578 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
5579 ASSERT(del.br_startblock ==
5580 prev.br_startblock + prev.br_blockcount);
5581 if (prev.br_startoff < start) {
5582 mod = start - prev.br_startoff;
5583 prev.br_blockcount -= mod;
5584 prev.br_startblock += mod;
5585 prev.br_startoff = start;
5587 prev.br_state = XFS_EXT_UNWRITTEN;
5588 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5589 &prev, firstblock, flist, &logflags,
5590 delta, XFS_DATA_FORK, 0);
5595 ASSERT(del.br_state == XFS_EXT_NORM);
5596 del.br_state = XFS_EXT_UNWRITTEN;
5597 error = xfs_bmap_add_extent(ip, lastx, &cur,
5598 &del, firstblock, flist, &logflags,
5599 delta, XFS_DATA_FORK, 0);
5606 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
5607 /* Update realtime/data freespace, unreserve quota */
5609 xfs_filblks_t rtexts;
5611 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5612 do_div(rtexts, mp->m_sb.sb_rextsize);
5613 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5614 (int64_t)rtexts, rsvd);
5615 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5616 NULL, ip, -((long)del.br_blockcount), 0,
5617 XFS_QMOPT_RES_RTBLKS);
5619 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
5620 (int64_t)del.br_blockcount, rsvd);
5621 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5622 NULL, ip, -((long)del.br_blockcount), 0,
5623 XFS_QMOPT_RES_REGBLKS);
5625 ip->i_delayed_blks -= del.br_blockcount;
5627 cur->bc_private.b.flags |=
5628 XFS_BTCUR_BPRV_WASDEL;
5630 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5632 * If it's the case where the directory code is running
5633 * with no block reservation, and the deleted block is in
5634 * the middle of its extent, and the resulting insert
5635 * of an extent would cause transformation to btree format,
5636 * then reject it. The calling code will then swap
5637 * blocks around instead.
5638 * We have to do this now, rather than waiting for the
5639 * conversion to btree format, since the transaction
5642 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5643 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5644 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5645 del.br_startoff > got.br_startoff &&
5646 del.br_startoff + del.br_blockcount <
5647 got.br_startoff + got.br_blockcount) {
5648 error = XFS_ERROR(ENOSPC);
5651 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5652 &tmp_logflags, delta, whichfork, rsvd);
5653 logflags |= tmp_logflags;
5656 bno = del.br_startoff - 1;
5658 lastx = ifp->if_lastex;
5660 * If not done go on to the next (previous) record.
5661 * Reset ep in case the extents array was re-alloced.
5663 ep = xfs_iext_get_ext(ifp, lastx);
5664 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5665 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5666 xfs_bmbt_get_startoff(ep) > bno) {
5668 ep = xfs_iext_get_ext(ifp, lastx);
5671 xfs_bmbt_get_all(ep, &got);
5675 ifp->if_lastex = lastx;
5676 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5677 ASSERT(ifp->if_ext_max ==
5678 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5680 * Convert to a btree if necessary.
5682 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5683 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5684 ASSERT(cur == NULL);
5685 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5686 &cur, 0, &tmp_logflags, whichfork);
5687 logflags |= tmp_logflags;
5692 * transform from btree to extents, give it cur
5694 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5695 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5696 ASSERT(cur != NULL);
5697 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5699 logflags |= tmp_logflags;
5704 * transform from extents to local?
5706 ASSERT(ifp->if_ext_max ==
5707 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5709 if (delta && delta->xed_startoff != NULLFILEOFF) {
5710 /* A change was actually made.
5711 * Note that delta->xed_blockount is an offset at this
5712 * point and needs to be converted to a block count.
5714 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5715 delta->xed_blockcount -= delta->xed_startoff;
5719 * Log everything. Do this after conversion, there's no point in
5720 * logging the extent records if we've converted to btree format.
5722 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5723 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5724 logflags &= ~XFS_ILOG_FEXT(whichfork);
5725 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5726 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5727 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5729 * Log inode even in the error case, if the transaction
5730 * is dirty we'll need to shut down the filesystem.
5733 xfs_trans_log_inode(tp, ip, logflags);
5736 *firstblock = cur->bc_private.b.firstblock;
5737 cur->bc_private.b.allocated = 0;
5739 xfs_btree_del_cursor(cur,
5740 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5746 * Fcntl interface to xfs_bmapi.
5748 int /* error code */
5750 bhv_desc_t *bdp, /* XFS behavior descriptor*/
5751 struct getbmap *bmv, /* user bmap structure */
5752 void __user *ap, /* pointer to user's array */
5753 int interface) /* interface flags */
5755 __int64_t bmvend; /* last block requested */
5756 int error; /* return value */
5757 __int64_t fixlen; /* length for -1 case */
5758 int i; /* extent number */
5759 xfs_inode_t *ip; /* xfs incore inode pointer */
5760 bhv_vnode_t *vp; /* corresponding vnode */
5761 int lock; /* lock state */
5762 xfs_bmbt_irec_t *map; /* buffer for user's data */
5763 xfs_mount_t *mp; /* file system mount point */
5764 int nex; /* # of user extents can do */
5765 int nexleft; /* # of user extents left */
5766 int subnex; /* # of bmapi's can do */
5767 int nmap; /* number of map entries */
5768 struct getbmap out; /* output structure */
5769 int whichfork; /* data or attr fork */
5770 int prealloced; /* this is a file with
5771 * preallocated data space */
5772 int sh_unwritten; /* true, if unwritten */
5773 /* extents listed separately */
5774 int bmapi_flags; /* flags for xfs_bmapi */
5775 __int32_t oflags; /* getbmapx bmv_oflags field */
5777 vp = BHV_TO_VNODE(bdp);
5778 ip = XFS_BHVTOI(bdp);
5781 whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5782 sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
5784 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5785 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5786 * bit is set for the file, generate a read event in order
5787 * that the DMAPI application may do its thing before we return
5788 * the extents. Usually this means restoring user file data to
5789 * regions of the file that look like holes.
5791 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5792 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5793 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5794 * could misinterpret holes in a DMAPI file as true holes,
5795 * when in fact they may represent offline user data.
5797 if ( (interface & BMV_IF_NO_DMAPI_READ) == 0
5798 && DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)
5799 && whichfork == XFS_DATA_FORK) {
5801 error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, 0, 0, 0, NULL);
5803 return XFS_ERROR(error);
5806 if (whichfork == XFS_ATTR_FORK) {
5807 if (XFS_IFORK_Q(ip)) {
5808 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5809 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5810 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5811 return XFS_ERROR(EINVAL);
5812 } else if (unlikely(
5813 ip->i_d.di_aformat != 0 &&
5814 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5815 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5817 return XFS_ERROR(EFSCORRUPTED);
5819 } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5820 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5821 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5822 return XFS_ERROR(EINVAL);
5823 if (whichfork == XFS_DATA_FORK) {
5824 if (xfs_get_extsz_hint(ip) ||
5825 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5827 fixlen = XFS_MAXIOFFSET(mp);
5830 fixlen = ip->i_size;
5837 if (bmv->bmv_length == -1) {
5838 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5839 bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
5841 } else if (bmv->bmv_length < 0)
5842 return XFS_ERROR(EINVAL);
5843 if (bmv->bmv_length == 0) {
5844 bmv->bmv_entries = 0;
5847 nex = bmv->bmv_count - 1;
5849 return XFS_ERROR(EINVAL);
5850 bmvend = bmv->bmv_offset + bmv->bmv_length;
5852 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5854 if (whichfork == XFS_DATA_FORK &&
5855 (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size)) {
5856 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
5857 error = bhv_vop_flush_pages(vp, (xfs_off_t)0, -1, 0, FI_REMAPF);
5860 ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
5862 lock = xfs_ilock_map_shared(ip);
5865 * Don't let nex be bigger than the number of extents
5866 * we can have assuming alternating holes and real extents.
5868 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5869 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5871 bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
5872 ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
5875 * Allocate enough space to handle "subnex" maps at a time.
5878 map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
5880 bmv->bmv_entries = 0;
5882 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
5884 goto unlock_and_return;
5890 nmap = (nexleft > subnex) ? subnex : nexleft;
5891 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5892 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5893 bmapi_flags, NULL, 0, map, &nmap,
5896 goto unlock_and_return;
5897 ASSERT(nmap <= subnex);
5899 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5901 oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
5902 BMV_OF_PREALLOC : 0;
5903 out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
5904 out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5905 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
5906 if (map[i].br_startblock == HOLESTARTBLOCK &&
5907 ((prealloced && out.bmv_offset + out.bmv_length == bmvend) ||
5908 whichfork == XFS_ATTR_FORK )) {
5910 * came to hole at end of file or the end of
5913 goto unlock_and_return;
5916 (map[i].br_startblock == HOLESTARTBLOCK) ?
5918 XFS_FSB_TO_DB(ip, map[i].br_startblock);
5920 /* return either getbmap/getbmapx structure. */
5921 if (interface & BMV_IF_EXTENDED) {
5922 struct getbmapx outx;
5924 GETBMAP_CONVERT(out,outx);
5925 outx.bmv_oflags = oflags;
5926 outx.bmv_unused1 = outx.bmv_unused2 = 0;
5927 if (copy_to_user(ap, &outx,
5929 error = XFS_ERROR(EFAULT);
5930 goto unlock_and_return;
5933 if (copy_to_user(ap, &out,
5935 error = XFS_ERROR(EFAULT);
5936 goto unlock_and_return;
5940 out.bmv_offset + out.bmv_length;
5941 bmv->bmv_length = MAX((__int64_t)0,
5942 (__int64_t)(bmvend - bmv->bmv_offset));
5944 ap = (interface & BMV_IF_EXTENDED) ?
5946 ((struct getbmapx __user *)ap + 1) :
5948 ((struct getbmap __user *)ap + 1);
5951 } while (nmap && nexleft && bmv->bmv_length);
5954 xfs_iunlock_map_shared(ip, lock);
5955 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5957 kmem_free(map, subnex * sizeof(*map));
5963 * Check the last inode extent to determine whether this allocation will result
5964 * in blocks being allocated at the end of the file. When we allocate new data
5965 * blocks at the end of the file which do not start at the previous data block,
5966 * we will try to align the new blocks at stripe unit boundaries.
5968 STATIC int /* error */
5970 xfs_inode_t *ip, /* incore inode pointer */
5971 xfs_fileoff_t off, /* file offset in fsblocks */
5972 int whichfork, /* data or attribute fork */
5973 char *aeof) /* return value */
5975 int error; /* error return value */
5976 xfs_ifork_t *ifp; /* inode fork pointer */
5977 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
5978 xfs_extnum_t nextents; /* number of file extents */
5979 xfs_bmbt_irec_t s; /* expanded extent record */
5981 ASSERT(whichfork == XFS_DATA_FORK);
5982 ifp = XFS_IFORK_PTR(ip, whichfork);
5983 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5984 (error = xfs_iread_extents(NULL, ip, whichfork)))
5986 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5987 if (nextents == 0) {
5992 * Go to the last extent
5994 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5995 xfs_bmbt_get_all(lastrec, &s);
5997 * Check we are allocating in the last extent (for delayed allocations)
5998 * or past the last extent for non-delayed allocations.
6000 *aeof = (off >= s.br_startoff &&
6001 off < s.br_startoff + s.br_blockcount &&
6002 ISNULLSTARTBLOCK(s.br_startblock)) ||
6003 off >= s.br_startoff + s.br_blockcount;
6008 * Check if the endoff is outside the last extent. If so the caller will grow
6009 * the allocation to a stripe unit boundary.
6013 xfs_inode_t *ip, /* incore inode pointer */
6014 xfs_fileoff_t endoff, /* file offset in fsblocks */
6015 int whichfork, /* data or attribute fork */
6016 int *eof) /* result value */
6018 xfs_fsblock_t blockcount; /* extent block count */
6019 int error; /* error return value */
6020 xfs_ifork_t *ifp; /* inode fork pointer */
6021 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
6022 xfs_extnum_t nextents; /* number of file extents */
6023 xfs_fileoff_t startoff; /* extent starting file offset */
6025 ASSERT(whichfork == XFS_DATA_FORK);
6026 ifp = XFS_IFORK_PTR(ip, whichfork);
6027 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6028 (error = xfs_iread_extents(NULL, ip, whichfork)))
6030 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6031 if (nextents == 0) {
6036 * Go to the last extent
6038 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
6039 startoff = xfs_bmbt_get_startoff(lastrec);
6040 blockcount = xfs_bmbt_get_blockcount(lastrec);
6041 *eof = endoff >= startoff + blockcount;
6049 xfs_btree_cur_t *cur,
6059 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6060 bp = cur->bc_bufs[i];
6062 if (XFS_BUF_ADDR(bp) == bno)
6063 break; /* Found it */
6065 if (i == XFS_BTREE_MAXLEVELS)
6068 if (!bp) { /* Chase down all the log items to see if the bp is there */
6069 xfs_log_item_chunk_t *licp;
6073 licp = &tp->t_items;
6074 while (!bp && licp != NULL) {
6075 if (XFS_LIC_ARE_ALL_FREE(licp)) {
6076 licp = licp->lic_next;
6079 for (i = 0; i < licp->lic_unused; i++) {
6080 xfs_log_item_desc_t *lidp;
6081 xfs_log_item_t *lip;
6082 xfs_buf_log_item_t *bip;
6085 if (XFS_LIC_ISFREE(licp, i)) {
6089 lidp = XFS_LIC_SLOT(licp, i);
6090 lip = lidp->lid_item;
6091 if (lip->li_type != XFS_LI_BUF)
6094 bip = (xfs_buf_log_item_t *)lip;
6097 if (XFS_BUF_ADDR(lbp) == bno) {
6099 break; /* Found it */
6102 licp = licp->lic_next;
6110 xfs_bmbt_block_t *block,
6116 __be64 *pp, *thispa; /* pointer to block address */
6117 xfs_bmbt_key_t *prevp, *keyp;
6119 ASSERT(be16_to_cpu(block->bb_level) > 0);
6122 for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
6123 dmxr = mp->m_bmap_dmxr[0];
6126 keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
6128 keyp = XFS_BTREE_KEY_ADDR(xfs_bmbt, block, i);
6132 xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
6137 * Compare the block numbers to see if there are dups.
6141 pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
6143 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, i, dmxr);
6145 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
6147 thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
6149 thispa = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, j,
6152 if (*thispa == *pp) {
6153 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
6155 (unsigned long long)be64_to_cpu(*thispa));
6156 panic("%s: ptrs are equal in node\n",
6164 * Check that the extents for the inode ip are in the right order in all
6169 xfs_bmap_check_leaf_extents(
6170 xfs_btree_cur_t *cur, /* btree cursor or null */
6171 xfs_inode_t *ip, /* incore inode pointer */
6172 int whichfork) /* data or attr fork */
6174 xfs_bmbt_block_t *block; /* current btree block */
6175 xfs_fsblock_t bno; /* block # of "block" */
6176 xfs_buf_t *bp; /* buffer for "block" */
6177 int error; /* error return value */
6178 xfs_extnum_t i=0, j; /* index into the extents list */
6179 xfs_ifork_t *ifp; /* fork structure */
6180 int level; /* btree level, for checking */
6181 xfs_mount_t *mp; /* file system mount structure */
6182 __be64 *pp; /* pointer to block address */
6183 xfs_bmbt_rec_t *ep; /* pointer to current extent */
6184 xfs_bmbt_rec_t *lastp; /* pointer to previous extent */
6185 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
6188 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6194 ifp = XFS_IFORK_PTR(ip, whichfork);
6195 block = ifp->if_broot;
6197 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6199 level = be16_to_cpu(block->bb_level);
6201 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6202 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6203 bno = be64_to_cpu(*pp);
6205 ASSERT(bno != NULLDFSBNO);
6206 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6207 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6210 * Go down the tree until leaf level is reached, following the first
6211 * pointer (leftmost) at each level.
6213 while (level-- > 0) {
6214 /* See if buf is in cur first */
6215 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6221 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6222 XFS_BMAP_BTREE_REF)))
6224 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6225 XFS_WANT_CORRUPTED_GOTO(
6226 XFS_BMAP_SANITY_CHECK(mp, block, level),
6232 * Check this block for basic sanity (increasing keys and
6233 * no duplicate blocks).
6236 xfs_check_block(block, mp, 0, 0);
6237 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
6238 bno = be64_to_cpu(*pp);
6239 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
6242 xfs_trans_brelse(NULL, bp);
6247 * Here with bp and block set to the leftmost leaf node in the tree.
6252 * Loop over all leaf nodes checking that all extents are in the right order.
6256 xfs_fsblock_t nextbno;
6257 xfs_extnum_t num_recs;
6260 num_recs = be16_to_cpu(block->bb_numrecs);
6263 * Read-ahead the next leaf block, if any.
6266 nextbno = be64_to_cpu(block->bb_rightsib);
6269 * Check all the extents to make sure they are OK.
6270 * If we had a previous block, the last entry should
6271 * conform with the first entry in this one.
6274 ep = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
6275 for (j = 1; j < num_recs; j++) {
6276 nextp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, j + 1);
6278 xfs_btree_check_rec(XFS_BTNUM_BMAP,
6279 (void *)lastp, (void *)ep);
6281 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
6290 xfs_trans_brelse(NULL, bp);
6294 * If we've reached the end, stop.
6296 if (bno == NULLFSBLOCK)
6299 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6305 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6306 XFS_BMAP_BTREE_REF)))
6308 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6312 xfs_trans_brelse(NULL, bp);
6317 cmn_err(CE_WARN, "%s: at error0", __FUNCTION__);
6319 xfs_trans_brelse(NULL, bp);
6321 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
6323 panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__);
6329 * Count fsblocks of the given fork.
6332 xfs_bmap_count_blocks(
6333 xfs_trans_t *tp, /* transaction pointer */
6334 xfs_inode_t *ip, /* incore inode */
6335 int whichfork, /* data or attr fork */
6336 int *count) /* out: count of blocks */
6338 xfs_bmbt_block_t *block; /* current btree block */
6339 xfs_fsblock_t bno; /* block # of "block" */
6340 xfs_ifork_t *ifp; /* fork structure */
6341 int level; /* btree level, for checking */
6342 xfs_mount_t *mp; /* file system mount structure */
6343 __be64 *pp; /* pointer to block address */
6347 ifp = XFS_IFORK_PTR(ip, whichfork);
6348 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6349 if (unlikely(xfs_bmap_count_leaves(ifp, 0,
6350 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6352 XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)",
6353 XFS_ERRLEVEL_LOW, mp);
6354 return XFS_ERROR(EFSCORRUPTED);
6360 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6362 block = ifp->if_broot;
6363 level = be16_to_cpu(block->bb_level);
6365 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6366 bno = be64_to_cpu(*pp);
6367 ASSERT(bno != NULLDFSBNO);
6368 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6369 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6371 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
6372 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6374 return XFS_ERROR(EFSCORRUPTED);
6381 * Recursively walks each level of a btree
6382 * to count total fsblocks is use.
6385 xfs_bmap_count_tree(
6386 xfs_mount_t *mp, /* file system mount point */
6387 xfs_trans_t *tp, /* transaction pointer */
6388 xfs_ifork_t *ifp, /* inode fork pointer */
6389 xfs_fsblock_t blockno, /* file system block number */
6390 int levelin, /* level in btree */
6391 int *count) /* Count of blocks */
6394 xfs_buf_t *bp, *nbp;
6395 int level = levelin;
6397 xfs_fsblock_t bno = blockno;
6398 xfs_fsblock_t nextbno;
6399 xfs_bmbt_block_t *block, *nextblock;
6402 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6405 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6408 /* Not at node above leafs, count this level of nodes */
6409 nextbno = be64_to_cpu(block->bb_rightsib);
6410 while (nextbno != NULLFSBLOCK) {
6411 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6412 0, &nbp, XFS_BMAP_BTREE_REF)))
6415 nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
6416 nextbno = be64_to_cpu(nextblock->bb_rightsib);
6417 xfs_trans_brelse(tp, nbp);
6420 /* Dive to the next level */
6421 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
6422 bno = be64_to_cpu(*pp);
6423 if (unlikely((error =
6424 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6425 xfs_trans_brelse(tp, bp);
6426 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6427 XFS_ERRLEVEL_LOW, mp);
6428 return XFS_ERROR(EFSCORRUPTED);
6430 xfs_trans_brelse(tp, bp);
6432 /* count all level 1 nodes and their leaves */
6434 nextbno = be64_to_cpu(block->bb_rightsib);
6435 numrecs = be16_to_cpu(block->bb_numrecs);
6436 if (unlikely(xfs_bmap_disk_count_leaves(0,
6437 block, numrecs, count) < 0)) {
6438 xfs_trans_brelse(tp, bp);
6439 XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
6440 XFS_ERRLEVEL_LOW, mp);
6441 return XFS_ERROR(EFSCORRUPTED);
6443 xfs_trans_brelse(tp, bp);
6444 if (nextbno == NULLFSBLOCK)
6447 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6448 XFS_BMAP_BTREE_REF)))
6451 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6458 * Count leaf blocks given a range of extent records.
6461 xfs_bmap_count_leaves(
6468 xfs_bmbt_rec_t *frp;
6470 for (b = 0; b < numrecs; b++) {
6471 frp = xfs_iext_get_ext(ifp, idx + b);
6472 *count += xfs_bmbt_get_blockcount(frp);
6478 * Count leaf blocks given a range of extent records originally
6482 xfs_bmap_disk_count_leaves(
6484 xfs_bmbt_block_t *block,
6489 xfs_bmbt_rec_t *frp;
6491 for (b = 1; b <= numrecs; b++) {
6492 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, idx + b);
6493 *count += xfs_bmbt_disk_get_blockcount(frp);