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