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