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