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