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