blob: d9de5ae1fd5c950b1612f1dafb524e184532a437 [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_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_itable.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_extfree_item.h"
39#include "xfs_alloc.h"
40#include "xfs_bmap.h"
41#include "xfs_rtalloc.h"
42#include "xfs_error.h"
Nathan Scottd8cc8902005-11-02 10:34:53 +110043#include "xfs_attr_leaf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_rw.h"
45#include "xfs_quota.h"
46#include "xfs_trans_space.h"
47#include "xfs_buf_item.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100048#include "xfs_filestream.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100049#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000050#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52
53#ifdef DEBUG
54STATIC void
55xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
56#endif
57
58kmem_zone_t *xfs_bmap_free_item_zone;
59
60/*
61 * Prototypes for internal bmap routines.
62 */
63
64
65/*
66 * Called from xfs_bmap_add_attrfork to handle extents format files.
67 */
68STATIC int /* error */
69xfs_bmap_add_attrfork_extents(
70 xfs_trans_t *tp, /* transaction pointer */
71 xfs_inode_t *ip, /* incore inode pointer */
72 xfs_fsblock_t *firstblock, /* first block allocated */
73 xfs_bmap_free_t *flist, /* blocks to free at commit */
74 int *flags); /* inode logging flags */
75
76/*
77 * Called from xfs_bmap_add_attrfork to handle local format files.
78 */
79STATIC int /* error */
80xfs_bmap_add_attrfork_local(
81 xfs_trans_t *tp, /* transaction pointer */
82 xfs_inode_t *ip, /* incore inode pointer */
83 xfs_fsblock_t *firstblock, /* first block allocated */
84 xfs_bmap_free_t *flist, /* blocks to free at commit */
85 int *flags); /* inode logging flags */
86
87/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * Called by xfs_bmap_add_extent to handle cases converting a delayed
89 * allocation to a real allocation.
90 */
91STATIC int /* error */
92xfs_bmap_add_extent_delay_real(
Christoph Hellwigf3ca8732011-07-08 14:34:47 +020093 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +000095 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +110097 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
99 xfs_fsblock_t *first, /* pointer to firstblock variable */
100 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwig54893272011-05-11 15:04:03 +0000101 int *logflagsp); /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/*
104 * Called by xfs_bmap_add_extent to handle cases converting a hole
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * to a real allocation.
106 */
107STATIC int /* error */
108xfs_bmap_add_extent_hole_real(
109 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000110 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100112 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 int *logflagsp, /* inode logging flags */
114 int whichfork); /* data or attr fork */
115
116/*
117 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
118 * allocation to a real allocation or vice versa.
119 */
120STATIC int /* error */
121xfs_bmap_add_extent_unwritten_real(
122 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000123 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100125 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000126 int *logflagsp); /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/*
129 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
130 * It figures out where to ask the underlying allocator to put the new extent.
131 */
132STATIC int /* error */
133xfs_bmap_alloc(
134 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
135
136/*
137 * Transform a btree format file with only one leaf node, where the
138 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100139 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * give up the space for the btree root and pitch the leaf block.
141 */
142STATIC int /* error */
143xfs_bmap_btree_to_extents(
144 xfs_trans_t *tp, /* transaction pointer */
145 xfs_inode_t *ip, /* incore inode pointer */
146 xfs_btree_cur_t *cur, /* btree cursor */
147 int *logflagsp, /* inode logging flags */
148 int whichfork); /* data or attr fork */
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * Remove the entry "free" from the free item list. Prev points to the
152 * previous entry, unless "free" is the head of the list.
153 */
154STATIC void
155xfs_bmap_del_free(
156 xfs_bmap_free_t *flist, /* free item list header */
157 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
158 xfs_bmap_free_item_t *free); /* list item to be freed */
159
160/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * Convert an extents-format file into a btree-format file.
162 * The new file will have a root block (in the inode) and a single child block.
163 */
164STATIC int /* error */
165xfs_bmap_extents_to_btree(
166 xfs_trans_t *tp, /* transaction pointer */
167 xfs_inode_t *ip, /* incore inode pointer */
168 xfs_fsblock_t *firstblock, /* first-block-allocated */
169 xfs_bmap_free_t *flist, /* blocks freed in xaction */
170 xfs_btree_cur_t **curp, /* cursor returned to caller */
171 int wasdel, /* converting a delayed alloc */
172 int *logflagsp, /* inode logging flags */
173 int whichfork); /* data or attr fork */
174
175/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * Convert a local file to an extents file.
177 * This code is sort of bogus, since the file data needs to get
178 * logged so it won't be lost. The bmap-level manipulations are ok, though.
179 */
180STATIC int /* error */
181xfs_bmap_local_to_extents(
182 xfs_trans_t *tp, /* transaction pointer */
183 xfs_inode_t *ip, /* incore inode pointer */
184 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
185 xfs_extlen_t total, /* total blocks needed by transaction */
186 int *logflagsp, /* inode logging flags */
187 int whichfork); /* data or attr fork */
188
189/*
190 * Search the extents list for the inode, for the extent containing bno.
191 * If bno lies in a hole, point to the next entry. If bno lies past eof,
192 * *eofp will be set, and *prevp will contain the last entry (null if none).
193 * Else, *lastxp will be set to the index of the found
194 * entry; *gotp will contain the entry.
195 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000196STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197xfs_bmap_search_extents(
198 xfs_inode_t *ip, /* incore inode pointer */
199 xfs_fileoff_t bno, /* block number searched for */
200 int whichfork, /* data or attr fork */
201 int *eofp, /* out: end of file found */
202 xfs_extnum_t *lastxp, /* out: last extent index */
203 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
204 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
205
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000206/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * Compute the worst-case number of indirect blocks that will be used
208 * for ip's delayed extent of length "len".
209 */
210STATIC xfs_filblks_t
211xfs_bmap_worst_indlen(
212 xfs_inode_t *ip, /* incore inode pointer */
213 xfs_filblks_t len); /* delayed extent length */
214
215#ifdef DEBUG
216/*
217 * Perform various validation checks on the values being returned
218 * from xfs_bmapi().
219 */
220STATIC void
221xfs_bmap_validate_ret(
222 xfs_fileoff_t bno,
223 xfs_filblks_t len,
224 int flags,
225 xfs_bmbt_irec_t *mval,
226 int nmap,
227 int ret_nmap);
228#else
229#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
230#endif /* DEBUG */
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232STATIC int
233xfs_bmap_count_tree(
234 xfs_mount_t *mp,
235 xfs_trans_t *tp,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100236 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 xfs_fsblock_t blockno,
238 int levelin,
239 int *count);
240
Ruben Porrasc94312d2008-08-13 16:52:25 +1000241STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100243 xfs_ifork_t *ifp,
244 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 int numrecs,
246 int *count);
247
Ruben Porrasc94312d2008-08-13 16:52:25 +1000248STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +1100249xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +1100250 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100251 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +1100252 int numrecs,
253 int *count);
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/*
256 * Bmap internal routines.
257 */
258
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100259STATIC int /* error */
260xfs_bmbt_lookup_eq(
261 struct xfs_btree_cur *cur,
262 xfs_fileoff_t off,
263 xfs_fsblock_t bno,
264 xfs_filblks_t len,
265 int *stat) /* success/failure */
266{
267 cur->bc_rec.b.br_startoff = off;
268 cur->bc_rec.b.br_startblock = bno;
269 cur->bc_rec.b.br_blockcount = len;
270 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
271}
272
273STATIC int /* error */
274xfs_bmbt_lookup_ge(
275 struct xfs_btree_cur *cur,
276 xfs_fileoff_t off,
277 xfs_fsblock_t bno,
278 xfs_filblks_t len,
279 int *stat) /* success/failure */
280{
281 cur->bc_rec.b.br_startoff = off;
282 cur->bc_rec.b.br_startblock = bno;
283 cur->bc_rec.b.br_blockcount = len;
284 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
285}
286
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100287/*
288* Update the record referred to by cur to the value given
289 * by [off, bno, len, state].
290 * This either works (return 0) or gets an EFSCORRUPTED error.
291 */
292STATIC int
293xfs_bmbt_update(
294 struct xfs_btree_cur *cur,
295 xfs_fileoff_t off,
296 xfs_fsblock_t bno,
297 xfs_filblks_t len,
298 xfs_exntst_t state)
299{
300 union xfs_btree_rec rec;
301
302 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
303 return xfs_btree_update(cur, &rec);
304}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/*
307 * Called from xfs_bmap_add_attrfork to handle btree format files.
308 */
309STATIC int /* error */
310xfs_bmap_add_attrfork_btree(
311 xfs_trans_t *tp, /* transaction pointer */
312 xfs_inode_t *ip, /* incore inode pointer */
313 xfs_fsblock_t *firstblock, /* first block allocated */
314 xfs_bmap_free_t *flist, /* blocks to free at commit */
315 int *flags) /* inode logging flags */
316{
317 xfs_btree_cur_t *cur; /* btree cursor */
318 int error; /* error return value */
319 xfs_mount_t *mp; /* file system mount struct */
320 int stat; /* newroot status */
321
322 mp = ip->i_mount;
323 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
324 *flags |= XFS_ILOG_DBROOT;
325 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100326 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 cur->bc_private.b.flist = flist;
328 cur->bc_private.b.firstblock = *firstblock;
329 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
330 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000331 /* must be at least one entry */
332 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100333 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 goto error0;
335 if (stat == 0) {
336 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
337 return XFS_ERROR(ENOSPC);
338 }
339 *firstblock = cur->bc_private.b.firstblock;
340 cur->bc_private.b.allocated = 0;
341 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
342 }
343 return 0;
344error0:
345 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
346 return error;
347}
348
349/*
350 * Called from xfs_bmap_add_attrfork to handle extents format files.
351 */
352STATIC int /* error */
353xfs_bmap_add_attrfork_extents(
354 xfs_trans_t *tp, /* transaction pointer */
355 xfs_inode_t *ip, /* incore inode pointer */
356 xfs_fsblock_t *firstblock, /* first block allocated */
357 xfs_bmap_free_t *flist, /* blocks to free at commit */
358 int *flags) /* inode logging flags */
359{
360 xfs_btree_cur_t *cur; /* bmap btree cursor */
361 int error; /* error return value */
362
363 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
364 return 0;
365 cur = NULL;
366 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
367 flags, XFS_DATA_FORK);
368 if (cur) {
369 cur->bc_private.b.allocated = 0;
370 xfs_btree_del_cursor(cur,
371 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
372 }
373 return error;
374}
375
376/*
377 * Called from xfs_bmap_add_attrfork to handle local format files.
378 */
379STATIC int /* error */
380xfs_bmap_add_attrfork_local(
381 xfs_trans_t *tp, /* transaction pointer */
382 xfs_inode_t *ip, /* incore inode pointer */
383 xfs_fsblock_t *firstblock, /* first block allocated */
384 xfs_bmap_free_t *flist, /* blocks to free at commit */
385 int *flags) /* inode logging flags */
386{
387 xfs_da_args_t dargs; /* args for dir/attr code */
388 int error; /* error return value */
389 xfs_mount_t *mp; /* mount structure pointer */
390
391 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
392 return 0;
Al Viroabbede12011-07-26 02:31:30 -0400393 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 mp = ip->i_mount;
395 memset(&dargs, 0, sizeof(dargs));
396 dargs.dp = ip;
397 dargs.firstblock = firstblock;
398 dargs.flist = flist;
399 dargs.total = mp->m_dirblkfsbs;
400 dargs.whichfork = XFS_DATA_FORK;
401 dargs.trans = tp;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000402 error = xfs_dir2_sf_to_block(&dargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 } else
404 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
405 XFS_DATA_FORK);
406 return error;
407}
408
409/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +0000410 * Update file extent records and the btree after allocating space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 */
412STATIC int /* error */
413xfs_bmap_add_extent(
Christoph Hellwigf3ca8732011-07-08 14:34:47 +0200414 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000416 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100418 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 xfs_fsblock_t *first, /* pointer to firstblock variable */
420 xfs_bmap_free_t *flist, /* list of extents to be freed */
421 int *logflagsp, /* inode logging flags */
Christoph Hellwig54893272011-05-11 15:04:03 +0000422 int whichfork) /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 xfs_btree_cur_t *cur; /* btree cursor or null */
425 xfs_filblks_t da_new; /* new count del alloc blocks used */
426 xfs_filblks_t da_old; /* old count del alloc blocks used */
427 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 xfs_ifork_t *ifp; /* inode fork ptr */
429 int logflags; /* returned value */
430 xfs_extnum_t nextents; /* number of extents in file now */
431
432 XFS_STATS_INC(xs_add_exlist);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 cur = *curp;
435 ifp = XFS_IFORK_PTR(ip, whichfork);
436 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 da_old = da_new = 0;
438 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000439
440 ASSERT(*idx >= 0);
441 ASSERT(*idx <= nextents);
Christoph Hellwig1fd044d2011-09-18 20:40:49 +0000442 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * Real allocation off the end of the file.
446 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +0000447 if (*idx == nextents) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (cur)
449 ASSERT((cur->bc_private.b.flags &
450 XFS_BTCUR_BPRV_WASDEL) == 0);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000451 error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
452 &logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 } else {
454 xfs_bmbt_irec_t prev; /* old extent at offset idx */
455
456 /*
457 * Get the record referred to by idx.
458 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000459 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /*
461 * If it's a real allocation record, and the new allocation ends
462 * after the start of the referred to record, then we're filling
463 * in a delayed or unwritten allocation with a real one, or
464 * converting real back to unwritten.
465 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600466 if (!isnullstartblock(new->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 new->br_startoff + new->br_blockcount > prev.br_startoff) {
468 if (prev.br_state != XFS_EXT_UNWRITTEN &&
Eric Sandeen9d87c312009-01-14 23:22:07 -0600469 isnullstartblock(prev.br_startblock)) {
470 da_old = startblockval(prev.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (cur)
472 ASSERT(cur->bc_private.b.flags &
473 XFS_BTCUR_BPRV_WASDEL);
Christoph Hellwigf3ca8732011-07-08 14:34:47 +0200474 error = xfs_bmap_add_extent_delay_real(tp, ip,
Christoph Hellwigec90c552011-05-23 08:52:53 +0000475 idx, &cur, new, &da_new,
476 first, flist, &logflags);
477 } else {
478 ASSERT(new->br_state == XFS_EXT_NORM ||
479 new->br_state == XFS_EXT_UNWRITTEN);
480
481 error = xfs_bmap_add_extent_unwritten_real(ip,
482 idx, &cur, new, &logflags);
Christoph Hellwig54893272011-05-11 15:04:03 +0000483 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 /*
488 * Otherwise we're filling in a hole with an allocation.
489 */
490 else {
491 if (cur)
492 ASSERT((cur->bc_private.b.flags &
493 XFS_BTCUR_BPRV_WASDEL) == 0);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000494 error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
495 new, &logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 }
498
Christoph Hellwigec90c552011-05-23 08:52:53 +0000499 if (error)
500 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 ASSERT(*curp == cur || *curp == NULL);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /*
504 * Convert to a btree if necessary.
505 */
506 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
507 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
508 int tmp_logflags; /* partial log flag return val */
509
510 ASSERT(cur == NULL);
Christoph Hellwigf3ca8732011-07-08 14:34:47 +0200511 error = xfs_bmap_extents_to_btree(tp, ip, first,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
513 logflags |= tmp_logflags;
514 if (error)
515 goto done;
516 }
517 /*
518 * Adjust for changes in reserved delayed indirect blocks.
519 * Nothing to do for disk quotas here.
520 */
521 if (da_old || da_new) {
522 xfs_filblks_t nblks;
523
524 nblks = da_new;
525 if (cur)
526 nblks += cur->bc_private.b.allocated;
527 ASSERT(nblks <= da_old);
528 if (nblks < da_old)
Christoph Hellwig96540c72010-09-30 02:25:55 +0000529 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +0000530 (int64_t)(da_old - nblks), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 /*
533 * Clear out the allocated field, done with it now in any case.
534 */
535 if (cur) {
536 cur->bc_private.b.allocated = 0;
537 *curp = cur;
538 }
539done:
540#ifdef DEBUG
541 if (!error)
542 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
543#endif
544 *logflagsp = logflags;
545 return error;
546}
547
548/*
549 * Called by xfs_bmap_add_extent to handle cases converting a delayed
550 * allocation to a real allocation.
551 */
552STATIC int /* error */
553xfs_bmap_add_extent_delay_real(
Christoph Hellwigf3ca8732011-07-08 14:34:47 +0200554 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000556 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100558 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
560 xfs_fsblock_t *first, /* pointer to firstblock variable */
561 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwig54893272011-05-11 15:04:03 +0000562 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 xfs_btree_cur_t *cur; /* btree cursor */
565 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000566 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100569 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 xfs_fileoff_t new_endoff; /* end offset of new entry */
571 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
572 /* left is 0, right is 1, prev is 2 */
573 int rval=0; /* return value (logging flags) */
574 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000575 xfs_filblks_t temp=0; /* value for dnew calculations */
576 xfs_filblks_t temp2=0;/* value for dnew calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 int tmp_rval; /* partial logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579#define LEFT r[0]
580#define RIGHT r[1]
581#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /*
584 * Set up a bunch of variables to make the tests simpler.
585 */
586 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100587 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000588 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 xfs_bmbt_get_all(ep, &PREV);
590 new_endoff = new->br_startoff + new->br_blockcount;
591 ASSERT(PREV.br_startoff <= new->br_startoff);
592 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /*
595 * Set flags determining what part of the previous delayed allocation
596 * extent is being replaced by a real allocation.
597 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000598 if (PREV.br_startoff == new->br_startoff)
599 state |= BMAP_LEFT_FILLING;
600 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
601 state |= BMAP_RIGHT_FILLING;
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /*
604 * Check and set flags if this segment has a left neighbor.
605 * Don't set contiguous if the combined extent would be too large.
606 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000607 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000608 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000609 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000610
611 if (isnullstartblock(LEFT.br_startblock))
612 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000614
615 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
616 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
617 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
618 LEFT.br_state == new->br_state &&
619 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
620 state |= BMAP_LEFT_CONTIG;
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /*
623 * Check and set flags if this segment has a right neighbor.
624 * Don't set contiguous if the combined extent would be too large.
625 * Also check for all-three-contiguous being too large.
626 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000627 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000628 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000629 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000630
631 if (isnullstartblock(RIGHT.br_startblock))
632 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000634
635 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
636 new_endoff == RIGHT.br_startoff &&
637 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
638 new->br_state == RIGHT.br_state &&
639 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
640 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
641 BMAP_RIGHT_FILLING)) !=
642 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
643 BMAP_RIGHT_FILLING) ||
644 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
645 <= MAXEXTLEN))
646 state |= BMAP_RIGHT_CONTIG;
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 error = 0;
649 /*
650 * Switch out based on the FILLING and CONTIG state bits.
651 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000652 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
653 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
654 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
655 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /*
657 * Filling in all of a previously delayed allocation extent.
658 * The left and right neighbors are both contiguous with new.
659 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000660 --*idx;
661 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
662 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 LEFT.br_blockcount + PREV.br_blockcount +
664 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000665 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000666
Christoph Hellwigec90c552011-05-23 08:52:53 +0000667 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 ip->i_d.di_nextents--;
669 if (cur == NULL)
670 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
671 else {
672 rval = XFS_ILOG_CORE;
673 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
674 RIGHT.br_startblock,
675 RIGHT.br_blockcount, &i)))
676 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000677 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100678 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000680 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100681 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000683 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
685 LEFT.br_startblock,
686 LEFT.br_blockcount +
687 PREV.br_blockcount +
688 RIGHT.br_blockcount, LEFT.br_state)))
689 goto done;
690 }
691 *dnew = 0;
692 break;
693
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000694 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /*
696 * Filling in all of a previously delayed allocation extent.
697 * The left neighbor is contiguous, the right is not.
698 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000699 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000700
Christoph Hellwigec90c552011-05-23 08:52:53 +0000701 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
702 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
703 LEFT.br_blockcount + PREV.br_blockcount);
704 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
705
706 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (cur == NULL)
708 rval = XFS_ILOG_DEXT;
709 else {
710 rval = 0;
711 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
712 LEFT.br_startblock, LEFT.br_blockcount,
713 &i)))
714 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000715 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
717 LEFT.br_startblock,
718 LEFT.br_blockcount +
719 PREV.br_blockcount, LEFT.br_state)))
720 goto done;
721 }
722 *dnew = 0;
723 break;
724
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000725 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /*
727 * Filling in all of a previously delayed allocation extent.
728 * The right neighbor is contiguous, the left is not.
729 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000730 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 xfs_bmbt_set_startblock(ep, new->br_startblock);
732 xfs_bmbt_set_blockcount(ep,
733 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000734 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000735
Christoph Hellwigec90c552011-05-23 08:52:53 +0000736 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (cur == NULL)
738 rval = XFS_ILOG_DEXT;
739 else {
740 rval = 0;
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
747 new->br_startblock,
748 PREV.br_blockcount +
749 RIGHT.br_blockcount, PREV.br_state)))
750 goto done;
751 }
Christoph Hellwigec90c552011-05-23 08:52:53 +0000752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 *dnew = 0;
754 break;
755
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000756 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /*
758 * Filling in all of a previously delayed allocation extent.
759 * Neither the left nor right neighbors are contiguous with
760 * the new one.
761 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000762 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000764 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 ip->i_d.di_nextents++;
767 if (cur == NULL)
768 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
769 else {
770 rval = XFS_ILOG_CORE;
771 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
772 new->br_startblock, new->br_blockcount,
773 &i)))
774 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000775 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100777 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000779 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
Christoph Hellwigec90c552011-05-23 08:52:53 +0000781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 *dnew = 0;
783 break;
784
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000785 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 /*
787 * Filling in the first part of a previous delayed allocation.
788 * The left neighbor is contiguous.
789 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000790 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
791 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 LEFT.br_blockcount + new->br_blockcount);
793 xfs_bmbt_set_startoff(ep,
794 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000795 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000798 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 xfs_bmbt_set_blockcount(ep, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (cur == NULL)
801 rval = XFS_ILOG_DEXT;
802 else {
803 rval = 0;
804 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
805 LEFT.br_startblock, LEFT.br_blockcount,
806 &i)))
807 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000808 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
810 LEFT.br_startblock,
811 LEFT.br_blockcount +
812 new->br_blockcount,
813 LEFT.br_state)))
814 goto done;
815 }
816 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600817 startblockval(PREV.br_startblock));
818 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000819 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
820
821 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 *dnew = temp;
823 break;
824
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000825 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /*
827 * Filling in the first part of a previous delayed allocation.
828 * The left neighbor is not contiguous.
829 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000830 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 xfs_bmbt_set_startoff(ep, new_endoff);
832 temp = PREV.br_blockcount - new->br_blockcount;
833 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000834 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 ip->i_d.di_nextents++;
836 if (cur == NULL)
837 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
838 else {
839 rval = XFS_ILOG_CORE;
840 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
841 new->br_startblock, new->br_blockcount,
842 &i)))
843 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000844 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100846 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000848 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
851 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
Christoph Hellwigf3ca8732011-07-08 14:34:47 +0200852 error = xfs_bmap_extents_to_btree(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 first, flist, &cur, 1, &tmp_rval,
854 XFS_DATA_FORK);
855 rval |= tmp_rval;
856 if (error)
857 goto done;
858 }
859 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600860 startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 (cur ? cur->bc_private.b.allocated : 0));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000862 ep = xfs_iext_get_ext(ifp, *idx + 1);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600863 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000864 trace_xfs_bmap_post_update(ip, *idx + 1, state, _THIS_IP_);
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 *dnew = temp;
867 break;
868
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000869 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /*
871 * Filling in the last part of a previous delayed allocation.
872 * The right neighbor is contiguous with the new allocation.
873 */
874 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000875 trace_xfs_bmap_pre_update(ip, *idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000877 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100878 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 new->br_blockcount + RIGHT.br_blockcount,
880 RIGHT.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000881 trace_xfs_bmap_post_update(ip, *idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (cur == NULL)
883 rval = XFS_ILOG_DEXT;
884 else {
885 rval = 0;
886 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
887 RIGHT.br_startblock,
888 RIGHT.br_blockcount, &i)))
889 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000890 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if ((error = xfs_bmbt_update(cur, new->br_startoff,
892 new->br_startblock,
893 new->br_blockcount +
894 RIGHT.br_blockcount,
895 RIGHT.br_state)))
896 goto done;
897 }
Christoph Hellwigec90c552011-05-23 08:52:53 +0000898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600900 startblockval(PREV.br_startblock));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000901 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600902 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000903 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
904
905 ++*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 *dnew = temp;
907 break;
908
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000909 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /*
911 * Filling in the last part of a previous delayed allocation.
912 * The right neighbor is not contiguous.
913 */
914 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000915 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000917 xfs_iext_insert(ip, *idx + 1, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 ip->i_d.di_nextents++;
919 if (cur == NULL)
920 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
921 else {
922 rval = XFS_ILOG_CORE;
923 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
924 new->br_startblock, new->br_blockcount,
925 &i)))
926 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000927 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100929 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000931 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
934 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
Christoph Hellwigf3ca8732011-07-08 14:34:47 +0200935 error = xfs_bmap_extents_to_btree(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 first, flist, &cur, 1, &tmp_rval,
937 XFS_DATA_FORK);
938 rval |= tmp_rval;
939 if (error)
940 goto done;
941 }
942 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600943 startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 (cur ? cur->bc_private.b.allocated : 0));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000945 ep = xfs_iext_get_ext(ifp, *idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600946 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000947 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
948
949 ++*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 *dnew = temp;
951 break;
952
953 case 0:
954 /*
955 * Filling in the middle part of a previous delayed allocation.
956 * Contiguity is impossible here.
957 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +0000958 *
959 * We start with a delayed allocation:
960 *
961 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
962 * PREV @ idx
963 *
964 * and we are allocating:
965 * +rrrrrrrrrrrrrrrrr+
966 * new
967 *
968 * and we set it up for insertion as:
969 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
970 * new
971 * PREV @ idx LEFT RIGHT
972 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 */
974 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000976 trace_xfs_bmap_pre_update(ip, *idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +0000977 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
978 LEFT = *new;
979 RIGHT.br_state = PREV.br_state;
980 RIGHT.br_startblock = nullstartblock(
981 (int)xfs_bmap_worst_indlen(ip, temp2));
982 RIGHT.br_startoff = new_endoff;
983 RIGHT.br_blockcount = temp2;
984 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000985 xfs_iext_insert(ip, *idx + 1, 2, &LEFT, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 ip->i_d.di_nextents++;
987 if (cur == NULL)
988 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
989 else {
990 rval = XFS_ILOG_CORE;
991 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
992 new->br_startblock, new->br_blockcount,
993 &i)))
994 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000995 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100997 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000999 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1002 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
Christoph Hellwigf3ca8732011-07-08 14:34:47 +02001003 error = xfs_bmap_extents_to_btree(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 first, flist, &cur, 1, &tmp_rval,
1005 XFS_DATA_FORK);
1006 rval |= tmp_rval;
1007 if (error)
1008 goto done;
1009 }
1010 temp = xfs_bmap_worst_indlen(ip, temp);
1011 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001012 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 (cur ? cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00001014 if (diff > 0) {
1015 error = xfs_icsb_modify_counters(ip->i_mount,
1016 XFS_SBS_FDBLOCKS,
1017 -((int64_t)diff), 0);
1018 ASSERT(!error);
1019 if (error)
1020 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00001022
Christoph Hellwigec90c552011-05-23 08:52:53 +00001023 ep = xfs_iext_get_ext(ifp, *idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001024 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +00001025 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1026 trace_xfs_bmap_pre_update(ip, *idx + 2, state, _THIS_IP_);
1027 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001028 nullstartblock((int)temp2));
Christoph Hellwigec90c552011-05-23 08:52:53 +00001029 trace_xfs_bmap_post_update(ip, *idx + 2, state, _THIS_IP_);
1030
1031 ++*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 *dnew = temp + temp2;
1033 break;
1034
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001035 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1036 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1037 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1038 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1039 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1040 case BMAP_LEFT_CONTIG:
1041 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /*
1043 * These cases are all impossible.
1044 */
1045 ASSERT(0);
1046 }
1047 *curp = cur;
1048done:
1049 *logflagsp = rval;
1050 return error;
1051#undef LEFT
1052#undef RIGHT
1053#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
1056/*
1057 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1058 * allocation to a real allocation or vice versa.
1059 */
1060STATIC int /* error */
1061xfs_bmap_add_extent_unwritten_real(
1062 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001063 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001065 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001066 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001069 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001072 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 xfs_fileoff_t new_endoff; /* end offset of new entry */
1074 xfs_exntst_t newext; /* new extent state */
1075 xfs_exntst_t oldext; /* old extent state */
1076 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1077 /* left is 0, right is 1, prev is 2 */
1078 int rval=0; /* return value (logging flags) */
1079 int state = 0;/* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081#define LEFT r[0]
1082#define RIGHT r[1]
1083#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /*
1085 * Set up a bunch of variables to make the tests simpler.
1086 */
1087 error = 0;
1088 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001089 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001090 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 xfs_bmbt_get_all(ep, &PREV);
1092 newext = new->br_state;
1093 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1094 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1095 ASSERT(PREV.br_state == oldext);
1096 new_endoff = new->br_startoff + new->br_blockcount;
1097 ASSERT(PREV.br_startoff <= new->br_startoff);
1098 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 /*
1101 * Set flags determining what part of the previous oldext allocation
1102 * extent is being replaced by a newext allocation.
1103 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001104 if (PREV.br_startoff == new->br_startoff)
1105 state |= BMAP_LEFT_FILLING;
1106 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1107 state |= BMAP_RIGHT_FILLING;
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 /*
1110 * Check and set flags if this segment has a left neighbor.
1111 * Don't set contiguous if the combined extent would be too large.
1112 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001113 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001114 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001115 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001116
1117 if (isnullstartblock(LEFT.br_startblock))
1118 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001120
1121 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1122 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1123 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1124 LEFT.br_state == newext &&
1125 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1126 state |= BMAP_LEFT_CONTIG;
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 /*
1129 * Check and set flags if this segment has a right neighbor.
1130 * Don't set contiguous if the combined extent would be too large.
1131 * Also check for all-three-contiguous being too large.
1132 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001133 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001134 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001135 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001136 if (isnullstartblock(RIGHT.br_startblock))
1137 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001139
1140 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1141 new_endoff == RIGHT.br_startoff &&
1142 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1143 newext == RIGHT.br_state &&
1144 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1145 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1146 BMAP_RIGHT_FILLING)) !=
1147 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1148 BMAP_RIGHT_FILLING) ||
1149 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1150 <= MAXEXTLEN))
1151 state |= BMAP_RIGHT_CONTIG;
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /*
1154 * Switch out based on the FILLING and CONTIG state bits.
1155 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001156 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1157 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1158 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1159 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 /*
1161 * Setting all of a previous oldext extent to newext.
1162 * The left and right neighbors are both contiguous with new.
1163 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001164 --*idx;
1165
1166 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1167 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 LEFT.br_blockcount + PREV.br_blockcount +
1169 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001170 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001171
Christoph Hellwigec90c552011-05-23 08:52:53 +00001172 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 ip->i_d.di_nextents -= 2;
1174 if (cur == NULL)
1175 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1176 else {
1177 rval = XFS_ILOG_CORE;
1178 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1179 RIGHT.br_startblock,
1180 RIGHT.br_blockcount, &i)))
1181 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001182 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11001183 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001185 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001186 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001188 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11001189 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001191 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001192 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001194 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1196 LEFT.br_startblock,
1197 LEFT.br_blockcount + PREV.br_blockcount +
1198 RIGHT.br_blockcount, LEFT.br_state)))
1199 goto done;
1200 }
1201 break;
1202
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001203 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /*
1205 * Setting all of a previous oldext extent to newext.
1206 * The left neighbor is contiguous, the right is not.
1207 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001208 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001209
Christoph Hellwigec90c552011-05-23 08:52:53 +00001210 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1211 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1212 LEFT.br_blockcount + PREV.br_blockcount);
1213 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1214
1215 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 ip->i_d.di_nextents--;
1217 if (cur == NULL)
1218 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1219 else {
1220 rval = XFS_ILOG_CORE;
1221 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1222 PREV.br_startblock, PREV.br_blockcount,
1223 &i)))
1224 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001225 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11001226 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001228 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001229 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001231 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1233 LEFT.br_startblock,
1234 LEFT.br_blockcount + PREV.br_blockcount,
1235 LEFT.br_state)))
1236 goto done;
1237 }
1238 break;
1239
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001240 case BMAP_LEFT_FILLING | 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 right neighbor is contiguous, the left is not.
1244 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001245 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 xfs_bmbt_set_blockcount(ep,
1247 PREV.br_blockcount + RIGHT.br_blockcount);
1248 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001249 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1250 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 ip->i_d.di_nextents--;
1252 if (cur == NULL)
1253 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1254 else {
1255 rval = XFS_ILOG_CORE;
1256 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1257 RIGHT.br_startblock,
1258 RIGHT.br_blockcount, &i)))
1259 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001260 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11001261 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001263 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001264 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001266 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1268 new->br_startblock,
1269 new->br_blockcount + RIGHT.br_blockcount,
1270 newext)))
1271 goto done;
1272 }
1273 break;
1274
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001275 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 /*
1277 * Setting all of a previous oldext extent to newext.
1278 * Neither the left nor right neighbors are contiguous with
1279 * the new one.
1280 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001281 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001283 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (cur == NULL)
1286 rval = XFS_ILOG_DEXT;
1287 else {
1288 rval = 0;
1289 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1290 new->br_startblock, new->br_blockcount,
1291 &i)))
1292 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001293 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1295 new->br_startblock, new->br_blockcount,
1296 newext)))
1297 goto done;
1298 }
1299 break;
1300
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001301 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /*
1303 * Setting the first part of a previous oldext extent to newext.
1304 * The left neighbor is contiguous.
1305 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001306 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
1307 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 LEFT.br_blockcount + new->br_blockcount);
1309 xfs_bmbt_set_startoff(ep,
1310 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001311 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001312
Christoph Hellwigec90c552011-05-23 08:52:53 +00001313 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 xfs_bmbt_set_startblock(ep,
1315 new->br_startblock + new->br_blockcount);
1316 xfs_bmbt_set_blockcount(ep,
1317 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001318 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001319
Christoph Hellwigec90c552011-05-23 08:52:53 +00001320 --*idx;
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (cur == NULL)
1323 rval = XFS_ILOG_DEXT;
1324 else {
1325 rval = 0;
1326 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1327 PREV.br_startblock, PREV.br_blockcount,
1328 &i)))
1329 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001330 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if ((error = xfs_bmbt_update(cur,
1332 PREV.br_startoff + new->br_blockcount,
1333 PREV.br_startblock + new->br_blockcount,
1334 PREV.br_blockcount - new->br_blockcount,
1335 oldext)))
1336 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001337 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 goto done;
1339 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1340 LEFT.br_startblock,
1341 LEFT.br_blockcount + new->br_blockcount,
1342 LEFT.br_state))
1343 goto done;
1344 }
1345 break;
1346
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001347 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 /*
1349 * Setting the first part of a previous oldext extent to newext.
1350 * The left neighbor is not contiguous.
1351 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001352 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1354 xfs_bmbt_set_startoff(ep, new_endoff);
1355 xfs_bmbt_set_blockcount(ep,
1356 PREV.br_blockcount - new->br_blockcount);
1357 xfs_bmbt_set_startblock(ep,
1358 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001359 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001360
Christoph Hellwigec90c552011-05-23 08:52:53 +00001361 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 ip->i_d.di_nextents++;
1363 if (cur == NULL)
1364 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1365 else {
1366 rval = XFS_ILOG_CORE;
1367 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1368 PREV.br_startblock, PREV.br_blockcount,
1369 &i)))
1370 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001371 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if ((error = xfs_bmbt_update(cur,
1373 PREV.br_startoff + new->br_blockcount,
1374 PREV.br_startblock + new->br_blockcount,
1375 PREV.br_blockcount - new->br_blockcount,
1376 oldext)))
1377 goto done;
1378 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001379 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001381 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383 break;
1384
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001385 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /*
1387 * Setting the last part of a previous oldext extent to newext.
1388 * The right neighbor is contiguous with the new allocation.
1389 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001390 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 xfs_bmbt_set_blockcount(ep,
1392 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001393 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1394
1395 ++*idx;
1396
1397 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1398 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001399 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001401 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (cur == NULL)
1404 rval = XFS_ILOG_DEXT;
1405 else {
1406 rval = 0;
1407 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1408 PREV.br_startblock,
1409 PREV.br_blockcount, &i)))
1410 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001411 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1413 PREV.br_startblock,
1414 PREV.br_blockcount - new->br_blockcount,
1415 oldext)))
1416 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001417 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 goto done;
1419 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1420 new->br_startblock,
1421 new->br_blockcount + RIGHT.br_blockcount,
1422 newext)))
1423 goto done;
1424 }
1425 break;
1426
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001427 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 /*
1429 * Setting the last part of a previous oldext extent to newext.
1430 * The right neighbor is not contiguous.
1431 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001432 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 xfs_bmbt_set_blockcount(ep,
1434 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001435 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001436
Christoph Hellwigec90c552011-05-23 08:52:53 +00001437 ++*idx;
1438 xfs_iext_insert(ip, *idx, 1, new, state);
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 ip->i_d.di_nextents++;
1441 if (cur == NULL)
1442 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1443 else {
1444 rval = XFS_ILOG_CORE;
1445 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1446 PREV.br_startblock, PREV.br_blockcount,
1447 &i)))
1448 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001449 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1451 PREV.br_startblock,
1452 PREV.br_blockcount - new->br_blockcount,
1453 oldext)))
1454 goto done;
1455 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1456 new->br_startblock, new->br_blockcount,
1457 &i)))
1458 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001459 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001461 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001463 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
1465 break;
1466
1467 case 0:
1468 /*
1469 * Setting the middle part of a previous oldext extent to
1470 * newext. Contiguity is impossible here.
1471 * One extent becomes three extents.
1472 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001473 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 xfs_bmbt_set_blockcount(ep,
1475 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001476 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 r[0] = *new;
1479 r[1].br_startoff = new_endoff;
1480 r[1].br_blockcount =
1481 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1482 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1483 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001484
1485 ++*idx;
1486 xfs_iext_insert(ip, *idx, 2, &r[0], state);
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 ip->i_d.di_nextents += 2;
1489 if (cur == NULL)
1490 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1491 else {
1492 rval = XFS_ILOG_CORE;
1493 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1494 PREV.br_startblock, PREV.br_blockcount,
1495 &i)))
1496 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001497 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 /* new right extent - oldext */
1499 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1500 r[1].br_startblock, r[1].br_blockcount,
1501 r[1].br_state)))
1502 goto done;
1503 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10001505 cur->bc_rec.b.br_blockcount =
1506 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001507 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001509 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001510 /*
1511 * Reset the cursor to the position of the new extent
1512 * we are about to insert as we can't trust it after
1513 * the previous insert.
1514 */
1515 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1516 new->br_startblock, new->br_blockcount,
1517 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001519 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001521 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001522 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001524 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
1526 break;
1527
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001528 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1529 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1530 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1531 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1532 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1533 case BMAP_LEFT_CONTIG:
1534 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 /*
1536 * These cases are all impossible.
1537 */
1538 ASSERT(0);
1539 }
1540 *curp = cur;
1541done:
1542 *logflagsp = rval;
1543 return error;
1544#undef LEFT
1545#undef RIGHT
1546#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
1549/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00001550 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00001552STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553xfs_bmap_add_extent_hole_delay(
1554 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001555 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00001556 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001558 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1560 xfs_filblks_t newlen=0; /* new indirect size */
1561 xfs_filblks_t oldlen=0; /* old indirect size */
1562 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1563 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001564 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001566 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001568 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /*
1571 * Check and set flags if this segment has a left neighbor
1572 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001573 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001574 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001575 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001576
1577 if (isnullstartblock(left.br_startblock))
1578 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 /*
1582 * Check and set flags if the current (right) segment exists.
1583 * If it doesn't exist, we're converting the hole at end-of-file.
1584 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001585 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001586 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00001587 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001588
1589 if (isnullstartblock(right.br_startblock))
1590 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 /*
1594 * Set contiguity flags on the left and right neighbors.
1595 * Don't let extents get too large, even if the pieces are contiguous.
1596 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001597 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1598 left.br_startoff + left.br_blockcount == new->br_startoff &&
1599 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1600 state |= BMAP_LEFT_CONTIG;
1601
1602 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1603 new->br_startoff + new->br_blockcount == right.br_startoff &&
1604 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1605 (!(state & BMAP_LEFT_CONTIG) ||
1606 (left.br_blockcount + new->br_blockcount +
1607 right.br_blockcount <= MAXEXTLEN)))
1608 state |= BMAP_RIGHT_CONTIG;
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /*
1611 * Switch out based on the contiguity flags.
1612 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001613 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1614 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 /*
1616 * New allocation is contiguous with delayed allocations
1617 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001618 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001620 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 temp = left.br_blockcount + new->br_blockcount +
1622 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001623
Christoph Hellwigec90c552011-05-23 08:52:53 +00001624 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1625 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001626 oldlen = startblockval(left.br_startblock) +
1627 startblockval(new->br_startblock) +
1628 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001630 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001631 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00001632 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001633
Christoph Hellwigec90c552011-05-23 08:52:53 +00001634 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 break;
1636
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001637 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 /*
1639 * New allocation is contiguous with a delayed allocation
1640 * on the left.
1641 * Merge the new allocation with the left neighbor.
1642 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001643 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001645
1646 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1647 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001648 oldlen = startblockval(left.br_startblock) +
1649 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001651 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001652 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00001653 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 break;
1655
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001656 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 /*
1658 * New allocation is contiguous with a delayed allocation
1659 * on the right.
1660 * Merge the new allocation with the right neighbor.
1661 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001662 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001664 oldlen = startblockval(new->br_startblock) +
1665 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00001667 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1668 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06001669 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001670 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 break;
1672
1673 case 0:
1674 /*
1675 * New allocation is not contiguous with another
1676 * delayed allocation.
1677 * Insert a new entry.
1678 */
1679 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001680 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 break;
1682 }
1683 if (oldlen != newlen) {
1684 ASSERT(oldlen > newlen);
Christoph Hellwig96540c72010-09-30 02:25:55 +00001685 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00001686 (int64_t)(oldlen - newlen), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /*
1688 * Nothing to do for disk quota accounting here.
1689 */
1690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
1693/*
1694 * Called by xfs_bmap_add_extent to handle cases converting a hole
1695 * to a real allocation.
1696 */
1697STATIC int /* error */
1698xfs_bmap_add_extent_hole_real(
1699 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001700 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001702 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 int *logflagsp, /* inode logging flags */
1704 int whichfork) /* data or attr fork */
1705{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 int i; /* temp state */
1708 xfs_ifork_t *ifp; /* inode fork pointer */
1709 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1710 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001711 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001715 ASSERT(*idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 state = 0;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001717
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001718 if (whichfork == XFS_ATTR_FORK)
1719 state |= BMAP_ATTRFORK;
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 /*
1722 * Check and set flags if this segment has a left neighbor.
1723 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001724 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001725 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001726 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001727 if (isnullstartblock(left.br_startblock))
1728 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /*
1732 * Check and set flags if this segment has a current value.
1733 * Not true if we're inserting into the "hole" at eof.
1734 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001735 if (*idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001736 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00001737 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001738 if (isnullstartblock(right.br_startblock))
1739 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 /*
1743 * We're inserting a real allocation between "left" and "right".
1744 * Set the contiguity flags. Don't let extents get too large.
1745 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001746 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1747 left.br_startoff + left.br_blockcount == new->br_startoff &&
1748 left.br_startblock + left.br_blockcount == new->br_startblock &&
1749 left.br_state == new->br_state &&
1750 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1751 state |= BMAP_LEFT_CONTIG;
1752
1753 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1754 new->br_startoff + new->br_blockcount == right.br_startoff &&
1755 new->br_startblock + new->br_blockcount == right.br_startblock &&
1756 new->br_state == right.br_state &&
1757 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1758 (!(state & BMAP_LEFT_CONTIG) ||
1759 left.br_blockcount + new->br_blockcount +
1760 right.br_blockcount <= MAXEXTLEN))
1761 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001763 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 /*
1765 * Select which case we're in here, and implement it.
1766 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001767 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1768 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 /*
1770 * New allocation is contiguous with real allocations on the
1771 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001772 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001774 --*idx;
1775 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1776 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 left.br_blockcount + new->br_blockcount +
1778 right.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001779 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001780
Christoph Hellwigec90c552011-05-23 08:52:53 +00001781 xfs_iext_remove(ip, *idx + 1, 1, state);
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 XFS_IFORK_NEXT_SET(ip, whichfork,
1784 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1785 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001786 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001787 } else {
1788 rval = XFS_ILOG_CORE;
1789 if ((error = xfs_bmbt_lookup_eq(cur,
1790 right.br_startoff,
1791 right.br_startblock,
1792 right.br_blockcount, &i)))
1793 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001794 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11001795 if ((error = xfs_btree_delete(cur, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001796 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001797 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001798 if ((error = xfs_btree_decrement(cur, 0, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001799 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001800 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001801 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1802 left.br_startblock,
1803 left.br_blockcount +
1804 new->br_blockcount +
1805 right.br_blockcount,
1806 left.br_state)))
1807 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001809 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001811 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /*
1813 * New allocation is contiguous with a real allocation
1814 * on the left.
1815 * Merge the new allocation with the left neighbor.
1816 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001817 --*idx;
1818 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1819 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 left.br_blockcount + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001821 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001824 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001825 } else {
1826 rval = 0;
1827 if ((error = xfs_bmbt_lookup_eq(cur,
1828 left.br_startoff,
1829 left.br_startblock,
1830 left.br_blockcount, &i)))
1831 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001832 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001833 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1834 left.br_startblock,
1835 left.br_blockcount +
1836 new->br_blockcount,
1837 left.br_state)))
1838 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001840 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001842 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 /*
1844 * New allocation is contiguous with a real allocation
1845 * on the right.
1846 * Merge the new allocation with the right neighbor.
1847 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001848 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00001849 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1850 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 new->br_blockcount + right.br_blockcount,
1852 right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001853 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001856 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001857 } else {
1858 rval = 0;
1859 if ((error = xfs_bmbt_lookup_eq(cur,
1860 right.br_startoff,
1861 right.br_startblock,
1862 right.br_blockcount, &i)))
1863 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001864 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001865 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1866 new->br_startblock,
1867 new->br_blockcount +
1868 right.br_blockcount,
1869 right.br_state)))
1870 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001872 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 case 0:
1875 /*
1876 * New allocation is not contiguous with another
1877 * real allocation.
1878 * Insert a new entry.
1879 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001880 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 XFS_IFORK_NEXT_SET(ip, whichfork,
1882 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
1883 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001884 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001885 } else {
1886 rval = XFS_ILOG_CORE;
1887 if ((error = xfs_bmbt_lookup_eq(cur,
1888 new->br_startoff,
1889 new->br_startblock,
1890 new->br_blockcount, &i)))
1891 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001892 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001893 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001894 if ((error = xfs_btree_insert(cur, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001895 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001896 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001898 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001900done:
1901 *logflagsp = rval;
1902 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
Nathan Scottdd9f4382006-01-11 15:28:28 +11001905/*
1906 * Adjust the size of the new extent based on di_extsize and rt extsize.
1907 */
1908STATIC int
1909xfs_bmap_extsize_align(
1910 xfs_mount_t *mp,
1911 xfs_bmbt_irec_t *gotp, /* next extent pointer */
1912 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
1913 xfs_extlen_t extsz, /* align to this extent size */
1914 int rt, /* is this a realtime inode? */
1915 int eof, /* is extent at end-of-file? */
1916 int delay, /* creating delalloc extent? */
1917 int convert, /* overwriting unwritten extent? */
1918 xfs_fileoff_t *offp, /* in/out: aligned offset */
1919 xfs_extlen_t *lenp) /* in/out: aligned length */
1920{
1921 xfs_fileoff_t orig_off; /* original offset */
1922 xfs_extlen_t orig_alen; /* original length */
1923 xfs_fileoff_t orig_end; /* original off+len */
1924 xfs_fileoff_t nexto; /* next file offset */
1925 xfs_fileoff_t prevo; /* previous file offset */
1926 xfs_fileoff_t align_off; /* temp for offset */
1927 xfs_extlen_t align_alen; /* temp for length */
1928 xfs_extlen_t temp; /* temp for calculations */
1929
1930 if (convert)
1931 return 0;
1932
1933 orig_off = align_off = *offp;
1934 orig_alen = align_alen = *lenp;
1935 orig_end = orig_off + orig_alen;
1936
1937 /*
1938 * If this request overlaps an existing extent, then don't
1939 * attempt to perform any additional alignment.
1940 */
1941 if (!delay && !eof &&
1942 (orig_off >= gotp->br_startoff) &&
1943 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
1944 return 0;
1945 }
1946
1947 /*
1948 * If the file offset is unaligned vs. the extent size
1949 * we need to align it. This will be possible unless
1950 * the file was previously written with a kernel that didn't
1951 * perform this alignment, or if a truncate shot us in the
1952 * foot.
1953 */
1954 temp = do_mod(orig_off, extsz);
1955 if (temp) {
1956 align_alen += temp;
1957 align_off -= temp;
1958 }
1959 /*
1960 * Same adjustment for the end of the requested area.
1961 */
1962 if ((temp = (align_alen % extsz))) {
1963 align_alen += extsz - temp;
1964 }
1965 /*
1966 * If the previous block overlaps with this proposed allocation
1967 * then move the start forward without adjusting the length.
1968 */
1969 if (prevp->br_startoff != NULLFILEOFF) {
1970 if (prevp->br_startblock == HOLESTARTBLOCK)
1971 prevo = prevp->br_startoff;
1972 else
1973 prevo = prevp->br_startoff + prevp->br_blockcount;
1974 } else
1975 prevo = 0;
1976 if (align_off != orig_off && align_off < prevo)
1977 align_off = prevo;
1978 /*
1979 * If the next block overlaps with this proposed allocation
1980 * then move the start back without adjusting the length,
1981 * but not before offset 0.
1982 * This may of course make the start overlap previous block,
1983 * and if we hit the offset 0 limit then the next block
1984 * can still overlap too.
1985 */
1986 if (!eof && gotp->br_startoff != NULLFILEOFF) {
1987 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
1988 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
1989 nexto = gotp->br_startoff + gotp->br_blockcount;
1990 else
1991 nexto = gotp->br_startoff;
1992 } else
1993 nexto = NULLFILEOFF;
1994 if (!eof &&
1995 align_off + align_alen != orig_end &&
1996 align_off + align_alen > nexto)
1997 align_off = nexto > align_alen ? nexto - align_alen : 0;
1998 /*
1999 * If we're now overlapping the next or previous extent that
2000 * means we can't fit an extsz piece in this hole. Just move
2001 * the start forward to the first valid spot and set
2002 * the length so we hit the end.
2003 */
2004 if (align_off != orig_off && align_off < prevo)
2005 align_off = prevo;
2006 if (align_off + align_alen != orig_end &&
2007 align_off + align_alen > nexto &&
2008 nexto != NULLFILEOFF) {
2009 ASSERT(nexto > prevo);
2010 align_alen = nexto - align_off;
2011 }
2012
2013 /*
2014 * If realtime, and the result isn't a multiple of the realtime
2015 * extent size we need to remove blocks until it is.
2016 */
2017 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2018 /*
2019 * We're not covering the original request, or
2020 * we won't be able to once we fix the length.
2021 */
2022 if (orig_off < align_off ||
2023 orig_end > align_off + align_alen ||
2024 align_alen - temp < orig_alen)
2025 return XFS_ERROR(EINVAL);
2026 /*
2027 * Try to fix it by moving the start up.
2028 */
2029 if (align_off + temp <= orig_off) {
2030 align_alen -= temp;
2031 align_off += temp;
2032 }
2033 /*
2034 * Try to fix it by moving the end in.
2035 */
2036 else if (align_off + align_alen - temp >= orig_end)
2037 align_alen -= temp;
2038 /*
2039 * Set the start to the minimum then trim the length.
2040 */
2041 else {
2042 align_alen -= orig_off - align_off;
2043 align_off = orig_off;
2044 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2045 }
2046 /*
2047 * Result doesn't cover the request, fail it.
2048 */
2049 if (orig_off < align_off || orig_end > align_off + align_alen)
2050 return XFS_ERROR(EINVAL);
2051 } else {
2052 ASSERT(orig_off >= align_off);
2053 ASSERT(orig_end <= align_off + align_alen);
2054 }
2055
2056#ifdef DEBUG
2057 if (!eof && gotp->br_startoff != NULLFILEOFF)
2058 ASSERT(align_off + align_alen <= gotp->br_startoff);
2059 if (prevp->br_startoff != NULLFILEOFF)
2060 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2061#endif
2062
2063 *lenp = align_alen;
2064 *offp = align_off;
2065 return 0;
2066}
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068#define XFS_ALLOC_GAP_UNITS 4
2069
David Chinnerc2b1cba2008-04-10 12:21:40 +10002070STATIC void
Nathan Scotta365bdd2006-03-14 13:34:16 +11002071xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2073{
2074 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2076 xfs_mount_t *mp; /* mount point structure */
2077 int nullfb; /* true if ap->firstblock isn't set */
2078 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080#define ISVALID(x,y) \
2081 (rt ? \
2082 (x) < mp->m_sb.sb_rblocks : \
2083 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2084 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2085 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 mp = ap->ip->i_mount;
2088 nullfb = ap->firstblock == NULLFSBLOCK;
2089 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2090 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 /*
2092 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002093 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 */
2095 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002096 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2098 ap->prevp->br_startblock)) {
2099 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2100 /*
2101 * Adjust for the gap between prevp and us.
2102 */
2103 adjust = ap->off -
2104 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2105 if (adjust &&
2106 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2107 ap->rval += adjust;
2108 }
2109 /*
2110 * If not at eof, then compare the two neighbor blocks.
2111 * Figure out whether either one gives us a good starting point,
2112 * and pick the better one.
2113 */
2114 else if (!ap->eof) {
2115 xfs_fsblock_t gotbno; /* right side block number */
2116 xfs_fsblock_t gotdiff=0; /* right side difference */
2117 xfs_fsblock_t prevbno; /* left side block number */
2118 xfs_fsblock_t prevdiff=0; /* left side difference */
2119
2120 /*
2121 * If there's a previous (left) block, select a requested
2122 * start block based on it.
2123 */
2124 if (ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002125 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 (prevbno = ap->prevp->br_startblock +
2127 ap->prevp->br_blockcount) &&
2128 ISVALID(prevbno, ap->prevp->br_startblock)) {
2129 /*
2130 * Calculate gap to end of previous block.
2131 */
2132 adjust = prevdiff = ap->off -
2133 (ap->prevp->br_startoff +
2134 ap->prevp->br_blockcount);
2135 /*
2136 * Figure the startblock based on the previous block's
2137 * end and the gap size.
2138 * Heuristic!
2139 * If the gap is large relative to the piece we're
2140 * allocating, or using it gives us an invalid block
2141 * number, then just use the end of the previous block.
2142 */
2143 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2144 ISVALID(prevbno + prevdiff,
2145 ap->prevp->br_startblock))
2146 prevbno += adjust;
2147 else
2148 prevdiff += adjust;
2149 /*
2150 * If the firstblock forbids it, can't use it,
2151 * must use default.
2152 */
2153 if (!rt && !nullfb &&
2154 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2155 prevbno = NULLFSBLOCK;
2156 }
2157 /*
2158 * No previous block or can't follow it, just default.
2159 */
2160 else
2161 prevbno = NULLFSBLOCK;
2162 /*
2163 * If there's a following (right) block, select a requested
2164 * start block based on it.
2165 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002166 if (!isnullstartblock(ap->gotp->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 /*
2168 * Calculate gap to start of next block.
2169 */
2170 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2171 /*
2172 * Figure the startblock based on the next block's
2173 * start and the gap size.
2174 */
2175 gotbno = ap->gotp->br_startblock;
2176 /*
2177 * Heuristic!
2178 * If the gap is large relative to the piece we're
2179 * allocating, or using it gives us an invalid block
2180 * number, then just use the start of the next block
2181 * offset by our length.
2182 */
2183 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2184 ISVALID(gotbno - gotdiff, gotbno))
2185 gotbno -= adjust;
2186 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2187 gotbno -= ap->alen;
2188 gotdiff += adjust - ap->alen;
2189 } else
2190 gotdiff += adjust;
2191 /*
2192 * If the firstblock forbids it, can't use it,
2193 * must use default.
2194 */
2195 if (!rt && !nullfb &&
2196 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2197 gotbno = NULLFSBLOCK;
2198 }
2199 /*
2200 * No next block, just default.
2201 */
2202 else
2203 gotbno = NULLFSBLOCK;
2204 /*
2205 * If both valid, pick the better one, else the only good
2206 * one, else ap->rval is already set (to 0 or the inode block).
2207 */
2208 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2209 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2210 else if (prevbno != NULLFSBLOCK)
2211 ap->rval = prevbno;
2212 else if (gotbno != NULLFSBLOCK)
2213 ap->rval = gotbno;
2214 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002215#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11002216}
2217
2218STATIC int
2219xfs_bmap_rtalloc(
2220 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2221{
2222 xfs_alloctype_t atype = 0; /* type for allocation routines */
2223 int error; /* error return value */
2224 xfs_mount_t *mp; /* mount point structure */
2225 xfs_extlen_t prod = 0; /* product factor for allocators */
2226 xfs_extlen_t ralen = 0; /* realtime allocation length */
2227 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002228 xfs_rtblock_t rtb;
2229
2230 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002231 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002232 prod = align / mp->m_sb.sb_rextsize;
2233 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2234 align, 1, ap->eof, 0,
2235 ap->conv, &ap->off, &ap->alen);
2236 if (error)
2237 return error;
2238 ASSERT(ap->alen);
2239 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2240
2241 /*
2242 * If the offset & length are not perfectly aligned
2243 * then kill prod, it will just get us in trouble.
2244 */
2245 if (do_mod(ap->off, align) || ap->alen % align)
2246 prod = 1;
2247 /*
2248 * Set ralen to be the actual requested length in rtextents.
2249 */
2250 ralen = ap->alen / mp->m_sb.sb_rextsize;
2251 /*
2252 * If the old value was close enough to MAXEXTLEN that
2253 * we rounded up to it, cut it back so it's valid again.
2254 * Note that if it's a really large request (bigger than
2255 * MAXEXTLEN), we don't hear about that number, and can't
2256 * adjust the starting point to match it.
2257 */
2258 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2259 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
Christoph Hellwig04e99452011-01-25 09:06:19 +00002260
2261 /*
2262 * Lock out other modifications to the RT bitmap inode.
2263 */
Christoph Hellwig1050c712011-02-13 13:25:31 +00002264 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
2265 xfs_trans_ijoin_ref(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Christoph Hellwig04e99452011-01-25 09:06:19 +00002266
Nathan Scotta365bdd2006-03-14 13:34:16 +11002267 /*
2268 * If it's an allocation to an empty file at offset 0,
2269 * pick an extent that will space things out in the rt area.
2270 */
2271 if (ap->eof && ap->off == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10002272 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2273
Nathan Scotta365bdd2006-03-14 13:34:16 +11002274 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2275 if (error)
2276 return error;
2277 ap->rval = rtx * mp->m_sb.sb_rextsize;
2278 } else {
2279 ap->rval = 0;
2280 }
2281
2282 xfs_bmap_adjacent(ap);
2283
2284 /*
2285 * Realtime allocation, done through xfs_rtallocate_extent.
2286 */
2287 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2288 do_div(ap->rval, mp->m_sb.sb_rextsize);
2289 rtb = ap->rval;
2290 ap->alen = ralen;
2291 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2292 &ralen, atype, ap->wasdel, prod, &rtb)))
2293 return error;
2294 if (rtb == NULLFSBLOCK && prod > 1 &&
2295 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2296 ap->alen, &ralen, atype,
2297 ap->wasdel, 1, &rtb)))
2298 return error;
2299 ap->rval = rtb;
2300 if (ap->rval != NULLFSBLOCK) {
2301 ap->rval *= mp->m_sb.sb_rextsize;
2302 ralen *= mp->m_sb.sb_rextsize;
2303 ap->alen = ralen;
2304 ap->ip->i_d.di_nblocks += ralen;
2305 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2306 if (ap->wasdel)
2307 ap->ip->i_delayed_blks -= ralen;
2308 /*
2309 * Adjust the disk quota also. This was reserved
2310 * earlier.
2311 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002312 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002313 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2314 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2315 } else {
2316 ap->alen = 0;
2317 }
2318 return 0;
2319}
2320
2321STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00002322xfs_bmap_btalloc_nullfb(
2323 struct xfs_bmalloca *ap,
2324 struct xfs_alloc_arg *args,
2325 xfs_extlen_t *blen)
2326{
2327 struct xfs_mount *mp = ap->ip->i_mount;
2328 struct xfs_perag *pag;
2329 xfs_agnumber_t ag, startag;
2330 int notinit = 0;
2331 int error;
2332
2333 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2334 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2335 else
2336 args->type = XFS_ALLOCTYPE_START_BNO;
2337 args->total = ap->total;
2338
2339 /*
2340 * Search for an allocation group with a single extent large enough
2341 * for the request. If one isn't found, then adjust the minimum
2342 * allocation size to the largest space found.
2343 */
2344 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2345 if (startag == NULLAGNUMBER)
2346 startag = ag = 0;
2347
2348 pag = xfs_perag_get(mp, ag);
Dave Chinner14b064c2011-01-27 12:16:28 +11002349 while (*blen < args->maxlen) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00002350 if (!pag->pagf_init) {
2351 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2352 XFS_ALLOC_FLAG_TRYLOCK);
2353 if (error) {
2354 xfs_perag_put(pag);
2355 return error;
2356 }
2357 }
2358
2359 /*
2360 * See xfs_alloc_fix_freelist...
2361 */
2362 if (pag->pagf_init) {
2363 xfs_extlen_t longest;
2364 longest = xfs_alloc_longest_free_extent(mp, pag);
2365 if (*blen < longest)
2366 *blen = longest;
2367 } else
2368 notinit = 1;
2369
2370 if (xfs_inode_is_filestream(ap->ip)) {
Dave Chinner14b064c2011-01-27 12:16:28 +11002371 if (*blen >= args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00002372 break;
2373
2374 if (ap->userdata) {
2375 /*
2376 * If startag is an invalid AG, we've
2377 * come here once before and
2378 * xfs_filestream_new_ag picked the
2379 * best currently available.
2380 *
2381 * Don't continue looping, since we
2382 * could loop forever.
2383 */
2384 if (startag == NULLAGNUMBER)
2385 break;
2386
2387 error = xfs_filestream_new_ag(ap, &ag);
2388 xfs_perag_put(pag);
2389 if (error)
2390 return error;
2391
2392 /* loop again to set 'blen'*/
2393 startag = NULLAGNUMBER;
2394 pag = xfs_perag_get(mp, ag);
2395 continue;
2396 }
2397 }
2398 if (++ag == mp->m_sb.sb_agcount)
2399 ag = 0;
2400 if (ag == startag)
2401 break;
2402 xfs_perag_put(pag);
2403 pag = xfs_perag_get(mp, ag);
2404 }
2405 xfs_perag_put(pag);
2406
2407 /*
2408 * Since the above loop did a BUF_TRYLOCK, it is
2409 * possible that there is space for this request.
2410 */
2411 if (notinit || *blen < ap->minlen)
2412 args->minlen = ap->minlen;
2413 /*
2414 * If the best seen length is less than the request
2415 * length, use the best as the minimum.
2416 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002417 else if (*blen < args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00002418 args->minlen = *blen;
2419 /*
Dave Chinner14b064c2011-01-27 12:16:28 +11002420 * Otherwise we've seen an extent as big as maxlen,
Christoph Hellwigc467c042010-02-15 23:34:42 +00002421 * use that as the minimum.
2422 */
2423 else
Dave Chinner14b064c2011-01-27 12:16:28 +11002424 args->minlen = args->maxlen;
Christoph Hellwigc467c042010-02-15 23:34:42 +00002425
2426 /*
2427 * set the failure fallback case to look in the selected
2428 * AG as the stream may have moved.
2429 */
2430 if (xfs_inode_is_filestream(ap->ip))
2431 ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2432
2433 return 0;
2434}
2435
2436STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11002437xfs_bmap_btalloc(
2438 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2439{
2440 xfs_mount_t *mp; /* mount point structure */
2441 xfs_alloctype_t atype = 0; /* type for allocation routines */
2442 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002443 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00002444 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002445 xfs_alloc_arg_t args;
2446 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002447 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002448 int nullfb; /* true if ap->firstblock isn't set */
2449 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002450 int tryagain;
2451 int error;
2452
2453 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002454 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002455 if (unlikely(align)) {
2456 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2457 align, 0, ap->eof, 0, ap->conv,
2458 &ap->off, &ap->alen);
2459 ASSERT(!error);
2460 ASSERT(ap->alen);
2461 }
2462 nullfb = ap->firstblock == NULLFSBLOCK;
2463 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10002464 if (nullfb) {
2465 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2466 ag = xfs_filestream_lookup_ag(ap->ip);
2467 ag = (ag != NULLAGNUMBER) ? ag : 0;
2468 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2469 } else {
2470 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2471 }
2472 } else
Nathan Scotta365bdd2006-03-14 13:34:16 +11002473 ap->rval = ap->firstblock;
2474
2475 xfs_bmap_adjacent(ap);
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 /*
2478 * If allowed, use ap->rval; otherwise must use firstblock since
2479 * it's in the right allocation group.
2480 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002481 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 ;
2483 else
2484 ap->rval = ap->firstblock;
2485 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 * Normal allocation, done through xfs_alloc_vextent.
2487 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002488 tryagain = isaligned = 0;
2489 args.tp = ap->tp;
2490 args.mp = mp;
2491 args.fsbno = ap->rval;
Dave Chinner14b064c2011-01-27 12:16:28 +11002492
2493 /* Trim the allocation back to the maximum an AG can fit. */
2494 args.maxlen = MIN(ap->alen, XFS_ALLOC_AG_MAX_USABLE(mp));
Yingping Lud210a282006-06-09 14:55:18 +10002495 args.firstblock = ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002496 blen = 0;
2497 if (nullfb) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00002498 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2499 if (error)
2500 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002501 } else if (ap->low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002502 if (xfs_inode_is_filestream(ap->ip))
2503 args.type = XFS_ALLOCTYPE_FIRST_AG;
2504 else
2505 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002506 args.total = args.minlen = ap->minlen;
2507 } else {
2508 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2509 args.total = ap->total;
2510 args.minlen = ap->minlen;
2511 }
David Chinner957d0eb2007-06-18 16:50:37 +10002512 /* apply extent size hints if obtained earlier */
2513 if (unlikely(align)) {
2514 args.prod = align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002515 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2516 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11002517 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11002518 args.prod = 1;
2519 args.mod = 0;
2520 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002521 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002522 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2523 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2524 }
2525 /*
2526 * If we are not low on available data blocks, and the
2527 * underlying logical volume manager is a stripe, and
2528 * the file offset is zero then try to allocate data
2529 * blocks on stripe unit boundary.
2530 * NOTE: ap->aeof is only set if the allocation length
2531 * is >= the stripe unit and the allocation offset is
2532 * at the end of file.
2533 */
2534 if (!ap->low && ap->aeof) {
2535 if (!ap->off) {
2536 args.alignment = mp->m_dalign;
2537 atype = args.type;
2538 isaligned = 1;
2539 /*
2540 * Adjust for alignment
2541 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002542 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002543 args.minlen = blen - args.alignment;
2544 args.minalignslop = 0;
2545 } else {
2546 /*
2547 * First try an exact bno allocation.
2548 * If it fails then do a near or start bno
2549 * allocation with alignment turned on.
2550 */
2551 atype = args.type;
2552 tryagain = 1;
2553 args.type = XFS_ALLOCTYPE_THIS_BNO;
2554 args.alignment = 1;
2555 /*
2556 * Compute the minlen+alignment for the
2557 * next case. Set slop so that the value
2558 * of minlen+alignment+slop doesn't go up
2559 * between the calls.
2560 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002561 if (blen > mp->m_dalign && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002562 nextminlen = blen - mp->m_dalign;
2563 else
2564 nextminlen = args.minlen;
2565 if (nextminlen + mp->m_dalign > args.minlen + 1)
2566 args.minalignslop =
2567 nextminlen + mp->m_dalign -
2568 args.minlen - 1;
2569 else
2570 args.minalignslop = 0;
2571 }
2572 } else {
2573 args.alignment = 1;
2574 args.minalignslop = 0;
2575 }
2576 args.minleft = ap->minleft;
2577 args.wasdel = ap->wasdel;
2578 args.isfl = 0;
2579 args.userdata = ap->userdata;
2580 if ((error = xfs_alloc_vextent(&args)))
2581 return error;
2582 if (tryagain && args.fsbno == NULLFSBLOCK) {
2583 /*
2584 * Exact allocation failed. Now try with alignment
2585 * turned on.
2586 */
2587 args.type = atype;
2588 args.fsbno = ap->rval;
2589 args.alignment = mp->m_dalign;
2590 args.minlen = nextminlen;
2591 args.minalignslop = 0;
2592 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if ((error = xfs_alloc_vextent(&args)))
2594 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002595 }
2596 if (isaligned && args.fsbno == NULLFSBLOCK) {
2597 /*
2598 * allocation failed, so turn off alignment and
2599 * try again.
2600 */
2601 args.type = atype;
2602 args.fsbno = ap->rval;
2603 args.alignment = 0;
2604 if ((error = xfs_alloc_vextent(&args)))
2605 return error;
2606 }
2607 if (args.fsbno == NULLFSBLOCK && nullfb &&
2608 args.minlen > ap->minlen) {
2609 args.minlen = ap->minlen;
2610 args.type = XFS_ALLOCTYPE_START_BNO;
2611 args.fsbno = ap->rval;
2612 if ((error = xfs_alloc_vextent(&args)))
2613 return error;
2614 }
2615 if (args.fsbno == NULLFSBLOCK && nullfb) {
2616 args.fsbno = 0;
2617 args.type = XFS_ALLOCTYPE_FIRST_AG;
2618 args.total = ap->minlen;
2619 args.minleft = 0;
2620 if ((error = xfs_alloc_vextent(&args)))
2621 return error;
2622 ap->low = 1;
2623 }
2624 if (args.fsbno != NULLFSBLOCK) {
2625 ap->firstblock = ap->rval = args.fsbno;
2626 ASSERT(nullfb || fb_agno == args.agno ||
2627 (ap->low && fb_agno < args.agno));
2628 ap->alen = args.len;
2629 ap->ip->i_d.di_nblocks += args.len;
2630 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2631 if (ap->wasdel)
2632 ap->ip->i_delayed_blks -= args.len;
2633 /*
2634 * Adjust the disk quota also. This was reserved
2635 * earlier.
2636 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002637 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002638 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2639 XFS_TRANS_DQ_BCOUNT,
2640 (long) args.len);
2641 } else {
2642 ap->rval = NULLFSBLOCK;
2643 ap->alen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
2645 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002646}
2647
2648/*
2649 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2650 * It figures out where to ask the underlying allocator to put the new extent.
2651 */
2652STATIC int
2653xfs_bmap_alloc(
2654 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2655{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002656 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002657 return xfs_bmap_rtalloc(ap);
2658 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}
2660
2661/*
2662 * Transform a btree format file with only one leaf node, where the
2663 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002664 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 * give up the space for the btree root and pitch the leaf block.
2666 */
2667STATIC int /* error */
2668xfs_bmap_btree_to_extents(
2669 xfs_trans_t *tp, /* transaction pointer */
2670 xfs_inode_t *ip, /* incore inode pointer */
2671 xfs_btree_cur_t *cur, /* btree cursor */
2672 int *logflagsp, /* inode logging flags */
2673 int whichfork) /* data or attr fork */
2674{
2675 /* REFERENCED */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002676 struct xfs_btree_block *cblock;/* child btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 xfs_fsblock_t cbno; /* child block number */
2678 xfs_buf_t *cbp; /* child block's buffer */
2679 int error; /* error return value */
2680 xfs_ifork_t *ifp; /* inode fork data */
2681 xfs_mount_t *mp; /* mount point structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10002682 __be64 *pp; /* ptr to block address */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002683 struct xfs_btree_block *rblock;/* root btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Christoph Hellwig60197e82008-10-30 17:11:19 +11002685 mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 ifp = XFS_IFORK_PTR(ip, whichfork);
2687 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2688 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2689 rblock = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11002690 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2691 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
Christoph Hellwig60197e82008-10-30 17:11:19 +11002692 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2693 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10002694 cbno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 *logflagsp = 0;
2696#ifdef DEBUG
Christoph Hellwig576039c2006-09-28 10:58:06 +10002697 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 return error;
2699#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2701 XFS_BMAP_BTREE_REF)))
2702 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002703 cblock = XFS_BUF_TO_BLOCK(cbp);
2704 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 return error;
2706 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2707 ip->i_d.di_nblocks--;
Christoph Hellwig7d095252009-06-08 15:33:32 +02002708 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 xfs_trans_binval(tp, cbp);
2710 if (cur->bc_bufs[0] == cbp)
2711 cur->bc_bufs[0] = NULL;
2712 xfs_iroot_realloc(ip, -1, whichfork);
2713 ASSERT(ifp->if_broot == NULL);
2714 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2715 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002716 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 return 0;
2718}
2719
2720/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002721 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 * after removing space (or undoing a delayed allocation).
2723 */
2724STATIC int /* error */
2725xfs_bmap_del_extent(
2726 xfs_inode_t *ip, /* incore inode pointer */
2727 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002728 xfs_extnum_t *idx, /* extent number to update/delete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 xfs_bmap_free_t *flist, /* list of extents to be freed */
2730 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002731 xfs_bmbt_irec_t *del, /* data to remove from extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 int *logflagsp, /* inode logging flags */
Christoph Hellwig54893272011-05-11 15:04:03 +00002733 int whichfork) /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
2735 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2736 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2737 xfs_fsblock_t del_endblock=0; /* first block past del */
2738 xfs_fileoff_t del_endoff; /* first offset past del */
2739 int delay; /* current block is delayed allocated */
2740 int do_fx; /* free extent at end of routine */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002741 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 int error; /* error return value */
2743 int flags; /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 xfs_bmbt_irec_t got; /* current extent entry */
2745 xfs_fileoff_t got_endoff; /* first offset past got */
2746 int i; /* temp state */
2747 xfs_ifork_t *ifp; /* inode fork pointer */
2748 xfs_mount_t *mp; /* mount structure */
2749 xfs_filblks_t nblks; /* quota/sb block count */
2750 xfs_bmbt_irec_t new; /* new record to be inserted */
2751 /* REFERENCED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 uint qfield; /* quota field to update */
2753 xfs_filblks_t temp; /* for indirect length calculations */
2754 xfs_filblks_t temp2; /* for indirect length calculations */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002755 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 XFS_STATS_INC(xs_del_exlist);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002758
2759 if (whichfork == XFS_ATTR_FORK)
2760 state |= BMAP_ATTRFORK;
2761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 mp = ip->i_mount;
2763 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002764 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002765 (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 ASSERT(del->br_blockcount > 0);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002767 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 xfs_bmbt_get_all(ep, &got);
2769 ASSERT(got.br_startoff <= del->br_startoff);
2770 del_endoff = del->br_startoff + del->br_blockcount;
2771 got_endoff = got.br_startoff + got.br_blockcount;
2772 ASSERT(got_endoff >= del_endoff);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002773 delay = isnullstartblock(got.br_startblock);
2774 ASSERT(isnullstartblock(del->br_startblock) == delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 flags = 0;
2776 qfield = 0;
2777 error = 0;
2778 /*
2779 * If deleting a real allocation, must free up the disk space.
2780 */
2781 if (!delay) {
2782 flags = XFS_ILOG_CORE;
2783 /*
2784 * Realtime allocation. Free it and record di_nblocks update.
2785 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002786 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 xfs_fsblock_t bno;
2788 xfs_filblks_t len;
2789
2790 ASSERT(do_mod(del->br_blockcount,
2791 mp->m_sb.sb_rextsize) == 0);
2792 ASSERT(do_mod(del->br_startblock,
2793 mp->m_sb.sb_rextsize) == 0);
2794 bno = del->br_startblock;
2795 len = del->br_blockcount;
2796 do_div(bno, mp->m_sb.sb_rextsize);
2797 do_div(len, mp->m_sb.sb_rextsize);
Christoph Hellwigf3ca8732011-07-08 14:34:47 +02002798 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
2799 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 goto done;
2801 do_fx = 0;
2802 nblks = len * mp->m_sb.sb_rextsize;
2803 qfield = XFS_TRANS_DQ_RTBCOUNT;
2804 }
2805 /*
2806 * Ordinary allocation.
2807 */
2808 else {
2809 do_fx = 1;
2810 nblks = del->br_blockcount;
2811 qfield = XFS_TRANS_DQ_BCOUNT;
2812 }
2813 /*
2814 * Set up del_endblock and cur for later.
2815 */
2816 del_endblock = del->br_startblock + del->br_blockcount;
2817 if (cur) {
2818 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2819 got.br_startblock, got.br_blockcount,
2820 &i)))
2821 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002822 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 }
2824 da_old = da_new = 0;
2825 } else {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002826 da_old = startblockval(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 da_new = 0;
2828 nblks = 0;
2829 do_fx = 0;
2830 }
2831 /*
2832 * Set flag value to use in switch statement.
2833 * Left-contig is 2, right-contig is 1.
2834 */
2835 switch (((got.br_startoff == del->br_startoff) << 1) |
2836 (got_endoff == del_endoff)) {
2837 case 3:
2838 /*
2839 * Matches the whole extent. Delete the entry.
2840 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002841 xfs_iext_remove(ip, *idx, 1,
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002842 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
Christoph Hellwig233eebb2011-05-11 15:04:05 +00002843 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 if (delay)
2845 break;
Christoph Hellwig233eebb2011-05-11 15:04:05 +00002846
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 XFS_IFORK_NEXT_SET(ip, whichfork,
2848 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2849 flags |= XFS_ILOG_CORE;
2850 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002851 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 break;
2853 }
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11002854 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002856 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 break;
2858
2859 case 2:
2860 /*
2861 * Deleting the first part of the extent.
2862 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002863 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 xfs_bmbt_set_startoff(ep, del_endoff);
2865 temp = got.br_blockcount - del->br_blockcount;
2866 xfs_bmbt_set_blockcount(ep, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 if (delay) {
2868 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2869 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002870 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002871 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 da_new = temp;
2873 break;
2874 }
2875 xfs_bmbt_set_startblock(ep, del_endblock);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002876 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002878 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 break;
2880 }
2881 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2882 got.br_blockcount - del->br_blockcount,
2883 got.br_state)))
2884 goto done;
2885 break;
2886
2887 case 1:
2888 /*
2889 * Deleting the last part of the extent.
2890 */
2891 temp = got.br_blockcount - del->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002892 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 xfs_bmbt_set_blockcount(ep, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (delay) {
2895 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2896 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002897 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002898 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 da_new = temp;
2900 break;
2901 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002902 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002904 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 break;
2906 }
2907 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2908 got.br_startblock,
2909 got.br_blockcount - del->br_blockcount,
2910 got.br_state)))
2911 goto done;
2912 break;
2913
2914 case 0:
2915 /*
2916 * Deleting the middle of the extent.
2917 */
2918 temp = del->br_startoff - got.br_startoff;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002919 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 xfs_bmbt_set_blockcount(ep, temp);
2921 new.br_startoff = del_endoff;
2922 temp2 = got_endoff - del_endoff;
2923 new.br_blockcount = temp2;
2924 new.br_state = got.br_state;
2925 if (!delay) {
2926 new.br_startblock = del_endblock;
2927 flags |= XFS_ILOG_CORE;
2928 if (cur) {
2929 if ((error = xfs_bmbt_update(cur,
2930 got.br_startoff,
2931 got.br_startblock, temp,
2932 got.br_state)))
2933 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002934 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 goto done;
2936 cur->bc_rec.b = new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002937 error = xfs_btree_insert(cur, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 if (error && error != ENOSPC)
2939 goto done;
2940 /*
2941 * If get no-space back from btree insert,
2942 * it tried a split, and we have a zero
2943 * block reservation.
2944 * Fix up our state and return the error.
2945 */
2946 if (error == ENOSPC) {
2947 /*
2948 * Reset the cursor, don't trust
2949 * it after any insert operation.
2950 */
2951 if ((error = xfs_bmbt_lookup_eq(cur,
2952 got.br_startoff,
2953 got.br_startblock,
2954 temp, &i)))
2955 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002956 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 /*
2958 * Update the btree record back
2959 * to the original value.
2960 */
2961 if ((error = xfs_bmbt_update(cur,
2962 got.br_startoff,
2963 got.br_startblock,
2964 got.br_blockcount,
2965 got.br_state)))
2966 goto done;
2967 /*
2968 * Reset the extent record back
2969 * to the original value.
2970 */
2971 xfs_bmbt_set_blockcount(ep,
2972 got.br_blockcount);
2973 flags = 0;
2974 error = XFS_ERROR(ENOSPC);
2975 goto done;
2976 }
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002977 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 } else
Eric Sandeen9d87c312009-01-14 23:22:07 -06002979 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 XFS_IFORK_NEXT_SET(ip, whichfork,
2981 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2982 } else {
2983 ASSERT(whichfork == XFS_DATA_FORK);
2984 temp = xfs_bmap_worst_indlen(ip, temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002985 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002987 new.br_startblock = nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 da_new = temp + temp2;
2989 while (da_new > da_old) {
2990 if (temp) {
2991 temp--;
2992 da_new--;
2993 xfs_bmbt_set_startblock(ep,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002994 nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 }
2996 if (da_new == da_old)
2997 break;
2998 if (temp2) {
2999 temp2--;
3000 da_new--;
3001 new.br_startblock =
Eric Sandeen9d87c312009-01-14 23:22:07 -06003002 nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 }
3004 }
3005 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00003006 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
3007 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
3008 ++*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 break;
3010 }
3011 /*
3012 * If we need to, add to list of extents to delete.
3013 */
3014 if (do_fx)
3015 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3016 mp);
3017 /*
3018 * Adjust inode # blocks in the file.
3019 */
3020 if (nblks)
3021 ip->i_d.di_nblocks -= nblks;
3022 /*
3023 * Adjust quota data.
3024 */
3025 if (qfield)
Christoph Hellwig7d095252009-06-08 15:33:32 +02003026 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
3028 /*
3029 * Account for change in delayed indirect blocks.
3030 * Nothing to do for disk quota accounting here.
3031 */
3032 ASSERT(da_old >= da_new);
Christoph Hellwig96540c72010-09-30 02:25:55 +00003033 if (da_old > da_new) {
3034 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00003035 (int64_t)(da_old - da_new), 0);
Christoph Hellwig96540c72010-09-30 02:25:55 +00003036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037done:
3038 *logflagsp = flags;
3039 return error;
3040}
3041
3042/*
3043 * Remove the entry "free" from the free item list. Prev points to the
3044 * previous entry, unless "free" is the head of the list.
3045 */
3046STATIC void
3047xfs_bmap_del_free(
3048 xfs_bmap_free_t *flist, /* free item list header */
3049 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3050 xfs_bmap_free_item_t *free) /* list item to be freed */
3051{
3052 if (prev)
3053 prev->xbfi_next = free->xbfi_next;
3054 else
3055 flist->xbf_first = free->xbfi_next;
3056 flist->xbf_count--;
3057 kmem_zone_free(xfs_bmap_free_item_zone, free);
3058}
3059
3060/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 * Convert an extents-format file into a btree-format file.
3062 * The new file will have a root block (in the inode) and a single child block.
3063 */
3064STATIC int /* error */
3065xfs_bmap_extents_to_btree(
3066 xfs_trans_t *tp, /* transaction pointer */
3067 xfs_inode_t *ip, /* incore inode pointer */
3068 xfs_fsblock_t *firstblock, /* first-block-allocated */
3069 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3070 xfs_btree_cur_t **curp, /* cursor returned to caller */
3071 int wasdel, /* converting a delayed alloc */
3072 int *logflagsp, /* inode logging flags */
3073 int whichfork) /* data or attr fork */
3074{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003075 struct xfs_btree_block *ablock; /* allocated (child) bt block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 xfs_buf_t *abp; /* buffer for ablock */
3077 xfs_alloc_arg_t args; /* allocation arguments */
3078 xfs_bmbt_rec_t *arp; /* child record pointer */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003079 struct xfs_btree_block *block; /* btree root block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 xfs_btree_cur_t *cur; /* bmap btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003081 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003083 xfs_extnum_t i, cnt; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 xfs_ifork_t *ifp; /* inode fork pointer */
3085 xfs_bmbt_key_t *kp; /* root block key pointer */
3086 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003087 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3089
3090 ifp = XFS_IFORK_PTR(ip, whichfork);
3091 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3092 ASSERT(ifp->if_ext_max ==
3093 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3094 /*
3095 * Make space in the inode incore.
3096 */
3097 xfs_iroot_realloc(ip, 1, whichfork);
3098 ifp->if_flags |= XFS_IFBROOT;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003099
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 /*
3101 * Fill in the root.
3102 */
3103 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003104 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3105 block->bb_level = cpu_to_be16(1);
3106 block->bb_numrecs = cpu_to_be16(1);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003107 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3108 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 /*
3111 * Need a cursor. Can't allocate until bb_level is filled in.
3112 */
3113 mp = ip->i_mount;
Christoph Hellwig561f7d12008-10-30 16:53:59 +11003114 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 cur->bc_private.b.firstblock = *firstblock;
3116 cur->bc_private.b.flist = flist;
3117 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3118 /*
3119 * Convert to a btree with two levels, one record in root.
3120 */
3121 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3122 args.tp = tp;
3123 args.mp = mp;
Yingping Lud210a282006-06-09 14:55:18 +10003124 args.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 if (*firstblock == NULLFSBLOCK) {
3126 args.type = XFS_ALLOCTYPE_START_BNO;
3127 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3128 } else if (flist->xbf_low) {
3129 args.type = XFS_ALLOCTYPE_START_BNO;
3130 args.fsbno = *firstblock;
3131 } else {
3132 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3133 args.fsbno = *firstblock;
3134 }
3135 args.minlen = args.maxlen = args.prod = 1;
3136 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3137 args.minalignslop = 0;
3138 args.wasdel = wasdel;
3139 *logflagsp = 0;
3140 if ((error = xfs_alloc_vextent(&args))) {
3141 xfs_iroot_realloc(ip, -1, whichfork);
3142 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3143 return error;
3144 }
3145 /*
3146 * Allocation can't fail, the space was reserved.
3147 */
3148 ASSERT(args.fsbno != NULLFSBLOCK);
3149 ASSERT(*firstblock == NULLFSBLOCK ||
3150 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3151 (flist->xbf_low &&
3152 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3153 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3154 cur->bc_private.b.allocated++;
3155 ip->i_d.di_nblocks++;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003156 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3158 /*
3159 * Fill in the child block.
3160 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003161 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003162 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 ablock->bb_level = 0;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003164 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3165 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003166 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003168 for (cnt = i = 0; i < nextents; i++) {
3169 ep = xfs_iext_get_ext(ifp, i);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003170 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10003171 arp->l0 = cpu_to_be64(ep->l0);
3172 arp->l1 = cpu_to_be64(ep->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 arp++; cnt++;
3174 }
3175 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11003176 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003177 xfs_btree_set_numrecs(ablock, cnt);
3178
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 /*
3180 * Fill in the root key and pointer.
3181 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11003182 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3183 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Christoph Hellwig8801bb92006-09-28 10:58:17 +10003184 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
Christoph Hellwig136341b2008-10-30 17:11:40 +11003185 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3186 be16_to_cpu(block->bb_level)));
Christoph Hellwig576039c2006-09-28 10:58:06 +10003187 *pp = cpu_to_be64(args.fsbno);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 /*
3190 * Do all this logging at the end so that
3191 * the root is at the right level.
3192 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11003193 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3194 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 ASSERT(*curp == NULL);
3196 *curp = cur;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003197 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 return 0;
3199}
3200
3201/*
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003202 * Calculate the default attribute fork offset for newly created inodes.
3203 */
3204uint
3205xfs_default_attroffset(
3206 struct xfs_inode *ip)
3207{
3208 struct xfs_mount *mp = ip->i_mount;
3209 uint offset;
3210
3211 if (mp->m_sb.sb_inodesize == 256) {
3212 offset = XFS_LITINO(mp) -
3213 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3214 } else {
3215 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3216 }
3217
3218 ASSERT(offset < XFS_LITINO(mp));
3219 return offset;
3220}
3221
3222/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11003223 * Helper routine to reset inode di_forkoff field when switching
3224 * attribute fork from local to extent format - we reset it where
3225 * possible to make space available for inline data fork extents.
3226 */
3227STATIC void
3228xfs_bmap_forkoff_reset(
3229 xfs_mount_t *mp,
3230 xfs_inode_t *ip,
3231 int whichfork)
3232{
3233 if (whichfork == XFS_ATTR_FORK &&
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003234 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3235 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3236 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3237 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3238
3239 if (dfl_forkoff > ip->i_d.di_forkoff) {
3240 ip->i_d.di_forkoff = dfl_forkoff;
3241 ip->i_df.if_ext_max =
3242 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3243 ip->i_afp->if_ext_max =
3244 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3245 }
Nathan Scottd8cc8902005-11-02 10:34:53 +11003246 }
3247}
3248
3249/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 * Convert a local file to an extents file.
3251 * This code is out of bounds for data forks of regular files,
3252 * since the file data needs to get logged so things will stay consistent.
3253 * (The bmap-level manipulations are ok, though).
3254 */
3255STATIC int /* error */
3256xfs_bmap_local_to_extents(
3257 xfs_trans_t *tp, /* transaction pointer */
3258 xfs_inode_t *ip, /* incore inode pointer */
3259 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3260 xfs_extlen_t total, /* total blocks needed by transaction */
3261 int *logflagsp, /* inode logging flags */
3262 int whichfork) /* data or attr fork */
3263{
3264 int error; /* error return value */
3265 int flags; /* logging flags returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 xfs_ifork_t *ifp; /* inode fork pointer */
3267
3268 /*
3269 * We don't want to deal with the case of keeping inode data inline yet.
3270 * So sending the data fork of a regular inode is invalid.
3271 */
Al Viroabbede12011-07-26 02:31:30 -04003272 ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 ifp = XFS_IFORK_PTR(ip, whichfork);
3274 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3275 flags = 0;
3276 error = 0;
3277 if (ifp->if_bytes) {
3278 xfs_alloc_arg_t args; /* allocation arguments */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003279 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003280 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
3282 args.tp = tp;
3283 args.mp = ip->i_mount;
Yingping Lud210a282006-06-09 14:55:18 +10003284 args.firstblock = *firstblock;
Mandy Kirkconnellf020b672006-03-14 14:07:24 +11003285 ASSERT((ifp->if_flags &
3286 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 /*
3288 * Allocate a block. We know we need only one, since the
3289 * file currently fits in an inode.
3290 */
3291 if (*firstblock == NULLFSBLOCK) {
3292 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3293 args.type = XFS_ALLOCTYPE_START_BNO;
3294 } else {
3295 args.fsbno = *firstblock;
3296 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3297 }
3298 args.total = total;
3299 args.mod = args.minleft = args.alignment = args.wasdel =
3300 args.isfl = args.minalignslop = 0;
3301 args.minlen = args.maxlen = args.prod = 1;
3302 if ((error = xfs_alloc_vextent(&args)))
3303 goto done;
3304 /*
3305 * Can't fail, the space was reserved.
3306 */
3307 ASSERT(args.fsbno != NULLFSBLOCK);
3308 ASSERT(args.len == 1);
3309 *firstblock = args.fsbno;
3310 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
Chandra Seetharaman62926042011-07-22 23:40:15 +00003311 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003313 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003315 xfs_iext_add(ifp, 0, 1);
3316 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003318 trace_xfs_bmap_post_update(ip, 0,
3319 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3320 _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3322 ip->i_d.di_nblocks = 1;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003323 xfs_trans_mod_dquot_byino(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 XFS_TRANS_DQ_BCOUNT, 1L);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003325 flags |= xfs_ilog_fext(whichfork);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003326 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003328 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 ifp->if_flags &= ~XFS_IFINLINE;
3331 ifp->if_flags |= XFS_IFEXTENTS;
3332 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3333 flags |= XFS_ILOG_CORE;
3334done:
3335 *logflagsp = flags;
3336 return error;
3337}
3338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003340 * Search the extent records for the entry containing block bno.
3341 * If bno lies in a hole, point to the next entry. If bno lies
3342 * past eof, *eofp will be set, and *prevp will contain the last
3343 * entry (null if none). Else, *lastxp will be set to the index
3344 * of the found entry; *gotp will contain the entry.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003345 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003346STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003347xfs_bmap_search_multi_extents(
3348 xfs_ifork_t *ifp, /* inode fork pointer */
3349 xfs_fileoff_t bno, /* block number searched for */
3350 int *eofp, /* out: end of file found */
3351 xfs_extnum_t *lastxp, /* out: last extent index */
3352 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3353 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3354{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003355 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003356 xfs_extnum_t lastx; /* last extent index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003357
3358 /*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003359 * Initialize the extent entry structure to catch access to
3360 * uninitialized br_startblock field.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003361 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003362 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3363 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3364 gotp->br_state = XFS_EXT_INVALID;
3365#if XFS_BIG_BLKNOS
3366 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3367#else
3368 gotp->br_startblock = 0xffffa5a5;
3369#endif
3370 prevp->br_startoff = NULLFILEOFF;
3371
3372 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3373 if (lastx > 0) {
3374 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3375 }
3376 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3377 xfs_bmbt_get_all(ep, gotp);
3378 *eofp = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003379 } else {
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003380 if (lastx > 0) {
3381 *gotp = *prevp;
3382 }
3383 *eofp = 1;
3384 ep = NULL;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003385 }
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003386 *lastxp = lastx;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003387 return ep;
3388}
3389
3390/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 * Search the extents list for the inode, for the extent containing bno.
3392 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3393 * *eofp will be set, and *prevp will contain the last entry (null if none).
3394 * Else, *lastxp will be set to the index of the found
3395 * entry; *gotp will contain the entry.
3396 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003397STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398xfs_bmap_search_extents(
3399 xfs_inode_t *ip, /* incore inode pointer */
3400 xfs_fileoff_t bno, /* block number searched for */
Nathan Scott572d95f2006-09-28 11:03:20 +10003401 int fork, /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 int *eofp, /* out: end of file found */
3403 xfs_extnum_t *lastxp, /* out: last extent index */
3404 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3405 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3406{
3407 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003408 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
3410 XFS_STATS_INC(xs_look_exlist);
Nathan Scott572d95f2006-09-28 11:03:20 +10003411 ifp = XFS_IFORK_PTR(ip, fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003413 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3414
Nathan Scott572d95f2006-09-28 11:03:20 +10003415 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3416 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003417 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
Nathan Scott572d95f2006-09-28 11:03:20 +10003418 "Access to block zero in inode %llu "
3419 "start_block: %llx start_off: %llx "
3420 "blkcnt: %llx extent-state: %x lastx: %x\n",
3421 (unsigned long long)ip->i_ino,
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003422 (unsigned long long)gotp->br_startblock,
3423 (unsigned long long)gotp->br_startoff,
3424 (unsigned long long)gotp->br_blockcount,
Nathan Scott572d95f2006-09-28 11:03:20 +10003425 gotp->br_state, *lastxp);
3426 *lastxp = NULLEXTNUM;
3427 *eofp = 1;
3428 return NULL;
3429 }
3430 return ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431}
3432
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433/*
3434 * Compute the worst-case number of indirect blocks that will be used
3435 * for ip's delayed extent of length "len".
3436 */
3437STATIC xfs_filblks_t
3438xfs_bmap_worst_indlen(
3439 xfs_inode_t *ip, /* incore inode pointer */
3440 xfs_filblks_t len) /* delayed extent length */
3441{
3442 int level; /* btree level number */
3443 int maxrecs; /* maximum record count at this level */
3444 xfs_mount_t *mp; /* mount structure */
3445 xfs_filblks_t rval; /* return value */
3446
3447 mp = ip->i_mount;
3448 maxrecs = mp->m_bmap_dmxr[0];
3449 for (level = 0, rval = 0;
3450 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3451 level++) {
3452 len += maxrecs - 1;
3453 do_div(len, maxrecs);
3454 rval += len;
3455 if (len == 1)
3456 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3457 level - 1;
3458 if (level == 0)
3459 maxrecs = mp->m_bmap_dmxr[1];
3460 }
3461 return rval;
3462}
3463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464/*
3465 * Convert inode from non-attributed to attributed.
3466 * Must not be in a transaction, ip must not be locked.
3467 */
3468int /* error code */
3469xfs_bmap_add_attrfork(
3470 xfs_inode_t *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003471 int size, /* space new attribute needs */
3472 int rsvd) /* xact may use reserved blks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003475 xfs_bmap_free_t flist; /* freed extent records */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 xfs_trans_t *tp; /* transaction pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003478 int blks; /* space reservation */
3479 int version = 1; /* superblock attr version */
3480 int committed; /* xaction was committed */
3481 int logflags; /* logging flags */
3482 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Nathan Scottd8cc8902005-11-02 10:34:53 +11003484 ASSERT(XFS_IFORK_Q(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 ASSERT(ip->i_df.if_ext_max ==
3486 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
Nathan Scottd8cc8902005-11-02 10:34:53 +11003487
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 mp = ip->i_mount;
3489 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3490 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3491 blks = XFS_ADDAFORK_SPACE_RES(mp);
3492 if (rsvd)
3493 tp->t_flags |= XFS_TRANS_RESERVE;
3494 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3495 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3496 goto error0;
3497 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02003498 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3500 XFS_QMOPT_RES_REGBLKS);
3501 if (error) {
3502 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3503 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3504 return error;
3505 }
3506 if (XFS_IFORK_Q(ip))
3507 goto error1;
3508 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3509 /*
3510 * For inodes coming from pre-6.2 filesystems.
3511 */
3512 ASSERT(ip->i_d.di_aformat == 0);
3513 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3514 }
3515 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwig898621d2010-06-24 11:36:58 +10003516
3517 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig898621d2010-06-24 11:36:58 +10003519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 switch (ip->i_d.di_format) {
3521 case XFS_DINODE_FMT_DEV:
3522 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3523 break;
3524 case XFS_DINODE_FMT_UUID:
3525 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3526 break;
3527 case XFS_DINODE_FMT_LOCAL:
3528 case XFS_DINODE_FMT_EXTENTS:
3529 case XFS_DINODE_FMT_BTREE:
Nathan Scottd8cc8902005-11-02 10:34:53 +11003530 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3531 if (!ip->i_d.di_forkoff)
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003532 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
Nathan Scott13059ff2006-01-11 15:32:01 +11003533 else if (mp->m_flags & XFS_MOUNT_ATTR2)
Nathan Scottd8cc8902005-11-02 10:34:53 +11003534 version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 break;
3536 default:
3537 ASSERT(0);
3538 error = XFS_ERROR(EINVAL);
3539 goto error1;
3540 }
3541 ip->i_df.if_ext_max =
3542 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3543 ASSERT(ip->i_afp == NULL);
3544 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3545 ip->i_afp->if_ext_max =
3546 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3547 ip->i_afp->if_flags = XFS_IFEXTENTS;
3548 logflags = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003549 xfs_bmap_init(&flist, &firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 switch (ip->i_d.di_format) {
3551 case XFS_DINODE_FMT_LOCAL:
3552 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3553 &logflags);
3554 break;
3555 case XFS_DINODE_FMT_EXTENTS:
3556 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3557 &flist, &logflags);
3558 break;
3559 case XFS_DINODE_FMT_BTREE:
3560 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3561 &logflags);
3562 break;
3563 default:
3564 error = 0;
3565 break;
3566 }
3567 if (logflags)
3568 xfs_trans_log_inode(tp, ip, logflags);
3569 if (error)
3570 goto error2;
Eric Sandeen62118702008-03-06 13:44:28 +11003571 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3572 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +11003573 __int64_t sbfields = 0;
3574
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003575 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +11003576 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3577 xfs_sb_version_addattr(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003578 sbfields |= XFS_SB_VERSIONNUM;
Nathan Scottd8cc8902005-11-02 10:34:53 +11003579 }
Eric Sandeen62118702008-03-06 13:44:28 +11003580 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3581 xfs_sb_version_addattr2(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003582 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003583 }
Nathan Scottda087ba2005-11-02 15:00:20 +11003584 if (sbfields) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003585 spin_unlock(&mp->m_sb_lock);
Nathan Scottda087ba2005-11-02 15:00:20 +11003586 xfs_mod_sb(tp, sbfields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003588 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 }
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003590 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 goto error2;
Dave Chinner713bf882010-03-08 11:26:23 +11003592 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 ASSERT(ip->i_df.if_ext_max ==
3594 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3595 return error;
3596error2:
3597 xfs_bmap_cancel(&flist);
3598error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3600error0:
3601 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3602 ASSERT(ip->i_df.if_ext_max ==
3603 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3604 return error;
3605}
3606
3607/*
3608 * Add the extent to the list of extents to be free at transaction end.
3609 * The list is maintained sorted (by block number).
3610 */
3611/* ARGSUSED */
3612void
3613xfs_bmap_add_free(
3614 xfs_fsblock_t bno, /* fs block number of extent */
3615 xfs_filblks_t len, /* length of extent */
3616 xfs_bmap_free_t *flist, /* list of extents */
3617 xfs_mount_t *mp) /* mount point structure */
3618{
3619 xfs_bmap_free_item_t *cur; /* current (next) element */
3620 xfs_bmap_free_item_t *new; /* new element */
3621 xfs_bmap_free_item_t *prev; /* previous element */
3622#ifdef DEBUG
3623 xfs_agnumber_t agno;
3624 xfs_agblock_t agbno;
3625
3626 ASSERT(bno != NULLFSBLOCK);
3627 ASSERT(len > 0);
3628 ASSERT(len <= MAXEXTLEN);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003629 ASSERT(!isnullstartblock(bno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 agno = XFS_FSB_TO_AGNO(mp, bno);
3631 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3632 ASSERT(agno < mp->m_sb.sb_agcount);
3633 ASSERT(agbno < mp->m_sb.sb_agblocks);
3634 ASSERT(len < mp->m_sb.sb_agblocks);
3635 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3636#endif
3637 ASSERT(xfs_bmap_free_item_zone != NULL);
3638 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3639 new->xbfi_startblock = bno;
3640 new->xbfi_blockcount = (xfs_extlen_t)len;
3641 for (prev = NULL, cur = flist->xbf_first;
3642 cur != NULL;
3643 prev = cur, cur = cur->xbfi_next) {
3644 if (cur->xbfi_startblock >= bno)
3645 break;
3646 }
3647 if (prev)
3648 prev->xbfi_next = new;
3649 else
3650 flist->xbf_first = new;
3651 new->xbfi_next = cur;
3652 flist->xbf_count++;
3653}
3654
3655/*
3656 * Compute and fill in the value of the maximum depth of a bmap btree
3657 * in this filesystem. Done once, during mount.
3658 */
3659void
3660xfs_bmap_compute_maxlevels(
3661 xfs_mount_t *mp, /* file system mount structure */
3662 int whichfork) /* data or attr fork */
3663{
3664 int level; /* btree level */
3665 uint maxblocks; /* max blocks at this level */
3666 uint maxleafents; /* max leaf entries possible */
3667 int maxrootrecs; /* max records in root block */
3668 int minleafrecs; /* min records in leaf block */
3669 int minnoderecs; /* min records in node block */
3670 int sz; /* root block size */
3671
3672 /*
3673 * The maximum number of extents in a file, hence the maximum
3674 * number of leaf entries, is controlled by the type of di_nextents
3675 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3676 * (a signed 16-bit number, xfs_aextnum_t).
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003677 *
3678 * Note that we can no longer assume that if we are in ATTR1 that
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003679 * the fork offset of all the inodes will be
3680 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3681 * with ATTR2 and then mounted back with ATTR1, keeping the
3682 * di_forkoff's fixed but probably at various positions. Therefore,
3683 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3684 * of a minimum size available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003686 if (whichfork == XFS_DATA_FORK) {
3687 maxleafents = MAXEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003688 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003689 } else {
3690 maxleafents = MAXAEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003691 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003692 }
Christoph Hellwig60197e82008-10-30 17:11:19 +11003693 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 minleafrecs = mp->m_bmap_dmnr[0];
3695 minnoderecs = mp->m_bmap_dmnr[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3697 for (level = 1; maxblocks > 1; level++) {
3698 if (maxblocks <= maxrootrecs)
3699 maxblocks = 1;
3700 else
3701 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3702 }
3703 mp->m_bm_maxlevels[whichfork] = level;
3704}
3705
3706/*
3707 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3708 * caller. Frees all the extents that need freeing, which must be done
3709 * last due to locking considerations. We never free any extents in
Christoph Hellwig859f57c2011-08-27 14:45:11 +00003710 * the first transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 *
3712 * Return 1 if the given transaction was committed and a new one
3713 * started, and 0 otherwise in the committed parameter.
3714 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715int /* error */
3716xfs_bmap_finish(
3717 xfs_trans_t **tp, /* transaction pointer addr */
3718 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 int *committed) /* xact committed or not */
3720{
3721 xfs_efd_log_item_t *efd; /* extent free data */
3722 xfs_efi_log_item_t *efi; /* extent free intention */
3723 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003724 xfs_bmap_free_item_t *free; /* free extent item */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 unsigned int logres; /* new log reservation */
3726 unsigned int logcount; /* new log count */
3727 xfs_mount_t *mp; /* filesystem mount structure */
3728 xfs_bmap_free_item_t *next; /* next item on free list */
3729 xfs_trans_t *ntp; /* new transaction pointer */
3730
3731 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3732 if (flist->xbf_count == 0) {
3733 *committed = 0;
3734 return 0;
3735 }
3736 ntp = *tp;
3737 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3738 for (free = flist->xbf_first; free; free = free->xbfi_next)
3739 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3740 free->xbfi_blockcount);
3741 logres = ntp->t_log_res;
3742 logcount = ntp->t_log_count;
3743 ntp = xfs_trans_dup(*tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003744 error = xfs_trans_commit(*tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 *tp = ntp;
3746 *committed = 1;
3747 /*
3748 * We have a new transaction, so we should return committed=1,
3749 * even though we're returning an error.
3750 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003751 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 return error;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003753
3754 /*
3755 * transaction commit worked ok so we can drop the extra ticket
3756 * reference that we gained in xfs_trans_dup()
3757 */
3758 xfs_log_ticket_put(ntp->t_ticket);
3759
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3761 logcount)))
3762 return error;
3763 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3764 for (free = flist->xbf_first; free != NULL; free = next) {
3765 next = free->xbfi_next;
3766 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3767 free->xbfi_blockcount))) {
3768 /*
3769 * The bmap free list will be cleaned up at a
3770 * higher level. The EFI will be canceled when
3771 * this transaction is aborted.
3772 * Need to force shutdown here to make sure it
3773 * happens, since this transaction may not be
3774 * dirty yet.
3775 */
3776 mp = ntp->t_mountp;
3777 if (!XFS_FORCED_SHUTDOWN(mp))
3778 xfs_force_shutdown(mp,
3779 (error == EFSCORRUPTED) ?
Nathan Scott7d04a332006-06-09 14:58:38 +10003780 SHUTDOWN_CORRUPT_INCORE :
3781 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 return error;
3783 }
3784 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3785 free->xbfi_blockcount);
3786 xfs_bmap_del_free(flist, NULL, free);
3787 }
3788 return 0;
3789}
3790
3791/*
3792 * Free up any items left in the list.
3793 */
3794void
3795xfs_bmap_cancel(
3796 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3797{
3798 xfs_bmap_free_item_t *free; /* free list item */
3799 xfs_bmap_free_item_t *next;
3800
3801 if (flist->xbf_count == 0)
3802 return;
3803 ASSERT(flist->xbf_first != NULL);
3804 for (free = flist->xbf_first; free; free = next) {
3805 next = free->xbfi_next;
3806 xfs_bmap_del_free(flist, NULL, free);
3807 }
3808 ASSERT(flist->xbf_count == 0);
3809}
3810
3811/*
3812 * Returns the file-relative block number of the first unused block(s)
3813 * in the file with at least "len" logically contiguous blocks free.
3814 * This is the lowest-address hole if the file has holes, else the first block
3815 * past the end of file.
3816 * Return 0 if the file is currently local (in-inode).
3817 */
3818int /* error */
3819xfs_bmap_first_unused(
3820 xfs_trans_t *tp, /* transaction pointer */
3821 xfs_inode_t *ip, /* incore inode */
3822 xfs_extlen_t len, /* size of hole to find */
3823 xfs_fileoff_t *first_unused, /* unused block */
3824 int whichfork) /* data or attr fork */
3825{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003827 int idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 xfs_ifork_t *ifp; /* inode fork pointer */
3829 xfs_fileoff_t lastaddr; /* last block number seen */
3830 xfs_fileoff_t lowest; /* lowest useful block */
3831 xfs_fileoff_t max; /* starting useful block */
3832 xfs_fileoff_t off; /* offset for this block */
3833 xfs_extnum_t nextents; /* number of extent entries */
3834
3835 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3836 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3837 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3838 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3839 *first_unused = 0;
3840 return 0;
3841 }
3842 ifp = XFS_IFORK_PTR(ip, whichfork);
3843 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3844 (error = xfs_iread_extents(tp, ip, whichfork)))
3845 return error;
3846 lowest = *first_unused;
3847 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003848 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003849 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 off = xfs_bmbt_get_startoff(ep);
3851 /*
3852 * See if the hole before this extent will work.
3853 */
3854 if (off >= lowest + len && off - max >= len) {
3855 *first_unused = max;
3856 return 0;
3857 }
3858 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3859 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3860 }
3861 *first_unused = max;
3862 return 0;
3863}
3864
3865/*
3866 * Returns the file-relative block number of the last block + 1 before
3867 * last_block (input value) in the file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003868 * This is not based on i_size, it is based on the extent records.
3869 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 */
3871int /* error */
3872xfs_bmap_last_before(
3873 xfs_trans_t *tp, /* transaction pointer */
3874 xfs_inode_t *ip, /* incore inode */
3875 xfs_fileoff_t *last_block, /* last block */
3876 int whichfork) /* data or attr fork */
3877{
3878 xfs_fileoff_t bno; /* input file offset */
3879 int eof; /* hit end of file */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003880 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 int error; /* error return value */
3882 xfs_bmbt_irec_t got; /* current extent value */
3883 xfs_ifork_t *ifp; /* inode fork pointer */
3884 xfs_extnum_t lastx; /* last extent used */
3885 xfs_bmbt_irec_t prev; /* previous extent value */
3886
3887 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3888 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3889 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3890 return XFS_ERROR(EIO);
3891 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3892 *last_block = 0;
3893 return 0;
3894 }
3895 ifp = XFS_IFORK_PTR(ip, whichfork);
3896 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3897 (error = xfs_iread_extents(tp, ip, whichfork)))
3898 return error;
3899 bno = *last_block - 1;
3900 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3901 &prev);
3902 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3903 if (prev.br_startoff == NULLFILEOFF)
3904 *last_block = 0;
3905 else
3906 *last_block = prev.br_startoff + prev.br_blockcount;
3907 }
3908 /*
3909 * Otherwise *last_block is already the right answer.
3910 */
3911 return 0;
3912}
3913
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00003914STATIC int
3915xfs_bmap_last_extent(
3916 struct xfs_trans *tp,
3917 struct xfs_inode *ip,
3918 int whichfork,
3919 struct xfs_bmbt_irec *rec,
3920 int *is_empty)
3921{
3922 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3923 int error;
3924 int nextents;
3925
3926 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3927 error = xfs_iread_extents(tp, ip, whichfork);
3928 if (error)
3929 return error;
3930 }
3931
3932 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
3933 if (nextents == 0) {
3934 *is_empty = 1;
3935 return 0;
3936 }
3937
3938 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
3939 *is_empty = 0;
3940 return 0;
3941}
3942
3943/*
3944 * Check the last inode extent to determine whether this allocation will result
3945 * in blocks being allocated at the end of the file. When we allocate new data
3946 * blocks at the end of the file which do not start at the previous data block,
3947 * we will try to align the new blocks at stripe unit boundaries.
3948 *
3949 * Returns 0 in *aeof if the file (fork) is empty as any new write will be at,
3950 * or past the EOF.
3951 */
3952STATIC int
3953xfs_bmap_isaeof(
3954 struct xfs_inode *ip,
3955 xfs_fileoff_t off,
3956 int whichfork,
3957 char *aeof)
3958{
3959 struct xfs_bmbt_irec rec;
3960 int is_empty;
3961 int error;
3962
3963 *aeof = 0;
3964 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
3965 if (error || is_empty)
3966 return error;
3967
3968 /*
3969 * Check if we are allocation or past the last extent, or at least into
3970 * the last delayed allocated extent.
3971 */
3972 *aeof = off >= rec.br_startoff + rec.br_blockcount ||
3973 (off >= rec.br_startoff && isnullstartblock(rec.br_startblock));
3974 return 0;
3975}
3976
3977/*
3978 * Check if the endoff is outside the last extent. If so the caller will grow
3979 * the allocation to a stripe unit boundary. All offsets are considered outside
3980 * the end of file for an empty fork, so 1 is returned in *eof in that case.
3981 */
3982int
3983xfs_bmap_eof(
3984 struct xfs_inode *ip,
3985 xfs_fileoff_t endoff,
3986 int whichfork,
3987 int *eof)
3988{
3989 struct xfs_bmbt_irec rec;
3990 int error;
3991
3992 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
3993 if (error || *eof)
3994 return error;
3995
3996 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
3997 return 0;
3998}
3999
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000/*
4001 * Returns the file-relative block number of the first block past eof in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004002 * the file. This is not based on i_size, it is based on the extent records.
4003 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 */
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00004005int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006xfs_bmap_last_offset(
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00004007 struct xfs_trans *tp,
4008 struct xfs_inode *ip,
4009 xfs_fileoff_t *last_block,
4010 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011{
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00004012 struct xfs_bmbt_irec rec;
4013 int is_empty;
4014 int error;
4015
4016 *last_block = 0;
4017
4018 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
4019 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
4021 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00004022 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 return XFS_ERROR(EIO);
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00004024
4025 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
4026 if (error || is_empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 return error;
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00004028
4029 *last_block = rec.br_startoff + rec.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 return 0;
4031}
4032
4033/*
4034 * Returns whether the selected fork of the inode has exactly one
4035 * block or not. For the data fork we check this matches di_size,
4036 * implying the file's range is 0..bsize-1.
4037 */
4038int /* 1=>1 block, 0=>otherwise */
4039xfs_bmap_one_block(
4040 xfs_inode_t *ip, /* incore inode */
4041 int whichfork) /* data or attr fork */
4042{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004043 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 xfs_ifork_t *ifp; /* inode fork pointer */
4045 int rval; /* return value */
4046 xfs_bmbt_irec_t s; /* internal version of extent */
4047
4048#ifndef DEBUG
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004049 if (whichfork == XFS_DATA_FORK) {
Al Viroabbede12011-07-26 02:31:30 -04004050 return S_ISREG(ip->i_d.di_mode) ?
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004051 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4052 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054#endif /* !DEBUG */
4055 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4056 return 0;
4057 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4058 return 0;
4059 ifp = XFS_IFORK_PTR(ip, whichfork);
4060 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004061 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 xfs_bmbt_get_all(ep, &s);
4063 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4064 if (rval && whichfork == XFS_DATA_FORK)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004065 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 return rval;
4067}
4068
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004069STATIC int
4070xfs_bmap_sanity_check(
4071 struct xfs_mount *mp,
4072 struct xfs_buf *bp,
4073 int level)
4074{
4075 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4076
Christoph Hellwig69ef9212011-07-08 14:36:05 +02004077 if (block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC) ||
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004078 be16_to_cpu(block->bb_level) != level ||
4079 be16_to_cpu(block->bb_numrecs) == 0 ||
4080 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4081 return 0;
4082 return 1;
4083}
4084
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085/*
4086 * Read in the extents to if_extents.
4087 * All inode fields are set up by caller, we just traverse the btree
4088 * and copy the records in. If the file system cannot contain unwritten
4089 * extents, the records are checked for no "state" flags.
4090 */
4091int /* error */
4092xfs_bmap_read_extents(
4093 xfs_trans_t *tp, /* transaction pointer */
4094 xfs_inode_t *ip, /* incore inode */
4095 int whichfork) /* data or attr fork */
4096{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004097 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 xfs_fsblock_t bno; /* block # of "block" */
4099 xfs_buf_t *bp; /* buffer for "block" */
4100 int error; /* error return value */
4101 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 xfs_extnum_t i, j; /* index into the extents list */
4103 xfs_ifork_t *ifp; /* fork structure */
4104 int level; /* btree level, for checking */
4105 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10004106 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 /* REFERENCED */
4108 xfs_extnum_t room; /* number of entries there's room for */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
4110 bno = NULLFSBLOCK;
4111 mp = ip->i_mount;
4112 ifp = XFS_IFORK_PTR(ip, whichfork);
4113 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4114 XFS_EXTFMT_INODE(ip);
4115 block = ifp->if_broot;
4116 /*
4117 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4118 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004119 level = be16_to_cpu(block->bb_level);
4120 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11004121 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004122 bno = be64_to_cpu(*pp);
4123 ASSERT(bno != NULLDFSBNO);
4124 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4125 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 /*
4127 * Go down the tree until leaf level is reached, following the first
4128 * pointer (leftmost) at each level.
4129 */
4130 while (level-- > 0) {
4131 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4132 XFS_BMAP_BTREE_REF)))
4133 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004134 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004136 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 error0);
4138 if (level == 0)
4139 break;
Christoph Hellwig136341b2008-10-30 17:11:40 +11004140 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004141 bno = be64_to_cpu(*pp);
4142 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 xfs_trans_brelse(tp, bp);
4144 }
4145 /*
4146 * Here with bp and block set to the leftmost leaf node in the tree.
4147 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004148 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 i = 0;
4150 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004151 * Loop over all leaf nodes. Copy information to the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 */
4153 for (;;) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004154 xfs_bmbt_rec_t *frp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 xfs_fsblock_t nextbno;
4156 xfs_extnum_t num_recs;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004157 xfs_extnum_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004159 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 if (unlikely(i + num_recs > room)) {
4161 ASSERT(i + num_recs <= room);
Dave Chinner65333b42011-03-07 10:03:35 +11004162 xfs_warn(ip->i_mount,
Nathan Scott3762ec62006-01-12 10:29:53 +11004163 "corrupt dinode %Lu, (btree extents).",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 (unsigned long long) ip->i_ino);
Dave Chinner65333b42011-03-07 10:03:35 +11004165 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
4166 XFS_ERRLEVEL_LOW, ip->i_mount, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 goto error0;
4168 }
4169 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004170 xfs_bmap_sanity_check(mp, bp, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 error0);
4172 /*
4173 * Read-ahead the next leaf block, if any.
4174 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004175 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 if (nextbno != NULLFSBLOCK)
4177 xfs_btree_reada_bufl(mp, nextbno, 1);
4178 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004179 * Copy records into the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11004181 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004182 start = i;
4183 for (j = 0; j < num_recs; j++, i++, frp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004184 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10004185 trp->l0 = be64_to_cpu(frp->l0);
4186 trp->l1 = be64_to_cpu(frp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 }
4188 if (exntf == XFS_EXTFMT_NOSTATE) {
4189 /*
4190 * Check all attribute bmap btree records and
4191 * any "older" data bmap btree records for a
4192 * set bit in the "extent flag" position.
4193 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004194 if (unlikely(xfs_check_nostate_extents(ifp,
4195 start, num_recs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4197 XFS_ERRLEVEL_LOW,
4198 ip->i_mount);
4199 goto error0;
4200 }
4201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 xfs_trans_brelse(tp, bp);
4203 bno = nextbno;
4204 /*
4205 * If we've reached the end, stop.
4206 */
4207 if (bno == NULLFSBLOCK)
4208 break;
4209 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4210 XFS_BMAP_BTREE_REF)))
4211 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004212 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004214 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
Eric Sandeen3a59c942007-07-11 11:09:47 +10004216 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 return 0;
4218error0:
4219 xfs_trans_brelse(tp, bp);
4220 return XFS_ERROR(EFSCORRUPTED);
4221}
4222
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004223#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004225 * Add bmap trace insert entries for all the contents of the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 */
4227void
4228xfs_bmap_trace_exlist(
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 xfs_inode_t *ip, /* incore inode pointer */
4230 xfs_extnum_t cnt, /* count of entries in the list */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004231 int whichfork, /* data or attr fork */
4232 unsigned long caller_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004234 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004236 int state = 0;
4237
4238 if (whichfork == XFS_ATTR_FORK)
4239 state |= BMAP_ATTRFORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
4241 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004242 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004243 for (idx = 0; idx < cnt; idx++)
4244 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247/*
4248 * Validate that the bmbt_irecs being returned from bmapi are valid
4249 * given the callers original parameters. Specifically check the
4250 * ranges of the returned irecs to ensure that they only extent beyond
4251 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4252 */
4253STATIC void
4254xfs_bmap_validate_ret(
4255 xfs_fileoff_t bno,
4256 xfs_filblks_t len,
4257 int flags,
4258 xfs_bmbt_irec_t *mval,
4259 int nmap,
4260 int ret_nmap)
4261{
4262 int i; /* index to map values */
4263
4264 ASSERT(ret_nmap <= nmap);
4265
4266 for (i = 0; i < ret_nmap; i++) {
4267 ASSERT(mval[i].br_blockcount > 0);
4268 if (!(flags & XFS_BMAPI_ENTIRE)) {
4269 ASSERT(mval[i].br_startoff >= bno);
4270 ASSERT(mval[i].br_blockcount <= len);
4271 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4272 bno + len);
4273 } else {
4274 ASSERT(mval[i].br_startoff < bno + len);
4275 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4276 bno);
4277 }
4278 ASSERT(i == 0 ||
4279 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4280 mval[i].br_startoff);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004281 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4282 mval[i].br_startblock != HOLESTARTBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4284 mval[i].br_state == XFS_EXT_UNWRITTEN);
4285 }
4286}
4287#endif /* DEBUG */
4288
4289
4290/*
Dave Chinneraef9a892011-09-18 20:40:44 +00004291 * Trim the returned map to the required bounds
4292 */
4293STATIC void
4294xfs_bmapi_trim_map(
4295 struct xfs_bmbt_irec *mval,
4296 struct xfs_bmbt_irec *got,
4297 xfs_fileoff_t *bno,
4298 xfs_filblks_t len,
4299 xfs_fileoff_t obno,
4300 xfs_fileoff_t end,
4301 int n,
4302 int flags)
4303{
4304 if ((flags & XFS_BMAPI_ENTIRE) ||
4305 got->br_startoff + got->br_blockcount <= obno) {
4306 *mval = *got;
4307 if (isnullstartblock(got->br_startblock))
4308 mval->br_startblock = DELAYSTARTBLOCK;
4309 return;
4310 }
4311
4312 if (obno > *bno)
4313 *bno = obno;
4314 ASSERT((*bno >= obno) || (n == 0));
4315 ASSERT(*bno < end);
4316 mval->br_startoff = *bno;
4317 if (isnullstartblock(got->br_startblock))
4318 mval->br_startblock = DELAYSTARTBLOCK;
4319 else
4320 mval->br_startblock = got->br_startblock +
4321 (*bno - got->br_startoff);
4322 /*
4323 * Return the minimum of what we got and what we asked for for
4324 * the length. We can use the len variable here because it is
4325 * modified below and we could have been there before coming
4326 * here if the first part of the allocation didn't overlap what
4327 * was asked for.
4328 */
4329 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
4330 got->br_blockcount - (*bno - got->br_startoff));
4331 mval->br_state = got->br_state;
4332 ASSERT(mval->br_blockcount <= len);
4333 return;
4334}
4335
4336/*
4337 * Update and validate the extent map to return
4338 */
4339STATIC void
4340xfs_bmapi_update_map(
4341 struct xfs_bmbt_irec **map,
4342 xfs_fileoff_t *bno,
4343 xfs_filblks_t *len,
4344 xfs_fileoff_t obno,
4345 xfs_fileoff_t end,
4346 int *n,
4347 int flags)
4348{
4349 xfs_bmbt_irec_t *mval = *map;
4350
4351 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4352 ((mval->br_startoff + mval->br_blockcount) <= end));
4353 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4354 (mval->br_startoff < obno));
4355
4356 *bno = mval->br_startoff + mval->br_blockcount;
4357 *len = end - *bno;
4358 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4359 /* update previous map with new information */
4360 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4361 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4362 ASSERT(mval->br_state == mval[-1].br_state);
4363 mval[-1].br_blockcount = mval->br_blockcount;
4364 mval[-1].br_state = mval->br_state;
4365 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4366 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4367 mval[-1].br_startblock != HOLESTARTBLOCK &&
4368 mval->br_startblock == mval[-1].br_startblock +
4369 mval[-1].br_blockcount &&
4370 ((flags & XFS_BMAPI_IGSTATE) ||
4371 mval[-1].br_state == mval->br_state)) {
4372 ASSERT(mval->br_startoff ==
4373 mval[-1].br_startoff + mval[-1].br_blockcount);
4374 mval[-1].br_blockcount += mval->br_blockcount;
4375 } else if (*n > 0 &&
4376 mval->br_startblock == DELAYSTARTBLOCK &&
4377 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4378 mval->br_startoff ==
4379 mval[-1].br_startoff + mval[-1].br_blockcount) {
4380 mval[-1].br_blockcount += mval->br_blockcount;
4381 mval[-1].br_state = mval->br_state;
4382 } else if (!((*n == 0) &&
4383 ((mval->br_startoff + mval->br_blockcount) <=
4384 obno))) {
4385 mval++;
4386 (*n)++;
4387 }
4388 *map = mval;
4389}
4390
4391/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00004392 * Map file blocks to filesystem blocks without allocation.
4393 */
4394int
4395xfs_bmapi_read(
4396 struct xfs_inode *ip,
4397 xfs_fileoff_t bno,
4398 xfs_filblks_t len,
4399 struct xfs_bmbt_irec *mval,
4400 int *nmap,
4401 int flags)
4402{
4403 struct xfs_mount *mp = ip->i_mount;
4404 struct xfs_ifork *ifp;
4405 struct xfs_bmbt_irec got;
4406 struct xfs_bmbt_irec prev;
4407 xfs_fileoff_t obno;
4408 xfs_fileoff_t end;
4409 xfs_extnum_t lastx;
4410 int error;
4411 int eof;
4412 int n = 0;
4413 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4414 XFS_ATTR_FORK : XFS_DATA_FORK;
4415
4416 ASSERT(*nmap >= 1);
4417 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4418 XFS_BMAPI_IGSTATE)));
4419
4420 if (unlikely(XFS_TEST_ERROR(
4421 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4422 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4423 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4424 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
4425 return XFS_ERROR(EFSCORRUPTED);
4426 }
4427
4428 if (XFS_FORCED_SHUTDOWN(mp))
4429 return XFS_ERROR(EIO);
4430
4431 XFS_STATS_INC(xs_blk_mapr);
4432
4433 ifp = XFS_IFORK_PTR(ip, whichfork);
4434 ASSERT(ifp->if_ext_max ==
4435 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4436
4437 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4438 error = xfs_iread_extents(NULL, ip, whichfork);
4439 if (error)
4440 return error;
4441 }
4442
4443 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4444 end = bno + len;
4445 obno = bno;
4446
4447 while (bno < end && n < *nmap) {
4448 /* Reading past eof, act as though there's a hole up to end. */
4449 if (eof)
4450 got.br_startoff = end;
4451 if (got.br_startoff > bno) {
4452 /* Reading in a hole. */
4453 mval->br_startoff = bno;
4454 mval->br_startblock = HOLESTARTBLOCK;
4455 mval->br_blockcount =
4456 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4457 mval->br_state = XFS_EXT_NORM;
4458 bno += mval->br_blockcount;
4459 len -= mval->br_blockcount;
4460 mval++;
4461 n++;
4462 continue;
4463 }
4464
4465 /* set up the extent map to return. */
4466 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4467 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4468
4469 /* If we're done, stop now. */
4470 if (bno >= end || n >= *nmap)
4471 break;
4472
4473 /* Else go on to the next record. */
4474 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4475 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4476 else
4477 eof = 1;
4478 }
4479 *nmap = n;
4480 return 0;
4481}
4482
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004483STATIC int
4484xfs_bmapi_reserve_delalloc(
4485 struct xfs_inode *ip,
4486 xfs_fileoff_t aoff,
4487 xfs_filblks_t len,
4488 struct xfs_bmbt_irec *got,
4489 struct xfs_bmbt_irec *prev,
4490 xfs_extnum_t *lastx,
4491 int eof)
4492{
4493 struct xfs_mount *mp = ip->i_mount;
4494 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4495 xfs_extlen_t alen;
4496 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004497 char rt = XFS_IS_REALTIME_INODE(ip);
4498 xfs_extlen_t extsz;
4499 int error;
4500
4501 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4502 if (!eof)
4503 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4504
4505 /* Figure out the extent size, adjust alen */
4506 extsz = xfs_get_extsz_hint(ip);
4507 if (extsz) {
4508 /*
4509 * Make sure we don't exceed a single extent length when we
4510 * align the extent by reducing length we are going to
4511 * allocate by the maximum amount extent size aligment may
4512 * require.
4513 */
4514 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1));
4515 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4516 1, 0, &aoff, &alen);
4517 ASSERT(!error);
4518 }
4519
4520 if (rt)
4521 extsz = alen / mp->m_sb.sb_rextsize;
4522
4523 /*
4524 * Make a transaction-less quota reservation for delayed allocation
4525 * blocks. This number gets adjusted later. We return if we haven't
4526 * allocated blocks already inside this loop.
4527 */
4528 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4529 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4530 if (error)
4531 return error;
4532
4533 /*
4534 * Split changing sb for alen and indlen since they could be coming
4535 * from different places.
4536 */
4537 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4538 ASSERT(indlen > 0);
4539
4540 if (rt) {
4541 error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
4542 -((int64_t)extsz), 0);
4543 } else {
4544 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4545 -((int64_t)alen), 0);
4546 }
4547
4548 if (error)
4549 goto out_unreserve_quota;
4550
4551 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4552 -((int64_t)indlen), 0);
4553 if (error)
4554 goto out_unreserve_blocks;
4555
4556
4557 ip->i_delayed_blks += alen;
4558
4559 got->br_startoff = aoff;
4560 got->br_startblock = nullstartblock(indlen);
4561 got->br_blockcount = alen;
4562 got->br_state = XFS_EXT_NORM;
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004563 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004564
4565 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004566 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4567 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004568 */
4569 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4570
4571 ASSERT(got->br_startoff <= aoff);
4572 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4573 ASSERT(isnullstartblock(got->br_startblock));
4574 ASSERT(got->br_state == XFS_EXT_NORM);
4575 return 0;
4576
4577out_unreserve_blocks:
4578 if (rt)
4579 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0);
4580 else
4581 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0);
4582out_unreserve_quota:
4583 if (XFS_IS_QUOTA_ON(mp))
4584 xfs_trans_unreserve_quota_nblks(NULL, ip, alen, 0, rt ?
4585 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4586 return error;
4587}
4588
Dave Chinner5c8ed202011-09-18 20:40:45 +00004589/*
Christoph Hellwig44032802011-09-18 20:40:48 +00004590 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4591 */
4592int
4593xfs_bmapi_delay(
4594 struct xfs_inode *ip, /* incore inode */
4595 xfs_fileoff_t bno, /* starting file offs. mapped */
4596 xfs_filblks_t len, /* length to map in file */
4597 struct xfs_bmbt_irec *mval, /* output: map values */
4598 int *nmap, /* i/o: mval size/count */
4599 int flags) /* XFS_BMAPI_... */
4600{
4601 struct xfs_mount *mp = ip->i_mount;
4602 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4603 struct xfs_bmbt_irec got; /* current file extent record */
4604 struct xfs_bmbt_irec prev; /* previous file extent record */
4605 xfs_fileoff_t obno; /* old block number (offset) */
4606 xfs_fileoff_t end; /* end of mapped file region */
4607 xfs_extnum_t lastx; /* last useful extent number */
4608 int eof; /* we've hit the end of extents */
4609 int n = 0; /* current extent index */
4610 int error = 0;
4611
4612 ASSERT(*nmap >= 1);
4613 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4614 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
4615
4616 if (unlikely(XFS_TEST_ERROR(
4617 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4618 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4619 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4620 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
4621 return XFS_ERROR(EFSCORRUPTED);
4622 }
4623
4624 if (XFS_FORCED_SHUTDOWN(mp))
4625 return XFS_ERROR(EIO);
4626
4627 XFS_STATS_INC(xs_blk_mapw);
4628
4629 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4630 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4631 if (error)
4632 return error;
4633 }
4634
4635 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4636 end = bno + len;
4637 obno = bno;
4638
4639 while (bno < end && n < *nmap) {
4640 if (eof || got.br_startoff > bno) {
4641 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4642 &prev, &lastx, eof);
4643 if (error) {
4644 if (n == 0) {
4645 *nmap = 0;
4646 return error;
4647 }
4648 break;
4649 }
4650 }
4651
4652 /* set up the extent map to return. */
4653 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4654 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4655
4656 /* If we're done, stop now. */
4657 if (bno >= end || n >= *nmap)
4658 break;
4659
4660 /* Else go on to the next record. */
4661 prev = got;
4662 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4663 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4664 else
4665 eof = 1;
4666 }
4667
4668 *nmap = n;
4669 return 0;
4670}
4671
4672
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004673STATIC int
4674xfs_bmapi_allocate(
4675 struct xfs_bmalloca *bma,
4676 xfs_extnum_t *lastx,
4677 struct xfs_btree_cur **cur,
4678 xfs_fsblock_t *firstblock,
4679 struct xfs_bmap_free *flist,
4680 int flags,
4681 int *nallocs,
4682 int *logflags)
4683{
4684 struct xfs_mount *mp = bma->ip->i_mount;
4685 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4686 XFS_ATTR_FORK : XFS_DATA_FORK;
4687 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4688 xfs_fsblock_t abno;
4689 xfs_extlen_t alen;
4690 xfs_fileoff_t aoff;
4691 int error;
4692 int rt;
4693
4694 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(bma->ip);
4695
4696 /*
4697 * For the wasdelay case, we could also just allocate the stuff asked
4698 * for in this bmap call but that wouldn't be as good.
4699 */
4700 if (bma->wasdel) {
4701 alen = (xfs_extlen_t)bma->gotp->br_blockcount;
4702 aoff = bma->gotp->br_startoff;
4703 if (*lastx != NULLEXTNUM && *lastx) {
4704 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx - 1),
4705 bma->prevp);
4706 }
4707 } else {
4708 alen = (xfs_extlen_t)XFS_FILBLKS_MIN(bma->alen, MAXEXTLEN);
4709 if (!bma->eof)
4710 alen = (xfs_extlen_t)XFS_FILBLKS_MIN(alen,
4711 bma->gotp->br_startoff - bma->off);
4712 aoff = bma->off;
4713 }
4714
4715 /*
4716 * Indicate if this is the first user data in the file, or just any
4717 * user data.
4718 */
4719 if (!(flags & XFS_BMAPI_METADATA)) {
4720 bma->userdata = (aoff == 0) ?
4721 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4722 }
4723
4724 /*
4725 * Fill in changeable bma fields.
4726 */
4727 bma->alen = alen;
4728 bma->off = aoff;
4729 bma->firstblock = *firstblock;
4730 bma->minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4731 bma->low = flist->xbf_low;
4732 bma->aeof = 0;
4733
4734 /*
4735 * Only want to do the alignment at the eof if it is userdata and
4736 * allocation length is larger than a stripe unit.
4737 */
4738 if (mp->m_dalign && alen >= mp->m_dalign &&
4739 !(flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
4740 error = xfs_bmap_isaeof(bma->ip, aoff, whichfork, &bma->aeof);
4741 if (error)
4742 return error;
4743 }
4744
4745 error = xfs_bmap_alloc(bma);
4746 if (error)
4747 return error;
4748
4749 /*
4750 * Copy out result fields.
4751 */
4752 abno = bma->rval;
4753 flist->xbf_low = bma->low;
4754 alen = bma->alen;
4755 aoff = bma->off;
4756 ASSERT(*firstblock == NULLFSBLOCK ||
4757 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4758 XFS_FSB_TO_AGNO(mp, bma->firstblock) ||
4759 (flist->xbf_low &&
4760 XFS_FSB_TO_AGNO(mp, *firstblock) <
4761 XFS_FSB_TO_AGNO(mp, bma->firstblock)));
4762 *firstblock = bma->firstblock;
4763 if (*cur)
4764 (*cur)->bc_private.b.firstblock = *firstblock;
4765 if (abno == NULLFSBLOCK)
4766 return 0;
4767 if ((ifp->if_flags & XFS_IFBROOT) && !*cur) {
4768 (*cur) = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4769 (*cur)->bc_private.b.firstblock = *firstblock;
4770 (*cur)->bc_private.b.flist = flist;
4771 }
4772 /*
4773 * Bump the number of extents we've allocated
4774 * in this call.
4775 */
4776 (*nallocs)++;
4777
4778 if (*cur)
4779 (*cur)->bc_private.b.flags =
4780 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4781
4782 bma->gotp->br_startoff = aoff;
4783 bma->gotp->br_startblock = abno;
4784 bma->gotp->br_blockcount = alen;
4785 bma->gotp->br_state = XFS_EXT_NORM;
4786
4787 /*
4788 * A wasdelay extent has been initialized, so shouldn't be flagged
4789 * as unwritten.
4790 */
4791 if (!bma->wasdel && (flags & XFS_BMAPI_PREALLOC) &&
4792 xfs_sb_version_hasextflgbit(&mp->m_sb))
4793 bma->gotp->br_state = XFS_EXT_UNWRITTEN;
4794
4795 error = xfs_bmap_add_extent(bma->tp, bma->ip, lastx, cur, bma->gotp,
4796 firstblock, flist, logflags, whichfork);
4797 if (error)
4798 return error;
4799
4800 /*
4801 * Update our extent pointer, given that xfs_bmap_add_extent might
4802 * have merged it into one of the neighbouring ones.
4803 */
4804 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), bma->gotp);
4805
4806 ASSERT(bma->gotp->br_startoff <= aoff);
4807 ASSERT(bma->gotp->br_startoff + bma->gotp->br_blockcount >=
4808 aoff + alen);
4809 ASSERT(bma->gotp->br_state == XFS_EXT_NORM ||
4810 bma->gotp->br_state == XFS_EXT_UNWRITTEN);
4811 return 0;
4812}
4813
Dave Chinnerb447fe52011-09-18 20:40:51 +00004814STATIC int
4815xfs_bmapi_convert_unwritten(
4816 struct xfs_bmalloca *bma,
4817 struct xfs_bmbt_irec *mval,
4818 xfs_filblks_t len,
4819 xfs_extnum_t *lastx,
4820 struct xfs_btree_cur **cur,
4821 xfs_fsblock_t *firstblock,
4822 struct xfs_bmap_free *flist,
4823 int flags,
4824 int *logflags)
4825{
4826 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4827 XFS_ATTR_FORK : XFS_DATA_FORK;
4828 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4829 int error;
4830
4831 *logflags = 0;
4832
4833 /* check if we need to do unwritten->real conversion */
4834 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4835 (flags & XFS_BMAPI_PREALLOC))
4836 return 0;
4837
4838 /* check if we need to do real->unwritten conversion */
4839 if (mval->br_state == XFS_EXT_NORM &&
4840 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4841 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4842 return 0;
4843
4844 /*
4845 * Modify (by adding) the state flag, if writing.
4846 */
4847 ASSERT(mval->br_blockcount <= len);
4848 if ((ifp->if_flags & XFS_IFBROOT) && !*cur) {
4849 *cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4850 bma->ip, whichfork);
4851 (*cur)->bc_private.b.firstblock = *firstblock;
4852 (*cur)->bc_private.b.flist = flist;
4853 }
4854 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4855 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4856
4857 error = xfs_bmap_add_extent(bma->tp, bma->ip, lastx, cur, mval,
4858 firstblock, flist, logflags, whichfork);
4859 if (error)
4860 return error;
4861
4862 /*
4863 * Update our extent pointer, given that xfs_bmap_add_extent might
4864 * have merged it into one of the neighbouring ones.
4865 */
4866 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), bma->gotp);
4867
4868 /*
4869 * We may have combined previously unwritten space with written space,
4870 * so generate another request.
4871 */
4872 if (mval->br_blockcount < len)
4873 return EAGAIN;
4874 return 0;
4875}
4876
Christoph Hellwig44032802011-09-18 20:40:48 +00004877/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004878 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4879 * extent state if necessary. Details behaviour is controlled by the flags
4880 * parameter. Only allocates blocks from a single allocation group, to avoid
4881 * locking problems.
4882 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 * The returned value in "firstblock" from the first call in a transaction
4884 * must be remembered and presented to subsequent calls in "firstblock".
4885 * An upper bound for the number of blocks to be allocated is supplied to
4886 * the first call in "total"; if no allocation group has that many free
4887 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4888 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004889int
4890xfs_bmapi_write(
4891 struct xfs_trans *tp, /* transaction pointer */
4892 struct xfs_inode *ip, /* incore inode */
4893 xfs_fileoff_t bno, /* starting file offs. mapped */
4894 xfs_filblks_t len, /* length to map in file */
4895 int flags, /* XFS_BMAPI_... */
4896 xfs_fsblock_t *firstblock, /* first allocated block
4897 controls a.g. for allocs */
4898 xfs_extlen_t total, /* total blocks needed */
4899 struct xfs_bmbt_irec *mval, /* output: map values */
4900 int *nmap, /* i/o: mval size/count */
4901 struct xfs_bmap_free *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004903 struct xfs_mount *mp = ip->i_mount;
4904 struct xfs_ifork *ifp;
4905 struct xfs_bmalloca bma = { 0 }; /* args for xfs_bmap_alloc */
4906 struct xfs_btree_cur *cur; /* bmap btree cursor */
4907 xfs_fileoff_t end; /* end of mapped file region */
4908 int eof; /* after the end of extents */
4909 int error; /* error return */
4910 struct xfs_bmbt_irec got; /* current file extent record */
4911 xfs_extnum_t lastx; /* last useful extent number */
4912 int logflags; /* flags for transaction logging */
4913 xfs_extlen_t minleft; /* min blocks left after allocation */
4914 int n; /* current extent index */
4915 int nallocs; /* number of extents alloc'd */
4916 xfs_fileoff_t obno; /* old block number (offset) */
4917 struct xfs_bmbt_irec prev; /* previous file extent record */
4918 int tmp_logflags; /* temp flags holder */
4919 int whichfork; /* data or attr fork */
4920 char inhole; /* current location is hole in file */
4921 char wasdelay; /* old extent was delayed */
4922
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004924 xfs_fileoff_t orig_bno; /* original block number value */
4925 int orig_flags; /* original flags arg value */
4926 xfs_filblks_t orig_len; /* original value of len arg */
4927 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4928 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
4930 orig_bno = bno;
4931 orig_len = len;
4932 orig_flags = flags;
4933 orig_mval = mval;
4934 orig_nmap = *nmap;
4935#endif
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004936
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004938 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4939 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4940 ASSERT(tp != NULL);
4941
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4943 XFS_ATTR_FORK : XFS_DATA_FORK;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004944
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 if (unlikely(XFS_TEST_ERROR(
4946 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4947 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4948 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4949 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004950 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 return XFS_ERROR(EFSCORRUPTED);
4952 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004953
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 if (XFS_FORCED_SHUTDOWN(mp))
4955 return XFS_ERROR(EIO);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004956
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 ifp = XFS_IFORK_PTR(ip, whichfork);
4958 ASSERT(ifp->if_ext_max ==
4959 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004960
4961 XFS_STATS_INC(xs_blk_mapw);
4962
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 logflags = 0;
4964 nallocs = 0;
4965 cur = NULL;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004966
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004968 error = xfs_bmap_local_to_extents(tp, ip, firstblock, total,
4969 &logflags, whichfork);
4970 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 goto error0;
4972 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004973
4974 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Christoph Hellwig16259e72005-11-02 15:11:25 +11004976 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 else
4978 minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004979 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004981 }
4982
4983 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4984 error = xfs_iread_extents(tp, ip, whichfork);
4985 if (error)
4986 goto error0;
4987 }
4988
4989 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 n = 0;
4991 end = bno + len;
4992 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004993
4994 bma.tp = tp;
4995 bma.ip = ip;
4996 bma.prevp = &prev;
4997 bma.gotp = &got;
4998 bma.total = total;
4999 bma.userdata = 0;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005000
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 while (bno < end && n < *nmap) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 inhole = eof || got.br_startoff > bno;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005003 wasdelay = !inhole && isnullstartblock(got.br_startblock);
5004
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 /*
5006 * First, deal with the hole before the allocated space
5007 * that we found, if any.
5008 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005009 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00005010 bma.eof = eof;
5011 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
5012 bma.wasdel = wasdelay;
5013 bma.alen = len;
5014 bma.off = bno;
5015 bma.minleft = minleft;
5016
5017 error = xfs_bmapi_allocate(&bma, &lastx, &cur,
5018 firstblock, flist, flags, &nallocs,
5019 &tmp_logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 logflags |= tmp_logflags;
5021 if (error)
5022 goto error0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00005023 if (flist && flist->xbf_low)
5024 minleft = 0;
5025 if (bma.rval == NULLFSBLOCK)
5026 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 }
Christoph Hellwig44032802011-09-18 20:40:48 +00005028
Dave Chinneraef9a892011-09-18 20:40:44 +00005029 /* Deal with the allocated space we found. */
5030 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
Dave Chinnerb447fe52011-09-18 20:40:51 +00005032 /* Execute unwritten extent conversion if necessary */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005033 error = xfs_bmapi_convert_unwritten(&bma, mval, len, &lastx,
5034 &cur, firstblock, flist,
5035 flags, &tmp_logflags);
5036 logflags |= tmp_logflags;
5037 if (error == EAGAIN)
5038 continue;
5039 if (error)
5040 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
Dave Chinneraef9a892011-09-18 20:40:44 +00005042 /* update the extent map to return */
5043 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
5044
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 /*
5046 * If we're done, stop now. Stop when we've allocated
5047 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5048 * the transaction may get too big.
5049 */
5050 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5051 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005052
5053 /* Else go on to the next record. */
David Chinner4e5ae832007-06-05 16:24:15 +10005054 prev = got;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005055 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
5056 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
5057 else
Christoph Hellwig5690f922011-05-11 15:04:07 +00005058 eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005061
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 /*
5063 * Transform from btree to extents, give it cur.
5064 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005065 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005067 ASSERT(cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5069 &tmp_logflags, whichfork);
5070 logflags |= tmp_logflags;
5071 if (error)
5072 goto error0;
5073 }
5074 ASSERT(ifp->if_ext_max ==
5075 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5076 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5077 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5078 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079error0:
5080 /*
5081 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005082 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005084 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005086 logflags &= ~xfs_ilog_fext(whichfork);
5087 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005089 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 /*
5091 * Log whatever the flags say, even if error. Otherwise we might miss
5092 * detecting a case where the data is changed, there's an error,
5093 * and it's not logged so we don't shutdown when we should.
5094 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005095 if (logflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 xfs_trans_log_inode(tp, ip, logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005097
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 if (cur) {
5099 if (!error) {
5100 ASSERT(*firstblock == NULLFSBLOCK ||
5101 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5102 XFS_FSB_TO_AGNO(mp,
5103 cur->bc_private.b.firstblock) ||
5104 (flist->xbf_low &&
5105 XFS_FSB_TO_AGNO(mp, *firstblock) <
5106 XFS_FSB_TO_AGNO(mp,
5107 cur->bc_private.b.firstblock)));
5108 *firstblock = cur->bc_private.b.firstblock;
5109 }
5110 xfs_btree_del_cursor(cur,
5111 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5112 }
5113 if (!error)
5114 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5115 orig_nmap, *nmap);
5116 return error;
5117}
5118
5119/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 * Unmap (remove) blocks from a file.
5121 * If nexts is nonzero then the number of extents to remove is limited to
5122 * that value. If not all extents in the block range can be removed then
5123 * *done is set.
5124 */
5125int /* error */
5126xfs_bunmapi(
5127 xfs_trans_t *tp, /* transaction pointer */
5128 struct xfs_inode *ip, /* incore inode */
5129 xfs_fileoff_t bno, /* starting offset to unmap */
5130 xfs_filblks_t len, /* length to unmap in file */
5131 int flags, /* misc flags */
5132 xfs_extnum_t nexts, /* number of extents max */
5133 xfs_fsblock_t *firstblock, /* first allocated block
5134 controls a.g. for allocs */
5135 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5136 int *done) /* set if not done yet */
5137{
5138 xfs_btree_cur_t *cur; /* bmap btree cursor */
5139 xfs_bmbt_irec_t del; /* extent being deleted */
5140 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005141 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 int error; /* error return value */
5143 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005144 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 xfs_ifork_t *ifp; /* inode fork pointer */
5146 int isrt; /* freeing in rt area */
5147 xfs_extnum_t lastx; /* last extent index used */
5148 int logflags; /* transaction logging flags */
5149 xfs_extlen_t mod; /* rt extent offset */
5150 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005151 xfs_extnum_t nextents; /* number of file extents */
5152 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 xfs_fileoff_t start; /* first file offset deleted */
5154 int tmp_logflags; /* partial logging flags */
5155 int wasdel; /* was a delayed alloc extent */
5156 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 xfs_fsblock_t sum;
5158
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005159 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5160
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5162 XFS_ATTR_FORK : XFS_DATA_FORK;
5163 ifp = XFS_IFORK_PTR(ip, whichfork);
5164 if (unlikely(
5165 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5166 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5167 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5168 ip->i_mount);
5169 return XFS_ERROR(EFSCORRUPTED);
5170 }
5171 mp = ip->i_mount;
5172 if (XFS_FORCED_SHUTDOWN(mp))
5173 return XFS_ERROR(EIO);
Christoph Hellwig54893272011-05-11 15:04:03 +00005174
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 ASSERT(len > 0);
5176 ASSERT(nexts >= 0);
5177 ASSERT(ifp->if_ext_max ==
5178 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5179 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5180 (error = xfs_iread_extents(tp, ip, whichfork)))
5181 return error;
5182 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5183 if (nextents == 0) {
5184 *done = 1;
5185 return 0;
5186 }
5187 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005188 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 start = bno;
5190 bno = start + len - 1;
5191 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5192 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005193
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 /*
5195 * Check to see if the given block number is past the end of the
5196 * file, back up to the last block if so...
5197 */
5198 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005199 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 xfs_bmbt_get_all(ep, &got);
5201 bno = got.br_startoff + got.br_blockcount - 1;
5202 }
5203 logflags = 0;
5204 if (ifp->if_flags & XFS_IFBROOT) {
5205 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005206 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 cur->bc_private.b.firstblock = *firstblock;
5208 cur->bc_private.b.flist = flist;
5209 cur->bc_private.b.flags = 0;
5210 } else
5211 cur = NULL;
5212 extno = 0;
5213 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5214 (nexts == 0 || extno < nexts)) {
5215 /*
5216 * Is the found extent after a hole in which bno lives?
5217 * Just back up to the previous extent, if so.
5218 */
5219 if (got.br_startoff > bno) {
5220 if (--lastx < 0)
5221 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005222 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 xfs_bmbt_get_all(ep, &got);
5224 }
5225 /*
5226 * Is the last block of this extent before the range
5227 * we're supposed to delete? If so, we're done.
5228 */
5229 bno = XFS_FILEOFF_MIN(bno,
5230 got.br_startoff + got.br_blockcount - 1);
5231 if (bno < start)
5232 break;
5233 /*
5234 * Then deal with the (possibly delayed) allocated space
5235 * we found.
5236 */
5237 ASSERT(ep != NULL);
5238 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005239 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 if (got.br_startoff < start) {
5241 del.br_startoff = start;
5242 del.br_blockcount -= start - got.br_startoff;
5243 if (!wasdel)
5244 del.br_startblock += start - got.br_startoff;
5245 }
5246 if (del.br_startoff + del.br_blockcount > bno + 1)
5247 del.br_blockcount = bno + 1 - del.br_startoff;
5248 sum = del.br_startblock + del.br_blockcount;
5249 if (isrt &&
5250 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5251 /*
5252 * Realtime extent not lined up at the end.
5253 * The extent could have been split into written
5254 * and unwritten pieces, or we could just be
5255 * unmapping part of it. But we can't really
5256 * get rid of part of a realtime extent.
5257 */
5258 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005259 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 /*
5261 * This piece is unwritten, or we're not
5262 * using unwritten extents. Skip over it.
5263 */
5264 ASSERT(bno >= mod);
5265 bno -= mod > del.br_blockcount ?
5266 del.br_blockcount : mod;
5267 if (bno < got.br_startoff) {
5268 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005269 xfs_bmbt_get_all(xfs_iext_get_ext(
5270 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 }
5272 continue;
5273 }
5274 /*
5275 * It's written, turn it unwritten.
5276 * This is better than zeroing it.
5277 */
5278 ASSERT(del.br_state == XFS_EXT_NORM);
5279 ASSERT(xfs_trans_get_block_res(tp) > 0);
5280 /*
5281 * If this spans a realtime extent boundary,
5282 * chop it back to the start of the one we end at.
5283 */
5284 if (del.br_blockcount > mod) {
5285 del.br_startoff += del.br_blockcount - mod;
5286 del.br_startblock += del.br_blockcount - mod;
5287 del.br_blockcount = mod;
5288 }
5289 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigf3ca8732011-07-08 14:34:47 +02005290 error = xfs_bmap_add_extent(tp, ip, &lastx, &cur, &del,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005291 firstblock, flist, &logflags,
Christoph Hellwig54893272011-05-11 15:04:03 +00005292 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 if (error)
5294 goto error0;
5295 goto nodelete;
5296 }
5297 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5298 /*
5299 * Realtime extent is lined up at the end but not
5300 * at the front. We'll get rid of full extents if
5301 * we can.
5302 */
5303 mod = mp->m_sb.sb_rextsize - mod;
5304 if (del.br_blockcount > mod) {
5305 del.br_blockcount -= mod;
5306 del.br_startoff += mod;
5307 del.br_startblock += mod;
5308 } else if ((del.br_startoff == start &&
5309 (del.br_state == XFS_EXT_UNWRITTEN ||
5310 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005311 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 /*
5313 * Can't make it unwritten. There isn't
5314 * a full extent here so just skip it.
5315 */
5316 ASSERT(bno >= del.br_blockcount);
5317 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005318 if (got.br_startoff > bno) {
5319 if (--lastx >= 0) {
5320 ep = xfs_iext_get_ext(ifp,
5321 lastx);
5322 xfs_bmbt_get_all(ep, &got);
5323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 }
5325 continue;
5326 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5327 /*
5328 * This one is already unwritten.
5329 * It must have a written left neighbor.
5330 * Unwrite the killed part of that one and
5331 * try again.
5332 */
5333 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005334 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5335 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005337 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 ASSERT(del.br_startblock ==
5339 prev.br_startblock + prev.br_blockcount);
5340 if (prev.br_startoff < start) {
5341 mod = start - prev.br_startoff;
5342 prev.br_blockcount -= mod;
5343 prev.br_startblock += mod;
5344 prev.br_startoff = start;
5345 }
5346 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005347 lastx--;
Christoph Hellwigf3ca8732011-07-08 14:34:47 +02005348 error = xfs_bmap_add_extent(tp, ip, &lastx,
5349 &cur, &prev, firstblock, flist,
5350 &logflags, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 if (error)
5352 goto error0;
5353 goto nodelete;
5354 } else {
5355 ASSERT(del.br_state == XFS_EXT_NORM);
5356 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigf3ca8732011-07-08 14:34:47 +02005357 error = xfs_bmap_add_extent(tp, ip, &lastx,
5358 &cur, &del, firstblock, flist,
5359 &logflags, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 if (error)
5361 goto error0;
5362 goto nodelete;
5363 }
5364 }
5365 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005366 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005367 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005368 if (isrt) {
5369 xfs_filblks_t rtexts;
5370
5371 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5372 do_div(rtexts, mp->m_sb.sb_rextsize);
5373 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005374 (int64_t)rtexts, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005375 (void)xfs_trans_reserve_quota_nblks(NULL,
5376 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005377 XFS_QMOPT_RES_RTBLKS);
5378 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00005379 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005380 (int64_t)del.br_blockcount, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005381 (void)xfs_trans_reserve_quota_nblks(NULL,
5382 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 ip->i_delayed_blks -= del.br_blockcount;
5386 if (cur)
5387 cur->bc_private.b.flags |=
5388 XFS_BTCUR_BPRV_WASDEL;
5389 } else if (cur)
5390 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5391 /*
5392 * If it's the case where the directory code is running
5393 * with no block reservation, and the deleted block is in
5394 * the middle of its extent, and the resulting insert
5395 * of an extent would cause transformation to btree format,
5396 * then reject it. The calling code will then swap
5397 * blocks around instead.
5398 * We have to do this now, rather than waiting for the
5399 * conversion to btree format, since the transaction
5400 * will be dirty.
5401 */
5402 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5403 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5404 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5405 del.br_startoff > got.br_startoff &&
5406 del.br_startoff + del.br_blockcount <
5407 got.br_startoff + got.br_blockcount) {
5408 error = XFS_ERROR(ENOSPC);
5409 goto error0;
5410 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00005411 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005412 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 logflags |= tmp_logflags;
5414 if (error)
5415 goto error0;
5416 bno = del.br_startoff - 1;
5417nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 /*
5419 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005422 if (lastx >= 0) {
5423 ep = xfs_iext_get_ext(ifp, lastx);
5424 if (xfs_bmbt_get_startoff(ep) > bno) {
5425 if (--lastx >= 0)
5426 ep = xfs_iext_get_ext(ifp,
5427 lastx);
5428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 extno++;
5432 }
5433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5435 ASSERT(ifp->if_ext_max ==
5436 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5437 /*
5438 * Convert to a btree if necessary.
5439 */
5440 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5441 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5442 ASSERT(cur == NULL);
5443 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5444 &cur, 0, &tmp_logflags, whichfork);
5445 logflags |= tmp_logflags;
5446 if (error)
5447 goto error0;
5448 }
5449 /*
5450 * transform from btree to extents, give it cur
5451 */
5452 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5453 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5454 ASSERT(cur != NULL);
5455 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5456 whichfork);
5457 logflags |= tmp_logflags;
5458 if (error)
5459 goto error0;
5460 }
5461 /*
5462 * transform from extents to local?
5463 */
5464 ASSERT(ifp->if_ext_max ==
5465 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5466 error = 0;
5467error0:
5468 /*
5469 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005470 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005472 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005474 logflags &= ~xfs_ilog_fext(whichfork);
5475 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005477 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 /*
5479 * Log inode even in the error case, if the transaction
5480 * is dirty we'll need to shut down the filesystem.
5481 */
5482 if (logflags)
5483 xfs_trans_log_inode(tp, ip, logflags);
5484 if (cur) {
5485 if (!error) {
5486 *firstblock = cur->bc_private.b.firstblock;
5487 cur->bc_private.b.allocated = 0;
5488 }
5489 xfs_btree_del_cursor(cur,
5490 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5491 }
5492 return error;
5493}
5494
5495/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005496 * returns 1 for success, 0 if we failed to map the extent.
5497 */
5498STATIC int
5499xfs_getbmapx_fix_eof_hole(
5500 xfs_inode_t *ip, /* xfs incore inode pointer */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005501 struct getbmapx *out, /* output structure */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005502 int prealloced, /* this is a file with
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005503 * preallocated data space */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005504 __int64_t end, /* last block requested */
5505 xfs_fsblock_t startblock)
5506{
5507 __int64_t fixlen;
5508 xfs_mount_t *mp; /* file system mount point */
Eric Sandeen5af317c2008-11-28 14:23:35 +11005509 xfs_ifork_t *ifp; /* inode fork pointer */
5510 xfs_extnum_t lastx; /* last extent pointer */
5511 xfs_fileoff_t fileblock;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005512
5513 if (startblock == HOLESTARTBLOCK) {
5514 mp = ip->i_mount;
5515 out->bmv_block = -1;
5516 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5517 fixlen -= out->bmv_offset;
5518 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5519 /* Came to hole at EOF. Trim it. */
5520 if (fixlen <= 0)
5521 return 0;
5522 out->bmv_length = fixlen;
5523 }
5524 } else {
Eric Sandeen5af317c2008-11-28 14:23:35 +11005525 if (startblock == DELAYSTARTBLOCK)
5526 out->bmv_block = -2;
5527 else
Eric Sandeen9d87c312009-01-14 23:22:07 -06005528 out->bmv_block = xfs_fsb_to_db(ip, startblock);
Eric Sandeen5af317c2008-11-28 14:23:35 +11005529 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5530 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5531 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5532 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5533 out->bmv_oflags |= BMV_OF_LAST;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005534 }
5535
5536 return 1;
5537}
5538
5539/*
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005540 * Get inode's extents as described in bmv, and format for output.
5541 * Calls formatter to fill the user's buffer until all extents
5542 * are mapped, until the passed-in bmv->bmv_count slots have
5543 * been filled, or until the formatter short-circuits the loop,
5544 * if it is tracking filled-in extents on its own.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 */
5546int /* error code */
5547xfs_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10005548 xfs_inode_t *ip,
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005549 struct getbmapx *bmv, /* user bmap structure */
5550 xfs_bmap_format_t formatter, /* format to user */
5551 void *arg) /* formatter arg */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552{
5553 __int64_t bmvend; /* last block requested */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005554 int error = 0; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 __int64_t fixlen; /* length for -1 case */
5556 int i; /* extent number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 int lock; /* lock state */
5558 xfs_bmbt_irec_t *map; /* buffer for user's data */
5559 xfs_mount_t *mp; /* file system mount point */
5560 int nex; /* # of user extents can do */
5561 int nexleft; /* # of user extents left */
5562 int subnex; /* # of bmapi's can do */
5563 int nmap; /* number of map entries */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005564 struct getbmapx *out; /* output structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 int whichfork; /* data or attr fork */
5566 int prealloced; /* this is a file with
5567 * preallocated data space */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005568 int iflags; /* interface flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 int bmapi_flags; /* flags for xfs_bmapi */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005570 int cur_ext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 mp = ip->i_mount;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005573 iflags = bmv->bmv_iflags;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005574 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 if (whichfork == XFS_ATTR_FORK) {
5577 if (XFS_IFORK_Q(ip)) {
5578 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5579 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5580 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5581 return XFS_ERROR(EINVAL);
5582 } else if (unlikely(
5583 ip->i_d.di_aformat != 0 &&
5584 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5585 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5586 ip->i_mount);
5587 return XFS_ERROR(EFSCORRUPTED);
5588 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005589
5590 prealloced = 0;
5591 fixlen = 1LL << 32;
5592 } else {
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005593 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5594 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5595 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5596 return XFS_ERROR(EINVAL);
5597
David Chinner957d0eb2007-06-18 16:50:37 +10005598 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005599 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 prealloced = 1;
5601 fixlen = XFS_MAXIOFFSET(mp);
5602 } else {
5603 prealloced = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005604 fixlen = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 }
5607
5608 if (bmv->bmv_length == -1) {
5609 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005610 bmv->bmv_length =
5611 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5612 } else if (bmv->bmv_length == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 bmv->bmv_entries = 0;
5614 return 0;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005615 } else if (bmv->bmv_length < 0) {
5616 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005618
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 nex = bmv->bmv_count - 1;
5620 if (nex <= 0)
5621 return XFS_ERROR(EINVAL);
5622 bmvend = bmv->bmv_offset + bmv->bmv_length;
5623
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005624
5625 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5626 return XFS_ERROR(ENOMEM);
5627 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5628 if (!out)
5629 return XFS_ERROR(ENOMEM);
5630
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005632 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5633 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5634 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5635 if (error)
5636 goto out_unlock_iolock;
Niv Sardie12070a2008-03-06 13:43:03 +11005637 }
Dave Chinner309c8482010-11-30 15:16:02 +11005638 /*
5639 * even after flushing the inode, there can still be delalloc
5640 * blocks on the inode beyond EOF due to speculative
5641 * preallocation. These are not removed until the release
5642 * function is called or the inode is inactivated. Hence we
5643 * cannot assert here that ip->i_delayed_blks == 0.
5644 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
5647 lock = xfs_ilock_map_shared(ip);
5648
5649 /*
5650 * Don't let nex be bigger than the number of extents
5651 * we can have assuming alternating holes and real extents.
5652 */
5653 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5654 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5655
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005656 bmapi_flags = xfs_bmapi_aflag(whichfork);
5657 if (!(iflags & BMV_IF_PREALLOC))
5658 bmapi_flags |= XFS_BMAPI_IGSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659
5660 /*
5661 * Allocate enough space to handle "subnex" maps at a time.
5662 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005663 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 subnex = 16;
Christoph Hellwigca35dcd2009-07-18 18:14:54 -04005665 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005666 if (!map)
5667 goto out_unlock_ilock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668
5669 bmv->bmv_entries = 0;
5670
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005671 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5672 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5673 error = 0;
5674 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 }
5676
5677 nexleft = nex;
5678
5679 do {
5680 nmap = (nexleft > subnex) ? subnex : nexleft;
Dave Chinner5c8ed202011-09-18 20:40:45 +00005681 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5682 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5683 map, &nmap, bmapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 if (error)
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005685 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 ASSERT(nmap <= subnex);
5687
5688 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005689 out[cur_ext].bmv_oflags = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005690 if (map[i].br_state == XFS_EXT_UNWRITTEN)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005691 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005692 else if (map[i].br_startblock == DELAYSTARTBLOCK)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005693 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5694 out[cur_ext].bmv_offset =
5695 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5696 out[cur_ext].bmv_length =
5697 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5698 out[cur_ext].bmv_unused1 = 0;
5699 out[cur_ext].bmv_unused2 = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005700 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5701 (map[i].br_startblock != DELAYSTARTBLOCK));
Yingping Lu9af0a702005-11-02 15:09:54 +11005702 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005703 whichfork == XFS_ATTR_FORK) {
5704 /* came to the end of attribute fork */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005705 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005706 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005708
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005709 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5710 prealloced, bmvend,
5711 map[i].br_startblock))
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005712 goto out_free_map;
5713
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005714 bmv->bmv_offset =
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005715 out[cur_ext].bmv_offset +
5716 out[cur_ext].bmv_length;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005717 bmv->bmv_length =
5718 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
Tao Ma9af25462010-08-30 02:44:03 +00005719
5720 /*
5721 * In case we don't want to return the hole,
5722 * don't increase cur_ext so that we can reuse
5723 * it in the next loop.
5724 */
5725 if ((iflags & BMV_IF_NO_HOLES) &&
5726 map[i].br_startblock == HOLESTARTBLOCK) {
5727 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5728 continue;
5729 }
5730
5731 nexleft--;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005732 bmv->bmv_entries++;
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005733 cur_ext++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 }
5735 } while (nmap && nexleft && bmv->bmv_length);
5736
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005737 out_free_map:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10005738 kmem_free(map);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005739 out_unlock_ilock:
5740 xfs_iunlock_map_shared(ip, lock);
5741 out_unlock_iolock:
5742 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005743
5744 for (i = 0; i < cur_ext; i++) {
5745 int full = 0; /* user array is full */
5746
5747 /* format results & advance arg */
5748 error = formatter(&arg, &out[i], &full);
5749 if (error || full)
5750 break;
5751 }
5752
Felix Blyakher7747a0b2009-06-11 17:07:28 -05005753 kmem_free(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 return error;
5755}
5756
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757#ifdef DEBUG
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005758STATIC struct xfs_buf *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759xfs_bmap_get_bp(
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005760 struct xfs_btree_cur *cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 xfs_fsblock_t bno)
5762{
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005763 struct xfs_log_item_desc *lidp;
5764 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765
5766 if (!cur)
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005767 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005769 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5770 if (!cur->bc_bufs[i])
5771 break;
5772 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5773 return cur->bc_bufs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005776 /* Chase down all the log items to see if the bp is there */
5777 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
Christoph Hellwige98c4142010-06-23 18:11:15 +10005778 struct xfs_buf_log_item *bip;
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005779 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5780 if (bip->bli_item.li_type == XFS_LI_BUF &&
5781 XFS_BUF_ADDR(bip->bli_buf) == bno)
5782 return bip->bli_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 }
Christoph Hellwige98c4142010-06-23 18:11:15 +10005784
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005785 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786}
5787
Hannes Eder3180e662009-03-04 19:34:10 +01005788STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789xfs_check_block(
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005790 struct xfs_btree_block *block,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 xfs_mount_t *mp,
5792 int root,
5793 short sz)
5794{
5795 int i, j, dmxr;
Christoph Hellwig576039c2006-09-28 10:58:06 +10005796 __be64 *pp, *thispa; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 xfs_bmbt_key_t *prevp, *keyp;
5798
Christoph Hellwig16259e72005-11-02 15:11:25 +11005799 ASSERT(be16_to_cpu(block->bb_level) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
5801 prevp = NULL;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005802 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 dmxr = mp->m_bmap_dmxr[0];
Christoph Hellwig136341b2008-10-30 17:11:40 +11005804 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
5806 if (prevp) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005807 ASSERT(be64_to_cpu(prevp->br_startoff) <
5808 be64_to_cpu(keyp->br_startoff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 }
5810 prevp = keyp;
5811
5812 /*
5813 * Compare the block numbers to see if there are dups.
5814 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11005815 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005816 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005817 else
5818 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5819
Christoph Hellwig16259e72005-11-02 15:11:25 +11005820 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11005821 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005822 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005823 else
5824 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005825 if (*thispa == *pp) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11005826 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005827 __func__, j, i,
Christoph Hellwig576039c2006-09-28 10:58:06 +10005828 (unsigned long long)be64_to_cpu(*thispa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 panic("%s: ptrs are equal in node\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005830 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 }
5832 }
5833 }
5834}
5835
5836/*
5837 * Check that the extents for the inode ip are in the right order in all
5838 * btree leaves.
5839 */
5840
5841STATIC void
5842xfs_bmap_check_leaf_extents(
5843 xfs_btree_cur_t *cur, /* btree cursor or null */
5844 xfs_inode_t *ip, /* incore inode pointer */
5845 int whichfork) /* data or attr fork */
5846{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005847 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 xfs_fsblock_t bno; /* block # of "block" */
5849 xfs_buf_t *bp; /* buffer for "block" */
5850 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005851 xfs_extnum_t i=0, j; /* index into the extents list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 xfs_ifork_t *ifp; /* fork structure */
5853 int level; /* btree level, for checking */
5854 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10005855 __be64 *pp; /* pointer to block address */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005856 xfs_bmbt_rec_t *ep; /* pointer to current extent */
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005857 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005858 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 int bp_release = 0;
5860
5861 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5862 return;
5863 }
5864
5865 bno = NULLFSBLOCK;
5866 mp = ip->i_mount;
5867 ifp = XFS_IFORK_PTR(ip, whichfork);
5868 block = ifp->if_broot;
5869 /*
5870 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5871 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11005872 level = be16_to_cpu(block->bb_level);
5873 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11005875 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005876 bno = be64_to_cpu(*pp);
5877
5878 ASSERT(bno != NULLDFSBNO);
5879 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5880 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5881
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 /*
5883 * Go down the tree until leaf level is reached, following the first
5884 * pointer (leftmost) at each level.
5885 */
5886 while (level-- > 0) {
5887 /* See if buf is in cur first */
5888 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5889 if (bp) {
5890 bp_release = 0;
5891 } else {
5892 bp_release = 1;
5893 }
5894 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5895 XFS_BMAP_BTREE_REF)))
5896 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005897 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11005899 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 error0);
5901 if (level == 0)
5902 break;
5903
5904 /*
5905 * Check this block for basic sanity (increasing keys and
5906 * no duplicate blocks).
5907 */
5908
5909 xfs_check_block(block, mp, 0, 0);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005910 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005911 bno = be64_to_cpu(*pp);
5912 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 if (bp_release) {
5914 bp_release = 0;
5915 xfs_trans_brelse(NULL, bp);
5916 }
5917 }
5918
5919 /*
5920 * Here with bp and block set to the leftmost leaf node in the tree.
5921 */
5922 i = 0;
5923
5924 /*
5925 * Loop over all leaf nodes checking that all extents are in the right order.
5926 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 xfs_fsblock_t nextbno;
5929 xfs_extnum_t num_recs;
5930
5931
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005932 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933
5934 /*
5935 * Read-ahead the next leaf block, if any.
5936 */
5937
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005938 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
5940 /*
5941 * Check all the extents to make sure they are OK.
5942 * If we had a previous block, the last entry should
5943 * conform with the first entry in this one.
5944 */
5945
Christoph Hellwig136341b2008-10-30 17:11:40 +11005946 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005947 if (i) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005948 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
5949 xfs_bmbt_disk_get_blockcount(&last) <=
5950 xfs_bmbt_disk_get_startoff(ep));
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005951 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005952 for (j = 1; j < num_recs; j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11005953 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005954 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
5955 xfs_bmbt_disk_get_blockcount(ep) <=
5956 xfs_bmbt_disk_get_startoff(nextp));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005957 ep = nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005960 last = *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 i += num_recs;
5962 if (bp_release) {
5963 bp_release = 0;
5964 xfs_trans_brelse(NULL, bp);
5965 }
5966 bno = nextbno;
5967 /*
5968 * If we've reached the end, stop.
5969 */
5970 if (bno == NULLFSBLOCK)
5971 break;
5972
5973 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5974 if (bp) {
5975 bp_release = 0;
5976 } else {
5977 bp_release = 1;
5978 }
5979 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5980 XFS_BMAP_BTREE_REF)))
5981 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005982 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 }
5984 if (bp_release) {
5985 bp_release = 0;
5986 xfs_trans_brelse(NULL, bp);
5987 }
5988 return;
5989
5990error0:
Dave Chinner0b932cc2011-03-07 10:08:35 +11005991 xfs_warn(mp, "%s: at error0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 if (bp_release)
5993 xfs_trans_brelse(NULL, bp);
5994error_norelse:
Dave Chinner0b932cc2011-03-07 10:08:35 +11005995 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005996 __func__, i);
5997 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 return;
5999}
6000#endif
6001
6002/*
6003 * Count fsblocks of the given fork.
6004 */
6005int /* error */
6006xfs_bmap_count_blocks(
6007 xfs_trans_t *tp, /* transaction pointer */
6008 xfs_inode_t *ip, /* incore inode */
6009 int whichfork, /* data or attr fork */
6010 int *count) /* out: count of blocks */
6011{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006012 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 xfs_fsblock_t bno; /* block # of "block" */
6014 xfs_ifork_t *ifp; /* fork structure */
6015 int level; /* btree level, for checking */
6016 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10006017 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018
6019 bno = NULLFSBLOCK;
6020 mp = ip->i_mount;
6021 ifp = XFS_IFORK_PTR(ip, whichfork);
6022 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
Ruben Porrasc94312d2008-08-13 16:52:25 +10006023 xfs_bmap_count_leaves(ifp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
Ruben Porrasc94312d2008-08-13 16:52:25 +10006025 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 return 0;
6027 }
6028
6029 /*
6030 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6031 */
6032 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11006033 level = be16_to_cpu(block->bb_level);
6034 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11006035 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006036 bno = be64_to_cpu(*pp);
6037 ASSERT(bno != NULLDFSBNO);
6038 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6039 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006041 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6043 mp);
6044 return XFS_ERROR(EFSCORRUPTED);
6045 }
6046
6047 return 0;
6048}
6049
6050/*
6051 * Recursively walks each level of a btree
6052 * to count total fsblocks is use.
6053 */
David Chinnera8272ce2007-11-23 16:28:09 +11006054STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055xfs_bmap_count_tree(
6056 xfs_mount_t *mp, /* file system mount point */
6057 xfs_trans_t *tp, /* transaction pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006058 xfs_ifork_t *ifp, /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 xfs_fsblock_t blockno, /* file system block number */
6060 int levelin, /* level in btree */
6061 int *count) /* Count of blocks */
6062{
6063 int error;
6064 xfs_buf_t *bp, *nbp;
6065 int level = levelin;
Christoph Hellwig576039c2006-09-28 10:58:06 +10006066 __be64 *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 xfs_fsblock_t bno = blockno;
6068 xfs_fsblock_t nextbno;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006069 struct xfs_btree_block *block, *nextblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071
6072 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6073 return error;
6074 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006075 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076
6077 if (--level) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +02006078 /* Not at node above leaves, count this level of nodes */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006079 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 while (nextbno != NULLFSBLOCK) {
6081 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6082 0, &nbp, XFS_BMAP_BTREE_REF)))
6083 return error;
6084 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006085 nextblock = XFS_BUF_TO_BLOCK(nbp);
6086 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 xfs_trans_brelse(tp, nbp);
6088 }
6089
6090 /* Dive to the next level */
Christoph Hellwig136341b2008-10-30 17:11:40 +11006091 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006092 bno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 if (unlikely((error =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006094 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 xfs_trans_brelse(tp, bp);
6096 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6097 XFS_ERRLEVEL_LOW, mp);
6098 return XFS_ERROR(EFSCORRUPTED);
6099 }
6100 xfs_trans_brelse(tp, bp);
6101 } else {
6102 /* count all level 1 nodes and their leaves */
6103 for (;;) {
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006104 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Christoph Hellwig16259e72005-11-02 15:11:25 +11006105 numrecs = be16_to_cpu(block->bb_numrecs);
Christoph Hellwig136341b2008-10-30 17:11:40 +11006106 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 xfs_trans_brelse(tp, bp);
6108 if (nextbno == NULLFSBLOCK)
6109 break;
6110 bno = nextbno;
6111 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6112 XFS_BMAP_BTREE_REF)))
6113 return error;
6114 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006115 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 }
6117 }
6118 return 0;
6119}
6120
6121/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006122 * Count leaf blocks given a range of extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006124STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006126 xfs_ifork_t *ifp,
6127 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 int numrecs,
6129 int *count)
6130{
6131 int b;
6132
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006133 for (b = 0; b < numrecs; b++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006134 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
Yingping Lu91e11082005-11-02 15:10:24 +11006135 *count += xfs_bmbt_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006136 }
Yingping Lu91e11082005-11-02 15:10:24 +11006137}
6138
6139/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006140 * Count leaf blocks given a range of extent records originally
6141 * in btree format.
Yingping Lu91e11082005-11-02 15:10:24 +11006142 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006143STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +11006144xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +11006145 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006146 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +11006147 int numrecs,
6148 int *count)
6149{
6150 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006151 xfs_bmbt_rec_t *frp;
Yingping Lu91e11082005-11-02 15:10:24 +11006152
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006153 for (b = 1; b <= numrecs; b++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11006154 frp = XFS_BMBT_REC_ADDR(mp, block, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 *count += xfs_bmbt_disk_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157}
Dave Chinnerc726de42010-11-30 15:14:39 +11006158
6159/*
6160 * dead simple method of punching delalyed allocation blocks from a range in
6161 * the inode. Walks a block at a time so will be slow, but is only executed in
6162 * rare error cases so the overhead is not critical. This will alays punch out
6163 * both the start and end blocks, even if the ranges only partially overlap
6164 * them, so it is up to the caller to ensure that partial blocks are not
6165 * passed in.
6166 */
6167int
6168xfs_bmap_punch_delalloc_range(
6169 struct xfs_inode *ip,
6170 xfs_fileoff_t start_fsb,
6171 xfs_fileoff_t length)
6172{
6173 xfs_fileoff_t remaining = length;
6174 int error = 0;
6175
6176 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6177
6178 do {
6179 int done;
6180 xfs_bmbt_irec_t imap;
6181 int nimaps = 1;
6182 xfs_fsblock_t firstblock;
6183 xfs_bmap_free_t flist;
6184
6185 /*
6186 * Map the range first and check that it is a delalloc extent
6187 * before trying to unmap the range. Otherwise we will be
6188 * trying to remove a real extent (which requires a
6189 * transaction) or a hole, which is probably a bad idea...
6190 */
Dave Chinner5c8ed202011-09-18 20:40:45 +00006191 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
6192 XFS_BMAPI_ENTIRE);
Dave Chinnerc726de42010-11-30 15:14:39 +11006193
6194 if (error) {
6195 /* something screwed, just bail */
6196 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner53487782011-03-07 10:05:35 +11006197 xfs_alert(ip->i_mount,
Dave Chinnerc726de42010-11-30 15:14:39 +11006198 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6199 ip->i_ino, start_fsb);
6200 }
6201 break;
6202 }
6203 if (!nimaps) {
6204 /* nothing there */
6205 goto next_block;
6206 }
6207 if (imap.br_startblock != DELAYSTARTBLOCK) {
6208 /* been converted, ignore */
6209 goto next_block;
6210 }
6211 WARN_ON(imap.br_blockcount == 0);
6212
6213 /*
6214 * Note: while we initialise the firstblock/flist pair, they
6215 * should never be used because blocks should never be
6216 * allocated or freed for a delalloc extent and hence we need
6217 * don't cancel or finish them after the xfs_bunmapi() call.
6218 */
6219 xfs_bmap_init(&flist, &firstblock);
6220 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6221 &flist, &done);
6222 if (error)
6223 break;
6224
6225 ASSERT(!flist.xbf_count && !flist.xbf_first);
6226next_block:
6227 start_fsb++;
6228 remaining--;
6229 } while(remaining > 0);
6230
6231 return error;
6232}