[media] saa7134: use full keycode for BeholdTV
[linux-2.6.git] / fs / xfs / xfs_bmap.c
blob4111cd3966c764b3c41300d1e7a51a7579ead435
1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.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_dinode.h"
34 #include "xfs_inode.h"
35 #include "xfs_btree.h"
36 #include "xfs_mount.h"
37 #include "xfs_itable.h"
38 #include "xfs_dir2_data.h"
39 #include "xfs_dir2_leaf.h"
40 #include "xfs_dir2_block.h"
41 #include "xfs_inode_item.h"
42 #include "xfs_extfree_item.h"
43 #include "xfs_alloc.h"
44 #include "xfs_bmap.h"
45 #include "xfs_rtalloc.h"
46 #include "xfs_error.h"
47 #include "xfs_attr_leaf.h"
48 #include "xfs_rw.h"
49 #include "xfs_quota.h"
50 #include "xfs_trans_space.h"
51 #include "xfs_buf_item.h"
52 #include "xfs_filestream.h"
53 #include "xfs_vnodeops.h"
54 #include "xfs_trace.h"
57 #ifdef DEBUG
58 STATIC void
59 xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
60 #endif
62 kmem_zone_t *xfs_bmap_free_item_zone;
65 * Prototypes for internal bmap routines.
70 * Called from xfs_bmap_add_attrfork to handle extents format files.
72 STATIC int /* error */
73 xfs_bmap_add_attrfork_extents(
74 xfs_trans_t *tp, /* transaction pointer */
75 xfs_inode_t *ip, /* incore inode pointer */
76 xfs_fsblock_t *firstblock, /* first block allocated */
77 xfs_bmap_free_t *flist, /* blocks to free at commit */
78 int *flags); /* inode logging flags */
81 * Called from xfs_bmap_add_attrfork to handle local format files.
83 STATIC int /* error */
84 xfs_bmap_add_attrfork_local(
85 xfs_trans_t *tp, /* transaction pointer */
86 xfs_inode_t *ip, /* incore inode pointer */
87 xfs_fsblock_t *firstblock, /* first block allocated */
88 xfs_bmap_free_t *flist, /* blocks to free at commit */
89 int *flags); /* inode logging flags */
92 * Called by xfs_bmapi to update file extent records and the btree
93 * after allocating space (or doing a delayed allocation).
95 STATIC int /* error */
96 xfs_bmap_add_extent(
97 xfs_inode_t *ip, /* incore inode pointer */
98 xfs_extnum_t idx, /* extent number to update/insert */
99 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
100 xfs_bmbt_irec_t *new, /* new data to add to file extents */
101 xfs_fsblock_t *first, /* pointer to firstblock variable */
102 xfs_bmap_free_t *flist, /* list of extents to be freed */
103 int *logflagsp, /* inode logging flags */
104 int whichfork, /* data or attr fork */
105 int rsvd); /* OK to allocate reserved blocks */
108 * Called by xfs_bmap_add_extent to handle cases converting a delayed
109 * allocation to a real allocation.
111 STATIC int /* error */
112 xfs_bmap_add_extent_delay_real(
113 xfs_inode_t *ip, /* incore inode pointer */
114 xfs_extnum_t idx, /* extent number to update/insert */
115 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
116 xfs_bmbt_irec_t *new, /* new data to add to file extents */
117 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
118 xfs_fsblock_t *first, /* pointer to firstblock variable */
119 xfs_bmap_free_t *flist, /* list of extents to be freed */
120 int *logflagsp, /* inode logging flags */
121 int rsvd); /* OK to allocate reserved blocks */
124 * Called by xfs_bmap_add_extent to handle cases converting a hole
125 * to a delayed allocation.
127 STATIC int /* error */
128 xfs_bmap_add_extent_hole_delay(
129 xfs_inode_t *ip, /* incore inode pointer */
130 xfs_extnum_t idx, /* extent number to update/insert */
131 xfs_bmbt_irec_t *new, /* new data to add to file extents */
132 int *logflagsp,/* inode logging flags */
133 int rsvd); /* OK to allocate reserved blocks */
136 * Called by xfs_bmap_add_extent to handle cases converting a hole
137 * to a real allocation.
139 STATIC int /* error */
140 xfs_bmap_add_extent_hole_real(
141 xfs_inode_t *ip, /* incore inode pointer */
142 xfs_extnum_t idx, /* extent number to update/insert */
143 xfs_btree_cur_t *cur, /* if null, not a btree */
144 xfs_bmbt_irec_t *new, /* new data to add to file extents */
145 int *logflagsp, /* inode logging flags */
146 int whichfork); /* data or attr fork */
149 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
150 * allocation to a real allocation or vice versa.
152 STATIC int /* error */
153 xfs_bmap_add_extent_unwritten_real(
154 xfs_inode_t *ip, /* incore inode pointer */
155 xfs_extnum_t idx, /* extent number to update/insert */
156 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
157 xfs_bmbt_irec_t *new, /* new data to add to file extents */
158 int *logflagsp); /* inode logging flags */
161 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
162 * It figures out where to ask the underlying allocator to put the new extent.
164 STATIC int /* error */
165 xfs_bmap_alloc(
166 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
169 * Transform a btree format file with only one leaf node, where the
170 * extents list will fit in the inode, into an extents format file.
171 * Since the file extents are already in-core, all we have to do is
172 * give up the space for the btree root and pitch the leaf block.
174 STATIC int /* error */
175 xfs_bmap_btree_to_extents(
176 xfs_trans_t *tp, /* transaction pointer */
177 xfs_inode_t *ip, /* incore inode pointer */
178 xfs_btree_cur_t *cur, /* btree cursor */
179 int *logflagsp, /* inode logging flags */
180 int whichfork); /* data or attr fork */
183 * Called by xfs_bmapi to update file extent records and the btree
184 * after removing space (or undoing a delayed allocation).
186 STATIC int /* error */
187 xfs_bmap_del_extent(
188 xfs_inode_t *ip, /* incore inode pointer */
189 xfs_trans_t *tp, /* current trans pointer */
190 xfs_extnum_t idx, /* extent number to update/insert */
191 xfs_bmap_free_t *flist, /* list of extents to be freed */
192 xfs_btree_cur_t *cur, /* if null, not a btree */
193 xfs_bmbt_irec_t *new, /* new data to add to file extents */
194 int *logflagsp,/* inode logging flags */
195 int whichfork, /* data or attr fork */
196 int rsvd); /* OK to allocate reserved blocks */
199 * Remove the entry "free" from the free item list. Prev points to the
200 * previous entry, unless "free" is the head of the list.
202 STATIC void
203 xfs_bmap_del_free(
204 xfs_bmap_free_t *flist, /* free item list header */
205 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
206 xfs_bmap_free_item_t *free); /* list item to be freed */
209 * Convert an extents-format file into a btree-format file.
210 * The new file will have a root block (in the inode) and a single child block.
212 STATIC int /* error */
213 xfs_bmap_extents_to_btree(
214 xfs_trans_t *tp, /* transaction pointer */
215 xfs_inode_t *ip, /* incore inode pointer */
216 xfs_fsblock_t *firstblock, /* first-block-allocated */
217 xfs_bmap_free_t *flist, /* blocks freed in xaction */
218 xfs_btree_cur_t **curp, /* cursor returned to caller */
219 int wasdel, /* converting a delayed alloc */
220 int *logflagsp, /* inode logging flags */
221 int whichfork); /* data or attr fork */
224 * Convert a local file to an extents file.
225 * This code is sort of bogus, since the file data needs to get
226 * logged so it won't be lost. The bmap-level manipulations are ok, though.
228 STATIC int /* error */
229 xfs_bmap_local_to_extents(
230 xfs_trans_t *tp, /* transaction pointer */
231 xfs_inode_t *ip, /* incore inode pointer */
232 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
233 xfs_extlen_t total, /* total blocks needed by transaction */
234 int *logflagsp, /* inode logging flags */
235 int whichfork); /* data or attr fork */
238 * Search the extents list for the inode, for the extent containing bno.
239 * If bno lies in a hole, point to the next entry. If bno lies past eof,
240 * *eofp will be set, and *prevp will contain the last entry (null if none).
241 * Else, *lastxp will be set to the index of the found
242 * entry; *gotp will contain the entry.
244 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
245 xfs_bmap_search_extents(
246 xfs_inode_t *ip, /* incore inode pointer */
247 xfs_fileoff_t bno, /* block number searched for */
248 int whichfork, /* data or attr fork */
249 int *eofp, /* out: end of file found */
250 xfs_extnum_t *lastxp, /* out: last extent index */
251 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
252 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
255 * Check the last inode extent to determine whether this allocation will result
256 * in blocks being allocated at the end of the file. When we allocate new data
257 * blocks at the end of the file which do not start at the previous data block,
258 * we will try to align the new blocks at stripe unit boundaries.
260 STATIC int /* error */
261 xfs_bmap_isaeof(
262 xfs_inode_t *ip, /* incore inode pointer */
263 xfs_fileoff_t off, /* file offset in fsblocks */
264 int whichfork, /* data or attribute fork */
265 char *aeof); /* return value */
268 * Compute the worst-case number of indirect blocks that will be used
269 * for ip's delayed extent of length "len".
271 STATIC xfs_filblks_t
272 xfs_bmap_worst_indlen(
273 xfs_inode_t *ip, /* incore inode pointer */
274 xfs_filblks_t len); /* delayed extent length */
276 #ifdef DEBUG
278 * Perform various validation checks on the values being returned
279 * from xfs_bmapi().
281 STATIC void
282 xfs_bmap_validate_ret(
283 xfs_fileoff_t bno,
284 xfs_filblks_t len,
285 int flags,
286 xfs_bmbt_irec_t *mval,
287 int nmap,
288 int ret_nmap);
289 #else
290 #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
291 #endif /* DEBUG */
293 STATIC int
294 xfs_bmap_count_tree(
295 xfs_mount_t *mp,
296 xfs_trans_t *tp,
297 xfs_ifork_t *ifp,
298 xfs_fsblock_t blockno,
299 int levelin,
300 int *count);
302 STATIC void
303 xfs_bmap_count_leaves(
304 xfs_ifork_t *ifp,
305 xfs_extnum_t idx,
306 int numrecs,
307 int *count);
309 STATIC void
310 xfs_bmap_disk_count_leaves(
311 struct xfs_mount *mp,
312 struct xfs_btree_block *block,
313 int numrecs,
314 int *count);
317 * Bmap internal routines.
320 STATIC int /* error */
321 xfs_bmbt_lookup_eq(
322 struct xfs_btree_cur *cur,
323 xfs_fileoff_t off,
324 xfs_fsblock_t bno,
325 xfs_filblks_t len,
326 int *stat) /* success/failure */
328 cur->bc_rec.b.br_startoff = off;
329 cur->bc_rec.b.br_startblock = bno;
330 cur->bc_rec.b.br_blockcount = len;
331 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
334 STATIC int /* error */
335 xfs_bmbt_lookup_ge(
336 struct xfs_btree_cur *cur,
337 xfs_fileoff_t off,
338 xfs_fsblock_t bno,
339 xfs_filblks_t len,
340 int *stat) /* success/failure */
342 cur->bc_rec.b.br_startoff = off;
343 cur->bc_rec.b.br_startblock = bno;
344 cur->bc_rec.b.br_blockcount = len;
345 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
349 * Update the record referred to by cur to the value given
350 * by [off, bno, len, state].
351 * This either works (return 0) or gets an EFSCORRUPTED error.
353 STATIC int
354 xfs_bmbt_update(
355 struct xfs_btree_cur *cur,
356 xfs_fileoff_t off,
357 xfs_fsblock_t bno,
358 xfs_filblks_t len,
359 xfs_exntst_t state)
361 union xfs_btree_rec rec;
363 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
364 return xfs_btree_update(cur, &rec);
368 * Called from xfs_bmap_add_attrfork to handle btree format files.
370 STATIC int /* error */
371 xfs_bmap_add_attrfork_btree(
372 xfs_trans_t *tp, /* transaction pointer */
373 xfs_inode_t *ip, /* incore inode pointer */
374 xfs_fsblock_t *firstblock, /* first block allocated */
375 xfs_bmap_free_t *flist, /* blocks to free at commit */
376 int *flags) /* inode logging flags */
378 xfs_btree_cur_t *cur; /* btree cursor */
379 int error; /* error return value */
380 xfs_mount_t *mp; /* file system mount struct */
381 int stat; /* newroot status */
383 mp = ip->i_mount;
384 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
385 *flags |= XFS_ILOG_DBROOT;
386 else {
387 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
388 cur->bc_private.b.flist = flist;
389 cur->bc_private.b.firstblock = *firstblock;
390 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
391 goto error0;
392 /* must be at least one entry */
393 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
394 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
395 goto error0;
396 if (stat == 0) {
397 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
398 return XFS_ERROR(ENOSPC);
400 *firstblock = cur->bc_private.b.firstblock;
401 cur->bc_private.b.allocated = 0;
402 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
404 return 0;
405 error0:
406 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
407 return error;
411 * Called from xfs_bmap_add_attrfork to handle extents format files.
413 STATIC int /* error */
414 xfs_bmap_add_attrfork_extents(
415 xfs_trans_t *tp, /* transaction pointer */
416 xfs_inode_t *ip, /* incore inode pointer */
417 xfs_fsblock_t *firstblock, /* first block allocated */
418 xfs_bmap_free_t *flist, /* blocks to free at commit */
419 int *flags) /* inode logging flags */
421 xfs_btree_cur_t *cur; /* bmap btree cursor */
422 int error; /* error return value */
424 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
425 return 0;
426 cur = NULL;
427 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
428 flags, XFS_DATA_FORK);
429 if (cur) {
430 cur->bc_private.b.allocated = 0;
431 xfs_btree_del_cursor(cur,
432 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
434 return error;
438 * Called from xfs_bmap_add_attrfork to handle local format files.
440 STATIC int /* error */
441 xfs_bmap_add_attrfork_local(
442 xfs_trans_t *tp, /* transaction pointer */
443 xfs_inode_t *ip, /* incore inode pointer */
444 xfs_fsblock_t *firstblock, /* first block allocated */
445 xfs_bmap_free_t *flist, /* blocks to free at commit */
446 int *flags) /* inode logging flags */
448 xfs_da_args_t dargs; /* args for dir/attr code */
449 int error; /* error return value */
450 xfs_mount_t *mp; /* mount structure pointer */
452 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
453 return 0;
454 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
455 mp = ip->i_mount;
456 memset(&dargs, 0, sizeof(dargs));
457 dargs.dp = ip;
458 dargs.firstblock = firstblock;
459 dargs.flist = flist;
460 dargs.total = mp->m_dirblkfsbs;
461 dargs.whichfork = XFS_DATA_FORK;
462 dargs.trans = tp;
463 error = xfs_dir2_sf_to_block(&dargs);
464 } else
465 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
466 XFS_DATA_FORK);
467 return error;
471 * Called by xfs_bmapi to update file extent records and the btree
472 * after allocating space (or doing a delayed allocation).
474 STATIC int /* error */
475 xfs_bmap_add_extent(
476 xfs_inode_t *ip, /* incore inode pointer */
477 xfs_extnum_t idx, /* extent number to update/insert */
478 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
479 xfs_bmbt_irec_t *new, /* new data to add to file extents */
480 xfs_fsblock_t *first, /* pointer to firstblock variable */
481 xfs_bmap_free_t *flist, /* list of extents to be freed */
482 int *logflagsp, /* inode logging flags */
483 int whichfork, /* data or attr fork */
484 int rsvd) /* OK to use reserved data blocks */
486 xfs_btree_cur_t *cur; /* btree cursor or null */
487 xfs_filblks_t da_new; /* new count del alloc blocks used */
488 xfs_filblks_t da_old; /* old count del alloc blocks used */
489 int error; /* error return value */
490 xfs_ifork_t *ifp; /* inode fork ptr */
491 int logflags; /* returned value */
492 xfs_extnum_t nextents; /* number of extents in file now */
494 XFS_STATS_INC(xs_add_exlist);
495 cur = *curp;
496 ifp = XFS_IFORK_PTR(ip, whichfork);
497 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
498 ASSERT(idx <= nextents);
499 da_old = da_new = 0;
500 error = 0;
502 * This is the first extent added to a new/empty file.
503 * Special case this one, so other routines get to assume there are
504 * already extents in the list.
506 if (nextents == 0) {
507 xfs_iext_insert(ip, 0, 1, new,
508 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
510 ASSERT(cur == NULL);
511 ifp->if_lastex = 0;
512 if (!isnullstartblock(new->br_startblock)) {
513 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
514 logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
515 } else
516 logflags = 0;
519 * Any kind of new delayed allocation goes here.
521 else if (isnullstartblock(new->br_startblock)) {
522 if (cur)
523 ASSERT((cur->bc_private.b.flags &
524 XFS_BTCUR_BPRV_WASDEL) == 0);
525 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
526 &logflags, rsvd)))
527 goto done;
530 * Real allocation off the end of the file.
532 else if (idx == nextents) {
533 if (cur)
534 ASSERT((cur->bc_private.b.flags &
535 XFS_BTCUR_BPRV_WASDEL) == 0);
536 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
537 &logflags, whichfork)))
538 goto done;
539 } else {
540 xfs_bmbt_irec_t prev; /* old extent at offset idx */
543 * Get the record referred to by idx.
545 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
547 * If it's a real allocation record, and the new allocation ends
548 * after the start of the referred to record, then we're filling
549 * in a delayed or unwritten allocation with a real one, or
550 * converting real back to unwritten.
552 if (!isnullstartblock(new->br_startblock) &&
553 new->br_startoff + new->br_blockcount > prev.br_startoff) {
554 if (prev.br_state != XFS_EXT_UNWRITTEN &&
555 isnullstartblock(prev.br_startblock)) {
556 da_old = startblockval(prev.br_startblock);
557 if (cur)
558 ASSERT(cur->bc_private.b.flags &
559 XFS_BTCUR_BPRV_WASDEL);
560 if ((error = xfs_bmap_add_extent_delay_real(ip,
561 idx, &cur, new, &da_new, first, flist,
562 &logflags, rsvd)))
563 goto done;
564 } else if (new->br_state == XFS_EXT_NORM) {
565 ASSERT(new->br_state == XFS_EXT_NORM);
566 if ((error = xfs_bmap_add_extent_unwritten_real(
567 ip, idx, &cur, new, &logflags)))
568 goto done;
569 } else {
570 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
571 if ((error = xfs_bmap_add_extent_unwritten_real(
572 ip, idx, &cur, new, &logflags)))
573 goto done;
575 ASSERT(*curp == cur || *curp == NULL);
578 * Otherwise we're filling in a hole with an allocation.
580 else {
581 if (cur)
582 ASSERT((cur->bc_private.b.flags &
583 XFS_BTCUR_BPRV_WASDEL) == 0);
584 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
585 new, &logflags, whichfork)))
586 goto done;
590 ASSERT(*curp == cur || *curp == NULL);
592 * Convert to a btree if necessary.
594 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
595 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
596 int tmp_logflags; /* partial log flag return val */
598 ASSERT(cur == NULL);
599 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
600 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
601 logflags |= tmp_logflags;
602 if (error)
603 goto done;
606 * Adjust for changes in reserved delayed indirect blocks.
607 * Nothing to do for disk quotas here.
609 if (da_old || da_new) {
610 xfs_filblks_t nblks;
612 nblks = da_new;
613 if (cur)
614 nblks += cur->bc_private.b.allocated;
615 ASSERT(nblks <= da_old);
616 if (nblks < da_old)
617 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
618 (int64_t)(da_old - nblks), rsvd);
621 * Clear out the allocated field, done with it now in any case.
623 if (cur) {
624 cur->bc_private.b.allocated = 0;
625 *curp = cur;
627 done:
628 #ifdef DEBUG
629 if (!error)
630 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
631 #endif
632 *logflagsp = logflags;
633 return error;
637 * Called by xfs_bmap_add_extent to handle cases converting a delayed
638 * allocation to a real allocation.
640 STATIC int /* error */
641 xfs_bmap_add_extent_delay_real(
642 xfs_inode_t *ip, /* incore inode pointer */
643 xfs_extnum_t idx, /* extent number to update/insert */
644 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
645 xfs_bmbt_irec_t *new, /* new data to add to file extents */
646 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
647 xfs_fsblock_t *first, /* pointer to firstblock variable */
648 xfs_bmap_free_t *flist, /* list of extents to be freed */
649 int *logflagsp, /* inode logging flags */
650 int rsvd) /* OK to use reserved data block allocation */
652 xfs_btree_cur_t *cur; /* btree cursor */
653 int diff; /* temp value */
654 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
655 int error; /* error return value */
656 int i; /* temp state */
657 xfs_ifork_t *ifp; /* inode fork pointer */
658 xfs_fileoff_t new_endoff; /* end offset of new entry */
659 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
660 /* left is 0, right is 1, prev is 2 */
661 int rval=0; /* return value (logging flags) */
662 int state = 0;/* state bits, accessed thru macros */
663 xfs_filblks_t temp=0; /* value for dnew calculations */
664 xfs_filblks_t temp2=0;/* value for dnew calculations */
665 int tmp_rval; /* partial logging flags */
667 #define LEFT r[0]
668 #define RIGHT r[1]
669 #define PREV r[2]
672 * Set up a bunch of variables to make the tests simpler.
674 cur = *curp;
675 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
676 ep = xfs_iext_get_ext(ifp, idx);
677 xfs_bmbt_get_all(ep, &PREV);
678 new_endoff = new->br_startoff + new->br_blockcount;
679 ASSERT(PREV.br_startoff <= new->br_startoff);
680 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
683 * Set flags determining what part of the previous delayed allocation
684 * extent is being replaced by a real allocation.
686 if (PREV.br_startoff == new->br_startoff)
687 state |= BMAP_LEFT_FILLING;
688 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
689 state |= BMAP_RIGHT_FILLING;
692 * Check and set flags if this segment has a left neighbor.
693 * Don't set contiguous if the combined extent would be too large.
695 if (idx > 0) {
696 state |= BMAP_LEFT_VALID;
697 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
699 if (isnullstartblock(LEFT.br_startblock))
700 state |= BMAP_LEFT_DELAY;
703 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
704 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
705 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
706 LEFT.br_state == new->br_state &&
707 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
708 state |= BMAP_LEFT_CONTIG;
711 * Check and set flags if this segment has a right neighbor.
712 * Don't set contiguous if the combined extent would be too large.
713 * Also check for all-three-contiguous being too large.
715 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
716 state |= BMAP_RIGHT_VALID;
717 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
719 if (isnullstartblock(RIGHT.br_startblock))
720 state |= BMAP_RIGHT_DELAY;
723 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
724 new_endoff == RIGHT.br_startoff &&
725 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
726 new->br_state == RIGHT.br_state &&
727 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
728 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
729 BMAP_RIGHT_FILLING)) !=
730 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
731 BMAP_RIGHT_FILLING) ||
732 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
733 <= MAXEXTLEN))
734 state |= BMAP_RIGHT_CONTIG;
736 error = 0;
738 * Switch out based on the FILLING and CONTIG state bits.
740 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
741 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
742 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
743 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
745 * Filling in all of a previously delayed allocation extent.
746 * The left and right neighbors are both contiguous with new.
748 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
749 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
750 LEFT.br_blockcount + PREV.br_blockcount +
751 RIGHT.br_blockcount);
752 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
754 xfs_iext_remove(ip, idx, 2, state);
755 ip->i_df.if_lastex = idx - 1;
756 ip->i_d.di_nextents--;
757 if (cur == NULL)
758 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
759 else {
760 rval = XFS_ILOG_CORE;
761 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
762 RIGHT.br_startblock,
763 RIGHT.br_blockcount, &i)))
764 goto done;
765 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
766 if ((error = xfs_btree_delete(cur, &i)))
767 goto done;
768 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
769 if ((error = xfs_btree_decrement(cur, 0, &i)))
770 goto done;
771 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
772 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
773 LEFT.br_startblock,
774 LEFT.br_blockcount +
775 PREV.br_blockcount +
776 RIGHT.br_blockcount, LEFT.br_state)))
777 goto done;
779 *dnew = 0;
780 break;
782 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
784 * Filling in all of a previously delayed allocation extent.
785 * The left neighbor is contiguous, the right is not.
787 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
788 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
789 LEFT.br_blockcount + PREV.br_blockcount);
790 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
792 ip->i_df.if_lastex = idx - 1;
793 xfs_iext_remove(ip, idx, 1, state);
794 if (cur == NULL)
795 rval = XFS_ILOG_DEXT;
796 else {
797 rval = 0;
798 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
799 LEFT.br_startblock, LEFT.br_blockcount,
800 &i)))
801 goto done;
802 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
803 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
804 LEFT.br_startblock,
805 LEFT.br_blockcount +
806 PREV.br_blockcount, LEFT.br_state)))
807 goto done;
809 *dnew = 0;
810 break;
812 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
814 * Filling in all of a previously delayed allocation extent.
815 * The right neighbor is contiguous, the left is not.
817 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
818 xfs_bmbt_set_startblock(ep, new->br_startblock);
819 xfs_bmbt_set_blockcount(ep,
820 PREV.br_blockcount + RIGHT.br_blockcount);
821 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
823 ip->i_df.if_lastex = idx;
824 xfs_iext_remove(ip, idx + 1, 1, state);
825 if (cur == NULL)
826 rval = XFS_ILOG_DEXT;
827 else {
828 rval = 0;
829 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
830 RIGHT.br_startblock,
831 RIGHT.br_blockcount, &i)))
832 goto done;
833 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
834 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
835 new->br_startblock,
836 PREV.br_blockcount +
837 RIGHT.br_blockcount, PREV.br_state)))
838 goto done;
840 *dnew = 0;
841 break;
843 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
845 * Filling in all of a previously delayed allocation extent.
846 * Neither the left nor right neighbors are contiguous with
847 * the new one.
849 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
850 xfs_bmbt_set_startblock(ep, new->br_startblock);
851 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
853 ip->i_df.if_lastex = idx;
854 ip->i_d.di_nextents++;
855 if (cur == NULL)
856 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
857 else {
858 rval = XFS_ILOG_CORE;
859 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
860 new->br_startblock, new->br_blockcount,
861 &i)))
862 goto done;
863 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
864 cur->bc_rec.b.br_state = XFS_EXT_NORM;
865 if ((error = xfs_btree_insert(cur, &i)))
866 goto done;
867 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
869 *dnew = 0;
870 break;
872 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
874 * Filling in the first part of a previous delayed allocation.
875 * The left neighbor is contiguous.
877 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
878 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
879 LEFT.br_blockcount + new->br_blockcount);
880 xfs_bmbt_set_startoff(ep,
881 PREV.br_startoff + new->br_blockcount);
882 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
884 temp = PREV.br_blockcount - new->br_blockcount;
885 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
886 xfs_bmbt_set_blockcount(ep, temp);
887 ip->i_df.if_lastex = idx - 1;
888 if (cur == NULL)
889 rval = XFS_ILOG_DEXT;
890 else {
891 rval = 0;
892 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
893 LEFT.br_startblock, LEFT.br_blockcount,
894 &i)))
895 goto done;
896 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
897 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
898 LEFT.br_startblock,
899 LEFT.br_blockcount +
900 new->br_blockcount,
901 LEFT.br_state)))
902 goto done;
904 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
905 startblockval(PREV.br_startblock));
906 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
907 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
908 *dnew = temp;
909 break;
911 case BMAP_LEFT_FILLING:
913 * Filling in the first part of a previous delayed allocation.
914 * The left neighbor is not contiguous.
916 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
917 xfs_bmbt_set_startoff(ep, new_endoff);
918 temp = PREV.br_blockcount - new->br_blockcount;
919 xfs_bmbt_set_blockcount(ep, temp);
920 xfs_iext_insert(ip, idx, 1, new, state);
921 ip->i_df.if_lastex = idx;
922 ip->i_d.di_nextents++;
923 if (cur == NULL)
924 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
925 else {
926 rval = XFS_ILOG_CORE;
927 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
928 new->br_startblock, new->br_blockcount,
929 &i)))
930 goto done;
931 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
932 cur->bc_rec.b.br_state = XFS_EXT_NORM;
933 if ((error = xfs_btree_insert(cur, &i)))
934 goto done;
935 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
937 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
938 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
939 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
940 first, flist, &cur, 1, &tmp_rval,
941 XFS_DATA_FORK);
942 rval |= tmp_rval;
943 if (error)
944 goto done;
946 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
947 startblockval(PREV.br_startblock) -
948 (cur ? cur->bc_private.b.allocated : 0));
949 ep = xfs_iext_get_ext(ifp, idx + 1);
950 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
951 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
952 *dnew = temp;
953 break;
955 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
957 * Filling in the last part of a previous delayed allocation.
958 * The right neighbor is contiguous with the new allocation.
960 temp = PREV.br_blockcount - new->br_blockcount;
961 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
962 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
963 xfs_bmbt_set_blockcount(ep, temp);
964 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
965 new->br_startoff, new->br_startblock,
966 new->br_blockcount + RIGHT.br_blockcount,
967 RIGHT.br_state);
968 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
969 ip->i_df.if_lastex = idx + 1;
970 if (cur == NULL)
971 rval = XFS_ILOG_DEXT;
972 else {
973 rval = 0;
974 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
975 RIGHT.br_startblock,
976 RIGHT.br_blockcount, &i)))
977 goto done;
978 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
979 if ((error = xfs_bmbt_update(cur, new->br_startoff,
980 new->br_startblock,
981 new->br_blockcount +
982 RIGHT.br_blockcount,
983 RIGHT.br_state)))
984 goto done;
986 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
987 startblockval(PREV.br_startblock));
988 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
989 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
990 *dnew = temp;
991 break;
993 case BMAP_RIGHT_FILLING:
995 * Filling in the last part of a previous delayed allocation.
996 * The right neighbor is not contiguous.
998 temp = PREV.br_blockcount - new->br_blockcount;
999 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1000 xfs_bmbt_set_blockcount(ep, temp);
1001 xfs_iext_insert(ip, idx + 1, 1, new, state);
1002 ip->i_df.if_lastex = idx + 1;
1003 ip->i_d.di_nextents++;
1004 if (cur == NULL)
1005 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1006 else {
1007 rval = XFS_ILOG_CORE;
1008 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1009 new->br_startblock, new->br_blockcount,
1010 &i)))
1011 goto done;
1012 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1013 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1014 if ((error = xfs_btree_insert(cur, &i)))
1015 goto done;
1016 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1018 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1019 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1020 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1021 first, flist, &cur, 1, &tmp_rval,
1022 XFS_DATA_FORK);
1023 rval |= tmp_rval;
1024 if (error)
1025 goto done;
1027 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1028 startblockval(PREV.br_startblock) -
1029 (cur ? cur->bc_private.b.allocated : 0));
1030 ep = xfs_iext_get_ext(ifp, idx);
1031 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1032 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1033 *dnew = temp;
1034 break;
1036 case 0:
1038 * Filling in the middle part of a previous delayed allocation.
1039 * Contiguity is impossible here.
1040 * This case is avoided almost all the time.
1042 temp = new->br_startoff - PREV.br_startoff;
1043 trace_xfs_bmap_pre_update(ip, idx, 0, _THIS_IP_);
1044 xfs_bmbt_set_blockcount(ep, temp);
1045 r[0] = *new;
1046 r[1].br_state = PREV.br_state;
1047 r[1].br_startblock = 0;
1048 r[1].br_startoff = new_endoff;
1049 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1050 r[1].br_blockcount = temp2;
1051 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
1052 ip->i_df.if_lastex = idx + 1;
1053 ip->i_d.di_nextents++;
1054 if (cur == NULL)
1055 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1056 else {
1057 rval = XFS_ILOG_CORE;
1058 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1059 new->br_startblock, new->br_blockcount,
1060 &i)))
1061 goto done;
1062 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1063 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1064 if ((error = xfs_btree_insert(cur, &i)))
1065 goto done;
1066 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1068 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1069 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1070 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1071 first, flist, &cur, 1, &tmp_rval,
1072 XFS_DATA_FORK);
1073 rval |= tmp_rval;
1074 if (error)
1075 goto done;
1077 temp = xfs_bmap_worst_indlen(ip, temp);
1078 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1079 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
1080 (cur ? cur->bc_private.b.allocated : 0));
1081 if (diff > 0 &&
1082 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
1083 -((int64_t)diff), rsvd)) {
1085 * Ick gross gag me with a spoon.
1087 ASSERT(0); /* want to see if this ever happens! */
1088 while (diff > 0) {
1089 if (temp) {
1090 temp--;
1091 diff--;
1092 if (!diff ||
1093 !xfs_icsb_modify_counters(ip->i_mount,
1094 XFS_SBS_FDBLOCKS,
1095 -((int64_t)diff), rsvd))
1096 break;
1098 if (temp2) {
1099 temp2--;
1100 diff--;
1101 if (!diff ||
1102 !xfs_icsb_modify_counters(ip->i_mount,
1103 XFS_SBS_FDBLOCKS,
1104 -((int64_t)diff), rsvd))
1105 break;
1109 ep = xfs_iext_get_ext(ifp, idx);
1110 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1111 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1112 trace_xfs_bmap_pre_update(ip, idx + 2, state, _THIS_IP_);
1113 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1114 nullstartblock((int)temp2));
1115 trace_xfs_bmap_post_update(ip, idx + 2, state, _THIS_IP_);
1116 *dnew = temp + temp2;
1117 break;
1119 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1120 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1121 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1122 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1123 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1124 case BMAP_LEFT_CONTIG:
1125 case BMAP_RIGHT_CONTIG:
1127 * These cases are all impossible.
1129 ASSERT(0);
1131 *curp = cur;
1132 done:
1133 *logflagsp = rval;
1134 return error;
1135 #undef LEFT
1136 #undef RIGHT
1137 #undef PREV
1141 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1142 * allocation to a real allocation or vice versa.
1144 STATIC int /* error */
1145 xfs_bmap_add_extent_unwritten_real(
1146 xfs_inode_t *ip, /* incore inode pointer */
1147 xfs_extnum_t idx, /* extent number to update/insert */
1148 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
1149 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1150 int *logflagsp) /* inode logging flags */
1152 xfs_btree_cur_t *cur; /* btree cursor */
1153 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1154 int error; /* error return value */
1155 int i; /* temp state */
1156 xfs_ifork_t *ifp; /* inode fork pointer */
1157 xfs_fileoff_t new_endoff; /* end offset of new entry */
1158 xfs_exntst_t newext; /* new extent state */
1159 xfs_exntst_t oldext; /* old extent state */
1160 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1161 /* left is 0, right is 1, prev is 2 */
1162 int rval=0; /* return value (logging flags) */
1163 int state = 0;/* state bits, accessed thru macros */
1165 #define LEFT r[0]
1166 #define RIGHT r[1]
1167 #define PREV r[2]
1169 * Set up a bunch of variables to make the tests simpler.
1171 error = 0;
1172 cur = *curp;
1173 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1174 ep = xfs_iext_get_ext(ifp, idx);
1175 xfs_bmbt_get_all(ep, &PREV);
1176 newext = new->br_state;
1177 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1178 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1179 ASSERT(PREV.br_state == oldext);
1180 new_endoff = new->br_startoff + new->br_blockcount;
1181 ASSERT(PREV.br_startoff <= new->br_startoff);
1182 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1185 * Set flags determining what part of the previous oldext allocation
1186 * extent is being replaced by a newext allocation.
1188 if (PREV.br_startoff == new->br_startoff)
1189 state |= BMAP_LEFT_FILLING;
1190 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1191 state |= BMAP_RIGHT_FILLING;
1194 * Check and set flags if this segment has a left neighbor.
1195 * Don't set contiguous if the combined extent would be too large.
1197 if (idx > 0) {
1198 state |= BMAP_LEFT_VALID;
1199 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
1201 if (isnullstartblock(LEFT.br_startblock))
1202 state |= BMAP_LEFT_DELAY;
1205 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1206 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1207 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1208 LEFT.br_state == newext &&
1209 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1210 state |= BMAP_LEFT_CONTIG;
1213 * Check and set flags if this segment has a right neighbor.
1214 * Don't set contiguous if the combined extent would be too large.
1215 * Also check for all-three-contiguous being too large.
1217 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1218 state |= BMAP_RIGHT_VALID;
1219 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
1220 if (isnullstartblock(RIGHT.br_startblock))
1221 state |= BMAP_RIGHT_DELAY;
1224 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1225 new_endoff == RIGHT.br_startoff &&
1226 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1227 newext == RIGHT.br_state &&
1228 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1229 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1230 BMAP_RIGHT_FILLING)) !=
1231 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1232 BMAP_RIGHT_FILLING) ||
1233 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1234 <= MAXEXTLEN))
1235 state |= BMAP_RIGHT_CONTIG;
1238 * Switch out based on the FILLING and CONTIG state bits.
1240 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1241 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1242 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1243 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1245 * Setting all of a previous oldext extent to newext.
1246 * The left and right neighbors are both contiguous with new.
1248 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1249 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1250 LEFT.br_blockcount + PREV.br_blockcount +
1251 RIGHT.br_blockcount);
1252 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1254 xfs_iext_remove(ip, idx, 2, state);
1255 ip->i_df.if_lastex = idx - 1;
1256 ip->i_d.di_nextents -= 2;
1257 if (cur == NULL)
1258 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1259 else {
1260 rval = XFS_ILOG_CORE;
1261 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1262 RIGHT.br_startblock,
1263 RIGHT.br_blockcount, &i)))
1264 goto done;
1265 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1266 if ((error = xfs_btree_delete(cur, &i)))
1267 goto done;
1268 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1269 if ((error = xfs_btree_decrement(cur, 0, &i)))
1270 goto done;
1271 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1272 if ((error = xfs_btree_delete(cur, &i)))
1273 goto done;
1274 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1275 if ((error = xfs_btree_decrement(cur, 0, &i)))
1276 goto done;
1277 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1278 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1279 LEFT.br_startblock,
1280 LEFT.br_blockcount + PREV.br_blockcount +
1281 RIGHT.br_blockcount, LEFT.br_state)))
1282 goto done;
1284 break;
1286 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1288 * Setting all of a previous oldext extent to newext.
1289 * The left neighbor is contiguous, the right is not.
1291 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1292 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1293 LEFT.br_blockcount + PREV.br_blockcount);
1294 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1296 ip->i_df.if_lastex = idx - 1;
1297 xfs_iext_remove(ip, idx, 1, state);
1298 ip->i_d.di_nextents--;
1299 if (cur == NULL)
1300 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1301 else {
1302 rval = XFS_ILOG_CORE;
1303 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1304 PREV.br_startblock, PREV.br_blockcount,
1305 &i)))
1306 goto done;
1307 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1308 if ((error = xfs_btree_delete(cur, &i)))
1309 goto done;
1310 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1311 if ((error = xfs_btree_decrement(cur, 0, &i)))
1312 goto done;
1313 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1314 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1315 LEFT.br_startblock,
1316 LEFT.br_blockcount + PREV.br_blockcount,
1317 LEFT.br_state)))
1318 goto done;
1320 break;
1322 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1324 * Setting all of a previous oldext extent to newext.
1325 * The right neighbor is contiguous, the left is not.
1327 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1328 xfs_bmbt_set_blockcount(ep,
1329 PREV.br_blockcount + RIGHT.br_blockcount);
1330 xfs_bmbt_set_state(ep, newext);
1331 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1332 ip->i_df.if_lastex = idx;
1333 xfs_iext_remove(ip, idx + 1, 1, state);
1334 ip->i_d.di_nextents--;
1335 if (cur == NULL)
1336 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1337 else {
1338 rval = XFS_ILOG_CORE;
1339 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1340 RIGHT.br_startblock,
1341 RIGHT.br_blockcount, &i)))
1342 goto done;
1343 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1344 if ((error = xfs_btree_delete(cur, &i)))
1345 goto done;
1346 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1347 if ((error = xfs_btree_decrement(cur, 0, &i)))
1348 goto done;
1349 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1350 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1351 new->br_startblock,
1352 new->br_blockcount + RIGHT.br_blockcount,
1353 newext)))
1354 goto done;
1356 break;
1358 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1360 * Setting all of a previous oldext extent to newext.
1361 * Neither the left nor right neighbors are contiguous with
1362 * the new one.
1364 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1365 xfs_bmbt_set_state(ep, newext);
1366 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1368 ip->i_df.if_lastex = idx;
1369 if (cur == NULL)
1370 rval = XFS_ILOG_DEXT;
1371 else {
1372 rval = 0;
1373 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1374 new->br_startblock, new->br_blockcount,
1375 &i)))
1376 goto done;
1377 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1378 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1379 new->br_startblock, new->br_blockcount,
1380 newext)))
1381 goto done;
1383 break;
1385 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1387 * Setting the first part of a previous oldext extent to newext.
1388 * The left neighbor is contiguous.
1390 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1391 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1392 LEFT.br_blockcount + new->br_blockcount);
1393 xfs_bmbt_set_startoff(ep,
1394 PREV.br_startoff + new->br_blockcount);
1395 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1397 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1398 xfs_bmbt_set_startblock(ep,
1399 new->br_startblock + new->br_blockcount);
1400 xfs_bmbt_set_blockcount(ep,
1401 PREV.br_blockcount - new->br_blockcount);
1402 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1404 ip->i_df.if_lastex = idx - 1;
1405 if (cur == NULL)
1406 rval = XFS_ILOG_DEXT;
1407 else {
1408 rval = 0;
1409 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1410 PREV.br_startblock, PREV.br_blockcount,
1411 &i)))
1412 goto done;
1413 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1414 if ((error = xfs_bmbt_update(cur,
1415 PREV.br_startoff + new->br_blockcount,
1416 PREV.br_startblock + new->br_blockcount,
1417 PREV.br_blockcount - new->br_blockcount,
1418 oldext)))
1419 goto done;
1420 if ((error = xfs_btree_decrement(cur, 0, &i)))
1421 goto done;
1422 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1423 LEFT.br_startblock,
1424 LEFT.br_blockcount + new->br_blockcount,
1425 LEFT.br_state))
1426 goto done;
1428 break;
1430 case BMAP_LEFT_FILLING:
1432 * Setting the first part of a previous oldext extent to newext.
1433 * The left neighbor is not contiguous.
1435 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1436 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1437 xfs_bmbt_set_startoff(ep, new_endoff);
1438 xfs_bmbt_set_blockcount(ep,
1439 PREV.br_blockcount - new->br_blockcount);
1440 xfs_bmbt_set_startblock(ep,
1441 new->br_startblock + new->br_blockcount);
1442 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1444 xfs_iext_insert(ip, idx, 1, new, state);
1445 ip->i_df.if_lastex = idx;
1446 ip->i_d.di_nextents++;
1447 if (cur == NULL)
1448 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1449 else {
1450 rval = XFS_ILOG_CORE;
1451 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1452 PREV.br_startblock, PREV.br_blockcount,
1453 &i)))
1454 goto done;
1455 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1456 if ((error = xfs_bmbt_update(cur,
1457 PREV.br_startoff + new->br_blockcount,
1458 PREV.br_startblock + new->br_blockcount,
1459 PREV.br_blockcount - new->br_blockcount,
1460 oldext)))
1461 goto done;
1462 cur->bc_rec.b = *new;
1463 if ((error = xfs_btree_insert(cur, &i)))
1464 goto done;
1465 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1467 break;
1469 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1471 * Setting the last part of a previous oldext extent to newext.
1472 * The right neighbor is contiguous with the new allocation.
1474 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1475 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
1476 xfs_bmbt_set_blockcount(ep,
1477 PREV.br_blockcount - new->br_blockcount);
1478 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1479 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1480 new->br_startoff, new->br_startblock,
1481 new->br_blockcount + RIGHT.br_blockcount, newext);
1482 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
1484 ip->i_df.if_lastex = idx + 1;
1485 if (cur == NULL)
1486 rval = XFS_ILOG_DEXT;
1487 else {
1488 rval = 0;
1489 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1490 PREV.br_startblock,
1491 PREV.br_blockcount, &i)))
1492 goto done;
1493 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1494 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1495 PREV.br_startblock,
1496 PREV.br_blockcount - new->br_blockcount,
1497 oldext)))
1498 goto done;
1499 if ((error = xfs_btree_increment(cur, 0, &i)))
1500 goto done;
1501 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1502 new->br_startblock,
1503 new->br_blockcount + RIGHT.br_blockcount,
1504 newext)))
1505 goto done;
1507 break;
1509 case BMAP_RIGHT_FILLING:
1511 * Setting the last part of a previous oldext extent to newext.
1512 * The right neighbor is not contiguous.
1514 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1515 xfs_bmbt_set_blockcount(ep,
1516 PREV.br_blockcount - new->br_blockcount);
1517 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1519 xfs_iext_insert(ip, idx + 1, 1, new, state);
1520 ip->i_df.if_lastex = idx + 1;
1521 ip->i_d.di_nextents++;
1522 if (cur == NULL)
1523 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1524 else {
1525 rval = XFS_ILOG_CORE;
1526 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1527 PREV.br_startblock, PREV.br_blockcount,
1528 &i)))
1529 goto done;
1530 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1531 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1532 PREV.br_startblock,
1533 PREV.br_blockcount - new->br_blockcount,
1534 oldext)))
1535 goto done;
1536 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1537 new->br_startblock, new->br_blockcount,
1538 &i)))
1539 goto done;
1540 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1541 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1542 if ((error = xfs_btree_insert(cur, &i)))
1543 goto done;
1544 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1546 break;
1548 case 0:
1550 * Setting the middle part of a previous oldext extent to
1551 * newext. Contiguity is impossible here.
1552 * One extent becomes three extents.
1554 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1555 xfs_bmbt_set_blockcount(ep,
1556 new->br_startoff - PREV.br_startoff);
1557 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1559 r[0] = *new;
1560 r[1].br_startoff = new_endoff;
1561 r[1].br_blockcount =
1562 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1563 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1564 r[1].br_state = oldext;
1565 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
1566 ip->i_df.if_lastex = idx + 1;
1567 ip->i_d.di_nextents += 2;
1568 if (cur == NULL)
1569 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1570 else {
1571 rval = XFS_ILOG_CORE;
1572 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1573 PREV.br_startblock, PREV.br_blockcount,
1574 &i)))
1575 goto done;
1576 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1577 /* new right extent - oldext */
1578 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1579 r[1].br_startblock, r[1].br_blockcount,
1580 r[1].br_state)))
1581 goto done;
1582 /* new left extent - oldext */
1583 cur->bc_rec.b = PREV;
1584 cur->bc_rec.b.br_blockcount =
1585 new->br_startoff - PREV.br_startoff;
1586 if ((error = xfs_btree_insert(cur, &i)))
1587 goto done;
1588 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1590 * Reset the cursor to the position of the new extent
1591 * we are about to insert as we can't trust it after
1592 * the previous insert.
1594 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1595 new->br_startblock, new->br_blockcount,
1596 &i)))
1597 goto done;
1598 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1599 /* new middle extent - newext */
1600 cur->bc_rec.b.br_state = new->br_state;
1601 if ((error = xfs_btree_insert(cur, &i)))
1602 goto done;
1603 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1605 break;
1607 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1608 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1609 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1610 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1611 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1612 case BMAP_LEFT_CONTIG:
1613 case BMAP_RIGHT_CONTIG:
1615 * These cases are all impossible.
1617 ASSERT(0);
1619 *curp = cur;
1620 done:
1621 *logflagsp = rval;
1622 return error;
1623 #undef LEFT
1624 #undef RIGHT
1625 #undef PREV
1629 * Called by xfs_bmap_add_extent to handle cases converting a hole
1630 * to a delayed allocation.
1632 /*ARGSUSED*/
1633 STATIC int /* error */
1634 xfs_bmap_add_extent_hole_delay(
1635 xfs_inode_t *ip, /* incore inode pointer */
1636 xfs_extnum_t idx, /* extent number to update/insert */
1637 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1638 int *logflagsp, /* inode logging flags */
1639 int rsvd) /* OK to allocate reserved blocks */
1641 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
1642 xfs_ifork_t *ifp; /* inode fork pointer */
1643 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1644 xfs_filblks_t newlen=0; /* new indirect size */
1645 xfs_filblks_t oldlen=0; /* old indirect size */
1646 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1647 int state; /* state bits, accessed thru macros */
1648 xfs_filblks_t temp=0; /* temp for indirect calculations */
1650 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1651 ep = xfs_iext_get_ext(ifp, idx);
1652 state = 0;
1653 ASSERT(isnullstartblock(new->br_startblock));
1656 * Check and set flags if this segment has a left neighbor
1658 if (idx > 0) {
1659 state |= BMAP_LEFT_VALID;
1660 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1662 if (isnullstartblock(left.br_startblock))
1663 state |= BMAP_LEFT_DELAY;
1667 * Check and set flags if the current (right) segment exists.
1668 * If it doesn't exist, we're converting the hole at end-of-file.
1670 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1671 state |= BMAP_RIGHT_VALID;
1672 xfs_bmbt_get_all(ep, &right);
1674 if (isnullstartblock(right.br_startblock))
1675 state |= BMAP_RIGHT_DELAY;
1679 * Set contiguity flags on the left and right neighbors.
1680 * Don't let extents get too large, even if the pieces are contiguous.
1682 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1683 left.br_startoff + left.br_blockcount == new->br_startoff &&
1684 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1685 state |= BMAP_LEFT_CONTIG;
1687 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1688 new->br_startoff + new->br_blockcount == right.br_startoff &&
1689 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1690 (!(state & BMAP_LEFT_CONTIG) ||
1691 (left.br_blockcount + new->br_blockcount +
1692 right.br_blockcount <= MAXEXTLEN)))
1693 state |= BMAP_RIGHT_CONTIG;
1696 * Switch out based on the contiguity flags.
1698 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1699 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1701 * New allocation is contiguous with delayed allocations
1702 * on the left and on the right.
1703 * Merge all three into a single extent record.
1705 temp = left.br_blockcount + new->br_blockcount +
1706 right.br_blockcount;
1708 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1709 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1710 oldlen = startblockval(left.br_startblock) +
1711 startblockval(new->br_startblock) +
1712 startblockval(right.br_startblock);
1713 newlen = xfs_bmap_worst_indlen(ip, temp);
1714 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1715 nullstartblock((int)newlen));
1716 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1718 xfs_iext_remove(ip, idx, 1, state);
1719 ip->i_df.if_lastex = idx - 1;
1720 break;
1722 case BMAP_LEFT_CONTIG:
1724 * New allocation is contiguous with a delayed allocation
1725 * on the left.
1726 * Merge the new allocation with the left neighbor.
1728 temp = left.br_blockcount + new->br_blockcount;
1729 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1730 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
1731 oldlen = startblockval(left.br_startblock) +
1732 startblockval(new->br_startblock);
1733 newlen = xfs_bmap_worst_indlen(ip, temp);
1734 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1735 nullstartblock((int)newlen));
1736 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1738 ip->i_df.if_lastex = idx - 1;
1739 break;
1741 case BMAP_RIGHT_CONTIG:
1743 * New allocation is contiguous with a delayed allocation
1744 * on the right.
1745 * Merge the new allocation with the right neighbor.
1747 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1748 temp = new->br_blockcount + right.br_blockcount;
1749 oldlen = startblockval(new->br_startblock) +
1750 startblockval(right.br_startblock);
1751 newlen = xfs_bmap_worst_indlen(ip, temp);
1752 xfs_bmbt_set_allf(ep, new->br_startoff,
1753 nullstartblock((int)newlen), temp, right.br_state);
1754 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1756 ip->i_df.if_lastex = idx;
1757 break;
1759 case 0:
1761 * New allocation is not contiguous with another
1762 * delayed allocation.
1763 * Insert a new entry.
1765 oldlen = newlen = 0;
1766 xfs_iext_insert(ip, idx, 1, new, state);
1767 ip->i_df.if_lastex = idx;
1768 break;
1770 if (oldlen != newlen) {
1771 ASSERT(oldlen > newlen);
1772 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
1773 (int64_t)(oldlen - newlen), rsvd);
1775 * Nothing to do for disk quota accounting here.
1778 *logflagsp = 0;
1779 return 0;
1783 * Called by xfs_bmap_add_extent to handle cases converting a hole
1784 * to a real allocation.
1786 STATIC int /* error */
1787 xfs_bmap_add_extent_hole_real(
1788 xfs_inode_t *ip, /* incore inode pointer */
1789 xfs_extnum_t idx, /* extent number to update/insert */
1790 xfs_btree_cur_t *cur, /* if null, not a btree */
1791 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1792 int *logflagsp, /* inode logging flags */
1793 int whichfork) /* data or attr fork */
1795 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
1796 int error; /* error return value */
1797 int i; /* temp state */
1798 xfs_ifork_t *ifp; /* inode fork pointer */
1799 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1800 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1801 int rval=0; /* return value (logging flags) */
1802 int state; /* state bits, accessed thru macros */
1804 ifp = XFS_IFORK_PTR(ip, whichfork);
1805 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
1806 ep = xfs_iext_get_ext(ifp, idx);
1807 state = 0;
1809 if (whichfork == XFS_ATTR_FORK)
1810 state |= BMAP_ATTRFORK;
1813 * Check and set flags if this segment has a left neighbor.
1815 if (idx > 0) {
1816 state |= BMAP_LEFT_VALID;
1817 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
1818 if (isnullstartblock(left.br_startblock))
1819 state |= BMAP_LEFT_DELAY;
1823 * Check and set flags if this segment has a current value.
1824 * Not true if we're inserting into the "hole" at eof.
1826 if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1827 state |= BMAP_RIGHT_VALID;
1828 xfs_bmbt_get_all(ep, &right);
1829 if (isnullstartblock(right.br_startblock))
1830 state |= BMAP_RIGHT_DELAY;
1834 * We're inserting a real allocation between "left" and "right".
1835 * Set the contiguity flags. Don't let extents get too large.
1837 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1838 left.br_startoff + left.br_blockcount == new->br_startoff &&
1839 left.br_startblock + left.br_blockcount == new->br_startblock &&
1840 left.br_state == new->br_state &&
1841 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1842 state |= BMAP_LEFT_CONTIG;
1844 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1845 new->br_startoff + new->br_blockcount == right.br_startoff &&
1846 new->br_startblock + new->br_blockcount == right.br_startblock &&
1847 new->br_state == right.br_state &&
1848 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1849 (!(state & BMAP_LEFT_CONTIG) ||
1850 left.br_blockcount + new->br_blockcount +
1851 right.br_blockcount <= MAXEXTLEN))
1852 state |= BMAP_RIGHT_CONTIG;
1854 error = 0;
1856 * Select which case we're in here, and implement it.
1858 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1859 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1861 * New allocation is contiguous with real allocations on the
1862 * left and on the right.
1863 * Merge all three into a single extent record.
1865 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1866 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1867 left.br_blockcount + new->br_blockcount +
1868 right.br_blockcount);
1869 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1871 xfs_iext_remove(ip, idx, 1, state);
1872 ifp->if_lastex = idx - 1;
1873 XFS_IFORK_NEXT_SET(ip, whichfork,
1874 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1875 if (cur == NULL) {
1876 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1877 } else {
1878 rval = XFS_ILOG_CORE;
1879 if ((error = xfs_bmbt_lookup_eq(cur,
1880 right.br_startoff,
1881 right.br_startblock,
1882 right.br_blockcount, &i)))
1883 goto done;
1884 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1885 if ((error = xfs_btree_delete(cur, &i)))
1886 goto done;
1887 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1888 if ((error = xfs_btree_decrement(cur, 0, &i)))
1889 goto done;
1890 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1891 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1892 left.br_startblock,
1893 left.br_blockcount +
1894 new->br_blockcount +
1895 right.br_blockcount,
1896 left.br_state)))
1897 goto done;
1899 break;
1901 case BMAP_LEFT_CONTIG:
1903 * New allocation is contiguous with a real allocation
1904 * on the left.
1905 * Merge the new allocation with the left neighbor.
1907 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
1908 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1909 left.br_blockcount + new->br_blockcount);
1910 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1912 ifp->if_lastex = idx - 1;
1913 if (cur == NULL) {
1914 rval = xfs_ilog_fext(whichfork);
1915 } else {
1916 rval = 0;
1917 if ((error = xfs_bmbt_lookup_eq(cur,
1918 left.br_startoff,
1919 left.br_startblock,
1920 left.br_blockcount, &i)))
1921 goto done;
1922 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1923 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1924 left.br_startblock,
1925 left.br_blockcount +
1926 new->br_blockcount,
1927 left.br_state)))
1928 goto done;
1930 break;
1932 case BMAP_RIGHT_CONTIG:
1934 * New allocation is contiguous with a real allocation
1935 * on the right.
1936 * Merge the new allocation with the right neighbor.
1938 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1939 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
1940 new->br_blockcount + right.br_blockcount,
1941 right.br_state);
1942 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1944 ifp->if_lastex = idx;
1945 if (cur == NULL) {
1946 rval = xfs_ilog_fext(whichfork);
1947 } else {
1948 rval = 0;
1949 if ((error = xfs_bmbt_lookup_eq(cur,
1950 right.br_startoff,
1951 right.br_startblock,
1952 right.br_blockcount, &i)))
1953 goto done;
1954 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1955 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1956 new->br_startblock,
1957 new->br_blockcount +
1958 right.br_blockcount,
1959 right.br_state)))
1960 goto done;
1962 break;
1964 case 0:
1966 * New allocation is not contiguous with another
1967 * real allocation.
1968 * Insert a new entry.
1970 xfs_iext_insert(ip, idx, 1, new, state);
1971 ifp->if_lastex = idx;
1972 XFS_IFORK_NEXT_SET(ip, whichfork,
1973 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
1974 if (cur == NULL) {
1975 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1976 } else {
1977 rval = XFS_ILOG_CORE;
1978 if ((error = xfs_bmbt_lookup_eq(cur,
1979 new->br_startoff,
1980 new->br_startblock,
1981 new->br_blockcount, &i)))
1982 goto done;
1983 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1984 cur->bc_rec.b.br_state = new->br_state;
1985 if ((error = xfs_btree_insert(cur, &i)))
1986 goto done;
1987 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1989 break;
1991 done:
1992 *logflagsp = rval;
1993 return error;
1997 * Adjust the size of the new extent based on di_extsize and rt extsize.
1999 STATIC int
2000 xfs_bmap_extsize_align(
2001 xfs_mount_t *mp,
2002 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2003 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2004 xfs_extlen_t extsz, /* align to this extent size */
2005 int rt, /* is this a realtime inode? */
2006 int eof, /* is extent at end-of-file? */
2007 int delay, /* creating delalloc extent? */
2008 int convert, /* overwriting unwritten extent? */
2009 xfs_fileoff_t *offp, /* in/out: aligned offset */
2010 xfs_extlen_t *lenp) /* in/out: aligned length */
2012 xfs_fileoff_t orig_off; /* original offset */
2013 xfs_extlen_t orig_alen; /* original length */
2014 xfs_fileoff_t orig_end; /* original off+len */
2015 xfs_fileoff_t nexto; /* next file offset */
2016 xfs_fileoff_t prevo; /* previous file offset */
2017 xfs_fileoff_t align_off; /* temp for offset */
2018 xfs_extlen_t align_alen; /* temp for length */
2019 xfs_extlen_t temp; /* temp for calculations */
2021 if (convert)
2022 return 0;
2024 orig_off = align_off = *offp;
2025 orig_alen = align_alen = *lenp;
2026 orig_end = orig_off + orig_alen;
2029 * If this request overlaps an existing extent, then don't
2030 * attempt to perform any additional alignment.
2032 if (!delay && !eof &&
2033 (orig_off >= gotp->br_startoff) &&
2034 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2035 return 0;
2039 * If the file offset is unaligned vs. the extent size
2040 * we need to align it. This will be possible unless
2041 * the file was previously written with a kernel that didn't
2042 * perform this alignment, or if a truncate shot us in the
2043 * foot.
2045 temp = do_mod(orig_off, extsz);
2046 if (temp) {
2047 align_alen += temp;
2048 align_off -= temp;
2051 * Same adjustment for the end of the requested area.
2053 if ((temp = (align_alen % extsz))) {
2054 align_alen += extsz - temp;
2057 * If the previous block overlaps with this proposed allocation
2058 * then move the start forward without adjusting the length.
2060 if (prevp->br_startoff != NULLFILEOFF) {
2061 if (prevp->br_startblock == HOLESTARTBLOCK)
2062 prevo = prevp->br_startoff;
2063 else
2064 prevo = prevp->br_startoff + prevp->br_blockcount;
2065 } else
2066 prevo = 0;
2067 if (align_off != orig_off && align_off < prevo)
2068 align_off = prevo;
2070 * If the next block overlaps with this proposed allocation
2071 * then move the start back without adjusting the length,
2072 * but not before offset 0.
2073 * This may of course make the start overlap previous block,
2074 * and if we hit the offset 0 limit then the next block
2075 * can still overlap too.
2077 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2078 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2079 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2080 nexto = gotp->br_startoff + gotp->br_blockcount;
2081 else
2082 nexto = gotp->br_startoff;
2083 } else
2084 nexto = NULLFILEOFF;
2085 if (!eof &&
2086 align_off + align_alen != orig_end &&
2087 align_off + align_alen > nexto)
2088 align_off = nexto > align_alen ? nexto - align_alen : 0;
2090 * If we're now overlapping the next or previous extent that
2091 * means we can't fit an extsz piece in this hole. Just move
2092 * the start forward to the first valid spot and set
2093 * the length so we hit the end.
2095 if (align_off != orig_off && align_off < prevo)
2096 align_off = prevo;
2097 if (align_off + align_alen != orig_end &&
2098 align_off + align_alen > nexto &&
2099 nexto != NULLFILEOFF) {
2100 ASSERT(nexto > prevo);
2101 align_alen = nexto - align_off;
2105 * If realtime, and the result isn't a multiple of the realtime
2106 * extent size we need to remove blocks until it is.
2108 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2110 * We're not covering the original request, or
2111 * we won't be able to once we fix the length.
2113 if (orig_off < align_off ||
2114 orig_end > align_off + align_alen ||
2115 align_alen - temp < orig_alen)
2116 return XFS_ERROR(EINVAL);
2118 * Try to fix it by moving the start up.
2120 if (align_off + temp <= orig_off) {
2121 align_alen -= temp;
2122 align_off += temp;
2125 * Try to fix it by moving the end in.
2127 else if (align_off + align_alen - temp >= orig_end)
2128 align_alen -= temp;
2130 * Set the start to the minimum then trim the length.
2132 else {
2133 align_alen -= orig_off - align_off;
2134 align_off = orig_off;
2135 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2138 * Result doesn't cover the request, fail it.
2140 if (orig_off < align_off || orig_end > align_off + align_alen)
2141 return XFS_ERROR(EINVAL);
2142 } else {
2143 ASSERT(orig_off >= align_off);
2144 ASSERT(orig_end <= align_off + align_alen);
2147 #ifdef DEBUG
2148 if (!eof && gotp->br_startoff != NULLFILEOFF)
2149 ASSERT(align_off + align_alen <= gotp->br_startoff);
2150 if (prevp->br_startoff != NULLFILEOFF)
2151 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2152 #endif
2154 *lenp = align_alen;
2155 *offp = align_off;
2156 return 0;
2159 #define XFS_ALLOC_GAP_UNITS 4
2161 STATIC void
2162 xfs_bmap_adjacent(
2163 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2165 xfs_fsblock_t adjust; /* adjustment to block numbers */
2166 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2167 xfs_mount_t *mp; /* mount point structure */
2168 int nullfb; /* true if ap->firstblock isn't set */
2169 int rt; /* true if inode is realtime */
2171 #define ISVALID(x,y) \
2172 (rt ? \
2173 (x) < mp->m_sb.sb_rblocks : \
2174 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2175 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2176 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2178 mp = ap->ip->i_mount;
2179 nullfb = ap->firstblock == NULLFSBLOCK;
2180 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2181 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2183 * If allocating at eof, and there's a previous real block,
2184 * try to use its last block as our starting point.
2186 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2187 !isnullstartblock(ap->prevp->br_startblock) &&
2188 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2189 ap->prevp->br_startblock)) {
2190 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2192 * Adjust for the gap between prevp and us.
2194 adjust = ap->off -
2195 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2196 if (adjust &&
2197 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2198 ap->rval += adjust;
2201 * If not at eof, then compare the two neighbor blocks.
2202 * Figure out whether either one gives us a good starting point,
2203 * and pick the better one.
2205 else if (!ap->eof) {
2206 xfs_fsblock_t gotbno; /* right side block number */
2207 xfs_fsblock_t gotdiff=0; /* right side difference */
2208 xfs_fsblock_t prevbno; /* left side block number */
2209 xfs_fsblock_t prevdiff=0; /* left side difference */
2212 * If there's a previous (left) block, select a requested
2213 * start block based on it.
2215 if (ap->prevp->br_startoff != NULLFILEOFF &&
2216 !isnullstartblock(ap->prevp->br_startblock) &&
2217 (prevbno = ap->prevp->br_startblock +
2218 ap->prevp->br_blockcount) &&
2219 ISVALID(prevbno, ap->prevp->br_startblock)) {
2221 * Calculate gap to end of previous block.
2223 adjust = prevdiff = ap->off -
2224 (ap->prevp->br_startoff +
2225 ap->prevp->br_blockcount);
2227 * Figure the startblock based on the previous block's
2228 * end and the gap size.
2229 * Heuristic!
2230 * If the gap is large relative to the piece we're
2231 * allocating, or using it gives us an invalid block
2232 * number, then just use the end of the previous block.
2234 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2235 ISVALID(prevbno + prevdiff,
2236 ap->prevp->br_startblock))
2237 prevbno += adjust;
2238 else
2239 prevdiff += adjust;
2241 * If the firstblock forbids it, can't use it,
2242 * must use default.
2244 if (!rt && !nullfb &&
2245 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2246 prevbno = NULLFSBLOCK;
2249 * No previous block or can't follow it, just default.
2251 else
2252 prevbno = NULLFSBLOCK;
2254 * If there's a following (right) block, select a requested
2255 * start block based on it.
2257 if (!isnullstartblock(ap->gotp->br_startblock)) {
2259 * Calculate gap to start of next block.
2261 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2263 * Figure the startblock based on the next block's
2264 * start and the gap size.
2266 gotbno = ap->gotp->br_startblock;
2268 * Heuristic!
2269 * If the gap is large relative to the piece we're
2270 * allocating, or using it gives us an invalid block
2271 * number, then just use the start of the next block
2272 * offset by our length.
2274 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2275 ISVALID(gotbno - gotdiff, gotbno))
2276 gotbno -= adjust;
2277 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2278 gotbno -= ap->alen;
2279 gotdiff += adjust - ap->alen;
2280 } else
2281 gotdiff += adjust;
2283 * If the firstblock forbids it, can't use it,
2284 * must use default.
2286 if (!rt && !nullfb &&
2287 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2288 gotbno = NULLFSBLOCK;
2291 * No next block, just default.
2293 else
2294 gotbno = NULLFSBLOCK;
2296 * If both valid, pick the better one, else the only good
2297 * one, else ap->rval is already set (to 0 or the inode block).
2299 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2300 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2301 else if (prevbno != NULLFSBLOCK)
2302 ap->rval = prevbno;
2303 else if (gotbno != NULLFSBLOCK)
2304 ap->rval = gotbno;
2306 #undef ISVALID
2309 STATIC int
2310 xfs_bmap_rtalloc(
2311 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2313 xfs_alloctype_t atype = 0; /* type for allocation routines */
2314 int error; /* error return value */
2315 xfs_mount_t *mp; /* mount point structure */
2316 xfs_extlen_t prod = 0; /* product factor for allocators */
2317 xfs_extlen_t ralen = 0; /* realtime allocation length */
2318 xfs_extlen_t align; /* minimum allocation alignment */
2319 xfs_rtblock_t rtb;
2321 mp = ap->ip->i_mount;
2322 align = xfs_get_extsz_hint(ap->ip);
2323 prod = align / mp->m_sb.sb_rextsize;
2324 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2325 align, 1, ap->eof, 0,
2326 ap->conv, &ap->off, &ap->alen);
2327 if (error)
2328 return error;
2329 ASSERT(ap->alen);
2330 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2333 * If the offset & length are not perfectly aligned
2334 * then kill prod, it will just get us in trouble.
2336 if (do_mod(ap->off, align) || ap->alen % align)
2337 prod = 1;
2339 * Set ralen to be the actual requested length in rtextents.
2341 ralen = ap->alen / mp->m_sb.sb_rextsize;
2343 * If the old value was close enough to MAXEXTLEN that
2344 * we rounded up to it, cut it back so it's valid again.
2345 * Note that if it's a really large request (bigger than
2346 * MAXEXTLEN), we don't hear about that number, and can't
2347 * adjust the starting point to match it.
2349 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2350 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2352 * If it's an allocation to an empty file at offset 0,
2353 * pick an extent that will space things out in the rt area.
2355 if (ap->eof && ap->off == 0) {
2356 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2358 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2359 if (error)
2360 return error;
2361 ap->rval = rtx * mp->m_sb.sb_rextsize;
2362 } else {
2363 ap->rval = 0;
2366 xfs_bmap_adjacent(ap);
2369 * Realtime allocation, done through xfs_rtallocate_extent.
2371 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2372 do_div(ap->rval, mp->m_sb.sb_rextsize);
2373 rtb = ap->rval;
2374 ap->alen = ralen;
2375 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2376 &ralen, atype, ap->wasdel, prod, &rtb)))
2377 return error;
2378 if (rtb == NULLFSBLOCK && prod > 1 &&
2379 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2380 ap->alen, &ralen, atype,
2381 ap->wasdel, 1, &rtb)))
2382 return error;
2383 ap->rval = rtb;
2384 if (ap->rval != NULLFSBLOCK) {
2385 ap->rval *= mp->m_sb.sb_rextsize;
2386 ralen *= mp->m_sb.sb_rextsize;
2387 ap->alen = ralen;
2388 ap->ip->i_d.di_nblocks += ralen;
2389 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2390 if (ap->wasdel)
2391 ap->ip->i_delayed_blks -= ralen;
2393 * Adjust the disk quota also. This was reserved
2394 * earlier.
2396 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2397 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2398 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2399 } else {
2400 ap->alen = 0;
2402 return 0;
2405 STATIC int
2406 xfs_bmap_btalloc_nullfb(
2407 struct xfs_bmalloca *ap,
2408 struct xfs_alloc_arg *args,
2409 xfs_extlen_t *blen)
2411 struct xfs_mount *mp = ap->ip->i_mount;
2412 struct xfs_perag *pag;
2413 xfs_agnumber_t ag, startag;
2414 int notinit = 0;
2415 int error;
2417 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2418 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2419 else
2420 args->type = XFS_ALLOCTYPE_START_BNO;
2421 args->total = ap->total;
2424 * Search for an allocation group with a single extent large enough
2425 * for the request. If one isn't found, then adjust the minimum
2426 * allocation size to the largest space found.
2428 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2429 if (startag == NULLAGNUMBER)
2430 startag = ag = 0;
2432 pag = xfs_perag_get(mp, ag);
2433 while (*blen < ap->alen) {
2434 if (!pag->pagf_init) {
2435 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2436 XFS_ALLOC_FLAG_TRYLOCK);
2437 if (error) {
2438 xfs_perag_put(pag);
2439 return error;
2444 * See xfs_alloc_fix_freelist...
2446 if (pag->pagf_init) {
2447 xfs_extlen_t longest;
2448 longest = xfs_alloc_longest_free_extent(mp, pag);
2449 if (*blen < longest)
2450 *blen = longest;
2451 } else
2452 notinit = 1;
2454 if (xfs_inode_is_filestream(ap->ip)) {
2455 if (*blen >= ap->alen)
2456 break;
2458 if (ap->userdata) {
2460 * If startag is an invalid AG, we've
2461 * come here once before and
2462 * xfs_filestream_new_ag picked the
2463 * best currently available.
2465 * Don't continue looping, since we
2466 * could loop forever.
2468 if (startag == NULLAGNUMBER)
2469 break;
2471 error = xfs_filestream_new_ag(ap, &ag);
2472 xfs_perag_put(pag);
2473 if (error)
2474 return error;
2476 /* loop again to set 'blen'*/
2477 startag = NULLAGNUMBER;
2478 pag = xfs_perag_get(mp, ag);
2479 continue;
2482 if (++ag == mp->m_sb.sb_agcount)
2483 ag = 0;
2484 if (ag == startag)
2485 break;
2486 xfs_perag_put(pag);
2487 pag = xfs_perag_get(mp, ag);
2489 xfs_perag_put(pag);
2492 * Since the above loop did a BUF_TRYLOCK, it is
2493 * possible that there is space for this request.
2495 if (notinit || *blen < ap->minlen)
2496 args->minlen = ap->minlen;
2498 * If the best seen length is less than the request
2499 * length, use the best as the minimum.
2501 else if (*blen < ap->alen)
2502 args->minlen = *blen;
2504 * Otherwise we've seen an extent as big as alen,
2505 * use that as the minimum.
2507 else
2508 args->minlen = ap->alen;
2511 * set the failure fallback case to look in the selected
2512 * AG as the stream may have moved.
2514 if (xfs_inode_is_filestream(ap->ip))
2515 ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2517 return 0;
2520 STATIC int
2521 xfs_bmap_btalloc(
2522 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2524 xfs_mount_t *mp; /* mount point structure */
2525 xfs_alloctype_t atype = 0; /* type for allocation routines */
2526 xfs_extlen_t align; /* minimum allocation alignment */
2527 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2528 xfs_agnumber_t ag;
2529 xfs_alloc_arg_t args;
2530 xfs_extlen_t blen;
2531 xfs_extlen_t nextminlen = 0;
2532 int nullfb; /* true if ap->firstblock isn't set */
2533 int isaligned;
2534 int tryagain;
2535 int error;
2537 mp = ap->ip->i_mount;
2538 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2539 if (unlikely(align)) {
2540 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2541 align, 0, ap->eof, 0, ap->conv,
2542 &ap->off, &ap->alen);
2543 ASSERT(!error);
2544 ASSERT(ap->alen);
2546 nullfb = ap->firstblock == NULLFSBLOCK;
2547 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2548 if (nullfb) {
2549 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2550 ag = xfs_filestream_lookup_ag(ap->ip);
2551 ag = (ag != NULLAGNUMBER) ? ag : 0;
2552 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2553 } else {
2554 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2556 } else
2557 ap->rval = ap->firstblock;
2559 xfs_bmap_adjacent(ap);
2562 * If allowed, use ap->rval; otherwise must use firstblock since
2563 * it's in the right allocation group.
2565 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
2567 else
2568 ap->rval = ap->firstblock;
2570 * Normal allocation, done through xfs_alloc_vextent.
2572 tryagain = isaligned = 0;
2573 args.tp = ap->tp;
2574 args.mp = mp;
2575 args.fsbno = ap->rval;
2576 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2577 args.firstblock = ap->firstblock;
2578 blen = 0;
2579 if (nullfb) {
2580 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2581 if (error)
2582 return error;
2583 } else if (ap->low) {
2584 if (xfs_inode_is_filestream(ap->ip))
2585 args.type = XFS_ALLOCTYPE_FIRST_AG;
2586 else
2587 args.type = XFS_ALLOCTYPE_START_BNO;
2588 args.total = args.minlen = ap->minlen;
2589 } else {
2590 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2591 args.total = ap->total;
2592 args.minlen = ap->minlen;
2594 /* apply extent size hints if obtained earlier */
2595 if (unlikely(align)) {
2596 args.prod = align;
2597 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2598 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2599 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
2600 args.prod = 1;
2601 args.mod = 0;
2602 } else {
2603 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
2604 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2605 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2608 * If we are not low on available data blocks, and the
2609 * underlying logical volume manager is a stripe, and
2610 * the file offset is zero then try to allocate data
2611 * blocks on stripe unit boundary.
2612 * NOTE: ap->aeof is only set if the allocation length
2613 * is >= the stripe unit and the allocation offset is
2614 * at the end of file.
2616 if (!ap->low && ap->aeof) {
2617 if (!ap->off) {
2618 args.alignment = mp->m_dalign;
2619 atype = args.type;
2620 isaligned = 1;
2622 * Adjust for alignment
2624 if (blen > args.alignment && blen <= ap->alen)
2625 args.minlen = blen - args.alignment;
2626 args.minalignslop = 0;
2627 } else {
2629 * First try an exact bno allocation.
2630 * If it fails then do a near or start bno
2631 * allocation with alignment turned on.
2633 atype = args.type;
2634 tryagain = 1;
2635 args.type = XFS_ALLOCTYPE_THIS_BNO;
2636 args.alignment = 1;
2638 * Compute the minlen+alignment for the
2639 * next case. Set slop so that the value
2640 * of minlen+alignment+slop doesn't go up
2641 * between the calls.
2643 if (blen > mp->m_dalign && blen <= ap->alen)
2644 nextminlen = blen - mp->m_dalign;
2645 else
2646 nextminlen = args.minlen;
2647 if (nextminlen + mp->m_dalign > args.minlen + 1)
2648 args.minalignslop =
2649 nextminlen + mp->m_dalign -
2650 args.minlen - 1;
2651 else
2652 args.minalignslop = 0;
2654 } else {
2655 args.alignment = 1;
2656 args.minalignslop = 0;
2658 args.minleft = ap->minleft;
2659 args.wasdel = ap->wasdel;
2660 args.isfl = 0;
2661 args.userdata = ap->userdata;
2662 if ((error = xfs_alloc_vextent(&args)))
2663 return error;
2664 if (tryagain && args.fsbno == NULLFSBLOCK) {
2666 * Exact allocation failed. Now try with alignment
2667 * turned on.
2669 args.type = atype;
2670 args.fsbno = ap->rval;
2671 args.alignment = mp->m_dalign;
2672 args.minlen = nextminlen;
2673 args.minalignslop = 0;
2674 isaligned = 1;
2675 if ((error = xfs_alloc_vextent(&args)))
2676 return error;
2678 if (isaligned && args.fsbno == NULLFSBLOCK) {
2680 * allocation failed, so turn off alignment and
2681 * try again.
2683 args.type = atype;
2684 args.fsbno = ap->rval;
2685 args.alignment = 0;
2686 if ((error = xfs_alloc_vextent(&args)))
2687 return error;
2689 if (args.fsbno == NULLFSBLOCK && nullfb &&
2690 args.minlen > ap->minlen) {
2691 args.minlen = ap->minlen;
2692 args.type = XFS_ALLOCTYPE_START_BNO;
2693 args.fsbno = ap->rval;
2694 if ((error = xfs_alloc_vextent(&args)))
2695 return error;
2697 if (args.fsbno == NULLFSBLOCK && nullfb) {
2698 args.fsbno = 0;
2699 args.type = XFS_ALLOCTYPE_FIRST_AG;
2700 args.total = ap->minlen;
2701 args.minleft = 0;
2702 if ((error = xfs_alloc_vextent(&args)))
2703 return error;
2704 ap->low = 1;
2706 if (args.fsbno != NULLFSBLOCK) {
2707 ap->firstblock = ap->rval = args.fsbno;
2708 ASSERT(nullfb || fb_agno == args.agno ||
2709 (ap->low && fb_agno < args.agno));
2710 ap->alen = args.len;
2711 ap->ip->i_d.di_nblocks += args.len;
2712 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2713 if (ap->wasdel)
2714 ap->ip->i_delayed_blks -= args.len;
2716 * Adjust the disk quota also. This was reserved
2717 * earlier.
2719 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2720 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2721 XFS_TRANS_DQ_BCOUNT,
2722 (long) args.len);
2723 } else {
2724 ap->rval = NULLFSBLOCK;
2725 ap->alen = 0;
2727 return 0;
2731 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2732 * It figures out where to ask the underlying allocator to put the new extent.
2734 STATIC int
2735 xfs_bmap_alloc(
2736 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2738 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
2739 return xfs_bmap_rtalloc(ap);
2740 return xfs_bmap_btalloc(ap);
2744 * Transform a btree format file with only one leaf node, where the
2745 * extents list will fit in the inode, into an extents format file.
2746 * Since the file extents are already in-core, all we have to do is
2747 * give up the space for the btree root and pitch the leaf block.
2749 STATIC int /* error */
2750 xfs_bmap_btree_to_extents(
2751 xfs_trans_t *tp, /* transaction pointer */
2752 xfs_inode_t *ip, /* incore inode pointer */
2753 xfs_btree_cur_t *cur, /* btree cursor */
2754 int *logflagsp, /* inode logging flags */
2755 int whichfork) /* data or attr fork */
2757 /* REFERENCED */
2758 struct xfs_btree_block *cblock;/* child btree block */
2759 xfs_fsblock_t cbno; /* child block number */
2760 xfs_buf_t *cbp; /* child block's buffer */
2761 int error; /* error return value */
2762 xfs_ifork_t *ifp; /* inode fork data */
2763 xfs_mount_t *mp; /* mount point structure */
2764 __be64 *pp; /* ptr to block address */
2765 struct xfs_btree_block *rblock;/* root btree block */
2767 mp = ip->i_mount;
2768 ifp = XFS_IFORK_PTR(ip, whichfork);
2769 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2770 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2771 rblock = ifp->if_broot;
2772 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2773 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
2774 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2775 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
2776 cbno = be64_to_cpu(*pp);
2777 *logflagsp = 0;
2778 #ifdef DEBUG
2779 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
2780 return error;
2781 #endif
2782 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2783 XFS_BMAP_BTREE_REF)))
2784 return error;
2785 cblock = XFS_BUF_TO_BLOCK(cbp);
2786 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
2787 return error;
2788 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2789 ip->i_d.di_nblocks--;
2790 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
2791 xfs_trans_binval(tp, cbp);
2792 if (cur->bc_bufs[0] == cbp)
2793 cur->bc_bufs[0] = NULL;
2794 xfs_iroot_realloc(ip, -1, whichfork);
2795 ASSERT(ifp->if_broot == NULL);
2796 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2797 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2798 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2799 return 0;
2803 * Called by xfs_bmapi to update file extent records and the btree
2804 * after removing space (or undoing a delayed allocation).
2806 STATIC int /* error */
2807 xfs_bmap_del_extent(
2808 xfs_inode_t *ip, /* incore inode pointer */
2809 xfs_trans_t *tp, /* current transaction pointer */
2810 xfs_extnum_t idx, /* extent number to update/delete */
2811 xfs_bmap_free_t *flist, /* list of extents to be freed */
2812 xfs_btree_cur_t *cur, /* if null, not a btree */
2813 xfs_bmbt_irec_t *del, /* data to remove from extents */
2814 int *logflagsp, /* inode logging flags */
2815 int whichfork, /* data or attr fork */
2816 int rsvd) /* OK to allocate reserved blocks */
2818 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2819 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2820 xfs_fsblock_t del_endblock=0; /* first block past del */
2821 xfs_fileoff_t del_endoff; /* first offset past del */
2822 int delay; /* current block is delayed allocated */
2823 int do_fx; /* free extent at end of routine */
2824 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
2825 int error; /* error return value */
2826 int flags; /* inode logging flags */
2827 xfs_bmbt_irec_t got; /* current extent entry */
2828 xfs_fileoff_t got_endoff; /* first offset past got */
2829 int i; /* temp state */
2830 xfs_ifork_t *ifp; /* inode fork pointer */
2831 xfs_mount_t *mp; /* mount structure */
2832 xfs_filblks_t nblks; /* quota/sb block count */
2833 xfs_bmbt_irec_t new; /* new record to be inserted */
2834 /* REFERENCED */
2835 uint qfield; /* quota field to update */
2836 xfs_filblks_t temp; /* for indirect length calculations */
2837 xfs_filblks_t temp2; /* for indirect length calculations */
2838 int state = 0;
2840 XFS_STATS_INC(xs_del_exlist);
2842 if (whichfork == XFS_ATTR_FORK)
2843 state |= BMAP_ATTRFORK;
2845 mp = ip->i_mount;
2846 ifp = XFS_IFORK_PTR(ip, whichfork);
2847 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
2848 (uint)sizeof(xfs_bmbt_rec_t)));
2849 ASSERT(del->br_blockcount > 0);
2850 ep = xfs_iext_get_ext(ifp, idx);
2851 xfs_bmbt_get_all(ep, &got);
2852 ASSERT(got.br_startoff <= del->br_startoff);
2853 del_endoff = del->br_startoff + del->br_blockcount;
2854 got_endoff = got.br_startoff + got.br_blockcount;
2855 ASSERT(got_endoff >= del_endoff);
2856 delay = isnullstartblock(got.br_startblock);
2857 ASSERT(isnullstartblock(del->br_startblock) == delay);
2858 flags = 0;
2859 qfield = 0;
2860 error = 0;
2862 * If deleting a real allocation, must free up the disk space.
2864 if (!delay) {
2865 flags = XFS_ILOG_CORE;
2867 * Realtime allocation. Free it and record di_nblocks update.
2869 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
2870 xfs_fsblock_t bno;
2871 xfs_filblks_t len;
2873 ASSERT(do_mod(del->br_blockcount,
2874 mp->m_sb.sb_rextsize) == 0);
2875 ASSERT(do_mod(del->br_startblock,
2876 mp->m_sb.sb_rextsize) == 0);
2877 bno = del->br_startblock;
2878 len = del->br_blockcount;
2879 do_div(bno, mp->m_sb.sb_rextsize);
2880 do_div(len, mp->m_sb.sb_rextsize);
2881 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
2882 (xfs_extlen_t)len)))
2883 goto done;
2884 do_fx = 0;
2885 nblks = len * mp->m_sb.sb_rextsize;
2886 qfield = XFS_TRANS_DQ_RTBCOUNT;
2889 * Ordinary allocation.
2891 else {
2892 do_fx = 1;
2893 nblks = del->br_blockcount;
2894 qfield = XFS_TRANS_DQ_BCOUNT;
2897 * Set up del_endblock and cur for later.
2899 del_endblock = del->br_startblock + del->br_blockcount;
2900 if (cur) {
2901 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2902 got.br_startblock, got.br_blockcount,
2903 &i)))
2904 goto done;
2905 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2907 da_old = da_new = 0;
2908 } else {
2909 da_old = startblockval(got.br_startblock);
2910 da_new = 0;
2911 nblks = 0;
2912 do_fx = 0;
2915 * Set flag value to use in switch statement.
2916 * Left-contig is 2, right-contig is 1.
2918 switch (((got.br_startoff == del->br_startoff) << 1) |
2919 (got_endoff == del_endoff)) {
2920 case 3:
2922 * Matches the whole extent. Delete the entry.
2924 xfs_iext_remove(ip, idx, 1,
2925 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
2926 ifp->if_lastex = idx;
2927 if (delay)
2928 break;
2929 XFS_IFORK_NEXT_SET(ip, whichfork,
2930 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2931 flags |= XFS_ILOG_CORE;
2932 if (!cur) {
2933 flags |= xfs_ilog_fext(whichfork);
2934 break;
2936 if ((error = xfs_btree_delete(cur, &i)))
2937 goto done;
2938 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2939 break;
2941 case 2:
2943 * Deleting the first part of the extent.
2945 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
2946 xfs_bmbt_set_startoff(ep, del_endoff);
2947 temp = got.br_blockcount - del->br_blockcount;
2948 xfs_bmbt_set_blockcount(ep, temp);
2949 ifp->if_lastex = idx;
2950 if (delay) {
2951 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2952 da_old);
2953 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2954 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2955 da_new = temp;
2956 break;
2958 xfs_bmbt_set_startblock(ep, del_endblock);
2959 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2960 if (!cur) {
2961 flags |= xfs_ilog_fext(whichfork);
2962 break;
2964 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2965 got.br_blockcount - del->br_blockcount,
2966 got.br_state)))
2967 goto done;
2968 break;
2970 case 1:
2972 * Deleting the last part of the extent.
2974 temp = got.br_blockcount - del->br_blockcount;
2975 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
2976 xfs_bmbt_set_blockcount(ep, temp);
2977 ifp->if_lastex = idx;
2978 if (delay) {
2979 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2980 da_old);
2981 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2982 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2983 da_new = temp;
2984 break;
2986 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2987 if (!cur) {
2988 flags |= xfs_ilog_fext(whichfork);
2989 break;
2991 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2992 got.br_startblock,
2993 got.br_blockcount - del->br_blockcount,
2994 got.br_state)))
2995 goto done;
2996 break;
2998 case 0:
3000 * Deleting the middle of the extent.
3002 temp = del->br_startoff - got.br_startoff;
3003 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
3004 xfs_bmbt_set_blockcount(ep, temp);
3005 new.br_startoff = del_endoff;
3006 temp2 = got_endoff - del_endoff;
3007 new.br_blockcount = temp2;
3008 new.br_state = got.br_state;
3009 if (!delay) {
3010 new.br_startblock = del_endblock;
3011 flags |= XFS_ILOG_CORE;
3012 if (cur) {
3013 if ((error = xfs_bmbt_update(cur,
3014 got.br_startoff,
3015 got.br_startblock, temp,
3016 got.br_state)))
3017 goto done;
3018 if ((error = xfs_btree_increment(cur, 0, &i)))
3019 goto done;
3020 cur->bc_rec.b = new;
3021 error = xfs_btree_insert(cur, &i);
3022 if (error && error != ENOSPC)
3023 goto done;
3025 * If get no-space back from btree insert,
3026 * it tried a split, and we have a zero
3027 * block reservation.
3028 * Fix up our state and return the error.
3030 if (error == ENOSPC) {
3032 * Reset the cursor, don't trust
3033 * it after any insert operation.
3035 if ((error = xfs_bmbt_lookup_eq(cur,
3036 got.br_startoff,
3037 got.br_startblock,
3038 temp, &i)))
3039 goto done;
3040 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3042 * Update the btree record back
3043 * to the original value.
3045 if ((error = xfs_bmbt_update(cur,
3046 got.br_startoff,
3047 got.br_startblock,
3048 got.br_blockcount,
3049 got.br_state)))
3050 goto done;
3052 * Reset the extent record back
3053 * to the original value.
3055 xfs_bmbt_set_blockcount(ep,
3056 got.br_blockcount);
3057 flags = 0;
3058 error = XFS_ERROR(ENOSPC);
3059 goto done;
3061 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3062 } else
3063 flags |= xfs_ilog_fext(whichfork);
3064 XFS_IFORK_NEXT_SET(ip, whichfork,
3065 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3066 } else {
3067 ASSERT(whichfork == XFS_DATA_FORK);
3068 temp = xfs_bmap_worst_indlen(ip, temp);
3069 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
3070 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3071 new.br_startblock = nullstartblock((int)temp2);
3072 da_new = temp + temp2;
3073 while (da_new > da_old) {
3074 if (temp) {
3075 temp--;
3076 da_new--;
3077 xfs_bmbt_set_startblock(ep,
3078 nullstartblock((int)temp));
3080 if (da_new == da_old)
3081 break;
3082 if (temp2) {
3083 temp2--;
3084 da_new--;
3085 new.br_startblock =
3086 nullstartblock((int)temp2);
3090 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3091 xfs_iext_insert(ip, idx + 1, 1, &new, state);
3092 ifp->if_lastex = idx + 1;
3093 break;
3096 * If we need to, add to list of extents to delete.
3098 if (do_fx)
3099 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3100 mp);
3102 * Adjust inode # blocks in the file.
3104 if (nblks)
3105 ip->i_d.di_nblocks -= nblks;
3107 * Adjust quota data.
3109 if (qfield)
3110 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
3113 * Account for change in delayed indirect blocks.
3114 * Nothing to do for disk quota accounting here.
3116 ASSERT(da_old >= da_new);
3117 if (da_old > da_new) {
3118 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
3119 (int64_t)(da_old - da_new), rsvd);
3121 done:
3122 *logflagsp = flags;
3123 return error;
3127 * Remove the entry "free" from the free item list. Prev points to the
3128 * previous entry, unless "free" is the head of the list.
3130 STATIC void
3131 xfs_bmap_del_free(
3132 xfs_bmap_free_t *flist, /* free item list header */
3133 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3134 xfs_bmap_free_item_t *free) /* list item to be freed */
3136 if (prev)
3137 prev->xbfi_next = free->xbfi_next;
3138 else
3139 flist->xbf_first = free->xbfi_next;
3140 flist->xbf_count--;
3141 kmem_zone_free(xfs_bmap_free_item_zone, free);
3145 * Convert an extents-format file into a btree-format file.
3146 * The new file will have a root block (in the inode) and a single child block.
3148 STATIC int /* error */
3149 xfs_bmap_extents_to_btree(
3150 xfs_trans_t *tp, /* transaction pointer */
3151 xfs_inode_t *ip, /* incore inode pointer */
3152 xfs_fsblock_t *firstblock, /* first-block-allocated */
3153 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3154 xfs_btree_cur_t **curp, /* cursor returned to caller */
3155 int wasdel, /* converting a delayed alloc */
3156 int *logflagsp, /* inode logging flags */
3157 int whichfork) /* data or attr fork */
3159 struct xfs_btree_block *ablock; /* allocated (child) bt block */
3160 xfs_buf_t *abp; /* buffer for ablock */
3161 xfs_alloc_arg_t args; /* allocation arguments */
3162 xfs_bmbt_rec_t *arp; /* child record pointer */
3163 struct xfs_btree_block *block; /* btree root block */
3164 xfs_btree_cur_t *cur; /* bmap btree cursor */
3165 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3166 int error; /* error return value */
3167 xfs_extnum_t i, cnt; /* extent record index */
3168 xfs_ifork_t *ifp; /* inode fork pointer */
3169 xfs_bmbt_key_t *kp; /* root block key pointer */
3170 xfs_mount_t *mp; /* mount structure */
3171 xfs_extnum_t nextents; /* number of file extents */
3172 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3174 ifp = XFS_IFORK_PTR(ip, whichfork);
3175 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3176 ASSERT(ifp->if_ext_max ==
3177 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3179 * Make space in the inode incore.
3181 xfs_iroot_realloc(ip, 1, whichfork);
3182 ifp->if_flags |= XFS_IFBROOT;
3185 * Fill in the root.
3187 block = ifp->if_broot;
3188 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3189 block->bb_level = cpu_to_be16(1);
3190 block->bb_numrecs = cpu_to_be16(1);
3191 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3192 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3195 * Need a cursor. Can't allocate until bb_level is filled in.
3197 mp = ip->i_mount;
3198 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
3199 cur->bc_private.b.firstblock = *firstblock;
3200 cur->bc_private.b.flist = flist;
3201 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3203 * Convert to a btree with two levels, one record in root.
3205 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3206 args.tp = tp;
3207 args.mp = mp;
3208 args.firstblock = *firstblock;
3209 if (*firstblock == NULLFSBLOCK) {
3210 args.type = XFS_ALLOCTYPE_START_BNO;
3211 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3212 } else if (flist->xbf_low) {
3213 args.type = XFS_ALLOCTYPE_START_BNO;
3214 args.fsbno = *firstblock;
3215 } else {
3216 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3217 args.fsbno = *firstblock;
3219 args.minlen = args.maxlen = args.prod = 1;
3220 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3221 args.minalignslop = 0;
3222 args.wasdel = wasdel;
3223 *logflagsp = 0;
3224 if ((error = xfs_alloc_vextent(&args))) {
3225 xfs_iroot_realloc(ip, -1, whichfork);
3226 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3227 return error;
3230 * Allocation can't fail, the space was reserved.
3232 ASSERT(args.fsbno != NULLFSBLOCK);
3233 ASSERT(*firstblock == NULLFSBLOCK ||
3234 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3235 (flist->xbf_low &&
3236 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3237 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3238 cur->bc_private.b.allocated++;
3239 ip->i_d.di_nblocks++;
3240 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3241 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3243 * Fill in the child block.
3245 ablock = XFS_BUF_TO_BLOCK(abp);
3246 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3247 ablock->bb_level = 0;
3248 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3249 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3250 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3251 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3252 for (cnt = i = 0; i < nextents; i++) {
3253 ep = xfs_iext_get_ext(ifp, i);
3254 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
3255 arp->l0 = cpu_to_be64(ep->l0);
3256 arp->l1 = cpu_to_be64(ep->l1);
3257 arp++; cnt++;
3260 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3261 xfs_btree_set_numrecs(ablock, cnt);
3264 * Fill in the root key and pointer.
3266 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3267 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3268 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3269 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3270 be16_to_cpu(block->bb_level)));
3271 *pp = cpu_to_be64(args.fsbno);
3274 * Do all this logging at the end so that
3275 * the root is at the right level.
3277 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3278 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3279 ASSERT(*curp == NULL);
3280 *curp = cur;
3281 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
3282 return 0;
3286 * Calculate the default attribute fork offset for newly created inodes.
3288 uint
3289 xfs_default_attroffset(
3290 struct xfs_inode *ip)
3292 struct xfs_mount *mp = ip->i_mount;
3293 uint offset;
3295 if (mp->m_sb.sb_inodesize == 256) {
3296 offset = XFS_LITINO(mp) -
3297 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3298 } else {
3299 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3302 ASSERT(offset < XFS_LITINO(mp));
3303 return offset;
3307 * Helper routine to reset inode di_forkoff field when switching
3308 * attribute fork from local to extent format - we reset it where
3309 * possible to make space available for inline data fork extents.
3311 STATIC void
3312 xfs_bmap_forkoff_reset(
3313 xfs_mount_t *mp,
3314 xfs_inode_t *ip,
3315 int whichfork)
3317 if (whichfork == XFS_ATTR_FORK &&
3318 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3319 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3320 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3321 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3323 if (dfl_forkoff > ip->i_d.di_forkoff) {
3324 ip->i_d.di_forkoff = dfl_forkoff;
3325 ip->i_df.if_ext_max =
3326 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3327 ip->i_afp->if_ext_max =
3328 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3334 * Convert a local file to an extents file.
3335 * This code is out of bounds for data forks of regular files,
3336 * since the file data needs to get logged so things will stay consistent.
3337 * (The bmap-level manipulations are ok, though).
3339 STATIC int /* error */
3340 xfs_bmap_local_to_extents(
3341 xfs_trans_t *tp, /* transaction pointer */
3342 xfs_inode_t *ip, /* incore inode pointer */
3343 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3344 xfs_extlen_t total, /* total blocks needed by transaction */
3345 int *logflagsp, /* inode logging flags */
3346 int whichfork) /* data or attr fork */
3348 int error; /* error return value */
3349 int flags; /* logging flags returned */
3350 xfs_ifork_t *ifp; /* inode fork pointer */
3353 * We don't want to deal with the case of keeping inode data inline yet.
3354 * So sending the data fork of a regular inode is invalid.
3356 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3357 whichfork == XFS_DATA_FORK));
3358 ifp = XFS_IFORK_PTR(ip, whichfork);
3359 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3360 flags = 0;
3361 error = 0;
3362 if (ifp->if_bytes) {
3363 xfs_alloc_arg_t args; /* allocation arguments */
3364 xfs_buf_t *bp; /* buffer for extent block */
3365 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
3367 args.tp = tp;
3368 args.mp = ip->i_mount;
3369 args.firstblock = *firstblock;
3370 ASSERT((ifp->if_flags &
3371 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3373 * Allocate a block. We know we need only one, since the
3374 * file currently fits in an inode.
3376 if (*firstblock == NULLFSBLOCK) {
3377 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3378 args.type = XFS_ALLOCTYPE_START_BNO;
3379 } else {
3380 args.fsbno = *firstblock;
3381 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3383 args.total = total;
3384 args.mod = args.minleft = args.alignment = args.wasdel =
3385 args.isfl = args.minalignslop = 0;
3386 args.minlen = args.maxlen = args.prod = 1;
3387 if ((error = xfs_alloc_vextent(&args)))
3388 goto done;
3390 * Can't fail, the space was reserved.
3392 ASSERT(args.fsbno != NULLFSBLOCK);
3393 ASSERT(args.len == 1);
3394 *firstblock = args.fsbno;
3395 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3396 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3397 ifp->if_bytes);
3398 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3399 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3400 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3401 xfs_iext_add(ifp, 0, 1);
3402 ep = xfs_iext_get_ext(ifp, 0);
3403 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3404 trace_xfs_bmap_post_update(ip, 0,
3405 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3406 _THIS_IP_);
3407 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3408 ip->i_d.di_nblocks = 1;
3409 xfs_trans_mod_dquot_byino(tp, ip,
3410 XFS_TRANS_DQ_BCOUNT, 1L);
3411 flags |= xfs_ilog_fext(whichfork);
3412 } else {
3413 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3414 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3416 ifp->if_flags &= ~XFS_IFINLINE;
3417 ifp->if_flags |= XFS_IFEXTENTS;
3418 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3419 flags |= XFS_ILOG_CORE;
3420 done:
3421 *logflagsp = flags;
3422 return error;
3426 * Search the extent records for the entry containing block bno.
3427 * If bno lies in a hole, point to the next entry. If bno lies
3428 * past eof, *eofp will be set, and *prevp will contain the last
3429 * entry (null if none). Else, *lastxp will be set to the index
3430 * of the found entry; *gotp will contain the entry.
3432 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3433 xfs_bmap_search_multi_extents(
3434 xfs_ifork_t *ifp, /* inode fork pointer */
3435 xfs_fileoff_t bno, /* block number searched for */
3436 int *eofp, /* out: end of file found */
3437 xfs_extnum_t *lastxp, /* out: last extent index */
3438 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3439 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3441 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3442 xfs_extnum_t lastx; /* last extent index */
3445 * Initialize the extent entry structure to catch access to
3446 * uninitialized br_startblock field.
3448 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3449 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3450 gotp->br_state = XFS_EXT_INVALID;
3451 #if XFS_BIG_BLKNOS
3452 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3453 #else
3454 gotp->br_startblock = 0xffffa5a5;
3455 #endif
3456 prevp->br_startoff = NULLFILEOFF;
3458 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3459 if (lastx > 0) {
3460 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3462 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3463 xfs_bmbt_get_all(ep, gotp);
3464 *eofp = 0;
3465 } else {
3466 if (lastx > 0) {
3467 *gotp = *prevp;
3469 *eofp = 1;
3470 ep = NULL;
3472 *lastxp = lastx;
3473 return ep;
3477 * Search the extents list for the inode, for the extent containing bno.
3478 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3479 * *eofp will be set, and *prevp will contain the last entry (null if none).
3480 * Else, *lastxp will be set to the index of the found
3481 * entry; *gotp will contain the entry.
3483 STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
3484 xfs_bmap_search_extents(
3485 xfs_inode_t *ip, /* incore inode pointer */
3486 xfs_fileoff_t bno, /* block number searched for */
3487 int fork, /* data or attr fork */
3488 int *eofp, /* out: end of file found */
3489 xfs_extnum_t *lastxp, /* out: last extent index */
3490 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3491 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3493 xfs_ifork_t *ifp; /* inode fork pointer */
3494 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
3496 XFS_STATS_INC(xs_look_exlist);
3497 ifp = XFS_IFORK_PTR(ip, fork);
3499 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3501 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3502 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3503 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3504 "Access to block zero in inode %llu "
3505 "start_block: %llx start_off: %llx "
3506 "blkcnt: %llx extent-state: %x lastx: %x\n",
3507 (unsigned long long)ip->i_ino,
3508 (unsigned long long)gotp->br_startblock,
3509 (unsigned long long)gotp->br_startoff,
3510 (unsigned long long)gotp->br_blockcount,
3511 gotp->br_state, *lastxp);
3512 *lastxp = NULLEXTNUM;
3513 *eofp = 1;
3514 return NULL;
3516 return ep;
3520 * Compute the worst-case number of indirect blocks that will be used
3521 * for ip's delayed extent of length "len".
3523 STATIC xfs_filblks_t
3524 xfs_bmap_worst_indlen(
3525 xfs_inode_t *ip, /* incore inode pointer */
3526 xfs_filblks_t len) /* delayed extent length */
3528 int level; /* btree level number */
3529 int maxrecs; /* maximum record count at this level */
3530 xfs_mount_t *mp; /* mount structure */
3531 xfs_filblks_t rval; /* return value */
3533 mp = ip->i_mount;
3534 maxrecs = mp->m_bmap_dmxr[0];
3535 for (level = 0, rval = 0;
3536 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3537 level++) {
3538 len += maxrecs - 1;
3539 do_div(len, maxrecs);
3540 rval += len;
3541 if (len == 1)
3542 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3543 level - 1;
3544 if (level == 0)
3545 maxrecs = mp->m_bmap_dmxr[1];
3547 return rval;
3551 * Convert inode from non-attributed to attributed.
3552 * Must not be in a transaction, ip must not be locked.
3554 int /* error code */
3555 xfs_bmap_add_attrfork(
3556 xfs_inode_t *ip, /* incore inode pointer */
3557 int size, /* space new attribute needs */
3558 int rsvd) /* xact may use reserved blks */
3560 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
3561 xfs_bmap_free_t flist; /* freed extent records */
3562 xfs_mount_t *mp; /* mount structure */
3563 xfs_trans_t *tp; /* transaction pointer */
3564 int blks; /* space reservation */
3565 int version = 1; /* superblock attr version */
3566 int committed; /* xaction was committed */
3567 int logflags; /* logging flags */
3568 int error; /* error return value */
3570 ASSERT(XFS_IFORK_Q(ip) == 0);
3571 ASSERT(ip->i_df.if_ext_max ==
3572 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3574 mp = ip->i_mount;
3575 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3576 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3577 blks = XFS_ADDAFORK_SPACE_RES(mp);
3578 if (rsvd)
3579 tp->t_flags |= XFS_TRANS_RESERVE;
3580 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3581 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3582 goto error0;
3583 xfs_ilock(ip, XFS_ILOCK_EXCL);
3584 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
3585 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3586 XFS_QMOPT_RES_REGBLKS);
3587 if (error) {
3588 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3589 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3590 return error;
3592 if (XFS_IFORK_Q(ip))
3593 goto error1;
3594 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3596 * For inodes coming from pre-6.2 filesystems.
3598 ASSERT(ip->i_d.di_aformat == 0);
3599 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3601 ASSERT(ip->i_d.di_anextents == 0);
3603 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
3604 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3606 switch (ip->i_d.di_format) {
3607 case XFS_DINODE_FMT_DEV:
3608 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3609 break;
3610 case XFS_DINODE_FMT_UUID:
3611 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3612 break;
3613 case XFS_DINODE_FMT_LOCAL:
3614 case XFS_DINODE_FMT_EXTENTS:
3615 case XFS_DINODE_FMT_BTREE:
3616 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3617 if (!ip->i_d.di_forkoff)
3618 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
3619 else if (mp->m_flags & XFS_MOUNT_ATTR2)
3620 version = 2;
3621 break;
3622 default:
3623 ASSERT(0);
3624 error = XFS_ERROR(EINVAL);
3625 goto error1;
3627 ip->i_df.if_ext_max =
3628 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3629 ASSERT(ip->i_afp == NULL);
3630 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3631 ip->i_afp->if_ext_max =
3632 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3633 ip->i_afp->if_flags = XFS_IFEXTENTS;
3634 logflags = 0;
3635 xfs_bmap_init(&flist, &firstblock);
3636 switch (ip->i_d.di_format) {
3637 case XFS_DINODE_FMT_LOCAL:
3638 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3639 &logflags);
3640 break;
3641 case XFS_DINODE_FMT_EXTENTS:
3642 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3643 &flist, &logflags);
3644 break;
3645 case XFS_DINODE_FMT_BTREE:
3646 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3647 &logflags);
3648 break;
3649 default:
3650 error = 0;
3651 break;
3653 if (logflags)
3654 xfs_trans_log_inode(tp, ip, logflags);
3655 if (error)
3656 goto error2;
3657 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3658 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
3659 __int64_t sbfields = 0;
3661 spin_lock(&mp->m_sb_lock);
3662 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3663 xfs_sb_version_addattr(&mp->m_sb);
3664 sbfields |= XFS_SB_VERSIONNUM;
3666 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3667 xfs_sb_version_addattr2(&mp->m_sb);
3668 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
3670 if (sbfields) {
3671 spin_unlock(&mp->m_sb_lock);
3672 xfs_mod_sb(tp, sbfields);
3673 } else
3674 spin_unlock(&mp->m_sb_lock);
3676 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
3677 goto error2;
3678 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3679 ASSERT(ip->i_df.if_ext_max ==
3680 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3681 return error;
3682 error2:
3683 xfs_bmap_cancel(&flist);
3684 error1:
3685 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3686 error0:
3687 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3688 ASSERT(ip->i_df.if_ext_max ==
3689 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3690 return error;
3694 * Add the extent to the list of extents to be free at transaction end.
3695 * The list is maintained sorted (by block number).
3697 /* ARGSUSED */
3698 void
3699 xfs_bmap_add_free(
3700 xfs_fsblock_t bno, /* fs block number of extent */
3701 xfs_filblks_t len, /* length of extent */
3702 xfs_bmap_free_t *flist, /* list of extents */
3703 xfs_mount_t *mp) /* mount point structure */
3705 xfs_bmap_free_item_t *cur; /* current (next) element */
3706 xfs_bmap_free_item_t *new; /* new element */
3707 xfs_bmap_free_item_t *prev; /* previous element */
3708 #ifdef DEBUG
3709 xfs_agnumber_t agno;
3710 xfs_agblock_t agbno;
3712 ASSERT(bno != NULLFSBLOCK);
3713 ASSERT(len > 0);
3714 ASSERT(len <= MAXEXTLEN);
3715 ASSERT(!isnullstartblock(bno));
3716 agno = XFS_FSB_TO_AGNO(mp, bno);
3717 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3718 ASSERT(agno < mp->m_sb.sb_agcount);
3719 ASSERT(agbno < mp->m_sb.sb_agblocks);
3720 ASSERT(len < mp->m_sb.sb_agblocks);
3721 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3722 #endif
3723 ASSERT(xfs_bmap_free_item_zone != NULL);
3724 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3725 new->xbfi_startblock = bno;
3726 new->xbfi_blockcount = (xfs_extlen_t)len;
3727 for (prev = NULL, cur = flist->xbf_first;
3728 cur != NULL;
3729 prev = cur, cur = cur->xbfi_next) {
3730 if (cur->xbfi_startblock >= bno)
3731 break;
3733 if (prev)
3734 prev->xbfi_next = new;
3735 else
3736 flist->xbf_first = new;
3737 new->xbfi_next = cur;
3738 flist->xbf_count++;
3742 * Compute and fill in the value of the maximum depth of a bmap btree
3743 * in this filesystem. Done once, during mount.
3745 void
3746 xfs_bmap_compute_maxlevels(
3747 xfs_mount_t *mp, /* file system mount structure */
3748 int whichfork) /* data or attr fork */
3750 int level; /* btree level */
3751 uint maxblocks; /* max blocks at this level */
3752 uint maxleafents; /* max leaf entries possible */
3753 int maxrootrecs; /* max records in root block */
3754 int minleafrecs; /* min records in leaf block */
3755 int minnoderecs; /* min records in node block */
3756 int sz; /* root block size */
3759 * The maximum number of extents in a file, hence the maximum
3760 * number of leaf entries, is controlled by the type of di_nextents
3761 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3762 * (a signed 16-bit number, xfs_aextnum_t).
3764 * Note that we can no longer assume that if we are in ATTR1 that
3765 * the fork offset of all the inodes will be
3766 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3767 * with ATTR2 and then mounted back with ATTR1, keeping the
3768 * di_forkoff's fixed but probably at various positions. Therefore,
3769 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3770 * of a minimum size available.
3772 if (whichfork == XFS_DATA_FORK) {
3773 maxleafents = MAXEXTNUM;
3774 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
3775 } else {
3776 maxleafents = MAXAEXTNUM;
3777 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
3779 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
3780 minleafrecs = mp->m_bmap_dmnr[0];
3781 minnoderecs = mp->m_bmap_dmnr[1];
3782 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3783 for (level = 1; maxblocks > 1; level++) {
3784 if (maxblocks <= maxrootrecs)
3785 maxblocks = 1;
3786 else
3787 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3789 mp->m_bm_maxlevels[whichfork] = level;
3793 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3794 * caller. Frees all the extents that need freeing, which must be done
3795 * last due to locking considerations. We never free any extents in
3796 * the first transaction. This is to allow the caller to make the first
3797 * transaction a synchronous one so that the pointers to the data being
3798 * broken in this transaction will be permanent before the data is actually
3799 * freed. This is necessary to prevent blocks from being reallocated
3800 * and written to before the free and reallocation are actually permanent.
3801 * We do not just make the first transaction synchronous here, because
3802 * there are more efficient ways to gain the same protection in some cases
3803 * (see the file truncation code).
3805 * Return 1 if the given transaction was committed and a new one
3806 * started, and 0 otherwise in the committed parameter.
3808 /*ARGSUSED*/
3809 int /* error */
3810 xfs_bmap_finish(
3811 xfs_trans_t **tp, /* transaction pointer addr */
3812 xfs_bmap_free_t *flist, /* i/o: list extents to free */
3813 int *committed) /* xact committed or not */
3815 xfs_efd_log_item_t *efd; /* extent free data */
3816 xfs_efi_log_item_t *efi; /* extent free intention */
3817 int error; /* error return value */
3818 xfs_bmap_free_item_t *free; /* free extent item */
3819 unsigned int logres; /* new log reservation */
3820 unsigned int logcount; /* new log count */
3821 xfs_mount_t *mp; /* filesystem mount structure */
3822 xfs_bmap_free_item_t *next; /* next item on free list */
3823 xfs_trans_t *ntp; /* new transaction pointer */
3825 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3826 if (flist->xbf_count == 0) {
3827 *committed = 0;
3828 return 0;
3830 ntp = *tp;
3831 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3832 for (free = flist->xbf_first; free; free = free->xbfi_next)
3833 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3834 free->xbfi_blockcount);
3835 logres = ntp->t_log_res;
3836 logcount = ntp->t_log_count;
3837 ntp = xfs_trans_dup(*tp);
3838 error = xfs_trans_commit(*tp, 0);
3839 *tp = ntp;
3840 *committed = 1;
3842 * We have a new transaction, so we should return committed=1,
3843 * even though we're returning an error.
3845 if (error)
3846 return error;
3849 * transaction commit worked ok so we can drop the extra ticket
3850 * reference that we gained in xfs_trans_dup()
3852 xfs_log_ticket_put(ntp->t_ticket);
3854 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3855 logcount)))
3856 return error;
3857 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3858 for (free = flist->xbf_first; free != NULL; free = next) {
3859 next = free->xbfi_next;
3860 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3861 free->xbfi_blockcount))) {
3863 * The bmap free list will be cleaned up at a
3864 * higher level. The EFI will be canceled when
3865 * this transaction is aborted.
3866 * Need to force shutdown here to make sure it
3867 * happens, since this transaction may not be
3868 * dirty yet.
3870 mp = ntp->t_mountp;
3871 if (!XFS_FORCED_SHUTDOWN(mp))
3872 xfs_force_shutdown(mp,
3873 (error == EFSCORRUPTED) ?
3874 SHUTDOWN_CORRUPT_INCORE :
3875 SHUTDOWN_META_IO_ERROR);
3876 return error;
3878 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3879 free->xbfi_blockcount);
3880 xfs_bmap_del_free(flist, NULL, free);
3882 return 0;
3886 * Free up any items left in the list.
3888 void
3889 xfs_bmap_cancel(
3890 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3892 xfs_bmap_free_item_t *free; /* free list item */
3893 xfs_bmap_free_item_t *next;
3895 if (flist->xbf_count == 0)
3896 return;
3897 ASSERT(flist->xbf_first != NULL);
3898 for (free = flist->xbf_first; free; free = next) {
3899 next = free->xbfi_next;
3900 xfs_bmap_del_free(flist, NULL, free);
3902 ASSERT(flist->xbf_count == 0);
3906 * Returns the file-relative block number of the first unused block(s)
3907 * in the file with at least "len" logically contiguous blocks free.
3908 * This is the lowest-address hole if the file has holes, else the first block
3909 * past the end of file.
3910 * Return 0 if the file is currently local (in-inode).
3912 int /* error */
3913 xfs_bmap_first_unused(
3914 xfs_trans_t *tp, /* transaction pointer */
3915 xfs_inode_t *ip, /* incore inode */
3916 xfs_extlen_t len, /* size of hole to find */
3917 xfs_fileoff_t *first_unused, /* unused block */
3918 int whichfork) /* data or attr fork */
3920 int error; /* error return value */
3921 int idx; /* extent record index */
3922 xfs_ifork_t *ifp; /* inode fork pointer */
3923 xfs_fileoff_t lastaddr; /* last block number seen */
3924 xfs_fileoff_t lowest; /* lowest useful block */
3925 xfs_fileoff_t max; /* starting useful block */
3926 xfs_fileoff_t off; /* offset for this block */
3927 xfs_extnum_t nextents; /* number of extent entries */
3929 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3930 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3931 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3932 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3933 *first_unused = 0;
3934 return 0;
3936 ifp = XFS_IFORK_PTR(ip, whichfork);
3937 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3938 (error = xfs_iread_extents(tp, ip, whichfork)))
3939 return error;
3940 lowest = *first_unused;
3941 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3942 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
3943 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
3944 off = xfs_bmbt_get_startoff(ep);
3946 * See if the hole before this extent will work.
3948 if (off >= lowest + len && off - max >= len) {
3949 *first_unused = max;
3950 return 0;
3952 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3953 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3955 *first_unused = max;
3956 return 0;
3960 * Returns the file-relative block number of the last block + 1 before
3961 * last_block (input value) in the file.
3962 * This is not based on i_size, it is based on the extent records.
3963 * Returns 0 for local files, as they do not have extent records.
3965 int /* error */
3966 xfs_bmap_last_before(
3967 xfs_trans_t *tp, /* transaction pointer */
3968 xfs_inode_t *ip, /* incore inode */
3969 xfs_fileoff_t *last_block, /* last block */
3970 int whichfork) /* data or attr fork */
3972 xfs_fileoff_t bno; /* input file offset */
3973 int eof; /* hit end of file */
3974 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
3975 int error; /* error return value */
3976 xfs_bmbt_irec_t got; /* current extent value */
3977 xfs_ifork_t *ifp; /* inode fork pointer */
3978 xfs_extnum_t lastx; /* last extent used */
3979 xfs_bmbt_irec_t prev; /* previous extent value */
3981 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3982 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3983 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3984 return XFS_ERROR(EIO);
3985 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3986 *last_block = 0;
3987 return 0;
3989 ifp = XFS_IFORK_PTR(ip, whichfork);
3990 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3991 (error = xfs_iread_extents(tp, ip, whichfork)))
3992 return error;
3993 bno = *last_block - 1;
3994 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3995 &prev);
3996 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3997 if (prev.br_startoff == NULLFILEOFF)
3998 *last_block = 0;
3999 else
4000 *last_block = prev.br_startoff + prev.br_blockcount;
4003 * Otherwise *last_block is already the right answer.
4005 return 0;
4009 * Returns the file-relative block number of the first block past eof in
4010 * the file. This is not based on i_size, it is based on the extent records.
4011 * Returns 0 for local files, as they do not have extent records.
4013 int /* error */
4014 xfs_bmap_last_offset(
4015 xfs_trans_t *tp, /* transaction pointer */
4016 xfs_inode_t *ip, /* incore inode */
4017 xfs_fileoff_t *last_block, /* last block */
4018 int whichfork) /* data or attr fork */
4020 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
4021 int error; /* error return value */
4022 xfs_ifork_t *ifp; /* inode fork pointer */
4023 xfs_extnum_t nextents; /* number of extent entries */
4025 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4026 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4027 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4028 return XFS_ERROR(EIO);
4029 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4030 *last_block = 0;
4031 return 0;
4033 ifp = XFS_IFORK_PTR(ip, whichfork);
4034 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4035 (error = xfs_iread_extents(tp, ip, whichfork)))
4036 return error;
4037 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4038 if (!nextents) {
4039 *last_block = 0;
4040 return 0;
4042 ep = xfs_iext_get_ext(ifp, nextents - 1);
4043 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4044 return 0;
4048 * Returns whether the selected fork of the inode has exactly one
4049 * block or not. For the data fork we check this matches di_size,
4050 * implying the file's range is 0..bsize-1.
4052 int /* 1=>1 block, 0=>otherwise */
4053 xfs_bmap_one_block(
4054 xfs_inode_t *ip, /* incore inode */
4055 int whichfork) /* data or attr fork */
4057 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
4058 xfs_ifork_t *ifp; /* inode fork pointer */
4059 int rval; /* return value */
4060 xfs_bmbt_irec_t s; /* internal version of extent */
4062 #ifndef DEBUG
4063 if (whichfork == XFS_DATA_FORK) {
4064 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4065 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4066 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4068 #endif /* !DEBUG */
4069 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4070 return 0;
4071 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4072 return 0;
4073 ifp = XFS_IFORK_PTR(ip, whichfork);
4074 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4075 ep = xfs_iext_get_ext(ifp, 0);
4076 xfs_bmbt_get_all(ep, &s);
4077 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4078 if (rval && whichfork == XFS_DATA_FORK)
4079 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
4080 return rval;
4083 STATIC int
4084 xfs_bmap_sanity_check(
4085 struct xfs_mount *mp,
4086 struct xfs_buf *bp,
4087 int level)
4089 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4091 if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
4092 be16_to_cpu(block->bb_level) != level ||
4093 be16_to_cpu(block->bb_numrecs) == 0 ||
4094 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4095 return 0;
4096 return 1;
4100 * Read in the extents to if_extents.
4101 * All inode fields are set up by caller, we just traverse the btree
4102 * and copy the records in. If the file system cannot contain unwritten
4103 * extents, the records are checked for no "state" flags.
4105 int /* error */
4106 xfs_bmap_read_extents(
4107 xfs_trans_t *tp, /* transaction pointer */
4108 xfs_inode_t *ip, /* incore inode */
4109 int whichfork) /* data or attr fork */
4111 struct xfs_btree_block *block; /* current btree block */
4112 xfs_fsblock_t bno; /* block # of "block" */
4113 xfs_buf_t *bp; /* buffer for "block" */
4114 int error; /* error return value */
4115 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4116 xfs_extnum_t i, j; /* index into the extents list */
4117 xfs_ifork_t *ifp; /* fork structure */
4118 int level; /* btree level, for checking */
4119 xfs_mount_t *mp; /* file system mount structure */
4120 __be64 *pp; /* pointer to block address */
4121 /* REFERENCED */
4122 xfs_extnum_t room; /* number of entries there's room for */
4124 bno = NULLFSBLOCK;
4125 mp = ip->i_mount;
4126 ifp = XFS_IFORK_PTR(ip, whichfork);
4127 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4128 XFS_EXTFMT_INODE(ip);
4129 block = ifp->if_broot;
4131 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4133 level = be16_to_cpu(block->bb_level);
4134 ASSERT(level > 0);
4135 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
4136 bno = be64_to_cpu(*pp);
4137 ASSERT(bno != NULLDFSBNO);
4138 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4139 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4141 * Go down the tree until leaf level is reached, following the first
4142 * pointer (leftmost) at each level.
4144 while (level-- > 0) {
4145 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4146 XFS_BMAP_BTREE_REF)))
4147 return error;
4148 block = XFS_BUF_TO_BLOCK(bp);
4149 XFS_WANT_CORRUPTED_GOTO(
4150 xfs_bmap_sanity_check(mp, bp, level),
4151 error0);
4152 if (level == 0)
4153 break;
4154 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
4155 bno = be64_to_cpu(*pp);
4156 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4157 xfs_trans_brelse(tp, bp);
4160 * Here with bp and block set to the leftmost leaf node in the tree.
4162 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4163 i = 0;
4165 * Loop over all leaf nodes. Copy information to the extent records.
4167 for (;;) {
4168 xfs_bmbt_rec_t *frp;
4169 xfs_fsblock_t nextbno;
4170 xfs_extnum_t num_recs;
4171 xfs_extnum_t start;
4174 num_recs = xfs_btree_get_numrecs(block);
4175 if (unlikely(i + num_recs > room)) {
4176 ASSERT(i + num_recs <= room);
4177 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4178 "corrupt dinode %Lu, (btree extents).",
4179 (unsigned long long) ip->i_ino);
4180 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4181 XFS_ERRLEVEL_LOW,
4182 ip->i_mount);
4183 goto error0;
4185 XFS_WANT_CORRUPTED_GOTO(
4186 xfs_bmap_sanity_check(mp, bp, 0),
4187 error0);
4189 * Read-ahead the next leaf block, if any.
4191 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
4192 if (nextbno != NULLFSBLOCK)
4193 xfs_btree_reada_bufl(mp, nextbno, 1);
4195 * Copy records into the extent records.
4197 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
4198 start = i;
4199 for (j = 0; j < num_recs; j++, i++, frp++) {
4200 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
4201 trp->l0 = be64_to_cpu(frp->l0);
4202 trp->l1 = be64_to_cpu(frp->l1);
4204 if (exntf == XFS_EXTFMT_NOSTATE) {
4206 * Check all attribute bmap btree records and
4207 * any "older" data bmap btree records for a
4208 * set bit in the "extent flag" position.
4210 if (unlikely(xfs_check_nostate_extents(ifp,
4211 start, num_recs))) {
4212 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4213 XFS_ERRLEVEL_LOW,
4214 ip->i_mount);
4215 goto error0;
4218 xfs_trans_brelse(tp, bp);
4219 bno = nextbno;
4221 * If we've reached the end, stop.
4223 if (bno == NULLFSBLOCK)
4224 break;
4225 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4226 XFS_BMAP_BTREE_REF)))
4227 return error;
4228 block = XFS_BUF_TO_BLOCK(bp);
4230 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4231 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4232 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
4233 return 0;
4234 error0:
4235 xfs_trans_brelse(tp, bp);
4236 return XFS_ERROR(EFSCORRUPTED);
4239 #ifdef DEBUG
4241 * Add bmap trace insert entries for all the contents of the extent records.
4243 void
4244 xfs_bmap_trace_exlist(
4245 xfs_inode_t *ip, /* incore inode pointer */
4246 xfs_extnum_t cnt, /* count of entries in the list */
4247 int whichfork, /* data or attr fork */
4248 unsigned long caller_ip)
4250 xfs_extnum_t idx; /* extent record index */
4251 xfs_ifork_t *ifp; /* inode fork pointer */
4252 int state = 0;
4254 if (whichfork == XFS_ATTR_FORK)
4255 state |= BMAP_ATTRFORK;
4257 ifp = XFS_IFORK_PTR(ip, whichfork);
4258 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4259 for (idx = 0; idx < cnt; idx++)
4260 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
4264 * Validate that the bmbt_irecs being returned from bmapi are valid
4265 * given the callers original parameters. Specifically check the
4266 * ranges of the returned irecs to ensure that they only extent beyond
4267 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4269 STATIC void
4270 xfs_bmap_validate_ret(
4271 xfs_fileoff_t bno,
4272 xfs_filblks_t len,
4273 int flags,
4274 xfs_bmbt_irec_t *mval,
4275 int nmap,
4276 int ret_nmap)
4278 int i; /* index to map values */
4280 ASSERT(ret_nmap <= nmap);
4282 for (i = 0; i < ret_nmap; i++) {
4283 ASSERT(mval[i].br_blockcount > 0);
4284 if (!(flags & XFS_BMAPI_ENTIRE)) {
4285 ASSERT(mval[i].br_startoff >= bno);
4286 ASSERT(mval[i].br_blockcount <= len);
4287 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4288 bno + len);
4289 } else {
4290 ASSERT(mval[i].br_startoff < bno + len);
4291 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4292 bno);
4294 ASSERT(i == 0 ||
4295 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4296 mval[i].br_startoff);
4297 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4298 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4299 mval[i].br_startblock != HOLESTARTBLOCK);
4300 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4301 mval[i].br_state == XFS_EXT_UNWRITTEN);
4304 #endif /* DEBUG */
4308 * Map file blocks to filesystem blocks.
4309 * File range is given by the bno/len pair.
4310 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4311 * into a hole or past eof.
4312 * Only allocates blocks from a single allocation group,
4313 * to avoid locking problems.
4314 * The returned value in "firstblock" from the first call in a transaction
4315 * must be remembered and presented to subsequent calls in "firstblock".
4316 * An upper bound for the number of blocks to be allocated is supplied to
4317 * the first call in "total"; if no allocation group has that many free
4318 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4320 int /* error */
4321 xfs_bmapi(
4322 xfs_trans_t *tp, /* transaction pointer */
4323 xfs_inode_t *ip, /* incore inode */
4324 xfs_fileoff_t bno, /* starting file offs. mapped */
4325 xfs_filblks_t len, /* length to map in file */
4326 int flags, /* XFS_BMAPI_... */
4327 xfs_fsblock_t *firstblock, /* first allocated block
4328 controls a.g. for allocs */
4329 xfs_extlen_t total, /* total blocks needed */
4330 xfs_bmbt_irec_t *mval, /* output: map values */
4331 int *nmap, /* i/o: mval size/count */
4332 xfs_bmap_free_t *flist) /* i/o: list extents to free */
4334 xfs_fsblock_t abno; /* allocated block number */
4335 xfs_extlen_t alen; /* allocated extent length */
4336 xfs_fileoff_t aoff; /* allocated file offset */
4337 xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */
4338 xfs_btree_cur_t *cur; /* bmap btree cursor */
4339 xfs_fileoff_t end; /* end of mapped file region */
4340 int eof; /* we've hit the end of extents */
4341 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
4342 int error; /* error return */
4343 xfs_bmbt_irec_t got; /* current file extent record */
4344 xfs_ifork_t *ifp; /* inode fork pointer */
4345 xfs_extlen_t indlen; /* indirect blocks length */
4346 xfs_extnum_t lastx; /* last useful extent number */
4347 int logflags; /* flags for transaction logging */
4348 xfs_extlen_t minleft; /* min blocks left after allocation */
4349 xfs_extlen_t minlen; /* min allocation size */
4350 xfs_mount_t *mp; /* xfs mount structure */
4351 int n; /* current extent index */
4352 int nallocs; /* number of extents alloc'd */
4353 xfs_extnum_t nextents; /* number of extents in file */
4354 xfs_fileoff_t obno; /* old block number (offset) */
4355 xfs_bmbt_irec_t prev; /* previous file extent record */
4356 int tmp_logflags; /* temp flags holder */
4357 int whichfork; /* data or attr fork */
4358 char inhole; /* current location is hole in file */
4359 char wasdelay; /* old extent was delayed */
4360 char wr; /* this is a write request */
4361 char rt; /* this is a realtime file */
4362 #ifdef DEBUG
4363 xfs_fileoff_t orig_bno; /* original block number value */
4364 int orig_flags; /* original flags arg value */
4365 xfs_filblks_t orig_len; /* original value of len arg */
4366 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4367 int orig_nmap; /* original value of *nmap */
4369 orig_bno = bno;
4370 orig_len = len;
4371 orig_flags = flags;
4372 orig_mval = mval;
4373 orig_nmap = *nmap;
4374 #endif
4375 ASSERT(*nmap >= 1);
4376 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4377 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4378 XFS_ATTR_FORK : XFS_DATA_FORK;
4379 mp = ip->i_mount;
4380 if (unlikely(XFS_TEST_ERROR(
4381 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4382 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4383 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4384 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4385 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4386 return XFS_ERROR(EFSCORRUPTED);
4388 if (XFS_FORCED_SHUTDOWN(mp))
4389 return XFS_ERROR(EIO);
4390 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4391 ifp = XFS_IFORK_PTR(ip, whichfork);
4392 ASSERT(ifp->if_ext_max ==
4393 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4394 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4395 XFS_STATS_INC(xs_blk_mapw);
4396 else
4397 XFS_STATS_INC(xs_blk_mapr);
4399 * IGSTATE flag is used to combine extents which
4400 * differ only due to the state of the extents.
4401 * This technique is used from xfs_getbmap()
4402 * when the caller does not wish to see the
4403 * separation (which is the default).
4405 * This technique is also used when writing a
4406 * buffer which has been partially written,
4407 * (usually by being flushed during a chunkread),
4408 * to ensure one write takes place. This also
4409 * prevents a change in the xfs inode extents at
4410 * this time, intentionally. This change occurs
4411 * on completion of the write operation, in
4412 * xfs_strat_comp(), where the xfs_bmapi() call
4413 * is transactioned, and the extents combined.
4415 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4416 wr = 0; /* no allocations are allowed */
4417 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
4418 logflags = 0;
4419 nallocs = 0;
4420 cur = NULL;
4421 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4422 ASSERT(wr && tp);
4423 if ((error = xfs_bmap_local_to_extents(tp, ip,
4424 firstblock, total, &logflags, whichfork)))
4425 goto error0;
4427 if (wr && *firstblock == NULLFSBLOCK) {
4428 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4429 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4430 else
4431 minleft = 1;
4432 } else
4433 minleft = 0;
4434 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4435 (error = xfs_iread_extents(tp, ip, whichfork)))
4436 goto error0;
4437 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4438 &prev);
4439 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4440 n = 0;
4441 end = bno + len;
4442 obno = bno;
4443 bma.ip = NULL;
4445 while (bno < end && n < *nmap) {
4447 * Reading past eof, act as though there's a hole
4448 * up to end.
4450 if (eof && !wr)
4451 got.br_startoff = end;
4452 inhole = eof || got.br_startoff > bno;
4453 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
4454 isnullstartblock(got.br_startblock);
4456 * First, deal with the hole before the allocated space
4457 * that we found, if any.
4459 if (wr && (inhole || wasdelay)) {
4461 * For the wasdelay case, we could also just
4462 * allocate the stuff asked for in this bmap call
4463 * but that wouldn't be as good.
4465 if (wasdelay) {
4466 alen = (xfs_extlen_t)got.br_blockcount;
4467 aoff = got.br_startoff;
4468 if (lastx != NULLEXTNUM && lastx) {
4469 ep = xfs_iext_get_ext(ifp, lastx - 1);
4470 xfs_bmbt_get_all(ep, &prev);
4472 } else {
4473 alen = (xfs_extlen_t)
4474 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4475 if (!eof)
4476 alen = (xfs_extlen_t)
4477 XFS_FILBLKS_MIN(alen,
4478 got.br_startoff - bno);
4479 aoff = bno;
4481 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4482 if (flags & XFS_BMAPI_DELAY) {
4483 xfs_extlen_t extsz;
4485 /* Figure out the extent size, adjust alen */
4486 extsz = xfs_get_extsz_hint(ip);
4487 if (extsz) {
4488 error = xfs_bmap_extsize_align(mp,
4489 &got, &prev, extsz,
4490 rt, eof,
4491 flags&XFS_BMAPI_DELAY,
4492 flags&XFS_BMAPI_CONVERT,
4493 &aoff, &alen);
4494 ASSERT(!error);
4497 if (rt)
4498 extsz = alen / mp->m_sb.sb_rextsize;
4501 * Make a transaction-less quota reservation for
4502 * delayed allocation blocks. This number gets
4503 * adjusted later. We return if we haven't
4504 * allocated blocks already inside this loop.
4506 error = xfs_trans_reserve_quota_nblks(
4507 NULL, ip, (long)alen, 0,
4508 rt ? XFS_QMOPT_RES_RTBLKS :
4509 XFS_QMOPT_RES_REGBLKS);
4510 if (error) {
4511 if (n == 0) {
4512 *nmap = 0;
4513 ASSERT(cur == NULL);
4514 return error;
4516 break;
4520 * Split changing sb for alen and indlen since
4521 * they could be coming from different places.
4523 indlen = (xfs_extlen_t)
4524 xfs_bmap_worst_indlen(ip, alen);
4525 ASSERT(indlen > 0);
4527 if (rt) {
4528 error = xfs_mod_incore_sb(mp,
4529 XFS_SBS_FREXTENTS,
4530 -((int64_t)extsz), (flags &
4531 XFS_BMAPI_RSVBLOCKS));
4532 } else {
4533 error = xfs_icsb_modify_counters(mp,
4534 XFS_SBS_FDBLOCKS,
4535 -((int64_t)alen), (flags &
4536 XFS_BMAPI_RSVBLOCKS));
4538 if (!error) {
4539 error = xfs_icsb_modify_counters(mp,
4540 XFS_SBS_FDBLOCKS,
4541 -((int64_t)indlen), (flags &
4542 XFS_BMAPI_RSVBLOCKS));
4543 if (error && rt)
4544 xfs_mod_incore_sb(mp,
4545 XFS_SBS_FREXTENTS,
4546 (int64_t)extsz, (flags &
4547 XFS_BMAPI_RSVBLOCKS));
4548 else if (error)
4549 xfs_icsb_modify_counters(mp,
4550 XFS_SBS_FDBLOCKS,
4551 (int64_t)alen, (flags &
4552 XFS_BMAPI_RSVBLOCKS));
4555 if (error) {
4556 if (XFS_IS_QUOTA_ON(mp))
4557 /* unreserve the blocks now */
4558 (void)
4559 xfs_trans_unreserve_quota_nblks(
4560 NULL, ip,
4561 (long)alen, 0, rt ?
4562 XFS_QMOPT_RES_RTBLKS :
4563 XFS_QMOPT_RES_REGBLKS);
4564 break;
4567 ip->i_delayed_blks += alen;
4568 abno = nullstartblock(indlen);
4569 } else {
4571 * If first time, allocate and fill in
4572 * once-only bma fields.
4574 if (bma.ip == NULL) {
4575 bma.tp = tp;
4576 bma.ip = ip;
4577 bma.prevp = &prev;
4578 bma.gotp = &got;
4579 bma.total = total;
4580 bma.userdata = 0;
4582 /* Indicate if this is the first user data
4583 * in the file, or just any user data.
4585 if (!(flags & XFS_BMAPI_METADATA)) {
4586 bma.userdata = (aoff == 0) ?
4587 XFS_ALLOC_INITIAL_USER_DATA :
4588 XFS_ALLOC_USERDATA;
4591 * Fill in changeable bma fields.
4593 bma.eof = eof;
4594 bma.firstblock = *firstblock;
4595 bma.alen = alen;
4596 bma.off = aoff;
4597 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4598 bma.wasdel = wasdelay;
4599 bma.minlen = minlen;
4600 bma.low = flist->xbf_low;
4601 bma.minleft = minleft;
4603 * Only want to do the alignment at the
4604 * eof if it is userdata and allocation length
4605 * is larger than a stripe unit.
4607 if (mp->m_dalign && alen >= mp->m_dalign &&
4608 (!(flags & XFS_BMAPI_METADATA)) &&
4609 (whichfork == XFS_DATA_FORK)) {
4610 if ((error = xfs_bmap_isaeof(ip, aoff,
4611 whichfork, &bma.aeof)))
4612 goto error0;
4613 } else
4614 bma.aeof = 0;
4616 * Call allocator.
4618 if ((error = xfs_bmap_alloc(&bma)))
4619 goto error0;
4621 * Copy out result fields.
4623 abno = bma.rval;
4624 if ((flist->xbf_low = bma.low))
4625 minleft = 0;
4626 alen = bma.alen;
4627 aoff = bma.off;
4628 ASSERT(*firstblock == NULLFSBLOCK ||
4629 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4630 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4631 (flist->xbf_low &&
4632 XFS_FSB_TO_AGNO(mp, *firstblock) <
4633 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4634 *firstblock = bma.firstblock;
4635 if (cur)
4636 cur->bc_private.b.firstblock =
4637 *firstblock;
4638 if (abno == NULLFSBLOCK)
4639 break;
4640 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4641 cur = xfs_bmbt_init_cursor(mp, tp,
4642 ip, whichfork);
4643 cur->bc_private.b.firstblock =
4644 *firstblock;
4645 cur->bc_private.b.flist = flist;
4648 * Bump the number of extents we've allocated
4649 * in this call.
4651 nallocs++;
4653 if (cur)
4654 cur->bc_private.b.flags =
4655 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4656 got.br_startoff = aoff;
4657 got.br_startblock = abno;
4658 got.br_blockcount = alen;
4659 got.br_state = XFS_EXT_NORM; /* assume normal */
4661 * Determine state of extent, and the filesystem.
4662 * A wasdelay extent has been initialized, so
4663 * shouldn't be flagged as unwritten.
4665 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
4666 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
4667 got.br_state = XFS_EXT_UNWRITTEN;
4669 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
4670 firstblock, flist, &tmp_logflags,
4671 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
4672 logflags |= tmp_logflags;
4673 if (error)
4674 goto error0;
4675 lastx = ifp->if_lastex;
4676 ep = xfs_iext_get_ext(ifp, lastx);
4677 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4678 xfs_bmbt_get_all(ep, &got);
4679 ASSERT(got.br_startoff <= aoff);
4680 ASSERT(got.br_startoff + got.br_blockcount >=
4681 aoff + alen);
4682 #ifdef DEBUG
4683 if (flags & XFS_BMAPI_DELAY) {
4684 ASSERT(isnullstartblock(got.br_startblock));
4685 ASSERT(startblockval(got.br_startblock) > 0);
4687 ASSERT(got.br_state == XFS_EXT_NORM ||
4688 got.br_state == XFS_EXT_UNWRITTEN);
4689 #endif
4691 * Fall down into the found allocated space case.
4693 } else if (inhole) {
4695 * Reading in a hole.
4697 mval->br_startoff = bno;
4698 mval->br_startblock = HOLESTARTBLOCK;
4699 mval->br_blockcount =
4700 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4701 mval->br_state = XFS_EXT_NORM;
4702 bno += mval->br_blockcount;
4703 len -= mval->br_blockcount;
4704 mval++;
4705 n++;
4706 continue;
4709 * Then deal with the allocated space we found.
4711 ASSERT(ep != NULL);
4712 if (!(flags & XFS_BMAPI_ENTIRE) &&
4713 (got.br_startoff + got.br_blockcount > obno)) {
4714 if (obno > bno)
4715 bno = obno;
4716 ASSERT((bno >= obno) || (n == 0));
4717 ASSERT(bno < end);
4718 mval->br_startoff = bno;
4719 if (isnullstartblock(got.br_startblock)) {
4720 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
4721 mval->br_startblock = DELAYSTARTBLOCK;
4722 } else
4723 mval->br_startblock =
4724 got.br_startblock +
4725 (bno - got.br_startoff);
4727 * Return the minimum of what we got and what we
4728 * asked for for the length. We can use the len
4729 * variable here because it is modified below
4730 * and we could have been there before coming
4731 * here if the first part of the allocation
4732 * didn't overlap what was asked for.
4734 mval->br_blockcount =
4735 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
4736 (bno - got.br_startoff));
4737 mval->br_state = got.br_state;
4738 ASSERT(mval->br_blockcount <= len);
4739 } else {
4740 *mval = got;
4741 if (isnullstartblock(mval->br_startblock)) {
4742 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
4743 mval->br_startblock = DELAYSTARTBLOCK;
4748 * Check if writing previously allocated but
4749 * unwritten extents.
4751 if (wr &&
4752 ((mval->br_state == XFS_EXT_UNWRITTEN &&
4753 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) ||
4754 (mval->br_state == XFS_EXT_NORM &&
4755 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT)) ==
4756 (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT))))) {
4758 * Modify (by adding) the state flag, if writing.
4760 ASSERT(mval->br_blockcount <= len);
4761 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
4762 cur = xfs_bmbt_init_cursor(mp,
4763 tp, ip, whichfork);
4764 cur->bc_private.b.firstblock =
4765 *firstblock;
4766 cur->bc_private.b.flist = flist;
4768 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4769 ? XFS_EXT_NORM
4770 : XFS_EXT_UNWRITTEN;
4771 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
4772 firstblock, flist, &tmp_logflags,
4773 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
4774 logflags |= tmp_logflags;
4775 if (error)
4776 goto error0;
4777 lastx = ifp->if_lastex;
4778 ep = xfs_iext_get_ext(ifp, lastx);
4779 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4780 xfs_bmbt_get_all(ep, &got);
4782 * We may have combined previously unwritten
4783 * space with written space, so generate
4784 * another request.
4786 if (mval->br_blockcount < len)
4787 continue;
4790 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4791 ((mval->br_startoff + mval->br_blockcount) <= end));
4792 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4793 (mval->br_blockcount <= len) ||
4794 (mval->br_startoff < obno));
4795 bno = mval->br_startoff + mval->br_blockcount;
4796 len = end - bno;
4797 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4798 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4799 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4800 ASSERT(mval->br_state == mval[-1].br_state);
4801 mval[-1].br_blockcount = mval->br_blockcount;
4802 mval[-1].br_state = mval->br_state;
4803 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4804 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4805 mval[-1].br_startblock != HOLESTARTBLOCK &&
4806 mval->br_startblock ==
4807 mval[-1].br_startblock + mval[-1].br_blockcount &&
4808 ((flags & XFS_BMAPI_IGSTATE) ||
4809 mval[-1].br_state == mval->br_state)) {
4810 ASSERT(mval->br_startoff ==
4811 mval[-1].br_startoff + mval[-1].br_blockcount);
4812 mval[-1].br_blockcount += mval->br_blockcount;
4813 } else if (n > 0 &&
4814 mval->br_startblock == DELAYSTARTBLOCK &&
4815 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4816 mval->br_startoff ==
4817 mval[-1].br_startoff + mval[-1].br_blockcount) {
4818 mval[-1].br_blockcount += mval->br_blockcount;
4819 mval[-1].br_state = mval->br_state;
4820 } else if (!((n == 0) &&
4821 ((mval->br_startoff + mval->br_blockcount) <=
4822 obno))) {
4823 mval++;
4824 n++;
4827 * If we're done, stop now. Stop when we've allocated
4828 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4829 * the transaction may get too big.
4831 if (bno >= end || n >= *nmap || nallocs >= *nmap)
4832 break;
4834 * Else go on to the next record.
4836 ep = xfs_iext_get_ext(ifp, ++lastx);
4837 prev = got;
4838 if (lastx >= nextents)
4839 eof = 1;
4840 else
4841 xfs_bmbt_get_all(ep, &got);
4843 ifp->if_lastex = lastx;
4844 *nmap = n;
4846 * Transform from btree to extents, give it cur.
4848 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4849 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4850 ASSERT(wr && cur);
4851 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4852 &tmp_logflags, whichfork);
4853 logflags |= tmp_logflags;
4854 if (error)
4855 goto error0;
4857 ASSERT(ifp->if_ext_max ==
4858 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4859 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4860 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
4861 error = 0;
4862 error0:
4864 * Log everything. Do this after conversion, there's no point in
4865 * logging the extent records if we've converted to btree format.
4867 if ((logflags & xfs_ilog_fext(whichfork)) &&
4868 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4869 logflags &= ~xfs_ilog_fext(whichfork);
4870 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
4871 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4872 logflags &= ~xfs_ilog_fbroot(whichfork);
4874 * Log whatever the flags say, even if error. Otherwise we might miss
4875 * detecting a case where the data is changed, there's an error,
4876 * and it's not logged so we don't shutdown when we should.
4878 if (logflags) {
4879 ASSERT(tp && wr);
4880 xfs_trans_log_inode(tp, ip, logflags);
4882 if (cur) {
4883 if (!error) {
4884 ASSERT(*firstblock == NULLFSBLOCK ||
4885 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4886 XFS_FSB_TO_AGNO(mp,
4887 cur->bc_private.b.firstblock) ||
4888 (flist->xbf_low &&
4889 XFS_FSB_TO_AGNO(mp, *firstblock) <
4890 XFS_FSB_TO_AGNO(mp,
4891 cur->bc_private.b.firstblock)));
4892 *firstblock = cur->bc_private.b.firstblock;
4894 xfs_btree_del_cursor(cur,
4895 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4897 if (!error)
4898 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4899 orig_nmap, *nmap);
4900 return error;
4904 * Map file blocks to filesystem blocks, simple version.
4905 * One block (extent) only, read-only.
4906 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
4907 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
4908 * was set and all the others were clear.
4910 int /* error */
4911 xfs_bmapi_single(
4912 xfs_trans_t *tp, /* transaction pointer */
4913 xfs_inode_t *ip, /* incore inode */
4914 int whichfork, /* data or attr fork */
4915 xfs_fsblock_t *fsb, /* output: mapped block */
4916 xfs_fileoff_t bno) /* starting file offs. mapped */
4918 int eof; /* we've hit the end of extents */
4919 int error; /* error return */
4920 xfs_bmbt_irec_t got; /* current file extent record */
4921 xfs_ifork_t *ifp; /* inode fork pointer */
4922 xfs_extnum_t lastx; /* last useful extent number */
4923 xfs_bmbt_irec_t prev; /* previous file extent record */
4925 ifp = XFS_IFORK_PTR(ip, whichfork);
4926 if (unlikely(
4927 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4928 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
4929 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
4930 ip->i_mount);
4931 return XFS_ERROR(EFSCORRUPTED);
4933 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
4934 return XFS_ERROR(EIO);
4935 XFS_STATS_INC(xs_blk_mapr);
4936 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4937 (error = xfs_iread_extents(tp, ip, whichfork)))
4938 return error;
4939 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4940 &prev);
4942 * Reading past eof, act as though there's a hole
4943 * up to end.
4945 if (eof || got.br_startoff > bno) {
4946 *fsb = NULLFSBLOCK;
4947 return 0;
4949 ASSERT(!isnullstartblock(got.br_startblock));
4950 ASSERT(bno < got.br_startoff + got.br_blockcount);
4951 *fsb = got.br_startblock + (bno - got.br_startoff);
4952 ifp->if_lastex = lastx;
4953 return 0;
4957 * Unmap (remove) blocks from a file.
4958 * If nexts is nonzero then the number of extents to remove is limited to
4959 * that value. If not all extents in the block range can be removed then
4960 * *done is set.
4962 int /* error */
4963 xfs_bunmapi(
4964 xfs_trans_t *tp, /* transaction pointer */
4965 struct xfs_inode *ip, /* incore inode */
4966 xfs_fileoff_t bno, /* starting offset to unmap */
4967 xfs_filblks_t len, /* length to unmap in file */
4968 int flags, /* misc flags */
4969 xfs_extnum_t nexts, /* number of extents max */
4970 xfs_fsblock_t *firstblock, /* first allocated block
4971 controls a.g. for allocs */
4972 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4973 int *done) /* set if not done yet */
4975 xfs_btree_cur_t *cur; /* bmap btree cursor */
4976 xfs_bmbt_irec_t del; /* extent being deleted */
4977 int eof; /* is deleting at eof */
4978 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
4979 int error; /* error return value */
4980 xfs_extnum_t extno; /* extent number in list */
4981 xfs_bmbt_irec_t got; /* current extent record */
4982 xfs_ifork_t *ifp; /* inode fork pointer */
4983 int isrt; /* freeing in rt area */
4984 xfs_extnum_t lastx; /* last extent index used */
4985 int logflags; /* transaction logging flags */
4986 xfs_extlen_t mod; /* rt extent offset */
4987 xfs_mount_t *mp; /* mount structure */
4988 xfs_extnum_t nextents; /* number of file extents */
4989 xfs_bmbt_irec_t prev; /* previous extent record */
4990 xfs_fileoff_t start; /* first file offset deleted */
4991 int tmp_logflags; /* partial logging flags */
4992 int wasdel; /* was a delayed alloc extent */
4993 int whichfork; /* data or attribute fork */
4994 int rsvd; /* OK to allocate reserved blocks */
4995 xfs_fsblock_t sum;
4997 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
4999 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5000 XFS_ATTR_FORK : XFS_DATA_FORK;
5001 ifp = XFS_IFORK_PTR(ip, whichfork);
5002 if (unlikely(
5003 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5004 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5005 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5006 ip->i_mount);
5007 return XFS_ERROR(EFSCORRUPTED);
5009 mp = ip->i_mount;
5010 if (XFS_FORCED_SHUTDOWN(mp))
5011 return XFS_ERROR(EIO);
5012 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5013 ASSERT(len > 0);
5014 ASSERT(nexts >= 0);
5015 ASSERT(ifp->if_ext_max ==
5016 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5017 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5018 (error = xfs_iread_extents(tp, ip, whichfork)))
5019 return error;
5020 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5021 if (nextents == 0) {
5022 *done = 1;
5023 return 0;
5025 XFS_STATS_INC(xs_blk_unmap);
5026 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5027 start = bno;
5028 bno = start + len - 1;
5029 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5030 &prev);
5033 * Check to see if the given block number is past the end of the
5034 * file, back up to the last block if so...
5036 if (eof) {
5037 ep = xfs_iext_get_ext(ifp, --lastx);
5038 xfs_bmbt_get_all(ep, &got);
5039 bno = got.br_startoff + got.br_blockcount - 1;
5041 logflags = 0;
5042 if (ifp->if_flags & XFS_IFBROOT) {
5043 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5044 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5045 cur->bc_private.b.firstblock = *firstblock;
5046 cur->bc_private.b.flist = flist;
5047 cur->bc_private.b.flags = 0;
5048 } else
5049 cur = NULL;
5050 extno = 0;
5051 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5052 (nexts == 0 || extno < nexts)) {
5054 * Is the found extent after a hole in which bno lives?
5055 * Just back up to the previous extent, if so.
5057 if (got.br_startoff > bno) {
5058 if (--lastx < 0)
5059 break;
5060 ep = xfs_iext_get_ext(ifp, lastx);
5061 xfs_bmbt_get_all(ep, &got);
5064 * Is the last block of this extent before the range
5065 * we're supposed to delete? If so, we're done.
5067 bno = XFS_FILEOFF_MIN(bno,
5068 got.br_startoff + got.br_blockcount - 1);
5069 if (bno < start)
5070 break;
5072 * Then deal with the (possibly delayed) allocated space
5073 * we found.
5075 ASSERT(ep != NULL);
5076 del = got;
5077 wasdel = isnullstartblock(del.br_startblock);
5078 if (got.br_startoff < start) {
5079 del.br_startoff = start;
5080 del.br_blockcount -= start - got.br_startoff;
5081 if (!wasdel)
5082 del.br_startblock += start - got.br_startoff;
5084 if (del.br_startoff + del.br_blockcount > bno + 1)
5085 del.br_blockcount = bno + 1 - del.br_startoff;
5086 sum = del.br_startblock + del.br_blockcount;
5087 if (isrt &&
5088 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5090 * Realtime extent not lined up at the end.
5091 * The extent could have been split into written
5092 * and unwritten pieces, or we could just be
5093 * unmapping part of it. But we can't really
5094 * get rid of part of a realtime extent.
5096 if (del.br_state == XFS_EXT_UNWRITTEN ||
5097 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5099 * This piece is unwritten, or we're not
5100 * using unwritten extents. Skip over it.
5102 ASSERT(bno >= mod);
5103 bno -= mod > del.br_blockcount ?
5104 del.br_blockcount : mod;
5105 if (bno < got.br_startoff) {
5106 if (--lastx >= 0)
5107 xfs_bmbt_get_all(xfs_iext_get_ext(
5108 ifp, lastx), &got);
5110 continue;
5113 * It's written, turn it unwritten.
5114 * This is better than zeroing it.
5116 ASSERT(del.br_state == XFS_EXT_NORM);
5117 ASSERT(xfs_trans_get_block_res(tp) > 0);
5119 * If this spans a realtime extent boundary,
5120 * chop it back to the start of the one we end at.
5122 if (del.br_blockcount > mod) {
5123 del.br_startoff += del.br_blockcount - mod;
5124 del.br_startblock += del.br_blockcount - mod;
5125 del.br_blockcount = mod;
5127 del.br_state = XFS_EXT_UNWRITTEN;
5128 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
5129 firstblock, flist, &logflags,
5130 XFS_DATA_FORK, 0);
5131 if (error)
5132 goto error0;
5133 goto nodelete;
5135 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5137 * Realtime extent is lined up at the end but not
5138 * at the front. We'll get rid of full extents if
5139 * we can.
5141 mod = mp->m_sb.sb_rextsize - mod;
5142 if (del.br_blockcount > mod) {
5143 del.br_blockcount -= mod;
5144 del.br_startoff += mod;
5145 del.br_startblock += mod;
5146 } else if ((del.br_startoff == start &&
5147 (del.br_state == XFS_EXT_UNWRITTEN ||
5148 xfs_trans_get_block_res(tp) == 0)) ||
5149 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5151 * Can't make it unwritten. There isn't
5152 * a full extent here so just skip it.
5154 ASSERT(bno >= del.br_blockcount);
5155 bno -= del.br_blockcount;
5156 if (bno < got.br_startoff) {
5157 if (--lastx >= 0)
5158 xfs_bmbt_get_all(--ep, &got);
5160 continue;
5161 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5163 * This one is already unwritten.
5164 * It must have a written left neighbor.
5165 * Unwrite the killed part of that one and
5166 * try again.
5168 ASSERT(lastx > 0);
5169 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5170 lastx - 1), &prev);
5171 ASSERT(prev.br_state == XFS_EXT_NORM);
5172 ASSERT(!isnullstartblock(prev.br_startblock));
5173 ASSERT(del.br_startblock ==
5174 prev.br_startblock + prev.br_blockcount);
5175 if (prev.br_startoff < start) {
5176 mod = start - prev.br_startoff;
5177 prev.br_blockcount -= mod;
5178 prev.br_startblock += mod;
5179 prev.br_startoff = start;
5181 prev.br_state = XFS_EXT_UNWRITTEN;
5182 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5183 &prev, firstblock, flist, &logflags,
5184 XFS_DATA_FORK, 0);
5185 if (error)
5186 goto error0;
5187 goto nodelete;
5188 } else {
5189 ASSERT(del.br_state == XFS_EXT_NORM);
5190 del.br_state = XFS_EXT_UNWRITTEN;
5191 error = xfs_bmap_add_extent(ip, lastx, &cur,
5192 &del, firstblock, flist, &logflags,
5193 XFS_DATA_FORK, 0);
5194 if (error)
5195 goto error0;
5196 goto nodelete;
5199 if (wasdel) {
5200 ASSERT(startblockval(del.br_startblock) > 0);
5201 /* Update realtime/data freespace, unreserve quota */
5202 if (isrt) {
5203 xfs_filblks_t rtexts;
5205 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5206 do_div(rtexts, mp->m_sb.sb_rextsize);
5207 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5208 (int64_t)rtexts, rsvd);
5209 (void)xfs_trans_reserve_quota_nblks(NULL,
5210 ip, -((long)del.br_blockcount), 0,
5211 XFS_QMOPT_RES_RTBLKS);
5212 } else {
5213 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
5214 (int64_t)del.br_blockcount, rsvd);
5215 (void)xfs_trans_reserve_quota_nblks(NULL,
5216 ip, -((long)del.br_blockcount), 0,
5217 XFS_QMOPT_RES_REGBLKS);
5219 ip->i_delayed_blks -= del.br_blockcount;
5220 if (cur)
5221 cur->bc_private.b.flags |=
5222 XFS_BTCUR_BPRV_WASDEL;
5223 } else if (cur)
5224 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5226 * If it's the case where the directory code is running
5227 * with no block reservation, and the deleted block is in
5228 * the middle of its extent, and the resulting insert
5229 * of an extent would cause transformation to btree format,
5230 * then reject it. The calling code will then swap
5231 * blocks around instead.
5232 * We have to do this now, rather than waiting for the
5233 * conversion to btree format, since the transaction
5234 * will be dirty.
5236 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5237 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5238 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5239 del.br_startoff > got.br_startoff &&
5240 del.br_startoff + del.br_blockcount <
5241 got.br_startoff + got.br_blockcount) {
5242 error = XFS_ERROR(ENOSPC);
5243 goto error0;
5245 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
5246 &tmp_logflags, whichfork, rsvd);
5247 logflags |= tmp_logflags;
5248 if (error)
5249 goto error0;
5250 bno = del.br_startoff - 1;
5251 nodelete:
5252 lastx = ifp->if_lastex;
5254 * If not done go on to the next (previous) record.
5255 * Reset ep in case the extents array was re-alloced.
5257 ep = xfs_iext_get_ext(ifp, lastx);
5258 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5259 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5260 xfs_bmbt_get_startoff(ep) > bno) {
5261 if (--lastx >= 0)
5262 ep = xfs_iext_get_ext(ifp, lastx);
5264 if (lastx >= 0)
5265 xfs_bmbt_get_all(ep, &got);
5266 extno++;
5269 ifp->if_lastex = lastx;
5270 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5271 ASSERT(ifp->if_ext_max ==
5272 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5274 * Convert to a btree if necessary.
5276 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5277 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5278 ASSERT(cur == NULL);
5279 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5280 &cur, 0, &tmp_logflags, whichfork);
5281 logflags |= tmp_logflags;
5282 if (error)
5283 goto error0;
5286 * transform from btree to extents, give it cur
5288 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5289 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5290 ASSERT(cur != NULL);
5291 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5292 whichfork);
5293 logflags |= tmp_logflags;
5294 if (error)
5295 goto error0;
5298 * transform from extents to local?
5300 ASSERT(ifp->if_ext_max ==
5301 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5302 error = 0;
5303 error0:
5305 * Log everything. Do this after conversion, there's no point in
5306 * logging the extent records if we've converted to btree format.
5308 if ((logflags & xfs_ilog_fext(whichfork)) &&
5309 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5310 logflags &= ~xfs_ilog_fext(whichfork);
5311 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5312 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5313 logflags &= ~xfs_ilog_fbroot(whichfork);
5315 * Log inode even in the error case, if the transaction
5316 * is dirty we'll need to shut down the filesystem.
5318 if (logflags)
5319 xfs_trans_log_inode(tp, ip, logflags);
5320 if (cur) {
5321 if (!error) {
5322 *firstblock = cur->bc_private.b.firstblock;
5323 cur->bc_private.b.allocated = 0;
5325 xfs_btree_del_cursor(cur,
5326 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5328 return error;
5332 * returns 1 for success, 0 if we failed to map the extent.
5334 STATIC int
5335 xfs_getbmapx_fix_eof_hole(
5336 xfs_inode_t *ip, /* xfs incore inode pointer */
5337 struct getbmapx *out, /* output structure */
5338 int prealloced, /* this is a file with
5339 * preallocated data space */
5340 __int64_t end, /* last block requested */
5341 xfs_fsblock_t startblock)
5343 __int64_t fixlen;
5344 xfs_mount_t *mp; /* file system mount point */
5345 xfs_ifork_t *ifp; /* inode fork pointer */
5346 xfs_extnum_t lastx; /* last extent pointer */
5347 xfs_fileoff_t fileblock;
5349 if (startblock == HOLESTARTBLOCK) {
5350 mp = ip->i_mount;
5351 out->bmv_block = -1;
5352 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5353 fixlen -= out->bmv_offset;
5354 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5355 /* Came to hole at EOF. Trim it. */
5356 if (fixlen <= 0)
5357 return 0;
5358 out->bmv_length = fixlen;
5360 } else {
5361 if (startblock == DELAYSTARTBLOCK)
5362 out->bmv_block = -2;
5363 else
5364 out->bmv_block = xfs_fsb_to_db(ip, startblock);
5365 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5366 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5367 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5368 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5369 out->bmv_oflags |= BMV_OF_LAST;
5372 return 1;
5376 * Get inode's extents as described in bmv, and format for output.
5377 * Calls formatter to fill the user's buffer until all extents
5378 * are mapped, until the passed-in bmv->bmv_count slots have
5379 * been filled, or until the formatter short-circuits the loop,
5380 * if it is tracking filled-in extents on its own.
5382 int /* error code */
5383 xfs_getbmap(
5384 xfs_inode_t *ip,
5385 struct getbmapx *bmv, /* user bmap structure */
5386 xfs_bmap_format_t formatter, /* format to user */
5387 void *arg) /* formatter arg */
5389 __int64_t bmvend; /* last block requested */
5390 int error = 0; /* return value */
5391 __int64_t fixlen; /* length for -1 case */
5392 int i; /* extent number */
5393 int lock; /* lock state */
5394 xfs_bmbt_irec_t *map; /* buffer for user's data */
5395 xfs_mount_t *mp; /* file system mount point */
5396 int nex; /* # of user extents can do */
5397 int nexleft; /* # of user extents left */
5398 int subnex; /* # of bmapi's can do */
5399 int nmap; /* number of map entries */
5400 struct getbmapx *out; /* output structure */
5401 int whichfork; /* data or attr fork */
5402 int prealloced; /* this is a file with
5403 * preallocated data space */
5404 int iflags; /* interface flags */
5405 int bmapi_flags; /* flags for xfs_bmapi */
5406 int cur_ext = 0;
5408 mp = ip->i_mount;
5409 iflags = bmv->bmv_iflags;
5410 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5412 if (whichfork == XFS_ATTR_FORK) {
5413 if (XFS_IFORK_Q(ip)) {
5414 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5415 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5416 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5417 return XFS_ERROR(EINVAL);
5418 } else if (unlikely(
5419 ip->i_d.di_aformat != 0 &&
5420 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5421 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5422 ip->i_mount);
5423 return XFS_ERROR(EFSCORRUPTED);
5426 prealloced = 0;
5427 fixlen = 1LL << 32;
5428 } else {
5429 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5430 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5431 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5432 return XFS_ERROR(EINVAL);
5434 if (xfs_get_extsz_hint(ip) ||
5435 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5436 prealloced = 1;
5437 fixlen = XFS_MAXIOFFSET(mp);
5438 } else {
5439 prealloced = 0;
5440 fixlen = ip->i_size;
5444 if (bmv->bmv_length == -1) {
5445 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5446 bmv->bmv_length =
5447 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5448 } else if (bmv->bmv_length == 0) {
5449 bmv->bmv_entries = 0;
5450 return 0;
5451 } else if (bmv->bmv_length < 0) {
5452 return XFS_ERROR(EINVAL);
5455 nex = bmv->bmv_count - 1;
5456 if (nex <= 0)
5457 return XFS_ERROR(EINVAL);
5458 bmvend = bmv->bmv_offset + bmv->bmv_length;
5461 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5462 return XFS_ERROR(ENOMEM);
5463 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5464 if (!out)
5465 return XFS_ERROR(ENOMEM);
5467 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5468 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5469 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5470 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5471 if (error)
5472 goto out_unlock_iolock;
5475 * even after flushing the inode, there can still be delalloc
5476 * blocks on the inode beyond EOF due to speculative
5477 * preallocation. These are not removed until the release
5478 * function is called or the inode is inactivated. Hence we
5479 * cannot assert here that ip->i_delayed_blks == 0.
5483 lock = xfs_ilock_map_shared(ip);
5486 * Don't let nex be bigger than the number of extents
5487 * we can have assuming alternating holes and real extents.
5489 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5490 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5492 bmapi_flags = xfs_bmapi_aflag(whichfork);
5493 if (!(iflags & BMV_IF_PREALLOC))
5494 bmapi_flags |= XFS_BMAPI_IGSTATE;
5497 * Allocate enough space to handle "subnex" maps at a time.
5499 error = ENOMEM;
5500 subnex = 16;
5501 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
5502 if (!map)
5503 goto out_unlock_ilock;
5505 bmv->bmv_entries = 0;
5507 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5508 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5509 error = 0;
5510 goto out_free_map;
5513 nexleft = nex;
5515 do {
5516 nmap = (nexleft > subnex) ? subnex : nexleft;
5517 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5518 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5519 bmapi_flags, NULL, 0, map, &nmap,
5520 NULL);
5521 if (error)
5522 goto out_free_map;
5523 ASSERT(nmap <= subnex);
5525 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5526 out[cur_ext].bmv_oflags = 0;
5527 if (map[i].br_state == XFS_EXT_UNWRITTEN)
5528 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
5529 else if (map[i].br_startblock == DELAYSTARTBLOCK)
5530 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5531 out[cur_ext].bmv_offset =
5532 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5533 out[cur_ext].bmv_length =
5534 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5535 out[cur_ext].bmv_unused1 = 0;
5536 out[cur_ext].bmv_unused2 = 0;
5537 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5538 (map[i].br_startblock != DELAYSTARTBLOCK));
5539 if (map[i].br_startblock == HOLESTARTBLOCK &&
5540 whichfork == XFS_ATTR_FORK) {
5541 /* came to the end of attribute fork */
5542 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
5543 goto out_free_map;
5546 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5547 prealloced, bmvend,
5548 map[i].br_startblock))
5549 goto out_free_map;
5551 bmv->bmv_offset =
5552 out[cur_ext].bmv_offset +
5553 out[cur_ext].bmv_length;
5554 bmv->bmv_length =
5555 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
5558 * In case we don't want to return the hole,
5559 * don't increase cur_ext so that we can reuse
5560 * it in the next loop.
5562 if ((iflags & BMV_IF_NO_HOLES) &&
5563 map[i].br_startblock == HOLESTARTBLOCK) {
5564 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5565 continue;
5568 nexleft--;
5569 bmv->bmv_entries++;
5570 cur_ext++;
5572 } while (nmap && nexleft && bmv->bmv_length);
5574 out_free_map:
5575 kmem_free(map);
5576 out_unlock_ilock:
5577 xfs_iunlock_map_shared(ip, lock);
5578 out_unlock_iolock:
5579 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5581 for (i = 0; i < cur_ext; i++) {
5582 int full = 0; /* user array is full */
5584 /* format results & advance arg */
5585 error = formatter(&arg, &out[i], &full);
5586 if (error || full)
5587 break;
5590 kmem_free(out);
5591 return error;
5595 * Check the last inode extent to determine whether this allocation will result
5596 * in blocks being allocated at the end of the file. When we allocate new data
5597 * blocks at the end of the file which do not start at the previous data block,
5598 * we will try to align the new blocks at stripe unit boundaries.
5600 STATIC int /* error */
5601 xfs_bmap_isaeof(
5602 xfs_inode_t *ip, /* incore inode pointer */
5603 xfs_fileoff_t off, /* file offset in fsblocks */
5604 int whichfork, /* data or attribute fork */
5605 char *aeof) /* return value */
5607 int error; /* error return value */
5608 xfs_ifork_t *ifp; /* inode fork pointer */
5609 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
5610 xfs_extnum_t nextents; /* number of file extents */
5611 xfs_bmbt_irec_t s; /* expanded extent record */
5613 ASSERT(whichfork == XFS_DATA_FORK);
5614 ifp = XFS_IFORK_PTR(ip, whichfork);
5615 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5616 (error = xfs_iread_extents(NULL, ip, whichfork)))
5617 return error;
5618 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5619 if (nextents == 0) {
5620 *aeof = 1;
5621 return 0;
5624 * Go to the last extent
5626 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5627 xfs_bmbt_get_all(lastrec, &s);
5629 * Check we are allocating in the last extent (for delayed allocations)
5630 * or past the last extent for non-delayed allocations.
5632 *aeof = (off >= s.br_startoff &&
5633 off < s.br_startoff + s.br_blockcount &&
5634 isnullstartblock(s.br_startblock)) ||
5635 off >= s.br_startoff + s.br_blockcount;
5636 return 0;
5640 * Check if the endoff is outside the last extent. If so the caller will grow
5641 * the allocation to a stripe unit boundary.
5643 int /* error */
5644 xfs_bmap_eof(
5645 xfs_inode_t *ip, /* incore inode pointer */
5646 xfs_fileoff_t endoff, /* file offset in fsblocks */
5647 int whichfork, /* data or attribute fork */
5648 int *eof) /* result value */
5650 xfs_fsblock_t blockcount; /* extent block count */
5651 int error; /* error return value */
5652 xfs_ifork_t *ifp; /* inode fork pointer */
5653 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
5654 xfs_extnum_t nextents; /* number of file extents */
5655 xfs_fileoff_t startoff; /* extent starting file offset */
5657 ASSERT(whichfork == XFS_DATA_FORK);
5658 ifp = XFS_IFORK_PTR(ip, whichfork);
5659 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5660 (error = xfs_iread_extents(NULL, ip, whichfork)))
5661 return error;
5662 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5663 if (nextents == 0) {
5664 *eof = 1;
5665 return 0;
5668 * Go to the last extent
5670 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
5671 startoff = xfs_bmbt_get_startoff(lastrec);
5672 blockcount = xfs_bmbt_get_blockcount(lastrec);
5673 *eof = endoff >= startoff + blockcount;
5674 return 0;
5677 #ifdef DEBUG
5678 STATIC struct xfs_buf *
5679 xfs_bmap_get_bp(
5680 struct xfs_btree_cur *cur,
5681 xfs_fsblock_t bno)
5683 struct xfs_log_item_desc *lidp;
5684 int i;
5686 if (!cur)
5687 return NULL;
5689 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5690 if (!cur->bc_bufs[i])
5691 break;
5692 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5693 return cur->bc_bufs[i];
5696 /* Chase down all the log items to see if the bp is there */
5697 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
5698 struct xfs_buf_log_item *bip;
5699 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5700 if (bip->bli_item.li_type == XFS_LI_BUF &&
5701 XFS_BUF_ADDR(bip->bli_buf) == bno)
5702 return bip->bli_buf;
5705 return NULL;
5708 STATIC void
5709 xfs_check_block(
5710 struct xfs_btree_block *block,
5711 xfs_mount_t *mp,
5712 int root,
5713 short sz)
5715 int i, j, dmxr;
5716 __be64 *pp, *thispa; /* pointer to block address */
5717 xfs_bmbt_key_t *prevp, *keyp;
5719 ASSERT(be16_to_cpu(block->bb_level) > 0);
5721 prevp = NULL;
5722 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
5723 dmxr = mp->m_bmap_dmxr[0];
5724 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
5726 if (prevp) {
5727 ASSERT(be64_to_cpu(prevp->br_startoff) <
5728 be64_to_cpu(keyp->br_startoff));
5730 prevp = keyp;
5733 * Compare the block numbers to see if there are dups.
5735 if (root)
5736 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
5737 else
5738 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5740 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
5741 if (root)
5742 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
5743 else
5744 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
5745 if (*thispa == *pp) {
5746 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
5747 __func__, j, i,
5748 (unsigned long long)be64_to_cpu(*thispa));
5749 panic("%s: ptrs are equal in node\n",
5750 __func__);
5757 * Check that the extents for the inode ip are in the right order in all
5758 * btree leaves.
5761 STATIC void
5762 xfs_bmap_check_leaf_extents(
5763 xfs_btree_cur_t *cur, /* btree cursor or null */
5764 xfs_inode_t *ip, /* incore inode pointer */
5765 int whichfork) /* data or attr fork */
5767 struct xfs_btree_block *block; /* current btree block */
5768 xfs_fsblock_t bno; /* block # of "block" */
5769 xfs_buf_t *bp; /* buffer for "block" */
5770 int error; /* error return value */
5771 xfs_extnum_t i=0, j; /* index into the extents list */
5772 xfs_ifork_t *ifp; /* fork structure */
5773 int level; /* btree level, for checking */
5774 xfs_mount_t *mp; /* file system mount structure */
5775 __be64 *pp; /* pointer to block address */
5776 xfs_bmbt_rec_t *ep; /* pointer to current extent */
5777 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
5778 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
5779 int bp_release = 0;
5781 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5782 return;
5785 bno = NULLFSBLOCK;
5786 mp = ip->i_mount;
5787 ifp = XFS_IFORK_PTR(ip, whichfork);
5788 block = ifp->if_broot;
5790 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5792 level = be16_to_cpu(block->bb_level);
5793 ASSERT(level > 0);
5794 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
5795 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
5796 bno = be64_to_cpu(*pp);
5798 ASSERT(bno != NULLDFSBNO);
5799 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5800 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5803 * Go down the tree until leaf level is reached, following the first
5804 * pointer (leftmost) at each level.
5806 while (level-- > 0) {
5807 /* See if buf is in cur first */
5808 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5809 if (bp) {
5810 bp_release = 0;
5811 } else {
5812 bp_release = 1;
5814 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5815 XFS_BMAP_BTREE_REF)))
5816 goto error_norelse;
5817 block = XFS_BUF_TO_BLOCK(bp);
5818 XFS_WANT_CORRUPTED_GOTO(
5819 xfs_bmap_sanity_check(mp, bp, level),
5820 error0);
5821 if (level == 0)
5822 break;
5825 * Check this block for basic sanity (increasing keys and
5826 * no duplicate blocks).
5829 xfs_check_block(block, mp, 0, 0);
5830 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
5831 bno = be64_to_cpu(*pp);
5832 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
5833 if (bp_release) {
5834 bp_release = 0;
5835 xfs_trans_brelse(NULL, bp);
5840 * Here with bp and block set to the leftmost leaf node in the tree.
5842 i = 0;
5845 * Loop over all leaf nodes checking that all extents are in the right order.
5847 for (;;) {
5848 xfs_fsblock_t nextbno;
5849 xfs_extnum_t num_recs;
5852 num_recs = xfs_btree_get_numrecs(block);
5855 * Read-ahead the next leaf block, if any.
5858 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
5861 * Check all the extents to make sure they are OK.
5862 * If we had a previous block, the last entry should
5863 * conform with the first entry in this one.
5866 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
5867 if (i) {
5868 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
5869 xfs_bmbt_disk_get_blockcount(&last) <=
5870 xfs_bmbt_disk_get_startoff(ep));
5872 for (j = 1; j < num_recs; j++) {
5873 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
5874 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
5875 xfs_bmbt_disk_get_blockcount(ep) <=
5876 xfs_bmbt_disk_get_startoff(nextp));
5877 ep = nextp;
5880 last = *ep;
5881 i += num_recs;
5882 if (bp_release) {
5883 bp_release = 0;
5884 xfs_trans_brelse(NULL, bp);
5886 bno = nextbno;
5888 * If we've reached the end, stop.
5890 if (bno == NULLFSBLOCK)
5891 break;
5893 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5894 if (bp) {
5895 bp_release = 0;
5896 } else {
5897 bp_release = 1;
5899 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5900 XFS_BMAP_BTREE_REF)))
5901 goto error_norelse;
5902 block = XFS_BUF_TO_BLOCK(bp);
5904 if (bp_release) {
5905 bp_release = 0;
5906 xfs_trans_brelse(NULL, bp);
5908 return;
5910 error0:
5911 cmn_err(CE_WARN, "%s: at error0", __func__);
5912 if (bp_release)
5913 xfs_trans_brelse(NULL, bp);
5914 error_norelse:
5915 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
5916 __func__, i);
5917 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
5918 return;
5920 #endif
5923 * Count fsblocks of the given fork.
5925 int /* error */
5926 xfs_bmap_count_blocks(
5927 xfs_trans_t *tp, /* transaction pointer */
5928 xfs_inode_t *ip, /* incore inode */
5929 int whichfork, /* data or attr fork */
5930 int *count) /* out: count of blocks */
5932 struct xfs_btree_block *block; /* current btree block */
5933 xfs_fsblock_t bno; /* block # of "block" */
5934 xfs_ifork_t *ifp; /* fork structure */
5935 int level; /* btree level, for checking */
5936 xfs_mount_t *mp; /* file system mount structure */
5937 __be64 *pp; /* pointer to block address */
5939 bno = NULLFSBLOCK;
5940 mp = ip->i_mount;
5941 ifp = XFS_IFORK_PTR(ip, whichfork);
5942 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
5943 xfs_bmap_count_leaves(ifp, 0,
5944 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
5945 count);
5946 return 0;
5950 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5952 block = ifp->if_broot;
5953 level = be16_to_cpu(block->bb_level);
5954 ASSERT(level > 0);
5955 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
5956 bno = be64_to_cpu(*pp);
5957 ASSERT(bno != NULLDFSBNO);
5958 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5959 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5961 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
5962 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
5963 mp);
5964 return XFS_ERROR(EFSCORRUPTED);
5967 return 0;
5971 * Recursively walks each level of a btree
5972 * to count total fsblocks is use.
5974 STATIC int /* error */
5975 xfs_bmap_count_tree(
5976 xfs_mount_t *mp, /* file system mount point */
5977 xfs_trans_t *tp, /* transaction pointer */
5978 xfs_ifork_t *ifp, /* inode fork pointer */
5979 xfs_fsblock_t blockno, /* file system block number */
5980 int levelin, /* level in btree */
5981 int *count) /* Count of blocks */
5983 int error;
5984 xfs_buf_t *bp, *nbp;
5985 int level = levelin;
5986 __be64 *pp;
5987 xfs_fsblock_t bno = blockno;
5988 xfs_fsblock_t nextbno;
5989 struct xfs_btree_block *block, *nextblock;
5990 int numrecs;
5992 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
5993 return error;
5994 *count += 1;
5995 block = XFS_BUF_TO_BLOCK(bp);
5997 if (--level) {
5998 /* Not at node above leaves, count this level of nodes */
5999 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6000 while (nextbno != NULLFSBLOCK) {
6001 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6002 0, &nbp, XFS_BMAP_BTREE_REF)))
6003 return error;
6004 *count += 1;
6005 nextblock = XFS_BUF_TO_BLOCK(nbp);
6006 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
6007 xfs_trans_brelse(tp, nbp);
6010 /* Dive to the next level */
6011 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
6012 bno = be64_to_cpu(*pp);
6013 if (unlikely((error =
6014 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6015 xfs_trans_brelse(tp, bp);
6016 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6017 XFS_ERRLEVEL_LOW, mp);
6018 return XFS_ERROR(EFSCORRUPTED);
6020 xfs_trans_brelse(tp, bp);
6021 } else {
6022 /* count all level 1 nodes and their leaves */
6023 for (;;) {
6024 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6025 numrecs = be16_to_cpu(block->bb_numrecs);
6026 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
6027 xfs_trans_brelse(tp, bp);
6028 if (nextbno == NULLFSBLOCK)
6029 break;
6030 bno = nextbno;
6031 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6032 XFS_BMAP_BTREE_REF)))
6033 return error;
6034 *count += 1;
6035 block = XFS_BUF_TO_BLOCK(bp);
6038 return 0;
6042 * Count leaf blocks given a range of extent records.
6044 STATIC void
6045 xfs_bmap_count_leaves(
6046 xfs_ifork_t *ifp,
6047 xfs_extnum_t idx,
6048 int numrecs,
6049 int *count)
6051 int b;
6053 for (b = 0; b < numrecs; b++) {
6054 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
6055 *count += xfs_bmbt_get_blockcount(frp);
6060 * Count leaf blocks given a range of extent records originally
6061 * in btree format.
6063 STATIC void
6064 xfs_bmap_disk_count_leaves(
6065 struct xfs_mount *mp,
6066 struct xfs_btree_block *block,
6067 int numrecs,
6068 int *count)
6070 int b;
6071 xfs_bmbt_rec_t *frp;
6073 for (b = 1; b <= numrecs; b++) {
6074 frp = XFS_BMBT_REC_ADDR(mp, block, b);
6075 *count += xfs_bmbt_disk_get_blockcount(frp);
6080 * dead simple method of punching delalyed allocation blocks from a range in
6081 * the inode. Walks a block at a time so will be slow, but is only executed in
6082 * rare error cases so the overhead is not critical. This will alays punch out
6083 * both the start and end blocks, even if the ranges only partially overlap
6084 * them, so it is up to the caller to ensure that partial blocks are not
6085 * passed in.
6088 xfs_bmap_punch_delalloc_range(
6089 struct xfs_inode *ip,
6090 xfs_fileoff_t start_fsb,
6091 xfs_fileoff_t length)
6093 xfs_fileoff_t remaining = length;
6094 int error = 0;
6096 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6098 do {
6099 int done;
6100 xfs_bmbt_irec_t imap;
6101 int nimaps = 1;
6102 xfs_fsblock_t firstblock;
6103 xfs_bmap_free_t flist;
6106 * Map the range first and check that it is a delalloc extent
6107 * before trying to unmap the range. Otherwise we will be
6108 * trying to remove a real extent (which requires a
6109 * transaction) or a hole, which is probably a bad idea...
6111 error = xfs_bmapi(NULL, ip, start_fsb, 1,
6112 XFS_BMAPI_ENTIRE, NULL, 0, &imap,
6113 &nimaps, NULL);
6115 if (error) {
6116 /* something screwed, just bail */
6117 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
6118 xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
6119 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6120 ip->i_ino, start_fsb);
6122 break;
6124 if (!nimaps) {
6125 /* nothing there */
6126 goto next_block;
6128 if (imap.br_startblock != DELAYSTARTBLOCK) {
6129 /* been converted, ignore */
6130 goto next_block;
6132 WARN_ON(imap.br_blockcount == 0);
6135 * Note: while we initialise the firstblock/flist pair, they
6136 * should never be used because blocks should never be
6137 * allocated or freed for a delalloc extent and hence we need
6138 * don't cancel or finish them after the xfs_bunmapi() call.
6140 xfs_bmap_init(&flist, &firstblock);
6141 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6142 &flist, &done);
6143 if (error)
6144 break;
6146 ASSERT(!flist.xbf_count && !flist.xbf_first);
6147 next_block:
6148 start_fsb++;
6149 remaining--;
6150 } while(remaining > 0);
6152 return error;