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