blob: 7b46b78ba3f011b1ed32ab7c2ee09c959650c361 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_dir2_sf.h"
33#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_itable.h"
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100038#include "xfs_dir2_data.h"
39#include "xfs_dir2_leaf.h"
40#include "xfs_dir2_block.h"
Nathan Scotta844f452005-11-02 14:38:42 +110041#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_extfree_item.h"
43#include "xfs_alloc.h"
44#include "xfs_bmap.h"
45#include "xfs_rtalloc.h"
46#include "xfs_error.h"
Nathan Scottd8cc8902005-11-02 10:34:53 +110047#include "xfs_attr_leaf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "xfs_rw.h"
49#include "xfs_quota.h"
50#include "xfs_trans_space.h"
51#include "xfs_buf_item.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100052#include "xfs_filestream.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100053#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000054#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56
57#ifdef DEBUG
58STATIC void
59xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
60#endif
61
62kmem_zone_t *xfs_bmap_free_item_zone;
63
64/*
65 * Prototypes for internal bmap routines.
66 */
67
68
69/*
70 * Called from xfs_bmap_add_attrfork to handle extents format files.
71 */
72STATIC int /* error */
73xfs_bmap_add_attrfork_extents(
74 xfs_trans_t *tp, /* transaction pointer */
75 xfs_inode_t *ip, /* incore inode pointer */
76 xfs_fsblock_t *firstblock, /* first block allocated */
77 xfs_bmap_free_t *flist, /* blocks to free at commit */
78 int *flags); /* inode logging flags */
79
80/*
81 * Called from xfs_bmap_add_attrfork to handle local format files.
82 */
83STATIC int /* error */
84xfs_bmap_add_attrfork_local(
85 xfs_trans_t *tp, /* transaction pointer */
86 xfs_inode_t *ip, /* incore inode pointer */
87 xfs_fsblock_t *firstblock, /* first block allocated */
88 xfs_bmap_free_t *flist, /* blocks to free at commit */
89 int *flags); /* inode logging flags */
90
91/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * Called by xfs_bmap_add_extent to handle cases converting a delayed
93 * allocation to a real allocation.
94 */
95STATIC int /* error */
96xfs_bmap_add_extent_delay_real(
Christoph Hellwigf3ca8732011-07-08 14:34:47 +020097 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +000099 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100101 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
103 xfs_fsblock_t *first, /* pointer to firstblock variable */
104 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwig54893272011-05-11 15:04:03 +0000105 int *logflagsp); /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/*
108 * Called by xfs_bmap_add_extent to handle cases converting a hole
109 * to a delayed allocation.
110 */
111STATIC int /* error */
112xfs_bmap_add_extent_hole_delay(
113 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000114 xfs_extnum_t *idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100115 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwig54893272011-05-11 15:04:03 +0000116 int *logflagsp); /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/*
119 * Called by xfs_bmap_add_extent to handle cases converting a hole
120 * to a real allocation.
121 */
122STATIC int /* error */
123xfs_bmap_add_extent_hole_real(
124 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000125 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100127 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int *logflagsp, /* inode logging flags */
129 int whichfork); /* data or attr fork */
130
131/*
132 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
133 * allocation to a real allocation or vice versa.
134 */
135STATIC int /* error */
136xfs_bmap_add_extent_unwritten_real(
137 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000138 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100140 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000141 int *logflagsp); /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143/*
144 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
145 * It figures out where to ask the underlying allocator to put the new extent.
146 */
147STATIC int /* error */
148xfs_bmap_alloc(
149 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
150
151/*
152 * Transform a btree format file with only one leaf node, where the
153 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100154 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * give up the space for the btree root and pitch the leaf block.
156 */
157STATIC int /* error */
158xfs_bmap_btree_to_extents(
159 xfs_trans_t *tp, /* transaction pointer */
160 xfs_inode_t *ip, /* incore inode pointer */
161 xfs_btree_cur_t *cur, /* btree cursor */
162 int *logflagsp, /* inode logging flags */
163 int whichfork); /* data or attr fork */
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * Remove the entry "free" from the free item list. Prev points to the
167 * previous entry, unless "free" is the head of the list.
168 */
169STATIC void
170xfs_bmap_del_free(
171 xfs_bmap_free_t *flist, /* free item list header */
172 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
173 xfs_bmap_free_item_t *free); /* list item to be freed */
174
175/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * Convert an extents-format file into a btree-format file.
177 * The new file will have a root block (in the inode) and a single child block.
178 */
179STATIC int /* error */
180xfs_bmap_extents_to_btree(
181 xfs_trans_t *tp, /* transaction pointer */
182 xfs_inode_t *ip, /* incore inode pointer */
183 xfs_fsblock_t *firstblock, /* first-block-allocated */
184 xfs_bmap_free_t *flist, /* blocks freed in xaction */
185 xfs_btree_cur_t **curp, /* cursor returned to caller */
186 int wasdel, /* converting a delayed alloc */
187 int *logflagsp, /* inode logging flags */
188 int whichfork); /* data or attr fork */
189
190/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * Convert a local file to an extents file.
192 * This code is sort of bogus, since the file data needs to get
193 * logged so it won't be lost. The bmap-level manipulations are ok, though.
194 */
195STATIC int /* error */
196xfs_bmap_local_to_extents(
197 xfs_trans_t *tp, /* transaction pointer */
198 xfs_inode_t *ip, /* incore inode pointer */
199 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
200 xfs_extlen_t total, /* total blocks needed by transaction */
201 int *logflagsp, /* inode logging flags */
202 int whichfork); /* data or attr fork */
203
204/*
205 * Search the extents list for the inode, for the extent containing bno.
206 * If bno lies in a hole, point to the next entry. If bno lies past eof,
207 * *eofp will be set, and *prevp will contain the last entry (null if none).
208 * Else, *lastxp will be set to the index of the found
209 * entry; *gotp will contain the entry.
210 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000211STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212xfs_bmap_search_extents(
213 xfs_inode_t *ip, /* incore inode pointer */
214 xfs_fileoff_t bno, /* block number searched for */
215 int whichfork, /* data or attr fork */
216 int *eofp, /* out: end of file found */
217 xfs_extnum_t *lastxp, /* out: last extent index */
218 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
219 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
220
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000221/*
222 * Check the last inode extent to determine whether this allocation will result
223 * in blocks being allocated at the end of the file. When we allocate new data
224 * blocks at the end of the file which do not start at the previous data block,
225 * we will try to align the new blocks at stripe unit boundaries.
226 */
227STATIC int /* error */
228xfs_bmap_isaeof(
229 xfs_inode_t *ip, /* incore inode pointer */
230 xfs_fileoff_t off, /* file offset in fsblocks */
231 int whichfork, /* data or attribute fork */
232 char *aeof); /* return value */
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/*
235 * Compute the worst-case number of indirect blocks that will be used
236 * for ip's delayed extent of length "len".
237 */
238STATIC xfs_filblks_t
239xfs_bmap_worst_indlen(
240 xfs_inode_t *ip, /* incore inode pointer */
241 xfs_filblks_t len); /* delayed extent length */
242
243#ifdef DEBUG
244/*
245 * Perform various validation checks on the values being returned
246 * from xfs_bmapi().
247 */
248STATIC void
249xfs_bmap_validate_ret(
250 xfs_fileoff_t bno,
251 xfs_filblks_t len,
252 int flags,
253 xfs_bmbt_irec_t *mval,
254 int nmap,
255 int ret_nmap);
256#else
257#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
258#endif /* DEBUG */
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260STATIC int
261xfs_bmap_count_tree(
262 xfs_mount_t *mp,
263 xfs_trans_t *tp,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100264 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 xfs_fsblock_t blockno,
266 int levelin,
267 int *count);
268
Ruben Porrasc94312d2008-08-13 16:52:25 +1000269STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100271 xfs_ifork_t *ifp,
272 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 int numrecs,
274 int *count);
275
Ruben Porrasc94312d2008-08-13 16:52:25 +1000276STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +1100277xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +1100278 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100279 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +1100280 int numrecs,
281 int *count);
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283/*
284 * Bmap internal routines.
285 */
286
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100287STATIC int /* error */
288xfs_bmbt_lookup_eq(
289 struct xfs_btree_cur *cur,
290 xfs_fileoff_t off,
291 xfs_fsblock_t bno,
292 xfs_filblks_t len,
293 int *stat) /* success/failure */
294{
295 cur->bc_rec.b.br_startoff = off;
296 cur->bc_rec.b.br_startblock = bno;
297 cur->bc_rec.b.br_blockcount = len;
298 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
299}
300
301STATIC int /* error */
302xfs_bmbt_lookup_ge(
303 struct xfs_btree_cur *cur,
304 xfs_fileoff_t off,
305 xfs_fsblock_t bno,
306 xfs_filblks_t len,
307 int *stat) /* success/failure */
308{
309 cur->bc_rec.b.br_startoff = off;
310 cur->bc_rec.b.br_startblock = bno;
311 cur->bc_rec.b.br_blockcount = len;
312 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
313}
314
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100315/*
316* Update the record referred to by cur to the value given
317 * by [off, bno, len, state].
318 * This either works (return 0) or gets an EFSCORRUPTED error.
319 */
320STATIC int
321xfs_bmbt_update(
322 struct xfs_btree_cur *cur,
323 xfs_fileoff_t off,
324 xfs_fsblock_t bno,
325 xfs_filblks_t len,
326 xfs_exntst_t state)
327{
328 union xfs_btree_rec rec;
329
330 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
331 return xfs_btree_update(cur, &rec);
332}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/*
335 * Called from xfs_bmap_add_attrfork to handle btree format files.
336 */
337STATIC int /* error */
338xfs_bmap_add_attrfork_btree(
339 xfs_trans_t *tp, /* transaction pointer */
340 xfs_inode_t *ip, /* incore inode pointer */
341 xfs_fsblock_t *firstblock, /* first block allocated */
342 xfs_bmap_free_t *flist, /* blocks to free at commit */
343 int *flags) /* inode logging flags */
344{
345 xfs_btree_cur_t *cur; /* btree cursor */
346 int error; /* error return value */
347 xfs_mount_t *mp; /* file system mount struct */
348 int stat; /* newroot status */
349
350 mp = ip->i_mount;
351 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
352 *flags |= XFS_ILOG_DBROOT;
353 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100354 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 cur->bc_private.b.flist = flist;
356 cur->bc_private.b.firstblock = *firstblock;
357 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
358 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000359 /* must be at least one entry */
360 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100361 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 goto error0;
363 if (stat == 0) {
364 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
365 return XFS_ERROR(ENOSPC);
366 }
367 *firstblock = cur->bc_private.b.firstblock;
368 cur->bc_private.b.allocated = 0;
369 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
370 }
371 return 0;
372error0:
373 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
374 return error;
375}
376
377/*
378 * Called from xfs_bmap_add_attrfork to handle extents format files.
379 */
380STATIC int /* error */
381xfs_bmap_add_attrfork_extents(
382 xfs_trans_t *tp, /* transaction pointer */
383 xfs_inode_t *ip, /* incore inode pointer */
384 xfs_fsblock_t *firstblock, /* first block allocated */
385 xfs_bmap_free_t *flist, /* blocks to free at commit */
386 int *flags) /* inode logging flags */
387{
388 xfs_btree_cur_t *cur; /* bmap btree cursor */
389 int error; /* error return value */
390
391 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
392 return 0;
393 cur = NULL;
394 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
395 flags, XFS_DATA_FORK);
396 if (cur) {
397 cur->bc_private.b.allocated = 0;
398 xfs_btree_del_cursor(cur,
399 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
400 }
401 return error;
402}
403
404/*
405 * Called from xfs_bmap_add_attrfork to handle local format files.
406 */
407STATIC int /* error */
408xfs_bmap_add_attrfork_local(
409 xfs_trans_t *tp, /* transaction pointer */
410 xfs_inode_t *ip, /* incore inode pointer */
411 xfs_fsblock_t *firstblock, /* first block allocated */
412 xfs_bmap_free_t *flist, /* blocks to free at commit */
413 int *flags) /* inode logging flags */
414{
415 xfs_da_args_t dargs; /* args for dir/attr code */
416 int error; /* error return value */
417 xfs_mount_t *mp; /* mount structure pointer */
418
419 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
420 return 0;
421 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
422 mp = ip->i_mount;
423 memset(&dargs, 0, sizeof(dargs));
424 dargs.dp = ip;
425 dargs.firstblock = firstblock;
426 dargs.flist = flist;
427 dargs.total = mp->m_dirblkfsbs;
428 dargs.whichfork = XFS_DATA_FORK;
429 dargs.trans = tp;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000430 error = xfs_dir2_sf_to_block(&dargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 } else
432 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
433 XFS_DATA_FORK);
434 return error;
435}
436
437/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100438 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * after allocating space (or doing a delayed allocation).
440 */
441STATIC int /* error */
442xfs_bmap_add_extent(
Christoph Hellwigf3ca8732011-07-08 14:34:47 +0200443 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000445 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100447 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 xfs_fsblock_t *first, /* pointer to firstblock variable */
449 xfs_bmap_free_t *flist, /* list of extents to be freed */
450 int *logflagsp, /* inode logging flags */
Christoph Hellwig54893272011-05-11 15:04:03 +0000451 int whichfork) /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 xfs_btree_cur_t *cur; /* btree cursor or null */
454 xfs_filblks_t da_new; /* new count del alloc blocks used */
455 xfs_filblks_t da_old; /* old count del alloc blocks used */
456 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 xfs_ifork_t *ifp; /* inode fork ptr */
458 int logflags; /* returned value */
459 xfs_extnum_t nextents; /* number of extents in file now */
460
461 XFS_STATS_INC(xs_add_exlist);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 cur = *curp;
464 ifp = XFS_IFORK_PTR(ip, whichfork);
465 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 da_old = da_new = 0;
467 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000468
469 ASSERT(*idx >= 0);
470 ASSERT(*idx <= nextents);
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /*
473 * This is the first extent added to a new/empty file.
474 * Special case this one, so other routines get to assume there are
475 * already extents in the list.
476 */
477 if (nextents == 0) {
Christoph Hellwigec90c552011-05-23 08:52:53 +0000478 xfs_iext_insert(ip, *idx, 1, new,
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000479 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 ASSERT(cur == NULL);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000482
Eric Sandeen9d87c312009-01-14 23:22:07 -0600483 if (!isnullstartblock(new->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600485 logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 } else
487 logflags = 0;
488 }
489 /*
490 * Any kind of new delayed allocation goes here.
491 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600492 else if (isnullstartblock(new->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (cur)
494 ASSERT((cur->bc_private.b.flags &
495 XFS_BTCUR_BPRV_WASDEL) == 0);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000496 error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
497 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499 /*
500 * Real allocation off the end of the file.
501 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000502 else if (*idx == nextents) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (cur)
504 ASSERT((cur->bc_private.b.flags &
505 XFS_BTCUR_BPRV_WASDEL) == 0);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000506 error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
507 &logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 } else {
509 xfs_bmbt_irec_t prev; /* old extent at offset idx */
510
511 /*
512 * Get the record referred to by idx.
513 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000514 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /*
516 * If it's a real allocation record, and the new allocation ends
517 * after the start of the referred to record, then we're filling
518 * in a delayed or unwritten allocation with a real one, or
519 * converting real back to unwritten.
520 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600521 if (!isnullstartblock(new->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 new->br_startoff + new->br_blockcount > prev.br_startoff) {
523 if (prev.br_state != XFS_EXT_UNWRITTEN &&
Eric Sandeen9d87c312009-01-14 23:22:07 -0600524 isnullstartblock(prev.br_startblock)) {
525 da_old = startblockval(prev.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (cur)
527 ASSERT(cur->bc_private.b.flags &
528 XFS_BTCUR_BPRV_WASDEL);
Christoph Hellwigf3ca8732011-07-08 14:34:47 +0200529 error = xfs_bmap_add_extent_delay_real(tp, ip,
Christoph Hellwigec90c552011-05-23 08:52:53 +0000530 idx, &cur, new, &da_new,
531 first, flist, &logflags);
532 } else {
533 ASSERT(new->br_state == XFS_EXT_NORM ||
534 new->br_state == XFS_EXT_UNWRITTEN);
535
536 error = xfs_bmap_add_extent_unwritten_real(ip,
537 idx, &cur, new, &logflags);
Christoph Hellwig54893272011-05-11 15:04:03 +0000538 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542 /*
543 * Otherwise we're filling in a hole with an allocation.
544 */
545 else {
546 if (cur)
547 ASSERT((cur->bc_private.b.flags &
548 XFS_BTCUR_BPRV_WASDEL) == 0);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000549 error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
550 new, &logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552 }
553
Christoph Hellwigec90c552011-05-23 08:52:53 +0000554 if (error)
555 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 ASSERT(*curp == cur || *curp == NULL);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /*
559 * Convert to a btree if necessary.
560 */
561 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
562 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
563 int tmp_logflags; /* partial log flag return val */
564
565 ASSERT(cur == NULL);
Christoph Hellwigf3ca8732011-07-08 14:34:47 +0200566 error = xfs_bmap_extents_to_btree(tp, ip, first,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
568 logflags |= tmp_logflags;
569 if (error)
570 goto done;
571 }
572 /*
573 * Adjust for changes in reserved delayed indirect blocks.
574 * Nothing to do for disk quotas here.
575 */
576 if (da_old || da_new) {
577 xfs_filblks_t nblks;
578
579 nblks = da_new;
580 if (cur)
581 nblks += cur->bc_private.b.allocated;
582 ASSERT(nblks <= da_old);
583 if (nblks < da_old)
Christoph Hellwig96540c72010-09-30 02:25:55 +0000584 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +0000585 (int64_t)(da_old - nblks), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587 /*
588 * Clear out the allocated field, done with it now in any case.
589 */
590 if (cur) {
591 cur->bc_private.b.allocated = 0;
592 *curp = cur;
593 }
594done:
595#ifdef DEBUG
596 if (!error)
597 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
598#endif
599 *logflagsp = logflags;
600 return error;
601}
602
603/*
604 * Called by xfs_bmap_add_extent to handle cases converting a delayed
605 * allocation to a real allocation.
606 */
607STATIC int /* error */
608xfs_bmap_add_extent_delay_real(
Christoph Hellwigf3ca8732011-07-08 14:34:47 +0200609 struct xfs_trans *tp, /* transaction pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000611 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100613 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
615 xfs_fsblock_t *first, /* pointer to firstblock variable */
616 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwig54893272011-05-11 15:04:03 +0000617 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 xfs_btree_cur_t *cur; /* btree cursor */
620 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000621 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100624 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 xfs_fileoff_t new_endoff; /* end offset of new entry */
626 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
627 /* left is 0, right is 1, prev is 2 */
628 int rval=0; /* return value (logging flags) */
629 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000630 xfs_filblks_t temp=0; /* value for dnew calculations */
631 xfs_filblks_t temp2=0;/* value for dnew calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int tmp_rval; /* partial logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634#define LEFT r[0]
635#define RIGHT r[1]
636#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 /*
639 * Set up a bunch of variables to make the tests simpler.
640 */
641 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100642 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000643 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 xfs_bmbt_get_all(ep, &PREV);
645 new_endoff = new->br_startoff + new->br_blockcount;
646 ASSERT(PREV.br_startoff <= new->br_startoff);
647 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /*
650 * Set flags determining what part of the previous delayed allocation
651 * extent is being replaced by a real allocation.
652 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000653 if (PREV.br_startoff == new->br_startoff)
654 state |= BMAP_LEFT_FILLING;
655 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
656 state |= BMAP_RIGHT_FILLING;
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /*
659 * Check and set flags if this segment has a left neighbor.
660 * Don't set contiguous if the combined extent would be too large.
661 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000662 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000663 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000664 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000665
666 if (isnullstartblock(LEFT.br_startblock))
667 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000669
670 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
671 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
672 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
673 LEFT.br_state == new->br_state &&
674 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
675 state |= BMAP_LEFT_CONTIG;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /*
678 * Check and set flags if this segment has a right neighbor.
679 * Don't set contiguous if the combined extent would be too large.
680 * Also check for all-three-contiguous being too large.
681 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000682 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000683 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000684 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000685
686 if (isnullstartblock(RIGHT.br_startblock))
687 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000689
690 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
691 new_endoff == RIGHT.br_startoff &&
692 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
693 new->br_state == RIGHT.br_state &&
694 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
695 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
696 BMAP_RIGHT_FILLING)) !=
697 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
698 BMAP_RIGHT_FILLING) ||
699 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
700 <= MAXEXTLEN))
701 state |= BMAP_RIGHT_CONTIG;
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 error = 0;
704 /*
705 * Switch out based on the FILLING and CONTIG state bits.
706 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000707 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
708 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
709 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
710 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /*
712 * Filling in all of a previously delayed allocation extent.
713 * The left and right neighbors are both contiguous with new.
714 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000715 --*idx;
716 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
717 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 LEFT.br_blockcount + PREV.br_blockcount +
719 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000720 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000721
Christoph Hellwigec90c552011-05-23 08:52:53 +0000722 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 ip->i_d.di_nextents--;
724 if (cur == NULL)
725 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
726 else {
727 rval = XFS_ILOG_CORE;
728 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
729 RIGHT.br_startblock,
730 RIGHT.br_blockcount, &i)))
731 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000732 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +1100733 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000735 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100736 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000738 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
740 LEFT.br_startblock,
741 LEFT.br_blockcount +
742 PREV.br_blockcount +
743 RIGHT.br_blockcount, LEFT.br_state)))
744 goto done;
745 }
746 *dnew = 0;
747 break;
748
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000749 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /*
751 * Filling in all of a previously delayed allocation extent.
752 * The left neighbor is contiguous, the right is not.
753 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000754 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000755
Christoph Hellwigec90c552011-05-23 08:52:53 +0000756 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
757 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
758 LEFT.br_blockcount + PREV.br_blockcount);
759 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
760
761 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (cur == NULL)
763 rval = XFS_ILOG_DEXT;
764 else {
765 rval = 0;
766 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
767 LEFT.br_startblock, LEFT.br_blockcount,
768 &i)))
769 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000770 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
772 LEFT.br_startblock,
773 LEFT.br_blockcount +
774 PREV.br_blockcount, LEFT.br_state)))
775 goto done;
776 }
777 *dnew = 0;
778 break;
779
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000780 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /*
782 * Filling in all of a previously delayed allocation extent.
783 * The right neighbor is contiguous, the left is not.
784 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000785 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 xfs_bmbt_set_startblock(ep, new->br_startblock);
787 xfs_bmbt_set_blockcount(ep,
788 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000789 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000790
Christoph Hellwigec90c552011-05-23 08:52:53 +0000791 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (cur == NULL)
793 rval = XFS_ILOG_DEXT;
794 else {
795 rval = 0;
796 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
797 RIGHT.br_startblock,
798 RIGHT.br_blockcount, &i)))
799 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000800 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
802 new->br_startblock,
803 PREV.br_blockcount +
804 RIGHT.br_blockcount, PREV.br_state)))
805 goto done;
806 }
Christoph Hellwigec90c552011-05-23 08:52:53 +0000807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 *dnew = 0;
809 break;
810
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000811 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /*
813 * Filling in all of a previously delayed allocation extent.
814 * Neither the left nor right neighbors are contiguous with
815 * the new one.
816 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000817 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000819 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 ip->i_d.di_nextents++;
822 if (cur == NULL)
823 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
824 else {
825 rval = XFS_ILOG_CORE;
826 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
827 new->br_startblock, new->br_blockcount,
828 &i)))
829 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000830 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100832 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000834 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
Christoph Hellwigec90c552011-05-23 08:52:53 +0000836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 *dnew = 0;
838 break;
839
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000840 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /*
842 * Filling in the first part of a previous delayed allocation.
843 * The left neighbor is contiguous.
844 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000845 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
846 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 LEFT.br_blockcount + new->br_blockcount);
848 xfs_bmbt_set_startoff(ep,
849 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000850 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000853 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 xfs_bmbt_set_blockcount(ep, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (cur == NULL)
856 rval = XFS_ILOG_DEXT;
857 else {
858 rval = 0;
859 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
860 LEFT.br_startblock, LEFT.br_blockcount,
861 &i)))
862 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000863 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
865 LEFT.br_startblock,
866 LEFT.br_blockcount +
867 new->br_blockcount,
868 LEFT.br_state)))
869 goto done;
870 }
871 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600872 startblockval(PREV.br_startblock));
873 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000874 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
875
876 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 *dnew = temp;
878 break;
879
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000880 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /*
882 * Filling in the first part of a previous delayed allocation.
883 * The left neighbor is not contiguous.
884 */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000885 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 xfs_bmbt_set_startoff(ep, new_endoff);
887 temp = PREV.br_blockcount - new->br_blockcount;
888 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000889 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 ip->i_d.di_nextents++;
891 if (cur == NULL)
892 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
893 else {
894 rval = XFS_ILOG_CORE;
895 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
896 new->br_startblock, new->br_blockcount,
897 &i)))
898 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000899 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100901 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000903 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
906 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
Christoph Hellwigf3ca8732011-07-08 14:34:47 +0200907 error = xfs_bmap_extents_to_btree(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 first, flist, &cur, 1, &tmp_rval,
909 XFS_DATA_FORK);
910 rval |= tmp_rval;
911 if (error)
912 goto done;
913 }
914 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600915 startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 (cur ? cur->bc_private.b.allocated : 0));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000917 ep = xfs_iext_get_ext(ifp, *idx + 1);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600918 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000919 trace_xfs_bmap_post_update(ip, *idx + 1, state, _THIS_IP_);
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 *dnew = temp;
922 break;
923
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000924 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /*
926 * Filling in the last part of a previous delayed allocation.
927 * The right neighbor is contiguous with the new allocation.
928 */
929 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000930 trace_xfs_bmap_pre_update(ip, *idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000932 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100933 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 new->br_blockcount + RIGHT.br_blockcount,
935 RIGHT.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000936 trace_xfs_bmap_post_update(ip, *idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (cur == NULL)
938 rval = XFS_ILOG_DEXT;
939 else {
940 rval = 0;
941 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
942 RIGHT.br_startblock,
943 RIGHT.br_blockcount, &i)))
944 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000945 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if ((error = xfs_bmbt_update(cur, new->br_startoff,
947 new->br_startblock,
948 new->br_blockcount +
949 RIGHT.br_blockcount,
950 RIGHT.br_state)))
951 goto done;
952 }
Christoph Hellwigec90c552011-05-23 08:52:53 +0000953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600955 startblockval(PREV.br_startblock));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000956 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600957 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +0000958 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
959
960 ++*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 *dnew = temp;
962 break;
963
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000964 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /*
966 * Filling in the last part of a previous delayed allocation.
967 * The right neighbor is not contiguous.
968 */
969 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +0000970 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000972 xfs_iext_insert(ip, *idx + 1, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 ip->i_d.di_nextents++;
974 if (cur == NULL)
975 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
976 else {
977 rval = XFS_ILOG_CORE;
978 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
979 new->br_startblock, new->br_blockcount,
980 &i)))
981 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000982 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100984 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000986 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
989 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
Christoph Hellwigf3ca8732011-07-08 14:34:47 +0200990 error = xfs_bmap_extents_to_btree(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 first, flist, &cur, 1, &tmp_rval,
992 XFS_DATA_FORK);
993 rval |= tmp_rval;
994 if (error)
995 goto done;
996 }
997 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600998 startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 (cur ? cur->bc_private.b.allocated : 0));
Christoph Hellwigec90c552011-05-23 08:52:53 +00001000 ep = xfs_iext_get_ext(ifp, *idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001001 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +00001002 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1003
1004 ++*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 *dnew = temp;
1006 break;
1007
1008 case 0:
1009 /*
1010 * Filling in the middle part of a previous delayed allocation.
1011 * Contiguity is impossible here.
1012 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001013 *
1014 * We start with a delayed allocation:
1015 *
1016 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1017 * PREV @ idx
1018 *
1019 * and we are allocating:
1020 * +rrrrrrrrrrrrrrrrr+
1021 * new
1022 *
1023 * and we set it up for insertion as:
1024 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1025 * new
1026 * PREV @ idx LEFT RIGHT
1027 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 */
1029 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001031 trace_xfs_bmap_pre_update(ip, *idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001032 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
1033 LEFT = *new;
1034 RIGHT.br_state = PREV.br_state;
1035 RIGHT.br_startblock = nullstartblock(
1036 (int)xfs_bmap_worst_indlen(ip, temp2));
1037 RIGHT.br_startoff = new_endoff;
1038 RIGHT.br_blockcount = temp2;
1039 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001040 xfs_iext_insert(ip, *idx + 1, 2, &LEFT, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 ip->i_d.di_nextents++;
1042 if (cur == NULL)
1043 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1044 else {
1045 rval = XFS_ILOG_CORE;
1046 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1047 new->br_startblock, new->br_blockcount,
1048 &i)))
1049 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001050 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001052 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001054 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1057 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
Christoph Hellwigf3ca8732011-07-08 14:34:47 +02001058 error = xfs_bmap_extents_to_btree(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 first, flist, &cur, 1, &tmp_rval,
1060 XFS_DATA_FORK);
1061 rval |= tmp_rval;
1062 if (error)
1063 goto done;
1064 }
1065 temp = xfs_bmap_worst_indlen(ip, temp);
1066 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001067 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 (cur ? cur->bc_private.b.allocated : 0));
1069 if (diff > 0 &&
Christoph Hellwig96540c72010-09-30 02:25:55 +00001070 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00001071 -((int64_t)diff), 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 /*
1073 * Ick gross gag me with a spoon.
1074 */
1075 ASSERT(0); /* want to see if this ever happens! */
1076 while (diff > 0) {
1077 if (temp) {
1078 temp--;
1079 diff--;
1080 if (!diff ||
Christoph Hellwig96540c72010-09-30 02:25:55 +00001081 !xfs_icsb_modify_counters(ip->i_mount,
1082 XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00001083 -((int64_t)diff), 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 break;
1085 }
1086 if (temp2) {
1087 temp2--;
1088 diff--;
1089 if (!diff ||
Christoph Hellwig96540c72010-09-30 02:25:55 +00001090 !xfs_icsb_modify_counters(ip->i_mount,
1091 XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00001092 -((int64_t)diff), 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 break;
1094 }
1095 }
1096 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001097 ep = xfs_iext_get_ext(ifp, *idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001098 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +00001099 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1100 trace_xfs_bmap_pre_update(ip, *idx + 2, state, _THIS_IP_);
1101 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001102 nullstartblock((int)temp2));
Christoph Hellwigec90c552011-05-23 08:52:53 +00001103 trace_xfs_bmap_post_update(ip, *idx + 2, state, _THIS_IP_);
1104
1105 ++*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 *dnew = temp + temp2;
1107 break;
1108
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001109 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1110 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1111 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1112 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1113 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1114 case BMAP_LEFT_CONTIG:
1115 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /*
1117 * These cases are all impossible.
1118 */
1119 ASSERT(0);
1120 }
1121 *curp = cur;
1122done:
1123 *logflagsp = rval;
1124 return error;
1125#undef LEFT
1126#undef RIGHT
1127#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130/*
1131 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1132 * allocation to a real allocation or vice versa.
1133 */
1134STATIC int /* error */
1135xfs_bmap_add_extent_unwritten_real(
1136 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001137 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001139 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001140 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001143 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001146 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 xfs_fileoff_t new_endoff; /* end offset of new entry */
1148 xfs_exntst_t newext; /* new extent state */
1149 xfs_exntst_t oldext; /* old extent state */
1150 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1151 /* left is 0, right is 1, prev is 2 */
1152 int rval=0; /* return value (logging flags) */
1153 int state = 0;/* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155#define LEFT r[0]
1156#define RIGHT r[1]
1157#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 /*
1159 * Set up a bunch of variables to make the tests simpler.
1160 */
1161 error = 0;
1162 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001163 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001164 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 xfs_bmbt_get_all(ep, &PREV);
1166 newext = new->br_state;
1167 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1168 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1169 ASSERT(PREV.br_state == oldext);
1170 new_endoff = new->br_startoff + new->br_blockcount;
1171 ASSERT(PREV.br_startoff <= new->br_startoff);
1172 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 /*
1175 * Set flags determining what part of the previous oldext allocation
1176 * extent is being replaced by a newext allocation.
1177 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001178 if (PREV.br_startoff == new->br_startoff)
1179 state |= BMAP_LEFT_FILLING;
1180 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1181 state |= BMAP_RIGHT_FILLING;
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /*
1184 * Check and set flags if this segment has a left neighbor.
1185 * Don't set contiguous if the combined extent would be too large.
1186 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001187 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001188 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001189 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001190
1191 if (isnullstartblock(LEFT.br_startblock))
1192 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001194
1195 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1196 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1197 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1198 LEFT.br_state == newext &&
1199 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1200 state |= BMAP_LEFT_CONTIG;
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /*
1203 * Check and set flags if this segment has a right neighbor.
1204 * Don't set contiguous if the combined extent would be too large.
1205 * Also check for all-three-contiguous being too large.
1206 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001207 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001208 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001209 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001210 if (isnullstartblock(RIGHT.br_startblock))
1211 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001213
1214 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1215 new_endoff == RIGHT.br_startoff &&
1216 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1217 newext == RIGHT.br_state &&
1218 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1219 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1220 BMAP_RIGHT_FILLING)) !=
1221 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1222 BMAP_RIGHT_FILLING) ||
1223 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1224 <= MAXEXTLEN))
1225 state |= BMAP_RIGHT_CONTIG;
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /*
1228 * Switch out based on the FILLING and CONTIG state bits.
1229 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001230 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1231 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1232 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1233 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 /*
1235 * Setting all of a previous oldext extent to newext.
1236 * The left and right neighbors are both contiguous with new.
1237 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001238 --*idx;
1239
1240 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1241 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 LEFT.br_blockcount + PREV.br_blockcount +
1243 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001244 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001245
Christoph Hellwigec90c552011-05-23 08:52:53 +00001246 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 ip->i_d.di_nextents -= 2;
1248 if (cur == NULL)
1249 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1250 else {
1251 rval = XFS_ILOG_CORE;
1252 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1253 RIGHT.br_startblock,
1254 RIGHT.br_blockcount, &i)))
1255 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001256 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11001257 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001259 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001260 if ((error = xfs_btree_decrement(cur, 0, &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 Hellwig91cca5d2008-10-30 16:58:01 +11001263 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001265 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001266 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001268 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1270 LEFT.br_startblock,
1271 LEFT.br_blockcount + PREV.br_blockcount +
1272 RIGHT.br_blockcount, LEFT.br_state)))
1273 goto done;
1274 }
1275 break;
1276
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001277 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 /*
1279 * Setting all of a previous oldext extent to newext.
1280 * The left neighbor is contiguous, the right is not.
1281 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001282 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001283
Christoph Hellwigec90c552011-05-23 08:52:53 +00001284 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1285 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1286 LEFT.br_blockcount + PREV.br_blockcount);
1287 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1288
1289 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 ip->i_d.di_nextents--;
1291 if (cur == NULL)
1292 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1293 else {
1294 rval = XFS_ILOG_CORE;
1295 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1296 PREV.br_startblock, PREV.br_blockcount,
1297 &i)))
1298 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001299 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11001300 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001302 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001303 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001305 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1307 LEFT.br_startblock,
1308 LEFT.br_blockcount + PREV.br_blockcount,
1309 LEFT.br_state)))
1310 goto done;
1311 }
1312 break;
1313
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001314 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /*
1316 * Setting all of a previous oldext extent to newext.
1317 * The right neighbor is contiguous, the left is not.
1318 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001319 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 xfs_bmbt_set_blockcount(ep,
1321 PREV.br_blockcount + RIGHT.br_blockcount);
1322 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001323 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1324 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 ip->i_d.di_nextents--;
1326 if (cur == NULL)
1327 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1328 else {
1329 rval = XFS_ILOG_CORE;
1330 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1331 RIGHT.br_startblock,
1332 RIGHT.br_blockcount, &i)))
1333 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001334 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11001335 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001337 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001338 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001340 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1342 new->br_startblock,
1343 new->br_blockcount + RIGHT.br_blockcount,
1344 newext)))
1345 goto done;
1346 }
1347 break;
1348
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001349 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 /*
1351 * Setting all of a previous oldext extent to newext.
1352 * Neither the left nor right neighbors are contiguous with
1353 * the new one.
1354 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001355 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001357 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (cur == NULL)
1360 rval = XFS_ILOG_DEXT;
1361 else {
1362 rval = 0;
1363 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1364 new->br_startblock, new->br_blockcount,
1365 &i)))
1366 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001367 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1369 new->br_startblock, new->br_blockcount,
1370 newext)))
1371 goto done;
1372 }
1373 break;
1374
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001375 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 /*
1377 * Setting the first part of a previous oldext extent to newext.
1378 * The left neighbor is contiguous.
1379 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001380 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
1381 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 LEFT.br_blockcount + new->br_blockcount);
1383 xfs_bmbt_set_startoff(ep,
1384 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001385 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001386
Christoph Hellwigec90c552011-05-23 08:52:53 +00001387 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 xfs_bmbt_set_startblock(ep,
1389 new->br_startblock + new->br_blockcount);
1390 xfs_bmbt_set_blockcount(ep,
1391 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001392 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001393
Christoph Hellwigec90c552011-05-23 08:52:53 +00001394 --*idx;
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (cur == NULL)
1397 rval = XFS_ILOG_DEXT;
1398 else {
1399 rval = 0;
1400 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1401 PREV.br_startblock, PREV.br_blockcount,
1402 &i)))
1403 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001404 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 if ((error = xfs_bmbt_update(cur,
1406 PREV.br_startoff + new->br_blockcount,
1407 PREV.br_startblock + new->br_blockcount,
1408 PREV.br_blockcount - new->br_blockcount,
1409 oldext)))
1410 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001411 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 goto done;
1413 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1414 LEFT.br_startblock,
1415 LEFT.br_blockcount + new->br_blockcount,
1416 LEFT.br_state))
1417 goto done;
1418 }
1419 break;
1420
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001421 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 /*
1423 * Setting the first part of a previous oldext extent to newext.
1424 * The left neighbor is not contiguous.
1425 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001426 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1428 xfs_bmbt_set_startoff(ep, new_endoff);
1429 xfs_bmbt_set_blockcount(ep,
1430 PREV.br_blockcount - new->br_blockcount);
1431 xfs_bmbt_set_startblock(ep,
1432 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001433 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001434
Christoph Hellwigec90c552011-05-23 08:52:53 +00001435 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 ip->i_d.di_nextents++;
1437 if (cur == NULL)
1438 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1439 else {
1440 rval = XFS_ILOG_CORE;
1441 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1442 PREV.br_startblock, PREV.br_blockcount,
1443 &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,
1447 PREV.br_startoff + new->br_blockcount,
1448 PREV.br_startblock + new->br_blockcount,
1449 PREV.br_blockcount - new->br_blockcount,
1450 oldext)))
1451 goto done;
1452 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001453 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001455 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457 break;
1458
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001459 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /*
1461 * Setting the last part of a previous oldext extent to newext.
1462 * The right neighbor is contiguous with the new allocation.
1463 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001464 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 xfs_bmbt_set_blockcount(ep,
1466 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001467 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1468
1469 ++*idx;
1470
1471 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1472 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001473 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001475 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (cur == NULL)
1478 rval = XFS_ILOG_DEXT;
1479 else {
1480 rval = 0;
1481 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1482 PREV.br_startblock,
1483 PREV.br_blockcount, &i)))
1484 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001485 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1487 PREV.br_startblock,
1488 PREV.br_blockcount - new->br_blockcount,
1489 oldext)))
1490 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001491 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 goto done;
1493 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1494 new->br_startblock,
1495 new->br_blockcount + RIGHT.br_blockcount,
1496 newext)))
1497 goto done;
1498 }
1499 break;
1500
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001501 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 /*
1503 * Setting the last part of a previous oldext extent to newext.
1504 * The right neighbor is not contiguous.
1505 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001506 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 xfs_bmbt_set_blockcount(ep,
1508 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001509 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001510
Christoph Hellwigec90c552011-05-23 08:52:53 +00001511 ++*idx;
1512 xfs_iext_insert(ip, *idx, 1, new, state);
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 ip->i_d.di_nextents++;
1515 if (cur == NULL)
1516 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1517 else {
1518 rval = XFS_ILOG_CORE;
1519 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1520 PREV.br_startblock, PREV.br_blockcount,
1521 &i)))
1522 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001523 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1525 PREV.br_startblock,
1526 PREV.br_blockcount - new->br_blockcount,
1527 oldext)))
1528 goto done;
1529 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1530 new->br_startblock, new->br_blockcount,
1531 &i)))
1532 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001533 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001535 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001537 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539 break;
1540
1541 case 0:
1542 /*
1543 * Setting the middle part of a previous oldext extent to
1544 * newext. Contiguity is impossible here.
1545 * One extent becomes three extents.
1546 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001547 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 xfs_bmbt_set_blockcount(ep,
1549 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001550 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 r[0] = *new;
1553 r[1].br_startoff = new_endoff;
1554 r[1].br_blockcount =
1555 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1556 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1557 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001558
1559 ++*idx;
1560 xfs_iext_insert(ip, *idx, 2, &r[0], state);
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 ip->i_d.di_nextents += 2;
1563 if (cur == NULL)
1564 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1565 else {
1566 rval = XFS_ILOG_CORE;
1567 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1568 PREV.br_startblock, PREV.br_blockcount,
1569 &i)))
1570 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001571 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /* new right extent - oldext */
1573 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1574 r[1].br_startblock, r[1].br_blockcount,
1575 r[1].br_state)))
1576 goto done;
1577 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10001579 cur->bc_rec.b.br_blockcount =
1580 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001581 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001583 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001584 /*
1585 * Reset the cursor to the position of the new extent
1586 * we are about to insert as we can't trust it after
1587 * the previous insert.
1588 */
1589 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1590 new->br_startblock, new->br_blockcount,
1591 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001593 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001595 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001596 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001598 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
1600 break;
1601
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001602 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1603 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1604 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1605 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1606 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1607 case BMAP_LEFT_CONTIG:
1608 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 /*
1610 * These cases are all impossible.
1611 */
1612 ASSERT(0);
1613 }
1614 *curp = cur;
1615done:
1616 *logflagsp = rval;
1617 return error;
1618#undef LEFT
1619#undef RIGHT
1620#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
1623/*
1624 * Called by xfs_bmap_add_extent to handle cases converting a hole
1625 * to a delayed allocation.
1626 */
1627/*ARGSUSED*/
1628STATIC int /* error */
1629xfs_bmap_add_extent_hole_delay(
1630 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001631 xfs_extnum_t *idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001632 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwig54893272011-05-11 15:04:03 +00001633 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001635 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1637 xfs_filblks_t newlen=0; /* new indirect size */
1638 xfs_filblks_t oldlen=0; /* old indirect size */
1639 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1640 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001641 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001643 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001645 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 /*
1648 * Check and set flags if this segment has a left neighbor
1649 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001650 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001651 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001652 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001653
1654 if (isnullstartblock(left.br_startblock))
1655 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 /*
1659 * Check and set flags if the current (right) segment exists.
1660 * If it doesn't exist, we're converting the hole at end-of-file.
1661 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001662 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001663 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00001664 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001665
1666 if (isnullstartblock(right.br_startblock))
1667 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 /*
1671 * Set contiguity flags on the left and right neighbors.
1672 * Don't let extents get too large, even if the pieces are contiguous.
1673 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001674 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1675 left.br_startoff + left.br_blockcount == new->br_startoff &&
1676 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1677 state |= BMAP_LEFT_CONTIG;
1678
1679 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1680 new->br_startoff + new->br_blockcount == right.br_startoff &&
1681 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1682 (!(state & BMAP_LEFT_CONTIG) ||
1683 (left.br_blockcount + new->br_blockcount +
1684 right.br_blockcount <= MAXEXTLEN)))
1685 state |= BMAP_RIGHT_CONTIG;
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /*
1688 * Switch out based on the contiguity flags.
1689 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001690 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1691 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 /*
1693 * New allocation is contiguous with delayed allocations
1694 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001695 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001697 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 temp = left.br_blockcount + new->br_blockcount +
1699 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001700
Christoph Hellwigec90c552011-05-23 08:52:53 +00001701 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1702 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001703 oldlen = startblockval(left.br_startblock) +
1704 startblockval(new->br_startblock) +
1705 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001707 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001708 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00001709 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001710
Christoph Hellwigec90c552011-05-23 08:52:53 +00001711 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 break;
1713
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001714 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 /*
1716 * New allocation is contiguous with a delayed allocation
1717 * on the left.
1718 * Merge the new allocation with the left neighbor.
1719 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001720 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001722
1723 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1724 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001725 oldlen = startblockval(left.br_startblock) +
1726 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001728 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001729 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00001730 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 break;
1732
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001733 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 /*
1735 * New allocation is contiguous with a delayed allocation
1736 * on the right.
1737 * Merge the new allocation with the right neighbor.
1738 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001739 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001741 oldlen = startblockval(new->br_startblock) +
1742 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00001744 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1745 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06001746 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001747 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 break;
1749
1750 case 0:
1751 /*
1752 * New allocation is not contiguous with another
1753 * delayed allocation.
1754 * Insert a new entry.
1755 */
1756 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001757 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 break;
1759 }
1760 if (oldlen != newlen) {
1761 ASSERT(oldlen > newlen);
Christoph Hellwig96540c72010-09-30 02:25:55 +00001762 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00001763 (int64_t)(oldlen - newlen), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 /*
1765 * Nothing to do for disk quota accounting here.
1766 */
1767 }
1768 *logflagsp = 0;
1769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
1772/*
1773 * Called by xfs_bmap_add_extent to handle cases converting a hole
1774 * to a real allocation.
1775 */
1776STATIC int /* error */
1777xfs_bmap_add_extent_hole_real(
1778 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001779 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001781 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 int *logflagsp, /* inode logging flags */
1783 int whichfork) /* data or attr fork */
1784{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 int i; /* temp state */
1787 xfs_ifork_t *ifp; /* inode fork pointer */
1788 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1789 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001790 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001794 ASSERT(*idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 state = 0;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001796
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001797 if (whichfork == XFS_ATTR_FORK)
1798 state |= BMAP_ATTRFORK;
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 /*
1801 * Check and set flags if this segment has a left neighbor.
1802 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001803 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001804 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001805 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001806 if (isnullstartblock(left.br_startblock))
1807 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 /*
1811 * Check and set flags if this segment has a current value.
1812 * Not true if we're inserting into the "hole" at eof.
1813 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001814 if (*idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001815 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00001816 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001817 if (isnullstartblock(right.br_startblock))
1818 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 /*
1822 * We're inserting a real allocation between "left" and "right".
1823 * Set the contiguity flags. Don't let extents get too large.
1824 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001825 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1826 left.br_startoff + left.br_blockcount == new->br_startoff &&
1827 left.br_startblock + left.br_blockcount == new->br_startblock &&
1828 left.br_state == new->br_state &&
1829 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1830 state |= BMAP_LEFT_CONTIG;
1831
1832 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1833 new->br_startoff + new->br_blockcount == right.br_startoff &&
1834 new->br_startblock + new->br_blockcount == right.br_startblock &&
1835 new->br_state == right.br_state &&
1836 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1837 (!(state & BMAP_LEFT_CONTIG) ||
1838 left.br_blockcount + new->br_blockcount +
1839 right.br_blockcount <= MAXEXTLEN))
1840 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001842 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 /*
1844 * Select which case we're in here, and implement it.
1845 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001846 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1847 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 /*
1849 * New allocation is contiguous with real allocations on the
1850 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001851 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001853 --*idx;
1854 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1855 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 left.br_blockcount + new->br_blockcount +
1857 right.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001858 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001859
Christoph Hellwigec90c552011-05-23 08:52:53 +00001860 xfs_iext_remove(ip, *idx + 1, 1, state);
1861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 XFS_IFORK_NEXT_SET(ip, whichfork,
1863 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1864 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001865 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001866 } else {
1867 rval = XFS_ILOG_CORE;
1868 if ((error = xfs_bmbt_lookup_eq(cur,
1869 right.br_startoff,
1870 right.br_startblock,
1871 right.br_blockcount, &i)))
1872 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001873 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11001874 if ((error = xfs_btree_delete(cur, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001875 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001876 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001877 if ((error = xfs_btree_decrement(cur, 0, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001878 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001879 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001880 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1881 left.br_startblock,
1882 left.br_blockcount +
1883 new->br_blockcount +
1884 right.br_blockcount,
1885 left.br_state)))
1886 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001888 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001890 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 /*
1892 * New allocation is contiguous with a real allocation
1893 * on the left.
1894 * Merge the new allocation with the left neighbor.
1895 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001896 --*idx;
1897 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1898 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 left.br_blockcount + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001900 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001903 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001904 } else {
1905 rval = 0;
1906 if ((error = xfs_bmbt_lookup_eq(cur,
1907 left.br_startoff,
1908 left.br_startblock,
1909 left.br_blockcount, &i)))
1910 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001911 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001912 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1913 left.br_startblock,
1914 left.br_blockcount +
1915 new->br_blockcount,
1916 left.br_state)))
1917 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001919 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001921 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 /*
1923 * New allocation is contiguous with a real allocation
1924 * on the right.
1925 * Merge the new allocation with the right neighbor.
1926 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001927 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00001928 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1929 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 new->br_blockcount + right.br_blockcount,
1931 right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001932 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001935 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001936 } else {
1937 rval = 0;
1938 if ((error = xfs_bmbt_lookup_eq(cur,
1939 right.br_startoff,
1940 right.br_startblock,
1941 right.br_blockcount, &i)))
1942 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001943 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001944 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1945 new->br_startblock,
1946 new->br_blockcount +
1947 right.br_blockcount,
1948 right.br_state)))
1949 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001951 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 case 0:
1954 /*
1955 * New allocation is not contiguous with another
1956 * real allocation.
1957 * Insert a new entry.
1958 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001959 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 XFS_IFORK_NEXT_SET(ip, whichfork,
1961 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
1962 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001963 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001964 } else {
1965 rval = XFS_ILOG_CORE;
1966 if ((error = xfs_bmbt_lookup_eq(cur,
1967 new->br_startoff,
1968 new->br_startblock,
1969 new->br_blockcount, &i)))
1970 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001971 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001972 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001973 if ((error = xfs_btree_insert(cur, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001974 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001975 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001977 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001979done:
1980 *logflagsp = rval;
1981 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
Nathan Scottdd9f4382006-01-11 15:28:28 +11001984/*
1985 * Adjust the size of the new extent based on di_extsize and rt extsize.
1986 */
1987STATIC int
1988xfs_bmap_extsize_align(
1989 xfs_mount_t *mp,
1990 xfs_bmbt_irec_t *gotp, /* next extent pointer */
1991 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
1992 xfs_extlen_t extsz, /* align to this extent size */
1993 int rt, /* is this a realtime inode? */
1994 int eof, /* is extent at end-of-file? */
1995 int delay, /* creating delalloc extent? */
1996 int convert, /* overwriting unwritten extent? */
1997 xfs_fileoff_t *offp, /* in/out: aligned offset */
1998 xfs_extlen_t *lenp) /* in/out: aligned length */
1999{
2000 xfs_fileoff_t orig_off; /* original offset */
2001 xfs_extlen_t orig_alen; /* original length */
2002 xfs_fileoff_t orig_end; /* original off+len */
2003 xfs_fileoff_t nexto; /* next file offset */
2004 xfs_fileoff_t prevo; /* previous file offset */
2005 xfs_fileoff_t align_off; /* temp for offset */
2006 xfs_extlen_t align_alen; /* temp for length */
2007 xfs_extlen_t temp; /* temp for calculations */
2008
2009 if (convert)
2010 return 0;
2011
2012 orig_off = align_off = *offp;
2013 orig_alen = align_alen = *lenp;
2014 orig_end = orig_off + orig_alen;
2015
2016 /*
2017 * If this request overlaps an existing extent, then don't
2018 * attempt to perform any additional alignment.
2019 */
2020 if (!delay && !eof &&
2021 (orig_off >= gotp->br_startoff) &&
2022 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2023 return 0;
2024 }
2025
2026 /*
2027 * If the file offset is unaligned vs. the extent size
2028 * we need to align it. This will be possible unless
2029 * the file was previously written with a kernel that didn't
2030 * perform this alignment, or if a truncate shot us in the
2031 * foot.
2032 */
2033 temp = do_mod(orig_off, extsz);
2034 if (temp) {
2035 align_alen += temp;
2036 align_off -= temp;
2037 }
2038 /*
2039 * Same adjustment for the end of the requested area.
2040 */
2041 if ((temp = (align_alen % extsz))) {
2042 align_alen += extsz - temp;
2043 }
2044 /*
2045 * If the previous block overlaps with this proposed allocation
2046 * then move the start forward without adjusting the length.
2047 */
2048 if (prevp->br_startoff != NULLFILEOFF) {
2049 if (prevp->br_startblock == HOLESTARTBLOCK)
2050 prevo = prevp->br_startoff;
2051 else
2052 prevo = prevp->br_startoff + prevp->br_blockcount;
2053 } else
2054 prevo = 0;
2055 if (align_off != orig_off && align_off < prevo)
2056 align_off = prevo;
2057 /*
2058 * If the next block overlaps with this proposed allocation
2059 * then move the start back without adjusting the length,
2060 * but not before offset 0.
2061 * This may of course make the start overlap previous block,
2062 * and if we hit the offset 0 limit then the next block
2063 * can still overlap too.
2064 */
2065 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2066 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2067 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2068 nexto = gotp->br_startoff + gotp->br_blockcount;
2069 else
2070 nexto = gotp->br_startoff;
2071 } else
2072 nexto = NULLFILEOFF;
2073 if (!eof &&
2074 align_off + align_alen != orig_end &&
2075 align_off + align_alen > nexto)
2076 align_off = nexto > align_alen ? nexto - align_alen : 0;
2077 /*
2078 * If we're now overlapping the next or previous extent that
2079 * means we can't fit an extsz piece in this hole. Just move
2080 * the start forward to the first valid spot and set
2081 * the length so we hit the end.
2082 */
2083 if (align_off != orig_off && align_off < prevo)
2084 align_off = prevo;
2085 if (align_off + align_alen != orig_end &&
2086 align_off + align_alen > nexto &&
2087 nexto != NULLFILEOFF) {
2088 ASSERT(nexto > prevo);
2089 align_alen = nexto - align_off;
2090 }
2091
2092 /*
2093 * If realtime, and the result isn't a multiple of the realtime
2094 * extent size we need to remove blocks until it is.
2095 */
2096 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2097 /*
2098 * We're not covering the original request, or
2099 * we won't be able to once we fix the length.
2100 */
2101 if (orig_off < align_off ||
2102 orig_end > align_off + align_alen ||
2103 align_alen - temp < orig_alen)
2104 return XFS_ERROR(EINVAL);
2105 /*
2106 * Try to fix it by moving the start up.
2107 */
2108 if (align_off + temp <= orig_off) {
2109 align_alen -= temp;
2110 align_off += temp;
2111 }
2112 /*
2113 * Try to fix it by moving the end in.
2114 */
2115 else if (align_off + align_alen - temp >= orig_end)
2116 align_alen -= temp;
2117 /*
2118 * Set the start to the minimum then trim the length.
2119 */
2120 else {
2121 align_alen -= orig_off - align_off;
2122 align_off = orig_off;
2123 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2124 }
2125 /*
2126 * Result doesn't cover the request, fail it.
2127 */
2128 if (orig_off < align_off || orig_end > align_off + align_alen)
2129 return XFS_ERROR(EINVAL);
2130 } else {
2131 ASSERT(orig_off >= align_off);
2132 ASSERT(orig_end <= align_off + align_alen);
2133 }
2134
2135#ifdef DEBUG
2136 if (!eof && gotp->br_startoff != NULLFILEOFF)
2137 ASSERT(align_off + align_alen <= gotp->br_startoff);
2138 if (prevp->br_startoff != NULLFILEOFF)
2139 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2140#endif
2141
2142 *lenp = align_alen;
2143 *offp = align_off;
2144 return 0;
2145}
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147#define XFS_ALLOC_GAP_UNITS 4
2148
David Chinnerc2b1cba2008-04-10 12:21:40 +10002149STATIC void
Nathan Scotta365bdd2006-03-14 13:34:16 +11002150xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2152{
2153 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2155 xfs_mount_t *mp; /* mount point structure */
2156 int nullfb; /* true if ap->firstblock isn't set */
2157 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
2159#define ISVALID(x,y) \
2160 (rt ? \
2161 (x) < mp->m_sb.sb_rblocks : \
2162 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2163 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2164 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 mp = ap->ip->i_mount;
2167 nullfb = ap->firstblock == NULLFSBLOCK;
2168 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2169 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /*
2171 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002172 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 */
2174 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002175 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2177 ap->prevp->br_startblock)) {
2178 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2179 /*
2180 * Adjust for the gap between prevp and us.
2181 */
2182 adjust = ap->off -
2183 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2184 if (adjust &&
2185 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2186 ap->rval += adjust;
2187 }
2188 /*
2189 * If not at eof, then compare the two neighbor blocks.
2190 * Figure out whether either one gives us a good starting point,
2191 * and pick the better one.
2192 */
2193 else if (!ap->eof) {
2194 xfs_fsblock_t gotbno; /* right side block number */
2195 xfs_fsblock_t gotdiff=0; /* right side difference */
2196 xfs_fsblock_t prevbno; /* left side block number */
2197 xfs_fsblock_t prevdiff=0; /* left side difference */
2198
2199 /*
2200 * If there's a previous (left) block, select a requested
2201 * start block based on it.
2202 */
2203 if (ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002204 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 (prevbno = ap->prevp->br_startblock +
2206 ap->prevp->br_blockcount) &&
2207 ISVALID(prevbno, ap->prevp->br_startblock)) {
2208 /*
2209 * Calculate gap to end of previous block.
2210 */
2211 adjust = prevdiff = ap->off -
2212 (ap->prevp->br_startoff +
2213 ap->prevp->br_blockcount);
2214 /*
2215 * Figure the startblock based on the previous block's
2216 * end and the gap size.
2217 * Heuristic!
2218 * If the gap is large relative to the piece we're
2219 * allocating, or using it gives us an invalid block
2220 * number, then just use the end of the previous block.
2221 */
2222 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2223 ISVALID(prevbno + prevdiff,
2224 ap->prevp->br_startblock))
2225 prevbno += adjust;
2226 else
2227 prevdiff += adjust;
2228 /*
2229 * If the firstblock forbids it, can't use it,
2230 * must use default.
2231 */
2232 if (!rt && !nullfb &&
2233 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2234 prevbno = NULLFSBLOCK;
2235 }
2236 /*
2237 * No previous block or can't follow it, just default.
2238 */
2239 else
2240 prevbno = NULLFSBLOCK;
2241 /*
2242 * If there's a following (right) block, select a requested
2243 * start block based on it.
2244 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002245 if (!isnullstartblock(ap->gotp->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 /*
2247 * Calculate gap to start of next block.
2248 */
2249 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2250 /*
2251 * Figure the startblock based on the next block's
2252 * start and the gap size.
2253 */
2254 gotbno = ap->gotp->br_startblock;
2255 /*
2256 * Heuristic!
2257 * If the gap is large relative to the piece we're
2258 * allocating, or using it gives us an invalid block
2259 * number, then just use the start of the next block
2260 * offset by our length.
2261 */
2262 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2263 ISVALID(gotbno - gotdiff, gotbno))
2264 gotbno -= adjust;
2265 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2266 gotbno -= ap->alen;
2267 gotdiff += adjust - ap->alen;
2268 } else
2269 gotdiff += adjust;
2270 /*
2271 * If the firstblock forbids it, can't use it,
2272 * must use default.
2273 */
2274 if (!rt && !nullfb &&
2275 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2276 gotbno = NULLFSBLOCK;
2277 }
2278 /*
2279 * No next block, just default.
2280 */
2281 else
2282 gotbno = NULLFSBLOCK;
2283 /*
2284 * If both valid, pick the better one, else the only good
2285 * one, else ap->rval is already set (to 0 or the inode block).
2286 */
2287 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2288 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2289 else if (prevbno != NULLFSBLOCK)
2290 ap->rval = prevbno;
2291 else if (gotbno != NULLFSBLOCK)
2292 ap->rval = gotbno;
2293 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002294#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11002295}
2296
2297STATIC int
2298xfs_bmap_rtalloc(
2299 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2300{
2301 xfs_alloctype_t atype = 0; /* type for allocation routines */
2302 int error; /* error return value */
2303 xfs_mount_t *mp; /* mount point structure */
2304 xfs_extlen_t prod = 0; /* product factor for allocators */
2305 xfs_extlen_t ralen = 0; /* realtime allocation length */
2306 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002307 xfs_rtblock_t rtb;
2308
2309 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002310 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002311 prod = align / mp->m_sb.sb_rextsize;
2312 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2313 align, 1, ap->eof, 0,
2314 ap->conv, &ap->off, &ap->alen);
2315 if (error)
2316 return error;
2317 ASSERT(ap->alen);
2318 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2319
2320 /*
2321 * If the offset & length are not perfectly aligned
2322 * then kill prod, it will just get us in trouble.
2323 */
2324 if (do_mod(ap->off, align) || ap->alen % align)
2325 prod = 1;
2326 /*
2327 * Set ralen to be the actual requested length in rtextents.
2328 */
2329 ralen = ap->alen / mp->m_sb.sb_rextsize;
2330 /*
2331 * If the old value was close enough to MAXEXTLEN that
2332 * we rounded up to it, cut it back so it's valid again.
2333 * Note that if it's a really large request (bigger than
2334 * MAXEXTLEN), we don't hear about that number, and can't
2335 * adjust the starting point to match it.
2336 */
2337 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2338 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
Christoph Hellwig04e99452011-01-25 09:06:19 +00002339
2340 /*
2341 * Lock out other modifications to the RT bitmap inode.
2342 */
Christoph Hellwig1050c712011-02-13 13:25:31 +00002343 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
2344 xfs_trans_ijoin_ref(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Christoph Hellwig04e99452011-01-25 09:06:19 +00002345
Nathan Scotta365bdd2006-03-14 13:34:16 +11002346 /*
2347 * If it's an allocation to an empty file at offset 0,
2348 * pick an extent that will space things out in the rt area.
2349 */
2350 if (ap->eof && ap->off == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10002351 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2352
Nathan Scotta365bdd2006-03-14 13:34:16 +11002353 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2354 if (error)
2355 return error;
2356 ap->rval = rtx * mp->m_sb.sb_rextsize;
2357 } else {
2358 ap->rval = 0;
2359 }
2360
2361 xfs_bmap_adjacent(ap);
2362
2363 /*
2364 * Realtime allocation, done through xfs_rtallocate_extent.
2365 */
2366 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2367 do_div(ap->rval, mp->m_sb.sb_rextsize);
2368 rtb = ap->rval;
2369 ap->alen = ralen;
2370 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2371 &ralen, atype, ap->wasdel, prod, &rtb)))
2372 return error;
2373 if (rtb == NULLFSBLOCK && prod > 1 &&
2374 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2375 ap->alen, &ralen, atype,
2376 ap->wasdel, 1, &rtb)))
2377 return error;
2378 ap->rval = rtb;
2379 if (ap->rval != NULLFSBLOCK) {
2380 ap->rval *= mp->m_sb.sb_rextsize;
2381 ralen *= mp->m_sb.sb_rextsize;
2382 ap->alen = ralen;
2383 ap->ip->i_d.di_nblocks += ralen;
2384 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2385 if (ap->wasdel)
2386 ap->ip->i_delayed_blks -= ralen;
2387 /*
2388 * Adjust the disk quota also. This was reserved
2389 * earlier.
2390 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002391 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002392 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2393 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2394 } else {
2395 ap->alen = 0;
2396 }
2397 return 0;
2398}
2399
2400STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00002401xfs_bmap_btalloc_nullfb(
2402 struct xfs_bmalloca *ap,
2403 struct xfs_alloc_arg *args,
2404 xfs_extlen_t *blen)
2405{
2406 struct xfs_mount *mp = ap->ip->i_mount;
2407 struct xfs_perag *pag;
2408 xfs_agnumber_t ag, startag;
2409 int notinit = 0;
2410 int error;
2411
2412 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2413 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2414 else
2415 args->type = XFS_ALLOCTYPE_START_BNO;
2416 args->total = ap->total;
2417
2418 /*
2419 * Search for an allocation group with a single extent large enough
2420 * for the request. If one isn't found, then adjust the minimum
2421 * allocation size to the largest space found.
2422 */
2423 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2424 if (startag == NULLAGNUMBER)
2425 startag = ag = 0;
2426
2427 pag = xfs_perag_get(mp, ag);
Dave Chinner14b064c2011-01-27 12:16:28 +11002428 while (*blen < args->maxlen) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00002429 if (!pag->pagf_init) {
2430 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2431 XFS_ALLOC_FLAG_TRYLOCK);
2432 if (error) {
2433 xfs_perag_put(pag);
2434 return error;
2435 }
2436 }
2437
2438 /*
2439 * See xfs_alloc_fix_freelist...
2440 */
2441 if (pag->pagf_init) {
2442 xfs_extlen_t longest;
2443 longest = xfs_alloc_longest_free_extent(mp, pag);
2444 if (*blen < longest)
2445 *blen = longest;
2446 } else
2447 notinit = 1;
2448
2449 if (xfs_inode_is_filestream(ap->ip)) {
Dave Chinner14b064c2011-01-27 12:16:28 +11002450 if (*blen >= args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00002451 break;
2452
2453 if (ap->userdata) {
2454 /*
2455 * If startag is an invalid AG, we've
2456 * come here once before and
2457 * xfs_filestream_new_ag picked the
2458 * best currently available.
2459 *
2460 * Don't continue looping, since we
2461 * could loop forever.
2462 */
2463 if (startag == NULLAGNUMBER)
2464 break;
2465
2466 error = xfs_filestream_new_ag(ap, &ag);
2467 xfs_perag_put(pag);
2468 if (error)
2469 return error;
2470
2471 /* loop again to set 'blen'*/
2472 startag = NULLAGNUMBER;
2473 pag = xfs_perag_get(mp, ag);
2474 continue;
2475 }
2476 }
2477 if (++ag == mp->m_sb.sb_agcount)
2478 ag = 0;
2479 if (ag == startag)
2480 break;
2481 xfs_perag_put(pag);
2482 pag = xfs_perag_get(mp, ag);
2483 }
2484 xfs_perag_put(pag);
2485
2486 /*
2487 * Since the above loop did a BUF_TRYLOCK, it is
2488 * possible that there is space for this request.
2489 */
2490 if (notinit || *blen < ap->minlen)
2491 args->minlen = ap->minlen;
2492 /*
2493 * If the best seen length is less than the request
2494 * length, use the best as the minimum.
2495 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002496 else if (*blen < args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00002497 args->minlen = *blen;
2498 /*
Dave Chinner14b064c2011-01-27 12:16:28 +11002499 * Otherwise we've seen an extent as big as maxlen,
Christoph Hellwigc467c042010-02-15 23:34:42 +00002500 * use that as the minimum.
2501 */
2502 else
Dave Chinner14b064c2011-01-27 12:16:28 +11002503 args->minlen = args->maxlen;
Christoph Hellwigc467c042010-02-15 23:34:42 +00002504
2505 /*
2506 * set the failure fallback case to look in the selected
2507 * AG as the stream may have moved.
2508 */
2509 if (xfs_inode_is_filestream(ap->ip))
2510 ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2511
2512 return 0;
2513}
2514
2515STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11002516xfs_bmap_btalloc(
2517 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2518{
2519 xfs_mount_t *mp; /* mount point structure */
2520 xfs_alloctype_t atype = 0; /* type for allocation routines */
2521 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002522 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00002523 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002524 xfs_alloc_arg_t args;
2525 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002526 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002527 int nullfb; /* true if ap->firstblock isn't set */
2528 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002529 int tryagain;
2530 int error;
2531
2532 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002533 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002534 if (unlikely(align)) {
2535 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2536 align, 0, ap->eof, 0, ap->conv,
2537 &ap->off, &ap->alen);
2538 ASSERT(!error);
2539 ASSERT(ap->alen);
2540 }
2541 nullfb = ap->firstblock == NULLFSBLOCK;
2542 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10002543 if (nullfb) {
2544 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2545 ag = xfs_filestream_lookup_ag(ap->ip);
2546 ag = (ag != NULLAGNUMBER) ? ag : 0;
2547 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2548 } else {
2549 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2550 }
2551 } else
Nathan Scotta365bdd2006-03-14 13:34:16 +11002552 ap->rval = ap->firstblock;
2553
2554 xfs_bmap_adjacent(ap);
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 /*
2557 * If allowed, use ap->rval; otherwise must use firstblock since
2558 * it's in the right allocation group.
2559 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002560 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 ;
2562 else
2563 ap->rval = ap->firstblock;
2564 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 * Normal allocation, done through xfs_alloc_vextent.
2566 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002567 tryagain = isaligned = 0;
2568 args.tp = ap->tp;
2569 args.mp = mp;
2570 args.fsbno = ap->rval;
Dave Chinner14b064c2011-01-27 12:16:28 +11002571
2572 /* Trim the allocation back to the maximum an AG can fit. */
2573 args.maxlen = MIN(ap->alen, XFS_ALLOC_AG_MAX_USABLE(mp));
Yingping Lud210a282006-06-09 14:55:18 +10002574 args.firstblock = ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002575 blen = 0;
2576 if (nullfb) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00002577 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2578 if (error)
2579 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002580 } else if (ap->low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002581 if (xfs_inode_is_filestream(ap->ip))
2582 args.type = XFS_ALLOCTYPE_FIRST_AG;
2583 else
2584 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002585 args.total = args.minlen = ap->minlen;
2586 } else {
2587 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2588 args.total = ap->total;
2589 args.minlen = ap->minlen;
2590 }
David Chinner957d0eb2007-06-18 16:50:37 +10002591 /* apply extent size hints if obtained earlier */
2592 if (unlikely(align)) {
2593 args.prod = align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002594 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2595 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11002596 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11002597 args.prod = 1;
2598 args.mod = 0;
2599 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002600 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002601 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2602 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2603 }
2604 /*
2605 * If we are not low on available data blocks, and the
2606 * underlying logical volume manager is a stripe, and
2607 * the file offset is zero then try to allocate data
2608 * blocks on stripe unit boundary.
2609 * NOTE: ap->aeof is only set if the allocation length
2610 * is >= the stripe unit and the allocation offset is
2611 * at the end of file.
2612 */
2613 if (!ap->low && ap->aeof) {
2614 if (!ap->off) {
2615 args.alignment = mp->m_dalign;
2616 atype = args.type;
2617 isaligned = 1;
2618 /*
2619 * Adjust for alignment
2620 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002621 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002622 args.minlen = blen - args.alignment;
2623 args.minalignslop = 0;
2624 } else {
2625 /*
2626 * First try an exact bno allocation.
2627 * If it fails then do a near or start bno
2628 * allocation with alignment turned on.
2629 */
2630 atype = args.type;
2631 tryagain = 1;
2632 args.type = XFS_ALLOCTYPE_THIS_BNO;
2633 args.alignment = 1;
2634 /*
2635 * Compute the minlen+alignment for the
2636 * next case. Set slop so that the value
2637 * of minlen+alignment+slop doesn't go up
2638 * between the calls.
2639 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002640 if (blen > mp->m_dalign && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002641 nextminlen = blen - mp->m_dalign;
2642 else
2643 nextminlen = args.minlen;
2644 if (nextminlen + mp->m_dalign > args.minlen + 1)
2645 args.minalignslop =
2646 nextminlen + mp->m_dalign -
2647 args.minlen - 1;
2648 else
2649 args.minalignslop = 0;
2650 }
2651 } else {
2652 args.alignment = 1;
2653 args.minalignslop = 0;
2654 }
2655 args.minleft = ap->minleft;
2656 args.wasdel = ap->wasdel;
2657 args.isfl = 0;
2658 args.userdata = ap->userdata;
2659 if ((error = xfs_alloc_vextent(&args)))
2660 return error;
2661 if (tryagain && args.fsbno == NULLFSBLOCK) {
2662 /*
2663 * Exact allocation failed. Now try with alignment
2664 * turned on.
2665 */
2666 args.type = atype;
2667 args.fsbno = ap->rval;
2668 args.alignment = mp->m_dalign;
2669 args.minlen = nextminlen;
2670 args.minalignslop = 0;
2671 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 if ((error = xfs_alloc_vextent(&args)))
2673 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002674 }
2675 if (isaligned && args.fsbno == NULLFSBLOCK) {
2676 /*
2677 * allocation failed, so turn off alignment and
2678 * try again.
2679 */
2680 args.type = atype;
2681 args.fsbno = ap->rval;
2682 args.alignment = 0;
2683 if ((error = xfs_alloc_vextent(&args)))
2684 return error;
2685 }
2686 if (args.fsbno == NULLFSBLOCK && nullfb &&
2687 args.minlen > ap->minlen) {
2688 args.minlen = ap->minlen;
2689 args.type = XFS_ALLOCTYPE_START_BNO;
2690 args.fsbno = ap->rval;
2691 if ((error = xfs_alloc_vextent(&args)))
2692 return error;
2693 }
2694 if (args.fsbno == NULLFSBLOCK && nullfb) {
2695 args.fsbno = 0;
2696 args.type = XFS_ALLOCTYPE_FIRST_AG;
2697 args.total = ap->minlen;
2698 args.minleft = 0;
2699 if ((error = xfs_alloc_vextent(&args)))
2700 return error;
2701 ap->low = 1;
2702 }
2703 if (args.fsbno != NULLFSBLOCK) {
2704 ap->firstblock = ap->rval = args.fsbno;
2705 ASSERT(nullfb || fb_agno == args.agno ||
2706 (ap->low && fb_agno < args.agno));
2707 ap->alen = args.len;
2708 ap->ip->i_d.di_nblocks += args.len;
2709 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2710 if (ap->wasdel)
2711 ap->ip->i_delayed_blks -= args.len;
2712 /*
2713 * Adjust the disk quota also. This was reserved
2714 * earlier.
2715 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002716 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002717 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2718 XFS_TRANS_DQ_BCOUNT,
2719 (long) args.len);
2720 } else {
2721 ap->rval = NULLFSBLOCK;
2722 ap->alen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
2724 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002725}
2726
2727/*
2728 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2729 * It figures out where to ask the underlying allocator to put the new extent.
2730 */
2731STATIC int
2732xfs_bmap_alloc(
2733 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2734{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002735 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002736 return xfs_bmap_rtalloc(ap);
2737 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
2740/*
2741 * Transform a btree format file with only one leaf node, where the
2742 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002743 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 * give up the space for the btree root and pitch the leaf block.
2745 */
2746STATIC int /* error */
2747xfs_bmap_btree_to_extents(
2748 xfs_trans_t *tp, /* transaction pointer */
2749 xfs_inode_t *ip, /* incore inode pointer */
2750 xfs_btree_cur_t *cur, /* btree cursor */
2751 int *logflagsp, /* inode logging flags */
2752 int whichfork) /* data or attr fork */
2753{
2754 /* REFERENCED */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002755 struct xfs_btree_block *cblock;/* child btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 xfs_fsblock_t cbno; /* child block number */
2757 xfs_buf_t *cbp; /* child block's buffer */
2758 int error; /* error return value */
2759 xfs_ifork_t *ifp; /* inode fork data */
2760 xfs_mount_t *mp; /* mount point structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10002761 __be64 *pp; /* ptr to block address */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002762 struct xfs_btree_block *rblock;/* root btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Christoph Hellwig60197e82008-10-30 17:11:19 +11002764 mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 ifp = XFS_IFORK_PTR(ip, whichfork);
2766 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2767 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2768 rblock = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11002769 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2770 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
Christoph Hellwig60197e82008-10-30 17:11:19 +11002771 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2772 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10002773 cbno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 *logflagsp = 0;
2775#ifdef DEBUG
Christoph Hellwig576039c2006-09-28 10:58:06 +10002776 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 return error;
2778#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2780 XFS_BMAP_BTREE_REF)))
2781 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002782 cblock = XFS_BUF_TO_BLOCK(cbp);
2783 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return error;
2785 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2786 ip->i_d.di_nblocks--;
Christoph Hellwig7d095252009-06-08 15:33:32 +02002787 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 xfs_trans_binval(tp, cbp);
2789 if (cur->bc_bufs[0] == cbp)
2790 cur->bc_bufs[0] = NULL;
2791 xfs_iroot_realloc(ip, -1, whichfork);
2792 ASSERT(ifp->if_broot == NULL);
2793 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2794 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002795 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 return 0;
2797}
2798
2799/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002800 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 * after removing space (or undoing a delayed allocation).
2802 */
2803STATIC int /* error */
2804xfs_bmap_del_extent(
2805 xfs_inode_t *ip, /* incore inode pointer */
2806 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002807 xfs_extnum_t *idx, /* extent number to update/delete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 xfs_bmap_free_t *flist, /* list of extents to be freed */
2809 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002810 xfs_bmbt_irec_t *del, /* data to remove from extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 int *logflagsp, /* inode logging flags */
Christoph Hellwig54893272011-05-11 15:04:03 +00002812 int whichfork) /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
2814 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2815 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2816 xfs_fsblock_t del_endblock=0; /* first block past del */
2817 xfs_fileoff_t del_endoff; /* first offset past del */
2818 int delay; /* current block is delayed allocated */
2819 int do_fx; /* free extent at end of routine */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002820 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 int error; /* error return value */
2822 int flags; /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 xfs_bmbt_irec_t got; /* current extent entry */
2824 xfs_fileoff_t got_endoff; /* first offset past got */
2825 int i; /* temp state */
2826 xfs_ifork_t *ifp; /* inode fork pointer */
2827 xfs_mount_t *mp; /* mount structure */
2828 xfs_filblks_t nblks; /* quota/sb block count */
2829 xfs_bmbt_irec_t new; /* new record to be inserted */
2830 /* REFERENCED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 uint qfield; /* quota field to update */
2832 xfs_filblks_t temp; /* for indirect length calculations */
2833 xfs_filblks_t temp2; /* for indirect length calculations */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002834 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
2836 XFS_STATS_INC(xs_del_exlist);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002837
2838 if (whichfork == XFS_ATTR_FORK)
2839 state |= BMAP_ATTRFORK;
2840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 mp = ip->i_mount;
2842 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002843 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002844 (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 ASSERT(del->br_blockcount > 0);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002846 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 xfs_bmbt_get_all(ep, &got);
2848 ASSERT(got.br_startoff <= del->br_startoff);
2849 del_endoff = del->br_startoff + del->br_blockcount;
2850 got_endoff = got.br_startoff + got.br_blockcount;
2851 ASSERT(got_endoff >= del_endoff);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002852 delay = isnullstartblock(got.br_startblock);
2853 ASSERT(isnullstartblock(del->br_startblock) == delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 flags = 0;
2855 qfield = 0;
2856 error = 0;
2857 /*
2858 * If deleting a real allocation, must free up the disk space.
2859 */
2860 if (!delay) {
2861 flags = XFS_ILOG_CORE;
2862 /*
2863 * Realtime allocation. Free it and record di_nblocks update.
2864 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002865 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 xfs_fsblock_t bno;
2867 xfs_filblks_t len;
2868
2869 ASSERT(do_mod(del->br_blockcount,
2870 mp->m_sb.sb_rextsize) == 0);
2871 ASSERT(do_mod(del->br_startblock,
2872 mp->m_sb.sb_rextsize) == 0);
2873 bno = del->br_startblock;
2874 len = del->br_blockcount;
2875 do_div(bno, mp->m_sb.sb_rextsize);
2876 do_div(len, mp->m_sb.sb_rextsize);
Christoph Hellwigf3ca8732011-07-08 14:34:47 +02002877 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
2878 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 goto done;
2880 do_fx = 0;
2881 nblks = len * mp->m_sb.sb_rextsize;
2882 qfield = XFS_TRANS_DQ_RTBCOUNT;
2883 }
2884 /*
2885 * Ordinary allocation.
2886 */
2887 else {
2888 do_fx = 1;
2889 nblks = del->br_blockcount;
2890 qfield = XFS_TRANS_DQ_BCOUNT;
2891 }
2892 /*
2893 * Set up del_endblock and cur for later.
2894 */
2895 del_endblock = del->br_startblock + del->br_blockcount;
2896 if (cur) {
2897 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2898 got.br_startblock, got.br_blockcount,
2899 &i)))
2900 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002901 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 }
2903 da_old = da_new = 0;
2904 } else {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002905 da_old = startblockval(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 da_new = 0;
2907 nblks = 0;
2908 do_fx = 0;
2909 }
2910 /*
2911 * Set flag value to use in switch statement.
2912 * Left-contig is 2, right-contig is 1.
2913 */
2914 switch (((got.br_startoff == del->br_startoff) << 1) |
2915 (got_endoff == del_endoff)) {
2916 case 3:
2917 /*
2918 * Matches the whole extent. Delete the entry.
2919 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002920 xfs_iext_remove(ip, *idx, 1,
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002921 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
Christoph Hellwig233eebb2011-05-11 15:04:05 +00002922 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 if (delay)
2924 break;
Christoph Hellwig233eebb2011-05-11 15:04:05 +00002925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 XFS_IFORK_NEXT_SET(ip, whichfork,
2927 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2928 flags |= XFS_ILOG_CORE;
2929 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002930 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 break;
2932 }
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11002933 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002935 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 break;
2937
2938 case 2:
2939 /*
2940 * Deleting the first part of the extent.
2941 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002942 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 xfs_bmbt_set_startoff(ep, del_endoff);
2944 temp = got.br_blockcount - del->br_blockcount;
2945 xfs_bmbt_set_blockcount(ep, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 if (delay) {
2947 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2948 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002949 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002950 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 da_new = temp;
2952 break;
2953 }
2954 xfs_bmbt_set_startblock(ep, del_endblock);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002955 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002957 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 break;
2959 }
2960 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2961 got.br_blockcount - del->br_blockcount,
2962 got.br_state)))
2963 goto done;
2964 break;
2965
2966 case 1:
2967 /*
2968 * Deleting the last part of the extent.
2969 */
2970 temp = got.br_blockcount - del->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002971 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 xfs_bmbt_set_blockcount(ep, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 if (delay) {
2974 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2975 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002976 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002977 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 da_new = temp;
2979 break;
2980 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002981 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002983 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 break;
2985 }
2986 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2987 got.br_startblock,
2988 got.br_blockcount - del->br_blockcount,
2989 got.br_state)))
2990 goto done;
2991 break;
2992
2993 case 0:
2994 /*
2995 * Deleting the middle of the extent.
2996 */
2997 temp = del->br_startoff - got.br_startoff;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002998 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 xfs_bmbt_set_blockcount(ep, temp);
3000 new.br_startoff = del_endoff;
3001 temp2 = got_endoff - del_endoff;
3002 new.br_blockcount = temp2;
3003 new.br_state = got.br_state;
3004 if (!delay) {
3005 new.br_startblock = del_endblock;
3006 flags |= XFS_ILOG_CORE;
3007 if (cur) {
3008 if ((error = xfs_bmbt_update(cur,
3009 got.br_startoff,
3010 got.br_startblock, temp,
3011 got.br_state)))
3012 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11003013 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 goto done;
3015 cur->bc_rec.b = new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003016 error = xfs_btree_insert(cur, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 if (error && error != ENOSPC)
3018 goto done;
3019 /*
3020 * If get no-space back from btree insert,
3021 * it tried a split, and we have a zero
3022 * block reservation.
3023 * Fix up our state and return the error.
3024 */
3025 if (error == ENOSPC) {
3026 /*
3027 * Reset the cursor, don't trust
3028 * it after any insert operation.
3029 */
3030 if ((error = xfs_bmbt_lookup_eq(cur,
3031 got.br_startoff,
3032 got.br_startblock,
3033 temp, &i)))
3034 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003035 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 /*
3037 * Update the btree record back
3038 * to the original value.
3039 */
3040 if ((error = xfs_bmbt_update(cur,
3041 got.br_startoff,
3042 got.br_startblock,
3043 got.br_blockcount,
3044 got.br_state)))
3045 goto done;
3046 /*
3047 * Reset the extent record back
3048 * to the original value.
3049 */
3050 xfs_bmbt_set_blockcount(ep,
3051 got.br_blockcount);
3052 flags = 0;
3053 error = XFS_ERROR(ENOSPC);
3054 goto done;
3055 }
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003056 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 } else
Eric Sandeen9d87c312009-01-14 23:22:07 -06003058 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 XFS_IFORK_NEXT_SET(ip, whichfork,
3060 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3061 } else {
3062 ASSERT(whichfork == XFS_DATA_FORK);
3063 temp = xfs_bmap_worst_indlen(ip, temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003064 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003066 new.br_startblock = nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 da_new = temp + temp2;
3068 while (da_new > da_old) {
3069 if (temp) {
3070 temp--;
3071 da_new--;
3072 xfs_bmbt_set_startblock(ep,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003073 nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 }
3075 if (da_new == da_old)
3076 break;
3077 if (temp2) {
3078 temp2--;
3079 da_new--;
3080 new.br_startblock =
Eric Sandeen9d87c312009-01-14 23:22:07 -06003081 nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
3083 }
3084 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00003085 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
3086 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
3087 ++*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 break;
3089 }
3090 /*
3091 * If we need to, add to list of extents to delete.
3092 */
3093 if (do_fx)
3094 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3095 mp);
3096 /*
3097 * Adjust inode # blocks in the file.
3098 */
3099 if (nblks)
3100 ip->i_d.di_nblocks -= nblks;
3101 /*
3102 * Adjust quota data.
3103 */
3104 if (qfield)
Christoph Hellwig7d095252009-06-08 15:33:32 +02003105 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107 /*
3108 * Account for change in delayed indirect blocks.
3109 * Nothing to do for disk quota accounting here.
3110 */
3111 ASSERT(da_old >= da_new);
Christoph Hellwig96540c72010-09-30 02:25:55 +00003112 if (da_old > da_new) {
3113 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00003114 (int64_t)(da_old - da_new), 0);
Christoph Hellwig96540c72010-09-30 02:25:55 +00003115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116done:
3117 *logflagsp = flags;
3118 return error;
3119}
3120
3121/*
3122 * Remove the entry "free" from the free item list. Prev points to the
3123 * previous entry, unless "free" is the head of the list.
3124 */
3125STATIC void
3126xfs_bmap_del_free(
3127 xfs_bmap_free_t *flist, /* free item list header */
3128 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3129 xfs_bmap_free_item_t *free) /* list item to be freed */
3130{
3131 if (prev)
3132 prev->xbfi_next = free->xbfi_next;
3133 else
3134 flist->xbf_first = free->xbfi_next;
3135 flist->xbf_count--;
3136 kmem_zone_free(xfs_bmap_free_item_zone, free);
3137}
3138
3139/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 * Convert an extents-format file into a btree-format file.
3141 * The new file will have a root block (in the inode) and a single child block.
3142 */
3143STATIC int /* error */
3144xfs_bmap_extents_to_btree(
3145 xfs_trans_t *tp, /* transaction pointer */
3146 xfs_inode_t *ip, /* incore inode pointer */
3147 xfs_fsblock_t *firstblock, /* first-block-allocated */
3148 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3149 xfs_btree_cur_t **curp, /* cursor returned to caller */
3150 int wasdel, /* converting a delayed alloc */
3151 int *logflagsp, /* inode logging flags */
3152 int whichfork) /* data or attr fork */
3153{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003154 struct xfs_btree_block *ablock; /* allocated (child) bt block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 xfs_buf_t *abp; /* buffer for ablock */
3156 xfs_alloc_arg_t args; /* allocation arguments */
3157 xfs_bmbt_rec_t *arp; /* child record pointer */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003158 struct xfs_btree_block *block; /* btree root block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 xfs_btree_cur_t *cur; /* bmap btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003160 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003162 xfs_extnum_t i, cnt; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 xfs_ifork_t *ifp; /* inode fork pointer */
3164 xfs_bmbt_key_t *kp; /* root block key pointer */
3165 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003166 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3168
3169 ifp = XFS_IFORK_PTR(ip, whichfork);
3170 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3171 ASSERT(ifp->if_ext_max ==
3172 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3173 /*
3174 * Make space in the inode incore.
3175 */
3176 xfs_iroot_realloc(ip, 1, whichfork);
3177 ifp->if_flags |= XFS_IFBROOT;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003178
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 /*
3180 * Fill in the root.
3181 */
3182 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003183 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3184 block->bb_level = cpu_to_be16(1);
3185 block->bb_numrecs = cpu_to_be16(1);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003186 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3187 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 /*
3190 * Need a cursor. Can't allocate until bb_level is filled in.
3191 */
3192 mp = ip->i_mount;
Christoph Hellwig561f7d12008-10-30 16:53:59 +11003193 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 cur->bc_private.b.firstblock = *firstblock;
3195 cur->bc_private.b.flist = flist;
3196 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3197 /*
3198 * Convert to a btree with two levels, one record in root.
3199 */
3200 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3201 args.tp = tp;
3202 args.mp = mp;
Yingping Lud210a282006-06-09 14:55:18 +10003203 args.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 if (*firstblock == NULLFSBLOCK) {
3205 args.type = XFS_ALLOCTYPE_START_BNO;
3206 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3207 } else if (flist->xbf_low) {
3208 args.type = XFS_ALLOCTYPE_START_BNO;
3209 args.fsbno = *firstblock;
3210 } else {
3211 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3212 args.fsbno = *firstblock;
3213 }
3214 args.minlen = args.maxlen = args.prod = 1;
3215 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3216 args.minalignslop = 0;
3217 args.wasdel = wasdel;
3218 *logflagsp = 0;
3219 if ((error = xfs_alloc_vextent(&args))) {
3220 xfs_iroot_realloc(ip, -1, whichfork);
3221 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3222 return error;
3223 }
3224 /*
3225 * Allocation can't fail, the space was reserved.
3226 */
3227 ASSERT(args.fsbno != NULLFSBLOCK);
3228 ASSERT(*firstblock == NULLFSBLOCK ||
3229 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3230 (flist->xbf_low &&
3231 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3232 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3233 cur->bc_private.b.allocated++;
3234 ip->i_d.di_nblocks++;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003235 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3237 /*
3238 * Fill in the child block.
3239 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003240 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003241 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 ablock->bb_level = 0;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003243 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3244 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003245 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003247 for (cnt = i = 0; i < nextents; i++) {
3248 ep = xfs_iext_get_ext(ifp, i);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003249 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10003250 arp->l0 = cpu_to_be64(ep->l0);
3251 arp->l1 = cpu_to_be64(ep->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 arp++; cnt++;
3253 }
3254 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11003255 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003256 xfs_btree_set_numrecs(ablock, cnt);
3257
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 /*
3259 * Fill in the root key and pointer.
3260 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11003261 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3262 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Christoph Hellwig8801bb92006-09-28 10:58:17 +10003263 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
Christoph Hellwig136341b2008-10-30 17:11:40 +11003264 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3265 be16_to_cpu(block->bb_level)));
Christoph Hellwig576039c2006-09-28 10:58:06 +10003266 *pp = cpu_to_be64(args.fsbno);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 /*
3269 * Do all this logging at the end so that
3270 * the root is at the right level.
3271 */
Christoph Hellwigfd6bcc52008-10-30 16:58:21 +11003272 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3273 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 ASSERT(*curp == NULL);
3275 *curp = cur;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003276 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 return 0;
3278}
3279
3280/*
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003281 * Calculate the default attribute fork offset for newly created inodes.
3282 */
3283uint
3284xfs_default_attroffset(
3285 struct xfs_inode *ip)
3286{
3287 struct xfs_mount *mp = ip->i_mount;
3288 uint offset;
3289
3290 if (mp->m_sb.sb_inodesize == 256) {
3291 offset = XFS_LITINO(mp) -
3292 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3293 } else {
3294 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3295 }
3296
3297 ASSERT(offset < XFS_LITINO(mp));
3298 return offset;
3299}
3300
3301/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11003302 * Helper routine to reset inode di_forkoff field when switching
3303 * attribute fork from local to extent format - we reset it where
3304 * possible to make space available for inline data fork extents.
3305 */
3306STATIC void
3307xfs_bmap_forkoff_reset(
3308 xfs_mount_t *mp,
3309 xfs_inode_t *ip,
3310 int whichfork)
3311{
3312 if (whichfork == XFS_ATTR_FORK &&
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003313 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3314 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3315 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3316 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3317
3318 if (dfl_forkoff > ip->i_d.di_forkoff) {
3319 ip->i_d.di_forkoff = dfl_forkoff;
3320 ip->i_df.if_ext_max =
3321 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3322 ip->i_afp->if_ext_max =
3323 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3324 }
Nathan Scottd8cc8902005-11-02 10:34:53 +11003325 }
3326}
3327
3328/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 * Convert a local file to an extents file.
3330 * This code is out of bounds for data forks of regular files,
3331 * since the file data needs to get logged so things will stay consistent.
3332 * (The bmap-level manipulations are ok, though).
3333 */
3334STATIC int /* error */
3335xfs_bmap_local_to_extents(
3336 xfs_trans_t *tp, /* transaction pointer */
3337 xfs_inode_t *ip, /* incore inode pointer */
3338 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3339 xfs_extlen_t total, /* total blocks needed by transaction */
3340 int *logflagsp, /* inode logging flags */
3341 int whichfork) /* data or attr fork */
3342{
3343 int error; /* error return value */
3344 int flags; /* logging flags returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 xfs_ifork_t *ifp; /* inode fork pointer */
3346
3347 /*
3348 * We don't want to deal with the case of keeping inode data inline yet.
3349 * So sending the data fork of a regular inode is invalid.
3350 */
3351 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3352 whichfork == XFS_DATA_FORK));
3353 ifp = XFS_IFORK_PTR(ip, whichfork);
3354 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3355 flags = 0;
3356 error = 0;
3357 if (ifp->if_bytes) {
3358 xfs_alloc_arg_t args; /* allocation arguments */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003359 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003360 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
3362 args.tp = tp;
3363 args.mp = ip->i_mount;
Yingping Lud210a282006-06-09 14:55:18 +10003364 args.firstblock = *firstblock;
Mandy Kirkconnellf020b672006-03-14 14:07:24 +11003365 ASSERT((ifp->if_flags &
3366 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 /*
3368 * Allocate a block. We know we need only one, since the
3369 * file currently fits in an inode.
3370 */
3371 if (*firstblock == NULLFSBLOCK) {
3372 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3373 args.type = XFS_ALLOCTYPE_START_BNO;
3374 } else {
3375 args.fsbno = *firstblock;
3376 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3377 }
3378 args.total = total;
3379 args.mod = args.minleft = args.alignment = args.wasdel =
3380 args.isfl = args.minalignslop = 0;
3381 args.minlen = args.maxlen = args.prod = 1;
3382 if ((error = xfs_alloc_vextent(&args)))
3383 goto done;
3384 /*
3385 * Can't fail, the space was reserved.
3386 */
3387 ASSERT(args.fsbno != NULLFSBLOCK);
3388 ASSERT(args.len == 1);
3389 *firstblock = args.fsbno;
3390 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3391 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3392 ifp->if_bytes);
3393 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003394 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003396 xfs_iext_add(ifp, 0, 1);
3397 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003399 trace_xfs_bmap_post_update(ip, 0,
3400 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3401 _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3403 ip->i_d.di_nblocks = 1;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003404 xfs_trans_mod_dquot_byino(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 XFS_TRANS_DQ_BCOUNT, 1L);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003406 flags |= xfs_ilog_fext(whichfork);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003407 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003409 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 ifp->if_flags &= ~XFS_IFINLINE;
3412 ifp->if_flags |= XFS_IFEXTENTS;
3413 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3414 flags |= XFS_ILOG_CORE;
3415done:
3416 *logflagsp = flags;
3417 return error;
3418}
3419
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003421 * Search the extent records for the entry containing block bno.
3422 * If bno lies in a hole, point to the next entry. If bno lies
3423 * past eof, *eofp will be set, and *prevp will contain the last
3424 * entry (null if none). Else, *lastxp will be set to the index
3425 * of the found entry; *gotp will contain the entry.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003426 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003427STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003428xfs_bmap_search_multi_extents(
3429 xfs_ifork_t *ifp, /* inode fork pointer */
3430 xfs_fileoff_t bno, /* block number searched for */
3431 int *eofp, /* out: end of file found */
3432 xfs_extnum_t *lastxp, /* out: last extent index */
3433 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3434 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3435{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003436 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003437 xfs_extnum_t lastx; /* last extent index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003438
3439 /*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003440 * Initialize the extent entry structure to catch access to
3441 * uninitialized br_startblock field.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003442 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003443 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3444 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3445 gotp->br_state = XFS_EXT_INVALID;
3446#if XFS_BIG_BLKNOS
3447 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3448#else
3449 gotp->br_startblock = 0xffffa5a5;
3450#endif
3451 prevp->br_startoff = NULLFILEOFF;
3452
3453 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3454 if (lastx > 0) {
3455 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3456 }
3457 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3458 xfs_bmbt_get_all(ep, gotp);
3459 *eofp = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003460 } else {
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003461 if (lastx > 0) {
3462 *gotp = *prevp;
3463 }
3464 *eofp = 1;
3465 ep = NULL;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003466 }
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003467 *lastxp = lastx;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003468 return ep;
3469}
3470
3471/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 * Search the extents list for the inode, for the extent containing bno.
3473 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3474 * *eofp will be set, and *prevp will contain the last entry (null if none).
3475 * Else, *lastxp will be set to the index of the found
3476 * entry; *gotp will contain the entry.
3477 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003478STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479xfs_bmap_search_extents(
3480 xfs_inode_t *ip, /* incore inode pointer */
3481 xfs_fileoff_t bno, /* block number searched for */
Nathan Scott572d95f2006-09-28 11:03:20 +10003482 int fork, /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 int *eofp, /* out: end of file found */
3484 xfs_extnum_t *lastxp, /* out: last extent index */
3485 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3486 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3487{
3488 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003489 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
3491 XFS_STATS_INC(xs_look_exlist);
Nathan Scott572d95f2006-09-28 11:03:20 +10003492 ifp = XFS_IFORK_PTR(ip, fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003494 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3495
Nathan Scott572d95f2006-09-28 11:03:20 +10003496 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3497 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003498 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
Nathan Scott572d95f2006-09-28 11:03:20 +10003499 "Access to block zero in inode %llu "
3500 "start_block: %llx start_off: %llx "
3501 "blkcnt: %llx extent-state: %x lastx: %x\n",
3502 (unsigned long long)ip->i_ino,
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003503 (unsigned long long)gotp->br_startblock,
3504 (unsigned long long)gotp->br_startoff,
3505 (unsigned long long)gotp->br_blockcount,
Nathan Scott572d95f2006-09-28 11:03:20 +10003506 gotp->br_state, *lastxp);
3507 *lastxp = NULLEXTNUM;
3508 *eofp = 1;
3509 return NULL;
3510 }
3511 return ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514/*
3515 * Compute the worst-case number of indirect blocks that will be used
3516 * for ip's delayed extent of length "len".
3517 */
3518STATIC xfs_filblks_t
3519xfs_bmap_worst_indlen(
3520 xfs_inode_t *ip, /* incore inode pointer */
3521 xfs_filblks_t len) /* delayed extent length */
3522{
3523 int level; /* btree level number */
3524 int maxrecs; /* maximum record count at this level */
3525 xfs_mount_t *mp; /* mount structure */
3526 xfs_filblks_t rval; /* return value */
3527
3528 mp = ip->i_mount;
3529 maxrecs = mp->m_bmap_dmxr[0];
3530 for (level = 0, rval = 0;
3531 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3532 level++) {
3533 len += maxrecs - 1;
3534 do_div(len, maxrecs);
3535 rval += len;
3536 if (len == 1)
3537 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3538 level - 1;
3539 if (level == 0)
3540 maxrecs = mp->m_bmap_dmxr[1];
3541 }
3542 return rval;
3543}
3544
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545/*
3546 * Convert inode from non-attributed to attributed.
3547 * Must not be in a transaction, ip must not be locked.
3548 */
3549int /* error code */
3550xfs_bmap_add_attrfork(
3551 xfs_inode_t *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003552 int size, /* space new attribute needs */
3553 int rsvd) /* xact may use reserved blks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003556 xfs_bmap_free_t flist; /* freed extent records */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 xfs_trans_t *tp; /* transaction pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003559 int blks; /* space reservation */
3560 int version = 1; /* superblock attr version */
3561 int committed; /* xaction was committed */
3562 int logflags; /* logging flags */
3563 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Nathan Scottd8cc8902005-11-02 10:34:53 +11003565 ASSERT(XFS_IFORK_Q(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 ASSERT(ip->i_df.if_ext_max ==
3567 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
Nathan Scottd8cc8902005-11-02 10:34:53 +11003568
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 mp = ip->i_mount;
3570 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3571 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3572 blks = XFS_ADDAFORK_SPACE_RES(mp);
3573 if (rsvd)
3574 tp->t_flags |= XFS_TRANS_RESERVE;
3575 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3576 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3577 goto error0;
3578 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02003579 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3581 XFS_QMOPT_RES_REGBLKS);
3582 if (error) {
3583 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3584 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3585 return error;
3586 }
3587 if (XFS_IFORK_Q(ip))
3588 goto error1;
3589 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3590 /*
3591 * For inodes coming from pre-6.2 filesystems.
3592 */
3593 ASSERT(ip->i_d.di_aformat == 0);
3594 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3595 }
3596 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwig898621d2010-06-24 11:36:58 +10003597
3598 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig898621d2010-06-24 11:36:58 +10003600
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 switch (ip->i_d.di_format) {
3602 case XFS_DINODE_FMT_DEV:
3603 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3604 break;
3605 case XFS_DINODE_FMT_UUID:
3606 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3607 break;
3608 case XFS_DINODE_FMT_LOCAL:
3609 case XFS_DINODE_FMT_EXTENTS:
3610 case XFS_DINODE_FMT_BTREE:
Nathan Scottd8cc8902005-11-02 10:34:53 +11003611 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3612 if (!ip->i_d.di_forkoff)
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003613 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
Nathan Scott13059ff2006-01-11 15:32:01 +11003614 else if (mp->m_flags & XFS_MOUNT_ATTR2)
Nathan Scottd8cc8902005-11-02 10:34:53 +11003615 version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 break;
3617 default:
3618 ASSERT(0);
3619 error = XFS_ERROR(EINVAL);
3620 goto error1;
3621 }
3622 ip->i_df.if_ext_max =
3623 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3624 ASSERT(ip->i_afp == NULL);
3625 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3626 ip->i_afp->if_ext_max =
3627 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3628 ip->i_afp->if_flags = XFS_IFEXTENTS;
3629 logflags = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003630 xfs_bmap_init(&flist, &firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 switch (ip->i_d.di_format) {
3632 case XFS_DINODE_FMT_LOCAL:
3633 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3634 &logflags);
3635 break;
3636 case XFS_DINODE_FMT_EXTENTS:
3637 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3638 &flist, &logflags);
3639 break;
3640 case XFS_DINODE_FMT_BTREE:
3641 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3642 &logflags);
3643 break;
3644 default:
3645 error = 0;
3646 break;
3647 }
3648 if (logflags)
3649 xfs_trans_log_inode(tp, ip, logflags);
3650 if (error)
3651 goto error2;
Eric Sandeen62118702008-03-06 13:44:28 +11003652 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3653 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +11003654 __int64_t sbfields = 0;
3655
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003656 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +11003657 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3658 xfs_sb_version_addattr(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003659 sbfields |= XFS_SB_VERSIONNUM;
Nathan Scottd8cc8902005-11-02 10:34:53 +11003660 }
Eric Sandeen62118702008-03-06 13:44:28 +11003661 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3662 xfs_sb_version_addattr2(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003663 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003664 }
Nathan Scottda087ba2005-11-02 15:00:20 +11003665 if (sbfields) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003666 spin_unlock(&mp->m_sb_lock);
Nathan Scottda087ba2005-11-02 15:00:20 +11003667 xfs_mod_sb(tp, sbfields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003669 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 }
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003671 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 goto error2;
Dave Chinner713bf882010-03-08 11:26:23 +11003673 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 ASSERT(ip->i_df.if_ext_max ==
3675 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3676 return error;
3677error2:
3678 xfs_bmap_cancel(&flist);
3679error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3681error0:
3682 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3683 ASSERT(ip->i_df.if_ext_max ==
3684 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3685 return error;
3686}
3687
3688/*
3689 * Add the extent to the list of extents to be free at transaction end.
3690 * The list is maintained sorted (by block number).
3691 */
3692/* ARGSUSED */
3693void
3694xfs_bmap_add_free(
3695 xfs_fsblock_t bno, /* fs block number of extent */
3696 xfs_filblks_t len, /* length of extent */
3697 xfs_bmap_free_t *flist, /* list of extents */
3698 xfs_mount_t *mp) /* mount point structure */
3699{
3700 xfs_bmap_free_item_t *cur; /* current (next) element */
3701 xfs_bmap_free_item_t *new; /* new element */
3702 xfs_bmap_free_item_t *prev; /* previous element */
3703#ifdef DEBUG
3704 xfs_agnumber_t agno;
3705 xfs_agblock_t agbno;
3706
3707 ASSERT(bno != NULLFSBLOCK);
3708 ASSERT(len > 0);
3709 ASSERT(len <= MAXEXTLEN);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003710 ASSERT(!isnullstartblock(bno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 agno = XFS_FSB_TO_AGNO(mp, bno);
3712 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3713 ASSERT(agno < mp->m_sb.sb_agcount);
3714 ASSERT(agbno < mp->m_sb.sb_agblocks);
3715 ASSERT(len < mp->m_sb.sb_agblocks);
3716 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3717#endif
3718 ASSERT(xfs_bmap_free_item_zone != NULL);
3719 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3720 new->xbfi_startblock = bno;
3721 new->xbfi_blockcount = (xfs_extlen_t)len;
3722 for (prev = NULL, cur = flist->xbf_first;
3723 cur != NULL;
3724 prev = cur, cur = cur->xbfi_next) {
3725 if (cur->xbfi_startblock >= bno)
3726 break;
3727 }
3728 if (prev)
3729 prev->xbfi_next = new;
3730 else
3731 flist->xbf_first = new;
3732 new->xbfi_next = cur;
3733 flist->xbf_count++;
3734}
3735
3736/*
3737 * Compute and fill in the value of the maximum depth of a bmap btree
3738 * in this filesystem. Done once, during mount.
3739 */
3740void
3741xfs_bmap_compute_maxlevels(
3742 xfs_mount_t *mp, /* file system mount structure */
3743 int whichfork) /* data or attr fork */
3744{
3745 int level; /* btree level */
3746 uint maxblocks; /* max blocks at this level */
3747 uint maxleafents; /* max leaf entries possible */
3748 int maxrootrecs; /* max records in root block */
3749 int minleafrecs; /* min records in leaf block */
3750 int minnoderecs; /* min records in node block */
3751 int sz; /* root block size */
3752
3753 /*
3754 * The maximum number of extents in a file, hence the maximum
3755 * number of leaf entries, is controlled by the type of di_nextents
3756 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3757 * (a signed 16-bit number, xfs_aextnum_t).
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003758 *
3759 * Note that we can no longer assume that if we are in ATTR1 that
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003760 * the fork offset of all the inodes will be
3761 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3762 * with ATTR2 and then mounted back with ATTR1, keeping the
3763 * di_forkoff's fixed but probably at various positions. Therefore,
3764 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3765 * of a minimum size available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003767 if (whichfork == XFS_DATA_FORK) {
3768 maxleafents = MAXEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003769 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003770 } else {
3771 maxleafents = MAXAEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003772 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003773 }
Christoph Hellwig60197e82008-10-30 17:11:19 +11003774 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 minleafrecs = mp->m_bmap_dmnr[0];
3776 minnoderecs = mp->m_bmap_dmnr[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3778 for (level = 1; maxblocks > 1; level++) {
3779 if (maxblocks <= maxrootrecs)
3780 maxblocks = 1;
3781 else
3782 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3783 }
3784 mp->m_bm_maxlevels[whichfork] = level;
3785}
3786
3787/*
3788 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3789 * caller. Frees all the extents that need freeing, which must be done
3790 * last due to locking considerations. We never free any extents in
3791 * the first transaction. This is to allow the caller to make the first
3792 * transaction a synchronous one so that the pointers to the data being
3793 * broken in this transaction will be permanent before the data is actually
3794 * freed. This is necessary to prevent blocks from being reallocated
3795 * and written to before the free and reallocation are actually permanent.
3796 * We do not just make the first transaction synchronous here, because
3797 * there are more efficient ways to gain the same protection in some cases
3798 * (see the file truncation code).
3799 *
3800 * Return 1 if the given transaction was committed and a new one
3801 * started, and 0 otherwise in the committed parameter.
3802 */
3803/*ARGSUSED*/
3804int /* error */
3805xfs_bmap_finish(
3806 xfs_trans_t **tp, /* transaction pointer addr */
3807 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 int *committed) /* xact committed or not */
3809{
3810 xfs_efd_log_item_t *efd; /* extent free data */
3811 xfs_efi_log_item_t *efi; /* extent free intention */
3812 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003813 xfs_bmap_free_item_t *free; /* free extent item */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 unsigned int logres; /* new log reservation */
3815 unsigned int logcount; /* new log count */
3816 xfs_mount_t *mp; /* filesystem mount structure */
3817 xfs_bmap_free_item_t *next; /* next item on free list */
3818 xfs_trans_t *ntp; /* new transaction pointer */
3819
3820 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3821 if (flist->xbf_count == 0) {
3822 *committed = 0;
3823 return 0;
3824 }
3825 ntp = *tp;
3826 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3827 for (free = flist->xbf_first; free; free = free->xbfi_next)
3828 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3829 free->xbfi_blockcount);
3830 logres = ntp->t_log_res;
3831 logcount = ntp->t_log_count;
3832 ntp = xfs_trans_dup(*tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003833 error = xfs_trans_commit(*tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 *tp = ntp;
3835 *committed = 1;
3836 /*
3837 * We have a new transaction, so we should return committed=1,
3838 * even though we're returning an error.
3839 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003840 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 return error;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003842
3843 /*
3844 * transaction commit worked ok so we can drop the extra ticket
3845 * reference that we gained in xfs_trans_dup()
3846 */
3847 xfs_log_ticket_put(ntp->t_ticket);
3848
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3850 logcount)))
3851 return error;
3852 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3853 for (free = flist->xbf_first; free != NULL; free = next) {
3854 next = free->xbfi_next;
3855 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3856 free->xbfi_blockcount))) {
3857 /*
3858 * The bmap free list will be cleaned up at a
3859 * higher level. The EFI will be canceled when
3860 * this transaction is aborted.
3861 * Need to force shutdown here to make sure it
3862 * happens, since this transaction may not be
3863 * dirty yet.
3864 */
3865 mp = ntp->t_mountp;
3866 if (!XFS_FORCED_SHUTDOWN(mp))
3867 xfs_force_shutdown(mp,
3868 (error == EFSCORRUPTED) ?
Nathan Scott7d04a332006-06-09 14:58:38 +10003869 SHUTDOWN_CORRUPT_INCORE :
3870 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 return error;
3872 }
3873 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3874 free->xbfi_blockcount);
3875 xfs_bmap_del_free(flist, NULL, free);
3876 }
3877 return 0;
3878}
3879
3880/*
3881 * Free up any items left in the list.
3882 */
3883void
3884xfs_bmap_cancel(
3885 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3886{
3887 xfs_bmap_free_item_t *free; /* free list item */
3888 xfs_bmap_free_item_t *next;
3889
3890 if (flist->xbf_count == 0)
3891 return;
3892 ASSERT(flist->xbf_first != NULL);
3893 for (free = flist->xbf_first; free; free = next) {
3894 next = free->xbfi_next;
3895 xfs_bmap_del_free(flist, NULL, free);
3896 }
3897 ASSERT(flist->xbf_count == 0);
3898}
3899
3900/*
3901 * Returns the file-relative block number of the first unused block(s)
3902 * in the file with at least "len" logically contiguous blocks free.
3903 * This is the lowest-address hole if the file has holes, else the first block
3904 * past the end of file.
3905 * Return 0 if the file is currently local (in-inode).
3906 */
3907int /* error */
3908xfs_bmap_first_unused(
3909 xfs_trans_t *tp, /* transaction pointer */
3910 xfs_inode_t *ip, /* incore inode */
3911 xfs_extlen_t len, /* size of hole to find */
3912 xfs_fileoff_t *first_unused, /* unused block */
3913 int whichfork) /* data or attr fork */
3914{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003916 int idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 xfs_ifork_t *ifp; /* inode fork pointer */
3918 xfs_fileoff_t lastaddr; /* last block number seen */
3919 xfs_fileoff_t lowest; /* lowest useful block */
3920 xfs_fileoff_t max; /* starting useful block */
3921 xfs_fileoff_t off; /* offset for this block */
3922 xfs_extnum_t nextents; /* number of extent entries */
3923
3924 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3925 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3926 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3927 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3928 *first_unused = 0;
3929 return 0;
3930 }
3931 ifp = XFS_IFORK_PTR(ip, whichfork);
3932 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3933 (error = xfs_iread_extents(tp, ip, whichfork)))
3934 return error;
3935 lowest = *first_unused;
3936 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003937 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003938 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 off = xfs_bmbt_get_startoff(ep);
3940 /*
3941 * See if the hole before this extent will work.
3942 */
3943 if (off >= lowest + len && off - max >= len) {
3944 *first_unused = max;
3945 return 0;
3946 }
3947 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3948 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3949 }
3950 *first_unused = max;
3951 return 0;
3952}
3953
3954/*
3955 * Returns the file-relative block number of the last block + 1 before
3956 * last_block (input value) in the file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003957 * This is not based on i_size, it is based on the extent records.
3958 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 */
3960int /* error */
3961xfs_bmap_last_before(
3962 xfs_trans_t *tp, /* transaction pointer */
3963 xfs_inode_t *ip, /* incore inode */
3964 xfs_fileoff_t *last_block, /* last block */
3965 int whichfork) /* data or attr fork */
3966{
3967 xfs_fileoff_t bno; /* input file offset */
3968 int eof; /* hit end of file */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003969 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 int error; /* error return value */
3971 xfs_bmbt_irec_t got; /* current extent value */
3972 xfs_ifork_t *ifp; /* inode fork pointer */
3973 xfs_extnum_t lastx; /* last extent used */
3974 xfs_bmbt_irec_t prev; /* previous extent value */
3975
3976 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3977 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3978 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3979 return XFS_ERROR(EIO);
3980 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3981 *last_block = 0;
3982 return 0;
3983 }
3984 ifp = XFS_IFORK_PTR(ip, whichfork);
3985 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3986 (error = xfs_iread_extents(tp, ip, whichfork)))
3987 return error;
3988 bno = *last_block - 1;
3989 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3990 &prev);
3991 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3992 if (prev.br_startoff == NULLFILEOFF)
3993 *last_block = 0;
3994 else
3995 *last_block = prev.br_startoff + prev.br_blockcount;
3996 }
3997 /*
3998 * Otherwise *last_block is already the right answer.
3999 */
4000 return 0;
4001}
4002
4003/*
4004 * Returns the file-relative block number of the first block past eof in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004005 * the file. This is not based on i_size, it is based on the extent records.
4006 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 */
4008int /* error */
4009xfs_bmap_last_offset(
4010 xfs_trans_t *tp, /* transaction pointer */
4011 xfs_inode_t *ip, /* incore inode */
4012 xfs_fileoff_t *last_block, /* last block */
4013 int whichfork) /* data or attr fork */
4014{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004015 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 int error; /* error return value */
4017 xfs_ifork_t *ifp; /* inode fork pointer */
4018 xfs_extnum_t nextents; /* number of extent entries */
4019
4020 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4021 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4022 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4023 return XFS_ERROR(EIO);
4024 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4025 *last_block = 0;
4026 return 0;
4027 }
4028 ifp = XFS_IFORK_PTR(ip, whichfork);
4029 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4030 (error = xfs_iread_extents(tp, ip, whichfork)))
4031 return error;
4032 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4033 if (!nextents) {
4034 *last_block = 0;
4035 return 0;
4036 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004037 ep = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4039 return 0;
4040}
4041
4042/*
4043 * Returns whether the selected fork of the inode has exactly one
4044 * block or not. For the data fork we check this matches di_size,
4045 * implying the file's range is 0..bsize-1.
4046 */
4047int /* 1=>1 block, 0=>otherwise */
4048xfs_bmap_one_block(
4049 xfs_inode_t *ip, /* incore inode */
4050 int whichfork) /* data or attr fork */
4051{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004052 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 xfs_ifork_t *ifp; /* inode fork pointer */
4054 int rval; /* return value */
4055 xfs_bmbt_irec_t s; /* internal version of extent */
4056
4057#ifndef DEBUG
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004058 if (whichfork == XFS_DATA_FORK) {
4059 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4060 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4061 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063#endif /* !DEBUG */
4064 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4065 return 0;
4066 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4067 return 0;
4068 ifp = XFS_IFORK_PTR(ip, whichfork);
4069 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004070 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 xfs_bmbt_get_all(ep, &s);
4072 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4073 if (rval && whichfork == XFS_DATA_FORK)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004074 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 return rval;
4076}
4077
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004078STATIC int
4079xfs_bmap_sanity_check(
4080 struct xfs_mount *mp,
4081 struct xfs_buf *bp,
4082 int level)
4083{
4084 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4085
4086 if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
4087 be16_to_cpu(block->bb_level) != level ||
4088 be16_to_cpu(block->bb_numrecs) == 0 ||
4089 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4090 return 0;
4091 return 1;
4092}
4093
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094/*
4095 * Read in the extents to if_extents.
4096 * All inode fields are set up by caller, we just traverse the btree
4097 * and copy the records in. If the file system cannot contain unwritten
4098 * extents, the records are checked for no "state" flags.
4099 */
4100int /* error */
4101xfs_bmap_read_extents(
4102 xfs_trans_t *tp, /* transaction pointer */
4103 xfs_inode_t *ip, /* incore inode */
4104 int whichfork) /* data or attr fork */
4105{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004106 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 xfs_fsblock_t bno; /* block # of "block" */
4108 xfs_buf_t *bp; /* buffer for "block" */
4109 int error; /* error return value */
4110 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 xfs_extnum_t i, j; /* index into the extents list */
4112 xfs_ifork_t *ifp; /* fork structure */
4113 int level; /* btree level, for checking */
4114 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10004115 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 /* REFERENCED */
4117 xfs_extnum_t room; /* number of entries there's room for */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
4119 bno = NULLFSBLOCK;
4120 mp = ip->i_mount;
4121 ifp = XFS_IFORK_PTR(ip, whichfork);
4122 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4123 XFS_EXTFMT_INODE(ip);
4124 block = ifp->if_broot;
4125 /*
4126 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4127 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004128 level = be16_to_cpu(block->bb_level);
4129 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11004130 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004131 bno = be64_to_cpu(*pp);
4132 ASSERT(bno != NULLDFSBNO);
4133 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4134 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 /*
4136 * Go down the tree until leaf level is reached, following the first
4137 * pointer (leftmost) at each level.
4138 */
4139 while (level-- > 0) {
4140 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4141 XFS_BMAP_BTREE_REF)))
4142 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004143 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004145 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 error0);
4147 if (level == 0)
4148 break;
Christoph Hellwig136341b2008-10-30 17:11:40 +11004149 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004150 bno = be64_to_cpu(*pp);
4151 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 xfs_trans_brelse(tp, bp);
4153 }
4154 /*
4155 * Here with bp and block set to the leftmost leaf node in the tree.
4156 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004157 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 i = 0;
4159 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004160 * Loop over all leaf nodes. Copy information to the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 */
4162 for (;;) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004163 xfs_bmbt_rec_t *frp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 xfs_fsblock_t nextbno;
4165 xfs_extnum_t num_recs;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004166 xfs_extnum_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
4168
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004169 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 if (unlikely(i + num_recs > room)) {
4171 ASSERT(i + num_recs <= room);
Dave Chinner65333b42011-03-07 10:03:35 +11004172 xfs_warn(ip->i_mount,
Nathan Scott3762ec62006-01-12 10:29:53 +11004173 "corrupt dinode %Lu, (btree extents).",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 (unsigned long long) ip->i_ino);
Dave Chinner65333b42011-03-07 10:03:35 +11004175 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
4176 XFS_ERRLEVEL_LOW, ip->i_mount, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 goto error0;
4178 }
4179 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004180 xfs_bmap_sanity_check(mp, bp, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 error0);
4182 /*
4183 * Read-ahead the next leaf block, if any.
4184 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004185 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 if (nextbno != NULLFSBLOCK)
4187 xfs_btree_reada_bufl(mp, nextbno, 1);
4188 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004189 * Copy records into the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11004191 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004192 start = i;
4193 for (j = 0; j < num_recs; j++, i++, frp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004194 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10004195 trp->l0 = be64_to_cpu(frp->l0);
4196 trp->l1 = be64_to_cpu(frp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 }
4198 if (exntf == XFS_EXTFMT_NOSTATE) {
4199 /*
4200 * Check all attribute bmap btree records and
4201 * any "older" data bmap btree records for a
4202 * set bit in the "extent flag" position.
4203 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004204 if (unlikely(xfs_check_nostate_extents(ifp,
4205 start, num_recs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4207 XFS_ERRLEVEL_LOW,
4208 ip->i_mount);
4209 goto error0;
4210 }
4211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 xfs_trans_brelse(tp, bp);
4213 bno = nextbno;
4214 /*
4215 * If we've reached the end, stop.
4216 */
4217 if (bno == NULLFSBLOCK)
4218 break;
4219 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4220 XFS_BMAP_BTREE_REF)))
4221 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004222 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004224 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
Eric Sandeen3a59c942007-07-11 11:09:47 +10004226 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 return 0;
4228error0:
4229 xfs_trans_brelse(tp, bp);
4230 return XFS_ERROR(EFSCORRUPTED);
4231}
4232
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004233#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004235 * Add bmap trace insert entries for all the contents of the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 */
4237void
4238xfs_bmap_trace_exlist(
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 xfs_inode_t *ip, /* incore inode pointer */
4240 xfs_extnum_t cnt, /* count of entries in the list */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004241 int whichfork, /* data or attr fork */
4242 unsigned long caller_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004244 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004246 int state = 0;
4247
4248 if (whichfork == XFS_ATTR_FORK)
4249 state |= BMAP_ATTRFORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
4251 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004252 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004253 for (idx = 0; idx < cnt; idx++)
4254 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257/*
4258 * Validate that the bmbt_irecs being returned from bmapi are valid
4259 * given the callers original parameters. Specifically check the
4260 * ranges of the returned irecs to ensure that they only extent beyond
4261 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4262 */
4263STATIC void
4264xfs_bmap_validate_ret(
4265 xfs_fileoff_t bno,
4266 xfs_filblks_t len,
4267 int flags,
4268 xfs_bmbt_irec_t *mval,
4269 int nmap,
4270 int ret_nmap)
4271{
4272 int i; /* index to map values */
4273
4274 ASSERT(ret_nmap <= nmap);
4275
4276 for (i = 0; i < ret_nmap; i++) {
4277 ASSERT(mval[i].br_blockcount > 0);
4278 if (!(flags & XFS_BMAPI_ENTIRE)) {
4279 ASSERT(mval[i].br_startoff >= bno);
4280 ASSERT(mval[i].br_blockcount <= len);
4281 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4282 bno + len);
4283 } else {
4284 ASSERT(mval[i].br_startoff < bno + len);
4285 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4286 bno);
4287 }
4288 ASSERT(i == 0 ||
4289 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4290 mval[i].br_startoff);
4291 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4292 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4293 mval[i].br_startblock != HOLESTARTBLOCK);
4294 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4295 mval[i].br_state == XFS_EXT_UNWRITTEN);
4296 }
4297}
4298#endif /* DEBUG */
4299
4300
4301/*
4302 * Map file blocks to filesystem blocks.
4303 * File range is given by the bno/len pair.
4304 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4305 * into a hole or past eof.
4306 * Only allocates blocks from a single allocation group,
4307 * to avoid locking problems.
4308 * The returned value in "firstblock" from the first call in a transaction
4309 * must be remembered and presented to subsequent calls in "firstblock".
4310 * An upper bound for the number of blocks to be allocated is supplied to
4311 * the first call in "total"; if no allocation group has that many free
4312 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4313 */
4314int /* error */
4315xfs_bmapi(
4316 xfs_trans_t *tp, /* transaction pointer */
4317 xfs_inode_t *ip, /* incore inode */
4318 xfs_fileoff_t bno, /* starting file offs. mapped */
4319 xfs_filblks_t len, /* length to map in file */
4320 int flags, /* XFS_BMAPI_... */
4321 xfs_fsblock_t *firstblock, /* first allocated block
4322 controls a.g. for allocs */
4323 xfs_extlen_t total, /* total blocks needed */
4324 xfs_bmbt_irec_t *mval, /* output: map values */
4325 int *nmap, /* i/o: mval size/count */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004326 xfs_bmap_free_t *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327{
4328 xfs_fsblock_t abno; /* allocated block number */
4329 xfs_extlen_t alen; /* allocated extent length */
4330 xfs_fileoff_t aoff; /* allocated file offset */
Dave Chinnerf0a0eaa2010-01-20 10:50:06 +11004331 xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 xfs_btree_cur_t *cur; /* bmap btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 xfs_fileoff_t end; /* end of mapped file region */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004334 int eof; /* we've hit the end of extents */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004335 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004337 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 xfs_ifork_t *ifp; /* inode fork pointer */
4339 xfs_extlen_t indlen; /* indirect blocks length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 xfs_extnum_t lastx; /* last useful extent number */
4341 int logflags; /* flags for transaction logging */
4342 xfs_extlen_t minleft; /* min blocks left after allocation */
4343 xfs_extlen_t minlen; /* min allocation size */
4344 xfs_mount_t *mp; /* xfs mount structure */
4345 int n; /* current extent index */
Malcolm Parsons9da096f2009-03-29 09:55:42 +02004346 int nallocs; /* number of extents alloc'd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 xfs_extnum_t nextents; /* number of extents in file */
4348 xfs_fileoff_t obno; /* old block number (offset) */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004349 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 int tmp_logflags; /* temp flags holder */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004351 int whichfork; /* data or attr fork */
4352 char inhole; /* current location is hole in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 char wasdelay; /* old extent was delayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 char wr; /* this is a write request */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004355 char rt; /* this is a realtime file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356#ifdef DEBUG
4357 xfs_fileoff_t orig_bno; /* original block number value */
4358 int orig_flags; /* original flags arg value */
4359 xfs_filblks_t orig_len; /* original value of len arg */
4360 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4361 int orig_nmap; /* original value of *nmap */
4362
4363 orig_bno = bno;
4364 orig_len = len;
4365 orig_flags = flags;
4366 orig_mval = mval;
4367 orig_nmap = *nmap;
4368#endif
4369 ASSERT(*nmap >= 1);
4370 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4371 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4372 XFS_ATTR_FORK : XFS_DATA_FORK;
4373 mp = ip->i_mount;
4374 if (unlikely(XFS_TEST_ERROR(
4375 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4376 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4377 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4378 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4379 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4380 return XFS_ERROR(EFSCORRUPTED);
4381 }
4382 if (XFS_FORCED_SHUTDOWN(mp))
4383 return XFS_ERROR(EIO);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004384 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 ifp = XFS_IFORK_PTR(ip, whichfork);
4386 ASSERT(ifp->if_ext_max ==
4387 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4388 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4389 XFS_STATS_INC(xs_blk_mapw);
4390 else
4391 XFS_STATS_INC(xs_blk_mapr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 /*
Nathan Scott39269e22006-03-14 13:33:50 +11004393 * IGSTATE flag is used to combine extents which
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 * differ only due to the state of the extents.
4395 * This technique is used from xfs_getbmap()
4396 * when the caller does not wish to see the
4397 * separation (which is the default).
4398 *
4399 * This technique is also used when writing a
4400 * buffer which has been partially written,
4401 * (usually by being flushed during a chunkread),
4402 * to ensure one write takes place. This also
4403 * prevents a change in the xfs inode extents at
4404 * this time, intentionally. This change occurs
4405 * on completion of the write operation, in
4406 * xfs_strat_comp(), where the xfs_bmapi() call
4407 * is transactioned, and the extents combined.
4408 */
Nathan Scott39269e22006-03-14 13:33:50 +11004409 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4410 wr = 0; /* no allocations are allowed */
4411 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 logflags = 0;
4413 nallocs = 0;
4414 cur = NULL;
4415 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4416 ASSERT(wr && tp);
4417 if ((error = xfs_bmap_local_to_extents(tp, ip,
4418 firstblock, total, &logflags, whichfork)))
4419 goto error0;
4420 }
4421 if (wr && *firstblock == NULLFSBLOCK) {
4422 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Christoph Hellwig16259e72005-11-02 15:11:25 +11004423 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 else
4425 minleft = 1;
4426 } else
4427 minleft = 0;
4428 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4429 (error = xfs_iread_extents(tp, ip, whichfork)))
4430 goto error0;
4431 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4432 &prev);
4433 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4434 n = 0;
4435 end = bno + len;
4436 obno = bno;
4437 bma.ip = NULL;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004438
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 while (bno < end && n < *nmap) {
4440 /*
4441 * Reading past eof, act as though there's a hole
4442 * up to end.
4443 */
4444 if (eof && !wr)
4445 got.br_startoff = end;
4446 inhole = eof || got.br_startoff > bno;
Nathan Scott39269e22006-03-14 13:33:50 +11004447 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06004448 isnullstartblock(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 /*
4450 * First, deal with the hole before the allocated space
4451 * that we found, if any.
4452 */
4453 if (wr && (inhole || wasdelay)) {
4454 /*
4455 * For the wasdelay case, we could also just
4456 * allocate the stuff asked for in this bmap call
4457 * but that wouldn't be as good.
4458 */
Christoph Hellwigcd8b0bb2010-06-23 18:11:15 +10004459 if (wasdelay) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 alen = (xfs_extlen_t)got.br_blockcount;
4461 aoff = got.br_startoff;
4462 if (lastx != NULLEXTNUM && lastx) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004463 ep = xfs_iext_get_ext(ifp, lastx - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 xfs_bmbt_get_all(ep, &prev);
4465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 } else {
4467 alen = (xfs_extlen_t)
4468 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4469 if (!eof)
4470 alen = (xfs_extlen_t)
4471 XFS_FILBLKS_MIN(alen,
4472 got.br_startoff - bno);
4473 aoff = bno;
4474 }
Nathan Scott39269e22006-03-14 13:33:50 +11004475 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4476 if (flags & XFS_BMAPI_DELAY) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11004477 xfs_extlen_t extsz;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004478
4479 /* Figure out the extent size, adjust alen */
David Chinner957d0eb2007-06-18 16:50:37 +10004480 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004481 if (extsz) {
Dave Chinner4ce15982011-01-27 12:17:58 +11004482 /*
4483 * make sure we don't exceed a single
4484 * extent length when we align the
4485 * extent by reducing length we are
4486 * going to allocate by the maximum
4487 * amount extent size aligment may
4488 * require.
4489 */
4490 alen = XFS_FILBLKS_MIN(len,
4491 MAXEXTLEN - (2 * extsz - 1));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004492 error = xfs_bmap_extsize_align(mp,
4493 &got, &prev, extsz,
Nathan Scott39269e22006-03-14 13:33:50 +11004494 rt, eof,
4495 flags&XFS_BMAPI_DELAY,
4496 flags&XFS_BMAPI_CONVERT,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004497 &aoff, &alen);
4498 ASSERT(!error);
4499 }
4500
4501 if (rt)
4502 extsz = alen / mp->m_sb.sb_rextsize;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004503
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 /*
4505 * Make a transaction-less quota reservation for
4506 * delayed allocation blocks. This number gets
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004507 * adjusted later. We return if we haven't
4508 * allocated blocks already inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02004510 error = xfs_trans_reserve_quota_nblks(
4511 NULL, ip, (long)alen, 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004512 rt ? XFS_QMOPT_RES_RTBLKS :
Christoph Hellwig7d095252009-06-08 15:33:32 +02004513 XFS_QMOPT_RES_REGBLKS);
4514 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 if (n == 0) {
4516 *nmap = 0;
4517 ASSERT(cur == NULL);
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004518 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 }
4520 break;
4521 }
4522
4523 /*
4524 * Split changing sb for alen and indlen since
4525 * they could be coming from different places.
4526 */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004527 indlen = (xfs_extlen_t)
4528 xfs_bmap_worst_indlen(ip, alen);
4529 ASSERT(indlen > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
Nathan Scottdd9f4382006-01-11 15:28:28 +11004531 if (rt) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004532 error = xfs_mod_incore_sb(mp,
4533 XFS_SBS_FREXTENTS,
Christoph Hellwig54893272011-05-11 15:04:03 +00004534 -((int64_t)extsz), 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004535 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00004536 error = xfs_icsb_modify_counters(mp,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004537 XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00004538 -((int64_t)alen), 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004539 }
David Chinner3bdbfb12005-09-02 16:40:47 +10004540 if (!error) {
Christoph Hellwig96540c72010-09-30 02:25:55 +00004541 error = xfs_icsb_modify_counters(mp,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004542 XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00004543 -((int64_t)indlen), 0);
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004544 if (error && rt)
4545 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004546 XFS_SBS_FREXTENTS,
Christoph Hellwig54893272011-05-11 15:04:03 +00004547 (int64_t)extsz, 0);
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004548 else if (error)
Christoph Hellwig96540c72010-09-30 02:25:55 +00004549 xfs_icsb_modify_counters(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004550 XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00004551 (int64_t)alen, 0);
David Chinner3bdbfb12005-09-02 16:40:47 +10004552 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004553
4554 if (error) {
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004555 if (XFS_IS_QUOTA_ON(mp))
Nathan Scott06d10dd2005-06-21 15:48:47 +10004556 /* unreserve the blocks now */
Nathan Scottdd9f4382006-01-11 15:28:28 +11004557 (void)
Christoph Hellwig7d095252009-06-08 15:33:32 +02004558 xfs_trans_unreserve_quota_nblks(
4559 NULL, ip,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004560 (long)alen, 0, rt ?
4561 XFS_QMOPT_RES_RTBLKS :
4562 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 break;
4564 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004565
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 ip->i_delayed_blks += alen;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004567 abno = nullstartblock(indlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 } else {
4569 /*
4570 * If first time, allocate and fill in
4571 * once-only bma fields.
4572 */
4573 if (bma.ip == NULL) {
4574 bma.tp = tp;
4575 bma.ip = ip;
4576 bma.prevp = &prev;
4577 bma.gotp = &got;
4578 bma.total = total;
4579 bma.userdata = 0;
4580 }
4581 /* Indicate if this is the first user data
4582 * in the file, or just any user data.
4583 */
Nathan Scott39269e22006-03-14 13:33:50 +11004584 if (!(flags & XFS_BMAPI_METADATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 bma.userdata = (aoff == 0) ?
4586 XFS_ALLOC_INITIAL_USER_DATA :
4587 XFS_ALLOC_USERDATA;
4588 }
4589 /*
4590 * Fill in changeable bma fields.
4591 */
4592 bma.eof = eof;
4593 bma.firstblock = *firstblock;
4594 bma.alen = alen;
4595 bma.off = aoff;
Adrian Bunk72880262006-08-30 13:41:58 +10004596 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 bma.wasdel = wasdelay;
4598 bma.minlen = minlen;
4599 bma.low = flist->xbf_low;
4600 bma.minleft = minleft;
4601 /*
4602 * Only want to do the alignment at the
4603 * eof if it is userdata and allocation length
4604 * is larger than a stripe unit.
4605 */
4606 if (mp->m_dalign && alen >= mp->m_dalign &&
Nathan Scott39269e22006-03-14 13:33:50 +11004607 (!(flags & XFS_BMAPI_METADATA)) &&
4608 (whichfork == XFS_DATA_FORK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 if ((error = xfs_bmap_isaeof(ip, aoff,
4610 whichfork, &bma.aeof)))
4611 goto error0;
4612 } else
4613 bma.aeof = 0;
4614 /*
4615 * Call allocator.
4616 */
4617 if ((error = xfs_bmap_alloc(&bma)))
4618 goto error0;
4619 /*
4620 * Copy out result fields.
4621 */
4622 abno = bma.rval;
4623 if ((flist->xbf_low = bma.low))
4624 minleft = 0;
4625 alen = bma.alen;
4626 aoff = bma.off;
4627 ASSERT(*firstblock == NULLFSBLOCK ||
4628 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4629 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4630 (flist->xbf_low &&
4631 XFS_FSB_TO_AGNO(mp, *firstblock) <
4632 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4633 *firstblock = bma.firstblock;
4634 if (cur)
4635 cur->bc_private.b.firstblock =
4636 *firstblock;
4637 if (abno == NULLFSBLOCK)
4638 break;
4639 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11004640 cur = xfs_bmbt_init_cursor(mp, tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 ip, whichfork);
4642 cur->bc_private.b.firstblock =
4643 *firstblock;
4644 cur->bc_private.b.flist = flist;
4645 }
4646 /*
4647 * Bump the number of extents we've allocated
4648 * in this call.
4649 */
4650 nallocs++;
4651 }
4652 if (cur)
4653 cur->bc_private.b.flags =
4654 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4655 got.br_startoff = aoff;
4656 got.br_startblock = abno;
4657 got.br_blockcount = alen;
4658 got.br_state = XFS_EXT_NORM; /* assume normal */
4659 /*
4660 * Determine state of extent, and the filesystem.
4661 * A wasdelay extent has been initialized, so
4662 * shouldn't be flagged as unwritten.
4663 */
Eric Sandeen62118702008-03-06 13:44:28 +11004664 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
4666 got.br_state = XFS_EXT_UNWRITTEN;
4667 }
Christoph Hellwigf3ca8732011-07-08 14:34:47 +02004668 error = xfs_bmap_add_extent(tp, ip, &lastx, &cur, &got,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004669 firstblock, flist, &tmp_logflags,
Christoph Hellwig54893272011-05-11 15:04:03 +00004670 whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 logflags |= tmp_logflags;
4672 if (error)
4673 goto error0;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004674 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4676 xfs_bmbt_get_all(ep, &got);
4677 ASSERT(got.br_startoff <= aoff);
4678 ASSERT(got.br_startoff + got.br_blockcount >=
4679 aoff + alen);
4680#ifdef DEBUG
Nathan Scott39269e22006-03-14 13:33:50 +11004681 if (flags & XFS_BMAPI_DELAY) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06004682 ASSERT(isnullstartblock(got.br_startblock));
4683 ASSERT(startblockval(got.br_startblock) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 }
4685 ASSERT(got.br_state == XFS_EXT_NORM ||
4686 got.br_state == XFS_EXT_UNWRITTEN);
4687#endif
4688 /*
4689 * Fall down into the found allocated space case.
4690 */
4691 } else if (inhole) {
4692 /*
4693 * Reading in a hole.
4694 */
4695 mval->br_startoff = bno;
4696 mval->br_startblock = HOLESTARTBLOCK;
4697 mval->br_blockcount =
4698 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4699 mval->br_state = XFS_EXT_NORM;
4700 bno += mval->br_blockcount;
4701 len -= mval->br_blockcount;
4702 mval++;
4703 n++;
4704 continue;
4705 }
4706 /*
4707 * Then deal with the allocated space we found.
4708 */
4709 ASSERT(ep != NULL);
Nathan Scott39269e22006-03-14 13:33:50 +11004710 if (!(flags & XFS_BMAPI_ENTIRE) &&
4711 (got.br_startoff + got.br_blockcount > obno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 if (obno > bno)
4713 bno = obno;
4714 ASSERT((bno >= obno) || (n == 0));
4715 ASSERT(bno < end);
4716 mval->br_startoff = bno;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004717 if (isnullstartblock(got.br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11004718 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 mval->br_startblock = DELAYSTARTBLOCK;
4720 } else
4721 mval->br_startblock =
4722 got.br_startblock +
4723 (bno - got.br_startoff);
4724 /*
4725 * Return the minimum of what we got and what we
4726 * asked for for the length. We can use the len
4727 * variable here because it is modified below
4728 * and we could have been there before coming
4729 * here if the first part of the allocation
4730 * didn't overlap what was asked for.
4731 */
4732 mval->br_blockcount =
4733 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
4734 (bno - got.br_startoff));
4735 mval->br_state = got.br_state;
4736 ASSERT(mval->br_blockcount <= len);
4737 } else {
4738 *mval = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004739 if (isnullstartblock(mval->br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11004740 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 mval->br_startblock = DELAYSTARTBLOCK;
4742 }
4743 }
4744
4745 /*
4746 * Check if writing previously allocated but
4747 * unwritten extents.
4748 */
Dave Chinner44722352010-08-24 12:02:11 +10004749 if (wr &&
4750 ((mval->br_state == XFS_EXT_UNWRITTEN &&
4751 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) ||
4752 (mval->br_state == XFS_EXT_NORM &&
4753 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT)) ==
4754 (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 /*
4756 * Modify (by adding) the state flag, if writing.
4757 */
4758 ASSERT(mval->br_blockcount <= len);
4759 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11004760 cur = xfs_bmbt_init_cursor(mp,
4761 tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 cur->bc_private.b.firstblock =
4763 *firstblock;
4764 cur->bc_private.b.flist = flist;
4765 }
Dave Chinner44722352010-08-24 12:02:11 +10004766 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4767 ? XFS_EXT_NORM
4768 : XFS_EXT_UNWRITTEN;
Christoph Hellwigf3ca8732011-07-08 14:34:47 +02004769 error = xfs_bmap_add_extent(tp, ip, &lastx, &cur, mval,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004770 firstblock, flist, &tmp_logflags,
Christoph Hellwig54893272011-05-11 15:04:03 +00004771 whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 logflags |= tmp_logflags;
4773 if (error)
4774 goto error0;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004775 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4777 xfs_bmbt_get_all(ep, &got);
4778 /*
4779 * We may have combined previously unwritten
4780 * space with written space, so generate
4781 * another request.
4782 */
4783 if (mval->br_blockcount < len)
4784 continue;
4785 }
4786
Nathan Scott39269e22006-03-14 13:33:50 +11004787 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 ((mval->br_startoff + mval->br_blockcount) <= end));
Nathan Scott39269e22006-03-14 13:33:50 +11004789 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4790 (mval->br_blockcount <= len) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 (mval->br_startoff < obno));
4792 bno = mval->br_startoff + mval->br_blockcount;
4793 len = end - bno;
4794 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4795 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4796 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4797 ASSERT(mval->br_state == mval[-1].br_state);
4798 mval[-1].br_blockcount = mval->br_blockcount;
4799 mval[-1].br_state = mval->br_state;
4800 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4801 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4802 mval[-1].br_startblock != HOLESTARTBLOCK &&
4803 mval->br_startblock ==
4804 mval[-1].br_startblock + mval[-1].br_blockcount &&
Nathan Scott39269e22006-03-14 13:33:50 +11004805 ((flags & XFS_BMAPI_IGSTATE) ||
4806 mval[-1].br_state == mval->br_state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 ASSERT(mval->br_startoff ==
4808 mval[-1].br_startoff + mval[-1].br_blockcount);
4809 mval[-1].br_blockcount += mval->br_blockcount;
4810 } else if (n > 0 &&
4811 mval->br_startblock == DELAYSTARTBLOCK &&
4812 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4813 mval->br_startoff ==
4814 mval[-1].br_startoff + mval[-1].br_blockcount) {
4815 mval[-1].br_blockcount += mval->br_blockcount;
4816 mval[-1].br_state = mval->br_state;
4817 } else if (!((n == 0) &&
4818 ((mval->br_startoff + mval->br_blockcount) <=
4819 obno))) {
4820 mval++;
4821 n++;
4822 }
4823 /*
4824 * If we're done, stop now. Stop when we've allocated
4825 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4826 * the transaction may get too big.
4827 */
4828 if (bno >= end || n >= *nmap || nallocs >= *nmap)
4829 break;
4830 /*
4831 * Else go on to the next record.
4832 */
David Chinner4e5ae832007-06-05 16:24:15 +10004833 prev = got;
Christoph Hellwig5690f922011-05-11 15:04:07 +00004834 if (++lastx < nextents) {
4835 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig5690f922011-05-11 15:04:07 +00004837 } else {
4838 eof = 1;
4839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 *nmap = n;
4842 /*
4843 * Transform from btree to extents, give it cur.
4844 */
4845 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4846 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4847 ASSERT(wr && cur);
4848 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4849 &tmp_logflags, whichfork);
4850 logflags |= tmp_logflags;
4851 if (error)
4852 goto error0;
4853 }
4854 ASSERT(ifp->if_ext_max ==
4855 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4856 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4857 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
4858 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859error0:
4860 /*
4861 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004862 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06004864 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06004866 logflags &= ~xfs_ilog_fext(whichfork);
4867 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06004869 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 /*
4871 * Log whatever the flags say, even if error. Otherwise we might miss
4872 * detecting a case where the data is changed, there's an error,
4873 * and it's not logged so we don't shutdown when we should.
4874 */
4875 if (logflags) {
4876 ASSERT(tp && wr);
4877 xfs_trans_log_inode(tp, ip, logflags);
4878 }
4879 if (cur) {
4880 if (!error) {
4881 ASSERT(*firstblock == NULLFSBLOCK ||
4882 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4883 XFS_FSB_TO_AGNO(mp,
4884 cur->bc_private.b.firstblock) ||
4885 (flist->xbf_low &&
4886 XFS_FSB_TO_AGNO(mp, *firstblock) <
4887 XFS_FSB_TO_AGNO(mp,
4888 cur->bc_private.b.firstblock)));
4889 *firstblock = cur->bc_private.b.firstblock;
4890 }
4891 xfs_btree_del_cursor(cur,
4892 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4893 }
4894 if (!error)
4895 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4896 orig_nmap, *nmap);
4897 return error;
4898}
4899
4900/*
4901 * Map file blocks to filesystem blocks, simple version.
4902 * One block (extent) only, read-only.
4903 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
4904 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
4905 * was set and all the others were clear.
4906 */
4907int /* error */
4908xfs_bmapi_single(
4909 xfs_trans_t *tp, /* transaction pointer */
4910 xfs_inode_t *ip, /* incore inode */
4911 int whichfork, /* data or attr fork */
4912 xfs_fsblock_t *fsb, /* output: mapped block */
4913 xfs_fileoff_t bno) /* starting file offs. mapped */
4914{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004915 int eof; /* we've hit the end of extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004917 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 xfs_ifork_t *ifp; /* inode fork pointer */
4919 xfs_extnum_t lastx; /* last useful extent number */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004920 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
4922 ifp = XFS_IFORK_PTR(ip, whichfork);
4923 if (unlikely(
4924 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4925 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
4926 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
4927 ip->i_mount);
4928 return XFS_ERROR(EFSCORRUPTED);
4929 }
4930 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
4931 return XFS_ERROR(EIO);
4932 XFS_STATS_INC(xs_blk_mapr);
4933 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4934 (error = xfs_iread_extents(tp, ip, whichfork)))
4935 return error;
4936 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4937 &prev);
4938 /*
4939 * Reading past eof, act as though there's a hole
4940 * up to end.
4941 */
4942 if (eof || got.br_startoff > bno) {
4943 *fsb = NULLFSBLOCK;
4944 return 0;
4945 }
Eric Sandeen9d87c312009-01-14 23:22:07 -06004946 ASSERT(!isnullstartblock(got.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 ASSERT(bno < got.br_startoff + got.br_blockcount);
4948 *fsb = got.br_startblock + (bno - got.br_startoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 return 0;
4950}
4951
4952/*
4953 * Unmap (remove) blocks from a file.
4954 * If nexts is nonzero then the number of extents to remove is limited to
4955 * that value. If not all extents in the block range can be removed then
4956 * *done is set.
4957 */
4958int /* error */
4959xfs_bunmapi(
4960 xfs_trans_t *tp, /* transaction pointer */
4961 struct xfs_inode *ip, /* incore inode */
4962 xfs_fileoff_t bno, /* starting offset to unmap */
4963 xfs_filblks_t len, /* length to unmap in file */
4964 int flags, /* misc flags */
4965 xfs_extnum_t nexts, /* number of extents max */
4966 xfs_fsblock_t *firstblock, /* first allocated block
4967 controls a.g. for allocs */
4968 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4969 int *done) /* set if not done yet */
4970{
4971 xfs_btree_cur_t *cur; /* bmap btree cursor */
4972 xfs_bmbt_irec_t del; /* extent being deleted */
4973 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004974 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 int error; /* error return value */
4976 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004977 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 xfs_ifork_t *ifp; /* inode fork pointer */
4979 int isrt; /* freeing in rt area */
4980 xfs_extnum_t lastx; /* last extent index used */
4981 int logflags; /* transaction logging flags */
4982 xfs_extlen_t mod; /* rt extent offset */
4983 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004984 xfs_extnum_t nextents; /* number of file extents */
4985 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 xfs_fileoff_t start; /* first file offset deleted */
4987 int tmp_logflags; /* partial logging flags */
4988 int wasdel; /* was a delayed alloc extent */
4989 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 xfs_fsblock_t sum;
4991
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004992 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
4993
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4995 XFS_ATTR_FORK : XFS_DATA_FORK;
4996 ifp = XFS_IFORK_PTR(ip, whichfork);
4997 if (unlikely(
4998 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4999 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5000 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5001 ip->i_mount);
5002 return XFS_ERROR(EFSCORRUPTED);
5003 }
5004 mp = ip->i_mount;
5005 if (XFS_FORCED_SHUTDOWN(mp))
5006 return XFS_ERROR(EIO);
Christoph Hellwig54893272011-05-11 15:04:03 +00005007
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 ASSERT(len > 0);
5009 ASSERT(nexts >= 0);
5010 ASSERT(ifp->if_ext_max ==
5011 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5012 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5013 (error = xfs_iread_extents(tp, ip, whichfork)))
5014 return error;
5015 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5016 if (nextents == 0) {
5017 *done = 1;
5018 return 0;
5019 }
5020 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005021 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 start = bno;
5023 bno = start + len - 1;
5024 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5025 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005026
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 /*
5028 * Check to see if the given block number is past the end of the
5029 * file, back up to the last block if so...
5030 */
5031 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005032 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 xfs_bmbt_get_all(ep, &got);
5034 bno = got.br_startoff + got.br_blockcount - 1;
5035 }
5036 logflags = 0;
5037 if (ifp->if_flags & XFS_IFBROOT) {
5038 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005039 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 cur->bc_private.b.firstblock = *firstblock;
5041 cur->bc_private.b.flist = flist;
5042 cur->bc_private.b.flags = 0;
5043 } else
5044 cur = NULL;
5045 extno = 0;
5046 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5047 (nexts == 0 || extno < nexts)) {
5048 /*
5049 * Is the found extent after a hole in which bno lives?
5050 * Just back up to the previous extent, if so.
5051 */
5052 if (got.br_startoff > bno) {
5053 if (--lastx < 0)
5054 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005055 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 xfs_bmbt_get_all(ep, &got);
5057 }
5058 /*
5059 * Is the last block of this extent before the range
5060 * we're supposed to delete? If so, we're done.
5061 */
5062 bno = XFS_FILEOFF_MIN(bno,
5063 got.br_startoff + got.br_blockcount - 1);
5064 if (bno < start)
5065 break;
5066 /*
5067 * Then deal with the (possibly delayed) allocated space
5068 * we found.
5069 */
5070 ASSERT(ep != NULL);
5071 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005072 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 if (got.br_startoff < start) {
5074 del.br_startoff = start;
5075 del.br_blockcount -= start - got.br_startoff;
5076 if (!wasdel)
5077 del.br_startblock += start - got.br_startoff;
5078 }
5079 if (del.br_startoff + del.br_blockcount > bno + 1)
5080 del.br_blockcount = bno + 1 - del.br_startoff;
5081 sum = del.br_startblock + del.br_blockcount;
5082 if (isrt &&
5083 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5084 /*
5085 * Realtime extent not lined up at the end.
5086 * The extent could have been split into written
5087 * and unwritten pieces, or we could just be
5088 * unmapping part of it. But we can't really
5089 * get rid of part of a realtime extent.
5090 */
5091 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005092 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 /*
5094 * This piece is unwritten, or we're not
5095 * using unwritten extents. Skip over it.
5096 */
5097 ASSERT(bno >= mod);
5098 bno -= mod > del.br_blockcount ?
5099 del.br_blockcount : mod;
5100 if (bno < got.br_startoff) {
5101 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005102 xfs_bmbt_get_all(xfs_iext_get_ext(
5103 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 }
5105 continue;
5106 }
5107 /*
5108 * It's written, turn it unwritten.
5109 * This is better than zeroing it.
5110 */
5111 ASSERT(del.br_state == XFS_EXT_NORM);
5112 ASSERT(xfs_trans_get_block_res(tp) > 0);
5113 /*
5114 * If this spans a realtime extent boundary,
5115 * chop it back to the start of the one we end at.
5116 */
5117 if (del.br_blockcount > mod) {
5118 del.br_startoff += del.br_blockcount - mod;
5119 del.br_startblock += del.br_blockcount - mod;
5120 del.br_blockcount = mod;
5121 }
5122 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigf3ca8732011-07-08 14:34:47 +02005123 error = xfs_bmap_add_extent(tp, ip, &lastx, &cur, &del,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005124 firstblock, flist, &logflags,
Christoph Hellwig54893272011-05-11 15:04:03 +00005125 XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 if (error)
5127 goto error0;
5128 goto nodelete;
5129 }
5130 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5131 /*
5132 * Realtime extent is lined up at the end but not
5133 * at the front. We'll get rid of full extents if
5134 * we can.
5135 */
5136 mod = mp->m_sb.sb_rextsize - mod;
5137 if (del.br_blockcount > mod) {
5138 del.br_blockcount -= mod;
5139 del.br_startoff += mod;
5140 del.br_startblock += mod;
5141 } else if ((del.br_startoff == start &&
5142 (del.br_state == XFS_EXT_UNWRITTEN ||
5143 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005144 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 /*
5146 * Can't make it unwritten. There isn't
5147 * a full extent here so just skip it.
5148 */
5149 ASSERT(bno >= del.br_blockcount);
5150 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005151 if (got.br_startoff > bno) {
5152 if (--lastx >= 0) {
5153 ep = xfs_iext_get_ext(ifp,
5154 lastx);
5155 xfs_bmbt_get_all(ep, &got);
5156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 }
5158 continue;
5159 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5160 /*
5161 * This one is already unwritten.
5162 * It must have a written left neighbor.
5163 * Unwrite the killed part of that one and
5164 * try again.
5165 */
5166 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005167 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5168 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005170 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 ASSERT(del.br_startblock ==
5172 prev.br_startblock + prev.br_blockcount);
5173 if (prev.br_startoff < start) {
5174 mod = start - prev.br_startoff;
5175 prev.br_blockcount -= mod;
5176 prev.br_startblock += mod;
5177 prev.br_startoff = start;
5178 }
5179 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005180 lastx--;
Christoph Hellwigf3ca8732011-07-08 14:34:47 +02005181 error = xfs_bmap_add_extent(tp, ip, &lastx,
5182 &cur, &prev, firstblock, flist,
5183 &logflags, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 if (error)
5185 goto error0;
5186 goto nodelete;
5187 } else {
5188 ASSERT(del.br_state == XFS_EXT_NORM);
5189 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigf3ca8732011-07-08 14:34:47 +02005190 error = xfs_bmap_add_extent(tp, ip, &lastx,
5191 &cur, &del, firstblock, flist,
5192 &logflags, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 if (error)
5194 goto error0;
5195 goto nodelete;
5196 }
5197 }
5198 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005199 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005200 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005201 if (isrt) {
5202 xfs_filblks_t rtexts;
5203
5204 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5205 do_div(rtexts, mp->m_sb.sb_rextsize);
5206 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005207 (int64_t)rtexts, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005208 (void)xfs_trans_reserve_quota_nblks(NULL,
5209 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005210 XFS_QMOPT_RES_RTBLKS);
5211 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00005212 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005213 (int64_t)del.br_blockcount, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005214 (void)xfs_trans_reserve_quota_nblks(NULL,
5215 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 ip->i_delayed_blks -= del.br_blockcount;
5219 if (cur)
5220 cur->bc_private.b.flags |=
5221 XFS_BTCUR_BPRV_WASDEL;
5222 } else if (cur)
5223 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5224 /*
5225 * If it's the case where the directory code is running
5226 * with no block reservation, and the deleted block is in
5227 * the middle of its extent, and the resulting insert
5228 * of an extent would cause transformation to btree format,
5229 * then reject it. The calling code will then swap
5230 * blocks around instead.
5231 * We have to do this now, rather than waiting for the
5232 * conversion to btree format, since the transaction
5233 * will be dirty.
5234 */
5235 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5236 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5237 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5238 del.br_startoff > got.br_startoff &&
5239 del.br_startoff + del.br_blockcount <
5240 got.br_startoff + got.br_blockcount) {
5241 error = XFS_ERROR(ENOSPC);
5242 goto error0;
5243 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00005244 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005245 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 logflags |= tmp_logflags;
5247 if (error)
5248 goto error0;
5249 bno = del.br_startoff - 1;
5250nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 /*
5252 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005255 if (lastx >= 0) {
5256 ep = xfs_iext_get_ext(ifp, lastx);
5257 if (xfs_bmbt_get_startoff(ep) > bno) {
5258 if (--lastx >= 0)
5259 ep = xfs_iext_get_ext(ifp,
5260 lastx);
5261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 extno++;
5265 }
5266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5268 ASSERT(ifp->if_ext_max ==
5269 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5270 /*
5271 * Convert to a btree if necessary.
5272 */
5273 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5274 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5275 ASSERT(cur == NULL);
5276 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5277 &cur, 0, &tmp_logflags, whichfork);
5278 logflags |= tmp_logflags;
5279 if (error)
5280 goto error0;
5281 }
5282 /*
5283 * transform from btree to extents, give it cur
5284 */
5285 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5286 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5287 ASSERT(cur != NULL);
5288 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5289 whichfork);
5290 logflags |= tmp_logflags;
5291 if (error)
5292 goto error0;
5293 }
5294 /*
5295 * transform from extents to local?
5296 */
5297 ASSERT(ifp->if_ext_max ==
5298 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5299 error = 0;
5300error0:
5301 /*
5302 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005303 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005305 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005307 logflags &= ~xfs_ilog_fext(whichfork);
5308 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005310 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 /*
5312 * Log inode even in the error case, if the transaction
5313 * is dirty we'll need to shut down the filesystem.
5314 */
5315 if (logflags)
5316 xfs_trans_log_inode(tp, ip, logflags);
5317 if (cur) {
5318 if (!error) {
5319 *firstblock = cur->bc_private.b.firstblock;
5320 cur->bc_private.b.allocated = 0;
5321 }
5322 xfs_btree_del_cursor(cur,
5323 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5324 }
5325 return error;
5326}
5327
5328/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005329 * returns 1 for success, 0 if we failed to map the extent.
5330 */
5331STATIC int
5332xfs_getbmapx_fix_eof_hole(
5333 xfs_inode_t *ip, /* xfs incore inode pointer */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005334 struct getbmapx *out, /* output structure */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005335 int prealloced, /* this is a file with
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005336 * preallocated data space */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005337 __int64_t end, /* last block requested */
5338 xfs_fsblock_t startblock)
5339{
5340 __int64_t fixlen;
5341 xfs_mount_t *mp; /* file system mount point */
Eric Sandeen5af317c2008-11-28 14:23:35 +11005342 xfs_ifork_t *ifp; /* inode fork pointer */
5343 xfs_extnum_t lastx; /* last extent pointer */
5344 xfs_fileoff_t fileblock;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005345
5346 if (startblock == HOLESTARTBLOCK) {
5347 mp = ip->i_mount;
5348 out->bmv_block = -1;
5349 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5350 fixlen -= out->bmv_offset;
5351 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5352 /* Came to hole at EOF. Trim it. */
5353 if (fixlen <= 0)
5354 return 0;
5355 out->bmv_length = fixlen;
5356 }
5357 } else {
Eric Sandeen5af317c2008-11-28 14:23:35 +11005358 if (startblock == DELAYSTARTBLOCK)
5359 out->bmv_block = -2;
5360 else
Eric Sandeen9d87c312009-01-14 23:22:07 -06005361 out->bmv_block = xfs_fsb_to_db(ip, startblock);
Eric Sandeen5af317c2008-11-28 14:23:35 +11005362 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5363 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5364 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5365 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5366 out->bmv_oflags |= BMV_OF_LAST;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005367 }
5368
5369 return 1;
5370}
5371
5372/*
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005373 * Get inode's extents as described in bmv, and format for output.
5374 * Calls formatter to fill the user's buffer until all extents
5375 * are mapped, until the passed-in bmv->bmv_count slots have
5376 * been filled, or until the formatter short-circuits the loop,
5377 * if it is tracking filled-in extents on its own.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 */
5379int /* error code */
5380xfs_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10005381 xfs_inode_t *ip,
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005382 struct getbmapx *bmv, /* user bmap structure */
5383 xfs_bmap_format_t formatter, /* format to user */
5384 void *arg) /* formatter arg */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385{
5386 __int64_t bmvend; /* last block requested */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005387 int error = 0; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 __int64_t fixlen; /* length for -1 case */
5389 int i; /* extent number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 int lock; /* lock state */
5391 xfs_bmbt_irec_t *map; /* buffer for user's data */
5392 xfs_mount_t *mp; /* file system mount point */
5393 int nex; /* # of user extents can do */
5394 int nexleft; /* # of user extents left */
5395 int subnex; /* # of bmapi's can do */
5396 int nmap; /* number of map entries */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005397 struct getbmapx *out; /* output structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 int whichfork; /* data or attr fork */
5399 int prealloced; /* this is a file with
5400 * preallocated data space */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005401 int iflags; /* interface flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 int bmapi_flags; /* flags for xfs_bmapi */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005403 int cur_ext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 mp = ip->i_mount;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005406 iflags = bmv->bmv_iflags;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005407 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 if (whichfork == XFS_ATTR_FORK) {
5410 if (XFS_IFORK_Q(ip)) {
5411 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5412 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5413 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5414 return XFS_ERROR(EINVAL);
5415 } else if (unlikely(
5416 ip->i_d.di_aformat != 0 &&
5417 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5418 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5419 ip->i_mount);
5420 return XFS_ERROR(EFSCORRUPTED);
5421 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005422
5423 prealloced = 0;
5424 fixlen = 1LL << 32;
5425 } else {
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005426 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5427 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5428 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5429 return XFS_ERROR(EINVAL);
5430
David Chinner957d0eb2007-06-18 16:50:37 +10005431 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005432 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 prealloced = 1;
5434 fixlen = XFS_MAXIOFFSET(mp);
5435 } else {
5436 prealloced = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005437 fixlen = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 }
5440
5441 if (bmv->bmv_length == -1) {
5442 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005443 bmv->bmv_length =
5444 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5445 } else if (bmv->bmv_length == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 bmv->bmv_entries = 0;
5447 return 0;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005448 } else if (bmv->bmv_length < 0) {
5449 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005451
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 nex = bmv->bmv_count - 1;
5453 if (nex <= 0)
5454 return XFS_ERROR(EINVAL);
5455 bmvend = bmv->bmv_offset + bmv->bmv_length;
5456
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005457
5458 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5459 return XFS_ERROR(ENOMEM);
5460 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5461 if (!out)
5462 return XFS_ERROR(ENOMEM);
5463
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005465 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5466 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5467 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5468 if (error)
5469 goto out_unlock_iolock;
Niv Sardie12070a2008-03-06 13:43:03 +11005470 }
Dave Chinner309c8482010-11-30 15:16:02 +11005471 /*
5472 * even after flushing the inode, there can still be delalloc
5473 * blocks on the inode beyond EOF due to speculative
5474 * preallocation. These are not removed until the release
5475 * function is called or the inode is inactivated. Hence we
5476 * cannot assert here that ip->i_delayed_blks == 0.
5477 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479
5480 lock = xfs_ilock_map_shared(ip);
5481
5482 /*
5483 * Don't let nex be bigger than the number of extents
5484 * we can have assuming alternating holes and real extents.
5485 */
5486 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5487 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5488
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005489 bmapi_flags = xfs_bmapi_aflag(whichfork);
5490 if (!(iflags & BMV_IF_PREALLOC))
5491 bmapi_flags |= XFS_BMAPI_IGSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492
5493 /*
5494 * Allocate enough space to handle "subnex" maps at a time.
5495 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005496 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 subnex = 16;
Christoph Hellwigca35dcd2009-07-18 18:14:54 -04005498 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005499 if (!map)
5500 goto out_unlock_ilock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501
5502 bmv->bmv_entries = 0;
5503
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005504 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5505 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5506 error = 0;
5507 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 }
5509
5510 nexleft = nex;
5511
5512 do {
5513 nmap = (nexleft > subnex) ? subnex : nexleft;
5514 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5515 XFS_BB_TO_FSB(mp, bmv->bmv_length),
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005516 bmapi_flags, NULL, 0, map, &nmap,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005517 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 if (error)
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005519 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 ASSERT(nmap <= subnex);
5521
5522 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005523 out[cur_ext].bmv_oflags = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005524 if (map[i].br_state == XFS_EXT_UNWRITTEN)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005525 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005526 else if (map[i].br_startblock == DELAYSTARTBLOCK)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005527 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5528 out[cur_ext].bmv_offset =
5529 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5530 out[cur_ext].bmv_length =
5531 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5532 out[cur_ext].bmv_unused1 = 0;
5533 out[cur_ext].bmv_unused2 = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005534 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5535 (map[i].br_startblock != DELAYSTARTBLOCK));
Yingping Lu9af0a702005-11-02 15:09:54 +11005536 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005537 whichfork == XFS_ATTR_FORK) {
5538 /* came to the end of attribute fork */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005539 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005540 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005542
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005543 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5544 prealloced, bmvend,
5545 map[i].br_startblock))
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005546 goto out_free_map;
5547
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005548 bmv->bmv_offset =
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005549 out[cur_ext].bmv_offset +
5550 out[cur_ext].bmv_length;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005551 bmv->bmv_length =
5552 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
Tao Ma9af25462010-08-30 02:44:03 +00005553
5554 /*
5555 * In case we don't want to return the hole,
5556 * don't increase cur_ext so that we can reuse
5557 * it in the next loop.
5558 */
5559 if ((iflags & BMV_IF_NO_HOLES) &&
5560 map[i].br_startblock == HOLESTARTBLOCK) {
5561 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5562 continue;
5563 }
5564
5565 nexleft--;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005566 bmv->bmv_entries++;
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005567 cur_ext++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 }
5569 } while (nmap && nexleft && bmv->bmv_length);
5570
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005571 out_free_map:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10005572 kmem_free(map);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005573 out_unlock_ilock:
5574 xfs_iunlock_map_shared(ip, lock);
5575 out_unlock_iolock:
5576 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005577
5578 for (i = 0; i < cur_ext; i++) {
5579 int full = 0; /* user array is full */
5580
5581 /* format results & advance arg */
5582 error = formatter(&arg, &out[i], &full);
5583 if (error || full)
5584 break;
5585 }
5586
Felix Blyakher7747a0b2009-06-11 17:07:28 -05005587 kmem_free(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 return error;
5589}
5590
5591/*
5592 * Check the last inode extent to determine whether this allocation will result
5593 * in blocks being allocated at the end of the file. When we allocate new data
5594 * blocks at the end of the file which do not start at the previous data block,
5595 * we will try to align the new blocks at stripe unit boundaries.
5596 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10005597STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598xfs_bmap_isaeof(
5599 xfs_inode_t *ip, /* incore inode pointer */
5600 xfs_fileoff_t off, /* file offset in fsblocks */
5601 int whichfork, /* data or attribute fork */
5602 char *aeof) /* return value */
5603{
5604 int error; /* error return value */
5605 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005606 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005607 xfs_extnum_t nextents; /* number of file extents */
5608 xfs_bmbt_irec_t s; /* expanded extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609
5610 ASSERT(whichfork == XFS_DATA_FORK);
5611 ifp = XFS_IFORK_PTR(ip, whichfork);
5612 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5613 (error = xfs_iread_extents(NULL, ip, whichfork)))
5614 return error;
5615 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5616 if (nextents == 0) {
5617 *aeof = 1;
5618 return 0;
5619 }
5620 /*
5621 * Go to the last extent
5622 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005623 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 xfs_bmbt_get_all(lastrec, &s);
5625 /*
5626 * Check we are allocating in the last extent (for delayed allocations)
5627 * or past the last extent for non-delayed allocations.
5628 */
5629 *aeof = (off >= s.br_startoff &&
5630 off < s.br_startoff + s.br_blockcount &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06005631 isnullstartblock(s.br_startblock)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 off >= s.br_startoff + s.br_blockcount;
5633 return 0;
5634}
5635
5636/*
5637 * Check if the endoff is outside the last extent. If so the caller will grow
5638 * the allocation to a stripe unit boundary.
5639 */
5640int /* error */
5641xfs_bmap_eof(
5642 xfs_inode_t *ip, /* incore inode pointer */
5643 xfs_fileoff_t endoff, /* file offset in fsblocks */
5644 int whichfork, /* data or attribute fork */
5645 int *eof) /* result value */
5646{
5647 xfs_fsblock_t blockcount; /* extent block count */
5648 int error; /* error return value */
5649 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005650 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005651 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 xfs_fileoff_t startoff; /* extent starting file offset */
5653
5654 ASSERT(whichfork == XFS_DATA_FORK);
5655 ifp = XFS_IFORK_PTR(ip, whichfork);
5656 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5657 (error = xfs_iread_extents(NULL, ip, whichfork)))
5658 return error;
5659 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5660 if (nextents == 0) {
5661 *eof = 1;
5662 return 0;
5663 }
5664 /*
5665 * Go to the last extent
5666 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005667 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 startoff = xfs_bmbt_get_startoff(lastrec);
5669 blockcount = xfs_bmbt_get_blockcount(lastrec);
5670 *eof = endoff >= startoff + blockcount;
5671 return 0;
5672}
5673
5674#ifdef DEBUG
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005675STATIC struct xfs_buf *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676xfs_bmap_get_bp(
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005677 struct xfs_btree_cur *cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 xfs_fsblock_t bno)
5679{
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005680 struct xfs_log_item_desc *lidp;
5681 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
5683 if (!cur)
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005684 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005686 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5687 if (!cur->bc_bufs[i])
5688 break;
5689 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5690 return cur->bc_bufs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005693 /* Chase down all the log items to see if the bp is there */
5694 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
Christoph Hellwige98c4142010-06-23 18:11:15 +10005695 struct xfs_buf_log_item *bip;
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005696 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5697 if (bip->bli_item.li_type == XFS_LI_BUF &&
5698 XFS_BUF_ADDR(bip->bli_buf) == bno)
5699 return bip->bli_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 }
Christoph Hellwige98c4142010-06-23 18:11:15 +10005701
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005702 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703}
5704
Hannes Eder3180e662009-03-04 19:34:10 +01005705STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706xfs_check_block(
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005707 struct xfs_btree_block *block,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 xfs_mount_t *mp,
5709 int root,
5710 short sz)
5711{
5712 int i, j, dmxr;
Christoph Hellwig576039c2006-09-28 10:58:06 +10005713 __be64 *pp, *thispa; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 xfs_bmbt_key_t *prevp, *keyp;
5715
Christoph Hellwig16259e72005-11-02 15:11:25 +11005716 ASSERT(be16_to_cpu(block->bb_level) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717
5718 prevp = NULL;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005719 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 dmxr = mp->m_bmap_dmxr[0];
Christoph Hellwig136341b2008-10-30 17:11:40 +11005721 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
5723 if (prevp) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005724 ASSERT(be64_to_cpu(prevp->br_startoff) <
5725 be64_to_cpu(keyp->br_startoff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 }
5727 prevp = keyp;
5728
5729 /*
5730 * Compare the block numbers to see if there are dups.
5731 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11005732 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005733 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005734 else
5735 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5736
Christoph Hellwig16259e72005-11-02 15:11:25 +11005737 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11005738 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005739 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005740 else
5741 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005742 if (*thispa == *pp) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11005743 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005744 __func__, j, i,
Christoph Hellwig576039c2006-09-28 10:58:06 +10005745 (unsigned long long)be64_to_cpu(*thispa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 panic("%s: ptrs are equal in node\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005747 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 }
5749 }
5750 }
5751}
5752
5753/*
5754 * Check that the extents for the inode ip are in the right order in all
5755 * btree leaves.
5756 */
5757
5758STATIC void
5759xfs_bmap_check_leaf_extents(
5760 xfs_btree_cur_t *cur, /* btree cursor or null */
5761 xfs_inode_t *ip, /* incore inode pointer */
5762 int whichfork) /* data or attr fork */
5763{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005764 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 xfs_fsblock_t bno; /* block # of "block" */
5766 xfs_buf_t *bp; /* buffer for "block" */
5767 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005768 xfs_extnum_t i=0, j; /* index into the extents list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 xfs_ifork_t *ifp; /* fork structure */
5770 int level; /* btree level, for checking */
5771 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10005772 __be64 *pp; /* pointer to block address */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005773 xfs_bmbt_rec_t *ep; /* pointer to current extent */
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005774 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005775 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 int bp_release = 0;
5777
5778 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5779 return;
5780 }
5781
5782 bno = NULLFSBLOCK;
5783 mp = ip->i_mount;
5784 ifp = XFS_IFORK_PTR(ip, whichfork);
5785 block = ifp->if_broot;
5786 /*
5787 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5788 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11005789 level = be16_to_cpu(block->bb_level);
5790 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11005792 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005793 bno = be64_to_cpu(*pp);
5794
5795 ASSERT(bno != NULLDFSBNO);
5796 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5797 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5798
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 /*
5800 * Go down the tree until leaf level is reached, following the first
5801 * pointer (leftmost) at each level.
5802 */
5803 while (level-- > 0) {
5804 /* See if buf is in cur first */
5805 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5806 if (bp) {
5807 bp_release = 0;
5808 } else {
5809 bp_release = 1;
5810 }
5811 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5812 XFS_BMAP_BTREE_REF)))
5813 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005814 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11005816 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 error0);
5818 if (level == 0)
5819 break;
5820
5821 /*
5822 * Check this block for basic sanity (increasing keys and
5823 * no duplicate blocks).
5824 */
5825
5826 xfs_check_block(block, mp, 0, 0);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005827 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005828 bno = be64_to_cpu(*pp);
5829 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 if (bp_release) {
5831 bp_release = 0;
5832 xfs_trans_brelse(NULL, bp);
5833 }
5834 }
5835
5836 /*
5837 * Here with bp and block set to the leftmost leaf node in the tree.
5838 */
5839 i = 0;
5840
5841 /*
5842 * Loop over all leaf nodes checking that all extents are in the right order.
5843 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 xfs_fsblock_t nextbno;
5846 xfs_extnum_t num_recs;
5847
5848
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005849 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850
5851 /*
5852 * Read-ahead the next leaf block, if any.
5853 */
5854
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005855 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
5857 /*
5858 * Check all the extents to make sure they are OK.
5859 * If we had a previous block, the last entry should
5860 * conform with the first entry in this one.
5861 */
5862
Christoph Hellwig136341b2008-10-30 17:11:40 +11005863 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005864 if (i) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005865 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
5866 xfs_bmbt_disk_get_blockcount(&last) <=
5867 xfs_bmbt_disk_get_startoff(ep));
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005868 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005869 for (j = 1; j < num_recs; j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11005870 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005871 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
5872 xfs_bmbt_disk_get_blockcount(ep) <=
5873 xfs_bmbt_disk_get_startoff(nextp));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005874 ep = nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005877 last = *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 i += num_recs;
5879 if (bp_release) {
5880 bp_release = 0;
5881 xfs_trans_brelse(NULL, bp);
5882 }
5883 bno = nextbno;
5884 /*
5885 * If we've reached the end, stop.
5886 */
5887 if (bno == NULLFSBLOCK)
5888 break;
5889
5890 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5891 if (bp) {
5892 bp_release = 0;
5893 } else {
5894 bp_release = 1;
5895 }
5896 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5897 XFS_BMAP_BTREE_REF)))
5898 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005899 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 }
5901 if (bp_release) {
5902 bp_release = 0;
5903 xfs_trans_brelse(NULL, bp);
5904 }
5905 return;
5906
5907error0:
Dave Chinner0b932cc2011-03-07 10:08:35 +11005908 xfs_warn(mp, "%s: at error0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 if (bp_release)
5910 xfs_trans_brelse(NULL, bp);
5911error_norelse:
Dave Chinner0b932cc2011-03-07 10:08:35 +11005912 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005913 __func__, i);
5914 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 return;
5916}
5917#endif
5918
5919/*
5920 * Count fsblocks of the given fork.
5921 */
5922int /* error */
5923xfs_bmap_count_blocks(
5924 xfs_trans_t *tp, /* transaction pointer */
5925 xfs_inode_t *ip, /* incore inode */
5926 int whichfork, /* data or attr fork */
5927 int *count) /* out: count of blocks */
5928{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005929 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 xfs_fsblock_t bno; /* block # of "block" */
5931 xfs_ifork_t *ifp; /* fork structure */
5932 int level; /* btree level, for checking */
5933 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10005934 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935
5936 bno = NULLFSBLOCK;
5937 mp = ip->i_mount;
5938 ifp = XFS_IFORK_PTR(ip, whichfork);
5939 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
Ruben Porrasc94312d2008-08-13 16:52:25 +10005940 xfs_bmap_count_leaves(ifp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
Ruben Porrasc94312d2008-08-13 16:52:25 +10005942 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 return 0;
5944 }
5945
5946 /*
5947 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5948 */
5949 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11005950 level = be16_to_cpu(block->bb_level);
5951 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11005952 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005953 bno = be64_to_cpu(*pp);
5954 ASSERT(bno != NULLDFSBNO);
5955 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5956 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005958 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
5960 mp);
5961 return XFS_ERROR(EFSCORRUPTED);
5962 }
5963
5964 return 0;
5965}
5966
5967/*
5968 * Recursively walks each level of a btree
5969 * to count total fsblocks is use.
5970 */
David Chinnera8272ce2007-11-23 16:28:09 +11005971STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972xfs_bmap_count_tree(
5973 xfs_mount_t *mp, /* file system mount point */
5974 xfs_trans_t *tp, /* transaction pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005975 xfs_ifork_t *ifp, /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 xfs_fsblock_t blockno, /* file system block number */
5977 int levelin, /* level in btree */
5978 int *count) /* Count of blocks */
5979{
5980 int error;
5981 xfs_buf_t *bp, *nbp;
5982 int level = levelin;
Christoph Hellwig576039c2006-09-28 10:58:06 +10005983 __be64 *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 xfs_fsblock_t bno = blockno;
5985 xfs_fsblock_t nextbno;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005986 struct xfs_btree_block *block, *nextblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988
5989 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
5990 return error;
5991 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005992 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993
5994 if (--level) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +02005995 /* Not at node above leaves, count this level of nodes */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005996 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 while (nextbno != NULLFSBLOCK) {
5998 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
5999 0, &nbp, XFS_BMAP_BTREE_REF)))
6000 return error;
6001 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006002 nextblock = XFS_BUF_TO_BLOCK(nbp);
6003 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 xfs_trans_brelse(tp, nbp);
6005 }
6006
6007 /* Dive to the next level */
Christoph Hellwig136341b2008-10-30 17:11:40 +11006008 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006009 bno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 if (unlikely((error =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006011 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 xfs_trans_brelse(tp, bp);
6013 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6014 XFS_ERRLEVEL_LOW, mp);
6015 return XFS_ERROR(EFSCORRUPTED);
6016 }
6017 xfs_trans_brelse(tp, bp);
6018 } else {
6019 /* count all level 1 nodes and their leaves */
6020 for (;;) {
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006021 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Christoph Hellwig16259e72005-11-02 15:11:25 +11006022 numrecs = be16_to_cpu(block->bb_numrecs);
Christoph Hellwig136341b2008-10-30 17:11:40 +11006023 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 xfs_trans_brelse(tp, bp);
6025 if (nextbno == NULLFSBLOCK)
6026 break;
6027 bno = nextbno;
6028 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6029 XFS_BMAP_BTREE_REF)))
6030 return error;
6031 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006032 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 }
6034 }
6035 return 0;
6036}
6037
6038/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006039 * Count leaf blocks given a range of extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006041STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006043 xfs_ifork_t *ifp,
6044 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 int numrecs,
6046 int *count)
6047{
6048 int b;
6049
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006050 for (b = 0; b < numrecs; b++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006051 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
Yingping Lu91e11082005-11-02 15:10:24 +11006052 *count += xfs_bmbt_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006053 }
Yingping Lu91e11082005-11-02 15:10:24 +11006054}
6055
6056/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006057 * Count leaf blocks given a range of extent records originally
6058 * in btree format.
Yingping Lu91e11082005-11-02 15:10:24 +11006059 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006060STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +11006061xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +11006062 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006063 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +11006064 int numrecs,
6065 int *count)
6066{
6067 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006068 xfs_bmbt_rec_t *frp;
Yingping Lu91e11082005-11-02 15:10:24 +11006069
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006070 for (b = 1; b <= numrecs; b++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11006071 frp = XFS_BMBT_REC_ADDR(mp, block, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 *count += xfs_bmbt_disk_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074}
Dave Chinnerc726de42010-11-30 15:14:39 +11006075
6076/*
6077 * dead simple method of punching delalyed allocation blocks from a range in
6078 * the inode. Walks a block at a time so will be slow, but is only executed in
6079 * rare error cases so the overhead is not critical. This will alays punch out
6080 * both the start and end blocks, even if the ranges only partially overlap
6081 * them, so it is up to the caller to ensure that partial blocks are not
6082 * passed in.
6083 */
6084int
6085xfs_bmap_punch_delalloc_range(
6086 struct xfs_inode *ip,
6087 xfs_fileoff_t start_fsb,
6088 xfs_fileoff_t length)
6089{
6090 xfs_fileoff_t remaining = length;
6091 int error = 0;
6092
6093 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6094
6095 do {
6096 int done;
6097 xfs_bmbt_irec_t imap;
6098 int nimaps = 1;
6099 xfs_fsblock_t firstblock;
6100 xfs_bmap_free_t flist;
6101
6102 /*
6103 * Map the range first and check that it is a delalloc extent
6104 * before trying to unmap the range. Otherwise we will be
6105 * trying to remove a real extent (which requires a
6106 * transaction) or a hole, which is probably a bad idea...
6107 */
6108 error = xfs_bmapi(NULL, ip, start_fsb, 1,
6109 XFS_BMAPI_ENTIRE, NULL, 0, &imap,
6110 &nimaps, NULL);
6111
6112 if (error) {
6113 /* something screwed, just bail */
6114 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner53487782011-03-07 10:05:35 +11006115 xfs_alert(ip->i_mount,
Dave Chinnerc726de42010-11-30 15:14:39 +11006116 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6117 ip->i_ino, start_fsb);
6118 }
6119 break;
6120 }
6121 if (!nimaps) {
6122 /* nothing there */
6123 goto next_block;
6124 }
6125 if (imap.br_startblock != DELAYSTARTBLOCK) {
6126 /* been converted, ignore */
6127 goto next_block;
6128 }
6129 WARN_ON(imap.br_blockcount == 0);
6130
6131 /*
6132 * Note: while we initialise the firstblock/flist pair, they
6133 * should never be used because blocks should never be
6134 * allocated or freed for a delalloc extent and hence we need
6135 * don't cancel or finish them after the xfs_bunmapi() call.
6136 */
6137 xfs_bmap_init(&flist, &firstblock);
6138 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6139 &flist, &done);
6140 if (error)
6141 break;
6142
6143 ASSERT(!flist.xbf_count && !flist.xbf_first);
6144next_block:
6145 start_fsb++;
6146 remaining--;
6147 } while(remaining > 0);
6148
6149 return error;
6150}