blob: 59f63949d3bc6ea1058bd0eae0f966ebdfaad00a [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 Hellwigf3ca87382011-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
105 * to a delayed allocation.
106 */
107STATIC int /* error */
108xfs_bmap_add_extent_hole_delay(
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 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100111 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwig54893272011-05-11 15:04:03 +0000112 int *logflagsp); /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/*
115 * Called by xfs_bmap_add_extent to handle cases converting a hole
116 * to a real allocation.
117 */
118STATIC int /* error */
119xfs_bmap_add_extent_hole_real(
120 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000121 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100123 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int *logflagsp, /* inode logging flags */
125 int whichfork); /* data or attr fork */
126
127/*
128 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
129 * allocation to a real allocation or vice versa.
130 */
131STATIC int /* error */
132xfs_bmap_add_extent_unwritten_real(
133 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000134 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100136 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000137 int *logflagsp); /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139/*
140 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
141 * It figures out where to ask the underlying allocator to put the new extent.
142 */
143STATIC int /* error */
144xfs_bmap_alloc(
145 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
146
147/*
148 * Transform a btree format file with only one leaf node, where the
149 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100150 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * give up the space for the btree root and pitch the leaf block.
152 */
153STATIC int /* error */
154xfs_bmap_btree_to_extents(
155 xfs_trans_t *tp, /* transaction pointer */
156 xfs_inode_t *ip, /* incore inode pointer */
157 xfs_btree_cur_t *cur, /* btree cursor */
158 int *logflagsp, /* inode logging flags */
159 int whichfork); /* data or attr fork */
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * Remove the entry "free" from the free item list. Prev points to the
163 * previous entry, unless "free" is the head of the list.
164 */
165STATIC void
166xfs_bmap_del_free(
167 xfs_bmap_free_t *flist, /* free item list header */
168 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
169 xfs_bmap_free_item_t *free); /* list item to be freed */
170
171/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * Convert an extents-format file into a btree-format file.
173 * The new file will have a root block (in the inode) and a single child block.
174 */
175STATIC int /* error */
176xfs_bmap_extents_to_btree(
177 xfs_trans_t *tp, /* transaction pointer */
178 xfs_inode_t *ip, /* incore inode pointer */
179 xfs_fsblock_t *firstblock, /* first-block-allocated */
180 xfs_bmap_free_t *flist, /* blocks freed in xaction */
181 xfs_btree_cur_t **curp, /* cursor returned to caller */
182 int wasdel, /* converting a delayed alloc */
183 int *logflagsp, /* inode logging flags */
184 int whichfork); /* data or attr fork */
185
186/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * Convert a local file to an extents file.
188 * This code is sort of bogus, since the file data needs to get
189 * logged so it won't be lost. The bmap-level manipulations are ok, though.
190 */
191STATIC int /* error */
192xfs_bmap_local_to_extents(
193 xfs_trans_t *tp, /* transaction pointer */
194 xfs_inode_t *ip, /* incore inode pointer */
195 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
196 xfs_extlen_t total, /* total blocks needed by transaction */
197 int *logflagsp, /* inode logging flags */
198 int whichfork); /* data or attr fork */
199
200/*
201 * Search the extents list for the inode, for the extent containing bno.
202 * If bno lies in a hole, point to the next entry. If bno lies past eof,
203 * *eofp will be set, and *prevp will contain the last entry (null if none).
204 * Else, *lastxp will be set to the index of the found
205 * entry; *gotp will contain the entry.
206 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000207STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208xfs_bmap_search_extents(
209 xfs_inode_t *ip, /* incore inode pointer */
210 xfs_fileoff_t bno, /* block number searched for */
211 int whichfork, /* data or attr fork */
212 int *eofp, /* out: end of file found */
213 xfs_extnum_t *lastxp, /* out: last extent index */
214 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
215 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
216
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000217/*
218 * Check the last inode extent to determine whether this allocation will result
219 * in blocks being allocated at the end of the file. When we allocate new data
220 * blocks at the end of the file which do not start at the previous data block,
221 * we will try to align the new blocks at stripe unit boundaries.
222 */
223STATIC int /* error */
224xfs_bmap_isaeof(
225 xfs_inode_t *ip, /* incore inode pointer */
226 xfs_fileoff_t off, /* file offset in fsblocks */
227 int whichfork, /* data or attribute fork */
228 char *aeof); /* return value */
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/*
231 * Compute the worst-case number of indirect blocks that will be used
232 * for ip's delayed extent of length "len".
233 */
234STATIC xfs_filblks_t
235xfs_bmap_worst_indlen(
236 xfs_inode_t *ip, /* incore inode pointer */
237 xfs_filblks_t len); /* delayed extent length */
238
239#ifdef DEBUG
240/*
241 * Perform various validation checks on the values being returned
242 * from xfs_bmapi().
243 */
244STATIC void
245xfs_bmap_validate_ret(
246 xfs_fileoff_t bno,
247 xfs_filblks_t len,
248 int flags,
249 xfs_bmbt_irec_t *mval,
250 int nmap,
251 int ret_nmap);
252#else
253#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
254#endif /* DEBUG */
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256STATIC int
257xfs_bmap_count_tree(
258 xfs_mount_t *mp,
259 xfs_trans_t *tp,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100260 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 xfs_fsblock_t blockno,
262 int levelin,
263 int *count);
264
Ruben Porrasc94312d2008-08-13 16:52:25 +1000265STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100267 xfs_ifork_t *ifp,
268 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 int numrecs,
270 int *count);
271
Ruben Porrasc94312d2008-08-13 16:52:25 +1000272STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +1100273xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +1100274 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100275 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +1100276 int numrecs,
277 int *count);
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279/*
280 * Bmap internal routines.
281 */
282
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100283STATIC int /* error */
284xfs_bmbt_lookup_eq(
285 struct xfs_btree_cur *cur,
286 xfs_fileoff_t off,
287 xfs_fsblock_t bno,
288 xfs_filblks_t len,
289 int *stat) /* success/failure */
290{
291 cur->bc_rec.b.br_startoff = off;
292 cur->bc_rec.b.br_startblock = bno;
293 cur->bc_rec.b.br_blockcount = len;
294 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
295}
296
297STATIC int /* error */
298xfs_bmbt_lookup_ge(
299 struct xfs_btree_cur *cur,
300 xfs_fileoff_t off,
301 xfs_fsblock_t bno,
302 xfs_filblks_t len,
303 int *stat) /* success/failure */
304{
305 cur->bc_rec.b.br_startoff = off;
306 cur->bc_rec.b.br_startblock = bno;
307 cur->bc_rec.b.br_blockcount = len;
308 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
309}
310
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100311/*
312* Update the record referred to by cur to the value given
313 * by [off, bno, len, state].
314 * This either works (return 0) or gets an EFSCORRUPTED error.
315 */
316STATIC int
317xfs_bmbt_update(
318 struct xfs_btree_cur *cur,
319 xfs_fileoff_t off,
320 xfs_fsblock_t bno,
321 xfs_filblks_t len,
322 xfs_exntst_t state)
323{
324 union xfs_btree_rec rec;
325
326 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
327 return xfs_btree_update(cur, &rec);
328}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330/*
331 * Called from xfs_bmap_add_attrfork to handle btree format files.
332 */
333STATIC int /* error */
334xfs_bmap_add_attrfork_btree(
335 xfs_trans_t *tp, /* transaction pointer */
336 xfs_inode_t *ip, /* incore inode pointer */
337 xfs_fsblock_t *firstblock, /* first block allocated */
338 xfs_bmap_free_t *flist, /* blocks to free at commit */
339 int *flags) /* inode logging flags */
340{
341 xfs_btree_cur_t *cur; /* btree cursor */
342 int error; /* error return value */
343 xfs_mount_t *mp; /* file system mount struct */
344 int stat; /* newroot status */
345
346 mp = ip->i_mount;
347 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
348 *flags |= XFS_ILOG_DBROOT;
349 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100350 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 cur->bc_private.b.flist = flist;
352 cur->bc_private.b.firstblock = *firstblock;
353 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
354 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000355 /* must be at least one entry */
356 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100357 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 goto error0;
359 if (stat == 0) {
360 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
361 return XFS_ERROR(ENOSPC);
362 }
363 *firstblock = cur->bc_private.b.firstblock;
364 cur->bc_private.b.allocated = 0;
365 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
366 }
367 return 0;
368error0:
369 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
370 return error;
371}
372
373/*
374 * Called from xfs_bmap_add_attrfork to handle extents format files.
375 */
376STATIC int /* error */
377xfs_bmap_add_attrfork_extents(
378 xfs_trans_t *tp, /* transaction pointer */
379 xfs_inode_t *ip, /* incore inode pointer */
380 xfs_fsblock_t *firstblock, /* first block allocated */
381 xfs_bmap_free_t *flist, /* blocks to free at commit */
382 int *flags) /* inode logging flags */
383{
384 xfs_btree_cur_t *cur; /* bmap btree cursor */
385 int error; /* error return value */
386
387 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
388 return 0;
389 cur = NULL;
390 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
391 flags, XFS_DATA_FORK);
392 if (cur) {
393 cur->bc_private.b.allocated = 0;
394 xfs_btree_del_cursor(cur,
395 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
396 }
397 return error;
398}
399
400/*
401 * Called from xfs_bmap_add_attrfork to handle local format files.
402 */
403STATIC int /* error */
404xfs_bmap_add_attrfork_local(
405 xfs_trans_t *tp, /* transaction pointer */
406 xfs_inode_t *ip, /* incore inode pointer */
407 xfs_fsblock_t *firstblock, /* first block allocated */
408 xfs_bmap_free_t *flist, /* blocks to free at commit */
409 int *flags) /* inode logging flags */
410{
411 xfs_da_args_t dargs; /* args for dir/attr code */
412 int error; /* error return value */
413 xfs_mount_t *mp; /* mount structure pointer */
414
415 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
416 return 0;
Al Viroabbede12011-07-26 02:31:30 -0400417 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 mp = ip->i_mount;
419 memset(&dargs, 0, sizeof(dargs));
420 dargs.dp = ip;
421 dargs.firstblock = firstblock;
422 dargs.flist = flist;
423 dargs.total = mp->m_dirblkfsbs;
424 dargs.whichfork = XFS_DATA_FORK;
425 dargs.trans = tp;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000426 error = xfs_dir2_sf_to_block(&dargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 } else
428 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
429 XFS_DATA_FORK);
430 return error;
431}
432
433/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100434 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 * after allocating space (or doing a delayed allocation).
436 */
437STATIC int /* error */
438xfs_bmap_add_extent(
Christoph Hellwigf3ca87382011-07-08 14:34:47 +0200439 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000441 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100443 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 xfs_fsblock_t *first, /* pointer to firstblock variable */
445 xfs_bmap_free_t *flist, /* list of extents to be freed */
446 int *logflagsp, /* inode logging flags */
Christoph Hellwig54893272011-05-11 15:04:03 +0000447 int whichfork) /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 xfs_btree_cur_t *cur; /* btree cursor or null */
450 xfs_filblks_t da_new; /* new count del alloc blocks used */
451 xfs_filblks_t da_old; /* old count del alloc blocks used */
452 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 xfs_ifork_t *ifp; /* inode fork ptr */
454 int logflags; /* returned value */
455 xfs_extnum_t nextents; /* number of extents in file now */
456
457 XFS_STATS_INC(xs_add_exlist);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 cur = *curp;
460 ifp = XFS_IFORK_PTR(ip, whichfork);
461 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 da_old = da_new = 0;
463 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000464
465 ASSERT(*idx >= 0);
466 ASSERT(*idx <= nextents);
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 * Any kind of new delayed allocation goes here.
470 */
Christoph Hellwige7455e02011-09-18 20:40:41 +0000471 if (isnullstartblock(new->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (cur)
473 ASSERT((cur->bc_private.b.flags &
474 XFS_BTCUR_BPRV_WASDEL) == 0);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000475 error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
476 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 /*
479 * Real allocation off the end of the file.
480 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000481 else if (*idx == nextents) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (cur)
483 ASSERT((cur->bc_private.b.flags &
484 XFS_BTCUR_BPRV_WASDEL) == 0);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000485 error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
486 &logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 } else {
488 xfs_bmbt_irec_t prev; /* old extent at offset idx */
489
490 /*
491 * Get the record referred to by idx.
492 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000493 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /*
495 * If it's a real allocation record, and the new allocation ends
496 * after the start of the referred to record, then we're filling
497 * in a delayed or unwritten allocation with a real one, or
498 * converting real back to unwritten.
499 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600500 if (!isnullstartblock(new->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 new->br_startoff + new->br_blockcount > prev.br_startoff) {
502 if (prev.br_state != XFS_EXT_UNWRITTEN &&
Eric Sandeen9d87c312009-01-14 23:22:07 -0600503 isnullstartblock(prev.br_startblock)) {
504 da_old = startblockval(prev.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (cur)
506 ASSERT(cur->bc_private.b.flags &
507 XFS_BTCUR_BPRV_WASDEL);
Christoph Hellwigf3ca87382011-07-08 14:34:47 +0200508 error = xfs_bmap_add_extent_delay_real(tp, ip,
Christoph Hellwigec90c552011-05-23 08:52:53 +0000509 idx, &cur, new, &da_new,
510 first, flist, &logflags);
511 } else {
512 ASSERT(new->br_state == XFS_EXT_NORM ||
513 new->br_state == XFS_EXT_UNWRITTEN);
514
515 error = xfs_bmap_add_extent_unwritten_real(ip,
516 idx, &cur, new, &logflags);
Christoph Hellwig54893272011-05-11 15:04:03 +0000517 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521 /*
522 * Otherwise we're filling in a hole with an allocation.
523 */
524 else {
525 if (cur)
526 ASSERT((cur->bc_private.b.flags &
527 XFS_BTCUR_BPRV_WASDEL) == 0);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000528 error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
529 new, &logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531 }
532
Christoph Hellwigec90c552011-05-23 08:52:53 +0000533 if (error)
534 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 ASSERT(*curp == cur || *curp == NULL);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /*
538 * Convert to a btree if necessary.
539 */
540 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
541 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
542 int tmp_logflags; /* partial log flag return val */
543
544 ASSERT(cur == NULL);
Christoph Hellwigf3ca87382011-07-08 14:34:47 +0200545 error = xfs_bmap_extents_to_btree(tp, ip, first,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
547 logflags |= tmp_logflags;
548 if (error)
549 goto done;
550 }
551 /*
552 * Adjust for changes in reserved delayed indirect blocks.
553 * Nothing to do for disk quotas here.
554 */
555 if (da_old || da_new) {
556 xfs_filblks_t nblks;
557
558 nblks = da_new;
559 if (cur)
560 nblks += cur->bc_private.b.allocated;
561 ASSERT(nblks <= da_old);
562 if (nblks < da_old)
Christoph Hellwig96540c72010-09-30 02:25:55 +0000563 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +0000564 (int64_t)(da_old - nblks), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566 /*
567 * Clear out the allocated field, done with it now in any case.
568 */
569 if (cur) {
570 cur->bc_private.b.allocated = 0;
571 *curp = cur;
572 }
573done:
574#ifdef DEBUG
575 if (!error)
576 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
577#endif
578 *logflagsp = logflags;
579 return error;
580}
581
582/*
583 * Called by xfs_bmap_add_extent to handle cases converting a delayed
584 * allocation to a real allocation.
585 */
586STATIC int /* error */
587xfs_bmap_add_extent_delay_real(
Christoph Hellwigf3ca87382011-07-08 14:34:47 +0200588 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000590 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100592 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
594 xfs_fsblock_t *first, /* pointer to firstblock variable */
595 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwig54893272011-05-11 15:04:03 +0000596 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 xfs_btree_cur_t *cur; /* btree cursor */
599 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000600 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100603 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 xfs_fileoff_t new_endoff; /* end offset of new entry */
605 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
606 /* left is 0, right is 1, prev is 2 */
607 int rval=0; /* return value (logging flags) */
608 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000609 xfs_filblks_t temp=0; /* value for dnew calculations */
610 xfs_filblks_t temp2=0;/* value for dnew calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 int tmp_rval; /* partial logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613#define LEFT r[0]
614#define RIGHT r[1]
615#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 /*
618 * Set up a bunch of variables to make the tests simpler.
619 */
620 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100621 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000622 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 xfs_bmbt_get_all(ep, &PREV);
624 new_endoff = new->br_startoff + new->br_blockcount;
625 ASSERT(PREV.br_startoff <= new->br_startoff);
626 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /*
629 * Set flags determining what part of the previous delayed allocation
630 * extent is being replaced by a real allocation.
631 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000632 if (PREV.br_startoff == new->br_startoff)
633 state |= BMAP_LEFT_FILLING;
634 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
635 state |= BMAP_RIGHT_FILLING;
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /*
638 * Check and set flags if this segment has a left neighbor.
639 * Don't set contiguous if the combined extent would be too large.
640 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000641 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000642 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000643 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000644
645 if (isnullstartblock(LEFT.br_startblock))
646 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000648
649 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
650 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
651 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
652 LEFT.br_state == new->br_state &&
653 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
654 state |= BMAP_LEFT_CONTIG;
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /*
657 * Check and set flags if this segment has a right neighbor.
658 * Don't set contiguous if the combined extent would be too large.
659 * Also check for all-three-contiguous being too large.
660 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000661 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000662 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000663 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000664
665 if (isnullstartblock(RIGHT.br_startblock))
666 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000668
669 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
670 new_endoff == RIGHT.br_startoff &&
671 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
672 new->br_state == RIGHT.br_state &&
673 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
674 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
675 BMAP_RIGHT_FILLING)) !=
676 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
677 BMAP_RIGHT_FILLING) ||
678 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
679 <= MAXEXTLEN))
680 state |= BMAP_RIGHT_CONTIG;
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 error = 0;
683 /*
684 * Switch out based on the FILLING and CONTIG state bits.
685 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000686 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
687 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
688 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
689 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /*
691 * Filling in all of a previously delayed allocation extent.
692 * The left and right neighbors are both contiguous with new.
693 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000694 --*idx;
695 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
696 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 LEFT.br_blockcount + PREV.br_blockcount +
698 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000699 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000700
Christoph Hellwigec90c552011-05-23 08:52:53 +0000701 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 ip->i_d.di_nextents--;
703 if (cur == NULL)
704 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
705 else {
706 rval = XFS_ILOG_CORE;
707 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
708 RIGHT.br_startblock,
709 RIGHT.br_blockcount, &i)))
710 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000711 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100712 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000714 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100715 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000717 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
719 LEFT.br_startblock,
720 LEFT.br_blockcount +
721 PREV.br_blockcount +
722 RIGHT.br_blockcount, LEFT.br_state)))
723 goto done;
724 }
725 *dnew = 0;
726 break;
727
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000728 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /*
730 * Filling in all of a previously delayed allocation extent.
731 * The left neighbor is contiguous, the right is not.
732 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000733 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000734
Christoph Hellwigec90c552011-05-23 08:52:53 +0000735 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
736 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
737 LEFT.br_blockcount + PREV.br_blockcount);
738 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
739
740 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (cur == NULL)
742 rval = XFS_ILOG_DEXT;
743 else {
744 rval = 0;
745 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
746 LEFT.br_startblock, LEFT.br_blockcount,
747 &i)))
748 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000749 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
751 LEFT.br_startblock,
752 LEFT.br_blockcount +
753 PREV.br_blockcount, LEFT.br_state)))
754 goto done;
755 }
756 *dnew = 0;
757 break;
758
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000759 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /*
761 * Filling in all of a previously delayed allocation extent.
762 * The right neighbor is contiguous, the left is not.
763 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000764 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 xfs_bmbt_set_startblock(ep, new->br_startblock);
766 xfs_bmbt_set_blockcount(ep,
767 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000768 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000769
Christoph Hellwigec90c552011-05-23 08:52:53 +0000770 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (cur == NULL)
772 rval = XFS_ILOG_DEXT;
773 else {
774 rval = 0;
775 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
776 RIGHT.br_startblock,
777 RIGHT.br_blockcount, &i)))
778 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 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
781 new->br_startblock,
782 PREV.br_blockcount +
783 RIGHT.br_blockcount, PREV.br_state)))
784 goto done;
785 }
Christoph Hellwigec90c552011-05-23 08:52:53 +0000786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 *dnew = 0;
788 break;
789
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000790 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /*
792 * Filling in all of a previously delayed allocation extent.
793 * Neither the left nor right neighbors are contiguous with
794 * the new one.
795 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000796 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000798 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 ip->i_d.di_nextents++;
801 if (cur == NULL)
802 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
803 else {
804 rval = XFS_ILOG_CORE;
805 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
806 new->br_startblock, new->br_blockcount,
807 &i)))
808 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000809 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100811 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000813 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
Christoph Hellwigec90c552011-05-23 08:52:53 +0000815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 *dnew = 0;
817 break;
818
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000819 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /*
821 * Filling in the first part of a previous delayed allocation.
822 * The left neighbor is contiguous.
823 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000824 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
825 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 LEFT.br_blockcount + new->br_blockcount);
827 xfs_bmbt_set_startoff(ep,
828 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000829 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000832 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 xfs_bmbt_set_blockcount(ep, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (cur == NULL)
835 rval = XFS_ILOG_DEXT;
836 else {
837 rval = 0;
838 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
839 LEFT.br_startblock, LEFT.br_blockcount,
840 &i)))
841 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000842 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
844 LEFT.br_startblock,
845 LEFT.br_blockcount +
846 new->br_blockcount,
847 LEFT.br_state)))
848 goto done;
849 }
850 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600851 startblockval(PREV.br_startblock));
852 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000853 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
854
855 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 *dnew = temp;
857 break;
858
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000859 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /*
861 * Filling in the first part of a previous delayed allocation.
862 * The left neighbor is not contiguous.
863 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000864 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 xfs_bmbt_set_startoff(ep, new_endoff);
866 temp = PREV.br_blockcount - new->br_blockcount;
867 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000868 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 ip->i_d.di_nextents++;
870 if (cur == NULL)
871 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
872 else {
873 rval = XFS_ILOG_CORE;
874 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
875 new->br_startblock, new->br_blockcount,
876 &i)))
877 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000878 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100880 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000882 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
885 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
Christoph Hellwigf3ca87382011-07-08 14:34:47 +0200886 error = xfs_bmap_extents_to_btree(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 first, flist, &cur, 1, &tmp_rval,
888 XFS_DATA_FORK);
889 rval |= tmp_rval;
890 if (error)
891 goto done;
892 }
893 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600894 startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 (cur ? cur->bc_private.b.allocated : 0));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000896 ep = xfs_iext_get_ext(ifp, *idx + 1);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600897 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000898 trace_xfs_bmap_post_update(ip, *idx + 1, state, _THIS_IP_);
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 *dnew = temp;
901 break;
902
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000903 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /*
905 * Filling in the last part of a previous delayed allocation.
906 * The right neighbor is contiguous with the new allocation.
907 */
908 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000909 trace_xfs_bmap_pre_update(ip, *idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000911 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100912 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 new->br_blockcount + RIGHT.br_blockcount,
914 RIGHT.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000915 trace_xfs_bmap_post_update(ip, *idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (cur == NULL)
917 rval = XFS_ILOG_DEXT;
918 else {
919 rval = 0;
920 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
921 RIGHT.br_startblock,
922 RIGHT.br_blockcount, &i)))
923 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000924 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if ((error = xfs_bmbt_update(cur, new->br_startoff,
926 new->br_startblock,
927 new->br_blockcount +
928 RIGHT.br_blockcount,
929 RIGHT.br_state)))
930 goto done;
931 }
Christoph Hellwigec90c552011-05-23 08:52:53 +0000932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600934 startblockval(PREV.br_startblock));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000935 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600936 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000937 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
938
939 ++*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 *dnew = temp;
941 break;
942
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000943 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /*
945 * Filling in the last part of a previous delayed allocation.
946 * The right neighbor is not contiguous.
947 */
948 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000949 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000951 xfs_iext_insert(ip, *idx + 1, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 ip->i_d.di_nextents++;
953 if (cur == NULL)
954 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
955 else {
956 rval = XFS_ILOG_CORE;
957 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
958 new->br_startblock, new->br_blockcount,
959 &i)))
960 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000961 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100963 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000965 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
968 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
Christoph Hellwigf3ca87382011-07-08 14:34:47 +0200969 error = xfs_bmap_extents_to_btree(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 first, flist, &cur, 1, &tmp_rval,
971 XFS_DATA_FORK);
972 rval |= tmp_rval;
973 if (error)
974 goto done;
975 }
976 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600977 startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 (cur ? cur->bc_private.b.allocated : 0));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000979 ep = xfs_iext_get_ext(ifp, *idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600980 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000981 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
982
983 ++*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 *dnew = temp;
985 break;
986
987 case 0:
988 /*
989 * Filling in the middle part of a previous delayed allocation.
990 * Contiguity is impossible here.
991 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +0000992 *
993 * We start with a delayed allocation:
994 *
995 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
996 * PREV @ idx
997 *
998 * and we are allocating:
999 * +rrrrrrrrrrrrrrrrr+
1000 * new
1001 *
1002 * and we set it up for insertion as:
1003 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1004 * new
1005 * PREV @ idx LEFT RIGHT
1006 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 */
1008 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001010 trace_xfs_bmap_pre_update(ip, *idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001011 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
1012 LEFT = *new;
1013 RIGHT.br_state = PREV.br_state;
1014 RIGHT.br_startblock = nullstartblock(
1015 (int)xfs_bmap_worst_indlen(ip, temp2));
1016 RIGHT.br_startoff = new_endoff;
1017 RIGHT.br_blockcount = temp2;
1018 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001019 xfs_iext_insert(ip, *idx + 1, 2, &LEFT, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 ip->i_d.di_nextents++;
1021 if (cur == NULL)
1022 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1023 else {
1024 rval = XFS_ILOG_CORE;
1025 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1026 new->br_startblock, new->br_blockcount,
1027 &i)))
1028 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001029 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001031 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001033 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1036 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
Christoph Hellwigf3ca87382011-07-08 14:34:47 +02001037 error = xfs_bmap_extents_to_btree(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 first, flist, &cur, 1, &tmp_rval,
1039 XFS_DATA_FORK);
1040 rval |= tmp_rval;
1041 if (error)
1042 goto done;
1043 }
1044 temp = xfs_bmap_worst_indlen(ip, temp);
1045 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001046 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 (cur ? cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00001048 if (diff > 0) {
1049 error = xfs_icsb_modify_counters(ip->i_mount,
1050 XFS_SBS_FDBLOCKS,
1051 -((int64_t)diff), 0);
1052 ASSERT(!error);
1053 if (error)
1054 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00001056
Christoph Hellwigec90c552011-05-23 08:52:53 +00001057 ep = xfs_iext_get_ext(ifp, *idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001058 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +00001059 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1060 trace_xfs_bmap_pre_update(ip, *idx + 2, state, _THIS_IP_);
1061 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001062 nullstartblock((int)temp2));
Christoph Hellwigec90c552011-05-23 08:52:53 +00001063 trace_xfs_bmap_post_update(ip, *idx + 2, state, _THIS_IP_);
1064
1065 ++*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 *dnew = temp + temp2;
1067 break;
1068
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001069 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1070 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1071 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1072 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1073 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1074 case BMAP_LEFT_CONTIG:
1075 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 /*
1077 * These cases are all impossible.
1078 */
1079 ASSERT(0);
1080 }
1081 *curp = cur;
1082done:
1083 *logflagsp = rval;
1084 return error;
1085#undef LEFT
1086#undef RIGHT
1087#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
1090/*
1091 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1092 * allocation to a real allocation or vice versa.
1093 */
1094STATIC int /* error */
1095xfs_bmap_add_extent_unwritten_real(
1096 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001097 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001099 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001100 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001103 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001106 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 xfs_fileoff_t new_endoff; /* end offset of new entry */
1108 xfs_exntst_t newext; /* new extent state */
1109 xfs_exntst_t oldext; /* old extent state */
1110 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1111 /* left is 0, right is 1, prev is 2 */
1112 int rval=0; /* return value (logging flags) */
1113 int state = 0;/* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115#define LEFT r[0]
1116#define RIGHT r[1]
1117#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 /*
1119 * Set up a bunch of variables to make the tests simpler.
1120 */
1121 error = 0;
1122 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001123 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001124 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 xfs_bmbt_get_all(ep, &PREV);
1126 newext = new->br_state;
1127 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1128 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1129 ASSERT(PREV.br_state == oldext);
1130 new_endoff = new->br_startoff + new->br_blockcount;
1131 ASSERT(PREV.br_startoff <= new->br_startoff);
1132 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 /*
1135 * Set flags determining what part of the previous oldext allocation
1136 * extent is being replaced by a newext allocation.
1137 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001138 if (PREV.br_startoff == new->br_startoff)
1139 state |= BMAP_LEFT_FILLING;
1140 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1141 state |= BMAP_RIGHT_FILLING;
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 /*
1144 * Check and set flags if this segment has a left neighbor.
1145 * Don't set contiguous if the combined extent would be too large.
1146 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001147 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001148 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001149 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001150
1151 if (isnullstartblock(LEFT.br_startblock))
1152 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001154
1155 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1156 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1157 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1158 LEFT.br_state == newext &&
1159 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1160 state |= BMAP_LEFT_CONTIG;
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 /*
1163 * Check and set flags if this segment has a right neighbor.
1164 * Don't set contiguous if the combined extent would be too large.
1165 * Also check for all-three-contiguous being too large.
1166 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001167 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001168 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001169 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001170 if (isnullstartblock(RIGHT.br_startblock))
1171 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001173
1174 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1175 new_endoff == RIGHT.br_startoff &&
1176 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1177 newext == RIGHT.br_state &&
1178 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1179 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1180 BMAP_RIGHT_FILLING)) !=
1181 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1182 BMAP_RIGHT_FILLING) ||
1183 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1184 <= MAXEXTLEN))
1185 state |= BMAP_RIGHT_CONTIG;
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 /*
1188 * Switch out based on the FILLING and CONTIG state bits.
1189 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001190 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1191 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1192 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1193 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /*
1195 * Setting all of a previous oldext extent to newext.
1196 * The left and right neighbors are both contiguous with new.
1197 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001198 --*idx;
1199
1200 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1201 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 LEFT.br_blockcount + PREV.br_blockcount +
1203 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001204 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001205
Christoph Hellwigec90c552011-05-23 08:52:53 +00001206 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 ip->i_d.di_nextents -= 2;
1208 if (cur == NULL)
1209 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1210 else {
1211 rval = XFS_ILOG_CORE;
1212 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1213 RIGHT.br_startblock,
1214 RIGHT.br_blockcount, &i)))
1215 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001216 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001217 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001219 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001220 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001222 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001223 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001225 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001226 if ((error = xfs_btree_decrement(cur, 0, &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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1230 LEFT.br_startblock,
1231 LEFT.br_blockcount + PREV.br_blockcount +
1232 RIGHT.br_blockcount, LEFT.br_state)))
1233 goto done;
1234 }
1235 break;
1236
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001237 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 /*
1239 * Setting all of a previous oldext extent to newext.
1240 * The left neighbor is contiguous, the right is not.
1241 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001242 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001243
Christoph Hellwigec90c552011-05-23 08:52:53 +00001244 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1245 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1246 LEFT.br_blockcount + PREV.br_blockcount);
1247 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1248
1249 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 ip->i_d.di_nextents--;
1251 if (cur == NULL)
1252 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1253 else {
1254 rval = XFS_ILOG_CORE;
1255 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1256 PREV.br_startblock, PREV.br_blockcount,
1257 &i)))
1258 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001259 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001260 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001262 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001263 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001265 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1267 LEFT.br_startblock,
1268 LEFT.br_blockcount + PREV.br_blockcount,
1269 LEFT.br_state)))
1270 goto done;
1271 }
1272 break;
1273
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001274 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 /*
1276 * Setting all of a previous oldext extent to newext.
1277 * The right neighbor is contiguous, the left is not.
1278 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001279 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 xfs_bmbt_set_blockcount(ep,
1281 PREV.br_blockcount + RIGHT.br_blockcount);
1282 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001283 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1284 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 ip->i_d.di_nextents--;
1286 if (cur == NULL)
1287 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1288 else {
1289 rval = XFS_ILOG_CORE;
1290 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1291 RIGHT.br_startblock,
1292 RIGHT.br_blockcount, &i)))
1293 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001294 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001295 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001297 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001298 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001300 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1302 new->br_startblock,
1303 new->br_blockcount + RIGHT.br_blockcount,
1304 newext)))
1305 goto done;
1306 }
1307 break;
1308
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001309 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 /*
1311 * Setting all of a previous oldext extent to newext.
1312 * Neither the left nor right neighbors are contiguous with
1313 * the new one.
1314 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001315 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001317 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (cur == NULL)
1320 rval = XFS_ILOG_DEXT;
1321 else {
1322 rval = 0;
1323 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1324 new->br_startblock, new->br_blockcount,
1325 &i)))
1326 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001327 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1329 new->br_startblock, new->br_blockcount,
1330 newext)))
1331 goto done;
1332 }
1333 break;
1334
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001335 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 /*
1337 * Setting the first part of a previous oldext extent to newext.
1338 * The left neighbor is contiguous.
1339 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001340 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
1341 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 LEFT.br_blockcount + new->br_blockcount);
1343 xfs_bmbt_set_startoff(ep,
1344 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001345 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001346
Christoph Hellwigec90c552011-05-23 08:52:53 +00001347 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 xfs_bmbt_set_startblock(ep,
1349 new->br_startblock + new->br_blockcount);
1350 xfs_bmbt_set_blockcount(ep,
1351 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001352 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001353
Christoph Hellwigec90c552011-05-23 08:52:53 +00001354 --*idx;
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (cur == NULL)
1357 rval = XFS_ILOG_DEXT;
1358 else {
1359 rval = 0;
1360 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1361 PREV.br_startblock, PREV.br_blockcount,
1362 &i)))
1363 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001364 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if ((error = xfs_bmbt_update(cur,
1366 PREV.br_startoff + new->br_blockcount,
1367 PREV.br_startblock + new->br_blockcount,
1368 PREV.br_blockcount - new->br_blockcount,
1369 oldext)))
1370 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001371 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 goto done;
1373 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1374 LEFT.br_startblock,
1375 LEFT.br_blockcount + new->br_blockcount,
1376 LEFT.br_state))
1377 goto done;
1378 }
1379 break;
1380
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001381 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /*
1383 * Setting the first part of a previous oldext extent to newext.
1384 * The left neighbor is not contiguous.
1385 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001386 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1388 xfs_bmbt_set_startoff(ep, new_endoff);
1389 xfs_bmbt_set_blockcount(ep,
1390 PREV.br_blockcount - new->br_blockcount);
1391 xfs_bmbt_set_startblock(ep,
1392 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001393 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001394
Christoph Hellwigec90c552011-05-23 08:52:53 +00001395 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 ip->i_d.di_nextents++;
1397 if (cur == NULL)
1398 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1399 else {
1400 rval = XFS_ILOG_CORE;
1401 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1402 PREV.br_startblock, PREV.br_blockcount,
1403 &i)))
1404 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001405 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if ((error = xfs_bmbt_update(cur,
1407 PREV.br_startoff + new->br_blockcount,
1408 PREV.br_startblock + new->br_blockcount,
1409 PREV.br_blockcount - new->br_blockcount,
1410 oldext)))
1411 goto done;
1412 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001413 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001415 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417 break;
1418
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001419 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 /*
1421 * Setting the last part of a previous oldext extent to newext.
1422 * The right neighbor is contiguous with the new allocation.
1423 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001424 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 xfs_bmbt_set_blockcount(ep,
1426 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001427 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1428
1429 ++*idx;
1430
1431 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1432 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001433 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 new->br_blockcount + RIGHT.br_blockcount, newext);
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (cur == NULL)
1438 rval = XFS_ILOG_DEXT;
1439 else {
1440 rval = 0;
1441 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1442 PREV.br_startblock,
1443 PREV.br_blockcount, &i)))
1444 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001445 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1447 PREV.br_startblock,
1448 PREV.br_blockcount - new->br_blockcount,
1449 oldext)))
1450 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001451 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 goto done;
1453 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1454 new->br_startblock,
1455 new->br_blockcount + RIGHT.br_blockcount,
1456 newext)))
1457 goto done;
1458 }
1459 break;
1460
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001461 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /*
1463 * Setting the last part of a previous oldext extent to newext.
1464 * The right neighbor is not contiguous.
1465 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001466 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 xfs_bmbt_set_blockcount(ep,
1468 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001469 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001470
Christoph Hellwigec90c552011-05-23 08:52:53 +00001471 ++*idx;
1472 xfs_iext_insert(ip, *idx, 1, new, state);
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 ip->i_d.di_nextents++;
1475 if (cur == NULL)
1476 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1477 else {
1478 rval = XFS_ILOG_CORE;
1479 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1480 PREV.br_startblock, PREV.br_blockcount,
1481 &i)))
1482 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001483 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1485 PREV.br_startblock,
1486 PREV.br_blockcount - new->br_blockcount,
1487 oldext)))
1488 goto done;
1489 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1490 new->br_startblock, new->br_blockcount,
1491 &i)))
1492 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001493 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001495 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 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 }
1499 break;
1500
1501 case 0:
1502 /*
1503 * Setting the middle part of a previous oldext extent to
1504 * newext. Contiguity is impossible here.
1505 * One extent becomes three extents.
1506 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001507 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 xfs_bmbt_set_blockcount(ep,
1509 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001510 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 r[0] = *new;
1513 r[1].br_startoff = new_endoff;
1514 r[1].br_blockcount =
1515 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1516 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1517 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001518
1519 ++*idx;
1520 xfs_iext_insert(ip, *idx, 2, &r[0], state);
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 ip->i_d.di_nextents += 2;
1523 if (cur == NULL)
1524 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1525 else {
1526 rval = XFS_ILOG_CORE;
1527 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1528 PREV.br_startblock, PREV.br_blockcount,
1529 &i)))
1530 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001531 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 /* new right extent - oldext */
1533 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1534 r[1].br_startblock, r[1].br_blockcount,
1535 r[1].br_state)))
1536 goto done;
1537 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10001539 cur->bc_rec.b.br_blockcount =
1540 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001541 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001543 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001544 /*
1545 * Reset the cursor to the position of the new extent
1546 * we are about to insert as we can't trust it after
1547 * the previous insert.
1548 */
1549 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1550 new->br_startblock, new->br_blockcount,
1551 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001553 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001555 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001556 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001558 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
1560 break;
1561
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001562 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1563 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1564 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1565 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1566 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1567 case BMAP_LEFT_CONTIG:
1568 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 /*
1570 * These cases are all impossible.
1571 */
1572 ASSERT(0);
1573 }
1574 *curp = cur;
1575done:
1576 *logflagsp = rval;
1577 return error;
1578#undef LEFT
1579#undef RIGHT
1580#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
1583/*
1584 * Called by xfs_bmap_add_extent to handle cases converting a hole
1585 * to a delayed allocation.
1586 */
1587/*ARGSUSED*/
1588STATIC int /* error */
1589xfs_bmap_add_extent_hole_delay(
1590 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001591 xfs_extnum_t *idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001592 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwig54893272011-05-11 15:04:03 +00001593 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001595 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1597 xfs_filblks_t newlen=0; /* new indirect size */
1598 xfs_filblks_t oldlen=0; /* old indirect size */
1599 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1600 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001601 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001603 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001605 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 /*
1608 * Check and set flags if this segment has a left neighbor
1609 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001610 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001611 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001612 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001613
1614 if (isnullstartblock(left.br_startblock))
1615 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 /*
1619 * Check and set flags if the current (right) segment exists.
1620 * If it doesn't exist, we're converting the hole at end-of-file.
1621 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001622 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001623 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00001624 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001625
1626 if (isnullstartblock(right.br_startblock))
1627 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 /*
1631 * Set contiguity flags on the left and right neighbors.
1632 * Don't let extents get too large, even if the pieces are contiguous.
1633 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001634 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1635 left.br_startoff + left.br_blockcount == new->br_startoff &&
1636 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1637 state |= BMAP_LEFT_CONTIG;
1638
1639 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1640 new->br_startoff + new->br_blockcount == right.br_startoff &&
1641 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1642 (!(state & BMAP_LEFT_CONTIG) ||
1643 (left.br_blockcount + new->br_blockcount +
1644 right.br_blockcount <= MAXEXTLEN)))
1645 state |= BMAP_RIGHT_CONTIG;
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 /*
1648 * Switch out based on the contiguity flags.
1649 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001650 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1651 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 /*
1653 * New allocation is contiguous with delayed allocations
1654 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001655 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001657 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 temp = left.br_blockcount + new->br_blockcount +
1659 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001660
Christoph Hellwigec90c552011-05-23 08:52:53 +00001661 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1662 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001663 oldlen = startblockval(left.br_startblock) +
1664 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 Hellwigec90c552011-05-23 08:52:53 +00001667 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001668 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00001669 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001670
Christoph Hellwigec90c552011-05-23 08:52:53 +00001671 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 break;
1673
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001674 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 /*
1676 * New allocation is contiguous with a delayed allocation
1677 * on the left.
1678 * Merge the new allocation with the left neighbor.
1679 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001680 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001682
1683 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1684 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001685 oldlen = startblockval(left.br_startblock) +
1686 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001688 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001689 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00001690 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 break;
1692
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001693 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 /*
1695 * New allocation is contiguous with a delayed allocation
1696 * on the right.
1697 * Merge the new allocation with the right neighbor.
1698 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001699 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001701 oldlen = startblockval(new->br_startblock) +
1702 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00001704 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1705 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06001706 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001707 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 break;
1709
1710 case 0:
1711 /*
1712 * New allocation is not contiguous with another
1713 * delayed allocation.
1714 * Insert a new entry.
1715 */
1716 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001717 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 break;
1719 }
1720 if (oldlen != newlen) {
1721 ASSERT(oldlen > newlen);
Christoph Hellwig96540c72010-09-30 02:25:55 +00001722 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00001723 (int64_t)(oldlen - newlen), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 /*
1725 * Nothing to do for disk quota accounting here.
1726 */
1727 }
1728 *logflagsp = 0;
1729 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
1732/*
1733 * Called by xfs_bmap_add_extent to handle cases converting a hole
1734 * to a real allocation.
1735 */
1736STATIC int /* error */
1737xfs_bmap_add_extent_hole_real(
1738 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001739 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001741 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 int *logflagsp, /* inode logging flags */
1743 int whichfork) /* data or attr fork */
1744{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 int i; /* temp state */
1747 xfs_ifork_t *ifp; /* inode fork pointer */
1748 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1749 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001750 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001754 ASSERT(*idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 state = 0;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001756
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001757 if (whichfork == XFS_ATTR_FORK)
1758 state |= BMAP_ATTRFORK;
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 /*
1761 * Check and set flags if this segment has a left neighbor.
1762 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001763 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001764 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001765 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001766 if (isnullstartblock(left.br_startblock))
1767 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 /*
1771 * Check and set flags if this segment has a current value.
1772 * Not true if we're inserting into the "hole" at eof.
1773 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001774 if (*idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001775 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00001776 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001777 if (isnullstartblock(right.br_startblock))
1778 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 /*
1782 * We're inserting a real allocation between "left" and "right".
1783 * Set the contiguity flags. Don't let extents get too large.
1784 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001785 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1786 left.br_startoff + left.br_blockcount == new->br_startoff &&
1787 left.br_startblock + left.br_blockcount == new->br_startblock &&
1788 left.br_state == new->br_state &&
1789 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1790 state |= BMAP_LEFT_CONTIG;
1791
1792 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1793 new->br_startoff + new->br_blockcount == right.br_startoff &&
1794 new->br_startblock + new->br_blockcount == right.br_startblock &&
1795 new->br_state == right.br_state &&
1796 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1797 (!(state & BMAP_LEFT_CONTIG) ||
1798 left.br_blockcount + new->br_blockcount +
1799 right.br_blockcount <= MAXEXTLEN))
1800 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001802 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 /*
1804 * Select which case we're in here, and implement it.
1805 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001806 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1807 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 /*
1809 * New allocation is contiguous with real allocations on the
1810 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001811 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001813 --*idx;
1814 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1815 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 left.br_blockcount + new->br_blockcount +
1817 right.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001818 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001819
Christoph Hellwigec90c552011-05-23 08:52:53 +00001820 xfs_iext_remove(ip, *idx + 1, 1, state);
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 XFS_IFORK_NEXT_SET(ip, whichfork,
1823 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1824 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001825 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001826 } else {
1827 rval = XFS_ILOG_CORE;
1828 if ((error = xfs_bmbt_lookup_eq(cur,
1829 right.br_startoff,
1830 right.br_startblock,
1831 right.br_blockcount, &i)))
1832 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001833 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001834 if ((error = xfs_btree_delete(cur, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001835 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001836 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001837 if ((error = xfs_btree_decrement(cur, 0, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001838 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001839 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001840 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1841 left.br_startblock,
1842 left.br_blockcount +
1843 new->br_blockcount +
1844 right.br_blockcount,
1845 left.br_state)))
1846 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001848 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001850 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 /*
1852 * New allocation is contiguous with a real allocation
1853 * on the left.
1854 * Merge the new allocation with the left neighbor.
1855 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001856 --*idx;
1857 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1858 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 left.br_blockcount + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001860 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001863 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001864 } else {
1865 rval = 0;
1866 if ((error = xfs_bmbt_lookup_eq(cur,
1867 left.br_startoff,
1868 left.br_startblock,
1869 left.br_blockcount, &i)))
1870 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001871 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001872 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1873 left.br_startblock,
1874 left.br_blockcount +
1875 new->br_blockcount,
1876 left.br_state)))
1877 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001879 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001881 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 /*
1883 * New allocation is contiguous with a real allocation
1884 * on the right.
1885 * Merge the new allocation with the right neighbor.
1886 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001887 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00001888 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1889 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 new->br_blockcount + right.br_blockcount,
1891 right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001892 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001895 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001896 } else {
1897 rval = 0;
1898 if ((error = xfs_bmbt_lookup_eq(cur,
1899 right.br_startoff,
1900 right.br_startblock,
1901 right.br_blockcount, &i)))
1902 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001903 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001904 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1905 new->br_startblock,
1906 new->br_blockcount +
1907 right.br_blockcount,
1908 right.br_state)))
1909 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001911 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 case 0:
1914 /*
1915 * New allocation is not contiguous with another
1916 * real allocation.
1917 * Insert a new entry.
1918 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001919 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 XFS_IFORK_NEXT_SET(ip, whichfork,
1921 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
1922 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001923 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001924 } else {
1925 rval = XFS_ILOG_CORE;
1926 if ((error = xfs_bmbt_lookup_eq(cur,
1927 new->br_startoff,
1928 new->br_startblock,
1929 new->br_blockcount, &i)))
1930 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001931 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001932 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001933 if ((error = xfs_btree_insert(cur, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001934 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001935 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001937 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001939done:
1940 *logflagsp = rval;
1941 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
Nathan Scottdd9f4382006-01-11 15:28:28 +11001944/*
1945 * Adjust the size of the new extent based on di_extsize and rt extsize.
1946 */
1947STATIC int
1948xfs_bmap_extsize_align(
1949 xfs_mount_t *mp,
1950 xfs_bmbt_irec_t *gotp, /* next extent pointer */
1951 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
1952 xfs_extlen_t extsz, /* align to this extent size */
1953 int rt, /* is this a realtime inode? */
1954 int eof, /* is extent at end-of-file? */
1955 int delay, /* creating delalloc extent? */
1956 int convert, /* overwriting unwritten extent? */
1957 xfs_fileoff_t *offp, /* in/out: aligned offset */
1958 xfs_extlen_t *lenp) /* in/out: aligned length */
1959{
1960 xfs_fileoff_t orig_off; /* original offset */
1961 xfs_extlen_t orig_alen; /* original length */
1962 xfs_fileoff_t orig_end; /* original off+len */
1963 xfs_fileoff_t nexto; /* next file offset */
1964 xfs_fileoff_t prevo; /* previous file offset */
1965 xfs_fileoff_t align_off; /* temp for offset */
1966 xfs_extlen_t align_alen; /* temp for length */
1967 xfs_extlen_t temp; /* temp for calculations */
1968
1969 if (convert)
1970 return 0;
1971
1972 orig_off = align_off = *offp;
1973 orig_alen = align_alen = *lenp;
1974 orig_end = orig_off + orig_alen;
1975
1976 /*
1977 * If this request overlaps an existing extent, then don't
1978 * attempt to perform any additional alignment.
1979 */
1980 if (!delay && !eof &&
1981 (orig_off >= gotp->br_startoff) &&
1982 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
1983 return 0;
1984 }
1985
1986 /*
1987 * If the file offset is unaligned vs. the extent size
1988 * we need to align it. This will be possible unless
1989 * the file was previously written with a kernel that didn't
1990 * perform this alignment, or if a truncate shot us in the
1991 * foot.
1992 */
1993 temp = do_mod(orig_off, extsz);
1994 if (temp) {
1995 align_alen += temp;
1996 align_off -= temp;
1997 }
1998 /*
1999 * Same adjustment for the end of the requested area.
2000 */
2001 if ((temp = (align_alen % extsz))) {
2002 align_alen += extsz - temp;
2003 }
2004 /*
2005 * If the previous block overlaps with this proposed allocation
2006 * then move the start forward without adjusting the length.
2007 */
2008 if (prevp->br_startoff != NULLFILEOFF) {
2009 if (prevp->br_startblock == HOLESTARTBLOCK)
2010 prevo = prevp->br_startoff;
2011 else
2012 prevo = prevp->br_startoff + prevp->br_blockcount;
2013 } else
2014 prevo = 0;
2015 if (align_off != orig_off && align_off < prevo)
2016 align_off = prevo;
2017 /*
2018 * If the next block overlaps with this proposed allocation
2019 * then move the start back without adjusting the length,
2020 * but not before offset 0.
2021 * This may of course make the start overlap previous block,
2022 * and if we hit the offset 0 limit then the next block
2023 * can still overlap too.
2024 */
2025 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2026 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2027 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2028 nexto = gotp->br_startoff + gotp->br_blockcount;
2029 else
2030 nexto = gotp->br_startoff;
2031 } else
2032 nexto = NULLFILEOFF;
2033 if (!eof &&
2034 align_off + align_alen != orig_end &&
2035 align_off + align_alen > nexto)
2036 align_off = nexto > align_alen ? nexto - align_alen : 0;
2037 /*
2038 * If we're now overlapping the next or previous extent that
2039 * means we can't fit an extsz piece in this hole. Just move
2040 * the start forward to the first valid spot and set
2041 * the length so we hit the end.
2042 */
2043 if (align_off != orig_off && align_off < prevo)
2044 align_off = prevo;
2045 if (align_off + align_alen != orig_end &&
2046 align_off + align_alen > nexto &&
2047 nexto != NULLFILEOFF) {
2048 ASSERT(nexto > prevo);
2049 align_alen = nexto - align_off;
2050 }
2051
2052 /*
2053 * If realtime, and the result isn't a multiple of the realtime
2054 * extent size we need to remove blocks until it is.
2055 */
2056 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2057 /*
2058 * We're not covering the original request, or
2059 * we won't be able to once we fix the length.
2060 */
2061 if (orig_off < align_off ||
2062 orig_end > align_off + align_alen ||
2063 align_alen - temp < orig_alen)
2064 return XFS_ERROR(EINVAL);
2065 /*
2066 * Try to fix it by moving the start up.
2067 */
2068 if (align_off + temp <= orig_off) {
2069 align_alen -= temp;
2070 align_off += temp;
2071 }
2072 /*
2073 * Try to fix it by moving the end in.
2074 */
2075 else if (align_off + align_alen - temp >= orig_end)
2076 align_alen -= temp;
2077 /*
2078 * Set the start to the minimum then trim the length.
2079 */
2080 else {
2081 align_alen -= orig_off - align_off;
2082 align_off = orig_off;
2083 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2084 }
2085 /*
2086 * Result doesn't cover the request, fail it.
2087 */
2088 if (orig_off < align_off || orig_end > align_off + align_alen)
2089 return XFS_ERROR(EINVAL);
2090 } else {
2091 ASSERT(orig_off >= align_off);
2092 ASSERT(orig_end <= align_off + align_alen);
2093 }
2094
2095#ifdef DEBUG
2096 if (!eof && gotp->br_startoff != NULLFILEOFF)
2097 ASSERT(align_off + align_alen <= gotp->br_startoff);
2098 if (prevp->br_startoff != NULLFILEOFF)
2099 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2100#endif
2101
2102 *lenp = align_alen;
2103 *offp = align_off;
2104 return 0;
2105}
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107#define XFS_ALLOC_GAP_UNITS 4
2108
David Chinnerc2b1cba2008-04-10 12:21:40 +10002109STATIC void
Nathan Scotta365bdd2006-03-14 13:34:16 +11002110xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2112{
2113 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2115 xfs_mount_t *mp; /* mount point structure */
2116 int nullfb; /* true if ap->firstblock isn't set */
2117 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119#define ISVALID(x,y) \
2120 (rt ? \
2121 (x) < mp->m_sb.sb_rblocks : \
2122 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2123 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2124 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 mp = ap->ip->i_mount;
2127 nullfb = ap->firstblock == NULLFSBLOCK;
2128 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2129 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 /*
2131 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002132 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 */
2134 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002135 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2137 ap->prevp->br_startblock)) {
2138 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2139 /*
2140 * Adjust for the gap between prevp and us.
2141 */
2142 adjust = ap->off -
2143 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2144 if (adjust &&
2145 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2146 ap->rval += adjust;
2147 }
2148 /*
2149 * If not at eof, then compare the two neighbor blocks.
2150 * Figure out whether either one gives us a good starting point,
2151 * and pick the better one.
2152 */
2153 else if (!ap->eof) {
2154 xfs_fsblock_t gotbno; /* right side block number */
2155 xfs_fsblock_t gotdiff=0; /* right side difference */
2156 xfs_fsblock_t prevbno; /* left side block number */
2157 xfs_fsblock_t prevdiff=0; /* left side difference */
2158
2159 /*
2160 * If there's a previous (left) block, select a requested
2161 * start block based on it.
2162 */
2163 if (ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002164 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 (prevbno = ap->prevp->br_startblock +
2166 ap->prevp->br_blockcount) &&
2167 ISVALID(prevbno, ap->prevp->br_startblock)) {
2168 /*
2169 * Calculate gap to end of previous block.
2170 */
2171 adjust = prevdiff = ap->off -
2172 (ap->prevp->br_startoff +
2173 ap->prevp->br_blockcount);
2174 /*
2175 * Figure the startblock based on the previous block's
2176 * end and the gap size.
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 end of the previous block.
2181 */
2182 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2183 ISVALID(prevbno + prevdiff,
2184 ap->prevp->br_startblock))
2185 prevbno += adjust;
2186 else
2187 prevdiff += adjust;
2188 /*
2189 * If the firstblock forbids it, can't use it,
2190 * must use default.
2191 */
2192 if (!rt && !nullfb &&
2193 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2194 prevbno = NULLFSBLOCK;
2195 }
2196 /*
2197 * No previous block or can't follow it, just default.
2198 */
2199 else
2200 prevbno = NULLFSBLOCK;
2201 /*
2202 * If there's a following (right) block, select a requested
2203 * start block based on it.
2204 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002205 if (!isnullstartblock(ap->gotp->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 /*
2207 * Calculate gap to start of next block.
2208 */
2209 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2210 /*
2211 * Figure the startblock based on the next block's
2212 * start and the gap size.
2213 */
2214 gotbno = ap->gotp->br_startblock;
2215 /*
2216 * Heuristic!
2217 * If the gap is large relative to the piece we're
2218 * allocating, or using it gives us an invalid block
2219 * number, then just use the start of the next block
2220 * offset by our length.
2221 */
2222 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2223 ISVALID(gotbno - gotdiff, gotbno))
2224 gotbno -= adjust;
2225 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2226 gotbno -= ap->alen;
2227 gotdiff += adjust - ap->alen;
2228 } else
2229 gotdiff += adjust;
2230 /*
2231 * If the firstblock forbids it, can't use it,
2232 * must use default.
2233 */
2234 if (!rt && !nullfb &&
2235 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2236 gotbno = NULLFSBLOCK;
2237 }
2238 /*
2239 * No next block, just default.
2240 */
2241 else
2242 gotbno = NULLFSBLOCK;
2243 /*
2244 * If both valid, pick the better one, else the only good
2245 * one, else ap->rval is already set (to 0 or the inode block).
2246 */
2247 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2248 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2249 else if (prevbno != NULLFSBLOCK)
2250 ap->rval = prevbno;
2251 else if (gotbno != NULLFSBLOCK)
2252 ap->rval = gotbno;
2253 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002254#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11002255}
2256
2257STATIC int
2258xfs_bmap_rtalloc(
2259 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2260{
2261 xfs_alloctype_t atype = 0; /* type for allocation routines */
2262 int error; /* error return value */
2263 xfs_mount_t *mp; /* mount point structure */
2264 xfs_extlen_t prod = 0; /* product factor for allocators */
2265 xfs_extlen_t ralen = 0; /* realtime allocation length */
2266 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002267 xfs_rtblock_t rtb;
2268
2269 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002270 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002271 prod = align / mp->m_sb.sb_rextsize;
2272 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2273 align, 1, ap->eof, 0,
2274 ap->conv, &ap->off, &ap->alen);
2275 if (error)
2276 return error;
2277 ASSERT(ap->alen);
2278 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2279
2280 /*
2281 * If the offset & length are not perfectly aligned
2282 * then kill prod, it will just get us in trouble.
2283 */
2284 if (do_mod(ap->off, align) || ap->alen % align)
2285 prod = 1;
2286 /*
2287 * Set ralen to be the actual requested length in rtextents.
2288 */
2289 ralen = ap->alen / mp->m_sb.sb_rextsize;
2290 /*
2291 * If the old value was close enough to MAXEXTLEN that
2292 * we rounded up to it, cut it back so it's valid again.
2293 * Note that if it's a really large request (bigger than
2294 * MAXEXTLEN), we don't hear about that number, and can't
2295 * adjust the starting point to match it.
2296 */
2297 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2298 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
Christoph Hellwig04e99452011-01-25 09:06:19 +00002299
2300 /*
2301 * Lock out other modifications to the RT bitmap inode.
2302 */
Christoph Hellwig1050c712011-02-13 13:25:31 +00002303 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
2304 xfs_trans_ijoin_ref(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Christoph Hellwig04e99452011-01-25 09:06:19 +00002305
Nathan Scotta365bdd2006-03-14 13:34:16 +11002306 /*
2307 * If it's an allocation to an empty file at offset 0,
2308 * pick an extent that will space things out in the rt area.
2309 */
2310 if (ap->eof && ap->off == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10002311 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2312
Nathan Scotta365bdd2006-03-14 13:34:16 +11002313 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2314 if (error)
2315 return error;
2316 ap->rval = rtx * mp->m_sb.sb_rextsize;
2317 } else {
2318 ap->rval = 0;
2319 }
2320
2321 xfs_bmap_adjacent(ap);
2322
2323 /*
2324 * Realtime allocation, done through xfs_rtallocate_extent.
2325 */
2326 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2327 do_div(ap->rval, mp->m_sb.sb_rextsize);
2328 rtb = ap->rval;
2329 ap->alen = ralen;
2330 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2331 &ralen, atype, ap->wasdel, prod, &rtb)))
2332 return error;
2333 if (rtb == NULLFSBLOCK && prod > 1 &&
2334 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2335 ap->alen, &ralen, atype,
2336 ap->wasdel, 1, &rtb)))
2337 return error;
2338 ap->rval = rtb;
2339 if (ap->rval != NULLFSBLOCK) {
2340 ap->rval *= mp->m_sb.sb_rextsize;
2341 ralen *= mp->m_sb.sb_rextsize;
2342 ap->alen = ralen;
2343 ap->ip->i_d.di_nblocks += ralen;
2344 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2345 if (ap->wasdel)
2346 ap->ip->i_delayed_blks -= ralen;
2347 /*
2348 * Adjust the disk quota also. This was reserved
2349 * earlier.
2350 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002351 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002352 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2353 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2354 } else {
2355 ap->alen = 0;
2356 }
2357 return 0;
2358}
2359
2360STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00002361xfs_bmap_btalloc_nullfb(
2362 struct xfs_bmalloca *ap,
2363 struct xfs_alloc_arg *args,
2364 xfs_extlen_t *blen)
2365{
2366 struct xfs_mount *mp = ap->ip->i_mount;
2367 struct xfs_perag *pag;
2368 xfs_agnumber_t ag, startag;
2369 int notinit = 0;
2370 int error;
2371
2372 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2373 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2374 else
2375 args->type = XFS_ALLOCTYPE_START_BNO;
2376 args->total = ap->total;
2377
2378 /*
2379 * Search for an allocation group with a single extent large enough
2380 * for the request. If one isn't found, then adjust the minimum
2381 * allocation size to the largest space found.
2382 */
2383 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2384 if (startag == NULLAGNUMBER)
2385 startag = ag = 0;
2386
2387 pag = xfs_perag_get(mp, ag);
Dave Chinner14b064c2011-01-27 12:16:28 +11002388 while (*blen < args->maxlen) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00002389 if (!pag->pagf_init) {
2390 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2391 XFS_ALLOC_FLAG_TRYLOCK);
2392 if (error) {
2393 xfs_perag_put(pag);
2394 return error;
2395 }
2396 }
2397
2398 /*
2399 * See xfs_alloc_fix_freelist...
2400 */
2401 if (pag->pagf_init) {
2402 xfs_extlen_t longest;
2403 longest = xfs_alloc_longest_free_extent(mp, pag);
2404 if (*blen < longest)
2405 *blen = longest;
2406 } else
2407 notinit = 1;
2408
2409 if (xfs_inode_is_filestream(ap->ip)) {
Dave Chinner14b064c2011-01-27 12:16:28 +11002410 if (*blen >= args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00002411 break;
2412
2413 if (ap->userdata) {
2414 /*
2415 * If startag is an invalid AG, we've
2416 * come here once before and
2417 * xfs_filestream_new_ag picked the
2418 * best currently available.
2419 *
2420 * Don't continue looping, since we
2421 * could loop forever.
2422 */
2423 if (startag == NULLAGNUMBER)
2424 break;
2425
2426 error = xfs_filestream_new_ag(ap, &ag);
2427 xfs_perag_put(pag);
2428 if (error)
2429 return error;
2430
2431 /* loop again to set 'blen'*/
2432 startag = NULLAGNUMBER;
2433 pag = xfs_perag_get(mp, ag);
2434 continue;
2435 }
2436 }
2437 if (++ag == mp->m_sb.sb_agcount)
2438 ag = 0;
2439 if (ag == startag)
2440 break;
2441 xfs_perag_put(pag);
2442 pag = xfs_perag_get(mp, ag);
2443 }
2444 xfs_perag_put(pag);
2445
2446 /*
2447 * Since the above loop did a BUF_TRYLOCK, it is
2448 * possible that there is space for this request.
2449 */
2450 if (notinit || *blen < ap->minlen)
2451 args->minlen = ap->minlen;
2452 /*
2453 * If the best seen length is less than the request
2454 * length, use the best as the minimum.
2455 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002456 else if (*blen < args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00002457 args->minlen = *blen;
2458 /*
Dave Chinner14b064c2011-01-27 12:16:28 +11002459 * Otherwise we've seen an extent as big as maxlen,
Christoph Hellwigc467c042010-02-15 23:34:42 +00002460 * use that as the minimum.
2461 */
2462 else
Dave Chinner14b064c2011-01-27 12:16:28 +11002463 args->minlen = args->maxlen;
Christoph Hellwigc467c042010-02-15 23:34:42 +00002464
2465 /*
2466 * set the failure fallback case to look in the selected
2467 * AG as the stream may have moved.
2468 */
2469 if (xfs_inode_is_filestream(ap->ip))
2470 ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2471
2472 return 0;
2473}
2474
2475STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11002476xfs_bmap_btalloc(
2477 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2478{
2479 xfs_mount_t *mp; /* mount point structure */
2480 xfs_alloctype_t atype = 0; /* type for allocation routines */
2481 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002482 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00002483 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002484 xfs_alloc_arg_t args;
2485 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002486 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002487 int nullfb; /* true if ap->firstblock isn't set */
2488 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002489 int tryagain;
2490 int error;
2491
2492 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002493 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002494 if (unlikely(align)) {
2495 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2496 align, 0, ap->eof, 0, ap->conv,
2497 &ap->off, &ap->alen);
2498 ASSERT(!error);
2499 ASSERT(ap->alen);
2500 }
2501 nullfb = ap->firstblock == NULLFSBLOCK;
2502 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10002503 if (nullfb) {
2504 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2505 ag = xfs_filestream_lookup_ag(ap->ip);
2506 ag = (ag != NULLAGNUMBER) ? ag : 0;
2507 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2508 } else {
2509 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2510 }
2511 } else
Nathan Scotta365bdd2006-03-14 13:34:16 +11002512 ap->rval = ap->firstblock;
2513
2514 xfs_bmap_adjacent(ap);
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 /*
2517 * If allowed, use ap->rval; otherwise must use firstblock since
2518 * it's in the right allocation group.
2519 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002520 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 ;
2522 else
2523 ap->rval = ap->firstblock;
2524 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 * Normal allocation, done through xfs_alloc_vextent.
2526 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002527 tryagain = isaligned = 0;
2528 args.tp = ap->tp;
2529 args.mp = mp;
2530 args.fsbno = ap->rval;
Dave Chinner14b064c2011-01-27 12:16:28 +11002531
2532 /* Trim the allocation back to the maximum an AG can fit. */
2533 args.maxlen = MIN(ap->alen, XFS_ALLOC_AG_MAX_USABLE(mp));
Yingping Lud210a282006-06-09 14:55:18 +10002534 args.firstblock = ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002535 blen = 0;
2536 if (nullfb) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00002537 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2538 if (error)
2539 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002540 } else if (ap->low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002541 if (xfs_inode_is_filestream(ap->ip))
2542 args.type = XFS_ALLOCTYPE_FIRST_AG;
2543 else
2544 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002545 args.total = args.minlen = ap->minlen;
2546 } else {
2547 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2548 args.total = ap->total;
2549 args.minlen = ap->minlen;
2550 }
David Chinner957d0eb2007-06-18 16:50:37 +10002551 /* apply extent size hints if obtained earlier */
2552 if (unlikely(align)) {
2553 args.prod = align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002554 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2555 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11002556 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11002557 args.prod = 1;
2558 args.mod = 0;
2559 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002560 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002561 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2562 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2563 }
2564 /*
2565 * If we are not low on available data blocks, and the
2566 * underlying logical volume manager is a stripe, and
2567 * the file offset is zero then try to allocate data
2568 * blocks on stripe unit boundary.
2569 * NOTE: ap->aeof is only set if the allocation length
2570 * is >= the stripe unit and the allocation offset is
2571 * at the end of file.
2572 */
2573 if (!ap->low && ap->aeof) {
2574 if (!ap->off) {
2575 args.alignment = mp->m_dalign;
2576 atype = args.type;
2577 isaligned = 1;
2578 /*
2579 * Adjust for alignment
2580 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002581 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002582 args.minlen = blen - args.alignment;
2583 args.minalignslop = 0;
2584 } else {
2585 /*
2586 * First try an exact bno allocation.
2587 * If it fails then do a near or start bno
2588 * allocation with alignment turned on.
2589 */
2590 atype = args.type;
2591 tryagain = 1;
2592 args.type = XFS_ALLOCTYPE_THIS_BNO;
2593 args.alignment = 1;
2594 /*
2595 * Compute the minlen+alignment for the
2596 * next case. Set slop so that the value
2597 * of minlen+alignment+slop doesn't go up
2598 * between the calls.
2599 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002600 if (blen > mp->m_dalign && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002601 nextminlen = blen - mp->m_dalign;
2602 else
2603 nextminlen = args.minlen;
2604 if (nextminlen + mp->m_dalign > args.minlen + 1)
2605 args.minalignslop =
2606 nextminlen + mp->m_dalign -
2607 args.minlen - 1;
2608 else
2609 args.minalignslop = 0;
2610 }
2611 } else {
2612 args.alignment = 1;
2613 args.minalignslop = 0;
2614 }
2615 args.minleft = ap->minleft;
2616 args.wasdel = ap->wasdel;
2617 args.isfl = 0;
2618 args.userdata = ap->userdata;
2619 if ((error = xfs_alloc_vextent(&args)))
2620 return error;
2621 if (tryagain && args.fsbno == NULLFSBLOCK) {
2622 /*
2623 * Exact allocation failed. Now try with alignment
2624 * turned on.
2625 */
2626 args.type = atype;
2627 args.fsbno = ap->rval;
2628 args.alignment = mp->m_dalign;
2629 args.minlen = nextminlen;
2630 args.minalignslop = 0;
2631 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 if ((error = xfs_alloc_vextent(&args)))
2633 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002634 }
2635 if (isaligned && args.fsbno == NULLFSBLOCK) {
2636 /*
2637 * allocation failed, so turn off alignment and
2638 * try again.
2639 */
2640 args.type = atype;
2641 args.fsbno = ap->rval;
2642 args.alignment = 0;
2643 if ((error = xfs_alloc_vextent(&args)))
2644 return error;
2645 }
2646 if (args.fsbno == NULLFSBLOCK && nullfb &&
2647 args.minlen > ap->minlen) {
2648 args.minlen = ap->minlen;
2649 args.type = XFS_ALLOCTYPE_START_BNO;
2650 args.fsbno = ap->rval;
2651 if ((error = xfs_alloc_vextent(&args)))
2652 return error;
2653 }
2654 if (args.fsbno == NULLFSBLOCK && nullfb) {
2655 args.fsbno = 0;
2656 args.type = XFS_ALLOCTYPE_FIRST_AG;
2657 args.total = ap->minlen;
2658 args.minleft = 0;
2659 if ((error = xfs_alloc_vextent(&args)))
2660 return error;
2661 ap->low = 1;
2662 }
2663 if (args.fsbno != NULLFSBLOCK) {
2664 ap->firstblock = ap->rval = args.fsbno;
2665 ASSERT(nullfb || fb_agno == args.agno ||
2666 (ap->low && fb_agno < args.agno));
2667 ap->alen = args.len;
2668 ap->ip->i_d.di_nblocks += args.len;
2669 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2670 if (ap->wasdel)
2671 ap->ip->i_delayed_blks -= args.len;
2672 /*
2673 * Adjust the disk quota also. This was reserved
2674 * earlier.
2675 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002676 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002677 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2678 XFS_TRANS_DQ_BCOUNT,
2679 (long) args.len);
2680 } else {
2681 ap->rval = NULLFSBLOCK;
2682 ap->alen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 }
2684 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002685}
2686
2687/*
2688 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2689 * It figures out where to ask the underlying allocator to put the new extent.
2690 */
2691STATIC int
2692xfs_bmap_alloc(
2693 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2694{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002695 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002696 return xfs_bmap_rtalloc(ap);
2697 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698}
2699
2700/*
2701 * Transform a btree format file with only one leaf node, where the
2702 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002703 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 * give up the space for the btree root and pitch the leaf block.
2705 */
2706STATIC int /* error */
2707xfs_bmap_btree_to_extents(
2708 xfs_trans_t *tp, /* transaction pointer */
2709 xfs_inode_t *ip, /* incore inode pointer */
2710 xfs_btree_cur_t *cur, /* btree cursor */
2711 int *logflagsp, /* inode logging flags */
2712 int whichfork) /* data or attr fork */
2713{
2714 /* REFERENCED */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002715 struct xfs_btree_block *cblock;/* child btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 xfs_fsblock_t cbno; /* child block number */
2717 xfs_buf_t *cbp; /* child block's buffer */
2718 int error; /* error return value */
2719 xfs_ifork_t *ifp; /* inode fork data */
2720 xfs_mount_t *mp; /* mount point structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10002721 __be64 *pp; /* ptr to block address */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002722 struct xfs_btree_block *rblock;/* root btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Christoph Hellwig60197e82008-10-30 17:11:19 +11002724 mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 ifp = XFS_IFORK_PTR(ip, whichfork);
2726 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2727 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2728 rblock = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11002729 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2730 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
Christoph Hellwig60197e82008-10-30 17:11:19 +11002731 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2732 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10002733 cbno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 *logflagsp = 0;
2735#ifdef DEBUG
Christoph Hellwig576039c2006-09-28 10:58:06 +10002736 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 return error;
2738#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2740 XFS_BMAP_BTREE_REF)))
2741 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002742 cblock = XFS_BUF_TO_BLOCK(cbp);
2743 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return error;
2745 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2746 ip->i_d.di_nblocks--;
Christoph Hellwig7d095252009-06-08 15:33:32 +02002747 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 xfs_trans_binval(tp, cbp);
2749 if (cur->bc_bufs[0] == cbp)
2750 cur->bc_bufs[0] = NULL;
2751 xfs_iroot_realloc(ip, -1, whichfork);
2752 ASSERT(ifp->if_broot == NULL);
2753 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2754 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002755 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 return 0;
2757}
2758
2759/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002760 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 * after removing space (or undoing a delayed allocation).
2762 */
2763STATIC int /* error */
2764xfs_bmap_del_extent(
2765 xfs_inode_t *ip, /* incore inode pointer */
2766 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002767 xfs_extnum_t *idx, /* extent number to update/delete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 xfs_bmap_free_t *flist, /* list of extents to be freed */
2769 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002770 xfs_bmbt_irec_t *del, /* data to remove from extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 int *logflagsp, /* inode logging flags */
Christoph Hellwig54893272011-05-11 15:04:03 +00002772 int whichfork) /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
2774 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2775 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2776 xfs_fsblock_t del_endblock=0; /* first block past del */
2777 xfs_fileoff_t del_endoff; /* first offset past del */
2778 int delay; /* current block is delayed allocated */
2779 int do_fx; /* free extent at end of routine */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002780 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 int error; /* error return value */
2782 int flags; /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 xfs_bmbt_irec_t got; /* current extent entry */
2784 xfs_fileoff_t got_endoff; /* first offset past got */
2785 int i; /* temp state */
2786 xfs_ifork_t *ifp; /* inode fork pointer */
2787 xfs_mount_t *mp; /* mount structure */
2788 xfs_filblks_t nblks; /* quota/sb block count */
2789 xfs_bmbt_irec_t new; /* new record to be inserted */
2790 /* REFERENCED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 uint qfield; /* quota field to update */
2792 xfs_filblks_t temp; /* for indirect length calculations */
2793 xfs_filblks_t temp2; /* for indirect length calculations */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002794 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 XFS_STATS_INC(xs_del_exlist);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002797
2798 if (whichfork == XFS_ATTR_FORK)
2799 state |= BMAP_ATTRFORK;
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 mp = ip->i_mount;
2802 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002803 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002804 (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 ASSERT(del->br_blockcount > 0);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002806 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 xfs_bmbt_get_all(ep, &got);
2808 ASSERT(got.br_startoff <= del->br_startoff);
2809 del_endoff = del->br_startoff + del->br_blockcount;
2810 got_endoff = got.br_startoff + got.br_blockcount;
2811 ASSERT(got_endoff >= del_endoff);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002812 delay = isnullstartblock(got.br_startblock);
2813 ASSERT(isnullstartblock(del->br_startblock) == delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 flags = 0;
2815 qfield = 0;
2816 error = 0;
2817 /*
2818 * If deleting a real allocation, must free up the disk space.
2819 */
2820 if (!delay) {
2821 flags = XFS_ILOG_CORE;
2822 /*
2823 * Realtime allocation. Free it and record di_nblocks update.
2824 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002825 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 xfs_fsblock_t bno;
2827 xfs_filblks_t len;
2828
2829 ASSERT(do_mod(del->br_blockcount,
2830 mp->m_sb.sb_rextsize) == 0);
2831 ASSERT(do_mod(del->br_startblock,
2832 mp->m_sb.sb_rextsize) == 0);
2833 bno = del->br_startblock;
2834 len = del->br_blockcount;
2835 do_div(bno, mp->m_sb.sb_rextsize);
2836 do_div(len, mp->m_sb.sb_rextsize);
Christoph Hellwigf3ca87382011-07-08 14:34:47 +02002837 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
2838 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 goto done;
2840 do_fx = 0;
2841 nblks = len * mp->m_sb.sb_rextsize;
2842 qfield = XFS_TRANS_DQ_RTBCOUNT;
2843 }
2844 /*
2845 * Ordinary allocation.
2846 */
2847 else {
2848 do_fx = 1;
2849 nblks = del->br_blockcount;
2850 qfield = XFS_TRANS_DQ_BCOUNT;
2851 }
2852 /*
2853 * Set up del_endblock and cur for later.
2854 */
2855 del_endblock = del->br_startblock + del->br_blockcount;
2856 if (cur) {
2857 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2858 got.br_startblock, got.br_blockcount,
2859 &i)))
2860 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002861 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 }
2863 da_old = da_new = 0;
2864 } else {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002865 da_old = startblockval(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 da_new = 0;
2867 nblks = 0;
2868 do_fx = 0;
2869 }
2870 /*
2871 * Set flag value to use in switch statement.
2872 * Left-contig is 2, right-contig is 1.
2873 */
2874 switch (((got.br_startoff == del->br_startoff) << 1) |
2875 (got_endoff == del_endoff)) {
2876 case 3:
2877 /*
2878 * Matches the whole extent. Delete the entry.
2879 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002880 xfs_iext_remove(ip, *idx, 1,
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002881 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
Christoph Hellwig233eebb2011-05-11 15:04:05 +00002882 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (delay)
2884 break;
Christoph Hellwig233eebb2011-05-11 15:04:05 +00002885
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 XFS_IFORK_NEXT_SET(ip, whichfork,
2887 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2888 flags |= XFS_ILOG_CORE;
2889 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002890 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 break;
2892 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002893 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002895 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 break;
2897
2898 case 2:
2899 /*
2900 * Deleting the first part of the extent.
2901 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002902 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 xfs_bmbt_set_startoff(ep, del_endoff);
2904 temp = got.br_blockcount - del->br_blockcount;
2905 xfs_bmbt_set_blockcount(ep, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (delay) {
2907 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2908 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002909 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002910 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 da_new = temp;
2912 break;
2913 }
2914 xfs_bmbt_set_startblock(ep, del_endblock);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002915 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002917 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 break;
2919 }
2920 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2921 got.br_blockcount - del->br_blockcount,
2922 got.br_state)))
2923 goto done;
2924 break;
2925
2926 case 1:
2927 /*
2928 * Deleting the last part of the extent.
2929 */
2930 temp = got.br_blockcount - del->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002931 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 xfs_bmbt_set_blockcount(ep, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 if (delay) {
2934 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2935 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002936 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002937 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 da_new = temp;
2939 break;
2940 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002941 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002943 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 break;
2945 }
2946 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2947 got.br_startblock,
2948 got.br_blockcount - del->br_blockcount,
2949 got.br_state)))
2950 goto done;
2951 break;
2952
2953 case 0:
2954 /*
2955 * Deleting the middle of the extent.
2956 */
2957 temp = del->br_startoff - got.br_startoff;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002958 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 xfs_bmbt_set_blockcount(ep, temp);
2960 new.br_startoff = del_endoff;
2961 temp2 = got_endoff - del_endoff;
2962 new.br_blockcount = temp2;
2963 new.br_state = got.br_state;
2964 if (!delay) {
2965 new.br_startblock = del_endblock;
2966 flags |= XFS_ILOG_CORE;
2967 if (cur) {
2968 if ((error = xfs_bmbt_update(cur,
2969 got.br_startoff,
2970 got.br_startblock, temp,
2971 got.br_state)))
2972 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002973 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 goto done;
2975 cur->bc_rec.b = new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002976 error = xfs_btree_insert(cur, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 if (error && error != ENOSPC)
2978 goto done;
2979 /*
2980 * If get no-space back from btree insert,
2981 * it tried a split, and we have a zero
2982 * block reservation.
2983 * Fix up our state and return the error.
2984 */
2985 if (error == ENOSPC) {
2986 /*
2987 * Reset the cursor, don't trust
2988 * it after any insert operation.
2989 */
2990 if ((error = xfs_bmbt_lookup_eq(cur,
2991 got.br_startoff,
2992 got.br_startblock,
2993 temp, &i)))
2994 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002995 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 /*
2997 * Update the btree record back
2998 * to the original value.
2999 */
3000 if ((error = xfs_bmbt_update(cur,
3001 got.br_startoff,
3002 got.br_startblock,
3003 got.br_blockcount,
3004 got.br_state)))
3005 goto done;
3006 /*
3007 * Reset the extent record back
3008 * to the original value.
3009 */
3010 xfs_bmbt_set_blockcount(ep,
3011 got.br_blockcount);
3012 flags = 0;
3013 error = XFS_ERROR(ENOSPC);
3014 goto done;
3015 }
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003016 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 } else
Eric Sandeen9d87c312009-01-14 23:22:07 -06003018 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 XFS_IFORK_NEXT_SET(ip, whichfork,
3020 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3021 } else {
3022 ASSERT(whichfork == XFS_DATA_FORK);
3023 temp = xfs_bmap_worst_indlen(ip, temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003024 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003026 new.br_startblock = nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 da_new = temp + temp2;
3028 while (da_new > da_old) {
3029 if (temp) {
3030 temp--;
3031 da_new--;
3032 xfs_bmbt_set_startblock(ep,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003033 nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 }
3035 if (da_new == da_old)
3036 break;
3037 if (temp2) {
3038 temp2--;
3039 da_new--;
3040 new.br_startblock =
Eric Sandeen9d87c312009-01-14 23:22:07 -06003041 nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 }
3043 }
3044 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00003045 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
3046 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
3047 ++*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 break;
3049 }
3050 /*
3051 * If we need to, add to list of extents to delete.
3052 */
3053 if (do_fx)
3054 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3055 mp);
3056 /*
3057 * Adjust inode # blocks in the file.
3058 */
3059 if (nblks)
3060 ip->i_d.di_nblocks -= nblks;
3061 /*
3062 * Adjust quota data.
3063 */
3064 if (qfield)
Christoph Hellwig7d095252009-06-08 15:33:32 +02003065 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
3067 /*
3068 * Account for change in delayed indirect blocks.
3069 * Nothing to do for disk quota accounting here.
3070 */
3071 ASSERT(da_old >= da_new);
Christoph Hellwig96540c72010-09-30 02:25:55 +00003072 if (da_old > da_new) {
3073 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00003074 (int64_t)(da_old - da_new), 0);
Christoph Hellwig96540c72010-09-30 02:25:55 +00003075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076done:
3077 *logflagsp = flags;
3078 return error;
3079}
3080
3081/*
3082 * Remove the entry "free" from the free item list. Prev points to the
3083 * previous entry, unless "free" is the head of the list.
3084 */
3085STATIC void
3086xfs_bmap_del_free(
3087 xfs_bmap_free_t *flist, /* free item list header */
3088 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3089 xfs_bmap_free_item_t *free) /* list item to be freed */
3090{
3091 if (prev)
3092 prev->xbfi_next = free->xbfi_next;
3093 else
3094 flist->xbf_first = free->xbfi_next;
3095 flist->xbf_count--;
3096 kmem_zone_free(xfs_bmap_free_item_zone, free);
3097}
3098
3099/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 * Convert an extents-format file into a btree-format file.
3101 * The new file will have a root block (in the inode) and a single child block.
3102 */
3103STATIC int /* error */
3104xfs_bmap_extents_to_btree(
3105 xfs_trans_t *tp, /* transaction pointer */
3106 xfs_inode_t *ip, /* incore inode pointer */
3107 xfs_fsblock_t *firstblock, /* first-block-allocated */
3108 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3109 xfs_btree_cur_t **curp, /* cursor returned to caller */
3110 int wasdel, /* converting a delayed alloc */
3111 int *logflagsp, /* inode logging flags */
3112 int whichfork) /* data or attr fork */
3113{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003114 struct xfs_btree_block *ablock; /* allocated (child) bt block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 xfs_buf_t *abp; /* buffer for ablock */
3116 xfs_alloc_arg_t args; /* allocation arguments */
3117 xfs_bmbt_rec_t *arp; /* child record pointer */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003118 struct xfs_btree_block *block; /* btree root block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 xfs_btree_cur_t *cur; /* bmap btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003120 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003122 xfs_extnum_t i, cnt; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 xfs_ifork_t *ifp; /* inode fork pointer */
3124 xfs_bmbt_key_t *kp; /* root block key pointer */
3125 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003126 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3128
3129 ifp = XFS_IFORK_PTR(ip, whichfork);
3130 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3131 ASSERT(ifp->if_ext_max ==
3132 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3133 /*
3134 * Make space in the inode incore.
3135 */
3136 xfs_iroot_realloc(ip, 1, whichfork);
3137 ifp->if_flags |= XFS_IFBROOT;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003138
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 /*
3140 * Fill in the root.
3141 */
3142 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003143 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3144 block->bb_level = cpu_to_be16(1);
3145 block->bb_numrecs = cpu_to_be16(1);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003146 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3147 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3148
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 /*
3150 * Need a cursor. Can't allocate until bb_level is filled in.
3151 */
3152 mp = ip->i_mount;
Christoph Hellwig561f7d12008-10-30 16:53:59 +11003153 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 cur->bc_private.b.firstblock = *firstblock;
3155 cur->bc_private.b.flist = flist;
3156 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3157 /*
3158 * Convert to a btree with two levels, one record in root.
3159 */
3160 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3161 args.tp = tp;
3162 args.mp = mp;
Yingping Lud210a282006-06-09 14:55:18 +10003163 args.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 if (*firstblock == NULLFSBLOCK) {
3165 args.type = XFS_ALLOCTYPE_START_BNO;
3166 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3167 } else if (flist->xbf_low) {
3168 args.type = XFS_ALLOCTYPE_START_BNO;
3169 args.fsbno = *firstblock;
3170 } else {
3171 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3172 args.fsbno = *firstblock;
3173 }
3174 args.minlen = args.maxlen = args.prod = 1;
3175 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3176 args.minalignslop = 0;
3177 args.wasdel = wasdel;
3178 *logflagsp = 0;
3179 if ((error = xfs_alloc_vextent(&args))) {
3180 xfs_iroot_realloc(ip, -1, whichfork);
3181 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3182 return error;
3183 }
3184 /*
3185 * Allocation can't fail, the space was reserved.
3186 */
3187 ASSERT(args.fsbno != NULLFSBLOCK);
3188 ASSERT(*firstblock == NULLFSBLOCK ||
3189 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3190 (flist->xbf_low &&
3191 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3192 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3193 cur->bc_private.b.allocated++;
3194 ip->i_d.di_nblocks++;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003195 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3197 /*
3198 * Fill in the child block.
3199 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003200 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003201 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 ablock->bb_level = 0;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003203 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3204 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003205 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003207 for (cnt = i = 0; i < nextents; i++) {
3208 ep = xfs_iext_get_ext(ifp, i);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003209 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10003210 arp->l0 = cpu_to_be64(ep->l0);
3211 arp->l1 = cpu_to_be64(ep->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 arp++; cnt++;
3213 }
3214 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11003215 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003216 xfs_btree_set_numrecs(ablock, cnt);
3217
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 /*
3219 * Fill in the root key and pointer.
3220 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11003221 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3222 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Christoph Hellwig8801bb92006-09-28 10:58:17 +10003223 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
Christoph Hellwig136341b2008-10-30 17:11:40 +11003224 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3225 be16_to_cpu(block->bb_level)));
Christoph Hellwig576039c2006-09-28 10:58:06 +10003226 *pp = cpu_to_be64(args.fsbno);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003227
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 /*
3229 * Do all this logging at the end so that
3230 * the root is at the right level.
3231 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11003232 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3233 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 ASSERT(*curp == NULL);
3235 *curp = cur;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003236 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 return 0;
3238}
3239
3240/*
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003241 * Calculate the default attribute fork offset for newly created inodes.
3242 */
3243uint
3244xfs_default_attroffset(
3245 struct xfs_inode *ip)
3246{
3247 struct xfs_mount *mp = ip->i_mount;
3248 uint offset;
3249
3250 if (mp->m_sb.sb_inodesize == 256) {
3251 offset = XFS_LITINO(mp) -
3252 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3253 } else {
3254 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3255 }
3256
3257 ASSERT(offset < XFS_LITINO(mp));
3258 return offset;
3259}
3260
3261/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11003262 * Helper routine to reset inode di_forkoff field when switching
3263 * attribute fork from local to extent format - we reset it where
3264 * possible to make space available for inline data fork extents.
3265 */
3266STATIC void
3267xfs_bmap_forkoff_reset(
3268 xfs_mount_t *mp,
3269 xfs_inode_t *ip,
3270 int whichfork)
3271{
3272 if (whichfork == XFS_ATTR_FORK &&
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003273 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3274 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3275 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3276 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3277
3278 if (dfl_forkoff > ip->i_d.di_forkoff) {
3279 ip->i_d.di_forkoff = dfl_forkoff;
3280 ip->i_df.if_ext_max =
3281 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3282 ip->i_afp->if_ext_max =
3283 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3284 }
Nathan Scottd8cc8902005-11-02 10:34:53 +11003285 }
3286}
3287
3288/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 * Convert a local file to an extents file.
3290 * This code is out of bounds for data forks of regular files,
3291 * since the file data needs to get logged so things will stay consistent.
3292 * (The bmap-level manipulations are ok, though).
3293 */
3294STATIC int /* error */
3295xfs_bmap_local_to_extents(
3296 xfs_trans_t *tp, /* transaction pointer */
3297 xfs_inode_t *ip, /* incore inode pointer */
3298 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3299 xfs_extlen_t total, /* total blocks needed by transaction */
3300 int *logflagsp, /* inode logging flags */
3301 int whichfork) /* data or attr fork */
3302{
3303 int error; /* error return value */
3304 int flags; /* logging flags returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 xfs_ifork_t *ifp; /* inode fork pointer */
3306
3307 /*
3308 * We don't want to deal with the case of keeping inode data inline yet.
3309 * So sending the data fork of a regular inode is invalid.
3310 */
Al Viroabbede12011-07-26 02:31:30 -04003311 ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 ifp = XFS_IFORK_PTR(ip, whichfork);
3313 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3314 flags = 0;
3315 error = 0;
3316 if (ifp->if_bytes) {
3317 xfs_alloc_arg_t args; /* allocation arguments */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003318 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003319 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
3321 args.tp = tp;
3322 args.mp = ip->i_mount;
Yingping Lud210a282006-06-09 14:55:18 +10003323 args.firstblock = *firstblock;
Mandy Kirkconnellf020b672006-03-14 14:07:24 +11003324 ASSERT((ifp->if_flags &
3325 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 /*
3327 * Allocate a block. We know we need only one, since the
3328 * file currently fits in an inode.
3329 */
3330 if (*firstblock == NULLFSBLOCK) {
3331 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3332 args.type = XFS_ALLOCTYPE_START_BNO;
3333 } else {
3334 args.fsbno = *firstblock;
3335 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3336 }
3337 args.total = total;
3338 args.mod = args.minleft = args.alignment = args.wasdel =
3339 args.isfl = args.minalignslop = 0;
3340 args.minlen = args.maxlen = args.prod = 1;
3341 if ((error = xfs_alloc_vextent(&args)))
3342 goto done;
3343 /*
3344 * Can't fail, the space was reserved.
3345 */
3346 ASSERT(args.fsbno != NULLFSBLOCK);
3347 ASSERT(args.len == 1);
3348 *firstblock = args.fsbno;
3349 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
Chandra Seetharaman62926042011-07-22 23:40:15 +00003350 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003352 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003354 xfs_iext_add(ifp, 0, 1);
3355 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003357 trace_xfs_bmap_post_update(ip, 0,
3358 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3359 _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3361 ip->i_d.di_nblocks = 1;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003362 xfs_trans_mod_dquot_byino(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 XFS_TRANS_DQ_BCOUNT, 1L);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003364 flags |= xfs_ilog_fext(whichfork);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003365 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003367 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 ifp->if_flags &= ~XFS_IFINLINE;
3370 ifp->if_flags |= XFS_IFEXTENTS;
3371 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3372 flags |= XFS_ILOG_CORE;
3373done:
3374 *logflagsp = flags;
3375 return error;
3376}
3377
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003379 * Search the extent records for the entry containing block bno.
3380 * If bno lies in a hole, point to the next entry. If bno lies
3381 * past eof, *eofp will be set, and *prevp will contain the last
3382 * entry (null if none). Else, *lastxp will be set to the index
3383 * of the found entry; *gotp will contain the entry.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003384 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003385STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003386xfs_bmap_search_multi_extents(
3387 xfs_ifork_t *ifp, /* inode fork pointer */
3388 xfs_fileoff_t bno, /* block number searched for */
3389 int *eofp, /* out: end of file found */
3390 xfs_extnum_t *lastxp, /* out: last extent index */
3391 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3392 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3393{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003394 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003395 xfs_extnum_t lastx; /* last extent index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003396
3397 /*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003398 * Initialize the extent entry structure to catch access to
3399 * uninitialized br_startblock field.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003400 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003401 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3402 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3403 gotp->br_state = XFS_EXT_INVALID;
3404#if XFS_BIG_BLKNOS
3405 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3406#else
3407 gotp->br_startblock = 0xffffa5a5;
3408#endif
3409 prevp->br_startoff = NULLFILEOFF;
3410
3411 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3412 if (lastx > 0) {
3413 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3414 }
3415 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3416 xfs_bmbt_get_all(ep, gotp);
3417 *eofp = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003418 } else {
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003419 if (lastx > 0) {
3420 *gotp = *prevp;
3421 }
3422 *eofp = 1;
3423 ep = NULL;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003424 }
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003425 *lastxp = lastx;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003426 return ep;
3427}
3428
3429/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 * Search the extents list for the inode, for the extent containing bno.
3431 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3432 * *eofp will be set, and *prevp will contain the last entry (null if none).
3433 * Else, *lastxp will be set to the index of the found
3434 * entry; *gotp will contain the entry.
3435 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003436STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437xfs_bmap_search_extents(
3438 xfs_inode_t *ip, /* incore inode pointer */
3439 xfs_fileoff_t bno, /* block number searched for */
Nathan Scott572d95f2006-09-28 11:03:20 +10003440 int fork, /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 int *eofp, /* out: end of file found */
3442 xfs_extnum_t *lastxp, /* out: last extent index */
3443 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3444 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3445{
3446 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003447 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
3449 XFS_STATS_INC(xs_look_exlist);
Nathan Scott572d95f2006-09-28 11:03:20 +10003450 ifp = XFS_IFORK_PTR(ip, fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003452 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3453
Nathan Scott572d95f2006-09-28 11:03:20 +10003454 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3455 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003456 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
Nathan Scott572d95f2006-09-28 11:03:20 +10003457 "Access to block zero in inode %llu "
3458 "start_block: %llx start_off: %llx "
3459 "blkcnt: %llx extent-state: %x lastx: %x\n",
3460 (unsigned long long)ip->i_ino,
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003461 (unsigned long long)gotp->br_startblock,
3462 (unsigned long long)gotp->br_startoff,
3463 (unsigned long long)gotp->br_blockcount,
Nathan Scott572d95f2006-09-28 11:03:20 +10003464 gotp->br_state, *lastxp);
3465 *lastxp = NULLEXTNUM;
3466 *eofp = 1;
3467 return NULL;
3468 }
3469 return ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470}
3471
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472/*
3473 * Compute the worst-case number of indirect blocks that will be used
3474 * for ip's delayed extent of length "len".
3475 */
3476STATIC xfs_filblks_t
3477xfs_bmap_worst_indlen(
3478 xfs_inode_t *ip, /* incore inode pointer */
3479 xfs_filblks_t len) /* delayed extent length */
3480{
3481 int level; /* btree level number */
3482 int maxrecs; /* maximum record count at this level */
3483 xfs_mount_t *mp; /* mount structure */
3484 xfs_filblks_t rval; /* return value */
3485
3486 mp = ip->i_mount;
3487 maxrecs = mp->m_bmap_dmxr[0];
3488 for (level = 0, rval = 0;
3489 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3490 level++) {
3491 len += maxrecs - 1;
3492 do_div(len, maxrecs);
3493 rval += len;
3494 if (len == 1)
3495 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3496 level - 1;
3497 if (level == 0)
3498 maxrecs = mp->m_bmap_dmxr[1];
3499 }
3500 return rval;
3501}
3502
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503/*
3504 * Convert inode from non-attributed to attributed.
3505 * Must not be in a transaction, ip must not be locked.
3506 */
3507int /* error code */
3508xfs_bmap_add_attrfork(
3509 xfs_inode_t *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003510 int size, /* space new attribute needs */
3511 int rsvd) /* xact may use reserved blks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003514 xfs_bmap_free_t flist; /* freed extent records */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 xfs_trans_t *tp; /* transaction pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003517 int blks; /* space reservation */
3518 int version = 1; /* superblock attr version */
3519 int committed; /* xaction was committed */
3520 int logflags; /* logging flags */
3521 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Nathan Scottd8cc8902005-11-02 10:34:53 +11003523 ASSERT(XFS_IFORK_Q(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 ASSERT(ip->i_df.if_ext_max ==
3525 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
Nathan Scottd8cc8902005-11-02 10:34:53 +11003526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 mp = ip->i_mount;
3528 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3529 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3530 blks = XFS_ADDAFORK_SPACE_RES(mp);
3531 if (rsvd)
3532 tp->t_flags |= XFS_TRANS_RESERVE;
3533 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3534 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3535 goto error0;
3536 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02003537 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3539 XFS_QMOPT_RES_REGBLKS);
3540 if (error) {
3541 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3542 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3543 return error;
3544 }
3545 if (XFS_IFORK_Q(ip))
3546 goto error1;
3547 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3548 /*
3549 * For inodes coming from pre-6.2 filesystems.
3550 */
3551 ASSERT(ip->i_d.di_aformat == 0);
3552 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3553 }
3554 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwig898621d2010-06-24 11:36:58 +10003555
3556 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig898621d2010-06-24 11:36:58 +10003558
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 switch (ip->i_d.di_format) {
3560 case XFS_DINODE_FMT_DEV:
3561 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3562 break;
3563 case XFS_DINODE_FMT_UUID:
3564 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3565 break;
3566 case XFS_DINODE_FMT_LOCAL:
3567 case XFS_DINODE_FMT_EXTENTS:
3568 case XFS_DINODE_FMT_BTREE:
Nathan Scottd8cc8902005-11-02 10:34:53 +11003569 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3570 if (!ip->i_d.di_forkoff)
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003571 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
Nathan Scott13059ff2006-01-11 15:32:01 +11003572 else if (mp->m_flags & XFS_MOUNT_ATTR2)
Nathan Scottd8cc8902005-11-02 10:34:53 +11003573 version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 break;
3575 default:
3576 ASSERT(0);
3577 error = XFS_ERROR(EINVAL);
3578 goto error1;
3579 }
3580 ip->i_df.if_ext_max =
3581 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3582 ASSERT(ip->i_afp == NULL);
3583 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3584 ip->i_afp->if_ext_max =
3585 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3586 ip->i_afp->if_flags = XFS_IFEXTENTS;
3587 logflags = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003588 xfs_bmap_init(&flist, &firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 switch (ip->i_d.di_format) {
3590 case XFS_DINODE_FMT_LOCAL:
3591 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3592 &logflags);
3593 break;
3594 case XFS_DINODE_FMT_EXTENTS:
3595 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3596 &flist, &logflags);
3597 break;
3598 case XFS_DINODE_FMT_BTREE:
3599 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3600 &logflags);
3601 break;
3602 default:
3603 error = 0;
3604 break;
3605 }
3606 if (logflags)
3607 xfs_trans_log_inode(tp, ip, logflags);
3608 if (error)
3609 goto error2;
Eric Sandeen62118702008-03-06 13:44:28 +11003610 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3611 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +11003612 __int64_t sbfields = 0;
3613
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003614 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +11003615 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3616 xfs_sb_version_addattr(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003617 sbfields |= XFS_SB_VERSIONNUM;
Nathan Scottd8cc8902005-11-02 10:34:53 +11003618 }
Eric Sandeen62118702008-03-06 13:44:28 +11003619 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3620 xfs_sb_version_addattr2(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003621 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003622 }
Nathan Scottda087ba2005-11-02 15:00:20 +11003623 if (sbfields) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003624 spin_unlock(&mp->m_sb_lock);
Nathan Scottda087ba2005-11-02 15:00:20 +11003625 xfs_mod_sb(tp, sbfields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003627 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 }
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003629 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 goto error2;
Dave Chinner713bf882010-03-08 11:26:23 +11003631 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 ASSERT(ip->i_df.if_ext_max ==
3633 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3634 return error;
3635error2:
3636 xfs_bmap_cancel(&flist);
3637error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3639error0:
3640 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3641 ASSERT(ip->i_df.if_ext_max ==
3642 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3643 return error;
3644}
3645
3646/*
3647 * Add the extent to the list of extents to be free at transaction end.
3648 * The list is maintained sorted (by block number).
3649 */
3650/* ARGSUSED */
3651void
3652xfs_bmap_add_free(
3653 xfs_fsblock_t bno, /* fs block number of extent */
3654 xfs_filblks_t len, /* length of extent */
3655 xfs_bmap_free_t *flist, /* list of extents */
3656 xfs_mount_t *mp) /* mount point structure */
3657{
3658 xfs_bmap_free_item_t *cur; /* current (next) element */
3659 xfs_bmap_free_item_t *new; /* new element */
3660 xfs_bmap_free_item_t *prev; /* previous element */
3661#ifdef DEBUG
3662 xfs_agnumber_t agno;
3663 xfs_agblock_t agbno;
3664
3665 ASSERT(bno != NULLFSBLOCK);
3666 ASSERT(len > 0);
3667 ASSERT(len <= MAXEXTLEN);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003668 ASSERT(!isnullstartblock(bno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 agno = XFS_FSB_TO_AGNO(mp, bno);
3670 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3671 ASSERT(agno < mp->m_sb.sb_agcount);
3672 ASSERT(agbno < mp->m_sb.sb_agblocks);
3673 ASSERT(len < mp->m_sb.sb_agblocks);
3674 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3675#endif
3676 ASSERT(xfs_bmap_free_item_zone != NULL);
3677 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3678 new->xbfi_startblock = bno;
3679 new->xbfi_blockcount = (xfs_extlen_t)len;
3680 for (prev = NULL, cur = flist->xbf_first;
3681 cur != NULL;
3682 prev = cur, cur = cur->xbfi_next) {
3683 if (cur->xbfi_startblock >= bno)
3684 break;
3685 }
3686 if (prev)
3687 prev->xbfi_next = new;
3688 else
3689 flist->xbf_first = new;
3690 new->xbfi_next = cur;
3691 flist->xbf_count++;
3692}
3693
3694/*
3695 * Compute and fill in the value of the maximum depth of a bmap btree
3696 * in this filesystem. Done once, during mount.
3697 */
3698void
3699xfs_bmap_compute_maxlevels(
3700 xfs_mount_t *mp, /* file system mount structure */
3701 int whichfork) /* data or attr fork */
3702{
3703 int level; /* btree level */
3704 uint maxblocks; /* max blocks at this level */
3705 uint maxleafents; /* max leaf entries possible */
3706 int maxrootrecs; /* max records in root block */
3707 int minleafrecs; /* min records in leaf block */
3708 int minnoderecs; /* min records in node block */
3709 int sz; /* root block size */
3710
3711 /*
3712 * The maximum number of extents in a file, hence the maximum
3713 * number of leaf entries, is controlled by the type of di_nextents
3714 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3715 * (a signed 16-bit number, xfs_aextnum_t).
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003716 *
3717 * Note that we can no longer assume that if we are in ATTR1 that
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003718 * the fork offset of all the inodes will be
3719 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3720 * with ATTR2 and then mounted back with ATTR1, keeping the
3721 * di_forkoff's fixed but probably at various positions. Therefore,
3722 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3723 * of a minimum size available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003725 if (whichfork == XFS_DATA_FORK) {
3726 maxleafents = MAXEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003727 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003728 } else {
3729 maxleafents = MAXAEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003730 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003731 }
Christoph Hellwig60197e82008-10-30 17:11:19 +11003732 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 minleafrecs = mp->m_bmap_dmnr[0];
3734 minnoderecs = mp->m_bmap_dmnr[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3736 for (level = 1; maxblocks > 1; level++) {
3737 if (maxblocks <= maxrootrecs)
3738 maxblocks = 1;
3739 else
3740 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3741 }
3742 mp->m_bm_maxlevels[whichfork] = level;
3743}
3744
3745/*
3746 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3747 * caller. Frees all the extents that need freeing, which must be done
3748 * last due to locking considerations. We never free any extents in
Christoph Hellwig859f57c2011-08-27 14:45:11 +00003749 * the first transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 *
3751 * Return 1 if the given transaction was committed and a new one
3752 * started, and 0 otherwise in the committed parameter.
3753 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754int /* error */
3755xfs_bmap_finish(
3756 xfs_trans_t **tp, /* transaction pointer addr */
3757 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 int *committed) /* xact committed or not */
3759{
3760 xfs_efd_log_item_t *efd; /* extent free data */
3761 xfs_efi_log_item_t *efi; /* extent free intention */
3762 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003763 xfs_bmap_free_item_t *free; /* free extent item */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 unsigned int logres; /* new log reservation */
3765 unsigned int logcount; /* new log count */
3766 xfs_mount_t *mp; /* filesystem mount structure */
3767 xfs_bmap_free_item_t *next; /* next item on free list */
3768 xfs_trans_t *ntp; /* new transaction pointer */
3769
3770 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3771 if (flist->xbf_count == 0) {
3772 *committed = 0;
3773 return 0;
3774 }
3775 ntp = *tp;
3776 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3777 for (free = flist->xbf_first; free; free = free->xbfi_next)
3778 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3779 free->xbfi_blockcount);
3780 logres = ntp->t_log_res;
3781 logcount = ntp->t_log_count;
3782 ntp = xfs_trans_dup(*tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003783 error = xfs_trans_commit(*tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 *tp = ntp;
3785 *committed = 1;
3786 /*
3787 * We have a new transaction, so we should return committed=1,
3788 * even though we're returning an error.
3789 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003790 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 return error;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003792
3793 /*
3794 * transaction commit worked ok so we can drop the extra ticket
3795 * reference that we gained in xfs_trans_dup()
3796 */
3797 xfs_log_ticket_put(ntp->t_ticket);
3798
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3800 logcount)))
3801 return error;
3802 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3803 for (free = flist->xbf_first; free != NULL; free = next) {
3804 next = free->xbfi_next;
3805 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3806 free->xbfi_blockcount))) {
3807 /*
3808 * The bmap free list will be cleaned up at a
3809 * higher level. The EFI will be canceled when
3810 * this transaction is aborted.
3811 * Need to force shutdown here to make sure it
3812 * happens, since this transaction may not be
3813 * dirty yet.
3814 */
3815 mp = ntp->t_mountp;
3816 if (!XFS_FORCED_SHUTDOWN(mp))
3817 xfs_force_shutdown(mp,
3818 (error == EFSCORRUPTED) ?
Nathan Scott7d04a332006-06-09 14:58:38 +10003819 SHUTDOWN_CORRUPT_INCORE :
3820 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 return error;
3822 }
3823 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3824 free->xbfi_blockcount);
3825 xfs_bmap_del_free(flist, NULL, free);
3826 }
3827 return 0;
3828}
3829
3830/*
3831 * Free up any items left in the list.
3832 */
3833void
3834xfs_bmap_cancel(
3835 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3836{
3837 xfs_bmap_free_item_t *free; /* free list item */
3838 xfs_bmap_free_item_t *next;
3839
3840 if (flist->xbf_count == 0)
3841 return;
3842 ASSERT(flist->xbf_first != NULL);
3843 for (free = flist->xbf_first; free; free = next) {
3844 next = free->xbfi_next;
3845 xfs_bmap_del_free(flist, NULL, free);
3846 }
3847 ASSERT(flist->xbf_count == 0);
3848}
3849
3850/*
3851 * Returns the file-relative block number of the first unused block(s)
3852 * in the file with at least "len" logically contiguous blocks free.
3853 * This is the lowest-address hole if the file has holes, else the first block
3854 * past the end of file.
3855 * Return 0 if the file is currently local (in-inode).
3856 */
3857int /* error */
3858xfs_bmap_first_unused(
3859 xfs_trans_t *tp, /* transaction pointer */
3860 xfs_inode_t *ip, /* incore inode */
3861 xfs_extlen_t len, /* size of hole to find */
3862 xfs_fileoff_t *first_unused, /* unused block */
3863 int whichfork) /* data or attr fork */
3864{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003866 int idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 xfs_ifork_t *ifp; /* inode fork pointer */
3868 xfs_fileoff_t lastaddr; /* last block number seen */
3869 xfs_fileoff_t lowest; /* lowest useful block */
3870 xfs_fileoff_t max; /* starting useful block */
3871 xfs_fileoff_t off; /* offset for this block */
3872 xfs_extnum_t nextents; /* number of extent entries */
3873
3874 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3875 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3876 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3877 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3878 *first_unused = 0;
3879 return 0;
3880 }
3881 ifp = XFS_IFORK_PTR(ip, whichfork);
3882 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3883 (error = xfs_iread_extents(tp, ip, whichfork)))
3884 return error;
3885 lowest = *first_unused;
3886 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003887 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003888 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 off = xfs_bmbt_get_startoff(ep);
3890 /*
3891 * See if the hole before this extent will work.
3892 */
3893 if (off >= lowest + len && off - max >= len) {
3894 *first_unused = max;
3895 return 0;
3896 }
3897 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3898 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3899 }
3900 *first_unused = max;
3901 return 0;
3902}
3903
3904/*
3905 * Returns the file-relative block number of the last block + 1 before
3906 * last_block (input value) in the file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003907 * This is not based on i_size, it is based on the extent records.
3908 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 */
3910int /* error */
3911xfs_bmap_last_before(
3912 xfs_trans_t *tp, /* transaction pointer */
3913 xfs_inode_t *ip, /* incore inode */
3914 xfs_fileoff_t *last_block, /* last block */
3915 int whichfork) /* data or attr fork */
3916{
3917 xfs_fileoff_t bno; /* input file offset */
3918 int eof; /* hit end of file */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003919 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 int error; /* error return value */
3921 xfs_bmbt_irec_t got; /* current extent value */
3922 xfs_ifork_t *ifp; /* inode fork pointer */
3923 xfs_extnum_t lastx; /* last extent used */
3924 xfs_bmbt_irec_t prev; /* previous extent value */
3925
3926 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3927 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3928 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3929 return XFS_ERROR(EIO);
3930 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3931 *last_block = 0;
3932 return 0;
3933 }
3934 ifp = XFS_IFORK_PTR(ip, whichfork);
3935 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3936 (error = xfs_iread_extents(tp, ip, whichfork)))
3937 return error;
3938 bno = *last_block - 1;
3939 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3940 &prev);
3941 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3942 if (prev.br_startoff == NULLFILEOFF)
3943 *last_block = 0;
3944 else
3945 *last_block = prev.br_startoff + prev.br_blockcount;
3946 }
3947 /*
3948 * Otherwise *last_block is already the right answer.
3949 */
3950 return 0;
3951}
3952
3953/*
3954 * Returns the file-relative block number of the first block past eof in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003955 * the file. This is not based on i_size, it is based on the extent records.
3956 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 */
3958int /* error */
3959xfs_bmap_last_offset(
3960 xfs_trans_t *tp, /* transaction pointer */
3961 xfs_inode_t *ip, /* incore inode */
3962 xfs_fileoff_t *last_block, /* last block */
3963 int whichfork) /* data or attr fork */
3964{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003965 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 int error; /* error return value */
3967 xfs_ifork_t *ifp; /* inode fork pointer */
3968 xfs_extnum_t nextents; /* number of extent entries */
3969
3970 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3971 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3972 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3973 return XFS_ERROR(EIO);
3974 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3975 *last_block = 0;
3976 return 0;
3977 }
3978 ifp = XFS_IFORK_PTR(ip, whichfork);
3979 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3980 (error = xfs_iread_extents(tp, ip, whichfork)))
3981 return error;
3982 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3983 if (!nextents) {
3984 *last_block = 0;
3985 return 0;
3986 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003987 ep = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
3989 return 0;
3990}
3991
3992/*
3993 * Returns whether the selected fork of the inode has exactly one
3994 * block or not. For the data fork we check this matches di_size,
3995 * implying the file's range is 0..bsize-1.
3996 */
3997int /* 1=>1 block, 0=>otherwise */
3998xfs_bmap_one_block(
3999 xfs_inode_t *ip, /* incore inode */
4000 int whichfork) /* data or attr fork */
4001{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004002 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 xfs_ifork_t *ifp; /* inode fork pointer */
4004 int rval; /* return value */
4005 xfs_bmbt_irec_t s; /* internal version of extent */
4006
4007#ifndef DEBUG
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004008 if (whichfork == XFS_DATA_FORK) {
Al Viroabbede12011-07-26 02:31:30 -04004009 return S_ISREG(ip->i_d.di_mode) ?
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004010 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4011 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013#endif /* !DEBUG */
4014 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4015 return 0;
4016 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4017 return 0;
4018 ifp = XFS_IFORK_PTR(ip, whichfork);
4019 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004020 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 xfs_bmbt_get_all(ep, &s);
4022 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4023 if (rval && whichfork == XFS_DATA_FORK)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004024 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 return rval;
4026}
4027
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004028STATIC int
4029xfs_bmap_sanity_check(
4030 struct xfs_mount *mp,
4031 struct xfs_buf *bp,
4032 int level)
4033{
4034 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4035
Christoph Hellwig69ef9212011-07-08 14:36:05 +02004036 if (block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC) ||
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004037 be16_to_cpu(block->bb_level) != level ||
4038 be16_to_cpu(block->bb_numrecs) == 0 ||
4039 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4040 return 0;
4041 return 1;
4042}
4043
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044/*
4045 * Read in the extents to if_extents.
4046 * All inode fields are set up by caller, we just traverse the btree
4047 * and copy the records in. If the file system cannot contain unwritten
4048 * extents, the records are checked for no "state" flags.
4049 */
4050int /* error */
4051xfs_bmap_read_extents(
4052 xfs_trans_t *tp, /* transaction pointer */
4053 xfs_inode_t *ip, /* incore inode */
4054 int whichfork) /* data or attr fork */
4055{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004056 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 xfs_fsblock_t bno; /* block # of "block" */
4058 xfs_buf_t *bp; /* buffer for "block" */
4059 int error; /* error return value */
4060 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 xfs_extnum_t i, j; /* index into the extents list */
4062 xfs_ifork_t *ifp; /* fork structure */
4063 int level; /* btree level, for checking */
4064 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10004065 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 /* REFERENCED */
4067 xfs_extnum_t room; /* number of entries there's room for */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
4069 bno = NULLFSBLOCK;
4070 mp = ip->i_mount;
4071 ifp = XFS_IFORK_PTR(ip, whichfork);
4072 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4073 XFS_EXTFMT_INODE(ip);
4074 block = ifp->if_broot;
4075 /*
4076 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4077 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004078 level = be16_to_cpu(block->bb_level);
4079 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11004080 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004081 bno = be64_to_cpu(*pp);
4082 ASSERT(bno != NULLDFSBNO);
4083 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4084 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 /*
4086 * Go down the tree until leaf level is reached, following the first
4087 * pointer (leftmost) at each level.
4088 */
4089 while (level-- > 0) {
4090 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4091 XFS_BMAP_BTREE_REF)))
4092 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004093 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004095 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 error0);
4097 if (level == 0)
4098 break;
Christoph Hellwig136341b2008-10-30 17:11:40 +11004099 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004100 bno = be64_to_cpu(*pp);
4101 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 xfs_trans_brelse(tp, bp);
4103 }
4104 /*
4105 * Here with bp and block set to the leftmost leaf node in the tree.
4106 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004107 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 i = 0;
4109 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004110 * Loop over all leaf nodes. Copy information to the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 */
4112 for (;;) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004113 xfs_bmbt_rec_t *frp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 xfs_fsblock_t nextbno;
4115 xfs_extnum_t num_recs;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004116 xfs_extnum_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
4118
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004119 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 if (unlikely(i + num_recs > room)) {
4121 ASSERT(i + num_recs <= room);
Dave Chinner65333b42011-03-07 10:03:35 +11004122 xfs_warn(ip->i_mount,
Nathan Scott3762ec62006-01-12 10:29:53 +11004123 "corrupt dinode %Lu, (btree extents).",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 (unsigned long long) ip->i_ino);
Dave Chinner65333b42011-03-07 10:03:35 +11004125 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
4126 XFS_ERRLEVEL_LOW, ip->i_mount, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 goto error0;
4128 }
4129 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004130 xfs_bmap_sanity_check(mp, bp, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 error0);
4132 /*
4133 * Read-ahead the next leaf block, if any.
4134 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004135 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 if (nextbno != NULLFSBLOCK)
4137 xfs_btree_reada_bufl(mp, nextbno, 1);
4138 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004139 * Copy records into the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11004141 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004142 start = i;
4143 for (j = 0; j < num_recs; j++, i++, frp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004144 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10004145 trp->l0 = be64_to_cpu(frp->l0);
4146 trp->l1 = be64_to_cpu(frp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 }
4148 if (exntf == XFS_EXTFMT_NOSTATE) {
4149 /*
4150 * Check all attribute bmap btree records and
4151 * any "older" data bmap btree records for a
4152 * set bit in the "extent flag" position.
4153 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004154 if (unlikely(xfs_check_nostate_extents(ifp,
4155 start, num_recs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4157 XFS_ERRLEVEL_LOW,
4158 ip->i_mount);
4159 goto error0;
4160 }
4161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 xfs_trans_brelse(tp, bp);
4163 bno = nextbno;
4164 /*
4165 * If we've reached the end, stop.
4166 */
4167 if (bno == NULLFSBLOCK)
4168 break;
4169 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4170 XFS_BMAP_BTREE_REF)))
4171 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004172 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004174 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
Eric Sandeen3a59c942007-07-11 11:09:47 +10004176 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 return 0;
4178error0:
4179 xfs_trans_brelse(tp, bp);
4180 return XFS_ERROR(EFSCORRUPTED);
4181}
4182
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004183#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004185 * Add bmap trace insert entries for all the contents of the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 */
4187void
4188xfs_bmap_trace_exlist(
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 xfs_inode_t *ip, /* incore inode pointer */
4190 xfs_extnum_t cnt, /* count of entries in the list */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004191 int whichfork, /* data or attr fork */
4192 unsigned long caller_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004194 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004196 int state = 0;
4197
4198 if (whichfork == XFS_ATTR_FORK)
4199 state |= BMAP_ATTRFORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
4201 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004202 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004203 for (idx = 0; idx < cnt; idx++)
4204 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207/*
4208 * Validate that the bmbt_irecs being returned from bmapi are valid
4209 * given the callers original parameters. Specifically check the
4210 * ranges of the returned irecs to ensure that they only extent beyond
4211 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4212 */
4213STATIC void
4214xfs_bmap_validate_ret(
4215 xfs_fileoff_t bno,
4216 xfs_filblks_t len,
4217 int flags,
4218 xfs_bmbt_irec_t *mval,
4219 int nmap,
4220 int ret_nmap)
4221{
4222 int i; /* index to map values */
4223
4224 ASSERT(ret_nmap <= nmap);
4225
4226 for (i = 0; i < ret_nmap; i++) {
4227 ASSERT(mval[i].br_blockcount > 0);
4228 if (!(flags & XFS_BMAPI_ENTIRE)) {
4229 ASSERT(mval[i].br_startoff >= bno);
4230 ASSERT(mval[i].br_blockcount <= len);
4231 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4232 bno + len);
4233 } else {
4234 ASSERT(mval[i].br_startoff < bno + len);
4235 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4236 bno);
4237 }
4238 ASSERT(i == 0 ||
4239 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4240 mval[i].br_startoff);
4241 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4242 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4243 mval[i].br_startblock != HOLESTARTBLOCK);
4244 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4245 mval[i].br_state == XFS_EXT_UNWRITTEN);
4246 }
4247}
4248#endif /* DEBUG */
4249
4250
4251/*
Dave Chinneraef9a892011-09-18 20:40:44 +00004252 * Trim the returned map to the required bounds
4253 */
4254STATIC void
4255xfs_bmapi_trim_map(
4256 struct xfs_bmbt_irec *mval,
4257 struct xfs_bmbt_irec *got,
4258 xfs_fileoff_t *bno,
4259 xfs_filblks_t len,
4260 xfs_fileoff_t obno,
4261 xfs_fileoff_t end,
4262 int n,
4263 int flags)
4264{
4265 if ((flags & XFS_BMAPI_ENTIRE) ||
4266 got->br_startoff + got->br_blockcount <= obno) {
4267 *mval = *got;
4268 if (isnullstartblock(got->br_startblock))
4269 mval->br_startblock = DELAYSTARTBLOCK;
4270 return;
4271 }
4272
4273 if (obno > *bno)
4274 *bno = obno;
4275 ASSERT((*bno >= obno) || (n == 0));
4276 ASSERT(*bno < end);
4277 mval->br_startoff = *bno;
4278 if (isnullstartblock(got->br_startblock))
4279 mval->br_startblock = DELAYSTARTBLOCK;
4280 else
4281 mval->br_startblock = got->br_startblock +
4282 (*bno - got->br_startoff);
4283 /*
4284 * Return the minimum of what we got and what we asked for for
4285 * the length. We can use the len variable here because it is
4286 * modified below and we could have been there before coming
4287 * here if the first part of the allocation didn't overlap what
4288 * was asked for.
4289 */
4290 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
4291 got->br_blockcount - (*bno - got->br_startoff));
4292 mval->br_state = got->br_state;
4293 ASSERT(mval->br_blockcount <= len);
4294 return;
4295}
4296
4297/*
4298 * Update and validate the extent map to return
4299 */
4300STATIC void
4301xfs_bmapi_update_map(
4302 struct xfs_bmbt_irec **map,
4303 xfs_fileoff_t *bno,
4304 xfs_filblks_t *len,
4305 xfs_fileoff_t obno,
4306 xfs_fileoff_t end,
4307 int *n,
4308 int flags)
4309{
4310 xfs_bmbt_irec_t *mval = *map;
4311
4312 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4313 ((mval->br_startoff + mval->br_blockcount) <= end));
4314 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4315 (mval->br_startoff < obno));
4316
4317 *bno = mval->br_startoff + mval->br_blockcount;
4318 *len = end - *bno;
4319 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4320 /* update previous map with new information */
4321 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4322 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4323 ASSERT(mval->br_state == mval[-1].br_state);
4324 mval[-1].br_blockcount = mval->br_blockcount;
4325 mval[-1].br_state = mval->br_state;
4326 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4327 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4328 mval[-1].br_startblock != HOLESTARTBLOCK &&
4329 mval->br_startblock == mval[-1].br_startblock +
4330 mval[-1].br_blockcount &&
4331 ((flags & XFS_BMAPI_IGSTATE) ||
4332 mval[-1].br_state == mval->br_state)) {
4333 ASSERT(mval->br_startoff ==
4334 mval[-1].br_startoff + mval[-1].br_blockcount);
4335 mval[-1].br_blockcount += mval->br_blockcount;
4336 } else if (*n > 0 &&
4337 mval->br_startblock == DELAYSTARTBLOCK &&
4338 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4339 mval->br_startoff ==
4340 mval[-1].br_startoff + mval[-1].br_blockcount) {
4341 mval[-1].br_blockcount += mval->br_blockcount;
4342 mval[-1].br_state = mval->br_state;
4343 } else if (!((*n == 0) &&
4344 ((mval->br_startoff + mval->br_blockcount) <=
4345 obno))) {
4346 mval++;
4347 (*n)++;
4348 }
4349 *map = mval;
4350}
4351
4352/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 * Map file blocks to filesystem blocks.
4354 * File range is given by the bno/len pair.
4355 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4356 * into a hole or past eof.
4357 * Only allocates blocks from a single allocation group,
4358 * to avoid locking problems.
4359 * The returned value in "firstblock" from the first call in a transaction
4360 * must be remembered and presented to subsequent calls in "firstblock".
4361 * An upper bound for the number of blocks to be allocated is supplied to
4362 * the first call in "total"; if no allocation group has that many free
4363 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4364 */
4365int /* error */
4366xfs_bmapi(
4367 xfs_trans_t *tp, /* transaction pointer */
4368 xfs_inode_t *ip, /* incore inode */
4369 xfs_fileoff_t bno, /* starting file offs. mapped */
4370 xfs_filblks_t len, /* length to map in file */
4371 int flags, /* XFS_BMAPI_... */
4372 xfs_fsblock_t *firstblock, /* first allocated block
4373 controls a.g. for allocs */
4374 xfs_extlen_t total, /* total blocks needed */
4375 xfs_bmbt_irec_t *mval, /* output: map values */
4376 int *nmap, /* i/o: mval size/count */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004377 xfs_bmap_free_t *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378{
4379 xfs_fsblock_t abno; /* allocated block number */
4380 xfs_extlen_t alen; /* allocated extent length */
4381 xfs_fileoff_t aoff; /* allocated file offset */
Dave Chinnerf0a0eaa2010-01-20 10:50:06 +11004382 xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 xfs_btree_cur_t *cur; /* bmap btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 xfs_fileoff_t end; /* end of mapped file region */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004385 int eof; /* we've hit the end of extents */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004386 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004388 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 xfs_ifork_t *ifp; /* inode fork pointer */
4390 xfs_extlen_t indlen; /* indirect blocks length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 xfs_extnum_t lastx; /* last useful extent number */
4392 int logflags; /* flags for transaction logging */
4393 xfs_extlen_t minleft; /* min blocks left after allocation */
4394 xfs_extlen_t minlen; /* min allocation size */
4395 xfs_mount_t *mp; /* xfs mount structure */
4396 int n; /* current extent index */
Malcolm Parsons9da096f2009-03-29 09:55:42 +02004397 int nallocs; /* number of extents alloc'd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 xfs_fileoff_t obno; /* old block number (offset) */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004399 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 int tmp_logflags; /* temp flags holder */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004401 int whichfork; /* data or attr fork */
4402 char inhole; /* current location is hole in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 char wasdelay; /* old extent was delayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 char wr; /* this is a write request */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004405 char rt; /* this is a realtime file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406#ifdef DEBUG
4407 xfs_fileoff_t orig_bno; /* original block number value */
4408 int orig_flags; /* original flags arg value */
4409 xfs_filblks_t orig_len; /* original value of len arg */
4410 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4411 int orig_nmap; /* original value of *nmap */
4412
4413 orig_bno = bno;
4414 orig_len = len;
4415 orig_flags = flags;
4416 orig_mval = mval;
4417 orig_nmap = *nmap;
4418#endif
4419 ASSERT(*nmap >= 1);
4420 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4421 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4422 XFS_ATTR_FORK : XFS_DATA_FORK;
4423 mp = ip->i_mount;
4424 if (unlikely(XFS_TEST_ERROR(
4425 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4426 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4427 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4428 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4429 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4430 return XFS_ERROR(EFSCORRUPTED);
4431 }
4432 if (XFS_FORCED_SHUTDOWN(mp))
4433 return XFS_ERROR(EIO);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004434 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 ifp = XFS_IFORK_PTR(ip, whichfork);
4436 ASSERT(ifp->if_ext_max ==
4437 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4438 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4439 XFS_STATS_INC(xs_blk_mapw);
4440 else
4441 XFS_STATS_INC(xs_blk_mapr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 /*
Nathan Scott39269e22006-03-14 13:33:50 +11004443 * IGSTATE flag is used to combine extents which
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 * differ only due to the state of the extents.
4445 * This technique is used from xfs_getbmap()
4446 * when the caller does not wish to see the
4447 * separation (which is the default).
4448 *
4449 * This technique is also used when writing a
4450 * buffer which has been partially written,
4451 * (usually by being flushed during a chunkread),
4452 * to ensure one write takes place. This also
4453 * prevents a change in the xfs inode extents at
4454 * this time, intentionally. This change occurs
4455 * on completion of the write operation, in
4456 * xfs_strat_comp(), where the xfs_bmapi() call
4457 * is transactioned, and the extents combined.
4458 */
Nathan Scott39269e22006-03-14 13:33:50 +11004459 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4460 wr = 0; /* no allocations are allowed */
4461 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 logflags = 0;
4463 nallocs = 0;
4464 cur = NULL;
4465 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4466 ASSERT(wr && tp);
4467 if ((error = xfs_bmap_local_to_extents(tp, ip,
4468 firstblock, total, &logflags, whichfork)))
4469 goto error0;
4470 }
4471 if (wr && *firstblock == NULLFSBLOCK) {
4472 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Christoph Hellwig16259e72005-11-02 15:11:25 +11004473 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 else
4475 minleft = 1;
4476 } else
4477 minleft = 0;
4478 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4479 (error = xfs_iread_extents(tp, ip, whichfork)))
4480 goto error0;
4481 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4482 &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 n = 0;
4484 end = bno + len;
4485 obno = bno;
4486 bma.ip = NULL;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004487
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 while (bno < end && n < *nmap) {
4489 /*
4490 * Reading past eof, act as though there's a hole
4491 * up to end.
4492 */
4493 if (eof && !wr)
4494 got.br_startoff = end;
4495 inhole = eof || got.br_startoff > bno;
Nathan Scott39269e22006-03-14 13:33:50 +11004496 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06004497 isnullstartblock(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 /*
4499 * First, deal with the hole before the allocated space
4500 * that we found, if any.
4501 */
4502 if (wr && (inhole || wasdelay)) {
4503 /*
4504 * For the wasdelay case, we could also just
4505 * allocate the stuff asked for in this bmap call
4506 * but that wouldn't be as good.
4507 */
Christoph Hellwigcd8b0bb2010-06-23 18:11:15 +10004508 if (wasdelay) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 alen = (xfs_extlen_t)got.br_blockcount;
4510 aoff = got.br_startoff;
4511 if (lastx != NULLEXTNUM && lastx) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004512 ep = xfs_iext_get_ext(ifp, lastx - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 xfs_bmbt_get_all(ep, &prev);
4514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 } else {
4516 alen = (xfs_extlen_t)
4517 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4518 if (!eof)
4519 alen = (xfs_extlen_t)
4520 XFS_FILBLKS_MIN(alen,
4521 got.br_startoff - bno);
4522 aoff = bno;
4523 }
Nathan Scott39269e22006-03-14 13:33:50 +11004524 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4525 if (flags & XFS_BMAPI_DELAY) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11004526 xfs_extlen_t extsz;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004527
4528 /* Figure out the extent size, adjust alen */
David Chinner957d0eb2007-06-18 16:50:37 +10004529 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004530 if (extsz) {
Dave Chinner4ce15982011-01-27 12:17:58 +11004531 /*
4532 * make sure we don't exceed a single
4533 * extent length when we align the
4534 * extent by reducing length we are
4535 * going to allocate by the maximum
4536 * amount extent size aligment may
4537 * require.
4538 */
4539 alen = XFS_FILBLKS_MIN(len,
4540 MAXEXTLEN - (2 * extsz - 1));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004541 error = xfs_bmap_extsize_align(mp,
4542 &got, &prev, extsz,
Nathan Scott39269e22006-03-14 13:33:50 +11004543 rt, eof,
4544 flags&XFS_BMAPI_DELAY,
4545 flags&XFS_BMAPI_CONVERT,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004546 &aoff, &alen);
4547 ASSERT(!error);
4548 }
4549
4550 if (rt)
4551 extsz = alen / mp->m_sb.sb_rextsize;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004552
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 /*
4554 * Make a transaction-less quota reservation for
4555 * delayed allocation blocks. This number gets
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004556 * adjusted later. We return if we haven't
4557 * allocated blocks already inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02004559 error = xfs_trans_reserve_quota_nblks(
4560 NULL, ip, (long)alen, 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004561 rt ? XFS_QMOPT_RES_RTBLKS :
Christoph Hellwig7d095252009-06-08 15:33:32 +02004562 XFS_QMOPT_RES_REGBLKS);
4563 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 if (n == 0) {
4565 *nmap = 0;
4566 ASSERT(cur == NULL);
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004567 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 }
4569 break;
4570 }
4571
4572 /*
4573 * Split changing sb for alen and indlen since
4574 * they could be coming from different places.
4575 */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004576 indlen = (xfs_extlen_t)
4577 xfs_bmap_worst_indlen(ip, alen);
4578 ASSERT(indlen > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
Nathan Scottdd9f4382006-01-11 15:28:28 +11004580 if (rt) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004581 error = xfs_mod_incore_sb(mp,
4582 XFS_SBS_FREXTENTS,
Christoph Hellwig54893272011-05-11 15:04:03 +00004583 -((int64_t)extsz), 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004584 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00004585 error = xfs_icsb_modify_counters(mp,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004586 XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00004587 -((int64_t)alen), 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004588 }
David Chinner3bdbfb12005-09-02 16:40:47 +10004589 if (!error) {
Christoph Hellwig96540c72010-09-30 02:25:55 +00004590 error = xfs_icsb_modify_counters(mp,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004591 XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00004592 -((int64_t)indlen), 0);
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004593 if (error && rt)
4594 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004595 XFS_SBS_FREXTENTS,
Christoph Hellwig54893272011-05-11 15:04:03 +00004596 (int64_t)extsz, 0);
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004597 else if (error)
Christoph Hellwig96540c72010-09-30 02:25:55 +00004598 xfs_icsb_modify_counters(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004599 XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00004600 (int64_t)alen, 0);
David Chinner3bdbfb12005-09-02 16:40:47 +10004601 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004602
4603 if (error) {
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004604 if (XFS_IS_QUOTA_ON(mp))
Nathan Scott06d10dd2005-06-21 15:48:47 +10004605 /* unreserve the blocks now */
Nathan Scottdd9f4382006-01-11 15:28:28 +11004606 (void)
Christoph Hellwig7d095252009-06-08 15:33:32 +02004607 xfs_trans_unreserve_quota_nblks(
4608 NULL, ip,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004609 (long)alen, 0, rt ?
4610 XFS_QMOPT_RES_RTBLKS :
4611 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 break;
4613 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004614
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 ip->i_delayed_blks += alen;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004616 abno = nullstartblock(indlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 } else {
4618 /*
4619 * If first time, allocate and fill in
4620 * once-only bma fields.
4621 */
4622 if (bma.ip == NULL) {
4623 bma.tp = tp;
4624 bma.ip = ip;
4625 bma.prevp = &prev;
4626 bma.gotp = &got;
4627 bma.total = total;
4628 bma.userdata = 0;
4629 }
4630 /* Indicate if this is the first user data
4631 * in the file, or just any user data.
4632 */
Nathan Scott39269e22006-03-14 13:33:50 +11004633 if (!(flags & XFS_BMAPI_METADATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 bma.userdata = (aoff == 0) ?
4635 XFS_ALLOC_INITIAL_USER_DATA :
4636 XFS_ALLOC_USERDATA;
4637 }
4638 /*
4639 * Fill in changeable bma fields.
4640 */
4641 bma.eof = eof;
4642 bma.firstblock = *firstblock;
4643 bma.alen = alen;
4644 bma.off = aoff;
Adrian Bunk72880262006-08-30 13:41:58 +10004645 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 bma.wasdel = wasdelay;
4647 bma.minlen = minlen;
4648 bma.low = flist->xbf_low;
4649 bma.minleft = minleft;
4650 /*
4651 * Only want to do the alignment at the
4652 * eof if it is userdata and allocation length
4653 * is larger than a stripe unit.
4654 */
4655 if (mp->m_dalign && alen >= mp->m_dalign &&
Nathan Scott39269e22006-03-14 13:33:50 +11004656 (!(flags & XFS_BMAPI_METADATA)) &&
4657 (whichfork == XFS_DATA_FORK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 if ((error = xfs_bmap_isaeof(ip, aoff,
4659 whichfork, &bma.aeof)))
4660 goto error0;
4661 } else
4662 bma.aeof = 0;
4663 /*
4664 * Call allocator.
4665 */
4666 if ((error = xfs_bmap_alloc(&bma)))
4667 goto error0;
4668 /*
4669 * Copy out result fields.
4670 */
4671 abno = bma.rval;
4672 if ((flist->xbf_low = bma.low))
4673 minleft = 0;
4674 alen = bma.alen;
4675 aoff = bma.off;
4676 ASSERT(*firstblock == NULLFSBLOCK ||
4677 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4678 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4679 (flist->xbf_low &&
4680 XFS_FSB_TO_AGNO(mp, *firstblock) <
4681 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4682 *firstblock = bma.firstblock;
4683 if (cur)
4684 cur->bc_private.b.firstblock =
4685 *firstblock;
4686 if (abno == NULLFSBLOCK)
4687 break;
4688 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11004689 cur = xfs_bmbt_init_cursor(mp, tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 ip, whichfork);
4691 cur->bc_private.b.firstblock =
4692 *firstblock;
4693 cur->bc_private.b.flist = flist;
4694 }
4695 /*
4696 * Bump the number of extents we've allocated
4697 * in this call.
4698 */
4699 nallocs++;
4700 }
4701 if (cur)
4702 cur->bc_private.b.flags =
4703 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4704 got.br_startoff = aoff;
4705 got.br_startblock = abno;
4706 got.br_blockcount = alen;
4707 got.br_state = XFS_EXT_NORM; /* assume normal */
4708 /*
4709 * Determine state of extent, and the filesystem.
4710 * A wasdelay extent has been initialized, so
4711 * shouldn't be flagged as unwritten.
4712 */
Eric Sandeen62118702008-03-06 13:44:28 +11004713 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
4715 got.br_state = XFS_EXT_UNWRITTEN;
4716 }
Christoph Hellwigf3ca87382011-07-08 14:34:47 +02004717 error = xfs_bmap_add_extent(tp, ip, &lastx, &cur, &got,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004718 firstblock, flist, &tmp_logflags,
Christoph Hellwig54893272011-05-11 15:04:03 +00004719 whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 logflags |= tmp_logflags;
4721 if (error)
4722 goto error0;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004723 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 xfs_bmbt_get_all(ep, &got);
4725 ASSERT(got.br_startoff <= aoff);
4726 ASSERT(got.br_startoff + got.br_blockcount >=
4727 aoff + alen);
4728#ifdef DEBUG
Nathan Scott39269e22006-03-14 13:33:50 +11004729 if (flags & XFS_BMAPI_DELAY) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06004730 ASSERT(isnullstartblock(got.br_startblock));
4731 ASSERT(startblockval(got.br_startblock) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 }
4733 ASSERT(got.br_state == XFS_EXT_NORM ||
4734 got.br_state == XFS_EXT_UNWRITTEN);
4735#endif
4736 /*
4737 * Fall down into the found allocated space case.
4738 */
4739 } else if (inhole) {
4740 /*
4741 * Reading in a hole.
4742 */
4743 mval->br_startoff = bno;
4744 mval->br_startblock = HOLESTARTBLOCK;
4745 mval->br_blockcount =
4746 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4747 mval->br_state = XFS_EXT_NORM;
4748 bno += mval->br_blockcount;
4749 len -= mval->br_blockcount;
4750 mval++;
4751 n++;
4752 continue;
4753 }
Dave Chinneraef9a892011-09-18 20:40:44 +00004754
4755 /* Deal with the allocated space we found. */
4756 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
4758 /*
4759 * Check if writing previously allocated but
4760 * unwritten extents.
4761 */
Dave Chinner44722352010-08-24 12:02:11 +10004762 if (wr &&
4763 ((mval->br_state == XFS_EXT_UNWRITTEN &&
4764 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) ||
4765 (mval->br_state == XFS_EXT_NORM &&
4766 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT)) ==
4767 (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 /*
4769 * Modify (by adding) the state flag, if writing.
4770 */
4771 ASSERT(mval->br_blockcount <= len);
4772 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11004773 cur = xfs_bmbt_init_cursor(mp,
4774 tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 cur->bc_private.b.firstblock =
4776 *firstblock;
4777 cur->bc_private.b.flist = flist;
4778 }
Dave Chinner44722352010-08-24 12:02:11 +10004779 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4780 ? XFS_EXT_NORM
4781 : XFS_EXT_UNWRITTEN;
Christoph Hellwigf3ca87382011-07-08 14:34:47 +02004782 error = xfs_bmap_add_extent(tp, ip, &lastx, &cur, mval,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004783 firstblock, flist, &tmp_logflags,
Christoph Hellwig54893272011-05-11 15:04:03 +00004784 whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 logflags |= tmp_logflags;
4786 if (error)
4787 goto error0;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004788 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 xfs_bmbt_get_all(ep, &got);
4790 /*
4791 * We may have combined previously unwritten
4792 * space with written space, so generate
4793 * another request.
4794 */
4795 if (mval->br_blockcount < len)
4796 continue;
4797 }
4798
Dave Chinneraef9a892011-09-18 20:40:44 +00004799 /* update the extent map to return */
4800 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4801
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 /*
4803 * If we're done, stop now. Stop when we've allocated
4804 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4805 * the transaction may get too big.
4806 */
4807 if (bno >= end || n >= *nmap || nallocs >= *nmap)
4808 break;
4809 /*
4810 * Else go on to the next record.
4811 */
David Chinner4e5ae832007-06-05 16:24:15 +10004812 prev = got;
Christoph Hellwigecee76b2011-09-18 20:40:43 +00004813 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig5690f922011-05-11 15:04:07 +00004814 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig5690f922011-05-11 15:04:07 +00004816 } else {
4817 eof = 1;
4818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 *nmap = n;
4821 /*
4822 * Transform from btree to extents, give it cur.
4823 */
4824 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4825 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4826 ASSERT(wr && cur);
4827 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4828 &tmp_logflags, whichfork);
4829 logflags |= tmp_logflags;
4830 if (error)
4831 goto error0;
4832 }
4833 ASSERT(ifp->if_ext_max ==
4834 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4835 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4836 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
4837 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838error0:
4839 /*
4840 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004841 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06004843 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06004845 logflags &= ~xfs_ilog_fext(whichfork);
4846 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06004848 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 /*
4850 * Log whatever the flags say, even if error. Otherwise we might miss
4851 * detecting a case where the data is changed, there's an error,
4852 * and it's not logged so we don't shutdown when we should.
4853 */
4854 if (logflags) {
4855 ASSERT(tp && wr);
4856 xfs_trans_log_inode(tp, ip, logflags);
4857 }
4858 if (cur) {
4859 if (!error) {
4860 ASSERT(*firstblock == NULLFSBLOCK ||
4861 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4862 XFS_FSB_TO_AGNO(mp,
4863 cur->bc_private.b.firstblock) ||
4864 (flist->xbf_low &&
4865 XFS_FSB_TO_AGNO(mp, *firstblock) <
4866 XFS_FSB_TO_AGNO(mp,
4867 cur->bc_private.b.firstblock)));
4868 *firstblock = cur->bc_private.b.firstblock;
4869 }
4870 xfs_btree_del_cursor(cur,
4871 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4872 }
4873 if (!error)
4874 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4875 orig_nmap, *nmap);
4876 return error;
4877}
4878
4879/*
4880 * Map file blocks to filesystem blocks, simple version.
4881 * One block (extent) only, read-only.
4882 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
4883 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
4884 * was set and all the others were clear.
4885 */
4886int /* error */
4887xfs_bmapi_single(
4888 xfs_trans_t *tp, /* transaction pointer */
4889 xfs_inode_t *ip, /* incore inode */
4890 int whichfork, /* data or attr fork */
4891 xfs_fsblock_t *fsb, /* output: mapped block */
4892 xfs_fileoff_t bno) /* starting file offs. mapped */
4893{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004894 int eof; /* we've hit the end of extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004896 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 xfs_ifork_t *ifp; /* inode fork pointer */
4898 xfs_extnum_t lastx; /* last useful extent number */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004899 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
4901 ifp = XFS_IFORK_PTR(ip, whichfork);
4902 if (unlikely(
4903 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4904 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
4905 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
4906 ip->i_mount);
4907 return XFS_ERROR(EFSCORRUPTED);
4908 }
4909 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
4910 return XFS_ERROR(EIO);
4911 XFS_STATS_INC(xs_blk_mapr);
4912 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4913 (error = xfs_iread_extents(tp, ip, whichfork)))
4914 return error;
4915 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4916 &prev);
4917 /*
4918 * Reading past eof, act as though there's a hole
4919 * up to end.
4920 */
4921 if (eof || got.br_startoff > bno) {
4922 *fsb = NULLFSBLOCK;
4923 return 0;
4924 }
Eric Sandeen9d87c312009-01-14 23:22:07 -06004925 ASSERT(!isnullstartblock(got.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 ASSERT(bno < got.br_startoff + got.br_blockcount);
4927 *fsb = got.br_startblock + (bno - got.br_startoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 return 0;
4929}
4930
4931/*
4932 * Unmap (remove) blocks from a file.
4933 * If nexts is nonzero then the number of extents to remove is limited to
4934 * that value. If not all extents in the block range can be removed then
4935 * *done is set.
4936 */
4937int /* error */
4938xfs_bunmapi(
4939 xfs_trans_t *tp, /* transaction pointer */
4940 struct xfs_inode *ip, /* incore inode */
4941 xfs_fileoff_t bno, /* starting offset to unmap */
4942 xfs_filblks_t len, /* length to unmap in file */
4943 int flags, /* misc flags */
4944 xfs_extnum_t nexts, /* number of extents max */
4945 xfs_fsblock_t *firstblock, /* first allocated block
4946 controls a.g. for allocs */
4947 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4948 int *done) /* set if not done yet */
4949{
4950 xfs_btree_cur_t *cur; /* bmap btree cursor */
4951 xfs_bmbt_irec_t del; /* extent being deleted */
4952 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004953 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 int error; /* error return value */
4955 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004956 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 xfs_ifork_t *ifp; /* inode fork pointer */
4958 int isrt; /* freeing in rt area */
4959 xfs_extnum_t lastx; /* last extent index used */
4960 int logflags; /* transaction logging flags */
4961 xfs_extlen_t mod; /* rt extent offset */
4962 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004963 xfs_extnum_t nextents; /* number of file extents */
4964 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 xfs_fileoff_t start; /* first file offset deleted */
4966 int tmp_logflags; /* partial logging flags */
4967 int wasdel; /* was a delayed alloc extent */
4968 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 xfs_fsblock_t sum;
4970
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004971 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
4972
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4974 XFS_ATTR_FORK : XFS_DATA_FORK;
4975 ifp = XFS_IFORK_PTR(ip, whichfork);
4976 if (unlikely(
4977 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4978 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
4979 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
4980 ip->i_mount);
4981 return XFS_ERROR(EFSCORRUPTED);
4982 }
4983 mp = ip->i_mount;
4984 if (XFS_FORCED_SHUTDOWN(mp))
4985 return XFS_ERROR(EIO);
Christoph Hellwig54893272011-05-11 15:04:03 +00004986
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 ASSERT(len > 0);
4988 ASSERT(nexts >= 0);
4989 ASSERT(ifp->if_ext_max ==
4990 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4991 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4992 (error = xfs_iread_extents(tp, ip, whichfork)))
4993 return error;
4994 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4995 if (nextents == 0) {
4996 *done = 1;
4997 return 0;
4998 }
4999 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005000 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 start = bno;
5002 bno = start + len - 1;
5003 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5004 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005005
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 /*
5007 * Check to see if the given block number is past the end of the
5008 * file, back up to the last block if so...
5009 */
5010 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005011 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 xfs_bmbt_get_all(ep, &got);
5013 bno = got.br_startoff + got.br_blockcount - 1;
5014 }
5015 logflags = 0;
5016 if (ifp->if_flags & XFS_IFBROOT) {
5017 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005018 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 cur->bc_private.b.firstblock = *firstblock;
5020 cur->bc_private.b.flist = flist;
5021 cur->bc_private.b.flags = 0;
5022 } else
5023 cur = NULL;
5024 extno = 0;
5025 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5026 (nexts == 0 || extno < nexts)) {
5027 /*
5028 * Is the found extent after a hole in which bno lives?
5029 * Just back up to the previous extent, if so.
5030 */
5031 if (got.br_startoff > bno) {
5032 if (--lastx < 0)
5033 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005034 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 xfs_bmbt_get_all(ep, &got);
5036 }
5037 /*
5038 * Is the last block of this extent before the range
5039 * we're supposed to delete? If so, we're done.
5040 */
5041 bno = XFS_FILEOFF_MIN(bno,
5042 got.br_startoff + got.br_blockcount - 1);
5043 if (bno < start)
5044 break;
5045 /*
5046 * Then deal with the (possibly delayed) allocated space
5047 * we found.
5048 */
5049 ASSERT(ep != NULL);
5050 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005051 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 if (got.br_startoff < start) {
5053 del.br_startoff = start;
5054 del.br_blockcount -= start - got.br_startoff;
5055 if (!wasdel)
5056 del.br_startblock += start - got.br_startoff;
5057 }
5058 if (del.br_startoff + del.br_blockcount > bno + 1)
5059 del.br_blockcount = bno + 1 - del.br_startoff;
5060 sum = del.br_startblock + del.br_blockcount;
5061 if (isrt &&
5062 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5063 /*
5064 * Realtime extent not lined up at the end.
5065 * The extent could have been split into written
5066 * and unwritten pieces, or we could just be
5067 * unmapping part of it. But we can't really
5068 * get rid of part of a realtime extent.
5069 */
5070 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005071 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 /*
5073 * This piece is unwritten, or we're not
5074 * using unwritten extents. Skip over it.
5075 */
5076 ASSERT(bno >= mod);
5077 bno -= mod > del.br_blockcount ?
5078 del.br_blockcount : mod;
5079 if (bno < got.br_startoff) {
5080 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005081 xfs_bmbt_get_all(xfs_iext_get_ext(
5082 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 }
5084 continue;
5085 }
5086 /*
5087 * It's written, turn it unwritten.
5088 * This is better than zeroing it.
5089 */
5090 ASSERT(del.br_state == XFS_EXT_NORM);
5091 ASSERT(xfs_trans_get_block_res(tp) > 0);
5092 /*
5093 * If this spans a realtime extent boundary,
5094 * chop it back to the start of the one we end at.
5095 */
5096 if (del.br_blockcount > mod) {
5097 del.br_startoff += del.br_blockcount - mod;
5098 del.br_startblock += del.br_blockcount - mod;
5099 del.br_blockcount = mod;
5100 }
5101 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigf3ca87382011-07-08 14:34:47 +02005102 error = xfs_bmap_add_extent(tp, ip, &lastx, &cur, &del,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005103 firstblock, flist, &logflags,
Christoph Hellwig54893272011-05-11 15:04:03 +00005104 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 if (error)
5106 goto error0;
5107 goto nodelete;
5108 }
5109 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5110 /*
5111 * Realtime extent is lined up at the end but not
5112 * at the front. We'll get rid of full extents if
5113 * we can.
5114 */
5115 mod = mp->m_sb.sb_rextsize - mod;
5116 if (del.br_blockcount > mod) {
5117 del.br_blockcount -= mod;
5118 del.br_startoff += mod;
5119 del.br_startblock += mod;
5120 } else if ((del.br_startoff == start &&
5121 (del.br_state == XFS_EXT_UNWRITTEN ||
5122 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005123 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 /*
5125 * Can't make it unwritten. There isn't
5126 * a full extent here so just skip it.
5127 */
5128 ASSERT(bno >= del.br_blockcount);
5129 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005130 if (got.br_startoff > bno) {
5131 if (--lastx >= 0) {
5132 ep = xfs_iext_get_ext(ifp,
5133 lastx);
5134 xfs_bmbt_get_all(ep, &got);
5135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 }
5137 continue;
5138 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5139 /*
5140 * This one is already unwritten.
5141 * It must have a written left neighbor.
5142 * Unwrite the killed part of that one and
5143 * try again.
5144 */
5145 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005146 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5147 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005149 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 ASSERT(del.br_startblock ==
5151 prev.br_startblock + prev.br_blockcount);
5152 if (prev.br_startoff < start) {
5153 mod = start - prev.br_startoff;
5154 prev.br_blockcount -= mod;
5155 prev.br_startblock += mod;
5156 prev.br_startoff = start;
5157 }
5158 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005159 lastx--;
Christoph Hellwigf3ca87382011-07-08 14:34:47 +02005160 error = xfs_bmap_add_extent(tp, ip, &lastx,
5161 &cur, &prev, firstblock, flist,
5162 &logflags, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 if (error)
5164 goto error0;
5165 goto nodelete;
5166 } else {
5167 ASSERT(del.br_state == XFS_EXT_NORM);
5168 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigf3ca87382011-07-08 14:34:47 +02005169 error = xfs_bmap_add_extent(tp, ip, &lastx,
5170 &cur, &del, firstblock, flist,
5171 &logflags, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 if (error)
5173 goto error0;
5174 goto nodelete;
5175 }
5176 }
5177 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005178 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005179 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005180 if (isrt) {
5181 xfs_filblks_t rtexts;
5182
5183 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5184 do_div(rtexts, mp->m_sb.sb_rextsize);
5185 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005186 (int64_t)rtexts, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005187 (void)xfs_trans_reserve_quota_nblks(NULL,
5188 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005189 XFS_QMOPT_RES_RTBLKS);
5190 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00005191 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005192 (int64_t)del.br_blockcount, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005193 (void)xfs_trans_reserve_quota_nblks(NULL,
5194 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 ip->i_delayed_blks -= del.br_blockcount;
5198 if (cur)
5199 cur->bc_private.b.flags |=
5200 XFS_BTCUR_BPRV_WASDEL;
5201 } else if (cur)
5202 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5203 /*
5204 * If it's the case where the directory code is running
5205 * with no block reservation, and the deleted block is in
5206 * the middle of its extent, and the resulting insert
5207 * of an extent would cause transformation to btree format,
5208 * then reject it. The calling code will then swap
5209 * blocks around instead.
5210 * We have to do this now, rather than waiting for the
5211 * conversion to btree format, since the transaction
5212 * will be dirty.
5213 */
5214 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5215 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5216 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5217 del.br_startoff > got.br_startoff &&
5218 del.br_startoff + del.br_blockcount <
5219 got.br_startoff + got.br_blockcount) {
5220 error = XFS_ERROR(ENOSPC);
5221 goto error0;
5222 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00005223 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005224 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 logflags |= tmp_logflags;
5226 if (error)
5227 goto error0;
5228 bno = del.br_startoff - 1;
5229nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 /*
5231 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005234 if (lastx >= 0) {
5235 ep = xfs_iext_get_ext(ifp, lastx);
5236 if (xfs_bmbt_get_startoff(ep) > bno) {
5237 if (--lastx >= 0)
5238 ep = xfs_iext_get_ext(ifp,
5239 lastx);
5240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 extno++;
5244 }
5245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5247 ASSERT(ifp->if_ext_max ==
5248 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5249 /*
5250 * Convert to a btree if necessary.
5251 */
5252 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5253 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5254 ASSERT(cur == NULL);
5255 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5256 &cur, 0, &tmp_logflags, whichfork);
5257 logflags |= tmp_logflags;
5258 if (error)
5259 goto error0;
5260 }
5261 /*
5262 * transform from btree to extents, give it cur
5263 */
5264 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5265 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5266 ASSERT(cur != NULL);
5267 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5268 whichfork);
5269 logflags |= tmp_logflags;
5270 if (error)
5271 goto error0;
5272 }
5273 /*
5274 * transform from extents to local?
5275 */
5276 ASSERT(ifp->if_ext_max ==
5277 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5278 error = 0;
5279error0:
5280 /*
5281 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005282 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005284 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005286 logflags &= ~xfs_ilog_fext(whichfork);
5287 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005289 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 /*
5291 * Log inode even in the error case, if the transaction
5292 * is dirty we'll need to shut down the filesystem.
5293 */
5294 if (logflags)
5295 xfs_trans_log_inode(tp, ip, logflags);
5296 if (cur) {
5297 if (!error) {
5298 *firstblock = cur->bc_private.b.firstblock;
5299 cur->bc_private.b.allocated = 0;
5300 }
5301 xfs_btree_del_cursor(cur,
5302 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5303 }
5304 return error;
5305}
5306
5307/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005308 * returns 1 for success, 0 if we failed to map the extent.
5309 */
5310STATIC int
5311xfs_getbmapx_fix_eof_hole(
5312 xfs_inode_t *ip, /* xfs incore inode pointer */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005313 struct getbmapx *out, /* output structure */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005314 int prealloced, /* this is a file with
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005315 * preallocated data space */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005316 __int64_t end, /* last block requested */
5317 xfs_fsblock_t startblock)
5318{
5319 __int64_t fixlen;
5320 xfs_mount_t *mp; /* file system mount point */
Eric Sandeen5af317c2008-11-28 14:23:35 +11005321 xfs_ifork_t *ifp; /* inode fork pointer */
5322 xfs_extnum_t lastx; /* last extent pointer */
5323 xfs_fileoff_t fileblock;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005324
5325 if (startblock == HOLESTARTBLOCK) {
5326 mp = ip->i_mount;
5327 out->bmv_block = -1;
5328 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5329 fixlen -= out->bmv_offset;
5330 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5331 /* Came to hole at EOF. Trim it. */
5332 if (fixlen <= 0)
5333 return 0;
5334 out->bmv_length = fixlen;
5335 }
5336 } else {
Eric Sandeen5af317c2008-11-28 14:23:35 +11005337 if (startblock == DELAYSTARTBLOCK)
5338 out->bmv_block = -2;
5339 else
Eric Sandeen9d87c312009-01-14 23:22:07 -06005340 out->bmv_block = xfs_fsb_to_db(ip, startblock);
Eric Sandeen5af317c2008-11-28 14:23:35 +11005341 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5342 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5343 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5344 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5345 out->bmv_oflags |= BMV_OF_LAST;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005346 }
5347
5348 return 1;
5349}
5350
5351/*
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005352 * Get inode's extents as described in bmv, and format for output.
5353 * Calls formatter to fill the user's buffer until all extents
5354 * are mapped, until the passed-in bmv->bmv_count slots have
5355 * been filled, or until the formatter short-circuits the loop,
5356 * if it is tracking filled-in extents on its own.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 */
5358int /* error code */
5359xfs_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10005360 xfs_inode_t *ip,
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005361 struct getbmapx *bmv, /* user bmap structure */
5362 xfs_bmap_format_t formatter, /* format to user */
5363 void *arg) /* formatter arg */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364{
5365 __int64_t bmvend; /* last block requested */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005366 int error = 0; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 __int64_t fixlen; /* length for -1 case */
5368 int i; /* extent number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 int lock; /* lock state */
5370 xfs_bmbt_irec_t *map; /* buffer for user's data */
5371 xfs_mount_t *mp; /* file system mount point */
5372 int nex; /* # of user extents can do */
5373 int nexleft; /* # of user extents left */
5374 int subnex; /* # of bmapi's can do */
5375 int nmap; /* number of map entries */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005376 struct getbmapx *out; /* output structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 int whichfork; /* data or attr fork */
5378 int prealloced; /* this is a file with
5379 * preallocated data space */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005380 int iflags; /* interface flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 int bmapi_flags; /* flags for xfs_bmapi */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005382 int cur_ext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 mp = ip->i_mount;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005385 iflags = bmv->bmv_iflags;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005386 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 if (whichfork == XFS_ATTR_FORK) {
5389 if (XFS_IFORK_Q(ip)) {
5390 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5391 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5392 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5393 return XFS_ERROR(EINVAL);
5394 } else if (unlikely(
5395 ip->i_d.di_aformat != 0 &&
5396 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5397 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5398 ip->i_mount);
5399 return XFS_ERROR(EFSCORRUPTED);
5400 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005401
5402 prealloced = 0;
5403 fixlen = 1LL << 32;
5404 } else {
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005405 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5406 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5407 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5408 return XFS_ERROR(EINVAL);
5409
David Chinner957d0eb2007-06-18 16:50:37 +10005410 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005411 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 prealloced = 1;
5413 fixlen = XFS_MAXIOFFSET(mp);
5414 } else {
5415 prealloced = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005416 fixlen = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 }
5419
5420 if (bmv->bmv_length == -1) {
5421 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005422 bmv->bmv_length =
5423 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5424 } else if (bmv->bmv_length == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 bmv->bmv_entries = 0;
5426 return 0;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005427 } else if (bmv->bmv_length < 0) {
5428 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005430
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 nex = bmv->bmv_count - 1;
5432 if (nex <= 0)
5433 return XFS_ERROR(EINVAL);
5434 bmvend = bmv->bmv_offset + bmv->bmv_length;
5435
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005436
5437 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5438 return XFS_ERROR(ENOMEM);
5439 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5440 if (!out)
5441 return XFS_ERROR(ENOMEM);
5442
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005444 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5445 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5446 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5447 if (error)
5448 goto out_unlock_iolock;
Niv Sardie12070a2008-03-06 13:43:03 +11005449 }
Dave Chinner309c8482010-11-30 15:16:02 +11005450 /*
5451 * even after flushing the inode, there can still be delalloc
5452 * blocks on the inode beyond EOF due to speculative
5453 * preallocation. These are not removed until the release
5454 * function is called or the inode is inactivated. Hence we
5455 * cannot assert here that ip->i_delayed_blks == 0.
5456 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
5459 lock = xfs_ilock_map_shared(ip);
5460
5461 /*
5462 * Don't let nex be bigger than the number of extents
5463 * we can have assuming alternating holes and real extents.
5464 */
5465 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5466 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5467
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005468 bmapi_flags = xfs_bmapi_aflag(whichfork);
5469 if (!(iflags & BMV_IF_PREALLOC))
5470 bmapi_flags |= XFS_BMAPI_IGSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471
5472 /*
5473 * Allocate enough space to handle "subnex" maps at a time.
5474 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005475 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 subnex = 16;
Christoph Hellwigca35dcd2009-07-18 18:14:54 -04005477 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005478 if (!map)
5479 goto out_unlock_ilock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
5481 bmv->bmv_entries = 0;
5482
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005483 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5484 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5485 error = 0;
5486 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 }
5488
5489 nexleft = nex;
5490
5491 do {
5492 nmap = (nexleft > subnex) ? subnex : nexleft;
5493 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5494 XFS_BB_TO_FSB(mp, bmv->bmv_length),
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005495 bmapi_flags, NULL, 0, map, &nmap,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005496 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 if (error)
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005498 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 ASSERT(nmap <= subnex);
5500
5501 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005502 out[cur_ext].bmv_oflags = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005503 if (map[i].br_state == XFS_EXT_UNWRITTEN)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005504 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005505 else if (map[i].br_startblock == DELAYSTARTBLOCK)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005506 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5507 out[cur_ext].bmv_offset =
5508 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5509 out[cur_ext].bmv_length =
5510 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5511 out[cur_ext].bmv_unused1 = 0;
5512 out[cur_ext].bmv_unused2 = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005513 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5514 (map[i].br_startblock != DELAYSTARTBLOCK));
Yingping Lu9af0a702005-11-02 15:09:54 +11005515 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005516 whichfork == XFS_ATTR_FORK) {
5517 /* came to the end of attribute fork */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005518 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005519 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005521
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005522 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5523 prealloced, bmvend,
5524 map[i].br_startblock))
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005525 goto out_free_map;
5526
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005527 bmv->bmv_offset =
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005528 out[cur_ext].bmv_offset +
5529 out[cur_ext].bmv_length;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005530 bmv->bmv_length =
5531 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
Tao Ma9af25462010-08-30 02:44:03 +00005532
5533 /*
5534 * In case we don't want to return the hole,
5535 * don't increase cur_ext so that we can reuse
5536 * it in the next loop.
5537 */
5538 if ((iflags & BMV_IF_NO_HOLES) &&
5539 map[i].br_startblock == HOLESTARTBLOCK) {
5540 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5541 continue;
5542 }
5543
5544 nexleft--;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005545 bmv->bmv_entries++;
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005546 cur_ext++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 }
5548 } while (nmap && nexleft && bmv->bmv_length);
5549
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005550 out_free_map:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10005551 kmem_free(map);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005552 out_unlock_ilock:
5553 xfs_iunlock_map_shared(ip, lock);
5554 out_unlock_iolock:
5555 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005556
5557 for (i = 0; i < cur_ext; i++) {
5558 int full = 0; /* user array is full */
5559
5560 /* format results & advance arg */
5561 error = formatter(&arg, &out[i], &full);
5562 if (error || full)
5563 break;
5564 }
5565
Felix Blyakher7747a0b2009-06-11 17:07:28 -05005566 kmem_free(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 return error;
5568}
5569
5570/*
5571 * Check the last inode extent to determine whether this allocation will result
5572 * in blocks being allocated at the end of the file. When we allocate new data
5573 * blocks at the end of the file which do not start at the previous data block,
5574 * we will try to align the new blocks at stripe unit boundaries.
5575 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10005576STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577xfs_bmap_isaeof(
5578 xfs_inode_t *ip, /* incore inode pointer */
5579 xfs_fileoff_t off, /* file offset in fsblocks */
5580 int whichfork, /* data or attribute fork */
5581 char *aeof) /* return value */
5582{
5583 int error; /* error return value */
5584 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005585 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005586 xfs_extnum_t nextents; /* number of file extents */
5587 xfs_bmbt_irec_t s; /* expanded extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
5589 ASSERT(whichfork == XFS_DATA_FORK);
5590 ifp = XFS_IFORK_PTR(ip, whichfork);
5591 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5592 (error = xfs_iread_extents(NULL, ip, whichfork)))
5593 return error;
5594 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5595 if (nextents == 0) {
5596 *aeof = 1;
5597 return 0;
5598 }
5599 /*
5600 * Go to the last extent
5601 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005602 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 xfs_bmbt_get_all(lastrec, &s);
5604 /*
5605 * Check we are allocating in the last extent (for delayed allocations)
5606 * or past the last extent for non-delayed allocations.
5607 */
5608 *aeof = (off >= s.br_startoff &&
5609 off < s.br_startoff + s.br_blockcount &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06005610 isnullstartblock(s.br_startblock)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 off >= s.br_startoff + s.br_blockcount;
5612 return 0;
5613}
5614
5615/*
5616 * Check if the endoff is outside the last extent. If so the caller will grow
5617 * the allocation to a stripe unit boundary.
5618 */
5619int /* error */
5620xfs_bmap_eof(
5621 xfs_inode_t *ip, /* incore inode pointer */
5622 xfs_fileoff_t endoff, /* file offset in fsblocks */
5623 int whichfork, /* data or attribute fork */
5624 int *eof) /* result value */
5625{
5626 xfs_fsblock_t blockcount; /* extent block count */
5627 int error; /* error return value */
5628 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005629 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005630 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 xfs_fileoff_t startoff; /* extent starting file offset */
5632
5633 ASSERT(whichfork == XFS_DATA_FORK);
5634 ifp = XFS_IFORK_PTR(ip, whichfork);
5635 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5636 (error = xfs_iread_extents(NULL, ip, whichfork)))
5637 return error;
5638 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5639 if (nextents == 0) {
5640 *eof = 1;
5641 return 0;
5642 }
5643 /*
5644 * Go to the last extent
5645 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005646 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 startoff = xfs_bmbt_get_startoff(lastrec);
5648 blockcount = xfs_bmbt_get_blockcount(lastrec);
5649 *eof = endoff >= startoff + blockcount;
5650 return 0;
5651}
5652
5653#ifdef DEBUG
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005654STATIC struct xfs_buf *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655xfs_bmap_get_bp(
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005656 struct xfs_btree_cur *cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 xfs_fsblock_t bno)
5658{
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005659 struct xfs_log_item_desc *lidp;
5660 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661
5662 if (!cur)
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005663 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005665 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5666 if (!cur->bc_bufs[i])
5667 break;
5668 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5669 return cur->bc_bufs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005672 /* Chase down all the log items to see if the bp is there */
5673 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
Christoph Hellwige98c4142010-06-23 18:11:15 +10005674 struct xfs_buf_log_item *bip;
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005675 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5676 if (bip->bli_item.li_type == XFS_LI_BUF &&
5677 XFS_BUF_ADDR(bip->bli_buf) == bno)
5678 return bip->bli_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 }
Christoph Hellwige98c4142010-06-23 18:11:15 +10005680
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005681 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682}
5683
Hannes Eder3180e662009-03-04 19:34:10 +01005684STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685xfs_check_block(
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005686 struct xfs_btree_block *block,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 xfs_mount_t *mp,
5688 int root,
5689 short sz)
5690{
5691 int i, j, dmxr;
Christoph Hellwig576039c2006-09-28 10:58:06 +10005692 __be64 *pp, *thispa; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 xfs_bmbt_key_t *prevp, *keyp;
5694
Christoph Hellwig16259e72005-11-02 15:11:25 +11005695 ASSERT(be16_to_cpu(block->bb_level) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
5697 prevp = NULL;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005698 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 dmxr = mp->m_bmap_dmxr[0];
Christoph Hellwig136341b2008-10-30 17:11:40 +11005700 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701
5702 if (prevp) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005703 ASSERT(be64_to_cpu(prevp->br_startoff) <
5704 be64_to_cpu(keyp->br_startoff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 }
5706 prevp = keyp;
5707
5708 /*
5709 * Compare the block numbers to see if there are dups.
5710 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11005711 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005712 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005713 else
5714 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5715
Christoph Hellwig16259e72005-11-02 15:11:25 +11005716 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11005717 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005718 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005719 else
5720 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005721 if (*thispa == *pp) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11005722 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005723 __func__, j, i,
Christoph Hellwig576039c2006-09-28 10:58:06 +10005724 (unsigned long long)be64_to_cpu(*thispa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 panic("%s: ptrs are equal in node\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005726 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 }
5728 }
5729 }
5730}
5731
5732/*
5733 * Check that the extents for the inode ip are in the right order in all
5734 * btree leaves.
5735 */
5736
5737STATIC void
5738xfs_bmap_check_leaf_extents(
5739 xfs_btree_cur_t *cur, /* btree cursor or null */
5740 xfs_inode_t *ip, /* incore inode pointer */
5741 int whichfork) /* data or attr fork */
5742{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005743 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 xfs_fsblock_t bno; /* block # of "block" */
5745 xfs_buf_t *bp; /* buffer for "block" */
5746 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005747 xfs_extnum_t i=0, j; /* index into the extents list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 xfs_ifork_t *ifp; /* fork structure */
5749 int level; /* btree level, for checking */
5750 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10005751 __be64 *pp; /* pointer to block address */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005752 xfs_bmbt_rec_t *ep; /* pointer to current extent */
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005753 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005754 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 int bp_release = 0;
5756
5757 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5758 return;
5759 }
5760
5761 bno = NULLFSBLOCK;
5762 mp = ip->i_mount;
5763 ifp = XFS_IFORK_PTR(ip, whichfork);
5764 block = ifp->if_broot;
5765 /*
5766 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5767 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11005768 level = be16_to_cpu(block->bb_level);
5769 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11005771 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005772 bno = be64_to_cpu(*pp);
5773
5774 ASSERT(bno != NULLDFSBNO);
5775 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5776 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5777
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 /*
5779 * Go down the tree until leaf level is reached, following the first
5780 * pointer (leftmost) at each level.
5781 */
5782 while (level-- > 0) {
5783 /* See if buf is in cur first */
5784 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5785 if (bp) {
5786 bp_release = 0;
5787 } else {
5788 bp_release = 1;
5789 }
5790 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5791 XFS_BMAP_BTREE_REF)))
5792 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005793 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11005795 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 error0);
5797 if (level == 0)
5798 break;
5799
5800 /*
5801 * Check this block for basic sanity (increasing keys and
5802 * no duplicate blocks).
5803 */
5804
5805 xfs_check_block(block, mp, 0, 0);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005806 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005807 bno = be64_to_cpu(*pp);
5808 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 if (bp_release) {
5810 bp_release = 0;
5811 xfs_trans_brelse(NULL, bp);
5812 }
5813 }
5814
5815 /*
5816 * Here with bp and block set to the leftmost leaf node in the tree.
5817 */
5818 i = 0;
5819
5820 /*
5821 * Loop over all leaf nodes checking that all extents are in the right order.
5822 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 xfs_fsblock_t nextbno;
5825 xfs_extnum_t num_recs;
5826
5827
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005828 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829
5830 /*
5831 * Read-ahead the next leaf block, if any.
5832 */
5833
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005834 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835
5836 /*
5837 * Check all the extents to make sure they are OK.
5838 * If we had a previous block, the last entry should
5839 * conform with the first entry in this one.
5840 */
5841
Christoph Hellwig136341b2008-10-30 17:11:40 +11005842 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005843 if (i) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005844 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
5845 xfs_bmbt_disk_get_blockcount(&last) <=
5846 xfs_bmbt_disk_get_startoff(ep));
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005847 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005848 for (j = 1; j < num_recs; j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11005849 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005850 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
5851 xfs_bmbt_disk_get_blockcount(ep) <=
5852 xfs_bmbt_disk_get_startoff(nextp));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005853 ep = nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005856 last = *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 i += num_recs;
5858 if (bp_release) {
5859 bp_release = 0;
5860 xfs_trans_brelse(NULL, bp);
5861 }
5862 bno = nextbno;
5863 /*
5864 * If we've reached the end, stop.
5865 */
5866 if (bno == NULLFSBLOCK)
5867 break;
5868
5869 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5870 if (bp) {
5871 bp_release = 0;
5872 } else {
5873 bp_release = 1;
5874 }
5875 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5876 XFS_BMAP_BTREE_REF)))
5877 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005878 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 }
5880 if (bp_release) {
5881 bp_release = 0;
5882 xfs_trans_brelse(NULL, bp);
5883 }
5884 return;
5885
5886error0:
Dave Chinner0b932cc2011-03-07 10:08:35 +11005887 xfs_warn(mp, "%s: at error0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 if (bp_release)
5889 xfs_trans_brelse(NULL, bp);
5890error_norelse:
Dave Chinner0b932cc2011-03-07 10:08:35 +11005891 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005892 __func__, i);
5893 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 return;
5895}
5896#endif
5897
5898/*
5899 * Count fsblocks of the given fork.
5900 */
5901int /* error */
5902xfs_bmap_count_blocks(
5903 xfs_trans_t *tp, /* transaction pointer */
5904 xfs_inode_t *ip, /* incore inode */
5905 int whichfork, /* data or attr fork */
5906 int *count) /* out: count of blocks */
5907{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005908 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 xfs_fsblock_t bno; /* block # of "block" */
5910 xfs_ifork_t *ifp; /* fork structure */
5911 int level; /* btree level, for checking */
5912 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10005913 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914
5915 bno = NULLFSBLOCK;
5916 mp = ip->i_mount;
5917 ifp = XFS_IFORK_PTR(ip, whichfork);
5918 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
Ruben Porrasc94312d2008-08-13 16:52:25 +10005919 xfs_bmap_count_leaves(ifp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
Ruben Porrasc94312d2008-08-13 16:52:25 +10005921 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 return 0;
5923 }
5924
5925 /*
5926 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5927 */
5928 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11005929 level = be16_to_cpu(block->bb_level);
5930 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11005931 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005932 bno = be64_to_cpu(*pp);
5933 ASSERT(bno != NULLDFSBNO);
5934 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5935 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005937 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
5939 mp);
5940 return XFS_ERROR(EFSCORRUPTED);
5941 }
5942
5943 return 0;
5944}
5945
5946/*
5947 * Recursively walks each level of a btree
5948 * to count total fsblocks is use.
5949 */
David Chinnera8272ce2007-11-23 16:28:09 +11005950STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951xfs_bmap_count_tree(
5952 xfs_mount_t *mp, /* file system mount point */
5953 xfs_trans_t *tp, /* transaction pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005954 xfs_ifork_t *ifp, /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 xfs_fsblock_t blockno, /* file system block number */
5956 int levelin, /* level in btree */
5957 int *count) /* Count of blocks */
5958{
5959 int error;
5960 xfs_buf_t *bp, *nbp;
5961 int level = levelin;
Christoph Hellwig576039c2006-09-28 10:58:06 +10005962 __be64 *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 xfs_fsblock_t bno = blockno;
5964 xfs_fsblock_t nextbno;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005965 struct xfs_btree_block *block, *nextblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967
5968 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
5969 return error;
5970 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005971 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972
5973 if (--level) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +02005974 /* Not at node above leaves, count this level of nodes */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005975 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 while (nextbno != NULLFSBLOCK) {
5977 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
5978 0, &nbp, XFS_BMAP_BTREE_REF)))
5979 return error;
5980 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005981 nextblock = XFS_BUF_TO_BLOCK(nbp);
5982 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 xfs_trans_brelse(tp, nbp);
5984 }
5985
5986 /* Dive to the next level */
Christoph Hellwig136341b2008-10-30 17:11:40 +11005987 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005988 bno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 if (unlikely((error =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005990 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 xfs_trans_brelse(tp, bp);
5992 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
5993 XFS_ERRLEVEL_LOW, mp);
5994 return XFS_ERROR(EFSCORRUPTED);
5995 }
5996 xfs_trans_brelse(tp, bp);
5997 } else {
5998 /* count all level 1 nodes and their leaves */
5999 for (;;) {
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006000 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Christoph Hellwig16259e72005-11-02 15:11:25 +11006001 numrecs = be16_to_cpu(block->bb_numrecs);
Christoph Hellwig136341b2008-10-30 17:11:40 +11006002 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 xfs_trans_brelse(tp, bp);
6004 if (nextbno == NULLFSBLOCK)
6005 break;
6006 bno = nextbno;
6007 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6008 XFS_BMAP_BTREE_REF)))
6009 return error;
6010 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006011 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 }
6013 }
6014 return 0;
6015}
6016
6017/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006018 * Count leaf blocks given a range of extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006020STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006022 xfs_ifork_t *ifp,
6023 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 int numrecs,
6025 int *count)
6026{
6027 int b;
6028
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006029 for (b = 0; b < numrecs; b++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006030 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
Yingping Lu91e11082005-11-02 15:10:24 +11006031 *count += xfs_bmbt_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006032 }
Yingping Lu91e11082005-11-02 15:10:24 +11006033}
6034
6035/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006036 * Count leaf blocks given a range of extent records originally
6037 * in btree format.
Yingping Lu91e11082005-11-02 15:10:24 +11006038 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006039STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +11006040xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +11006041 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006042 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +11006043 int numrecs,
6044 int *count)
6045{
6046 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006047 xfs_bmbt_rec_t *frp;
Yingping Lu91e11082005-11-02 15:10:24 +11006048
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006049 for (b = 1; b <= numrecs; b++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11006050 frp = XFS_BMBT_REC_ADDR(mp, block, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 *count += xfs_bmbt_disk_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053}
Dave Chinnerc726de42010-11-30 15:14:39 +11006054
6055/*
6056 * dead simple method of punching delalyed allocation blocks from a range in
6057 * the inode. Walks a block at a time so will be slow, but is only executed in
6058 * rare error cases so the overhead is not critical. This will alays punch out
6059 * both the start and end blocks, even if the ranges only partially overlap
6060 * them, so it is up to the caller to ensure that partial blocks are not
6061 * passed in.
6062 */
6063int
6064xfs_bmap_punch_delalloc_range(
6065 struct xfs_inode *ip,
6066 xfs_fileoff_t start_fsb,
6067 xfs_fileoff_t length)
6068{
6069 xfs_fileoff_t remaining = length;
6070 int error = 0;
6071
6072 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6073
6074 do {
6075 int done;
6076 xfs_bmbt_irec_t imap;
6077 int nimaps = 1;
6078 xfs_fsblock_t firstblock;
6079 xfs_bmap_free_t flist;
6080
6081 /*
6082 * Map the range first and check that it is a delalloc extent
6083 * before trying to unmap the range. Otherwise we will be
6084 * trying to remove a real extent (which requires a
6085 * transaction) or a hole, which is probably a bad idea...
6086 */
6087 error = xfs_bmapi(NULL, ip, start_fsb, 1,
6088 XFS_BMAPI_ENTIRE, NULL, 0, &imap,
6089 &nimaps, NULL);
6090
6091 if (error) {
6092 /* something screwed, just bail */
6093 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner53487782011-03-07 10:05:35 +11006094 xfs_alert(ip->i_mount,
Dave Chinnerc726de42010-11-30 15:14:39 +11006095 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6096 ip->i_ino, start_fsb);
6097 }
6098 break;
6099 }
6100 if (!nimaps) {
6101 /* nothing there */
6102 goto next_block;
6103 }
6104 if (imap.br_startblock != DELAYSTARTBLOCK) {
6105 /* been converted, ignore */
6106 goto next_block;
6107 }
6108 WARN_ON(imap.br_blockcount == 0);
6109
6110 /*
6111 * Note: while we initialise the firstblock/flist pair, they
6112 * should never be used because blocks should never be
6113 * allocated or freed for a delalloc extent and hence we need
6114 * don't cancel or finish them after the xfs_bunmapi() call.
6115 */
6116 xfs_bmap_init(&flist, &firstblock);
6117 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6118 &flist, &done);
6119 if (error)
6120 break;
6121
6122 ASSERT(!flist.xbf_count && !flist.xbf_first);
6123next_block:
6124 start_fsb++;
6125 remaining--;
6126 } while(remaining > 0);
6127
6128 return error;
6129}