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