blob: 183add2f21b721b7752ae9ec6c1686ced63ff7b3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_btree.h"
37#include "xfs_dmapi.h"
38#include "xfs_mount.h"
39#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_itable.h"
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100041#include "xfs_dir2_data.h"
42#include "xfs_dir2_leaf.h"
43#include "xfs_dir2_block.h"
Nathan Scotta844f452005-11-02 14:38:42 +110044#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_extfree_item.h"
46#include "xfs_alloc.h"
47#include "xfs_bmap.h"
48#include "xfs_rtalloc.h"
49#include "xfs_error.h"
Nathan Scottd8cc8902005-11-02 10:34:53 +110050#include "xfs_attr_leaf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "xfs_rw.h"
52#include "xfs_quota.h"
53#include "xfs_trans_space.h"
54#include "xfs_buf_item.h"
55
56
57#ifdef DEBUG
58STATIC void
59xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
60#endif
61
62kmem_zone_t *xfs_bmap_free_item_zone;
63
64/*
65 * Prototypes for internal bmap routines.
66 */
67
68
69/*
70 * Called from xfs_bmap_add_attrfork to handle extents format files.
71 */
72STATIC int /* error */
73xfs_bmap_add_attrfork_extents(
74 xfs_trans_t *tp, /* transaction pointer */
75 xfs_inode_t *ip, /* incore inode pointer */
76 xfs_fsblock_t *firstblock, /* first block allocated */
77 xfs_bmap_free_t *flist, /* blocks to free at commit */
78 int *flags); /* inode logging flags */
79
80/*
81 * Called from xfs_bmap_add_attrfork to handle local format files.
82 */
83STATIC int /* error */
84xfs_bmap_add_attrfork_local(
85 xfs_trans_t *tp, /* transaction pointer */
86 xfs_inode_t *ip, /* incore inode pointer */
87 xfs_fsblock_t *firstblock, /* first block allocated */
88 xfs_bmap_free_t *flist, /* blocks to free at commit */
89 int *flags); /* inode logging flags */
90
91/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +110092 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * after allocating space (or doing a delayed allocation).
94 */
95STATIC int /* error */
96xfs_bmap_add_extent(
97 xfs_inode_t *ip, /* incore inode pointer */
98 xfs_extnum_t idx, /* extent number to update/insert */
99 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100100 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 xfs_fsblock_t *first, /* pointer to firstblock variable */
102 xfs_bmap_free_t *flist, /* list of extents to be freed */
103 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000104 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int whichfork, /* data or attr fork */
106 int rsvd); /* OK to allocate reserved blocks */
107
108/*
109 * Called by xfs_bmap_add_extent to handle cases converting a delayed
110 * allocation to a real allocation.
111 */
112STATIC int /* error */
113xfs_bmap_add_extent_delay_real(
114 xfs_inode_t *ip, /* incore inode pointer */
115 xfs_extnum_t idx, /* extent number to update/insert */
116 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100117 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
119 xfs_fsblock_t *first, /* pointer to firstblock variable */
120 xfs_bmap_free_t *flist, /* list of extents to be freed */
121 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000122 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int rsvd); /* OK to allocate reserved blocks */
124
125/*
126 * Called by xfs_bmap_add_extent to handle cases converting a hole
127 * to a delayed allocation.
128 */
129STATIC int /* error */
130xfs_bmap_add_extent_hole_delay(
131 xfs_inode_t *ip, /* incore inode pointer */
132 xfs_extnum_t idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100133 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int *logflagsp,/* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000135 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int rsvd); /* OK to allocate reserved blocks */
137
138/*
139 * Called by xfs_bmap_add_extent to handle cases converting a hole
140 * to a real allocation.
141 */
142STATIC int /* error */
143xfs_bmap_add_extent_hole_real(
144 xfs_inode_t *ip, /* incore inode pointer */
145 xfs_extnum_t idx, /* extent number to update/insert */
146 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100147 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000149 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 int whichfork); /* data or attr fork */
151
152/*
153 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
154 * allocation to a real allocation or vice versa.
155 */
156STATIC int /* error */
157xfs_bmap_add_extent_unwritten_real(
158 xfs_inode_t *ip, /* incore inode pointer */
159 xfs_extnum_t idx, /* extent number to update/insert */
160 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100161 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000162 int *logflagsp, /* inode logging flags */
163 xfs_extdelta_t *delta); /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165/*
166 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
167 * It figures out where to ask the underlying allocator to put the new extent.
168 */
169STATIC int /* error */
170xfs_bmap_alloc(
171 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
172
173/*
174 * Transform a btree format file with only one leaf node, where the
175 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100176 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * give up the space for the btree root and pitch the leaf block.
178 */
179STATIC int /* error */
180xfs_bmap_btree_to_extents(
181 xfs_trans_t *tp, /* transaction pointer */
182 xfs_inode_t *ip, /* incore inode pointer */
183 xfs_btree_cur_t *cur, /* btree cursor */
184 int *logflagsp, /* inode logging flags */
185 int whichfork); /* data or attr fork */
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100188 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * after removing space (or undoing a delayed allocation).
190 */
191STATIC int /* error */
192xfs_bmap_del_extent(
193 xfs_inode_t *ip, /* incore inode pointer */
194 xfs_trans_t *tp, /* current trans pointer */
195 xfs_extnum_t idx, /* extent number to update/insert */
196 xfs_bmap_free_t *flist, /* list of extents to be freed */
197 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100198 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 int *logflagsp,/* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000200 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 int whichfork, /* data or attr fork */
202 int rsvd); /* OK to allocate reserved blocks */
203
204/*
205 * Remove the entry "free" from the free item list. Prev points to the
206 * previous entry, unless "free" is the head of the list.
207 */
208STATIC void
209xfs_bmap_del_free(
210 xfs_bmap_free_t *flist, /* free item list header */
211 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
212 xfs_bmap_free_item_t *free); /* list item to be freed */
213
214/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * Convert an extents-format file into a btree-format file.
216 * The new file will have a root block (in the inode) and a single child block.
217 */
218STATIC int /* error */
219xfs_bmap_extents_to_btree(
220 xfs_trans_t *tp, /* transaction pointer */
221 xfs_inode_t *ip, /* incore inode pointer */
222 xfs_fsblock_t *firstblock, /* first-block-allocated */
223 xfs_bmap_free_t *flist, /* blocks freed in xaction */
224 xfs_btree_cur_t **curp, /* cursor returned to caller */
225 int wasdel, /* converting a delayed alloc */
226 int *logflagsp, /* inode logging flags */
227 int whichfork); /* data or attr fork */
228
229/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * Convert a local file to an extents file.
231 * This code is sort of bogus, since the file data needs to get
232 * logged so it won't be lost. The bmap-level manipulations are ok, though.
233 */
234STATIC int /* error */
235xfs_bmap_local_to_extents(
236 xfs_trans_t *tp, /* transaction pointer */
237 xfs_inode_t *ip, /* incore inode pointer */
238 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
239 xfs_extlen_t total, /* total blocks needed by transaction */
240 int *logflagsp, /* inode logging flags */
241 int whichfork); /* data or attr fork */
242
243/*
244 * Search the extents list for the inode, for the extent containing bno.
245 * If bno lies in a hole, point to the next entry. If bno lies past eof,
246 * *eofp will be set, and *prevp will contain the last entry (null if none).
247 * Else, *lastxp will be set to the index of the found
248 * entry; *gotp will contain the entry.
249 */
250STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
251xfs_bmap_search_extents(
252 xfs_inode_t *ip, /* incore inode pointer */
253 xfs_fileoff_t bno, /* block number searched for */
254 int whichfork, /* data or attr fork */
255 int *eofp, /* out: end of file found */
256 xfs_extnum_t *lastxp, /* out: last extent index */
257 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
258 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
259
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000260/*
261 * Check the last inode extent to determine whether this allocation will result
262 * in blocks being allocated at the end of the file. When we allocate new data
263 * blocks at the end of the file which do not start at the previous data block,
264 * we will try to align the new blocks at stripe unit boundaries.
265 */
266STATIC int /* error */
267xfs_bmap_isaeof(
268 xfs_inode_t *ip, /* incore inode pointer */
269 xfs_fileoff_t off, /* file offset in fsblocks */
270 int whichfork, /* data or attribute fork */
271 char *aeof); /* return value */
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#ifdef XFS_BMAP_TRACE
274/*
275 * Add a bmap trace buffer entry. Base routine for the others.
276 */
277STATIC void
278xfs_bmap_trace_addentry(
279 int opcode, /* operation */
280 char *fname, /* function name */
281 char *desc, /* operation description */
282 xfs_inode_t *ip, /* incore inode pointer */
283 xfs_extnum_t idx, /* index of entry(ies) */
284 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
285 xfs_bmbt_rec_t *r1, /* first record */
286 xfs_bmbt_rec_t *r2, /* second record or null */
287 int whichfork); /* data or attr fork */
288
289/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100290 * Add bmap trace entry prior to a call to xfs_iext_remove.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
292STATIC void
293xfs_bmap_trace_delete(
294 char *fname, /* function name */
295 char *desc, /* operation description */
296 xfs_inode_t *ip, /* incore inode pointer */
297 xfs_extnum_t idx, /* index of entry(entries) deleted */
298 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
299 int whichfork); /* data or attr fork */
300
301/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100302 * Add bmap trace entry prior to a call to xfs_iext_insert, or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 * reading in the extents list from the disk (in the btree).
304 */
305STATIC void
306xfs_bmap_trace_insert(
307 char *fname, /* function name */
308 char *desc, /* operation description */
309 xfs_inode_t *ip, /* incore inode pointer */
310 xfs_extnum_t idx, /* index of entry(entries) inserted */
311 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
312 xfs_bmbt_irec_t *r1, /* inserted record 1 */
313 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
314 int whichfork); /* data or attr fork */
315
316/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100317 * Add bmap trace entry after updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
319STATIC void
320xfs_bmap_trace_post_update(
321 char *fname, /* function name */
322 char *desc, /* operation description */
323 xfs_inode_t *ip, /* incore inode pointer */
324 xfs_extnum_t idx, /* index of entry updated */
325 int whichfork); /* data or attr fork */
326
327/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100328 * Add bmap trace entry prior to updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
330STATIC void
331xfs_bmap_trace_pre_update(
332 char *fname, /* function name */
333 char *desc, /* operation description */
334 xfs_inode_t *ip, /* incore inode pointer */
335 xfs_extnum_t idx, /* index of entry to be updated */
336 int whichfork); /* data or attr fork */
337
338#else
339#define xfs_bmap_trace_delete(f,d,ip,i,c,w)
340#define xfs_bmap_trace_insert(f,d,ip,i,c,r1,r2,w)
341#define xfs_bmap_trace_post_update(f,d,ip,i,w)
342#define xfs_bmap_trace_pre_update(f,d,ip,i,w)
343#endif /* XFS_BMAP_TRACE */
344
345/*
346 * Compute the worst-case number of indirect blocks that will be used
347 * for ip's delayed extent of length "len".
348 */
349STATIC xfs_filblks_t
350xfs_bmap_worst_indlen(
351 xfs_inode_t *ip, /* incore inode pointer */
352 xfs_filblks_t len); /* delayed extent length */
353
354#ifdef DEBUG
355/*
356 * Perform various validation checks on the values being returned
357 * from xfs_bmapi().
358 */
359STATIC void
360xfs_bmap_validate_ret(
361 xfs_fileoff_t bno,
362 xfs_filblks_t len,
363 int flags,
364 xfs_bmbt_irec_t *mval,
365 int nmap,
366 int ret_nmap);
367#else
368#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
369#endif /* DEBUG */
370
371#if defined(XFS_RW_TRACE)
372STATIC void
373xfs_bunmap_trace(
374 xfs_inode_t *ip,
375 xfs_fileoff_t bno,
376 xfs_filblks_t len,
377 int flags,
378 inst_t *ra);
379#else
380#define xfs_bunmap_trace(ip, bno, len, flags, ra)
381#endif /* XFS_RW_TRACE */
382
383STATIC int
384xfs_bmap_count_tree(
385 xfs_mount_t *mp,
386 xfs_trans_t *tp,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100387 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 xfs_fsblock_t blockno,
389 int levelin,
390 int *count);
391
392STATIC int
393xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100394 xfs_ifork_t *ifp,
395 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int numrecs,
397 int *count);
398
Yingping Lu91e11082005-11-02 15:10:24 +1100399STATIC int
400xfs_bmap_disk_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100401 xfs_extnum_t idx,
402 xfs_bmbt_block_t *block,
Yingping Lu91e11082005-11-02 15:10:24 +1100403 int numrecs,
404 int *count);
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/*
407 * Bmap internal routines.
408 */
409
410/*
411 * Called from xfs_bmap_add_attrfork to handle btree format files.
412 */
413STATIC int /* error */
414xfs_bmap_add_attrfork_btree(
415 xfs_trans_t *tp, /* transaction pointer */
416 xfs_inode_t *ip, /* incore inode pointer */
417 xfs_fsblock_t *firstblock, /* first block allocated */
418 xfs_bmap_free_t *flist, /* blocks to free at commit */
419 int *flags) /* inode logging flags */
420{
421 xfs_btree_cur_t *cur; /* btree cursor */
422 int error; /* error return value */
423 xfs_mount_t *mp; /* file system mount struct */
424 int stat; /* newroot status */
425
426 mp = ip->i_mount;
427 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
428 *flags |= XFS_ILOG_DBROOT;
429 else {
430 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
431 XFS_DATA_FORK);
432 cur->bc_private.b.flist = flist;
433 cur->bc_private.b.firstblock = *firstblock;
434 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
435 goto error0;
436 ASSERT(stat == 1); /* must be at least one entry */
437 if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
438 goto error0;
439 if (stat == 0) {
440 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
441 return XFS_ERROR(ENOSPC);
442 }
443 *firstblock = cur->bc_private.b.firstblock;
444 cur->bc_private.b.allocated = 0;
445 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
446 }
447 return 0;
448error0:
449 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
450 return error;
451}
452
453/*
454 * Called from xfs_bmap_add_attrfork to handle extents format files.
455 */
456STATIC int /* error */
457xfs_bmap_add_attrfork_extents(
458 xfs_trans_t *tp, /* transaction pointer */
459 xfs_inode_t *ip, /* incore inode pointer */
460 xfs_fsblock_t *firstblock, /* first block allocated */
461 xfs_bmap_free_t *flist, /* blocks to free at commit */
462 int *flags) /* inode logging flags */
463{
464 xfs_btree_cur_t *cur; /* bmap btree cursor */
465 int error; /* error return value */
466
467 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
468 return 0;
469 cur = NULL;
470 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
471 flags, XFS_DATA_FORK);
472 if (cur) {
473 cur->bc_private.b.allocated = 0;
474 xfs_btree_del_cursor(cur,
475 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
476 }
477 return error;
478}
479
480/*
481 * Called from xfs_bmap_add_attrfork to handle local format files.
482 */
483STATIC int /* error */
484xfs_bmap_add_attrfork_local(
485 xfs_trans_t *tp, /* transaction pointer */
486 xfs_inode_t *ip, /* incore inode pointer */
487 xfs_fsblock_t *firstblock, /* first block allocated */
488 xfs_bmap_free_t *flist, /* blocks to free at commit */
489 int *flags) /* inode logging flags */
490{
491 xfs_da_args_t dargs; /* args for dir/attr code */
492 int error; /* error return value */
493 xfs_mount_t *mp; /* mount structure pointer */
494
495 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
496 return 0;
497 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
498 mp = ip->i_mount;
499 memset(&dargs, 0, sizeof(dargs));
500 dargs.dp = ip;
501 dargs.firstblock = firstblock;
502 dargs.flist = flist;
503 dargs.total = mp->m_dirblkfsbs;
504 dargs.whichfork = XFS_DATA_FORK;
505 dargs.trans = tp;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000506 error = xfs_dir2_sf_to_block(&dargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 } else
508 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
509 XFS_DATA_FORK);
510 return error;
511}
512
513/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100514 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * after allocating space (or doing a delayed allocation).
516 */
517STATIC int /* error */
518xfs_bmap_add_extent(
519 xfs_inode_t *ip, /* incore inode pointer */
520 xfs_extnum_t idx, /* extent number to update/insert */
521 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100522 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 xfs_fsblock_t *first, /* pointer to firstblock variable */
524 xfs_bmap_free_t *flist, /* list of extents to be freed */
525 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000526 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 int whichfork, /* data or attr fork */
528 int rsvd) /* OK to use reserved data blocks */
529{
530 xfs_btree_cur_t *cur; /* btree cursor or null */
531 xfs_filblks_t da_new; /* new count del alloc blocks used */
532 xfs_filblks_t da_old; /* old count del alloc blocks used */
533 int error; /* error return value */
534#ifdef XFS_BMAP_TRACE
535 static char fname[] = "xfs_bmap_add_extent";
536#endif
537 xfs_ifork_t *ifp; /* inode fork ptr */
538 int logflags; /* returned value */
539 xfs_extnum_t nextents; /* number of extents in file now */
540
541 XFS_STATS_INC(xs_add_exlist);
542 cur = *curp;
543 ifp = XFS_IFORK_PTR(ip, whichfork);
544 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
545 ASSERT(idx <= nextents);
546 da_old = da_new = 0;
547 error = 0;
548 /*
549 * This is the first extent added to a new/empty file.
550 * Special case this one, so other routines get to assume there are
551 * already extents in the list.
552 */
553 if (nextents == 0) {
554 xfs_bmap_trace_insert(fname, "insert empty", ip, 0, 1, new,
555 NULL, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100556 xfs_iext_insert(ifp, 0, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 ASSERT(cur == NULL);
558 ifp->if_lastex = 0;
559 if (!ISNULLSTARTBLOCK(new->br_startblock)) {
560 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
561 logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
562 } else
563 logflags = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000564 /* DELTA: single new extent */
565 if (delta) {
566 if (delta->xed_startoff > new->br_startoff)
567 delta->xed_startoff = new->br_startoff;
568 if (delta->xed_blockcount <
569 new->br_startoff + new->br_blockcount)
570 delta->xed_blockcount = new->br_startoff +
571 new->br_blockcount;
572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574 /*
575 * Any kind of new delayed allocation goes here.
576 */
577 else if (ISNULLSTARTBLOCK(new->br_startblock)) {
578 if (cur)
579 ASSERT((cur->bc_private.b.flags &
580 XFS_BTCUR_BPRV_WASDEL) == 0);
Eric Sandeene9ed9d22007-05-08 13:48:56 +1000581 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000582 &logflags, delta, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 goto done;
584 }
585 /*
586 * Real allocation off the end of the file.
587 */
588 else if (idx == nextents) {
589 if (cur)
590 ASSERT((cur->bc_private.b.flags &
591 XFS_BTCUR_BPRV_WASDEL) == 0);
592 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000593 &logflags, delta, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 goto done;
595 } else {
596 xfs_bmbt_irec_t prev; /* old extent at offset idx */
597
598 /*
599 * Get the record referred to by idx.
600 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100601 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /*
603 * If it's a real allocation record, and the new allocation ends
604 * after the start of the referred to record, then we're filling
605 * in a delayed or unwritten allocation with a real one, or
606 * converting real back to unwritten.
607 */
608 if (!ISNULLSTARTBLOCK(new->br_startblock) &&
609 new->br_startoff + new->br_blockcount > prev.br_startoff) {
610 if (prev.br_state != XFS_EXT_UNWRITTEN &&
611 ISNULLSTARTBLOCK(prev.br_startblock)) {
612 da_old = STARTBLOCKVAL(prev.br_startblock);
613 if (cur)
614 ASSERT(cur->bc_private.b.flags &
615 XFS_BTCUR_BPRV_WASDEL);
616 if ((error = xfs_bmap_add_extent_delay_real(ip,
617 idx, &cur, new, &da_new, first, flist,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000618 &logflags, delta, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 goto done;
620 } else if (new->br_state == XFS_EXT_NORM) {
621 ASSERT(new->br_state == XFS_EXT_NORM);
622 if ((error = xfs_bmap_add_extent_unwritten_real(
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000623 ip, idx, &cur, new, &logflags, delta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 goto done;
625 } else {
626 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
627 if ((error = xfs_bmap_add_extent_unwritten_real(
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000628 ip, idx, &cur, new, &logflags, delta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 goto done;
630 }
631 ASSERT(*curp == cur || *curp == NULL);
632 }
633 /*
634 * Otherwise we're filling in a hole with an allocation.
635 */
636 else {
637 if (cur)
638 ASSERT((cur->bc_private.b.flags &
639 XFS_BTCUR_BPRV_WASDEL) == 0);
640 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000641 new, &logflags, delta, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 goto done;
643 }
644 }
645
646 ASSERT(*curp == cur || *curp == NULL);
647 /*
648 * Convert to a btree if necessary.
649 */
650 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
651 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
652 int tmp_logflags; /* partial log flag return val */
653
654 ASSERT(cur == NULL);
655 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
656 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
657 logflags |= tmp_logflags;
658 if (error)
659 goto done;
660 }
661 /*
662 * Adjust for changes in reserved delayed indirect blocks.
663 * Nothing to do for disk quotas here.
664 */
665 if (da_old || da_new) {
666 xfs_filblks_t nblks;
667
668 nblks = da_new;
669 if (cur)
670 nblks += cur->bc_private.b.allocated;
671 ASSERT(nblks <= da_old);
672 if (nblks < da_old)
673 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100674 (int64_t)(da_old - nblks), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676 /*
677 * Clear out the allocated field, done with it now in any case.
678 */
679 if (cur) {
680 cur->bc_private.b.allocated = 0;
681 *curp = cur;
682 }
683done:
684#ifdef DEBUG
685 if (!error)
686 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
687#endif
688 *logflagsp = logflags;
689 return error;
690}
691
692/*
693 * Called by xfs_bmap_add_extent to handle cases converting a delayed
694 * allocation to a real allocation.
695 */
696STATIC int /* error */
697xfs_bmap_add_extent_delay_real(
698 xfs_inode_t *ip, /* incore inode pointer */
699 xfs_extnum_t idx, /* extent number to update/insert */
700 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100701 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
703 xfs_fsblock_t *first, /* pointer to firstblock variable */
704 xfs_bmap_free_t *flist, /* list of extents to be freed */
705 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000706 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 int rsvd) /* OK to use reserved data block allocation */
708{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 xfs_btree_cur_t *cur; /* btree cursor */
710 int diff; /* temp value */
711 xfs_bmbt_rec_t *ep; /* extent entry for idx */
712 int error; /* error return value */
713#ifdef XFS_BMAP_TRACE
714 static char fname[] = "xfs_bmap_add_extent_delay_real";
715#endif
716 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100717 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 xfs_fileoff_t new_endoff; /* end offset of new entry */
719 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
720 /* left is 0, right is 1, prev is 2 */
721 int rval=0; /* return value (logging flags) */
722 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000723 xfs_filblks_t temp=0; /* value for dnew calculations */
724 xfs_filblks_t temp2=0;/* value for dnew calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 int tmp_rval; /* partial logging flags */
726 enum { /* bit number definitions for state */
727 LEFT_CONTIG, RIGHT_CONTIG,
728 LEFT_FILLING, RIGHT_FILLING,
729 LEFT_DELAY, RIGHT_DELAY,
730 LEFT_VALID, RIGHT_VALID
731 };
732
733#define LEFT r[0]
734#define RIGHT r[1]
735#define PREV r[2]
736#define MASK(b) (1 << (b))
737#define MASK2(a,b) (MASK(a) | MASK(b))
738#define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
739#define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
740#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
741#define STATE_TEST(b) (state & MASK(b))
742#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
743 ((state &= ~MASK(b)), 0))
744#define SWITCH_STATE \
745 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
746
747 /*
748 * Set up a bunch of variables to make the tests simpler.
749 */
750 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100751 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
752 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 xfs_bmbt_get_all(ep, &PREV);
754 new_endoff = new->br_startoff + new->br_blockcount;
755 ASSERT(PREV.br_startoff <= new->br_startoff);
756 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
757 /*
758 * Set flags determining what part of the previous delayed allocation
759 * extent is being replaced by a real allocation.
760 */
761 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
762 STATE_SET(RIGHT_FILLING,
763 PREV.br_startoff + PREV.br_blockcount == new_endoff);
764 /*
765 * Check and set flags if this segment has a left neighbor.
766 * Don't set contiguous if the combined extent would be too large.
767 */
768 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100769 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
771 }
772 STATE_SET(LEFT_CONTIG,
773 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
774 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
775 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
776 LEFT.br_state == new->br_state &&
777 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
778 /*
779 * Check and set flags if this segment has a right neighbor.
780 * Don't set contiguous if the combined extent would be too large.
781 * Also check for all-three-contiguous being too large.
782 */
783 if (STATE_SET_TEST(RIGHT_VALID,
784 idx <
785 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100786 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
788 }
789 STATE_SET(RIGHT_CONTIG,
790 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
791 new_endoff == RIGHT.br_startoff &&
792 new->br_startblock + new->br_blockcount ==
793 RIGHT.br_startblock &&
794 new->br_state == RIGHT.br_state &&
795 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
796 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
797 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
798 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
799 <= MAXEXTLEN));
800 error = 0;
801 /*
802 * Switch out based on the FILLING and CONTIG state bits.
803 */
804 switch (SWITCH_STATE) {
805
806 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
807 /*
808 * Filling in all of a previously delayed allocation extent.
809 * The left and right neighbors are both contiguous with new.
810 */
811 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
812 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100813 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 LEFT.br_blockcount + PREV.br_blockcount +
815 RIGHT.br_blockcount);
816 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
817 XFS_DATA_FORK);
818 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
819 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100820 xfs_iext_remove(ifp, idx, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 ip->i_df.if_lastex = idx - 1;
822 ip->i_d.di_nextents--;
823 if (cur == NULL)
824 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
825 else {
826 rval = XFS_ILOG_CORE;
827 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
828 RIGHT.br_startblock,
829 RIGHT.br_blockcount, &i)))
830 goto done;
831 ASSERT(i == 1);
832 if ((error = xfs_bmbt_delete(cur, &i)))
833 goto done;
834 ASSERT(i == 1);
835 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
836 goto done;
837 ASSERT(i == 1);
838 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
839 LEFT.br_startblock,
840 LEFT.br_blockcount +
841 PREV.br_blockcount +
842 RIGHT.br_blockcount, LEFT.br_state)))
843 goto done;
844 }
845 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000846 /* DELTA: Three in-core extents are replaced by one. */
847 temp = LEFT.br_startoff;
848 temp2 = LEFT.br_blockcount +
849 PREV.br_blockcount +
850 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 break;
852
853 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
854 /*
855 * Filling in all of a previously delayed allocation extent.
856 * The left neighbor is contiguous, the right is not.
857 */
858 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
859 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100860 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 LEFT.br_blockcount + PREV.br_blockcount);
862 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
863 XFS_DATA_FORK);
864 ip->i_df.if_lastex = idx - 1;
865 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
866 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100867 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (cur == NULL)
869 rval = XFS_ILOG_DEXT;
870 else {
871 rval = 0;
872 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
873 LEFT.br_startblock, LEFT.br_blockcount,
874 &i)))
875 goto done;
876 ASSERT(i == 1);
877 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
878 LEFT.br_startblock,
879 LEFT.br_blockcount +
880 PREV.br_blockcount, LEFT.br_state)))
881 goto done;
882 }
883 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000884 /* DELTA: Two in-core extents are replaced by one. */
885 temp = LEFT.br_startoff;
886 temp2 = LEFT.br_blockcount +
887 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 break;
889
890 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
891 /*
892 * Filling in all of a previously delayed allocation extent.
893 * The right neighbor is contiguous, the left is not.
894 */
895 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
896 XFS_DATA_FORK);
897 xfs_bmbt_set_startblock(ep, new->br_startblock);
898 xfs_bmbt_set_blockcount(ep,
899 PREV.br_blockcount + RIGHT.br_blockcount);
900 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
901 XFS_DATA_FORK);
902 ip->i_df.if_lastex = idx;
903 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
904 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100905 xfs_iext_remove(ifp, idx + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (cur == NULL)
907 rval = XFS_ILOG_DEXT;
908 else {
909 rval = 0;
910 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
911 RIGHT.br_startblock,
912 RIGHT.br_blockcount, &i)))
913 goto done;
914 ASSERT(i == 1);
915 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
916 new->br_startblock,
917 PREV.br_blockcount +
918 RIGHT.br_blockcount, PREV.br_state)))
919 goto done;
920 }
921 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000922 /* DELTA: Two in-core extents are replaced by one. */
923 temp = PREV.br_startoff;
924 temp2 = PREV.br_blockcount +
925 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 break;
927
928 case MASK2(LEFT_FILLING, RIGHT_FILLING):
929 /*
930 * Filling in all of a previously delayed allocation extent.
931 * Neither the left nor right neighbors are contiguous with
932 * the new one.
933 */
934 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
935 XFS_DATA_FORK);
936 xfs_bmbt_set_startblock(ep, new->br_startblock);
937 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
938 XFS_DATA_FORK);
939 ip->i_df.if_lastex = idx;
940 ip->i_d.di_nextents++;
941 if (cur == NULL)
942 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
943 else {
944 rval = XFS_ILOG_CORE;
945 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
946 new->br_startblock, new->br_blockcount,
947 &i)))
948 goto done;
949 ASSERT(i == 0);
950 cur->bc_rec.b.br_state = XFS_EXT_NORM;
951 if ((error = xfs_bmbt_insert(cur, &i)))
952 goto done;
953 ASSERT(i == 1);
954 }
955 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000956 /* DELTA: The in-core extent described by new changed type. */
957 temp = new->br_startoff;
958 temp2 = new->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 break;
960
961 case MASK2(LEFT_FILLING, LEFT_CONTIG):
962 /*
963 * Filling in the first part of a previous delayed allocation.
964 * The left neighbor is contiguous.
965 */
966 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
967 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100968 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 LEFT.br_blockcount + new->br_blockcount);
970 xfs_bmbt_set_startoff(ep,
971 PREV.br_startoff + new->br_blockcount);
972 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
973 XFS_DATA_FORK);
974 temp = PREV.br_blockcount - new->br_blockcount;
975 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
976 XFS_DATA_FORK);
977 xfs_bmbt_set_blockcount(ep, temp);
978 ip->i_df.if_lastex = idx - 1;
979 if (cur == NULL)
980 rval = XFS_ILOG_DEXT;
981 else {
982 rval = 0;
983 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
984 LEFT.br_startblock, LEFT.br_blockcount,
985 &i)))
986 goto done;
987 ASSERT(i == 1);
988 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
989 LEFT.br_startblock,
990 LEFT.br_blockcount +
991 new->br_blockcount,
992 LEFT.br_state)))
993 goto done;
994 }
995 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
996 STARTBLOCKVAL(PREV.br_startblock));
997 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
998 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
999 XFS_DATA_FORK);
1000 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001001 /* DELTA: The boundary between two in-core extents moved. */
1002 temp = LEFT.br_startoff;
1003 temp2 = LEFT.br_blockcount +
1004 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 break;
1006
1007 case MASK(LEFT_FILLING):
1008 /*
1009 * Filling in the first part of a previous delayed allocation.
1010 * The left neighbor is not contiguous.
1011 */
1012 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1013 xfs_bmbt_set_startoff(ep, new_endoff);
1014 temp = PREV.br_blockcount - new->br_blockcount;
1015 xfs_bmbt_set_blockcount(ep, temp);
1016 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
1017 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001018 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 ip->i_df.if_lastex = idx;
1020 ip->i_d.di_nextents++;
1021 if (cur == NULL)
1022 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1023 else {
1024 rval = XFS_ILOG_CORE;
1025 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1026 new->br_startblock, new->br_blockcount,
1027 &i)))
1028 goto done;
1029 ASSERT(i == 0);
1030 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1031 if ((error = xfs_bmbt_insert(cur, &i)))
1032 goto done;
1033 ASSERT(i == 1);
1034 }
1035 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1036 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1037 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1038 first, flist, &cur, 1, &tmp_rval,
1039 XFS_DATA_FORK);
1040 rval |= tmp_rval;
1041 if (error)
1042 goto done;
1043 }
1044 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1045 STARTBLOCKVAL(PREV.br_startblock) -
1046 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001047 ep = xfs_iext_get_ext(ifp, idx + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1049 xfs_bmap_trace_post_update(fname, "LF", ip, idx + 1,
1050 XFS_DATA_FORK);
1051 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001052 /* DELTA: One in-core extent is split in two. */
1053 temp = PREV.br_startoff;
1054 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 break;
1056
1057 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1058 /*
1059 * Filling in the last part of a previous delayed allocation.
1060 * The right neighbor is contiguous with the new allocation.
1061 */
1062 temp = PREV.br_blockcount - new->br_blockcount;
1063 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1064 XFS_DATA_FORK);
1065 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1066 XFS_DATA_FORK);
1067 xfs_bmbt_set_blockcount(ep, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001068 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1069 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 new->br_blockcount + RIGHT.br_blockcount,
1071 RIGHT.br_state);
1072 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1073 XFS_DATA_FORK);
1074 ip->i_df.if_lastex = idx + 1;
1075 if (cur == NULL)
1076 rval = XFS_ILOG_DEXT;
1077 else {
1078 rval = 0;
1079 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1080 RIGHT.br_startblock,
1081 RIGHT.br_blockcount, &i)))
1082 goto done;
1083 ASSERT(i == 1);
1084 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1085 new->br_startblock,
1086 new->br_blockcount +
1087 RIGHT.br_blockcount,
1088 RIGHT.br_state)))
1089 goto done;
1090 }
1091 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1092 STARTBLOCKVAL(PREV.br_startblock));
1093 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1094 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1095 XFS_DATA_FORK);
1096 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001097 /* DELTA: The boundary between two in-core extents moved. */
1098 temp = PREV.br_startoff;
1099 temp2 = PREV.br_blockcount +
1100 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 break;
1102
1103 case MASK(RIGHT_FILLING):
1104 /*
1105 * Filling in the last part of a previous delayed allocation.
1106 * The right neighbor is not contiguous.
1107 */
1108 temp = PREV.br_blockcount - new->br_blockcount;
1109 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1110 xfs_bmbt_set_blockcount(ep, temp);
1111 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1112 new, NULL, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001113 xfs_iext_insert(ifp, idx + 1, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 ip->i_df.if_lastex = idx + 1;
1115 ip->i_d.di_nextents++;
1116 if (cur == NULL)
1117 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1118 else {
1119 rval = XFS_ILOG_CORE;
1120 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1121 new->br_startblock, new->br_blockcount,
1122 &i)))
1123 goto done;
1124 ASSERT(i == 0);
1125 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1126 if ((error = xfs_bmbt_insert(cur, &i)))
1127 goto done;
1128 ASSERT(i == 1);
1129 }
1130 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1131 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1132 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1133 first, flist, &cur, 1, &tmp_rval,
1134 XFS_DATA_FORK);
1135 rval |= tmp_rval;
1136 if (error)
1137 goto done;
1138 }
1139 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1140 STARTBLOCKVAL(PREV.br_startblock) -
1141 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001142 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1144 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1145 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001146 /* DELTA: One in-core extent is split in two. */
1147 temp = PREV.br_startoff;
1148 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 break;
1150
1151 case 0:
1152 /*
1153 * Filling in the middle part of a previous delayed allocation.
1154 * Contiguity is impossible here.
1155 * This case is avoided almost all the time.
1156 */
1157 temp = new->br_startoff - PREV.br_startoff;
1158 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1159 xfs_bmbt_set_blockcount(ep, temp);
1160 r[0] = *new;
Lachlan McIlroyd2133712006-11-21 18:55:16 +11001161 r[1].br_state = PREV.br_state;
1162 r[1].br_startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 r[1].br_startoff = new_endoff;
1164 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1165 r[1].br_blockcount = temp2;
1166 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1167 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001168 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 ip->i_df.if_lastex = idx + 1;
1170 ip->i_d.di_nextents++;
1171 if (cur == NULL)
1172 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1173 else {
1174 rval = XFS_ILOG_CORE;
1175 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1176 new->br_startblock, new->br_blockcount,
1177 &i)))
1178 goto done;
1179 ASSERT(i == 0);
1180 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1181 if ((error = xfs_bmbt_insert(cur, &i)))
1182 goto done;
1183 ASSERT(i == 1);
1184 }
1185 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1186 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1187 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1188 first, flist, &cur, 1, &tmp_rval,
1189 XFS_DATA_FORK);
1190 rval |= tmp_rval;
1191 if (error)
1192 goto done;
1193 }
1194 temp = xfs_bmap_worst_indlen(ip, temp);
1195 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1196 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
1197 (cur ? cur->bc_private.b.allocated : 0));
1198 if (diff > 0 &&
David Chinner20f4ebf2007-02-10 18:36:10 +11001199 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /*
1201 * Ick gross gag me with a spoon.
1202 */
1203 ASSERT(0); /* want to see if this ever happens! */
1204 while (diff > 0) {
1205 if (temp) {
1206 temp--;
1207 diff--;
1208 if (!diff ||
1209 !xfs_mod_incore_sb(ip->i_mount,
David Chinner20f4ebf2007-02-10 18:36:10 +11001210 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 break;
1212 }
1213 if (temp2) {
1214 temp2--;
1215 diff--;
1216 if (!diff ||
1217 !xfs_mod_incore_sb(ip->i_mount,
David Chinner20f4ebf2007-02-10 18:36:10 +11001218 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 break;
1220 }
1221 }
1222 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001223 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1225 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1226 xfs_bmap_trace_pre_update(fname, "0", ip, idx + 2,
1227 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001228 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1229 NULLSTARTBLOCK((int)temp2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 xfs_bmap_trace_post_update(fname, "0", ip, idx + 2,
1231 XFS_DATA_FORK);
1232 *dnew = temp + temp2;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001233 /* DELTA: One in-core extent is split in three. */
1234 temp = PREV.br_startoff;
1235 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 break;
1237
1238 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1239 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1240 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1241 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1242 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1243 case MASK(LEFT_CONTIG):
1244 case MASK(RIGHT_CONTIG):
1245 /*
1246 * These cases are all impossible.
1247 */
1248 ASSERT(0);
1249 }
1250 *curp = cur;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001251 if (delta) {
1252 temp2 += temp;
1253 if (delta->xed_startoff > temp)
1254 delta->xed_startoff = temp;
1255 if (delta->xed_blockcount < temp2)
1256 delta->xed_blockcount = temp2;
1257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258done:
1259 *logflagsp = rval;
1260 return error;
1261#undef LEFT
1262#undef RIGHT
1263#undef PREV
1264#undef MASK
1265#undef MASK2
1266#undef MASK3
1267#undef MASK4
1268#undef STATE_SET
1269#undef STATE_TEST
1270#undef STATE_SET_TEST
1271#undef SWITCH_STATE
1272}
1273
1274/*
1275 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1276 * allocation to a real allocation or vice versa.
1277 */
1278STATIC int /* error */
1279xfs_bmap_add_extent_unwritten_real(
1280 xfs_inode_t *ip, /* incore inode pointer */
1281 xfs_extnum_t idx, /* extent number to update/insert */
1282 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001283 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001284 int *logflagsp, /* inode logging flags */
1285 xfs_extdelta_t *delta) /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 xfs_btree_cur_t *cur; /* btree cursor */
1288 xfs_bmbt_rec_t *ep; /* extent entry for idx */
1289 int error; /* error return value */
1290#ifdef XFS_BMAP_TRACE
1291 static char fname[] = "xfs_bmap_add_extent_unwritten_real";
1292#endif
1293 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001294 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 xfs_fileoff_t new_endoff; /* end offset of new entry */
1296 xfs_exntst_t newext; /* new extent state */
1297 xfs_exntst_t oldext; /* old extent state */
1298 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1299 /* left is 0, right is 1, prev is 2 */
1300 int rval=0; /* return value (logging flags) */
1301 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001302 xfs_filblks_t temp=0;
1303 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 enum { /* bit number definitions for state */
1305 LEFT_CONTIG, RIGHT_CONTIG,
1306 LEFT_FILLING, RIGHT_FILLING,
1307 LEFT_DELAY, RIGHT_DELAY,
1308 LEFT_VALID, RIGHT_VALID
1309 };
1310
1311#define LEFT r[0]
1312#define RIGHT r[1]
1313#define PREV r[2]
1314#define MASK(b) (1 << (b))
1315#define MASK2(a,b) (MASK(a) | MASK(b))
1316#define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1317#define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1318#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1319#define STATE_TEST(b) (state & MASK(b))
1320#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1321 ((state &= ~MASK(b)), 0))
1322#define SWITCH_STATE \
1323 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1324
1325 /*
1326 * Set up a bunch of variables to make the tests simpler.
1327 */
1328 error = 0;
1329 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001330 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1331 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 xfs_bmbt_get_all(ep, &PREV);
1333 newext = new->br_state;
1334 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1335 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1336 ASSERT(PREV.br_state == oldext);
1337 new_endoff = new->br_startoff + new->br_blockcount;
1338 ASSERT(PREV.br_startoff <= new->br_startoff);
1339 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1340 /*
1341 * Set flags determining what part of the previous oldext allocation
1342 * extent is being replaced by a newext allocation.
1343 */
1344 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
1345 STATE_SET(RIGHT_FILLING,
1346 PREV.br_startoff + PREV.br_blockcount == new_endoff);
1347 /*
1348 * Check and set flags if this segment has a left neighbor.
1349 * Don't set contiguous if the combined extent would be too large.
1350 */
1351 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001352 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
1354 }
1355 STATE_SET(LEFT_CONTIG,
1356 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1357 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1358 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1359 LEFT.br_state == newext &&
1360 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1361 /*
1362 * Check and set flags if this segment has a right neighbor.
1363 * Don't set contiguous if the combined extent would be too large.
1364 * Also check for all-three-contiguous being too large.
1365 */
1366 if (STATE_SET_TEST(RIGHT_VALID,
1367 idx <
1368 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001369 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
1371 }
1372 STATE_SET(RIGHT_CONTIG,
1373 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1374 new_endoff == RIGHT.br_startoff &&
1375 new->br_startblock + new->br_blockcount ==
1376 RIGHT.br_startblock &&
1377 newext == RIGHT.br_state &&
1378 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1379 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
1380 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
1381 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1382 <= MAXEXTLEN));
1383 /*
1384 * Switch out based on the FILLING and CONTIG state bits.
1385 */
1386 switch (SWITCH_STATE) {
1387
1388 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1389 /*
1390 * Setting all of a previous oldext extent to newext.
1391 * The left and right neighbors are both contiguous with new.
1392 */
1393 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1394 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001395 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 LEFT.br_blockcount + PREV.br_blockcount +
1397 RIGHT.br_blockcount);
1398 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1399 XFS_DATA_FORK);
1400 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
1401 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001402 xfs_iext_remove(ifp, idx, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 ip->i_df.if_lastex = idx - 1;
1404 ip->i_d.di_nextents -= 2;
1405 if (cur == NULL)
1406 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1407 else {
1408 rval = XFS_ILOG_CORE;
1409 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1410 RIGHT.br_startblock,
1411 RIGHT.br_blockcount, &i)))
1412 goto done;
1413 ASSERT(i == 1);
1414 if ((error = xfs_bmbt_delete(cur, &i)))
1415 goto done;
1416 ASSERT(i == 1);
1417 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1418 goto done;
1419 ASSERT(i == 1);
1420 if ((error = xfs_bmbt_delete(cur, &i)))
1421 goto done;
1422 ASSERT(i == 1);
1423 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1424 goto done;
1425 ASSERT(i == 1);
1426 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1427 LEFT.br_startblock,
1428 LEFT.br_blockcount + PREV.br_blockcount +
1429 RIGHT.br_blockcount, LEFT.br_state)))
1430 goto done;
1431 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001432 /* DELTA: Three in-core extents are replaced by one. */
1433 temp = LEFT.br_startoff;
1434 temp2 = LEFT.br_blockcount +
1435 PREV.br_blockcount +
1436 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 break;
1438
1439 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
1440 /*
1441 * Setting all of a previous oldext extent to newext.
1442 * The left neighbor is contiguous, the right is not.
1443 */
1444 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
1445 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001446 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 LEFT.br_blockcount + PREV.br_blockcount);
1448 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
1449 XFS_DATA_FORK);
1450 ip->i_df.if_lastex = idx - 1;
1451 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
1452 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001453 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 ip->i_d.di_nextents--;
1455 if (cur == NULL)
1456 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1457 else {
1458 rval = XFS_ILOG_CORE;
1459 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1460 PREV.br_startblock, PREV.br_blockcount,
1461 &i)))
1462 goto done;
1463 ASSERT(i == 1);
1464 if ((error = xfs_bmbt_delete(cur, &i)))
1465 goto done;
1466 ASSERT(i == 1);
1467 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1468 goto done;
1469 ASSERT(i == 1);
1470 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1471 LEFT.br_startblock,
1472 LEFT.br_blockcount + PREV.br_blockcount,
1473 LEFT.br_state)))
1474 goto done;
1475 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001476 /* DELTA: Two in-core extents are replaced by one. */
1477 temp = LEFT.br_startoff;
1478 temp2 = LEFT.br_blockcount +
1479 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 break;
1481
1482 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
1483 /*
1484 * Setting all of a previous oldext extent to newext.
1485 * The right neighbor is contiguous, the left is not.
1486 */
1487 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
1488 XFS_DATA_FORK);
1489 xfs_bmbt_set_blockcount(ep,
1490 PREV.br_blockcount + RIGHT.br_blockcount);
1491 xfs_bmbt_set_state(ep, newext);
1492 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
1493 XFS_DATA_FORK);
1494 ip->i_df.if_lastex = idx;
1495 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
1496 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001497 xfs_iext_remove(ifp, idx + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 ip->i_d.di_nextents--;
1499 if (cur == NULL)
1500 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1501 else {
1502 rval = XFS_ILOG_CORE;
1503 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1504 RIGHT.br_startblock,
1505 RIGHT.br_blockcount, &i)))
1506 goto done;
1507 ASSERT(i == 1);
1508 if ((error = xfs_bmbt_delete(cur, &i)))
1509 goto done;
1510 ASSERT(i == 1);
1511 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1512 goto done;
1513 ASSERT(i == 1);
1514 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1515 new->br_startblock,
1516 new->br_blockcount + RIGHT.br_blockcount,
1517 newext)))
1518 goto done;
1519 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001520 /* DELTA: Two in-core extents are replaced by one. */
1521 temp = PREV.br_startoff;
1522 temp2 = PREV.br_blockcount +
1523 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 break;
1525
1526 case MASK2(LEFT_FILLING, RIGHT_FILLING):
1527 /*
1528 * Setting all of a previous oldext extent to newext.
1529 * Neither the left nor right neighbors are contiguous with
1530 * the new one.
1531 */
1532 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
1533 XFS_DATA_FORK);
1534 xfs_bmbt_set_state(ep, newext);
1535 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
1536 XFS_DATA_FORK);
1537 ip->i_df.if_lastex = idx;
1538 if (cur == NULL)
1539 rval = XFS_ILOG_DEXT;
1540 else {
1541 rval = 0;
1542 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1543 new->br_startblock, new->br_blockcount,
1544 &i)))
1545 goto done;
1546 ASSERT(i == 1);
1547 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1548 new->br_startblock, new->br_blockcount,
1549 newext)))
1550 goto done;
1551 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001552 /* DELTA: The in-core extent described by new changed type. */
1553 temp = new->br_startoff;
1554 temp2 = new->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 break;
1556
1557 case MASK2(LEFT_FILLING, LEFT_CONTIG):
1558 /*
1559 * Setting the first part of a previous oldext extent to newext.
1560 * The left neighbor is contiguous.
1561 */
1562 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
1563 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001564 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 LEFT.br_blockcount + new->br_blockcount);
1566 xfs_bmbt_set_startoff(ep,
1567 PREV.br_startoff + new->br_blockcount);
1568 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
1569 XFS_DATA_FORK);
1570 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
1571 XFS_DATA_FORK);
1572 xfs_bmbt_set_startblock(ep,
1573 new->br_startblock + new->br_blockcount);
1574 xfs_bmbt_set_blockcount(ep,
1575 PREV.br_blockcount - new->br_blockcount);
1576 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
1577 XFS_DATA_FORK);
1578 ip->i_df.if_lastex = idx - 1;
1579 if (cur == NULL)
1580 rval = XFS_ILOG_DEXT;
1581 else {
1582 rval = 0;
1583 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1584 PREV.br_startblock, PREV.br_blockcount,
1585 &i)))
1586 goto done;
1587 ASSERT(i == 1);
1588 if ((error = xfs_bmbt_update(cur,
1589 PREV.br_startoff + new->br_blockcount,
1590 PREV.br_startblock + new->br_blockcount,
1591 PREV.br_blockcount - new->br_blockcount,
1592 oldext)))
1593 goto done;
1594 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1595 goto done;
1596 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1597 LEFT.br_startblock,
1598 LEFT.br_blockcount + new->br_blockcount,
1599 LEFT.br_state))
1600 goto done;
1601 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001602 /* DELTA: The boundary between two in-core extents moved. */
1603 temp = LEFT.br_startoff;
1604 temp2 = LEFT.br_blockcount +
1605 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 break;
1607
1608 case MASK(LEFT_FILLING):
1609 /*
1610 * Setting the first part of a previous oldext extent to newext.
1611 * The left neighbor is not contiguous.
1612 */
1613 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1614 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1615 xfs_bmbt_set_startoff(ep, new_endoff);
1616 xfs_bmbt_set_blockcount(ep,
1617 PREV.br_blockcount - new->br_blockcount);
1618 xfs_bmbt_set_startblock(ep,
1619 new->br_startblock + new->br_blockcount);
1620 xfs_bmap_trace_post_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1621 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
1622 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001623 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 ip->i_df.if_lastex = idx;
1625 ip->i_d.di_nextents++;
1626 if (cur == NULL)
1627 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1628 else {
1629 rval = XFS_ILOG_CORE;
1630 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1631 PREV.br_startblock, PREV.br_blockcount,
1632 &i)))
1633 goto done;
1634 ASSERT(i == 1);
1635 if ((error = xfs_bmbt_update(cur,
1636 PREV.br_startoff + new->br_blockcount,
1637 PREV.br_startblock + new->br_blockcount,
1638 PREV.br_blockcount - new->br_blockcount,
1639 oldext)))
1640 goto done;
1641 cur->bc_rec.b = *new;
1642 if ((error = xfs_bmbt_insert(cur, &i)))
1643 goto done;
1644 ASSERT(i == 1);
1645 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001646 /* DELTA: One in-core extent is split in two. */
1647 temp = PREV.br_startoff;
1648 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 break;
1650
1651 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1652 /*
1653 * Setting the last part of a previous oldext extent to newext.
1654 * The right neighbor is contiguous with the new allocation.
1655 */
1656 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1657 XFS_DATA_FORK);
1658 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1659 XFS_DATA_FORK);
1660 xfs_bmbt_set_blockcount(ep,
1661 PREV.br_blockcount - new->br_blockcount);
1662 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1663 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001664 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1665 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 new->br_blockcount + RIGHT.br_blockcount, newext);
1667 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1668 XFS_DATA_FORK);
1669 ip->i_df.if_lastex = idx + 1;
1670 if (cur == NULL)
1671 rval = XFS_ILOG_DEXT;
1672 else {
1673 rval = 0;
1674 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1675 PREV.br_startblock,
1676 PREV.br_blockcount, &i)))
1677 goto done;
1678 ASSERT(i == 1);
1679 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1680 PREV.br_startblock,
1681 PREV.br_blockcount - new->br_blockcount,
1682 oldext)))
1683 goto done;
1684 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1685 goto done;
1686 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1687 new->br_startblock,
1688 new->br_blockcount + RIGHT.br_blockcount,
1689 newext)))
1690 goto done;
1691 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001692 /* DELTA: The boundary between two in-core extents moved. */
1693 temp = PREV.br_startoff;
1694 temp2 = PREV.br_blockcount +
1695 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 break;
1697
1698 case MASK(RIGHT_FILLING):
1699 /*
1700 * Setting the last part of a previous oldext extent to newext.
1701 * The right neighbor is not contiguous.
1702 */
1703 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1704 xfs_bmbt_set_blockcount(ep,
1705 PREV.br_blockcount - new->br_blockcount);
1706 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1707 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1708 new, NULL, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001709 xfs_iext_insert(ifp, idx + 1, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 ip->i_df.if_lastex = idx + 1;
1711 ip->i_d.di_nextents++;
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 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1722 PREV.br_startblock,
1723 PREV.br_blockcount - new->br_blockcount,
1724 oldext)))
1725 goto done;
1726 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1727 new->br_startblock, new->br_blockcount,
1728 &i)))
1729 goto done;
1730 ASSERT(i == 0);
1731 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1732 if ((error = xfs_bmbt_insert(cur, &i)))
1733 goto done;
1734 ASSERT(i == 1);
1735 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001736 /* DELTA: One in-core extent is split in two. */
1737 temp = PREV.br_startoff;
1738 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 break;
1740
1741 case 0:
1742 /*
1743 * Setting the middle part of a previous oldext extent to
1744 * newext. Contiguity is impossible here.
1745 * One extent becomes three extents.
1746 */
1747 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1748 xfs_bmbt_set_blockcount(ep,
1749 new->br_startoff - PREV.br_startoff);
1750 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1751 r[0] = *new;
1752 r[1].br_startoff = new_endoff;
1753 r[1].br_blockcount =
1754 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1755 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1756 r[1].br_state = oldext;
1757 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1758 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001759 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 ip->i_df.if_lastex = idx + 1;
1761 ip->i_d.di_nextents += 2;
1762 if (cur == NULL)
1763 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1764 else {
1765 rval = XFS_ILOG_CORE;
1766 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1767 PREV.br_startblock, PREV.br_blockcount,
1768 &i)))
1769 goto done;
1770 ASSERT(i == 1);
1771 /* new right extent - oldext */
1772 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1773 r[1].br_startblock, r[1].br_blockcount,
1774 r[1].br_state)))
1775 goto done;
1776 /* new left extent - oldext */
1777 PREV.br_blockcount =
1778 new->br_startoff - PREV.br_startoff;
1779 cur->bc_rec.b = PREV;
1780 if ((error = xfs_bmbt_insert(cur, &i)))
1781 goto done;
1782 ASSERT(i == 1);
1783 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1784 goto done;
1785 ASSERT(i == 1);
1786 /* new middle extent - newext */
1787 cur->bc_rec.b = *new;
1788 if ((error = xfs_bmbt_insert(cur, &i)))
1789 goto done;
1790 ASSERT(i == 1);
1791 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001792 /* DELTA: One in-core extent is split in three. */
1793 temp = PREV.br_startoff;
1794 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 break;
1796
1797 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1798 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1799 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1800 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1801 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1802 case MASK(LEFT_CONTIG):
1803 case MASK(RIGHT_CONTIG):
1804 /*
1805 * These cases are all impossible.
1806 */
1807 ASSERT(0);
1808 }
1809 *curp = cur;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001810 if (delta) {
1811 temp2 += temp;
1812 if (delta->xed_startoff > temp)
1813 delta->xed_startoff = temp;
1814 if (delta->xed_blockcount < temp2)
1815 delta->xed_blockcount = temp2;
1816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817done:
1818 *logflagsp = rval;
1819 return error;
1820#undef LEFT
1821#undef RIGHT
1822#undef PREV
1823#undef MASK
1824#undef MASK2
1825#undef MASK3
1826#undef MASK4
1827#undef STATE_SET
1828#undef STATE_TEST
1829#undef STATE_SET_TEST
1830#undef SWITCH_STATE
1831}
1832
1833/*
1834 * Called by xfs_bmap_add_extent to handle cases converting a hole
1835 * to a delayed allocation.
1836 */
1837/*ARGSUSED*/
1838STATIC int /* error */
1839xfs_bmap_add_extent_hole_delay(
1840 xfs_inode_t *ip, /* incore inode pointer */
1841 xfs_extnum_t idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001842 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001844 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 int rsvd) /* OK to allocate reserved blocks */
1846{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001847 xfs_bmbt_rec_t *ep; /* extent record for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848#ifdef XFS_BMAP_TRACE
1849 static char fname[] = "xfs_bmap_add_extent_hole_delay";
1850#endif
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001851 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1853 xfs_filblks_t newlen=0; /* new indirect size */
1854 xfs_filblks_t oldlen=0; /* old indirect size */
1855 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1856 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001857 xfs_filblks_t temp=0; /* temp for indirect calculations */
1858 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 enum { /* bit number definitions for state */
1860 LEFT_CONTIG, RIGHT_CONTIG,
1861 LEFT_DELAY, RIGHT_DELAY,
1862 LEFT_VALID, RIGHT_VALID
1863 };
1864
1865#define MASK(b) (1 << (b))
1866#define MASK2(a,b) (MASK(a) | MASK(b))
1867#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1868#define STATE_TEST(b) (state & MASK(b))
1869#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1870 ((state &= ~MASK(b)), 0))
1871#define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1872
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001873 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1874 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 state = 0;
1876 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1877 /*
1878 * Check and set flags if this segment has a left neighbor
1879 */
1880 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001881 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1883 }
1884 /*
1885 * Check and set flags if the current (right) segment exists.
1886 * If it doesn't exist, we're converting the hole at end-of-file.
1887 */
1888 if (STATE_SET_TEST(RIGHT_VALID,
1889 idx <
1890 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1891 xfs_bmbt_get_all(ep, &right);
1892 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1893 }
1894 /*
1895 * Set contiguity flags on the left and right neighbors.
1896 * Don't let extents get too large, even if the pieces are contiguous.
1897 */
1898 STATE_SET(LEFT_CONTIG,
1899 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1900 left.br_startoff + left.br_blockcount == new->br_startoff &&
1901 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1902 STATE_SET(RIGHT_CONTIG,
1903 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1904 new->br_startoff + new->br_blockcount == right.br_startoff &&
1905 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1906 (!STATE_TEST(LEFT_CONTIG) ||
1907 (left.br_blockcount + new->br_blockcount +
1908 right.br_blockcount <= MAXEXTLEN)));
1909 /*
1910 * Switch out based on the contiguity flags.
1911 */
1912 switch (SWITCH_STATE) {
1913
1914 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1915 /*
1916 * New allocation is contiguous with delayed allocations
1917 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001918 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 */
1920 temp = left.br_blockcount + new->br_blockcount +
1921 right.br_blockcount;
1922 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
1923 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001924 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 oldlen = STARTBLOCKVAL(left.br_startblock) +
1926 STARTBLOCKVAL(new->br_startblock) +
1927 STARTBLOCKVAL(right.br_startblock);
1928 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001929 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1930 NULLSTARTBLOCK((int)newlen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
1932 XFS_DATA_FORK);
1933 xfs_bmap_trace_delete(fname, "LC|RC", ip, idx, 1,
1934 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001935 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 ip->i_df.if_lastex = idx - 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001937 /* DELTA: Two in-core extents were replaced by one. */
1938 temp2 = temp;
1939 temp = left.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 break;
1941
1942 case MASK(LEFT_CONTIG):
1943 /*
1944 * New allocation is contiguous with a delayed allocation
1945 * on the left.
1946 * Merge the new allocation with the left neighbor.
1947 */
1948 temp = left.br_blockcount + new->br_blockcount;
1949 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1,
1950 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001951 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 oldlen = STARTBLOCKVAL(left.br_startblock) +
1953 STARTBLOCKVAL(new->br_startblock);
1954 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001955 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1956 NULLSTARTBLOCK((int)newlen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1,
1958 XFS_DATA_FORK);
1959 ip->i_df.if_lastex = idx - 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001960 /* DELTA: One in-core extent grew into a hole. */
1961 temp2 = temp;
1962 temp = left.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 break;
1964
1965 case MASK(RIGHT_CONTIG):
1966 /*
1967 * New allocation is contiguous with a delayed allocation
1968 * on the right.
1969 * Merge the new allocation with the right neighbor.
1970 */
1971 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1972 temp = new->br_blockcount + right.br_blockcount;
1973 oldlen = STARTBLOCKVAL(new->br_startblock) +
1974 STARTBLOCKVAL(right.br_startblock);
1975 newlen = xfs_bmap_worst_indlen(ip, temp);
1976 xfs_bmbt_set_allf(ep, new->br_startoff,
1977 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
1978 xfs_bmap_trace_post_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1979 ip->i_df.if_lastex = idx;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001980 /* DELTA: One in-core extent grew into a hole. */
1981 temp2 = temp;
1982 temp = new->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 break;
1984
1985 case 0:
1986 /*
1987 * New allocation is not contiguous with another
1988 * delayed allocation.
1989 * Insert a new entry.
1990 */
1991 oldlen = newlen = 0;
1992 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
1993 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001994 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 ip->i_df.if_lastex = idx;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001996 /* DELTA: A new in-core extent was added in a hole. */
1997 temp2 = new->br_blockcount;
1998 temp = new->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 break;
2000 }
2001 if (oldlen != newlen) {
2002 ASSERT(oldlen > newlen);
2003 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11002004 (int64_t)(oldlen - newlen), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 /*
2006 * Nothing to do for disk quota accounting here.
2007 */
2008 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002009 if (delta) {
2010 temp2 += temp;
2011 if (delta->xed_startoff > temp)
2012 delta->xed_startoff = temp;
2013 if (delta->xed_blockcount < temp2)
2014 delta->xed_blockcount = temp2;
2015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 *logflagsp = 0;
2017 return 0;
2018#undef MASK
2019#undef MASK2
2020#undef STATE_SET
2021#undef STATE_TEST
2022#undef STATE_SET_TEST
2023#undef SWITCH_STATE
2024}
2025
2026/*
2027 * Called by xfs_bmap_add_extent to handle cases converting a hole
2028 * to a real allocation.
2029 */
2030STATIC int /* error */
2031xfs_bmap_add_extent_hole_real(
2032 xfs_inode_t *ip, /* incore inode pointer */
2033 xfs_extnum_t idx, /* extent number to update/insert */
2034 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002035 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002037 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 int whichfork) /* data or attr fork */
2039{
2040 xfs_bmbt_rec_t *ep; /* pointer to extent entry ins. point */
2041 int error; /* error return value */
2042#ifdef XFS_BMAP_TRACE
2043 static char fname[] = "xfs_bmap_add_extent_hole_real";
2044#endif
2045 int i; /* temp state */
2046 xfs_ifork_t *ifp; /* inode fork pointer */
2047 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2048 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002049 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002051 xfs_filblks_t temp=0;
2052 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 enum { /* bit number definitions for state */
2054 LEFT_CONTIG, RIGHT_CONTIG,
2055 LEFT_DELAY, RIGHT_DELAY,
2056 LEFT_VALID, RIGHT_VALID
2057 };
2058
2059#define MASK(b) (1 << (b))
2060#define MASK2(a,b) (MASK(a) | MASK(b))
2061#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
2062#define STATE_TEST(b) (state & MASK(b))
2063#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
2064 ((state &= ~MASK(b)), 0))
2065#define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
2066
2067 ifp = XFS_IFORK_PTR(ip, whichfork);
2068 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002069 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 state = 0;
2071 /*
2072 * Check and set flags if this segment has a left neighbor.
2073 */
2074 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002075 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
2077 }
2078 /*
2079 * Check and set flags if this segment has a current value.
2080 * Not true if we're inserting into the "hole" at eof.
2081 */
2082 if (STATE_SET_TEST(RIGHT_VALID,
2083 idx <
2084 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
2085 xfs_bmbt_get_all(ep, &right);
2086 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
2087 }
2088 /*
2089 * We're inserting a real allocation between "left" and "right".
2090 * Set the contiguity flags. Don't let extents get too large.
2091 */
2092 STATE_SET(LEFT_CONTIG,
2093 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
2094 left.br_startoff + left.br_blockcount == new->br_startoff &&
2095 left.br_startblock + left.br_blockcount == new->br_startblock &&
2096 left.br_state == new->br_state &&
2097 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
2098 STATE_SET(RIGHT_CONTIG,
2099 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
2100 new->br_startoff + new->br_blockcount == right.br_startoff &&
2101 new->br_startblock + new->br_blockcount ==
2102 right.br_startblock &&
2103 new->br_state == right.br_state &&
2104 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2105 (!STATE_TEST(LEFT_CONTIG) ||
2106 left.br_blockcount + new->br_blockcount +
2107 right.br_blockcount <= MAXEXTLEN));
2108
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002109 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 /*
2111 * Select which case we're in here, and implement it.
2112 */
2113 switch (SWITCH_STATE) {
2114
2115 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
2116 /*
2117 * New allocation is contiguous with real allocations on the
2118 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002119 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 */
2121 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
2122 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002123 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 left.br_blockcount + new->br_blockcount +
2125 right.br_blockcount);
2126 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
2127 whichfork);
2128 xfs_bmap_trace_delete(fname, "LC|RC", ip,
2129 idx, 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002130 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 ifp->if_lastex = idx - 1;
2132 XFS_IFORK_NEXT_SET(ip, whichfork,
2133 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2134 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002135 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2136 } else {
2137 rval = XFS_ILOG_CORE;
2138 if ((error = xfs_bmbt_lookup_eq(cur,
2139 right.br_startoff,
2140 right.br_startblock,
2141 right.br_blockcount, &i)))
2142 goto done;
2143 ASSERT(i == 1);
2144 if ((error = xfs_bmbt_delete(cur, &i)))
2145 goto done;
2146 ASSERT(i == 1);
2147 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2148 goto done;
2149 ASSERT(i == 1);
2150 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2151 left.br_startblock,
2152 left.br_blockcount +
2153 new->br_blockcount +
2154 right.br_blockcount,
2155 left.br_state)))
2156 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002158 /* DELTA: Two in-core extents were replaced by one. */
2159 temp = left.br_startoff;
2160 temp2 = left.br_blockcount +
2161 new->br_blockcount +
2162 right.br_blockcount;
2163 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 case MASK(LEFT_CONTIG):
2166 /*
2167 * New allocation is contiguous with a real allocation
2168 * on the left.
2169 * Merge the new allocation with the left neighbor.
2170 */
2171 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002172 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 left.br_blockcount + new->br_blockcount);
2174 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1, whichfork);
2175 ifp->if_lastex = idx - 1;
2176 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002177 rval = XFS_ILOG_FEXT(whichfork);
2178 } else {
2179 rval = 0;
2180 if ((error = xfs_bmbt_lookup_eq(cur,
2181 left.br_startoff,
2182 left.br_startblock,
2183 left.br_blockcount, &i)))
2184 goto done;
2185 ASSERT(i == 1);
2186 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2187 left.br_startblock,
2188 left.br_blockcount +
2189 new->br_blockcount,
2190 left.br_state)))
2191 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002193 /* DELTA: One in-core extent grew. */
2194 temp = left.br_startoff;
2195 temp2 = left.br_blockcount +
2196 new->br_blockcount;
2197 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 case MASK(RIGHT_CONTIG):
2200 /*
2201 * New allocation is contiguous with a real allocation
2202 * on the right.
2203 * Merge the new allocation with the right neighbor.
2204 */
2205 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, whichfork);
2206 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2207 new->br_blockcount + right.br_blockcount,
2208 right.br_state);
2209 xfs_bmap_trace_post_update(fname, "RC", ip, idx, whichfork);
2210 ifp->if_lastex = idx;
2211 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002212 rval = XFS_ILOG_FEXT(whichfork);
2213 } else {
2214 rval = 0;
2215 if ((error = xfs_bmbt_lookup_eq(cur,
2216 right.br_startoff,
2217 right.br_startblock,
2218 right.br_blockcount, &i)))
2219 goto done;
2220 ASSERT(i == 1);
2221 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2222 new->br_startblock,
2223 new->br_blockcount +
2224 right.br_blockcount,
2225 right.br_state)))
2226 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002228 /* DELTA: One in-core extent grew. */
2229 temp = new->br_startoff;
2230 temp2 = new->br_blockcount +
2231 right.br_blockcount;
2232 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
2234 case 0:
2235 /*
2236 * New allocation is not contiguous with another
2237 * real allocation.
2238 * Insert a new entry.
2239 */
2240 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
2241 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002242 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 ifp->if_lastex = idx;
2244 XFS_IFORK_NEXT_SET(ip, whichfork,
2245 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2246 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002247 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2248 } else {
2249 rval = XFS_ILOG_CORE;
2250 if ((error = xfs_bmbt_lookup_eq(cur,
2251 new->br_startoff,
2252 new->br_startblock,
2253 new->br_blockcount, &i)))
2254 goto done;
2255 ASSERT(i == 0);
2256 cur->bc_rec.b.br_state = new->br_state;
2257 if ((error = xfs_bmbt_insert(cur, &i)))
2258 goto done;
2259 ASSERT(i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002261 /* DELTA: A new extent was added in a hole. */
2262 temp = new->br_startoff;
2263 temp2 = new->br_blockcount;
2264 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002266 if (delta) {
2267 temp2 += temp;
2268 if (delta->xed_startoff > temp)
2269 delta->xed_startoff = temp;
2270 if (delta->xed_blockcount < temp2)
2271 delta->xed_blockcount = temp2;
2272 }
2273done:
2274 *logflagsp = rval;
2275 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276#undef MASK
2277#undef MASK2
2278#undef STATE_SET
2279#undef STATE_TEST
2280#undef STATE_SET_TEST
2281#undef SWITCH_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
Nathan Scottdd9f4382006-01-11 15:28:28 +11002284/*
2285 * Adjust the size of the new extent based on di_extsize and rt extsize.
2286 */
2287STATIC int
2288xfs_bmap_extsize_align(
2289 xfs_mount_t *mp,
2290 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2291 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2292 xfs_extlen_t extsz, /* align to this extent size */
2293 int rt, /* is this a realtime inode? */
2294 int eof, /* is extent at end-of-file? */
2295 int delay, /* creating delalloc extent? */
2296 int convert, /* overwriting unwritten extent? */
2297 xfs_fileoff_t *offp, /* in/out: aligned offset */
2298 xfs_extlen_t *lenp) /* in/out: aligned length */
2299{
2300 xfs_fileoff_t orig_off; /* original offset */
2301 xfs_extlen_t orig_alen; /* original length */
2302 xfs_fileoff_t orig_end; /* original off+len */
2303 xfs_fileoff_t nexto; /* next file offset */
2304 xfs_fileoff_t prevo; /* previous file offset */
2305 xfs_fileoff_t align_off; /* temp for offset */
2306 xfs_extlen_t align_alen; /* temp for length */
2307 xfs_extlen_t temp; /* temp for calculations */
2308
2309 if (convert)
2310 return 0;
2311
2312 orig_off = align_off = *offp;
2313 orig_alen = align_alen = *lenp;
2314 orig_end = orig_off + orig_alen;
2315
2316 /*
2317 * If this request overlaps an existing extent, then don't
2318 * attempt to perform any additional alignment.
2319 */
2320 if (!delay && !eof &&
2321 (orig_off >= gotp->br_startoff) &&
2322 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2323 return 0;
2324 }
2325
2326 /*
2327 * If the file offset is unaligned vs. the extent size
2328 * we need to align it. This will be possible unless
2329 * the file was previously written with a kernel that didn't
2330 * perform this alignment, or if a truncate shot us in the
2331 * foot.
2332 */
2333 temp = do_mod(orig_off, extsz);
2334 if (temp) {
2335 align_alen += temp;
2336 align_off -= temp;
2337 }
2338 /*
2339 * Same adjustment for the end of the requested area.
2340 */
2341 if ((temp = (align_alen % extsz))) {
2342 align_alen += extsz - temp;
2343 }
2344 /*
2345 * If the previous block overlaps with this proposed allocation
2346 * then move the start forward without adjusting the length.
2347 */
2348 if (prevp->br_startoff != NULLFILEOFF) {
2349 if (prevp->br_startblock == HOLESTARTBLOCK)
2350 prevo = prevp->br_startoff;
2351 else
2352 prevo = prevp->br_startoff + prevp->br_blockcount;
2353 } else
2354 prevo = 0;
2355 if (align_off != orig_off && align_off < prevo)
2356 align_off = prevo;
2357 /*
2358 * If the next block overlaps with this proposed allocation
2359 * then move the start back without adjusting the length,
2360 * but not before offset 0.
2361 * This may of course make the start overlap previous block,
2362 * and if we hit the offset 0 limit then the next block
2363 * can still overlap too.
2364 */
2365 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2366 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2367 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2368 nexto = gotp->br_startoff + gotp->br_blockcount;
2369 else
2370 nexto = gotp->br_startoff;
2371 } else
2372 nexto = NULLFILEOFF;
2373 if (!eof &&
2374 align_off + align_alen != orig_end &&
2375 align_off + align_alen > nexto)
2376 align_off = nexto > align_alen ? nexto - align_alen : 0;
2377 /*
2378 * If we're now overlapping the next or previous extent that
2379 * means we can't fit an extsz piece in this hole. Just move
2380 * the start forward to the first valid spot and set
2381 * the length so we hit the end.
2382 */
2383 if (align_off != orig_off && align_off < prevo)
2384 align_off = prevo;
2385 if (align_off + align_alen != orig_end &&
2386 align_off + align_alen > nexto &&
2387 nexto != NULLFILEOFF) {
2388 ASSERT(nexto > prevo);
2389 align_alen = nexto - align_off;
2390 }
2391
2392 /*
2393 * If realtime, and the result isn't a multiple of the realtime
2394 * extent size we need to remove blocks until it is.
2395 */
2396 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2397 /*
2398 * We're not covering the original request, or
2399 * we won't be able to once we fix the length.
2400 */
2401 if (orig_off < align_off ||
2402 orig_end > align_off + align_alen ||
2403 align_alen - temp < orig_alen)
2404 return XFS_ERROR(EINVAL);
2405 /*
2406 * Try to fix it by moving the start up.
2407 */
2408 if (align_off + temp <= orig_off) {
2409 align_alen -= temp;
2410 align_off += temp;
2411 }
2412 /*
2413 * Try to fix it by moving the end in.
2414 */
2415 else if (align_off + align_alen - temp >= orig_end)
2416 align_alen -= temp;
2417 /*
2418 * Set the start to the minimum then trim the length.
2419 */
2420 else {
2421 align_alen -= orig_off - align_off;
2422 align_off = orig_off;
2423 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2424 }
2425 /*
2426 * Result doesn't cover the request, fail it.
2427 */
2428 if (orig_off < align_off || orig_end > align_off + align_alen)
2429 return XFS_ERROR(EINVAL);
2430 } else {
2431 ASSERT(orig_off >= align_off);
2432 ASSERT(orig_end <= align_off + align_alen);
2433 }
2434
2435#ifdef DEBUG
2436 if (!eof && gotp->br_startoff != NULLFILEOFF)
2437 ASSERT(align_off + align_alen <= gotp->br_startoff);
2438 if (prevp->br_startoff != NULLFILEOFF)
2439 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2440#endif
2441
2442 *lenp = align_alen;
2443 *offp = align_off;
2444 return 0;
2445}
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447#define XFS_ALLOC_GAP_UNITS 4
2448
Nathan Scottdd9f4382006-01-11 15:28:28 +11002449STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11002450xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2452{
2453 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2455 xfs_mount_t *mp; /* mount point structure */
2456 int nullfb; /* true if ap->firstblock isn't set */
2457 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
2459#define ISVALID(x,y) \
2460 (rt ? \
2461 (x) < mp->m_sb.sb_rblocks : \
2462 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2463 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2464 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 mp = ap->ip->i_mount;
2467 nullfb = ap->firstblock == NULLFSBLOCK;
2468 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2469 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 /*
2471 * If allocating at eof, and there's a previous real block,
2472 * try to use it's last block as our starting point.
2473 */
2474 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2475 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2476 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2477 ap->prevp->br_startblock)) {
2478 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2479 /*
2480 * Adjust for the gap between prevp and us.
2481 */
2482 adjust = ap->off -
2483 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2484 if (adjust &&
2485 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2486 ap->rval += adjust;
2487 }
2488 /*
2489 * If not at eof, then compare the two neighbor blocks.
2490 * Figure out whether either one gives us a good starting point,
2491 * and pick the better one.
2492 */
2493 else if (!ap->eof) {
2494 xfs_fsblock_t gotbno; /* right side block number */
2495 xfs_fsblock_t gotdiff=0; /* right side difference */
2496 xfs_fsblock_t prevbno; /* left side block number */
2497 xfs_fsblock_t prevdiff=0; /* left side difference */
2498
2499 /*
2500 * If there's a previous (left) block, select a requested
2501 * start block based on it.
2502 */
2503 if (ap->prevp->br_startoff != NULLFILEOFF &&
2504 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2505 (prevbno = ap->prevp->br_startblock +
2506 ap->prevp->br_blockcount) &&
2507 ISVALID(prevbno, ap->prevp->br_startblock)) {
2508 /*
2509 * Calculate gap to end of previous block.
2510 */
2511 adjust = prevdiff = ap->off -
2512 (ap->prevp->br_startoff +
2513 ap->prevp->br_blockcount);
2514 /*
2515 * Figure the startblock based on the previous block's
2516 * end and the gap size.
2517 * Heuristic!
2518 * If the gap is large relative to the piece we're
2519 * allocating, or using it gives us an invalid block
2520 * number, then just use the end of the previous block.
2521 */
2522 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2523 ISVALID(prevbno + prevdiff,
2524 ap->prevp->br_startblock))
2525 prevbno += adjust;
2526 else
2527 prevdiff += adjust;
2528 /*
2529 * If the firstblock forbids it, can't use it,
2530 * must use default.
2531 */
2532 if (!rt && !nullfb &&
2533 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2534 prevbno = NULLFSBLOCK;
2535 }
2536 /*
2537 * No previous block or can't follow it, just default.
2538 */
2539 else
2540 prevbno = NULLFSBLOCK;
2541 /*
2542 * If there's a following (right) block, select a requested
2543 * start block based on it.
2544 */
2545 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
2546 /*
2547 * Calculate gap to start of next block.
2548 */
2549 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2550 /*
2551 * Figure the startblock based on the next block's
2552 * start and the gap size.
2553 */
2554 gotbno = ap->gotp->br_startblock;
2555 /*
2556 * Heuristic!
2557 * If the gap is large relative to the piece we're
2558 * allocating, or using it gives us an invalid block
2559 * number, then just use the start of the next block
2560 * offset by our length.
2561 */
2562 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2563 ISVALID(gotbno - gotdiff, gotbno))
2564 gotbno -= adjust;
2565 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2566 gotbno -= ap->alen;
2567 gotdiff += adjust - ap->alen;
2568 } else
2569 gotdiff += adjust;
2570 /*
2571 * If the firstblock forbids it, can't use it,
2572 * must use default.
2573 */
2574 if (!rt && !nullfb &&
2575 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2576 gotbno = NULLFSBLOCK;
2577 }
2578 /*
2579 * No next block, just default.
2580 */
2581 else
2582 gotbno = NULLFSBLOCK;
2583 /*
2584 * If both valid, pick the better one, else the only good
2585 * one, else ap->rval is already set (to 0 or the inode block).
2586 */
2587 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2588 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2589 else if (prevbno != NULLFSBLOCK)
2590 ap->rval = prevbno;
2591 else if (gotbno != NULLFSBLOCK)
2592 ap->rval = gotbno;
2593 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002594#undef ISVALID
2595 return 0;
2596}
2597
2598STATIC int
2599xfs_bmap_rtalloc(
2600 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2601{
2602 xfs_alloctype_t atype = 0; /* type for allocation routines */
2603 int error; /* error return value */
2604 xfs_mount_t *mp; /* mount point structure */
2605 xfs_extlen_t prod = 0; /* product factor for allocators */
2606 xfs_extlen_t ralen = 0; /* realtime allocation length */
2607 xfs_extlen_t align; /* minimum allocation alignment */
2608 xfs_rtblock_t rtx; /* realtime extent number */
2609 xfs_rtblock_t rtb;
2610
2611 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002612 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002613 prod = align / mp->m_sb.sb_rextsize;
2614 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2615 align, 1, ap->eof, 0,
2616 ap->conv, &ap->off, &ap->alen);
2617 if (error)
2618 return error;
2619 ASSERT(ap->alen);
2620 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2621
2622 /*
2623 * If the offset & length are not perfectly aligned
2624 * then kill prod, it will just get us in trouble.
2625 */
2626 if (do_mod(ap->off, align) || ap->alen % align)
2627 prod = 1;
2628 /*
2629 * Set ralen to be the actual requested length in rtextents.
2630 */
2631 ralen = ap->alen / mp->m_sb.sb_rextsize;
2632 /*
2633 * If the old value was close enough to MAXEXTLEN that
2634 * we rounded up to it, cut it back so it's valid again.
2635 * Note that if it's a really large request (bigger than
2636 * MAXEXTLEN), we don't hear about that number, and can't
2637 * adjust the starting point to match it.
2638 */
2639 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2640 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2641 /*
2642 * If it's an allocation to an empty file at offset 0,
2643 * pick an extent that will space things out in the rt area.
2644 */
2645 if (ap->eof && ap->off == 0) {
2646 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2647 if (error)
2648 return error;
2649 ap->rval = rtx * mp->m_sb.sb_rextsize;
2650 } else {
2651 ap->rval = 0;
2652 }
2653
2654 xfs_bmap_adjacent(ap);
2655
2656 /*
2657 * Realtime allocation, done through xfs_rtallocate_extent.
2658 */
2659 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2660 do_div(ap->rval, mp->m_sb.sb_rextsize);
2661 rtb = ap->rval;
2662 ap->alen = ralen;
2663 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2664 &ralen, atype, ap->wasdel, prod, &rtb)))
2665 return error;
2666 if (rtb == NULLFSBLOCK && prod > 1 &&
2667 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2668 ap->alen, &ralen, atype,
2669 ap->wasdel, 1, &rtb)))
2670 return error;
2671 ap->rval = rtb;
2672 if (ap->rval != NULLFSBLOCK) {
2673 ap->rval *= mp->m_sb.sb_rextsize;
2674 ralen *= mp->m_sb.sb_rextsize;
2675 ap->alen = ralen;
2676 ap->ip->i_d.di_nblocks += ralen;
2677 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2678 if (ap->wasdel)
2679 ap->ip->i_delayed_blks -= ralen;
2680 /*
2681 * Adjust the disk quota also. This was reserved
2682 * earlier.
2683 */
2684 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2685 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2686 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2687 } else {
2688 ap->alen = 0;
2689 }
2690 return 0;
2691}
2692
2693STATIC int
2694xfs_bmap_btalloc(
2695 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2696{
2697 xfs_mount_t *mp; /* mount point structure */
2698 xfs_alloctype_t atype = 0; /* type for allocation routines */
2699 xfs_extlen_t align; /* minimum allocation alignment */
2700 xfs_agnumber_t ag;
2701 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2702 xfs_agnumber_t startag;
2703 xfs_alloc_arg_t args;
2704 xfs_extlen_t blen;
2705 xfs_extlen_t delta;
2706 xfs_extlen_t longest;
2707 xfs_extlen_t need;
2708 xfs_extlen_t nextminlen = 0;
2709 xfs_perag_t *pag;
2710 int nullfb; /* true if ap->firstblock isn't set */
2711 int isaligned;
2712 int notinit;
2713 int tryagain;
2714 int error;
2715
2716 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002717 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002718 if (unlikely(align)) {
2719 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2720 align, 0, ap->eof, 0, ap->conv,
2721 &ap->off, &ap->alen);
2722 ASSERT(!error);
2723 ASSERT(ap->alen);
2724 }
2725 nullfb = ap->firstblock == NULLFSBLOCK;
2726 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2727 if (nullfb)
2728 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2729 else
2730 ap->rval = ap->firstblock;
2731
2732 xfs_bmap_adjacent(ap);
2733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 /*
2735 * If allowed, use ap->rval; otherwise must use firstblock since
2736 * it's in the right allocation group.
2737 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002738 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 ;
2740 else
2741 ap->rval = ap->firstblock;
2742 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 * Normal allocation, done through xfs_alloc_vextent.
2744 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002745 tryagain = isaligned = 0;
2746 args.tp = ap->tp;
2747 args.mp = mp;
2748 args.fsbno = ap->rval;
2749 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
Yingping Lud210a282006-06-09 14:55:18 +10002750 args.firstblock = ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002751 blen = 0;
2752 if (nullfb) {
2753 args.type = XFS_ALLOCTYPE_START_BNO;
2754 args.total = ap->total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 /*
Nathan Scotta365bdd2006-03-14 13:34:16 +11002756 * Find the longest available space.
2757 * We're going to try for the whole allocation at once.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002759 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2760 notinit = 0;
2761 down_read(&mp->m_peraglock);
2762 while (blen < ap->alen) {
2763 pag = &mp->m_perag[ag];
2764 if (!pag->pagf_init &&
2765 (error = xfs_alloc_pagf_init(mp, args.tp,
2766 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2767 up_read(&mp->m_peraglock);
2768 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002770 /*
2771 * See xfs_alloc_fix_freelist...
2772 */
2773 if (pag->pagf_init) {
2774 need = XFS_MIN_FREELIST_PAG(pag, mp);
2775 delta = need > pag->pagf_flcount ?
2776 need - pag->pagf_flcount : 0;
2777 longest = (pag->pagf_longest > delta) ?
2778 (pag->pagf_longest - delta) :
2779 (pag->pagf_flcount > 0 ||
2780 pag->pagf_longest > 0);
2781 if (blen < longest)
2782 blen = longest;
2783 } else
2784 notinit = 1;
2785 if (++ag == mp->m_sb.sb_agcount)
2786 ag = 0;
2787 if (ag == startag)
2788 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002790 up_read(&mp->m_peraglock);
2791 /*
2792 * Since the above loop did a BUF_TRYLOCK, it is
2793 * possible that there is space for this request.
2794 */
2795 if (notinit || blen < ap->minlen)
2796 args.minlen = ap->minlen;
2797 /*
2798 * If the best seen length is less than the request
2799 * length, use the best as the minimum.
2800 */
2801 else if (blen < ap->alen)
2802 args.minlen = blen;
2803 /*
2804 * Otherwise we've seen an extent as big as alen,
2805 * use that as the minimum.
2806 */
2807 else
2808 args.minlen = ap->alen;
2809 } else if (ap->low) {
Yingping Lud210a282006-06-09 14:55:18 +10002810 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002811 args.total = args.minlen = ap->minlen;
2812 } else {
2813 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2814 args.total = ap->total;
2815 args.minlen = ap->minlen;
2816 }
David Chinner957d0eb2007-06-18 16:50:37 +10002817 /* apply extent size hints if obtained earlier */
2818 if (unlikely(align)) {
2819 args.prod = align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002820 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2821 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Nathan Scott6fe90e6d2006-06-19 08:40:12 +10002822 } else if (mp->m_sb.sb_blocksize >= NBPP) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11002823 args.prod = 1;
2824 args.mod = 0;
2825 } else {
2826 args.prod = NBPP >> mp->m_sb.sb_blocklog;
2827 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2828 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2829 }
2830 /*
2831 * If we are not low on available data blocks, and the
2832 * underlying logical volume manager is a stripe, and
2833 * the file offset is zero then try to allocate data
2834 * blocks on stripe unit boundary.
2835 * NOTE: ap->aeof is only set if the allocation length
2836 * is >= the stripe unit and the allocation offset is
2837 * at the end of file.
2838 */
2839 if (!ap->low && ap->aeof) {
2840 if (!ap->off) {
2841 args.alignment = mp->m_dalign;
2842 atype = args.type;
2843 isaligned = 1;
2844 /*
2845 * Adjust for alignment
2846 */
2847 if (blen > args.alignment && blen <= ap->alen)
2848 args.minlen = blen - args.alignment;
2849 args.minalignslop = 0;
2850 } else {
2851 /*
2852 * First try an exact bno allocation.
2853 * If it fails then do a near or start bno
2854 * allocation with alignment turned on.
2855 */
2856 atype = args.type;
2857 tryagain = 1;
2858 args.type = XFS_ALLOCTYPE_THIS_BNO;
2859 args.alignment = 1;
2860 /*
2861 * Compute the minlen+alignment for the
2862 * next case. Set slop so that the value
2863 * of minlen+alignment+slop doesn't go up
2864 * between the calls.
2865 */
2866 if (blen > mp->m_dalign && blen <= ap->alen)
2867 nextminlen = blen - mp->m_dalign;
2868 else
2869 nextminlen = args.minlen;
2870 if (nextminlen + mp->m_dalign > args.minlen + 1)
2871 args.minalignslop =
2872 nextminlen + mp->m_dalign -
2873 args.minlen - 1;
2874 else
2875 args.minalignslop = 0;
2876 }
2877 } else {
2878 args.alignment = 1;
2879 args.minalignslop = 0;
2880 }
2881 args.minleft = ap->minleft;
2882 args.wasdel = ap->wasdel;
2883 args.isfl = 0;
2884 args.userdata = ap->userdata;
2885 if ((error = xfs_alloc_vextent(&args)))
2886 return error;
2887 if (tryagain && args.fsbno == NULLFSBLOCK) {
2888 /*
2889 * Exact allocation failed. Now try with alignment
2890 * turned on.
2891 */
2892 args.type = atype;
2893 args.fsbno = ap->rval;
2894 args.alignment = mp->m_dalign;
2895 args.minlen = nextminlen;
2896 args.minalignslop = 0;
2897 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if ((error = xfs_alloc_vextent(&args)))
2899 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002900 }
2901 if (isaligned && args.fsbno == NULLFSBLOCK) {
2902 /*
2903 * allocation failed, so turn off alignment and
2904 * try again.
2905 */
2906 args.type = atype;
2907 args.fsbno = ap->rval;
2908 args.alignment = 0;
2909 if ((error = xfs_alloc_vextent(&args)))
2910 return error;
2911 }
2912 if (args.fsbno == NULLFSBLOCK && nullfb &&
2913 args.minlen > ap->minlen) {
2914 args.minlen = ap->minlen;
2915 args.type = XFS_ALLOCTYPE_START_BNO;
2916 args.fsbno = ap->rval;
2917 if ((error = xfs_alloc_vextent(&args)))
2918 return error;
2919 }
2920 if (args.fsbno == NULLFSBLOCK && nullfb) {
2921 args.fsbno = 0;
2922 args.type = XFS_ALLOCTYPE_FIRST_AG;
2923 args.total = ap->minlen;
2924 args.minleft = 0;
2925 if ((error = xfs_alloc_vextent(&args)))
2926 return error;
2927 ap->low = 1;
2928 }
2929 if (args.fsbno != NULLFSBLOCK) {
2930 ap->firstblock = ap->rval = args.fsbno;
2931 ASSERT(nullfb || fb_agno == args.agno ||
2932 (ap->low && fb_agno < args.agno));
2933 ap->alen = args.len;
2934 ap->ip->i_d.di_nblocks += args.len;
2935 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2936 if (ap->wasdel)
2937 ap->ip->i_delayed_blks -= args.len;
2938 /*
2939 * Adjust the disk quota also. This was reserved
2940 * earlier.
2941 */
2942 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2943 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2944 XFS_TRANS_DQ_BCOUNT,
2945 (long) args.len);
2946 } else {
2947 ap->rval = NULLFSBLOCK;
2948 ap->alen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
2950 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002951}
2952
2953/*
2954 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2955 * It figures out where to ask the underlying allocator to put the new extent.
2956 */
2957STATIC int
2958xfs_bmap_alloc(
2959 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2960{
2961 if ((ap->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) && ap->userdata)
2962 return xfs_bmap_rtalloc(ap);
2963 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964}
2965
2966/*
2967 * Transform a btree format file with only one leaf node, where the
2968 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002969 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 * give up the space for the btree root and pitch the leaf block.
2971 */
2972STATIC int /* error */
2973xfs_bmap_btree_to_extents(
2974 xfs_trans_t *tp, /* transaction pointer */
2975 xfs_inode_t *ip, /* incore inode pointer */
2976 xfs_btree_cur_t *cur, /* btree cursor */
2977 int *logflagsp, /* inode logging flags */
2978 int whichfork) /* data or attr fork */
2979{
2980 /* REFERENCED */
2981 xfs_bmbt_block_t *cblock;/* child btree block */
2982 xfs_fsblock_t cbno; /* child block number */
2983 xfs_buf_t *cbp; /* child block's buffer */
2984 int error; /* error return value */
2985 xfs_ifork_t *ifp; /* inode fork data */
2986 xfs_mount_t *mp; /* mount point structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10002987 __be64 *pp; /* ptr to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 xfs_bmbt_block_t *rblock;/* root btree block */
2989
2990 ifp = XFS_IFORK_PTR(ip, whichfork);
2991 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2992 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2993 rblock = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11002994 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2995 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
2997 mp = ip->i_mount;
2998 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10002999 cbno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 *logflagsp = 0;
3001#ifdef DEBUG
Christoph Hellwig576039c2006-09-28 10:58:06 +10003002 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return error;
3004#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
3006 XFS_BMAP_BTREE_REF)))
3007 return error;
3008 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
3009 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
3010 return error;
3011 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
3012 ip->i_d.di_nblocks--;
3013 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
3014 xfs_trans_binval(tp, cbp);
3015 if (cur->bc_bufs[0] == cbp)
3016 cur->bc_bufs[0] = NULL;
3017 xfs_iroot_realloc(ip, -1, whichfork);
3018 ASSERT(ifp->if_broot == NULL);
3019 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
3020 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3021 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
3022 return 0;
3023}
3024
3025/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003026 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 * after removing space (or undoing a delayed allocation).
3028 */
3029STATIC int /* error */
3030xfs_bmap_del_extent(
3031 xfs_inode_t *ip, /* incore inode pointer */
3032 xfs_trans_t *tp, /* current transaction pointer */
3033 xfs_extnum_t idx, /* extent number to update/delete */
3034 xfs_bmap_free_t *flist, /* list of extents to be freed */
3035 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003036 xfs_bmbt_irec_t *del, /* data to remove from extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003038 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 int whichfork, /* data or attr fork */
3040 int rsvd) /* OK to allocate reserved blocks */
3041{
3042 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
3043 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
3044 xfs_fsblock_t del_endblock=0; /* first block past del */
3045 xfs_fileoff_t del_endoff; /* first offset past del */
3046 int delay; /* current block is delayed allocated */
3047 int do_fx; /* free extent at end of routine */
3048 xfs_bmbt_rec_t *ep; /* current extent entry pointer */
3049 int error; /* error return value */
3050 int flags; /* inode logging flags */
3051#ifdef XFS_BMAP_TRACE
3052 static char fname[] = "xfs_bmap_del_extent";
3053#endif
3054 xfs_bmbt_irec_t got; /* current extent entry */
3055 xfs_fileoff_t got_endoff; /* first offset past got */
3056 int i; /* temp state */
3057 xfs_ifork_t *ifp; /* inode fork pointer */
3058 xfs_mount_t *mp; /* mount structure */
3059 xfs_filblks_t nblks; /* quota/sb block count */
3060 xfs_bmbt_irec_t new; /* new record to be inserted */
3061 /* REFERENCED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 uint qfield; /* quota field to update */
3063 xfs_filblks_t temp; /* for indirect length calculations */
3064 xfs_filblks_t temp2; /* for indirect length calculations */
3065
3066 XFS_STATS_INC(xs_del_exlist);
3067 mp = ip->i_mount;
3068 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003069 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
3070 (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 ASSERT(del->br_blockcount > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003072 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 xfs_bmbt_get_all(ep, &got);
3074 ASSERT(got.br_startoff <= del->br_startoff);
3075 del_endoff = del->br_startoff + del->br_blockcount;
3076 got_endoff = got.br_startoff + got.br_blockcount;
3077 ASSERT(got_endoff >= del_endoff);
3078 delay = ISNULLSTARTBLOCK(got.br_startblock);
3079 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
3080 flags = 0;
3081 qfield = 0;
3082 error = 0;
3083 /*
3084 * If deleting a real allocation, must free up the disk space.
3085 */
3086 if (!delay) {
3087 flags = XFS_ILOG_CORE;
3088 /*
3089 * Realtime allocation. Free it and record di_nblocks update.
3090 */
3091 if (whichfork == XFS_DATA_FORK &&
3092 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
3093 xfs_fsblock_t bno;
3094 xfs_filblks_t len;
3095
3096 ASSERT(do_mod(del->br_blockcount,
3097 mp->m_sb.sb_rextsize) == 0);
3098 ASSERT(do_mod(del->br_startblock,
3099 mp->m_sb.sb_rextsize) == 0);
3100 bno = del->br_startblock;
3101 len = del->br_blockcount;
3102 do_div(bno, mp->m_sb.sb_rextsize);
3103 do_div(len, mp->m_sb.sb_rextsize);
3104 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3105 (xfs_extlen_t)len)))
3106 goto done;
3107 do_fx = 0;
3108 nblks = len * mp->m_sb.sb_rextsize;
3109 qfield = XFS_TRANS_DQ_RTBCOUNT;
3110 }
3111 /*
3112 * Ordinary allocation.
3113 */
3114 else {
3115 do_fx = 1;
3116 nblks = del->br_blockcount;
3117 qfield = XFS_TRANS_DQ_BCOUNT;
3118 }
3119 /*
3120 * Set up del_endblock and cur for later.
3121 */
3122 del_endblock = del->br_startblock + del->br_blockcount;
3123 if (cur) {
3124 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3125 got.br_startblock, got.br_blockcount,
3126 &i)))
3127 goto done;
3128 ASSERT(i == 1);
3129 }
3130 da_old = da_new = 0;
3131 } else {
3132 da_old = STARTBLOCKVAL(got.br_startblock);
3133 da_new = 0;
3134 nblks = 0;
3135 do_fx = 0;
3136 }
3137 /*
3138 * Set flag value to use in switch statement.
3139 * Left-contig is 2, right-contig is 1.
3140 */
3141 switch (((got.br_startoff == del->br_startoff) << 1) |
3142 (got_endoff == del_endoff)) {
3143 case 3:
3144 /*
3145 * Matches the whole extent. Delete the entry.
3146 */
3147 xfs_bmap_trace_delete(fname, "3", ip, idx, 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003148 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 ifp->if_lastex = idx;
3150 if (delay)
3151 break;
3152 XFS_IFORK_NEXT_SET(ip, whichfork,
3153 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3154 flags |= XFS_ILOG_CORE;
3155 if (!cur) {
3156 flags |= XFS_ILOG_FEXT(whichfork);
3157 break;
3158 }
3159 if ((error = xfs_bmbt_delete(cur, &i)))
3160 goto done;
3161 ASSERT(i == 1);
3162 break;
3163
3164 case 2:
3165 /*
3166 * Deleting the first part of the extent.
3167 */
3168 xfs_bmap_trace_pre_update(fname, "2", ip, idx, whichfork);
3169 xfs_bmbt_set_startoff(ep, del_endoff);
3170 temp = got.br_blockcount - del->br_blockcount;
3171 xfs_bmbt_set_blockcount(ep, temp);
3172 ifp->if_lastex = idx;
3173 if (delay) {
3174 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3175 da_old);
3176 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3177 xfs_bmap_trace_post_update(fname, "2", ip, idx,
3178 whichfork);
3179 da_new = temp;
3180 break;
3181 }
3182 xfs_bmbt_set_startblock(ep, del_endblock);
3183 xfs_bmap_trace_post_update(fname, "2", ip, idx, whichfork);
3184 if (!cur) {
3185 flags |= XFS_ILOG_FEXT(whichfork);
3186 break;
3187 }
3188 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3189 got.br_blockcount - del->br_blockcount,
3190 got.br_state)))
3191 goto done;
3192 break;
3193
3194 case 1:
3195 /*
3196 * Deleting the last part of the extent.
3197 */
3198 temp = got.br_blockcount - del->br_blockcount;
3199 xfs_bmap_trace_pre_update(fname, "1", ip, idx, whichfork);
3200 xfs_bmbt_set_blockcount(ep, temp);
3201 ifp->if_lastex = idx;
3202 if (delay) {
3203 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3204 da_old);
3205 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3206 xfs_bmap_trace_post_update(fname, "1", ip, idx,
3207 whichfork);
3208 da_new = temp;
3209 break;
3210 }
3211 xfs_bmap_trace_post_update(fname, "1", ip, idx, whichfork);
3212 if (!cur) {
3213 flags |= XFS_ILOG_FEXT(whichfork);
3214 break;
3215 }
3216 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3217 got.br_startblock,
3218 got.br_blockcount - del->br_blockcount,
3219 got.br_state)))
3220 goto done;
3221 break;
3222
3223 case 0:
3224 /*
3225 * Deleting the middle of the extent.
3226 */
3227 temp = del->br_startoff - got.br_startoff;
3228 xfs_bmap_trace_pre_update(fname, "0", ip, idx, whichfork);
3229 xfs_bmbt_set_blockcount(ep, temp);
3230 new.br_startoff = del_endoff;
3231 temp2 = got_endoff - del_endoff;
3232 new.br_blockcount = temp2;
3233 new.br_state = got.br_state;
3234 if (!delay) {
3235 new.br_startblock = del_endblock;
3236 flags |= XFS_ILOG_CORE;
3237 if (cur) {
3238 if ((error = xfs_bmbt_update(cur,
3239 got.br_startoff,
3240 got.br_startblock, temp,
3241 got.br_state)))
3242 goto done;
3243 if ((error = xfs_bmbt_increment(cur, 0, &i)))
3244 goto done;
3245 cur->bc_rec.b = new;
3246 error = xfs_bmbt_insert(cur, &i);
3247 if (error && error != ENOSPC)
3248 goto done;
3249 /*
3250 * If get no-space back from btree insert,
3251 * it tried a split, and we have a zero
3252 * block reservation.
3253 * Fix up our state and return the error.
3254 */
3255 if (error == ENOSPC) {
3256 /*
3257 * Reset the cursor, don't trust
3258 * it after any insert operation.
3259 */
3260 if ((error = xfs_bmbt_lookup_eq(cur,
3261 got.br_startoff,
3262 got.br_startblock,
3263 temp, &i)))
3264 goto done;
3265 ASSERT(i == 1);
3266 /*
3267 * Update the btree record back
3268 * to the original value.
3269 */
3270 if ((error = xfs_bmbt_update(cur,
3271 got.br_startoff,
3272 got.br_startblock,
3273 got.br_blockcount,
3274 got.br_state)))
3275 goto done;
3276 /*
3277 * Reset the extent record back
3278 * to the original value.
3279 */
3280 xfs_bmbt_set_blockcount(ep,
3281 got.br_blockcount);
3282 flags = 0;
3283 error = XFS_ERROR(ENOSPC);
3284 goto done;
3285 }
3286 ASSERT(i == 1);
3287 } else
3288 flags |= XFS_ILOG_FEXT(whichfork);
3289 XFS_IFORK_NEXT_SET(ip, whichfork,
3290 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3291 } else {
3292 ASSERT(whichfork == XFS_DATA_FORK);
3293 temp = xfs_bmap_worst_indlen(ip, temp);
3294 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3295 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3296 new.br_startblock = NULLSTARTBLOCK((int)temp2);
3297 da_new = temp + temp2;
3298 while (da_new > da_old) {
3299 if (temp) {
3300 temp--;
3301 da_new--;
3302 xfs_bmbt_set_startblock(ep,
3303 NULLSTARTBLOCK((int)temp));
3304 }
3305 if (da_new == da_old)
3306 break;
3307 if (temp2) {
3308 temp2--;
3309 da_new--;
3310 new.br_startblock =
3311 NULLSTARTBLOCK((int)temp2);
3312 }
3313 }
3314 }
3315 xfs_bmap_trace_post_update(fname, "0", ip, idx, whichfork);
3316 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 1, &new, NULL,
3317 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003318 xfs_iext_insert(ifp, idx + 1, 1, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 ifp->if_lastex = idx + 1;
3320 break;
3321 }
3322 /*
3323 * If we need to, add to list of extents to delete.
3324 */
3325 if (do_fx)
3326 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3327 mp);
3328 /*
3329 * Adjust inode # blocks in the file.
3330 */
3331 if (nblks)
3332 ip->i_d.di_nblocks -= nblks;
3333 /*
3334 * Adjust quota data.
3335 */
3336 if (qfield)
3337 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
3338
3339 /*
3340 * Account for change in delayed indirect blocks.
3341 * Nothing to do for disk quota accounting here.
3342 */
3343 ASSERT(da_old >= da_new);
3344 if (da_old > da_new)
David Chinner20f4ebf2007-02-10 18:36:10 +11003345 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 rsvd);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003347 if (delta) {
3348 /* DELTA: report the original extent. */
3349 if (delta->xed_startoff > got.br_startoff)
3350 delta->xed_startoff = got.br_startoff;
3351 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3352 delta->xed_blockcount = got.br_startoff +
3353 got.br_blockcount;
3354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355done:
3356 *logflagsp = flags;
3357 return error;
3358}
3359
3360/*
3361 * Remove the entry "free" from the free item list. Prev points to the
3362 * previous entry, unless "free" is the head of the list.
3363 */
3364STATIC void
3365xfs_bmap_del_free(
3366 xfs_bmap_free_t *flist, /* free item list header */
3367 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3368 xfs_bmap_free_item_t *free) /* list item to be freed */
3369{
3370 if (prev)
3371 prev->xbfi_next = free->xbfi_next;
3372 else
3373 flist->xbf_first = free->xbfi_next;
3374 flist->xbf_count--;
3375 kmem_zone_free(xfs_bmap_free_item_zone, free);
3376}
3377
3378/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 * Convert an extents-format file into a btree-format file.
3380 * The new file will have a root block (in the inode) and a single child block.
3381 */
3382STATIC int /* error */
3383xfs_bmap_extents_to_btree(
3384 xfs_trans_t *tp, /* transaction pointer */
3385 xfs_inode_t *ip, /* incore inode pointer */
3386 xfs_fsblock_t *firstblock, /* first-block-allocated */
3387 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3388 xfs_btree_cur_t **curp, /* cursor returned to caller */
3389 int wasdel, /* converting a delayed alloc */
3390 int *logflagsp, /* inode logging flags */
3391 int whichfork) /* data or attr fork */
3392{
3393 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
3394 xfs_buf_t *abp; /* buffer for ablock */
3395 xfs_alloc_arg_t args; /* allocation arguments */
3396 xfs_bmbt_rec_t *arp; /* child record pointer */
3397 xfs_bmbt_block_t *block; /* btree root block */
3398 xfs_btree_cur_t *cur; /* bmap btree cursor */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003399 xfs_bmbt_rec_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003401 xfs_extnum_t i, cnt; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 xfs_ifork_t *ifp; /* inode fork pointer */
3403 xfs_bmbt_key_t *kp; /* root block key pointer */
3404 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003405 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3407
3408 ifp = XFS_IFORK_PTR(ip, whichfork);
3409 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3410 ASSERT(ifp->if_ext_max ==
3411 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3412 /*
3413 * Make space in the inode incore.
3414 */
3415 xfs_iroot_realloc(ip, 1, whichfork);
3416 ifp->if_flags |= XFS_IFBROOT;
3417 /*
3418 * Fill in the root.
3419 */
3420 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003421 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3422 block->bb_level = cpu_to_be16(1);
3423 block->bb_numrecs = cpu_to_be16(1);
3424 block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3425 block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 /*
3427 * Need a cursor. Can't allocate until bb_level is filled in.
3428 */
3429 mp = ip->i_mount;
3430 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
3431 whichfork);
3432 cur->bc_private.b.firstblock = *firstblock;
3433 cur->bc_private.b.flist = flist;
3434 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3435 /*
3436 * Convert to a btree with two levels, one record in root.
3437 */
3438 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3439 args.tp = tp;
3440 args.mp = mp;
Yingping Lud210a282006-06-09 14:55:18 +10003441 args.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 if (*firstblock == NULLFSBLOCK) {
3443 args.type = XFS_ALLOCTYPE_START_BNO;
3444 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3445 } else if (flist->xbf_low) {
3446 args.type = XFS_ALLOCTYPE_START_BNO;
3447 args.fsbno = *firstblock;
3448 } else {
3449 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3450 args.fsbno = *firstblock;
3451 }
3452 args.minlen = args.maxlen = args.prod = 1;
3453 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3454 args.minalignslop = 0;
3455 args.wasdel = wasdel;
3456 *logflagsp = 0;
3457 if ((error = xfs_alloc_vextent(&args))) {
3458 xfs_iroot_realloc(ip, -1, whichfork);
3459 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3460 return error;
3461 }
3462 /*
3463 * Allocation can't fail, the space was reserved.
3464 */
3465 ASSERT(args.fsbno != NULLFSBLOCK);
3466 ASSERT(*firstblock == NULLFSBLOCK ||
3467 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3468 (flist->xbf_low &&
3469 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3470 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3471 cur->bc_private.b.allocated++;
3472 ip->i_d.di_nblocks++;
3473 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3474 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3475 /*
3476 * Fill in the child block.
3477 */
3478 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003479 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 ablock->bb_level = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003481 ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3482 ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3484 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003485 for (cnt = i = 0; i < nextents; i++) {
3486 ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
3488 arp->l0 = INT_GET(ep->l0, ARCH_CONVERT);
3489 arp->l1 = INT_GET(ep->l1, ARCH_CONVERT);
3490 arp++; cnt++;
3491 }
3492 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11003493 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3494 ablock->bb_numrecs = cpu_to_be16(cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 /*
3496 * Fill in the root key and pointer.
3497 */
3498 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
3499 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
Christoph Hellwig8801bb92006-09-28 10:58:17 +10003500 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
Christoph Hellwig576039c2006-09-28 10:58:06 +10003502 *pp = cpu_to_be64(args.fsbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 /*
3504 * Do all this logging at the end so that
3505 * the root is at the right level.
3506 */
3507 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003508 xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 ASSERT(*curp == NULL);
3510 *curp = cur;
3511 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
3512 return 0;
3513}
3514
3515/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11003516 * Helper routine to reset inode di_forkoff field when switching
3517 * attribute fork from local to extent format - we reset it where
3518 * possible to make space available for inline data fork extents.
3519 */
3520STATIC void
3521xfs_bmap_forkoff_reset(
3522 xfs_mount_t *mp,
3523 xfs_inode_t *ip,
3524 int whichfork)
3525{
3526 if (whichfork == XFS_ATTR_FORK &&
3527 (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
3528 (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
Barry Naujoke5889e92007-02-10 18:35:58 +11003529 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
Nathan Scottd8cc8902005-11-02 10:34:53 +11003530 ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
3531 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3532 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
3533 (uint)sizeof(xfs_bmbt_rec_t);
3534 ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
3535 (uint)sizeof(xfs_bmbt_rec_t);
3536 }
3537}
3538
3539/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 * Convert a local file to an extents file.
3541 * This code is out of bounds for data forks of regular files,
3542 * since the file data needs to get logged so things will stay consistent.
3543 * (The bmap-level manipulations are ok, though).
3544 */
3545STATIC int /* error */
3546xfs_bmap_local_to_extents(
3547 xfs_trans_t *tp, /* transaction pointer */
3548 xfs_inode_t *ip, /* incore inode pointer */
3549 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3550 xfs_extlen_t total, /* total blocks needed by transaction */
3551 int *logflagsp, /* inode logging flags */
3552 int whichfork) /* data or attr fork */
3553{
3554 int error; /* error return value */
3555 int flags; /* logging flags returned */
3556#ifdef XFS_BMAP_TRACE
3557 static char fname[] = "xfs_bmap_local_to_extents";
3558#endif
3559 xfs_ifork_t *ifp; /* inode fork pointer */
3560
3561 /*
3562 * We don't want to deal with the case of keeping inode data inline yet.
3563 * So sending the data fork of a regular inode is invalid.
3564 */
3565 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3566 whichfork == XFS_DATA_FORK));
3567 ifp = XFS_IFORK_PTR(ip, whichfork);
3568 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3569 flags = 0;
3570 error = 0;
3571 if (ifp->if_bytes) {
3572 xfs_alloc_arg_t args; /* allocation arguments */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003573 xfs_buf_t *bp; /* buffer for extent block */
3574 xfs_bmbt_rec_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
3576 args.tp = tp;
3577 args.mp = ip->i_mount;
Yingping Lud210a282006-06-09 14:55:18 +10003578 args.firstblock = *firstblock;
Mandy Kirkconnellf020b672006-03-14 14:07:24 +11003579 ASSERT((ifp->if_flags &
3580 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 /*
3582 * Allocate a block. We know we need only one, since the
3583 * file currently fits in an inode.
3584 */
3585 if (*firstblock == NULLFSBLOCK) {
3586 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3587 args.type = XFS_ALLOCTYPE_START_BNO;
3588 } else {
3589 args.fsbno = *firstblock;
3590 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3591 }
3592 args.total = total;
3593 args.mod = args.minleft = args.alignment = args.wasdel =
3594 args.isfl = args.minalignslop = 0;
3595 args.minlen = args.maxlen = args.prod = 1;
3596 if ((error = xfs_alloc_vextent(&args)))
3597 goto done;
3598 /*
3599 * Can't fail, the space was reserved.
3600 */
3601 ASSERT(args.fsbno != NULLFSBLOCK);
3602 ASSERT(args.len == 1);
3603 *firstblock = args.fsbno;
3604 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3605 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3606 ifp->if_bytes);
3607 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003608 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003610 xfs_iext_add(ifp, 0, 1);
3611 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3613 xfs_bmap_trace_post_update(fname, "new", ip, 0, whichfork);
3614 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3615 ip->i_d.di_nblocks = 1;
3616 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
3617 XFS_TRANS_DQ_BCOUNT, 1L);
3618 flags |= XFS_ILOG_FEXT(whichfork);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003619 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003621 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 ifp->if_flags &= ~XFS_IFINLINE;
3624 ifp->if_flags |= XFS_IFEXTENTS;
3625 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3626 flags |= XFS_ILOG_CORE;
3627done:
3628 *logflagsp = flags;
3629 return error;
3630}
3631
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003633 * Search the extent records for the entry containing block bno.
3634 * If bno lies in a hole, point to the next entry. If bno lies
3635 * past eof, *eofp will be set, and *prevp will contain the last
3636 * entry (null if none). Else, *lastxp will be set to the index
3637 * of the found entry; *gotp will contain the entry.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003638 */
3639xfs_bmbt_rec_t * /* pointer to found extent entry */
3640xfs_bmap_search_multi_extents(
3641 xfs_ifork_t *ifp, /* inode fork pointer */
3642 xfs_fileoff_t bno, /* block number searched for */
3643 int *eofp, /* out: end of file found */
3644 xfs_extnum_t *lastxp, /* out: last extent index */
3645 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3646 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3647{
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003648 xfs_bmbt_rec_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003649 xfs_extnum_t lastx; /* last extent index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003650
3651 /*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003652 * Initialize the extent entry structure to catch access to
3653 * uninitialized br_startblock field.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003654 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003655 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3656 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3657 gotp->br_state = XFS_EXT_INVALID;
3658#if XFS_BIG_BLKNOS
3659 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3660#else
3661 gotp->br_startblock = 0xffffa5a5;
3662#endif
3663 prevp->br_startoff = NULLFILEOFF;
3664
3665 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3666 if (lastx > 0) {
3667 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3668 }
3669 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3670 xfs_bmbt_get_all(ep, gotp);
3671 *eofp = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003672 } else {
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003673 if (lastx > 0) {
3674 *gotp = *prevp;
3675 }
3676 *eofp = 1;
3677 ep = NULL;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003678 }
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003679 *lastxp = lastx;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003680 return ep;
3681}
3682
3683/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 * Search the extents list for the inode, for the extent containing bno.
3685 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3686 * *eofp will be set, and *prevp will contain the last entry (null if none).
3687 * Else, *lastxp will be set to the index of the found
3688 * entry; *gotp will contain the entry.
3689 */
3690STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
3691xfs_bmap_search_extents(
3692 xfs_inode_t *ip, /* incore inode pointer */
3693 xfs_fileoff_t bno, /* block number searched for */
Nathan Scott572d95f2006-09-28 11:03:20 +10003694 int fork, /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 int *eofp, /* out: end of file found */
3696 xfs_extnum_t *lastxp, /* out: last extent index */
3697 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3698 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3699{
3700 xfs_ifork_t *ifp; /* inode fork pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003701 xfs_bmbt_rec_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
3703 XFS_STATS_INC(xs_look_exlist);
Nathan Scott572d95f2006-09-28 11:03:20 +10003704 ifp = XFS_IFORK_PTR(ip, fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003706 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3707
Nathan Scott572d95f2006-09-28 11:03:20 +10003708 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3709 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3710 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3711 "Access to block zero in inode %llu "
3712 "start_block: %llx start_off: %llx "
3713 "blkcnt: %llx extent-state: %x lastx: %x\n",
3714 (unsigned long long)ip->i_ino,
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003715 (unsigned long long)gotp->br_startblock,
3716 (unsigned long long)gotp->br_startoff,
3717 (unsigned long long)gotp->br_blockcount,
Nathan Scott572d95f2006-09-28 11:03:20 +10003718 gotp->br_state, *lastxp);
3719 *lastxp = NULLEXTNUM;
3720 *eofp = 1;
3721 return NULL;
3722 }
3723 return ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724}
3725
3726
3727#ifdef XFS_BMAP_TRACE
3728ktrace_t *xfs_bmap_trace_buf;
3729
3730/*
3731 * Add a bmap trace buffer entry. Base routine for the others.
3732 */
3733STATIC void
3734xfs_bmap_trace_addentry(
3735 int opcode, /* operation */
3736 char *fname, /* function name */
3737 char *desc, /* operation description */
3738 xfs_inode_t *ip, /* incore inode pointer */
3739 xfs_extnum_t idx, /* index of entry(ies) */
3740 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
3741 xfs_bmbt_rec_t *r1, /* first record */
3742 xfs_bmbt_rec_t *r2, /* second record or null */
3743 int whichfork) /* data or attr fork */
3744{
3745 xfs_bmbt_rec_t tr2;
3746
3747 ASSERT(cnt == 1 || cnt == 2);
3748 ASSERT(r1 != NULL);
3749 if (cnt == 1) {
3750 ASSERT(r2 == NULL);
3751 r2 = &tr2;
3752 memset(&tr2, 0, sizeof(tr2));
3753 } else
3754 ASSERT(r2 != NULL);
3755 ktrace_enter(xfs_bmap_trace_buf,
3756 (void *)(__psint_t)(opcode | (whichfork << 16)),
3757 (void *)fname, (void *)desc, (void *)ip,
3758 (void *)(__psint_t)idx,
3759 (void *)(__psint_t)cnt,
3760 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3761 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3762 (void *)(__psunsigned_t)(r1->l0 >> 32),
3763 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3764 (void *)(__psunsigned_t)(r1->l1 >> 32),
3765 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3766 (void *)(__psunsigned_t)(r2->l0 >> 32),
3767 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3768 (void *)(__psunsigned_t)(r2->l1 >> 32),
3769 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3770 );
3771 ASSERT(ip->i_xtrace);
3772 ktrace_enter(ip->i_xtrace,
3773 (void *)(__psint_t)(opcode | (whichfork << 16)),
3774 (void *)fname, (void *)desc, (void *)ip,
3775 (void *)(__psint_t)idx,
3776 (void *)(__psint_t)cnt,
3777 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3778 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3779 (void *)(__psunsigned_t)(r1->l0 >> 32),
3780 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3781 (void *)(__psunsigned_t)(r1->l1 >> 32),
3782 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3783 (void *)(__psunsigned_t)(r2->l0 >> 32),
3784 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3785 (void *)(__psunsigned_t)(r2->l1 >> 32),
3786 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3787 );
3788}
3789
3790/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003791 * Add bmap trace entry prior to a call to xfs_iext_remove.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 */
3793STATIC void
3794xfs_bmap_trace_delete(
3795 char *fname, /* function name */
3796 char *desc, /* operation description */
3797 xfs_inode_t *ip, /* incore inode pointer */
3798 xfs_extnum_t idx, /* index of entry(entries) deleted */
3799 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3800 int whichfork) /* data or attr fork */
3801{
3802 xfs_ifork_t *ifp; /* inode fork pointer */
3803
3804 ifp = XFS_IFORK_PTR(ip, whichfork);
3805 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003806 cnt, xfs_iext_get_ext(ifp, idx),
3807 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 whichfork);
3809}
3810
3811/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003812 * Add bmap trace entry prior to a call to xfs_iext_insert, or
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 * reading in the extents list from the disk (in the btree).
3814 */
3815STATIC void
3816xfs_bmap_trace_insert(
3817 char *fname, /* function name */
3818 char *desc, /* operation description */
3819 xfs_inode_t *ip, /* incore inode pointer */
3820 xfs_extnum_t idx, /* index of entry(entries) inserted */
3821 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3822 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3823 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3824 int whichfork) /* data or attr fork */
3825{
3826 xfs_bmbt_rec_t tr1; /* compressed record 1 */
3827 xfs_bmbt_rec_t tr2; /* compressed record 2 if needed */
3828
3829 xfs_bmbt_set_all(&tr1, r1);
3830 if (cnt == 2) {
3831 ASSERT(r2 != NULL);
3832 xfs_bmbt_set_all(&tr2, r2);
3833 } else {
3834 ASSERT(cnt == 1);
3835 ASSERT(r2 == NULL);
3836 }
3837 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3838 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3839}
3840
3841/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003842 * Add bmap trace entry after updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 */
3844STATIC void
3845xfs_bmap_trace_post_update(
3846 char *fname, /* function name */
3847 char *desc, /* operation description */
3848 xfs_inode_t *ip, /* incore inode pointer */
3849 xfs_extnum_t idx, /* index of entry updated */
3850 int whichfork) /* data or attr fork */
3851{
3852 xfs_ifork_t *ifp; /* inode fork pointer */
3853
3854 ifp = XFS_IFORK_PTR(ip, whichfork);
3855 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003856 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857}
3858
3859/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003860 * Add bmap trace entry prior to updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 */
3862STATIC void
3863xfs_bmap_trace_pre_update(
3864 char *fname, /* function name */
3865 char *desc, /* operation description */
3866 xfs_inode_t *ip, /* incore inode pointer */
3867 xfs_extnum_t idx, /* index of entry to be updated */
3868 int whichfork) /* data or attr fork */
3869{
3870 xfs_ifork_t *ifp; /* inode fork pointer */
3871
3872 ifp = XFS_IFORK_PTR(ip, whichfork);
3873 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003874 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875}
3876#endif /* XFS_BMAP_TRACE */
3877
3878/*
3879 * Compute the worst-case number of indirect blocks that will be used
3880 * for ip's delayed extent of length "len".
3881 */
3882STATIC xfs_filblks_t
3883xfs_bmap_worst_indlen(
3884 xfs_inode_t *ip, /* incore inode pointer */
3885 xfs_filblks_t len) /* delayed extent length */
3886{
3887 int level; /* btree level number */
3888 int maxrecs; /* maximum record count at this level */
3889 xfs_mount_t *mp; /* mount structure */
3890 xfs_filblks_t rval; /* return value */
3891
3892 mp = ip->i_mount;
3893 maxrecs = mp->m_bmap_dmxr[0];
3894 for (level = 0, rval = 0;
3895 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3896 level++) {
3897 len += maxrecs - 1;
3898 do_div(len, maxrecs);
3899 rval += len;
3900 if (len == 1)
3901 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3902 level - 1;
3903 if (level == 0)
3904 maxrecs = mp->m_bmap_dmxr[1];
3905 }
3906 return rval;
3907}
3908
3909#if defined(XFS_RW_TRACE)
3910STATIC void
3911xfs_bunmap_trace(
3912 xfs_inode_t *ip,
3913 xfs_fileoff_t bno,
3914 xfs_filblks_t len,
3915 int flags,
3916 inst_t *ra)
3917{
3918 if (ip->i_rwtrace == NULL)
3919 return;
3920 ktrace_enter(ip->i_rwtrace,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003921 (void *)(__psint_t)XFS_BUNMAP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 (void *)ip,
3923 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3924 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3925 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3926 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3927 (void *)(__psint_t)len,
3928 (void *)(__psint_t)flags,
3929 (void *)(unsigned long)current_cpu(),
3930 (void *)ra,
3931 (void *)0,
3932 (void *)0,
3933 (void *)0,
3934 (void *)0,
3935 (void *)0,
3936 (void *)0);
3937}
3938#endif
3939
3940/*
3941 * Convert inode from non-attributed to attributed.
3942 * Must not be in a transaction, ip must not be locked.
3943 */
3944int /* error code */
3945xfs_bmap_add_attrfork(
3946 xfs_inode_t *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003947 int size, /* space new attribute needs */
3948 int rsvd) /* xact may use reserved blks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003951 xfs_bmap_free_t flist; /* freed extent records */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 xfs_trans_t *tp; /* transaction pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003954 unsigned long s; /* spinlock spl value */
3955 int blks; /* space reservation */
3956 int version = 1; /* superblock attr version */
3957 int committed; /* xaction was committed */
3958 int logflags; /* logging flags */
3959 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
Nathan Scottd8cc8902005-11-02 10:34:53 +11003961 ASSERT(XFS_IFORK_Q(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 ASSERT(ip->i_df.if_ext_max ==
3963 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
Nathan Scottd8cc8902005-11-02 10:34:53 +11003964
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 mp = ip->i_mount;
3966 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3967 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3968 blks = XFS_ADDAFORK_SPACE_RES(mp);
3969 if (rsvd)
3970 tp->t_flags |= XFS_TRANS_RESERVE;
3971 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3972 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3973 goto error0;
3974 xfs_ilock(ip, XFS_ILOCK_EXCL);
3975 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
3976 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3977 XFS_QMOPT_RES_REGBLKS);
3978 if (error) {
3979 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3980 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3981 return error;
3982 }
3983 if (XFS_IFORK_Q(ip))
3984 goto error1;
3985 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3986 /*
3987 * For inodes coming from pre-6.2 filesystems.
3988 */
3989 ASSERT(ip->i_d.di_aformat == 0);
3990 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3991 }
3992 ASSERT(ip->i_d.di_anextents == 0);
3993 VN_HOLD(XFS_ITOV(ip));
3994 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3995 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3996 switch (ip->i_d.di_format) {
3997 case XFS_DINODE_FMT_DEV:
3998 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3999 break;
4000 case XFS_DINODE_FMT_UUID:
4001 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
4002 break;
4003 case XFS_DINODE_FMT_LOCAL:
4004 case XFS_DINODE_FMT_EXTENTS:
4005 case XFS_DINODE_FMT_BTREE:
Nathan Scottd8cc8902005-11-02 10:34:53 +11004006 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
4007 if (!ip->i_d.di_forkoff)
4008 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
Nathan Scott13059ff2006-01-11 15:32:01 +11004009 else if (mp->m_flags & XFS_MOUNT_ATTR2)
Nathan Scottd8cc8902005-11-02 10:34:53 +11004010 version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 break;
4012 default:
4013 ASSERT(0);
4014 error = XFS_ERROR(EINVAL);
4015 goto error1;
4016 }
4017 ip->i_df.if_ext_max =
4018 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4019 ASSERT(ip->i_afp == NULL);
4020 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
4021 ip->i_afp->if_ext_max =
4022 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4023 ip->i_afp->if_flags = XFS_IFEXTENTS;
4024 logflags = 0;
4025 XFS_BMAP_INIT(&flist, &firstblock);
4026 switch (ip->i_d.di_format) {
4027 case XFS_DINODE_FMT_LOCAL:
4028 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
4029 &logflags);
4030 break;
4031 case XFS_DINODE_FMT_EXTENTS:
4032 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
4033 &flist, &logflags);
4034 break;
4035 case XFS_DINODE_FMT_BTREE:
4036 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
4037 &logflags);
4038 break;
4039 default:
4040 error = 0;
4041 break;
4042 }
4043 if (logflags)
4044 xfs_trans_log_inode(tp, ip, logflags);
4045 if (error)
4046 goto error2;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004047 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb) ||
4048 (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +11004049 __int64_t sbfields = 0;
4050
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 s = XFS_SB_LOCK(mp);
4052 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) {
4053 XFS_SB_VERSION_ADDATTR(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11004054 sbfields |= XFS_SB_VERSIONNUM;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004055 }
4056 if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2) {
4057 XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11004058 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
Nathan Scottd8cc8902005-11-02 10:34:53 +11004059 }
Nathan Scottda087ba2005-11-02 15:00:20 +11004060 if (sbfields) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 XFS_SB_UNLOCK(mp, s);
Nathan Scottda087ba2005-11-02 15:00:20 +11004062 xfs_mod_sb(tp, sbfields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 } else
4064 XFS_SB_UNLOCK(mp, s);
4065 }
Eric Sandeenf7c99b62007-02-10 18:37:16 +11004066 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 goto error2;
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004068 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 ASSERT(ip->i_df.if_ext_max ==
4070 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4071 return error;
4072error2:
4073 xfs_bmap_cancel(&flist);
4074error1:
4075 ASSERT(ismrlocked(&ip->i_lock,MR_UPDATE));
4076 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4077error0:
4078 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4079 ASSERT(ip->i_df.if_ext_max ==
4080 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4081 return error;
4082}
4083
4084/*
4085 * Add the extent to the list of extents to be free at transaction end.
4086 * The list is maintained sorted (by block number).
4087 */
4088/* ARGSUSED */
4089void
4090xfs_bmap_add_free(
4091 xfs_fsblock_t bno, /* fs block number of extent */
4092 xfs_filblks_t len, /* length of extent */
4093 xfs_bmap_free_t *flist, /* list of extents */
4094 xfs_mount_t *mp) /* mount point structure */
4095{
4096 xfs_bmap_free_item_t *cur; /* current (next) element */
4097 xfs_bmap_free_item_t *new; /* new element */
4098 xfs_bmap_free_item_t *prev; /* previous element */
4099#ifdef DEBUG
4100 xfs_agnumber_t agno;
4101 xfs_agblock_t agbno;
4102
4103 ASSERT(bno != NULLFSBLOCK);
4104 ASSERT(len > 0);
4105 ASSERT(len <= MAXEXTLEN);
4106 ASSERT(!ISNULLSTARTBLOCK(bno));
4107 agno = XFS_FSB_TO_AGNO(mp, bno);
4108 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4109 ASSERT(agno < mp->m_sb.sb_agcount);
4110 ASSERT(agbno < mp->m_sb.sb_agblocks);
4111 ASSERT(len < mp->m_sb.sb_agblocks);
4112 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4113#endif
4114 ASSERT(xfs_bmap_free_item_zone != NULL);
4115 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4116 new->xbfi_startblock = bno;
4117 new->xbfi_blockcount = (xfs_extlen_t)len;
4118 for (prev = NULL, cur = flist->xbf_first;
4119 cur != NULL;
4120 prev = cur, cur = cur->xbfi_next) {
4121 if (cur->xbfi_startblock >= bno)
4122 break;
4123 }
4124 if (prev)
4125 prev->xbfi_next = new;
4126 else
4127 flist->xbf_first = new;
4128 new->xbfi_next = cur;
4129 flist->xbf_count++;
4130}
4131
4132/*
4133 * Compute and fill in the value of the maximum depth of a bmap btree
4134 * in this filesystem. Done once, during mount.
4135 */
4136void
4137xfs_bmap_compute_maxlevels(
4138 xfs_mount_t *mp, /* file system mount structure */
4139 int whichfork) /* data or attr fork */
4140{
4141 int level; /* btree level */
4142 uint maxblocks; /* max blocks at this level */
4143 uint maxleafents; /* max leaf entries possible */
4144 int maxrootrecs; /* max records in root block */
4145 int minleafrecs; /* min records in leaf block */
4146 int minnoderecs; /* min records in node block */
4147 int sz; /* root block size */
4148
4149 /*
4150 * The maximum number of extents in a file, hence the maximum
4151 * number of leaf entries, is controlled by the type of di_nextents
4152 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4153 * (a signed 16-bit number, xfs_aextnum_t).
4154 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11004155 if (whichfork == XFS_DATA_FORK) {
4156 maxleafents = MAXEXTNUM;
Nathan Scott13059ff2006-01-11 15:32:01 +11004157 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4158 XFS_BMDR_SPACE_CALC(MINDBTPTRS) : mp->m_attroffset;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004159 } else {
4160 maxleafents = MAXAEXTNUM;
Nathan Scott13059ff2006-01-11 15:32:01 +11004161 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4162 XFS_BMDR_SPACE_CALC(MINABTPTRS) :
4163 mp->m_sb.sb_inodesize - mp->m_attroffset;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004164 }
4165 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 minleafrecs = mp->m_bmap_dmnr[0];
4167 minnoderecs = mp->m_bmap_dmnr[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4169 for (level = 1; maxblocks > 1; level++) {
4170 if (maxblocks <= maxrootrecs)
4171 maxblocks = 1;
4172 else
4173 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4174 }
4175 mp->m_bm_maxlevels[whichfork] = level;
4176}
4177
4178/*
4179 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4180 * caller. Frees all the extents that need freeing, which must be done
4181 * last due to locking considerations. We never free any extents in
4182 * the first transaction. This is to allow the caller to make the first
4183 * transaction a synchronous one so that the pointers to the data being
4184 * broken in this transaction will be permanent before the data is actually
4185 * freed. This is necessary to prevent blocks from being reallocated
4186 * and written to before the free and reallocation are actually permanent.
4187 * We do not just make the first transaction synchronous here, because
4188 * there are more efficient ways to gain the same protection in some cases
4189 * (see the file truncation code).
4190 *
4191 * Return 1 if the given transaction was committed and a new one
4192 * started, and 0 otherwise in the committed parameter.
4193 */
4194/*ARGSUSED*/
4195int /* error */
4196xfs_bmap_finish(
4197 xfs_trans_t **tp, /* transaction pointer addr */
4198 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 int *committed) /* xact committed or not */
4200{
4201 xfs_efd_log_item_t *efd; /* extent free data */
4202 xfs_efi_log_item_t *efi; /* extent free intention */
4203 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004204 xfs_bmap_free_item_t *free; /* free extent item */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 unsigned int logres; /* new log reservation */
4206 unsigned int logcount; /* new log count */
4207 xfs_mount_t *mp; /* filesystem mount structure */
4208 xfs_bmap_free_item_t *next; /* next item on free list */
4209 xfs_trans_t *ntp; /* new transaction pointer */
4210
4211 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4212 if (flist->xbf_count == 0) {
4213 *committed = 0;
4214 return 0;
4215 }
4216 ntp = *tp;
4217 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4218 for (free = flist->xbf_first; free; free = free->xbfi_next)
4219 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4220 free->xbfi_blockcount);
4221 logres = ntp->t_log_res;
4222 logcount = ntp->t_log_count;
4223 ntp = xfs_trans_dup(*tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004224 error = xfs_trans_commit(*tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 *tp = ntp;
4226 *committed = 1;
4227 /*
4228 * We have a new transaction, so we should return committed=1,
4229 * even though we're returning an error.
4230 */
4231 if (error) {
4232 return error;
4233 }
4234 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4235 logcount)))
4236 return error;
4237 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4238 for (free = flist->xbf_first; free != NULL; free = next) {
4239 next = free->xbfi_next;
4240 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4241 free->xbfi_blockcount))) {
4242 /*
4243 * The bmap free list will be cleaned up at a
4244 * higher level. The EFI will be canceled when
4245 * this transaction is aborted.
4246 * Need to force shutdown here to make sure it
4247 * happens, since this transaction may not be
4248 * dirty yet.
4249 */
4250 mp = ntp->t_mountp;
4251 if (!XFS_FORCED_SHUTDOWN(mp))
4252 xfs_force_shutdown(mp,
4253 (error == EFSCORRUPTED) ?
Nathan Scott7d04a332006-06-09 14:58:38 +10004254 SHUTDOWN_CORRUPT_INCORE :
4255 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 return error;
4257 }
4258 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4259 free->xbfi_blockcount);
4260 xfs_bmap_del_free(flist, NULL, free);
4261 }
4262 return 0;
4263}
4264
4265/*
4266 * Free up any items left in the list.
4267 */
4268void
4269xfs_bmap_cancel(
4270 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4271{
4272 xfs_bmap_free_item_t *free; /* free list item */
4273 xfs_bmap_free_item_t *next;
4274
4275 if (flist->xbf_count == 0)
4276 return;
4277 ASSERT(flist->xbf_first != NULL);
4278 for (free = flist->xbf_first; free; free = next) {
4279 next = free->xbfi_next;
4280 xfs_bmap_del_free(flist, NULL, free);
4281 }
4282 ASSERT(flist->xbf_count == 0);
4283}
4284
4285/*
4286 * Returns the file-relative block number of the first unused block(s)
4287 * in the file with at least "len" logically contiguous blocks free.
4288 * This is the lowest-address hole if the file has holes, else the first block
4289 * past the end of file.
4290 * Return 0 if the file is currently local (in-inode).
4291 */
4292int /* error */
4293xfs_bmap_first_unused(
4294 xfs_trans_t *tp, /* transaction pointer */
4295 xfs_inode_t *ip, /* incore inode */
4296 xfs_extlen_t len, /* size of hole to find */
4297 xfs_fileoff_t *first_unused, /* unused block */
4298 int whichfork) /* data or attr fork */
4299{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
4301 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004302 int idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 xfs_ifork_t *ifp; /* inode fork pointer */
4304 xfs_fileoff_t lastaddr; /* last block number seen */
4305 xfs_fileoff_t lowest; /* lowest useful block */
4306 xfs_fileoff_t max; /* starting useful block */
4307 xfs_fileoff_t off; /* offset for this block */
4308 xfs_extnum_t nextents; /* number of extent entries */
4309
4310 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4311 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4312 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4313 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4314 *first_unused = 0;
4315 return 0;
4316 }
4317 ifp = XFS_IFORK_PTR(ip, whichfork);
4318 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4319 (error = xfs_iread_extents(tp, ip, whichfork)))
4320 return error;
4321 lowest = *first_unused;
4322 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004323 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
4324 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 off = xfs_bmbt_get_startoff(ep);
4326 /*
4327 * See if the hole before this extent will work.
4328 */
4329 if (off >= lowest + len && off - max >= len) {
4330 *first_unused = max;
4331 return 0;
4332 }
4333 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4334 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4335 }
4336 *first_unused = max;
4337 return 0;
4338}
4339
4340/*
4341 * Returns the file-relative block number of the last block + 1 before
4342 * last_block (input value) in the file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004343 * This is not based on i_size, it is based on the extent records.
4344 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 */
4346int /* error */
4347xfs_bmap_last_before(
4348 xfs_trans_t *tp, /* transaction pointer */
4349 xfs_inode_t *ip, /* incore inode */
4350 xfs_fileoff_t *last_block, /* last block */
4351 int whichfork) /* data or attr fork */
4352{
4353 xfs_fileoff_t bno; /* input file offset */
4354 int eof; /* hit end of file */
4355 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4356 int error; /* error return value */
4357 xfs_bmbt_irec_t got; /* current extent value */
4358 xfs_ifork_t *ifp; /* inode fork pointer */
4359 xfs_extnum_t lastx; /* last extent used */
4360 xfs_bmbt_irec_t prev; /* previous extent value */
4361
4362 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4363 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4364 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4365 return XFS_ERROR(EIO);
4366 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4367 *last_block = 0;
4368 return 0;
4369 }
4370 ifp = XFS_IFORK_PTR(ip, whichfork);
4371 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4372 (error = xfs_iread_extents(tp, ip, whichfork)))
4373 return error;
4374 bno = *last_block - 1;
4375 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4376 &prev);
4377 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4378 if (prev.br_startoff == NULLFILEOFF)
4379 *last_block = 0;
4380 else
4381 *last_block = prev.br_startoff + prev.br_blockcount;
4382 }
4383 /*
4384 * Otherwise *last_block is already the right answer.
4385 */
4386 return 0;
4387}
4388
4389/*
4390 * Returns the file-relative block number of the first block past eof in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004391 * the file. This is not based on i_size, it is based on the extent records.
4392 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 */
4394int /* error */
4395xfs_bmap_last_offset(
4396 xfs_trans_t *tp, /* transaction pointer */
4397 xfs_inode_t *ip, /* incore inode */
4398 xfs_fileoff_t *last_block, /* last block */
4399 int whichfork) /* data or attr fork */
4400{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4402 int error; /* error return value */
4403 xfs_ifork_t *ifp; /* inode fork pointer */
4404 xfs_extnum_t nextents; /* number of extent entries */
4405
4406 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4407 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4408 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4409 return XFS_ERROR(EIO);
4410 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4411 *last_block = 0;
4412 return 0;
4413 }
4414 ifp = XFS_IFORK_PTR(ip, whichfork);
4415 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4416 (error = xfs_iread_extents(tp, ip, whichfork)))
4417 return error;
4418 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4419 if (!nextents) {
4420 *last_block = 0;
4421 return 0;
4422 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004423 ep = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4425 return 0;
4426}
4427
4428/*
4429 * Returns whether the selected fork of the inode has exactly one
4430 * block or not. For the data fork we check this matches di_size,
4431 * implying the file's range is 0..bsize-1.
4432 */
4433int /* 1=>1 block, 0=>otherwise */
4434xfs_bmap_one_block(
4435 xfs_inode_t *ip, /* incore inode */
4436 int whichfork) /* data or attr fork */
4437{
4438 xfs_bmbt_rec_t *ep; /* ptr to fork's extent */
4439 xfs_ifork_t *ifp; /* inode fork pointer */
4440 int rval; /* return value */
4441 xfs_bmbt_irec_t s; /* internal version of extent */
4442
4443#ifndef DEBUG
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004444 if (whichfork == XFS_DATA_FORK) {
4445 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4446 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4447 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449#endif /* !DEBUG */
4450 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4451 return 0;
4452 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4453 return 0;
4454 ifp = XFS_IFORK_PTR(ip, whichfork);
4455 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004456 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 xfs_bmbt_get_all(ep, &s);
4458 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4459 if (rval && whichfork == XFS_DATA_FORK)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004460 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 return rval;
4462}
4463
4464/*
4465 * Read in the extents to if_extents.
4466 * All inode fields are set up by caller, we just traverse the btree
4467 * and copy the records in. If the file system cannot contain unwritten
4468 * extents, the records are checked for no "state" flags.
4469 */
4470int /* error */
4471xfs_bmap_read_extents(
4472 xfs_trans_t *tp, /* transaction pointer */
4473 xfs_inode_t *ip, /* incore inode */
4474 int whichfork) /* data or attr fork */
4475{
4476 xfs_bmbt_block_t *block; /* current btree block */
4477 xfs_fsblock_t bno; /* block # of "block" */
4478 xfs_buf_t *bp; /* buffer for "block" */
4479 int error; /* error return value */
4480 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4481#ifdef XFS_BMAP_TRACE
4482 static char fname[] = "xfs_bmap_read_extents";
4483#endif
4484 xfs_extnum_t i, j; /* index into the extents list */
4485 xfs_ifork_t *ifp; /* fork structure */
4486 int level; /* btree level, for checking */
4487 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10004488 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 /* REFERENCED */
4490 xfs_extnum_t room; /* number of entries there's room for */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
4492 bno = NULLFSBLOCK;
4493 mp = ip->i_mount;
4494 ifp = XFS_IFORK_PTR(ip, whichfork);
4495 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4496 XFS_EXTFMT_INODE(ip);
4497 block = ifp->if_broot;
4498 /*
4499 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4500 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004501 level = be16_to_cpu(block->bb_level);
4502 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004504 bno = be64_to_cpu(*pp);
4505 ASSERT(bno != NULLDFSBNO);
4506 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4507 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 /*
4509 * Go down the tree until leaf level is reached, following the first
4510 * pointer (leftmost) at each level.
4511 */
4512 while (level-- > 0) {
4513 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4514 XFS_BMAP_BTREE_REF)))
4515 return error;
4516 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4517 XFS_WANT_CORRUPTED_GOTO(
4518 XFS_BMAP_SANITY_CHECK(mp, block, level),
4519 error0);
4520 if (level == 0)
4521 break;
Eric Sandeen2c36dde2007-02-10 18:37:33 +11004522 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004523 bno = be64_to_cpu(*pp);
4524 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 xfs_trans_brelse(tp, bp);
4526 }
4527 /*
4528 * Here with bp and block set to the leftmost leaf node in the tree.
4529 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004530 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 i = 0;
4532 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004533 * Loop over all leaf nodes. Copy information to the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 */
4535 for (;;) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004536 xfs_bmbt_rec_t *frp, *trp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 xfs_fsblock_t nextbno;
4538 xfs_extnum_t num_recs;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004539 xfs_extnum_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
4541
Christoph Hellwig16259e72005-11-02 15:11:25 +11004542 num_recs = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 if (unlikely(i + num_recs > room)) {
4544 ASSERT(i + num_recs <= room);
Nathan Scott3762ec62006-01-12 10:29:53 +11004545 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4546 "corrupt dinode %Lu, (btree extents).",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 (unsigned long long) ip->i_ino);
4548 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4549 XFS_ERRLEVEL_LOW,
4550 ip->i_mount);
4551 goto error0;
4552 }
4553 XFS_WANT_CORRUPTED_GOTO(
4554 XFS_BMAP_SANITY_CHECK(mp, block, 0),
4555 error0);
4556 /*
4557 * Read-ahead the next leaf block, if any.
4558 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004559 nextbno = be64_to_cpu(block->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 if (nextbno != NULLFSBLOCK)
4561 xfs_btree_reada_bufl(mp, nextbno, 1);
4562 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004563 * Copy records into the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 */
Eric Sandeen2c36dde2007-02-10 18:37:33 +11004565 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004566 start = i;
4567 for (j = 0; j < num_recs; j++, i++, frp++) {
4568 trp = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 trp->l0 = INT_GET(frp->l0, ARCH_CONVERT);
4570 trp->l1 = INT_GET(frp->l1, ARCH_CONVERT);
4571 }
4572 if (exntf == XFS_EXTFMT_NOSTATE) {
4573 /*
4574 * Check all attribute bmap btree records and
4575 * any "older" data bmap btree records for a
4576 * set bit in the "extent flag" position.
4577 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004578 if (unlikely(xfs_check_nostate_extents(ifp,
4579 start, num_recs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4581 XFS_ERRLEVEL_LOW,
4582 ip->i_mount);
4583 goto error0;
4584 }
4585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 xfs_trans_brelse(tp, bp);
4587 bno = nextbno;
4588 /*
4589 * If we've reached the end, stop.
4590 */
4591 if (bno == NULLFSBLOCK)
4592 break;
4593 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4594 XFS_BMAP_BTREE_REF)))
4595 return error;
4596 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4597 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004598 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4600 xfs_bmap_trace_exlist(fname, ip, i, whichfork);
4601 return 0;
4602error0:
4603 xfs_trans_brelse(tp, bp);
4604 return XFS_ERROR(EFSCORRUPTED);
4605}
4606
4607#ifdef XFS_BMAP_TRACE
4608/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004609 * Add bmap trace insert entries for all the contents of the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 */
4611void
4612xfs_bmap_trace_exlist(
4613 char *fname, /* function name */
4614 xfs_inode_t *ip, /* incore inode pointer */
4615 xfs_extnum_t cnt, /* count of entries in the list */
4616 int whichfork) /* data or attr fork */
4617{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004618 xfs_bmbt_rec_t *ep; /* current extent record */
4619 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 xfs_ifork_t *ifp; /* inode fork pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004621 xfs_bmbt_irec_t s; /* file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622
4623 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004624 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4625 for (idx = 0; idx < cnt; idx++) {
4626 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 xfs_bmbt_get_all(ep, &s);
4628 xfs_bmap_trace_insert(fname, "exlist", ip, idx, 1, &s, NULL,
4629 whichfork);
4630 }
4631}
4632#endif
4633
4634#ifdef DEBUG
4635/*
4636 * Validate that the bmbt_irecs being returned from bmapi are valid
4637 * given the callers original parameters. Specifically check the
4638 * ranges of the returned irecs to ensure that they only extent beyond
4639 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4640 */
4641STATIC void
4642xfs_bmap_validate_ret(
4643 xfs_fileoff_t bno,
4644 xfs_filblks_t len,
4645 int flags,
4646 xfs_bmbt_irec_t *mval,
4647 int nmap,
4648 int ret_nmap)
4649{
4650 int i; /* index to map values */
4651
4652 ASSERT(ret_nmap <= nmap);
4653
4654 for (i = 0; i < ret_nmap; i++) {
4655 ASSERT(mval[i].br_blockcount > 0);
4656 if (!(flags & XFS_BMAPI_ENTIRE)) {
4657 ASSERT(mval[i].br_startoff >= bno);
4658 ASSERT(mval[i].br_blockcount <= len);
4659 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4660 bno + len);
4661 } else {
4662 ASSERT(mval[i].br_startoff < bno + len);
4663 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4664 bno);
4665 }
4666 ASSERT(i == 0 ||
4667 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4668 mval[i].br_startoff);
4669 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4670 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4671 mval[i].br_startblock != HOLESTARTBLOCK);
4672 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4673 mval[i].br_state == XFS_EXT_UNWRITTEN);
4674 }
4675}
4676#endif /* DEBUG */
4677
4678
4679/*
4680 * Map file blocks to filesystem blocks.
4681 * File range is given by the bno/len pair.
4682 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4683 * into a hole or past eof.
4684 * Only allocates blocks from a single allocation group,
4685 * to avoid locking problems.
4686 * The returned value in "firstblock" from the first call in a transaction
4687 * must be remembered and presented to subsequent calls in "firstblock".
4688 * An upper bound for the number of blocks to be allocated is supplied to
4689 * the first call in "total"; if no allocation group has that many free
4690 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4691 */
4692int /* error */
4693xfs_bmapi(
4694 xfs_trans_t *tp, /* transaction pointer */
4695 xfs_inode_t *ip, /* incore inode */
4696 xfs_fileoff_t bno, /* starting file offs. mapped */
4697 xfs_filblks_t len, /* length to map in file */
4698 int flags, /* XFS_BMAPI_... */
4699 xfs_fsblock_t *firstblock, /* first allocated block
4700 controls a.g. for allocs */
4701 xfs_extlen_t total, /* total blocks needed */
4702 xfs_bmbt_irec_t *mval, /* output: map values */
4703 int *nmap, /* i/o: mval size/count */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004704 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4705 xfs_extdelta_t *delta) /* o: change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706{
4707 xfs_fsblock_t abno; /* allocated block number */
4708 xfs_extlen_t alen; /* allocated extent length */
4709 xfs_fileoff_t aoff; /* allocated file offset */
4710 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 xfs_btree_cur_t *cur; /* bmap btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 xfs_fileoff_t end; /* end of mapped file region */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004713 int eof; /* we've hit the end of extents */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004714 xfs_bmbt_rec_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004716 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 xfs_ifork_t *ifp; /* inode fork pointer */
4718 xfs_extlen_t indlen; /* indirect blocks length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 xfs_extnum_t lastx; /* last useful extent number */
4720 int logflags; /* flags for transaction logging */
4721 xfs_extlen_t minleft; /* min blocks left after allocation */
4722 xfs_extlen_t minlen; /* min allocation size */
4723 xfs_mount_t *mp; /* xfs mount structure */
4724 int n; /* current extent index */
4725 int nallocs; /* number of extents alloc\'d */
4726 xfs_extnum_t nextents; /* number of extents in file */
4727 xfs_fileoff_t obno; /* old block number (offset) */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004728 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 int tmp_logflags; /* temp flags holder */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004730 int whichfork; /* data or attr fork */
4731 char inhole; /* current location is hole in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 char wasdelay; /* old extent was delayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 char wr; /* this is a write request */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004734 char rt; /* this is a realtime file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735#ifdef DEBUG
4736 xfs_fileoff_t orig_bno; /* original block number value */
4737 int orig_flags; /* original flags arg value */
4738 xfs_filblks_t orig_len; /* original value of len arg */
4739 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4740 int orig_nmap; /* original value of *nmap */
4741
4742 orig_bno = bno;
4743 orig_len = len;
4744 orig_flags = flags;
4745 orig_mval = mval;
4746 orig_nmap = *nmap;
4747#endif
4748 ASSERT(*nmap >= 1);
4749 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4750 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4751 XFS_ATTR_FORK : XFS_DATA_FORK;
4752 mp = ip->i_mount;
4753 if (unlikely(XFS_TEST_ERROR(
4754 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4755 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4756 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4757 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4758 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4759 return XFS_ERROR(EFSCORRUPTED);
4760 }
4761 if (XFS_FORCED_SHUTDOWN(mp))
4762 return XFS_ERROR(EIO);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004763 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 ifp = XFS_IFORK_PTR(ip, whichfork);
4765 ASSERT(ifp->if_ext_max ==
4766 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4767 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4768 XFS_STATS_INC(xs_blk_mapw);
4769 else
4770 XFS_STATS_INC(xs_blk_mapr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 /*
Nathan Scott39269e22006-03-14 13:33:50 +11004772 * IGSTATE flag is used to combine extents which
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 * differ only due to the state of the extents.
4774 * This technique is used from xfs_getbmap()
4775 * when the caller does not wish to see the
4776 * separation (which is the default).
4777 *
4778 * This technique is also used when writing a
4779 * buffer which has been partially written,
4780 * (usually by being flushed during a chunkread),
4781 * to ensure one write takes place. This also
4782 * prevents a change in the xfs inode extents at
4783 * this time, intentionally. This change occurs
4784 * on completion of the write operation, in
4785 * xfs_strat_comp(), where the xfs_bmapi() call
4786 * is transactioned, and the extents combined.
4787 */
Nathan Scott39269e22006-03-14 13:33:50 +11004788 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4789 wr = 0; /* no allocations are allowed */
4790 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 logflags = 0;
4792 nallocs = 0;
4793 cur = NULL;
4794 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4795 ASSERT(wr && tp);
4796 if ((error = xfs_bmap_local_to_extents(tp, ip,
4797 firstblock, total, &logflags, whichfork)))
4798 goto error0;
4799 }
4800 if (wr && *firstblock == NULLFSBLOCK) {
4801 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Christoph Hellwig16259e72005-11-02 15:11:25 +11004802 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 else
4804 minleft = 1;
4805 } else
4806 minleft = 0;
4807 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4808 (error = xfs_iread_extents(tp, ip, whichfork)))
4809 goto error0;
4810 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4811 &prev);
4812 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4813 n = 0;
4814 end = bno + len;
4815 obno = bno;
4816 bma.ip = NULL;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004817 if (delta) {
4818 delta->xed_startoff = NULLFILEOFF;
4819 delta->xed_blockcount = 0;
4820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 while (bno < end && n < *nmap) {
4822 /*
4823 * Reading past eof, act as though there's a hole
4824 * up to end.
4825 */
4826 if (eof && !wr)
4827 got.br_startoff = end;
4828 inhole = eof || got.br_startoff > bno;
Nathan Scott39269e22006-03-14 13:33:50 +11004829 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 ISNULLSTARTBLOCK(got.br_startblock);
4831 /*
4832 * First, deal with the hole before the allocated space
4833 * that we found, if any.
4834 */
4835 if (wr && (inhole || wasdelay)) {
4836 /*
4837 * For the wasdelay case, we could also just
4838 * allocate the stuff asked for in this bmap call
4839 * but that wouldn't be as good.
4840 */
Nathan Scott39269e22006-03-14 13:33:50 +11004841 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 alen = (xfs_extlen_t)got.br_blockcount;
4843 aoff = got.br_startoff;
4844 if (lastx != NULLEXTNUM && lastx) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004845 ep = xfs_iext_get_ext(ifp, lastx - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 xfs_bmbt_get_all(ep, &prev);
4847 }
4848 } else if (wasdelay) {
4849 alen = (xfs_extlen_t)
4850 XFS_FILBLKS_MIN(len,
4851 (got.br_startoff +
4852 got.br_blockcount) - bno);
4853 aoff = bno;
4854 } else {
4855 alen = (xfs_extlen_t)
4856 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4857 if (!eof)
4858 alen = (xfs_extlen_t)
4859 XFS_FILBLKS_MIN(alen,
4860 got.br_startoff - bno);
4861 aoff = bno;
4862 }
Nathan Scott39269e22006-03-14 13:33:50 +11004863 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4864 if (flags & XFS_BMAPI_DELAY) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11004865 xfs_extlen_t extsz;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004866
4867 /* Figure out the extent size, adjust alen */
David Chinner957d0eb2007-06-18 16:50:37 +10004868 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004869 if (extsz) {
4870 error = xfs_bmap_extsize_align(mp,
4871 &got, &prev, extsz,
Nathan Scott39269e22006-03-14 13:33:50 +11004872 rt, eof,
4873 flags&XFS_BMAPI_DELAY,
4874 flags&XFS_BMAPI_CONVERT,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004875 &aoff, &alen);
4876 ASSERT(!error);
4877 }
4878
4879 if (rt)
4880 extsz = alen / mp->m_sb.sb_rextsize;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004881
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 /*
4883 * Make a transaction-less quota reservation for
4884 * delayed allocation blocks. This number gets
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004885 * adjusted later. We return if we haven't
4886 * allocated blocks already inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 */
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004888 if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS(
Nathan Scott06d10dd2005-06-21 15:48:47 +10004889 mp, NULL, ip, (long)alen, 0,
4890 rt ? XFS_QMOPT_RES_RTBLKS :
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004891 XFS_QMOPT_RES_REGBLKS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 if (n == 0) {
4893 *nmap = 0;
4894 ASSERT(cur == NULL);
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004895 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 }
4897 break;
4898 }
4899
4900 /*
4901 * Split changing sb for alen and indlen since
4902 * they could be coming from different places.
4903 */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004904 indlen = (xfs_extlen_t)
4905 xfs_bmap_worst_indlen(ip, alen);
4906 ASSERT(indlen > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
Nathan Scottdd9f4382006-01-11 15:28:28 +11004908 if (rt) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004909 error = xfs_mod_incore_sb(mp,
4910 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004911 -((int64_t)extsz), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004912 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004913 } else {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004914 error = xfs_mod_incore_sb(mp,
4915 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004916 -((int64_t)alen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004917 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004918 }
David Chinner3bdbfb12005-09-02 16:40:47 +10004919 if (!error) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004920 error = xfs_mod_incore_sb(mp,
4921 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004922 -((int64_t)indlen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004923 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004924 if (error && rt)
4925 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004926 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004927 (int64_t)extsz, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004928 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004929 else if (error)
4930 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004931 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004932 (int64_t)alen, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004933 XFS_BMAPI_RSVBLOCKS));
David Chinner3bdbfb12005-09-02 16:40:47 +10004934 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004935
4936 if (error) {
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004937 if (XFS_IS_QUOTA_ON(mp))
Nathan Scott06d10dd2005-06-21 15:48:47 +10004938 /* unreserve the blocks now */
Nathan Scottdd9f4382006-01-11 15:28:28 +11004939 (void)
Nathan Scott06d10dd2005-06-21 15:48:47 +10004940 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4941 mp, NULL, ip,
4942 (long)alen, 0, rt ?
4943 XFS_QMOPT_RES_RTBLKS :
4944 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 break;
4946 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004947
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 ip->i_delayed_blks += alen;
4949 abno = NULLSTARTBLOCK(indlen);
4950 } else {
4951 /*
4952 * If first time, allocate and fill in
4953 * once-only bma fields.
4954 */
4955 if (bma.ip == NULL) {
4956 bma.tp = tp;
4957 bma.ip = ip;
4958 bma.prevp = &prev;
4959 bma.gotp = &got;
4960 bma.total = total;
4961 bma.userdata = 0;
4962 }
4963 /* Indicate if this is the first user data
4964 * in the file, or just any user data.
4965 */
Nathan Scott39269e22006-03-14 13:33:50 +11004966 if (!(flags & XFS_BMAPI_METADATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 bma.userdata = (aoff == 0) ?
4968 XFS_ALLOC_INITIAL_USER_DATA :
4969 XFS_ALLOC_USERDATA;
4970 }
4971 /*
4972 * Fill in changeable bma fields.
4973 */
4974 bma.eof = eof;
4975 bma.firstblock = *firstblock;
4976 bma.alen = alen;
4977 bma.off = aoff;
Adrian Bunk72880262006-08-30 13:41:58 +10004978 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 bma.wasdel = wasdelay;
4980 bma.minlen = minlen;
4981 bma.low = flist->xbf_low;
4982 bma.minleft = minleft;
4983 /*
4984 * Only want to do the alignment at the
4985 * eof if it is userdata and allocation length
4986 * is larger than a stripe unit.
4987 */
4988 if (mp->m_dalign && alen >= mp->m_dalign &&
Nathan Scott39269e22006-03-14 13:33:50 +11004989 (!(flags & XFS_BMAPI_METADATA)) &&
4990 (whichfork == XFS_DATA_FORK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 if ((error = xfs_bmap_isaeof(ip, aoff,
4992 whichfork, &bma.aeof)))
4993 goto error0;
4994 } else
4995 bma.aeof = 0;
4996 /*
4997 * Call allocator.
4998 */
4999 if ((error = xfs_bmap_alloc(&bma)))
5000 goto error0;
5001 /*
5002 * Copy out result fields.
5003 */
5004 abno = bma.rval;
5005 if ((flist->xbf_low = bma.low))
5006 minleft = 0;
5007 alen = bma.alen;
5008 aoff = bma.off;
5009 ASSERT(*firstblock == NULLFSBLOCK ||
5010 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5011 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
5012 (flist->xbf_low &&
5013 XFS_FSB_TO_AGNO(mp, *firstblock) <
5014 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
5015 *firstblock = bma.firstblock;
5016 if (cur)
5017 cur->bc_private.b.firstblock =
5018 *firstblock;
5019 if (abno == NULLFSBLOCK)
5020 break;
5021 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5022 cur = xfs_btree_init_cursor(mp,
5023 tp, NULL, 0, XFS_BTNUM_BMAP,
5024 ip, whichfork);
5025 cur->bc_private.b.firstblock =
5026 *firstblock;
5027 cur->bc_private.b.flist = flist;
5028 }
5029 /*
5030 * Bump the number of extents we've allocated
5031 * in this call.
5032 */
5033 nallocs++;
5034 }
5035 if (cur)
5036 cur->bc_private.b.flags =
5037 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
5038 got.br_startoff = aoff;
5039 got.br_startblock = abno;
5040 got.br_blockcount = alen;
5041 got.br_state = XFS_EXT_NORM; /* assume normal */
5042 /*
5043 * Determine state of extent, and the filesystem.
5044 * A wasdelay extent has been initialized, so
5045 * shouldn't be flagged as unwritten.
5046 */
5047 if (wr && XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5048 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5049 got.br_state = XFS_EXT_UNWRITTEN;
5050 }
5051 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005052 firstblock, flist, &tmp_logflags, delta,
5053 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 logflags |= tmp_logflags;
5055 if (error)
5056 goto error0;
5057 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005058 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5060 xfs_bmbt_get_all(ep, &got);
5061 ASSERT(got.br_startoff <= aoff);
5062 ASSERT(got.br_startoff + got.br_blockcount >=
5063 aoff + alen);
5064#ifdef DEBUG
Nathan Scott39269e22006-03-14 13:33:50 +11005065 if (flags & XFS_BMAPI_DELAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
5067 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
5068 }
5069 ASSERT(got.br_state == XFS_EXT_NORM ||
5070 got.br_state == XFS_EXT_UNWRITTEN);
5071#endif
5072 /*
5073 * Fall down into the found allocated space case.
5074 */
5075 } else if (inhole) {
5076 /*
5077 * Reading in a hole.
5078 */
5079 mval->br_startoff = bno;
5080 mval->br_startblock = HOLESTARTBLOCK;
5081 mval->br_blockcount =
5082 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5083 mval->br_state = XFS_EXT_NORM;
5084 bno += mval->br_blockcount;
5085 len -= mval->br_blockcount;
5086 mval++;
5087 n++;
5088 continue;
5089 }
5090 /*
5091 * Then deal with the allocated space we found.
5092 */
5093 ASSERT(ep != NULL);
Nathan Scott39269e22006-03-14 13:33:50 +11005094 if (!(flags & XFS_BMAPI_ENTIRE) &&
5095 (got.br_startoff + got.br_blockcount > obno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 if (obno > bno)
5097 bno = obno;
5098 ASSERT((bno >= obno) || (n == 0));
5099 ASSERT(bno < end);
5100 mval->br_startoff = bno;
5101 if (ISNULLSTARTBLOCK(got.br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11005102 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 mval->br_startblock = DELAYSTARTBLOCK;
5104 } else
5105 mval->br_startblock =
5106 got.br_startblock +
5107 (bno - got.br_startoff);
5108 /*
5109 * Return the minimum of what we got and what we
5110 * asked for for the length. We can use the len
5111 * variable here because it is modified below
5112 * and we could have been there before coming
5113 * here if the first part of the allocation
5114 * didn't overlap what was asked for.
5115 */
5116 mval->br_blockcount =
5117 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5118 (bno - got.br_startoff));
5119 mval->br_state = got.br_state;
5120 ASSERT(mval->br_blockcount <= len);
5121 } else {
5122 *mval = got;
5123 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11005124 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 mval->br_startblock = DELAYSTARTBLOCK;
5126 }
5127 }
5128
5129 /*
5130 * Check if writing previously allocated but
5131 * unwritten extents.
5132 */
5133 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5134 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5135 /*
5136 * Modify (by adding) the state flag, if writing.
5137 */
5138 ASSERT(mval->br_blockcount <= len);
5139 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5140 cur = xfs_btree_init_cursor(mp,
5141 tp, NULL, 0, XFS_BTNUM_BMAP,
5142 ip, whichfork);
5143 cur->bc_private.b.firstblock =
5144 *firstblock;
5145 cur->bc_private.b.flist = flist;
5146 }
5147 mval->br_state = XFS_EXT_NORM;
5148 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005149 firstblock, flist, &tmp_logflags, delta,
5150 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 logflags |= tmp_logflags;
5152 if (error)
5153 goto error0;
5154 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005155 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5157 xfs_bmbt_get_all(ep, &got);
5158 /*
5159 * We may have combined previously unwritten
5160 * space with written space, so generate
5161 * another request.
5162 */
5163 if (mval->br_blockcount < len)
5164 continue;
5165 }
5166
Nathan Scott39269e22006-03-14 13:33:50 +11005167 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 ((mval->br_startoff + mval->br_blockcount) <= end));
Nathan Scott39269e22006-03-14 13:33:50 +11005169 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5170 (mval->br_blockcount <= len) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 (mval->br_startoff < obno));
5172 bno = mval->br_startoff + mval->br_blockcount;
5173 len = end - bno;
5174 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5175 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5176 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5177 ASSERT(mval->br_state == mval[-1].br_state);
5178 mval[-1].br_blockcount = mval->br_blockcount;
5179 mval[-1].br_state = mval->br_state;
5180 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5181 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5182 mval[-1].br_startblock != HOLESTARTBLOCK &&
5183 mval->br_startblock ==
5184 mval[-1].br_startblock + mval[-1].br_blockcount &&
Nathan Scott39269e22006-03-14 13:33:50 +11005185 ((flags & XFS_BMAPI_IGSTATE) ||
5186 mval[-1].br_state == mval->br_state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 ASSERT(mval->br_startoff ==
5188 mval[-1].br_startoff + mval[-1].br_blockcount);
5189 mval[-1].br_blockcount += mval->br_blockcount;
5190 } else if (n > 0 &&
5191 mval->br_startblock == DELAYSTARTBLOCK &&
5192 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5193 mval->br_startoff ==
5194 mval[-1].br_startoff + mval[-1].br_blockcount) {
5195 mval[-1].br_blockcount += mval->br_blockcount;
5196 mval[-1].br_state = mval->br_state;
5197 } else if (!((n == 0) &&
5198 ((mval->br_startoff + mval->br_blockcount) <=
5199 obno))) {
5200 mval++;
5201 n++;
5202 }
5203 /*
5204 * If we're done, stop now. Stop when we've allocated
5205 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5206 * the transaction may get too big.
5207 */
5208 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5209 break;
5210 /*
5211 * Else go on to the next record.
5212 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005213 ep = xfs_iext_get_ext(ifp, ++lastx);
David Chinner4e5ae832007-06-05 16:24:15 +10005214 prev = got;
5215 if (lastx >= nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 eof = 1;
David Chinner4e5ae832007-06-05 16:24:15 +10005217 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 xfs_bmbt_get_all(ep, &got);
5219 }
5220 ifp->if_lastex = lastx;
5221 *nmap = n;
5222 /*
5223 * Transform from btree to extents, give it cur.
5224 */
5225 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5226 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5227 ASSERT(wr && cur);
5228 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5229 &tmp_logflags, whichfork);
5230 logflags |= tmp_logflags;
5231 if (error)
5232 goto error0;
5233 }
5234 ASSERT(ifp->if_ext_max ==
5235 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5236 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5237 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5238 error = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005239 if (delta && delta->xed_startoff != NULLFILEOFF) {
5240 /* A change was actually made.
5241 * Note that delta->xed_blockount is an offset at this
5242 * point and needs to be converted to a block count.
5243 */
5244 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5245 delta->xed_blockcount -= delta->xed_startoff;
5246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247error0:
5248 /*
5249 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005250 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 */
5252 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5253 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5254 logflags &= ~XFS_ILOG_FEXT(whichfork);
5255 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5256 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5257 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5258 /*
5259 * Log whatever the flags say, even if error. Otherwise we might miss
5260 * detecting a case where the data is changed, there's an error,
5261 * and it's not logged so we don't shutdown when we should.
5262 */
5263 if (logflags) {
5264 ASSERT(tp && wr);
5265 xfs_trans_log_inode(tp, ip, logflags);
5266 }
5267 if (cur) {
5268 if (!error) {
5269 ASSERT(*firstblock == NULLFSBLOCK ||
5270 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5271 XFS_FSB_TO_AGNO(mp,
5272 cur->bc_private.b.firstblock) ||
5273 (flist->xbf_low &&
5274 XFS_FSB_TO_AGNO(mp, *firstblock) <
5275 XFS_FSB_TO_AGNO(mp,
5276 cur->bc_private.b.firstblock)));
5277 *firstblock = cur->bc_private.b.firstblock;
5278 }
5279 xfs_btree_del_cursor(cur,
5280 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5281 }
5282 if (!error)
5283 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5284 orig_nmap, *nmap);
5285 return error;
5286}
5287
5288/*
5289 * Map file blocks to filesystem blocks, simple version.
5290 * One block (extent) only, read-only.
5291 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5292 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5293 * was set and all the others were clear.
5294 */
5295int /* error */
5296xfs_bmapi_single(
5297 xfs_trans_t *tp, /* transaction pointer */
5298 xfs_inode_t *ip, /* incore inode */
5299 int whichfork, /* data or attr fork */
5300 xfs_fsblock_t *fsb, /* output: mapped block */
5301 xfs_fileoff_t bno) /* starting file offs. mapped */
5302{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005303 int eof; /* we've hit the end of extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005305 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 xfs_ifork_t *ifp; /* inode fork pointer */
5307 xfs_extnum_t lastx; /* last useful extent number */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005308 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309
5310 ifp = XFS_IFORK_PTR(ip, whichfork);
5311 if (unlikely(
5312 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5313 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5314 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5315 ip->i_mount);
5316 return XFS_ERROR(EFSCORRUPTED);
5317 }
5318 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5319 return XFS_ERROR(EIO);
5320 XFS_STATS_INC(xs_blk_mapr);
5321 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5322 (error = xfs_iread_extents(tp, ip, whichfork)))
5323 return error;
5324 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5325 &prev);
5326 /*
5327 * Reading past eof, act as though there's a hole
5328 * up to end.
5329 */
5330 if (eof || got.br_startoff > bno) {
5331 *fsb = NULLFSBLOCK;
5332 return 0;
5333 }
5334 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
5335 ASSERT(bno < got.br_startoff + got.br_blockcount);
5336 *fsb = got.br_startblock + (bno - got.br_startoff);
5337 ifp->if_lastex = lastx;
5338 return 0;
5339}
5340
5341/*
5342 * Unmap (remove) blocks from a file.
5343 * If nexts is nonzero then the number of extents to remove is limited to
5344 * that value. If not all extents in the block range can be removed then
5345 * *done is set.
5346 */
5347int /* error */
5348xfs_bunmapi(
5349 xfs_trans_t *tp, /* transaction pointer */
5350 struct xfs_inode *ip, /* incore inode */
5351 xfs_fileoff_t bno, /* starting offset to unmap */
5352 xfs_filblks_t len, /* length to unmap in file */
5353 int flags, /* misc flags */
5354 xfs_extnum_t nexts, /* number of extents max */
5355 xfs_fsblock_t *firstblock, /* first allocated block
5356 controls a.g. for allocs */
5357 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005358 xfs_extdelta_t *delta, /* o: change made to incore
5359 extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 int *done) /* set if not done yet */
5361{
5362 xfs_btree_cur_t *cur; /* bmap btree cursor */
5363 xfs_bmbt_irec_t del; /* extent being deleted */
5364 int eof; /* is deleting at eof */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005365 xfs_bmbt_rec_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 int error; /* error return value */
5367 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005368 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 xfs_ifork_t *ifp; /* inode fork pointer */
5370 int isrt; /* freeing in rt area */
5371 xfs_extnum_t lastx; /* last extent index used */
5372 int logflags; /* transaction logging flags */
5373 xfs_extlen_t mod; /* rt extent offset */
5374 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005375 xfs_extnum_t nextents; /* number of file extents */
5376 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 xfs_fileoff_t start; /* first file offset deleted */
5378 int tmp_logflags; /* partial logging flags */
5379 int wasdel; /* was a delayed alloc extent */
5380 int whichfork; /* data or attribute fork */
5381 int rsvd; /* OK to allocate reserved blocks */
5382 xfs_fsblock_t sum;
5383
5384 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5385 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5386 XFS_ATTR_FORK : XFS_DATA_FORK;
5387 ifp = XFS_IFORK_PTR(ip, whichfork);
5388 if (unlikely(
5389 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5390 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5391 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5392 ip->i_mount);
5393 return XFS_ERROR(EFSCORRUPTED);
5394 }
5395 mp = ip->i_mount;
5396 if (XFS_FORCED_SHUTDOWN(mp))
5397 return XFS_ERROR(EIO);
5398 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5399 ASSERT(len > 0);
5400 ASSERT(nexts >= 0);
5401 ASSERT(ifp->if_ext_max ==
5402 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5403 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5404 (error = xfs_iread_extents(tp, ip, whichfork)))
5405 return error;
5406 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5407 if (nextents == 0) {
5408 *done = 1;
5409 return 0;
5410 }
5411 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005412 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 start = bno;
5414 bno = start + len - 1;
5415 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5416 &prev);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005417 if (delta) {
5418 delta->xed_startoff = NULLFILEOFF;
5419 delta->xed_blockcount = 0;
5420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 /*
5422 * Check to see if the given block number is past the end of the
5423 * file, back up to the last block if so...
5424 */
5425 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005426 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 xfs_bmbt_get_all(ep, &got);
5428 bno = got.br_startoff + got.br_blockcount - 1;
5429 }
5430 logflags = 0;
5431 if (ifp->if_flags & XFS_IFBROOT) {
5432 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5433 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
5434 whichfork);
5435 cur->bc_private.b.firstblock = *firstblock;
5436 cur->bc_private.b.flist = flist;
5437 cur->bc_private.b.flags = 0;
5438 } else
5439 cur = NULL;
5440 extno = 0;
5441 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5442 (nexts == 0 || extno < nexts)) {
5443 /*
5444 * Is the found extent after a hole in which bno lives?
5445 * Just back up to the previous extent, if so.
5446 */
5447 if (got.br_startoff > bno) {
5448 if (--lastx < 0)
5449 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005450 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 xfs_bmbt_get_all(ep, &got);
5452 }
5453 /*
5454 * Is the last block of this extent before the range
5455 * we're supposed to delete? If so, we're done.
5456 */
5457 bno = XFS_FILEOFF_MIN(bno,
5458 got.br_startoff + got.br_blockcount - 1);
5459 if (bno < start)
5460 break;
5461 /*
5462 * Then deal with the (possibly delayed) allocated space
5463 * we found.
5464 */
5465 ASSERT(ep != NULL);
5466 del = got;
5467 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
5468 if (got.br_startoff < start) {
5469 del.br_startoff = start;
5470 del.br_blockcount -= start - got.br_startoff;
5471 if (!wasdel)
5472 del.br_startblock += start - got.br_startoff;
5473 }
5474 if (del.br_startoff + del.br_blockcount > bno + 1)
5475 del.br_blockcount = bno + 1 - del.br_startoff;
5476 sum = del.br_startblock + del.br_blockcount;
5477 if (isrt &&
5478 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5479 /*
5480 * Realtime extent not lined up at the end.
5481 * The extent could have been split into written
5482 * and unwritten pieces, or we could just be
5483 * unmapping part of it. But we can't really
5484 * get rid of part of a realtime extent.
5485 */
5486 if (del.br_state == XFS_EXT_UNWRITTEN ||
5487 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5488 /*
5489 * This piece is unwritten, or we're not
5490 * using unwritten extents. Skip over it.
5491 */
5492 ASSERT(bno >= mod);
5493 bno -= mod > del.br_blockcount ?
5494 del.br_blockcount : mod;
5495 if (bno < got.br_startoff) {
5496 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005497 xfs_bmbt_get_all(xfs_iext_get_ext(
5498 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 }
5500 continue;
5501 }
5502 /*
5503 * It's written, turn it unwritten.
5504 * This is better than zeroing it.
5505 */
5506 ASSERT(del.br_state == XFS_EXT_NORM);
5507 ASSERT(xfs_trans_get_block_res(tp) > 0);
5508 /*
5509 * If this spans a realtime extent boundary,
5510 * chop it back to the start of the one we end at.
5511 */
5512 if (del.br_blockcount > mod) {
5513 del.br_startoff += del.br_blockcount - mod;
5514 del.br_startblock += del.br_blockcount - mod;
5515 del.br_blockcount = mod;
5516 }
5517 del.br_state = XFS_EXT_UNWRITTEN;
5518 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005519 firstblock, flist, &logflags, delta,
5520 XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 if (error)
5522 goto error0;
5523 goto nodelete;
5524 }
5525 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5526 /*
5527 * Realtime extent is lined up at the end but not
5528 * at the front. We'll get rid of full extents if
5529 * we can.
5530 */
5531 mod = mp->m_sb.sb_rextsize - mod;
5532 if (del.br_blockcount > mod) {
5533 del.br_blockcount -= mod;
5534 del.br_startoff += mod;
5535 del.br_startblock += mod;
5536 } else if ((del.br_startoff == start &&
5537 (del.br_state == XFS_EXT_UNWRITTEN ||
5538 xfs_trans_get_block_res(tp) == 0)) ||
5539 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5540 /*
5541 * Can't make it unwritten. There isn't
5542 * a full extent here so just skip it.
5543 */
5544 ASSERT(bno >= del.br_blockcount);
5545 bno -= del.br_blockcount;
5546 if (bno < got.br_startoff) {
5547 if (--lastx >= 0)
5548 xfs_bmbt_get_all(--ep, &got);
5549 }
5550 continue;
5551 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5552 /*
5553 * This one is already unwritten.
5554 * It must have a written left neighbor.
5555 * Unwrite the killed part of that one and
5556 * try again.
5557 */
5558 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005559 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5560 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 ASSERT(prev.br_state == XFS_EXT_NORM);
5562 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
5563 ASSERT(del.br_startblock ==
5564 prev.br_startblock + prev.br_blockcount);
5565 if (prev.br_startoff < start) {
5566 mod = start - prev.br_startoff;
5567 prev.br_blockcount -= mod;
5568 prev.br_startblock += mod;
5569 prev.br_startoff = start;
5570 }
5571 prev.br_state = XFS_EXT_UNWRITTEN;
5572 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5573 &prev, firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005574 delta, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 if (error)
5576 goto error0;
5577 goto nodelete;
5578 } else {
5579 ASSERT(del.br_state == XFS_EXT_NORM);
5580 del.br_state = XFS_EXT_UNWRITTEN;
5581 error = xfs_bmap_add_extent(ip, lastx, &cur,
5582 &del, firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005583 delta, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 if (error)
5585 goto error0;
5586 goto nodelete;
5587 }
5588 }
5589 if (wasdel) {
5590 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005591 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005592 if (isrt) {
5593 xfs_filblks_t rtexts;
5594
5595 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5596 do_div(rtexts, mp->m_sb.sb_rextsize);
5597 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005598 (int64_t)rtexts, rsvd);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005599 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5600 NULL, ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005601 XFS_QMOPT_RES_RTBLKS);
5602 } else {
5603 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005604 (int64_t)del.br_blockcount, rsvd);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005605 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5606 NULL, ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 ip->i_delayed_blks -= del.br_blockcount;
5610 if (cur)
5611 cur->bc_private.b.flags |=
5612 XFS_BTCUR_BPRV_WASDEL;
5613 } else if (cur)
5614 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5615 /*
5616 * If it's the case where the directory code is running
5617 * with no block reservation, and the deleted block is in
5618 * the middle of its extent, and the resulting insert
5619 * of an extent would cause transformation to btree format,
5620 * then reject it. The calling code will then swap
5621 * blocks around instead.
5622 * We have to do this now, rather than waiting for the
5623 * conversion to btree format, since the transaction
5624 * will be dirty.
5625 */
5626 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5627 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5628 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5629 del.br_startoff > got.br_startoff &&
5630 del.br_startoff + del.br_blockcount <
5631 got.br_startoff + got.br_blockcount) {
5632 error = XFS_ERROR(ENOSPC);
5633 goto error0;
5634 }
5635 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005636 &tmp_logflags, delta, whichfork, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 logflags |= tmp_logflags;
5638 if (error)
5639 goto error0;
5640 bno = del.br_startoff - 1;
5641nodelete:
5642 lastx = ifp->if_lastex;
5643 /*
5644 * If not done go on to the next (previous) record.
5645 * Reset ep in case the extents array was re-alloced.
5646 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005647 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5649 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5650 xfs_bmbt_get_startoff(ep) > bno) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005651 if (--lastx >= 0)
5652 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 }
5654 if (lastx >= 0)
5655 xfs_bmbt_get_all(ep, &got);
5656 extno++;
5657 }
5658 }
5659 ifp->if_lastex = lastx;
5660 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5661 ASSERT(ifp->if_ext_max ==
5662 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5663 /*
5664 * Convert to a btree if necessary.
5665 */
5666 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5667 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5668 ASSERT(cur == NULL);
5669 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5670 &cur, 0, &tmp_logflags, whichfork);
5671 logflags |= tmp_logflags;
5672 if (error)
5673 goto error0;
5674 }
5675 /*
5676 * transform from btree to extents, give it cur
5677 */
5678 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5679 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5680 ASSERT(cur != NULL);
5681 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5682 whichfork);
5683 logflags |= tmp_logflags;
5684 if (error)
5685 goto error0;
5686 }
5687 /*
5688 * transform from extents to local?
5689 */
5690 ASSERT(ifp->if_ext_max ==
5691 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5692 error = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005693 if (delta && delta->xed_startoff != NULLFILEOFF) {
5694 /* A change was actually made.
5695 * Note that delta->xed_blockount is an offset at this
5696 * point and needs to be converted to a block count.
5697 */
5698 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5699 delta->xed_blockcount -= delta->xed_startoff;
5700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701error0:
5702 /*
5703 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005704 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 */
5706 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5707 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5708 logflags &= ~XFS_ILOG_FEXT(whichfork);
5709 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5710 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5711 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5712 /*
5713 * Log inode even in the error case, if the transaction
5714 * is dirty we'll need to shut down the filesystem.
5715 */
5716 if (logflags)
5717 xfs_trans_log_inode(tp, ip, logflags);
5718 if (cur) {
5719 if (!error) {
5720 *firstblock = cur->bc_private.b.firstblock;
5721 cur->bc_private.b.allocated = 0;
5722 }
5723 xfs_btree_del_cursor(cur,
5724 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5725 }
5726 return error;
5727}
5728
5729/*
5730 * Fcntl interface to xfs_bmapi.
5731 */
5732int /* error code */
5733xfs_getbmap(
5734 bhv_desc_t *bdp, /* XFS behavior descriptor*/
5735 struct getbmap *bmv, /* user bmap structure */
5736 void __user *ap, /* pointer to user's array */
5737 int interface) /* interface flags */
5738{
5739 __int64_t bmvend; /* last block requested */
5740 int error; /* return value */
5741 __int64_t fixlen; /* length for -1 case */
5742 int i; /* extent number */
5743 xfs_inode_t *ip; /* xfs incore inode pointer */
Nathan Scott67fcaa72006-06-09 17:00:52 +10005744 bhv_vnode_t *vp; /* corresponding vnode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 int lock; /* lock state */
5746 xfs_bmbt_irec_t *map; /* buffer for user's data */
5747 xfs_mount_t *mp; /* file system mount point */
5748 int nex; /* # of user extents can do */
5749 int nexleft; /* # of user extents left */
5750 int subnex; /* # of bmapi's can do */
5751 int nmap; /* number of map entries */
5752 struct getbmap out; /* output structure */
5753 int whichfork; /* data or attr fork */
5754 int prealloced; /* this is a file with
5755 * preallocated data space */
5756 int sh_unwritten; /* true, if unwritten */
5757 /* extents listed separately */
5758 int bmapi_flags; /* flags for xfs_bmapi */
5759 __int32_t oflags; /* getbmapx bmv_oflags field */
5760
5761 vp = BHV_TO_VNODE(bdp);
5762 ip = XFS_BHVTOI(bdp);
5763 mp = ip->i_mount;
5764
5765 whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5766 sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
5767
5768 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5769 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5770 * bit is set for the file, generate a read event in order
5771 * that the DMAPI application may do its thing before we return
5772 * the extents. Usually this means restoring user file data to
5773 * regions of the file that look like holes.
5774 *
5775 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5776 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5777 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5778 * could misinterpret holes in a DMAPI file as true holes,
5779 * when in fact they may represent offline user data.
5780 */
5781 if ( (interface & BMV_IF_NO_DMAPI_READ) == 0
5782 && DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)
5783 && whichfork == XFS_DATA_FORK) {
5784
5785 error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, 0, 0, 0, NULL);
5786 if (error)
5787 return XFS_ERROR(error);
5788 }
5789
5790 if (whichfork == XFS_ATTR_FORK) {
5791 if (XFS_IFORK_Q(ip)) {
5792 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5793 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5794 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5795 return XFS_ERROR(EINVAL);
5796 } else if (unlikely(
5797 ip->i_d.di_aformat != 0 &&
5798 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5799 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5800 ip->i_mount);
5801 return XFS_ERROR(EFSCORRUPTED);
5802 }
5803 } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5804 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5805 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5806 return XFS_ERROR(EINVAL);
5807 if (whichfork == XFS_DATA_FORK) {
David Chinner957d0eb2007-06-18 16:50:37 +10005808 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005809 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 prealloced = 1;
5811 fixlen = XFS_MAXIOFFSET(mp);
5812 } else {
5813 prealloced = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005814 fixlen = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 }
5816 } else {
5817 prealloced = 0;
5818 fixlen = 1LL << 32;
5819 }
5820
5821 if (bmv->bmv_length == -1) {
5822 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5823 bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
5824 (__int64_t)0);
5825 } else if (bmv->bmv_length < 0)
5826 return XFS_ERROR(EINVAL);
5827 if (bmv->bmv_length == 0) {
5828 bmv->bmv_entries = 0;
5829 return 0;
5830 }
5831 nex = bmv->bmv_count - 1;
5832 if (nex <= 0)
5833 return XFS_ERROR(EINVAL);
5834 bmvend = bmv->bmv_offset + bmv->bmv_length;
5835
5836 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5837
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005838 if (whichfork == XFS_DATA_FORK &&
5839 (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
Nathan Scott67fcaa72006-06-09 17:00:52 +10005841 error = bhv_vop_flush_pages(vp, (xfs_off_t)0, -1, 0, FI_REMAPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 }
5843
5844 ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
5845
5846 lock = xfs_ilock_map_shared(ip);
5847
5848 /*
5849 * Don't let nex be bigger than the number of extents
5850 * we can have assuming alternating holes and real extents.
5851 */
5852 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5853 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5854
5855 bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
5856 ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
5857
5858 /*
5859 * Allocate enough space to handle "subnex" maps at a time.
5860 */
5861 subnex = 16;
5862 map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
5863
5864 bmv->bmv_entries = 0;
5865
5866 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
5867 error = 0;
5868 goto unlock_and_return;
5869 }
5870
5871 nexleft = nex;
5872
5873 do {
5874 nmap = (nexleft > subnex) ? subnex : nexleft;
5875 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5876 XFS_BB_TO_FSB(mp, bmv->bmv_length),
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005877 bmapi_flags, NULL, 0, map, &nmap,
5878 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 if (error)
5880 goto unlock_and_return;
5881 ASSERT(nmap <= subnex);
5882
5883 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5884 nexleft--;
5885 oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
5886 BMV_OF_PREALLOC : 0;
5887 out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
5888 out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5889 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
Yingping Lu9af0a702005-11-02 15:09:54 +11005890 if (map[i].br_startblock == HOLESTARTBLOCK &&
5891 ((prealloced && out.bmv_offset + out.bmv_length == bmvend) ||
5892 whichfork == XFS_ATTR_FORK )) {
5893 /*
5894 * came to hole at end of file or the end of
5895 attribute fork
5896 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 goto unlock_and_return;
5898 } else {
5899 out.bmv_block =
5900 (map[i].br_startblock == HOLESTARTBLOCK) ?
5901 -1 :
5902 XFS_FSB_TO_DB(ip, map[i].br_startblock);
5903
5904 /* return either getbmap/getbmapx structure. */
5905 if (interface & BMV_IF_EXTENDED) {
5906 struct getbmapx outx;
5907
5908 GETBMAP_CONVERT(out,outx);
5909 outx.bmv_oflags = oflags;
5910 outx.bmv_unused1 = outx.bmv_unused2 = 0;
5911 if (copy_to_user(ap, &outx,
5912 sizeof(outx))) {
5913 error = XFS_ERROR(EFAULT);
5914 goto unlock_and_return;
5915 }
5916 } else {
5917 if (copy_to_user(ap, &out,
5918 sizeof(out))) {
5919 error = XFS_ERROR(EFAULT);
5920 goto unlock_and_return;
5921 }
5922 }
5923 bmv->bmv_offset =
5924 out.bmv_offset + out.bmv_length;
5925 bmv->bmv_length = MAX((__int64_t)0,
5926 (__int64_t)(bmvend - bmv->bmv_offset));
5927 bmv->bmv_entries++;
5928 ap = (interface & BMV_IF_EXTENDED) ?
5929 (void __user *)
5930 ((struct getbmapx __user *)ap + 1) :
5931 (void __user *)
5932 ((struct getbmap __user *)ap + 1);
5933 }
5934 }
5935 } while (nmap && nexleft && bmv->bmv_length);
5936
5937unlock_and_return:
5938 xfs_iunlock_map_shared(ip, lock);
5939 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5940
5941 kmem_free(map, subnex * sizeof(*map));
5942
5943 return error;
5944}
5945
5946/*
5947 * Check the last inode extent to determine whether this allocation will result
5948 * in blocks being allocated at the end of the file. When we allocate new data
5949 * blocks at the end of the file which do not start at the previous data block,
5950 * we will try to align the new blocks at stripe unit boundaries.
5951 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10005952STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953xfs_bmap_isaeof(
5954 xfs_inode_t *ip, /* incore inode pointer */
5955 xfs_fileoff_t off, /* file offset in fsblocks */
5956 int whichfork, /* data or attribute fork */
5957 char *aeof) /* return value */
5958{
5959 int error; /* error return value */
5960 xfs_ifork_t *ifp; /* inode fork pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005961 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
5962 xfs_extnum_t nextents; /* number of file extents */
5963 xfs_bmbt_irec_t s; /* expanded extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964
5965 ASSERT(whichfork == XFS_DATA_FORK);
5966 ifp = XFS_IFORK_PTR(ip, whichfork);
5967 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5968 (error = xfs_iread_extents(NULL, ip, whichfork)))
5969 return error;
5970 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5971 if (nextents == 0) {
5972 *aeof = 1;
5973 return 0;
5974 }
5975 /*
5976 * Go to the last extent
5977 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005978 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 xfs_bmbt_get_all(lastrec, &s);
5980 /*
5981 * Check we are allocating in the last extent (for delayed allocations)
5982 * or past the last extent for non-delayed allocations.
5983 */
5984 *aeof = (off >= s.br_startoff &&
5985 off < s.br_startoff + s.br_blockcount &&
5986 ISNULLSTARTBLOCK(s.br_startblock)) ||
5987 off >= s.br_startoff + s.br_blockcount;
5988 return 0;
5989}
5990
5991/*
5992 * Check if the endoff is outside the last extent. If so the caller will grow
5993 * the allocation to a stripe unit boundary.
5994 */
5995int /* error */
5996xfs_bmap_eof(
5997 xfs_inode_t *ip, /* incore inode pointer */
5998 xfs_fileoff_t endoff, /* file offset in fsblocks */
5999 int whichfork, /* data or attribute fork */
6000 int *eof) /* result value */
6001{
6002 xfs_fsblock_t blockcount; /* extent block count */
6003 int error; /* error return value */
6004 xfs_ifork_t *ifp; /* inode fork pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006005 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
6006 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 xfs_fileoff_t startoff; /* extent starting file offset */
6008
6009 ASSERT(whichfork == XFS_DATA_FORK);
6010 ifp = XFS_IFORK_PTR(ip, whichfork);
6011 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6012 (error = xfs_iread_extents(NULL, ip, whichfork)))
6013 return error;
6014 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6015 if (nextents == 0) {
6016 *eof = 1;
6017 return 0;
6018 }
6019 /*
6020 * Go to the last extent
6021 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006022 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 startoff = xfs_bmbt_get_startoff(lastrec);
6024 blockcount = xfs_bmbt_get_blockcount(lastrec);
6025 *eof = endoff >= startoff + blockcount;
6026 return 0;
6027}
6028
6029#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030STATIC
6031xfs_buf_t *
6032xfs_bmap_get_bp(
6033 xfs_btree_cur_t *cur,
6034 xfs_fsblock_t bno)
6035{
6036 int i;
6037 xfs_buf_t *bp;
6038
6039 if (!cur)
6040 return(NULL);
6041
6042 bp = NULL;
6043 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6044 bp = cur->bc_bufs[i];
6045 if (!bp) break;
6046 if (XFS_BUF_ADDR(bp) == bno)
6047 break; /* Found it */
6048 }
6049 if (i == XFS_BTREE_MAXLEVELS)
6050 bp = NULL;
6051
6052 if (!bp) { /* Chase down all the log items to see if the bp is there */
6053 xfs_log_item_chunk_t *licp;
6054 xfs_trans_t *tp;
6055
6056 tp = cur->bc_tp;
6057 licp = &tp->t_items;
6058 while (!bp && licp != NULL) {
6059 if (XFS_LIC_ARE_ALL_FREE(licp)) {
6060 licp = licp->lic_next;
6061 continue;
6062 }
6063 for (i = 0; i < licp->lic_unused; i++) {
6064 xfs_log_item_desc_t *lidp;
6065 xfs_log_item_t *lip;
6066 xfs_buf_log_item_t *bip;
6067 xfs_buf_t *lbp;
6068
6069 if (XFS_LIC_ISFREE(licp, i)) {
6070 continue;
6071 }
6072
6073 lidp = XFS_LIC_SLOT(licp, i);
6074 lip = lidp->lid_item;
6075 if (lip->li_type != XFS_LI_BUF)
6076 continue;
6077
6078 bip = (xfs_buf_log_item_t *)lip;
6079 lbp = bip->bli_buf;
6080
6081 if (XFS_BUF_ADDR(lbp) == bno) {
6082 bp = lbp;
6083 break; /* Found it */
6084 }
6085 }
6086 licp = licp->lic_next;
6087 }
6088 }
6089 return(bp);
6090}
6091
6092void
6093xfs_check_block(
6094 xfs_bmbt_block_t *block,
6095 xfs_mount_t *mp,
6096 int root,
6097 short sz)
6098{
6099 int i, j, dmxr;
Christoph Hellwig576039c2006-09-28 10:58:06 +10006100 __be64 *pp, *thispa; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 xfs_bmbt_key_t *prevp, *keyp;
6102
Christoph Hellwig16259e72005-11-02 15:11:25 +11006103 ASSERT(be16_to_cpu(block->bb_level) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104
6105 prevp = NULL;
Christoph Hellwig16259e72005-11-02 15:11:25 +11006106 for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 dmxr = mp->m_bmap_dmxr[0];
6108
6109 if (root) {
6110 keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
6111 } else {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006112 keyp = XFS_BTREE_KEY_ADDR(xfs_bmbt, block, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 }
6114
6115 if (prevp) {
6116 xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
6117 }
6118 prevp = keyp;
6119
6120 /*
6121 * Compare the block numbers to see if there are dups.
6122 */
6123
6124 if (root) {
6125 pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
6126 } else {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006127 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, i, dmxr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11006129 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 if (root) {
6131 thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
6132 } else {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006133 thispa = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, j,
6134 dmxr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 }
Christoph Hellwig576039c2006-09-28 10:58:06 +10006136 if (*thispa == *pp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
6138 __FUNCTION__, j, i,
Christoph Hellwig576039c2006-09-28 10:58:06 +10006139 (unsigned long long)be64_to_cpu(*thispa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 panic("%s: ptrs are equal in node\n",
6141 __FUNCTION__);
6142 }
6143 }
6144 }
6145}
6146
6147/*
6148 * Check that the extents for the inode ip are in the right order in all
6149 * btree leaves.
6150 */
6151
6152STATIC void
6153xfs_bmap_check_leaf_extents(
6154 xfs_btree_cur_t *cur, /* btree cursor or null */
6155 xfs_inode_t *ip, /* incore inode pointer */
6156 int whichfork) /* data or attr fork */
6157{
6158 xfs_bmbt_block_t *block; /* current btree block */
6159 xfs_fsblock_t bno; /* block # of "block" */
6160 xfs_buf_t *bp; /* buffer for "block" */
6161 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006162 xfs_extnum_t i=0, j; /* index into the extents list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 xfs_ifork_t *ifp; /* fork structure */
6164 int level; /* btree level, for checking */
6165 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10006166 __be64 *pp; /* pointer to block address */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006167 xfs_bmbt_rec_t *ep; /* pointer to current extent */
6168 xfs_bmbt_rec_t *lastp; /* pointer to previous extent */
6169 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 int bp_release = 0;
6171
6172 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6173 return;
6174 }
6175
6176 bno = NULLFSBLOCK;
6177 mp = ip->i_mount;
6178 ifp = XFS_IFORK_PTR(ip, whichfork);
6179 block = ifp->if_broot;
6180 /*
6181 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6182 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11006183 level = be16_to_cpu(block->bb_level);
6184 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6186 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006187 bno = be64_to_cpu(*pp);
6188
6189 ASSERT(bno != NULLDFSBNO);
6190 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6191 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6192
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 /*
6194 * Go down the tree until leaf level is reached, following the first
6195 * pointer (leftmost) at each level.
6196 */
6197 while (level-- > 0) {
6198 /* See if buf is in cur first */
6199 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6200 if (bp) {
6201 bp_release = 0;
6202 } else {
6203 bp_release = 1;
6204 }
6205 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6206 XFS_BMAP_BTREE_REF)))
6207 goto error_norelse;
6208 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6209 XFS_WANT_CORRUPTED_GOTO(
6210 XFS_BMAP_SANITY_CHECK(mp, block, level),
6211 error0);
6212 if (level == 0)
6213 break;
6214
6215 /*
6216 * Check this block for basic sanity (increasing keys and
6217 * no duplicate blocks).
6218 */
6219
6220 xfs_check_block(block, mp, 0, 0);
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006221 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006222 bno = be64_to_cpu(*pp);
6223 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 if (bp_release) {
6225 bp_release = 0;
6226 xfs_trans_brelse(NULL, bp);
6227 }
6228 }
6229
6230 /*
6231 * Here with bp and block set to the leftmost leaf node in the tree.
6232 */
6233 i = 0;
6234
6235 /*
6236 * Loop over all leaf nodes checking that all extents are in the right order.
6237 */
6238 lastp = NULL;
6239 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 xfs_fsblock_t nextbno;
6241 xfs_extnum_t num_recs;
6242
6243
Christoph Hellwig16259e72005-11-02 15:11:25 +11006244 num_recs = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245
6246 /*
6247 * Read-ahead the next leaf block, if any.
6248 */
6249
Christoph Hellwig16259e72005-11-02 15:11:25 +11006250 nextbno = be64_to_cpu(block->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251
6252 /*
6253 * Check all the extents to make sure they are OK.
6254 * If we had a previous block, the last entry should
6255 * conform with the first entry in this one.
6256 */
6257
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006258 ep = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006259 for (j = 1; j < num_recs; j++) {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006260 nextp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, j + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 if (lastp) {
6262 xfs_btree_check_rec(XFS_BTNUM_BMAP,
6263 (void *)lastp, (void *)ep);
6264 }
6265 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006266 (void *)(nextp));
6267 lastp = ep;
6268 ep = nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270
6271 i += num_recs;
6272 if (bp_release) {
6273 bp_release = 0;
6274 xfs_trans_brelse(NULL, bp);
6275 }
6276 bno = nextbno;
6277 /*
6278 * If we've reached the end, stop.
6279 */
6280 if (bno == NULLFSBLOCK)
6281 break;
6282
6283 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6284 if (bp) {
6285 bp_release = 0;
6286 } else {
6287 bp_release = 1;
6288 }
6289 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6290 XFS_BMAP_BTREE_REF)))
6291 goto error_norelse;
6292 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6293 }
6294 if (bp_release) {
6295 bp_release = 0;
6296 xfs_trans_brelse(NULL, bp);
6297 }
6298 return;
6299
6300error0:
6301 cmn_err(CE_WARN, "%s: at error0", __FUNCTION__);
6302 if (bp_release)
6303 xfs_trans_brelse(NULL, bp);
6304error_norelse:
6305 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
Christoph Hellwigda1650a2005-11-02 10:21:35 +11006306 __FUNCTION__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__);
6308 return;
6309}
6310#endif
6311
6312/*
6313 * Count fsblocks of the given fork.
6314 */
6315int /* error */
6316xfs_bmap_count_blocks(
6317 xfs_trans_t *tp, /* transaction pointer */
6318 xfs_inode_t *ip, /* incore inode */
6319 int whichfork, /* data or attr fork */
6320 int *count) /* out: count of blocks */
6321{
6322 xfs_bmbt_block_t *block; /* current btree block */
6323 xfs_fsblock_t bno; /* block # of "block" */
6324 xfs_ifork_t *ifp; /* fork structure */
6325 int level; /* btree level, for checking */
6326 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10006327 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328
6329 bno = NULLFSBLOCK;
6330 mp = ip->i_mount;
6331 ifp = XFS_IFORK_PTR(ip, whichfork);
6332 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006333 if (unlikely(xfs_bmap_count_leaves(ifp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6335 count) < 0)) {
6336 XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)",
6337 XFS_ERRLEVEL_LOW, mp);
6338 return XFS_ERROR(EFSCORRUPTED);
6339 }
6340 return 0;
6341 }
6342
6343 /*
6344 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6345 */
6346 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11006347 level = be16_to_cpu(block->bb_level);
6348 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006350 bno = be64_to_cpu(*pp);
6351 ASSERT(bno != NULLDFSBNO);
6352 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6353 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006355 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6357 mp);
6358 return XFS_ERROR(EFSCORRUPTED);
6359 }
6360
6361 return 0;
6362}
6363
6364/*
6365 * Recursively walks each level of a btree
6366 * to count total fsblocks is use.
6367 */
6368int /* error */
6369xfs_bmap_count_tree(
6370 xfs_mount_t *mp, /* file system mount point */
6371 xfs_trans_t *tp, /* transaction pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006372 xfs_ifork_t *ifp, /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 xfs_fsblock_t blockno, /* file system block number */
6374 int levelin, /* level in btree */
6375 int *count) /* Count of blocks */
6376{
6377 int error;
6378 xfs_buf_t *bp, *nbp;
6379 int level = levelin;
Christoph Hellwig576039c2006-09-28 10:58:06 +10006380 __be64 *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 xfs_fsblock_t bno = blockno;
6382 xfs_fsblock_t nextbno;
6383 xfs_bmbt_block_t *block, *nextblock;
6384 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385
6386 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6387 return error;
6388 *count += 1;
6389 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6390
6391 if (--level) {
6392 /* Not at node above leafs, count this level of nodes */
Christoph Hellwig16259e72005-11-02 15:11:25 +11006393 nextbno = be64_to_cpu(block->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 while (nextbno != NULLFSBLOCK) {
6395 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6396 0, &nbp, XFS_BMAP_BTREE_REF)))
6397 return error;
6398 *count += 1;
6399 nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11006400 nextbno = be64_to_cpu(nextblock->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401 xfs_trans_brelse(tp, nbp);
6402 }
6403
6404 /* Dive to the next level */
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006405 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006406 bno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 if (unlikely((error =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006408 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 xfs_trans_brelse(tp, bp);
6410 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6411 XFS_ERRLEVEL_LOW, mp);
6412 return XFS_ERROR(EFSCORRUPTED);
6413 }
6414 xfs_trans_brelse(tp, bp);
6415 } else {
6416 /* count all level 1 nodes and their leaves */
6417 for (;;) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11006418 nextbno = be64_to_cpu(block->bb_rightsib);
6419 numrecs = be16_to_cpu(block->bb_numrecs);
Eric Sandeene9ed9d22007-05-08 13:48:56 +10006420 if (unlikely(xfs_bmap_disk_count_leaves(0,
6421 block, numrecs, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 xfs_trans_brelse(tp, bp);
6423 XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
6424 XFS_ERRLEVEL_LOW, mp);
6425 return XFS_ERROR(EFSCORRUPTED);
6426 }
6427 xfs_trans_brelse(tp, bp);
6428 if (nextbno == NULLFSBLOCK)
6429 break;
6430 bno = nextbno;
6431 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6432 XFS_BMAP_BTREE_REF)))
6433 return error;
6434 *count += 1;
6435 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6436 }
6437 }
6438 return 0;
6439}
6440
6441/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006442 * Count leaf blocks given a range of extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 */
6444int
6445xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006446 xfs_ifork_t *ifp,
6447 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 int numrecs,
6449 int *count)
6450{
6451 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006452 xfs_bmbt_rec_t *frp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006454 for (b = 0; b < numrecs; b++) {
6455 frp = xfs_iext_get_ext(ifp, idx + b);
Yingping Lu91e11082005-11-02 15:10:24 +11006456 *count += xfs_bmbt_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006457 }
Yingping Lu91e11082005-11-02 15:10:24 +11006458 return 0;
6459}
6460
6461/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006462 * Count leaf blocks given a range of extent records originally
6463 * in btree format.
Yingping Lu91e11082005-11-02 15:10:24 +11006464 */
6465int
6466xfs_bmap_disk_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006467 xfs_extnum_t idx,
6468 xfs_bmbt_block_t *block,
Yingping Lu91e11082005-11-02 15:10:24 +11006469 int numrecs,
6470 int *count)
6471{
6472 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006473 xfs_bmbt_rec_t *frp;
Yingping Lu91e11082005-11-02 15:10:24 +11006474
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006475 for (b = 1; b <= numrecs; b++) {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006476 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, idx + b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477 *count += xfs_bmbt_disk_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479 return 0;
6480}