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