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