blob: 451b4484795d0ce848364109a897521812b23651 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_dir2_sf.h"
33#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_itable.h"
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100038#include "xfs_dir2_data.h"
39#include "xfs_dir2_leaf.h"
40#include "xfs_dir2_block.h"
Nathan Scotta844f452005-11-02 14:38:42 +110041#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_extfree_item.h"
43#include "xfs_alloc.h"
44#include "xfs_bmap.h"
45#include "xfs_rtalloc.h"
46#include "xfs_error.h"
Nathan Scottd8cc8902005-11-02 10:34:53 +110047#include "xfs_attr_leaf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "xfs_rw.h"
49#include "xfs_quota.h"
50#include "xfs_trans_space.h"
51#include "xfs_buf_item.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100052#include "xfs_filestream.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100053#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000054#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
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 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100131 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int *logflagsp,/* inode logging flags */
133 int rsvd); /* OK to allocate reserved blocks */
134
135/*
136 * Called by xfs_bmap_add_extent to handle cases converting a hole
137 * to a real allocation.
138 */
139STATIC int /* error */
140xfs_bmap_add_extent_hole_real(
141 xfs_inode_t *ip, /* incore inode pointer */
142 xfs_extnum_t idx, /* extent number to update/insert */
143 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100144 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int *logflagsp, /* inode logging flags */
146 int whichfork); /* data or attr fork */
147
148/*
149 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
150 * allocation to a real allocation or vice versa.
151 */
152STATIC int /* error */
153xfs_bmap_add_extent_unwritten_real(
154 xfs_inode_t *ip, /* incore inode pointer */
155 xfs_extnum_t idx, /* extent number to update/insert */
156 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100157 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000158 int *logflagsp); /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160/*
161 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
162 * It figures out where to ask the underlying allocator to put the new extent.
163 */
164STATIC int /* error */
165xfs_bmap_alloc(
166 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
167
168/*
169 * Transform a btree format file with only one leaf node, where the
170 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100171 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * give up the space for the btree root and pitch the leaf block.
173 */
174STATIC int /* error */
175xfs_bmap_btree_to_extents(
176 xfs_trans_t *tp, /* transaction pointer */
177 xfs_inode_t *ip, /* incore inode pointer */
178 xfs_btree_cur_t *cur, /* btree cursor */
179 int *logflagsp, /* inode logging flags */
180 int whichfork); /* data or attr fork */
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100183 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * after removing space (or undoing a delayed allocation).
185 */
186STATIC int /* error */
187xfs_bmap_del_extent(
188 xfs_inode_t *ip, /* incore inode pointer */
189 xfs_trans_t *tp, /* current trans pointer */
190 xfs_extnum_t idx, /* extent number to update/insert */
191 xfs_bmap_free_t *flist, /* list of extents to be freed */
192 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100193 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int *logflagsp,/* inode logging flags */
195 int whichfork, /* data or attr fork */
196 int rsvd); /* OK to allocate reserved blocks */
197
198/*
199 * Remove the entry "free" from the free item list. Prev points to the
200 * previous entry, unless "free" is the head of the list.
201 */
202STATIC void
203xfs_bmap_del_free(
204 xfs_bmap_free_t *flist, /* free item list header */
205 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
206 xfs_bmap_free_item_t *free); /* list item to be freed */
207
208/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 * Convert an extents-format file into a btree-format file.
210 * The new file will have a root block (in the inode) and a single child block.
211 */
212STATIC int /* error */
213xfs_bmap_extents_to_btree(
214 xfs_trans_t *tp, /* transaction pointer */
215 xfs_inode_t *ip, /* incore inode pointer */
216 xfs_fsblock_t *firstblock, /* first-block-allocated */
217 xfs_bmap_free_t *flist, /* blocks freed in xaction */
218 xfs_btree_cur_t **curp, /* cursor returned to caller */
219 int wasdel, /* converting a delayed alloc */
220 int *logflagsp, /* inode logging flags */
221 int whichfork); /* data or attr fork */
222
223/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 * Convert a local file to an extents file.
225 * This code is sort of bogus, since the file data needs to get
226 * logged so it won't be lost. The bmap-level manipulations are ok, though.
227 */
228STATIC int /* error */
229xfs_bmap_local_to_extents(
230 xfs_trans_t *tp, /* transaction pointer */
231 xfs_inode_t *ip, /* incore inode pointer */
232 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
233 xfs_extlen_t total, /* total blocks needed by transaction */
234 int *logflagsp, /* inode logging flags */
235 int whichfork); /* data or attr fork */
236
237/*
238 * Search the extents list for the inode, for the extent containing bno.
239 * If bno lies in a hole, point to the next entry. If bno lies past eof,
240 * *eofp will be set, and *prevp will contain the last entry (null if none).
241 * Else, *lastxp will be set to the index of the found
242 * entry; *gotp will contain the entry.
243 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000244STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245xfs_bmap_search_extents(
246 xfs_inode_t *ip, /* incore inode pointer */
247 xfs_fileoff_t bno, /* block number searched for */
248 int whichfork, /* data or attr fork */
249 int *eofp, /* out: end of file found */
250 xfs_extnum_t *lastxp, /* out: last extent index */
251 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
252 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
253
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000254/*
255 * Check the last inode extent to determine whether this allocation will result
256 * in blocks being allocated at the end of the file. When we allocate new data
257 * blocks at the end of the file which do not start at the previous data block,
258 * we will try to align the new blocks at stripe unit boundaries.
259 */
260STATIC int /* error */
261xfs_bmap_isaeof(
262 xfs_inode_t *ip, /* incore inode pointer */
263 xfs_fileoff_t off, /* file offset in fsblocks */
264 int whichfork, /* data or attribute fork */
265 char *aeof); /* return value */
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/*
268 * Compute the worst-case number of indirect blocks that will be used
269 * for ip's delayed extent of length "len".
270 */
271STATIC xfs_filblks_t
272xfs_bmap_worst_indlen(
273 xfs_inode_t *ip, /* incore inode pointer */
274 xfs_filblks_t len); /* delayed extent length */
275
276#ifdef DEBUG
277/*
278 * Perform various validation checks on the values being returned
279 * from xfs_bmapi().
280 */
281STATIC void
282xfs_bmap_validate_ret(
283 xfs_fileoff_t bno,
284 xfs_filblks_t len,
285 int flags,
286 xfs_bmbt_irec_t *mval,
287 int nmap,
288 int ret_nmap);
289#else
290#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
291#endif /* DEBUG */
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293STATIC int
294xfs_bmap_count_tree(
295 xfs_mount_t *mp,
296 xfs_trans_t *tp,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100297 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 xfs_fsblock_t blockno,
299 int levelin,
300 int *count);
301
Ruben Porrasc94312d2008-08-13 16:52:25 +1000302STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100304 xfs_ifork_t *ifp,
305 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int numrecs,
307 int *count);
308
Ruben Porrasc94312d2008-08-13 16:52:25 +1000309STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +1100310xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +1100311 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100312 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +1100313 int numrecs,
314 int *count);
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/*
317 * Bmap internal routines.
318 */
319
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100320STATIC int /* error */
321xfs_bmbt_lookup_eq(
322 struct xfs_btree_cur *cur,
323 xfs_fileoff_t off,
324 xfs_fsblock_t bno,
325 xfs_filblks_t len,
326 int *stat) /* success/failure */
327{
328 cur->bc_rec.b.br_startoff = off;
329 cur->bc_rec.b.br_startblock = bno;
330 cur->bc_rec.b.br_blockcount = len;
331 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
332}
333
334STATIC int /* error */
335xfs_bmbt_lookup_ge(
336 struct xfs_btree_cur *cur,
337 xfs_fileoff_t off,
338 xfs_fsblock_t bno,
339 xfs_filblks_t len,
340 int *stat) /* success/failure */
341{
342 cur->bc_rec.b.br_startoff = off;
343 cur->bc_rec.b.br_startblock = bno;
344 cur->bc_rec.b.br_blockcount = len;
345 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
346}
347
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100348/*
349* Update the record referred to by cur to the value given
350 * by [off, bno, len, state].
351 * This either works (return 0) or gets an EFSCORRUPTED error.
352 */
353STATIC int
354xfs_bmbt_update(
355 struct xfs_btree_cur *cur,
356 xfs_fileoff_t off,
357 xfs_fsblock_t bno,
358 xfs_filblks_t len,
359 xfs_exntst_t state)
360{
361 union xfs_btree_rec rec;
362
363 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
364 return xfs_btree_update(cur, &rec);
365}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/*
368 * Called from xfs_bmap_add_attrfork to handle btree format files.
369 */
370STATIC int /* error */
371xfs_bmap_add_attrfork_btree(
372 xfs_trans_t *tp, /* transaction pointer */
373 xfs_inode_t *ip, /* incore inode pointer */
374 xfs_fsblock_t *firstblock, /* first block allocated */
375 xfs_bmap_free_t *flist, /* blocks to free at commit */
376 int *flags) /* inode logging flags */
377{
378 xfs_btree_cur_t *cur; /* btree cursor */
379 int error; /* error return value */
380 xfs_mount_t *mp; /* file system mount struct */
381 int stat; /* newroot status */
382
383 mp = ip->i_mount;
384 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
385 *flags |= XFS_ILOG_DBROOT;
386 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100387 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 cur->bc_private.b.flist = flist;
389 cur->bc_private.b.firstblock = *firstblock;
390 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
391 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000392 /* must be at least one entry */
393 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100394 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 goto error0;
396 if (stat == 0) {
397 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
398 return XFS_ERROR(ENOSPC);
399 }
400 *firstblock = cur->bc_private.b.firstblock;
401 cur->bc_private.b.allocated = 0;
402 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
403 }
404 return 0;
405error0:
406 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
407 return error;
408}
409
410/*
411 * Called from xfs_bmap_add_attrfork to handle extents format files.
412 */
413STATIC int /* error */
414xfs_bmap_add_attrfork_extents(
415 xfs_trans_t *tp, /* transaction pointer */
416 xfs_inode_t *ip, /* incore inode pointer */
417 xfs_fsblock_t *firstblock, /* first block allocated */
418 xfs_bmap_free_t *flist, /* blocks to free at commit */
419 int *flags) /* inode logging flags */
420{
421 xfs_btree_cur_t *cur; /* bmap btree cursor */
422 int error; /* error return value */
423
424 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
425 return 0;
426 cur = NULL;
427 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
428 flags, XFS_DATA_FORK);
429 if (cur) {
430 cur->bc_private.b.allocated = 0;
431 xfs_btree_del_cursor(cur,
432 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
433 }
434 return error;
435}
436
437/*
438 * Called from xfs_bmap_add_attrfork to handle local format files.
439 */
440STATIC int /* error */
441xfs_bmap_add_attrfork_local(
442 xfs_trans_t *tp, /* transaction pointer */
443 xfs_inode_t *ip, /* incore inode pointer */
444 xfs_fsblock_t *firstblock, /* first block allocated */
445 xfs_bmap_free_t *flist, /* blocks to free at commit */
446 int *flags) /* inode logging flags */
447{
448 xfs_da_args_t dargs; /* args for dir/attr code */
449 int error; /* error return value */
450 xfs_mount_t *mp; /* mount structure pointer */
451
452 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
453 return 0;
454 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
455 mp = ip->i_mount;
456 memset(&dargs, 0, sizeof(dargs));
457 dargs.dp = ip;
458 dargs.firstblock = firstblock;
459 dargs.flist = flist;
460 dargs.total = mp->m_dirblkfsbs;
461 dargs.whichfork = XFS_DATA_FORK;
462 dargs.trans = tp;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000463 error = xfs_dir2_sf_to_block(&dargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 } else
465 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
466 XFS_DATA_FORK);
467 return error;
468}
469
470/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100471 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 * after allocating space (or doing a delayed allocation).
473 */
474STATIC int /* error */
475xfs_bmap_add_extent(
476 xfs_inode_t *ip, /* incore inode pointer */
477 xfs_extnum_t idx, /* extent number to update/insert */
478 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100479 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 xfs_fsblock_t *first, /* pointer to firstblock variable */
481 xfs_bmap_free_t *flist, /* list of extents to be freed */
482 int *logflagsp, /* inode logging flags */
483 int whichfork, /* data or attr fork */
484 int rsvd) /* OK to use reserved data blocks */
485{
486 xfs_btree_cur_t *cur; /* btree cursor or null */
487 xfs_filblks_t da_new; /* new count del alloc blocks used */
488 xfs_filblks_t da_old; /* old count del alloc blocks used */
489 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 xfs_ifork_t *ifp; /* inode fork ptr */
491 int logflags; /* returned value */
492 xfs_extnum_t nextents; /* number of extents in file now */
493
494 XFS_STATS_INC(xs_add_exlist);
495 cur = *curp;
496 ifp = XFS_IFORK_PTR(ip, whichfork);
497 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
498 ASSERT(idx <= nextents);
499 da_old = da_new = 0;
500 error = 0;
501 /*
502 * This is the first extent added to a new/empty file.
503 * Special case this one, so other routines get to assume there are
504 * already extents in the list.
505 */
506 if (nextents == 0) {
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000507 xfs_iext_insert(ip, 0, 1, new,
508 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 ASSERT(cur == NULL);
511 ifp->if_lastex = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600512 if (!isnullstartblock(new->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600514 logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 } else
516 logflags = 0;
517 }
518 /*
519 * Any kind of new delayed allocation goes here.
520 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600521 else if (isnullstartblock(new->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (cur)
523 ASSERT((cur->bc_private.b.flags &
524 XFS_BTCUR_BPRV_WASDEL) == 0);
Eric Sandeene9ed9d22007-05-08 13:48:56 +1000525 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000526 &logflags, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 goto done;
528 }
529 /*
530 * Real allocation off the end of the file.
531 */
532 else if (idx == nextents) {
533 if (cur)
534 ASSERT((cur->bc_private.b.flags &
535 XFS_BTCUR_BPRV_WASDEL) == 0);
536 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000537 &logflags, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 goto done;
539 } else {
540 xfs_bmbt_irec_t prev; /* old extent at offset idx */
541
542 /*
543 * Get the record referred to by idx.
544 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100545 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /*
547 * If it's a real allocation record, and the new allocation ends
548 * after the start of the referred to record, then we're filling
549 * in a delayed or unwritten allocation with a real one, or
550 * converting real back to unwritten.
551 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600552 if (!isnullstartblock(new->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 new->br_startoff + new->br_blockcount > prev.br_startoff) {
554 if (prev.br_state != XFS_EXT_UNWRITTEN &&
Eric Sandeen9d87c312009-01-14 23:22:07 -0600555 isnullstartblock(prev.br_startblock)) {
556 da_old = startblockval(prev.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (cur)
558 ASSERT(cur->bc_private.b.flags &
559 XFS_BTCUR_BPRV_WASDEL);
560 if ((error = xfs_bmap_add_extent_delay_real(ip,
561 idx, &cur, new, &da_new, first, flist,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000562 &logflags, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 goto done;
564 } else if (new->br_state == XFS_EXT_NORM) {
565 ASSERT(new->br_state == XFS_EXT_NORM);
566 if ((error = xfs_bmap_add_extent_unwritten_real(
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000567 ip, idx, &cur, new, &logflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto done;
569 } else {
570 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
571 if ((error = xfs_bmap_add_extent_unwritten_real(
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000572 ip, idx, &cur, new, &logflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 goto done;
574 }
575 ASSERT(*curp == cur || *curp == NULL);
576 }
577 /*
578 * Otherwise we're filling in a hole with an allocation.
579 */
580 else {
581 if (cur)
582 ASSERT((cur->bc_private.b.flags &
583 XFS_BTCUR_BPRV_WASDEL) == 0);
584 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000585 new, &logflags, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto done;
587 }
588 }
589
590 ASSERT(*curp == cur || *curp == NULL);
591 /*
592 * Convert to a btree if necessary.
593 */
594 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
595 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
596 int tmp_logflags; /* partial log flag return val */
597
598 ASSERT(cur == NULL);
599 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
600 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
601 logflags |= tmp_logflags;
602 if (error)
603 goto done;
604 }
605 /*
606 * Adjust for changes in reserved delayed indirect blocks.
607 * Nothing to do for disk quotas here.
608 */
609 if (da_old || da_new) {
610 xfs_filblks_t nblks;
611
612 nblks = da_new;
613 if (cur)
614 nblks += cur->bc_private.b.allocated;
615 ASSERT(nblks <= da_old);
616 if (nblks < da_old)
Christoph Hellwig96540c72010-09-30 02:25:55 +0000617 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100618 (int64_t)(da_old - nblks), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620 /*
621 * Clear out the allocated field, done with it now in any case.
622 */
623 if (cur) {
624 cur->bc_private.b.allocated = 0;
625 *curp = cur;
626 }
627done:
628#ifdef DEBUG
629 if (!error)
630 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
631#endif
632 *logflagsp = logflags;
633 return error;
634}
635
636/*
637 * Called by xfs_bmap_add_extent to handle cases converting a delayed
638 * allocation to a real allocation.
639 */
640STATIC int /* error */
641xfs_bmap_add_extent_delay_real(
642 xfs_inode_t *ip, /* incore inode pointer */
643 xfs_extnum_t idx, /* extent number to update/insert */
644 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100645 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
647 xfs_fsblock_t *first, /* pointer to firstblock variable */
648 xfs_bmap_free_t *flist, /* list of extents to be freed */
649 int *logflagsp, /* inode logging flags */
650 int rsvd) /* OK to use reserved data block allocation */
651{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 xfs_btree_cur_t *cur; /* btree cursor */
653 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000654 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100657 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 xfs_fileoff_t new_endoff; /* end offset of new entry */
659 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
660 /* left is 0, right is 1, prev is 2 */
661 int rval=0; /* return value (logging flags) */
662 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000663 xfs_filblks_t temp=0; /* value for dnew calculations */
664 xfs_filblks_t temp2=0;/* value for dnew calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 int tmp_rval; /* partial logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667#define LEFT r[0]
668#define RIGHT r[1]
669#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 /*
672 * Set up a bunch of variables to make the tests simpler.
673 */
674 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100675 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
676 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 xfs_bmbt_get_all(ep, &PREV);
678 new_endoff = new->br_startoff + new->br_blockcount;
679 ASSERT(PREV.br_startoff <= new->br_startoff);
680 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /*
683 * Set flags determining what part of the previous delayed allocation
684 * extent is being replaced by a real allocation.
685 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000686 if (PREV.br_startoff == new->br_startoff)
687 state |= BMAP_LEFT_FILLING;
688 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
689 state |= BMAP_RIGHT_FILLING;
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /*
692 * Check and set flags if this segment has a left neighbor.
693 * Don't set contiguous if the combined extent would be too large.
694 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000695 if (idx > 0) {
696 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100697 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000698
699 if (isnullstartblock(LEFT.br_startblock))
700 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000702
703 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
704 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
705 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
706 LEFT.br_state == new->br_state &&
707 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
708 state |= BMAP_LEFT_CONTIG;
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /*
711 * Check and set flags if this segment has a right neighbor.
712 * Don't set contiguous if the combined extent would be too large.
713 * Also check for all-three-contiguous being too large.
714 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000715 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
716 state |= BMAP_RIGHT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100717 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000718
719 if (isnullstartblock(RIGHT.br_startblock))
720 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000722
723 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
724 new_endoff == RIGHT.br_startoff &&
725 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
726 new->br_state == RIGHT.br_state &&
727 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
728 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
729 BMAP_RIGHT_FILLING)) !=
730 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
731 BMAP_RIGHT_FILLING) ||
732 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
733 <= MAXEXTLEN))
734 state |= BMAP_RIGHT_CONTIG;
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 error = 0;
737 /*
738 * Switch out based on the FILLING and CONTIG state bits.
739 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000740 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
741 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
742 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
743 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /*
745 * Filling in all of a previously delayed allocation extent.
746 * The left and right neighbors are both contiguous with new.
747 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000748 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100749 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 LEFT.br_blockcount + PREV.br_blockcount +
751 RIGHT.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000752 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
753
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000754 xfs_iext_remove(ip, idx, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 ip->i_df.if_lastex = idx - 1;
756 ip->i_d.di_nextents--;
757 if (cur == NULL)
758 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
759 else {
760 rval = XFS_ILOG_CORE;
761 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
762 RIGHT.br_startblock,
763 RIGHT.br_blockcount, &i)))
764 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000765 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100766 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000768 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100769 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000771 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
773 LEFT.br_startblock,
774 LEFT.br_blockcount +
775 PREV.br_blockcount +
776 RIGHT.br_blockcount, LEFT.br_state)))
777 goto done;
778 }
779 *dnew = 0;
780 break;
781
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000782 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /*
784 * Filling in all of a previously delayed allocation extent.
785 * The left neighbor is contiguous, the right is not.
786 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000787 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100788 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000790 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 ip->i_df.if_lastex = idx - 1;
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000793 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (cur == NULL)
795 rval = XFS_ILOG_DEXT;
796 else {
797 rval = 0;
798 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
799 LEFT.br_startblock, LEFT.br_blockcount,
800 &i)))
801 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000802 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
804 LEFT.br_startblock,
805 LEFT.br_blockcount +
806 PREV.br_blockcount, LEFT.br_state)))
807 goto done;
808 }
809 *dnew = 0;
810 break;
811
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000812 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /*
814 * Filling in all of a previously delayed allocation extent.
815 * The right neighbor is contiguous, the left is not.
816 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000817 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 xfs_bmbt_set_startblock(ep, new->br_startblock);
819 xfs_bmbt_set_blockcount(ep,
820 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000821 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 ip->i_df.if_lastex = idx;
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000824 xfs_iext_remove(ip, idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (cur == NULL)
826 rval = XFS_ILOG_DEXT;
827 else {
828 rval = 0;
829 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
830 RIGHT.br_startblock,
831 RIGHT.br_blockcount, &i)))
832 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000833 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
835 new->br_startblock,
836 PREV.br_blockcount +
837 RIGHT.br_blockcount, PREV.br_state)))
838 goto done;
839 }
840 *dnew = 0;
841 break;
842
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000843 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /*
845 * Filling in all of a previously delayed allocation extent.
846 * Neither the left nor right neighbors are contiguous with
847 * the new one.
848 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000849 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000851 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 ip->i_df.if_lastex = idx;
854 ip->i_d.di_nextents++;
855 if (cur == NULL)
856 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
857 else {
858 rval = XFS_ILOG_CORE;
859 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
860 new->br_startblock, new->br_blockcount,
861 &i)))
862 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000863 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100865 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000867 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869 *dnew = 0;
870 break;
871
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000872 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 /*
874 * Filling in the first part of a previous delayed allocation.
875 * The left neighbor is contiguous.
876 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000877 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100878 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 LEFT.br_blockcount + new->br_blockcount);
880 xfs_bmbt_set_startoff(ep,
881 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000882 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000885 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 xfs_bmbt_set_blockcount(ep, temp);
887 ip->i_df.if_lastex = idx - 1;
888 if (cur == NULL)
889 rval = XFS_ILOG_DEXT;
890 else {
891 rval = 0;
892 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
893 LEFT.br_startblock, LEFT.br_blockcount,
894 &i)))
895 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000896 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
898 LEFT.br_startblock,
899 LEFT.br_blockcount +
900 new->br_blockcount,
901 LEFT.br_state)))
902 goto done;
903 }
904 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600905 startblockval(PREV.br_startblock));
906 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000907 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 *dnew = temp;
909 break;
910
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000911 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 /*
913 * Filling in the first part of a previous delayed allocation.
914 * The left neighbor is not contiguous.
915 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000916 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 xfs_bmbt_set_startoff(ep, new_endoff);
918 temp = PREV.br_blockcount - new->br_blockcount;
919 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000920 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 ip->i_df.if_lastex = idx;
922 ip->i_d.di_nextents++;
923 if (cur == NULL)
924 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
925 else {
926 rval = XFS_ILOG_CORE;
927 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
928 new->br_startblock, new->br_blockcount,
929 &i)))
930 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000931 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100933 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000935 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
938 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
939 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
940 first, flist, &cur, 1, &tmp_rval,
941 XFS_DATA_FORK);
942 rval |= tmp_rval;
943 if (error)
944 goto done;
945 }
946 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600947 startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100949 ep = xfs_iext_get_ext(ifp, idx + 1);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600950 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000951 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 *dnew = temp;
953 break;
954
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000955 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /*
957 * Filling in the last part of a previous delayed allocation.
958 * The right neighbor is contiguous with the new allocation.
959 */
960 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000961 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
962 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 xfs_bmbt_set_blockcount(ep, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100964 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
965 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 new->br_blockcount + RIGHT.br_blockcount,
967 RIGHT.br_state);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000968 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 ip->i_df.if_lastex = idx + 1;
970 if (cur == NULL)
971 rval = XFS_ILOG_DEXT;
972 else {
973 rval = 0;
974 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
975 RIGHT.br_startblock,
976 RIGHT.br_blockcount, &i)))
977 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000978 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if ((error = xfs_bmbt_update(cur, new->br_startoff,
980 new->br_startblock,
981 new->br_blockcount +
982 RIGHT.br_blockcount,
983 RIGHT.br_state)))
984 goto done;
985 }
986 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600987 startblockval(PREV.br_startblock));
988 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000989 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 *dnew = temp;
991 break;
992
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000993 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /*
995 * Filling in the last part of a previous delayed allocation.
996 * The right neighbor is not contiguous.
997 */
998 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000999 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001001 xfs_iext_insert(ip, idx + 1, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 ip->i_df.if_lastex = idx + 1;
1003 ip->i_d.di_nextents++;
1004 if (cur == NULL)
1005 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1006 else {
1007 rval = XFS_ILOG_CORE;
1008 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1009 new->br_startblock, new->br_blockcount,
1010 &i)))
1011 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001012 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001014 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001016 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1019 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1020 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1021 first, flist, &cur, 1, &tmp_rval,
1022 XFS_DATA_FORK);
1023 rval |= tmp_rval;
1024 if (error)
1025 goto done;
1026 }
1027 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001028 startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001030 ep = xfs_iext_get_ext(ifp, idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001031 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001032 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 *dnew = temp;
1034 break;
1035
1036 case 0:
1037 /*
1038 * Filling in the middle part of a previous delayed allocation.
1039 * Contiguity is impossible here.
1040 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001041 *
1042 * We start with a delayed allocation:
1043 *
1044 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1045 * PREV @ idx
1046 *
1047 * and we are allocating:
1048 * +rrrrrrrrrrrrrrrrr+
1049 * new
1050 *
1051 * and we set it up for insertion as:
1052 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1053 * new
1054 * PREV @ idx LEFT RIGHT
1055 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 */
1057 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001059 trace_xfs_bmap_pre_update(ip, idx, 0, _THIS_IP_);
1060 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
1061 LEFT = *new;
1062 RIGHT.br_state = PREV.br_state;
1063 RIGHT.br_startblock = nullstartblock(
1064 (int)xfs_bmap_worst_indlen(ip, temp2));
1065 RIGHT.br_startoff = new_endoff;
1066 RIGHT.br_blockcount = temp2;
1067 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
1068 xfs_iext_insert(ip, idx + 1, 2, &LEFT, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 ip->i_df.if_lastex = idx + 1;
1070 ip->i_d.di_nextents++;
1071 if (cur == NULL)
1072 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1073 else {
1074 rval = XFS_ILOG_CORE;
1075 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1076 new->br_startblock, new->br_blockcount,
1077 &i)))
1078 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001079 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001081 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001083 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1086 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1087 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1088 first, flist, &cur, 1, &tmp_rval,
1089 XFS_DATA_FORK);
1090 rval |= tmp_rval;
1091 if (error)
1092 goto done;
1093 }
1094 temp = xfs_bmap_worst_indlen(ip, temp);
1095 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001096 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 (cur ? cur->bc_private.b.allocated : 0));
1098 if (diff > 0 &&
Christoph Hellwig96540c72010-09-30 02:25:55 +00001099 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
1100 -((int64_t)diff), rsvd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /*
1102 * Ick gross gag me with a spoon.
1103 */
1104 ASSERT(0); /* want to see if this ever happens! */
1105 while (diff > 0) {
1106 if (temp) {
1107 temp--;
1108 diff--;
1109 if (!diff ||
Christoph Hellwig96540c72010-09-30 02:25:55 +00001110 !xfs_icsb_modify_counters(ip->i_mount,
1111 XFS_SBS_FDBLOCKS,
1112 -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 break;
1114 }
1115 if (temp2) {
1116 temp2--;
1117 diff--;
1118 if (!diff ||
Christoph Hellwig96540c72010-09-30 02:25:55 +00001119 !xfs_icsb_modify_counters(ip->i_mount,
1120 XFS_SBS_FDBLOCKS,
1121 -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 break;
1123 }
1124 }
1125 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001126 ep = xfs_iext_get_ext(ifp, idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001127 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001128 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1129 trace_xfs_bmap_pre_update(ip, idx + 2, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001130 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001131 nullstartblock((int)temp2));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001132 trace_xfs_bmap_post_update(ip, idx + 2, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 *dnew = temp + temp2;
1134 break;
1135
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001136 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1137 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1138 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1139 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1140 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1141 case BMAP_LEFT_CONTIG:
1142 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 /*
1144 * These cases are all impossible.
1145 */
1146 ASSERT(0);
1147 }
1148 *curp = cur;
1149done:
1150 *logflagsp = rval;
1151 return error;
1152#undef LEFT
1153#undef RIGHT
1154#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
1156
1157/*
1158 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1159 * allocation to a real allocation or vice versa.
1160 */
1161STATIC int /* error */
1162xfs_bmap_add_extent_unwritten_real(
1163 xfs_inode_t *ip, /* incore inode pointer */
1164 xfs_extnum_t idx, /* extent number to update/insert */
1165 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001166 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001167 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001170 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001173 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 xfs_fileoff_t new_endoff; /* end offset of new entry */
1175 xfs_exntst_t newext; /* new extent state */
1176 xfs_exntst_t oldext; /* old extent state */
1177 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1178 /* left is 0, right is 1, prev is 2 */
1179 int rval=0; /* return value (logging flags) */
1180 int state = 0;/* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182#define LEFT r[0]
1183#define RIGHT r[1]
1184#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /*
1186 * Set up a bunch of variables to make the tests simpler.
1187 */
1188 error = 0;
1189 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001190 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1191 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 xfs_bmbt_get_all(ep, &PREV);
1193 newext = new->br_state;
1194 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1195 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1196 ASSERT(PREV.br_state == oldext);
1197 new_endoff = new->br_startoff + new->br_blockcount;
1198 ASSERT(PREV.br_startoff <= new->br_startoff);
1199 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 /*
1202 * Set flags determining what part of the previous oldext allocation
1203 * extent is being replaced by a newext allocation.
1204 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001205 if (PREV.br_startoff == new->br_startoff)
1206 state |= BMAP_LEFT_FILLING;
1207 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1208 state |= BMAP_RIGHT_FILLING;
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 /*
1211 * Check and set flags if this segment has a left neighbor.
1212 * Don't set contiguous if the combined extent would be too large.
1213 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001214 if (idx > 0) {
1215 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001216 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001217
1218 if (isnullstartblock(LEFT.br_startblock))
1219 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001221
1222 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1223 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1224 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1225 LEFT.br_state == newext &&
1226 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1227 state |= BMAP_LEFT_CONTIG;
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 /*
1230 * Check and set flags if this segment has a right neighbor.
1231 * Don't set contiguous if the combined extent would be too large.
1232 * Also check for all-three-contiguous being too large.
1233 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001234 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1235 state |= BMAP_RIGHT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001236 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001237 if (isnullstartblock(RIGHT.br_startblock))
1238 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001240
1241 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1242 new_endoff == RIGHT.br_startoff &&
1243 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1244 newext == RIGHT.br_state &&
1245 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1246 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1247 BMAP_RIGHT_FILLING)) !=
1248 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1249 BMAP_RIGHT_FILLING) ||
1250 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1251 <= MAXEXTLEN))
1252 state |= BMAP_RIGHT_CONTIG;
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /*
1255 * Switch out based on the FILLING and CONTIG state bits.
1256 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001257 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1258 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1259 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1260 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 /*
1262 * Setting all of a previous oldext extent to newext.
1263 * The left and right neighbors are both contiguous with new.
1264 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001265 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001266 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 LEFT.br_blockcount + PREV.br_blockcount +
1268 RIGHT.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001269 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1270
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001271 xfs_iext_remove(ip, idx, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 ip->i_df.if_lastex = idx - 1;
1273 ip->i_d.di_nextents -= 2;
1274 if (cur == NULL)
1275 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1276 else {
1277 rval = XFS_ILOG_CORE;
1278 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1279 RIGHT.br_startblock,
1280 RIGHT.br_blockcount, &i)))
1281 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001282 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11001283 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001285 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001286 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001288 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11001289 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001291 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001292 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001294 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1296 LEFT.br_startblock,
1297 LEFT.br_blockcount + PREV.br_blockcount +
1298 RIGHT.br_blockcount, LEFT.br_state)))
1299 goto done;
1300 }
1301 break;
1302
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001303 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /*
1305 * Setting all of a previous oldext extent to newext.
1306 * The left neighbor is contiguous, the right is not.
1307 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001308 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001309 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001311 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 ip->i_df.if_lastex = idx - 1;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001314 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 ip->i_d.di_nextents--;
1316 if (cur == NULL)
1317 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1318 else {
1319 rval = XFS_ILOG_CORE;
1320 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1321 PREV.br_startblock, PREV.br_blockcount,
1322 &i)))
1323 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001324 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11001325 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001327 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001328 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001330 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1332 LEFT.br_startblock,
1333 LEFT.br_blockcount + PREV.br_blockcount,
1334 LEFT.br_state)))
1335 goto done;
1336 }
1337 break;
1338
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001339 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 /*
1341 * Setting all of a previous oldext extent to newext.
1342 * The right neighbor is contiguous, the left is not.
1343 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001344 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 xfs_bmbt_set_blockcount(ep,
1346 PREV.br_blockcount + RIGHT.br_blockcount);
1347 xfs_bmbt_set_state(ep, newext);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001348 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 ip->i_df.if_lastex = idx;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001350 xfs_iext_remove(ip, idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 ip->i_d.di_nextents--;
1352 if (cur == NULL)
1353 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1354 else {
1355 rval = XFS_ILOG_CORE;
1356 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1357 RIGHT.br_startblock,
1358 RIGHT.br_blockcount, &i)))
1359 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001360 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11001361 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001363 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001364 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001366 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1368 new->br_startblock,
1369 new->br_blockcount + RIGHT.br_blockcount,
1370 newext)))
1371 goto done;
1372 }
1373 break;
1374
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001375 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 /*
1377 * Setting all of a previous oldext extent to newext.
1378 * Neither the left nor right neighbors are contiguous with
1379 * the new one.
1380 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001381 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 xfs_bmbt_set_state(ep, newext);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001383 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 ip->i_df.if_lastex = idx;
1386 if (cur == NULL)
1387 rval = XFS_ILOG_DEXT;
1388 else {
1389 rval = 0;
1390 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1391 new->br_startblock, new->br_blockcount,
1392 &i)))
1393 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001394 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1396 new->br_startblock, new->br_blockcount,
1397 newext)))
1398 goto done;
1399 }
1400 break;
1401
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001402 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 /*
1404 * Setting the first part of a previous oldext extent to newext.
1405 * The left neighbor is contiguous.
1406 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001407 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001408 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 LEFT.br_blockcount + new->br_blockcount);
1410 xfs_bmbt_set_startoff(ep,
1411 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001412 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1413
1414 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 xfs_bmbt_set_startblock(ep,
1416 new->br_startblock + new->br_blockcount);
1417 xfs_bmbt_set_blockcount(ep,
1418 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001419 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 ip->i_df.if_lastex = idx - 1;
1422 if (cur == NULL)
1423 rval = XFS_ILOG_DEXT;
1424 else {
1425 rval = 0;
1426 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1427 PREV.br_startblock, PREV.br_blockcount,
1428 &i)))
1429 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001430 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if ((error = xfs_bmbt_update(cur,
1432 PREV.br_startoff + new->br_blockcount,
1433 PREV.br_startblock + new->br_blockcount,
1434 PREV.br_blockcount - new->br_blockcount,
1435 oldext)))
1436 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001437 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 goto done;
1439 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1440 LEFT.br_startblock,
1441 LEFT.br_blockcount + new->br_blockcount,
1442 LEFT.br_state))
1443 goto done;
1444 }
1445 break;
1446
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001447 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 /*
1449 * Setting the first part of a previous oldext extent to newext.
1450 * The left neighbor is not contiguous.
1451 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001452 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1454 xfs_bmbt_set_startoff(ep, new_endoff);
1455 xfs_bmbt_set_blockcount(ep,
1456 PREV.br_blockcount - new->br_blockcount);
1457 xfs_bmbt_set_startblock(ep,
1458 new->br_startblock + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001459 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1460
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001461 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 ip->i_df.if_lastex = idx;
1463 ip->i_d.di_nextents++;
1464 if (cur == NULL)
1465 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1466 else {
1467 rval = XFS_ILOG_CORE;
1468 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1469 PREV.br_startblock, PREV.br_blockcount,
1470 &i)))
1471 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001472 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if ((error = xfs_bmbt_update(cur,
1474 PREV.br_startoff + new->br_blockcount,
1475 PREV.br_startblock + new->br_blockcount,
1476 PREV.br_blockcount - new->br_blockcount,
1477 oldext)))
1478 goto done;
1479 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001480 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001482 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484 break;
1485
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001486 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 /*
1488 * Setting the last part of a previous oldext extent to newext.
1489 * The right neighbor is contiguous with the new allocation.
1490 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001491 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1492 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 xfs_bmbt_set_blockcount(ep,
1494 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001495 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001496 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1497 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001499 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 ip->i_df.if_lastex = idx + 1;
1502 if (cur == NULL)
1503 rval = XFS_ILOG_DEXT;
1504 else {
1505 rval = 0;
1506 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1507 PREV.br_startblock,
1508 PREV.br_blockcount, &i)))
1509 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001510 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1512 PREV.br_startblock,
1513 PREV.br_blockcount - new->br_blockcount,
1514 oldext)))
1515 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001516 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 goto done;
1518 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1519 new->br_startblock,
1520 new->br_blockcount + RIGHT.br_blockcount,
1521 newext)))
1522 goto done;
1523 }
1524 break;
1525
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001526 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 /*
1528 * Setting the last part of a previous oldext extent to newext.
1529 * The right neighbor is not contiguous.
1530 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001531 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 xfs_bmbt_set_blockcount(ep,
1533 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001534 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1535
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001536 xfs_iext_insert(ip, idx + 1, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 ip->i_df.if_lastex = idx + 1;
1538 ip->i_d.di_nextents++;
1539 if (cur == NULL)
1540 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1541 else {
1542 rval = XFS_ILOG_CORE;
1543 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1544 PREV.br_startblock, PREV.br_blockcount,
1545 &i)))
1546 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001547 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1549 PREV.br_startblock,
1550 PREV.br_blockcount - new->br_blockcount,
1551 oldext)))
1552 goto done;
1553 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1554 new->br_startblock, new->br_blockcount,
1555 &i)))
1556 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001557 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001559 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001561 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
1563 break;
1564
1565 case 0:
1566 /*
1567 * Setting the middle part of a previous oldext extent to
1568 * newext. Contiguity is impossible here.
1569 * One extent becomes three extents.
1570 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001571 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 xfs_bmbt_set_blockcount(ep,
1573 new->br_startoff - PREV.br_startoff);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001574 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 r[0] = *new;
1577 r[1].br_startoff = new_endoff;
1578 r[1].br_blockcount =
1579 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1580 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1581 r[1].br_state = oldext;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001582 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 ip->i_df.if_lastex = idx + 1;
1584 ip->i_d.di_nextents += 2;
1585 if (cur == NULL)
1586 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1587 else {
1588 rval = XFS_ILOG_CORE;
1589 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1590 PREV.br_startblock, PREV.br_blockcount,
1591 &i)))
1592 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001593 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 /* new right extent - oldext */
1595 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1596 r[1].br_startblock, r[1].br_blockcount,
1597 r[1].br_state)))
1598 goto done;
1599 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10001601 cur->bc_rec.b.br_blockcount =
1602 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001603 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001605 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001606 /*
1607 * Reset the cursor to the position of the new extent
1608 * we are about to insert as we can't trust it after
1609 * the previous insert.
1610 */
1611 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1612 new->br_startblock, new->br_blockcount,
1613 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001615 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001617 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001618 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001620 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
1622 break;
1623
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001624 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1625 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1626 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1627 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1628 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1629 case BMAP_LEFT_CONTIG:
1630 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 /*
1632 * These cases are all impossible.
1633 */
1634 ASSERT(0);
1635 }
1636 *curp = cur;
1637done:
1638 *logflagsp = rval;
1639 return error;
1640#undef LEFT
1641#undef RIGHT
1642#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
1644
1645/*
1646 * Called by xfs_bmap_add_extent to handle cases converting a hole
1647 * to a delayed allocation.
1648 */
1649/*ARGSUSED*/
1650STATIC int /* error */
1651xfs_bmap_add_extent_hole_delay(
1652 xfs_inode_t *ip, /* incore inode pointer */
1653 xfs_extnum_t idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001654 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 int *logflagsp, /* inode logging flags */
1656 int rsvd) /* OK to allocate reserved blocks */
1657{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001658 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001659 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1661 xfs_filblks_t newlen=0; /* new indirect size */
1662 xfs_filblks_t oldlen=0; /* old indirect size */
1663 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1664 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001665 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001667 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1668 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001670 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 /*
1673 * Check and set flags if this segment has a left neighbor
1674 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001675 if (idx > 0) {
1676 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001677 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001678
1679 if (isnullstartblock(left.br_startblock))
1680 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /*
1684 * Check and set flags if the current (right) segment exists.
1685 * If it doesn't exist, we're converting the hole at end-of-file.
1686 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001687 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1688 state |= BMAP_RIGHT_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 xfs_bmbt_get_all(ep, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001690
1691 if (isnullstartblock(right.br_startblock))
1692 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /*
1696 * Set contiguity flags on the left and right neighbors.
1697 * Don't let extents get too large, even if the pieces are contiguous.
1698 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001699 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1700 left.br_startoff + left.br_blockcount == new->br_startoff &&
1701 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1702 state |= BMAP_LEFT_CONTIG;
1703
1704 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1705 new->br_startoff + new->br_blockcount == right.br_startoff &&
1706 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1707 (!(state & BMAP_LEFT_CONTIG) ||
1708 (left.br_blockcount + new->br_blockcount +
1709 right.br_blockcount <= MAXEXTLEN)))
1710 state |= BMAP_RIGHT_CONTIG;
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 /*
1713 * Switch out based on the contiguity flags.
1714 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001715 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1716 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 /*
1718 * New allocation is contiguous with delayed allocations
1719 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001720 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 */
1722 temp = left.br_blockcount + new->br_blockcount +
1723 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001724
1725 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001726 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001727 oldlen = startblockval(left.br_startblock) +
1728 startblockval(new->br_startblock) +
1729 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001731 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001732 nullstartblock((int)newlen));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001733 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1734
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001735 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 ip->i_df.if_lastex = idx - 1;
1737 break;
1738
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001739 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 /*
1741 * New allocation is contiguous with a delayed allocation
1742 * on the left.
1743 * Merge the new allocation with the left neighbor.
1744 */
1745 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001746 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001747 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001748 oldlen = startblockval(left.br_startblock) +
1749 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001751 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001752 nullstartblock((int)newlen));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001753 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 ip->i_df.if_lastex = idx - 1;
1756 break;
1757
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001758 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 /*
1760 * New allocation is contiguous with a delayed allocation
1761 * on the right.
1762 * Merge the new allocation with the right neighbor.
1763 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001764 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001766 oldlen = startblockval(new->br_startblock) +
1767 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 newlen = xfs_bmap_worst_indlen(ip, temp);
1769 xfs_bmbt_set_allf(ep, new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06001770 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001771 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 ip->i_df.if_lastex = idx;
1774 break;
1775
1776 case 0:
1777 /*
1778 * New allocation is not contiguous with another
1779 * delayed allocation.
1780 * Insert a new entry.
1781 */
1782 oldlen = newlen = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001783 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 ip->i_df.if_lastex = idx;
1785 break;
1786 }
1787 if (oldlen != newlen) {
1788 ASSERT(oldlen > newlen);
Christoph Hellwig96540c72010-09-30 02:25:55 +00001789 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11001790 (int64_t)(oldlen - newlen), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 /*
1792 * Nothing to do for disk quota accounting here.
1793 */
1794 }
1795 *logflagsp = 0;
1796 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
1799/*
1800 * Called by xfs_bmap_add_extent to handle cases converting a hole
1801 * to a real allocation.
1802 */
1803STATIC int /* error */
1804xfs_bmap_add_extent_hole_real(
1805 xfs_inode_t *ip, /* incore inode pointer */
1806 xfs_extnum_t idx, /* extent number to update/insert */
1807 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001808 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 int *logflagsp, /* inode logging flags */
1810 int whichfork) /* data or attr fork */
1811{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001812 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 int i; /* temp state */
1815 xfs_ifork_t *ifp; /* inode fork pointer */
1816 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1817 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001818 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 ifp = XFS_IFORK_PTR(ip, whichfork);
1822 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001823 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 state = 0;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001825
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001826 if (whichfork == XFS_ATTR_FORK)
1827 state |= BMAP_ATTRFORK;
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 /*
1830 * Check and set flags if this segment has a left neighbor.
1831 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001832 if (idx > 0) {
1833 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001834 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001835 if (isnullstartblock(left.br_startblock))
1836 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 /*
1840 * Check and set flags if this segment has a current value.
1841 * Not true if we're inserting into the "hole" at eof.
1842 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001843 if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1844 state |= BMAP_RIGHT_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 xfs_bmbt_get_all(ep, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001846 if (isnullstartblock(right.br_startblock))
1847 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 /*
1851 * We're inserting a real allocation between "left" and "right".
1852 * Set the contiguity flags. Don't let extents get too large.
1853 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001854 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1855 left.br_startoff + left.br_blockcount == new->br_startoff &&
1856 left.br_startblock + left.br_blockcount == new->br_startblock &&
1857 left.br_state == new->br_state &&
1858 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1859 state |= BMAP_LEFT_CONTIG;
1860
1861 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1862 new->br_startoff + new->br_blockcount == right.br_startoff &&
1863 new->br_startblock + new->br_blockcount == right.br_startblock &&
1864 new->br_state == right.br_state &&
1865 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1866 (!(state & BMAP_LEFT_CONTIG) ||
1867 left.br_blockcount + new->br_blockcount +
1868 right.br_blockcount <= MAXEXTLEN))
1869 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001871 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /*
1873 * Select which case we're in here, and implement it.
1874 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001875 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1876 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 /*
1878 * New allocation is contiguous with real allocations on the
1879 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001880 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001882 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001883 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 left.br_blockcount + new->br_blockcount +
1885 right.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001886 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1887
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001888 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 ifp->if_lastex = idx - 1;
1890 XFS_IFORK_NEXT_SET(ip, whichfork,
1891 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1892 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001893 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001894 } else {
1895 rval = XFS_ILOG_CORE;
1896 if ((error = xfs_bmbt_lookup_eq(cur,
1897 right.br_startoff,
1898 right.br_startblock,
1899 right.br_blockcount, &i)))
1900 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001901 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11001902 if ((error = xfs_btree_delete(cur, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001903 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001904 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001905 if ((error = xfs_btree_decrement(cur, 0, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001906 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001907 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001908 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1909 left.br_startblock,
1910 left.br_blockcount +
1911 new->br_blockcount +
1912 right.br_blockcount,
1913 left.br_state)))
1914 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001916 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001918 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 /*
1920 * New allocation is contiguous with a real allocation
1921 * on the left.
1922 * Merge the new allocation with the left neighbor.
1923 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001924 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001925 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 left.br_blockcount + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001927 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 ifp->if_lastex = idx - 1;
1930 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001931 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001932 } else {
1933 rval = 0;
1934 if ((error = xfs_bmbt_lookup_eq(cur,
1935 left.br_startoff,
1936 left.br_startblock,
1937 left.br_blockcount, &i)))
1938 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001939 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001940 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1941 left.br_startblock,
1942 left.br_blockcount +
1943 new->br_blockcount,
1944 left.br_state)))
1945 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001947 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001949 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 /*
1951 * New allocation is contiguous with a real allocation
1952 * on the right.
1953 * Merge the new allocation with the right neighbor.
1954 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001955 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
1957 new->br_blockcount + right.br_blockcount,
1958 right.br_state);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001959 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 ifp->if_lastex = idx;
1962 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001963 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001964 } else {
1965 rval = 0;
1966 if ((error = xfs_bmbt_lookup_eq(cur,
1967 right.br_startoff,
1968 right.br_startblock,
1969 right.br_blockcount, &i)))
1970 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001971 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001972 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1973 new->br_startblock,
1974 new->br_blockcount +
1975 right.br_blockcount,
1976 right.br_state)))
1977 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001979 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 case 0:
1982 /*
1983 * New allocation is not contiguous with another
1984 * real allocation.
1985 * Insert a new entry.
1986 */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001987 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 ifp->if_lastex = idx;
1989 XFS_IFORK_NEXT_SET(ip, whichfork,
1990 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
1991 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001992 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001993 } else {
1994 rval = XFS_ILOG_CORE;
1995 if ((error = xfs_bmbt_lookup_eq(cur,
1996 new->br_startoff,
1997 new->br_startblock,
1998 new->br_blockcount, &i)))
1999 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002000 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002001 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002002 if ((error = xfs_btree_insert(cur, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002003 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002004 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002006 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002008done:
2009 *logflagsp = rval;
2010 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
Nathan Scottdd9f4382006-01-11 15:28:28 +11002013/*
2014 * Adjust the size of the new extent based on di_extsize and rt extsize.
2015 */
2016STATIC int
2017xfs_bmap_extsize_align(
2018 xfs_mount_t *mp,
2019 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2020 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2021 xfs_extlen_t extsz, /* align to this extent size */
2022 int rt, /* is this a realtime inode? */
2023 int eof, /* is extent at end-of-file? */
2024 int delay, /* creating delalloc extent? */
2025 int convert, /* overwriting unwritten extent? */
2026 xfs_fileoff_t *offp, /* in/out: aligned offset */
2027 xfs_extlen_t *lenp) /* in/out: aligned length */
2028{
2029 xfs_fileoff_t orig_off; /* original offset */
2030 xfs_extlen_t orig_alen; /* original length */
2031 xfs_fileoff_t orig_end; /* original off+len */
2032 xfs_fileoff_t nexto; /* next file offset */
2033 xfs_fileoff_t prevo; /* previous file offset */
2034 xfs_fileoff_t align_off; /* temp for offset */
2035 xfs_extlen_t align_alen; /* temp for length */
2036 xfs_extlen_t temp; /* temp for calculations */
2037
2038 if (convert)
2039 return 0;
2040
2041 orig_off = align_off = *offp;
2042 orig_alen = align_alen = *lenp;
2043 orig_end = orig_off + orig_alen;
2044
2045 /*
2046 * If this request overlaps an existing extent, then don't
2047 * attempt to perform any additional alignment.
2048 */
2049 if (!delay && !eof &&
2050 (orig_off >= gotp->br_startoff) &&
2051 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2052 return 0;
2053 }
2054
2055 /*
2056 * If the file offset is unaligned vs. the extent size
2057 * we need to align it. This will be possible unless
2058 * the file was previously written with a kernel that didn't
2059 * perform this alignment, or if a truncate shot us in the
2060 * foot.
2061 */
2062 temp = do_mod(orig_off, extsz);
2063 if (temp) {
2064 align_alen += temp;
2065 align_off -= temp;
2066 }
2067 /*
2068 * Same adjustment for the end of the requested area.
2069 */
2070 if ((temp = (align_alen % extsz))) {
2071 align_alen += extsz - temp;
2072 }
2073 /*
2074 * If the previous block overlaps with this proposed allocation
2075 * then move the start forward without adjusting the length.
2076 */
2077 if (prevp->br_startoff != NULLFILEOFF) {
2078 if (prevp->br_startblock == HOLESTARTBLOCK)
2079 prevo = prevp->br_startoff;
2080 else
2081 prevo = prevp->br_startoff + prevp->br_blockcount;
2082 } else
2083 prevo = 0;
2084 if (align_off != orig_off && align_off < prevo)
2085 align_off = prevo;
2086 /*
2087 * If the next block overlaps with this proposed allocation
2088 * then move the start back without adjusting the length,
2089 * but not before offset 0.
2090 * This may of course make the start overlap previous block,
2091 * and if we hit the offset 0 limit then the next block
2092 * can still overlap too.
2093 */
2094 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2095 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2096 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2097 nexto = gotp->br_startoff + gotp->br_blockcount;
2098 else
2099 nexto = gotp->br_startoff;
2100 } else
2101 nexto = NULLFILEOFF;
2102 if (!eof &&
2103 align_off + align_alen != orig_end &&
2104 align_off + align_alen > nexto)
2105 align_off = nexto > align_alen ? nexto - align_alen : 0;
2106 /*
2107 * If we're now overlapping the next or previous extent that
2108 * means we can't fit an extsz piece in this hole. Just move
2109 * the start forward to the first valid spot and set
2110 * the length so we hit the end.
2111 */
2112 if (align_off != orig_off && align_off < prevo)
2113 align_off = prevo;
2114 if (align_off + align_alen != orig_end &&
2115 align_off + align_alen > nexto &&
2116 nexto != NULLFILEOFF) {
2117 ASSERT(nexto > prevo);
2118 align_alen = nexto - align_off;
2119 }
2120
2121 /*
2122 * If realtime, and the result isn't a multiple of the realtime
2123 * extent size we need to remove blocks until it is.
2124 */
2125 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2126 /*
2127 * We're not covering the original request, or
2128 * we won't be able to once we fix the length.
2129 */
2130 if (orig_off < align_off ||
2131 orig_end > align_off + align_alen ||
2132 align_alen - temp < orig_alen)
2133 return XFS_ERROR(EINVAL);
2134 /*
2135 * Try to fix it by moving the start up.
2136 */
2137 if (align_off + temp <= orig_off) {
2138 align_alen -= temp;
2139 align_off += temp;
2140 }
2141 /*
2142 * Try to fix it by moving the end in.
2143 */
2144 else if (align_off + align_alen - temp >= orig_end)
2145 align_alen -= temp;
2146 /*
2147 * Set the start to the minimum then trim the length.
2148 */
2149 else {
2150 align_alen -= orig_off - align_off;
2151 align_off = orig_off;
2152 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2153 }
2154 /*
2155 * Result doesn't cover the request, fail it.
2156 */
2157 if (orig_off < align_off || orig_end > align_off + align_alen)
2158 return XFS_ERROR(EINVAL);
2159 } else {
2160 ASSERT(orig_off >= align_off);
2161 ASSERT(orig_end <= align_off + align_alen);
2162 }
2163
2164#ifdef DEBUG
2165 if (!eof && gotp->br_startoff != NULLFILEOFF)
2166 ASSERT(align_off + align_alen <= gotp->br_startoff);
2167 if (prevp->br_startoff != NULLFILEOFF)
2168 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2169#endif
2170
2171 *lenp = align_alen;
2172 *offp = align_off;
2173 return 0;
2174}
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176#define XFS_ALLOC_GAP_UNITS 4
2177
David Chinnerc2b1cba2008-04-10 12:21:40 +10002178STATIC void
Nathan Scotta365bdd2006-03-14 13:34:16 +11002179xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2181{
2182 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2184 xfs_mount_t *mp; /* mount point structure */
2185 int nullfb; /* true if ap->firstblock isn't set */
2186 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188#define ISVALID(x,y) \
2189 (rt ? \
2190 (x) < mp->m_sb.sb_rblocks : \
2191 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2192 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2193 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 mp = ap->ip->i_mount;
2196 nullfb = ap->firstblock == NULLFSBLOCK;
2197 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2198 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 /*
2200 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002201 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 */
2203 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002204 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2206 ap->prevp->br_startblock)) {
2207 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2208 /*
2209 * Adjust for the gap between prevp and us.
2210 */
2211 adjust = ap->off -
2212 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2213 if (adjust &&
2214 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2215 ap->rval += adjust;
2216 }
2217 /*
2218 * If not at eof, then compare the two neighbor blocks.
2219 * Figure out whether either one gives us a good starting point,
2220 * and pick the better one.
2221 */
2222 else if (!ap->eof) {
2223 xfs_fsblock_t gotbno; /* right side block number */
2224 xfs_fsblock_t gotdiff=0; /* right side difference */
2225 xfs_fsblock_t prevbno; /* left side block number */
2226 xfs_fsblock_t prevdiff=0; /* left side difference */
2227
2228 /*
2229 * If there's a previous (left) block, select a requested
2230 * start block based on it.
2231 */
2232 if (ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002233 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 (prevbno = ap->prevp->br_startblock +
2235 ap->prevp->br_blockcount) &&
2236 ISVALID(prevbno, ap->prevp->br_startblock)) {
2237 /*
2238 * Calculate gap to end of previous block.
2239 */
2240 adjust = prevdiff = ap->off -
2241 (ap->prevp->br_startoff +
2242 ap->prevp->br_blockcount);
2243 /*
2244 * Figure the startblock based on the previous block's
2245 * end and the gap size.
2246 * Heuristic!
2247 * If the gap is large relative to the piece we're
2248 * allocating, or using it gives us an invalid block
2249 * number, then just use the end of the previous block.
2250 */
2251 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2252 ISVALID(prevbno + prevdiff,
2253 ap->prevp->br_startblock))
2254 prevbno += adjust;
2255 else
2256 prevdiff += adjust;
2257 /*
2258 * If the firstblock forbids it, can't use it,
2259 * must use default.
2260 */
2261 if (!rt && !nullfb &&
2262 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2263 prevbno = NULLFSBLOCK;
2264 }
2265 /*
2266 * No previous block or can't follow it, just default.
2267 */
2268 else
2269 prevbno = NULLFSBLOCK;
2270 /*
2271 * If there's a following (right) block, select a requested
2272 * start block based on it.
2273 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002274 if (!isnullstartblock(ap->gotp->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 /*
2276 * Calculate gap to start of next block.
2277 */
2278 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2279 /*
2280 * Figure the startblock based on the next block's
2281 * start and the gap size.
2282 */
2283 gotbno = ap->gotp->br_startblock;
2284 /*
2285 * Heuristic!
2286 * If the gap is large relative to the piece we're
2287 * allocating, or using it gives us an invalid block
2288 * number, then just use the start of the next block
2289 * offset by our length.
2290 */
2291 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2292 ISVALID(gotbno - gotdiff, gotbno))
2293 gotbno -= adjust;
2294 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2295 gotbno -= ap->alen;
2296 gotdiff += adjust - ap->alen;
2297 } else
2298 gotdiff += adjust;
2299 /*
2300 * If the firstblock forbids it, can't use it,
2301 * must use default.
2302 */
2303 if (!rt && !nullfb &&
2304 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2305 gotbno = NULLFSBLOCK;
2306 }
2307 /*
2308 * No next block, just default.
2309 */
2310 else
2311 gotbno = NULLFSBLOCK;
2312 /*
2313 * If both valid, pick the better one, else the only good
2314 * one, else ap->rval is already set (to 0 or the inode block).
2315 */
2316 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2317 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2318 else if (prevbno != NULLFSBLOCK)
2319 ap->rval = prevbno;
2320 else if (gotbno != NULLFSBLOCK)
2321 ap->rval = gotbno;
2322 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002323#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11002324}
2325
2326STATIC int
2327xfs_bmap_rtalloc(
2328 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2329{
2330 xfs_alloctype_t atype = 0; /* type for allocation routines */
2331 int error; /* error return value */
2332 xfs_mount_t *mp; /* mount point structure */
2333 xfs_extlen_t prod = 0; /* product factor for allocators */
2334 xfs_extlen_t ralen = 0; /* realtime allocation length */
2335 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002336 xfs_rtblock_t rtb;
2337
2338 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002339 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002340 prod = align / mp->m_sb.sb_rextsize;
2341 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2342 align, 1, ap->eof, 0,
2343 ap->conv, &ap->off, &ap->alen);
2344 if (error)
2345 return error;
2346 ASSERT(ap->alen);
2347 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2348
2349 /*
2350 * If the offset & length are not perfectly aligned
2351 * then kill prod, it will just get us in trouble.
2352 */
2353 if (do_mod(ap->off, align) || ap->alen % align)
2354 prod = 1;
2355 /*
2356 * Set ralen to be the actual requested length in rtextents.
2357 */
2358 ralen = ap->alen / mp->m_sb.sb_rextsize;
2359 /*
2360 * If the old value was close enough to MAXEXTLEN that
2361 * we rounded up to it, cut it back so it's valid again.
2362 * Note that if it's a really large request (bigger than
2363 * MAXEXTLEN), we don't hear about that number, and can't
2364 * adjust the starting point to match it.
2365 */
2366 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2367 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
Christoph Hellwig04e99452011-01-25 09:06:19 +00002368
2369 /*
2370 * Lock out other modifications to the RT bitmap inode.
2371 */
Christoph Hellwig1050c712011-02-13 13:25:31 +00002372 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
2373 xfs_trans_ijoin_ref(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Christoph Hellwig04e99452011-01-25 09:06:19 +00002374
Nathan Scotta365bdd2006-03-14 13:34:16 +11002375 /*
2376 * If it's an allocation to an empty file at offset 0,
2377 * pick an extent that will space things out in the rt area.
2378 */
2379 if (ap->eof && ap->off == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10002380 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2381
Nathan Scotta365bdd2006-03-14 13:34:16 +11002382 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2383 if (error)
2384 return error;
2385 ap->rval = rtx * mp->m_sb.sb_rextsize;
2386 } else {
2387 ap->rval = 0;
2388 }
2389
2390 xfs_bmap_adjacent(ap);
2391
2392 /*
2393 * Realtime allocation, done through xfs_rtallocate_extent.
2394 */
2395 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2396 do_div(ap->rval, mp->m_sb.sb_rextsize);
2397 rtb = ap->rval;
2398 ap->alen = ralen;
2399 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2400 &ralen, atype, ap->wasdel, prod, &rtb)))
2401 return error;
2402 if (rtb == NULLFSBLOCK && prod > 1 &&
2403 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2404 ap->alen, &ralen, atype,
2405 ap->wasdel, 1, &rtb)))
2406 return error;
2407 ap->rval = rtb;
2408 if (ap->rval != NULLFSBLOCK) {
2409 ap->rval *= mp->m_sb.sb_rextsize;
2410 ralen *= mp->m_sb.sb_rextsize;
2411 ap->alen = ralen;
2412 ap->ip->i_d.di_nblocks += ralen;
2413 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2414 if (ap->wasdel)
2415 ap->ip->i_delayed_blks -= ralen;
2416 /*
2417 * Adjust the disk quota also. This was reserved
2418 * earlier.
2419 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002420 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002421 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2422 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2423 } else {
2424 ap->alen = 0;
2425 }
2426 return 0;
2427}
2428
2429STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00002430xfs_bmap_btalloc_nullfb(
2431 struct xfs_bmalloca *ap,
2432 struct xfs_alloc_arg *args,
2433 xfs_extlen_t *blen)
2434{
2435 struct xfs_mount *mp = ap->ip->i_mount;
2436 struct xfs_perag *pag;
2437 xfs_agnumber_t ag, startag;
2438 int notinit = 0;
2439 int error;
2440
2441 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2442 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2443 else
2444 args->type = XFS_ALLOCTYPE_START_BNO;
2445 args->total = ap->total;
2446
2447 /*
2448 * Search for an allocation group with a single extent large enough
2449 * for the request. If one isn't found, then adjust the minimum
2450 * allocation size to the largest space found.
2451 */
2452 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2453 if (startag == NULLAGNUMBER)
2454 startag = ag = 0;
2455
2456 pag = xfs_perag_get(mp, ag);
Dave Chinner14b064c2011-01-27 12:16:28 +11002457 while (*blen < args->maxlen) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00002458 if (!pag->pagf_init) {
2459 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2460 XFS_ALLOC_FLAG_TRYLOCK);
2461 if (error) {
2462 xfs_perag_put(pag);
2463 return error;
2464 }
2465 }
2466
2467 /*
2468 * See xfs_alloc_fix_freelist...
2469 */
2470 if (pag->pagf_init) {
2471 xfs_extlen_t longest;
2472 longest = xfs_alloc_longest_free_extent(mp, pag);
2473 if (*blen < longest)
2474 *blen = longest;
2475 } else
2476 notinit = 1;
2477
2478 if (xfs_inode_is_filestream(ap->ip)) {
Dave Chinner14b064c2011-01-27 12:16:28 +11002479 if (*blen >= args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00002480 break;
2481
2482 if (ap->userdata) {
2483 /*
2484 * If startag is an invalid AG, we've
2485 * come here once before and
2486 * xfs_filestream_new_ag picked the
2487 * best currently available.
2488 *
2489 * Don't continue looping, since we
2490 * could loop forever.
2491 */
2492 if (startag == NULLAGNUMBER)
2493 break;
2494
2495 error = xfs_filestream_new_ag(ap, &ag);
2496 xfs_perag_put(pag);
2497 if (error)
2498 return error;
2499
2500 /* loop again to set 'blen'*/
2501 startag = NULLAGNUMBER;
2502 pag = xfs_perag_get(mp, ag);
2503 continue;
2504 }
2505 }
2506 if (++ag == mp->m_sb.sb_agcount)
2507 ag = 0;
2508 if (ag == startag)
2509 break;
2510 xfs_perag_put(pag);
2511 pag = xfs_perag_get(mp, ag);
2512 }
2513 xfs_perag_put(pag);
2514
2515 /*
2516 * Since the above loop did a BUF_TRYLOCK, it is
2517 * possible that there is space for this request.
2518 */
2519 if (notinit || *blen < ap->minlen)
2520 args->minlen = ap->minlen;
2521 /*
2522 * If the best seen length is less than the request
2523 * length, use the best as the minimum.
2524 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002525 else if (*blen < args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00002526 args->minlen = *blen;
2527 /*
Dave Chinner14b064c2011-01-27 12:16:28 +11002528 * Otherwise we've seen an extent as big as maxlen,
Christoph Hellwigc467c042010-02-15 23:34:42 +00002529 * use that as the minimum.
2530 */
2531 else
Dave Chinner14b064c2011-01-27 12:16:28 +11002532 args->minlen = args->maxlen;
Christoph Hellwigc467c042010-02-15 23:34:42 +00002533
2534 /*
2535 * set the failure fallback case to look in the selected
2536 * AG as the stream may have moved.
2537 */
2538 if (xfs_inode_is_filestream(ap->ip))
2539 ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2540
2541 return 0;
2542}
2543
2544STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11002545xfs_bmap_btalloc(
2546 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2547{
2548 xfs_mount_t *mp; /* mount point structure */
2549 xfs_alloctype_t atype = 0; /* type for allocation routines */
2550 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002551 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00002552 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002553 xfs_alloc_arg_t args;
2554 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002555 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002556 int nullfb; /* true if ap->firstblock isn't set */
2557 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002558 int tryagain;
2559 int error;
2560
2561 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002562 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002563 if (unlikely(align)) {
2564 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2565 align, 0, ap->eof, 0, ap->conv,
2566 &ap->off, &ap->alen);
2567 ASSERT(!error);
2568 ASSERT(ap->alen);
2569 }
2570 nullfb = ap->firstblock == NULLFSBLOCK;
2571 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10002572 if (nullfb) {
2573 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2574 ag = xfs_filestream_lookup_ag(ap->ip);
2575 ag = (ag != NULLAGNUMBER) ? ag : 0;
2576 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2577 } else {
2578 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2579 }
2580 } else
Nathan Scotta365bdd2006-03-14 13:34:16 +11002581 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;
Dave Chinner14b064c2011-01-27 12:16:28 +11002600
2601 /* Trim the allocation back to the maximum an AG can fit. */
2602 args.maxlen = MIN(ap->alen, XFS_ALLOC_AG_MAX_USABLE(mp));
Yingping Lud210a282006-06-09 14:55:18 +10002603 args.firstblock = ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002604 blen = 0;
2605 if (nullfb) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00002606 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2607 if (error)
2608 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002609 } else if (ap->low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002610 if (xfs_inode_is_filestream(ap->ip))
2611 args.type = XFS_ALLOCTYPE_FIRST_AG;
2612 else
2613 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002614 args.total = args.minlen = ap->minlen;
2615 } else {
2616 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2617 args.total = ap->total;
2618 args.minlen = ap->minlen;
2619 }
David Chinner957d0eb2007-06-18 16:50:37 +10002620 /* apply extent size hints if obtained earlier */
2621 if (unlikely(align)) {
2622 args.prod = align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002623 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2624 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11002625 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11002626 args.prod = 1;
2627 args.mod = 0;
2628 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002629 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002630 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2631 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2632 }
2633 /*
2634 * If we are not low on available data blocks, and the
2635 * underlying logical volume manager is a stripe, and
2636 * the file offset is zero then try to allocate data
2637 * blocks on stripe unit boundary.
2638 * NOTE: ap->aeof is only set if the allocation length
2639 * is >= the stripe unit and the allocation offset is
2640 * at the end of file.
2641 */
2642 if (!ap->low && ap->aeof) {
2643 if (!ap->off) {
2644 args.alignment = mp->m_dalign;
2645 atype = args.type;
2646 isaligned = 1;
2647 /*
2648 * Adjust for alignment
2649 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002650 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002651 args.minlen = blen - args.alignment;
2652 args.minalignslop = 0;
2653 } else {
2654 /*
2655 * First try an exact bno allocation.
2656 * If it fails then do a near or start bno
2657 * allocation with alignment turned on.
2658 */
2659 atype = args.type;
2660 tryagain = 1;
2661 args.type = XFS_ALLOCTYPE_THIS_BNO;
2662 args.alignment = 1;
2663 /*
2664 * Compute the minlen+alignment for the
2665 * next case. Set slop so that the value
2666 * of minlen+alignment+slop doesn't go up
2667 * between the calls.
2668 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002669 if (blen > mp->m_dalign && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002670 nextminlen = blen - mp->m_dalign;
2671 else
2672 nextminlen = args.minlen;
2673 if (nextminlen + mp->m_dalign > args.minlen + 1)
2674 args.minalignslop =
2675 nextminlen + mp->m_dalign -
2676 args.minlen - 1;
2677 else
2678 args.minalignslop = 0;
2679 }
2680 } else {
2681 args.alignment = 1;
2682 args.minalignslop = 0;
2683 }
2684 args.minleft = ap->minleft;
2685 args.wasdel = ap->wasdel;
2686 args.isfl = 0;
2687 args.userdata = ap->userdata;
2688 if ((error = xfs_alloc_vextent(&args)))
2689 return error;
2690 if (tryagain && args.fsbno == NULLFSBLOCK) {
2691 /*
2692 * Exact allocation failed. Now try with alignment
2693 * turned on.
2694 */
2695 args.type = atype;
2696 args.fsbno = ap->rval;
2697 args.alignment = mp->m_dalign;
2698 args.minlen = nextminlen;
2699 args.minalignslop = 0;
2700 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 if ((error = xfs_alloc_vextent(&args)))
2702 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002703 }
2704 if (isaligned && args.fsbno == NULLFSBLOCK) {
2705 /*
2706 * allocation failed, so turn off alignment and
2707 * try again.
2708 */
2709 args.type = atype;
2710 args.fsbno = ap->rval;
2711 args.alignment = 0;
2712 if ((error = xfs_alloc_vextent(&args)))
2713 return error;
2714 }
2715 if (args.fsbno == NULLFSBLOCK && nullfb &&
2716 args.minlen > ap->minlen) {
2717 args.minlen = ap->minlen;
2718 args.type = XFS_ALLOCTYPE_START_BNO;
2719 args.fsbno = ap->rval;
2720 if ((error = xfs_alloc_vextent(&args)))
2721 return error;
2722 }
2723 if (args.fsbno == NULLFSBLOCK && nullfb) {
2724 args.fsbno = 0;
2725 args.type = XFS_ALLOCTYPE_FIRST_AG;
2726 args.total = ap->minlen;
2727 args.minleft = 0;
2728 if ((error = xfs_alloc_vextent(&args)))
2729 return error;
2730 ap->low = 1;
2731 }
2732 if (args.fsbno != NULLFSBLOCK) {
2733 ap->firstblock = ap->rval = args.fsbno;
2734 ASSERT(nullfb || fb_agno == args.agno ||
2735 (ap->low && fb_agno < args.agno));
2736 ap->alen = args.len;
2737 ap->ip->i_d.di_nblocks += args.len;
2738 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2739 if (ap->wasdel)
2740 ap->ip->i_delayed_blks -= args.len;
2741 /*
2742 * Adjust the disk quota also. This was reserved
2743 * earlier.
2744 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002745 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002746 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2747 XFS_TRANS_DQ_BCOUNT,
2748 (long) args.len);
2749 } else {
2750 ap->rval = NULLFSBLOCK;
2751 ap->alen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
2753 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002754}
2755
2756/*
2757 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2758 * It figures out where to ask the underlying allocator to put the new extent.
2759 */
2760STATIC int
2761xfs_bmap_alloc(
2762 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2763{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002764 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002765 return xfs_bmap_rtalloc(ap);
2766 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767}
2768
2769/*
2770 * Transform a btree format file with only one leaf node, where the
2771 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002772 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 * give up the space for the btree root and pitch the leaf block.
2774 */
2775STATIC int /* error */
2776xfs_bmap_btree_to_extents(
2777 xfs_trans_t *tp, /* transaction pointer */
2778 xfs_inode_t *ip, /* incore inode pointer */
2779 xfs_btree_cur_t *cur, /* btree cursor */
2780 int *logflagsp, /* inode logging flags */
2781 int whichfork) /* data or attr fork */
2782{
2783 /* REFERENCED */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002784 struct xfs_btree_block *cblock;/* child btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 xfs_fsblock_t cbno; /* child block number */
2786 xfs_buf_t *cbp; /* child block's buffer */
2787 int error; /* error return value */
2788 xfs_ifork_t *ifp; /* inode fork data */
2789 xfs_mount_t *mp; /* mount point structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10002790 __be64 *pp; /* ptr to block address */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002791 struct xfs_btree_block *rblock;/* root btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Christoph Hellwig60197e82008-10-30 17:11:19 +11002793 mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 ifp = XFS_IFORK_PTR(ip, whichfork);
2795 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2796 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2797 rblock = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11002798 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2799 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
Christoph Hellwig60197e82008-10-30 17:11:19 +11002800 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2801 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10002802 cbno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 *logflagsp = 0;
2804#ifdef DEBUG
Christoph Hellwig576039c2006-09-28 10:58:06 +10002805 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 return error;
2807#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2809 XFS_BMAP_BTREE_REF)))
2810 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002811 cblock = XFS_BUF_TO_BLOCK(cbp);
2812 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return error;
2814 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2815 ip->i_d.di_nblocks--;
Christoph Hellwig7d095252009-06-08 15:33:32 +02002816 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 xfs_trans_binval(tp, cbp);
2818 if (cur->bc_bufs[0] == cbp)
2819 cur->bc_bufs[0] = NULL;
2820 xfs_iroot_realloc(ip, -1, whichfork);
2821 ASSERT(ifp->if_broot == NULL);
2822 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2823 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002824 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return 0;
2826}
2827
2828/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002829 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 * after removing space (or undoing a delayed allocation).
2831 */
2832STATIC int /* error */
2833xfs_bmap_del_extent(
2834 xfs_inode_t *ip, /* incore inode pointer */
2835 xfs_trans_t *tp, /* current transaction pointer */
2836 xfs_extnum_t idx, /* extent number to update/delete */
2837 xfs_bmap_free_t *flist, /* list of extents to be freed */
2838 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002839 xfs_bmbt_irec_t *del, /* data to remove from extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 int *logflagsp, /* inode logging flags */
2841 int whichfork, /* data or attr fork */
2842 int rsvd) /* OK to allocate reserved blocks */
2843{
2844 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2845 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2846 xfs_fsblock_t del_endblock=0; /* first block past del */
2847 xfs_fileoff_t del_endoff; /* first offset past del */
2848 int delay; /* current block is delayed allocated */
2849 int do_fx; /* free extent at end of routine */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002850 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 int error; /* error return value */
2852 int flags; /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 xfs_bmbt_irec_t got; /* current extent entry */
2854 xfs_fileoff_t got_endoff; /* first offset past got */
2855 int i; /* temp state */
2856 xfs_ifork_t *ifp; /* inode fork pointer */
2857 xfs_mount_t *mp; /* mount structure */
2858 xfs_filblks_t nblks; /* quota/sb block count */
2859 xfs_bmbt_irec_t new; /* new record to be inserted */
2860 /* REFERENCED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 uint qfield; /* quota field to update */
2862 xfs_filblks_t temp; /* for indirect length calculations */
2863 xfs_filblks_t temp2; /* for indirect length calculations */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002864 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 XFS_STATS_INC(xs_del_exlist);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002867
2868 if (whichfork == XFS_ATTR_FORK)
2869 state |= BMAP_ATTRFORK;
2870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 mp = ip->i_mount;
2872 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002873 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
2874 (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 ASSERT(del->br_blockcount > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002876 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 xfs_bmbt_get_all(ep, &got);
2878 ASSERT(got.br_startoff <= del->br_startoff);
2879 del_endoff = del->br_startoff + del->br_blockcount;
2880 got_endoff = got.br_startoff + got.br_blockcount;
2881 ASSERT(got_endoff >= del_endoff);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002882 delay = isnullstartblock(got.br_startblock);
2883 ASSERT(isnullstartblock(del->br_startblock) == delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 flags = 0;
2885 qfield = 0;
2886 error = 0;
2887 /*
2888 * If deleting a real allocation, must free up the disk space.
2889 */
2890 if (!delay) {
2891 flags = XFS_ILOG_CORE;
2892 /*
2893 * Realtime allocation. Free it and record di_nblocks update.
2894 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002895 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 xfs_fsblock_t bno;
2897 xfs_filblks_t len;
2898
2899 ASSERT(do_mod(del->br_blockcount,
2900 mp->m_sb.sb_rextsize) == 0);
2901 ASSERT(do_mod(del->br_startblock,
2902 mp->m_sb.sb_rextsize) == 0);
2903 bno = del->br_startblock;
2904 len = del->br_blockcount;
2905 do_div(bno, mp->m_sb.sb_rextsize);
2906 do_div(len, mp->m_sb.sb_rextsize);
2907 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
2908 (xfs_extlen_t)len)))
2909 goto done;
2910 do_fx = 0;
2911 nblks = len * mp->m_sb.sb_rextsize;
2912 qfield = XFS_TRANS_DQ_RTBCOUNT;
2913 }
2914 /*
2915 * Ordinary allocation.
2916 */
2917 else {
2918 do_fx = 1;
2919 nblks = del->br_blockcount;
2920 qfield = XFS_TRANS_DQ_BCOUNT;
2921 }
2922 /*
2923 * Set up del_endblock and cur for later.
2924 */
2925 del_endblock = del->br_startblock + del->br_blockcount;
2926 if (cur) {
2927 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2928 got.br_startblock, got.br_blockcount,
2929 &i)))
2930 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002931 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 }
2933 da_old = da_new = 0;
2934 } else {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002935 da_old = startblockval(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 da_new = 0;
2937 nblks = 0;
2938 do_fx = 0;
2939 }
2940 /*
2941 * Set flag value to use in switch statement.
2942 * Left-contig is 2, right-contig is 1.
2943 */
2944 switch (((got.br_startoff == del->br_startoff) << 1) |
2945 (got_endoff == del_endoff)) {
2946 case 3:
2947 /*
2948 * Matches the whole extent. Delete the entry.
2949 */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002950 xfs_iext_remove(ip, idx, 1,
2951 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 ifp->if_lastex = idx;
2953 if (delay)
2954 break;
2955 XFS_IFORK_NEXT_SET(ip, whichfork,
2956 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2957 flags |= XFS_ILOG_CORE;
2958 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002959 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 break;
2961 }
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11002962 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002964 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 break;
2966
2967 case 2:
2968 /*
2969 * Deleting the first part of the extent.
2970 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002971 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 xfs_bmbt_set_startoff(ep, del_endoff);
2973 temp = got.br_blockcount - del->br_blockcount;
2974 xfs_bmbt_set_blockcount(ep, temp);
2975 ifp->if_lastex = idx;
2976 if (delay) {
2977 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2978 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002979 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002980 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 da_new = temp;
2982 break;
2983 }
2984 xfs_bmbt_set_startblock(ep, del_endblock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002985 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002987 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 break;
2989 }
2990 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2991 got.br_blockcount - del->br_blockcount,
2992 got.br_state)))
2993 goto done;
2994 break;
2995
2996 case 1:
2997 /*
2998 * Deleting the last part of the extent.
2999 */
3000 temp = got.br_blockcount - del->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003001 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 xfs_bmbt_set_blockcount(ep, temp);
3003 ifp->if_lastex = idx;
3004 if (delay) {
3005 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3006 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003007 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003008 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 da_new = temp;
3010 break;
3011 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003012 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003014 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 break;
3016 }
3017 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3018 got.br_startblock,
3019 got.br_blockcount - del->br_blockcount,
3020 got.br_state)))
3021 goto done;
3022 break;
3023
3024 case 0:
3025 /*
3026 * Deleting the middle of the extent.
3027 */
3028 temp = del->br_startoff - got.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003029 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 xfs_bmbt_set_blockcount(ep, temp);
3031 new.br_startoff = del_endoff;
3032 temp2 = got_endoff - del_endoff;
3033 new.br_blockcount = temp2;
3034 new.br_state = got.br_state;
3035 if (!delay) {
3036 new.br_startblock = del_endblock;
3037 flags |= XFS_ILOG_CORE;
3038 if (cur) {
3039 if ((error = xfs_bmbt_update(cur,
3040 got.br_startoff,
3041 got.br_startblock, temp,
3042 got.br_state)))
3043 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11003044 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 goto done;
3046 cur->bc_rec.b = new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003047 error = xfs_btree_insert(cur, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 if (error && error != ENOSPC)
3049 goto done;
3050 /*
3051 * If get no-space back from btree insert,
3052 * it tried a split, and we have a zero
3053 * block reservation.
3054 * Fix up our state and return the error.
3055 */
3056 if (error == ENOSPC) {
3057 /*
3058 * Reset the cursor, don't trust
3059 * it after any insert operation.
3060 */
3061 if ((error = xfs_bmbt_lookup_eq(cur,
3062 got.br_startoff,
3063 got.br_startblock,
3064 temp, &i)))
3065 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003066 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 /*
3068 * Update the btree record back
3069 * to the original value.
3070 */
3071 if ((error = xfs_bmbt_update(cur,
3072 got.br_startoff,
3073 got.br_startblock,
3074 got.br_blockcount,
3075 got.br_state)))
3076 goto done;
3077 /*
3078 * Reset the extent record back
3079 * to the original value.
3080 */
3081 xfs_bmbt_set_blockcount(ep,
3082 got.br_blockcount);
3083 flags = 0;
3084 error = XFS_ERROR(ENOSPC);
3085 goto done;
3086 }
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003087 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 } else
Eric Sandeen9d87c312009-01-14 23:22:07 -06003089 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 XFS_IFORK_NEXT_SET(ip, whichfork,
3091 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3092 } else {
3093 ASSERT(whichfork == XFS_DATA_FORK);
3094 temp = xfs_bmap_worst_indlen(ip, temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003095 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003097 new.br_startblock = nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 da_new = temp + temp2;
3099 while (da_new > da_old) {
3100 if (temp) {
3101 temp--;
3102 da_new--;
3103 xfs_bmbt_set_startblock(ep,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003104 nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
3106 if (da_new == da_old)
3107 break;
3108 if (temp2) {
3109 temp2--;
3110 da_new--;
3111 new.br_startblock =
Eric Sandeen9d87c312009-01-14 23:22:07 -06003112 nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 }
3114 }
3115 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003116 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3117 xfs_iext_insert(ip, idx + 1, 1, &new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 ifp->if_lastex = idx + 1;
3119 break;
3120 }
3121 /*
3122 * If we need to, add to list of extents to delete.
3123 */
3124 if (do_fx)
3125 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3126 mp);
3127 /*
3128 * Adjust inode # blocks in the file.
3129 */
3130 if (nblks)
3131 ip->i_d.di_nblocks -= nblks;
3132 /*
3133 * Adjust quota data.
3134 */
3135 if (qfield)
Christoph Hellwig7d095252009-06-08 15:33:32 +02003136 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
3138 /*
3139 * Account for change in delayed indirect blocks.
3140 * Nothing to do for disk quota accounting here.
3141 */
3142 ASSERT(da_old >= da_new);
Christoph Hellwig96540c72010-09-30 02:25:55 +00003143 if (da_old > da_new) {
3144 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
3145 (int64_t)(da_old - da_new), rsvd);
3146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147done:
3148 *logflagsp = flags;
3149 return error;
3150}
3151
3152/*
3153 * Remove the entry "free" from the free item list. Prev points to the
3154 * previous entry, unless "free" is the head of the list.
3155 */
3156STATIC void
3157xfs_bmap_del_free(
3158 xfs_bmap_free_t *flist, /* free item list header */
3159 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3160 xfs_bmap_free_item_t *free) /* list item to be freed */
3161{
3162 if (prev)
3163 prev->xbfi_next = free->xbfi_next;
3164 else
3165 flist->xbf_first = free->xbfi_next;
3166 flist->xbf_count--;
3167 kmem_zone_free(xfs_bmap_free_item_zone, free);
3168}
3169
3170/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 * Convert an extents-format file into a btree-format file.
3172 * The new file will have a root block (in the inode) and a single child block.
3173 */
3174STATIC int /* error */
3175xfs_bmap_extents_to_btree(
3176 xfs_trans_t *tp, /* transaction pointer */
3177 xfs_inode_t *ip, /* incore inode pointer */
3178 xfs_fsblock_t *firstblock, /* first-block-allocated */
3179 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3180 xfs_btree_cur_t **curp, /* cursor returned to caller */
3181 int wasdel, /* converting a delayed alloc */
3182 int *logflagsp, /* inode logging flags */
3183 int whichfork) /* data or attr fork */
3184{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003185 struct xfs_btree_block *ablock; /* allocated (child) bt block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 xfs_buf_t *abp; /* buffer for ablock */
3187 xfs_alloc_arg_t args; /* allocation arguments */
3188 xfs_bmbt_rec_t *arp; /* child record pointer */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003189 struct xfs_btree_block *block; /* btree root block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 xfs_btree_cur_t *cur; /* bmap btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003191 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003193 xfs_extnum_t i, cnt; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 xfs_ifork_t *ifp; /* inode fork pointer */
3195 xfs_bmbt_key_t *kp; /* root block key pointer */
3196 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003197 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3199
3200 ifp = XFS_IFORK_PTR(ip, whichfork);
3201 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3202 ASSERT(ifp->if_ext_max ==
3203 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3204 /*
3205 * Make space in the inode incore.
3206 */
3207 xfs_iroot_realloc(ip, 1, whichfork);
3208 ifp->if_flags |= XFS_IFBROOT;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003209
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 /*
3211 * Fill in the root.
3212 */
3213 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003214 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3215 block->bb_level = cpu_to_be16(1);
3216 block->bb_numrecs = cpu_to_be16(1);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003217 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3218 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 /*
3221 * Need a cursor. Can't allocate until bb_level is filled in.
3222 */
3223 mp = ip->i_mount;
Christoph Hellwig561f7d12008-10-30 16:53:59 +11003224 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 cur->bc_private.b.firstblock = *firstblock;
3226 cur->bc_private.b.flist = flist;
3227 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3228 /*
3229 * Convert to a btree with two levels, one record in root.
3230 */
3231 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3232 args.tp = tp;
3233 args.mp = mp;
Yingping Lud210a282006-06-09 14:55:18 +10003234 args.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 if (*firstblock == NULLFSBLOCK) {
3236 args.type = XFS_ALLOCTYPE_START_BNO;
3237 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3238 } else if (flist->xbf_low) {
3239 args.type = XFS_ALLOCTYPE_START_BNO;
3240 args.fsbno = *firstblock;
3241 } else {
3242 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3243 args.fsbno = *firstblock;
3244 }
3245 args.minlen = args.maxlen = args.prod = 1;
3246 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3247 args.minalignslop = 0;
3248 args.wasdel = wasdel;
3249 *logflagsp = 0;
3250 if ((error = xfs_alloc_vextent(&args))) {
3251 xfs_iroot_realloc(ip, -1, whichfork);
3252 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3253 return error;
3254 }
3255 /*
3256 * Allocation can't fail, the space was reserved.
3257 */
3258 ASSERT(args.fsbno != NULLFSBLOCK);
3259 ASSERT(*firstblock == NULLFSBLOCK ||
3260 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3261 (flist->xbf_low &&
3262 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3263 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3264 cur->bc_private.b.allocated++;
3265 ip->i_d.di_nblocks++;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003266 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3268 /*
3269 * Fill in the child block.
3270 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003271 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003272 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 ablock->bb_level = 0;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003274 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3275 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003276 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003278 for (cnt = i = 0; i < nextents; i++) {
3279 ep = xfs_iext_get_ext(ifp, i);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003280 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10003281 arp->l0 = cpu_to_be64(ep->l0);
3282 arp->l1 = cpu_to_be64(ep->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 arp++; cnt++;
3284 }
3285 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11003286 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003287 xfs_btree_set_numrecs(ablock, cnt);
3288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 /*
3290 * Fill in the root key and pointer.
3291 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11003292 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3293 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Christoph Hellwig8801bb92006-09-28 10:58:17 +10003294 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
Christoph Hellwig136341b2008-10-30 17:11:40 +11003295 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3296 be16_to_cpu(block->bb_level)));
Christoph Hellwig576039c2006-09-28 10:58:06 +10003297 *pp = cpu_to_be64(args.fsbno);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 /*
3300 * Do all this logging at the end so that
3301 * the root is at the right level.
3302 */
Christoph Hellwigfd6bcc52008-10-30 16:58:21 +11003303 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3304 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 ASSERT(*curp == NULL);
3306 *curp = cur;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003307 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 return 0;
3309}
3310
3311/*
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003312 * Calculate the default attribute fork offset for newly created inodes.
3313 */
3314uint
3315xfs_default_attroffset(
3316 struct xfs_inode *ip)
3317{
3318 struct xfs_mount *mp = ip->i_mount;
3319 uint offset;
3320
3321 if (mp->m_sb.sb_inodesize == 256) {
3322 offset = XFS_LITINO(mp) -
3323 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3324 } else {
3325 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3326 }
3327
3328 ASSERT(offset < XFS_LITINO(mp));
3329 return offset;
3330}
3331
3332/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11003333 * Helper routine to reset inode di_forkoff field when switching
3334 * attribute fork from local to extent format - we reset it where
3335 * possible to make space available for inline data fork extents.
3336 */
3337STATIC void
3338xfs_bmap_forkoff_reset(
3339 xfs_mount_t *mp,
3340 xfs_inode_t *ip,
3341 int whichfork)
3342{
3343 if (whichfork == XFS_ATTR_FORK &&
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003344 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3345 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3346 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3347 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3348
3349 if (dfl_forkoff > ip->i_d.di_forkoff) {
3350 ip->i_d.di_forkoff = dfl_forkoff;
3351 ip->i_df.if_ext_max =
3352 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3353 ip->i_afp->if_ext_max =
3354 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3355 }
Nathan Scottd8cc8902005-11-02 10:34:53 +11003356 }
3357}
3358
3359/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 * Convert a local file to an extents file.
3361 * This code is out of bounds for data forks of regular files,
3362 * since the file data needs to get logged so things will stay consistent.
3363 * (The bmap-level manipulations are ok, though).
3364 */
3365STATIC int /* error */
3366xfs_bmap_local_to_extents(
3367 xfs_trans_t *tp, /* transaction pointer */
3368 xfs_inode_t *ip, /* incore inode pointer */
3369 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3370 xfs_extlen_t total, /* total blocks needed by transaction */
3371 int *logflagsp, /* inode logging flags */
3372 int whichfork) /* data or attr fork */
3373{
3374 int error; /* error return value */
3375 int flags; /* logging flags returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 xfs_ifork_t *ifp; /* inode fork pointer */
3377
3378 /*
3379 * We don't want to deal with the case of keeping inode data inline yet.
3380 * So sending the data fork of a regular inode is invalid.
3381 */
3382 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3383 whichfork == XFS_DATA_FORK));
3384 ifp = XFS_IFORK_PTR(ip, whichfork);
3385 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3386 flags = 0;
3387 error = 0;
3388 if (ifp->if_bytes) {
3389 xfs_alloc_arg_t args; /* allocation arguments */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003390 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003391 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
3393 args.tp = tp;
3394 args.mp = ip->i_mount;
Yingping Lud210a282006-06-09 14:55:18 +10003395 args.firstblock = *firstblock;
Mandy Kirkconnellf020b672006-03-14 14:07:24 +11003396 ASSERT((ifp->if_flags &
3397 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 /*
3399 * Allocate a block. We know we need only one, since the
3400 * file currently fits in an inode.
3401 */
3402 if (*firstblock == NULLFSBLOCK) {
3403 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3404 args.type = XFS_ALLOCTYPE_START_BNO;
3405 } else {
3406 args.fsbno = *firstblock;
3407 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3408 }
3409 args.total = total;
3410 args.mod = args.minleft = args.alignment = args.wasdel =
3411 args.isfl = args.minalignslop = 0;
3412 args.minlen = args.maxlen = args.prod = 1;
3413 if ((error = xfs_alloc_vextent(&args)))
3414 goto done;
3415 /*
3416 * Can't fail, the space was reserved.
3417 */
3418 ASSERT(args.fsbno != NULLFSBLOCK);
3419 ASSERT(args.len == 1);
3420 *firstblock = args.fsbno;
3421 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3422 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3423 ifp->if_bytes);
3424 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003425 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003427 xfs_iext_add(ifp, 0, 1);
3428 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003430 trace_xfs_bmap_post_update(ip, 0,
3431 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3432 _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3434 ip->i_d.di_nblocks = 1;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003435 xfs_trans_mod_dquot_byino(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 XFS_TRANS_DQ_BCOUNT, 1L);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003437 flags |= xfs_ilog_fext(whichfork);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003438 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003440 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 ifp->if_flags &= ~XFS_IFINLINE;
3443 ifp->if_flags |= XFS_IFEXTENTS;
3444 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3445 flags |= XFS_ILOG_CORE;
3446done:
3447 *logflagsp = flags;
3448 return error;
3449}
3450
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003452 * Search the extent records for the entry containing block bno.
3453 * If bno lies in a hole, point to the next entry. If bno lies
3454 * past eof, *eofp will be set, and *prevp will contain the last
3455 * entry (null if none). Else, *lastxp will be set to the index
3456 * of the found entry; *gotp will contain the entry.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003457 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003458STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003459xfs_bmap_search_multi_extents(
3460 xfs_ifork_t *ifp, /* inode fork pointer */
3461 xfs_fileoff_t bno, /* block number searched for */
3462 int *eofp, /* out: end of file found */
3463 xfs_extnum_t *lastxp, /* out: last extent index */
3464 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3465 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3466{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003467 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003468 xfs_extnum_t lastx; /* last extent index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003469
3470 /*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003471 * Initialize the extent entry structure to catch access to
3472 * uninitialized br_startblock field.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003473 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003474 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3475 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3476 gotp->br_state = XFS_EXT_INVALID;
3477#if XFS_BIG_BLKNOS
3478 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3479#else
3480 gotp->br_startblock = 0xffffa5a5;
3481#endif
3482 prevp->br_startoff = NULLFILEOFF;
3483
3484 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3485 if (lastx > 0) {
3486 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3487 }
3488 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3489 xfs_bmbt_get_all(ep, gotp);
3490 *eofp = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003491 } else {
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003492 if (lastx > 0) {
3493 *gotp = *prevp;
3494 }
3495 *eofp = 1;
3496 ep = NULL;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003497 }
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003498 *lastxp = lastx;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003499 return ep;
3500}
3501
3502/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 * Search the extents list for the inode, for the extent containing bno.
3504 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3505 * *eofp will be set, and *prevp will contain the last entry (null if none).
3506 * Else, *lastxp will be set to the index of the found
3507 * entry; *gotp will contain the entry.
3508 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003509STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510xfs_bmap_search_extents(
3511 xfs_inode_t *ip, /* incore inode pointer */
3512 xfs_fileoff_t bno, /* block number searched for */
Nathan Scott572d95f2006-09-28 11:03:20 +10003513 int fork, /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 int *eofp, /* out: end of file found */
3515 xfs_extnum_t *lastxp, /* out: last extent index */
3516 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3517 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3518{
3519 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003520 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
3522 XFS_STATS_INC(xs_look_exlist);
Nathan Scott572d95f2006-09-28 11:03:20 +10003523 ifp = XFS_IFORK_PTR(ip, fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003525 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3526
Nathan Scott572d95f2006-09-28 11:03:20 +10003527 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3528 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003529 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
Nathan Scott572d95f2006-09-28 11:03:20 +10003530 "Access to block zero in inode %llu "
3531 "start_block: %llx start_off: %llx "
3532 "blkcnt: %llx extent-state: %x lastx: %x\n",
3533 (unsigned long long)ip->i_ino,
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003534 (unsigned long long)gotp->br_startblock,
3535 (unsigned long long)gotp->br_startoff,
3536 (unsigned long long)gotp->br_blockcount,
Nathan Scott572d95f2006-09-28 11:03:20 +10003537 gotp->br_state, *lastxp);
3538 *lastxp = NULLEXTNUM;
3539 *eofp = 1;
3540 return NULL;
3541 }
3542 return ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543}
3544
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545/*
3546 * Compute the worst-case number of indirect blocks that will be used
3547 * for ip's delayed extent of length "len".
3548 */
3549STATIC xfs_filblks_t
3550xfs_bmap_worst_indlen(
3551 xfs_inode_t *ip, /* incore inode pointer */
3552 xfs_filblks_t len) /* delayed extent length */
3553{
3554 int level; /* btree level number */
3555 int maxrecs; /* maximum record count at this level */
3556 xfs_mount_t *mp; /* mount structure */
3557 xfs_filblks_t rval; /* return value */
3558
3559 mp = ip->i_mount;
3560 maxrecs = mp->m_bmap_dmxr[0];
3561 for (level = 0, rval = 0;
3562 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3563 level++) {
3564 len += maxrecs - 1;
3565 do_div(len, maxrecs);
3566 rval += len;
3567 if (len == 1)
3568 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3569 level - 1;
3570 if (level == 0)
3571 maxrecs = mp->m_bmap_dmxr[1];
3572 }
3573 return rval;
3574}
3575
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576/*
3577 * Convert inode from non-attributed to attributed.
3578 * Must not be in a transaction, ip must not be locked.
3579 */
3580int /* error code */
3581xfs_bmap_add_attrfork(
3582 xfs_inode_t *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003583 int size, /* space new attribute needs */
3584 int rsvd) /* xact may use reserved blks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003587 xfs_bmap_free_t flist; /* freed extent records */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 xfs_trans_t *tp; /* transaction pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003590 int blks; /* space reservation */
3591 int version = 1; /* superblock attr version */
3592 int committed; /* xaction was committed */
3593 int logflags; /* logging flags */
3594 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Nathan Scottd8cc8902005-11-02 10:34:53 +11003596 ASSERT(XFS_IFORK_Q(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 ASSERT(ip->i_df.if_ext_max ==
3598 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
Nathan Scottd8cc8902005-11-02 10:34:53 +11003599
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 mp = ip->i_mount;
3601 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3602 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3603 blks = XFS_ADDAFORK_SPACE_RES(mp);
3604 if (rsvd)
3605 tp->t_flags |= XFS_TRANS_RESERVE;
3606 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3607 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3608 goto error0;
3609 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02003610 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3612 XFS_QMOPT_RES_REGBLKS);
3613 if (error) {
3614 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3615 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3616 return error;
3617 }
3618 if (XFS_IFORK_Q(ip))
3619 goto error1;
3620 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3621 /*
3622 * For inodes coming from pre-6.2 filesystems.
3623 */
3624 ASSERT(ip->i_d.di_aformat == 0);
3625 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3626 }
3627 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwig898621d2010-06-24 11:36:58 +10003628
3629 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig898621d2010-06-24 11:36:58 +10003631
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 switch (ip->i_d.di_format) {
3633 case XFS_DINODE_FMT_DEV:
3634 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3635 break;
3636 case XFS_DINODE_FMT_UUID:
3637 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3638 break;
3639 case XFS_DINODE_FMT_LOCAL:
3640 case XFS_DINODE_FMT_EXTENTS:
3641 case XFS_DINODE_FMT_BTREE:
Nathan Scottd8cc8902005-11-02 10:34:53 +11003642 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3643 if (!ip->i_d.di_forkoff)
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003644 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
Nathan Scott13059ff2006-01-11 15:32:01 +11003645 else if (mp->m_flags & XFS_MOUNT_ATTR2)
Nathan Scottd8cc8902005-11-02 10:34:53 +11003646 version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 break;
3648 default:
3649 ASSERT(0);
3650 error = XFS_ERROR(EINVAL);
3651 goto error1;
3652 }
3653 ip->i_df.if_ext_max =
3654 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3655 ASSERT(ip->i_afp == NULL);
3656 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3657 ip->i_afp->if_ext_max =
3658 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3659 ip->i_afp->if_flags = XFS_IFEXTENTS;
3660 logflags = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003661 xfs_bmap_init(&flist, &firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 switch (ip->i_d.di_format) {
3663 case XFS_DINODE_FMT_LOCAL:
3664 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3665 &logflags);
3666 break;
3667 case XFS_DINODE_FMT_EXTENTS:
3668 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3669 &flist, &logflags);
3670 break;
3671 case XFS_DINODE_FMT_BTREE:
3672 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3673 &logflags);
3674 break;
3675 default:
3676 error = 0;
3677 break;
3678 }
3679 if (logflags)
3680 xfs_trans_log_inode(tp, ip, logflags);
3681 if (error)
3682 goto error2;
Eric Sandeen62118702008-03-06 13:44:28 +11003683 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3684 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +11003685 __int64_t sbfields = 0;
3686
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003687 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +11003688 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3689 xfs_sb_version_addattr(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003690 sbfields |= XFS_SB_VERSIONNUM;
Nathan Scottd8cc8902005-11-02 10:34:53 +11003691 }
Eric Sandeen62118702008-03-06 13:44:28 +11003692 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3693 xfs_sb_version_addattr2(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003694 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003695 }
Nathan Scottda087ba2005-11-02 15:00:20 +11003696 if (sbfields) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003697 spin_unlock(&mp->m_sb_lock);
Nathan Scottda087ba2005-11-02 15:00:20 +11003698 xfs_mod_sb(tp, sbfields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003700 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 }
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003702 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 goto error2;
Dave Chinner713bf882010-03-08 11:26:23 +11003704 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 ASSERT(ip->i_df.if_ext_max ==
3706 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3707 return error;
3708error2:
3709 xfs_bmap_cancel(&flist);
3710error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3712error0:
3713 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3714 ASSERT(ip->i_df.if_ext_max ==
3715 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3716 return error;
3717}
3718
3719/*
3720 * Add the extent to the list of extents to be free at transaction end.
3721 * The list is maintained sorted (by block number).
3722 */
3723/* ARGSUSED */
3724void
3725xfs_bmap_add_free(
3726 xfs_fsblock_t bno, /* fs block number of extent */
3727 xfs_filblks_t len, /* length of extent */
3728 xfs_bmap_free_t *flist, /* list of extents */
3729 xfs_mount_t *mp) /* mount point structure */
3730{
3731 xfs_bmap_free_item_t *cur; /* current (next) element */
3732 xfs_bmap_free_item_t *new; /* new element */
3733 xfs_bmap_free_item_t *prev; /* previous element */
3734#ifdef DEBUG
3735 xfs_agnumber_t agno;
3736 xfs_agblock_t agbno;
3737
3738 ASSERT(bno != NULLFSBLOCK);
3739 ASSERT(len > 0);
3740 ASSERT(len <= MAXEXTLEN);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003741 ASSERT(!isnullstartblock(bno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 agno = XFS_FSB_TO_AGNO(mp, bno);
3743 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3744 ASSERT(agno < mp->m_sb.sb_agcount);
3745 ASSERT(agbno < mp->m_sb.sb_agblocks);
3746 ASSERT(len < mp->m_sb.sb_agblocks);
3747 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3748#endif
3749 ASSERT(xfs_bmap_free_item_zone != NULL);
3750 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3751 new->xbfi_startblock = bno;
3752 new->xbfi_blockcount = (xfs_extlen_t)len;
3753 for (prev = NULL, cur = flist->xbf_first;
3754 cur != NULL;
3755 prev = cur, cur = cur->xbfi_next) {
3756 if (cur->xbfi_startblock >= bno)
3757 break;
3758 }
3759 if (prev)
3760 prev->xbfi_next = new;
3761 else
3762 flist->xbf_first = new;
3763 new->xbfi_next = cur;
3764 flist->xbf_count++;
3765}
3766
3767/*
3768 * Compute and fill in the value of the maximum depth of a bmap btree
3769 * in this filesystem. Done once, during mount.
3770 */
3771void
3772xfs_bmap_compute_maxlevels(
3773 xfs_mount_t *mp, /* file system mount structure */
3774 int whichfork) /* data or attr fork */
3775{
3776 int level; /* btree level */
3777 uint maxblocks; /* max blocks at this level */
3778 uint maxleafents; /* max leaf entries possible */
3779 int maxrootrecs; /* max records in root block */
3780 int minleafrecs; /* min records in leaf block */
3781 int minnoderecs; /* min records in node block */
3782 int sz; /* root block size */
3783
3784 /*
3785 * The maximum number of extents in a file, hence the maximum
3786 * number of leaf entries, is controlled by the type of di_nextents
3787 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3788 * (a signed 16-bit number, xfs_aextnum_t).
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003789 *
3790 * Note that we can no longer assume that if we are in ATTR1 that
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003791 * the fork offset of all the inodes will be
3792 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3793 * with ATTR2 and then mounted back with ATTR1, keeping the
3794 * di_forkoff's fixed but probably at various positions. Therefore,
3795 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3796 * of a minimum size available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003798 if (whichfork == XFS_DATA_FORK) {
3799 maxleafents = MAXEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003800 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003801 } else {
3802 maxleafents = MAXAEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003803 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003804 }
Christoph Hellwig60197e82008-10-30 17:11:19 +11003805 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 minleafrecs = mp->m_bmap_dmnr[0];
3807 minnoderecs = mp->m_bmap_dmnr[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3809 for (level = 1; maxblocks > 1; level++) {
3810 if (maxblocks <= maxrootrecs)
3811 maxblocks = 1;
3812 else
3813 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3814 }
3815 mp->m_bm_maxlevels[whichfork] = level;
3816}
3817
3818/*
3819 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3820 * caller. Frees all the extents that need freeing, which must be done
3821 * last due to locking considerations. We never free any extents in
3822 * the first transaction. This is to allow the caller to make the first
3823 * transaction a synchronous one so that the pointers to the data being
3824 * broken in this transaction will be permanent before the data is actually
3825 * freed. This is necessary to prevent blocks from being reallocated
3826 * and written to before the free and reallocation are actually permanent.
3827 * We do not just make the first transaction synchronous here, because
3828 * there are more efficient ways to gain the same protection in some cases
3829 * (see the file truncation code).
3830 *
3831 * Return 1 if the given transaction was committed and a new one
3832 * started, and 0 otherwise in the committed parameter.
3833 */
3834/*ARGSUSED*/
3835int /* error */
3836xfs_bmap_finish(
3837 xfs_trans_t **tp, /* transaction pointer addr */
3838 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 int *committed) /* xact committed or not */
3840{
3841 xfs_efd_log_item_t *efd; /* extent free data */
3842 xfs_efi_log_item_t *efi; /* extent free intention */
3843 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003844 xfs_bmap_free_item_t *free; /* free extent item */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 unsigned int logres; /* new log reservation */
3846 unsigned int logcount; /* new log count */
3847 xfs_mount_t *mp; /* filesystem mount structure */
3848 xfs_bmap_free_item_t *next; /* next item on free list */
3849 xfs_trans_t *ntp; /* new transaction pointer */
3850
3851 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3852 if (flist->xbf_count == 0) {
3853 *committed = 0;
3854 return 0;
3855 }
3856 ntp = *tp;
3857 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3858 for (free = flist->xbf_first; free; free = free->xbfi_next)
3859 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3860 free->xbfi_blockcount);
3861 logres = ntp->t_log_res;
3862 logcount = ntp->t_log_count;
3863 ntp = xfs_trans_dup(*tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003864 error = xfs_trans_commit(*tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 *tp = ntp;
3866 *committed = 1;
3867 /*
3868 * We have a new transaction, so we should return committed=1,
3869 * even though we're returning an error.
3870 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003871 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 return error;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003873
3874 /*
3875 * transaction commit worked ok so we can drop the extra ticket
3876 * reference that we gained in xfs_trans_dup()
3877 */
3878 xfs_log_ticket_put(ntp->t_ticket);
3879
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3881 logcount)))
3882 return error;
3883 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3884 for (free = flist->xbf_first; free != NULL; free = next) {
3885 next = free->xbfi_next;
3886 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3887 free->xbfi_blockcount))) {
3888 /*
3889 * The bmap free list will be cleaned up at a
3890 * higher level. The EFI will be canceled when
3891 * this transaction is aborted.
3892 * Need to force shutdown here to make sure it
3893 * happens, since this transaction may not be
3894 * dirty yet.
3895 */
3896 mp = ntp->t_mountp;
3897 if (!XFS_FORCED_SHUTDOWN(mp))
3898 xfs_force_shutdown(mp,
3899 (error == EFSCORRUPTED) ?
Nathan Scott7d04a332006-06-09 14:58:38 +10003900 SHUTDOWN_CORRUPT_INCORE :
3901 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 return error;
3903 }
3904 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3905 free->xbfi_blockcount);
3906 xfs_bmap_del_free(flist, NULL, free);
3907 }
3908 return 0;
3909}
3910
3911/*
3912 * Free up any items left in the list.
3913 */
3914void
3915xfs_bmap_cancel(
3916 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3917{
3918 xfs_bmap_free_item_t *free; /* free list item */
3919 xfs_bmap_free_item_t *next;
3920
3921 if (flist->xbf_count == 0)
3922 return;
3923 ASSERT(flist->xbf_first != NULL);
3924 for (free = flist->xbf_first; free; free = next) {
3925 next = free->xbfi_next;
3926 xfs_bmap_del_free(flist, NULL, free);
3927 }
3928 ASSERT(flist->xbf_count == 0);
3929}
3930
3931/*
3932 * Returns the file-relative block number of the first unused block(s)
3933 * in the file with at least "len" logically contiguous blocks free.
3934 * This is the lowest-address hole if the file has holes, else the first block
3935 * past the end of file.
3936 * Return 0 if the file is currently local (in-inode).
3937 */
3938int /* error */
3939xfs_bmap_first_unused(
3940 xfs_trans_t *tp, /* transaction pointer */
3941 xfs_inode_t *ip, /* incore inode */
3942 xfs_extlen_t len, /* size of hole to find */
3943 xfs_fileoff_t *first_unused, /* unused block */
3944 int whichfork) /* data or attr fork */
3945{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003947 int idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 xfs_ifork_t *ifp; /* inode fork pointer */
3949 xfs_fileoff_t lastaddr; /* last block number seen */
3950 xfs_fileoff_t lowest; /* lowest useful block */
3951 xfs_fileoff_t max; /* starting useful block */
3952 xfs_fileoff_t off; /* offset for this block */
3953 xfs_extnum_t nextents; /* number of extent entries */
3954
3955 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3956 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3957 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3958 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3959 *first_unused = 0;
3960 return 0;
3961 }
3962 ifp = XFS_IFORK_PTR(ip, whichfork);
3963 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3964 (error = xfs_iread_extents(tp, ip, whichfork)))
3965 return error;
3966 lowest = *first_unused;
3967 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003968 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003969 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 off = xfs_bmbt_get_startoff(ep);
3971 /*
3972 * See if the hole before this extent will work.
3973 */
3974 if (off >= lowest + len && off - max >= len) {
3975 *first_unused = max;
3976 return 0;
3977 }
3978 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3979 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3980 }
3981 *first_unused = max;
3982 return 0;
3983}
3984
3985/*
3986 * Returns the file-relative block number of the last block + 1 before
3987 * last_block (input value) in the file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003988 * This is not based on i_size, it is based on the extent records.
3989 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 */
3991int /* error */
3992xfs_bmap_last_before(
3993 xfs_trans_t *tp, /* transaction pointer */
3994 xfs_inode_t *ip, /* incore inode */
3995 xfs_fileoff_t *last_block, /* last block */
3996 int whichfork) /* data or attr fork */
3997{
3998 xfs_fileoff_t bno; /* input file offset */
3999 int eof; /* hit end of file */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004000 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 int error; /* error return value */
4002 xfs_bmbt_irec_t got; /* current extent value */
4003 xfs_ifork_t *ifp; /* inode fork pointer */
4004 xfs_extnum_t lastx; /* last extent used */
4005 xfs_bmbt_irec_t prev; /* previous extent value */
4006
4007 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4008 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4009 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4010 return XFS_ERROR(EIO);
4011 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4012 *last_block = 0;
4013 return 0;
4014 }
4015 ifp = XFS_IFORK_PTR(ip, whichfork);
4016 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4017 (error = xfs_iread_extents(tp, ip, whichfork)))
4018 return error;
4019 bno = *last_block - 1;
4020 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4021 &prev);
4022 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4023 if (prev.br_startoff == NULLFILEOFF)
4024 *last_block = 0;
4025 else
4026 *last_block = prev.br_startoff + prev.br_blockcount;
4027 }
4028 /*
4029 * Otherwise *last_block is already the right answer.
4030 */
4031 return 0;
4032}
4033
4034/*
4035 * Returns the file-relative block number of the first block past eof in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004036 * the file. This is not based on i_size, it is based on the extent records.
4037 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 */
4039int /* error */
4040xfs_bmap_last_offset(
4041 xfs_trans_t *tp, /* transaction pointer */
4042 xfs_inode_t *ip, /* incore inode */
4043 xfs_fileoff_t *last_block, /* last block */
4044 int whichfork) /* data or attr fork */
4045{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004046 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 int error; /* error return value */
4048 xfs_ifork_t *ifp; /* inode fork pointer */
4049 xfs_extnum_t nextents; /* number of extent entries */
4050
4051 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4052 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4053 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4054 return XFS_ERROR(EIO);
4055 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4056 *last_block = 0;
4057 return 0;
4058 }
4059 ifp = XFS_IFORK_PTR(ip, whichfork);
4060 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4061 (error = xfs_iread_extents(tp, ip, whichfork)))
4062 return error;
4063 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4064 if (!nextents) {
4065 *last_block = 0;
4066 return 0;
4067 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004068 ep = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4070 return 0;
4071}
4072
4073/*
4074 * Returns whether the selected fork of the inode has exactly one
4075 * block or not. For the data fork we check this matches di_size,
4076 * implying the file's range is 0..bsize-1.
4077 */
4078int /* 1=>1 block, 0=>otherwise */
4079xfs_bmap_one_block(
4080 xfs_inode_t *ip, /* incore inode */
4081 int whichfork) /* data or attr fork */
4082{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004083 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 xfs_ifork_t *ifp; /* inode fork pointer */
4085 int rval; /* return value */
4086 xfs_bmbt_irec_t s; /* internal version of extent */
4087
4088#ifndef DEBUG
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004089 if (whichfork == XFS_DATA_FORK) {
4090 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4091 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4092 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094#endif /* !DEBUG */
4095 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4096 return 0;
4097 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4098 return 0;
4099 ifp = XFS_IFORK_PTR(ip, whichfork);
4100 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004101 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 xfs_bmbt_get_all(ep, &s);
4103 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4104 if (rval && whichfork == XFS_DATA_FORK)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004105 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 return rval;
4107}
4108
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004109STATIC int
4110xfs_bmap_sanity_check(
4111 struct xfs_mount *mp,
4112 struct xfs_buf *bp,
4113 int level)
4114{
4115 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4116
4117 if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
4118 be16_to_cpu(block->bb_level) != level ||
4119 be16_to_cpu(block->bb_numrecs) == 0 ||
4120 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4121 return 0;
4122 return 1;
4123}
4124
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125/*
4126 * Read in the extents to if_extents.
4127 * All inode fields are set up by caller, we just traverse the btree
4128 * and copy the records in. If the file system cannot contain unwritten
4129 * extents, the records are checked for no "state" flags.
4130 */
4131int /* error */
4132xfs_bmap_read_extents(
4133 xfs_trans_t *tp, /* transaction pointer */
4134 xfs_inode_t *ip, /* incore inode */
4135 int whichfork) /* data or attr fork */
4136{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004137 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 xfs_fsblock_t bno; /* block # of "block" */
4139 xfs_buf_t *bp; /* buffer for "block" */
4140 int error; /* error return value */
4141 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 xfs_extnum_t i, j; /* index into the extents list */
4143 xfs_ifork_t *ifp; /* fork structure */
4144 int level; /* btree level, for checking */
4145 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10004146 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 /* REFERENCED */
4148 xfs_extnum_t room; /* number of entries there's room for */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
4150 bno = NULLFSBLOCK;
4151 mp = ip->i_mount;
4152 ifp = XFS_IFORK_PTR(ip, whichfork);
4153 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4154 XFS_EXTFMT_INODE(ip);
4155 block = ifp->if_broot;
4156 /*
4157 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4158 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004159 level = be16_to_cpu(block->bb_level);
4160 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11004161 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004162 bno = be64_to_cpu(*pp);
4163 ASSERT(bno != NULLDFSBNO);
4164 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4165 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 /*
4167 * Go down the tree until leaf level is reached, following the first
4168 * pointer (leftmost) at each level.
4169 */
4170 while (level-- > 0) {
4171 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4172 XFS_BMAP_BTREE_REF)))
4173 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004174 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004176 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 error0);
4178 if (level == 0)
4179 break;
Christoph Hellwig136341b2008-10-30 17:11:40 +11004180 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004181 bno = be64_to_cpu(*pp);
4182 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 xfs_trans_brelse(tp, bp);
4184 }
4185 /*
4186 * Here with bp and block set to the leftmost leaf node in the tree.
4187 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004188 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 i = 0;
4190 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004191 * Loop over all leaf nodes. Copy information to the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 */
4193 for (;;) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004194 xfs_bmbt_rec_t *frp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 xfs_fsblock_t nextbno;
4196 xfs_extnum_t num_recs;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004197 xfs_extnum_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
4199
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004200 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 if (unlikely(i + num_recs > room)) {
4202 ASSERT(i + num_recs <= room);
Nathan Scott3762ec62006-01-12 10:29:53 +11004203 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4204 "corrupt dinode %Lu, (btree extents).",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 (unsigned long long) ip->i_ino);
4206 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4207 XFS_ERRLEVEL_LOW,
4208 ip->i_mount);
4209 goto error0;
4210 }
4211 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004212 xfs_bmap_sanity_check(mp, bp, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 error0);
4214 /*
4215 * Read-ahead the next leaf block, if any.
4216 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004217 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 if (nextbno != NULLFSBLOCK)
4219 xfs_btree_reada_bufl(mp, nextbno, 1);
4220 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004221 * Copy records into the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11004223 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004224 start = i;
4225 for (j = 0; j < num_recs; j++, i++, frp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004226 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10004227 trp->l0 = be64_to_cpu(frp->l0);
4228 trp->l1 = be64_to_cpu(frp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 }
4230 if (exntf == XFS_EXTFMT_NOSTATE) {
4231 /*
4232 * Check all attribute bmap btree records and
4233 * any "older" data bmap btree records for a
4234 * set bit in the "extent flag" position.
4235 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004236 if (unlikely(xfs_check_nostate_extents(ifp,
4237 start, num_recs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4239 XFS_ERRLEVEL_LOW,
4240 ip->i_mount);
4241 goto error0;
4242 }
4243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 xfs_trans_brelse(tp, bp);
4245 bno = nextbno;
4246 /*
4247 * If we've reached the end, stop.
4248 */
4249 if (bno == NULLFSBLOCK)
4250 break;
4251 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4252 XFS_BMAP_BTREE_REF)))
4253 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004254 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004256 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
Eric Sandeen3a59c942007-07-11 11:09:47 +10004258 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 return 0;
4260error0:
4261 xfs_trans_brelse(tp, bp);
4262 return XFS_ERROR(EFSCORRUPTED);
4263}
4264
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004265#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004267 * Add bmap trace insert entries for all the contents of the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 */
4269void
4270xfs_bmap_trace_exlist(
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 xfs_inode_t *ip, /* incore inode pointer */
4272 xfs_extnum_t cnt, /* count of entries in the list */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004273 int whichfork, /* data or attr fork */
4274 unsigned long caller_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004276 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004278 int state = 0;
4279
4280 if (whichfork == XFS_ATTR_FORK)
4281 state |= BMAP_ATTRFORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
4283 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004284 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004285 for (idx = 0; idx < cnt; idx++)
4286 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289/*
4290 * Validate that the bmbt_irecs being returned from bmapi are valid
4291 * given the callers original parameters. Specifically check the
4292 * ranges of the returned irecs to ensure that they only extent beyond
4293 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4294 */
4295STATIC void
4296xfs_bmap_validate_ret(
4297 xfs_fileoff_t bno,
4298 xfs_filblks_t len,
4299 int flags,
4300 xfs_bmbt_irec_t *mval,
4301 int nmap,
4302 int ret_nmap)
4303{
4304 int i; /* index to map values */
4305
4306 ASSERT(ret_nmap <= nmap);
4307
4308 for (i = 0; i < ret_nmap; i++) {
4309 ASSERT(mval[i].br_blockcount > 0);
4310 if (!(flags & XFS_BMAPI_ENTIRE)) {
4311 ASSERT(mval[i].br_startoff >= bno);
4312 ASSERT(mval[i].br_blockcount <= len);
4313 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4314 bno + len);
4315 } else {
4316 ASSERT(mval[i].br_startoff < bno + len);
4317 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4318 bno);
4319 }
4320 ASSERT(i == 0 ||
4321 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4322 mval[i].br_startoff);
4323 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4324 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4325 mval[i].br_startblock != HOLESTARTBLOCK);
4326 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4327 mval[i].br_state == XFS_EXT_UNWRITTEN);
4328 }
4329}
4330#endif /* DEBUG */
4331
4332
4333/*
4334 * Map file blocks to filesystem blocks.
4335 * File range is given by the bno/len pair.
4336 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4337 * into a hole or past eof.
4338 * Only allocates blocks from a single allocation group,
4339 * to avoid locking problems.
4340 * The returned value in "firstblock" from the first call in a transaction
4341 * must be remembered and presented to subsequent calls in "firstblock".
4342 * An upper bound for the number of blocks to be allocated is supplied to
4343 * the first call in "total"; if no allocation group has that many free
4344 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4345 */
4346int /* error */
4347xfs_bmapi(
4348 xfs_trans_t *tp, /* transaction pointer */
4349 xfs_inode_t *ip, /* incore inode */
4350 xfs_fileoff_t bno, /* starting file offs. mapped */
4351 xfs_filblks_t len, /* length to map in file */
4352 int flags, /* XFS_BMAPI_... */
4353 xfs_fsblock_t *firstblock, /* first allocated block
4354 controls a.g. for allocs */
4355 xfs_extlen_t total, /* total blocks needed */
4356 xfs_bmbt_irec_t *mval, /* output: map values */
4357 int *nmap, /* i/o: mval size/count */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004358 xfs_bmap_free_t *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359{
4360 xfs_fsblock_t abno; /* allocated block number */
4361 xfs_extlen_t alen; /* allocated extent length */
4362 xfs_fileoff_t aoff; /* allocated file offset */
Dave Chinnerf0a0eaa2010-01-20 10:50:06 +11004363 xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 xfs_btree_cur_t *cur; /* bmap btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 xfs_fileoff_t end; /* end of mapped file region */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004366 int eof; /* we've hit the end of extents */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004367 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004369 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 xfs_ifork_t *ifp; /* inode fork pointer */
4371 xfs_extlen_t indlen; /* indirect blocks length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 xfs_extnum_t lastx; /* last useful extent number */
4373 int logflags; /* flags for transaction logging */
4374 xfs_extlen_t minleft; /* min blocks left after allocation */
4375 xfs_extlen_t minlen; /* min allocation size */
4376 xfs_mount_t *mp; /* xfs mount structure */
4377 int n; /* current extent index */
Malcolm Parsons9da096f2009-03-29 09:55:42 +02004378 int nallocs; /* number of extents alloc'd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 xfs_extnum_t nextents; /* number of extents in file */
4380 xfs_fileoff_t obno; /* old block number (offset) */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004381 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 int tmp_logflags; /* temp flags holder */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004383 int whichfork; /* data or attr fork */
4384 char inhole; /* current location is hole in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 char wasdelay; /* old extent was delayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 char wr; /* this is a write request */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004387 char rt; /* this is a realtime file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388#ifdef DEBUG
4389 xfs_fileoff_t orig_bno; /* original block number value */
4390 int orig_flags; /* original flags arg value */
4391 xfs_filblks_t orig_len; /* original value of len arg */
4392 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4393 int orig_nmap; /* original value of *nmap */
4394
4395 orig_bno = bno;
4396 orig_len = len;
4397 orig_flags = flags;
4398 orig_mval = mval;
4399 orig_nmap = *nmap;
4400#endif
4401 ASSERT(*nmap >= 1);
4402 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4403 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4404 XFS_ATTR_FORK : XFS_DATA_FORK;
4405 mp = ip->i_mount;
4406 if (unlikely(XFS_TEST_ERROR(
4407 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4408 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4409 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4410 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4411 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4412 return XFS_ERROR(EFSCORRUPTED);
4413 }
4414 if (XFS_FORCED_SHUTDOWN(mp))
4415 return XFS_ERROR(EIO);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004416 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 ifp = XFS_IFORK_PTR(ip, whichfork);
4418 ASSERT(ifp->if_ext_max ==
4419 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4420 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4421 XFS_STATS_INC(xs_blk_mapw);
4422 else
4423 XFS_STATS_INC(xs_blk_mapr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 /*
Nathan Scott39269e22006-03-14 13:33:50 +11004425 * IGSTATE flag is used to combine extents which
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 * differ only due to the state of the extents.
4427 * This technique is used from xfs_getbmap()
4428 * when the caller does not wish to see the
4429 * separation (which is the default).
4430 *
4431 * This technique is also used when writing a
4432 * buffer which has been partially written,
4433 * (usually by being flushed during a chunkread),
4434 * to ensure one write takes place. This also
4435 * prevents a change in the xfs inode extents at
4436 * this time, intentionally. This change occurs
4437 * on completion of the write operation, in
4438 * xfs_strat_comp(), where the xfs_bmapi() call
4439 * is transactioned, and the extents combined.
4440 */
Nathan Scott39269e22006-03-14 13:33:50 +11004441 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4442 wr = 0; /* no allocations are allowed */
4443 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 logflags = 0;
4445 nallocs = 0;
4446 cur = NULL;
4447 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4448 ASSERT(wr && tp);
4449 if ((error = xfs_bmap_local_to_extents(tp, ip,
4450 firstblock, total, &logflags, whichfork)))
4451 goto error0;
4452 }
4453 if (wr && *firstblock == NULLFSBLOCK) {
4454 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Christoph Hellwig16259e72005-11-02 15:11:25 +11004455 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 else
4457 minleft = 1;
4458 } else
4459 minleft = 0;
4460 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4461 (error = xfs_iread_extents(tp, ip, whichfork)))
4462 goto error0;
4463 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4464 &prev);
4465 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4466 n = 0;
4467 end = bno + len;
4468 obno = bno;
4469 bma.ip = NULL;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004470
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 while (bno < end && n < *nmap) {
4472 /*
4473 * Reading past eof, act as though there's a hole
4474 * up to end.
4475 */
4476 if (eof && !wr)
4477 got.br_startoff = end;
4478 inhole = eof || got.br_startoff > bno;
Nathan Scott39269e22006-03-14 13:33:50 +11004479 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06004480 isnullstartblock(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 /*
4482 * First, deal with the hole before the allocated space
4483 * that we found, if any.
4484 */
4485 if (wr && (inhole || wasdelay)) {
4486 /*
4487 * For the wasdelay case, we could also just
4488 * allocate the stuff asked for in this bmap call
4489 * but that wouldn't be as good.
4490 */
Christoph Hellwigcd8b0bb2010-06-23 18:11:15 +10004491 if (wasdelay) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 alen = (xfs_extlen_t)got.br_blockcount;
4493 aoff = got.br_startoff;
4494 if (lastx != NULLEXTNUM && lastx) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004495 ep = xfs_iext_get_ext(ifp, lastx - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 xfs_bmbt_get_all(ep, &prev);
4497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 } else {
4499 alen = (xfs_extlen_t)
4500 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4501 if (!eof)
4502 alen = (xfs_extlen_t)
4503 XFS_FILBLKS_MIN(alen,
4504 got.br_startoff - bno);
4505 aoff = bno;
4506 }
Nathan Scott39269e22006-03-14 13:33:50 +11004507 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4508 if (flags & XFS_BMAPI_DELAY) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11004509 xfs_extlen_t extsz;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004510
4511 /* Figure out the extent size, adjust alen */
David Chinner957d0eb2007-06-18 16:50:37 +10004512 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004513 if (extsz) {
Dave Chinner4ce15982011-01-27 12:17:58 +11004514 /*
4515 * make sure we don't exceed a single
4516 * extent length when we align the
4517 * extent by reducing length we are
4518 * going to allocate by the maximum
4519 * amount extent size aligment may
4520 * require.
4521 */
4522 alen = XFS_FILBLKS_MIN(len,
4523 MAXEXTLEN - (2 * extsz - 1));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004524 error = xfs_bmap_extsize_align(mp,
4525 &got, &prev, extsz,
Nathan Scott39269e22006-03-14 13:33:50 +11004526 rt, eof,
4527 flags&XFS_BMAPI_DELAY,
4528 flags&XFS_BMAPI_CONVERT,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004529 &aoff, &alen);
4530 ASSERT(!error);
4531 }
4532
4533 if (rt)
4534 extsz = alen / mp->m_sb.sb_rextsize;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004535
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 /*
4537 * Make a transaction-less quota reservation for
4538 * delayed allocation blocks. This number gets
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004539 * adjusted later. We return if we haven't
4540 * allocated blocks already inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02004542 error = xfs_trans_reserve_quota_nblks(
4543 NULL, ip, (long)alen, 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004544 rt ? XFS_QMOPT_RES_RTBLKS :
Christoph Hellwig7d095252009-06-08 15:33:32 +02004545 XFS_QMOPT_RES_REGBLKS);
4546 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 if (n == 0) {
4548 *nmap = 0;
4549 ASSERT(cur == NULL);
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004550 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 }
4552 break;
4553 }
4554
4555 /*
4556 * Split changing sb for alen and indlen since
4557 * they could be coming from different places.
4558 */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004559 indlen = (xfs_extlen_t)
4560 xfs_bmap_worst_indlen(ip, alen);
4561 ASSERT(indlen > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
Nathan Scottdd9f4382006-01-11 15:28:28 +11004563 if (rt) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004564 error = xfs_mod_incore_sb(mp,
4565 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004566 -((int64_t)extsz), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004567 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004568 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00004569 error = xfs_icsb_modify_counters(mp,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004570 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004571 -((int64_t)alen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004572 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004573 }
David Chinner3bdbfb12005-09-02 16:40:47 +10004574 if (!error) {
Christoph Hellwig96540c72010-09-30 02:25:55 +00004575 error = xfs_icsb_modify_counters(mp,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004576 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004577 -((int64_t)indlen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004578 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004579 if (error && rt)
4580 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004581 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004582 (int64_t)extsz, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004583 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004584 else if (error)
Christoph Hellwig96540c72010-09-30 02:25:55 +00004585 xfs_icsb_modify_counters(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004586 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004587 (int64_t)alen, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004588 XFS_BMAPI_RSVBLOCKS));
David Chinner3bdbfb12005-09-02 16:40:47 +10004589 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004590
4591 if (error) {
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004592 if (XFS_IS_QUOTA_ON(mp))
Nathan Scott06d10dd2005-06-21 15:48:47 +10004593 /* unreserve the blocks now */
Nathan Scottdd9f4382006-01-11 15:28:28 +11004594 (void)
Christoph Hellwig7d095252009-06-08 15:33:32 +02004595 xfs_trans_unreserve_quota_nblks(
4596 NULL, ip,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004597 (long)alen, 0, rt ?
4598 XFS_QMOPT_RES_RTBLKS :
4599 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 break;
4601 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004602
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 ip->i_delayed_blks += alen;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004604 abno = nullstartblock(indlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 } else {
4606 /*
4607 * If first time, allocate and fill in
4608 * once-only bma fields.
4609 */
4610 if (bma.ip == NULL) {
4611 bma.tp = tp;
4612 bma.ip = ip;
4613 bma.prevp = &prev;
4614 bma.gotp = &got;
4615 bma.total = total;
4616 bma.userdata = 0;
4617 }
4618 /* Indicate if this is the first user data
4619 * in the file, or just any user data.
4620 */
Nathan Scott39269e22006-03-14 13:33:50 +11004621 if (!(flags & XFS_BMAPI_METADATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 bma.userdata = (aoff == 0) ?
4623 XFS_ALLOC_INITIAL_USER_DATA :
4624 XFS_ALLOC_USERDATA;
4625 }
4626 /*
4627 * Fill in changeable bma fields.
4628 */
4629 bma.eof = eof;
4630 bma.firstblock = *firstblock;
4631 bma.alen = alen;
4632 bma.off = aoff;
Adrian Bunk72880262006-08-30 13:41:58 +10004633 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 bma.wasdel = wasdelay;
4635 bma.minlen = minlen;
4636 bma.low = flist->xbf_low;
4637 bma.minleft = minleft;
4638 /*
4639 * Only want to do the alignment at the
4640 * eof if it is userdata and allocation length
4641 * is larger than a stripe unit.
4642 */
4643 if (mp->m_dalign && alen >= mp->m_dalign &&
Nathan Scott39269e22006-03-14 13:33:50 +11004644 (!(flags & XFS_BMAPI_METADATA)) &&
4645 (whichfork == XFS_DATA_FORK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 if ((error = xfs_bmap_isaeof(ip, aoff,
4647 whichfork, &bma.aeof)))
4648 goto error0;
4649 } else
4650 bma.aeof = 0;
4651 /*
4652 * Call allocator.
4653 */
4654 if ((error = xfs_bmap_alloc(&bma)))
4655 goto error0;
4656 /*
4657 * Copy out result fields.
4658 */
4659 abno = bma.rval;
4660 if ((flist->xbf_low = bma.low))
4661 minleft = 0;
4662 alen = bma.alen;
4663 aoff = bma.off;
4664 ASSERT(*firstblock == NULLFSBLOCK ||
4665 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4666 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4667 (flist->xbf_low &&
4668 XFS_FSB_TO_AGNO(mp, *firstblock) <
4669 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4670 *firstblock = bma.firstblock;
4671 if (cur)
4672 cur->bc_private.b.firstblock =
4673 *firstblock;
4674 if (abno == NULLFSBLOCK)
4675 break;
4676 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11004677 cur = xfs_bmbt_init_cursor(mp, tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 ip, whichfork);
4679 cur->bc_private.b.firstblock =
4680 *firstblock;
4681 cur->bc_private.b.flist = flist;
4682 }
4683 /*
4684 * Bump the number of extents we've allocated
4685 * in this call.
4686 */
4687 nallocs++;
4688 }
4689 if (cur)
4690 cur->bc_private.b.flags =
4691 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4692 got.br_startoff = aoff;
4693 got.br_startblock = abno;
4694 got.br_blockcount = alen;
4695 got.br_state = XFS_EXT_NORM; /* assume normal */
4696 /*
4697 * Determine state of extent, and the filesystem.
4698 * A wasdelay extent has been initialized, so
4699 * shouldn't be flagged as unwritten.
4700 */
Eric Sandeen62118702008-03-06 13:44:28 +11004701 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
4703 got.br_state = XFS_EXT_UNWRITTEN;
4704 }
4705 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004706 firstblock, flist, &tmp_logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004707 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 logflags |= tmp_logflags;
4709 if (error)
4710 goto error0;
4711 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004712 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4714 xfs_bmbt_get_all(ep, &got);
4715 ASSERT(got.br_startoff <= aoff);
4716 ASSERT(got.br_startoff + got.br_blockcount >=
4717 aoff + alen);
4718#ifdef DEBUG
Nathan Scott39269e22006-03-14 13:33:50 +11004719 if (flags & XFS_BMAPI_DELAY) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06004720 ASSERT(isnullstartblock(got.br_startblock));
4721 ASSERT(startblockval(got.br_startblock) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 }
4723 ASSERT(got.br_state == XFS_EXT_NORM ||
4724 got.br_state == XFS_EXT_UNWRITTEN);
4725#endif
4726 /*
4727 * Fall down into the found allocated space case.
4728 */
4729 } else if (inhole) {
4730 /*
4731 * Reading in a hole.
4732 */
4733 mval->br_startoff = bno;
4734 mval->br_startblock = HOLESTARTBLOCK;
4735 mval->br_blockcount =
4736 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4737 mval->br_state = XFS_EXT_NORM;
4738 bno += mval->br_blockcount;
4739 len -= mval->br_blockcount;
4740 mval++;
4741 n++;
4742 continue;
4743 }
4744 /*
4745 * Then deal with the allocated space we found.
4746 */
4747 ASSERT(ep != NULL);
Nathan Scott39269e22006-03-14 13:33:50 +11004748 if (!(flags & XFS_BMAPI_ENTIRE) &&
4749 (got.br_startoff + got.br_blockcount > obno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 if (obno > bno)
4751 bno = obno;
4752 ASSERT((bno >= obno) || (n == 0));
4753 ASSERT(bno < end);
4754 mval->br_startoff = bno;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004755 if (isnullstartblock(got.br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11004756 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 mval->br_startblock = DELAYSTARTBLOCK;
4758 } else
4759 mval->br_startblock =
4760 got.br_startblock +
4761 (bno - got.br_startoff);
4762 /*
4763 * Return the minimum of what we got and what we
4764 * asked for for the length. We can use the len
4765 * variable here because it is modified below
4766 * and we could have been there before coming
4767 * here if the first part of the allocation
4768 * didn't overlap what was asked for.
4769 */
4770 mval->br_blockcount =
4771 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
4772 (bno - got.br_startoff));
4773 mval->br_state = got.br_state;
4774 ASSERT(mval->br_blockcount <= len);
4775 } else {
4776 *mval = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004777 if (isnullstartblock(mval->br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11004778 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 mval->br_startblock = DELAYSTARTBLOCK;
4780 }
4781 }
4782
4783 /*
4784 * Check if writing previously allocated but
4785 * unwritten extents.
4786 */
Dave Chinner44722352010-08-24 12:02:11 +10004787 if (wr &&
4788 ((mval->br_state == XFS_EXT_UNWRITTEN &&
4789 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) ||
4790 (mval->br_state == XFS_EXT_NORM &&
4791 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT)) ==
4792 (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 /*
4794 * Modify (by adding) the state flag, if writing.
4795 */
4796 ASSERT(mval->br_blockcount <= len);
4797 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11004798 cur = xfs_bmbt_init_cursor(mp,
4799 tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 cur->bc_private.b.firstblock =
4801 *firstblock;
4802 cur->bc_private.b.flist = flist;
4803 }
Dave Chinner44722352010-08-24 12:02:11 +10004804 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4805 ? XFS_EXT_NORM
4806 : XFS_EXT_UNWRITTEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004808 firstblock, flist, &tmp_logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004809 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 logflags |= tmp_logflags;
4811 if (error)
4812 goto error0;
4813 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004814 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4816 xfs_bmbt_get_all(ep, &got);
4817 /*
4818 * We may have combined previously unwritten
4819 * space with written space, so generate
4820 * another request.
4821 */
4822 if (mval->br_blockcount < len)
4823 continue;
4824 }
4825
Nathan Scott39269e22006-03-14 13:33:50 +11004826 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 ((mval->br_startoff + mval->br_blockcount) <= end));
Nathan Scott39269e22006-03-14 13:33:50 +11004828 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4829 (mval->br_blockcount <= len) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 (mval->br_startoff < obno));
4831 bno = mval->br_startoff + mval->br_blockcount;
4832 len = end - bno;
4833 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4834 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4835 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4836 ASSERT(mval->br_state == mval[-1].br_state);
4837 mval[-1].br_blockcount = mval->br_blockcount;
4838 mval[-1].br_state = mval->br_state;
4839 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4840 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4841 mval[-1].br_startblock != HOLESTARTBLOCK &&
4842 mval->br_startblock ==
4843 mval[-1].br_startblock + mval[-1].br_blockcount &&
Nathan Scott39269e22006-03-14 13:33:50 +11004844 ((flags & XFS_BMAPI_IGSTATE) ||
4845 mval[-1].br_state == mval->br_state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 ASSERT(mval->br_startoff ==
4847 mval[-1].br_startoff + mval[-1].br_blockcount);
4848 mval[-1].br_blockcount += mval->br_blockcount;
4849 } else if (n > 0 &&
4850 mval->br_startblock == DELAYSTARTBLOCK &&
4851 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4852 mval->br_startoff ==
4853 mval[-1].br_startoff + mval[-1].br_blockcount) {
4854 mval[-1].br_blockcount += mval->br_blockcount;
4855 mval[-1].br_state = mval->br_state;
4856 } else if (!((n == 0) &&
4857 ((mval->br_startoff + mval->br_blockcount) <=
4858 obno))) {
4859 mval++;
4860 n++;
4861 }
4862 /*
4863 * If we're done, stop now. Stop when we've allocated
4864 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4865 * the transaction may get too big.
4866 */
4867 if (bno >= end || n >= *nmap || nallocs >= *nmap)
4868 break;
4869 /*
4870 * Else go on to the next record.
4871 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004872 ep = xfs_iext_get_ext(ifp, ++lastx);
David Chinner4e5ae832007-06-05 16:24:15 +10004873 prev = got;
4874 if (lastx >= nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 eof = 1;
David Chinner4e5ae832007-06-05 16:24:15 +10004876 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 xfs_bmbt_get_all(ep, &got);
4878 }
4879 ifp->if_lastex = lastx;
4880 *nmap = n;
4881 /*
4882 * Transform from btree to extents, give it cur.
4883 */
4884 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4885 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4886 ASSERT(wr && cur);
4887 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4888 &tmp_logflags, whichfork);
4889 logflags |= tmp_logflags;
4890 if (error)
4891 goto error0;
4892 }
4893 ASSERT(ifp->if_ext_max ==
4894 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4895 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4896 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
4897 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898error0:
4899 /*
4900 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004901 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06004903 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06004905 logflags &= ~xfs_ilog_fext(whichfork);
4906 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06004908 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 /*
4910 * Log whatever the flags say, even if error. Otherwise we might miss
4911 * detecting a case where the data is changed, there's an error,
4912 * and it's not logged so we don't shutdown when we should.
4913 */
4914 if (logflags) {
4915 ASSERT(tp && wr);
4916 xfs_trans_log_inode(tp, ip, logflags);
4917 }
4918 if (cur) {
4919 if (!error) {
4920 ASSERT(*firstblock == NULLFSBLOCK ||
4921 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4922 XFS_FSB_TO_AGNO(mp,
4923 cur->bc_private.b.firstblock) ||
4924 (flist->xbf_low &&
4925 XFS_FSB_TO_AGNO(mp, *firstblock) <
4926 XFS_FSB_TO_AGNO(mp,
4927 cur->bc_private.b.firstblock)));
4928 *firstblock = cur->bc_private.b.firstblock;
4929 }
4930 xfs_btree_del_cursor(cur,
4931 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4932 }
4933 if (!error)
4934 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4935 orig_nmap, *nmap);
4936 return error;
4937}
4938
4939/*
4940 * Map file blocks to filesystem blocks, simple version.
4941 * One block (extent) only, read-only.
4942 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
4943 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
4944 * was set and all the others were clear.
4945 */
4946int /* error */
4947xfs_bmapi_single(
4948 xfs_trans_t *tp, /* transaction pointer */
4949 xfs_inode_t *ip, /* incore inode */
4950 int whichfork, /* data or attr fork */
4951 xfs_fsblock_t *fsb, /* output: mapped block */
4952 xfs_fileoff_t bno) /* starting file offs. mapped */
4953{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004954 int eof; /* we've hit the end of extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004956 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 xfs_ifork_t *ifp; /* inode fork pointer */
4958 xfs_extnum_t lastx; /* last useful extent number */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004959 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
4961 ifp = XFS_IFORK_PTR(ip, whichfork);
4962 if (unlikely(
4963 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4964 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
4965 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
4966 ip->i_mount);
4967 return XFS_ERROR(EFSCORRUPTED);
4968 }
4969 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
4970 return XFS_ERROR(EIO);
4971 XFS_STATS_INC(xs_blk_mapr);
4972 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4973 (error = xfs_iread_extents(tp, ip, whichfork)))
4974 return error;
4975 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4976 &prev);
4977 /*
4978 * Reading past eof, act as though there's a hole
4979 * up to end.
4980 */
4981 if (eof || got.br_startoff > bno) {
4982 *fsb = NULLFSBLOCK;
4983 return 0;
4984 }
Eric Sandeen9d87c312009-01-14 23:22:07 -06004985 ASSERT(!isnullstartblock(got.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 ASSERT(bno < got.br_startoff + got.br_blockcount);
4987 *fsb = got.br_startblock + (bno - got.br_startoff);
4988 ifp->if_lastex = lastx;
4989 return 0;
4990}
4991
4992/*
4993 * Unmap (remove) blocks from a file.
4994 * If nexts is nonzero then the number of extents to remove is limited to
4995 * that value. If not all extents in the block range can be removed then
4996 * *done is set.
4997 */
4998int /* error */
4999xfs_bunmapi(
5000 xfs_trans_t *tp, /* transaction pointer */
5001 struct xfs_inode *ip, /* incore inode */
5002 xfs_fileoff_t bno, /* starting offset to unmap */
5003 xfs_filblks_t len, /* length to unmap in file */
5004 int flags, /* misc flags */
5005 xfs_extnum_t nexts, /* number of extents max */
5006 xfs_fsblock_t *firstblock, /* first allocated block
5007 controls a.g. for allocs */
5008 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5009 int *done) /* set if not done yet */
5010{
5011 xfs_btree_cur_t *cur; /* bmap btree cursor */
5012 xfs_bmbt_irec_t del; /* extent being deleted */
5013 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005014 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 int error; /* error return value */
5016 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005017 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 xfs_ifork_t *ifp; /* inode fork pointer */
5019 int isrt; /* freeing in rt area */
5020 xfs_extnum_t lastx; /* last extent index used */
5021 int logflags; /* transaction logging flags */
5022 xfs_extlen_t mod; /* rt extent offset */
5023 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005024 xfs_extnum_t nextents; /* number of file extents */
5025 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 xfs_fileoff_t start; /* first file offset deleted */
5027 int tmp_logflags; /* partial logging flags */
5028 int wasdel; /* was a delayed alloc extent */
5029 int whichfork; /* data or attribute fork */
5030 int rsvd; /* OK to allocate reserved blocks */
5031 xfs_fsblock_t sum;
5032
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005033 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5034
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5036 XFS_ATTR_FORK : XFS_DATA_FORK;
5037 ifp = XFS_IFORK_PTR(ip, whichfork);
5038 if (unlikely(
5039 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5040 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5041 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5042 ip->i_mount);
5043 return XFS_ERROR(EFSCORRUPTED);
5044 }
5045 mp = ip->i_mount;
5046 if (XFS_FORCED_SHUTDOWN(mp))
5047 return XFS_ERROR(EIO);
5048 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5049 ASSERT(len > 0);
5050 ASSERT(nexts >= 0);
5051 ASSERT(ifp->if_ext_max ==
5052 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5053 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5054 (error = xfs_iread_extents(tp, ip, whichfork)))
5055 return error;
5056 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5057 if (nextents == 0) {
5058 *done = 1;
5059 return 0;
5060 }
5061 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005062 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 start = bno;
5064 bno = start + len - 1;
5065 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5066 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005067
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 /*
5069 * Check to see if the given block number is past the end of the
5070 * file, back up to the last block if so...
5071 */
5072 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005073 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 xfs_bmbt_get_all(ep, &got);
5075 bno = got.br_startoff + got.br_blockcount - 1;
5076 }
5077 logflags = 0;
5078 if (ifp->if_flags & XFS_IFBROOT) {
5079 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005080 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 cur->bc_private.b.firstblock = *firstblock;
5082 cur->bc_private.b.flist = flist;
5083 cur->bc_private.b.flags = 0;
5084 } else
5085 cur = NULL;
5086 extno = 0;
5087 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5088 (nexts == 0 || extno < nexts)) {
5089 /*
5090 * Is the found extent after a hole in which bno lives?
5091 * Just back up to the previous extent, if so.
5092 */
5093 if (got.br_startoff > bno) {
5094 if (--lastx < 0)
5095 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005096 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 xfs_bmbt_get_all(ep, &got);
5098 }
5099 /*
5100 * Is the last block of this extent before the range
5101 * we're supposed to delete? If so, we're done.
5102 */
5103 bno = XFS_FILEOFF_MIN(bno,
5104 got.br_startoff + got.br_blockcount - 1);
5105 if (bno < start)
5106 break;
5107 /*
5108 * Then deal with the (possibly delayed) allocated space
5109 * we found.
5110 */
5111 ASSERT(ep != NULL);
5112 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005113 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 if (got.br_startoff < start) {
5115 del.br_startoff = start;
5116 del.br_blockcount -= start - got.br_startoff;
5117 if (!wasdel)
5118 del.br_startblock += start - got.br_startoff;
5119 }
5120 if (del.br_startoff + del.br_blockcount > bno + 1)
5121 del.br_blockcount = bno + 1 - del.br_startoff;
5122 sum = del.br_startblock + del.br_blockcount;
5123 if (isrt &&
5124 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5125 /*
5126 * Realtime extent not lined up at the end.
5127 * The extent could have been split into written
5128 * and unwritten pieces, or we could just be
5129 * unmapping part of it. But we can't really
5130 * get rid of part of a realtime extent.
5131 */
5132 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005133 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 /*
5135 * This piece is unwritten, or we're not
5136 * using unwritten extents. Skip over it.
5137 */
5138 ASSERT(bno >= mod);
5139 bno -= mod > del.br_blockcount ?
5140 del.br_blockcount : mod;
5141 if (bno < got.br_startoff) {
5142 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005143 xfs_bmbt_get_all(xfs_iext_get_ext(
5144 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 }
5146 continue;
5147 }
5148 /*
5149 * It's written, turn it unwritten.
5150 * This is better than zeroing it.
5151 */
5152 ASSERT(del.br_state == XFS_EXT_NORM);
5153 ASSERT(xfs_trans_get_block_res(tp) > 0);
5154 /*
5155 * If this spans a realtime extent boundary,
5156 * chop it back to the start of the one we end at.
5157 */
5158 if (del.br_blockcount > mod) {
5159 del.br_startoff += del.br_blockcount - mod;
5160 del.br_startblock += del.br_blockcount - mod;
5161 del.br_blockcount = mod;
5162 }
5163 del.br_state = XFS_EXT_UNWRITTEN;
5164 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005165 firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005166 XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 if (error)
5168 goto error0;
5169 goto nodelete;
5170 }
5171 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5172 /*
5173 * Realtime extent is lined up at the end but not
5174 * at the front. We'll get rid of full extents if
5175 * we can.
5176 */
5177 mod = mp->m_sb.sb_rextsize - mod;
5178 if (del.br_blockcount > mod) {
5179 del.br_blockcount -= mod;
5180 del.br_startoff += mod;
5181 del.br_startblock += mod;
5182 } else if ((del.br_startoff == start &&
5183 (del.br_state == XFS_EXT_UNWRITTEN ||
5184 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005185 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 /*
5187 * Can't make it unwritten. There isn't
5188 * a full extent here so just skip it.
5189 */
5190 ASSERT(bno >= del.br_blockcount);
5191 bno -= del.br_blockcount;
5192 if (bno < got.br_startoff) {
5193 if (--lastx >= 0)
5194 xfs_bmbt_get_all(--ep, &got);
5195 }
5196 continue;
5197 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5198 /*
5199 * This one is already unwritten.
5200 * It must have a written left neighbor.
5201 * Unwrite the killed part of that one and
5202 * try again.
5203 */
5204 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005205 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5206 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005208 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 ASSERT(del.br_startblock ==
5210 prev.br_startblock + prev.br_blockcount);
5211 if (prev.br_startoff < start) {
5212 mod = start - prev.br_startoff;
5213 prev.br_blockcount -= mod;
5214 prev.br_startblock += mod;
5215 prev.br_startoff = start;
5216 }
5217 prev.br_state = XFS_EXT_UNWRITTEN;
5218 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5219 &prev, firstblock, flist, &logflags,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005220 XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 if (error)
5222 goto error0;
5223 goto nodelete;
5224 } else {
5225 ASSERT(del.br_state == XFS_EXT_NORM);
5226 del.br_state = XFS_EXT_UNWRITTEN;
5227 error = xfs_bmap_add_extent(ip, lastx, &cur,
5228 &del, firstblock, flist, &logflags,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005229 XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 if (error)
5231 goto error0;
5232 goto nodelete;
5233 }
5234 }
5235 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005236 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005237 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005238 if (isrt) {
5239 xfs_filblks_t rtexts;
5240
5241 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5242 do_div(rtexts, mp->m_sb.sb_rextsize);
5243 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005244 (int64_t)rtexts, rsvd);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005245 (void)xfs_trans_reserve_quota_nblks(NULL,
5246 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005247 XFS_QMOPT_RES_RTBLKS);
5248 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00005249 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005250 (int64_t)del.br_blockcount, rsvd);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005251 (void)xfs_trans_reserve_quota_nblks(NULL,
5252 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 ip->i_delayed_blks -= del.br_blockcount;
5256 if (cur)
5257 cur->bc_private.b.flags |=
5258 XFS_BTCUR_BPRV_WASDEL;
5259 } else if (cur)
5260 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5261 /*
5262 * If it's the case where the directory code is running
5263 * with no block reservation, and the deleted block is in
5264 * the middle of its extent, and the resulting insert
5265 * of an extent would cause transformation to btree format,
5266 * then reject it. The calling code will then swap
5267 * blocks around instead.
5268 * We have to do this now, rather than waiting for the
5269 * conversion to btree format, since the transaction
5270 * will be dirty.
5271 */
5272 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5273 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5274 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5275 del.br_startoff > got.br_startoff &&
5276 del.br_startoff + del.br_blockcount <
5277 got.br_startoff + got.br_blockcount) {
5278 error = XFS_ERROR(ENOSPC);
5279 goto error0;
5280 }
5281 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005282 &tmp_logflags, whichfork, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 logflags |= tmp_logflags;
5284 if (error)
5285 goto error0;
5286 bno = del.br_startoff - 1;
5287nodelete:
5288 lastx = ifp->if_lastex;
5289 /*
5290 * If not done go on to the next (previous) record.
5291 * Reset ep in case the extents array was re-alloced.
5292 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005293 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5295 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5296 xfs_bmbt_get_startoff(ep) > bno) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005297 if (--lastx >= 0)
5298 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 }
5300 if (lastx >= 0)
5301 xfs_bmbt_get_all(ep, &got);
5302 extno++;
5303 }
5304 }
5305 ifp->if_lastex = lastx;
5306 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5307 ASSERT(ifp->if_ext_max ==
5308 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5309 /*
5310 * Convert to a btree if necessary.
5311 */
5312 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5313 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5314 ASSERT(cur == NULL);
5315 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5316 &cur, 0, &tmp_logflags, whichfork);
5317 logflags |= tmp_logflags;
5318 if (error)
5319 goto error0;
5320 }
5321 /*
5322 * transform from btree to extents, give it cur
5323 */
5324 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5325 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5326 ASSERT(cur != NULL);
5327 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5328 whichfork);
5329 logflags |= tmp_logflags;
5330 if (error)
5331 goto error0;
5332 }
5333 /*
5334 * transform from extents to local?
5335 */
5336 ASSERT(ifp->if_ext_max ==
5337 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5338 error = 0;
5339error0:
5340 /*
5341 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005342 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005344 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005346 logflags &= ~xfs_ilog_fext(whichfork);
5347 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005349 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 /*
5351 * Log inode even in the error case, if the transaction
5352 * is dirty we'll need to shut down the filesystem.
5353 */
5354 if (logflags)
5355 xfs_trans_log_inode(tp, ip, logflags);
5356 if (cur) {
5357 if (!error) {
5358 *firstblock = cur->bc_private.b.firstblock;
5359 cur->bc_private.b.allocated = 0;
5360 }
5361 xfs_btree_del_cursor(cur,
5362 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5363 }
5364 return error;
5365}
5366
5367/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005368 * returns 1 for success, 0 if we failed to map the extent.
5369 */
5370STATIC int
5371xfs_getbmapx_fix_eof_hole(
5372 xfs_inode_t *ip, /* xfs incore inode pointer */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005373 struct getbmapx *out, /* output structure */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005374 int prealloced, /* this is a file with
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005375 * preallocated data space */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005376 __int64_t end, /* last block requested */
5377 xfs_fsblock_t startblock)
5378{
5379 __int64_t fixlen;
5380 xfs_mount_t *mp; /* file system mount point */
Eric Sandeen5af317c2008-11-28 14:23:35 +11005381 xfs_ifork_t *ifp; /* inode fork pointer */
5382 xfs_extnum_t lastx; /* last extent pointer */
5383 xfs_fileoff_t fileblock;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005384
5385 if (startblock == HOLESTARTBLOCK) {
5386 mp = ip->i_mount;
5387 out->bmv_block = -1;
5388 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5389 fixlen -= out->bmv_offset;
5390 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5391 /* Came to hole at EOF. Trim it. */
5392 if (fixlen <= 0)
5393 return 0;
5394 out->bmv_length = fixlen;
5395 }
5396 } else {
Eric Sandeen5af317c2008-11-28 14:23:35 +11005397 if (startblock == DELAYSTARTBLOCK)
5398 out->bmv_block = -2;
5399 else
Eric Sandeen9d87c312009-01-14 23:22:07 -06005400 out->bmv_block = xfs_fsb_to_db(ip, startblock);
Eric Sandeen5af317c2008-11-28 14:23:35 +11005401 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5402 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5403 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5404 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5405 out->bmv_oflags |= BMV_OF_LAST;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005406 }
5407
5408 return 1;
5409}
5410
5411/*
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005412 * Get inode's extents as described in bmv, and format for output.
5413 * Calls formatter to fill the user's buffer until all extents
5414 * are mapped, until the passed-in bmv->bmv_count slots have
5415 * been filled, or until the formatter short-circuits the loop,
5416 * if it is tracking filled-in extents on its own.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 */
5418int /* error code */
5419xfs_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10005420 xfs_inode_t *ip,
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005421 struct getbmapx *bmv, /* user bmap structure */
5422 xfs_bmap_format_t formatter, /* format to user */
5423 void *arg) /* formatter arg */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424{
5425 __int64_t bmvend; /* last block requested */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005426 int error = 0; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 __int64_t fixlen; /* length for -1 case */
5428 int i; /* extent number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 int lock; /* lock state */
5430 xfs_bmbt_irec_t *map; /* buffer for user's data */
5431 xfs_mount_t *mp; /* file system mount point */
5432 int nex; /* # of user extents can do */
5433 int nexleft; /* # of user extents left */
5434 int subnex; /* # of bmapi's can do */
5435 int nmap; /* number of map entries */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005436 struct getbmapx *out; /* output structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 int whichfork; /* data or attr fork */
5438 int prealloced; /* this is a file with
5439 * preallocated data space */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005440 int iflags; /* interface flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 int bmapi_flags; /* flags for xfs_bmapi */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005442 int cur_ext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 mp = ip->i_mount;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005445 iflags = bmv->bmv_iflags;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005446 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 if (whichfork == XFS_ATTR_FORK) {
5449 if (XFS_IFORK_Q(ip)) {
5450 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5451 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5452 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5453 return XFS_ERROR(EINVAL);
5454 } else if (unlikely(
5455 ip->i_d.di_aformat != 0 &&
5456 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5457 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5458 ip->i_mount);
5459 return XFS_ERROR(EFSCORRUPTED);
5460 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005461
5462 prealloced = 0;
5463 fixlen = 1LL << 32;
5464 } else {
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005465 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5466 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5467 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5468 return XFS_ERROR(EINVAL);
5469
David Chinner957d0eb2007-06-18 16:50:37 +10005470 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005471 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 prealloced = 1;
5473 fixlen = XFS_MAXIOFFSET(mp);
5474 } else {
5475 prealloced = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005476 fixlen = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 }
5479
5480 if (bmv->bmv_length == -1) {
5481 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005482 bmv->bmv_length =
5483 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5484 } else if (bmv->bmv_length == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 bmv->bmv_entries = 0;
5486 return 0;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005487 } else if (bmv->bmv_length < 0) {
5488 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005490
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 nex = bmv->bmv_count - 1;
5492 if (nex <= 0)
5493 return XFS_ERROR(EINVAL);
5494 bmvend = bmv->bmv_offset + bmv->bmv_length;
5495
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005496
5497 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5498 return XFS_ERROR(ENOMEM);
5499 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5500 if (!out)
5501 return XFS_ERROR(ENOMEM);
5502
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005504 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5505 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5506 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5507 if (error)
5508 goto out_unlock_iolock;
Niv Sardie12070a2008-03-06 13:43:03 +11005509 }
Dave Chinner309c8482010-11-30 15:16:02 +11005510 /*
5511 * even after flushing the inode, there can still be delalloc
5512 * blocks on the inode beyond EOF due to speculative
5513 * preallocation. These are not removed until the release
5514 * function is called or the inode is inactivated. Hence we
5515 * cannot assert here that ip->i_delayed_blks == 0.
5516 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518
5519 lock = xfs_ilock_map_shared(ip);
5520
5521 /*
5522 * Don't let nex be bigger than the number of extents
5523 * we can have assuming alternating holes and real extents.
5524 */
5525 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5526 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5527
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005528 bmapi_flags = xfs_bmapi_aflag(whichfork);
5529 if (!(iflags & BMV_IF_PREALLOC))
5530 bmapi_flags |= XFS_BMAPI_IGSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531
5532 /*
5533 * Allocate enough space to handle "subnex" maps at a time.
5534 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005535 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 subnex = 16;
Christoph Hellwigca35dcd2009-07-18 18:14:54 -04005537 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005538 if (!map)
5539 goto out_unlock_ilock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
5541 bmv->bmv_entries = 0;
5542
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005543 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5544 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5545 error = 0;
5546 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 }
5548
5549 nexleft = nex;
5550
5551 do {
5552 nmap = (nexleft > subnex) ? subnex : nexleft;
5553 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5554 XFS_BB_TO_FSB(mp, bmv->bmv_length),
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005555 bmapi_flags, NULL, 0, map, &nmap,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005556 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 if (error)
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005558 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 ASSERT(nmap <= subnex);
5560
5561 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005562 out[cur_ext].bmv_oflags = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005563 if (map[i].br_state == XFS_EXT_UNWRITTEN)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005564 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005565 else if (map[i].br_startblock == DELAYSTARTBLOCK)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005566 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5567 out[cur_ext].bmv_offset =
5568 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5569 out[cur_ext].bmv_length =
5570 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5571 out[cur_ext].bmv_unused1 = 0;
5572 out[cur_ext].bmv_unused2 = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005573 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5574 (map[i].br_startblock != DELAYSTARTBLOCK));
Yingping Lu9af0a702005-11-02 15:09:54 +11005575 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005576 whichfork == XFS_ATTR_FORK) {
5577 /* came to the end of attribute fork */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005578 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005579 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005581
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005582 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5583 prealloced, bmvend,
5584 map[i].br_startblock))
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005585 goto out_free_map;
5586
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005587 bmv->bmv_offset =
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005588 out[cur_ext].bmv_offset +
5589 out[cur_ext].bmv_length;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005590 bmv->bmv_length =
5591 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
Tao Ma9af25462010-08-30 02:44:03 +00005592
5593 /*
5594 * In case we don't want to return the hole,
5595 * don't increase cur_ext so that we can reuse
5596 * it in the next loop.
5597 */
5598 if ((iflags & BMV_IF_NO_HOLES) &&
5599 map[i].br_startblock == HOLESTARTBLOCK) {
5600 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5601 continue;
5602 }
5603
5604 nexleft--;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005605 bmv->bmv_entries++;
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005606 cur_ext++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 }
5608 } while (nmap && nexleft && bmv->bmv_length);
5609
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005610 out_free_map:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10005611 kmem_free(map);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005612 out_unlock_ilock:
5613 xfs_iunlock_map_shared(ip, lock);
5614 out_unlock_iolock:
5615 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005616
5617 for (i = 0; i < cur_ext; i++) {
5618 int full = 0; /* user array is full */
5619
5620 /* format results & advance arg */
5621 error = formatter(&arg, &out[i], &full);
5622 if (error || full)
5623 break;
5624 }
5625
Felix Blyakher7747a0b2009-06-11 17:07:28 -05005626 kmem_free(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 return error;
5628}
5629
5630/*
5631 * Check the last inode extent to determine whether this allocation will result
5632 * in blocks being allocated at the end of the file. When we allocate new data
5633 * blocks at the end of the file which do not start at the previous data block,
5634 * we will try to align the new blocks at stripe unit boundaries.
5635 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10005636STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637xfs_bmap_isaeof(
5638 xfs_inode_t *ip, /* incore inode pointer */
5639 xfs_fileoff_t off, /* file offset in fsblocks */
5640 int whichfork, /* data or attribute fork */
5641 char *aeof) /* return value */
5642{
5643 int error; /* error return value */
5644 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005645 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005646 xfs_extnum_t nextents; /* number of file extents */
5647 xfs_bmbt_irec_t s; /* expanded extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648
5649 ASSERT(whichfork == XFS_DATA_FORK);
5650 ifp = XFS_IFORK_PTR(ip, whichfork);
5651 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5652 (error = xfs_iread_extents(NULL, ip, whichfork)))
5653 return error;
5654 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5655 if (nextents == 0) {
5656 *aeof = 1;
5657 return 0;
5658 }
5659 /*
5660 * Go to the last extent
5661 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005662 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 xfs_bmbt_get_all(lastrec, &s);
5664 /*
5665 * Check we are allocating in the last extent (for delayed allocations)
5666 * or past the last extent for non-delayed allocations.
5667 */
5668 *aeof = (off >= s.br_startoff &&
5669 off < s.br_startoff + s.br_blockcount &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06005670 isnullstartblock(s.br_startblock)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 off >= s.br_startoff + s.br_blockcount;
5672 return 0;
5673}
5674
5675/*
5676 * Check if the endoff is outside the last extent. If so the caller will grow
5677 * the allocation to a stripe unit boundary.
5678 */
5679int /* error */
5680xfs_bmap_eof(
5681 xfs_inode_t *ip, /* incore inode pointer */
5682 xfs_fileoff_t endoff, /* file offset in fsblocks */
5683 int whichfork, /* data or attribute fork */
5684 int *eof) /* result value */
5685{
5686 xfs_fsblock_t blockcount; /* extent block count */
5687 int error; /* error return value */
5688 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005689 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005690 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 xfs_fileoff_t startoff; /* extent starting file offset */
5692
5693 ASSERT(whichfork == XFS_DATA_FORK);
5694 ifp = XFS_IFORK_PTR(ip, whichfork);
5695 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5696 (error = xfs_iread_extents(NULL, ip, whichfork)))
5697 return error;
5698 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5699 if (nextents == 0) {
5700 *eof = 1;
5701 return 0;
5702 }
5703 /*
5704 * Go to the last extent
5705 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005706 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 startoff = xfs_bmbt_get_startoff(lastrec);
5708 blockcount = xfs_bmbt_get_blockcount(lastrec);
5709 *eof = endoff >= startoff + blockcount;
5710 return 0;
5711}
5712
5713#ifdef DEBUG
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005714STATIC struct xfs_buf *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715xfs_bmap_get_bp(
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005716 struct xfs_btree_cur *cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 xfs_fsblock_t bno)
5718{
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005719 struct xfs_log_item_desc *lidp;
5720 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721
5722 if (!cur)
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005723 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005725 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5726 if (!cur->bc_bufs[i])
5727 break;
5728 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5729 return cur->bc_bufs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005732 /* Chase down all the log items to see if the bp is there */
5733 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
Christoph Hellwige98c4142010-06-23 18:11:15 +10005734 struct xfs_buf_log_item *bip;
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005735 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5736 if (bip->bli_item.li_type == XFS_LI_BUF &&
5737 XFS_BUF_ADDR(bip->bli_buf) == bno)
5738 return bip->bli_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 }
Christoph Hellwige98c4142010-06-23 18:11:15 +10005740
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005741 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742}
5743
Hannes Eder3180e662009-03-04 19:34:10 +01005744STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745xfs_check_block(
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005746 struct xfs_btree_block *block,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 xfs_mount_t *mp,
5748 int root,
5749 short sz)
5750{
5751 int i, j, dmxr;
Christoph Hellwig576039c2006-09-28 10:58:06 +10005752 __be64 *pp, *thispa; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 xfs_bmbt_key_t *prevp, *keyp;
5754
Christoph Hellwig16259e72005-11-02 15:11:25 +11005755 ASSERT(be16_to_cpu(block->bb_level) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756
5757 prevp = NULL;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005758 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 dmxr = mp->m_bmap_dmxr[0];
Christoph Hellwig136341b2008-10-30 17:11:40 +11005760 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761
5762 if (prevp) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005763 ASSERT(be64_to_cpu(prevp->br_startoff) <
5764 be64_to_cpu(keyp->br_startoff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 }
5766 prevp = keyp;
5767
5768 /*
5769 * Compare the block numbers to see if there are dups.
5770 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11005771 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005772 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005773 else
5774 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5775
Christoph Hellwig16259e72005-11-02 15:11:25 +11005776 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11005777 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005778 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005779 else
5780 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005781 if (*thispa == *pp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005783 __func__, j, i,
Christoph Hellwig576039c2006-09-28 10:58:06 +10005784 (unsigned long long)be64_to_cpu(*thispa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 panic("%s: ptrs are equal in node\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005786 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 }
5788 }
5789 }
5790}
5791
5792/*
5793 * Check that the extents for the inode ip are in the right order in all
5794 * btree leaves.
5795 */
5796
5797STATIC void
5798xfs_bmap_check_leaf_extents(
5799 xfs_btree_cur_t *cur, /* btree cursor or null */
5800 xfs_inode_t *ip, /* incore inode pointer */
5801 int whichfork) /* data or attr fork */
5802{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005803 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 xfs_fsblock_t bno; /* block # of "block" */
5805 xfs_buf_t *bp; /* buffer for "block" */
5806 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005807 xfs_extnum_t i=0, j; /* index into the extents list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 xfs_ifork_t *ifp; /* fork structure */
5809 int level; /* btree level, for checking */
5810 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10005811 __be64 *pp; /* pointer to block address */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005812 xfs_bmbt_rec_t *ep; /* pointer to current extent */
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005813 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005814 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 int bp_release = 0;
5816
5817 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5818 return;
5819 }
5820
5821 bno = NULLFSBLOCK;
5822 mp = ip->i_mount;
5823 ifp = XFS_IFORK_PTR(ip, whichfork);
5824 block = ifp->if_broot;
5825 /*
5826 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5827 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11005828 level = be16_to_cpu(block->bb_level);
5829 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11005831 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005832 bno = be64_to_cpu(*pp);
5833
5834 ASSERT(bno != NULLDFSBNO);
5835 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5836 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5837
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 /*
5839 * Go down the tree until leaf level is reached, following the first
5840 * pointer (leftmost) at each level.
5841 */
5842 while (level-- > 0) {
5843 /* See if buf is in cur first */
5844 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5845 if (bp) {
5846 bp_release = 0;
5847 } else {
5848 bp_release = 1;
5849 }
5850 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5851 XFS_BMAP_BTREE_REF)))
5852 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005853 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11005855 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 error0);
5857 if (level == 0)
5858 break;
5859
5860 /*
5861 * Check this block for basic sanity (increasing keys and
5862 * no duplicate blocks).
5863 */
5864
5865 xfs_check_block(block, mp, 0, 0);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005866 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005867 bno = be64_to_cpu(*pp);
5868 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 if (bp_release) {
5870 bp_release = 0;
5871 xfs_trans_brelse(NULL, bp);
5872 }
5873 }
5874
5875 /*
5876 * Here with bp and block set to the leftmost leaf node in the tree.
5877 */
5878 i = 0;
5879
5880 /*
5881 * Loop over all leaf nodes checking that all extents are in the right order.
5882 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 xfs_fsblock_t nextbno;
5885 xfs_extnum_t num_recs;
5886
5887
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005888 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889
5890 /*
5891 * Read-ahead the next leaf block, if any.
5892 */
5893
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005894 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895
5896 /*
5897 * Check all the extents to make sure they are OK.
5898 * If we had a previous block, the last entry should
5899 * conform with the first entry in this one.
5900 */
5901
Christoph Hellwig136341b2008-10-30 17:11:40 +11005902 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005903 if (i) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005904 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
5905 xfs_bmbt_disk_get_blockcount(&last) <=
5906 xfs_bmbt_disk_get_startoff(ep));
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005907 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005908 for (j = 1; j < num_recs; j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11005909 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005910 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
5911 xfs_bmbt_disk_get_blockcount(ep) <=
5912 xfs_bmbt_disk_get_startoff(nextp));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005913 ep = nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005916 last = *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 i += num_recs;
5918 if (bp_release) {
5919 bp_release = 0;
5920 xfs_trans_brelse(NULL, bp);
5921 }
5922 bno = nextbno;
5923 /*
5924 * If we've reached the end, stop.
5925 */
5926 if (bno == NULLFSBLOCK)
5927 break;
5928
5929 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5930 if (bp) {
5931 bp_release = 0;
5932 } else {
5933 bp_release = 1;
5934 }
5935 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5936 XFS_BMAP_BTREE_REF)))
5937 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005938 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 }
5940 if (bp_release) {
5941 bp_release = 0;
5942 xfs_trans_brelse(NULL, bp);
5943 }
5944 return;
5945
5946error0:
Harvey Harrison34a622b2008-04-10 12:19:21 +10005947 cmn_err(CE_WARN, "%s: at error0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 if (bp_release)
5949 xfs_trans_brelse(NULL, bp);
5950error_norelse:
5951 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005952 __func__, i);
5953 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 return;
5955}
5956#endif
5957
5958/*
5959 * Count fsblocks of the given fork.
5960 */
5961int /* error */
5962xfs_bmap_count_blocks(
5963 xfs_trans_t *tp, /* transaction pointer */
5964 xfs_inode_t *ip, /* incore inode */
5965 int whichfork, /* data or attr fork */
5966 int *count) /* out: count of blocks */
5967{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005968 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 xfs_fsblock_t bno; /* block # of "block" */
5970 xfs_ifork_t *ifp; /* fork structure */
5971 int level; /* btree level, for checking */
5972 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10005973 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974
5975 bno = NULLFSBLOCK;
5976 mp = ip->i_mount;
5977 ifp = XFS_IFORK_PTR(ip, whichfork);
5978 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
Ruben Porrasc94312d2008-08-13 16:52:25 +10005979 xfs_bmap_count_leaves(ifp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
Ruben Porrasc94312d2008-08-13 16:52:25 +10005981 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 return 0;
5983 }
5984
5985 /*
5986 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5987 */
5988 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11005989 level = be16_to_cpu(block->bb_level);
5990 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11005991 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005992 bno = be64_to_cpu(*pp);
5993 ASSERT(bno != NULLDFSBNO);
5994 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5995 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005997 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
5999 mp);
6000 return XFS_ERROR(EFSCORRUPTED);
6001 }
6002
6003 return 0;
6004}
6005
6006/*
6007 * Recursively walks each level of a btree
6008 * to count total fsblocks is use.
6009 */
David Chinnera8272ce2007-11-23 16:28:09 +11006010STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011xfs_bmap_count_tree(
6012 xfs_mount_t *mp, /* file system mount point */
6013 xfs_trans_t *tp, /* transaction pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006014 xfs_ifork_t *ifp, /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 xfs_fsblock_t blockno, /* file system block number */
6016 int levelin, /* level in btree */
6017 int *count) /* Count of blocks */
6018{
6019 int error;
6020 xfs_buf_t *bp, *nbp;
6021 int level = levelin;
Christoph Hellwig576039c2006-09-28 10:58:06 +10006022 __be64 *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 xfs_fsblock_t bno = blockno;
6024 xfs_fsblock_t nextbno;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006025 struct xfs_btree_block *block, *nextblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
6028 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6029 return error;
6030 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006031 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032
6033 if (--level) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +02006034 /* Not at node above leaves, count this level of nodes */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006035 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 while (nextbno != NULLFSBLOCK) {
6037 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6038 0, &nbp, XFS_BMAP_BTREE_REF)))
6039 return error;
6040 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006041 nextblock = XFS_BUF_TO_BLOCK(nbp);
6042 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 xfs_trans_brelse(tp, nbp);
6044 }
6045
6046 /* Dive to the next level */
Christoph Hellwig136341b2008-10-30 17:11:40 +11006047 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006048 bno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 if (unlikely((error =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006050 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 xfs_trans_brelse(tp, bp);
6052 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6053 XFS_ERRLEVEL_LOW, mp);
6054 return XFS_ERROR(EFSCORRUPTED);
6055 }
6056 xfs_trans_brelse(tp, bp);
6057 } else {
6058 /* count all level 1 nodes and their leaves */
6059 for (;;) {
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006060 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Christoph Hellwig16259e72005-11-02 15:11:25 +11006061 numrecs = be16_to_cpu(block->bb_numrecs);
Christoph Hellwig136341b2008-10-30 17:11:40 +11006062 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 xfs_trans_brelse(tp, bp);
6064 if (nextbno == NULLFSBLOCK)
6065 break;
6066 bno = nextbno;
6067 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6068 XFS_BMAP_BTREE_REF)))
6069 return error;
6070 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006071 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 }
6073 }
6074 return 0;
6075}
6076
6077/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006078 * Count leaf blocks given a range of extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006080STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006082 xfs_ifork_t *ifp,
6083 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 int numrecs,
6085 int *count)
6086{
6087 int b;
6088
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006089 for (b = 0; b < numrecs; b++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006090 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
Yingping Lu91e11082005-11-02 15:10:24 +11006091 *count += xfs_bmbt_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006092 }
Yingping Lu91e11082005-11-02 15:10:24 +11006093}
6094
6095/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006096 * Count leaf blocks given a range of extent records originally
6097 * in btree format.
Yingping Lu91e11082005-11-02 15:10:24 +11006098 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006099STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +11006100xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +11006101 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006102 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +11006103 int numrecs,
6104 int *count)
6105{
6106 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006107 xfs_bmbt_rec_t *frp;
Yingping Lu91e11082005-11-02 15:10:24 +11006108
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006109 for (b = 1; b <= numrecs; b++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11006110 frp = XFS_BMBT_REC_ADDR(mp, block, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 *count += xfs_bmbt_disk_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113}
Dave Chinnerc726de42010-11-30 15:14:39 +11006114
6115/*
6116 * dead simple method of punching delalyed allocation blocks from a range in
6117 * the inode. Walks a block at a time so will be slow, but is only executed in
6118 * rare error cases so the overhead is not critical. This will alays punch out
6119 * both the start and end blocks, even if the ranges only partially overlap
6120 * them, so it is up to the caller to ensure that partial blocks are not
6121 * passed in.
6122 */
6123int
6124xfs_bmap_punch_delalloc_range(
6125 struct xfs_inode *ip,
6126 xfs_fileoff_t start_fsb,
6127 xfs_fileoff_t length)
6128{
6129 xfs_fileoff_t remaining = length;
6130 int error = 0;
6131
6132 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6133
6134 do {
6135 int done;
6136 xfs_bmbt_irec_t imap;
6137 int nimaps = 1;
6138 xfs_fsblock_t firstblock;
6139 xfs_bmap_free_t flist;
6140
6141 /*
6142 * Map the range first and check that it is a delalloc extent
6143 * before trying to unmap the range. Otherwise we will be
6144 * trying to remove a real extent (which requires a
6145 * transaction) or a hole, which is probably a bad idea...
6146 */
6147 error = xfs_bmapi(NULL, ip, start_fsb, 1,
6148 XFS_BMAPI_ENTIRE, NULL, 0, &imap,
6149 &nimaps, NULL);
6150
6151 if (error) {
6152 /* something screwed, just bail */
6153 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
6154 xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
6155 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6156 ip->i_ino, start_fsb);
6157 }
6158 break;
6159 }
6160 if (!nimaps) {
6161 /* nothing there */
6162 goto next_block;
6163 }
6164 if (imap.br_startblock != DELAYSTARTBLOCK) {
6165 /* been converted, ignore */
6166 goto next_block;
6167 }
6168 WARN_ON(imap.br_blockcount == 0);
6169
6170 /*
6171 * Note: while we initialise the firstblock/flist pair, they
6172 * should never be used because blocks should never be
6173 * allocated or freed for a delalloc extent and hence we need
6174 * don't cancel or finish them after the xfs_bunmapi() call.
6175 */
6176 xfs_bmap_init(&flist, &firstblock);
6177 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6178 &flist, &done);
6179 if (error)
6180 break;
6181
6182 ASSERT(!flist.xbf_count && !flist.xbf_first);
6183next_block:
6184 start_fsb++;
6185 remaining--;
6186 } while(remaining > 0);
6187
6188 return error;
6189}