blob: d4fc8ccafa08b2379d97eba6172a94c38e3a278b [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 */
Christoph Hellwig54893272011-05-11 15:04:03 +0000104 int whichfork); /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/*
107 * Called by xfs_bmap_add_extent to handle cases converting a delayed
108 * allocation to a real allocation.
109 */
110STATIC int /* error */
111xfs_bmap_add_extent_delay_real(
112 xfs_inode_t *ip, /* incore inode pointer */
113 xfs_extnum_t idx, /* extent number to update/insert */
114 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100115 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
117 xfs_fsblock_t *first, /* pointer to firstblock variable */
118 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwig54893272011-05-11 15:04:03 +0000119 int *logflagsp); /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121/*
122 * Called by xfs_bmap_add_extent to handle cases converting a hole
123 * to a delayed allocation.
124 */
125STATIC int /* error */
126xfs_bmap_add_extent_hole_delay(
127 xfs_inode_t *ip, /* incore inode pointer */
128 xfs_extnum_t idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100129 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwig54893272011-05-11 15:04:03 +0000130 int *logflagsp); /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132/*
133 * Called by xfs_bmap_add_extent to handle cases converting a hole
134 * to a real allocation.
135 */
136STATIC int /* error */
137xfs_bmap_add_extent_hole_real(
138 xfs_inode_t *ip, /* incore inode pointer */
139 xfs_extnum_t idx, /* extent number to update/insert */
140 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100141 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int *logflagsp, /* inode logging flags */
143 int whichfork); /* data or attr fork */
144
145/*
146 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
147 * allocation to a real allocation or vice versa.
148 */
149STATIC int /* error */
150xfs_bmap_add_extent_unwritten_real(
151 xfs_inode_t *ip, /* incore inode pointer */
152 xfs_extnum_t idx, /* extent number to update/insert */
153 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100154 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000155 int *logflagsp); /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157/*
158 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
159 * It figures out where to ask the underlying allocator to put the new extent.
160 */
161STATIC int /* error */
162xfs_bmap_alloc(
163 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
164
165/*
166 * Transform a btree format file with only one leaf node, where the
167 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100168 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * give up the space for the btree root and pitch the leaf block.
170 */
171STATIC int /* error */
172xfs_bmap_btree_to_extents(
173 xfs_trans_t *tp, /* transaction pointer */
174 xfs_inode_t *ip, /* incore inode pointer */
175 xfs_btree_cur_t *cur, /* btree cursor */
176 int *logflagsp, /* inode logging flags */
177 int whichfork); /* data or attr fork */
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * Remove the entry "free" from the free item list. Prev points to the
181 * previous entry, unless "free" is the head of the list.
182 */
183STATIC void
184xfs_bmap_del_free(
185 xfs_bmap_free_t *flist, /* free item list header */
186 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
187 xfs_bmap_free_item_t *free); /* list item to be freed */
188
189/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * Convert an extents-format file into a btree-format file.
191 * The new file will have a root block (in the inode) and a single child block.
192 */
193STATIC int /* error */
194xfs_bmap_extents_to_btree(
195 xfs_trans_t *tp, /* transaction pointer */
196 xfs_inode_t *ip, /* incore inode pointer */
197 xfs_fsblock_t *firstblock, /* first-block-allocated */
198 xfs_bmap_free_t *flist, /* blocks freed in xaction */
199 xfs_btree_cur_t **curp, /* cursor returned to caller */
200 int wasdel, /* converting a delayed alloc */
201 int *logflagsp, /* inode logging flags */
202 int whichfork); /* data or attr fork */
203
204/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * Convert a local file to an extents file.
206 * This code is sort of bogus, since the file data needs to get
207 * logged so it won't be lost. The bmap-level manipulations are ok, though.
208 */
209STATIC int /* error */
210xfs_bmap_local_to_extents(
211 xfs_trans_t *tp, /* transaction pointer */
212 xfs_inode_t *ip, /* incore inode pointer */
213 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
214 xfs_extlen_t total, /* total blocks needed by transaction */
215 int *logflagsp, /* inode logging flags */
216 int whichfork); /* data or attr fork */
217
218/*
219 * Search the extents list for the inode, for the extent containing bno.
220 * If bno lies in a hole, point to the next entry. If bno lies past eof,
221 * *eofp will be set, and *prevp will contain the last entry (null if none).
222 * Else, *lastxp will be set to the index of the found
223 * entry; *gotp will contain the entry.
224 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000225STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226xfs_bmap_search_extents(
227 xfs_inode_t *ip, /* incore inode pointer */
228 xfs_fileoff_t bno, /* block number searched for */
229 int whichfork, /* data or attr fork */
230 int *eofp, /* out: end of file found */
231 xfs_extnum_t *lastxp, /* out: last extent index */
232 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
233 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
234
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000235/*
236 * Check the last inode extent to determine whether this allocation will result
237 * in blocks being allocated at the end of the file. When we allocate new data
238 * blocks at the end of the file which do not start at the previous data block,
239 * we will try to align the new blocks at stripe unit boundaries.
240 */
241STATIC int /* error */
242xfs_bmap_isaeof(
243 xfs_inode_t *ip, /* incore inode pointer */
244 xfs_fileoff_t off, /* file offset in fsblocks */
245 int whichfork, /* data or attribute fork */
246 char *aeof); /* return value */
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/*
249 * Compute the worst-case number of indirect blocks that will be used
250 * for ip's delayed extent of length "len".
251 */
252STATIC xfs_filblks_t
253xfs_bmap_worst_indlen(
254 xfs_inode_t *ip, /* incore inode pointer */
255 xfs_filblks_t len); /* delayed extent length */
256
257#ifdef DEBUG
258/*
259 * Perform various validation checks on the values being returned
260 * from xfs_bmapi().
261 */
262STATIC void
263xfs_bmap_validate_ret(
264 xfs_fileoff_t bno,
265 xfs_filblks_t len,
266 int flags,
267 xfs_bmbt_irec_t *mval,
268 int nmap,
269 int ret_nmap);
270#else
271#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
272#endif /* DEBUG */
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274STATIC int
275xfs_bmap_count_tree(
276 xfs_mount_t *mp,
277 xfs_trans_t *tp,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100278 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 xfs_fsblock_t blockno,
280 int levelin,
281 int *count);
282
Ruben Porrasc94312d2008-08-13 16:52:25 +1000283STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100285 xfs_ifork_t *ifp,
286 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 int numrecs,
288 int *count);
289
Ruben Porrasc94312d2008-08-13 16:52:25 +1000290STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +1100291xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +1100292 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100293 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +1100294 int numrecs,
295 int *count);
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/*
298 * Bmap internal routines.
299 */
300
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100301STATIC int /* error */
302xfs_bmbt_lookup_eq(
303 struct xfs_btree_cur *cur,
304 xfs_fileoff_t off,
305 xfs_fsblock_t bno,
306 xfs_filblks_t len,
307 int *stat) /* success/failure */
308{
309 cur->bc_rec.b.br_startoff = off;
310 cur->bc_rec.b.br_startblock = bno;
311 cur->bc_rec.b.br_blockcount = len;
312 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
313}
314
315STATIC int /* error */
316xfs_bmbt_lookup_ge(
317 struct xfs_btree_cur *cur,
318 xfs_fileoff_t off,
319 xfs_fsblock_t bno,
320 xfs_filblks_t len,
321 int *stat) /* success/failure */
322{
323 cur->bc_rec.b.br_startoff = off;
324 cur->bc_rec.b.br_startblock = bno;
325 cur->bc_rec.b.br_blockcount = len;
326 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
327}
328
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100329/*
330* Update the record referred to by cur to the value given
331 * by [off, bno, len, state].
332 * This either works (return 0) or gets an EFSCORRUPTED error.
333 */
334STATIC int
335xfs_bmbt_update(
336 struct xfs_btree_cur *cur,
337 xfs_fileoff_t off,
338 xfs_fsblock_t bno,
339 xfs_filblks_t len,
340 xfs_exntst_t state)
341{
342 union xfs_btree_rec rec;
343
344 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
345 return xfs_btree_update(cur, &rec);
346}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/*
349 * Called from xfs_bmap_add_attrfork to handle btree format files.
350 */
351STATIC int /* error */
352xfs_bmap_add_attrfork_btree(
353 xfs_trans_t *tp, /* transaction pointer */
354 xfs_inode_t *ip, /* incore inode pointer */
355 xfs_fsblock_t *firstblock, /* first block allocated */
356 xfs_bmap_free_t *flist, /* blocks to free at commit */
357 int *flags) /* inode logging flags */
358{
359 xfs_btree_cur_t *cur; /* btree cursor */
360 int error; /* error return value */
361 xfs_mount_t *mp; /* file system mount struct */
362 int stat; /* newroot status */
363
364 mp = ip->i_mount;
365 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
366 *flags |= XFS_ILOG_DBROOT;
367 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100368 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 cur->bc_private.b.flist = flist;
370 cur->bc_private.b.firstblock = *firstblock;
371 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
372 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000373 /* must be at least one entry */
374 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100375 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 goto error0;
377 if (stat == 0) {
378 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
379 return XFS_ERROR(ENOSPC);
380 }
381 *firstblock = cur->bc_private.b.firstblock;
382 cur->bc_private.b.allocated = 0;
383 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
384 }
385 return 0;
386error0:
387 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
388 return error;
389}
390
391/*
392 * Called from xfs_bmap_add_attrfork to handle extents format files.
393 */
394STATIC int /* error */
395xfs_bmap_add_attrfork_extents(
396 xfs_trans_t *tp, /* transaction pointer */
397 xfs_inode_t *ip, /* incore inode pointer */
398 xfs_fsblock_t *firstblock, /* first block allocated */
399 xfs_bmap_free_t *flist, /* blocks to free at commit */
400 int *flags) /* inode logging flags */
401{
402 xfs_btree_cur_t *cur; /* bmap btree cursor */
403 int error; /* error return value */
404
405 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
406 return 0;
407 cur = NULL;
408 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
409 flags, XFS_DATA_FORK);
410 if (cur) {
411 cur->bc_private.b.allocated = 0;
412 xfs_btree_del_cursor(cur,
413 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
414 }
415 return error;
416}
417
418/*
419 * Called from xfs_bmap_add_attrfork to handle local format files.
420 */
421STATIC int /* error */
422xfs_bmap_add_attrfork_local(
423 xfs_trans_t *tp, /* transaction pointer */
424 xfs_inode_t *ip, /* incore inode pointer */
425 xfs_fsblock_t *firstblock, /* first block allocated */
426 xfs_bmap_free_t *flist, /* blocks to free at commit */
427 int *flags) /* inode logging flags */
428{
429 xfs_da_args_t dargs; /* args for dir/attr code */
430 int error; /* error return value */
431 xfs_mount_t *mp; /* mount structure pointer */
432
433 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
434 return 0;
435 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
436 mp = ip->i_mount;
437 memset(&dargs, 0, sizeof(dargs));
438 dargs.dp = ip;
439 dargs.firstblock = firstblock;
440 dargs.flist = flist;
441 dargs.total = mp->m_dirblkfsbs;
442 dargs.whichfork = XFS_DATA_FORK;
443 dargs.trans = tp;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000444 error = xfs_dir2_sf_to_block(&dargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 } else
446 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
447 XFS_DATA_FORK);
448 return error;
449}
450
451/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100452 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 * after allocating space (or doing a delayed allocation).
454 */
455STATIC int /* error */
456xfs_bmap_add_extent(
457 xfs_inode_t *ip, /* incore inode pointer */
458 xfs_extnum_t idx, /* extent number to update/insert */
459 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100460 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 xfs_fsblock_t *first, /* pointer to firstblock variable */
462 xfs_bmap_free_t *flist, /* list of extents to be freed */
463 int *logflagsp, /* inode logging flags */
Christoph Hellwig54893272011-05-11 15:04:03 +0000464 int whichfork) /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 xfs_btree_cur_t *cur; /* btree cursor or null */
467 xfs_filblks_t da_new; /* new count del alloc blocks used */
468 xfs_filblks_t da_old; /* old count del alloc blocks used */
469 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 xfs_ifork_t *ifp; /* inode fork ptr */
471 int logflags; /* returned value */
472 xfs_extnum_t nextents; /* number of extents in file now */
473
474 XFS_STATS_INC(xs_add_exlist);
475 cur = *curp;
476 ifp = XFS_IFORK_PTR(ip, whichfork);
477 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
478 ASSERT(idx <= nextents);
479 da_old = da_new = 0;
480 error = 0;
481 /*
482 * This is the first extent added to a new/empty file.
483 * Special case this one, so other routines get to assume there are
484 * already extents in the list.
485 */
486 if (nextents == 0) {
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000487 xfs_iext_insert(ip, 0, 1, new,
488 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 ASSERT(cur == NULL);
491 ifp->if_lastex = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600492 if (!isnullstartblock(new->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600494 logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 } else
496 logflags = 0;
497 }
498 /*
499 * Any kind of new delayed allocation goes here.
500 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600501 else if (isnullstartblock(new->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (cur)
503 ASSERT((cur->bc_private.b.flags &
504 XFS_BTCUR_BPRV_WASDEL) == 0);
Christoph Hellwig54893272011-05-11 15:04:03 +0000505 error = xfs_bmap_add_extent_hole_delay(ip, idx, new, &logflags);
506 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 goto done;
508 }
509 /*
510 * Real allocation off the end of the file.
511 */
512 else if (idx == nextents) {
513 if (cur)
514 ASSERT((cur->bc_private.b.flags &
515 XFS_BTCUR_BPRV_WASDEL) == 0);
516 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000517 &logflags, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 goto done;
519 } else {
520 xfs_bmbt_irec_t prev; /* old extent at offset idx */
521
522 /*
523 * Get the record referred to by idx.
524 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100525 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /*
527 * If it's a real allocation record, and the new allocation ends
528 * after the start of the referred to record, then we're filling
529 * in a delayed or unwritten allocation with a real one, or
530 * converting real back to unwritten.
531 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600532 if (!isnullstartblock(new->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 new->br_startoff + new->br_blockcount > prev.br_startoff) {
534 if (prev.br_state != XFS_EXT_UNWRITTEN &&
Eric Sandeen9d87c312009-01-14 23:22:07 -0600535 isnullstartblock(prev.br_startblock)) {
536 da_old = startblockval(prev.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (cur)
538 ASSERT(cur->bc_private.b.flags &
539 XFS_BTCUR_BPRV_WASDEL);
Christoph Hellwig54893272011-05-11 15:04:03 +0000540 error = xfs_bmap_add_extent_delay_real(ip, idx,
541 &cur, new, &da_new, first,
542 flist, &logflags);
543 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 goto done;
545 } else if (new->br_state == XFS_EXT_NORM) {
546 ASSERT(new->br_state == XFS_EXT_NORM);
547 if ((error = xfs_bmap_add_extent_unwritten_real(
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000548 ip, idx, &cur, new, &logflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 goto done;
550 } else {
551 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
552 if ((error = xfs_bmap_add_extent_unwritten_real(
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000553 ip, idx, &cur, new, &logflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 goto done;
555 }
556 ASSERT(*curp == cur || *curp == NULL);
557 }
558 /*
559 * Otherwise we're filling in a hole with an allocation.
560 */
561 else {
562 if (cur)
563 ASSERT((cur->bc_private.b.flags &
564 XFS_BTCUR_BPRV_WASDEL) == 0);
565 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000566 new, &logflags, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 goto done;
568 }
569 }
570
571 ASSERT(*curp == cur || *curp == NULL);
572 /*
573 * Convert to a btree if necessary.
574 */
575 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
576 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
577 int tmp_logflags; /* partial log flag return val */
578
579 ASSERT(cur == NULL);
580 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
581 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
582 logflags |= tmp_logflags;
583 if (error)
584 goto done;
585 }
586 /*
587 * Adjust for changes in reserved delayed indirect blocks.
588 * Nothing to do for disk quotas here.
589 */
590 if (da_old || da_new) {
591 xfs_filblks_t nblks;
592
593 nblks = da_new;
594 if (cur)
595 nblks += cur->bc_private.b.allocated;
596 ASSERT(nblks <= da_old);
597 if (nblks < da_old)
Christoph Hellwig96540c72010-09-30 02:25:55 +0000598 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +0000599 (int64_t)(da_old - nblks), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601 /*
602 * Clear out the allocated field, done with it now in any case.
603 */
604 if (cur) {
605 cur->bc_private.b.allocated = 0;
606 *curp = cur;
607 }
608done:
609#ifdef DEBUG
610 if (!error)
611 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
612#endif
613 *logflagsp = logflags;
614 return error;
615}
616
617/*
618 * Called by xfs_bmap_add_extent to handle cases converting a delayed
619 * allocation to a real allocation.
620 */
621STATIC int /* error */
622xfs_bmap_add_extent_delay_real(
623 xfs_inode_t *ip, /* incore inode pointer */
624 xfs_extnum_t idx, /* extent number to update/insert */
625 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100626 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
628 xfs_fsblock_t *first, /* pointer to firstblock variable */
629 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwig54893272011-05-11 15:04:03 +0000630 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 xfs_btree_cur_t *cur; /* btree cursor */
633 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000634 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100637 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 xfs_fileoff_t new_endoff; /* end offset of new entry */
639 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
640 /* left is 0, right is 1, prev is 2 */
641 int rval=0; /* return value (logging flags) */
642 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000643 xfs_filblks_t temp=0; /* value for dnew calculations */
644 xfs_filblks_t temp2=0;/* value for dnew calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 int tmp_rval; /* partial logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647#define LEFT r[0]
648#define RIGHT r[1]
649#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 /*
652 * Set up a bunch of variables to make the tests simpler.
653 */
654 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100655 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
656 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 xfs_bmbt_get_all(ep, &PREV);
658 new_endoff = new->br_startoff + new->br_blockcount;
659 ASSERT(PREV.br_startoff <= new->br_startoff);
660 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /*
663 * Set flags determining what part of the previous delayed allocation
664 * extent is being replaced by a real allocation.
665 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000666 if (PREV.br_startoff == new->br_startoff)
667 state |= BMAP_LEFT_FILLING;
668 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
669 state |= BMAP_RIGHT_FILLING;
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /*
672 * Check and set flags if this segment has a left neighbor.
673 * Don't set contiguous if the combined extent would be too large.
674 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000675 if (idx > 0) {
676 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100677 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000678
679 if (isnullstartblock(LEFT.br_startblock))
680 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000682
683 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
684 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
685 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
686 LEFT.br_state == new->br_state &&
687 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
688 state |= BMAP_LEFT_CONTIG;
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /*
691 * Check and set flags if this segment has a right neighbor.
692 * Don't set contiguous if the combined extent would be too large.
693 * Also check for all-three-contiguous being too large.
694 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000695 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
696 state |= BMAP_RIGHT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100697 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000698
699 if (isnullstartblock(RIGHT.br_startblock))
700 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000702
703 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
704 new_endoff == RIGHT.br_startoff &&
705 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
706 new->br_state == RIGHT.br_state &&
707 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
708 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
709 BMAP_RIGHT_FILLING)) !=
710 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
711 BMAP_RIGHT_FILLING) ||
712 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
713 <= MAXEXTLEN))
714 state |= BMAP_RIGHT_CONTIG;
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 error = 0;
717 /*
718 * Switch out based on the FILLING and CONTIG state bits.
719 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000720 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
721 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
722 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
723 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 /*
725 * Filling in all of a previously delayed allocation extent.
726 * The left and right neighbors are both contiguous with new.
727 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000728 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100729 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 LEFT.br_blockcount + PREV.br_blockcount +
731 RIGHT.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000732 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
733
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000734 xfs_iext_remove(ip, idx, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 ip->i_df.if_lastex = idx - 1;
736 ip->i_d.di_nextents--;
737 if (cur == NULL)
738 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
739 else {
740 rval = XFS_ILOG_CORE;
741 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
742 RIGHT.br_startblock,
743 RIGHT.br_blockcount, &i)))
744 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000745 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100746 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000748 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100749 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000751 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
753 LEFT.br_startblock,
754 LEFT.br_blockcount +
755 PREV.br_blockcount +
756 RIGHT.br_blockcount, LEFT.br_state)))
757 goto done;
758 }
759 *dnew = 0;
760 break;
761
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000762 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /*
764 * Filling in all of a previously delayed allocation extent.
765 * The left neighbor is contiguous, the right is not.
766 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000767 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100768 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000770 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 ip->i_df.if_lastex = idx - 1;
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000773 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (cur == NULL)
775 rval = XFS_ILOG_DEXT;
776 else {
777 rval = 0;
778 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
779 LEFT.br_startblock, LEFT.br_blockcount,
780 &i)))
781 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000782 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
784 LEFT.br_startblock,
785 LEFT.br_blockcount +
786 PREV.br_blockcount, LEFT.br_state)))
787 goto done;
788 }
789 *dnew = 0;
790 break;
791
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000792 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /*
794 * Filling in all of a previously delayed allocation extent.
795 * The right neighbor is contiguous, the left is not.
796 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000797 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 xfs_bmbt_set_startblock(ep, new->br_startblock);
799 xfs_bmbt_set_blockcount(ep,
800 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000801 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 ip->i_df.if_lastex = idx;
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000804 xfs_iext_remove(ip, idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (cur == NULL)
806 rval = XFS_ILOG_DEXT;
807 else {
808 rval = 0;
809 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
810 RIGHT.br_startblock,
811 RIGHT.br_blockcount, &i)))
812 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000813 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
815 new->br_startblock,
816 PREV.br_blockcount +
817 RIGHT.br_blockcount, PREV.br_state)))
818 goto done;
819 }
820 *dnew = 0;
821 break;
822
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000823 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /*
825 * Filling in all of a previously delayed allocation extent.
826 * Neither the left nor right neighbors are contiguous with
827 * the new one.
828 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000829 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000831 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 ip->i_df.if_lastex = idx;
834 ip->i_d.di_nextents++;
835 if (cur == NULL)
836 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
837 else {
838 rval = XFS_ILOG_CORE;
839 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
840 new->br_startblock, new->br_blockcount,
841 &i)))
842 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000843 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100845 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000847 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849 *dnew = 0;
850 break;
851
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000852 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /*
854 * Filling in the first part of a previous delayed allocation.
855 * The left neighbor is contiguous.
856 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000857 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100858 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 LEFT.br_blockcount + new->br_blockcount);
860 xfs_bmbt_set_startoff(ep,
861 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000862 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000865 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 xfs_bmbt_set_blockcount(ep, temp);
867 ip->i_df.if_lastex = idx - 1;
868 if (cur == NULL)
869 rval = XFS_ILOG_DEXT;
870 else {
871 rval = 0;
872 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
873 LEFT.br_startblock, LEFT.br_blockcount,
874 &i)))
875 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000876 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
878 LEFT.br_startblock,
879 LEFT.br_blockcount +
880 new->br_blockcount,
881 LEFT.br_state)))
882 goto done;
883 }
884 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600885 startblockval(PREV.br_startblock));
886 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000887 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 *dnew = temp;
889 break;
890
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000891 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /*
893 * Filling in the first part of a previous delayed allocation.
894 * The left neighbor is not contiguous.
895 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000896 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 xfs_bmbt_set_startoff(ep, new_endoff);
898 temp = PREV.br_blockcount - new->br_blockcount;
899 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000900 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 ip->i_df.if_lastex = idx;
902 ip->i_d.di_nextents++;
903 if (cur == NULL)
904 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
905 else {
906 rval = XFS_ILOG_CORE;
907 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
908 new->br_startblock, new->br_blockcount,
909 &i)))
910 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000911 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100913 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000915 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
918 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
919 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
920 first, flist, &cur, 1, &tmp_rval,
921 XFS_DATA_FORK);
922 rval |= tmp_rval;
923 if (error)
924 goto done;
925 }
926 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600927 startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100929 ep = xfs_iext_get_ext(ifp, idx + 1);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600930 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000931 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 *dnew = temp;
933 break;
934
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000935 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 /*
937 * Filling in the last part of a previous delayed allocation.
938 * The right neighbor is contiguous with the new allocation.
939 */
940 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000941 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
942 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 xfs_bmbt_set_blockcount(ep, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100944 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
945 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 new->br_blockcount + RIGHT.br_blockcount,
947 RIGHT.br_state);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000948 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 ip->i_df.if_lastex = idx + 1;
950 if (cur == NULL)
951 rval = XFS_ILOG_DEXT;
952 else {
953 rval = 0;
954 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
955 RIGHT.br_startblock,
956 RIGHT.br_blockcount, &i)))
957 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000958 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if ((error = xfs_bmbt_update(cur, new->br_startoff,
960 new->br_startblock,
961 new->br_blockcount +
962 RIGHT.br_blockcount,
963 RIGHT.br_state)))
964 goto done;
965 }
966 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600967 startblockval(PREV.br_startblock));
968 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000969 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 *dnew = temp;
971 break;
972
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000973 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 /*
975 * Filling in the last part of a previous delayed allocation.
976 * The right neighbor is not contiguous.
977 */
978 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000979 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000981 xfs_iext_insert(ip, idx + 1, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 ip->i_df.if_lastex = idx + 1;
983 ip->i_d.di_nextents++;
984 if (cur == NULL)
985 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
986 else {
987 rval = XFS_ILOG_CORE;
988 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
989 new->br_startblock, new->br_blockcount,
990 &i)))
991 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000992 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100994 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000996 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
999 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1000 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1001 first, flist, &cur, 1, &tmp_rval,
1002 XFS_DATA_FORK);
1003 rval |= tmp_rval;
1004 if (error)
1005 goto done;
1006 }
1007 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001008 startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001010 ep = xfs_iext_get_ext(ifp, idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001011 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001012 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 *dnew = temp;
1014 break;
1015
1016 case 0:
1017 /*
1018 * Filling in the middle part of a previous delayed allocation.
1019 * Contiguity is impossible here.
1020 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001021 *
1022 * We start with a delayed allocation:
1023 *
1024 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1025 * PREV @ idx
1026 *
1027 * and we are allocating:
1028 * +rrrrrrrrrrrrrrrrr+
1029 * new
1030 *
1031 * and we set it up for insertion as:
1032 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1033 * new
1034 * PREV @ idx LEFT RIGHT
1035 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 */
1037 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001039 trace_xfs_bmap_pre_update(ip, idx, 0, _THIS_IP_);
1040 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
1041 LEFT = *new;
1042 RIGHT.br_state = PREV.br_state;
1043 RIGHT.br_startblock = nullstartblock(
1044 (int)xfs_bmap_worst_indlen(ip, temp2));
1045 RIGHT.br_startoff = new_endoff;
1046 RIGHT.br_blockcount = temp2;
1047 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
1048 xfs_iext_insert(ip, idx + 1, 2, &LEFT, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 ip->i_df.if_lastex = idx + 1;
1050 ip->i_d.di_nextents++;
1051 if (cur == NULL)
1052 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1053 else {
1054 rval = XFS_ILOG_CORE;
1055 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1056 new->br_startblock, new->br_blockcount,
1057 &i)))
1058 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001059 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001061 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001063 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1066 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1067 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1068 first, flist, &cur, 1, &tmp_rval,
1069 XFS_DATA_FORK);
1070 rval |= tmp_rval;
1071 if (error)
1072 goto done;
1073 }
1074 temp = xfs_bmap_worst_indlen(ip, temp);
1075 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001076 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 (cur ? cur->bc_private.b.allocated : 0));
1078 if (diff > 0 &&
Christoph Hellwig96540c72010-09-30 02:25:55 +00001079 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00001080 -((int64_t)diff), 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 /*
1082 * Ick gross gag me with a spoon.
1083 */
1084 ASSERT(0); /* want to see if this ever happens! */
1085 while (diff > 0) {
1086 if (temp) {
1087 temp--;
1088 diff--;
1089 if (!diff ||
Christoph Hellwig96540c72010-09-30 02:25:55 +00001090 !xfs_icsb_modify_counters(ip->i_mount,
1091 XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00001092 -((int64_t)diff), 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 break;
1094 }
1095 if (temp2) {
1096 temp2--;
1097 diff--;
1098 if (!diff ||
Christoph Hellwig96540c72010-09-30 02:25:55 +00001099 !xfs_icsb_modify_counters(ip->i_mount,
1100 XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00001101 -((int64_t)diff), 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 break;
1103 }
1104 }
1105 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001106 ep = xfs_iext_get_ext(ifp, idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001107 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001108 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1109 trace_xfs_bmap_pre_update(ip, idx + 2, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001110 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001111 nullstartblock((int)temp2));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001112 trace_xfs_bmap_post_update(ip, idx + 2, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 *dnew = temp + temp2;
1114 break;
1115
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001116 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1117 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1118 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1119 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1120 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1121 case BMAP_LEFT_CONTIG:
1122 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /*
1124 * These cases are all impossible.
1125 */
1126 ASSERT(0);
1127 }
1128 *curp = cur;
1129done:
1130 *logflagsp = rval;
1131 return error;
1132#undef LEFT
1133#undef RIGHT
1134#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137/*
1138 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1139 * allocation to a real allocation or vice versa.
1140 */
1141STATIC int /* error */
1142xfs_bmap_add_extent_unwritten_real(
1143 xfs_inode_t *ip, /* incore inode pointer */
1144 xfs_extnum_t idx, /* extent number to update/insert */
1145 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001146 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001147 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001150 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001153 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 xfs_fileoff_t new_endoff; /* end offset of new entry */
1155 xfs_exntst_t newext; /* new extent state */
1156 xfs_exntst_t oldext; /* old extent state */
1157 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1158 /* left is 0, right is 1, prev is 2 */
1159 int rval=0; /* return value (logging flags) */
1160 int state = 0;/* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162#define LEFT r[0]
1163#define RIGHT r[1]
1164#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /*
1166 * Set up a bunch of variables to make the tests simpler.
1167 */
1168 error = 0;
1169 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001170 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1171 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 xfs_bmbt_get_all(ep, &PREV);
1173 newext = new->br_state;
1174 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1175 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1176 ASSERT(PREV.br_state == oldext);
1177 new_endoff = new->br_startoff + new->br_blockcount;
1178 ASSERT(PREV.br_startoff <= new->br_startoff);
1179 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /*
1182 * Set flags determining what part of the previous oldext allocation
1183 * extent is being replaced by a newext allocation.
1184 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001185 if (PREV.br_startoff == new->br_startoff)
1186 state |= BMAP_LEFT_FILLING;
1187 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1188 state |= BMAP_RIGHT_FILLING;
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 /*
1191 * Check and set flags if this segment has a left neighbor.
1192 * Don't set contiguous if the combined extent would be too large.
1193 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001194 if (idx > 0) {
1195 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001196 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001197
1198 if (isnullstartblock(LEFT.br_startblock))
1199 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001201
1202 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1203 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1204 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1205 LEFT.br_state == newext &&
1206 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1207 state |= BMAP_LEFT_CONTIG;
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /*
1210 * Check and set flags if this segment has a right neighbor.
1211 * Don't set contiguous if the combined extent would be too large.
1212 * Also check for all-three-contiguous being too large.
1213 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001214 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1215 state |= BMAP_RIGHT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001216 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001217 if (isnullstartblock(RIGHT.br_startblock))
1218 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001220
1221 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1222 new_endoff == RIGHT.br_startoff &&
1223 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1224 newext == RIGHT.br_state &&
1225 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1226 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1227 BMAP_RIGHT_FILLING)) !=
1228 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1229 BMAP_RIGHT_FILLING) ||
1230 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1231 <= MAXEXTLEN))
1232 state |= BMAP_RIGHT_CONTIG;
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 /*
1235 * Switch out based on the FILLING and CONTIG state bits.
1236 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001237 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1238 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1239 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1240 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /*
1242 * Setting all of a previous oldext extent to newext.
1243 * The left and right neighbors are both contiguous with new.
1244 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001245 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001246 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 LEFT.br_blockcount + PREV.br_blockcount +
1248 RIGHT.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001249 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1250
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001251 xfs_iext_remove(ip, idx, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 ip->i_df.if_lastex = idx - 1;
1253 ip->i_d.di_nextents -= 2;
1254 if (cur == NULL)
1255 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1256 else {
1257 rval = XFS_ILOG_CORE;
1258 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1259 RIGHT.br_startblock,
1260 RIGHT.br_blockcount, &i)))
1261 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001262 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001263 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001265 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001266 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001268 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001269 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001271 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001272 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001274 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1276 LEFT.br_startblock,
1277 LEFT.br_blockcount + PREV.br_blockcount +
1278 RIGHT.br_blockcount, LEFT.br_state)))
1279 goto done;
1280 }
1281 break;
1282
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001283 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /*
1285 * Setting all of a previous oldext extent to newext.
1286 * The left neighbor is contiguous, the right is not.
1287 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001288 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001289 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001291 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 ip->i_df.if_lastex = idx - 1;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001294 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 ip->i_d.di_nextents--;
1296 if (cur == NULL)
1297 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1298 else {
1299 rval = XFS_ILOG_CORE;
1300 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1301 PREV.br_startblock, PREV.br_blockcount,
1302 &i)))
1303 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001304 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001305 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001307 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001308 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001310 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1312 LEFT.br_startblock,
1313 LEFT.br_blockcount + PREV.br_blockcount,
1314 LEFT.br_state)))
1315 goto done;
1316 }
1317 break;
1318
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001319 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /*
1321 * Setting all of a previous oldext extent to newext.
1322 * The right neighbor is contiguous, the left is not.
1323 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001324 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 xfs_bmbt_set_blockcount(ep,
1326 PREV.br_blockcount + RIGHT.br_blockcount);
1327 xfs_bmbt_set_state(ep, newext);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001328 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 ip->i_df.if_lastex = idx;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001330 xfs_iext_remove(ip, idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 ip->i_d.di_nextents--;
1332 if (cur == NULL)
1333 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1334 else {
1335 rval = XFS_ILOG_CORE;
1336 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1337 RIGHT.br_startblock,
1338 RIGHT.br_blockcount, &i)))
1339 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001340 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001341 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001343 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001344 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001346 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1348 new->br_startblock,
1349 new->br_blockcount + RIGHT.br_blockcount,
1350 newext)))
1351 goto done;
1352 }
1353 break;
1354
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001355 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /*
1357 * Setting all of a previous oldext extent to newext.
1358 * Neither the left nor right neighbors are contiguous with
1359 * the new one.
1360 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001361 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 xfs_bmbt_set_state(ep, newext);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001363 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 ip->i_df.if_lastex = idx;
1366 if (cur == NULL)
1367 rval = XFS_ILOG_DEXT;
1368 else {
1369 rval = 0;
1370 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1371 new->br_startblock, new->br_blockcount,
1372 &i)))
1373 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001374 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1376 new->br_startblock, new->br_blockcount,
1377 newext)))
1378 goto done;
1379 }
1380 break;
1381
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001382 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 /*
1384 * Setting the first part of a previous oldext extent to newext.
1385 * The left neighbor is contiguous.
1386 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001387 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001388 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 LEFT.br_blockcount + new->br_blockcount);
1390 xfs_bmbt_set_startoff(ep,
1391 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001392 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1393
1394 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 xfs_bmbt_set_startblock(ep,
1396 new->br_startblock + new->br_blockcount);
1397 xfs_bmbt_set_blockcount(ep,
1398 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001399 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 ip->i_df.if_lastex = idx - 1;
1402 if (cur == NULL)
1403 rval = XFS_ILOG_DEXT;
1404 else {
1405 rval = 0;
1406 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1407 PREV.br_startblock, PREV.br_blockcount,
1408 &i)))
1409 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001410 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if ((error = xfs_bmbt_update(cur,
1412 PREV.br_startoff + new->br_blockcount,
1413 PREV.br_startblock + new->br_blockcount,
1414 PREV.br_blockcount - new->br_blockcount,
1415 oldext)))
1416 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001417 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 goto done;
1419 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1420 LEFT.br_startblock,
1421 LEFT.br_blockcount + new->br_blockcount,
1422 LEFT.br_state))
1423 goto done;
1424 }
1425 break;
1426
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001427 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 /*
1429 * Setting the first part of a previous oldext extent to newext.
1430 * The left neighbor is not contiguous.
1431 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001432 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1434 xfs_bmbt_set_startoff(ep, new_endoff);
1435 xfs_bmbt_set_blockcount(ep,
1436 PREV.br_blockcount - new->br_blockcount);
1437 xfs_bmbt_set_startblock(ep,
1438 new->br_startblock + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001439 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1440
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001441 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 ip->i_df.if_lastex = idx;
1443 ip->i_d.di_nextents++;
1444 if (cur == NULL)
1445 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1446 else {
1447 rval = XFS_ILOG_CORE;
1448 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1449 PREV.br_startblock, PREV.br_blockcount,
1450 &i)))
1451 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001452 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if ((error = xfs_bmbt_update(cur,
1454 PREV.br_startoff + new->br_blockcount,
1455 PREV.br_startblock + new->br_blockcount,
1456 PREV.br_blockcount - new->br_blockcount,
1457 oldext)))
1458 goto done;
1459 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001460 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001462 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464 break;
1465
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001466 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 /*
1468 * Setting the last part of a previous oldext extent to newext.
1469 * The right neighbor is contiguous with the new allocation.
1470 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001471 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1472 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 xfs_bmbt_set_blockcount(ep,
1474 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001475 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001476 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1477 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001479 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 ip->i_df.if_lastex = idx + 1;
1482 if (cur == NULL)
1483 rval = XFS_ILOG_DEXT;
1484 else {
1485 rval = 0;
1486 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1487 PREV.br_startblock,
1488 PREV.br_blockcount, &i)))
1489 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001490 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1492 PREV.br_startblock,
1493 PREV.br_blockcount - new->br_blockcount,
1494 oldext)))
1495 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001496 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 goto done;
1498 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1499 new->br_startblock,
1500 new->br_blockcount + RIGHT.br_blockcount,
1501 newext)))
1502 goto done;
1503 }
1504 break;
1505
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001506 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /*
1508 * Setting the last part of a previous oldext extent to newext.
1509 * The right neighbor is not contiguous.
1510 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001511 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 xfs_bmbt_set_blockcount(ep,
1513 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001514 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1515
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001516 xfs_iext_insert(ip, idx + 1, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 ip->i_df.if_lastex = idx + 1;
1518 ip->i_d.di_nextents++;
1519 if (cur == NULL)
1520 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1521 else {
1522 rval = XFS_ILOG_CORE;
1523 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1524 PREV.br_startblock, PREV.br_blockcount,
1525 &i)))
1526 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001527 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1529 PREV.br_startblock,
1530 PREV.br_blockcount - new->br_blockcount,
1531 oldext)))
1532 goto done;
1533 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1534 new->br_startblock, new->br_blockcount,
1535 &i)))
1536 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001537 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001539 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001541 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543 break;
1544
1545 case 0:
1546 /*
1547 * Setting the middle part of a previous oldext extent to
1548 * newext. Contiguity is impossible here.
1549 * One extent becomes three extents.
1550 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001551 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 xfs_bmbt_set_blockcount(ep,
1553 new->br_startoff - PREV.br_startoff);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001554 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 r[0] = *new;
1557 r[1].br_startoff = new_endoff;
1558 r[1].br_blockcount =
1559 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1560 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1561 r[1].br_state = oldext;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001562 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 ip->i_df.if_lastex = idx + 1;
1564 ip->i_d.di_nextents += 2;
1565 if (cur == NULL)
1566 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1567 else {
1568 rval = XFS_ILOG_CORE;
1569 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1570 PREV.br_startblock, PREV.br_blockcount,
1571 &i)))
1572 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001573 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 /* new right extent - oldext */
1575 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1576 r[1].br_startblock, r[1].br_blockcount,
1577 r[1].br_state)))
1578 goto done;
1579 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10001581 cur->bc_rec.b.br_blockcount =
1582 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001583 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001585 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001586 /*
1587 * Reset the cursor to the position of the new extent
1588 * we are about to insert as we can't trust it after
1589 * the previous insert.
1590 */
1591 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1592 new->br_startblock, new->br_blockcount,
1593 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001595 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001597 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001598 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001600 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
1602 break;
1603
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001604 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1605 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1606 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1607 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1608 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1609 case BMAP_LEFT_CONTIG:
1610 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 /*
1612 * These cases are all impossible.
1613 */
1614 ASSERT(0);
1615 }
1616 *curp = cur;
1617done:
1618 *logflagsp = rval;
1619 return error;
1620#undef LEFT
1621#undef RIGHT
1622#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
1624
1625/*
1626 * Called by xfs_bmap_add_extent to handle cases converting a hole
1627 * to a delayed allocation.
1628 */
1629/*ARGSUSED*/
1630STATIC int /* error */
1631xfs_bmap_add_extent_hole_delay(
1632 xfs_inode_t *ip, /* incore inode pointer */
1633 xfs_extnum_t idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001634 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwig54893272011-05-11 15:04:03 +00001635 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001637 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001638 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1640 xfs_filblks_t newlen=0; /* new indirect size */
1641 xfs_filblks_t oldlen=0; /* old indirect size */
1642 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1643 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001644 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001646 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1647 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001649 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 /*
1652 * Check and set flags if this segment has a left neighbor
1653 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001654 if (idx > 0) {
1655 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001656 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001657
1658 if (isnullstartblock(left.br_startblock))
1659 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 /*
1663 * Check and set flags if the current (right) segment exists.
1664 * If it doesn't exist, we're converting the hole at end-of-file.
1665 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001666 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1667 state |= BMAP_RIGHT_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 xfs_bmbt_get_all(ep, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001669
1670 if (isnullstartblock(right.br_startblock))
1671 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /*
1675 * Set contiguity flags on the left and right neighbors.
1676 * Don't let extents get too large, even if the pieces are contiguous.
1677 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001678 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1679 left.br_startoff + left.br_blockcount == new->br_startoff &&
1680 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1681 state |= BMAP_LEFT_CONTIG;
1682
1683 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1684 new->br_startoff + new->br_blockcount == right.br_startoff &&
1685 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1686 (!(state & BMAP_LEFT_CONTIG) ||
1687 (left.br_blockcount + new->br_blockcount +
1688 right.br_blockcount <= MAXEXTLEN)))
1689 state |= BMAP_RIGHT_CONTIG;
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 /*
1692 * Switch out based on the contiguity flags.
1693 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001694 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1695 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 /*
1697 * New allocation is contiguous with delayed allocations
1698 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001699 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 */
1701 temp = left.br_blockcount + new->br_blockcount +
1702 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001703
1704 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001705 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001706 oldlen = startblockval(left.br_startblock) +
1707 startblockval(new->br_startblock) +
1708 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001710 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001711 nullstartblock((int)newlen));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001712 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1713
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001714 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 ip->i_df.if_lastex = idx - 1;
1716 break;
1717
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001718 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 /*
1720 * New allocation is contiguous with a delayed allocation
1721 * on the left.
1722 * Merge the new allocation with the left neighbor.
1723 */
1724 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001725 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001730 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001731 nullstartblock((int)newlen));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001732 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 ip->i_df.if_lastex = idx - 1;
1735 break;
1736
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001737 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 /*
1739 * New allocation is contiguous with a delayed allocation
1740 * on the right.
1741 * Merge the new allocation with the right neighbor.
1742 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001743 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001745 oldlen = startblockval(new->br_startblock) +
1746 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 newlen = xfs_bmap_worst_indlen(ip, temp);
1748 xfs_bmbt_set_allf(ep, new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06001749 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001750 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 ip->i_df.if_lastex = idx;
1753 break;
1754
1755 case 0:
1756 /*
1757 * New allocation is not contiguous with another
1758 * delayed allocation.
1759 * Insert a new entry.
1760 */
1761 oldlen = newlen = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001762 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 ip->i_df.if_lastex = idx;
1764 break;
1765 }
1766 if (oldlen != newlen) {
1767 ASSERT(oldlen > newlen);
Christoph Hellwig96540c72010-09-30 02:25:55 +00001768 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00001769 (int64_t)(oldlen - newlen), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 /*
1771 * Nothing to do for disk quota accounting here.
1772 */
1773 }
1774 *logflagsp = 0;
1775 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
1778/*
1779 * Called by xfs_bmap_add_extent to handle cases converting a hole
1780 * to a real allocation.
1781 */
1782STATIC int /* error */
1783xfs_bmap_add_extent_hole_real(
1784 xfs_inode_t *ip, /* incore inode pointer */
1785 xfs_extnum_t idx, /* extent number to update/insert */
1786 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001787 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 int *logflagsp, /* inode logging flags */
1789 int whichfork) /* data or attr fork */
1790{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001791 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 int i; /* temp state */
1794 xfs_ifork_t *ifp; /* inode fork pointer */
1795 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1796 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001797 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 ifp = XFS_IFORK_PTR(ip, whichfork);
1801 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001802 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 state = 0;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001804
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001805 if (whichfork == XFS_ATTR_FORK)
1806 state |= BMAP_ATTRFORK;
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 /*
1809 * Check and set flags if this segment has a left neighbor.
1810 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001811 if (idx > 0) {
1812 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001813 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001814 if (isnullstartblock(left.br_startblock))
1815 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 /*
1819 * Check and set flags if this segment has a current value.
1820 * Not true if we're inserting into the "hole" at eof.
1821 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001822 if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1823 state |= BMAP_RIGHT_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 xfs_bmbt_get_all(ep, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001825 if (isnullstartblock(right.br_startblock))
1826 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 /*
1830 * We're inserting a real allocation between "left" and "right".
1831 * Set the contiguity flags. Don't let extents get too large.
1832 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001833 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1834 left.br_startoff + left.br_blockcount == new->br_startoff &&
1835 left.br_startblock + left.br_blockcount == new->br_startblock &&
1836 left.br_state == new->br_state &&
1837 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1838 state |= BMAP_LEFT_CONTIG;
1839
1840 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1841 new->br_startoff + new->br_blockcount == right.br_startoff &&
1842 new->br_startblock + new->br_blockcount == right.br_startblock &&
1843 new->br_state == right.br_state &&
1844 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1845 (!(state & BMAP_LEFT_CONTIG) ||
1846 left.br_blockcount + new->br_blockcount +
1847 right.br_blockcount <= MAXEXTLEN))
1848 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001850 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 /*
1852 * Select which case we're in here, and implement it.
1853 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001854 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1855 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 /*
1857 * New allocation is contiguous with real allocations on the
1858 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001859 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001861 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001862 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 left.br_blockcount + new->br_blockcount +
1864 right.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001865 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1866
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001867 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 ifp->if_lastex = idx - 1;
1869 XFS_IFORK_NEXT_SET(ip, whichfork,
1870 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1871 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001872 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001873 } else {
1874 rval = XFS_ILOG_CORE;
1875 if ((error = xfs_bmbt_lookup_eq(cur,
1876 right.br_startoff,
1877 right.br_startblock,
1878 right.br_blockcount, &i)))
1879 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001880 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001881 if ((error = xfs_btree_delete(cur, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001882 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001883 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001884 if ((error = xfs_btree_decrement(cur, 0, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001885 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001886 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001887 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1888 left.br_startblock,
1889 left.br_blockcount +
1890 new->br_blockcount +
1891 right.br_blockcount,
1892 left.br_state)))
1893 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001895 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001897 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 /*
1899 * New allocation is contiguous with a real allocation
1900 * on the left.
1901 * Merge the new allocation with the left neighbor.
1902 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001903 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001904 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 left.br_blockcount + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001906 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 ifp->if_lastex = idx - 1;
1909 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001910 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001911 } else {
1912 rval = 0;
1913 if ((error = xfs_bmbt_lookup_eq(cur,
1914 left.br_startoff,
1915 left.br_startblock,
1916 left.br_blockcount, &i)))
1917 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001918 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001919 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1920 left.br_startblock,
1921 left.br_blockcount +
1922 new->br_blockcount,
1923 left.br_state)))
1924 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001926 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001928 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 /*
1930 * New allocation is contiguous with a real allocation
1931 * on the right.
1932 * Merge the new allocation with the right neighbor.
1933 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001934 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
1936 new->br_blockcount + right.br_blockcount,
1937 right.br_state);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001938 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 ifp->if_lastex = idx;
1941 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001942 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001943 } else {
1944 rval = 0;
1945 if ((error = xfs_bmbt_lookup_eq(cur,
1946 right.br_startoff,
1947 right.br_startblock,
1948 right.br_blockcount, &i)))
1949 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001950 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001951 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1952 new->br_startblock,
1953 new->br_blockcount +
1954 right.br_blockcount,
1955 right.br_state)))
1956 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001958 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 case 0:
1961 /*
1962 * New allocation is not contiguous with another
1963 * real allocation.
1964 * Insert a new entry.
1965 */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001966 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 ifp->if_lastex = idx;
1968 XFS_IFORK_NEXT_SET(ip, whichfork,
1969 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
1970 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001971 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001972 } else {
1973 rval = XFS_ILOG_CORE;
1974 if ((error = xfs_bmbt_lookup_eq(cur,
1975 new->br_startoff,
1976 new->br_startblock,
1977 new->br_blockcount, &i)))
1978 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001979 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001980 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001981 if ((error = xfs_btree_insert(cur, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001982 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001983 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001985 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001987done:
1988 *logflagsp = rval;
1989 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
1991
Nathan Scottdd9f4382006-01-11 15:28:28 +11001992/*
1993 * Adjust the size of the new extent based on di_extsize and rt extsize.
1994 */
1995STATIC int
1996xfs_bmap_extsize_align(
1997 xfs_mount_t *mp,
1998 xfs_bmbt_irec_t *gotp, /* next extent pointer */
1999 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2000 xfs_extlen_t extsz, /* align to this extent size */
2001 int rt, /* is this a realtime inode? */
2002 int eof, /* is extent at end-of-file? */
2003 int delay, /* creating delalloc extent? */
2004 int convert, /* overwriting unwritten extent? */
2005 xfs_fileoff_t *offp, /* in/out: aligned offset */
2006 xfs_extlen_t *lenp) /* in/out: aligned length */
2007{
2008 xfs_fileoff_t orig_off; /* original offset */
2009 xfs_extlen_t orig_alen; /* original length */
2010 xfs_fileoff_t orig_end; /* original off+len */
2011 xfs_fileoff_t nexto; /* next file offset */
2012 xfs_fileoff_t prevo; /* previous file offset */
2013 xfs_fileoff_t align_off; /* temp for offset */
2014 xfs_extlen_t align_alen; /* temp for length */
2015 xfs_extlen_t temp; /* temp for calculations */
2016
2017 if (convert)
2018 return 0;
2019
2020 orig_off = align_off = *offp;
2021 orig_alen = align_alen = *lenp;
2022 orig_end = orig_off + orig_alen;
2023
2024 /*
2025 * If this request overlaps an existing extent, then don't
2026 * attempt to perform any additional alignment.
2027 */
2028 if (!delay && !eof &&
2029 (orig_off >= gotp->br_startoff) &&
2030 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2031 return 0;
2032 }
2033
2034 /*
2035 * If the file offset is unaligned vs. the extent size
2036 * we need to align it. This will be possible unless
2037 * the file was previously written with a kernel that didn't
2038 * perform this alignment, or if a truncate shot us in the
2039 * foot.
2040 */
2041 temp = do_mod(orig_off, extsz);
2042 if (temp) {
2043 align_alen += temp;
2044 align_off -= temp;
2045 }
2046 /*
2047 * Same adjustment for the end of the requested area.
2048 */
2049 if ((temp = (align_alen % extsz))) {
2050 align_alen += extsz - temp;
2051 }
2052 /*
2053 * If the previous block overlaps with this proposed allocation
2054 * then move the start forward without adjusting the length.
2055 */
2056 if (prevp->br_startoff != NULLFILEOFF) {
2057 if (prevp->br_startblock == HOLESTARTBLOCK)
2058 prevo = prevp->br_startoff;
2059 else
2060 prevo = prevp->br_startoff + prevp->br_blockcount;
2061 } else
2062 prevo = 0;
2063 if (align_off != orig_off && align_off < prevo)
2064 align_off = prevo;
2065 /*
2066 * If the next block overlaps with this proposed allocation
2067 * then move the start back without adjusting the length,
2068 * but not before offset 0.
2069 * This may of course make the start overlap previous block,
2070 * and if we hit the offset 0 limit then the next block
2071 * can still overlap too.
2072 */
2073 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2074 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2075 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2076 nexto = gotp->br_startoff + gotp->br_blockcount;
2077 else
2078 nexto = gotp->br_startoff;
2079 } else
2080 nexto = NULLFILEOFF;
2081 if (!eof &&
2082 align_off + align_alen != orig_end &&
2083 align_off + align_alen > nexto)
2084 align_off = nexto > align_alen ? nexto - align_alen : 0;
2085 /*
2086 * If we're now overlapping the next or previous extent that
2087 * means we can't fit an extsz piece in this hole. Just move
2088 * the start forward to the first valid spot and set
2089 * the length so we hit the end.
2090 */
2091 if (align_off != orig_off && align_off < prevo)
2092 align_off = prevo;
2093 if (align_off + align_alen != orig_end &&
2094 align_off + align_alen > nexto &&
2095 nexto != NULLFILEOFF) {
2096 ASSERT(nexto > prevo);
2097 align_alen = nexto - align_off;
2098 }
2099
2100 /*
2101 * If realtime, and the result isn't a multiple of the realtime
2102 * extent size we need to remove blocks until it is.
2103 */
2104 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2105 /*
2106 * We're not covering the original request, or
2107 * we won't be able to once we fix the length.
2108 */
2109 if (orig_off < align_off ||
2110 orig_end > align_off + align_alen ||
2111 align_alen - temp < orig_alen)
2112 return XFS_ERROR(EINVAL);
2113 /*
2114 * Try to fix it by moving the start up.
2115 */
2116 if (align_off + temp <= orig_off) {
2117 align_alen -= temp;
2118 align_off += temp;
2119 }
2120 /*
2121 * Try to fix it by moving the end in.
2122 */
2123 else if (align_off + align_alen - temp >= orig_end)
2124 align_alen -= temp;
2125 /*
2126 * Set the start to the minimum then trim the length.
2127 */
2128 else {
2129 align_alen -= orig_off - align_off;
2130 align_off = orig_off;
2131 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2132 }
2133 /*
2134 * Result doesn't cover the request, fail it.
2135 */
2136 if (orig_off < align_off || orig_end > align_off + align_alen)
2137 return XFS_ERROR(EINVAL);
2138 } else {
2139 ASSERT(orig_off >= align_off);
2140 ASSERT(orig_end <= align_off + align_alen);
2141 }
2142
2143#ifdef DEBUG
2144 if (!eof && gotp->br_startoff != NULLFILEOFF)
2145 ASSERT(align_off + align_alen <= gotp->br_startoff);
2146 if (prevp->br_startoff != NULLFILEOFF)
2147 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2148#endif
2149
2150 *lenp = align_alen;
2151 *offp = align_off;
2152 return 0;
2153}
2154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155#define XFS_ALLOC_GAP_UNITS 4
2156
David Chinnerc2b1cba2008-04-10 12:21:40 +10002157STATIC void
Nathan Scotta365bdd2006-03-14 13:34:16 +11002158xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2160{
2161 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2163 xfs_mount_t *mp; /* mount point structure */
2164 int nullfb; /* true if ap->firstblock isn't set */
2165 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167#define ISVALID(x,y) \
2168 (rt ? \
2169 (x) < mp->m_sb.sb_rblocks : \
2170 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2171 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2172 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 mp = ap->ip->i_mount;
2175 nullfb = ap->firstblock == NULLFSBLOCK;
2176 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2177 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 /*
2179 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002180 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 */
2182 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002183 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2185 ap->prevp->br_startblock)) {
2186 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2187 /*
2188 * Adjust for the gap between prevp and us.
2189 */
2190 adjust = ap->off -
2191 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2192 if (adjust &&
2193 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2194 ap->rval += adjust;
2195 }
2196 /*
2197 * If not at eof, then compare the two neighbor blocks.
2198 * Figure out whether either one gives us a good starting point,
2199 * and pick the better one.
2200 */
2201 else if (!ap->eof) {
2202 xfs_fsblock_t gotbno; /* right side block number */
2203 xfs_fsblock_t gotdiff=0; /* right side difference */
2204 xfs_fsblock_t prevbno; /* left side block number */
2205 xfs_fsblock_t prevdiff=0; /* left side difference */
2206
2207 /*
2208 * If there's a previous (left) block, select a requested
2209 * start block based on it.
2210 */
2211 if (ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002212 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 (prevbno = ap->prevp->br_startblock +
2214 ap->prevp->br_blockcount) &&
2215 ISVALID(prevbno, ap->prevp->br_startblock)) {
2216 /*
2217 * Calculate gap to end of previous block.
2218 */
2219 adjust = prevdiff = ap->off -
2220 (ap->prevp->br_startoff +
2221 ap->prevp->br_blockcount);
2222 /*
2223 * Figure the startblock based on the previous block's
2224 * end and the gap size.
2225 * Heuristic!
2226 * If the gap is large relative to the piece we're
2227 * allocating, or using it gives us an invalid block
2228 * number, then just use the end of the previous block.
2229 */
2230 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2231 ISVALID(prevbno + prevdiff,
2232 ap->prevp->br_startblock))
2233 prevbno += adjust;
2234 else
2235 prevdiff += adjust;
2236 /*
2237 * If the firstblock forbids it, can't use it,
2238 * must use default.
2239 */
2240 if (!rt && !nullfb &&
2241 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2242 prevbno = NULLFSBLOCK;
2243 }
2244 /*
2245 * No previous block or can't follow it, just default.
2246 */
2247 else
2248 prevbno = NULLFSBLOCK;
2249 /*
2250 * If there's a following (right) block, select a requested
2251 * start block based on it.
2252 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002253 if (!isnullstartblock(ap->gotp->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 /*
2255 * Calculate gap to start of next block.
2256 */
2257 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2258 /*
2259 * Figure the startblock based on the next block's
2260 * start and the gap size.
2261 */
2262 gotbno = ap->gotp->br_startblock;
2263 /*
2264 * Heuristic!
2265 * If the gap is large relative to the piece we're
2266 * allocating, or using it gives us an invalid block
2267 * number, then just use the start of the next block
2268 * offset by our length.
2269 */
2270 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2271 ISVALID(gotbno - gotdiff, gotbno))
2272 gotbno -= adjust;
2273 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2274 gotbno -= ap->alen;
2275 gotdiff += adjust - ap->alen;
2276 } else
2277 gotdiff += adjust;
2278 /*
2279 * If the firstblock forbids it, can't use it,
2280 * must use default.
2281 */
2282 if (!rt && !nullfb &&
2283 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2284 gotbno = NULLFSBLOCK;
2285 }
2286 /*
2287 * No next block, just default.
2288 */
2289 else
2290 gotbno = NULLFSBLOCK;
2291 /*
2292 * If both valid, pick the better one, else the only good
2293 * one, else ap->rval is already set (to 0 or the inode block).
2294 */
2295 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2296 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2297 else if (prevbno != NULLFSBLOCK)
2298 ap->rval = prevbno;
2299 else if (gotbno != NULLFSBLOCK)
2300 ap->rval = gotbno;
2301 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002302#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11002303}
2304
2305STATIC int
2306xfs_bmap_rtalloc(
2307 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2308{
2309 xfs_alloctype_t atype = 0; /* type for allocation routines */
2310 int error; /* error return value */
2311 xfs_mount_t *mp; /* mount point structure */
2312 xfs_extlen_t prod = 0; /* product factor for allocators */
2313 xfs_extlen_t ralen = 0; /* realtime allocation length */
2314 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002315 xfs_rtblock_t rtb;
2316
2317 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002318 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002319 prod = align / mp->m_sb.sb_rextsize;
2320 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2321 align, 1, ap->eof, 0,
2322 ap->conv, &ap->off, &ap->alen);
2323 if (error)
2324 return error;
2325 ASSERT(ap->alen);
2326 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2327
2328 /*
2329 * If the offset & length are not perfectly aligned
2330 * then kill prod, it will just get us in trouble.
2331 */
2332 if (do_mod(ap->off, align) || ap->alen % align)
2333 prod = 1;
2334 /*
2335 * Set ralen to be the actual requested length in rtextents.
2336 */
2337 ralen = ap->alen / mp->m_sb.sb_rextsize;
2338 /*
2339 * If the old value was close enough to MAXEXTLEN that
2340 * we rounded up to it, cut it back so it's valid again.
2341 * Note that if it's a really large request (bigger than
2342 * MAXEXTLEN), we don't hear about that number, and can't
2343 * adjust the starting point to match it.
2344 */
2345 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2346 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
Christoph Hellwig04e99452011-01-25 09:06:19 +00002347
2348 /*
2349 * Lock out other modifications to the RT bitmap inode.
2350 */
Christoph Hellwig1050c712011-02-13 13:25:31 +00002351 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
2352 xfs_trans_ijoin_ref(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Christoph Hellwig04e99452011-01-25 09:06:19 +00002353
Nathan Scotta365bdd2006-03-14 13:34:16 +11002354 /*
2355 * If it's an allocation to an empty file at offset 0,
2356 * pick an extent that will space things out in the rt area.
2357 */
2358 if (ap->eof && ap->off == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10002359 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2360
Nathan Scotta365bdd2006-03-14 13:34:16 +11002361 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2362 if (error)
2363 return error;
2364 ap->rval = rtx * mp->m_sb.sb_rextsize;
2365 } else {
2366 ap->rval = 0;
2367 }
2368
2369 xfs_bmap_adjacent(ap);
2370
2371 /*
2372 * Realtime allocation, done through xfs_rtallocate_extent.
2373 */
2374 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2375 do_div(ap->rval, mp->m_sb.sb_rextsize);
2376 rtb = ap->rval;
2377 ap->alen = ralen;
2378 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2379 &ralen, atype, ap->wasdel, prod, &rtb)))
2380 return error;
2381 if (rtb == NULLFSBLOCK && prod > 1 &&
2382 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2383 ap->alen, &ralen, atype,
2384 ap->wasdel, 1, &rtb)))
2385 return error;
2386 ap->rval = rtb;
2387 if (ap->rval != NULLFSBLOCK) {
2388 ap->rval *= mp->m_sb.sb_rextsize;
2389 ralen *= mp->m_sb.sb_rextsize;
2390 ap->alen = ralen;
2391 ap->ip->i_d.di_nblocks += ralen;
2392 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2393 if (ap->wasdel)
2394 ap->ip->i_delayed_blks -= ralen;
2395 /*
2396 * Adjust the disk quota also. This was reserved
2397 * earlier.
2398 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002399 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002400 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2401 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2402 } else {
2403 ap->alen = 0;
2404 }
2405 return 0;
2406}
2407
2408STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00002409xfs_bmap_btalloc_nullfb(
2410 struct xfs_bmalloca *ap,
2411 struct xfs_alloc_arg *args,
2412 xfs_extlen_t *blen)
2413{
2414 struct xfs_mount *mp = ap->ip->i_mount;
2415 struct xfs_perag *pag;
2416 xfs_agnumber_t ag, startag;
2417 int notinit = 0;
2418 int error;
2419
2420 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2421 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2422 else
2423 args->type = XFS_ALLOCTYPE_START_BNO;
2424 args->total = ap->total;
2425
2426 /*
2427 * Search for an allocation group with a single extent large enough
2428 * for the request. If one isn't found, then adjust the minimum
2429 * allocation size to the largest space found.
2430 */
2431 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2432 if (startag == NULLAGNUMBER)
2433 startag = ag = 0;
2434
2435 pag = xfs_perag_get(mp, ag);
Dave Chinner14b064c2011-01-27 12:16:28 +11002436 while (*blen < args->maxlen) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00002437 if (!pag->pagf_init) {
2438 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2439 XFS_ALLOC_FLAG_TRYLOCK);
2440 if (error) {
2441 xfs_perag_put(pag);
2442 return error;
2443 }
2444 }
2445
2446 /*
2447 * See xfs_alloc_fix_freelist...
2448 */
2449 if (pag->pagf_init) {
2450 xfs_extlen_t longest;
2451 longest = xfs_alloc_longest_free_extent(mp, pag);
2452 if (*blen < longest)
2453 *blen = longest;
2454 } else
2455 notinit = 1;
2456
2457 if (xfs_inode_is_filestream(ap->ip)) {
Dave Chinner14b064c2011-01-27 12:16:28 +11002458 if (*blen >= args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00002459 break;
2460
2461 if (ap->userdata) {
2462 /*
2463 * If startag is an invalid AG, we've
2464 * come here once before and
2465 * xfs_filestream_new_ag picked the
2466 * best currently available.
2467 *
2468 * Don't continue looping, since we
2469 * could loop forever.
2470 */
2471 if (startag == NULLAGNUMBER)
2472 break;
2473
2474 error = xfs_filestream_new_ag(ap, &ag);
2475 xfs_perag_put(pag);
2476 if (error)
2477 return error;
2478
2479 /* loop again to set 'blen'*/
2480 startag = NULLAGNUMBER;
2481 pag = xfs_perag_get(mp, ag);
2482 continue;
2483 }
2484 }
2485 if (++ag == mp->m_sb.sb_agcount)
2486 ag = 0;
2487 if (ag == startag)
2488 break;
2489 xfs_perag_put(pag);
2490 pag = xfs_perag_get(mp, ag);
2491 }
2492 xfs_perag_put(pag);
2493
2494 /*
2495 * Since the above loop did a BUF_TRYLOCK, it is
2496 * possible that there is space for this request.
2497 */
2498 if (notinit || *blen < ap->minlen)
2499 args->minlen = ap->minlen;
2500 /*
2501 * If the best seen length is less than the request
2502 * length, use the best as the minimum.
2503 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002504 else if (*blen < args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00002505 args->minlen = *blen;
2506 /*
Dave Chinner14b064c2011-01-27 12:16:28 +11002507 * Otherwise we've seen an extent as big as maxlen,
Christoph Hellwigc467c042010-02-15 23:34:42 +00002508 * use that as the minimum.
2509 */
2510 else
Dave Chinner14b064c2011-01-27 12:16:28 +11002511 args->minlen = args->maxlen;
Christoph Hellwigc467c042010-02-15 23:34:42 +00002512
2513 /*
2514 * set the failure fallback case to look in the selected
2515 * AG as the stream may have moved.
2516 */
2517 if (xfs_inode_is_filestream(ap->ip))
2518 ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2519
2520 return 0;
2521}
2522
2523STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11002524xfs_bmap_btalloc(
2525 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2526{
2527 xfs_mount_t *mp; /* mount point structure */
2528 xfs_alloctype_t atype = 0; /* type for allocation routines */
2529 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002530 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00002531 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002532 xfs_alloc_arg_t args;
2533 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002534 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002535 int nullfb; /* true if ap->firstblock isn't set */
2536 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002537 int tryagain;
2538 int error;
2539
2540 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002541 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002542 if (unlikely(align)) {
2543 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2544 align, 0, ap->eof, 0, ap->conv,
2545 &ap->off, &ap->alen);
2546 ASSERT(!error);
2547 ASSERT(ap->alen);
2548 }
2549 nullfb = ap->firstblock == NULLFSBLOCK;
2550 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10002551 if (nullfb) {
2552 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2553 ag = xfs_filestream_lookup_ag(ap->ip);
2554 ag = (ag != NULLAGNUMBER) ? ag : 0;
2555 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2556 } else {
2557 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2558 }
2559 } else
Nathan Scotta365bdd2006-03-14 13:34:16 +11002560 ap->rval = ap->firstblock;
2561
2562 xfs_bmap_adjacent(ap);
2563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 /*
2565 * If allowed, use ap->rval; otherwise must use firstblock since
2566 * it's in the right allocation group.
2567 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002568 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 ;
2570 else
2571 ap->rval = ap->firstblock;
2572 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 * Normal allocation, done through xfs_alloc_vextent.
2574 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002575 tryagain = isaligned = 0;
2576 args.tp = ap->tp;
2577 args.mp = mp;
2578 args.fsbno = ap->rval;
Dave Chinner14b064c2011-01-27 12:16:28 +11002579
2580 /* Trim the allocation back to the maximum an AG can fit. */
2581 args.maxlen = MIN(ap->alen, XFS_ALLOC_AG_MAX_USABLE(mp));
Yingping Lud210a282006-06-09 14:55:18 +10002582 args.firstblock = ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002583 blen = 0;
2584 if (nullfb) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00002585 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2586 if (error)
2587 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002588 } else if (ap->low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002589 if (xfs_inode_is_filestream(ap->ip))
2590 args.type = XFS_ALLOCTYPE_FIRST_AG;
2591 else
2592 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002593 args.total = args.minlen = ap->minlen;
2594 } else {
2595 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2596 args.total = ap->total;
2597 args.minlen = ap->minlen;
2598 }
David Chinner957d0eb2007-06-18 16:50:37 +10002599 /* apply extent size hints if obtained earlier */
2600 if (unlikely(align)) {
2601 args.prod = align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002602 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2603 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11002604 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11002605 args.prod = 1;
2606 args.mod = 0;
2607 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002608 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002609 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2610 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2611 }
2612 /*
2613 * If we are not low on available data blocks, and the
2614 * underlying logical volume manager is a stripe, and
2615 * the file offset is zero then try to allocate data
2616 * blocks on stripe unit boundary.
2617 * NOTE: ap->aeof is only set if the allocation length
2618 * is >= the stripe unit and the allocation offset is
2619 * at the end of file.
2620 */
2621 if (!ap->low && ap->aeof) {
2622 if (!ap->off) {
2623 args.alignment = mp->m_dalign;
2624 atype = args.type;
2625 isaligned = 1;
2626 /*
2627 * Adjust for alignment
2628 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002629 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002630 args.minlen = blen - args.alignment;
2631 args.minalignslop = 0;
2632 } else {
2633 /*
2634 * First try an exact bno allocation.
2635 * If it fails then do a near or start bno
2636 * allocation with alignment turned on.
2637 */
2638 atype = args.type;
2639 tryagain = 1;
2640 args.type = XFS_ALLOCTYPE_THIS_BNO;
2641 args.alignment = 1;
2642 /*
2643 * Compute the minlen+alignment for the
2644 * next case. Set slop so that the value
2645 * of minlen+alignment+slop doesn't go up
2646 * between the calls.
2647 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002648 if (blen > mp->m_dalign && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002649 nextminlen = blen - mp->m_dalign;
2650 else
2651 nextminlen = args.minlen;
2652 if (nextminlen + mp->m_dalign > args.minlen + 1)
2653 args.minalignslop =
2654 nextminlen + mp->m_dalign -
2655 args.minlen - 1;
2656 else
2657 args.minalignslop = 0;
2658 }
2659 } else {
2660 args.alignment = 1;
2661 args.minalignslop = 0;
2662 }
2663 args.minleft = ap->minleft;
2664 args.wasdel = ap->wasdel;
2665 args.isfl = 0;
2666 args.userdata = ap->userdata;
2667 if ((error = xfs_alloc_vextent(&args)))
2668 return error;
2669 if (tryagain && args.fsbno == NULLFSBLOCK) {
2670 /*
2671 * Exact allocation failed. Now try with alignment
2672 * turned on.
2673 */
2674 args.type = atype;
2675 args.fsbno = ap->rval;
2676 args.alignment = mp->m_dalign;
2677 args.minlen = nextminlen;
2678 args.minalignslop = 0;
2679 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 if ((error = xfs_alloc_vextent(&args)))
2681 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002682 }
2683 if (isaligned && args.fsbno == NULLFSBLOCK) {
2684 /*
2685 * allocation failed, so turn off alignment and
2686 * try again.
2687 */
2688 args.type = atype;
2689 args.fsbno = ap->rval;
2690 args.alignment = 0;
2691 if ((error = xfs_alloc_vextent(&args)))
2692 return error;
2693 }
2694 if (args.fsbno == NULLFSBLOCK && nullfb &&
2695 args.minlen > ap->minlen) {
2696 args.minlen = ap->minlen;
2697 args.type = XFS_ALLOCTYPE_START_BNO;
2698 args.fsbno = ap->rval;
2699 if ((error = xfs_alloc_vextent(&args)))
2700 return error;
2701 }
2702 if (args.fsbno == NULLFSBLOCK && nullfb) {
2703 args.fsbno = 0;
2704 args.type = XFS_ALLOCTYPE_FIRST_AG;
2705 args.total = ap->minlen;
2706 args.minleft = 0;
2707 if ((error = xfs_alloc_vextent(&args)))
2708 return error;
2709 ap->low = 1;
2710 }
2711 if (args.fsbno != NULLFSBLOCK) {
2712 ap->firstblock = ap->rval = args.fsbno;
2713 ASSERT(nullfb || fb_agno == args.agno ||
2714 (ap->low && fb_agno < args.agno));
2715 ap->alen = args.len;
2716 ap->ip->i_d.di_nblocks += args.len;
2717 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2718 if (ap->wasdel)
2719 ap->ip->i_delayed_blks -= args.len;
2720 /*
2721 * Adjust the disk quota also. This was reserved
2722 * earlier.
2723 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002724 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002725 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2726 XFS_TRANS_DQ_BCOUNT,
2727 (long) args.len);
2728 } else {
2729 ap->rval = NULLFSBLOCK;
2730 ap->alen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 }
2732 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002733}
2734
2735/*
2736 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2737 * It figures out where to ask the underlying allocator to put the new extent.
2738 */
2739STATIC int
2740xfs_bmap_alloc(
2741 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2742{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002743 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002744 return xfs_bmap_rtalloc(ap);
2745 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746}
2747
2748/*
2749 * Transform a btree format file with only one leaf node, where the
2750 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002751 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 * give up the space for the btree root and pitch the leaf block.
2753 */
2754STATIC int /* error */
2755xfs_bmap_btree_to_extents(
2756 xfs_trans_t *tp, /* transaction pointer */
2757 xfs_inode_t *ip, /* incore inode pointer */
2758 xfs_btree_cur_t *cur, /* btree cursor */
2759 int *logflagsp, /* inode logging flags */
2760 int whichfork) /* data or attr fork */
2761{
2762 /* REFERENCED */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002763 struct xfs_btree_block *cblock;/* child btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 xfs_fsblock_t cbno; /* child block number */
2765 xfs_buf_t *cbp; /* child block's buffer */
2766 int error; /* error return value */
2767 xfs_ifork_t *ifp; /* inode fork data */
2768 xfs_mount_t *mp; /* mount point structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10002769 __be64 *pp; /* ptr to block address */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002770 struct xfs_btree_block *rblock;/* root btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Christoph Hellwig60197e82008-10-30 17:11:19 +11002772 mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 ifp = XFS_IFORK_PTR(ip, whichfork);
2774 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2775 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2776 rblock = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11002777 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2778 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
Christoph Hellwig60197e82008-10-30 17:11:19 +11002779 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2780 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10002781 cbno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 *logflagsp = 0;
2783#ifdef DEBUG
Christoph Hellwig576039c2006-09-28 10:58:06 +10002784 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return error;
2786#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2788 XFS_BMAP_BTREE_REF)))
2789 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002790 cblock = XFS_BUF_TO_BLOCK(cbp);
2791 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 return error;
2793 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2794 ip->i_d.di_nblocks--;
Christoph Hellwig7d095252009-06-08 15:33:32 +02002795 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 xfs_trans_binval(tp, cbp);
2797 if (cur->bc_bufs[0] == cbp)
2798 cur->bc_bufs[0] = NULL;
2799 xfs_iroot_realloc(ip, -1, whichfork);
2800 ASSERT(ifp->if_broot == NULL);
2801 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2802 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002803 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 return 0;
2805}
2806
2807/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002808 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 * after removing space (or undoing a delayed allocation).
2810 */
2811STATIC int /* error */
2812xfs_bmap_del_extent(
2813 xfs_inode_t *ip, /* incore inode pointer */
2814 xfs_trans_t *tp, /* current transaction pointer */
2815 xfs_extnum_t idx, /* extent number to update/delete */
2816 xfs_bmap_free_t *flist, /* list of extents to be freed */
2817 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002818 xfs_bmbt_irec_t *del, /* data to remove from extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 int *logflagsp, /* inode logging flags */
Christoph Hellwig54893272011-05-11 15:04:03 +00002820 int whichfork) /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
2822 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2823 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2824 xfs_fsblock_t del_endblock=0; /* first block past del */
2825 xfs_fileoff_t del_endoff; /* first offset past del */
2826 int delay; /* current block is delayed allocated */
2827 int do_fx; /* free extent at end of routine */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002828 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 int error; /* error return value */
2830 int flags; /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 xfs_bmbt_irec_t got; /* current extent entry */
2832 xfs_fileoff_t got_endoff; /* first offset past got */
2833 int i; /* temp state */
2834 xfs_ifork_t *ifp; /* inode fork pointer */
2835 xfs_mount_t *mp; /* mount structure */
2836 xfs_filblks_t nblks; /* quota/sb block count */
2837 xfs_bmbt_irec_t new; /* new record to be inserted */
2838 /* REFERENCED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 uint qfield; /* quota field to update */
2840 xfs_filblks_t temp; /* for indirect length calculations */
2841 xfs_filblks_t temp2; /* for indirect length calculations */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002842 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
2844 XFS_STATS_INC(xs_del_exlist);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002845
2846 if (whichfork == XFS_ATTR_FORK)
2847 state |= BMAP_ATTRFORK;
2848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 mp = ip->i_mount;
2850 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002851 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
2852 (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 ASSERT(del->br_blockcount > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002854 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 xfs_bmbt_get_all(ep, &got);
2856 ASSERT(got.br_startoff <= del->br_startoff);
2857 del_endoff = del->br_startoff + del->br_blockcount;
2858 got_endoff = got.br_startoff + got.br_blockcount;
2859 ASSERT(got_endoff >= del_endoff);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002860 delay = isnullstartblock(got.br_startblock);
2861 ASSERT(isnullstartblock(del->br_startblock) == delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 flags = 0;
2863 qfield = 0;
2864 error = 0;
2865 /*
2866 * If deleting a real allocation, must free up the disk space.
2867 */
2868 if (!delay) {
2869 flags = XFS_ILOG_CORE;
2870 /*
2871 * Realtime allocation. Free it and record di_nblocks update.
2872 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002873 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 xfs_fsblock_t bno;
2875 xfs_filblks_t len;
2876
2877 ASSERT(do_mod(del->br_blockcount,
2878 mp->m_sb.sb_rextsize) == 0);
2879 ASSERT(do_mod(del->br_startblock,
2880 mp->m_sb.sb_rextsize) == 0);
2881 bno = del->br_startblock;
2882 len = del->br_blockcount;
2883 do_div(bno, mp->m_sb.sb_rextsize);
2884 do_div(len, mp->m_sb.sb_rextsize);
2885 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
2886 (xfs_extlen_t)len)))
2887 goto done;
2888 do_fx = 0;
2889 nblks = len * mp->m_sb.sb_rextsize;
2890 qfield = XFS_TRANS_DQ_RTBCOUNT;
2891 }
2892 /*
2893 * Ordinary allocation.
2894 */
2895 else {
2896 do_fx = 1;
2897 nblks = del->br_blockcount;
2898 qfield = XFS_TRANS_DQ_BCOUNT;
2899 }
2900 /*
2901 * Set up del_endblock and cur for later.
2902 */
2903 del_endblock = del->br_startblock + del->br_blockcount;
2904 if (cur) {
2905 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2906 got.br_startblock, got.br_blockcount,
2907 &i)))
2908 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002909 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 }
2911 da_old = da_new = 0;
2912 } else {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002913 da_old = startblockval(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 da_new = 0;
2915 nblks = 0;
2916 do_fx = 0;
2917 }
2918 /*
2919 * Set flag value to use in switch statement.
2920 * Left-contig is 2, right-contig is 1.
2921 */
2922 switch (((got.br_startoff == del->br_startoff) << 1) |
2923 (got_endoff == del_endoff)) {
2924 case 3:
2925 /*
2926 * Matches the whole extent. Delete the entry.
2927 */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002928 xfs_iext_remove(ip, idx, 1,
2929 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 ifp->if_lastex = idx;
2931 if (delay)
2932 break;
2933 XFS_IFORK_NEXT_SET(ip, whichfork,
2934 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2935 flags |= XFS_ILOG_CORE;
2936 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002937 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 break;
2939 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002940 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002942 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 break;
2944
2945 case 2:
2946 /*
2947 * Deleting the first part of the extent.
2948 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002949 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 xfs_bmbt_set_startoff(ep, del_endoff);
2951 temp = got.br_blockcount - del->br_blockcount;
2952 xfs_bmbt_set_blockcount(ep, temp);
2953 ifp->if_lastex = idx;
2954 if (delay) {
2955 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2956 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002957 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002958 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 da_new = temp;
2960 break;
2961 }
2962 xfs_bmbt_set_startblock(ep, del_endblock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002963 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002965 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 break;
2967 }
2968 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2969 got.br_blockcount - del->br_blockcount,
2970 got.br_state)))
2971 goto done;
2972 break;
2973
2974 case 1:
2975 /*
2976 * Deleting the last part of the extent.
2977 */
2978 temp = got.br_blockcount - del->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002979 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 xfs_bmbt_set_blockcount(ep, temp);
2981 ifp->if_lastex = idx;
2982 if (delay) {
2983 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2984 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002985 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002986 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 da_new = temp;
2988 break;
2989 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002990 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002992 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 break;
2994 }
2995 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2996 got.br_startblock,
2997 got.br_blockcount - del->br_blockcount,
2998 got.br_state)))
2999 goto done;
3000 break;
3001
3002 case 0:
3003 /*
3004 * Deleting the middle of the extent.
3005 */
3006 temp = del->br_startoff - got.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003007 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 xfs_bmbt_set_blockcount(ep, temp);
3009 new.br_startoff = del_endoff;
3010 temp2 = got_endoff - del_endoff;
3011 new.br_blockcount = temp2;
3012 new.br_state = got.br_state;
3013 if (!delay) {
3014 new.br_startblock = del_endblock;
3015 flags |= XFS_ILOG_CORE;
3016 if (cur) {
3017 if ((error = xfs_bmbt_update(cur,
3018 got.br_startoff,
3019 got.br_startblock, temp,
3020 got.br_state)))
3021 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11003022 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 goto done;
3024 cur->bc_rec.b = new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003025 error = xfs_btree_insert(cur, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 if (error && error != ENOSPC)
3027 goto done;
3028 /*
3029 * If get no-space back from btree insert,
3030 * it tried a split, and we have a zero
3031 * block reservation.
3032 * Fix up our state and return the error.
3033 */
3034 if (error == ENOSPC) {
3035 /*
3036 * Reset the cursor, don't trust
3037 * it after any insert operation.
3038 */
3039 if ((error = xfs_bmbt_lookup_eq(cur,
3040 got.br_startoff,
3041 got.br_startblock,
3042 temp, &i)))
3043 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003044 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 /*
3046 * Update the btree record back
3047 * to the original value.
3048 */
3049 if ((error = xfs_bmbt_update(cur,
3050 got.br_startoff,
3051 got.br_startblock,
3052 got.br_blockcount,
3053 got.br_state)))
3054 goto done;
3055 /*
3056 * Reset the extent record back
3057 * to the original value.
3058 */
3059 xfs_bmbt_set_blockcount(ep,
3060 got.br_blockcount);
3061 flags = 0;
3062 error = XFS_ERROR(ENOSPC);
3063 goto done;
3064 }
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003065 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 } else
Eric Sandeen9d87c312009-01-14 23:22:07 -06003067 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 XFS_IFORK_NEXT_SET(ip, whichfork,
3069 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3070 } else {
3071 ASSERT(whichfork == XFS_DATA_FORK);
3072 temp = xfs_bmap_worst_indlen(ip, temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003073 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003075 new.br_startblock = nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 da_new = temp + temp2;
3077 while (da_new > da_old) {
3078 if (temp) {
3079 temp--;
3080 da_new--;
3081 xfs_bmbt_set_startblock(ep,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003082 nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 }
3084 if (da_new == da_old)
3085 break;
3086 if (temp2) {
3087 temp2--;
3088 da_new--;
3089 new.br_startblock =
Eric Sandeen9d87c312009-01-14 23:22:07 -06003090 nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
3092 }
3093 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003094 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3095 xfs_iext_insert(ip, idx + 1, 1, &new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 ifp->if_lastex = idx + 1;
3097 break;
3098 }
3099 /*
3100 * If we need to, add to list of extents to delete.
3101 */
3102 if (do_fx)
3103 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3104 mp);
3105 /*
3106 * Adjust inode # blocks in the file.
3107 */
3108 if (nblks)
3109 ip->i_d.di_nblocks -= nblks;
3110 /*
3111 * Adjust quota data.
3112 */
3113 if (qfield)
Christoph Hellwig7d095252009-06-08 15:33:32 +02003114 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
3116 /*
3117 * Account for change in delayed indirect blocks.
3118 * Nothing to do for disk quota accounting here.
3119 */
3120 ASSERT(da_old >= da_new);
Christoph Hellwig96540c72010-09-30 02:25:55 +00003121 if (da_old > da_new) {
3122 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00003123 (int64_t)(da_old - da_new), 0);
Christoph Hellwig96540c72010-09-30 02:25:55 +00003124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125done:
3126 *logflagsp = flags;
3127 return error;
3128}
3129
3130/*
3131 * Remove the entry "free" from the free item list. Prev points to the
3132 * previous entry, unless "free" is the head of the list.
3133 */
3134STATIC void
3135xfs_bmap_del_free(
3136 xfs_bmap_free_t *flist, /* free item list header */
3137 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3138 xfs_bmap_free_item_t *free) /* list item to be freed */
3139{
3140 if (prev)
3141 prev->xbfi_next = free->xbfi_next;
3142 else
3143 flist->xbf_first = free->xbfi_next;
3144 flist->xbf_count--;
3145 kmem_zone_free(xfs_bmap_free_item_zone, free);
3146}
3147
3148/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 * Convert an extents-format file into a btree-format file.
3150 * The new file will have a root block (in the inode) and a single child block.
3151 */
3152STATIC int /* error */
3153xfs_bmap_extents_to_btree(
3154 xfs_trans_t *tp, /* transaction pointer */
3155 xfs_inode_t *ip, /* incore inode pointer */
3156 xfs_fsblock_t *firstblock, /* first-block-allocated */
3157 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3158 xfs_btree_cur_t **curp, /* cursor returned to caller */
3159 int wasdel, /* converting a delayed alloc */
3160 int *logflagsp, /* inode logging flags */
3161 int whichfork) /* data or attr fork */
3162{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003163 struct xfs_btree_block *ablock; /* allocated (child) bt block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 xfs_buf_t *abp; /* buffer for ablock */
3165 xfs_alloc_arg_t args; /* allocation arguments */
3166 xfs_bmbt_rec_t *arp; /* child record pointer */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003167 struct xfs_btree_block *block; /* btree root block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 xfs_btree_cur_t *cur; /* bmap btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003169 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003171 xfs_extnum_t i, cnt; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 xfs_ifork_t *ifp; /* inode fork pointer */
3173 xfs_bmbt_key_t *kp; /* root block key pointer */
3174 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003175 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3177
3178 ifp = XFS_IFORK_PTR(ip, whichfork);
3179 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3180 ASSERT(ifp->if_ext_max ==
3181 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3182 /*
3183 * Make space in the inode incore.
3184 */
3185 xfs_iroot_realloc(ip, 1, whichfork);
3186 ifp->if_flags |= XFS_IFBROOT;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003187
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 /*
3189 * Fill in the root.
3190 */
3191 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003192 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3193 block->bb_level = cpu_to_be16(1);
3194 block->bb_numrecs = cpu_to_be16(1);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003195 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3196 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 /*
3199 * Need a cursor. Can't allocate until bb_level is filled in.
3200 */
3201 mp = ip->i_mount;
Christoph Hellwig561f7d12008-10-30 16:53:59 +11003202 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 cur->bc_private.b.firstblock = *firstblock;
3204 cur->bc_private.b.flist = flist;
3205 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3206 /*
3207 * Convert to a btree with two levels, one record in root.
3208 */
3209 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3210 args.tp = tp;
3211 args.mp = mp;
Yingping Lud210a282006-06-09 14:55:18 +10003212 args.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 if (*firstblock == NULLFSBLOCK) {
3214 args.type = XFS_ALLOCTYPE_START_BNO;
3215 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3216 } else if (flist->xbf_low) {
3217 args.type = XFS_ALLOCTYPE_START_BNO;
3218 args.fsbno = *firstblock;
3219 } else {
3220 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3221 args.fsbno = *firstblock;
3222 }
3223 args.minlen = args.maxlen = args.prod = 1;
3224 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3225 args.minalignslop = 0;
3226 args.wasdel = wasdel;
3227 *logflagsp = 0;
3228 if ((error = xfs_alloc_vextent(&args))) {
3229 xfs_iroot_realloc(ip, -1, whichfork);
3230 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3231 return error;
3232 }
3233 /*
3234 * Allocation can't fail, the space was reserved.
3235 */
3236 ASSERT(args.fsbno != NULLFSBLOCK);
3237 ASSERT(*firstblock == NULLFSBLOCK ||
3238 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3239 (flist->xbf_low &&
3240 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3241 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3242 cur->bc_private.b.allocated++;
3243 ip->i_d.di_nblocks++;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003244 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3246 /*
3247 * Fill in the child block.
3248 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003249 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003250 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 ablock->bb_level = 0;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003252 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3253 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003254 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003256 for (cnt = i = 0; i < nextents; i++) {
3257 ep = xfs_iext_get_ext(ifp, i);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003258 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10003259 arp->l0 = cpu_to_be64(ep->l0);
3260 arp->l1 = cpu_to_be64(ep->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 arp++; cnt++;
3262 }
3263 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11003264 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003265 xfs_btree_set_numrecs(ablock, cnt);
3266
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 /*
3268 * Fill in the root key and pointer.
3269 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11003270 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3271 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Christoph Hellwig8801bb92006-09-28 10:58:17 +10003272 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
Christoph Hellwig136341b2008-10-30 17:11:40 +11003273 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3274 be16_to_cpu(block->bb_level)));
Christoph Hellwig576039c2006-09-28 10:58:06 +10003275 *pp = cpu_to_be64(args.fsbno);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003276
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 /*
3278 * Do all this logging at the end so that
3279 * the root is at the right level.
3280 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11003281 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3282 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 ASSERT(*curp == NULL);
3284 *curp = cur;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003285 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 return 0;
3287}
3288
3289/*
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003290 * Calculate the default attribute fork offset for newly created inodes.
3291 */
3292uint
3293xfs_default_attroffset(
3294 struct xfs_inode *ip)
3295{
3296 struct xfs_mount *mp = ip->i_mount;
3297 uint offset;
3298
3299 if (mp->m_sb.sb_inodesize == 256) {
3300 offset = XFS_LITINO(mp) -
3301 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3302 } else {
3303 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3304 }
3305
3306 ASSERT(offset < XFS_LITINO(mp));
3307 return offset;
3308}
3309
3310/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11003311 * Helper routine to reset inode di_forkoff field when switching
3312 * attribute fork from local to extent format - we reset it where
3313 * possible to make space available for inline data fork extents.
3314 */
3315STATIC void
3316xfs_bmap_forkoff_reset(
3317 xfs_mount_t *mp,
3318 xfs_inode_t *ip,
3319 int whichfork)
3320{
3321 if (whichfork == XFS_ATTR_FORK &&
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003322 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3323 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3324 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3325 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3326
3327 if (dfl_forkoff > ip->i_d.di_forkoff) {
3328 ip->i_d.di_forkoff = dfl_forkoff;
3329 ip->i_df.if_ext_max =
3330 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3331 ip->i_afp->if_ext_max =
3332 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3333 }
Nathan Scottd8cc8902005-11-02 10:34:53 +11003334 }
3335}
3336
3337/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 * Convert a local file to an extents file.
3339 * This code is out of bounds for data forks of regular files,
3340 * since the file data needs to get logged so things will stay consistent.
3341 * (The bmap-level manipulations are ok, though).
3342 */
3343STATIC int /* error */
3344xfs_bmap_local_to_extents(
3345 xfs_trans_t *tp, /* transaction pointer */
3346 xfs_inode_t *ip, /* incore inode pointer */
3347 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3348 xfs_extlen_t total, /* total blocks needed by transaction */
3349 int *logflagsp, /* inode logging flags */
3350 int whichfork) /* data or attr fork */
3351{
3352 int error; /* error return value */
3353 int flags; /* logging flags returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 xfs_ifork_t *ifp; /* inode fork pointer */
3355
3356 /*
3357 * We don't want to deal with the case of keeping inode data inline yet.
3358 * So sending the data fork of a regular inode is invalid.
3359 */
3360 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3361 whichfork == XFS_DATA_FORK));
3362 ifp = XFS_IFORK_PTR(ip, whichfork);
3363 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3364 flags = 0;
3365 error = 0;
3366 if (ifp->if_bytes) {
3367 xfs_alloc_arg_t args; /* allocation arguments */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003368 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003369 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
3371 args.tp = tp;
3372 args.mp = ip->i_mount;
Yingping Lud210a282006-06-09 14:55:18 +10003373 args.firstblock = *firstblock;
Mandy Kirkconnellf020b672006-03-14 14:07:24 +11003374 ASSERT((ifp->if_flags &
3375 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 /*
3377 * Allocate a block. We know we need only one, since the
3378 * file currently fits in an inode.
3379 */
3380 if (*firstblock == NULLFSBLOCK) {
3381 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3382 args.type = XFS_ALLOCTYPE_START_BNO;
3383 } else {
3384 args.fsbno = *firstblock;
3385 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3386 }
3387 args.total = total;
3388 args.mod = args.minleft = args.alignment = args.wasdel =
3389 args.isfl = args.minalignslop = 0;
3390 args.minlen = args.maxlen = args.prod = 1;
3391 if ((error = xfs_alloc_vextent(&args)))
3392 goto done;
3393 /*
3394 * Can't fail, the space was reserved.
3395 */
3396 ASSERT(args.fsbno != NULLFSBLOCK);
3397 ASSERT(args.len == 1);
3398 *firstblock = args.fsbno;
3399 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3400 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3401 ifp->if_bytes);
3402 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003403 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003405 xfs_iext_add(ifp, 0, 1);
3406 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003408 trace_xfs_bmap_post_update(ip, 0,
3409 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3410 _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3412 ip->i_d.di_nblocks = 1;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003413 xfs_trans_mod_dquot_byino(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 XFS_TRANS_DQ_BCOUNT, 1L);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003415 flags |= xfs_ilog_fext(whichfork);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003416 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003418 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 ifp->if_flags &= ~XFS_IFINLINE;
3421 ifp->if_flags |= XFS_IFEXTENTS;
3422 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3423 flags |= XFS_ILOG_CORE;
3424done:
3425 *logflagsp = flags;
3426 return error;
3427}
3428
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003430 * Search the extent records for the entry containing block bno.
3431 * If bno lies in a hole, point to the next entry. If bno lies
3432 * past eof, *eofp will be set, and *prevp will contain the last
3433 * entry (null if none). Else, *lastxp will be set to the index
3434 * of the found entry; *gotp will contain the entry.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003435 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003436STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003437xfs_bmap_search_multi_extents(
3438 xfs_ifork_t *ifp, /* inode fork pointer */
3439 xfs_fileoff_t bno, /* block number searched for */
3440 int *eofp, /* out: end of file found */
3441 xfs_extnum_t *lastxp, /* out: last extent index */
3442 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3443 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3444{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003445 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003446 xfs_extnum_t lastx; /* last extent index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003447
3448 /*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003449 * Initialize the extent entry structure to catch access to
3450 * uninitialized br_startblock field.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003451 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003452 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3453 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3454 gotp->br_state = XFS_EXT_INVALID;
3455#if XFS_BIG_BLKNOS
3456 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3457#else
3458 gotp->br_startblock = 0xffffa5a5;
3459#endif
3460 prevp->br_startoff = NULLFILEOFF;
3461
3462 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3463 if (lastx > 0) {
3464 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3465 }
3466 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3467 xfs_bmbt_get_all(ep, gotp);
3468 *eofp = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003469 } else {
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003470 if (lastx > 0) {
3471 *gotp = *prevp;
3472 }
3473 *eofp = 1;
3474 ep = NULL;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003475 }
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003476 *lastxp = lastx;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003477 return ep;
3478}
3479
3480/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 * Search the extents list for the inode, for the extent containing bno.
3482 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3483 * *eofp will be set, and *prevp will contain the last entry (null if none).
3484 * Else, *lastxp will be set to the index of the found
3485 * entry; *gotp will contain the entry.
3486 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003487STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488xfs_bmap_search_extents(
3489 xfs_inode_t *ip, /* incore inode pointer */
3490 xfs_fileoff_t bno, /* block number searched for */
Nathan Scott572d95f2006-09-28 11:03:20 +10003491 int fork, /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 int *eofp, /* out: end of file found */
3493 xfs_extnum_t *lastxp, /* out: last extent index */
3494 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3495 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3496{
3497 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003498 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
3500 XFS_STATS_INC(xs_look_exlist);
Nathan Scott572d95f2006-09-28 11:03:20 +10003501 ifp = XFS_IFORK_PTR(ip, fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003503 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3504
Nathan Scott572d95f2006-09-28 11:03:20 +10003505 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3506 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003507 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
Nathan Scott572d95f2006-09-28 11:03:20 +10003508 "Access to block zero in inode %llu "
3509 "start_block: %llx start_off: %llx "
3510 "blkcnt: %llx extent-state: %x lastx: %x\n",
3511 (unsigned long long)ip->i_ino,
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003512 (unsigned long long)gotp->br_startblock,
3513 (unsigned long long)gotp->br_startoff,
3514 (unsigned long long)gotp->br_blockcount,
Nathan Scott572d95f2006-09-28 11:03:20 +10003515 gotp->br_state, *lastxp);
3516 *lastxp = NULLEXTNUM;
3517 *eofp = 1;
3518 return NULL;
3519 }
3520 return ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521}
3522
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523/*
3524 * Compute the worst-case number of indirect blocks that will be used
3525 * for ip's delayed extent of length "len".
3526 */
3527STATIC xfs_filblks_t
3528xfs_bmap_worst_indlen(
3529 xfs_inode_t *ip, /* incore inode pointer */
3530 xfs_filblks_t len) /* delayed extent length */
3531{
3532 int level; /* btree level number */
3533 int maxrecs; /* maximum record count at this level */
3534 xfs_mount_t *mp; /* mount structure */
3535 xfs_filblks_t rval; /* return value */
3536
3537 mp = ip->i_mount;
3538 maxrecs = mp->m_bmap_dmxr[0];
3539 for (level = 0, rval = 0;
3540 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3541 level++) {
3542 len += maxrecs - 1;
3543 do_div(len, maxrecs);
3544 rval += len;
3545 if (len == 1)
3546 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3547 level - 1;
3548 if (level == 0)
3549 maxrecs = mp->m_bmap_dmxr[1];
3550 }
3551 return rval;
3552}
3553
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554/*
3555 * Convert inode from non-attributed to attributed.
3556 * Must not be in a transaction, ip must not be locked.
3557 */
3558int /* error code */
3559xfs_bmap_add_attrfork(
3560 xfs_inode_t *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003561 int size, /* space new attribute needs */
3562 int rsvd) /* xact may use reserved blks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003565 xfs_bmap_free_t flist; /* freed extent records */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 xfs_trans_t *tp; /* transaction pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003568 int blks; /* space reservation */
3569 int version = 1; /* superblock attr version */
3570 int committed; /* xaction was committed */
3571 int logflags; /* logging flags */
3572 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Nathan Scottd8cc8902005-11-02 10:34:53 +11003574 ASSERT(XFS_IFORK_Q(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 ASSERT(ip->i_df.if_ext_max ==
3576 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
Nathan Scottd8cc8902005-11-02 10:34:53 +11003577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 mp = ip->i_mount;
3579 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3580 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3581 blks = XFS_ADDAFORK_SPACE_RES(mp);
3582 if (rsvd)
3583 tp->t_flags |= XFS_TRANS_RESERVE;
3584 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3585 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3586 goto error0;
3587 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02003588 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3590 XFS_QMOPT_RES_REGBLKS);
3591 if (error) {
3592 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3593 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3594 return error;
3595 }
3596 if (XFS_IFORK_Q(ip))
3597 goto error1;
3598 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3599 /*
3600 * For inodes coming from pre-6.2 filesystems.
3601 */
3602 ASSERT(ip->i_d.di_aformat == 0);
3603 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3604 }
3605 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwig898621d2010-06-24 11:36:58 +10003606
3607 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig898621d2010-06-24 11:36:58 +10003609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 switch (ip->i_d.di_format) {
3611 case XFS_DINODE_FMT_DEV:
3612 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3613 break;
3614 case XFS_DINODE_FMT_UUID:
3615 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3616 break;
3617 case XFS_DINODE_FMT_LOCAL:
3618 case XFS_DINODE_FMT_EXTENTS:
3619 case XFS_DINODE_FMT_BTREE:
Nathan Scottd8cc8902005-11-02 10:34:53 +11003620 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3621 if (!ip->i_d.di_forkoff)
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003622 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
Nathan Scott13059ff2006-01-11 15:32:01 +11003623 else if (mp->m_flags & XFS_MOUNT_ATTR2)
Nathan Scottd8cc8902005-11-02 10:34:53 +11003624 version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 break;
3626 default:
3627 ASSERT(0);
3628 error = XFS_ERROR(EINVAL);
3629 goto error1;
3630 }
3631 ip->i_df.if_ext_max =
3632 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3633 ASSERT(ip->i_afp == NULL);
3634 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3635 ip->i_afp->if_ext_max =
3636 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3637 ip->i_afp->if_flags = XFS_IFEXTENTS;
3638 logflags = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003639 xfs_bmap_init(&flist, &firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 switch (ip->i_d.di_format) {
3641 case XFS_DINODE_FMT_LOCAL:
3642 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3643 &logflags);
3644 break;
3645 case XFS_DINODE_FMT_EXTENTS:
3646 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3647 &flist, &logflags);
3648 break;
3649 case XFS_DINODE_FMT_BTREE:
3650 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3651 &logflags);
3652 break;
3653 default:
3654 error = 0;
3655 break;
3656 }
3657 if (logflags)
3658 xfs_trans_log_inode(tp, ip, logflags);
3659 if (error)
3660 goto error2;
Eric Sandeen62118702008-03-06 13:44:28 +11003661 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3662 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +11003663 __int64_t sbfields = 0;
3664
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003665 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +11003666 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3667 xfs_sb_version_addattr(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003668 sbfields |= XFS_SB_VERSIONNUM;
Nathan Scottd8cc8902005-11-02 10:34:53 +11003669 }
Eric Sandeen62118702008-03-06 13:44:28 +11003670 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3671 xfs_sb_version_addattr2(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003672 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003673 }
Nathan Scottda087ba2005-11-02 15:00:20 +11003674 if (sbfields) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003675 spin_unlock(&mp->m_sb_lock);
Nathan Scottda087ba2005-11-02 15:00:20 +11003676 xfs_mod_sb(tp, sbfields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003678 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 }
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003680 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 goto error2;
Dave Chinner713bf882010-03-08 11:26:23 +11003682 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 ASSERT(ip->i_df.if_ext_max ==
3684 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3685 return error;
3686error2:
3687 xfs_bmap_cancel(&flist);
3688error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3690error0:
3691 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3692 ASSERT(ip->i_df.if_ext_max ==
3693 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3694 return error;
3695}
3696
3697/*
3698 * Add the extent to the list of extents to be free at transaction end.
3699 * The list is maintained sorted (by block number).
3700 */
3701/* ARGSUSED */
3702void
3703xfs_bmap_add_free(
3704 xfs_fsblock_t bno, /* fs block number of extent */
3705 xfs_filblks_t len, /* length of extent */
3706 xfs_bmap_free_t *flist, /* list of extents */
3707 xfs_mount_t *mp) /* mount point structure */
3708{
3709 xfs_bmap_free_item_t *cur; /* current (next) element */
3710 xfs_bmap_free_item_t *new; /* new element */
3711 xfs_bmap_free_item_t *prev; /* previous element */
3712#ifdef DEBUG
3713 xfs_agnumber_t agno;
3714 xfs_agblock_t agbno;
3715
3716 ASSERT(bno != NULLFSBLOCK);
3717 ASSERT(len > 0);
3718 ASSERT(len <= MAXEXTLEN);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003719 ASSERT(!isnullstartblock(bno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 agno = XFS_FSB_TO_AGNO(mp, bno);
3721 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3722 ASSERT(agno < mp->m_sb.sb_agcount);
3723 ASSERT(agbno < mp->m_sb.sb_agblocks);
3724 ASSERT(len < mp->m_sb.sb_agblocks);
3725 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3726#endif
3727 ASSERT(xfs_bmap_free_item_zone != NULL);
3728 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3729 new->xbfi_startblock = bno;
3730 new->xbfi_blockcount = (xfs_extlen_t)len;
3731 for (prev = NULL, cur = flist->xbf_first;
3732 cur != NULL;
3733 prev = cur, cur = cur->xbfi_next) {
3734 if (cur->xbfi_startblock >= bno)
3735 break;
3736 }
3737 if (prev)
3738 prev->xbfi_next = new;
3739 else
3740 flist->xbf_first = new;
3741 new->xbfi_next = cur;
3742 flist->xbf_count++;
3743}
3744
3745/*
3746 * Compute and fill in the value of the maximum depth of a bmap btree
3747 * in this filesystem. Done once, during mount.
3748 */
3749void
3750xfs_bmap_compute_maxlevels(
3751 xfs_mount_t *mp, /* file system mount structure */
3752 int whichfork) /* data or attr fork */
3753{
3754 int level; /* btree level */
3755 uint maxblocks; /* max blocks at this level */
3756 uint maxleafents; /* max leaf entries possible */
3757 int maxrootrecs; /* max records in root block */
3758 int minleafrecs; /* min records in leaf block */
3759 int minnoderecs; /* min records in node block */
3760 int sz; /* root block size */
3761
3762 /*
3763 * The maximum number of extents in a file, hence the maximum
3764 * number of leaf entries, is controlled by the type of di_nextents
3765 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3766 * (a signed 16-bit number, xfs_aextnum_t).
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003767 *
3768 * Note that we can no longer assume that if we are in ATTR1 that
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003769 * the fork offset of all the inodes will be
3770 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3771 * with ATTR2 and then mounted back with ATTR1, keeping the
3772 * di_forkoff's fixed but probably at various positions. Therefore,
3773 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3774 * of a minimum size available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003776 if (whichfork == XFS_DATA_FORK) {
3777 maxleafents = MAXEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003778 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003779 } else {
3780 maxleafents = MAXAEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003781 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003782 }
Christoph Hellwig60197e82008-10-30 17:11:19 +11003783 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 minleafrecs = mp->m_bmap_dmnr[0];
3785 minnoderecs = mp->m_bmap_dmnr[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3787 for (level = 1; maxblocks > 1; level++) {
3788 if (maxblocks <= maxrootrecs)
3789 maxblocks = 1;
3790 else
3791 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3792 }
3793 mp->m_bm_maxlevels[whichfork] = level;
3794}
3795
3796/*
3797 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3798 * caller. Frees all the extents that need freeing, which must be done
3799 * last due to locking considerations. We never free any extents in
3800 * the first transaction. This is to allow the caller to make the first
3801 * transaction a synchronous one so that the pointers to the data being
3802 * broken in this transaction will be permanent before the data is actually
3803 * freed. This is necessary to prevent blocks from being reallocated
3804 * and written to before the free and reallocation are actually permanent.
3805 * We do not just make the first transaction synchronous here, because
3806 * there are more efficient ways to gain the same protection in some cases
3807 * (see the file truncation code).
3808 *
3809 * Return 1 if the given transaction was committed and a new one
3810 * started, and 0 otherwise in the committed parameter.
3811 */
3812/*ARGSUSED*/
3813int /* error */
3814xfs_bmap_finish(
3815 xfs_trans_t **tp, /* transaction pointer addr */
3816 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 int *committed) /* xact committed or not */
3818{
3819 xfs_efd_log_item_t *efd; /* extent free data */
3820 xfs_efi_log_item_t *efi; /* extent free intention */
3821 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003822 xfs_bmap_free_item_t *free; /* free extent item */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 unsigned int logres; /* new log reservation */
3824 unsigned int logcount; /* new log count */
3825 xfs_mount_t *mp; /* filesystem mount structure */
3826 xfs_bmap_free_item_t *next; /* next item on free list */
3827 xfs_trans_t *ntp; /* new transaction pointer */
3828
3829 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3830 if (flist->xbf_count == 0) {
3831 *committed = 0;
3832 return 0;
3833 }
3834 ntp = *tp;
3835 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3836 for (free = flist->xbf_first; free; free = free->xbfi_next)
3837 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3838 free->xbfi_blockcount);
3839 logres = ntp->t_log_res;
3840 logcount = ntp->t_log_count;
3841 ntp = xfs_trans_dup(*tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003842 error = xfs_trans_commit(*tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 *tp = ntp;
3844 *committed = 1;
3845 /*
3846 * We have a new transaction, so we should return committed=1,
3847 * even though we're returning an error.
3848 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003849 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 return error;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003851
3852 /*
3853 * transaction commit worked ok so we can drop the extra ticket
3854 * reference that we gained in xfs_trans_dup()
3855 */
3856 xfs_log_ticket_put(ntp->t_ticket);
3857
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3859 logcount)))
3860 return error;
3861 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3862 for (free = flist->xbf_first; free != NULL; free = next) {
3863 next = free->xbfi_next;
3864 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3865 free->xbfi_blockcount))) {
3866 /*
3867 * The bmap free list will be cleaned up at a
3868 * higher level. The EFI will be canceled when
3869 * this transaction is aborted.
3870 * Need to force shutdown here to make sure it
3871 * happens, since this transaction may not be
3872 * dirty yet.
3873 */
3874 mp = ntp->t_mountp;
3875 if (!XFS_FORCED_SHUTDOWN(mp))
3876 xfs_force_shutdown(mp,
3877 (error == EFSCORRUPTED) ?
Nathan Scott7d04a332006-06-09 14:58:38 +10003878 SHUTDOWN_CORRUPT_INCORE :
3879 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 return error;
3881 }
3882 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3883 free->xbfi_blockcount);
3884 xfs_bmap_del_free(flist, NULL, free);
3885 }
3886 return 0;
3887}
3888
3889/*
3890 * Free up any items left in the list.
3891 */
3892void
3893xfs_bmap_cancel(
3894 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3895{
3896 xfs_bmap_free_item_t *free; /* free list item */
3897 xfs_bmap_free_item_t *next;
3898
3899 if (flist->xbf_count == 0)
3900 return;
3901 ASSERT(flist->xbf_first != NULL);
3902 for (free = flist->xbf_first; free; free = next) {
3903 next = free->xbfi_next;
3904 xfs_bmap_del_free(flist, NULL, free);
3905 }
3906 ASSERT(flist->xbf_count == 0);
3907}
3908
3909/*
3910 * Returns the file-relative block number of the first unused block(s)
3911 * in the file with at least "len" logically contiguous blocks free.
3912 * This is the lowest-address hole if the file has holes, else the first block
3913 * past the end of file.
3914 * Return 0 if the file is currently local (in-inode).
3915 */
3916int /* error */
3917xfs_bmap_first_unused(
3918 xfs_trans_t *tp, /* transaction pointer */
3919 xfs_inode_t *ip, /* incore inode */
3920 xfs_extlen_t len, /* size of hole to find */
3921 xfs_fileoff_t *first_unused, /* unused block */
3922 int whichfork) /* data or attr fork */
3923{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003925 int idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 xfs_ifork_t *ifp; /* inode fork pointer */
3927 xfs_fileoff_t lastaddr; /* last block number seen */
3928 xfs_fileoff_t lowest; /* lowest useful block */
3929 xfs_fileoff_t max; /* starting useful block */
3930 xfs_fileoff_t off; /* offset for this block */
3931 xfs_extnum_t nextents; /* number of extent entries */
3932
3933 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3934 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3935 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3936 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3937 *first_unused = 0;
3938 return 0;
3939 }
3940 ifp = XFS_IFORK_PTR(ip, whichfork);
3941 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3942 (error = xfs_iread_extents(tp, ip, whichfork)))
3943 return error;
3944 lowest = *first_unused;
3945 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003946 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003947 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 off = xfs_bmbt_get_startoff(ep);
3949 /*
3950 * See if the hole before this extent will work.
3951 */
3952 if (off >= lowest + len && off - max >= len) {
3953 *first_unused = max;
3954 return 0;
3955 }
3956 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3957 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3958 }
3959 *first_unused = max;
3960 return 0;
3961}
3962
3963/*
3964 * Returns the file-relative block number of the last block + 1 before
3965 * last_block (input value) in the file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003966 * This is not based on i_size, it is based on the extent records.
3967 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 */
3969int /* error */
3970xfs_bmap_last_before(
3971 xfs_trans_t *tp, /* transaction pointer */
3972 xfs_inode_t *ip, /* incore inode */
3973 xfs_fileoff_t *last_block, /* last block */
3974 int whichfork) /* data or attr fork */
3975{
3976 xfs_fileoff_t bno; /* input file offset */
3977 int eof; /* hit end of file */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003978 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 int error; /* error return value */
3980 xfs_bmbt_irec_t got; /* current extent value */
3981 xfs_ifork_t *ifp; /* inode fork pointer */
3982 xfs_extnum_t lastx; /* last extent used */
3983 xfs_bmbt_irec_t prev; /* previous extent value */
3984
3985 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3986 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3987 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3988 return XFS_ERROR(EIO);
3989 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3990 *last_block = 0;
3991 return 0;
3992 }
3993 ifp = XFS_IFORK_PTR(ip, whichfork);
3994 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3995 (error = xfs_iread_extents(tp, ip, whichfork)))
3996 return error;
3997 bno = *last_block - 1;
3998 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3999 &prev);
4000 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4001 if (prev.br_startoff == NULLFILEOFF)
4002 *last_block = 0;
4003 else
4004 *last_block = prev.br_startoff + prev.br_blockcount;
4005 }
4006 /*
4007 * Otherwise *last_block is already the right answer.
4008 */
4009 return 0;
4010}
4011
4012/*
4013 * Returns the file-relative block number of the first block past eof in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004014 * the file. This is not based on i_size, it is based on the extent records.
4015 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 */
4017int /* error */
4018xfs_bmap_last_offset(
4019 xfs_trans_t *tp, /* transaction pointer */
4020 xfs_inode_t *ip, /* incore inode */
4021 xfs_fileoff_t *last_block, /* last block */
4022 int whichfork) /* data or attr fork */
4023{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004024 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 int error; /* error return value */
4026 xfs_ifork_t *ifp; /* inode fork pointer */
4027 xfs_extnum_t nextents; /* number of extent entries */
4028
4029 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4030 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4031 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4032 return XFS_ERROR(EIO);
4033 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4034 *last_block = 0;
4035 return 0;
4036 }
4037 ifp = XFS_IFORK_PTR(ip, whichfork);
4038 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4039 (error = xfs_iread_extents(tp, ip, whichfork)))
4040 return error;
4041 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4042 if (!nextents) {
4043 *last_block = 0;
4044 return 0;
4045 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004046 ep = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4048 return 0;
4049}
4050
4051/*
4052 * Returns whether the selected fork of the inode has exactly one
4053 * block or not. For the data fork we check this matches di_size,
4054 * implying the file's range is 0..bsize-1.
4055 */
4056int /* 1=>1 block, 0=>otherwise */
4057xfs_bmap_one_block(
4058 xfs_inode_t *ip, /* incore inode */
4059 int whichfork) /* data or attr fork */
4060{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004061 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 xfs_ifork_t *ifp; /* inode fork pointer */
4063 int rval; /* return value */
4064 xfs_bmbt_irec_t s; /* internal version of extent */
4065
4066#ifndef DEBUG
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004067 if (whichfork == XFS_DATA_FORK) {
4068 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4069 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4070 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072#endif /* !DEBUG */
4073 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4074 return 0;
4075 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4076 return 0;
4077 ifp = XFS_IFORK_PTR(ip, whichfork);
4078 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004079 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 xfs_bmbt_get_all(ep, &s);
4081 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4082 if (rval && whichfork == XFS_DATA_FORK)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004083 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 return rval;
4085}
4086
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004087STATIC int
4088xfs_bmap_sanity_check(
4089 struct xfs_mount *mp,
4090 struct xfs_buf *bp,
4091 int level)
4092{
4093 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4094
4095 if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
4096 be16_to_cpu(block->bb_level) != level ||
4097 be16_to_cpu(block->bb_numrecs) == 0 ||
4098 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4099 return 0;
4100 return 1;
4101}
4102
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103/*
4104 * Read in the extents to if_extents.
4105 * All inode fields are set up by caller, we just traverse the btree
4106 * and copy the records in. If the file system cannot contain unwritten
4107 * extents, the records are checked for no "state" flags.
4108 */
4109int /* error */
4110xfs_bmap_read_extents(
4111 xfs_trans_t *tp, /* transaction pointer */
4112 xfs_inode_t *ip, /* incore inode */
4113 int whichfork) /* data or attr fork */
4114{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004115 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 xfs_fsblock_t bno; /* block # of "block" */
4117 xfs_buf_t *bp; /* buffer for "block" */
4118 int error; /* error return value */
4119 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 xfs_extnum_t i, j; /* index into the extents list */
4121 xfs_ifork_t *ifp; /* fork structure */
4122 int level; /* btree level, for checking */
4123 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10004124 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 /* REFERENCED */
4126 xfs_extnum_t room; /* number of entries there's room for */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
4128 bno = NULLFSBLOCK;
4129 mp = ip->i_mount;
4130 ifp = XFS_IFORK_PTR(ip, whichfork);
4131 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4132 XFS_EXTFMT_INODE(ip);
4133 block = ifp->if_broot;
4134 /*
4135 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4136 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004137 level = be16_to_cpu(block->bb_level);
4138 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11004139 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004140 bno = be64_to_cpu(*pp);
4141 ASSERT(bno != NULLDFSBNO);
4142 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4143 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 /*
4145 * Go down the tree until leaf level is reached, following the first
4146 * pointer (leftmost) at each level.
4147 */
4148 while (level-- > 0) {
4149 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4150 XFS_BMAP_BTREE_REF)))
4151 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004152 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004154 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 error0);
4156 if (level == 0)
4157 break;
Christoph Hellwig136341b2008-10-30 17:11:40 +11004158 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004159 bno = be64_to_cpu(*pp);
4160 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 xfs_trans_brelse(tp, bp);
4162 }
4163 /*
4164 * Here with bp and block set to the leftmost leaf node in the tree.
4165 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004166 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 i = 0;
4168 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004169 * Loop over all leaf nodes. Copy information to the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 */
4171 for (;;) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004172 xfs_bmbt_rec_t *frp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 xfs_fsblock_t nextbno;
4174 xfs_extnum_t num_recs;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004175 xfs_extnum_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
4177
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004178 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 if (unlikely(i + num_recs > room)) {
4180 ASSERT(i + num_recs <= room);
Dave Chinner65333b42011-03-07 10:03:35 +11004181 xfs_warn(ip->i_mount,
Nathan Scott3762ec62006-01-12 10:29:53 +11004182 "corrupt dinode %Lu, (btree extents).",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 (unsigned long long) ip->i_ino);
Dave Chinner65333b42011-03-07 10:03:35 +11004184 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
4185 XFS_ERRLEVEL_LOW, ip->i_mount, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 goto error0;
4187 }
4188 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004189 xfs_bmap_sanity_check(mp, bp, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 error0);
4191 /*
4192 * Read-ahead the next leaf block, if any.
4193 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004194 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 if (nextbno != NULLFSBLOCK)
4196 xfs_btree_reada_bufl(mp, nextbno, 1);
4197 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004198 * Copy records into the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11004200 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004201 start = i;
4202 for (j = 0; j < num_recs; j++, i++, frp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004203 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10004204 trp->l0 = be64_to_cpu(frp->l0);
4205 trp->l1 = be64_to_cpu(frp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 }
4207 if (exntf == XFS_EXTFMT_NOSTATE) {
4208 /*
4209 * Check all attribute bmap btree records and
4210 * any "older" data bmap btree records for a
4211 * set bit in the "extent flag" position.
4212 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004213 if (unlikely(xfs_check_nostate_extents(ifp,
4214 start, num_recs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4216 XFS_ERRLEVEL_LOW,
4217 ip->i_mount);
4218 goto error0;
4219 }
4220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 xfs_trans_brelse(tp, bp);
4222 bno = nextbno;
4223 /*
4224 * If we've reached the end, stop.
4225 */
4226 if (bno == NULLFSBLOCK)
4227 break;
4228 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4229 XFS_BMAP_BTREE_REF)))
4230 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004231 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004233 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
Eric Sandeen3a59c942007-07-11 11:09:47 +10004235 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 return 0;
4237error0:
4238 xfs_trans_brelse(tp, bp);
4239 return XFS_ERROR(EFSCORRUPTED);
4240}
4241
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004242#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004244 * Add bmap trace insert entries for all the contents of the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 */
4246void
4247xfs_bmap_trace_exlist(
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 xfs_inode_t *ip, /* incore inode pointer */
4249 xfs_extnum_t cnt, /* count of entries in the list */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004250 int whichfork, /* data or attr fork */
4251 unsigned long caller_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004253 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004255 int state = 0;
4256
4257 if (whichfork == XFS_ATTR_FORK)
4258 state |= BMAP_ATTRFORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
4260 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004261 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004262 for (idx = 0; idx < cnt; idx++)
4263 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266/*
4267 * Validate that the bmbt_irecs being returned from bmapi are valid
4268 * given the callers original parameters. Specifically check the
4269 * ranges of the returned irecs to ensure that they only extent beyond
4270 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4271 */
4272STATIC void
4273xfs_bmap_validate_ret(
4274 xfs_fileoff_t bno,
4275 xfs_filblks_t len,
4276 int flags,
4277 xfs_bmbt_irec_t *mval,
4278 int nmap,
4279 int ret_nmap)
4280{
4281 int i; /* index to map values */
4282
4283 ASSERT(ret_nmap <= nmap);
4284
4285 for (i = 0; i < ret_nmap; i++) {
4286 ASSERT(mval[i].br_blockcount > 0);
4287 if (!(flags & XFS_BMAPI_ENTIRE)) {
4288 ASSERT(mval[i].br_startoff >= bno);
4289 ASSERT(mval[i].br_blockcount <= len);
4290 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4291 bno + len);
4292 } else {
4293 ASSERT(mval[i].br_startoff < bno + len);
4294 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4295 bno);
4296 }
4297 ASSERT(i == 0 ||
4298 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4299 mval[i].br_startoff);
4300 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4301 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4302 mval[i].br_startblock != HOLESTARTBLOCK);
4303 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4304 mval[i].br_state == XFS_EXT_UNWRITTEN);
4305 }
4306}
4307#endif /* DEBUG */
4308
4309
4310/*
4311 * Map file blocks to filesystem blocks.
4312 * File range is given by the bno/len pair.
4313 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4314 * into a hole or past eof.
4315 * Only allocates blocks from a single allocation group,
4316 * to avoid locking problems.
4317 * The returned value in "firstblock" from the first call in a transaction
4318 * must be remembered and presented to subsequent calls in "firstblock".
4319 * An upper bound for the number of blocks to be allocated is supplied to
4320 * the first call in "total"; if no allocation group has that many free
4321 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4322 */
4323int /* error */
4324xfs_bmapi(
4325 xfs_trans_t *tp, /* transaction pointer */
4326 xfs_inode_t *ip, /* incore inode */
4327 xfs_fileoff_t bno, /* starting file offs. mapped */
4328 xfs_filblks_t len, /* length to map in file */
4329 int flags, /* XFS_BMAPI_... */
4330 xfs_fsblock_t *firstblock, /* first allocated block
4331 controls a.g. for allocs */
4332 xfs_extlen_t total, /* total blocks needed */
4333 xfs_bmbt_irec_t *mval, /* output: map values */
4334 int *nmap, /* i/o: mval size/count */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004335 xfs_bmap_free_t *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336{
4337 xfs_fsblock_t abno; /* allocated block number */
4338 xfs_extlen_t alen; /* allocated extent length */
4339 xfs_fileoff_t aoff; /* allocated file offset */
Dave Chinnerf0a0eaa2010-01-20 10:50:06 +11004340 xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 xfs_btree_cur_t *cur; /* bmap btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 xfs_fileoff_t end; /* end of mapped file region */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004343 int eof; /* we've hit the end of extents */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004344 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004346 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 xfs_ifork_t *ifp; /* inode fork pointer */
4348 xfs_extlen_t indlen; /* indirect blocks length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 xfs_extnum_t lastx; /* last useful extent number */
4350 int logflags; /* flags for transaction logging */
4351 xfs_extlen_t minleft; /* min blocks left after allocation */
4352 xfs_extlen_t minlen; /* min allocation size */
4353 xfs_mount_t *mp; /* xfs mount structure */
4354 int n; /* current extent index */
Malcolm Parsons9da096f2009-03-29 09:55:42 +02004355 int nallocs; /* number of extents alloc'd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 xfs_extnum_t nextents; /* number of extents in file */
4357 xfs_fileoff_t obno; /* old block number (offset) */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004358 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 int tmp_logflags; /* temp flags holder */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004360 int whichfork; /* data or attr fork */
4361 char inhole; /* current location is hole in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 char wasdelay; /* old extent was delayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 char wr; /* this is a write request */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004364 char rt; /* this is a realtime file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365#ifdef DEBUG
4366 xfs_fileoff_t orig_bno; /* original block number value */
4367 int orig_flags; /* original flags arg value */
4368 xfs_filblks_t orig_len; /* original value of len arg */
4369 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4370 int orig_nmap; /* original value of *nmap */
4371
4372 orig_bno = bno;
4373 orig_len = len;
4374 orig_flags = flags;
4375 orig_mval = mval;
4376 orig_nmap = *nmap;
4377#endif
4378 ASSERT(*nmap >= 1);
4379 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4380 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4381 XFS_ATTR_FORK : XFS_DATA_FORK;
4382 mp = ip->i_mount;
4383 if (unlikely(XFS_TEST_ERROR(
4384 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4385 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4386 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4387 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4388 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4389 return XFS_ERROR(EFSCORRUPTED);
4390 }
4391 if (XFS_FORCED_SHUTDOWN(mp))
4392 return XFS_ERROR(EIO);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004393 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 ifp = XFS_IFORK_PTR(ip, whichfork);
4395 ASSERT(ifp->if_ext_max ==
4396 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4397 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4398 XFS_STATS_INC(xs_blk_mapw);
4399 else
4400 XFS_STATS_INC(xs_blk_mapr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 /*
Nathan Scott39269e22006-03-14 13:33:50 +11004402 * IGSTATE flag is used to combine extents which
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 * differ only due to the state of the extents.
4404 * This technique is used from xfs_getbmap()
4405 * when the caller does not wish to see the
4406 * separation (which is the default).
4407 *
4408 * This technique is also used when writing a
4409 * buffer which has been partially written,
4410 * (usually by being flushed during a chunkread),
4411 * to ensure one write takes place. This also
4412 * prevents a change in the xfs inode extents at
4413 * this time, intentionally. This change occurs
4414 * on completion of the write operation, in
4415 * xfs_strat_comp(), where the xfs_bmapi() call
4416 * is transactioned, and the extents combined.
4417 */
Nathan Scott39269e22006-03-14 13:33:50 +11004418 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4419 wr = 0; /* no allocations are allowed */
4420 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 logflags = 0;
4422 nallocs = 0;
4423 cur = NULL;
4424 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4425 ASSERT(wr && tp);
4426 if ((error = xfs_bmap_local_to_extents(tp, ip,
4427 firstblock, total, &logflags, whichfork)))
4428 goto error0;
4429 }
4430 if (wr && *firstblock == NULLFSBLOCK) {
4431 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Christoph Hellwig16259e72005-11-02 15:11:25 +11004432 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 else
4434 minleft = 1;
4435 } else
4436 minleft = 0;
4437 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4438 (error = xfs_iread_extents(tp, ip, whichfork)))
4439 goto error0;
4440 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4441 &prev);
4442 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4443 n = 0;
4444 end = bno + len;
4445 obno = bno;
4446 bma.ip = NULL;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 while (bno < end && n < *nmap) {
4449 /*
4450 * Reading past eof, act as though there's a hole
4451 * up to end.
4452 */
4453 if (eof && !wr)
4454 got.br_startoff = end;
4455 inhole = eof || got.br_startoff > bno;
Nathan Scott39269e22006-03-14 13:33:50 +11004456 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06004457 isnullstartblock(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 /*
4459 * First, deal with the hole before the allocated space
4460 * that we found, if any.
4461 */
4462 if (wr && (inhole || wasdelay)) {
4463 /*
4464 * For the wasdelay case, we could also just
4465 * allocate the stuff asked for in this bmap call
4466 * but that wouldn't be as good.
4467 */
Christoph Hellwigcd8b0bb2010-06-23 18:11:15 +10004468 if (wasdelay) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 alen = (xfs_extlen_t)got.br_blockcount;
4470 aoff = got.br_startoff;
4471 if (lastx != NULLEXTNUM && lastx) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004472 ep = xfs_iext_get_ext(ifp, lastx - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 xfs_bmbt_get_all(ep, &prev);
4474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 } else {
4476 alen = (xfs_extlen_t)
4477 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4478 if (!eof)
4479 alen = (xfs_extlen_t)
4480 XFS_FILBLKS_MIN(alen,
4481 got.br_startoff - bno);
4482 aoff = bno;
4483 }
Nathan Scott39269e22006-03-14 13:33:50 +11004484 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4485 if (flags & XFS_BMAPI_DELAY) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11004486 xfs_extlen_t extsz;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004487
4488 /* Figure out the extent size, adjust alen */
David Chinner957d0eb2007-06-18 16:50:37 +10004489 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004490 if (extsz) {
Dave Chinner4ce15982011-01-27 12:17:58 +11004491 /*
4492 * make sure we don't exceed a single
4493 * extent length when we align the
4494 * extent by reducing length we are
4495 * going to allocate by the maximum
4496 * amount extent size aligment may
4497 * require.
4498 */
4499 alen = XFS_FILBLKS_MIN(len,
4500 MAXEXTLEN - (2 * extsz - 1));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004501 error = xfs_bmap_extsize_align(mp,
4502 &got, &prev, extsz,
Nathan Scott39269e22006-03-14 13:33:50 +11004503 rt, eof,
4504 flags&XFS_BMAPI_DELAY,
4505 flags&XFS_BMAPI_CONVERT,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004506 &aoff, &alen);
4507 ASSERT(!error);
4508 }
4509
4510 if (rt)
4511 extsz = alen / mp->m_sb.sb_rextsize;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004512
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 /*
4514 * Make a transaction-less quota reservation for
4515 * delayed allocation blocks. This number gets
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004516 * adjusted later. We return if we haven't
4517 * allocated blocks already inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02004519 error = xfs_trans_reserve_quota_nblks(
4520 NULL, ip, (long)alen, 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004521 rt ? XFS_QMOPT_RES_RTBLKS :
Christoph Hellwig7d095252009-06-08 15:33:32 +02004522 XFS_QMOPT_RES_REGBLKS);
4523 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 if (n == 0) {
4525 *nmap = 0;
4526 ASSERT(cur == NULL);
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004527 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 }
4529 break;
4530 }
4531
4532 /*
4533 * Split changing sb for alen and indlen since
4534 * they could be coming from different places.
4535 */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004536 indlen = (xfs_extlen_t)
4537 xfs_bmap_worst_indlen(ip, alen);
4538 ASSERT(indlen > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
Nathan Scottdd9f4382006-01-11 15:28:28 +11004540 if (rt) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004541 error = xfs_mod_incore_sb(mp,
4542 XFS_SBS_FREXTENTS,
Christoph Hellwig54893272011-05-11 15:04:03 +00004543 -((int64_t)extsz), 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004544 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00004545 error = xfs_icsb_modify_counters(mp,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004546 XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00004547 -((int64_t)alen), 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004548 }
David Chinner3bdbfb12005-09-02 16:40:47 +10004549 if (!error) {
Christoph Hellwig96540c72010-09-30 02:25:55 +00004550 error = xfs_icsb_modify_counters(mp,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004551 XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00004552 -((int64_t)indlen), 0);
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004553 if (error && rt)
4554 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004555 XFS_SBS_FREXTENTS,
Christoph Hellwig54893272011-05-11 15:04:03 +00004556 (int64_t)extsz, 0);
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004557 else if (error)
Christoph Hellwig96540c72010-09-30 02:25:55 +00004558 xfs_icsb_modify_counters(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004559 XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00004560 (int64_t)alen, 0);
David Chinner3bdbfb12005-09-02 16:40:47 +10004561 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004562
4563 if (error) {
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004564 if (XFS_IS_QUOTA_ON(mp))
Nathan Scott06d10dd2005-06-21 15:48:47 +10004565 /* unreserve the blocks now */
Nathan Scottdd9f4382006-01-11 15:28:28 +11004566 (void)
Christoph Hellwig7d095252009-06-08 15:33:32 +02004567 xfs_trans_unreserve_quota_nblks(
4568 NULL, ip,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004569 (long)alen, 0, rt ?
4570 XFS_QMOPT_RES_RTBLKS :
4571 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 break;
4573 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004574
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 ip->i_delayed_blks += alen;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004576 abno = nullstartblock(indlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 } else {
4578 /*
4579 * If first time, allocate and fill in
4580 * once-only bma fields.
4581 */
4582 if (bma.ip == NULL) {
4583 bma.tp = tp;
4584 bma.ip = ip;
4585 bma.prevp = &prev;
4586 bma.gotp = &got;
4587 bma.total = total;
4588 bma.userdata = 0;
4589 }
4590 /* Indicate if this is the first user data
4591 * in the file, or just any user data.
4592 */
Nathan Scott39269e22006-03-14 13:33:50 +11004593 if (!(flags & XFS_BMAPI_METADATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 bma.userdata = (aoff == 0) ?
4595 XFS_ALLOC_INITIAL_USER_DATA :
4596 XFS_ALLOC_USERDATA;
4597 }
4598 /*
4599 * Fill in changeable bma fields.
4600 */
4601 bma.eof = eof;
4602 bma.firstblock = *firstblock;
4603 bma.alen = alen;
4604 bma.off = aoff;
Adrian Bunk72880262006-08-30 13:41:58 +10004605 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 bma.wasdel = wasdelay;
4607 bma.minlen = minlen;
4608 bma.low = flist->xbf_low;
4609 bma.minleft = minleft;
4610 /*
4611 * Only want to do the alignment at the
4612 * eof if it is userdata and allocation length
4613 * is larger than a stripe unit.
4614 */
4615 if (mp->m_dalign && alen >= mp->m_dalign &&
Nathan Scott39269e22006-03-14 13:33:50 +11004616 (!(flags & XFS_BMAPI_METADATA)) &&
4617 (whichfork == XFS_DATA_FORK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 if ((error = xfs_bmap_isaeof(ip, aoff,
4619 whichfork, &bma.aeof)))
4620 goto error0;
4621 } else
4622 bma.aeof = 0;
4623 /*
4624 * Call allocator.
4625 */
4626 if ((error = xfs_bmap_alloc(&bma)))
4627 goto error0;
4628 /*
4629 * Copy out result fields.
4630 */
4631 abno = bma.rval;
4632 if ((flist->xbf_low = bma.low))
4633 minleft = 0;
4634 alen = bma.alen;
4635 aoff = bma.off;
4636 ASSERT(*firstblock == NULLFSBLOCK ||
4637 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4638 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4639 (flist->xbf_low &&
4640 XFS_FSB_TO_AGNO(mp, *firstblock) <
4641 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4642 *firstblock = bma.firstblock;
4643 if (cur)
4644 cur->bc_private.b.firstblock =
4645 *firstblock;
4646 if (abno == NULLFSBLOCK)
4647 break;
4648 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11004649 cur = xfs_bmbt_init_cursor(mp, tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 ip, whichfork);
4651 cur->bc_private.b.firstblock =
4652 *firstblock;
4653 cur->bc_private.b.flist = flist;
4654 }
4655 /*
4656 * Bump the number of extents we've allocated
4657 * in this call.
4658 */
4659 nallocs++;
4660 }
4661 if (cur)
4662 cur->bc_private.b.flags =
4663 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4664 got.br_startoff = aoff;
4665 got.br_startblock = abno;
4666 got.br_blockcount = alen;
4667 got.br_state = XFS_EXT_NORM; /* assume normal */
4668 /*
4669 * Determine state of extent, and the filesystem.
4670 * A wasdelay extent has been initialized, so
4671 * shouldn't be flagged as unwritten.
4672 */
Eric Sandeen62118702008-03-06 13:44:28 +11004673 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
4675 got.br_state = XFS_EXT_UNWRITTEN;
4676 }
4677 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004678 firstblock, flist, &tmp_logflags,
Christoph Hellwig54893272011-05-11 15:04:03 +00004679 whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 logflags |= tmp_logflags;
4681 if (error)
4682 goto error0;
4683 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004684 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4686 xfs_bmbt_get_all(ep, &got);
4687 ASSERT(got.br_startoff <= aoff);
4688 ASSERT(got.br_startoff + got.br_blockcount >=
4689 aoff + alen);
4690#ifdef DEBUG
Nathan Scott39269e22006-03-14 13:33:50 +11004691 if (flags & XFS_BMAPI_DELAY) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06004692 ASSERT(isnullstartblock(got.br_startblock));
4693 ASSERT(startblockval(got.br_startblock) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 }
4695 ASSERT(got.br_state == XFS_EXT_NORM ||
4696 got.br_state == XFS_EXT_UNWRITTEN);
4697#endif
4698 /*
4699 * Fall down into the found allocated space case.
4700 */
4701 } else if (inhole) {
4702 /*
4703 * Reading in a hole.
4704 */
4705 mval->br_startoff = bno;
4706 mval->br_startblock = HOLESTARTBLOCK;
4707 mval->br_blockcount =
4708 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4709 mval->br_state = XFS_EXT_NORM;
4710 bno += mval->br_blockcount;
4711 len -= mval->br_blockcount;
4712 mval++;
4713 n++;
4714 continue;
4715 }
4716 /*
4717 * Then deal with the allocated space we found.
4718 */
4719 ASSERT(ep != NULL);
Nathan Scott39269e22006-03-14 13:33:50 +11004720 if (!(flags & XFS_BMAPI_ENTIRE) &&
4721 (got.br_startoff + got.br_blockcount > obno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 if (obno > bno)
4723 bno = obno;
4724 ASSERT((bno >= obno) || (n == 0));
4725 ASSERT(bno < end);
4726 mval->br_startoff = bno;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004727 if (isnullstartblock(got.br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11004728 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 mval->br_startblock = DELAYSTARTBLOCK;
4730 } else
4731 mval->br_startblock =
4732 got.br_startblock +
4733 (bno - got.br_startoff);
4734 /*
4735 * Return the minimum of what we got and what we
4736 * asked for for the length. We can use the len
4737 * variable here because it is modified below
4738 * and we could have been there before coming
4739 * here if the first part of the allocation
4740 * didn't overlap what was asked for.
4741 */
4742 mval->br_blockcount =
4743 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
4744 (bno - got.br_startoff));
4745 mval->br_state = got.br_state;
4746 ASSERT(mval->br_blockcount <= len);
4747 } else {
4748 *mval = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004749 if (isnullstartblock(mval->br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11004750 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 mval->br_startblock = DELAYSTARTBLOCK;
4752 }
4753 }
4754
4755 /*
4756 * Check if writing previously allocated but
4757 * unwritten extents.
4758 */
Dave Chinner44722352010-08-24 12:02:11 +10004759 if (wr &&
4760 ((mval->br_state == XFS_EXT_UNWRITTEN &&
4761 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) ||
4762 (mval->br_state == XFS_EXT_NORM &&
4763 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT)) ==
4764 (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 /*
4766 * Modify (by adding) the state flag, if writing.
4767 */
4768 ASSERT(mval->br_blockcount <= len);
4769 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11004770 cur = xfs_bmbt_init_cursor(mp,
4771 tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 cur->bc_private.b.firstblock =
4773 *firstblock;
4774 cur->bc_private.b.flist = flist;
4775 }
Dave Chinner44722352010-08-24 12:02:11 +10004776 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4777 ? XFS_EXT_NORM
4778 : XFS_EXT_UNWRITTEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004780 firstblock, flist, &tmp_logflags,
Christoph Hellwig54893272011-05-11 15:04:03 +00004781 whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 logflags |= tmp_logflags;
4783 if (error)
4784 goto error0;
4785 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004786 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4788 xfs_bmbt_get_all(ep, &got);
4789 /*
4790 * We may have combined previously unwritten
4791 * space with written space, so generate
4792 * another request.
4793 */
4794 if (mval->br_blockcount < len)
4795 continue;
4796 }
4797
Nathan Scott39269e22006-03-14 13:33:50 +11004798 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 ((mval->br_startoff + mval->br_blockcount) <= end));
Nathan Scott39269e22006-03-14 13:33:50 +11004800 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4801 (mval->br_blockcount <= len) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 (mval->br_startoff < obno));
4803 bno = mval->br_startoff + mval->br_blockcount;
4804 len = end - bno;
4805 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4806 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4807 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4808 ASSERT(mval->br_state == mval[-1].br_state);
4809 mval[-1].br_blockcount = mval->br_blockcount;
4810 mval[-1].br_state = mval->br_state;
4811 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4812 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4813 mval[-1].br_startblock != HOLESTARTBLOCK &&
4814 mval->br_startblock ==
4815 mval[-1].br_startblock + mval[-1].br_blockcount &&
Nathan Scott39269e22006-03-14 13:33:50 +11004816 ((flags & XFS_BMAPI_IGSTATE) ||
4817 mval[-1].br_state == mval->br_state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 ASSERT(mval->br_startoff ==
4819 mval[-1].br_startoff + mval[-1].br_blockcount);
4820 mval[-1].br_blockcount += mval->br_blockcount;
4821 } else if (n > 0 &&
4822 mval->br_startblock == DELAYSTARTBLOCK &&
4823 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4824 mval->br_startoff ==
4825 mval[-1].br_startoff + mval[-1].br_blockcount) {
4826 mval[-1].br_blockcount += mval->br_blockcount;
4827 mval[-1].br_state = mval->br_state;
4828 } else if (!((n == 0) &&
4829 ((mval->br_startoff + mval->br_blockcount) <=
4830 obno))) {
4831 mval++;
4832 n++;
4833 }
4834 /*
4835 * If we're done, stop now. Stop when we've allocated
4836 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4837 * the transaction may get too big.
4838 */
4839 if (bno >= end || n >= *nmap || nallocs >= *nmap)
4840 break;
4841 /*
4842 * Else go on to the next record.
4843 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004844 ep = xfs_iext_get_ext(ifp, ++lastx);
David Chinner4e5ae832007-06-05 16:24:15 +10004845 prev = got;
4846 if (lastx >= nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 eof = 1;
David Chinner4e5ae832007-06-05 16:24:15 +10004848 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 xfs_bmbt_get_all(ep, &got);
4850 }
4851 ifp->if_lastex = lastx;
4852 *nmap = n;
4853 /*
4854 * Transform from btree to extents, give it cur.
4855 */
4856 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4857 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4858 ASSERT(wr && cur);
4859 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4860 &tmp_logflags, whichfork);
4861 logflags |= tmp_logflags;
4862 if (error)
4863 goto error0;
4864 }
4865 ASSERT(ifp->if_ext_max ==
4866 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4867 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4868 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
4869 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870error0:
4871 /*
4872 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004873 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06004875 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06004877 logflags &= ~xfs_ilog_fext(whichfork);
4878 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06004880 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 /*
4882 * Log whatever the flags say, even if error. Otherwise we might miss
4883 * detecting a case where the data is changed, there's an error,
4884 * and it's not logged so we don't shutdown when we should.
4885 */
4886 if (logflags) {
4887 ASSERT(tp && wr);
4888 xfs_trans_log_inode(tp, ip, logflags);
4889 }
4890 if (cur) {
4891 if (!error) {
4892 ASSERT(*firstblock == NULLFSBLOCK ||
4893 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4894 XFS_FSB_TO_AGNO(mp,
4895 cur->bc_private.b.firstblock) ||
4896 (flist->xbf_low &&
4897 XFS_FSB_TO_AGNO(mp, *firstblock) <
4898 XFS_FSB_TO_AGNO(mp,
4899 cur->bc_private.b.firstblock)));
4900 *firstblock = cur->bc_private.b.firstblock;
4901 }
4902 xfs_btree_del_cursor(cur,
4903 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4904 }
4905 if (!error)
4906 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4907 orig_nmap, *nmap);
4908 return error;
4909}
4910
4911/*
4912 * Map file blocks to filesystem blocks, simple version.
4913 * One block (extent) only, read-only.
4914 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
4915 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
4916 * was set and all the others were clear.
4917 */
4918int /* error */
4919xfs_bmapi_single(
4920 xfs_trans_t *tp, /* transaction pointer */
4921 xfs_inode_t *ip, /* incore inode */
4922 int whichfork, /* data or attr fork */
4923 xfs_fsblock_t *fsb, /* output: mapped block */
4924 xfs_fileoff_t bno) /* starting file offs. mapped */
4925{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004926 int eof; /* we've hit the end of extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004928 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 xfs_ifork_t *ifp; /* inode fork pointer */
4930 xfs_extnum_t lastx; /* last useful extent number */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004931 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
4933 ifp = XFS_IFORK_PTR(ip, whichfork);
4934 if (unlikely(
4935 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4936 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
4937 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
4938 ip->i_mount);
4939 return XFS_ERROR(EFSCORRUPTED);
4940 }
4941 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
4942 return XFS_ERROR(EIO);
4943 XFS_STATS_INC(xs_blk_mapr);
4944 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4945 (error = xfs_iread_extents(tp, ip, whichfork)))
4946 return error;
4947 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4948 &prev);
4949 /*
4950 * Reading past eof, act as though there's a hole
4951 * up to end.
4952 */
4953 if (eof || got.br_startoff > bno) {
4954 *fsb = NULLFSBLOCK;
4955 return 0;
4956 }
Eric Sandeen9d87c312009-01-14 23:22:07 -06004957 ASSERT(!isnullstartblock(got.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 ASSERT(bno < got.br_startoff + got.br_blockcount);
4959 *fsb = got.br_startblock + (bno - got.br_startoff);
4960 ifp->if_lastex = lastx;
4961 return 0;
4962}
4963
4964/*
4965 * Unmap (remove) blocks from a file.
4966 * If nexts is nonzero then the number of extents to remove is limited to
4967 * that value. If not all extents in the block range can be removed then
4968 * *done is set.
4969 */
4970int /* error */
4971xfs_bunmapi(
4972 xfs_trans_t *tp, /* transaction pointer */
4973 struct xfs_inode *ip, /* incore inode */
4974 xfs_fileoff_t bno, /* starting offset to unmap */
4975 xfs_filblks_t len, /* length to unmap in file */
4976 int flags, /* misc flags */
4977 xfs_extnum_t nexts, /* number of extents max */
4978 xfs_fsblock_t *firstblock, /* first allocated block
4979 controls a.g. for allocs */
4980 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4981 int *done) /* set if not done yet */
4982{
4983 xfs_btree_cur_t *cur; /* bmap btree cursor */
4984 xfs_bmbt_irec_t del; /* extent being deleted */
4985 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004986 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 int error; /* error return value */
4988 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004989 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 xfs_ifork_t *ifp; /* inode fork pointer */
4991 int isrt; /* freeing in rt area */
4992 xfs_extnum_t lastx; /* last extent index used */
4993 int logflags; /* transaction logging flags */
4994 xfs_extlen_t mod; /* rt extent offset */
4995 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004996 xfs_extnum_t nextents; /* number of file extents */
4997 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 xfs_fileoff_t start; /* first file offset deleted */
4999 int tmp_logflags; /* partial logging flags */
5000 int wasdel; /* was a delayed alloc extent */
5001 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 xfs_fsblock_t sum;
5003
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005004 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5005
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5007 XFS_ATTR_FORK : XFS_DATA_FORK;
5008 ifp = XFS_IFORK_PTR(ip, whichfork);
5009 if (unlikely(
5010 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5011 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5012 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5013 ip->i_mount);
5014 return XFS_ERROR(EFSCORRUPTED);
5015 }
5016 mp = ip->i_mount;
5017 if (XFS_FORCED_SHUTDOWN(mp))
5018 return XFS_ERROR(EIO);
Christoph Hellwig54893272011-05-11 15:04:03 +00005019
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 ASSERT(len > 0);
5021 ASSERT(nexts >= 0);
5022 ASSERT(ifp->if_ext_max ==
5023 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5024 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5025 (error = xfs_iread_extents(tp, ip, whichfork)))
5026 return error;
5027 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5028 if (nextents == 0) {
5029 *done = 1;
5030 return 0;
5031 }
5032 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005033 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 start = bno;
5035 bno = start + len - 1;
5036 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5037 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005038
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 /*
5040 * Check to see if the given block number is past the end of the
5041 * file, back up to the last block if so...
5042 */
5043 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005044 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 xfs_bmbt_get_all(ep, &got);
5046 bno = got.br_startoff + got.br_blockcount - 1;
5047 }
5048 logflags = 0;
5049 if (ifp->if_flags & XFS_IFBROOT) {
5050 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005051 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 cur->bc_private.b.firstblock = *firstblock;
5053 cur->bc_private.b.flist = flist;
5054 cur->bc_private.b.flags = 0;
5055 } else
5056 cur = NULL;
5057 extno = 0;
5058 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5059 (nexts == 0 || extno < nexts)) {
5060 /*
5061 * Is the found extent after a hole in which bno lives?
5062 * Just back up to the previous extent, if so.
5063 */
5064 if (got.br_startoff > bno) {
5065 if (--lastx < 0)
5066 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005067 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 xfs_bmbt_get_all(ep, &got);
5069 }
5070 /*
5071 * Is the last block of this extent before the range
5072 * we're supposed to delete? If so, we're done.
5073 */
5074 bno = XFS_FILEOFF_MIN(bno,
5075 got.br_startoff + got.br_blockcount - 1);
5076 if (bno < start)
5077 break;
5078 /*
5079 * Then deal with the (possibly delayed) allocated space
5080 * we found.
5081 */
5082 ASSERT(ep != NULL);
5083 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005084 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 if (got.br_startoff < start) {
5086 del.br_startoff = start;
5087 del.br_blockcount -= start - got.br_startoff;
5088 if (!wasdel)
5089 del.br_startblock += start - got.br_startoff;
5090 }
5091 if (del.br_startoff + del.br_blockcount > bno + 1)
5092 del.br_blockcount = bno + 1 - del.br_startoff;
5093 sum = del.br_startblock + del.br_blockcount;
5094 if (isrt &&
5095 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5096 /*
5097 * Realtime extent not lined up at the end.
5098 * The extent could have been split into written
5099 * and unwritten pieces, or we could just be
5100 * unmapping part of it. But we can't really
5101 * get rid of part of a realtime extent.
5102 */
5103 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005104 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 /*
5106 * This piece is unwritten, or we're not
5107 * using unwritten extents. Skip over it.
5108 */
5109 ASSERT(bno >= mod);
5110 bno -= mod > del.br_blockcount ?
5111 del.br_blockcount : mod;
5112 if (bno < got.br_startoff) {
5113 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005114 xfs_bmbt_get_all(xfs_iext_get_ext(
5115 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 }
5117 continue;
5118 }
5119 /*
5120 * It's written, turn it unwritten.
5121 * This is better than zeroing it.
5122 */
5123 ASSERT(del.br_state == XFS_EXT_NORM);
5124 ASSERT(xfs_trans_get_block_res(tp) > 0);
5125 /*
5126 * If this spans a realtime extent boundary,
5127 * chop it back to the start of the one we end at.
5128 */
5129 if (del.br_blockcount > mod) {
5130 del.br_startoff += del.br_blockcount - mod;
5131 del.br_startblock += del.br_blockcount - mod;
5132 del.br_blockcount = mod;
5133 }
5134 del.br_state = XFS_EXT_UNWRITTEN;
5135 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005136 firstblock, flist, &logflags,
Christoph Hellwig54893272011-05-11 15:04:03 +00005137 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 if (error)
5139 goto error0;
5140 goto nodelete;
5141 }
5142 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5143 /*
5144 * Realtime extent is lined up at the end but not
5145 * at the front. We'll get rid of full extents if
5146 * we can.
5147 */
5148 mod = mp->m_sb.sb_rextsize - mod;
5149 if (del.br_blockcount > mod) {
5150 del.br_blockcount -= mod;
5151 del.br_startoff += mod;
5152 del.br_startblock += mod;
5153 } else if ((del.br_startoff == start &&
5154 (del.br_state == XFS_EXT_UNWRITTEN ||
5155 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005156 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 /*
5158 * Can't make it unwritten. There isn't
5159 * a full extent here so just skip it.
5160 */
5161 ASSERT(bno >= del.br_blockcount);
5162 bno -= del.br_blockcount;
5163 if (bno < got.br_startoff) {
5164 if (--lastx >= 0)
5165 xfs_bmbt_get_all(--ep, &got);
5166 }
5167 continue;
5168 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5169 /*
5170 * This one is already unwritten.
5171 * It must have a written left neighbor.
5172 * Unwrite the killed part of that one and
5173 * try again.
5174 */
5175 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005176 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5177 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005179 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 ASSERT(del.br_startblock ==
5181 prev.br_startblock + prev.br_blockcount);
5182 if (prev.br_startoff < start) {
5183 mod = start - prev.br_startoff;
5184 prev.br_blockcount -= mod;
5185 prev.br_startblock += mod;
5186 prev.br_startoff = start;
5187 }
5188 prev.br_state = XFS_EXT_UNWRITTEN;
5189 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5190 &prev, firstblock, flist, &logflags,
Christoph Hellwig54893272011-05-11 15:04:03 +00005191 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 if (error)
5193 goto error0;
5194 goto nodelete;
5195 } else {
5196 ASSERT(del.br_state == XFS_EXT_NORM);
5197 del.br_state = XFS_EXT_UNWRITTEN;
5198 error = xfs_bmap_add_extent(ip, lastx, &cur,
5199 &del, firstblock, flist, &logflags,
Christoph Hellwig54893272011-05-11 15:04:03 +00005200 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 if (error)
5202 goto error0;
5203 goto nodelete;
5204 }
5205 }
5206 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005207 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005208 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005209 if (isrt) {
5210 xfs_filblks_t rtexts;
5211
5212 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5213 do_div(rtexts, mp->m_sb.sb_rextsize);
5214 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005215 (int64_t)rtexts, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005216 (void)xfs_trans_reserve_quota_nblks(NULL,
5217 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005218 XFS_QMOPT_RES_RTBLKS);
5219 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00005220 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005221 (int64_t)del.br_blockcount, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005222 (void)xfs_trans_reserve_quota_nblks(NULL,
5223 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 ip->i_delayed_blks -= del.br_blockcount;
5227 if (cur)
5228 cur->bc_private.b.flags |=
5229 XFS_BTCUR_BPRV_WASDEL;
5230 } else if (cur)
5231 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5232 /*
5233 * If it's the case where the directory code is running
5234 * with no block reservation, and the deleted block is in
5235 * the middle of its extent, and the resulting insert
5236 * of an extent would cause transformation to btree format,
5237 * then reject it. The calling code will then swap
5238 * blocks around instead.
5239 * We have to do this now, rather than waiting for the
5240 * conversion to btree format, since the transaction
5241 * will be dirty.
5242 */
5243 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5244 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5245 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5246 del.br_startoff > got.br_startoff &&
5247 del.br_startoff + del.br_blockcount <
5248 got.br_startoff + got.br_blockcount) {
5249 error = XFS_ERROR(ENOSPC);
5250 goto error0;
5251 }
5252 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005253 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 logflags |= tmp_logflags;
5255 if (error)
5256 goto error0;
5257 bno = del.br_startoff - 1;
5258nodelete:
5259 lastx = ifp->if_lastex;
5260 /*
5261 * If not done go on to the next (previous) record.
5262 * Reset ep in case the extents array was re-alloced.
5263 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005264 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5266 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5267 xfs_bmbt_get_startoff(ep) > bno) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005268 if (--lastx >= 0)
5269 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 }
5271 if (lastx >= 0)
5272 xfs_bmbt_get_all(ep, &got);
5273 extno++;
5274 }
5275 }
5276 ifp->if_lastex = lastx;
5277 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5278 ASSERT(ifp->if_ext_max ==
5279 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5280 /*
5281 * Convert to a btree if necessary.
5282 */
5283 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5284 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5285 ASSERT(cur == NULL);
5286 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5287 &cur, 0, &tmp_logflags, whichfork);
5288 logflags |= tmp_logflags;
5289 if (error)
5290 goto error0;
5291 }
5292 /*
5293 * transform from btree to extents, give it cur
5294 */
5295 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5296 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5297 ASSERT(cur != NULL);
5298 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5299 whichfork);
5300 logflags |= tmp_logflags;
5301 if (error)
5302 goto error0;
5303 }
5304 /*
5305 * transform from extents to local?
5306 */
5307 ASSERT(ifp->if_ext_max ==
5308 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5309 error = 0;
5310error0:
5311 /*
5312 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005313 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005315 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005317 logflags &= ~xfs_ilog_fext(whichfork);
5318 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005320 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 /*
5322 * Log inode even in the error case, if the transaction
5323 * is dirty we'll need to shut down the filesystem.
5324 */
5325 if (logflags)
5326 xfs_trans_log_inode(tp, ip, logflags);
5327 if (cur) {
5328 if (!error) {
5329 *firstblock = cur->bc_private.b.firstblock;
5330 cur->bc_private.b.allocated = 0;
5331 }
5332 xfs_btree_del_cursor(cur,
5333 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5334 }
5335 return error;
5336}
5337
5338/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005339 * returns 1 for success, 0 if we failed to map the extent.
5340 */
5341STATIC int
5342xfs_getbmapx_fix_eof_hole(
5343 xfs_inode_t *ip, /* xfs incore inode pointer */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005344 struct getbmapx *out, /* output structure */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005345 int prealloced, /* this is a file with
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005346 * preallocated data space */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005347 __int64_t end, /* last block requested */
5348 xfs_fsblock_t startblock)
5349{
5350 __int64_t fixlen;
5351 xfs_mount_t *mp; /* file system mount point */
Eric Sandeen5af317c2008-11-28 14:23:35 +11005352 xfs_ifork_t *ifp; /* inode fork pointer */
5353 xfs_extnum_t lastx; /* last extent pointer */
5354 xfs_fileoff_t fileblock;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005355
5356 if (startblock == HOLESTARTBLOCK) {
5357 mp = ip->i_mount;
5358 out->bmv_block = -1;
5359 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5360 fixlen -= out->bmv_offset;
5361 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5362 /* Came to hole at EOF. Trim it. */
5363 if (fixlen <= 0)
5364 return 0;
5365 out->bmv_length = fixlen;
5366 }
5367 } else {
Eric Sandeen5af317c2008-11-28 14:23:35 +11005368 if (startblock == DELAYSTARTBLOCK)
5369 out->bmv_block = -2;
5370 else
Eric Sandeen9d87c312009-01-14 23:22:07 -06005371 out->bmv_block = xfs_fsb_to_db(ip, startblock);
Eric Sandeen5af317c2008-11-28 14:23:35 +11005372 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5373 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5374 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5375 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5376 out->bmv_oflags |= BMV_OF_LAST;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005377 }
5378
5379 return 1;
5380}
5381
5382/*
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005383 * Get inode's extents as described in bmv, and format for output.
5384 * Calls formatter to fill the user's buffer until all extents
5385 * are mapped, until the passed-in bmv->bmv_count slots have
5386 * been filled, or until the formatter short-circuits the loop,
5387 * if it is tracking filled-in extents on its own.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 */
5389int /* error code */
5390xfs_getbmap(
Christoph Hellwig993386c12007-08-28 16:12:30 +10005391 xfs_inode_t *ip,
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005392 struct getbmapx *bmv, /* user bmap structure */
5393 xfs_bmap_format_t formatter, /* format to user */
5394 void *arg) /* formatter arg */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395{
5396 __int64_t bmvend; /* last block requested */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005397 int error = 0; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 __int64_t fixlen; /* length for -1 case */
5399 int i; /* extent number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 int lock; /* lock state */
5401 xfs_bmbt_irec_t *map; /* buffer for user's data */
5402 xfs_mount_t *mp; /* file system mount point */
5403 int nex; /* # of user extents can do */
5404 int nexleft; /* # of user extents left */
5405 int subnex; /* # of bmapi's can do */
5406 int nmap; /* number of map entries */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005407 struct getbmapx *out; /* output structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 int whichfork; /* data or attr fork */
5409 int prealloced; /* this is a file with
5410 * preallocated data space */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005411 int iflags; /* interface flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 int bmapi_flags; /* flags for xfs_bmapi */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005413 int cur_ext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 mp = ip->i_mount;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005416 iflags = bmv->bmv_iflags;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005417 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 if (whichfork == XFS_ATTR_FORK) {
5420 if (XFS_IFORK_Q(ip)) {
5421 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5422 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5423 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5424 return XFS_ERROR(EINVAL);
5425 } else if (unlikely(
5426 ip->i_d.di_aformat != 0 &&
5427 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5428 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5429 ip->i_mount);
5430 return XFS_ERROR(EFSCORRUPTED);
5431 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005432
5433 prealloced = 0;
5434 fixlen = 1LL << 32;
5435 } else {
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005436 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5437 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5438 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5439 return XFS_ERROR(EINVAL);
5440
David Chinner957d0eb2007-06-18 16:50:37 +10005441 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005442 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 prealloced = 1;
5444 fixlen = XFS_MAXIOFFSET(mp);
5445 } else {
5446 prealloced = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005447 fixlen = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 }
5450
5451 if (bmv->bmv_length == -1) {
5452 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005453 bmv->bmv_length =
5454 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5455 } else if (bmv->bmv_length == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 bmv->bmv_entries = 0;
5457 return 0;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005458 } else if (bmv->bmv_length < 0) {
5459 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005461
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 nex = bmv->bmv_count - 1;
5463 if (nex <= 0)
5464 return XFS_ERROR(EINVAL);
5465 bmvend = bmv->bmv_offset + bmv->bmv_length;
5466
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005467
5468 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5469 return XFS_ERROR(ENOMEM);
5470 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5471 if (!out)
5472 return XFS_ERROR(ENOMEM);
5473
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005475 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5476 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5477 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5478 if (error)
5479 goto out_unlock_iolock;
Niv Sardie12070a2008-03-06 13:43:03 +11005480 }
Dave Chinner309c8482010-11-30 15:16:02 +11005481 /*
5482 * even after flushing the inode, there can still be delalloc
5483 * blocks on the inode beyond EOF due to speculative
5484 * preallocation. These are not removed until the release
5485 * function is called or the inode is inactivated. Hence we
5486 * cannot assert here that ip->i_delayed_blks == 0.
5487 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
5490 lock = xfs_ilock_map_shared(ip);
5491
5492 /*
5493 * Don't let nex be bigger than the number of extents
5494 * we can have assuming alternating holes and real extents.
5495 */
5496 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5497 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5498
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005499 bmapi_flags = xfs_bmapi_aflag(whichfork);
5500 if (!(iflags & BMV_IF_PREALLOC))
5501 bmapi_flags |= XFS_BMAPI_IGSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502
5503 /*
5504 * Allocate enough space to handle "subnex" maps at a time.
5505 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005506 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 subnex = 16;
Christoph Hellwigca35dcd2009-07-18 18:14:54 -04005508 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005509 if (!map)
5510 goto out_unlock_ilock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
5512 bmv->bmv_entries = 0;
5513
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005514 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5515 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5516 error = 0;
5517 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 }
5519
5520 nexleft = nex;
5521
5522 do {
5523 nmap = (nexleft > subnex) ? subnex : nexleft;
5524 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5525 XFS_BB_TO_FSB(mp, bmv->bmv_length),
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005526 bmapi_flags, NULL, 0, map, &nmap,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005527 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 if (error)
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005529 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 ASSERT(nmap <= subnex);
5531
5532 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005533 out[cur_ext].bmv_oflags = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005534 if (map[i].br_state == XFS_EXT_UNWRITTEN)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005535 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005536 else if (map[i].br_startblock == DELAYSTARTBLOCK)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005537 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5538 out[cur_ext].bmv_offset =
5539 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5540 out[cur_ext].bmv_length =
5541 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5542 out[cur_ext].bmv_unused1 = 0;
5543 out[cur_ext].bmv_unused2 = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005544 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5545 (map[i].br_startblock != DELAYSTARTBLOCK));
Yingping Lu9af0a702005-11-02 15:09:54 +11005546 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005547 whichfork == XFS_ATTR_FORK) {
5548 /* came to the end of attribute fork */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005549 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005550 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005552
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005553 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5554 prealloced, bmvend,
5555 map[i].br_startblock))
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005556 goto out_free_map;
5557
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005558 bmv->bmv_offset =
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005559 out[cur_ext].bmv_offset +
5560 out[cur_ext].bmv_length;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005561 bmv->bmv_length =
5562 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
Tao Ma9af25462010-08-30 02:44:03 +00005563
5564 /*
5565 * In case we don't want to return the hole,
5566 * don't increase cur_ext so that we can reuse
5567 * it in the next loop.
5568 */
5569 if ((iflags & BMV_IF_NO_HOLES) &&
5570 map[i].br_startblock == HOLESTARTBLOCK) {
5571 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5572 continue;
5573 }
5574
5575 nexleft--;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005576 bmv->bmv_entries++;
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005577 cur_ext++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 }
5579 } while (nmap && nexleft && bmv->bmv_length);
5580
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005581 out_free_map:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10005582 kmem_free(map);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005583 out_unlock_ilock:
5584 xfs_iunlock_map_shared(ip, lock);
5585 out_unlock_iolock:
5586 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005587
5588 for (i = 0; i < cur_ext; i++) {
5589 int full = 0; /* user array is full */
5590
5591 /* format results & advance arg */
5592 error = formatter(&arg, &out[i], &full);
5593 if (error || full)
5594 break;
5595 }
5596
Felix Blyakher7747a0b2009-06-11 17:07:28 -05005597 kmem_free(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 return error;
5599}
5600
5601/*
5602 * Check the last inode extent to determine whether this allocation will result
5603 * in blocks being allocated at the end of the file. When we allocate new data
5604 * blocks at the end of the file which do not start at the previous data block,
5605 * we will try to align the new blocks at stripe unit boundaries.
5606 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10005607STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608xfs_bmap_isaeof(
5609 xfs_inode_t *ip, /* incore inode pointer */
5610 xfs_fileoff_t off, /* file offset in fsblocks */
5611 int whichfork, /* data or attribute fork */
5612 char *aeof) /* return value */
5613{
5614 int error; /* error return value */
5615 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005616 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005617 xfs_extnum_t nextents; /* number of file extents */
5618 xfs_bmbt_irec_t s; /* expanded extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
5620 ASSERT(whichfork == XFS_DATA_FORK);
5621 ifp = XFS_IFORK_PTR(ip, whichfork);
5622 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5623 (error = xfs_iread_extents(NULL, ip, whichfork)))
5624 return error;
5625 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5626 if (nextents == 0) {
5627 *aeof = 1;
5628 return 0;
5629 }
5630 /*
5631 * Go to the last extent
5632 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005633 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 xfs_bmbt_get_all(lastrec, &s);
5635 /*
5636 * Check we are allocating in the last extent (for delayed allocations)
5637 * or past the last extent for non-delayed allocations.
5638 */
5639 *aeof = (off >= s.br_startoff &&
5640 off < s.br_startoff + s.br_blockcount &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06005641 isnullstartblock(s.br_startblock)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 off >= s.br_startoff + s.br_blockcount;
5643 return 0;
5644}
5645
5646/*
5647 * Check if the endoff is outside the last extent. If so the caller will grow
5648 * the allocation to a stripe unit boundary.
5649 */
5650int /* error */
5651xfs_bmap_eof(
5652 xfs_inode_t *ip, /* incore inode pointer */
5653 xfs_fileoff_t endoff, /* file offset in fsblocks */
5654 int whichfork, /* data or attribute fork */
5655 int *eof) /* result value */
5656{
5657 xfs_fsblock_t blockcount; /* extent block count */
5658 int error; /* error return value */
5659 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005660 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005661 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 xfs_fileoff_t startoff; /* extent starting file offset */
5663
5664 ASSERT(whichfork == XFS_DATA_FORK);
5665 ifp = XFS_IFORK_PTR(ip, whichfork);
5666 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5667 (error = xfs_iread_extents(NULL, ip, whichfork)))
5668 return error;
5669 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5670 if (nextents == 0) {
5671 *eof = 1;
5672 return 0;
5673 }
5674 /*
5675 * Go to the last extent
5676 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005677 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 startoff = xfs_bmbt_get_startoff(lastrec);
5679 blockcount = xfs_bmbt_get_blockcount(lastrec);
5680 *eof = endoff >= startoff + blockcount;
5681 return 0;
5682}
5683
5684#ifdef DEBUG
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005685STATIC struct xfs_buf *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686xfs_bmap_get_bp(
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005687 struct xfs_btree_cur *cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 xfs_fsblock_t bno)
5689{
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005690 struct xfs_log_item_desc *lidp;
5691 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
5693 if (!cur)
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005694 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005696 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5697 if (!cur->bc_bufs[i])
5698 break;
5699 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5700 return cur->bc_bufs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005703 /* Chase down all the log items to see if the bp is there */
5704 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
Christoph Hellwige98c4142010-06-23 18:11:15 +10005705 struct xfs_buf_log_item *bip;
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005706 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5707 if (bip->bli_item.li_type == XFS_LI_BUF &&
5708 XFS_BUF_ADDR(bip->bli_buf) == bno)
5709 return bip->bli_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 }
Christoph Hellwige98c4142010-06-23 18:11:15 +10005711
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005712 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713}
5714
Hannes Eder3180e662009-03-04 19:34:10 +01005715STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716xfs_check_block(
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005717 struct xfs_btree_block *block,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 xfs_mount_t *mp,
5719 int root,
5720 short sz)
5721{
5722 int i, j, dmxr;
Christoph Hellwig576039c2006-09-28 10:58:06 +10005723 __be64 *pp, *thispa; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 xfs_bmbt_key_t *prevp, *keyp;
5725
Christoph Hellwig16259e72005-11-02 15:11:25 +11005726 ASSERT(be16_to_cpu(block->bb_level) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727
5728 prevp = NULL;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005729 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 dmxr = mp->m_bmap_dmxr[0];
Christoph Hellwig136341b2008-10-30 17:11:40 +11005731 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732
5733 if (prevp) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005734 ASSERT(be64_to_cpu(prevp->br_startoff) <
5735 be64_to_cpu(keyp->br_startoff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 }
5737 prevp = keyp;
5738
5739 /*
5740 * Compare the block numbers to see if there are dups.
5741 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11005742 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005743 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005744 else
5745 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5746
Christoph Hellwig16259e72005-11-02 15:11:25 +11005747 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11005748 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005749 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005750 else
5751 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005752 if (*thispa == *pp) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11005753 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005754 __func__, j, i,
Christoph Hellwig576039c2006-09-28 10:58:06 +10005755 (unsigned long long)be64_to_cpu(*thispa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 panic("%s: ptrs are equal in node\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005757 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 }
5759 }
5760 }
5761}
5762
5763/*
5764 * Check that the extents for the inode ip are in the right order in all
5765 * btree leaves.
5766 */
5767
5768STATIC void
5769xfs_bmap_check_leaf_extents(
5770 xfs_btree_cur_t *cur, /* btree cursor or null */
5771 xfs_inode_t *ip, /* incore inode pointer */
5772 int whichfork) /* data or attr fork */
5773{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005774 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 xfs_fsblock_t bno; /* block # of "block" */
5776 xfs_buf_t *bp; /* buffer for "block" */
5777 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005778 xfs_extnum_t i=0, j; /* index into the extents list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 xfs_ifork_t *ifp; /* fork structure */
5780 int level; /* btree level, for checking */
5781 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10005782 __be64 *pp; /* pointer to block address */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005783 xfs_bmbt_rec_t *ep; /* pointer to current extent */
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005784 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005785 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 int bp_release = 0;
5787
5788 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5789 return;
5790 }
5791
5792 bno = NULLFSBLOCK;
5793 mp = ip->i_mount;
5794 ifp = XFS_IFORK_PTR(ip, whichfork);
5795 block = ifp->if_broot;
5796 /*
5797 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5798 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11005799 level = be16_to_cpu(block->bb_level);
5800 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11005802 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005803 bno = be64_to_cpu(*pp);
5804
5805 ASSERT(bno != NULLDFSBNO);
5806 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5807 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5808
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 /*
5810 * Go down the tree until leaf level is reached, following the first
5811 * pointer (leftmost) at each level.
5812 */
5813 while (level-- > 0) {
5814 /* See if buf is in cur first */
5815 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5816 if (bp) {
5817 bp_release = 0;
5818 } else {
5819 bp_release = 1;
5820 }
5821 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5822 XFS_BMAP_BTREE_REF)))
5823 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005824 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11005826 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 error0);
5828 if (level == 0)
5829 break;
5830
5831 /*
5832 * Check this block for basic sanity (increasing keys and
5833 * no duplicate blocks).
5834 */
5835
5836 xfs_check_block(block, mp, 0, 0);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005837 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005838 bno = be64_to_cpu(*pp);
5839 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 if (bp_release) {
5841 bp_release = 0;
5842 xfs_trans_brelse(NULL, bp);
5843 }
5844 }
5845
5846 /*
5847 * Here with bp and block set to the leftmost leaf node in the tree.
5848 */
5849 i = 0;
5850
5851 /*
5852 * Loop over all leaf nodes checking that all extents are in the right order.
5853 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 xfs_fsblock_t nextbno;
5856 xfs_extnum_t num_recs;
5857
5858
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005859 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860
5861 /*
5862 * Read-ahead the next leaf block, if any.
5863 */
5864
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005865 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866
5867 /*
5868 * Check all the extents to make sure they are OK.
5869 * If we had a previous block, the last entry should
5870 * conform with the first entry in this one.
5871 */
5872
Christoph Hellwig136341b2008-10-30 17:11:40 +11005873 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005874 if (i) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005875 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
5876 xfs_bmbt_disk_get_blockcount(&last) <=
5877 xfs_bmbt_disk_get_startoff(ep));
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005878 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005879 for (j = 1; j < num_recs; j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11005880 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005881 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
5882 xfs_bmbt_disk_get_blockcount(ep) <=
5883 xfs_bmbt_disk_get_startoff(nextp));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005884 ep = nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005887 last = *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 i += num_recs;
5889 if (bp_release) {
5890 bp_release = 0;
5891 xfs_trans_brelse(NULL, bp);
5892 }
5893 bno = nextbno;
5894 /*
5895 * If we've reached the end, stop.
5896 */
5897 if (bno == NULLFSBLOCK)
5898 break;
5899
5900 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5901 if (bp) {
5902 bp_release = 0;
5903 } else {
5904 bp_release = 1;
5905 }
5906 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5907 XFS_BMAP_BTREE_REF)))
5908 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005909 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 }
5911 if (bp_release) {
5912 bp_release = 0;
5913 xfs_trans_brelse(NULL, bp);
5914 }
5915 return;
5916
5917error0:
Dave Chinner0b932cc2011-03-07 10:08:35 +11005918 xfs_warn(mp, "%s: at error0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 if (bp_release)
5920 xfs_trans_brelse(NULL, bp);
5921error_norelse:
Dave Chinner0b932cc2011-03-07 10:08:35 +11005922 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005923 __func__, i);
5924 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 return;
5926}
5927#endif
5928
5929/*
5930 * Count fsblocks of the given fork.
5931 */
5932int /* error */
5933xfs_bmap_count_blocks(
5934 xfs_trans_t *tp, /* transaction pointer */
5935 xfs_inode_t *ip, /* incore inode */
5936 int whichfork, /* data or attr fork */
5937 int *count) /* out: count of blocks */
5938{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005939 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 xfs_fsblock_t bno; /* block # of "block" */
5941 xfs_ifork_t *ifp; /* fork structure */
5942 int level; /* btree level, for checking */
5943 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10005944 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945
5946 bno = NULLFSBLOCK;
5947 mp = ip->i_mount;
5948 ifp = XFS_IFORK_PTR(ip, whichfork);
5949 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
Ruben Porrasc94312d2008-08-13 16:52:25 +10005950 xfs_bmap_count_leaves(ifp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
Ruben Porrasc94312d2008-08-13 16:52:25 +10005952 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 return 0;
5954 }
5955
5956 /*
5957 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5958 */
5959 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11005960 level = be16_to_cpu(block->bb_level);
5961 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11005962 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005963 bno = be64_to_cpu(*pp);
5964 ASSERT(bno != NULLDFSBNO);
5965 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5966 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005968 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
5970 mp);
5971 return XFS_ERROR(EFSCORRUPTED);
5972 }
5973
5974 return 0;
5975}
5976
5977/*
5978 * Recursively walks each level of a btree
5979 * to count total fsblocks is use.
5980 */
David Chinnera8272ce2007-11-23 16:28:09 +11005981STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982xfs_bmap_count_tree(
5983 xfs_mount_t *mp, /* file system mount point */
5984 xfs_trans_t *tp, /* transaction pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005985 xfs_ifork_t *ifp, /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 xfs_fsblock_t blockno, /* file system block number */
5987 int levelin, /* level in btree */
5988 int *count) /* Count of blocks */
5989{
5990 int error;
5991 xfs_buf_t *bp, *nbp;
5992 int level = levelin;
Christoph Hellwig576039c2006-09-28 10:58:06 +10005993 __be64 *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 xfs_fsblock_t bno = blockno;
5995 xfs_fsblock_t nextbno;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005996 struct xfs_btree_block *block, *nextblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998
5999 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6000 return error;
6001 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006002 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003
6004 if (--level) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +02006005 /* Not at node above leaves, count this level of nodes */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006006 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 while (nextbno != NULLFSBLOCK) {
6008 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6009 0, &nbp, XFS_BMAP_BTREE_REF)))
6010 return error;
6011 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006012 nextblock = XFS_BUF_TO_BLOCK(nbp);
6013 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 xfs_trans_brelse(tp, nbp);
6015 }
6016
6017 /* Dive to the next level */
Christoph Hellwig136341b2008-10-30 17:11:40 +11006018 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006019 bno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 if (unlikely((error =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006021 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 xfs_trans_brelse(tp, bp);
6023 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6024 XFS_ERRLEVEL_LOW, mp);
6025 return XFS_ERROR(EFSCORRUPTED);
6026 }
6027 xfs_trans_brelse(tp, bp);
6028 } else {
6029 /* count all level 1 nodes and their leaves */
6030 for (;;) {
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006031 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Christoph Hellwig16259e72005-11-02 15:11:25 +11006032 numrecs = be16_to_cpu(block->bb_numrecs);
Christoph Hellwig136341b2008-10-30 17:11:40 +11006033 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 xfs_trans_brelse(tp, bp);
6035 if (nextbno == NULLFSBLOCK)
6036 break;
6037 bno = nextbno;
6038 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6039 XFS_BMAP_BTREE_REF)))
6040 return error;
6041 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006042 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 }
6044 }
6045 return 0;
6046}
6047
6048/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006049 * Count leaf blocks given a range of extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006051STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006053 xfs_ifork_t *ifp,
6054 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 int numrecs,
6056 int *count)
6057{
6058 int b;
6059
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006060 for (b = 0; b < numrecs; b++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006061 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
Yingping Lu91e11082005-11-02 15:10:24 +11006062 *count += xfs_bmbt_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006063 }
Yingping Lu91e11082005-11-02 15:10:24 +11006064}
6065
6066/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006067 * Count leaf blocks given a range of extent records originally
6068 * in btree format.
Yingping Lu91e11082005-11-02 15:10:24 +11006069 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006070STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +11006071xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +11006072 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006073 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +11006074 int numrecs,
6075 int *count)
6076{
6077 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006078 xfs_bmbt_rec_t *frp;
Yingping Lu91e11082005-11-02 15:10:24 +11006079
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006080 for (b = 1; b <= numrecs; b++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11006081 frp = XFS_BMBT_REC_ADDR(mp, block, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 *count += xfs_bmbt_disk_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084}
Dave Chinnerc726de42010-11-30 15:14:39 +11006085
6086/*
6087 * dead simple method of punching delalyed allocation blocks from a range in
6088 * the inode. Walks a block at a time so will be slow, but is only executed in
6089 * rare error cases so the overhead is not critical. This will alays punch out
6090 * both the start and end blocks, even if the ranges only partially overlap
6091 * them, so it is up to the caller to ensure that partial blocks are not
6092 * passed in.
6093 */
6094int
6095xfs_bmap_punch_delalloc_range(
6096 struct xfs_inode *ip,
6097 xfs_fileoff_t start_fsb,
6098 xfs_fileoff_t length)
6099{
6100 xfs_fileoff_t remaining = length;
6101 int error = 0;
6102
6103 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6104
6105 do {
6106 int done;
6107 xfs_bmbt_irec_t imap;
6108 int nimaps = 1;
6109 xfs_fsblock_t firstblock;
6110 xfs_bmap_free_t flist;
6111
6112 /*
6113 * Map the range first and check that it is a delalloc extent
6114 * before trying to unmap the range. Otherwise we will be
6115 * trying to remove a real extent (which requires a
6116 * transaction) or a hole, which is probably a bad idea...
6117 */
6118 error = xfs_bmapi(NULL, ip, start_fsb, 1,
6119 XFS_BMAPI_ENTIRE, NULL, 0, &imap,
6120 &nimaps, NULL);
6121
6122 if (error) {
6123 /* something screwed, just bail */
6124 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner53487782011-03-07 10:05:35 +11006125 xfs_alert(ip->i_mount,
Dave Chinnerc726de42010-11-30 15:14:39 +11006126 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6127 ip->i_ino, start_fsb);
6128 }
6129 break;
6130 }
6131 if (!nimaps) {
6132 /* nothing there */
6133 goto next_block;
6134 }
6135 if (imap.br_startblock != DELAYSTARTBLOCK) {
6136 /* been converted, ignore */
6137 goto next_block;
6138 }
6139 WARN_ON(imap.br_blockcount == 0);
6140
6141 /*
6142 * Note: while we initialise the firstblock/flist pair, they
6143 * should never be used because blocks should never be
6144 * allocated or freed for a delalloc extent and hence we need
6145 * don't cancel or finish them after the xfs_bunmapi() call.
6146 */
6147 xfs_bmap_init(&flist, &firstblock);
6148 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6149 &flist, &done);
6150 if (error)
6151 break;
6152
6153 ASSERT(!flist.xbf_count && !flist.xbf_first);
6154next_block:
6155 start_fsb++;
6156 remaining--;
6157 } while(remaining > 0);
6158
6159 return error;
6160}