]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/xfs/xfs_bmap.c
Merge branch 'for-rmk' of git://git.pengutronix.de/git/imx/linux-2.6
[net-next-2.6.git] / fs / xfs / xfs_bmap.c
CommitLineData
1da177e4 1/*
3e57ecf6 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7b718769 3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
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
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
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.
1da177e4 13 *
7b718769
NS
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
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
a844f451 21#include "xfs_bit.h"
1da177e4 22#include "xfs_log.h"
a844f451 23#include "xfs_inum.h"
1da177e4
LT
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
1da177e4 27#include "xfs_dir2.h"
a844f451 28#include "xfs_da_btree.h"
1da177e4 29#include "xfs_bmap_btree.h"
a844f451 30#include "xfs_alloc_btree.h"
1da177e4 31#include "xfs_ialloc_btree.h"
1da177e4
LT
32#include "xfs_dir2_sf.h"
33#include "xfs_dinode.h"
1da177e4 34#include "xfs_inode.h"
a844f451 35#include "xfs_btree.h"
a844f451 36#include "xfs_mount.h"
1da177e4 37#include "xfs_itable.h"
f6c2d1fa
NS
38#include "xfs_dir2_data.h"
39#include "xfs_dir2_leaf.h"
40#include "xfs_dir2_block.h"
a844f451 41#include "xfs_inode_item.h"
1da177e4
LT
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"
d8cc890d 47#include "xfs_attr_leaf.h"
1da177e4
LT
48#include "xfs_rw.h"
49#include "xfs_quota.h"
50#include "xfs_trans_space.h"
51#include "xfs_buf_item.h"
2a82b8be 52#include "xfs_filestream.h"
739bfb2a 53#include "xfs_vnodeops.h"
0b1b213f 54#include "xfs_trace.h"
1da177e4
LT
55
56
57#ifdef DEBUG
58STATIC void
59xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
60#endif
61
62kmem_zone_t *xfs_bmap_free_item_zone;
63
64/*
65 * Prototypes for internal bmap routines.
66 */
67
68
69/*
70 * Called from xfs_bmap_add_attrfork to handle extents format files.
71 */
72STATIC int /* error */
73xfs_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 */
79
80/*
81 * Called from xfs_bmap_add_attrfork to handle local format files.
82 */
83STATIC int /* error */
84xfs_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 */
90
91/*
4eea22f0 92 * Called by xfs_bmapi to update file extent records and the btree
1da177e4
LT
93 * after allocating space (or doing a delayed allocation).
94 */
95STATIC int /* error */
96xfs_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 */
4eea22f0 100 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1da177e4
LT
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 */
106
107/*
108 * Called by xfs_bmap_add_extent to handle cases converting a delayed
109 * allocation to a real allocation.
110 */
111STATIC int /* error */
112xfs_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 */
4eea22f0 116 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1da177e4
LT
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 */
122
123/*
124 * Called by xfs_bmap_add_extent to handle cases converting a hole
125 * to a delayed allocation.
126 */
127STATIC int /* error */
128xfs_bmap_add_extent_hole_delay(
129 xfs_inode_t *ip, /* incore inode pointer */
130 xfs_extnum_t idx, /* extent number to update/insert */
4eea22f0 131 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1da177e4
LT
132 int *logflagsp,/* inode logging flags */
133 int rsvd); /* OK to allocate reserved blocks */
134
135/*
136 * Called by xfs_bmap_add_extent to handle cases converting a hole
137 * to a real allocation.
138 */
139STATIC int /* error */
140xfs_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 */
4eea22f0 144 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1da177e4
LT
145 int *logflagsp, /* inode logging flags */
146 int whichfork); /* data or attr fork */
147
148/*
149 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
150 * allocation to a real allocation or vice versa.
151 */
152STATIC int /* error */
153xfs_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 */
4eea22f0 157 xfs_bmbt_irec_t *new, /* new data to add to file extents */
b4e9181e 158 int *logflagsp); /* inode logging flags */
1da177e4
LT
159
160/*
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.
163 */
164STATIC int /* error */
165xfs_bmap_alloc(
166 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
167
168/*
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.
4eea22f0 171 * Since the file extents are already in-core, all we have to do is
1da177e4
LT
172 * give up the space for the btree root and pitch the leaf block.
173 */
174STATIC int /* error */
175xfs_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 */
181
1da177e4 182/*
4eea22f0 183 * Called by xfs_bmapi to update file extent records and the btree
1da177e4
LT
184 * after removing space (or undoing a delayed allocation).
185 */
186STATIC int /* error */
187xfs_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 */
4eea22f0 193 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1da177e4
LT
194 int *logflagsp,/* inode logging flags */
195 int whichfork, /* data or attr fork */
196 int rsvd); /* OK to allocate reserved blocks */
197
198/*
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.
201 */
202STATIC void
203xfs_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 */
207
1da177e4
LT
208/*
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.
211 */
212STATIC int /* error */
213xfs_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 */
222
1da177e4
LT
223/*
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.
227 */
228STATIC int /* error */
229xfs_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 */
236
237/*
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.
243 */
a6f64d4a 244STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1da177e4
LT
245xfs_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 */
253
ba0f32d4
CH
254/*
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.
259 */
260STATIC int /* error */
261xfs_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 */
266
1da177e4
LT
267/*
268 * Compute the worst-case number of indirect blocks that will be used
269 * for ip's delayed extent of length "len".
270 */
271STATIC xfs_filblks_t
272xfs_bmap_worst_indlen(
273 xfs_inode_t *ip, /* incore inode pointer */
274 xfs_filblks_t len); /* delayed extent length */
275
276#ifdef DEBUG
277/*
278 * Perform various validation checks on the values being returned
279 * from xfs_bmapi().
280 */
281STATIC void
282xfs_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 */
292
1da177e4
LT
293STATIC int
294xfs_bmap_count_tree(
295 xfs_mount_t *mp,
296 xfs_trans_t *tp,
4eea22f0 297 xfs_ifork_t *ifp,
1da177e4
LT
298 xfs_fsblock_t blockno,
299 int levelin,
300 int *count);
301
c94312de 302STATIC void
1da177e4 303xfs_bmap_count_leaves(
4eea22f0
MK
304 xfs_ifork_t *ifp,
305 xfs_extnum_t idx,
1da177e4
LT
306 int numrecs,
307 int *count);
308
c94312de 309STATIC void
91e11088 310xfs_bmap_disk_count_leaves(
136341b4 311 struct xfs_mount *mp,
7cc95a82 312 struct xfs_btree_block *block,
91e11088
YL
313 int numrecs,
314 int *count);
315
1da177e4
LT
316/*
317 * Bmap internal routines.
318 */
319
fe033cc8
CH
320STATIC int /* error */
321xfs_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 */
327{
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);
332}
333
334STATIC int /* error */
335xfs_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 */
341{
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);
346}
347
278d0ca1
CH
348/*
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.
352 */
353STATIC int
354xfs_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)
360{
361 union xfs_btree_rec rec;
362
363 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
364 return xfs_btree_update(cur, &rec);
365}
fe033cc8 366
1da177e4
LT
367/*
368 * Called from xfs_bmap_add_attrfork to handle btree format files.
369 */
370STATIC int /* error */
371xfs_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 */
377{
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 */
382
383 mp = ip->i_mount;
384 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
385 *flags |= XFS_ILOG_DBROOT;
386 else {
561f7d17 387 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
1da177e4
LT
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;
6bd8fc8a
LM
392 /* must be at least one entry */
393 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
ea77b0a6 394 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
1da177e4
LT
395 goto error0;
396 if (stat == 0) {
397 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
398 return XFS_ERROR(ENOSPC);
399 }
400 *firstblock = cur->bc_private.b.firstblock;
401 cur->bc_private.b.allocated = 0;
402 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
403 }
404 return 0;
405error0:
406 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
407 return error;
408}
409
410/*
411 * Called from xfs_bmap_add_attrfork to handle extents format files.
412 */
413STATIC int /* error */
414xfs_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 */
420{
421 xfs_btree_cur_t *cur; /* bmap btree cursor */
422 int error; /* error return value */
423
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);
433 }
434 return error;
435}
436
437/*
438 * Called from xfs_bmap_add_attrfork to handle local format files.
439 */
440STATIC int /* error */
441xfs_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 */
447{
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 */
451
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;
f6c2d1fa 463 error = xfs_dir2_sf_to_block(&dargs);
1da177e4
LT
464 } else
465 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
466 XFS_DATA_FORK);
467 return error;
468}
469
470/*
4eea22f0 471 * Called by xfs_bmapi to update file extent records and the btree
1da177e4
LT
472 * after allocating space (or doing a delayed allocation).
473 */
474STATIC int /* error */
475xfs_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 */
4eea22f0 479 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1da177e4
LT
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 */
485{
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 */
1da177e4
LT
490 xfs_ifork_t *ifp; /* inode fork ptr */
491 int logflags; /* returned value */
492 xfs_extnum_t nextents; /* number of extents in file now */
493
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;
501 /*
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.
505 */
506 if (nextents == 0) {
6ef35544
CH
507 xfs_iext_insert(ip, 0, 1, new,
508 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
509
1da177e4
LT
510 ASSERT(cur == NULL);
511 ifp->if_lastex = 0;
9d87c319 512 if (!isnullstartblock(new->br_startblock)) {
1da177e4 513 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
9d87c319 514 logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1da177e4
LT
515 } else
516 logflags = 0;
517 }
518 /*
519 * Any kind of new delayed allocation goes here.
520 */
9d87c319 521 else if (isnullstartblock(new->br_startblock)) {
1da177e4
LT
522 if (cur)
523 ASSERT((cur->bc_private.b.flags &
524 XFS_BTCUR_BPRV_WASDEL) == 0);
e9ed9d22 525 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
b4e9181e 526 &logflags, rsvd)))
1da177e4
LT
527 goto done;
528 }
529 /*
530 * Real allocation off the end of the file.
531 */
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,
b4e9181e 537 &logflags, whichfork)))
1da177e4
LT
538 goto done;
539 } else {
540 xfs_bmbt_irec_t prev; /* old extent at offset idx */
541
542 /*
543 * Get the record referred to by idx.
544 */
4eea22f0 545 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
1da177e4
LT
546 /*
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.
551 */
9d87c319 552 if (!isnullstartblock(new->br_startblock) &&
1da177e4
LT
553 new->br_startoff + new->br_blockcount > prev.br_startoff) {
554 if (prev.br_state != XFS_EXT_UNWRITTEN &&
9d87c319
ES
555 isnullstartblock(prev.br_startblock)) {
556 da_old = startblockval(prev.br_startblock);
1da177e4
LT
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,
b4e9181e 562 &logflags, rsvd)))
1da177e4
LT
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(
b4e9181e 567 ip, idx, &cur, new, &logflags)))
1da177e4
LT
568 goto done;
569 } else {
570 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
571 if ((error = xfs_bmap_add_extent_unwritten_real(
b4e9181e 572 ip, idx, &cur, new, &logflags)))
1da177e4
LT
573 goto done;
574 }
575 ASSERT(*curp == cur || *curp == NULL);
576 }
577 /*
578 * Otherwise we're filling in a hole with an allocation.
579 */
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,
b4e9181e 585 new, &logflags, whichfork)))
1da177e4
LT
586 goto done;
587 }
588 }
589
590 ASSERT(*curp == cur || *curp == NULL);
591 /*
592 * Convert to a btree if necessary.
593 */
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 */
597
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;
604 }
605 /*
606 * Adjust for changes in reserved delayed indirect blocks.
607 * Nothing to do for disk quotas here.
608 */
609 if (da_old || da_new) {
610 xfs_filblks_t nblks;
611
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_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
20f4ebf2 618 (int64_t)(da_old - nblks), rsvd);
1da177e4
LT
619 }
620 /*
621 * Clear out the allocated field, done with it now in any case.
622 */
623 if (cur) {
624 cur->bc_private.b.allocated = 0;
625 *curp = cur;
626 }
627done:
628#ifdef DEBUG
629 if (!error)
630 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
631#endif
632 *logflagsp = logflags;
633 return error;
634}
635
636/*
637 * Called by xfs_bmap_add_extent to handle cases converting a delayed
638 * allocation to a real allocation.
639 */
640STATIC int /* error */
641xfs_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 */
4eea22f0 645 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1da177e4
LT
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 */
651{
1da177e4
LT
652 xfs_btree_cur_t *cur; /* btree cursor */
653 int diff; /* temp value */
a6f64d4a 654 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1da177e4 655 int error; /* error return value */
1da177e4 656 int i; /* temp state */
4eea22f0 657 xfs_ifork_t *ifp; /* inode fork pointer */
1da177e4
LT
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 */
3e57ecf6
OW
663 xfs_filblks_t temp=0; /* value for dnew calculations */
664 xfs_filblks_t temp2=0;/* value for dnew calculations */
1da177e4 665 int tmp_rval; /* partial logging flags */
1da177e4
LT
666
667#define LEFT r[0]
668#define RIGHT r[1]
669#define PREV r[2]
1da177e4
LT
670
671 /*
672 * Set up a bunch of variables to make the tests simpler.
673 */
674 cur = *curp;
4eea22f0
MK
675 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
676 ep = xfs_iext_get_ext(ifp, idx);
1da177e4
LT
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);
7574aa92 681
1da177e4
LT
682 /*
683 * Set flags determining what part of the previous delayed allocation
684 * extent is being replaced by a real allocation.
685 */
7574aa92
CH
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;
690
1da177e4
LT
691 /*
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.
694 */
7574aa92
CH
695 if (idx > 0) {
696 state |= BMAP_LEFT_VALID;
4eea22f0 697 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
7574aa92
CH
698
699 if (isnullstartblock(LEFT.br_startblock))
700 state |= BMAP_LEFT_DELAY;
1da177e4 701 }
7574aa92
CH
702
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;
709
1da177e4
LT
710 /*
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.
714 */
7574aa92
CH
715 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
716 state |= BMAP_RIGHT_VALID;
4eea22f0 717 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
7574aa92
CH
718
719 if (isnullstartblock(RIGHT.br_startblock))
720 state |= BMAP_RIGHT_DELAY;
1da177e4 721 }
7574aa92
CH
722
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;
735
1da177e4
LT
736 error = 0;
737 /*
738 * Switch out based on the FILLING and CONTIG state bits.
739 */
7574aa92
CH
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:
1da177e4
LT
744 /*
745 * Filling in all of a previously delayed allocation extent.
746 * The left and right neighbors are both contiguous with new.
747 */
0b1b213f 748 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
4eea22f0 749 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1da177e4
LT
750 LEFT.br_blockcount + PREV.br_blockcount +
751 RIGHT.br_blockcount);
0b1b213f
CH
752 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
753
6ef35544 754 xfs_iext_remove(ip, idx, 2, state);
1da177e4
LT
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;
6bd8fc8a 765 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
91cca5df 766 if ((error = xfs_btree_delete(cur, &i)))
1da177e4 767 goto done;
6bd8fc8a 768 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
8df4da4a 769 if ((error = xfs_btree_decrement(cur, 0, &i)))
1da177e4 770 goto done;
6bd8fc8a 771 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
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;
778 }
779 *dnew = 0;
780 break;
781
7574aa92 782 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1da177e4
LT
783 /*
784 * Filling in all of a previously delayed allocation extent.
785 * The left neighbor is contiguous, the right is not.
786 */
0b1b213f 787 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
4eea22f0 788 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1da177e4 789 LEFT.br_blockcount + PREV.br_blockcount);
0b1b213f
CH
790 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
791
1da177e4 792 ip->i_df.if_lastex = idx - 1;
6ef35544 793 xfs_iext_remove(ip, idx, 1, state);
1da177e4
LT
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;
6bd8fc8a 802 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
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;
808 }
809 *dnew = 0;
810 break;
811
7574aa92 812 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1da177e4
LT
813 /*
814 * Filling in all of a previously delayed allocation extent.
815 * The right neighbor is contiguous, the left is not.
816 */
0b1b213f 817 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1da177e4
LT
818 xfs_bmbt_set_startblock(ep, new->br_startblock);
819 xfs_bmbt_set_blockcount(ep,
820 PREV.br_blockcount + RIGHT.br_blockcount);
0b1b213f
CH
821 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
822
1da177e4 823 ip->i_df.if_lastex = idx;
6ef35544 824 xfs_iext_remove(ip, idx + 1, 1, state);
1da177e4
LT
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;
6bd8fc8a 833 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
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;
839 }
840 *dnew = 0;
841 break;
842
7574aa92 843 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1da177e4
LT
844 /*
845 * Filling in all of a previously delayed allocation extent.
846 * Neither the left nor right neighbors are contiguous with
847 * the new one.
848 */
0b1b213f 849 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1da177e4 850 xfs_bmbt_set_startblock(ep, new->br_startblock);
0b1b213f
CH
851 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
852
1da177e4
LT
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;
6bd8fc8a 863 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1da177e4 864 cur->bc_rec.b.br_state = XFS_EXT_NORM;
4b22a571 865 if ((error = xfs_btree_insert(cur, &i)))
1da177e4 866 goto done;
6bd8fc8a 867 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
868 }
869 *dnew = 0;
870 break;
871
7574aa92 872 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1da177e4
LT
873 /*
874 * Filling in the first part of a previous delayed allocation.
875 * The left neighbor is contiguous.
876 */
0b1b213f 877 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
4eea22f0 878 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1da177e4
LT
879 LEFT.br_blockcount + new->br_blockcount);
880 xfs_bmbt_set_startoff(ep,
881 PREV.br_startoff + new->br_blockcount);
0b1b213f
CH
882 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
883
1da177e4 884 temp = PREV.br_blockcount - new->br_blockcount;
0b1b213f 885 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1da177e4
LT
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;
6bd8fc8a 896 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
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;
903 }
904 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
9d87c319
ES
905 startblockval(PREV.br_startblock));
906 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
0b1b213f 907 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1da177e4
LT
908 *dnew = temp;
909 break;
910
7574aa92 911 case BMAP_LEFT_FILLING:
1da177e4
LT
912 /*
913 * Filling in the first part of a previous delayed allocation.
914 * The left neighbor is not contiguous.
915 */
0b1b213f 916 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1da177e4
LT
917 xfs_bmbt_set_startoff(ep, new_endoff);
918 temp = PREV.br_blockcount - new->br_blockcount;
919 xfs_bmbt_set_blockcount(ep, temp);
6ef35544 920 xfs_iext_insert(ip, idx, 1, new, state);
1da177e4
LT
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;
6bd8fc8a 931 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1da177e4 932 cur->bc_rec.b.br_state = XFS_EXT_NORM;
4b22a571 933 if ((error = xfs_btree_insert(cur, &i)))
1da177e4 934 goto done;
6bd8fc8a 935 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
936 }
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;
945 }
946 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
9d87c319 947 startblockval(PREV.br_startblock) -
1da177e4 948 (cur ? cur->bc_private.b.allocated : 0));
4eea22f0 949 ep = xfs_iext_get_ext(ifp, idx + 1);
9d87c319 950 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
0b1b213f 951 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
1da177e4
LT
952 *dnew = temp;
953 break;
954
7574aa92 955 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1da177e4
LT
956 /*
957 * Filling in the last part of a previous delayed allocation.
958 * The right neighbor is contiguous with the new allocation.
959 */
960 temp = PREV.br_blockcount - new->br_blockcount;
0b1b213f
CH
961 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
962 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
1da177e4 963 xfs_bmbt_set_blockcount(ep, temp);
4eea22f0
MK
964 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
965 new->br_startoff, new->br_startblock,
1da177e4
LT
966 new->br_blockcount + RIGHT.br_blockcount,
967 RIGHT.br_state);
0b1b213f 968 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
1da177e4
LT
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;
6bd8fc8a 978 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
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;
985 }
986 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
9d87c319
ES
987 startblockval(PREV.br_startblock));
988 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
0b1b213f 989 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1da177e4
LT
990 *dnew = temp;
991 break;
992
7574aa92 993 case BMAP_RIGHT_FILLING:
1da177e4
LT
994 /*
995 * Filling in the last part of a previous delayed allocation.
996 * The right neighbor is not contiguous.
997 */
998 temp = PREV.br_blockcount - new->br_blockcount;
0b1b213f 999 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1da177e4 1000 xfs_bmbt_set_blockcount(ep, temp);
6ef35544 1001 xfs_iext_insert(ip, idx + 1, 1, new, state);
1da177e4
LT
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;
6bd8fc8a 1012 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1da177e4 1013 cur->bc_rec.b.br_state = XFS_EXT_NORM;
4b22a571 1014 if ((error = xfs_btree_insert(cur, &i)))
1da177e4 1015 goto done;
6bd8fc8a 1016 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1017 }
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;
1026 }
1027 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
9d87c319 1028 startblockval(PREV.br_startblock) -
1da177e4 1029 (cur ? cur->bc_private.b.allocated : 0));
4eea22f0 1030 ep = xfs_iext_get_ext(ifp, idx);
9d87c319 1031 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
0b1b213f 1032 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1da177e4
LT
1033 *dnew = temp;
1034 break;
1035
1036 case 0:
1037 /*
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.
1041 */
1042 temp = new->br_startoff - PREV.br_startoff;
0b1b213f 1043 trace_xfs_bmap_pre_update(ip, idx, 0, _THIS_IP_);
1da177e4
LT
1044 xfs_bmbt_set_blockcount(ep, temp);
1045 r[0] = *new;
d2133717
LM
1046 r[1].br_state = PREV.br_state;
1047 r[1].br_startblock = 0;
1da177e4
LT
1048 r[1].br_startoff = new_endoff;
1049 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1050 r[1].br_blockcount = temp2;
6ef35544 1051 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
1da177e4
LT
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;
6bd8fc8a 1062 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1da177e4 1063 cur->bc_rec.b.br_state = XFS_EXT_NORM;
4b22a571 1064 if ((error = xfs_btree_insert(cur, &i)))
1da177e4 1065 goto done;
6bd8fc8a 1066 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1067 }
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;
1076 }
1077 temp = xfs_bmap_worst_indlen(ip, temp);
1078 temp2 = xfs_bmap_worst_indlen(ip, temp2);
9d87c319 1079 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
1da177e4
LT
1080 (cur ? cur->bc_private.b.allocated : 0));
1081 if (diff > 0 &&
20f4ebf2 1082 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
1da177e4
LT
1083 /*
1084 * Ick gross gag me with a spoon.
1085 */
1086 ASSERT(0); /* want to see if this ever happens! */
1087 while (diff > 0) {
1088 if (temp) {
1089 temp--;
1090 diff--;
1091 if (!diff ||
1092 !xfs_mod_incore_sb(ip->i_mount,
20f4ebf2 1093 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1da177e4
LT
1094 break;
1095 }
1096 if (temp2) {
1097 temp2--;
1098 diff--;
1099 if (!diff ||
1100 !xfs_mod_incore_sb(ip->i_mount,
20f4ebf2 1101 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
1da177e4
LT
1102 break;
1103 }
1104 }
1105 }
4eea22f0 1106 ep = xfs_iext_get_ext(ifp, idx);
9d87c319 1107 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
0b1b213f
CH
1108 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1109 trace_xfs_bmap_pre_update(ip, idx + 2, state, _THIS_IP_);
4eea22f0 1110 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
9d87c319 1111 nullstartblock((int)temp2));
0b1b213f 1112 trace_xfs_bmap_post_update(ip, idx + 2, state, _THIS_IP_);
1da177e4
LT
1113 *dnew = temp + temp2;
1114 break;
1115
7574aa92
CH
1116 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1117 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1118 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1119 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1120 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1121 case BMAP_LEFT_CONTIG:
1122 case BMAP_RIGHT_CONTIG:
1da177e4
LT
1123 /*
1124 * These cases are all impossible.
1125 */
1126 ASSERT(0);
1127 }
1128 *curp = cur;
1129done:
1130 *logflagsp = rval;
1131 return error;
1132#undef LEFT
1133#undef RIGHT
1134#undef PREV
1da177e4
LT
1135}
1136
1137/*
1138 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1139 * allocation to a real allocation or vice versa.
1140 */
1141STATIC int /* error */
1142xfs_bmap_add_extent_unwritten_real(
1143 xfs_inode_t *ip, /* incore inode pointer */
1144 xfs_extnum_t idx, /* extent number to update/insert */
1145 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
4eea22f0 1146 xfs_bmbt_irec_t *new, /* new data to add to file extents */
b4e9181e 1147 int *logflagsp) /* inode logging flags */
1da177e4 1148{
1da177e4 1149 xfs_btree_cur_t *cur; /* btree cursor */
a6f64d4a 1150 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1da177e4 1151 int error; /* error return value */
1da177e4 1152 int i; /* temp state */
4eea22f0 1153 xfs_ifork_t *ifp; /* inode fork pointer */
1da177e4
LT
1154 xfs_fileoff_t new_endoff; /* end offset of new entry */
1155 xfs_exntst_t newext; /* new extent state */
1156 xfs_exntst_t oldext; /* old extent state */
1157 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1158 /* left is 0, right is 1, prev is 2 */
1159 int rval=0; /* return value (logging flags) */
1160 int state = 0;/* state bits, accessed thru macros */
1da177e4
LT
1161
1162#define LEFT r[0]
1163#define RIGHT r[1]
1164#define PREV r[2]
1da177e4
LT
1165 /*
1166 * Set up a bunch of variables to make the tests simpler.
1167 */
1168 error = 0;
1169 cur = *curp;
4eea22f0
MK
1170 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1171 ep = xfs_iext_get_ext(ifp, idx);
1da177e4
LT
1172 xfs_bmbt_get_all(ep, &PREV);
1173 newext = new->br_state;
1174 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1175 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1176 ASSERT(PREV.br_state == oldext);
1177 new_endoff = new->br_startoff + new->br_blockcount;
1178 ASSERT(PREV.br_startoff <= new->br_startoff);
1179 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
7574aa92 1180
1da177e4
LT
1181 /*
1182 * Set flags determining what part of the previous oldext allocation
1183 * extent is being replaced by a newext allocation.
1184 */
7574aa92
CH
1185 if (PREV.br_startoff == new->br_startoff)
1186 state |= BMAP_LEFT_FILLING;
1187 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1188 state |= BMAP_RIGHT_FILLING;
1189
1da177e4
LT
1190 /*
1191 * Check and set flags if this segment has a left neighbor.
1192 * Don't set contiguous if the combined extent would be too large.
1193 */
7574aa92
CH
1194 if (idx > 0) {
1195 state |= BMAP_LEFT_VALID;
4eea22f0 1196 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
7574aa92
CH
1197
1198 if (isnullstartblock(LEFT.br_startblock))
1199 state |= BMAP_LEFT_DELAY;
1da177e4 1200 }
7574aa92
CH
1201
1202 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1203 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1204 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1205 LEFT.br_state == newext &&
1206 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1207 state |= BMAP_LEFT_CONTIG;
1208
1da177e4
LT
1209 /*
1210 * Check and set flags if this segment has a right neighbor.
1211 * Don't set contiguous if the combined extent would be too large.
1212 * Also check for all-three-contiguous being too large.
1213 */
7574aa92
CH
1214 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1215 state |= BMAP_RIGHT_VALID;
4eea22f0 1216 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
7574aa92
CH
1217 if (isnullstartblock(RIGHT.br_startblock))
1218 state |= BMAP_RIGHT_DELAY;
1da177e4 1219 }
7574aa92
CH
1220
1221 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1222 new_endoff == RIGHT.br_startoff &&
1223 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1224 newext == RIGHT.br_state &&
1225 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1226 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1227 BMAP_RIGHT_FILLING)) !=
1228 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1229 BMAP_RIGHT_FILLING) ||
1230 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1231 <= MAXEXTLEN))
1232 state |= BMAP_RIGHT_CONTIG;
1233
1da177e4
LT
1234 /*
1235 * Switch out based on the FILLING and CONTIG state bits.
1236 */
7574aa92
CH
1237 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1238 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1239 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1240 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1da177e4
LT
1241 /*
1242 * Setting all of a previous oldext extent to newext.
1243 * The left and right neighbors are both contiguous with new.
1244 */
0b1b213f 1245 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
4eea22f0 1246 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1da177e4
LT
1247 LEFT.br_blockcount + PREV.br_blockcount +
1248 RIGHT.br_blockcount);
0b1b213f
CH
1249 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1250
6ef35544 1251 xfs_iext_remove(ip, idx, 2, state);
1da177e4
LT
1252 ip->i_df.if_lastex = idx - 1;
1253 ip->i_d.di_nextents -= 2;
1254 if (cur == NULL)
1255 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1256 else {
1257 rval = XFS_ILOG_CORE;
1258 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1259 RIGHT.br_startblock,
1260 RIGHT.br_blockcount, &i)))
1261 goto done;
6bd8fc8a 1262 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
91cca5df 1263 if ((error = xfs_btree_delete(cur, &i)))
1da177e4 1264 goto done;
6bd8fc8a 1265 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
8df4da4a 1266 if ((error = xfs_btree_decrement(cur, 0, &i)))
1da177e4 1267 goto done;
6bd8fc8a 1268 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
91cca5df 1269 if ((error = xfs_btree_delete(cur, &i)))
1da177e4 1270 goto done;
6bd8fc8a 1271 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
8df4da4a 1272 if ((error = xfs_btree_decrement(cur, 0, &i)))
1da177e4 1273 goto done;
6bd8fc8a 1274 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1275 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1276 LEFT.br_startblock,
1277 LEFT.br_blockcount + PREV.br_blockcount +
1278 RIGHT.br_blockcount, LEFT.br_state)))
1279 goto done;
1280 }
1281 break;
1282
7574aa92 1283 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1da177e4
LT
1284 /*
1285 * Setting all of a previous oldext extent to newext.
1286 * The left neighbor is contiguous, the right is not.
1287 */
0b1b213f 1288 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
4eea22f0 1289 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1da177e4 1290 LEFT.br_blockcount + PREV.br_blockcount);
0b1b213f
CH
1291 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1292
1da177e4 1293 ip->i_df.if_lastex = idx - 1;
6ef35544 1294 xfs_iext_remove(ip, idx, 1, state);
1da177e4
LT
1295 ip->i_d.di_nextents--;
1296 if (cur == NULL)
1297 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1298 else {
1299 rval = XFS_ILOG_CORE;
1300 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1301 PREV.br_startblock, PREV.br_blockcount,
1302 &i)))
1303 goto done;
6bd8fc8a 1304 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
91cca5df 1305 if ((error = xfs_btree_delete(cur, &i)))
1da177e4 1306 goto done;
6bd8fc8a 1307 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
8df4da4a 1308 if ((error = xfs_btree_decrement(cur, 0, &i)))
1da177e4 1309 goto done;
6bd8fc8a 1310 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1311 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1312 LEFT.br_startblock,
1313 LEFT.br_blockcount + PREV.br_blockcount,
1314 LEFT.br_state)))
1315 goto done;
1316 }
1317 break;
1318
7574aa92 1319 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1da177e4
LT
1320 /*
1321 * Setting all of a previous oldext extent to newext.
1322 * The right neighbor is contiguous, the left is not.
1323 */
0b1b213f 1324 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1da177e4
LT
1325 xfs_bmbt_set_blockcount(ep,
1326 PREV.br_blockcount + RIGHT.br_blockcount);
1327 xfs_bmbt_set_state(ep, newext);
0b1b213f 1328 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1da177e4 1329 ip->i_df.if_lastex = idx;
6ef35544 1330 xfs_iext_remove(ip, idx + 1, 1, state);
1da177e4
LT
1331 ip->i_d.di_nextents--;
1332 if (cur == NULL)
1333 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1334 else {
1335 rval = XFS_ILOG_CORE;
1336 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1337 RIGHT.br_startblock,
1338 RIGHT.br_blockcount, &i)))
1339 goto done;
6bd8fc8a 1340 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
91cca5df 1341 if ((error = xfs_btree_delete(cur, &i)))
1da177e4 1342 goto done;
6bd8fc8a 1343 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
8df4da4a 1344 if ((error = xfs_btree_decrement(cur, 0, &i)))
1da177e4 1345 goto done;
6bd8fc8a 1346 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1347 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1348 new->br_startblock,
1349 new->br_blockcount + RIGHT.br_blockcount,
1350 newext)))
1351 goto done;
1352 }
1353 break;
1354
7574aa92 1355 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1da177e4
LT
1356 /*
1357 * Setting all of a previous oldext extent to newext.
1358 * Neither the left nor right neighbors are contiguous with
1359 * the new one.
1360 */
0b1b213f 1361 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1da177e4 1362 xfs_bmbt_set_state(ep, newext);
0b1b213f
CH
1363 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1364
1da177e4
LT
1365 ip->i_df.if_lastex = idx;
1366 if (cur == NULL)
1367 rval = XFS_ILOG_DEXT;
1368 else {
1369 rval = 0;
1370 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1371 new->br_startblock, new->br_blockcount,
1372 &i)))
1373 goto done;
6bd8fc8a 1374 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1375 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1376 new->br_startblock, new->br_blockcount,
1377 newext)))
1378 goto done;
1379 }
1380 break;
1381
7574aa92 1382 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1da177e4
LT
1383 /*
1384 * Setting the first part of a previous oldext extent to newext.
1385 * The left neighbor is contiguous.
1386 */
0b1b213f 1387 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
4eea22f0 1388 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1da177e4
LT
1389 LEFT.br_blockcount + new->br_blockcount);
1390 xfs_bmbt_set_startoff(ep,
1391 PREV.br_startoff + new->br_blockcount);
0b1b213f
CH
1392 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1393
1394 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1da177e4
LT
1395 xfs_bmbt_set_startblock(ep,
1396 new->br_startblock + new->br_blockcount);
1397 xfs_bmbt_set_blockcount(ep,
1398 PREV.br_blockcount - new->br_blockcount);
0b1b213f
CH
1399 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1400
1da177e4
LT
1401 ip->i_df.if_lastex = idx - 1;
1402 if (cur == NULL)
1403 rval = XFS_ILOG_DEXT;
1404 else {
1405 rval = 0;
1406 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1407 PREV.br_startblock, PREV.br_blockcount,
1408 &i)))
1409 goto done;
6bd8fc8a 1410 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1411 if ((error = xfs_bmbt_update(cur,
1412 PREV.br_startoff + new->br_blockcount,
1413 PREV.br_startblock + new->br_blockcount,
1414 PREV.br_blockcount - new->br_blockcount,
1415 oldext)))
1416 goto done;
8df4da4a 1417 if ((error = xfs_btree_decrement(cur, 0, &i)))
1da177e4
LT
1418 goto done;
1419 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1420 LEFT.br_startblock,
1421 LEFT.br_blockcount + new->br_blockcount,
1422 LEFT.br_state))
1423 goto done;
1424 }
1425 break;
1426
7574aa92 1427 case BMAP_LEFT_FILLING:
1da177e4
LT
1428 /*
1429 * Setting the first part of a previous oldext extent to newext.
1430 * The left neighbor is not contiguous.
1431 */
0b1b213f 1432 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1da177e4
LT
1433 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1434 xfs_bmbt_set_startoff(ep, new_endoff);
1435 xfs_bmbt_set_blockcount(ep,
1436 PREV.br_blockcount - new->br_blockcount);
1437 xfs_bmbt_set_startblock(ep,
1438 new->br_startblock + new->br_blockcount);
0b1b213f
CH
1439 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1440
6ef35544 1441 xfs_iext_insert(ip, idx, 1, new, state);
1da177e4
LT
1442 ip->i_df.if_lastex = idx;
1443 ip->i_d.di_nextents++;
1444 if (cur == NULL)
1445 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1446 else {
1447 rval = XFS_ILOG_CORE;
1448 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1449 PREV.br_startblock, PREV.br_blockcount,
1450 &i)))
1451 goto done;
6bd8fc8a 1452 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1453 if ((error = xfs_bmbt_update(cur,
1454 PREV.br_startoff + new->br_blockcount,
1455 PREV.br_startblock + new->br_blockcount,
1456 PREV.br_blockcount - new->br_blockcount,
1457 oldext)))
1458 goto done;
1459 cur->bc_rec.b = *new;
4b22a571 1460 if ((error = xfs_btree_insert(cur, &i)))
1da177e4 1461 goto done;
6bd8fc8a 1462 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1463 }
1464 break;
1465
7574aa92 1466 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1da177e4
LT
1467 /*
1468 * Setting the last part of a previous oldext extent to newext.
1469 * The right neighbor is contiguous with the new allocation.
1470 */
0b1b213f
CH
1471 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1472 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
1da177e4
LT
1473 xfs_bmbt_set_blockcount(ep,
1474 PREV.br_blockcount - new->br_blockcount);
0b1b213f 1475 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
4eea22f0
MK
1476 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1477 new->br_startoff, new->br_startblock,
1da177e4 1478 new->br_blockcount + RIGHT.br_blockcount, newext);
0b1b213f
CH
1479 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
1480
1da177e4
LT
1481 ip->i_df.if_lastex = idx + 1;
1482 if (cur == NULL)
1483 rval = XFS_ILOG_DEXT;
1484 else {
1485 rval = 0;
1486 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1487 PREV.br_startblock,
1488 PREV.br_blockcount, &i)))
1489 goto done;
6bd8fc8a 1490 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1491 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1492 PREV.br_startblock,
1493 PREV.br_blockcount - new->br_blockcount,
1494 oldext)))
1495 goto done;
637aa50f 1496 if ((error = xfs_btree_increment(cur, 0, &i)))
1da177e4
LT
1497 goto done;
1498 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1499 new->br_startblock,
1500 new->br_blockcount + RIGHT.br_blockcount,
1501 newext)))
1502 goto done;
1503 }
1504 break;
1505
7574aa92 1506 case BMAP_RIGHT_FILLING:
1da177e4
LT
1507 /*
1508 * Setting the last part of a previous oldext extent to newext.
1509 * The right neighbor is not contiguous.
1510 */
0b1b213f 1511 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1da177e4
LT
1512 xfs_bmbt_set_blockcount(ep,
1513 PREV.br_blockcount - new->br_blockcount);
0b1b213f
CH
1514 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1515
6ef35544 1516 xfs_iext_insert(ip, idx + 1, 1, new, state);
1da177e4
LT
1517 ip->i_df.if_lastex = idx + 1;
1518 ip->i_d.di_nextents++;
1519 if (cur == NULL)
1520 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1521 else {
1522 rval = XFS_ILOG_CORE;
1523 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1524 PREV.br_startblock, PREV.br_blockcount,
1525 &i)))
1526 goto done;
6bd8fc8a 1527 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1528 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1529 PREV.br_startblock,
1530 PREV.br_blockcount - new->br_blockcount,
1531 oldext)))
1532 goto done;
1533 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1534 new->br_startblock, new->br_blockcount,
1535 &i)))
1536 goto done;
6bd8fc8a 1537 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1da177e4 1538 cur->bc_rec.b.br_state = XFS_EXT_NORM;
4b22a571 1539 if ((error = xfs_btree_insert(cur, &i)))
1da177e4 1540 goto done;
6bd8fc8a 1541 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1542 }
1543 break;
1544
1545 case 0:
1546 /*
1547 * Setting the middle part of a previous oldext extent to
1548 * newext. Contiguity is impossible here.
1549 * One extent becomes three extents.
1550 */
0b1b213f 1551 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1da177e4
LT
1552 xfs_bmbt_set_blockcount(ep,
1553 new->br_startoff - PREV.br_startoff);
0b1b213f
CH
1554 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1555
1da177e4
LT
1556 r[0] = *new;
1557 r[1].br_startoff = new_endoff;
1558 r[1].br_blockcount =
1559 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1560 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1561 r[1].br_state = oldext;
6ef35544 1562 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
1da177e4
LT
1563 ip->i_df.if_lastex = idx + 1;
1564 ip->i_d.di_nextents += 2;
1565 if (cur == NULL)
1566 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1567 else {
1568 rval = XFS_ILOG_CORE;
1569 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1570 PREV.br_startblock, PREV.br_blockcount,
1571 &i)))
1572 goto done;
6bd8fc8a 1573 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1574 /* new right extent - oldext */
1575 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1576 r[1].br_startblock, r[1].br_blockcount,
1577 r[1].br_state)))
1578 goto done;
1579 /* new left extent - oldext */
1da177e4 1580 cur->bc_rec.b = PREV;
6a617dd2
TS
1581 cur->bc_rec.b.br_blockcount =
1582 new->br_startoff - PREV.br_startoff;
4b22a571 1583 if ((error = xfs_btree_insert(cur, &i)))
1da177e4 1584 goto done;
6bd8fc8a 1585 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
ddea2d52
LM
1586 /*
1587 * Reset the cursor to the position of the new extent
1588 * we are about to insert as we can't trust it after
1589 * the previous insert.
1590 */
1591 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1592 new->br_startblock, new->br_blockcount,
1593 &i)))
1da177e4 1594 goto done;
ddea2d52 1595 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1da177e4 1596 /* new middle extent - newext */
ddea2d52 1597 cur->bc_rec.b.br_state = new->br_state;
4b22a571 1598 if ((error = xfs_btree_insert(cur, &i)))
1da177e4 1599 goto done;
6bd8fc8a 1600 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
1601 }
1602 break;
1603
7574aa92
CH
1604 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1605 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1606 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1607 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1608 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1609 case BMAP_LEFT_CONTIG:
1610 case BMAP_RIGHT_CONTIG:
1da177e4
LT
1611 /*
1612 * These cases are all impossible.
1613 */
1614 ASSERT(0);
1615 }
1616 *curp = cur;
1617done:
1618 *logflagsp = rval;
1619 return error;
1620#undef LEFT
1621#undef RIGHT
1622#undef PREV
1da177e4
LT
1623}
1624
1625/*
1626 * Called by xfs_bmap_add_extent to handle cases converting a hole
1627 * to a delayed allocation.
1628 */
1629/*ARGSUSED*/
1630STATIC int /* error */
1631xfs_bmap_add_extent_hole_delay(
1632 xfs_inode_t *ip, /* incore inode pointer */
1633 xfs_extnum_t idx, /* extent number to update/insert */
4eea22f0 1634 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1da177e4
LT
1635 int *logflagsp, /* inode logging flags */
1636 int rsvd) /* OK to allocate reserved blocks */
1637{
a6f64d4a 1638 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
4eea22f0 1639 xfs_ifork_t *ifp; /* inode fork pointer */
1da177e4
LT
1640 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1641 xfs_filblks_t newlen=0; /* new indirect size */
1642 xfs_filblks_t oldlen=0; /* old indirect size */
1643 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1644 int state; /* state bits, accessed thru macros */
3e57ecf6 1645 xfs_filblks_t temp=0; /* temp for indirect calculations */
1da177e4 1646
4eea22f0
MK
1647 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1648 ep = xfs_iext_get_ext(ifp, idx);
1da177e4 1649 state = 0;
9d87c319 1650 ASSERT(isnullstartblock(new->br_startblock));
7574aa92 1651
1da177e4
LT
1652 /*
1653 * Check and set flags if this segment has a left neighbor
1654 */
7574aa92
CH
1655 if (idx > 0) {
1656 state |= BMAP_LEFT_VALID;
4eea22f0 1657 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
7574aa92
CH
1658
1659 if (isnullstartblock(left.br_startblock))
1660 state |= BMAP_LEFT_DELAY;
1da177e4 1661 }
7574aa92 1662
1da177e4
LT
1663 /*
1664 * Check and set flags if the current (right) segment exists.
1665 * If it doesn't exist, we're converting the hole at end-of-file.
1666 */
7574aa92
CH
1667 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1668 state |= BMAP_RIGHT_VALID;
1da177e4 1669 xfs_bmbt_get_all(ep, &right);
7574aa92
CH
1670
1671 if (isnullstartblock(right.br_startblock))
1672 state |= BMAP_RIGHT_DELAY;
1da177e4 1673 }
7574aa92 1674
1da177e4
LT
1675 /*
1676 * Set contiguity flags on the left and right neighbors.
1677 * Don't let extents get too large, even if the pieces are contiguous.
1678 */
7574aa92
CH
1679 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1680 left.br_startoff + left.br_blockcount == new->br_startoff &&
1681 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1682 state |= BMAP_LEFT_CONTIG;
1683
1684 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1685 new->br_startoff + new->br_blockcount == right.br_startoff &&
1686 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1687 (!(state & BMAP_LEFT_CONTIG) ||
1688 (left.br_blockcount + new->br_blockcount +
1689 right.br_blockcount <= MAXEXTLEN)))
1690 state |= BMAP_RIGHT_CONTIG;
1691
1da177e4
LT
1692 /*
1693 * Switch out based on the contiguity flags.
1694 */
7574aa92
CH
1695 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1696 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1da177e4
LT
1697 /*
1698 * New allocation is contiguous with delayed allocations
1699 * on the left and on the right.
4eea22f0 1700 * Merge all three into a single extent record.
1da177e4
LT
1701 */
1702 temp = left.br_blockcount + new->br_blockcount +
1703 right.br_blockcount;
0b1b213f
CH
1704
1705 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
4eea22f0 1706 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
9d87c319
ES
1707 oldlen = startblockval(left.br_startblock) +
1708 startblockval(new->br_startblock) +
1709 startblockval(right.br_startblock);
1da177e4 1710 newlen = xfs_bmap_worst_indlen(ip, temp);
4eea22f0 1711 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
9d87c319 1712 nullstartblock((int)newlen));
0b1b213f
CH
1713 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1714
6ef35544 1715 xfs_iext_remove(ip, idx, 1, state);
1da177e4
LT
1716 ip->i_df.if_lastex = idx - 1;
1717 break;
1718
7574aa92 1719 case BMAP_LEFT_CONTIG:
1da177e4
LT
1720 /*
1721 * New allocation is contiguous with a delayed allocation
1722 * on the left.
1723 * Merge the new allocation with the left neighbor.
1724 */
1725 temp = left.br_blockcount + new->br_blockcount;
0b1b213f 1726 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
4eea22f0 1727 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
9d87c319
ES
1728 oldlen = startblockval(left.br_startblock) +
1729 startblockval(new->br_startblock);
1da177e4 1730 newlen = xfs_bmap_worst_indlen(ip, temp);
4eea22f0 1731 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
9d87c319 1732 nullstartblock((int)newlen));
0b1b213f
CH
1733 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1734
1da177e4
LT
1735 ip->i_df.if_lastex = idx - 1;
1736 break;
1737
7574aa92 1738 case BMAP_RIGHT_CONTIG:
1da177e4
LT
1739 /*
1740 * New allocation is contiguous with a delayed allocation
1741 * on the right.
1742 * Merge the new allocation with the right neighbor.
1743 */
0b1b213f 1744 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1da177e4 1745 temp = new->br_blockcount + right.br_blockcount;
9d87c319
ES
1746 oldlen = startblockval(new->br_startblock) +
1747 startblockval(right.br_startblock);
1da177e4
LT
1748 newlen = xfs_bmap_worst_indlen(ip, temp);
1749 xfs_bmbt_set_allf(ep, new->br_startoff,
9d87c319 1750 nullstartblock((int)newlen), temp, right.br_state);
0b1b213f
CH
1751 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1752
1da177e4
LT
1753 ip->i_df.if_lastex = idx;
1754 break;
1755
1756 case 0:
1757 /*
1758 * New allocation is not contiguous with another
1759 * delayed allocation.
1760 * Insert a new entry.
1761 */
1762 oldlen = newlen = 0;
6ef35544 1763 xfs_iext_insert(ip, idx, 1, new, state);
1da177e4
LT
1764 ip->i_df.if_lastex = idx;
1765 break;
1766 }
1767 if (oldlen != newlen) {
1768 ASSERT(oldlen > newlen);
1769 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
20f4ebf2 1770 (int64_t)(oldlen - newlen), rsvd);
1da177e4
LT
1771 /*
1772 * Nothing to do for disk quota accounting here.
1773 */
1774 }
1775 *logflagsp = 0;
1776 return 0;
1da177e4
LT
1777}
1778
1779/*
1780 * Called by xfs_bmap_add_extent to handle cases converting a hole
1781 * to a real allocation.
1782 */
1783STATIC int /* error */
1784xfs_bmap_add_extent_hole_real(
1785 xfs_inode_t *ip, /* incore inode pointer */
1786 xfs_extnum_t idx, /* extent number to update/insert */
1787 xfs_btree_cur_t *cur, /* if null, not a btree */
4eea22f0 1788 xfs_bmbt_irec_t *new, /* new data to add to file extents */
1da177e4
LT
1789 int *logflagsp, /* inode logging flags */
1790 int whichfork) /* data or attr fork */
1791{
a6f64d4a 1792 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
1da177e4 1793 int error; /* error return value */
1da177e4
LT
1794 int i; /* temp state */
1795 xfs_ifork_t *ifp; /* inode fork pointer */
1796 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1797 xfs_bmbt_irec_t right; /* right neighbor extent entry */
3e57ecf6 1798 int rval=0; /* return value (logging flags) */
1da177e4 1799 int state; /* state bits, accessed thru macros */
1da177e4
LT
1800
1801 ifp = XFS_IFORK_PTR(ip, whichfork);
1802 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
4eea22f0 1803 ep = xfs_iext_get_ext(ifp, idx);
1da177e4 1804 state = 0;
7574aa92 1805
6ef35544
CH
1806 if (whichfork == XFS_ATTR_FORK)
1807 state |= BMAP_ATTRFORK;
1808
1da177e4
LT
1809 /*
1810 * Check and set flags if this segment has a left neighbor.
1811 */
7574aa92
CH
1812 if (idx > 0) {
1813 state |= BMAP_LEFT_VALID;
4eea22f0 1814 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
7574aa92
CH
1815 if (isnullstartblock(left.br_startblock))
1816 state |= BMAP_LEFT_DELAY;
1da177e4 1817 }
7574aa92 1818
1da177e4
LT
1819 /*
1820 * Check and set flags if this segment has a current value.
1821 * Not true if we're inserting into the "hole" at eof.
1822 */
7574aa92
CH
1823 if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1824 state |= BMAP_RIGHT_VALID;
1da177e4 1825 xfs_bmbt_get_all(ep, &right);
7574aa92
CH
1826 if (isnullstartblock(right.br_startblock))
1827 state |= BMAP_RIGHT_DELAY;
1da177e4 1828 }
7574aa92 1829
1da177e4
LT
1830 /*
1831 * We're inserting a real allocation between "left" and "right".
1832 * Set the contiguity flags. Don't let extents get too large.
1833 */
7574aa92
CH
1834 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1835 left.br_startoff + left.br_blockcount == new->br_startoff &&
1836 left.br_startblock + left.br_blockcount == new->br_startblock &&
1837 left.br_state == new->br_state &&
1838 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1839 state |= BMAP_LEFT_CONTIG;
1840
1841 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1842 new->br_startoff + new->br_blockcount == right.br_startoff &&
1843 new->br_startblock + new->br_blockcount == right.br_startblock &&
1844 new->br_state == right.br_state &&
1845 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1846 (!(state & BMAP_LEFT_CONTIG) ||
1847 left.br_blockcount + new->br_blockcount +
1848 right.br_blockcount <= MAXEXTLEN))
1849 state |= BMAP_RIGHT_CONTIG;
1da177e4 1850
3e57ecf6 1851 error = 0;
1da177e4
LT
1852 /*
1853 * Select which case we're in here, and implement it.
1854 */
7574aa92
CH
1855 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1856 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1da177e4
LT
1857 /*
1858 * New allocation is contiguous with real allocations on the
1859 * left and on the right.
4eea22f0 1860 * Merge all three into a single extent record.
1da177e4 1861 */
0b1b213f 1862 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
4eea22f0 1863 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1da177e4
LT
1864 left.br_blockcount + new->br_blockcount +
1865 right.br_blockcount);
0b1b213f
CH
1866 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1867
6ef35544 1868 xfs_iext_remove(ip, idx, 1, state);
1da177e4
LT
1869 ifp->if_lastex = idx - 1;
1870 XFS_IFORK_NEXT_SET(ip, whichfork,
1871 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1872 if (cur == NULL) {
9d87c319 1873 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
3e57ecf6
OW
1874 } else {
1875 rval = XFS_ILOG_CORE;
1876 if ((error = xfs_bmbt_lookup_eq(cur,
1877 right.br_startoff,
1878 right.br_startblock,
1879 right.br_blockcount, &i)))
1880 goto done;
6bd8fc8a 1881 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
91cca5df 1882 if ((error = xfs_btree_delete(cur, &i)))
3e57ecf6 1883 goto done;
6bd8fc8a 1884 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
8df4da4a 1885 if ((error = xfs_btree_decrement(cur, 0, &i)))
3e57ecf6 1886 goto done;
6bd8fc8a 1887 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3e57ecf6
OW
1888 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1889 left.br_startblock,
1890 left.br_blockcount +
1891 new->br_blockcount +
1892 right.br_blockcount,
1893 left.br_state)))
1894 goto done;
1da177e4 1895 }
3e57ecf6 1896 break;
1da177e4 1897
7574aa92 1898 case BMAP_LEFT_CONTIG:
1da177e4
LT
1899 /*
1900 * New allocation is contiguous with a real allocation
1901 * on the left.
1902 * Merge the new allocation with the left neighbor.
1903 */
0b1b213f 1904 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
4eea22f0 1905 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
1da177e4 1906 left.br_blockcount + new->br_blockcount);
0b1b213f
CH
1907 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1908
1da177e4
LT
1909 ifp->if_lastex = idx - 1;
1910 if (cur == NULL) {
9d87c319 1911 rval = xfs_ilog_fext(whichfork);
3e57ecf6
OW
1912 } else {
1913 rval = 0;
1914 if ((error = xfs_bmbt_lookup_eq(cur,
1915 left.br_startoff,
1916 left.br_startblock,
1917 left.br_blockcount, &i)))
1918 goto done;
6bd8fc8a 1919 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3e57ecf6
OW
1920 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1921 left.br_startblock,
1922 left.br_blockcount +
1923 new->br_blockcount,
1924 left.br_state)))
1925 goto done;
1da177e4 1926 }
3e57ecf6 1927 break;
1da177e4 1928
7574aa92 1929 case BMAP_RIGHT_CONTIG:
1da177e4
LT
1930 /*
1931 * New allocation is contiguous with a real allocation
1932 * on the right.
1933 * Merge the new allocation with the right neighbor.
1934 */
0b1b213f 1935 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1da177e4
LT
1936 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
1937 new->br_blockcount + right.br_blockcount,
1938 right.br_state);
0b1b213f
CH
1939 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1940
1da177e4
LT
1941 ifp->if_lastex = idx;
1942 if (cur == NULL) {
9d87c319 1943 rval = xfs_ilog_fext(whichfork);
3e57ecf6
OW
1944 } else {
1945 rval = 0;
1946 if ((error = xfs_bmbt_lookup_eq(cur,
1947 right.br_startoff,
1948 right.br_startblock,
1949 right.br_blockcount, &i)))
1950 goto done;
6bd8fc8a 1951 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
3e57ecf6
OW
1952 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1953 new->br_startblock,
1954 new->br_blockcount +
1955 right.br_blockcount,
1956 right.br_state)))
1957 goto done;
1da177e4 1958 }
3e57ecf6 1959 break;
1da177e4
LT
1960
1961 case 0:
1962 /*
1963 * New allocation is not contiguous with another
1964 * real allocation.
1965 * Insert a new entry.
1966 */
6ef35544 1967 xfs_iext_insert(ip, idx, 1, new, state);
1da177e4
LT
1968 ifp->if_lastex = idx;
1969 XFS_IFORK_NEXT_SET(ip, whichfork,
1970 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
1971 if (cur == NULL) {
9d87c319 1972 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
3e57ecf6
OW
1973 } else {
1974 rval = XFS_ILOG_CORE;
1975 if ((error = xfs_bmbt_lookup_eq(cur,
1976 new->br_startoff,
1977 new->br_startblock,
1978 new->br_blockcount, &i)))
1979 goto done;
6bd8fc8a 1980 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
3e57ecf6 1981 cur->bc_rec.b.br_state = new->br_state;
4b22a571 1982 if ((error = xfs_btree_insert(cur, &i)))
3e57ecf6 1983 goto done;
6bd8fc8a 1984 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4 1985 }
3e57ecf6 1986 break;
1da177e4 1987 }
3e57ecf6
OW
1988done:
1989 *logflagsp = rval;
1990 return error;
1da177e4
LT
1991}
1992
dd9f438e
NS
1993/*
1994 * Adjust the size of the new extent based on di_extsize and rt extsize.
1995 */
1996STATIC int
1997xfs_bmap_extsize_align(
1998 xfs_mount_t *mp,
1999 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2000 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2001 xfs_extlen_t extsz, /* align to this extent size */
2002 int rt, /* is this a realtime inode? */
2003 int eof, /* is extent at end-of-file? */
2004 int delay, /* creating delalloc extent? */
2005 int convert, /* overwriting unwritten extent? */
2006 xfs_fileoff_t *offp, /* in/out: aligned offset */
2007 xfs_extlen_t *lenp) /* in/out: aligned length */
2008{
2009 xfs_fileoff_t orig_off; /* original offset */
2010 xfs_extlen_t orig_alen; /* original length */
2011 xfs_fileoff_t orig_end; /* original off+len */
2012 xfs_fileoff_t nexto; /* next file offset */
2013 xfs_fileoff_t prevo; /* previous file offset */
2014 xfs_fileoff_t align_off; /* temp for offset */
2015 xfs_extlen_t align_alen; /* temp for length */
2016 xfs_extlen_t temp; /* temp for calculations */
2017
2018 if (convert)
2019 return 0;
2020
2021 orig_off = align_off = *offp;
2022 orig_alen = align_alen = *lenp;
2023 orig_end = orig_off + orig_alen;
2024
2025 /*
2026 * If this request overlaps an existing extent, then don't
2027 * attempt to perform any additional alignment.
2028 */
2029 if (!delay && !eof &&
2030 (orig_off >= gotp->br_startoff) &&
2031 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2032 return 0;
2033 }
2034
2035 /*
2036 * If the file offset is unaligned vs. the extent size
2037 * we need to align it. This will be possible unless
2038 * the file was previously written with a kernel that didn't
2039 * perform this alignment, or if a truncate shot us in the
2040 * foot.
2041 */
2042 temp = do_mod(orig_off, extsz);
2043 if (temp) {
2044 align_alen += temp;
2045 align_off -= temp;
2046 }
2047 /*
2048 * Same adjustment for the end of the requested area.
2049 */
2050 if ((temp = (align_alen % extsz))) {
2051 align_alen += extsz - temp;
2052 }
2053 /*
2054 * If the previous block overlaps with this proposed allocation
2055 * then move the start forward without adjusting the length.
2056 */
2057 if (prevp->br_startoff != NULLFILEOFF) {
2058 if (prevp->br_startblock == HOLESTARTBLOCK)
2059 prevo = prevp->br_startoff;
2060 else
2061 prevo = prevp->br_startoff + prevp->br_blockcount;
2062 } else
2063 prevo = 0;
2064 if (align_off != orig_off && align_off < prevo)
2065 align_off = prevo;
2066 /*
2067 * If the next block overlaps with this proposed allocation
2068 * then move the start back without adjusting the length,
2069 * but not before offset 0.
2070 * This may of course make the start overlap previous block,
2071 * and if we hit the offset 0 limit then the next block
2072 * can still overlap too.
2073 */
2074 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2075 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2076 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2077 nexto = gotp->br_startoff + gotp->br_blockcount;
2078 else
2079 nexto = gotp->br_startoff;
2080 } else
2081 nexto = NULLFILEOFF;
2082 if (!eof &&
2083 align_off + align_alen != orig_end &&
2084 align_off + align_alen > nexto)
2085 align_off = nexto > align_alen ? nexto - align_alen : 0;
2086 /*
2087 * If we're now overlapping the next or previous extent that
2088 * means we can't fit an extsz piece in this hole. Just move
2089 * the start forward to the first valid spot and set
2090 * the length so we hit the end.
2091 */
2092 if (align_off != orig_off && align_off < prevo)
2093 align_off = prevo;
2094 if (align_off + align_alen != orig_end &&
2095 align_off + align_alen > nexto &&
2096 nexto != NULLFILEOFF) {
2097 ASSERT(nexto > prevo);
2098 align_alen = nexto - align_off;
2099 }
2100
2101 /*
2102 * If realtime, and the result isn't a multiple of the realtime
2103 * extent size we need to remove blocks until it is.
2104 */
2105 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2106 /*
2107 * We're not covering the original request, or
2108 * we won't be able to once we fix the length.
2109 */
2110 if (orig_off < align_off ||
2111 orig_end > align_off + align_alen ||
2112 align_alen - temp < orig_alen)
2113 return XFS_ERROR(EINVAL);
2114 /*
2115 * Try to fix it by moving the start up.
2116 */
2117 if (align_off + temp <= orig_off) {
2118 align_alen -= temp;
2119 align_off += temp;
2120 }
2121 /*
2122 * Try to fix it by moving the end in.
2123 */
2124 else if (align_off + align_alen - temp >= orig_end)
2125 align_alen -= temp;
2126 /*
2127 * Set the start to the minimum then trim the length.
2128 */
2129 else {
2130 align_alen -= orig_off - align_off;
2131 align_off = orig_off;
2132 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2133 }
2134 /*
2135 * Result doesn't cover the request, fail it.
2136 */
2137 if (orig_off < align_off || orig_end > align_off + align_alen)
2138 return XFS_ERROR(EINVAL);
2139 } else {
2140 ASSERT(orig_off >= align_off);
2141 ASSERT(orig_end <= align_off + align_alen);
2142 }
2143
2144#ifdef DEBUG
2145 if (!eof && gotp->br_startoff != NULLFILEOFF)
2146 ASSERT(align_off + align_alen <= gotp->br_startoff);
2147 if (prevp->br_startoff != NULLFILEOFF)
2148 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2149#endif
2150
2151 *lenp = align_alen;
2152 *offp = align_off;
2153 return 0;
2154}
2155
1da177e4
LT
2156#define XFS_ALLOC_GAP_UNITS 4
2157
c2b1cba6 2158STATIC void
a365bdd5 2159xfs_bmap_adjacent(
1da177e4
LT
2160 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2161{
2162 xfs_fsblock_t adjust; /* adjustment to block numbers */
1da177e4
LT
2163 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2164 xfs_mount_t *mp; /* mount point structure */
2165 int nullfb; /* true if ap->firstblock isn't set */
2166 int rt; /* true if inode is realtime */
1da177e4
LT
2167
2168#define ISVALID(x,y) \
2169 (rt ? \
2170 (x) < mp->m_sb.sb_rblocks : \
2171 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2172 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2173 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2174
1da177e4
LT
2175 mp = ap->ip->i_mount;
2176 nullfb = ap->firstblock == NULLFSBLOCK;
2177 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2178 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
1da177e4
LT
2179 /*
2180 * If allocating at eof, and there's a previous real block,
9da096fd 2181 * try to use its last block as our starting point.
1da177e4
LT
2182 */
2183 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
9d87c319 2184 !isnullstartblock(ap->prevp->br_startblock) &&
1da177e4
LT
2185 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2186 ap->prevp->br_startblock)) {
2187 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2188 /*
2189 * Adjust for the gap between prevp and us.
2190 */
2191 adjust = ap->off -
2192 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2193 if (adjust &&
2194 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2195 ap->rval += adjust;
2196 }
2197 /*
2198 * If not at eof, then compare the two neighbor blocks.
2199 * Figure out whether either one gives us a good starting point,
2200 * and pick the better one.
2201 */
2202 else if (!ap->eof) {
2203 xfs_fsblock_t gotbno; /* right side block number */
2204 xfs_fsblock_t gotdiff=0; /* right side difference */
2205 xfs_fsblock_t prevbno; /* left side block number */
2206 xfs_fsblock_t prevdiff=0; /* left side difference */
2207
2208 /*
2209 * If there's a previous (left) block, select a requested
2210 * start block based on it.
2211 */
2212 if (ap->prevp->br_startoff != NULLFILEOFF &&
9d87c319 2213 !isnullstartblock(ap->prevp->br_startblock) &&
1da177e4
LT
2214 (prevbno = ap->prevp->br_startblock +
2215 ap->prevp->br_blockcount) &&
2216 ISVALID(prevbno, ap->prevp->br_startblock)) {
2217 /*
2218 * Calculate gap to end of previous block.
2219 */
2220 adjust = prevdiff = ap->off -
2221 (ap->prevp->br_startoff +
2222 ap->prevp->br_blockcount);
2223 /*
2224 * Figure the startblock based on the previous block's
2225 * end and the gap size.
2226 * Heuristic!
2227 * If the gap is large relative to the piece we're
2228 * allocating, or using it gives us an invalid block
2229 * number, then just use the end of the previous block.
2230 */
2231 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2232 ISVALID(prevbno + prevdiff,
2233 ap->prevp->br_startblock))
2234 prevbno += adjust;
2235 else
2236 prevdiff += adjust;
2237 /*
2238 * If the firstblock forbids it, can't use it,
2239 * must use default.
2240 */
2241 if (!rt && !nullfb &&
2242 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2243 prevbno = NULLFSBLOCK;
2244 }
2245 /*
2246 * No previous block or can't follow it, just default.
2247 */
2248 else
2249 prevbno = NULLFSBLOCK;
2250 /*
2251 * If there's a following (right) block, select a requested
2252 * start block based on it.
2253 */
9d87c319 2254 if (!isnullstartblock(ap->gotp->br_startblock)) {
1da177e4
LT
2255 /*
2256 * Calculate gap to start of next block.
2257 */
2258 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2259 /*
2260 * Figure the startblock based on the next block's
2261 * start and the gap size.
2262 */
2263 gotbno = ap->gotp->br_startblock;
2264 /*
2265 * Heuristic!
2266 * If the gap is large relative to the piece we're
2267 * allocating, or using it gives us an invalid block
2268 * number, then just use the start of the next block
2269 * offset by our length.
2270 */
2271 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2272 ISVALID(gotbno - gotdiff, gotbno))
2273 gotbno -= adjust;
2274 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2275 gotbno -= ap->alen;
2276 gotdiff += adjust - ap->alen;
2277 } else
2278 gotdiff += adjust;
2279 /*
2280 * If the firstblock forbids it, can't use it,
2281 * must use default.
2282 */
2283 if (!rt && !nullfb &&
2284 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2285 gotbno = NULLFSBLOCK;
2286 }
2287 /*
2288 * No next block, just default.
2289 */
2290 else
2291 gotbno = NULLFSBLOCK;
2292 /*
2293 * If both valid, pick the better one, else the only good
2294 * one, else ap->rval is already set (to 0 or the inode block).
2295 */
2296 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2297 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2298 else if (prevbno != NULLFSBLOCK)
2299 ap->rval = prevbno;
2300 else if (gotbno != NULLFSBLOCK)
2301 ap->rval = gotbno;
2302 }
a365bdd5 2303#undef ISVALID
a365bdd5
NS
2304}
2305
2306STATIC int
2307xfs_bmap_rtalloc(
2308 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2309{
2310 xfs_alloctype_t atype = 0; /* type for allocation routines */
2311 int error; /* error return value */
2312 xfs_mount_t *mp; /* mount point structure */
2313 xfs_extlen_t prod = 0; /* product factor for allocators */
2314 xfs_extlen_t ralen = 0; /* realtime allocation length */
2315 xfs_extlen_t align; /* minimum allocation alignment */
a365bdd5
NS
2316 xfs_rtblock_t rtb;
2317
2318 mp = ap->ip->i_mount;
957d0ebe 2319 align = xfs_get_extsz_hint(ap->ip);
a365bdd5
NS
2320 prod = align / mp->m_sb.sb_rextsize;
2321 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2322 align, 1, ap->eof, 0,
2323 ap->conv, &ap->off, &ap->alen);
2324 if (error)
2325 return error;
2326 ASSERT(ap->alen);
2327 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2328
2329 /*
2330 * If the offset & length are not perfectly aligned
2331 * then kill prod, it will just get us in trouble.
2332 */
2333 if (do_mod(ap->off, align) || ap->alen % align)
2334 prod = 1;
2335 /*
2336 * Set ralen to be the actual requested length in rtextents.
2337 */
2338 ralen = ap->alen / mp->m_sb.sb_rextsize;
2339 /*
2340 * If the old value was close enough to MAXEXTLEN that
2341 * we rounded up to it, cut it back so it's valid again.
2342 * Note that if it's a really large request (bigger than
2343 * MAXEXTLEN), we don't hear about that number, and can't
2344 * adjust the starting point to match it.
2345 */
2346 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2347 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2348 /*
2349 * If it's an allocation to an empty file at offset 0,
2350 * pick an extent that will space things out in the rt area.
2351 */
2352 if (ap->eof && ap->off == 0) {
0892ccd6
AM
2353 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2354
a365bdd5
NS
2355 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2356 if (error)
2357 return error;
2358 ap->rval = rtx * mp->m_sb.sb_rextsize;
2359 } else {
2360 ap->rval = 0;
2361 }
2362
2363 xfs_bmap_adjacent(ap);
2364
2365 /*
2366 * Realtime allocation, done through xfs_rtallocate_extent.
2367 */
2368 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2369 do_div(ap->rval, mp->m_sb.sb_rextsize);
2370 rtb = ap->rval;
2371 ap->alen = ralen;
2372 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2373 &ralen, atype, ap->wasdel, prod, &rtb)))
2374 return error;
2375 if (rtb == NULLFSBLOCK && prod > 1 &&
2376 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2377 ap->alen, &ralen, atype,
2378 ap->wasdel, 1, &rtb)))
2379 return error;
2380 ap->rval = rtb;
2381 if (ap->rval != NULLFSBLOCK) {
2382 ap->rval *= mp->m_sb.sb_rextsize;
2383 ralen *= mp->m_sb.sb_rextsize;
2384 ap->alen = ralen;
2385 ap->ip->i_d.di_nblocks += ralen;
2386 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2387 if (ap->wasdel)
2388 ap->ip->i_delayed_blks -= ralen;
2389 /*
2390 * Adjust the disk quota also. This was reserved
2391 * earlier.
2392 */
7d095257 2393 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
a365bdd5
NS
2394 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2395 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2396 } else {
2397 ap->alen = 0;
2398 }
2399 return 0;
2400}
2401
c467c049
CH
2402STATIC int
2403xfs_bmap_btalloc_nullfb(
2404 struct xfs_bmalloca *ap,
2405 struct xfs_alloc_arg *args,
2406 xfs_extlen_t *blen)
2407{
2408 struct xfs_mount *mp = ap->ip->i_mount;
2409 struct xfs_perag *pag;
2410 xfs_agnumber_t ag, startag;
2411 int notinit = 0;
2412 int error;
2413
2414 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2415 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2416 else
2417 args->type = XFS_ALLOCTYPE_START_BNO;
2418 args->total = ap->total;
2419
2420 /*
2421 * Search for an allocation group with a single extent large enough
2422 * for the request. If one isn't found, then adjust the minimum
2423 * allocation size to the largest space found.
2424 */
2425 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2426 if (startag == NULLAGNUMBER)
2427 startag = ag = 0;
2428
2429 pag = xfs_perag_get(mp, ag);
2430 while (*blen < ap->alen) {
2431 if (!pag->pagf_init) {
2432 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2433 XFS_ALLOC_FLAG_TRYLOCK);
2434 if (error) {
2435 xfs_perag_put(pag);
2436 return error;
2437 }
2438 }
2439
2440 /*
2441 * See xfs_alloc_fix_freelist...
2442 */
2443 if (pag->pagf_init) {
2444 xfs_extlen_t longest;
2445 longest = xfs_alloc_longest_free_extent(mp, pag);
2446 if (*blen < longest)
2447 *blen = longest;
2448 } else
2449 notinit = 1;
2450
2451 if (xfs_inode_is_filestream(ap->ip)) {
2452 if (*blen >= ap->alen)
2453 break;
2454
2455 if (ap->userdata) {
2456 /*
2457 * If startag is an invalid AG, we've
2458 * come here once before and
2459 * xfs_filestream_new_ag picked the
2460 * best currently available.
2461 *
2462 * Don't continue looping, since we
2463 * could loop forever.
2464 */
2465 if (startag == NULLAGNUMBER)
2466 break;
2467
2468 error = xfs_filestream_new_ag(ap, &ag);
2469 xfs_perag_put(pag);
2470 if (error)
2471 return error;
2472
2473 /* loop again to set 'blen'*/
2474 startag = NULLAGNUMBER;
2475 pag = xfs_perag_get(mp, ag);
2476 continue;
2477 }
2478 }
2479 if (++ag == mp->m_sb.sb_agcount)
2480 ag = 0;
2481 if (ag == startag)
2482 break;
2483 xfs_perag_put(pag);
2484 pag = xfs_perag_get(mp, ag);
2485 }
2486 xfs_perag_put(pag);
2487
2488 /*
2489 * Since the above loop did a BUF_TRYLOCK, it is
2490 * possible that there is space for this request.
2491 */
2492 if (notinit || *blen < ap->minlen)
2493 args->minlen = ap->minlen;
2494 /*
2495 * If the best seen length is less than the request
2496 * length, use the best as the minimum.
2497 */
2498 else if (*blen < ap->alen)
2499 args->minlen = *blen;
2500 /*
2501 * Otherwise we've seen an extent as big as alen,
2502 * use that as the minimum.
2503 */
2504 else
2505 args->minlen = ap->alen;
2506
2507 /*
2508 * set the failure fallback case to look in the selected
2509 * AG as the stream may have moved.
2510 */
2511 if (xfs_inode_is_filestream(ap->ip))
2512 ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2513
2514 return 0;
2515}
2516
a365bdd5
NS
2517STATIC int
2518xfs_bmap_btalloc(
2519 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2520{
2521 xfs_mount_t *mp; /* mount point structure */
2522 xfs_alloctype_t atype = 0; /* type for allocation routines */
2523 xfs_extlen_t align; /* minimum allocation alignment */
a365bdd5 2524 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
c467c049 2525 xfs_agnumber_t ag;
a365bdd5
NS
2526 xfs_alloc_arg_t args;
2527 xfs_extlen_t blen;
a365bdd5 2528 xfs_extlen_t nextminlen = 0;
a365bdd5
NS
2529 int nullfb; /* true if ap->firstblock isn't set */
2530 int isaligned;
a365bdd5
NS
2531 int tryagain;
2532 int error;
2533
2534 mp = ap->ip->i_mount;
957d0ebe 2535 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
a365bdd5
NS
2536 if (unlikely(align)) {
2537 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2538 align, 0, ap->eof, 0, ap->conv,
2539 &ap->off, &ap->alen);
2540 ASSERT(!error);
2541 ASSERT(ap->alen);
2542 }
2543 nullfb = ap->firstblock == NULLFSBLOCK;
2544 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2a82b8be
DC
2545 if (nullfb) {
2546 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2547 ag = xfs_filestream_lookup_ag(ap->ip);
2548 ag = (ag != NULLAGNUMBER) ? ag : 0;
2549 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2550 } else {
2551 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2552 }
2553 } else
a365bdd5
NS
2554 ap->rval = ap->firstblock;
2555
2556 xfs_bmap_adjacent(ap);
2557
1da177e4
LT
2558 /*
2559 * If allowed, use ap->rval; otherwise must use firstblock since
2560 * it's in the right allocation group.
2561 */
a365bdd5 2562 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
1da177e4
LT
2563 ;
2564 else
2565 ap->rval = ap->firstblock;
2566 /*
a365bdd5 2567 * Normal allocation, done through xfs_alloc_vextent.
1da177e4 2568 */
a365bdd5
NS
2569 tryagain = isaligned = 0;
2570 args.tp = ap->tp;
2571 args.mp = mp;
2572 args.fsbno = ap->rval;
2573 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
d210a28c 2574 args.firstblock = ap->firstblock;
a365bdd5
NS
2575 blen = 0;
2576 if (nullfb) {
c467c049
CH
2577 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2578 if (error)
2579 return error;
a365bdd5 2580 } else if (ap->low) {
2a82b8be
DC
2581 if (xfs_inode_is_filestream(ap->ip))
2582 args.type = XFS_ALLOCTYPE_FIRST_AG;
2583 else
2584 args.type = XFS_ALLOCTYPE_START_BNO;
a365bdd5
NS
2585 args.total = args.minlen = ap->minlen;
2586 } else {
2587 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2588 args.total = ap->total;
2589 args.minlen = ap->minlen;
2590 }
957d0ebe
DC
2591 /* apply extent size hints if obtained earlier */
2592 if (unlikely(align)) {
2593 args.prod = align;
a365bdd5
NS
2594 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2595 args.mod = (xfs_extlen_t)(args.prod - args.mod);
e6a4b37f 2596 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
a365bdd5
NS
2597 args.prod = 1;
2598 args.mod = 0;
2599 } else {
e6a4b37f 2600 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
a365bdd5
NS
2601 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2602 args.mod = (xfs_extlen_t)(args.prod - args.mod);
1da177e4
LT
2603 }
2604 /*
a365bdd5
NS
2605 * If we are not low on available data blocks, and the
2606 * underlying logical volume manager is a stripe, and
2607 * the file offset is zero then try to allocate data
2608 * blocks on stripe unit boundary.
2609 * NOTE: ap->aeof is only set if the allocation length
2610 * is >= the stripe unit and the allocation offset is
2611 * at the end of file.
1da177e4 2612 */
a365bdd5
NS
2613 if (!ap->low && ap->aeof) {
2614 if (!ap->off) {
2615 args.alignment = mp->m_dalign;
2616 atype = args.type;
2617 isaligned = 1;
1da177e4 2618 /*
a365bdd5 2619 * Adjust for alignment
1da177e4 2620 */
a365bdd5
NS
2621 if (blen > args.alignment && blen <= ap->alen)
2622 args.minlen = blen - args.alignment;
2623 args.minalignslop = 0;
2624 } else {
1da177e4 2625 /*
a365bdd5
NS
2626 * First try an exact bno allocation.
2627 * If it fails then do a near or start bno
2628 * allocation with alignment turned on.
1da177e4 2629 */
a365bdd5
NS
2630 atype = args.type;
2631 tryagain = 1;
2632 args.type = XFS_ALLOCTYPE_THIS_BNO;
2633 args.alignment = 1;
1da177e4 2634 /*
a365bdd5
NS
2635 * Compute the minlen+alignment for the
2636 * next case. Set slop so that the value
2637 * of minlen+alignment+slop doesn't go up
2638 * between the calls.
1da177e4 2639 */
a365bdd5
NS
2640 if (blen > mp->m_dalign && blen <= ap->alen)
2641 nextminlen = blen - mp->m_dalign;
1da177e4 2642 else
a365bdd5
NS
2643 nextminlen = args.minlen;
2644 if (nextminlen + mp->m_dalign > args.minlen + 1)
2645 args.minalignslop =
2646 nextminlen + mp->m_dalign -
2647 args.minlen - 1;
2648 else
2649 args.minalignslop = 0;
1da177e4 2650 }
a365bdd5
NS
2651 } else {
2652 args.alignment = 1;
2653 args.minalignslop = 0;
2654 }
2655 args.minleft = ap->minleft;
2656 args.wasdel = ap->wasdel;
2657 args.isfl = 0;
2658 args.userdata = ap->userdata;
2659 if ((error = xfs_alloc_vextent(&args)))
2660 return error;
2661 if (tryagain && args.fsbno == NULLFSBLOCK) {
1da177e4 2662 /*
a365bdd5
NS
2663 * Exact allocation failed. Now try with alignment
2664 * turned on.
1da177e4 2665 */
a365bdd5
NS
2666 args.type = atype;
2667 args.fsbno = ap->rval;
2668 args.alignment = mp->m_dalign;
2669 args.minlen = nextminlen;
2670 args.minalignslop = 0;
2671 isaligned = 1;
1da177e4
LT
2672 if ((error = xfs_alloc_vextent(&args)))
2673 return error;
a365bdd5
NS
2674 }
2675 if (isaligned && args.fsbno == NULLFSBLOCK) {
2676 /*
2677 * allocation failed, so turn off alignment and
2678 * try again.
2679 */
2680 args.type = atype;
2681 args.fsbno = ap->rval;
2682 args.alignment = 0;
2683 if ((error = xfs_alloc_vextent(&args)))
2684 return error;
2685 }
2686 if (args.fsbno == NULLFSBLOCK && nullfb &&
2687 args.minlen > ap->minlen) {
2688 args.minlen = ap->minlen;
2689 args.type = XFS_ALLOCTYPE_START_BNO;
2690 args.fsbno = ap->rval;
2691 if ((error = xfs_alloc_vextent(&args)))
2692 return error;
2693 }
2694 if (args.fsbno == NULLFSBLOCK && nullfb) {
2695 args.fsbno = 0;
2696 args.type = XFS_ALLOCTYPE_FIRST_AG;
2697 args.total = ap->minlen;
2698 args.minleft = 0;
2699 if ((error = xfs_alloc_vextent(&args)))
2700 return error;
2701 ap->low = 1;
2702 }
2703 if (args.fsbno != NULLFSBLOCK) {
2704 ap->firstblock = ap->rval = args.fsbno;
2705 ASSERT(nullfb || fb_agno == args.agno ||
2706 (ap->low && fb_agno < args.agno));
2707 ap->alen = args.len;
2708 ap->ip->i_d.di_nblocks += args.len;
2709 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2710 if (ap->wasdel)
2711 ap->ip->i_delayed_blks -= args.len;
2712 /*
2713 * Adjust the disk quota also. This was reserved
2714 * earlier.
2715 */
7d095257 2716 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
a365bdd5
NS
2717 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2718 XFS_TRANS_DQ_BCOUNT,
2719 (long) args.len);
2720 } else {
2721 ap->rval = NULLFSBLOCK;
2722 ap->alen = 0;
1da177e4
LT
2723 }
2724 return 0;
a365bdd5
NS
2725}
2726
2727/*
2728 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2729 * It figures out where to ask the underlying allocator to put the new extent.
2730 */
2731STATIC int
2732xfs_bmap_alloc(
2733 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2734{
71ddabb9 2735 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
a365bdd5
NS
2736 return xfs_bmap_rtalloc(ap);
2737 return xfs_bmap_btalloc(ap);
1da177e4
LT
2738}
2739
2740/*
2741 * Transform a btree format file with only one leaf node, where the
2742 * extents list will fit in the inode, into an extents format file.
4eea22f0 2743 * Since the file extents are already in-core, all we have to do is
1da177e4
LT
2744 * give up the space for the btree root and pitch the leaf block.
2745 */
2746STATIC int /* error */
2747xfs_bmap_btree_to_extents(
2748 xfs_trans_t *tp, /* transaction pointer */
2749 xfs_inode_t *ip, /* incore inode pointer */
2750 xfs_btree_cur_t *cur, /* btree cursor */
2751 int *logflagsp, /* inode logging flags */
2752 int whichfork) /* data or attr fork */
2753{
2754 /* REFERENCED */
7cc95a82 2755 struct xfs_btree_block *cblock;/* child btree block */
1da177e4
LT
2756 xfs_fsblock_t cbno; /* child block number */
2757 xfs_buf_t *cbp; /* child block's buffer */
2758 int error; /* error return value */
2759 xfs_ifork_t *ifp; /* inode fork data */
2760 xfs_mount_t *mp; /* mount point structure */
576039cf 2761 __be64 *pp; /* ptr to block address */
7cc95a82 2762 struct xfs_btree_block *rblock;/* root btree block */
1da177e4 2763
60197e8d 2764 mp = ip->i_mount;
1da177e4
LT
2765 ifp = XFS_IFORK_PTR(ip, whichfork);
2766 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2767 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2768 rblock = ifp->if_broot;
16259e7d
CH
2769 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2770 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
60197e8d
CH
2771 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2772 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
576039cf 2773 cbno = be64_to_cpu(*pp);
1da177e4
LT
2774 *logflagsp = 0;
2775#ifdef DEBUG
576039cf 2776 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
1da177e4
LT
2777 return error;
2778#endif
1da177e4
LT
2779 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2780 XFS_BMAP_BTREE_REF)))
2781 return error;
7cc95a82
CH
2782 cblock = XFS_BUF_TO_BLOCK(cbp);
2783 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
1da177e4
LT
2784 return error;
2785 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2786 ip->i_d.di_nblocks--;
7d095257 2787 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
1da177e4
LT
2788 xfs_trans_binval(tp, cbp);
2789 if (cur->bc_bufs[0] == cbp)
2790 cur->bc_bufs[0] = NULL;
2791 xfs_iroot_realloc(ip, -1, whichfork);
2792 ASSERT(ifp->if_broot == NULL);
2793 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2794 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
9d87c319 2795 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1da177e4
LT
2796 return 0;
2797}
2798
2799/*
4eea22f0 2800 * Called by xfs_bmapi to update file extent records and the btree
1da177e4
LT
2801 * after removing space (or undoing a delayed allocation).
2802 */
2803STATIC int /* error */
2804xfs_bmap_del_extent(
2805 xfs_inode_t *ip, /* incore inode pointer */
2806 xfs_trans_t *tp, /* current transaction pointer */
2807 xfs_extnum_t idx, /* extent number to update/delete */
2808 xfs_bmap_free_t *flist, /* list of extents to be freed */
2809 xfs_btree_cur_t *cur, /* if null, not a btree */
4eea22f0 2810 xfs_bmbt_irec_t *del, /* data to remove from extents */
1da177e4
LT
2811 int *logflagsp, /* inode logging flags */
2812 int whichfork, /* data or attr fork */
2813 int rsvd) /* OK to allocate reserved blocks */
2814{
2815 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2816 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2817 xfs_fsblock_t del_endblock=0; /* first block past del */
2818 xfs_fileoff_t del_endoff; /* first offset past del */
2819 int delay; /* current block is delayed allocated */
2820 int do_fx; /* free extent at end of routine */
a6f64d4a 2821 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
1da177e4
LT
2822 int error; /* error return value */
2823 int flags; /* inode logging flags */
1da177e4
LT
2824 xfs_bmbt_irec_t got; /* current extent entry */
2825 xfs_fileoff_t got_endoff; /* first offset past got */
2826 int i; /* temp state */
2827 xfs_ifork_t *ifp; /* inode fork pointer */
2828 xfs_mount_t *mp; /* mount structure */
2829 xfs_filblks_t nblks; /* quota/sb block count */
2830 xfs_bmbt_irec_t new; /* new record to be inserted */
2831 /* REFERENCED */
1da177e4
LT
2832 uint qfield; /* quota field to update */
2833 xfs_filblks_t temp; /* for indirect length calculations */
2834 xfs_filblks_t temp2; /* for indirect length calculations */
0b1b213f 2835 int state = 0;
1da177e4
LT
2836
2837 XFS_STATS_INC(xs_del_exlist);
0b1b213f
CH
2838
2839 if (whichfork == XFS_ATTR_FORK)
2840 state |= BMAP_ATTRFORK;
2841
1da177e4
LT
2842 mp = ip->i_mount;
2843 ifp = XFS_IFORK_PTR(ip, whichfork);
4eea22f0
MK
2844 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
2845 (uint)sizeof(xfs_bmbt_rec_t)));
1da177e4 2846 ASSERT(del->br_blockcount > 0);
4eea22f0 2847 ep = xfs_iext_get_ext(ifp, idx);
1da177e4
LT
2848 xfs_bmbt_get_all(ep, &got);
2849 ASSERT(got.br_startoff <= del->br_startoff);
2850 del_endoff = del->br_startoff + del->br_blockcount;
2851 got_endoff = got.br_startoff + got.br_blockcount;
2852 ASSERT(got_endoff >= del_endoff);
9d87c319
ES
2853 delay = isnullstartblock(got.br_startblock);
2854 ASSERT(isnullstartblock(del->br_startblock) == delay);
1da177e4
LT
2855 flags = 0;
2856 qfield = 0;
2857 error = 0;
2858 /*
2859 * If deleting a real allocation, must free up the disk space.
2860 */
2861 if (!delay) {
2862 flags = XFS_ILOG_CORE;
2863 /*
2864 * Realtime allocation. Free it and record di_nblocks update.
2865 */
71ddabb9 2866 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
1da177e4
LT
2867 xfs_fsblock_t bno;
2868 xfs_filblks_t len;
2869
2870 ASSERT(do_mod(del->br_blockcount,
2871 mp->m_sb.sb_rextsize) == 0);
2872 ASSERT(do_mod(del->br_startblock,
2873 mp->m_sb.sb_rextsize) == 0);
2874 bno = del->br_startblock;
2875 len = del->br_blockcount;
2876 do_div(bno, mp->m_sb.sb_rextsize);
2877 do_div(len, mp->m_sb.sb_rextsize);
2878 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
2879 (xfs_extlen_t)len)))
2880 goto done;
2881 do_fx = 0;
2882 nblks = len * mp->m_sb.sb_rextsize;
2883 qfield = XFS_TRANS_DQ_RTBCOUNT;
2884 }
2885 /*
2886 * Ordinary allocation.
2887 */
2888 else {
2889 do_fx = 1;
2890 nblks = del->br_blockcount;
2891 qfield = XFS_TRANS_DQ_BCOUNT;
2892 }
2893 /*
2894 * Set up del_endblock and cur for later.
2895 */
2896 del_endblock = del->br_startblock + del->br_blockcount;
2897 if (cur) {
2898 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2899 got.br_startblock, got.br_blockcount,
2900 &i)))
2901 goto done;
6bd8fc8a 2902 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
2903 }
2904 da_old = da_new = 0;
2905 } else {
9d87c319 2906 da_old = startblockval(got.br_startblock);
1da177e4
LT
2907 da_new = 0;
2908 nblks = 0;
2909 do_fx = 0;
2910 }
2911 /*
2912 * Set flag value to use in switch statement.
2913 * Left-contig is 2, right-contig is 1.
2914 */
2915 switch (((got.br_startoff == del->br_startoff) << 1) |
2916 (got_endoff == del_endoff)) {
2917 case 3:
2918 /*
2919 * Matches the whole extent. Delete the entry.
2920 */
6ef35544
CH
2921 xfs_iext_remove(ip, idx, 1,
2922 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
1da177e4
LT
2923 ifp->if_lastex = idx;
2924 if (delay)
2925 break;
2926 XFS_IFORK_NEXT_SET(ip, whichfork,
2927 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2928 flags |= XFS_ILOG_CORE;
2929 if (!cur) {
9d87c319 2930 flags |= xfs_ilog_fext(whichfork);
1da177e4
LT
2931 break;
2932 }
91cca5df 2933 if ((error = xfs_btree_delete(cur, &i)))
1da177e4 2934 goto done;
6bd8fc8a 2935 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
2936 break;
2937
2938 case 2:
2939 /*
2940 * Deleting the first part of the extent.
2941 */
0b1b213f 2942 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1da177e4
LT
2943 xfs_bmbt_set_startoff(ep, del_endoff);
2944 temp = got.br_blockcount - del->br_blockcount;
2945 xfs_bmbt_set_blockcount(ep, temp);
2946 ifp->if_lastex = idx;
2947 if (delay) {
2948 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2949 da_old);
9d87c319 2950 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
0b1b213f 2951 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1da177e4
LT
2952 da_new = temp;
2953 break;
2954 }
2955 xfs_bmbt_set_startblock(ep, del_endblock);
0b1b213f 2956 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1da177e4 2957 if (!cur) {
9d87c319 2958 flags |= xfs_ilog_fext(whichfork);
1da177e4
LT
2959 break;
2960 }
2961 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2962 got.br_blockcount - del->br_blockcount,
2963 got.br_state)))
2964 goto done;
2965 break;
2966
2967 case 1:
2968 /*
2969 * Deleting the last part of the extent.
2970 */
2971 temp = got.br_blockcount - del->br_blockcount;
0b1b213f 2972 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1da177e4
LT
2973 xfs_bmbt_set_blockcount(ep, temp);
2974 ifp->if_lastex = idx;
2975 if (delay) {
2976 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2977 da_old);
9d87c319 2978 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
0b1b213f 2979 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1da177e4
LT
2980 da_new = temp;
2981 break;
2982 }
0b1b213f 2983 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1da177e4 2984 if (!cur) {
9d87c319 2985 flags |= xfs_ilog_fext(whichfork);
1da177e4
LT
2986 break;
2987 }
2988 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2989 got.br_startblock,
2990 got.br_blockcount - del->br_blockcount,
2991 got.br_state)))
2992 goto done;
2993 break;
2994
2995 case 0:
2996 /*
2997 * Deleting the middle of the extent.
2998 */
2999 temp = del->br_startoff - got.br_startoff;
0b1b213f 3000 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1da177e4
LT
3001 xfs_bmbt_set_blockcount(ep, temp);
3002 new.br_startoff = del_endoff;
3003 temp2 = got_endoff - del_endoff;
3004 new.br_blockcount = temp2;
3005 new.br_state = got.br_state;
3006 if (!delay) {
3007 new.br_startblock = del_endblock;
3008 flags |= XFS_ILOG_CORE;
3009 if (cur) {
3010 if ((error = xfs_bmbt_update(cur,
3011 got.br_startoff,
3012 got.br_startblock, temp,
3013 got.br_state)))
3014 goto done;
637aa50f 3015 if ((error = xfs_btree_increment(cur, 0, &i)))
1da177e4
LT
3016 goto done;
3017 cur->bc_rec.b = new;
4b22a571 3018 error = xfs_btree_insert(cur, &i);
1da177e4
LT
3019 if (error && error != ENOSPC)
3020 goto done;
3021 /*
3022 * If get no-space back from btree insert,
3023 * it tried a split, and we have a zero
3024 * block reservation.
3025 * Fix up our state and return the error.
3026 */
3027 if (error == ENOSPC) {
3028 /*
3029 * Reset the cursor, don't trust
3030 * it after any insert operation.
3031 */
3032 if ((error = xfs_bmbt_lookup_eq(cur,
3033 got.br_startoff,
3034 got.br_startblock,
3035 temp, &i)))
3036 goto done;
6bd8fc8a 3037 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4
LT
3038 /*
3039 * Update the btree record back
3040 * to the original value.
3041 */
3042 if ((error = xfs_bmbt_update(cur,
3043 got.br_startoff,
3044 got.br_startblock,
3045 got.br_blockcount,
3046 got.br_state)))
3047 goto done;
3048 /*
3049 * Reset the extent record back
3050 * to the original value.
3051 */
3052 xfs_bmbt_set_blockcount(ep,
3053 got.br_blockcount);
3054 flags = 0;
3055 error = XFS_ERROR(ENOSPC);
3056 goto done;
3057 }
6bd8fc8a 3058 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1da177e4 3059 } else
9d87c319 3060 flags |= xfs_ilog_fext(whichfork);
1da177e4
LT
3061 XFS_IFORK_NEXT_SET(ip, whichfork,
3062 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3063 } else {
3064 ASSERT(whichfork == XFS_DATA_FORK);
3065 temp = xfs_bmap_worst_indlen(ip, temp);
9d87c319 3066 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
1da177e4 3067 temp2 = xfs_bmap_worst_indlen(ip, temp2);
9d87c319 3068 new.br_startblock = nullstartblock((int)temp2);
1da177e4
LT
3069 da_new = temp + temp2;
3070 while (da_new > da_old) {
3071 if (temp) {
3072 temp--;
3073 da_new--;
3074 xfs_bmbt_set_startblock(ep,
9d87c319 3075 nullstartblock((int)temp));
1da177e4
LT
3076 }
3077 if (da_new == da_old)
3078 break;
3079 if (temp2) {
3080 temp2--;
3081 da_new--;
3082 new.br_startblock =
9d87c319 3083 nullstartblock((int)temp2);
1da177e4
LT
3084 }
3085 }
3086 }
0b1b213f
CH
3087 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3088 xfs_iext_insert(ip, idx + 1, 1, &new, state);
1da177e4
LT
3089 ifp->if_lastex = idx + 1;
3090 break;
3091 }
3092 /*
3093 * If we need to, add to list of extents to delete.
3094 */
3095 if (do_fx)
3096 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3097 mp);
3098 /*
3099 * Adjust inode # blocks in the file.
3100 */
3101 if (nblks)
3102 ip->i_d.di_nblocks -= nblks;
3103 /*
3104 * Adjust quota data.
3105 */
3106 if (qfield)
7d095257 3107 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
1da177e4
LT
3108
3109 /*
3110 * Account for change in delayed indirect blocks.
3111 * Nothing to do for disk quota accounting here.
3112 */
3113 ASSERT(da_old >= da_new);
3114 if (da_old > da_new)
20f4ebf2 3115 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
1da177e4
LT
3116 rsvd);
3117done:
3118 *logflagsp = flags;
3119 return error;
3120}
3121
3122/*
3123 * Remove the entry "free" from the free item list. Prev points to the
3124 * previous entry, unless "free" is the head of the list.
3125 */
3126STATIC void
3127xfs_bmap_del_free(
3128 xfs_bmap_free_t *flist, /* free item list header */
3129 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3130 xfs_bmap_free_item_t *free) /* list item to be freed */
3131{
3132 if (prev)
3133 prev->xbfi_next = free->xbfi_next;
3134 else
3135 flist->xbf_first = free->xbfi_next;
3136 flist->xbf_count--;
3137 kmem_zone_free(xfs_bmap_free_item_zone, free);
3138}
3139
1da177e4
LT
3140/*
3141 * Convert an extents-format file into a btree-format file.
3142 * The new file will have a root block (in the inode) and a single child block.
3143 */
3144STATIC int /* error */
3145xfs_bmap_extents_to_btree(
3146 xfs_trans_t *tp, /* transaction pointer */
3147 xfs_inode_t *ip, /* incore inode pointer */
3148 xfs_fsblock_t *firstblock, /* first-block-allocated */
3149 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3150 xfs_btree_cur_t **curp, /* cursor returned to caller */
3151 int wasdel, /* converting a delayed alloc */
3152 int *logflagsp, /* inode logging flags */
3153 int whichfork) /* data or attr fork */
3154{
7cc95a82 3155 struct xfs_btree_block *ablock; /* allocated (child) bt block */
1da177e4
LT
3156 xfs_buf_t *abp; /* buffer for ablock */
3157 xfs_alloc_arg_t args; /* allocation arguments */
3158 xfs_bmbt_rec_t *arp; /* child record pointer */
7cc95a82 3159 struct xfs_btree_block *block; /* btree root block */
1da177e4 3160 xfs_btree_cur_t *cur; /* bmap btree cursor */
a6f64d4a 3161 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1da177e4 3162 int error; /* error return value */
4eea22f0 3163 xfs_extnum_t i, cnt; /* extent record index */
1da177e4
LT
3164 xfs_ifork_t *ifp; /* inode fork pointer */
3165 xfs_bmbt_key_t *kp; /* root block key pointer */
3166 xfs_mount_t *mp; /* mount structure */
4eea22f0 3167 xfs_extnum_t nextents; /* number of file extents */
1da177e4
LT
3168 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3169
3170 ifp = XFS_IFORK_PTR(ip, whichfork);
3171 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3172 ASSERT(ifp->if_ext_max ==
3173 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3174 /*
3175 * Make space in the inode incore.
3176 */
3177 xfs_iroot_realloc(ip, 1, whichfork);
3178 ifp->if_flags |= XFS_IFBROOT;
7cc95a82 3179
1da177e4
LT
3180 /*
3181 * Fill in the root.
3182 */
3183 block = ifp->if_broot;
16259e7d
CH
3184 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3185 block->bb_level = cpu_to_be16(1);
3186 block->bb_numrecs = cpu_to_be16(1);
7cc95a82
CH
3187 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3188 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3189
1da177e4
LT
3190 /*
3191 * Need a cursor. Can't allocate until bb_level is filled in.
3192 */
3193 mp = ip->i_mount;
561f7d17 3194 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1da177e4
LT
3195 cur->bc_private.b.firstblock = *firstblock;
3196 cur->bc_private.b.flist = flist;
3197 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3198 /*
3199 * Convert to a btree with two levels, one record in root.
3200 */
3201 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3202 args.tp = tp;
3203 args.mp = mp;
d210a28c 3204 args.firstblock = *firstblock;
1da177e4
LT
3205 if (*firstblock == NULLFSBLOCK) {
3206 args.type = XFS_ALLOCTYPE_START_BNO;
3207 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3208 } else if (flist->xbf_low) {
3209 args.type = XFS_ALLOCTYPE_START_BNO;
3210 args.fsbno = *firstblock;
3211 } else {
3212 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3213 args.fsbno = *firstblock;
3214 }
3215 args.minlen = args.maxlen = args.prod = 1;
3216 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3217 args.minalignslop = 0;
3218 args.wasdel = wasdel;
3219 *logflagsp = 0;
3220 if ((error = xfs_alloc_vextent(&args))) {
3221 xfs_iroot_realloc(ip, -1, whichfork);
3222 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3223 return error;
3224 }
3225 /*
3226 * Allocation can't fail, the space was reserved.
3227 */
3228 ASSERT(args.fsbno != NULLFSBLOCK);
3229 ASSERT(*firstblock == NULLFSBLOCK ||
3230 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3231 (flist->xbf_low &&
3232 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3233 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3234 cur->bc_private.b.allocated++;
3235 ip->i_d.di_nblocks++;
7d095257 3236 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
1da177e4
LT
3237 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3238 /*
3239 * Fill in the child block.
3240 */
7cc95a82 3241 ablock = XFS_BUF_TO_BLOCK(abp);
16259e7d 3242 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
1da177e4 3243 ablock->bb_level = 0;
7cc95a82
CH
3244 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3245 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
136341b4 3246 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
1da177e4 3247 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4eea22f0
MK
3248 for (cnt = i = 0; i < nextents; i++) {
3249 ep = xfs_iext_get_ext(ifp, i);
9d87c319 3250 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
cd8b0a97
CH
3251 arp->l0 = cpu_to_be64(ep->l0);
3252 arp->l1 = cpu_to_be64(ep->l1);
1da177e4
LT
3253 arp++; cnt++;
3254 }
3255 }
16259e7d 3256 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
7cc95a82
CH
3257 xfs_btree_set_numrecs(ablock, cnt);
3258
1da177e4
LT
3259 /*
3260 * Fill in the root key and pointer.
3261 */
136341b4
CH
3262 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3263 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
8801bb99 3264 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
136341b4
CH
3265 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3266 be16_to_cpu(block->bb_level)));
576039cf 3267 *pp = cpu_to_be64(args.fsbno);
136341b4 3268
1da177e4
LT
3269 /*
3270 * Do all this logging at the end so that
3271 * the root is at the right level.
3272 */
fd6bcc5b
CH
3273 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3274 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
1da177e4
LT
3275 ASSERT(*curp == NULL);
3276 *curp = cur;
9d87c319 3277 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
1da177e4
LT
3278 return 0;
3279}
3280
1a5902c5
CH
3281/*
3282 * Calculate the default attribute fork offset for newly created inodes.
3283 */
3284uint
3285xfs_default_attroffset(
3286 struct xfs_inode *ip)
3287{
3288 struct xfs_mount *mp = ip->i_mount;
3289 uint offset;
3290
3291 if (mp->m_sb.sb_inodesize == 256) {
3292 offset = XFS_LITINO(mp) -
3293 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3294 } else {
3295 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3296 }
3297
3298 ASSERT(offset < XFS_LITINO(mp));
3299 return offset;
3300}
3301
d8cc890d
NS
3302/*
3303 * Helper routine to reset inode di_forkoff field when switching
3304 * attribute fork from local to extent format - we reset it where
3305 * possible to make space available for inline data fork extents.
3306 */
3307STATIC void
3308xfs_bmap_forkoff_reset(
3309 xfs_mount_t *mp,
3310 xfs_inode_t *ip,
3311 int whichfork)
3312{
3313 if (whichfork == XFS_ATTR_FORK &&
1a5902c5
CH
3314 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3315 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3316 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3317 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3318
3319 if (dfl_forkoff > ip->i_d.di_forkoff) {
3320 ip->i_d.di_forkoff = dfl_forkoff;
3321 ip->i_df.if_ext_max =
3322 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3323 ip->i_afp->if_ext_max =
3324 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3325 }
d8cc890d
NS
3326 }
3327}
3328
1da177e4
LT
3329/*
3330 * Convert a local file to an extents file.
3331 * This code is out of bounds for data forks of regular files,
3332 * since the file data needs to get logged so things will stay consistent.
3333 * (The bmap-level manipulations are ok, though).
3334 */
3335STATIC int /* error */
3336xfs_bmap_local_to_extents(
3337 xfs_trans_t *tp, /* transaction pointer */
3338 xfs_inode_t *ip, /* incore inode pointer */
3339 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3340 xfs_extlen_t total, /* total blocks needed by transaction */
3341 int *logflagsp, /* inode logging flags */
3342 int whichfork) /* data or attr fork */
3343{
3344 int error; /* error return value */
3345 int flags; /* logging flags returned */
1da177e4
LT
3346 xfs_ifork_t *ifp; /* inode fork pointer */
3347
3348 /*
3349 * We don't want to deal with the case of keeping inode data inline yet.
3350 * So sending the data fork of a regular inode is invalid.
3351 */
3352 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3353 whichfork == XFS_DATA_FORK));
3354 ifp = XFS_IFORK_PTR(ip, whichfork);
3355 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3356 flags = 0;
3357 error = 0;
3358 if (ifp->if_bytes) {
3359 xfs_alloc_arg_t args; /* allocation arguments */
4eea22f0 3360 xfs_buf_t *bp; /* buffer for extent block */
a6f64d4a 3361 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
1da177e4
LT
3362
3363 args.tp = tp;
3364 args.mp = ip->i_mount;
d210a28c 3365 args.firstblock = *firstblock;
f020b67f
MK
3366 ASSERT((ifp->if_flags &
3367 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
1da177e4
LT
3368 /*
3369 * Allocate a block. We know we need only one, since the
3370 * file currently fits in an inode.
3371 */
3372 if (*firstblock == NULLFSBLOCK) {
3373 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3374 args.type = XFS_ALLOCTYPE_START_BNO;
3375 } else {
3376 args.fsbno = *firstblock;
3377 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3378 }
3379 args.total = total;
3380 args.mod = args.minleft = args.alignment = args.wasdel =
3381 args.isfl = args.minalignslop = 0;
3382 args.minlen = args.maxlen = args.prod = 1;
3383 if ((error = xfs_alloc_vextent(&args)))
3384 goto done;
3385 /*
3386 * Can't fail, the space was reserved.
3387 */
3388 ASSERT(args.fsbno != NULLFSBLOCK);
3389 ASSERT(args.len == 1);
3390 *firstblock = args.fsbno;
3391 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3392 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3393 ifp->if_bytes);
3394 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
d8cc890d 3395 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
1da177e4 3396 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
4eea22f0
MK
3397 xfs_iext_add(ifp, 0, 1);
3398 ep = xfs_iext_get_ext(ifp, 0);
1da177e4 3399 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
0b1b213f
CH
3400 trace_xfs_bmap_post_update(ip, 0,
3401 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3402 _THIS_IP_);
1da177e4
LT
3403 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3404 ip->i_d.di_nblocks = 1;
7d095257 3405 xfs_trans_mod_dquot_byino(tp, ip,
1da177e4 3406 XFS_TRANS_DQ_BCOUNT, 1L);
9d87c319 3407 flags |= xfs_ilog_fext(whichfork);
d8cc890d 3408 } else {
1da177e4 3409 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
d8cc890d
NS
3410 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3411 }
1da177e4
LT
3412 ifp->if_flags &= ~XFS_IFINLINE;
3413 ifp->if_flags |= XFS_IFEXTENTS;
3414 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3415 flags |= XFS_ILOG_CORE;
3416done:
3417 *logflagsp = flags;
3418 return error;
3419}
3420
0293ce3a 3421/*
8867bc9b
MK
3422 * Search the extent records for the entry containing block bno.
3423 * If bno lies in a hole, point to the next entry. If bno lies
3424 * past eof, *eofp will be set, and *prevp will contain the last
3425 * entry (null if none). Else, *lastxp will be set to the index
3426 * of the found entry; *gotp will contain the entry.
0293ce3a 3427 */
d96f8f89 3428STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
0293ce3a
MK
3429xfs_bmap_search_multi_extents(
3430 xfs_ifork_t *ifp, /* inode fork pointer */
3431 xfs_fileoff_t bno, /* block number searched for */
3432 int *eofp, /* out: end of file found */
3433 xfs_extnum_t *lastxp, /* out: last extent index */
3434 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3435 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3436{
a6f64d4a 3437 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
0293ce3a 3438 xfs_extnum_t lastx; /* last extent index */
0293ce3a
MK
3439
3440 /*
8867bc9b
MK
3441 * Initialize the extent entry structure to catch access to
3442 * uninitialized br_startblock field.
0293ce3a 3443 */
8867bc9b
MK
3444 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3445 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3446 gotp->br_state = XFS_EXT_INVALID;
3447#if XFS_BIG_BLKNOS
3448 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3449#else
3450 gotp->br_startblock = 0xffffa5a5;
3451#endif
3452 prevp->br_startoff = NULLFILEOFF;
3453
3454 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3455 if (lastx > 0) {
3456 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
0293ce3a 3457 }
8867bc9b
MK
3458 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3459 xfs_bmbt_get_all(ep, gotp);
3460 *eofp = 0;
3461 } else {
3462 if (lastx > 0) {
3463 *gotp = *prevp;
3464 }
3465 *eofp = 1;
3466 ep = NULL;
0293ce3a 3467 }
8867bc9b 3468 *lastxp = lastx;
0293ce3a
MK
3469 return ep;
3470}
3471
1da177e4
LT
3472/*
3473 * Search the extents list for the inode, for the extent containing bno.
3474 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3475 * *eofp will be set, and *prevp will contain the last entry (null if none).
3476 * Else, *lastxp will be set to the index of the found
3477 * entry; *gotp will contain the entry.
3478 */
a6f64d4a 3479STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1da177e4
LT
3480xfs_bmap_search_extents(
3481 xfs_inode_t *ip, /* incore inode pointer */
3482 xfs_fileoff_t bno, /* block number searched for */
572d95f4 3483 int fork, /* data or attr fork */
1da177e4
LT
3484 int *eofp, /* out: end of file found */
3485 xfs_extnum_t *lastxp, /* out: last extent index */
3486 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3487 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3488{
3489 xfs_ifork_t *ifp; /* inode fork pointer */
a6f64d4a 3490 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1da177e4
LT
3491
3492 XFS_STATS_INC(xs_look_exlist);
572d95f4 3493 ifp = XFS_IFORK_PTR(ip, fork);
1da177e4 3494
0293ce3a
MK
3495 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3496
572d95f4
NS
3497 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3498 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3499 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3500 "Access to block zero in inode %llu "
3501 "start_block: %llx start_off: %llx "
3502 "blkcnt: %llx extent-state: %x lastx: %x\n",
3503 (unsigned long long)ip->i_ino,
3ddb8fa9
NS
3504 (unsigned long long)gotp->br_startblock,
3505 (unsigned long long)gotp->br_startoff,
3506 (unsigned long long)gotp->br_blockcount,
572d95f4
NS
3507 gotp->br_state, *lastxp);
3508 *lastxp = NULLEXTNUM;
3509 *eofp = 1;
3510 return NULL;
3511 }
3512 return ep;
1da177e4
LT
3513}
3514
1da177e4
LT
3515/*
3516 * Compute the worst-case number of indirect blocks that will be used
3517 * for ip's delayed extent of length "len".
3518 */
3519STATIC xfs_filblks_t
3520xfs_bmap_worst_indlen(
3521 xfs_inode_t *ip, /* incore inode pointer */
3522 xfs_filblks_t len) /* delayed extent length */
3523{
3524 int level; /* btree level number */
3525 int maxrecs; /* maximum record count at this level */
3526 xfs_mount_t *mp; /* mount structure */
3527 xfs_filblks_t rval; /* return value */
3528
3529 mp = ip->i_mount;
3530 maxrecs = mp->m_bmap_dmxr[0];
3531 for (level = 0, rval = 0;
3532 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3533 level++) {
3534 len += maxrecs - 1;
3535 do_div(len, maxrecs);
3536 rval += len;
3537 if (len == 1)
3538 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3539 level - 1;
3540 if (level == 0)
3541 maxrecs = mp->m_bmap_dmxr[1];
3542 }
3543 return rval;
3544}
3545
1da177e4
LT
3546/*
3547 * Convert inode from non-attributed to attributed.
3548 * Must not be in a transaction, ip must not be locked.
3549 */
3550int /* error code */
3551xfs_bmap_add_attrfork(
3552 xfs_inode_t *ip, /* incore inode pointer */
d8cc890d
NS
3553 int size, /* space new attribute needs */
3554 int rsvd) /* xact may use reserved blks */
1da177e4 3555{
1da177e4 3556 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
4eea22f0 3557 xfs_bmap_free_t flist; /* freed extent records */
1da177e4 3558 xfs_mount_t *mp; /* mount structure */
1da177e4 3559 xfs_trans_t *tp; /* transaction pointer */
d8cc890d
NS
3560 int blks; /* space reservation */
3561 int version = 1; /* superblock attr version */
3562 int committed; /* xaction was committed */
3563 int logflags; /* logging flags */
3564 int error; /* error return value */
1da177e4 3565
d8cc890d 3566 ASSERT(XFS_IFORK_Q(ip) == 0);
1da177e4
LT
3567 ASSERT(ip->i_df.if_ext_max ==
3568 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
d8cc890d 3569
1da177e4
LT
3570 mp = ip->i_mount;
3571 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3572 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3573 blks = XFS_ADDAFORK_SPACE_RES(mp);
3574 if (rsvd)
3575 tp->t_flags |= XFS_TRANS_RESERVE;
3576 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3577 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3578 goto error0;
3579 xfs_ilock(ip, XFS_ILOCK_EXCL);
7d095257 3580 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1da177e4
LT
3581 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3582 XFS_QMOPT_RES_REGBLKS);
3583 if (error) {
3584 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3585 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3586 return error;
3587 }
3588 if (XFS_IFORK_Q(ip))
3589 goto error1;
3590 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3591 /*
3592 * For inodes coming from pre-6.2 filesystems.
3593 */
3594 ASSERT(ip->i_d.di_aformat == 0);
3595 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3596 }
3597 ASSERT(ip->i_d.di_anextents == 0);
898621d5
CH
3598
3599 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
1da177e4 3600 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
898621d5 3601
1da177e4
LT
3602 switch (ip->i_d.di_format) {
3603 case XFS_DINODE_FMT_DEV:
3604 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3605 break;
3606 case XFS_DINODE_FMT_UUID:
3607 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3608 break;
3609 case XFS_DINODE_FMT_LOCAL:
3610 case XFS_DINODE_FMT_EXTENTS:
3611 case XFS_DINODE_FMT_BTREE:
d8cc890d
NS
3612 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3613 if (!ip->i_d.di_forkoff)
1a5902c5 3614 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
13059ff0 3615 else if (mp->m_flags & XFS_MOUNT_ATTR2)
d8cc890d 3616 version = 2;
1da177e4
LT
3617 break;
3618 default:
3619 ASSERT(0);
3620 error = XFS_ERROR(EINVAL);
3621 goto error1;
3622 }
3623 ip->i_df.if_ext_max =
3624 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3625 ASSERT(ip->i_afp == NULL);
3626 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3627 ip->i_afp->if_ext_max =
3628 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3629 ip->i_afp->if_flags = XFS_IFEXTENTS;
3630 logflags = 0;
9d87c319 3631 xfs_bmap_init(&flist, &firstblock);
1da177e4
LT
3632 switch (ip->i_d.di_format) {
3633 case XFS_DINODE_FMT_LOCAL:
3634 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3635 &logflags);
3636 break;
3637 case XFS_DINODE_FMT_EXTENTS:
3638 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3639 &flist, &logflags);
3640 break;
3641 case XFS_DINODE_FMT_BTREE:
3642 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3643 &logflags);
3644 break;
3645 default:
3646 error = 0;
3647 break;
3648 }
3649 if (logflags)
3650 xfs_trans_log_inode(tp, ip, logflags);
3651 if (error)
3652 goto error2;
62118709
ES
3653 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3654 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
da087bad
NS
3655 __int64_t sbfields = 0;
3656
3685c2a1 3657 spin_lock(&mp->m_sb_lock);
62118709
ES
3658 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3659 xfs_sb_version_addattr(&mp->m_sb);
da087bad 3660 sbfields |= XFS_SB_VERSIONNUM;
d8cc890d 3661 }
62118709
ES
3662 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3663 xfs_sb_version_addattr2(&mp->m_sb);
da087bad 3664 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
d8cc890d 3665 }
da087bad 3666 if (sbfields) {
3685c2a1 3667 spin_unlock(&mp->m_sb_lock);
da087bad 3668 xfs_mod_sb(tp, sbfields);
1da177e4 3669 } else
3685c2a1 3670 spin_unlock(&mp->m_sb_lock);
1da177e4 3671 }
f7c99b6f 3672 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
1da177e4 3673 goto error2;
713bf88b 3674 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
3675 ASSERT(ip->i_df.if_ext_max ==
3676 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3677 return error;
3678error2:
3679 xfs_bmap_cancel(&flist);
3680error1:
1da177e4
LT
3681 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3682error0:
3683 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3684 ASSERT(ip->i_df.if_ext_max ==
3685 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3686 return error;
3687}
3688
3689/*
3690 * Add the extent to the list of extents to be free at transaction end.
3691 * The list is maintained sorted (by block number).
3692 */
3693/* ARGSUSED */
3694void
3695xfs_bmap_add_free(
3696 xfs_fsblock_t bno, /* fs block number of extent */
3697 xfs_filblks_t len, /* length of extent */
3698 xfs_bmap_free_t *flist, /* list of extents */
3699 xfs_mount_t *mp) /* mount point structure */
3700{
3701 xfs_bmap_free_item_t *cur; /* current (next) element */
3702 xfs_bmap_free_item_t *new; /* new element */
3703 xfs_bmap_free_item_t *prev; /* previous element */
3704#ifdef DEBUG
3705 xfs_agnumber_t agno;
3706 xfs_agblock_t agbno;
3707
3708 ASSERT(bno != NULLFSBLOCK);
3709 ASSERT(len > 0);
3710 ASSERT(len <= MAXEXTLEN);
9d87c319 3711 ASSERT(!isnullstartblock(bno));
1da177e4
LT
3712 agno = XFS_FSB_TO_AGNO(mp, bno);
3713 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3714 ASSERT(agno < mp->m_sb.sb_agcount);
3715 ASSERT(agbno < mp->m_sb.sb_agblocks);
3716 ASSERT(len < mp->m_sb.sb_agblocks);
3717 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3718#endif
3719 ASSERT(xfs_bmap_free_item_zone != NULL);
3720 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3721 new->xbfi_startblock = bno;
3722 new->xbfi_blockcount = (xfs_extlen_t)len;
3723 for (prev = NULL, cur = flist->xbf_first;
3724 cur != NULL;
3725 prev = cur, cur = cur->xbfi_next) {
3726 if (cur->xbfi_startblock >= bno)
3727 break;
3728 }
3729 if (prev)
3730 prev->xbfi_next = new;
3731 else
3732 flist->xbf_first = new;
3733 new->xbfi_next = cur;
3734 flist->xbf_count++;
3735}
3736
3737/*
3738 * Compute and fill in the value of the maximum depth of a bmap btree
3739 * in this filesystem. Done once, during mount.
3740 */
3741void
3742xfs_bmap_compute_maxlevels(
3743 xfs_mount_t *mp, /* file system mount structure */
3744 int whichfork) /* data or attr fork */
3745{
3746 int level; /* btree level */
3747 uint maxblocks; /* max blocks at this level */
3748 uint maxleafents; /* max leaf entries possible */
3749 int maxrootrecs; /* max records in root block */
3750 int minleafrecs; /* min records in leaf block */
3751 int minnoderecs; /* min records in node block */
3752 int sz; /* root block size */
3753
3754 /*
3755 * The maximum number of extents in a file, hence the maximum
3756 * number of leaf entries, is controlled by the type of di_nextents
3757 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3758 * (a signed 16-bit number, xfs_aextnum_t).
6d1337b2
TS
3759 *
3760 * Note that we can no longer assume that if we are in ATTR1 that
1a5902c5
CH
3761 * the fork offset of all the inodes will be
3762 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3763 * with ATTR2 and then mounted back with ATTR1, keeping the
3764 * di_forkoff's fixed but probably at various positions. Therefore,
3765 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3766 * of a minimum size available.
1da177e4 3767 */
d8cc890d
NS
3768 if (whichfork == XFS_DATA_FORK) {
3769 maxleafents = MAXEXTNUM;
6d1337b2 3770 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
d8cc890d
NS
3771 } else {
3772 maxleafents = MAXAEXTNUM;
6d1337b2 3773 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
d8cc890d 3774 }
60197e8d 3775 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
1da177e4
LT
3776 minleafrecs = mp->m_bmap_dmnr[0];
3777 minnoderecs = mp->m_bmap_dmnr[1];
1da177e4
LT
3778 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3779 for (level = 1; maxblocks > 1; level++) {
3780 if (maxblocks <= maxrootrecs)
3781 maxblocks = 1;
3782 else
3783 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3784 }
3785 mp->m_bm_maxlevels[whichfork] = level;
3786}
3787
3788/*
3789 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3790 * caller. Frees all the extents that need freeing, which must be done
3791 * last due to locking considerations. We never free any extents in
3792 * the first transaction. This is to allow the caller to make the first
3793 * transaction a synchronous one so that the pointers to the data being
3794 * broken in this transaction will be permanent before the data is actually
3795 * freed. This is necessary to prevent blocks from being reallocated
3796 * and written to before the free and reallocation are actually permanent.
3797 * We do not just make the first transaction synchronous here, because
3798 * there are more efficient ways to gain the same protection in some cases
3799 * (see the file truncation code).
3800 *
3801 * Return 1 if the given transaction was committed and a new one
3802 * started, and 0 otherwise in the committed parameter.
3803 */
3804/*ARGSUSED*/
3805int /* error */
3806xfs_bmap_finish(
3807 xfs_trans_t **tp, /* transaction pointer addr */
3808 xfs_bmap_free_t *flist, /* i/o: list extents to free */
1da177e4
LT
3809 int *committed) /* xact committed or not */
3810{
3811 xfs_efd_log_item_t *efd; /* extent free data */
3812 xfs_efi_log_item_t *efi; /* extent free intention */
3813 int error; /* error return value */
4eea22f0 3814 xfs_bmap_free_item_t *free; /* free extent item */
1da177e4
LT
3815 unsigned int logres; /* new log reservation */
3816 unsigned int logcount; /* new log count */
3817 xfs_mount_t *mp; /* filesystem mount structure */
3818 xfs_bmap_free_item_t *next; /* next item on free list */
3819 xfs_trans_t *ntp; /* new transaction pointer */
3820
3821 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3822 if (flist->xbf_count == 0) {
3823 *committed = 0;
3824 return 0;
3825 }
3826 ntp = *tp;
3827 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3828 for (free = flist->xbf_first; free; free = free->xbfi_next)
3829 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3830 free->xbfi_blockcount);
3831 logres = ntp->t_log_res;
3832 logcount = ntp->t_log_count;
3833 ntp = xfs_trans_dup(*tp);
1c72bf90 3834 error = xfs_trans_commit(*tp, 0);
1da177e4
LT
3835 *tp = ntp;
3836 *committed = 1;
3837 /*
3838 * We have a new transaction, so we should return committed=1,
3839 * even though we're returning an error.
3840 */
cc09c0dc 3841 if (error)
1da177e4 3842 return error;
cc09c0dc
DC
3843
3844 /*
3845 * transaction commit worked ok so we can drop the extra ticket
3846 * reference that we gained in xfs_trans_dup()
3847 */
3848 xfs_log_ticket_put(ntp->t_ticket);
3849
1da177e4
LT
3850 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3851 logcount)))
3852 return error;
3853 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3854 for (free = flist->xbf_first; free != NULL; free = next) {
3855 next = free->xbfi_next;
3856 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3857 free->xbfi_blockcount))) {
3858 /*
3859 * The bmap free list will be cleaned up at a
3860 * higher level. The EFI will be canceled when
3861 * this transaction is aborted.
3862 * Need to force shutdown here to make sure it
3863 * happens, since this transaction may not be
3864 * dirty yet.
3865 */
3866 mp = ntp->t_mountp;
3867 if (!XFS_FORCED_SHUTDOWN(mp))
3868 xfs_force_shutdown(mp,
3869 (error == EFSCORRUPTED) ?
7d04a335
NS
3870 SHUTDOWN_CORRUPT_INCORE :
3871 SHUTDOWN_META_IO_ERROR);
1da177e4
LT
3872 return error;
3873 }
3874 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3875 free->xbfi_blockcount);
3876 xfs_bmap_del_free(flist, NULL, free);
3877 }
3878 return 0;
3879}
3880
3881/*
3882 * Free up any items left in the list.
3883 */
3884void
3885xfs_bmap_cancel(
3886 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3887{
3888 xfs_bmap_free_item_t *free; /* free list item */
3889 xfs_bmap_free_item_t *next;
3890
3891 if (flist->xbf_count == 0)
3892 return;
3893 ASSERT(flist->xbf_first != NULL);
3894 for (free = flist->xbf_first; free; free = next) {
3895 next = free->xbfi_next;
3896 xfs_bmap_del_free(flist, NULL, free);
3897 }
3898 ASSERT(flist->xbf_count == 0);
3899}
3900
3901/*
3902 * Returns the file-relative block number of the first unused block(s)
3903 * in the file with at least "len" logically contiguous blocks free.
3904 * This is the lowest-address hole if the file has holes, else the first block
3905 * past the end of file.
3906 * Return 0 if the file is currently local (in-inode).
3907 */
3908int /* error */
3909xfs_bmap_first_unused(
3910 xfs_trans_t *tp, /* transaction pointer */
3911 xfs_inode_t *ip, /* incore inode */
3912 xfs_extlen_t len, /* size of hole to find */
3913 xfs_fileoff_t *first_unused, /* unused block */
3914 int whichfork) /* data or attr fork */
3915{
1da177e4 3916 int error; /* error return value */
4eea22f0 3917 int idx; /* extent record index */
1da177e4
LT
3918 xfs_ifork_t *ifp; /* inode fork pointer */
3919 xfs_fileoff_t lastaddr; /* last block number seen */
3920 xfs_fileoff_t lowest; /* lowest useful block */
3921 xfs_fileoff_t max; /* starting useful block */
3922 xfs_fileoff_t off; /* offset for this block */
3923 xfs_extnum_t nextents; /* number of extent entries */
3924
3925 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3926 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3927 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3928 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3929 *first_unused = 0;
3930 return 0;
3931 }
3932 ifp = XFS_IFORK_PTR(ip, whichfork);
3933 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3934 (error = xfs_iread_extents(tp, ip, whichfork)))
3935 return error;
3936 lowest = *first_unused;
3937 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4eea22f0 3938 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
a6f64d4a 3939 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1da177e4
LT
3940 off = xfs_bmbt_get_startoff(ep);
3941 /*
3942 * See if the hole before this extent will work.
3943 */
3944 if (off >= lowest + len && off - max >= len) {
3945 *first_unused = max;
3946 return 0;
3947 }
3948 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3949 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3950 }
3951 *first_unused = max;
3952 return 0;
3953}
3954
3955/*
3956 * Returns the file-relative block number of the last block + 1 before
3957 * last_block (input value) in the file.
4eea22f0
MK
3958 * This is not based on i_size, it is based on the extent records.
3959 * Returns 0 for local files, as they do not have extent records.
1da177e4
LT
3960 */
3961int /* error */
3962xfs_bmap_last_before(
3963 xfs_trans_t *tp, /* transaction pointer */
3964 xfs_inode_t *ip, /* incore inode */
3965 xfs_fileoff_t *last_block, /* last block */
3966 int whichfork) /* data or attr fork */
3967{
3968 xfs_fileoff_t bno; /* input file offset */
3969 int eof; /* hit end of file */
a6f64d4a 3970 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1da177e4
LT
3971 int error; /* error return value */
3972 xfs_bmbt_irec_t got; /* current extent value */
3973 xfs_ifork_t *ifp; /* inode fork pointer */
3974 xfs_extnum_t lastx; /* last extent used */
3975 xfs_bmbt_irec_t prev; /* previous extent value */
3976
3977 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3978 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3979 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3980 return XFS_ERROR(EIO);
3981 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3982 *last_block = 0;
3983 return 0;
3984 }
3985 ifp = XFS_IFORK_PTR(ip, whichfork);
3986 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3987 (error = xfs_iread_extents(tp, ip, whichfork)))
3988 return error;
3989 bno = *last_block - 1;
3990 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3991 &prev);
3992 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3993 if (prev.br_startoff == NULLFILEOFF)
3994 *last_block = 0;
3995 else
3996 *last_block = prev.br_startoff + prev.br_blockcount;
3997 }
3998 /*
3999 * Otherwise *last_block is already the right answer.
4000 */
4001 return 0;
4002}
4003
4004/*
4005 * Returns the file-relative block number of the first block past eof in
4eea22f0
MK
4006 * the file. This is not based on i_size, it is based on the extent records.
4007 * Returns 0 for local files, as they do not have extent records.
1da177e4
LT
4008 */
4009int /* error */
4010xfs_bmap_last_offset(
4011 xfs_trans_t *tp, /* transaction pointer */
4012 xfs_inode_t *ip, /* incore inode */
4013 xfs_fileoff_t *last_block, /* last block */
4014 int whichfork) /* data or attr fork */
4015{
a6f64d4a 4016 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1da177e4
LT
4017 int error; /* error return value */
4018 xfs_ifork_t *ifp; /* inode fork pointer */
4019 xfs_extnum_t nextents; /* number of extent entries */
4020
4021 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4022 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4023 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4024 return XFS_ERROR(EIO);
4025 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4026 *last_block = 0;
4027 return 0;
4028 }
4029 ifp = XFS_IFORK_PTR(ip, whichfork);
4030 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4031 (error = xfs_iread_extents(tp, ip, whichfork)))
4032 return error;
4033 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4034 if (!nextents) {
4035 *last_block = 0;
4036 return 0;
4037 }
4eea22f0 4038 ep = xfs_iext_get_ext(ifp, nextents - 1);
1da177e4
LT
4039 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4040 return 0;
4041}
4042
4043/*
4044 * Returns whether the selected fork of the inode has exactly one
4045 * block or not. For the data fork we check this matches di_size,
4046 * implying the file's range is 0..bsize-1.
4047 */
4048int /* 1=>1 block, 0=>otherwise */
4049xfs_bmap_one_block(
4050 xfs_inode_t *ip, /* incore inode */
4051 int whichfork) /* data or attr fork */
4052{
a6f64d4a 4053 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1da177e4
LT
4054 xfs_ifork_t *ifp; /* inode fork pointer */
4055 int rval; /* return value */
4056 xfs_bmbt_irec_t s; /* internal version of extent */
4057
4058#ifndef DEBUG
ba87ea69
LM
4059 if (whichfork == XFS_DATA_FORK) {
4060 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4061 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4062 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4063 }
1da177e4
LT
4064#endif /* !DEBUG */
4065 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4066 return 0;
4067 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4068 return 0;
4069 ifp = XFS_IFORK_PTR(ip, whichfork);
4070 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4eea22f0 4071 ep = xfs_iext_get_ext(ifp, 0);
1da177e4
LT
4072 xfs_bmbt_get_all(ep, &s);
4073 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4074 if (rval && whichfork == XFS_DATA_FORK)
ba87ea69 4075 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
1da177e4
LT
4076 return rval;
4077}
4078
4e8938fe
CH
4079STATIC int
4080xfs_bmap_sanity_check(
4081 struct xfs_mount *mp,
4082 struct xfs_buf *bp,
4083 int level)
4084{
4085 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4086
4087 if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
4088 be16_to_cpu(block->bb_level) != level ||
4089 be16_to_cpu(block->bb_numrecs) == 0 ||
4090 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4091 return 0;
4092 return 1;
4093}
4094
1da177e4
LT
4095/*
4096 * Read in the extents to if_extents.
4097 * All inode fields are set up by caller, we just traverse the btree
4098 * and copy the records in. If the file system cannot contain unwritten
4099 * extents, the records are checked for no "state" flags.
4100 */
4101int /* error */
4102xfs_bmap_read_extents(
4103 xfs_trans_t *tp, /* transaction pointer */
4104 xfs_inode_t *ip, /* incore inode */
4105 int whichfork) /* data or attr fork */
4106{
7cc95a82 4107 struct xfs_btree_block *block; /* current btree block */
1da177e4
LT
4108 xfs_fsblock_t bno; /* block # of "block" */
4109 xfs_buf_t *bp; /* buffer for "block" */
4110 int error; /* error return value */
4111 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1da177e4
LT
4112 xfs_extnum_t i, j; /* index into the extents list */
4113 xfs_ifork_t *ifp; /* fork structure */
4114 int level; /* btree level, for checking */
4115 xfs_mount_t *mp; /* file system mount structure */
576039cf 4116 __be64 *pp; /* pointer to block address */
1da177e4
LT
4117 /* REFERENCED */
4118 xfs_extnum_t room; /* number of entries there's room for */
1da177e4
LT
4119
4120 bno = NULLFSBLOCK;
4121 mp = ip->i_mount;
4122 ifp = XFS_IFORK_PTR(ip, whichfork);
4123 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4124 XFS_EXTFMT_INODE(ip);
4125 block = ifp->if_broot;
4126 /*
4127 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4128 */
16259e7d
CH
4129 level = be16_to_cpu(block->bb_level);
4130 ASSERT(level > 0);
60197e8d 4131 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
576039cf
CH
4132 bno = be64_to_cpu(*pp);
4133 ASSERT(bno != NULLDFSBNO);
4134 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4135 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1da177e4
LT
4136 /*
4137 * Go down the tree until leaf level is reached, following the first
4138 * pointer (leftmost) at each level.
4139 */
4140 while (level-- > 0) {
4141 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4142 XFS_BMAP_BTREE_REF)))
4143 return error;
7cc95a82 4144 block = XFS_BUF_TO_BLOCK(bp);
1da177e4 4145 XFS_WANT_CORRUPTED_GOTO(
4e8938fe 4146 xfs_bmap_sanity_check(mp, bp, level),
1da177e4
LT
4147 error0);
4148 if (level == 0)
4149 break;
136341b4 4150 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
576039cf
CH
4151 bno = be64_to_cpu(*pp);
4152 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
1da177e4
LT
4153 xfs_trans_brelse(tp, bp);
4154 }
4155 /*
4156 * Here with bp and block set to the leftmost leaf node in the tree.
4157 */
4eea22f0 4158 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1da177e4
LT
4159 i = 0;
4160 /*
4eea22f0 4161 * Loop over all leaf nodes. Copy information to the extent records.
1da177e4
LT
4162 */
4163 for (;;) {
a6f64d4a 4164 xfs_bmbt_rec_t *frp;
1da177e4
LT
4165 xfs_fsblock_t nextbno;
4166 xfs_extnum_t num_recs;
4eea22f0 4167 xfs_extnum_t start;
1da177e4
LT
4168
4169
7cc95a82 4170 num_recs = xfs_btree_get_numrecs(block);
1da177e4
LT
4171 if (unlikely(i + num_recs > room)) {
4172 ASSERT(i + num_recs <= room);
3762ec6b
NS
4173 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4174 "corrupt dinode %Lu, (btree extents).",
1da177e4
LT
4175 (unsigned long long) ip->i_ino);
4176 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4177 XFS_ERRLEVEL_LOW,
4178 ip->i_mount);
4179 goto error0;
4180 }
4181 XFS_WANT_CORRUPTED_GOTO(
4e8938fe 4182 xfs_bmap_sanity_check(mp, bp, 0),
1da177e4
LT
4183 error0);
4184 /*
4185 * Read-ahead the next leaf block, if any.
4186 */
7cc95a82 4187 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1da177e4
LT
4188 if (nextbno != NULLFSBLOCK)
4189 xfs_btree_reada_bufl(mp, nextbno, 1);
4190 /*
4eea22f0 4191 * Copy records into the extent records.
1da177e4 4192 */
136341b4 4193 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
4eea22f0
MK
4194 start = i;
4195 for (j = 0; j < num_recs; j++, i++, frp++) {
a6f64d4a 4196 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
cd8b0a97
CH
4197 trp->l0 = be64_to_cpu(frp->l0);
4198 trp->l1 = be64_to_cpu(frp->l1);
1da177e4
LT
4199 }
4200 if (exntf == XFS_EXTFMT_NOSTATE) {
4201 /*
4202 * Check all attribute bmap btree records and
4203 * any "older" data bmap btree records for a
4204 * set bit in the "extent flag" position.
4205 */
4eea22f0
MK
4206 if (unlikely(xfs_check_nostate_extents(ifp,
4207 start, num_recs))) {
1da177e4
LT
4208 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4209 XFS_ERRLEVEL_LOW,
4210 ip->i_mount);
4211 goto error0;
4212 }
4213 }
1da177e4
LT
4214 xfs_trans_brelse(tp, bp);
4215 bno = nextbno;
4216 /*
4217 * If we've reached the end, stop.
4218 */
4219 if (bno == NULLFSBLOCK)
4220 break;
4221 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4222 XFS_BMAP_BTREE_REF)))
4223 return error;
7cc95a82 4224 block = XFS_BUF_TO_BLOCK(bp);
1da177e4 4225 }
4eea22f0 4226 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1da177e4 4227 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
3a59c94c 4228 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1da177e4
LT
4229 return 0;
4230error0:
4231 xfs_trans_brelse(tp, bp);
4232 return XFS_ERROR(EFSCORRUPTED);
4233}
4234
0b1b213f 4235#ifdef DEBUG
1da177e4 4236/*
4eea22f0 4237 * Add bmap trace insert entries for all the contents of the extent records.
1da177e4
LT
4238 */
4239void
4240xfs_bmap_trace_exlist(
1da177e4
LT
4241 xfs_inode_t *ip, /* incore inode pointer */
4242 xfs_extnum_t cnt, /* count of entries in the list */
0b1b213f
CH
4243 int whichfork, /* data or attr fork */
4244 unsigned long caller_ip)
1da177e4 4245{
4eea22f0 4246 xfs_extnum_t idx; /* extent record index */
1da177e4 4247 xfs_ifork_t *ifp; /* inode fork pointer */
0b1b213f
CH
4248 int state = 0;
4249
4250 if (whichfork == XFS_ATTR_FORK)
4251 state |= BMAP_ATTRFORK;
1da177e4
LT
4252
4253 ifp = XFS_IFORK_PTR(ip, whichfork);
4eea22f0 4254 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
0b1b213f
CH
4255 for (idx = 0; idx < cnt; idx++)
4256 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
1da177e4 4257}
1da177e4 4258
1da177e4
LT
4259/*
4260 * Validate that the bmbt_irecs being returned from bmapi are valid
4261 * given the callers original parameters. Specifically check the
4262 * ranges of the returned irecs to ensure that they only extent beyond
4263 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4264 */
4265STATIC void
4266xfs_bmap_validate_ret(
4267 xfs_fileoff_t bno,
4268 xfs_filblks_t len,
4269 int flags,
4270 xfs_bmbt_irec_t *mval,
4271 int nmap,
4272 int ret_nmap)
4273{
4274 int i; /* index to map values */
4275
4276 ASSERT(ret_nmap <= nmap);
4277
4278 for (i = 0; i < ret_nmap; i++) {
4279 ASSERT(mval[i].br_blockcount > 0);
4280 if (!(flags & XFS_BMAPI_ENTIRE)) {
4281 ASSERT(mval[i].br_startoff >= bno);
4282 ASSERT(mval[i].br_blockcount <= len);
4283 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4284 bno + len);
4285 } else {
4286 ASSERT(mval[i].br_startoff < bno + len);
4287 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4288 bno);
4289 }
4290 ASSERT(i == 0 ||
4291 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4292 mval[i].br_startoff);
4293 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4294 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4295 mval[i].br_startblock != HOLESTARTBLOCK);
4296 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4297 mval[i].br_state == XFS_EXT_UNWRITTEN);
4298 }
4299}
4300#endif /* DEBUG */
4301
4302
4303/*
4304 * Map file blocks to filesystem blocks.
4305 * File range is given by the bno/len pair.
4306 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4307 * into a hole or past eof.
4308 * Only allocates blocks from a single allocation group,
4309 * to avoid locking problems.
4310 * The returned value in "firstblock" from the first call in a transaction
4311 * must be remembered and presented to subsequent calls in "firstblock".
4312 * An upper bound for the number of blocks to be allocated is supplied to
4313 * the first call in "total"; if no allocation group has that many free
4314 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4315 */
4316int /* error */
4317xfs_bmapi(
4318 xfs_trans_t *tp, /* transaction pointer */
4319 xfs_inode_t *ip, /* incore inode */
4320 xfs_fileoff_t bno, /* starting file offs. mapped */
4321 xfs_filblks_t len, /* length to map in file */
4322 int flags, /* XFS_BMAPI_... */
4323 xfs_fsblock_t *firstblock, /* first allocated block
4324 controls a.g. for allocs */
4325 xfs_extlen_t total, /* total blocks needed */
4326 xfs_bmbt_irec_t *mval, /* output: map values */
4327 int *nmap, /* i/o: mval size/count */
b4e9181e 4328 xfs_bmap_free_t *flist) /* i/o: list extents to free */
1da177e4
LT
4329{
4330 xfs_fsblock_t abno; /* allocated block number */
4331 xfs_extlen_t alen; /* allocated extent length */
4332 xfs_fileoff_t aoff; /* allocated file offset */
f0a0eaa8 4333 xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */
1da177e4 4334 xfs_btree_cur_t *cur; /* bmap btree cursor */
1da177e4 4335 xfs_fileoff_t end; /* end of mapped file region */
4eea22f0 4336 int eof; /* we've hit the end of extents */
a6f64d4a 4337 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1da177e4 4338 int error; /* error return */
4eea22f0 4339 xfs_bmbt_irec_t got; /* current file extent record */
1da177e4
LT
4340 xfs_ifork_t *ifp; /* inode fork pointer */
4341 xfs_extlen_t indlen; /* indirect blocks length */
1da177e4
LT
4342 xfs_extnum_t lastx; /* last useful extent number */
4343 int logflags; /* flags for transaction logging */
4344 xfs_extlen_t minleft; /* min blocks left after allocation */
4345 xfs_extlen_t minlen; /* min allocation size */
4346 xfs_mount_t *mp; /* xfs mount structure */
4347 int n; /* current extent index */
9da096fd 4348 int nallocs; /* number of extents alloc'd */
1da177e4
LT
4349 xfs_extnum_t nextents; /* number of extents in file */
4350 xfs_fileoff_t obno; /* old block number (offset) */
4eea22f0 4351 xfs_bmbt_irec_t prev; /* previous file extent record */
1da177e4 4352 int tmp_logflags; /* temp flags holder */
06d10dd9
NS
4353 int whichfork; /* data or attr fork */
4354 char inhole; /* current location is hole in file */
1da177e4 4355 char wasdelay; /* old extent was delayed */
1da177e4 4356 char wr; /* this is a write request */
06d10dd9 4357 char rt; /* this is a realtime file */
1da177e4
LT
4358#ifdef DEBUG
4359 xfs_fileoff_t orig_bno; /* original block number value */
4360 int orig_flags; /* original flags arg value */
4361 xfs_filblks_t orig_len; /* original value of len arg */
4362 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4363 int orig_nmap; /* original value of *nmap */
4364
4365 orig_bno = bno;
4366 orig_len = len;
4367 orig_flags = flags;
4368 orig_mval = mval;
4369 orig_nmap = *nmap;
4370#endif
4371 ASSERT(*nmap >= 1);
4372 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4373 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4374 XFS_ATTR_FORK : XFS_DATA_FORK;
4375 mp = ip->i_mount;
4376 if (unlikely(XFS_TEST_ERROR(
4377 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4378 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4379 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4380 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4381 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4382 return XFS_ERROR(EFSCORRUPTED);
4383 }
4384 if (XFS_FORCED_SHUTDOWN(mp))
4385 return XFS_ERROR(EIO);
dd9f438e 4386 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
1da177e4
LT
4387 ifp = XFS_IFORK_PTR(ip, whichfork);
4388 ASSERT(ifp->if_ext_max ==
4389 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4390 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4391 XFS_STATS_INC(xs_blk_mapw);
4392 else
4393 XFS_STATS_INC(xs_blk_mapr);
1da177e4 4394 /*
39269e29 4395 * IGSTATE flag is used to combine extents which
1da177e4
LT
4396 * differ only due to the state of the extents.
4397 * This technique is used from xfs_getbmap()
4398 * when the caller does not wish to see the
4399 * separation (which is the default).
4400 *
4401 * This technique is also used when writing a
4402 * buffer which has been partially written,
4403 * (usually by being flushed during a chunkread),
4404 * to ensure one write takes place. This also
4405 * prevents a change in the xfs inode extents at
4406 * this time, intentionally. This change occurs
4407 * on completion of the write operation, in
4408 * xfs_strat_comp(), where the xfs_bmapi() call
4409 * is transactioned, and the extents combined.
4410 */
39269e29
NS
4411 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4412 wr = 0; /* no allocations are allowed */
4413 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
1da177e4
LT
4414 logflags = 0;
4415 nallocs = 0;
4416 cur = NULL;
4417 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4418 ASSERT(wr && tp);
4419 if ((error = xfs_bmap_local_to_extents(tp, ip,
4420 firstblock, total, &logflags, whichfork)))
4421 goto error0;
4422 }
4423 if (wr && *firstblock == NULLFSBLOCK) {
4424 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
16259e7d 4425 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
1da177e4
LT
4426 else
4427 minleft = 1;
4428 } else
4429 minleft = 0;
4430 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4431 (error = xfs_iread_extents(tp, ip, whichfork)))
4432 goto error0;
4433 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4434 &prev);
4435 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4436 n = 0;
4437 end = bno + len;
4438 obno = bno;
4439 bma.ip = NULL;
b4e9181e 4440
1da177e4
LT
4441 while (bno < end && n < *nmap) {
4442 /*
4443 * Reading past eof, act as though there's a hole
4444 * up to end.
4445 */
4446 if (eof && !wr)
4447 got.br_startoff = end;
4448 inhole = eof || got.br_startoff > bno;
39269e29 4449 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
9d87c319 4450 isnullstartblock(got.br_startblock);
1da177e4
LT
4451 /*
4452 * First, deal with the hole before the allocated space
4453 * that we found, if any.
4454 */
4455 if (wr && (inhole || wasdelay)) {
4456 /*
4457 * For the wasdelay case, we could also just
4458 * allocate the stuff asked for in this bmap call
4459 * but that wouldn't be as good.
4460 */
cd8b0bb3 4461 if (wasdelay) {
1da177e4
LT
4462 alen = (xfs_extlen_t)got.br_blockcount;
4463 aoff = got.br_startoff;
4464 if (lastx != NULLEXTNUM && lastx) {
4eea22f0 4465 ep = xfs_iext_get_ext(ifp, lastx - 1);
1da177e4
LT
4466 xfs_bmbt_get_all(ep, &prev);
4467 }
1da177e4
LT
4468 } else {
4469 alen = (xfs_extlen_t)
4470 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4471 if (!eof)
4472 alen = (xfs_extlen_t)
4473 XFS_FILBLKS_MIN(alen,
4474 got.br_startoff - bno);
4475 aoff = bno;
4476 }
39269e29
NS
4477 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4478 if (flags & XFS_BMAPI_DELAY) {
dd9f438e 4479 xfs_extlen_t extsz;
06d10dd9
NS
4480
4481 /* Figure out the extent size, adjust alen */
957d0ebe 4482 extsz = xfs_get_extsz_hint(ip);
dd9f438e
NS
4483 if (extsz) {
4484 error = xfs_bmap_extsize_align(mp,
4485 &got, &prev, extsz,
39269e29
NS
4486 rt, eof,
4487 flags&XFS_BMAPI_DELAY,
4488 flags&XFS_BMAPI_CONVERT,
dd9f438e
NS
4489 &aoff, &alen);
4490 ASSERT(!error);
06d10dd9
NS
4491 }
4492
dd9f438e
NS
4493 if (rt)
4494 extsz = alen / mp->m_sb.sb_rextsize;
4495
1da177e4
LT
4496 /*
4497 * Make a transaction-less quota reservation for
4498 * delayed allocation blocks. This number gets
9a2a7de2
NS
4499 * adjusted later. We return if we haven't
4500 * allocated blocks already inside this loop.
1da177e4 4501 */
7d095257
CH
4502 error = xfs_trans_reserve_quota_nblks(
4503 NULL, ip, (long)alen, 0,
06d10dd9 4504 rt ? XFS_QMOPT_RES_RTBLKS :
7d095257
CH
4505 XFS_QMOPT_RES_REGBLKS);
4506 if (error) {
1da177e4
LT
4507 if (n == 0) {
4508 *nmap = 0;
4509 ASSERT(cur == NULL);
9a2a7de2 4510 return error;
1da177e4
LT
4511 }
4512 break;
4513 }
4514
4515 /*
4516 * Split changing sb for alen and indlen since
4517 * they could be coming from different places.
4518 */
06d10dd9
NS
4519 indlen = (xfs_extlen_t)
4520 xfs_bmap_worst_indlen(ip, alen);
4521 ASSERT(indlen > 0);
1da177e4 4522
dd9f438e 4523 if (rt) {
06d10dd9
NS
4524 error = xfs_mod_incore_sb(mp,
4525 XFS_SBS_FREXTENTS,
20f4ebf2 4526 -((int64_t)extsz), (flags &
39269e29 4527 XFS_BMAPI_RSVBLOCKS));
dd9f438e 4528 } else {
06d10dd9
NS
4529 error = xfs_mod_incore_sb(mp,
4530 XFS_SBS_FDBLOCKS,
20f4ebf2 4531 -((int64_t)alen), (flags &
39269e29 4532 XFS_BMAPI_RSVBLOCKS));
dd9f438e 4533 }
3bdbfb10 4534 if (!error) {
06d10dd9
NS
4535 error = xfs_mod_incore_sb(mp,
4536 XFS_SBS_FDBLOCKS,
20f4ebf2 4537 -((int64_t)indlen), (flags &
39269e29 4538 XFS_BMAPI_RSVBLOCKS));
3ddb8fa9
NS
4539 if (error && rt)
4540 xfs_mod_incore_sb(mp,
3bdbfb10 4541 XFS_SBS_FREXTENTS,
20f4ebf2 4542 (int64_t)extsz, (flags &
39269e29 4543 XFS_BMAPI_RSVBLOCKS));
3ddb8fa9
NS
4544 else if (error)
4545 xfs_mod_incore_sb(mp,
3bdbfb10 4546 XFS_SBS_FDBLOCKS,
20f4ebf2 4547 (int64_t)alen, (flags &
39269e29 4548 XFS_BMAPI_RSVBLOCKS));
3bdbfb10 4549 }
06d10dd9
NS
4550
4551 if (error) {
3ddb8fa9 4552 if (XFS_IS_QUOTA_ON(mp))
06d10dd9 4553 /* unreserve the blocks now */
dd9f438e 4554 (void)
7d095257
CH
4555 xfs_trans_unreserve_quota_nblks(
4556 NULL, ip,
06d10dd9
NS
4557 (long)alen, 0, rt ?
4558 XFS_QMOPT_RES_RTBLKS :
4559 XFS_QMOPT_RES_REGBLKS);
1da177e4
LT
4560 break;
4561 }
06d10dd9 4562
1da177e4 4563 ip->i_delayed_blks += alen;
9d87c319 4564 abno = nullstartblock(indlen);
1da177e4
LT
4565 } else {
4566 /*
4567 * If first time, allocate and fill in
4568 * once-only bma fields.
4569 */
4570 if (bma.ip == NULL) {
4571 bma.tp = tp;
4572 bma.ip = ip;
4573 bma.prevp = &prev;
4574 bma.gotp = &got;
4575 bma.total = total;
4576 bma.userdata = 0;
4577 }
4578 /* Indicate if this is the first user data
4579 * in the file, or just any user data.
4580 */
39269e29 4581 if (!(flags & XFS_BMAPI_METADATA)) {
1da177e4
LT
4582 bma.userdata = (aoff == 0) ?
4583 XFS_ALLOC_INITIAL_USER_DATA :
4584 XFS_ALLOC_USERDATA;
4585 }
4586 /*
4587 * Fill in changeable bma fields.
4588 */
4589 bma.eof = eof;
4590 bma.firstblock = *firstblock;
4591 bma.alen = alen;
4592 bma.off = aoff;
7288026b 4593 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
1da177e4
LT
4594 bma.wasdel = wasdelay;
4595 bma.minlen = minlen;
4596 bma.low = flist->xbf_low;
4597 bma.minleft = minleft;
4598 /*
4599 * Only want to do the alignment at the
4600 * eof if it is userdata and allocation length
4601 * is larger than a stripe unit.
4602 */
4603 if (mp->m_dalign && alen >= mp->m_dalign &&
39269e29
NS
4604 (!(flags & XFS_BMAPI_METADATA)) &&
4605 (whichfork == XFS_DATA_FORK)) {
1da177e4
LT
4606 if ((error = xfs_bmap_isaeof(ip, aoff,
4607 whichfork, &bma.aeof)))
4608 goto error0;
4609 } else
4610 bma.aeof = 0;
4611 /*
4612 * Call allocator.
4613 */
4614 if ((error = xfs_bmap_alloc(&bma)))
4615 goto error0;
4616 /*
4617 * Copy out result fields.
4618 */
4619 abno = bma.rval;
4620 if ((flist->xbf_low = bma.low))
4621 minleft = 0;
4622 alen = bma.alen;
4623 aoff = bma.off;
4624 ASSERT(*firstblock == NULLFSBLOCK ||
4625 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4626 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4627 (flist->xbf_low &&
4628 XFS_FSB_TO_AGNO(mp, *firstblock) <
4629 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4630 *firstblock = bma.firstblock;
4631 if (cur)
4632 cur->bc_private.b.firstblock =
4633 *firstblock;
4634 if (abno == NULLFSBLOCK)
4635 break;
4636 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
561f7d17 4637 cur = xfs_bmbt_init_cursor(mp, tp,
1da177e4
LT
4638 ip, whichfork);
4639 cur->bc_private.b.firstblock =
4640 *firstblock;
4641 cur->bc_private.b.flist = flist;
4642 }
4643 /*
4644 * Bump the number of extents we've allocated
4645 * in this call.
4646 */
4647 nallocs++;
4648 }
4649 if (cur)
4650 cur->bc_private.b.flags =
4651 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4652 got.br_startoff = aoff;
4653 got.br_startblock = abno;
4654 got.br_blockcount = alen;
4655 got.br_state = XFS_EXT_NORM; /* assume normal */
4656 /*
4657 * Determine state of extent, and the filesystem.
4658 * A wasdelay extent has been initialized, so
4659 * shouldn't be flagged as unwritten.
4660 */
62118709 4661 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
1da177e4
LT
4662 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
4663 got.br_state = XFS_EXT_UNWRITTEN;
4664 }
4665 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
b4e9181e 4666 firstblock, flist, &tmp_logflags,
3e57ecf6 4667 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
1da177e4
LT
4668 logflags |= tmp_logflags;
4669 if (error)
4670 goto error0;
4671 lastx = ifp->if_lastex;
4eea22f0 4672 ep = xfs_iext_get_ext(ifp, lastx);
1da177e4
LT
4673 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4674 xfs_bmbt_get_all(ep, &got);
4675 ASSERT(got.br_startoff <= aoff);
4676 ASSERT(got.br_startoff + got.br_blockcount >=
4677 aoff + alen);
4678#ifdef DEBUG
39269e29 4679 if (flags & XFS_BMAPI_DELAY) {
9d87c319
ES
4680 ASSERT(isnullstartblock(got.br_startblock));
4681 ASSERT(startblockval(got.br_startblock) > 0);
1da177e4
LT
4682 }
4683 ASSERT(got.br_state == XFS_EXT_NORM ||
4684 got.br_state == XFS_EXT_UNWRITTEN);
4685#endif
4686 /*
4687 * Fall down into the found allocated space case.
4688 */
4689 } else if (inhole) {
4690 /*
4691 * Reading in a hole.
4692 */
4693 mval->br_startoff = bno;
4694 mval->br_startblock = HOLESTARTBLOCK;
4695 mval->br_blockcount =
4696 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4697 mval->br_state = XFS_EXT_NORM;
4698 bno += mval->br_blockcount;
4699 len -= mval->br_blockcount;
4700 mval++;
4701 n++;
4702 continue;
4703 }
4704 /*
4705 * Then deal with the allocated space we found.
4706 */
4707 ASSERT(ep != NULL);
39269e29
NS
4708 if (!(flags & XFS_BMAPI_ENTIRE) &&
4709 (got.br_startoff + got.br_blockcount > obno)) {
1da177e4
LT
4710 if (obno > bno)
4711 bno = obno;
4712 ASSERT((bno >= obno) || (n == 0));
4713 ASSERT(bno < end);
4714 mval->br_startoff = bno;
9d87c319 4715 if (isnullstartblock(got.br_startblock)) {
39269e29 4716 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
1da177e4
LT
4717 mval->br_startblock = DELAYSTARTBLOCK;
4718 } else
4719 mval->br_startblock =
4720 got.br_startblock +
4721 (bno - got.br_startoff);
4722 /*
4723 * Return the minimum of what we got and what we
4724 * asked for for the length. We can use the len
4725 * variable here because it is modified below
4726 * and we could have been there before coming
4727 * here if the first part of the allocation
4728 * didn't overlap what was asked for.
4729 */
4730 mval->br_blockcount =
4731 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
4732 (bno - got.br_startoff));
4733 mval->br_state = got.br_state;
4734 ASSERT(mval->br_blockcount <= len);
4735 } else {
4736 *mval = got;
9d87c319 4737 if (isnullstartblock(mval->br_startblock)) {
39269e29 4738 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
1da177e4
LT
4739 mval->br_startblock = DELAYSTARTBLOCK;
4740 }
4741 }
4742
4743 /*
4744 * Check if writing previously allocated but
4745 * unwritten extents.
4746 */
4747 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
4748 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
4749 /*
4750 * Modify (by adding) the state flag, if writing.
4751 */
4752 ASSERT(mval->br_blockcount <= len);
4753 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
561f7d17
CH
4754 cur = xfs_bmbt_init_cursor(mp,
4755 tp, ip, whichfork);
1da177e4
LT
4756 cur->bc_private.b.firstblock =
4757 *firstblock;
4758 cur->bc_private.b.flist = flist;
4759 }
4760 mval->br_state = XFS_EXT_NORM;
4761 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
b4e9181e 4762 firstblock, flist, &tmp_logflags,
3e57ecf6 4763 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
1da177e4
LT
4764 logflags |= tmp_logflags;
4765 if (error)
4766 goto error0;
4767 lastx = ifp->if_lastex;
4eea22f0 4768 ep = xfs_iext_get_ext(ifp, lastx);
1da177e4
LT
4769 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4770 xfs_bmbt_get_all(ep, &got);
4771 /*
4772 * We may have combined previously unwritten
4773 * space with written space, so generate
4774 * another request.
4775 */
4776 if (mval->br_blockcount < len)
4777 continue;
4778 }
4779
39269e29 4780 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
1da177e4 4781 ((mval->br_startoff + mval->br_blockcount) <= end));
39269e29
NS
4782 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4783 (mval->br_blockcount <= len) ||
1da177e4
LT
4784 (mval->br_startoff < obno));
4785 bno = mval->br_startoff + mval->br_blockcount;
4786 len = end - bno;
4787 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4788 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4789 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4790 ASSERT(mval->br_state == mval[-1].br_state);
4791 mval[-1].br_blockcount = mval->br_blockcount;
4792 mval[-1].br_state = mval->br_state;
4793 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4794 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4795 mval[-1].br_startblock != HOLESTARTBLOCK &&
4796 mval->br_startblock ==
4797 mval[-1].br_startblock + mval[-1].br_blockcount &&
39269e29
NS
4798 ((flags & XFS_BMAPI_IGSTATE) ||
4799 mval[-1].br_state == mval->br_state)) {
1da177e4
LT
4800 ASSERT(mval->br_startoff ==
4801 mval[-1].br_startoff + mval[-1].br_blockcount);
4802 mval[-1].br_blockcount += mval->br_blockcount;
4803 } else if (n > 0 &&
4804 mval->br_startblock == DELAYSTARTBLOCK &&
4805 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4806 mval->br_startoff ==
4807 mval[-1].br_startoff + mval[-1].br_blockcount) {
4808 mval[-1].br_blockcount += mval->br_blockcount;
4809 mval[-1].br_state = mval->br_state;
4810 } else if (!((n == 0) &&
4811 ((mval->br_startoff + mval->br_blockcount) <=
4812 obno))) {
4813 mval++;
4814 n++;
4815 }
4816 /*
4817 * If we're done, stop now. Stop when we've allocated
4818 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4819 * the transaction may get too big.
4820 */
4821 if (bno >= end || n >= *nmap || nallocs >= *nmap)
4822 break;
4823 /*
4824 * Else go on to the next record.
4825 */
4eea22f0 4826 ep = xfs_iext_get_ext(ifp, ++lastx);
4e5ae838
DC
4827 prev = got;
4828 if (lastx >= nextents)
1da177e4 4829 eof = 1;
4e5ae838 4830 else
1da177e4
LT
4831 xfs_bmbt_get_all(ep, &got);
4832 }
4833 ifp->if_lastex = lastx;
4834 *nmap = n;
4835 /*
4836 * Transform from btree to extents, give it cur.
4837 */
4838 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4839 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4840 ASSERT(wr && cur);
4841 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4842 &tmp_logflags, whichfork);
4843 logflags |= tmp_logflags;
4844 if (error)
4845 goto error0;
4846 }
4847 ASSERT(ifp->if_ext_max ==
4848 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4849 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4850 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
4851 error = 0;
1da177e4
LT
4852error0:
4853 /*
4854 * Log everything. Do this after conversion, there's no point in
4eea22f0 4855 * logging the extent records if we've converted to btree format.
1da177e4 4856 */
9d87c319 4857 if ((logflags & xfs_ilog_fext(whichfork)) &&
1da177e4 4858 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
9d87c319
ES
4859 logflags &= ~xfs_ilog_fext(whichfork);
4860 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
1da177e4 4861 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
9d87c319 4862 logflags &= ~xfs_ilog_fbroot(whichfork);
1da177e4
LT
4863 /*
4864 * Log whatever the flags say, even if error. Otherwise we might miss
4865 * detecting a case where the data is changed, there's an error,
4866 * and it's not logged so we don't shutdown when we should.
4867 */
4868 if (logflags) {
4869 ASSERT(tp && wr);
4870 xfs_trans_log_inode(tp, ip, logflags);
4871 }
4872 if (cur) {
4873 if (!error) {
4874 ASSERT(*firstblock == NULLFSBLOCK ||
4875 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4876 XFS_FSB_TO_AGNO(mp,
4877 cur->bc_private.b.firstblock) ||
4878 (flist->xbf_low &&
4879 XFS_FSB_TO_AGNO(mp, *firstblock) <
4880 XFS_FSB_TO_AGNO(mp,
4881 cur->bc_private.b.firstblock)));
4882 *firstblock = cur->bc_private.b.firstblock;
4883 }
4884 xfs_btree_del_cursor(cur,
4885 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4886 }
4887 if (!error)
4888 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4889 orig_nmap, *nmap);
4890 return error;
4891}
4892
4893/*
4894 * Map file blocks to filesystem blocks, simple version.
4895 * One block (extent) only, read-only.
4896 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
4897 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
4898 * was set and all the others were clear.
4899 */
4900int /* error */
4901xfs_bmapi_single(
4902 xfs_trans_t *tp, /* transaction pointer */
4903 xfs_inode_t *ip, /* incore inode */
4904 int whichfork, /* data or attr fork */
4905 xfs_fsblock_t *fsb, /* output: mapped block */
4906 xfs_fileoff_t bno) /* starting file offs. mapped */
4907{
4eea22f0 4908 int eof; /* we've hit the end of extents */
1da177e4 4909 int error; /* error return */
4eea22f0 4910 xfs_bmbt_irec_t got; /* current file extent record */
1da177e4
LT
4911 xfs_ifork_t *ifp; /* inode fork pointer */
4912 xfs_extnum_t lastx; /* last useful extent number */
4eea22f0 4913 xfs_bmbt_irec_t prev; /* previous file extent record */
1da177e4
LT
4914
4915 ifp = XFS_IFORK_PTR(ip, whichfork);
4916 if (unlikely(
4917 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4918 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
4919 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
4920 ip->i_mount);
4921 return XFS_ERROR(EFSCORRUPTED);
4922 }
4923 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
4924 return XFS_ERROR(EIO);
4925 XFS_STATS_INC(xs_blk_mapr);
4926 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4927 (error = xfs_iread_extents(tp, ip, whichfork)))
4928 return error;
4929 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4930 &prev);
4931 /*
4932 * Reading past eof, act as though there's a hole
4933 * up to end.
4934 */
4935 if (eof || got.br_startoff > bno) {
4936 *fsb = NULLFSBLOCK;
4937 return 0;
4938 }
9d87c319 4939 ASSERT(!isnullstartblock(got.br_startblock));
1da177e4
LT
4940 ASSERT(bno < got.br_startoff + got.br_blockcount);
4941 *fsb = got.br_startblock + (bno - got.br_startoff);
4942 ifp->if_lastex = lastx;
4943 return 0;
4944}
4945
4946/*
4947 * Unmap (remove) blocks from a file.
4948 * If nexts is nonzero then the number of extents to remove is limited to
4949 * that value. If not all extents in the block range can be removed then
4950 * *done is set.
4951 */
4952int /* error */
4953xfs_bunmapi(
4954 xfs_trans_t *tp, /* transaction pointer */
4955 struct xfs_inode *ip, /* incore inode */
4956 xfs_fileoff_t bno, /* starting offset to unmap */
4957 xfs_filblks_t len, /* length to unmap in file */
4958 int flags, /* misc flags */
4959 xfs_extnum_t nexts, /* number of extents max */
4960 xfs_fsblock_t *firstblock, /* first allocated block
4961 controls a.g. for allocs */
4962 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4963 int *done) /* set if not done yet */
4964{
4965 xfs_btree_cur_t *cur; /* bmap btree cursor */
4966 xfs_bmbt_irec_t del; /* extent being deleted */
4967 int eof; /* is deleting at eof */
a6f64d4a 4968 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1da177e4
LT
4969 int error; /* error return value */
4970 xfs_extnum_t extno; /* extent number in list */
4eea22f0 4971 xfs_bmbt_irec_t got; /* current extent record */
1da177e4
LT
4972 xfs_ifork_t *ifp; /* inode fork pointer */
4973 int isrt; /* freeing in rt area */
4974 xfs_extnum_t lastx; /* last extent index used */
4975 int logflags; /* transaction logging flags */
4976 xfs_extlen_t mod; /* rt extent offset */
4977 xfs_mount_t *mp; /* mount structure */
4eea22f0
MK
4978 xfs_extnum_t nextents; /* number of file extents */
4979 xfs_bmbt_irec_t prev; /* previous extent record */
1da177e4
LT
4980 xfs_fileoff_t start; /* first file offset deleted */
4981 int tmp_logflags; /* partial logging flags */
4982 int wasdel; /* was a delayed alloc extent */
4983 int whichfork; /* data or attribute fork */
4984 int rsvd; /* OK to allocate reserved blocks */
4985 xfs_fsblock_t sum;
4986
0b1b213f
CH
4987 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
4988
1da177e4
LT
4989 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4990 XFS_ATTR_FORK : XFS_DATA_FORK;
4991 ifp = XFS_IFORK_PTR(ip, whichfork);
4992 if (unlikely(
4993 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4994 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
4995 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
4996 ip->i_mount);
4997 return XFS_ERROR(EFSCORRUPTED);
4998 }
4999 mp = ip->i_mount;
5000 if (XFS_FORCED_SHUTDOWN(mp))
5001 return XFS_ERROR(EIO);
5002 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5003 ASSERT(len > 0);
5004 ASSERT(nexts >= 0);
5005 ASSERT(ifp->if_ext_max ==
5006 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5007 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5008 (error = xfs_iread_extents(tp, ip, whichfork)))
5009 return error;
5010 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5011 if (nextents == 0) {
5012 *done = 1;
5013 return 0;
5014 }
5015 XFS_STATS_INC(xs_blk_unmap);
dd9f438e 5016 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
1da177e4
LT
5017 start = bno;
5018 bno = start + len - 1;
5019 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5020 &prev);
b4e9181e 5021
1da177e4
LT
5022 /*
5023 * Check to see if the given block number is past the end of the
5024 * file, back up to the last block if so...
5025 */
5026 if (eof) {
4eea22f0 5027 ep = xfs_iext_get_ext(ifp, --lastx);
1da177e4
LT
5028 xfs_bmbt_get_all(ep, &got);
5029 bno = got.br_startoff + got.br_blockcount - 1;
5030 }
5031 logflags = 0;
5032 if (ifp->if_flags & XFS_IFBROOT) {
5033 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
561f7d17 5034 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1da177e4
LT
5035 cur->bc_private.b.firstblock = *firstblock;
5036 cur->bc_private.b.flist = flist;
5037 cur->bc_private.b.flags = 0;
5038 } else
5039 cur = NULL;
5040 extno = 0;
5041 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5042 (nexts == 0 || extno < nexts)) {
5043 /*
5044 * Is the found extent after a hole in which bno lives?
5045 * Just back up to the previous extent, if so.
5046 */
5047 if (got.br_startoff > bno) {
5048 if (--lastx < 0)
5049 break;
4eea22f0 5050 ep = xfs_iext_get_ext(ifp, lastx);
1da177e4
LT
5051 xfs_bmbt_get_all(ep, &got);
5052 }
5053 /*
5054 * Is the last block of this extent before the range
5055 * we're supposed to delete? If so, we're done.
5056 */
5057 bno = XFS_FILEOFF_MIN(bno,
5058 got.br_startoff + got.br_blockcount - 1);
5059 if (bno < start)
5060 break;
5061 /*
5062 * Then deal with the (possibly delayed) allocated space
5063 * we found.
5064 */
5065 ASSERT(ep != NULL);
5066 del = got;
9d87c319 5067 wasdel = isnullstartblock(del.br_startblock);
1da177e4
LT
5068 if (got.br_startoff < start) {
5069 del.br_startoff = start;
5070 del.br_blockcount -= start - got.br_startoff;
5071 if (!wasdel)
5072 del.br_startblock += start - got.br_startoff;
5073 }
5074 if (del.br_startoff + del.br_blockcount > bno + 1)
5075 del.br_blockcount = bno + 1 - del.br_startoff;
5076 sum = del.br_startblock + del.br_blockcount;
5077 if (isrt &&
5078 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5079 /*
5080 * Realtime extent not lined up at the end.
5081 * The extent could have been split into written
5082 * and unwritten pieces, or we could just be
5083 * unmapping part of it. But we can't really
5084 * get rid of part of a realtime extent.
5085 */
5086 if (del.br_state == XFS_EXT_UNWRITTEN ||
62118709 5087 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
1da177e4
LT
5088 /*
5089 * This piece is unwritten, or we're not
5090 * using unwritten extents. Skip over it.
5091 */
5092 ASSERT(bno >= mod);
5093 bno -= mod > del.br_blockcount ?
5094 del.br_blockcount : mod;
5095 if (bno < got.br_startoff) {
5096 if (--lastx >= 0)
4eea22f0
MK
5097 xfs_bmbt_get_all(xfs_iext_get_ext(
5098 ifp, lastx), &got);
1da177e4
LT
5099 }
5100 continue;
5101 }
5102 /*
5103 * It's written, turn it unwritten.
5104 * This is better than zeroing it.
5105 */
5106 ASSERT(del.br_state == XFS_EXT_NORM);
5107 ASSERT(xfs_trans_get_block_res(tp) > 0);
5108 /*
5109 * If this spans a realtime extent boundary,
5110 * chop it back to the start of the one we end at.
5111 */
5112 if (del.br_blockcount > mod) {
5113 del.br_startoff += del.br_blockcount - mod;
5114 del.br_startblock += del.br_blockcount - mod;
5115 del.br_blockcount = mod;
5116 }
5117 del.br_state = XFS_EXT_UNWRITTEN;
5118 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
b4e9181e 5119 firstblock, flist, &logflags,
3e57ecf6 5120 XFS_DATA_FORK, 0);
1da177e4
LT
5121 if (error)
5122 goto error0;
5123 goto nodelete;
5124 }
5125 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5126 /*
5127 * Realtime extent is lined up at the end but not
5128 * at the front. We'll get rid of full extents if
5129 * we can.
5130 */
5131 mod = mp->m_sb.sb_rextsize - mod;
5132 if (del.br_blockcount > mod) {
5133 del.br_blockcount -= mod;
5134 del.br_startoff += mod;
5135 del.br_startblock += mod;
5136 } else if ((del.br_startoff == start &&
5137 (del.br_state == XFS_EXT_UNWRITTEN ||
5138 xfs_trans_get_block_res(tp) == 0)) ||
62118709 5139 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
1da177e4
LT
5140 /*
5141 * Can't make it unwritten. There isn't
5142 * a full extent here so just skip it.
5143 */
5144 ASSERT(bno >= del.br_blockcount);
5145 bno -= del.br_blockcount;
5146 if (bno < got.br_startoff) {
5147 if (--lastx >= 0)
5148 xfs_bmbt_get_all(--ep, &got);
5149 }
5150 continue;
5151 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5152 /*
5153 * This one is already unwritten.
5154 * It must have a written left neighbor.
5155 * Unwrite the killed part of that one and
5156 * try again.
5157 */
5158 ASSERT(lastx > 0);
4eea22f0
MK
5159 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5160 lastx - 1), &prev);
1da177e4 5161 ASSERT(prev.br_state == XFS_EXT_NORM);
9d87c319 5162 ASSERT(!isnullstartblock(prev.br_startblock));
1da177e4
LT
5163 ASSERT(del.br_startblock ==
5164 prev.br_startblock + prev.br_blockcount);
5165 if (prev.br_startoff < start) {
5166 mod = start - prev.br_startoff;
5167 prev.br_blockcount -= mod;
5168 prev.br_startblock += mod;
5169 prev.br_startoff = start;
5170 }
5171 prev.br_state = XFS_EXT_UNWRITTEN;
5172 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5173 &prev, firstblock, flist, &logflags,
b4e9181e 5174 XFS_DATA_FORK, 0);
1da177e4
LT
5175 if (error)
5176 goto error0;
5177 goto nodelete;
5178 } else {
5179 ASSERT(del.br_state == XFS_EXT_NORM);
5180 del.br_state = XFS_EXT_UNWRITTEN;
5181 error = xfs_bmap_add_extent(ip, lastx, &cur,
5182 &del, firstblock, flist, &logflags,
b4e9181e 5183 XFS_DATA_FORK, 0);
1da177e4
LT
5184 if (error)
5185 goto error0;
5186 goto nodelete;
5187 }
5188 }
5189 if (wasdel) {
9d87c319 5190 ASSERT(startblockval(del.br_startblock) > 0);
dd9f438e 5191 /* Update realtime/data freespace, unreserve quota */
06d10dd9
NS
5192 if (isrt) {
5193 xfs_filblks_t rtexts;
5194
5195 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5196 do_div(rtexts, mp->m_sb.sb_rextsize);
5197 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
20f4ebf2 5198 (int64_t)rtexts, rsvd);
7d095257
CH
5199 (void)xfs_trans_reserve_quota_nblks(NULL,
5200 ip, -((long)del.br_blockcount), 0,
06d10dd9
NS
5201 XFS_QMOPT_RES_RTBLKS);
5202 } else {
5203 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
20f4ebf2 5204 (int64_t)del.br_blockcount, rsvd);
7d095257
CH
5205 (void)xfs_trans_reserve_quota_nblks(NULL,
5206 ip, -((long)del.br_blockcount), 0,
1da177e4 5207 XFS_QMOPT_RES_REGBLKS);
06d10dd9 5208 }
1da177e4
LT
5209 ip->i_delayed_blks -= del.br_blockcount;
5210 if (cur)
5211 cur->bc_private.b.flags |=
5212 XFS_BTCUR_BPRV_WASDEL;
5213 } else if (cur)
5214 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5215 /*
5216 * If it's the case where the directory code is running
5217 * with no block reservation, and the deleted block is in
5218 * the middle of its extent, and the resulting insert
5219 * of an extent would cause transformation to btree format,
5220 * then reject it. The calling code will then swap
5221 * blocks around instead.
5222 * We have to do this now, rather than waiting for the
5223 * conversion to btree format, since the transaction
5224 * will be dirty.
5225 */
5226 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5227 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5228 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5229 del.br_startoff > got.br_startoff &&
5230 del.br_startoff + del.br_blockcount <
5231 got.br_startoff + got.br_blockcount) {
5232 error = XFS_ERROR(ENOSPC);
5233 goto error0;
5234 }
5235 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
b4e9181e 5236 &tmp_logflags, whichfork, rsvd);
1da177e4
LT
5237 logflags |= tmp_logflags;
5238 if (error)
5239 goto error0;
5240 bno = del.br_startoff - 1;
5241nodelete:
5242 lastx = ifp->if_lastex;
5243 /*
5244 * If not done go on to the next (previous) record.
5245 * Reset ep in case the extents array was re-alloced.
5246 */
4eea22f0 5247 ep = xfs_iext_get_ext(ifp, lastx);
1da177e4
LT
5248 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5249 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5250 xfs_bmbt_get_startoff(ep) > bno) {
4eea22f0
MK
5251 if (--lastx >= 0)
5252 ep = xfs_iext_get_ext(ifp, lastx);
1da177e4
LT
5253 }
5254 if (lastx >= 0)
5255 xfs_bmbt_get_all(ep, &got);
5256 extno++;
5257 }
5258 }
5259 ifp->if_lastex = lastx;
5260 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5261 ASSERT(ifp->if_ext_max ==
5262 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5263 /*
5264 * Convert to a btree if necessary.
5265 */
5266 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5267 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5268 ASSERT(cur == NULL);
5269 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5270 &cur, 0, &tmp_logflags, whichfork);
5271 logflags |= tmp_logflags;
5272 if (error)
5273 goto error0;
5274 }
5275 /*
5276 * transform from btree to extents, give it cur
5277 */
5278 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5279 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5280 ASSERT(cur != NULL);
5281 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5282 whichfork);
5283 logflags |= tmp_logflags;
5284 if (error)
5285 goto error0;
5286 }
5287 /*
5288 * transform from extents to local?
5289 */
5290 ASSERT(ifp->if_ext_max ==
5291 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5292 error = 0;
5293error0:
5294 /*
5295 * Log everything. Do this after conversion, there's no point in
4eea22f0 5296 * logging the extent records if we've converted to btree format.
1da177e4 5297 */
9d87c319 5298 if ((logflags & xfs_ilog_fext(whichfork)) &&
1da177e4 5299 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
9d87c319
ES
5300 logflags &= ~xfs_ilog_fext(whichfork);
5301 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
1da177e4 5302 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
9d87c319 5303 logflags &= ~xfs_ilog_fbroot(whichfork);
1da177e4
LT
5304 /*
5305 * Log inode even in the error case, if the transaction
5306 * is dirty we'll need to shut down the filesystem.
5307 */
5308 if (logflags)
5309 xfs_trans_log_inode(tp, ip, logflags);
5310 if (cur) {
5311 if (!error) {
5312 *firstblock = cur->bc_private.b.firstblock;
5313 cur->bc_private.b.allocated = 0;
5314 }
5315 xfs_btree_del_cursor(cur,
5316 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5317 }
5318 return error;
5319}
5320
3bacbcd8
VA
5321/*
5322 * returns 1 for success, 0 if we failed to map the extent.
5323 */
5324STATIC int
5325xfs_getbmapx_fix_eof_hole(
5326 xfs_inode_t *ip, /* xfs incore inode pointer */
8a7141a8 5327 struct getbmapx *out, /* output structure */
3bacbcd8 5328 int prealloced, /* this is a file with
8a7141a8 5329 * preallocated data space */
3bacbcd8
VA
5330 __int64_t end, /* last block requested */
5331 xfs_fsblock_t startblock)
5332{
5333 __int64_t fixlen;
5334 xfs_mount_t *mp; /* file system mount point */
5af317c9
ES
5335 xfs_ifork_t *ifp; /* inode fork pointer */
5336 xfs_extnum_t lastx; /* last extent pointer */
5337 xfs_fileoff_t fileblock;
3bacbcd8
VA
5338
5339 if (startblock == HOLESTARTBLOCK) {
5340 mp = ip->i_mount;
5341 out->bmv_block = -1;
5342 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5343 fixlen -= out->bmv_offset;
5344 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5345 /* Came to hole at EOF. Trim it. */
5346 if (fixlen <= 0)
5347 return 0;
5348 out->bmv_length = fixlen;
5349 }
5350 } else {
5af317c9
ES
5351 if (startblock == DELAYSTARTBLOCK)
5352 out->bmv_block = -2;
5353 else
9d87c319 5354 out->bmv_block = xfs_fsb_to_db(ip, startblock);
5af317c9
ES
5355 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5356 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5357 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5358 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5359 out->bmv_oflags |= BMV_OF_LAST;
3bacbcd8
VA
5360 }
5361
5362 return 1;
5363}
5364
1da177e4 5365/*
8a7141a8
ES
5366 * Get inode's extents as described in bmv, and format for output.
5367 * Calls formatter to fill the user's buffer until all extents
5368 * are mapped, until the passed-in bmv->bmv_count slots have
5369 * been filled, or until the formatter short-circuits the loop,
5370 * if it is tracking filled-in extents on its own.
1da177e4
LT
5371 */
5372int /* error code */
5373xfs_getbmap(
993386c1 5374 xfs_inode_t *ip,
8a7141a8
ES
5375 struct getbmapx *bmv, /* user bmap structure */
5376 xfs_bmap_format_t formatter, /* format to user */
5377 void *arg) /* formatter arg */
1da177e4
LT
5378{
5379 __int64_t bmvend; /* last block requested */
4be4a00f 5380 int error = 0; /* return value */
1da177e4
LT
5381 __int64_t fixlen; /* length for -1 case */
5382 int i; /* extent number */
1da177e4
LT
5383 int lock; /* lock state */
5384 xfs_bmbt_irec_t *map; /* buffer for user's data */
5385 xfs_mount_t *mp; /* file system mount point */
5386 int nex; /* # of user extents can do */
5387 int nexleft; /* # of user extents left */
5388 int subnex; /* # of bmapi's can do */
5389 int nmap; /* number of map entries */
6321e3ed 5390 struct getbmapx *out; /* output structure */
1da177e4
LT
5391 int whichfork; /* data or attr fork */
5392 int prealloced; /* this is a file with
5393 * preallocated data space */
8a7141a8 5394 int iflags; /* interface flags */
1da177e4 5395 int bmapi_flags; /* flags for xfs_bmapi */
6321e3ed 5396 int cur_ext = 0;
1da177e4 5397
1da177e4 5398 mp = ip->i_mount;
8a7141a8 5399 iflags = bmv->bmv_iflags;
8a7141a8 5400 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
1da177e4 5401
1da177e4
LT
5402 if (whichfork == XFS_ATTR_FORK) {
5403 if (XFS_IFORK_Q(ip)) {
5404 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5405 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5406 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5407 return XFS_ERROR(EINVAL);
5408 } else if (unlikely(
5409 ip->i_d.di_aformat != 0 &&
5410 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5411 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5412 ip->i_mount);
5413 return XFS_ERROR(EFSCORRUPTED);
5414 }
4be4a00f
CH
5415
5416 prealloced = 0;
5417 fixlen = 1LL << 32;
5418 } else {
4be4a00f
CH
5419 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5420 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5421 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5422 return XFS_ERROR(EINVAL);
5423
957d0ebe 5424 if (xfs_get_extsz_hint(ip) ||
dd9f438e 5425 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
1da177e4
LT
5426 prealloced = 1;
5427 fixlen = XFS_MAXIOFFSET(mp);
5428 } else {
5429 prealloced = 0;
ba87ea69 5430 fixlen = ip->i_size;
1da177e4 5431 }
1da177e4
LT
5432 }
5433
5434 if (bmv->bmv_length == -1) {
5435 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
4be4a00f
CH
5436 bmv->bmv_length =
5437 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5438 } else if (bmv->bmv_length == 0) {
1da177e4
LT
5439 bmv->bmv_entries = 0;
5440 return 0;
4be4a00f
CH
5441 } else if (bmv->bmv_length < 0) {
5442 return XFS_ERROR(EINVAL);
1da177e4 5443 }
4be4a00f 5444
1da177e4
LT
5445 nex = bmv->bmv_count - 1;
5446 if (nex <= 0)
5447 return XFS_ERROR(EINVAL);
5448 bmvend = bmv->bmv_offset + bmv->bmv_length;
5449
6321e3ed
CH
5450
5451 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5452 return XFS_ERROR(ENOMEM);
5453 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5454 if (!out)
5455 return XFS_ERROR(ENOMEM);
5456
1da177e4 5457 xfs_ilock(ip, XFS_IOLOCK_SHARED);
4be4a00f
CH
5458 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5459 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5460 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5461 if (error)
5462 goto out_unlock_iolock;
e12070a5 5463 }
1da177e4 5464
4be4a00f
CH
5465 ASSERT(ip->i_delayed_blks == 0);
5466 }
1da177e4
LT
5467
5468 lock = xfs_ilock_map_shared(ip);
5469
5470 /*
5471 * Don't let nex be bigger than the number of extents
5472 * we can have assuming alternating holes and real extents.
5473 */
5474 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5475 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5476
4be4a00f
CH
5477 bmapi_flags = xfs_bmapi_aflag(whichfork);
5478 if (!(iflags & BMV_IF_PREALLOC))
5479 bmapi_flags |= XFS_BMAPI_IGSTATE;
1da177e4
LT
5480
5481 /*
5482 * Allocate enough space to handle "subnex" maps at a time.
5483 */
4be4a00f 5484 error = ENOMEM;
1da177e4 5485 subnex = 16;
ca35dcd6 5486 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
4be4a00f
CH
5487 if (!map)
5488 goto out_unlock_ilock;
1da177e4
LT
5489
5490 bmv->bmv_entries = 0;
5491
4be4a00f
CH
5492 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5493 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5494 error = 0;
5495 goto out_free_map;
1da177e4
LT
5496 }
5497
5498 nexleft = nex;
5499
5500 do {
5501 nmap = (nexleft > subnex) ? subnex : nexleft;
5502 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5503 XFS_BB_TO_FSB(mp, bmv->bmv_length),
3e57ecf6 5504 bmapi_flags, NULL, 0, map, &nmap,
b4e9181e 5505 NULL);
1da177e4 5506 if (error)
4be4a00f 5507 goto out_free_map;
1da177e4
LT
5508 ASSERT(nmap <= subnex);
5509
5510 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
6321e3ed 5511 out[cur_ext].bmv_oflags = 0;
5af317c9 5512 if (map[i].br_state == XFS_EXT_UNWRITTEN)
6321e3ed 5513 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
5af317c9 5514 else if (map[i].br_startblock == DELAYSTARTBLOCK)
6321e3ed
CH
5515 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5516 out[cur_ext].bmv_offset =
5517 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5518 out[cur_ext].bmv_length =
5519 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5520 out[cur_ext].bmv_unused1 = 0;
5521 out[cur_ext].bmv_unused2 = 0;
5af317c9
ES
5522 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5523 (map[i].br_startblock != DELAYSTARTBLOCK));
9af0a70c 5524 if (map[i].br_startblock == HOLESTARTBLOCK &&
3bacbcd8
VA
5525 whichfork == XFS_ATTR_FORK) {
5526 /* came to the end of attribute fork */
6321e3ed 5527 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
4be4a00f 5528 goto out_free_map;
1da177e4 5529 }
4be4a00f 5530
6321e3ed
CH
5531 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5532 prealloced, bmvend,
5533 map[i].br_startblock))
4be4a00f
CH
5534 goto out_free_map;
5535
4be4a00f 5536 bmv->bmv_offset =
6321e3ed
CH
5537 out[cur_ext].bmv_offset +
5538 out[cur_ext].bmv_length;
4be4a00f
CH
5539 bmv->bmv_length =
5540 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
9af25465
TM
5541
5542 /*
5543 * In case we don't want to return the hole,
5544 * don't increase cur_ext so that we can reuse
5545 * it in the next loop.
5546 */
5547 if ((iflags & BMV_IF_NO_HOLES) &&
5548 map[i].br_startblock == HOLESTARTBLOCK) {
5549 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5550 continue;
5551 }
5552
5553 nexleft--;
4be4a00f 5554 bmv->bmv_entries++;
6321e3ed 5555 cur_ext++;
1da177e4
LT
5556 }
5557 } while (nmap && nexleft && bmv->bmv_length);
5558
4be4a00f
CH
5559 out_free_map:
5560 kmem_free(map);
5561 out_unlock_ilock:
1da177e4 5562 xfs_iunlock_map_shared(ip, lock);
4be4a00f 5563 out_unlock_iolock:
1da177e4 5564 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
6321e3ed
CH
5565
5566 for (i = 0; i < cur_ext; i++) {
5567 int full = 0; /* user array is full */
5568
5569 /* format results & advance arg */
5570 error = formatter(&arg, &out[i], &full);
5571 if (error || full)
5572 break;
5573 }
5574
7747a0b0 5575 kmem_free(out);
1da177e4
LT
5576 return error;
5577}
5578
5579/*
5580 * Check the last inode extent to determine whether this allocation will result
5581 * in blocks being allocated at the end of the file. When we allocate new data
5582 * blocks at the end of the file which do not start at the previous data block,
5583 * we will try to align the new blocks at stripe unit boundaries.
5584 */
ba0f32d4 5585STATIC int /* error */
1da177e4
LT
5586xfs_bmap_isaeof(
5587 xfs_inode_t *ip, /* incore inode pointer */
5588 xfs_fileoff_t off, /* file offset in fsblocks */
5589 int whichfork, /* data or attribute fork */
5590 char *aeof) /* return value */
5591{
5592 int error; /* error return value */
5593 xfs_ifork_t *ifp; /* inode fork pointer */
a6f64d4a 5594 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
4eea22f0
MK
5595 xfs_extnum_t nextents; /* number of file extents */
5596 xfs_bmbt_irec_t s; /* expanded extent record */
1da177e4
LT
5597
5598 ASSERT(whichfork == XFS_DATA_FORK);
5599 ifp = XFS_IFORK_PTR(ip, whichfork);
5600 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5601 (error = xfs_iread_extents(NULL, ip, whichfork)))
5602 return error;
5603 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5604 if (nextents == 0) {
5605 *aeof = 1;
5606 return 0;
5607 }
5608 /*
5609 * Go to the last extent
5610 */
4eea22f0 5611 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
1da177e4
LT
5612 xfs_bmbt_get_all(lastrec, &s);
5613 /*
5614 * Check we are allocating in the last extent (for delayed allocations)
5615 * or past the last extent for non-delayed allocations.
5616 */
5617 *aeof = (off >= s.br_startoff &&
5618 off < s.br_startoff + s.br_blockcount &&
9d87c319 5619 isnullstartblock(s.br_startblock)) ||
1da177e4
LT
5620 off >= s.br_startoff + s.br_blockcount;
5621 return 0;
5622}
5623
5624/*
5625 * Check if the endoff is outside the last extent. If so the caller will grow
5626 * the allocation to a stripe unit boundary.
5627 */
5628int /* error */
5629xfs_bmap_eof(
5630 xfs_inode_t *ip, /* incore inode pointer */
5631 xfs_fileoff_t endoff, /* file offset in fsblocks */
5632 int whichfork, /* data or attribute fork */
5633 int *eof) /* result value */
5634{
5635 xfs_fsblock_t blockcount; /* extent block count */
5636 int error; /* error return value */
5637 xfs_ifork_t *ifp; /* inode fork pointer */
a6f64d4a 5638 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
4eea22f0 5639 xfs_extnum_t nextents; /* number of file extents */
1da177e4
LT
5640 xfs_fileoff_t startoff; /* extent starting file offset */
5641
5642 ASSERT(whichfork == XFS_DATA_FORK);
5643 ifp = XFS_IFORK_PTR(ip, whichfork);
5644 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5645 (error = xfs_iread_extents(NULL, ip, whichfork)))
5646 return error;
5647 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5648 if (nextents == 0) {
5649 *eof = 1;
5650 return 0;
5651 }
5652 /*
5653 * Go to the last extent
5654 */
4eea22f0 5655 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
1da177e4
LT
5656 startoff = xfs_bmbt_get_startoff(lastrec);
5657 blockcount = xfs_bmbt_get_blockcount(lastrec);
5658 *eof = endoff >= startoff + blockcount;
5659 return 0;
5660}
5661
5662#ifdef DEBUG
ecd7f082 5663STATIC struct xfs_buf *
1da177e4 5664xfs_bmap_get_bp(
ecd7f082 5665 struct xfs_btree_cur *cur,
1da177e4
LT
5666 xfs_fsblock_t bno)
5667{
ecd7f082
CH
5668 struct xfs_log_item_desc *lidp;
5669 int i;
1da177e4
LT
5670
5671 if (!cur)
ecd7f082
CH
5672 return NULL;
5673
5674 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5675 if (!cur->bc_bufs[i])
5676 break;
5677 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5678 return cur->bc_bufs[i];
1da177e4 5679 }
1da177e4 5680
ecd7f082
CH
5681 /* Chase down all the log items to see if the bp is there */
5682 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
e98c414f 5683 struct xfs_buf_log_item *bip;
ecd7f082
CH
5684 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5685 if (bip->bli_item.li_type == XFS_LI_BUF &&
5686 XFS_BUF_ADDR(bip->bli_buf) == bno)
5687 return bip->bli_buf;
1da177e4 5688 }
e98c414f 5689
ecd7f082 5690 return NULL;
1da177e4
LT
5691}
5692
3180e66d 5693STATIC void
1da177e4 5694xfs_check_block(
7cc95a82 5695 struct xfs_btree_block *block,
1da177e4
LT
5696 xfs_mount_t *mp,
5697 int root,
5698 short sz)
5699{
5700 int i, j, dmxr;
576039cf 5701 __be64 *pp, *thispa; /* pointer to block address */
1da177e4
LT
5702 xfs_bmbt_key_t *prevp, *keyp;
5703
16259e7d 5704 ASSERT(be16_to_cpu(block->bb_level) > 0);
1da177e4
LT
5705
5706 prevp = NULL;
7cc95a82 5707 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
1da177e4 5708 dmxr = mp->m_bmap_dmxr[0];
136341b4 5709 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
1da177e4
LT
5710
5711 if (prevp) {
4a26e66e
CH
5712 ASSERT(be64_to_cpu(prevp->br_startoff) <
5713 be64_to_cpu(keyp->br_startoff));
1da177e4
LT
5714 }
5715 prevp = keyp;
5716
5717 /*
5718 * Compare the block numbers to see if there are dups.
5719 */
136341b4 5720 if (root)
60197e8d 5721 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
136341b4
CH
5722 else
5723 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5724
16259e7d 5725 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
136341b4 5726 if (root)
60197e8d 5727 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
136341b4
CH
5728 else
5729 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
576039cf 5730 if (*thispa == *pp) {
1da177e4 5731 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
34a622b2 5732 __func__, j, i,
576039cf 5733 (unsigned long long)be64_to_cpu(*thispa));
1da177e4 5734 panic("%s: ptrs are equal in node\n",
34a622b2 5735 __func__);
1da177e4
LT
5736 }
5737 }
5738 }
5739}
5740
5741/*
5742 * Check that the extents for the inode ip are in the right order in all
5743 * btree leaves.
5744 */
5745
5746STATIC void
5747xfs_bmap_check_leaf_extents(
5748 xfs_btree_cur_t *cur, /* btree cursor or null */
5749 xfs_inode_t *ip, /* incore inode pointer */
5750 int whichfork) /* data or attr fork */
5751{
7cc95a82 5752 struct xfs_btree_block *block; /* current btree block */
1da177e4
LT
5753 xfs_fsblock_t bno; /* block # of "block" */
5754 xfs_buf_t *bp; /* buffer for "block" */
5755 int error; /* error return value */
4eea22f0 5756 xfs_extnum_t i=0, j; /* index into the extents list */
1da177e4
LT
5757 xfs_ifork_t *ifp; /* fork structure */
5758 int level; /* btree level, for checking */
5759 xfs_mount_t *mp; /* file system mount structure */
576039cf 5760 __be64 *pp; /* pointer to block address */
4eea22f0 5761 xfs_bmbt_rec_t *ep; /* pointer to current extent */
2abdb8c8 5762 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
4eea22f0 5763 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
1da177e4
LT
5764 int bp_release = 0;
5765
5766 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5767 return;
5768 }
5769
5770 bno = NULLFSBLOCK;
5771 mp = ip->i_mount;
5772 ifp = XFS_IFORK_PTR(ip, whichfork);
5773 block = ifp->if_broot;
5774 /*
5775 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5776 */
16259e7d
CH
5777 level = be16_to_cpu(block->bb_level);
5778 ASSERT(level > 0);
1da177e4 5779 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
60197e8d 5780 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
576039cf
CH
5781 bno = be64_to_cpu(*pp);
5782
5783 ASSERT(bno != NULLDFSBNO);
5784 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5785 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5786
1da177e4
LT
5787 /*
5788 * Go down the tree until leaf level is reached, following the first
5789 * pointer (leftmost) at each level.
5790 */
5791 while (level-- > 0) {
5792 /* See if buf is in cur first */
5793 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5794 if (bp) {
5795 bp_release = 0;
5796 } else {
5797 bp_release = 1;
5798 }
5799 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5800 XFS_BMAP_BTREE_REF)))
5801 goto error_norelse;
7cc95a82 5802 block = XFS_BUF_TO_BLOCK(bp);
1da177e4 5803 XFS_WANT_CORRUPTED_GOTO(
4e8938fe 5804 xfs_bmap_sanity_check(mp, bp, level),
1da177e4
LT
5805 error0);
5806 if (level == 0)
5807 break;
5808
5809 /*
5810 * Check this block for basic sanity (increasing keys and
5811 * no duplicate blocks).
5812 */
5813
5814 xfs_check_block(block, mp, 0, 0);
136341b4 5815 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
576039cf
CH
5816 bno = be64_to_cpu(*pp);
5817 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
1da177e4
LT
5818 if (bp_release) {
5819 bp_release = 0;
5820 xfs_trans_brelse(NULL, bp);
5821 }
5822 }
5823
5824 /*
5825 * Here with bp and block set to the leftmost leaf node in the tree.
5826 */
5827 i = 0;
5828
5829 /*
5830 * Loop over all leaf nodes checking that all extents are in the right order.
5831 */
1da177e4 5832 for (;;) {
1da177e4
LT
5833 xfs_fsblock_t nextbno;
5834 xfs_extnum_t num_recs;
5835
5836
7cc95a82 5837 num_recs = xfs_btree_get_numrecs(block);
1da177e4
LT
5838
5839 /*
5840 * Read-ahead the next leaf block, if any.
5841 */
5842
7cc95a82 5843 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1da177e4
LT
5844
5845 /*
5846 * Check all the extents to make sure they are OK.
5847 * If we had a previous block, the last entry should
5848 * conform with the first entry in this one.
5849 */
5850
136341b4 5851 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
2abdb8c8 5852 if (i) {
4a26e66e
CH
5853 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
5854 xfs_bmbt_disk_get_blockcount(&last) <=
5855 xfs_bmbt_disk_get_startoff(ep));
2abdb8c8 5856 }
4eea22f0 5857 for (j = 1; j < num_recs; j++) {
136341b4 5858 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
4a26e66e
CH
5859 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
5860 xfs_bmbt_disk_get_blockcount(ep) <=
5861 xfs_bmbt_disk_get_startoff(nextp));
4eea22f0 5862 ep = nextp;
1da177e4 5863 }
1da177e4 5864
2abdb8c8 5865 last = *ep;
1da177e4
LT
5866 i += num_recs;
5867 if (bp_release) {
5868 bp_release = 0;
5869 xfs_trans_brelse(NULL, bp);
5870 }
5871 bno = nextbno;
5872 /*
5873 * If we've reached the end, stop.
5874 */
5875 if (bno == NULLFSBLOCK)
5876 break;
5877
5878 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5879 if (bp) {
5880 bp_release = 0;
5881 } else {
5882 bp_release = 1;
5883 }
5884 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5885 XFS_BMAP_BTREE_REF)))
5886 goto error_norelse;
7cc95a82 5887 block = XFS_BUF_TO_BLOCK(bp);
1da177e4
LT
5888 }
5889 if (bp_release) {
5890 bp_release = 0;
5891 xfs_trans_brelse(NULL, bp);
5892 }
5893 return;
5894
5895error0:
34a622b2 5896 cmn_err(CE_WARN, "%s: at error0", __func__);
1da177e4
LT
5897 if (bp_release)
5898 xfs_trans_brelse(NULL, bp);
5899error_norelse:
5900 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
34a622b2
HH
5901 __func__, i);
5902 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
1da177e4
LT
5903 return;
5904}
5905#endif
5906
5907/*
5908 * Count fsblocks of the given fork.
5909 */
5910int /* error */
5911xfs_bmap_count_blocks(
5912 xfs_trans_t *tp, /* transaction pointer */
5913 xfs_inode_t *ip, /* incore inode */
5914 int whichfork, /* data or attr fork */
5915 int *count) /* out: count of blocks */
5916{
7cc95a82 5917 struct xfs_btree_block *block; /* current btree block */
1da177e4
LT
5918 xfs_fsblock_t bno; /* block # of "block" */
5919 xfs_ifork_t *ifp; /* fork structure */
5920 int level; /* btree level, for checking */
5921 xfs_mount_t *mp; /* file system mount structure */
576039cf 5922 __be64 *pp; /* pointer to block address */
1da177e4
LT
5923
5924 bno = NULLFSBLOCK;
5925 mp = ip->i_mount;
5926 ifp = XFS_IFORK_PTR(ip, whichfork);
5927 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
c94312de 5928 xfs_bmap_count_leaves(ifp, 0,
1da177e4 5929 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
c94312de 5930 count);
1da177e4
LT
5931 return 0;
5932 }
5933
5934 /*
5935 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5936 */
5937 block = ifp->if_broot;
16259e7d
CH
5938 level = be16_to_cpu(block->bb_level);
5939 ASSERT(level > 0);
60197e8d 5940 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
576039cf
CH
5941 bno = be64_to_cpu(*pp);
5942 ASSERT(bno != NULLDFSBNO);
5943 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5944 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1da177e4 5945
4eea22f0 5946 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
1da177e4
LT
5947 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
5948 mp);
5949 return XFS_ERROR(EFSCORRUPTED);
5950 }
5951
5952 return 0;
5953}
5954
5955/*
5956 * Recursively walks each level of a btree
5957 * to count total fsblocks is use.
5958 */
a8272ce0 5959STATIC int /* error */
1da177e4
LT
5960xfs_bmap_count_tree(
5961 xfs_mount_t *mp, /* file system mount point */
5962 xfs_trans_t *tp, /* transaction pointer */
4eea22f0 5963 xfs_ifork_t *ifp, /* inode fork pointer */
1da177e4
LT
5964 xfs_fsblock_t blockno, /* file system block number */
5965 int levelin, /* level in btree */
5966 int *count) /* Count of blocks */
5967{
5968 int error;
5969 xfs_buf_t *bp, *nbp;
5970 int level = levelin;
576039cf 5971 __be64 *pp;
1da177e4
LT
5972 xfs_fsblock_t bno = blockno;
5973 xfs_fsblock_t nextbno;
7cc95a82 5974 struct xfs_btree_block *block, *nextblock;
1da177e4 5975 int numrecs;
1da177e4
LT
5976
5977 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
5978 return error;
5979 *count += 1;
7cc95a82 5980 block = XFS_BUF_TO_BLOCK(bp);
1da177e4
LT
5981
5982 if (--level) {
9da096fd 5983 /* Not at node above leaves, count this level of nodes */
7cc95a82 5984 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1da177e4
LT
5985 while (nextbno != NULLFSBLOCK) {
5986 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
5987 0, &nbp, XFS_BMAP_BTREE_REF)))
5988 return error;
5989 *count += 1;
7cc95a82
CH
5990 nextblock = XFS_BUF_TO_BLOCK(nbp);
5991 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
1da177e4
LT
5992 xfs_trans_brelse(tp, nbp);
5993 }
5994
5995 /* Dive to the next level */
136341b4 5996 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
576039cf 5997 bno = be64_to_cpu(*pp);
1da177e4 5998 if (unlikely((error =
4eea22f0 5999 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
1da177e4
LT
6000 xfs_trans_brelse(tp, bp);
6001 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6002 XFS_ERRLEVEL_LOW, mp);
6003 return XFS_ERROR(EFSCORRUPTED);
6004 }
6005 xfs_trans_brelse(tp, bp);
6006 } else {
6007 /* count all level 1 nodes and their leaves */
6008 for (;;) {
7cc95a82 6009 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
16259e7d 6010 numrecs = be16_to_cpu(block->bb_numrecs);
136341b4 6011 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
1da177e4
LT
6012 xfs_trans_brelse(tp, bp);
6013 if (nextbno == NULLFSBLOCK)
6014 break;
6015 bno = nextbno;
6016 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6017 XFS_BMAP_BTREE_REF)))
6018 return error;
6019 *count += 1;
7cc95a82 6020 block = XFS_BUF_TO_BLOCK(bp);
1da177e4
LT
6021 }
6022 }
6023 return 0;
6024}
6025
6026/*
4eea22f0 6027 * Count leaf blocks given a range of extent records.
1da177e4 6028 */
c94312de 6029STATIC void
1da177e4 6030xfs_bmap_count_leaves(
4eea22f0
MK
6031 xfs_ifork_t *ifp,
6032 xfs_extnum_t idx,
1da177e4
LT
6033 int numrecs,
6034 int *count)
6035{
6036 int b;
6037
4eea22f0 6038 for (b = 0; b < numrecs; b++) {
a6f64d4a 6039 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
91e11088 6040 *count += xfs_bmbt_get_blockcount(frp);
4eea22f0 6041 }
91e11088
YL
6042}
6043
6044/*
4eea22f0
MK
6045 * Count leaf blocks given a range of extent records originally
6046 * in btree format.
91e11088 6047 */
c94312de 6048STATIC void
91e11088 6049xfs_bmap_disk_count_leaves(
136341b4 6050 struct xfs_mount *mp,
7cc95a82 6051 struct xfs_btree_block *block,
91e11088
YL
6052 int numrecs,
6053 int *count)
6054{
6055 int b;
4eea22f0 6056 xfs_bmbt_rec_t *frp;
91e11088 6057
4eea22f0 6058 for (b = 1; b <= numrecs; b++) {
136341b4 6059 frp = XFS_BMBT_REC_ADDR(mp, block, b);
1da177e4 6060 *count += xfs_bmbt_disk_get_blockcount(frp);
4eea22f0 6061 }
1da177e4 6062}