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