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