blob: f90dadd5a96877ff9975ecaaf2fb260863f7d089 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_dir2_sf.h"
33#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_itable.h"
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100038#include "xfs_dir2_data.h"
39#include "xfs_dir2_leaf.h"
40#include "xfs_dir2_block.h"
Nathan Scotta844f452005-11-02 14:38:42 +110041#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_extfree_item.h"
43#include "xfs_alloc.h"
44#include "xfs_bmap.h"
45#include "xfs_rtalloc.h"
46#include "xfs_error.h"
Nathan Scottd8cc8902005-11-02 10:34:53 +110047#include "xfs_attr_leaf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "xfs_rw.h"
49#include "xfs_quota.h"
50#include "xfs_trans_space.h"
51#include "xfs_buf_item.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100052#include "xfs_filestream.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100053#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000054#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56
57#ifdef DEBUG
58STATIC void
59xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
60#endif
61
62kmem_zone_t *xfs_bmap_free_item_zone;
63
64/*
65 * Prototypes for internal bmap routines.
66 */
67
68
69/*
70 * Called from xfs_bmap_add_attrfork to handle extents format files.
71 */
72STATIC int /* error */
73xfs_bmap_add_attrfork_extents(
74 xfs_trans_t *tp, /* transaction pointer */
75 xfs_inode_t *ip, /* incore inode pointer */
76 xfs_fsblock_t *firstblock, /* first block allocated */
77 xfs_bmap_free_t *flist, /* blocks to free at commit */
78 int *flags); /* inode logging flags */
79
80/*
81 * Called from xfs_bmap_add_attrfork to handle local format files.
82 */
83STATIC int /* error */
84xfs_bmap_add_attrfork_local(
85 xfs_trans_t *tp, /* transaction pointer */
86 xfs_inode_t *ip, /* incore inode pointer */
87 xfs_fsblock_t *firstblock, /* first block allocated */
88 xfs_bmap_free_t *flist, /* blocks to free at commit */
89 int *flags); /* inode logging flags */
90
91/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +110092 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * after allocating space (or doing a delayed allocation).
94 */
95STATIC int /* error */
96xfs_bmap_add_extent(
97 xfs_inode_t *ip, /* incore inode pointer */
98 xfs_extnum_t idx, /* extent number to update/insert */
99 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100100 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 xfs_fsblock_t *first, /* pointer to firstblock variable */
102 xfs_bmap_free_t *flist, /* list of extents to be freed */
103 int *logflagsp, /* inode logging flags */
104 int whichfork, /* data or attr fork */
105 int rsvd); /* OK to allocate reserved blocks */
106
107/*
108 * Called by xfs_bmap_add_extent to handle cases converting a delayed
109 * allocation to a real allocation.
110 */
111STATIC int /* error */
112xfs_bmap_add_extent_delay_real(
113 xfs_inode_t *ip, /* incore inode pointer */
114 xfs_extnum_t idx, /* extent number to update/insert */
115 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100116 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
118 xfs_fsblock_t *first, /* pointer to firstblock variable */
119 xfs_bmap_free_t *flist, /* list of extents to be freed */
120 int *logflagsp, /* inode logging flags */
121 int rsvd); /* OK to allocate reserved blocks */
122
123/*
124 * Called by xfs_bmap_add_extent to handle cases converting a hole
125 * to a delayed allocation.
126 */
127STATIC int /* error */
128xfs_bmap_add_extent_hole_delay(
129 xfs_inode_t *ip, /* incore inode pointer */
130 xfs_extnum_t idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100131 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int *logflagsp,/* inode logging flags */
133 int rsvd); /* OK to allocate reserved blocks */
134
135/*
136 * Called by xfs_bmap_add_extent to handle cases converting a hole
137 * to a real allocation.
138 */
139STATIC int /* error */
140xfs_bmap_add_extent_hole_real(
141 xfs_inode_t *ip, /* incore inode pointer */
142 xfs_extnum_t idx, /* extent number to update/insert */
143 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100144 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int *logflagsp, /* inode logging flags */
146 int whichfork); /* data or attr fork */
147
148/*
149 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
150 * allocation to a real allocation or vice versa.
151 */
152STATIC int /* error */
153xfs_bmap_add_extent_unwritten_real(
154 xfs_inode_t *ip, /* incore inode pointer */
155 xfs_extnum_t idx, /* extent number to update/insert */
156 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100157 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000158 int *logflagsp); /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160/*
161 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
162 * It figures out where to ask the underlying allocator to put the new extent.
163 */
164STATIC int /* error */
165xfs_bmap_alloc(
166 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
167
168/*
169 * Transform a btree format file with only one leaf node, where the
170 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100171 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * give up the space for the btree root and pitch the leaf block.
173 */
174STATIC int /* error */
175xfs_bmap_btree_to_extents(
176 xfs_trans_t *tp, /* transaction pointer */
177 xfs_inode_t *ip, /* incore inode pointer */
178 xfs_btree_cur_t *cur, /* btree cursor */
179 int *logflagsp, /* inode logging flags */
180 int whichfork); /* data or attr fork */
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100183 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * after removing space (or undoing a delayed allocation).
185 */
186STATIC int /* error */
187xfs_bmap_del_extent(
188 xfs_inode_t *ip, /* incore inode pointer */
189 xfs_trans_t *tp, /* current trans pointer */
190 xfs_extnum_t idx, /* extent number to update/insert */
191 xfs_bmap_free_t *flist, /* list of extents to be freed */
192 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100193 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int *logflagsp,/* inode logging flags */
195 int whichfork, /* data or attr fork */
196 int rsvd); /* OK to allocate reserved blocks */
197
198/*
199 * Remove the entry "free" from the free item list. Prev points to the
200 * previous entry, unless "free" is the head of the list.
201 */
202STATIC void
203xfs_bmap_del_free(
204 xfs_bmap_free_t *flist, /* free item list header */
205 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
206 xfs_bmap_free_item_t *free); /* list item to be freed */
207
208/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 * Convert an extents-format file into a btree-format file.
210 * The new file will have a root block (in the inode) and a single child block.
211 */
212STATIC int /* error */
213xfs_bmap_extents_to_btree(
214 xfs_trans_t *tp, /* transaction pointer */
215 xfs_inode_t *ip, /* incore inode pointer */
216 xfs_fsblock_t *firstblock, /* first-block-allocated */
217 xfs_bmap_free_t *flist, /* blocks freed in xaction */
218 xfs_btree_cur_t **curp, /* cursor returned to caller */
219 int wasdel, /* converting a delayed alloc */
220 int *logflagsp, /* inode logging flags */
221 int whichfork); /* data or attr fork */
222
223/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 * Convert a local file to an extents file.
225 * This code is sort of bogus, since the file data needs to get
226 * logged so it won't be lost. The bmap-level manipulations are ok, though.
227 */
228STATIC int /* error */
229xfs_bmap_local_to_extents(
230 xfs_trans_t *tp, /* transaction pointer */
231 xfs_inode_t *ip, /* incore inode pointer */
232 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
233 xfs_extlen_t total, /* total blocks needed by transaction */
234 int *logflagsp, /* inode logging flags */
235 int whichfork); /* data or attr fork */
236
237/*
238 * Search the extents list for the inode, for the extent containing bno.
239 * If bno lies in a hole, point to the next entry. If bno lies past eof,
240 * *eofp will be set, and *prevp will contain the last entry (null if none).
241 * Else, *lastxp will be set to the index of the found
242 * entry; *gotp will contain the entry.
243 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000244STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245xfs_bmap_search_extents(
246 xfs_inode_t *ip, /* incore inode pointer */
247 xfs_fileoff_t bno, /* block number searched for */
248 int whichfork, /* data or attr fork */
249 int *eofp, /* out: end of file found */
250 xfs_extnum_t *lastxp, /* out: last extent index */
251 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
252 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
253
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000254/*
255 * Check the last inode extent to determine whether this allocation will result
256 * in blocks being allocated at the end of the file. When we allocate new data
257 * blocks at the end of the file which do not start at the previous data block,
258 * we will try to align the new blocks at stripe unit boundaries.
259 */
260STATIC int /* error */
261xfs_bmap_isaeof(
262 xfs_inode_t *ip, /* incore inode pointer */
263 xfs_fileoff_t off, /* file offset in fsblocks */
264 int whichfork, /* data or attribute fork */
265 char *aeof); /* return value */
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/*
268 * Compute the worst-case number of indirect blocks that will be used
269 * for ip's delayed extent of length "len".
270 */
271STATIC xfs_filblks_t
272xfs_bmap_worst_indlen(
273 xfs_inode_t *ip, /* incore inode pointer */
274 xfs_filblks_t len); /* delayed extent length */
275
276#ifdef DEBUG
277/*
278 * Perform various validation checks on the values being returned
279 * from xfs_bmapi().
280 */
281STATIC void
282xfs_bmap_validate_ret(
283 xfs_fileoff_t bno,
284 xfs_filblks_t len,
285 int flags,
286 xfs_bmbt_irec_t *mval,
287 int nmap,
288 int ret_nmap);
289#else
290#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
291#endif /* DEBUG */
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293STATIC int
294xfs_bmap_count_tree(
295 xfs_mount_t *mp,
296 xfs_trans_t *tp,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100297 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 xfs_fsblock_t blockno,
299 int levelin,
300 int *count);
301
Ruben Porrasc94312d2008-08-13 16:52:25 +1000302STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100304 xfs_ifork_t *ifp,
305 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int numrecs,
307 int *count);
308
Ruben Porrasc94312d2008-08-13 16:52:25 +1000309STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +1100310xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +1100311 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100312 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +1100313 int numrecs,
314 int *count);
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/*
317 * Bmap internal routines.
318 */
319
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100320STATIC int /* error */
321xfs_bmbt_lookup_eq(
322 struct xfs_btree_cur *cur,
323 xfs_fileoff_t off,
324 xfs_fsblock_t bno,
325 xfs_filblks_t len,
326 int *stat) /* success/failure */
327{
328 cur->bc_rec.b.br_startoff = off;
329 cur->bc_rec.b.br_startblock = bno;
330 cur->bc_rec.b.br_blockcount = len;
331 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
332}
333
334STATIC int /* error */
335xfs_bmbt_lookup_ge(
336 struct xfs_btree_cur *cur,
337 xfs_fileoff_t off,
338 xfs_fsblock_t bno,
339 xfs_filblks_t len,
340 int *stat) /* success/failure */
341{
342 cur->bc_rec.b.br_startoff = off;
343 cur->bc_rec.b.br_startblock = bno;
344 cur->bc_rec.b.br_blockcount = len;
345 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
346}
347
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100348/*
349* Update the record referred to by cur to the value given
350 * by [off, bno, len, state].
351 * This either works (return 0) or gets an EFSCORRUPTED error.
352 */
353STATIC int
354xfs_bmbt_update(
355 struct xfs_btree_cur *cur,
356 xfs_fileoff_t off,
357 xfs_fsblock_t bno,
358 xfs_filblks_t len,
359 xfs_exntst_t state)
360{
361 union xfs_btree_rec rec;
362
363 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
364 return xfs_btree_update(cur, &rec);
365}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/*
368 * Called from xfs_bmap_add_attrfork to handle btree format files.
369 */
370STATIC int /* error */
371xfs_bmap_add_attrfork_btree(
372 xfs_trans_t *tp, /* transaction pointer */
373 xfs_inode_t *ip, /* incore inode pointer */
374 xfs_fsblock_t *firstblock, /* first block allocated */
375 xfs_bmap_free_t *flist, /* blocks to free at commit */
376 int *flags) /* inode logging flags */
377{
378 xfs_btree_cur_t *cur; /* btree cursor */
379 int error; /* error return value */
380 xfs_mount_t *mp; /* file system mount struct */
381 int stat; /* newroot status */
382
383 mp = ip->i_mount;
384 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
385 *flags |= XFS_ILOG_DBROOT;
386 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100387 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 cur->bc_private.b.flist = flist;
389 cur->bc_private.b.firstblock = *firstblock;
390 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
391 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000392 /* must be at least one entry */
393 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100394 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 goto error0;
396 if (stat == 0) {
397 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
398 return XFS_ERROR(ENOSPC);
399 }
400 *firstblock = cur->bc_private.b.firstblock;
401 cur->bc_private.b.allocated = 0;
402 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
403 }
404 return 0;
405error0:
406 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
407 return error;
408}
409
410/*
411 * Called from xfs_bmap_add_attrfork to handle extents format files.
412 */
413STATIC int /* error */
414xfs_bmap_add_attrfork_extents(
415 xfs_trans_t *tp, /* transaction pointer */
416 xfs_inode_t *ip, /* incore inode pointer */
417 xfs_fsblock_t *firstblock, /* first block allocated */
418 xfs_bmap_free_t *flist, /* blocks to free at commit */
419 int *flags) /* inode logging flags */
420{
421 xfs_btree_cur_t *cur; /* bmap btree cursor */
422 int error; /* error return value */
423
424 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
425 return 0;
426 cur = NULL;
427 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
428 flags, XFS_DATA_FORK);
429 if (cur) {
430 cur->bc_private.b.allocated = 0;
431 xfs_btree_del_cursor(cur,
432 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
433 }
434 return error;
435}
436
437/*
438 * Called from xfs_bmap_add_attrfork to handle local format files.
439 */
440STATIC int /* error */
441xfs_bmap_add_attrfork_local(
442 xfs_trans_t *tp, /* transaction pointer */
443 xfs_inode_t *ip, /* incore inode pointer */
444 xfs_fsblock_t *firstblock, /* first block allocated */
445 xfs_bmap_free_t *flist, /* blocks to free at commit */
446 int *flags) /* inode logging flags */
447{
448 xfs_da_args_t dargs; /* args for dir/attr code */
449 int error; /* error return value */
450 xfs_mount_t *mp; /* mount structure pointer */
451
452 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
453 return 0;
454 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
455 mp = ip->i_mount;
456 memset(&dargs, 0, sizeof(dargs));
457 dargs.dp = ip;
458 dargs.firstblock = firstblock;
459 dargs.flist = flist;
460 dargs.total = mp->m_dirblkfsbs;
461 dargs.whichfork = XFS_DATA_FORK;
462 dargs.trans = tp;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000463 error = xfs_dir2_sf_to_block(&dargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 } else
465 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
466 XFS_DATA_FORK);
467 return error;
468}
469
470/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100471 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 * after allocating space (or doing a delayed allocation).
473 */
474STATIC int /* error */
475xfs_bmap_add_extent(
476 xfs_inode_t *ip, /* incore inode pointer */
477 xfs_extnum_t idx, /* extent number to update/insert */
478 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100479 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 xfs_fsblock_t *first, /* pointer to firstblock variable */
481 xfs_bmap_free_t *flist, /* list of extents to be freed */
482 int *logflagsp, /* inode logging flags */
483 int whichfork, /* data or attr fork */
484 int rsvd) /* OK to use reserved data blocks */
485{
486 xfs_btree_cur_t *cur; /* btree cursor or null */
487 xfs_filblks_t da_new; /* new count del alloc blocks used */
488 xfs_filblks_t da_old; /* old count del alloc blocks used */
489 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 xfs_ifork_t *ifp; /* inode fork ptr */
491 int logflags; /* returned value */
492 xfs_extnum_t nextents; /* number of extents in file now */
493
494 XFS_STATS_INC(xs_add_exlist);
495 cur = *curp;
496 ifp = XFS_IFORK_PTR(ip, whichfork);
497 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
498 ASSERT(idx <= nextents);
499 da_old = da_new = 0;
500 error = 0;
501 /*
502 * This is the first extent added to a new/empty file.
503 * Special case this one, so other routines get to assume there are
504 * already extents in the list.
505 */
506 if (nextents == 0) {
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000507 xfs_iext_insert(ip, 0, 1, new,
508 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 ASSERT(cur == NULL);
511 ifp->if_lastex = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600512 if (!isnullstartblock(new->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600514 logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 } else
516 logflags = 0;
517 }
518 /*
519 * Any kind of new delayed allocation goes here.
520 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600521 else if (isnullstartblock(new->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (cur)
523 ASSERT((cur->bc_private.b.flags &
524 XFS_BTCUR_BPRV_WASDEL) == 0);
Eric Sandeene9ed9d22007-05-08 13:48:56 +1000525 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000526 &logflags, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 goto done;
528 }
529 /*
530 * Real allocation off the end of the file.
531 */
532 else if (idx == nextents) {
533 if (cur)
534 ASSERT((cur->bc_private.b.flags &
535 XFS_BTCUR_BPRV_WASDEL) == 0);
536 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000537 &logflags, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 goto done;
539 } else {
540 xfs_bmbt_irec_t prev; /* old extent at offset idx */
541
542 /*
543 * Get the record referred to by idx.
544 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100545 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /*
547 * If it's a real allocation record, and the new allocation ends
548 * after the start of the referred to record, then we're filling
549 * in a delayed or unwritten allocation with a real one, or
550 * converting real back to unwritten.
551 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600552 if (!isnullstartblock(new->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 new->br_startoff + new->br_blockcount > prev.br_startoff) {
554 if (prev.br_state != XFS_EXT_UNWRITTEN &&
Eric Sandeen9d87c312009-01-14 23:22:07 -0600555 isnullstartblock(prev.br_startblock)) {
556 da_old = startblockval(prev.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (cur)
558 ASSERT(cur->bc_private.b.flags &
559 XFS_BTCUR_BPRV_WASDEL);
560 if ((error = xfs_bmap_add_extent_delay_real(ip,
561 idx, &cur, new, &da_new, first, flist,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000562 &logflags, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 goto done;
564 } else if (new->br_state == XFS_EXT_NORM) {
565 ASSERT(new->br_state == XFS_EXT_NORM);
566 if ((error = xfs_bmap_add_extent_unwritten_real(
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000567 ip, idx, &cur, new, &logflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto done;
569 } else {
570 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
571 if ((error = xfs_bmap_add_extent_unwritten_real(
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000572 ip, idx, &cur, new, &logflags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 goto done;
574 }
575 ASSERT(*curp == cur || *curp == NULL);
576 }
577 /*
578 * Otherwise we're filling in a hole with an allocation.
579 */
580 else {
581 if (cur)
582 ASSERT((cur->bc_private.b.flags &
583 XFS_BTCUR_BPRV_WASDEL) == 0);
584 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000585 new, &logflags, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto done;
587 }
588 }
589
590 ASSERT(*curp == cur || *curp == NULL);
591 /*
592 * Convert to a btree if necessary.
593 */
594 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
595 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
596 int tmp_logflags; /* partial log flag return val */
597
598 ASSERT(cur == NULL);
599 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
600 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
601 logflags |= tmp_logflags;
602 if (error)
603 goto done;
604 }
605 /*
606 * Adjust for changes in reserved delayed indirect blocks.
607 * Nothing to do for disk quotas here.
608 */
609 if (da_old || da_new) {
610 xfs_filblks_t nblks;
611
612 nblks = da_new;
613 if (cur)
614 nblks += cur->bc_private.b.allocated;
615 ASSERT(nblks <= da_old);
616 if (nblks < da_old)
617 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100618 (int64_t)(da_old - nblks), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620 /*
621 * Clear out the allocated field, done with it now in any case.
622 */
623 if (cur) {
624 cur->bc_private.b.allocated = 0;
625 *curp = cur;
626 }
627done:
628#ifdef DEBUG
629 if (!error)
630 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
631#endif
632 *logflagsp = logflags;
633 return error;
634}
635
636/*
637 * Called by xfs_bmap_add_extent to handle cases converting a delayed
638 * allocation to a real allocation.
639 */
640STATIC int /* error */
641xfs_bmap_add_extent_delay_real(
642 xfs_inode_t *ip, /* incore inode pointer */
643 xfs_extnum_t idx, /* extent number to update/insert */
644 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100645 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
647 xfs_fsblock_t *first, /* pointer to firstblock variable */
648 xfs_bmap_free_t *flist, /* list of extents to be freed */
649 int *logflagsp, /* inode logging flags */
650 int rsvd) /* OK to use reserved data block allocation */
651{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 xfs_btree_cur_t *cur; /* btree cursor */
653 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000654 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100657 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 xfs_fileoff_t new_endoff; /* end offset of new entry */
659 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
660 /* left is 0, right is 1, prev is 2 */
661 int rval=0; /* return value (logging flags) */
662 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000663 xfs_filblks_t temp=0; /* value for dnew calculations */
664 xfs_filblks_t temp2=0;/* value for dnew calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 int tmp_rval; /* partial logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667#define LEFT r[0]
668#define RIGHT r[1]
669#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 /*
672 * Set up a bunch of variables to make the tests simpler.
673 */
674 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100675 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
676 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 xfs_bmbt_get_all(ep, &PREV);
678 new_endoff = new->br_startoff + new->br_blockcount;
679 ASSERT(PREV.br_startoff <= new->br_startoff);
680 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /*
683 * Set flags determining what part of the previous delayed allocation
684 * extent is being replaced by a real allocation.
685 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000686 if (PREV.br_startoff == new->br_startoff)
687 state |= BMAP_LEFT_FILLING;
688 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
689 state |= BMAP_RIGHT_FILLING;
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /*
692 * Check and set flags if this segment has a left neighbor.
693 * Don't set contiguous if the combined extent would be too large.
694 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000695 if (idx > 0) {
696 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100697 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000698
699 if (isnullstartblock(LEFT.br_startblock))
700 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000702
703 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
704 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
705 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
706 LEFT.br_state == new->br_state &&
707 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
708 state |= BMAP_LEFT_CONTIG;
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /*
711 * Check and set flags if this segment has a right neighbor.
712 * Don't set contiguous if the combined extent would be too large.
713 * Also check for all-three-contiguous being too large.
714 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000715 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
716 state |= BMAP_RIGHT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100717 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000718
719 if (isnullstartblock(RIGHT.br_startblock))
720 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000722
723 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
724 new_endoff == RIGHT.br_startoff &&
725 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
726 new->br_state == RIGHT.br_state &&
727 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
728 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
729 BMAP_RIGHT_FILLING)) !=
730 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
731 BMAP_RIGHT_FILLING) ||
732 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
733 <= MAXEXTLEN))
734 state |= BMAP_RIGHT_CONTIG;
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 error = 0;
737 /*
738 * Switch out based on the FILLING and CONTIG state bits.
739 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000740 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
741 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
742 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
743 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /*
745 * Filling in all of a previously delayed allocation extent.
746 * The left and right neighbors are both contiguous with new.
747 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000748 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100749 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 LEFT.br_blockcount + PREV.br_blockcount +
751 RIGHT.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000752 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
753
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000754 xfs_iext_remove(ip, idx, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 ip->i_df.if_lastex = idx - 1;
756 ip->i_d.di_nextents--;
757 if (cur == NULL)
758 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
759 else {
760 rval = XFS_ILOG_CORE;
761 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
762 RIGHT.br_startblock,
763 RIGHT.br_blockcount, &i)))
764 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000765 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100766 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000768 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100769 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000771 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
773 LEFT.br_startblock,
774 LEFT.br_blockcount +
775 PREV.br_blockcount +
776 RIGHT.br_blockcount, LEFT.br_state)))
777 goto done;
778 }
779 *dnew = 0;
780 break;
781
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000782 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /*
784 * Filling in all of a previously delayed allocation extent.
785 * The left neighbor is contiguous, the right is not.
786 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000787 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100788 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000790 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 ip->i_df.if_lastex = idx - 1;
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000793 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (cur == NULL)
795 rval = XFS_ILOG_DEXT;
796 else {
797 rval = 0;
798 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
799 LEFT.br_startblock, LEFT.br_blockcount,
800 &i)))
801 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000802 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
804 LEFT.br_startblock,
805 LEFT.br_blockcount +
806 PREV.br_blockcount, LEFT.br_state)))
807 goto done;
808 }
809 *dnew = 0;
810 break;
811
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000812 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /*
814 * Filling in all of a previously delayed allocation extent.
815 * The right neighbor is contiguous, the left is not.
816 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000817 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 xfs_bmbt_set_startblock(ep, new->br_startblock);
819 xfs_bmbt_set_blockcount(ep,
820 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000821 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 ip->i_df.if_lastex = idx;
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000824 xfs_iext_remove(ip, idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (cur == NULL)
826 rval = XFS_ILOG_DEXT;
827 else {
828 rval = 0;
829 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
830 RIGHT.br_startblock,
831 RIGHT.br_blockcount, &i)))
832 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000833 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
835 new->br_startblock,
836 PREV.br_blockcount +
837 RIGHT.br_blockcount, PREV.br_state)))
838 goto done;
839 }
840 *dnew = 0;
841 break;
842
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000843 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /*
845 * Filling in all of a previously delayed allocation extent.
846 * Neither the left nor right neighbors are contiguous with
847 * the new one.
848 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000849 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000851 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 ip->i_df.if_lastex = idx;
854 ip->i_d.di_nextents++;
855 if (cur == NULL)
856 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
857 else {
858 rval = XFS_ILOG_CORE;
859 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
860 new->br_startblock, new->br_blockcount,
861 &i)))
862 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000863 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100865 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000867 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869 *dnew = 0;
870 break;
871
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000872 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 /*
874 * Filling in the first part of a previous delayed allocation.
875 * The left neighbor is contiguous.
876 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000877 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100878 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 LEFT.br_blockcount + new->br_blockcount);
880 xfs_bmbt_set_startoff(ep,
881 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000882 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000885 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 xfs_bmbt_set_blockcount(ep, temp);
887 ip->i_df.if_lastex = idx - 1;
888 if (cur == NULL)
889 rval = XFS_ILOG_DEXT;
890 else {
891 rval = 0;
892 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
893 LEFT.br_startblock, LEFT.br_blockcount,
894 &i)))
895 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000896 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
898 LEFT.br_startblock,
899 LEFT.br_blockcount +
900 new->br_blockcount,
901 LEFT.br_state)))
902 goto done;
903 }
904 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600905 startblockval(PREV.br_startblock));
906 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000907 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 *dnew = temp;
909 break;
910
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000911 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 /*
913 * Filling in the first part of a previous delayed allocation.
914 * The left neighbor is not contiguous.
915 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000916 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 xfs_bmbt_set_startoff(ep, new_endoff);
918 temp = PREV.br_blockcount - new->br_blockcount;
919 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000920 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 ip->i_df.if_lastex = idx;
922 ip->i_d.di_nextents++;
923 if (cur == NULL)
924 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
925 else {
926 rval = XFS_ILOG_CORE;
927 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
928 new->br_startblock, new->br_blockcount,
929 &i)))
930 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000931 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100933 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000935 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
938 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
939 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
940 first, flist, &cur, 1, &tmp_rval,
941 XFS_DATA_FORK);
942 rval |= tmp_rval;
943 if (error)
944 goto done;
945 }
946 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600947 startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100949 ep = xfs_iext_get_ext(ifp, idx + 1);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600950 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000951 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 *dnew = temp;
953 break;
954
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000955 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /*
957 * Filling in the last part of a previous delayed allocation.
958 * The right neighbor is contiguous with the new allocation.
959 */
960 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000961 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
962 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 xfs_bmbt_set_blockcount(ep, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100964 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
965 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 new->br_blockcount + RIGHT.br_blockcount,
967 RIGHT.br_state);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000968 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 ip->i_df.if_lastex = idx + 1;
970 if (cur == NULL)
971 rval = XFS_ILOG_DEXT;
972 else {
973 rval = 0;
974 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
975 RIGHT.br_startblock,
976 RIGHT.br_blockcount, &i)))
977 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000978 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if ((error = xfs_bmbt_update(cur, new->br_startoff,
980 new->br_startblock,
981 new->br_blockcount +
982 RIGHT.br_blockcount,
983 RIGHT.br_state)))
984 goto done;
985 }
986 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600987 startblockval(PREV.br_startblock));
988 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000989 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 *dnew = temp;
991 break;
992
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000993 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /*
995 * Filling in the last part of a previous delayed allocation.
996 * The right neighbor is not contiguous.
997 */
998 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000999 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001001 xfs_iext_insert(ip, idx + 1, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 ip->i_df.if_lastex = idx + 1;
1003 ip->i_d.di_nextents++;
1004 if (cur == NULL)
1005 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1006 else {
1007 rval = XFS_ILOG_CORE;
1008 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1009 new->br_startblock, new->br_blockcount,
1010 &i)))
1011 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001012 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001014 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001016 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1019 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1020 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1021 first, flist, &cur, 1, &tmp_rval,
1022 XFS_DATA_FORK);
1023 rval |= tmp_rval;
1024 if (error)
1025 goto done;
1026 }
1027 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001028 startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001030 ep = xfs_iext_get_ext(ifp, idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001031 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001032 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 *dnew = temp;
1034 break;
1035
1036 case 0:
1037 /*
1038 * Filling in the middle part of a previous delayed allocation.
1039 * Contiguity is impossible here.
1040 * This case is avoided almost all the time.
1041 */
1042 temp = new->br_startoff - PREV.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001043 trace_xfs_bmap_pre_update(ip, idx, 0, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 xfs_bmbt_set_blockcount(ep, temp);
1045 r[0] = *new;
Lachlan McIlroyd2133712006-11-21 18:55:16 +11001046 r[1].br_state = PREV.br_state;
1047 r[1].br_startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 r[1].br_startoff = new_endoff;
1049 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1050 r[1].br_blockcount = temp2;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001051 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 ip->i_df.if_lastex = idx + 1;
1053 ip->i_d.di_nextents++;
1054 if (cur == NULL)
1055 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1056 else {
1057 rval = XFS_ILOG_CORE;
1058 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1059 new->br_startblock, new->br_blockcount,
1060 &i)))
1061 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001062 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001064 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001066 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1069 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1070 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1071 first, flist, &cur, 1, &tmp_rval,
1072 XFS_DATA_FORK);
1073 rval |= tmp_rval;
1074 if (error)
1075 goto done;
1076 }
1077 temp = xfs_bmap_worst_indlen(ip, temp);
1078 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001079 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 (cur ? cur->bc_private.b.allocated : 0));
1081 if (diff > 0 &&
David Chinner20f4ebf2007-02-10 18:36:10 +11001082 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /*
1084 * Ick gross gag me with a spoon.
1085 */
1086 ASSERT(0); /* want to see if this ever happens! */
1087 while (diff > 0) {
1088 if (temp) {
1089 temp--;
1090 diff--;
1091 if (!diff ||
1092 !xfs_mod_incore_sb(ip->i_mount,
David Chinner20f4ebf2007-02-10 18:36:10 +11001093 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 break;
1095 }
1096 if (temp2) {
1097 temp2--;
1098 diff--;
1099 if (!diff ||
1100 !xfs_mod_incore_sb(ip->i_mount,
David Chinner20f4ebf2007-02-10 18:36:10 +11001101 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 break;
1103 }
1104 }
1105 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001106 ep = xfs_iext_get_ext(ifp, idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001107 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001108 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1109 trace_xfs_bmap_pre_update(ip, idx + 2, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001110 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001111 nullstartblock((int)temp2));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001112 trace_xfs_bmap_post_update(ip, idx + 2, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 *dnew = temp + temp2;
1114 break;
1115
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001116 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1117 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1118 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1119 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1120 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1121 case BMAP_LEFT_CONTIG:
1122 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /*
1124 * These cases are all impossible.
1125 */
1126 ASSERT(0);
1127 }
1128 *curp = cur;
1129done:
1130 *logflagsp = rval;
1131 return error;
1132#undef LEFT
1133#undef RIGHT
1134#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137/*
1138 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1139 * allocation to a real allocation or vice versa.
1140 */
1141STATIC int /* error */
1142xfs_bmap_add_extent_unwritten_real(
1143 xfs_inode_t *ip, /* incore inode pointer */
1144 xfs_extnum_t idx, /* extent number to update/insert */
1145 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001146 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001147 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001150 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001153 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 xfs_fileoff_t new_endoff; /* end offset of new entry */
1155 xfs_exntst_t newext; /* new extent state */
1156 xfs_exntst_t oldext; /* old extent state */
1157 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1158 /* left is 0, right is 1, prev is 2 */
1159 int rval=0; /* return value (logging flags) */
1160 int state = 0;/* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162#define LEFT r[0]
1163#define RIGHT r[1]
1164#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /*
1166 * Set up a bunch of variables to make the tests simpler.
1167 */
1168 error = 0;
1169 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001170 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1171 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 xfs_bmbt_get_all(ep, &PREV);
1173 newext = new->br_state;
1174 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1175 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1176 ASSERT(PREV.br_state == oldext);
1177 new_endoff = new->br_startoff + new->br_blockcount;
1178 ASSERT(PREV.br_startoff <= new->br_startoff);
1179 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /*
1182 * Set flags determining what part of the previous oldext allocation
1183 * extent is being replaced by a newext allocation.
1184 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001185 if (PREV.br_startoff == new->br_startoff)
1186 state |= BMAP_LEFT_FILLING;
1187 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1188 state |= BMAP_RIGHT_FILLING;
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 /*
1191 * Check and set flags if this segment has a left neighbor.
1192 * Don't set contiguous if the combined extent would be too large.
1193 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001194 if (idx > 0) {
1195 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001196 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001197
1198 if (isnullstartblock(LEFT.br_startblock))
1199 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001201
1202 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1203 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1204 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1205 LEFT.br_state == newext &&
1206 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1207 state |= BMAP_LEFT_CONTIG;
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /*
1210 * Check and set flags if this segment has a right neighbor.
1211 * Don't set contiguous if the combined extent would be too large.
1212 * Also check for all-three-contiguous being too large.
1213 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001214 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1215 state |= BMAP_RIGHT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001216 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001217 if (isnullstartblock(RIGHT.br_startblock))
1218 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001220
1221 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1222 new_endoff == RIGHT.br_startoff &&
1223 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1224 newext == RIGHT.br_state &&
1225 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1226 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1227 BMAP_RIGHT_FILLING)) !=
1228 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1229 BMAP_RIGHT_FILLING) ||
1230 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1231 <= MAXEXTLEN))
1232 state |= BMAP_RIGHT_CONTIG;
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 /*
1235 * Switch out based on the FILLING and CONTIG state bits.
1236 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001237 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1238 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1239 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1240 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /*
1242 * Setting all of a previous oldext extent to newext.
1243 * The left and right neighbors are both contiguous with new.
1244 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001245 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001246 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 LEFT.br_blockcount + PREV.br_blockcount +
1248 RIGHT.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001249 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1250
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001251 xfs_iext_remove(ip, idx, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 ip->i_df.if_lastex = idx - 1;
1253 ip->i_d.di_nextents -= 2;
1254 if (cur == NULL)
1255 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1256 else {
1257 rval = XFS_ILOG_CORE;
1258 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1259 RIGHT.br_startblock,
1260 RIGHT.br_blockcount, &i)))
1261 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001262 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001263 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001265 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001266 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001268 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001269 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001271 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001272 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001274 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1276 LEFT.br_startblock,
1277 LEFT.br_blockcount + PREV.br_blockcount +
1278 RIGHT.br_blockcount, LEFT.br_state)))
1279 goto done;
1280 }
1281 break;
1282
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001283 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /*
1285 * Setting all of a previous oldext extent to newext.
1286 * The left neighbor is contiguous, the right is not.
1287 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001288 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001289 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001291 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 ip->i_df.if_lastex = idx - 1;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001294 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 ip->i_d.di_nextents--;
1296 if (cur == NULL)
1297 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1298 else {
1299 rval = XFS_ILOG_CORE;
1300 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1301 PREV.br_startblock, PREV.br_blockcount,
1302 &i)))
1303 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001304 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001305 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001307 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001308 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001310 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1312 LEFT.br_startblock,
1313 LEFT.br_blockcount + PREV.br_blockcount,
1314 LEFT.br_state)))
1315 goto done;
1316 }
1317 break;
1318
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001319 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /*
1321 * Setting all of a previous oldext extent to newext.
1322 * The right neighbor is contiguous, the left is not.
1323 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001324 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 xfs_bmbt_set_blockcount(ep,
1326 PREV.br_blockcount + RIGHT.br_blockcount);
1327 xfs_bmbt_set_state(ep, newext);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001328 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 ip->i_df.if_lastex = idx;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001330 xfs_iext_remove(ip, idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 ip->i_d.di_nextents--;
1332 if (cur == NULL)
1333 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1334 else {
1335 rval = XFS_ILOG_CORE;
1336 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1337 RIGHT.br_startblock,
1338 RIGHT.br_blockcount, &i)))
1339 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001340 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001341 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001343 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001344 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001346 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1348 new->br_startblock,
1349 new->br_blockcount + RIGHT.br_blockcount,
1350 newext)))
1351 goto done;
1352 }
1353 break;
1354
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001355 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /*
1357 * Setting all of a previous oldext extent to newext.
1358 * Neither the left nor right neighbors are contiguous with
1359 * the new one.
1360 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001361 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 xfs_bmbt_set_state(ep, newext);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001363 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 ip->i_df.if_lastex = idx;
1366 if (cur == NULL)
1367 rval = XFS_ILOG_DEXT;
1368 else {
1369 rval = 0;
1370 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1371 new->br_startblock, new->br_blockcount,
1372 &i)))
1373 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001374 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1376 new->br_startblock, new->br_blockcount,
1377 newext)))
1378 goto done;
1379 }
1380 break;
1381
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001382 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 /*
1384 * Setting the first part of a previous oldext extent to newext.
1385 * The left neighbor is contiguous.
1386 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001387 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001388 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 LEFT.br_blockcount + new->br_blockcount);
1390 xfs_bmbt_set_startoff(ep,
1391 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001392 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1393
1394 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 xfs_bmbt_set_startblock(ep,
1396 new->br_startblock + new->br_blockcount);
1397 xfs_bmbt_set_blockcount(ep,
1398 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001399 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 ip->i_df.if_lastex = idx - 1;
1402 if (cur == NULL)
1403 rval = XFS_ILOG_DEXT;
1404 else {
1405 rval = 0;
1406 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1407 PREV.br_startblock, PREV.br_blockcount,
1408 &i)))
1409 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001410 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if ((error = xfs_bmbt_update(cur,
1412 PREV.br_startoff + new->br_blockcount,
1413 PREV.br_startblock + new->br_blockcount,
1414 PREV.br_blockcount - new->br_blockcount,
1415 oldext)))
1416 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001417 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 goto done;
1419 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1420 LEFT.br_startblock,
1421 LEFT.br_blockcount + new->br_blockcount,
1422 LEFT.br_state))
1423 goto done;
1424 }
1425 break;
1426
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001427 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 /*
1429 * Setting the first part of a previous oldext extent to newext.
1430 * The left neighbor is not contiguous.
1431 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001432 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1434 xfs_bmbt_set_startoff(ep, new_endoff);
1435 xfs_bmbt_set_blockcount(ep,
1436 PREV.br_blockcount - new->br_blockcount);
1437 xfs_bmbt_set_startblock(ep,
1438 new->br_startblock + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001439 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1440
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001441 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 ip->i_df.if_lastex = idx;
1443 ip->i_d.di_nextents++;
1444 if (cur == NULL)
1445 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1446 else {
1447 rval = XFS_ILOG_CORE;
1448 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1449 PREV.br_startblock, PREV.br_blockcount,
1450 &i)))
1451 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001452 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if ((error = xfs_bmbt_update(cur,
1454 PREV.br_startoff + new->br_blockcount,
1455 PREV.br_startblock + new->br_blockcount,
1456 PREV.br_blockcount - new->br_blockcount,
1457 oldext)))
1458 goto done;
1459 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001460 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001462 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464 break;
1465
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001466 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 /*
1468 * Setting the last part of a previous oldext extent to newext.
1469 * The right neighbor is contiguous with the new allocation.
1470 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001471 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1472 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 xfs_bmbt_set_blockcount(ep,
1474 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001475 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001476 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1477 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001479 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 ip->i_df.if_lastex = idx + 1;
1482 if (cur == NULL)
1483 rval = XFS_ILOG_DEXT;
1484 else {
1485 rval = 0;
1486 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1487 PREV.br_startblock,
1488 PREV.br_blockcount, &i)))
1489 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001490 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1492 PREV.br_startblock,
1493 PREV.br_blockcount - new->br_blockcount,
1494 oldext)))
1495 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001496 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 goto done;
1498 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1499 new->br_startblock,
1500 new->br_blockcount + RIGHT.br_blockcount,
1501 newext)))
1502 goto done;
1503 }
1504 break;
1505
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001506 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /*
1508 * Setting the last part of a previous oldext extent to newext.
1509 * The right neighbor is not contiguous.
1510 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001511 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 xfs_bmbt_set_blockcount(ep,
1513 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001514 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1515
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001516 xfs_iext_insert(ip, idx + 1, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 ip->i_df.if_lastex = idx + 1;
1518 ip->i_d.di_nextents++;
1519 if (cur == NULL)
1520 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1521 else {
1522 rval = XFS_ILOG_CORE;
1523 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1524 PREV.br_startblock, PREV.br_blockcount,
1525 &i)))
1526 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001527 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1529 PREV.br_startblock,
1530 PREV.br_blockcount - new->br_blockcount,
1531 oldext)))
1532 goto done;
1533 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1534 new->br_startblock, new->br_blockcount,
1535 &i)))
1536 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001537 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001539 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001541 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543 break;
1544
1545 case 0:
1546 /*
1547 * Setting the middle part of a previous oldext extent to
1548 * newext. Contiguity is impossible here.
1549 * One extent becomes three extents.
1550 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001551 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 xfs_bmbt_set_blockcount(ep,
1553 new->br_startoff - PREV.br_startoff);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001554 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 r[0] = *new;
1557 r[1].br_startoff = new_endoff;
1558 r[1].br_blockcount =
1559 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1560 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1561 r[1].br_state = oldext;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001562 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 ip->i_df.if_lastex = idx + 1;
1564 ip->i_d.di_nextents += 2;
1565 if (cur == NULL)
1566 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1567 else {
1568 rval = XFS_ILOG_CORE;
1569 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1570 PREV.br_startblock, PREV.br_blockcount,
1571 &i)))
1572 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001573 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 /* new right extent - oldext */
1575 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1576 r[1].br_startblock, r[1].br_blockcount,
1577 r[1].br_state)))
1578 goto done;
1579 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10001581 cur->bc_rec.b.br_blockcount =
1582 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001583 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001585 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001586 /*
1587 * Reset the cursor to the position of the new extent
1588 * we are about to insert as we can't trust it after
1589 * the previous insert.
1590 */
1591 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1592 new->br_startblock, new->br_blockcount,
1593 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001595 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001597 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001598 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001600 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
1602 break;
1603
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001604 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1605 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1606 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1607 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1608 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1609 case BMAP_LEFT_CONTIG:
1610 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 /*
1612 * These cases are all impossible.
1613 */
1614 ASSERT(0);
1615 }
1616 *curp = cur;
1617done:
1618 *logflagsp = rval;
1619 return error;
1620#undef LEFT
1621#undef RIGHT
1622#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
1624
1625/*
1626 * Called by xfs_bmap_add_extent to handle cases converting a hole
1627 * to a delayed allocation.
1628 */
1629/*ARGSUSED*/
1630STATIC int /* error */
1631xfs_bmap_add_extent_hole_delay(
1632 xfs_inode_t *ip, /* incore inode pointer */
1633 xfs_extnum_t idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001634 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 int *logflagsp, /* inode logging flags */
1636 int rsvd) /* OK to allocate reserved blocks */
1637{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001638 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001639 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1641 xfs_filblks_t newlen=0; /* new indirect size */
1642 xfs_filblks_t oldlen=0; /* old indirect size */
1643 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1644 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001645 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001647 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1648 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001650 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 /*
1653 * Check and set flags if this segment has a left neighbor
1654 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001655 if (idx > 0) {
1656 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001657 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001658
1659 if (isnullstartblock(left.br_startblock))
1660 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 /*
1664 * Check and set flags if the current (right) segment exists.
1665 * If it doesn't exist, we're converting the hole at end-of-file.
1666 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001667 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1668 state |= BMAP_RIGHT_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 xfs_bmbt_get_all(ep, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001670
1671 if (isnullstartblock(right.br_startblock))
1672 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 /*
1676 * Set contiguity flags on the left and right neighbors.
1677 * Don't let extents get too large, even if the pieces are contiguous.
1678 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001679 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1680 left.br_startoff + left.br_blockcount == new->br_startoff &&
1681 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1682 state |= BMAP_LEFT_CONTIG;
1683
1684 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1685 new->br_startoff + new->br_blockcount == right.br_startoff &&
1686 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1687 (!(state & BMAP_LEFT_CONTIG) ||
1688 (left.br_blockcount + new->br_blockcount +
1689 right.br_blockcount <= MAXEXTLEN)))
1690 state |= BMAP_RIGHT_CONTIG;
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 /*
1693 * Switch out based on the contiguity flags.
1694 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001695 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1696 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /*
1698 * New allocation is contiguous with delayed allocations
1699 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001700 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 */
1702 temp = left.br_blockcount + new->br_blockcount +
1703 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001704
1705 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001706 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001707 oldlen = startblockval(left.br_startblock) +
1708 startblockval(new->br_startblock) +
1709 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001711 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001712 nullstartblock((int)newlen));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001713 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1714
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001715 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 ip->i_df.if_lastex = idx - 1;
1717 break;
1718
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001719 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 /*
1721 * New allocation is contiguous with a delayed allocation
1722 * on the left.
1723 * Merge the new allocation with the left neighbor.
1724 */
1725 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001726 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001727 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001728 oldlen = startblockval(left.br_startblock) +
1729 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001731 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001732 nullstartblock((int)newlen));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001733 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 ip->i_df.if_lastex = idx - 1;
1736 break;
1737
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001738 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /*
1740 * New allocation is contiguous with a delayed allocation
1741 * on the right.
1742 * Merge the new allocation with the right neighbor.
1743 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001744 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001746 oldlen = startblockval(new->br_startblock) +
1747 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 newlen = xfs_bmap_worst_indlen(ip, temp);
1749 xfs_bmbt_set_allf(ep, new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06001750 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001751 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 ip->i_df.if_lastex = idx;
1754 break;
1755
1756 case 0:
1757 /*
1758 * New allocation is not contiguous with another
1759 * delayed allocation.
1760 * Insert a new entry.
1761 */
1762 oldlen = newlen = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001763 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 ip->i_df.if_lastex = idx;
1765 break;
1766 }
1767 if (oldlen != newlen) {
1768 ASSERT(oldlen > newlen);
1769 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11001770 (int64_t)(oldlen - newlen), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 /*
1772 * Nothing to do for disk quota accounting here.
1773 */
1774 }
1775 *logflagsp = 0;
1776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
1779/*
1780 * Called by xfs_bmap_add_extent to handle cases converting a hole
1781 * to a real allocation.
1782 */
1783STATIC int /* error */
1784xfs_bmap_add_extent_hole_real(
1785 xfs_inode_t *ip, /* incore inode pointer */
1786 xfs_extnum_t idx, /* extent number to update/insert */
1787 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001788 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 int *logflagsp, /* inode logging flags */
1790 int whichfork) /* data or attr fork */
1791{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001792 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 int i; /* temp state */
1795 xfs_ifork_t *ifp; /* inode fork pointer */
1796 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1797 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001798 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 ifp = XFS_IFORK_PTR(ip, whichfork);
1802 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001803 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 state = 0;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001805
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001806 if (whichfork == XFS_ATTR_FORK)
1807 state |= BMAP_ATTRFORK;
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 /*
1810 * Check and set flags if this segment has a left neighbor.
1811 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001812 if (idx > 0) {
1813 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001814 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001815 if (isnullstartblock(left.br_startblock))
1816 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 /*
1820 * Check and set flags if this segment has a current value.
1821 * Not true if we're inserting into the "hole" at eof.
1822 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001823 if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1824 state |= BMAP_RIGHT_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 xfs_bmbt_get_all(ep, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001826 if (isnullstartblock(right.br_startblock))
1827 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 /*
1831 * We're inserting a real allocation between "left" and "right".
1832 * Set the contiguity flags. Don't let extents get too large.
1833 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001834 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1835 left.br_startoff + left.br_blockcount == new->br_startoff &&
1836 left.br_startblock + left.br_blockcount == new->br_startblock &&
1837 left.br_state == new->br_state &&
1838 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1839 state |= BMAP_LEFT_CONTIG;
1840
1841 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1842 new->br_startoff + new->br_blockcount == right.br_startoff &&
1843 new->br_startblock + new->br_blockcount == right.br_startblock &&
1844 new->br_state == right.br_state &&
1845 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1846 (!(state & BMAP_LEFT_CONTIG) ||
1847 left.br_blockcount + new->br_blockcount +
1848 right.br_blockcount <= MAXEXTLEN))
1849 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001851 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 /*
1853 * Select which case we're in here, and implement it.
1854 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001855 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1856 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /*
1858 * New allocation is contiguous with real allocations on the
1859 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001860 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001862 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001863 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 left.br_blockcount + new->br_blockcount +
1865 right.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001866 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1867
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001868 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 ifp->if_lastex = idx - 1;
1870 XFS_IFORK_NEXT_SET(ip, whichfork,
1871 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1872 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001873 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001874 } else {
1875 rval = XFS_ILOG_CORE;
1876 if ((error = xfs_bmbt_lookup_eq(cur,
1877 right.br_startoff,
1878 right.br_startblock,
1879 right.br_blockcount, &i)))
1880 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001881 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001882 if ((error = xfs_btree_delete(cur, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001883 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001884 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001885 if ((error = xfs_btree_decrement(cur, 0, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001886 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001887 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001888 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1889 left.br_startblock,
1890 left.br_blockcount +
1891 new->br_blockcount +
1892 right.br_blockcount,
1893 left.br_state)))
1894 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001896 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001898 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 /*
1900 * New allocation is contiguous with a real allocation
1901 * on the left.
1902 * Merge the new allocation with the left neighbor.
1903 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001904 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001905 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 left.br_blockcount + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001907 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 ifp->if_lastex = idx - 1;
1910 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001911 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001912 } else {
1913 rval = 0;
1914 if ((error = xfs_bmbt_lookup_eq(cur,
1915 left.br_startoff,
1916 left.br_startblock,
1917 left.br_blockcount, &i)))
1918 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001919 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001920 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1921 left.br_startblock,
1922 left.br_blockcount +
1923 new->br_blockcount,
1924 left.br_state)))
1925 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001927 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001929 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 /*
1931 * New allocation is contiguous with a real allocation
1932 * on the right.
1933 * Merge the new allocation with the right neighbor.
1934 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001935 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
1937 new->br_blockcount + right.br_blockcount,
1938 right.br_state);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001939 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 ifp->if_lastex = idx;
1942 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001943 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001944 } else {
1945 rval = 0;
1946 if ((error = xfs_bmbt_lookup_eq(cur,
1947 right.br_startoff,
1948 right.br_startblock,
1949 right.br_blockcount, &i)))
1950 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001951 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001952 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1953 new->br_startblock,
1954 new->br_blockcount +
1955 right.br_blockcount,
1956 right.br_state)))
1957 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001959 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 case 0:
1962 /*
1963 * New allocation is not contiguous with another
1964 * real allocation.
1965 * Insert a new entry.
1966 */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001967 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 ifp->if_lastex = idx;
1969 XFS_IFORK_NEXT_SET(ip, whichfork,
1970 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
1971 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001972 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001973 } else {
1974 rval = XFS_ILOG_CORE;
1975 if ((error = xfs_bmbt_lookup_eq(cur,
1976 new->br_startoff,
1977 new->br_startblock,
1978 new->br_blockcount, &i)))
1979 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001980 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001981 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001982 if ((error = xfs_btree_insert(cur, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001983 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001984 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001986 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001988done:
1989 *logflagsp = rval;
1990 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
Nathan Scottdd9f4382006-01-11 15:28:28 +11001993/*
1994 * Adjust the size of the new extent based on di_extsize and rt extsize.
1995 */
1996STATIC int
1997xfs_bmap_extsize_align(
1998 xfs_mount_t *mp,
1999 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2000 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2001 xfs_extlen_t extsz, /* align to this extent size */
2002 int rt, /* is this a realtime inode? */
2003 int eof, /* is extent at end-of-file? */
2004 int delay, /* creating delalloc extent? */
2005 int convert, /* overwriting unwritten extent? */
2006 xfs_fileoff_t *offp, /* in/out: aligned offset */
2007 xfs_extlen_t *lenp) /* in/out: aligned length */
2008{
2009 xfs_fileoff_t orig_off; /* original offset */
2010 xfs_extlen_t orig_alen; /* original length */
2011 xfs_fileoff_t orig_end; /* original off+len */
2012 xfs_fileoff_t nexto; /* next file offset */
2013 xfs_fileoff_t prevo; /* previous file offset */
2014 xfs_fileoff_t align_off; /* temp for offset */
2015 xfs_extlen_t align_alen; /* temp for length */
2016 xfs_extlen_t temp; /* temp for calculations */
2017
2018 if (convert)
2019 return 0;
2020
2021 orig_off = align_off = *offp;
2022 orig_alen = align_alen = *lenp;
2023 orig_end = orig_off + orig_alen;
2024
2025 /*
2026 * If this request overlaps an existing extent, then don't
2027 * attempt to perform any additional alignment.
2028 */
2029 if (!delay && !eof &&
2030 (orig_off >= gotp->br_startoff) &&
2031 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2032 return 0;
2033 }
2034
2035 /*
2036 * If the file offset is unaligned vs. the extent size
2037 * we need to align it. This will be possible unless
2038 * the file was previously written with a kernel that didn't
2039 * perform this alignment, or if a truncate shot us in the
2040 * foot.
2041 */
2042 temp = do_mod(orig_off, extsz);
2043 if (temp) {
2044 align_alen += temp;
2045 align_off -= temp;
2046 }
2047 /*
2048 * Same adjustment for the end of the requested area.
2049 */
2050 if ((temp = (align_alen % extsz))) {
2051 align_alen += extsz - temp;
2052 }
2053 /*
2054 * If the previous block overlaps with this proposed allocation
2055 * then move the start forward without adjusting the length.
2056 */
2057 if (prevp->br_startoff != NULLFILEOFF) {
2058 if (prevp->br_startblock == HOLESTARTBLOCK)
2059 prevo = prevp->br_startoff;
2060 else
2061 prevo = prevp->br_startoff + prevp->br_blockcount;
2062 } else
2063 prevo = 0;
2064 if (align_off != orig_off && align_off < prevo)
2065 align_off = prevo;
2066 /*
2067 * If the next block overlaps with this proposed allocation
2068 * then move the start back without adjusting the length,
2069 * but not before offset 0.
2070 * This may of course make the start overlap previous block,
2071 * and if we hit the offset 0 limit then the next block
2072 * can still overlap too.
2073 */
2074 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2075 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2076 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2077 nexto = gotp->br_startoff + gotp->br_blockcount;
2078 else
2079 nexto = gotp->br_startoff;
2080 } else
2081 nexto = NULLFILEOFF;
2082 if (!eof &&
2083 align_off + align_alen != orig_end &&
2084 align_off + align_alen > nexto)
2085 align_off = nexto > align_alen ? nexto - align_alen : 0;
2086 /*
2087 * If we're now overlapping the next or previous extent that
2088 * means we can't fit an extsz piece in this hole. Just move
2089 * the start forward to the first valid spot and set
2090 * the length so we hit the end.
2091 */
2092 if (align_off != orig_off && align_off < prevo)
2093 align_off = prevo;
2094 if (align_off + align_alen != orig_end &&
2095 align_off + align_alen > nexto &&
2096 nexto != NULLFILEOFF) {
2097 ASSERT(nexto > prevo);
2098 align_alen = nexto - align_off;
2099 }
2100
2101 /*
2102 * If realtime, and the result isn't a multiple of the realtime
2103 * extent size we need to remove blocks until it is.
2104 */
2105 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2106 /*
2107 * We're not covering the original request, or
2108 * we won't be able to once we fix the length.
2109 */
2110 if (orig_off < align_off ||
2111 orig_end > align_off + align_alen ||
2112 align_alen - temp < orig_alen)
2113 return XFS_ERROR(EINVAL);
2114 /*
2115 * Try to fix it by moving the start up.
2116 */
2117 if (align_off + temp <= orig_off) {
2118 align_alen -= temp;
2119 align_off += temp;
2120 }
2121 /*
2122 * Try to fix it by moving the end in.
2123 */
2124 else if (align_off + align_alen - temp >= orig_end)
2125 align_alen -= temp;
2126 /*
2127 * Set the start to the minimum then trim the length.
2128 */
2129 else {
2130 align_alen -= orig_off - align_off;
2131 align_off = orig_off;
2132 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2133 }
2134 /*
2135 * Result doesn't cover the request, fail it.
2136 */
2137 if (orig_off < align_off || orig_end > align_off + align_alen)
2138 return XFS_ERROR(EINVAL);
2139 } else {
2140 ASSERT(orig_off >= align_off);
2141 ASSERT(orig_end <= align_off + align_alen);
2142 }
2143
2144#ifdef DEBUG
2145 if (!eof && gotp->br_startoff != NULLFILEOFF)
2146 ASSERT(align_off + align_alen <= gotp->br_startoff);
2147 if (prevp->br_startoff != NULLFILEOFF)
2148 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2149#endif
2150
2151 *lenp = align_alen;
2152 *offp = align_off;
2153 return 0;
2154}
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156#define XFS_ALLOC_GAP_UNITS 4
2157
David Chinnerc2b1cba2008-04-10 12:21:40 +10002158STATIC void
Nathan Scotta365bdd2006-03-14 13:34:16 +11002159xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2161{
2162 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2164 xfs_mount_t *mp; /* mount point structure */
2165 int nullfb; /* true if ap->firstblock isn't set */
2166 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168#define ISVALID(x,y) \
2169 (rt ? \
2170 (x) < mp->m_sb.sb_rblocks : \
2171 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2172 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2173 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 mp = ap->ip->i_mount;
2176 nullfb = ap->firstblock == NULLFSBLOCK;
2177 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2178 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 /*
2180 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002181 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 */
2183 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002184 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2186 ap->prevp->br_startblock)) {
2187 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2188 /*
2189 * Adjust for the gap between prevp and us.
2190 */
2191 adjust = ap->off -
2192 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2193 if (adjust &&
2194 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2195 ap->rval += adjust;
2196 }
2197 /*
2198 * If not at eof, then compare the two neighbor blocks.
2199 * Figure out whether either one gives us a good starting point,
2200 * and pick the better one.
2201 */
2202 else if (!ap->eof) {
2203 xfs_fsblock_t gotbno; /* right side block number */
2204 xfs_fsblock_t gotdiff=0; /* right side difference */
2205 xfs_fsblock_t prevbno; /* left side block number */
2206 xfs_fsblock_t prevdiff=0; /* left side difference */
2207
2208 /*
2209 * If there's a previous (left) block, select a requested
2210 * start block based on it.
2211 */
2212 if (ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002213 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 (prevbno = ap->prevp->br_startblock +
2215 ap->prevp->br_blockcount) &&
2216 ISVALID(prevbno, ap->prevp->br_startblock)) {
2217 /*
2218 * Calculate gap to end of previous block.
2219 */
2220 adjust = prevdiff = ap->off -
2221 (ap->prevp->br_startoff +
2222 ap->prevp->br_blockcount);
2223 /*
2224 * Figure the startblock based on the previous block's
2225 * end and the gap size.
2226 * Heuristic!
2227 * If the gap is large relative to the piece we're
2228 * allocating, or using it gives us an invalid block
2229 * number, then just use the end of the previous block.
2230 */
2231 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2232 ISVALID(prevbno + prevdiff,
2233 ap->prevp->br_startblock))
2234 prevbno += adjust;
2235 else
2236 prevdiff += adjust;
2237 /*
2238 * If the firstblock forbids it, can't use it,
2239 * must use default.
2240 */
2241 if (!rt && !nullfb &&
2242 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2243 prevbno = NULLFSBLOCK;
2244 }
2245 /*
2246 * No previous block or can't follow it, just default.
2247 */
2248 else
2249 prevbno = NULLFSBLOCK;
2250 /*
2251 * If there's a following (right) block, select a requested
2252 * start block based on it.
2253 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002254 if (!isnullstartblock(ap->gotp->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 /*
2256 * Calculate gap to start of next block.
2257 */
2258 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2259 /*
2260 * Figure the startblock based on the next block's
2261 * start and the gap size.
2262 */
2263 gotbno = ap->gotp->br_startblock;
2264 /*
2265 * Heuristic!
2266 * If the gap is large relative to the piece we're
2267 * allocating, or using it gives us an invalid block
2268 * number, then just use the start of the next block
2269 * offset by our length.
2270 */
2271 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2272 ISVALID(gotbno - gotdiff, gotbno))
2273 gotbno -= adjust;
2274 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2275 gotbno -= ap->alen;
2276 gotdiff += adjust - ap->alen;
2277 } else
2278 gotdiff += adjust;
2279 /*
2280 * If the firstblock forbids it, can't use it,
2281 * must use default.
2282 */
2283 if (!rt && !nullfb &&
2284 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2285 gotbno = NULLFSBLOCK;
2286 }
2287 /*
2288 * No next block, just default.
2289 */
2290 else
2291 gotbno = NULLFSBLOCK;
2292 /*
2293 * If both valid, pick the better one, else the only good
2294 * one, else ap->rval is already set (to 0 or the inode block).
2295 */
2296 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2297 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2298 else if (prevbno != NULLFSBLOCK)
2299 ap->rval = prevbno;
2300 else if (gotbno != NULLFSBLOCK)
2301 ap->rval = gotbno;
2302 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002303#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11002304}
2305
2306STATIC int
2307xfs_bmap_rtalloc(
2308 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2309{
2310 xfs_alloctype_t atype = 0; /* type for allocation routines */
2311 int error; /* error return value */
2312 xfs_mount_t *mp; /* mount point structure */
2313 xfs_extlen_t prod = 0; /* product factor for allocators */
2314 xfs_extlen_t ralen = 0; /* realtime allocation length */
2315 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002316 xfs_rtblock_t rtb;
2317
2318 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002319 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002320 prod = align / mp->m_sb.sb_rextsize;
2321 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2322 align, 1, ap->eof, 0,
2323 ap->conv, &ap->off, &ap->alen);
2324 if (error)
2325 return error;
2326 ASSERT(ap->alen);
2327 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2328
2329 /*
2330 * If the offset & length are not perfectly aligned
2331 * then kill prod, it will just get us in trouble.
2332 */
2333 if (do_mod(ap->off, align) || ap->alen % align)
2334 prod = 1;
2335 /*
2336 * Set ralen to be the actual requested length in rtextents.
2337 */
2338 ralen = ap->alen / mp->m_sb.sb_rextsize;
2339 /*
2340 * If the old value was close enough to MAXEXTLEN that
2341 * we rounded up to it, cut it back so it's valid again.
2342 * Note that if it's a really large request (bigger than
2343 * MAXEXTLEN), we don't hear about that number, and can't
2344 * adjust the starting point to match it.
2345 */
2346 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2347 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2348 /*
2349 * If it's an allocation to an empty file at offset 0,
2350 * pick an extent that will space things out in the rt area.
2351 */
2352 if (ap->eof && ap->off == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10002353 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2354
Nathan Scotta365bdd2006-03-14 13:34:16 +11002355 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2356 if (error)
2357 return error;
2358 ap->rval = rtx * mp->m_sb.sb_rextsize;
2359 } else {
2360 ap->rval = 0;
2361 }
2362
2363 xfs_bmap_adjacent(ap);
2364
2365 /*
2366 * Realtime allocation, done through xfs_rtallocate_extent.
2367 */
2368 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2369 do_div(ap->rval, mp->m_sb.sb_rextsize);
2370 rtb = ap->rval;
2371 ap->alen = ralen;
2372 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2373 &ralen, atype, ap->wasdel, prod, &rtb)))
2374 return error;
2375 if (rtb == NULLFSBLOCK && prod > 1 &&
2376 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2377 ap->alen, &ralen, atype,
2378 ap->wasdel, 1, &rtb)))
2379 return error;
2380 ap->rval = rtb;
2381 if (ap->rval != NULLFSBLOCK) {
2382 ap->rval *= mp->m_sb.sb_rextsize;
2383 ralen *= mp->m_sb.sb_rextsize;
2384 ap->alen = ralen;
2385 ap->ip->i_d.di_nblocks += ralen;
2386 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2387 if (ap->wasdel)
2388 ap->ip->i_delayed_blks -= ralen;
2389 /*
2390 * Adjust the disk quota also. This was reserved
2391 * earlier.
2392 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002393 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002394 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2395 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2396 } else {
2397 ap->alen = 0;
2398 }
2399 return 0;
2400}
2401
2402STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00002403xfs_bmap_btalloc_nullfb(
2404 struct xfs_bmalloca *ap,
2405 struct xfs_alloc_arg *args,
2406 xfs_extlen_t *blen)
2407{
2408 struct xfs_mount *mp = ap->ip->i_mount;
2409 struct xfs_perag *pag;
2410 xfs_agnumber_t ag, startag;
2411 int notinit = 0;
2412 int error;
2413
2414 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2415 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2416 else
2417 args->type = XFS_ALLOCTYPE_START_BNO;
2418 args->total = ap->total;
2419
2420 /*
2421 * Search for an allocation group with a single extent large enough
2422 * for the request. If one isn't found, then adjust the minimum
2423 * allocation size to the largest space found.
2424 */
2425 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2426 if (startag == NULLAGNUMBER)
2427 startag = ag = 0;
2428
2429 pag = xfs_perag_get(mp, ag);
2430 while (*blen < ap->alen) {
2431 if (!pag->pagf_init) {
2432 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2433 XFS_ALLOC_FLAG_TRYLOCK);
2434 if (error) {
2435 xfs_perag_put(pag);
2436 return error;
2437 }
2438 }
2439
2440 /*
2441 * See xfs_alloc_fix_freelist...
2442 */
2443 if (pag->pagf_init) {
2444 xfs_extlen_t longest;
2445 longest = xfs_alloc_longest_free_extent(mp, pag);
2446 if (*blen < longest)
2447 *blen = longest;
2448 } else
2449 notinit = 1;
2450
2451 if (xfs_inode_is_filestream(ap->ip)) {
2452 if (*blen >= ap->alen)
2453 break;
2454
2455 if (ap->userdata) {
2456 /*
2457 * If startag is an invalid AG, we've
2458 * come here once before and
2459 * xfs_filestream_new_ag picked the
2460 * best currently available.
2461 *
2462 * Don't continue looping, since we
2463 * could loop forever.
2464 */
2465 if (startag == NULLAGNUMBER)
2466 break;
2467
2468 error = xfs_filestream_new_ag(ap, &ag);
2469 xfs_perag_put(pag);
2470 if (error)
2471 return error;
2472
2473 /* loop again to set 'blen'*/
2474 startag = NULLAGNUMBER;
2475 pag = xfs_perag_get(mp, ag);
2476 continue;
2477 }
2478 }
2479 if (++ag == mp->m_sb.sb_agcount)
2480 ag = 0;
2481 if (ag == startag)
2482 break;
2483 xfs_perag_put(pag);
2484 pag = xfs_perag_get(mp, ag);
2485 }
2486 xfs_perag_put(pag);
2487
2488 /*
2489 * Since the above loop did a BUF_TRYLOCK, it is
2490 * possible that there is space for this request.
2491 */
2492 if (notinit || *blen < ap->minlen)
2493 args->minlen = ap->minlen;
2494 /*
2495 * If the best seen length is less than the request
2496 * length, use the best as the minimum.
2497 */
2498 else if (*blen < ap->alen)
2499 args->minlen = *blen;
2500 /*
2501 * Otherwise we've seen an extent as big as alen,
2502 * use that as the minimum.
2503 */
2504 else
2505 args->minlen = ap->alen;
2506
2507 /*
2508 * set the failure fallback case to look in the selected
2509 * AG as the stream may have moved.
2510 */
2511 if (xfs_inode_is_filestream(ap->ip))
2512 ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2513
2514 return 0;
2515}
2516
2517STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11002518xfs_bmap_btalloc(
2519 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2520{
2521 xfs_mount_t *mp; /* mount point structure */
2522 xfs_alloctype_t atype = 0; /* type for allocation routines */
2523 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002524 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00002525 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002526 xfs_alloc_arg_t args;
2527 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002528 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002529 int nullfb; /* true if ap->firstblock isn't set */
2530 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002531 int tryagain;
2532 int error;
2533
2534 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002535 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002536 if (unlikely(align)) {
2537 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2538 align, 0, ap->eof, 0, ap->conv,
2539 &ap->off, &ap->alen);
2540 ASSERT(!error);
2541 ASSERT(ap->alen);
2542 }
2543 nullfb = ap->firstblock == NULLFSBLOCK;
2544 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10002545 if (nullfb) {
2546 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2547 ag = xfs_filestream_lookup_ag(ap->ip);
2548 ag = (ag != NULLAGNUMBER) ? ag : 0;
2549 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2550 } else {
2551 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2552 }
2553 } else
Nathan Scotta365bdd2006-03-14 13:34:16 +11002554 ap->rval = ap->firstblock;
2555
2556 xfs_bmap_adjacent(ap);
2557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 /*
2559 * If allowed, use ap->rval; otherwise must use firstblock since
2560 * it's in the right allocation group.
2561 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002562 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 ;
2564 else
2565 ap->rval = ap->firstblock;
2566 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 * Normal allocation, done through xfs_alloc_vextent.
2568 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002569 tryagain = isaligned = 0;
2570 args.tp = ap->tp;
2571 args.mp = mp;
2572 args.fsbno = ap->rval;
2573 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
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 */
2621 if (blen > args.alignment && blen <= ap->alen)
2622 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 */
2640 if (blen > mp->m_dalign && blen <= ap->alen)
2641 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 */
2807 xfs_extnum_t idx, /* extent number to update/delete */
2808 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 */
2812 int whichfork, /* data or attr fork */
2813 int rsvd) /* OK to allocate reserved blocks */
2814{
2815 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2816 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2817 xfs_fsblock_t del_endblock=0; /* first block past del */
2818 xfs_fileoff_t del_endoff; /* first offset past del */
2819 int delay; /* current block is delayed allocated */
2820 int do_fx; /* free extent at end of routine */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002821 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 int error; /* error return value */
2823 int flags; /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 xfs_bmbt_irec_t got; /* current extent entry */
2825 xfs_fileoff_t got_endoff; /* first offset past got */
2826 int i; /* temp state */
2827 xfs_ifork_t *ifp; /* inode fork pointer */
2828 xfs_mount_t *mp; /* mount structure */
2829 xfs_filblks_t nblks; /* quota/sb block count */
2830 xfs_bmbt_irec_t new; /* new record to be inserted */
2831 /* REFERENCED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 uint qfield; /* quota field to update */
2833 xfs_filblks_t temp; /* for indirect length calculations */
2834 xfs_filblks_t temp2; /* for indirect length calculations */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002835 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
2837 XFS_STATS_INC(xs_del_exlist);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002838
2839 if (whichfork == XFS_ATTR_FORK)
2840 state |= BMAP_ATTRFORK;
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 mp = ip->i_mount;
2843 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002844 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
2845 (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 ASSERT(del->br_blockcount > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002847 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 xfs_bmbt_get_all(ep, &got);
2849 ASSERT(got.br_startoff <= del->br_startoff);
2850 del_endoff = del->br_startoff + del->br_blockcount;
2851 got_endoff = got.br_startoff + got.br_blockcount;
2852 ASSERT(got_endoff >= del_endoff);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002853 delay = isnullstartblock(got.br_startblock);
2854 ASSERT(isnullstartblock(del->br_startblock) == delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 flags = 0;
2856 qfield = 0;
2857 error = 0;
2858 /*
2859 * If deleting a real allocation, must free up the disk space.
2860 */
2861 if (!delay) {
2862 flags = XFS_ILOG_CORE;
2863 /*
2864 * Realtime allocation. Free it and record di_nblocks update.
2865 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002866 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 xfs_fsblock_t bno;
2868 xfs_filblks_t len;
2869
2870 ASSERT(do_mod(del->br_blockcount,
2871 mp->m_sb.sb_rextsize) == 0);
2872 ASSERT(do_mod(del->br_startblock,
2873 mp->m_sb.sb_rextsize) == 0);
2874 bno = del->br_startblock;
2875 len = del->br_blockcount;
2876 do_div(bno, mp->m_sb.sb_rextsize);
2877 do_div(len, mp->m_sb.sb_rextsize);
2878 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
2879 (xfs_extlen_t)len)))
2880 goto done;
2881 do_fx = 0;
2882 nblks = len * mp->m_sb.sb_rextsize;
2883 qfield = XFS_TRANS_DQ_RTBCOUNT;
2884 }
2885 /*
2886 * Ordinary allocation.
2887 */
2888 else {
2889 do_fx = 1;
2890 nblks = del->br_blockcount;
2891 qfield = XFS_TRANS_DQ_BCOUNT;
2892 }
2893 /*
2894 * Set up del_endblock and cur for later.
2895 */
2896 del_endblock = del->br_startblock + del->br_blockcount;
2897 if (cur) {
2898 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2899 got.br_startblock, got.br_blockcount,
2900 &i)))
2901 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002902 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 }
2904 da_old = da_new = 0;
2905 } else {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002906 da_old = startblockval(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 da_new = 0;
2908 nblks = 0;
2909 do_fx = 0;
2910 }
2911 /*
2912 * Set flag value to use in switch statement.
2913 * Left-contig is 2, right-contig is 1.
2914 */
2915 switch (((got.br_startoff == del->br_startoff) << 1) |
2916 (got_endoff == del_endoff)) {
2917 case 3:
2918 /*
2919 * Matches the whole extent. Delete the entry.
2920 */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002921 xfs_iext_remove(ip, idx, 1,
2922 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 ifp->if_lastex = idx;
2924 if (delay)
2925 break;
2926 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 Hellwig91cca5df2008-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 Hellwig0b1b2132009-12-14 23:14:59 +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);
2946 ifp->if_lastex = idx;
2947 if (delay) {
2948 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2949 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002950 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002951 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 da_new = temp;
2953 break;
2954 }
2955 xfs_bmbt_set_startblock(ep, del_endblock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002956 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002958 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 break;
2960 }
2961 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2962 got.br_blockcount - del->br_blockcount,
2963 got.br_state)))
2964 goto done;
2965 break;
2966
2967 case 1:
2968 /*
2969 * Deleting the last part of the extent.
2970 */
2971 temp = got.br_blockcount - del->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002972 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 xfs_bmbt_set_blockcount(ep, temp);
2974 ifp->if_lastex = idx;
2975 if (delay) {
2976 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2977 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002978 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002979 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 da_new = temp;
2981 break;
2982 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002983 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002985 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 break;
2987 }
2988 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2989 got.br_startblock,
2990 got.br_blockcount - del->br_blockcount,
2991 got.br_state)))
2992 goto done;
2993 break;
2994
2995 case 0:
2996 /*
2997 * Deleting the middle of the extent.
2998 */
2999 temp = del->br_startoff - got.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003000 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 xfs_bmbt_set_blockcount(ep, temp);
3002 new.br_startoff = del_endoff;
3003 temp2 = got_endoff - del_endoff;
3004 new.br_blockcount = temp2;
3005 new.br_state = got.br_state;
3006 if (!delay) {
3007 new.br_startblock = del_endblock;
3008 flags |= XFS_ILOG_CORE;
3009 if (cur) {
3010 if ((error = xfs_bmbt_update(cur,
3011 got.br_startoff,
3012 got.br_startblock, temp,
3013 got.br_state)))
3014 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11003015 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 goto done;
3017 cur->bc_rec.b = new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003018 error = xfs_btree_insert(cur, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 if (error && error != ENOSPC)
3020 goto done;
3021 /*
3022 * If get no-space back from btree insert,
3023 * it tried a split, and we have a zero
3024 * block reservation.
3025 * Fix up our state and return the error.
3026 */
3027 if (error == ENOSPC) {
3028 /*
3029 * Reset the cursor, don't trust
3030 * it after any insert operation.
3031 */
3032 if ((error = xfs_bmbt_lookup_eq(cur,
3033 got.br_startoff,
3034 got.br_startblock,
3035 temp, &i)))
3036 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003037 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 /*
3039 * Update the btree record back
3040 * to the original value.
3041 */
3042 if ((error = xfs_bmbt_update(cur,
3043 got.br_startoff,
3044 got.br_startblock,
3045 got.br_blockcount,
3046 got.br_state)))
3047 goto done;
3048 /*
3049 * Reset the extent record back
3050 * to the original value.
3051 */
3052 xfs_bmbt_set_blockcount(ep,
3053 got.br_blockcount);
3054 flags = 0;
3055 error = XFS_ERROR(ENOSPC);
3056 goto done;
3057 }
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003058 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 } else
Eric Sandeen9d87c312009-01-14 23:22:07 -06003060 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 XFS_IFORK_NEXT_SET(ip, whichfork,
3062 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3063 } else {
3064 ASSERT(whichfork == XFS_DATA_FORK);
3065 temp = xfs_bmap_worst_indlen(ip, temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003066 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003068 new.br_startblock = nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 da_new = temp + temp2;
3070 while (da_new > da_old) {
3071 if (temp) {
3072 temp--;
3073 da_new--;
3074 xfs_bmbt_set_startblock(ep,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003075 nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 }
3077 if (da_new == da_old)
3078 break;
3079 if (temp2) {
3080 temp2--;
3081 da_new--;
3082 new.br_startblock =
Eric Sandeen9d87c312009-01-14 23:22:07 -06003083 nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 }
3085 }
3086 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003087 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3088 xfs_iext_insert(ip, idx + 1, 1, &new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 ifp->if_lastex = idx + 1;
3090 break;
3091 }
3092 /*
3093 * If we need to, add to list of extents to delete.
3094 */
3095 if (do_fx)
3096 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3097 mp);
3098 /*
3099 * Adjust inode # blocks in the file.
3100 */
3101 if (nblks)
3102 ip->i_d.di_nblocks -= nblks;
3103 /*
3104 * Adjust quota data.
3105 */
3106 if (qfield)
Christoph Hellwig7d095252009-06-08 15:33:32 +02003107 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
3109 /*
3110 * Account for change in delayed indirect blocks.
3111 * Nothing to do for disk quota accounting here.
3112 */
3113 ASSERT(da_old >= da_new);
3114 if (da_old > da_new)
David Chinner20f4ebf2007-02-10 18:36:10 +11003115 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 rsvd);
3117done:
3118 *logflagsp = flags;
3119 return error;
3120}
3121
3122/*
3123 * Remove the entry "free" from the free item list. Prev points to the
3124 * previous entry, unless "free" is the head of the list.
3125 */
3126STATIC void
3127xfs_bmap_del_free(
3128 xfs_bmap_free_t *flist, /* free item list header */
3129 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3130 xfs_bmap_free_item_t *free) /* list item to be freed */
3131{
3132 if (prev)
3133 prev->xbfi_next = free->xbfi_next;
3134 else
3135 flist->xbf_first = free->xbfi_next;
3136 flist->xbf_count--;
3137 kmem_zone_free(xfs_bmap_free_item_zone, free);
3138}
3139
3140/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 * Convert an extents-format file into a btree-format file.
3142 * The new file will have a root block (in the inode) and a single child block.
3143 */
3144STATIC int /* error */
3145xfs_bmap_extents_to_btree(
3146 xfs_trans_t *tp, /* transaction pointer */
3147 xfs_inode_t *ip, /* incore inode pointer */
3148 xfs_fsblock_t *firstblock, /* first-block-allocated */
3149 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3150 xfs_btree_cur_t **curp, /* cursor returned to caller */
3151 int wasdel, /* converting a delayed alloc */
3152 int *logflagsp, /* inode logging flags */
3153 int whichfork) /* data or attr fork */
3154{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003155 struct xfs_btree_block *ablock; /* allocated (child) bt block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 xfs_buf_t *abp; /* buffer for ablock */
3157 xfs_alloc_arg_t args; /* allocation arguments */
3158 xfs_bmbt_rec_t *arp; /* child record pointer */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003159 struct xfs_btree_block *block; /* btree root block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 xfs_btree_cur_t *cur; /* bmap btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003161 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003163 xfs_extnum_t i, cnt; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 xfs_ifork_t *ifp; /* inode fork pointer */
3165 xfs_bmbt_key_t *kp; /* root block key pointer */
3166 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003167 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3169
3170 ifp = XFS_IFORK_PTR(ip, whichfork);
3171 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3172 ASSERT(ifp->if_ext_max ==
3173 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3174 /*
3175 * Make space in the inode incore.
3176 */
3177 xfs_iroot_realloc(ip, 1, whichfork);
3178 ifp->if_flags |= XFS_IFBROOT;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003179
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 /*
3181 * Fill in the root.
3182 */
3183 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003184 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3185 block->bb_level = cpu_to_be16(1);
3186 block->bb_numrecs = cpu_to_be16(1);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003187 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3188 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3189
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 /*
3191 * Need a cursor. Can't allocate until bb_level is filled in.
3192 */
3193 mp = ip->i_mount;
Christoph Hellwig561f7d12008-10-30 16:53:59 +11003194 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 cur->bc_private.b.firstblock = *firstblock;
3196 cur->bc_private.b.flist = flist;
3197 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3198 /*
3199 * Convert to a btree with two levels, one record in root.
3200 */
3201 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3202 args.tp = tp;
3203 args.mp = mp;
Yingping Lud210a282006-06-09 14:55:18 +10003204 args.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 if (*firstblock == NULLFSBLOCK) {
3206 args.type = XFS_ALLOCTYPE_START_BNO;
3207 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3208 } else if (flist->xbf_low) {
3209 args.type = XFS_ALLOCTYPE_START_BNO;
3210 args.fsbno = *firstblock;
3211 } else {
3212 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3213 args.fsbno = *firstblock;
3214 }
3215 args.minlen = args.maxlen = args.prod = 1;
3216 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3217 args.minalignslop = 0;
3218 args.wasdel = wasdel;
3219 *logflagsp = 0;
3220 if ((error = xfs_alloc_vextent(&args))) {
3221 xfs_iroot_realloc(ip, -1, whichfork);
3222 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3223 return error;
3224 }
3225 /*
3226 * Allocation can't fail, the space was reserved.
3227 */
3228 ASSERT(args.fsbno != NULLFSBLOCK);
3229 ASSERT(*firstblock == NULLFSBLOCK ||
3230 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3231 (flist->xbf_low &&
3232 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3233 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3234 cur->bc_private.b.allocated++;
3235 ip->i_d.di_nblocks++;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003236 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3238 /*
3239 * Fill in the child block.
3240 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003241 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003242 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 ablock->bb_level = 0;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003244 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3245 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003246 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003248 for (cnt = i = 0; i < nextents; i++) {
3249 ep = xfs_iext_get_ext(ifp, i);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003250 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10003251 arp->l0 = cpu_to_be64(ep->l0);
3252 arp->l1 = cpu_to_be64(ep->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 arp++; cnt++;
3254 }
3255 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11003256 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003257 xfs_btree_set_numrecs(ablock, cnt);
3258
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 /*
3260 * Fill in the root key and pointer.
3261 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11003262 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3263 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Christoph Hellwig8801bb92006-09-28 10:58:17 +10003264 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
Christoph Hellwig136341b2008-10-30 17:11:40 +11003265 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3266 be16_to_cpu(block->bb_level)));
Christoph Hellwig576039c2006-09-28 10:58:06 +10003267 *pp = cpu_to_be64(args.fsbno);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003268
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 /*
3270 * Do all this logging at the end so that
3271 * the root is at the right level.
3272 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11003273 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3274 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 ASSERT(*curp == NULL);
3276 *curp = cur;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003277 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 return 0;
3279}
3280
3281/*
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003282 * Calculate the default attribute fork offset for newly created inodes.
3283 */
3284uint
3285xfs_default_attroffset(
3286 struct xfs_inode *ip)
3287{
3288 struct xfs_mount *mp = ip->i_mount;
3289 uint offset;
3290
3291 if (mp->m_sb.sb_inodesize == 256) {
3292 offset = XFS_LITINO(mp) -
3293 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3294 } else {
3295 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3296 }
3297
3298 ASSERT(offset < XFS_LITINO(mp));
3299 return offset;
3300}
3301
3302/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11003303 * Helper routine to reset inode di_forkoff field when switching
3304 * attribute fork from local to extent format - we reset it where
3305 * possible to make space available for inline data fork extents.
3306 */
3307STATIC void
3308xfs_bmap_forkoff_reset(
3309 xfs_mount_t *mp,
3310 xfs_inode_t *ip,
3311 int whichfork)
3312{
3313 if (whichfork == XFS_ATTR_FORK &&
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003314 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3315 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3316 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3317 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3318
3319 if (dfl_forkoff > ip->i_d.di_forkoff) {
3320 ip->i_d.di_forkoff = dfl_forkoff;
3321 ip->i_df.if_ext_max =
3322 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3323 ip->i_afp->if_ext_max =
3324 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3325 }
Nathan Scottd8cc8902005-11-02 10:34:53 +11003326 }
3327}
3328
3329/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 * Convert a local file to an extents file.
3331 * This code is out of bounds for data forks of regular files,
3332 * since the file data needs to get logged so things will stay consistent.
3333 * (The bmap-level manipulations are ok, though).
3334 */
3335STATIC int /* error */
3336xfs_bmap_local_to_extents(
3337 xfs_trans_t *tp, /* transaction pointer */
3338 xfs_inode_t *ip, /* incore inode pointer */
3339 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3340 xfs_extlen_t total, /* total blocks needed by transaction */
3341 int *logflagsp, /* inode logging flags */
3342 int whichfork) /* data or attr fork */
3343{
3344 int error; /* error return value */
3345 int flags; /* logging flags returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 xfs_ifork_t *ifp; /* inode fork pointer */
3347
3348 /*
3349 * We don't want to deal with the case of keeping inode data inline yet.
3350 * So sending the data fork of a regular inode is invalid.
3351 */
3352 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3353 whichfork == XFS_DATA_FORK));
3354 ifp = XFS_IFORK_PTR(ip, whichfork);
3355 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3356 flags = 0;
3357 error = 0;
3358 if (ifp->if_bytes) {
3359 xfs_alloc_arg_t args; /* allocation arguments */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003360 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003361 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
3363 args.tp = tp;
3364 args.mp = ip->i_mount;
Yingping Lud210a282006-06-09 14:55:18 +10003365 args.firstblock = *firstblock;
Mandy Kirkconnellf020b672006-03-14 14:07:24 +11003366 ASSERT((ifp->if_flags &
3367 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 /*
3369 * Allocate a block. We know we need only one, since the
3370 * file currently fits in an inode.
3371 */
3372 if (*firstblock == NULLFSBLOCK) {
3373 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3374 args.type = XFS_ALLOCTYPE_START_BNO;
3375 } else {
3376 args.fsbno = *firstblock;
3377 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3378 }
3379 args.total = total;
3380 args.mod = args.minleft = args.alignment = args.wasdel =
3381 args.isfl = args.minalignslop = 0;
3382 args.minlen = args.maxlen = args.prod = 1;
3383 if ((error = xfs_alloc_vextent(&args)))
3384 goto done;
3385 /*
3386 * Can't fail, the space was reserved.
3387 */
3388 ASSERT(args.fsbno != NULLFSBLOCK);
3389 ASSERT(args.len == 1);
3390 *firstblock = args.fsbno;
3391 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3392 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3393 ifp->if_bytes);
3394 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003395 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003397 xfs_iext_add(ifp, 0, 1);
3398 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003400 trace_xfs_bmap_post_update(ip, 0,
3401 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3402 _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3404 ip->i_d.di_nblocks = 1;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003405 xfs_trans_mod_dquot_byino(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 XFS_TRANS_DQ_BCOUNT, 1L);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003407 flags |= xfs_ilog_fext(whichfork);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003408 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003410 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 ifp->if_flags &= ~XFS_IFINLINE;
3413 ifp->if_flags |= XFS_IFEXTENTS;
3414 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3415 flags |= XFS_ILOG_CORE;
3416done:
3417 *logflagsp = flags;
3418 return error;
3419}
3420
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003422 * Search the extent records for the entry containing block bno.
3423 * If bno lies in a hole, point to the next entry. If bno lies
3424 * past eof, *eofp will be set, and *prevp will contain the last
3425 * entry (null if none). Else, *lastxp will be set to the index
3426 * of the found entry; *gotp will contain the entry.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003427 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003428STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003429xfs_bmap_search_multi_extents(
3430 xfs_ifork_t *ifp, /* inode fork pointer */
3431 xfs_fileoff_t bno, /* block number searched for */
3432 int *eofp, /* out: end of file found */
3433 xfs_extnum_t *lastxp, /* out: last extent index */
3434 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3435 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3436{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003437 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003438 xfs_extnum_t lastx; /* last extent index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003439
3440 /*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003441 * Initialize the extent entry structure to catch access to
3442 * uninitialized br_startblock field.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003443 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003444 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3445 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3446 gotp->br_state = XFS_EXT_INVALID;
3447#if XFS_BIG_BLKNOS
3448 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3449#else
3450 gotp->br_startblock = 0xffffa5a5;
3451#endif
3452 prevp->br_startoff = NULLFILEOFF;
3453
3454 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3455 if (lastx > 0) {
3456 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3457 }
3458 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3459 xfs_bmbt_get_all(ep, gotp);
3460 *eofp = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003461 } else {
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003462 if (lastx > 0) {
3463 *gotp = *prevp;
3464 }
3465 *eofp = 1;
3466 ep = NULL;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003467 }
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003468 *lastxp = lastx;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003469 return ep;
3470}
3471
3472/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 * Search the extents list for the inode, for the extent containing bno.
3474 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3475 * *eofp will be set, and *prevp will contain the last entry (null if none).
3476 * Else, *lastxp will be set to the index of the found
3477 * entry; *gotp will contain the entry.
3478 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003479STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480xfs_bmap_search_extents(
3481 xfs_inode_t *ip, /* incore inode pointer */
3482 xfs_fileoff_t bno, /* block number searched for */
Nathan Scott572d95f2006-09-28 11:03:20 +10003483 int fork, /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 int *eofp, /* out: end of file found */
3485 xfs_extnum_t *lastxp, /* out: last extent index */
3486 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3487 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3488{
3489 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003490 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
3492 XFS_STATS_INC(xs_look_exlist);
Nathan Scott572d95f2006-09-28 11:03:20 +10003493 ifp = XFS_IFORK_PTR(ip, fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003495 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3496
Nathan Scott572d95f2006-09-28 11:03:20 +10003497 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3498 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3499 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3500 "Access to block zero in inode %llu "
3501 "start_block: %llx start_off: %llx "
3502 "blkcnt: %llx extent-state: %x lastx: %x\n",
3503 (unsigned long long)ip->i_ino,
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003504 (unsigned long long)gotp->br_startblock,
3505 (unsigned long long)gotp->br_startoff,
3506 (unsigned long long)gotp->br_blockcount,
Nathan Scott572d95f2006-09-28 11:03:20 +10003507 gotp->br_state, *lastxp);
3508 *lastxp = NULLEXTNUM;
3509 *eofp = 1;
3510 return NULL;
3511 }
3512 return ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513}
3514
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515/*
3516 * Compute the worst-case number of indirect blocks that will be used
3517 * for ip's delayed extent of length "len".
3518 */
3519STATIC xfs_filblks_t
3520xfs_bmap_worst_indlen(
3521 xfs_inode_t *ip, /* incore inode pointer */
3522 xfs_filblks_t len) /* delayed extent length */
3523{
3524 int level; /* btree level number */
3525 int maxrecs; /* maximum record count at this level */
3526 xfs_mount_t *mp; /* mount structure */
3527 xfs_filblks_t rval; /* return value */
3528
3529 mp = ip->i_mount;
3530 maxrecs = mp->m_bmap_dmxr[0];
3531 for (level = 0, rval = 0;
3532 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3533 level++) {
3534 len += maxrecs - 1;
3535 do_div(len, maxrecs);
3536 rval += len;
3537 if (len == 1)
3538 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3539 level - 1;
3540 if (level == 0)
3541 maxrecs = mp->m_bmap_dmxr[1];
3542 }
3543 return rval;
3544}
3545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546/*
3547 * Convert inode from non-attributed to attributed.
3548 * Must not be in a transaction, ip must not be locked.
3549 */
3550int /* error code */
3551xfs_bmap_add_attrfork(
3552 xfs_inode_t *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003553 int size, /* space new attribute needs */
3554 int rsvd) /* xact may use reserved blks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003557 xfs_bmap_free_t flist; /* freed extent records */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 xfs_trans_t *tp; /* transaction pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003560 int blks; /* space reservation */
3561 int version = 1; /* superblock attr version */
3562 int committed; /* xaction was committed */
3563 int logflags; /* logging flags */
3564 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Nathan Scottd8cc8902005-11-02 10:34:53 +11003566 ASSERT(XFS_IFORK_Q(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 ASSERT(ip->i_df.if_ext_max ==
3568 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
Nathan Scottd8cc8902005-11-02 10:34:53 +11003569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 mp = ip->i_mount;
3571 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3572 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3573 blks = XFS_ADDAFORK_SPACE_RES(mp);
3574 if (rsvd)
3575 tp->t_flags |= XFS_TRANS_RESERVE;
3576 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3577 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3578 goto error0;
3579 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02003580 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3582 XFS_QMOPT_RES_REGBLKS);
3583 if (error) {
3584 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3585 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3586 return error;
3587 }
3588 if (XFS_IFORK_Q(ip))
3589 goto error1;
3590 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3591 /*
3592 * For inodes coming from pre-6.2 filesystems.
3593 */
3594 ASSERT(ip->i_d.di_aformat == 0);
3595 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3596 }
3597 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwig898621d2010-06-24 11:36:58 +10003598
3599 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig898621d2010-06-24 11:36:58 +10003601
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 switch (ip->i_d.di_format) {
3603 case XFS_DINODE_FMT_DEV:
3604 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3605 break;
3606 case XFS_DINODE_FMT_UUID:
3607 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3608 break;
3609 case XFS_DINODE_FMT_LOCAL:
3610 case XFS_DINODE_FMT_EXTENTS:
3611 case XFS_DINODE_FMT_BTREE:
Nathan Scottd8cc8902005-11-02 10:34:53 +11003612 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3613 if (!ip->i_d.di_forkoff)
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003614 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
Nathan Scott13059ff2006-01-11 15:32:01 +11003615 else if (mp->m_flags & XFS_MOUNT_ATTR2)
Nathan Scottd8cc8902005-11-02 10:34:53 +11003616 version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 break;
3618 default:
3619 ASSERT(0);
3620 error = XFS_ERROR(EINVAL);
3621 goto error1;
3622 }
3623 ip->i_df.if_ext_max =
3624 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3625 ASSERT(ip->i_afp == NULL);
3626 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3627 ip->i_afp->if_ext_max =
3628 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3629 ip->i_afp->if_flags = XFS_IFEXTENTS;
3630 logflags = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003631 xfs_bmap_init(&flist, &firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 switch (ip->i_d.di_format) {
3633 case XFS_DINODE_FMT_LOCAL:
3634 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3635 &logflags);
3636 break;
3637 case XFS_DINODE_FMT_EXTENTS:
3638 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3639 &flist, &logflags);
3640 break;
3641 case XFS_DINODE_FMT_BTREE:
3642 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3643 &logflags);
3644 break;
3645 default:
3646 error = 0;
3647 break;
3648 }
3649 if (logflags)
3650 xfs_trans_log_inode(tp, ip, logflags);
3651 if (error)
3652 goto error2;
Eric Sandeen62118702008-03-06 13:44:28 +11003653 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3654 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +11003655 __int64_t sbfields = 0;
3656
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003657 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +11003658 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3659 xfs_sb_version_addattr(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003660 sbfields |= XFS_SB_VERSIONNUM;
Nathan Scottd8cc8902005-11-02 10:34:53 +11003661 }
Eric Sandeen62118702008-03-06 13:44:28 +11003662 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3663 xfs_sb_version_addattr2(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003664 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003665 }
Nathan Scottda087ba2005-11-02 15:00:20 +11003666 if (sbfields) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003667 spin_unlock(&mp->m_sb_lock);
Nathan Scottda087ba2005-11-02 15:00:20 +11003668 xfs_mod_sb(tp, sbfields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003670 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 }
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003672 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 goto error2;
Dave Chinner713bf882010-03-08 11:26:23 +11003674 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 ASSERT(ip->i_df.if_ext_max ==
3676 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3677 return error;
3678error2:
3679 xfs_bmap_cancel(&flist);
3680error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3682error0:
3683 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3684 ASSERT(ip->i_df.if_ext_max ==
3685 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3686 return error;
3687}
3688
3689/*
3690 * Add the extent to the list of extents to be free at transaction end.
3691 * The list is maintained sorted (by block number).
3692 */
3693/* ARGSUSED */
3694void
3695xfs_bmap_add_free(
3696 xfs_fsblock_t bno, /* fs block number of extent */
3697 xfs_filblks_t len, /* length of extent */
3698 xfs_bmap_free_t *flist, /* list of extents */
3699 xfs_mount_t *mp) /* mount point structure */
3700{
3701 xfs_bmap_free_item_t *cur; /* current (next) element */
3702 xfs_bmap_free_item_t *new; /* new element */
3703 xfs_bmap_free_item_t *prev; /* previous element */
3704#ifdef DEBUG
3705 xfs_agnumber_t agno;
3706 xfs_agblock_t agbno;
3707
3708 ASSERT(bno != NULLFSBLOCK);
3709 ASSERT(len > 0);
3710 ASSERT(len <= MAXEXTLEN);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003711 ASSERT(!isnullstartblock(bno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 agno = XFS_FSB_TO_AGNO(mp, bno);
3713 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3714 ASSERT(agno < mp->m_sb.sb_agcount);
3715 ASSERT(agbno < mp->m_sb.sb_agblocks);
3716 ASSERT(len < mp->m_sb.sb_agblocks);
3717 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3718#endif
3719 ASSERT(xfs_bmap_free_item_zone != NULL);
3720 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3721 new->xbfi_startblock = bno;
3722 new->xbfi_blockcount = (xfs_extlen_t)len;
3723 for (prev = NULL, cur = flist->xbf_first;
3724 cur != NULL;
3725 prev = cur, cur = cur->xbfi_next) {
3726 if (cur->xbfi_startblock >= bno)
3727 break;
3728 }
3729 if (prev)
3730 prev->xbfi_next = new;
3731 else
3732 flist->xbf_first = new;
3733 new->xbfi_next = cur;
3734 flist->xbf_count++;
3735}
3736
3737/*
3738 * Compute and fill in the value of the maximum depth of a bmap btree
3739 * in this filesystem. Done once, during mount.
3740 */
3741void
3742xfs_bmap_compute_maxlevels(
3743 xfs_mount_t *mp, /* file system mount structure */
3744 int whichfork) /* data or attr fork */
3745{
3746 int level; /* btree level */
3747 uint maxblocks; /* max blocks at this level */
3748 uint maxleafents; /* max leaf entries possible */
3749 int maxrootrecs; /* max records in root block */
3750 int minleafrecs; /* min records in leaf block */
3751 int minnoderecs; /* min records in node block */
3752 int sz; /* root block size */
3753
3754 /*
3755 * The maximum number of extents in a file, hence the maximum
3756 * number of leaf entries, is controlled by the type of di_nextents
3757 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3758 * (a signed 16-bit number, xfs_aextnum_t).
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003759 *
3760 * Note that we can no longer assume that if we are in ATTR1 that
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003761 * the fork offset of all the inodes will be
3762 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3763 * with ATTR2 and then mounted back with ATTR1, keeping the
3764 * di_forkoff's fixed but probably at various positions. Therefore,
3765 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3766 * of a minimum size available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003768 if (whichfork == XFS_DATA_FORK) {
3769 maxleafents = MAXEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003770 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003771 } else {
3772 maxleafents = MAXAEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003773 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003774 }
Christoph Hellwig60197e82008-10-30 17:11:19 +11003775 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 minleafrecs = mp->m_bmap_dmnr[0];
3777 minnoderecs = mp->m_bmap_dmnr[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3779 for (level = 1; maxblocks > 1; level++) {
3780 if (maxblocks <= maxrootrecs)
3781 maxblocks = 1;
3782 else
3783 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3784 }
3785 mp->m_bm_maxlevels[whichfork] = level;
3786}
3787
3788/*
3789 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3790 * caller. Frees all the extents that need freeing, which must be done
3791 * last due to locking considerations. We never free any extents in
3792 * the first transaction. This is to allow the caller to make the first
3793 * transaction a synchronous one so that the pointers to the data being
3794 * broken in this transaction will be permanent before the data is actually
3795 * freed. This is necessary to prevent blocks from being reallocated
3796 * and written to before the free and reallocation are actually permanent.
3797 * We do not just make the first transaction synchronous here, because
3798 * there are more efficient ways to gain the same protection in some cases
3799 * (see the file truncation code).
3800 *
3801 * Return 1 if the given transaction was committed and a new one
3802 * started, and 0 otherwise in the committed parameter.
3803 */
3804/*ARGSUSED*/
3805int /* error */
3806xfs_bmap_finish(
3807 xfs_trans_t **tp, /* transaction pointer addr */
3808 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 int *committed) /* xact committed or not */
3810{
3811 xfs_efd_log_item_t *efd; /* extent free data */
3812 xfs_efi_log_item_t *efi; /* extent free intention */
3813 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003814 xfs_bmap_free_item_t *free; /* free extent item */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 unsigned int logres; /* new log reservation */
3816 unsigned int logcount; /* new log count */
3817 xfs_mount_t *mp; /* filesystem mount structure */
3818 xfs_bmap_free_item_t *next; /* next item on free list */
3819 xfs_trans_t *ntp; /* new transaction pointer */
3820
3821 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3822 if (flist->xbf_count == 0) {
3823 *committed = 0;
3824 return 0;
3825 }
3826 ntp = *tp;
3827 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3828 for (free = flist->xbf_first; free; free = free->xbfi_next)
3829 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3830 free->xbfi_blockcount);
3831 logres = ntp->t_log_res;
3832 logcount = ntp->t_log_count;
3833 ntp = xfs_trans_dup(*tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003834 error = xfs_trans_commit(*tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 *tp = ntp;
3836 *committed = 1;
3837 /*
3838 * We have a new transaction, so we should return committed=1,
3839 * even though we're returning an error.
3840 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003841 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 return error;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003843
3844 /*
3845 * transaction commit worked ok so we can drop the extra ticket
3846 * reference that we gained in xfs_trans_dup()
3847 */
3848 xfs_log_ticket_put(ntp->t_ticket);
3849
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3851 logcount)))
3852 return error;
3853 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3854 for (free = flist->xbf_first; free != NULL; free = next) {
3855 next = free->xbfi_next;
3856 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3857 free->xbfi_blockcount))) {
3858 /*
3859 * The bmap free list will be cleaned up at a
3860 * higher level. The EFI will be canceled when
3861 * this transaction is aborted.
3862 * Need to force shutdown here to make sure it
3863 * happens, since this transaction may not be
3864 * dirty yet.
3865 */
3866 mp = ntp->t_mountp;
3867 if (!XFS_FORCED_SHUTDOWN(mp))
3868 xfs_force_shutdown(mp,
3869 (error == EFSCORRUPTED) ?
Nathan Scott7d04a332006-06-09 14:58:38 +10003870 SHUTDOWN_CORRUPT_INCORE :
3871 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 return error;
3873 }
3874 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3875 free->xbfi_blockcount);
3876 xfs_bmap_del_free(flist, NULL, free);
3877 }
3878 return 0;
3879}
3880
3881/*
3882 * Free up any items left in the list.
3883 */
3884void
3885xfs_bmap_cancel(
3886 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3887{
3888 xfs_bmap_free_item_t *free; /* free list item */
3889 xfs_bmap_free_item_t *next;
3890
3891 if (flist->xbf_count == 0)
3892 return;
3893 ASSERT(flist->xbf_first != NULL);
3894 for (free = flist->xbf_first; free; free = next) {
3895 next = free->xbfi_next;
3896 xfs_bmap_del_free(flist, NULL, free);
3897 }
3898 ASSERT(flist->xbf_count == 0);
3899}
3900
3901/*
3902 * Returns the file-relative block number of the first unused block(s)
3903 * in the file with at least "len" logically contiguous blocks free.
3904 * This is the lowest-address hole if the file has holes, else the first block
3905 * past the end of file.
3906 * Return 0 if the file is currently local (in-inode).
3907 */
3908int /* error */
3909xfs_bmap_first_unused(
3910 xfs_trans_t *tp, /* transaction pointer */
3911 xfs_inode_t *ip, /* incore inode */
3912 xfs_extlen_t len, /* size of hole to find */
3913 xfs_fileoff_t *first_unused, /* unused block */
3914 int whichfork) /* data or attr fork */
3915{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003917 int idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 xfs_ifork_t *ifp; /* inode fork pointer */
3919 xfs_fileoff_t lastaddr; /* last block number seen */
3920 xfs_fileoff_t lowest; /* lowest useful block */
3921 xfs_fileoff_t max; /* starting useful block */
3922 xfs_fileoff_t off; /* offset for this block */
3923 xfs_extnum_t nextents; /* number of extent entries */
3924
3925 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3926 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3927 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3928 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3929 *first_unused = 0;
3930 return 0;
3931 }
3932 ifp = XFS_IFORK_PTR(ip, whichfork);
3933 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3934 (error = xfs_iread_extents(tp, ip, whichfork)))
3935 return error;
3936 lowest = *first_unused;
3937 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003938 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003939 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 off = xfs_bmbt_get_startoff(ep);
3941 /*
3942 * See if the hole before this extent will work.
3943 */
3944 if (off >= lowest + len && off - max >= len) {
3945 *first_unused = max;
3946 return 0;
3947 }
3948 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3949 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3950 }
3951 *first_unused = max;
3952 return 0;
3953}
3954
3955/*
3956 * Returns the file-relative block number of the last block + 1 before
3957 * last_block (input value) in the file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003958 * This is not based on i_size, it is based on the extent records.
3959 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 */
3961int /* error */
3962xfs_bmap_last_before(
3963 xfs_trans_t *tp, /* transaction pointer */
3964 xfs_inode_t *ip, /* incore inode */
3965 xfs_fileoff_t *last_block, /* last block */
3966 int whichfork) /* data or attr fork */
3967{
3968 xfs_fileoff_t bno; /* input file offset */
3969 int eof; /* hit end of file */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003970 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 int error; /* error return value */
3972 xfs_bmbt_irec_t got; /* current extent value */
3973 xfs_ifork_t *ifp; /* inode fork pointer */
3974 xfs_extnum_t lastx; /* last extent used */
3975 xfs_bmbt_irec_t prev; /* previous extent value */
3976
3977 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3978 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3979 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3980 return XFS_ERROR(EIO);
3981 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3982 *last_block = 0;
3983 return 0;
3984 }
3985 ifp = XFS_IFORK_PTR(ip, whichfork);
3986 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3987 (error = xfs_iread_extents(tp, ip, whichfork)))
3988 return error;
3989 bno = *last_block - 1;
3990 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3991 &prev);
3992 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3993 if (prev.br_startoff == NULLFILEOFF)
3994 *last_block = 0;
3995 else
3996 *last_block = prev.br_startoff + prev.br_blockcount;
3997 }
3998 /*
3999 * Otherwise *last_block is already the right answer.
4000 */
4001 return 0;
4002}
4003
4004/*
4005 * Returns the file-relative block number of the first block past eof in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004006 * the file. This is not based on i_size, it is based on the extent records.
4007 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 */
4009int /* error */
4010xfs_bmap_last_offset(
4011 xfs_trans_t *tp, /* transaction pointer */
4012 xfs_inode_t *ip, /* incore inode */
4013 xfs_fileoff_t *last_block, /* last block */
4014 int whichfork) /* data or attr fork */
4015{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004016 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 int error; /* error return value */
4018 xfs_ifork_t *ifp; /* inode fork pointer */
4019 xfs_extnum_t nextents; /* number of extent entries */
4020
4021 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4022 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4023 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4024 return XFS_ERROR(EIO);
4025 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4026 *last_block = 0;
4027 return 0;
4028 }
4029 ifp = XFS_IFORK_PTR(ip, whichfork);
4030 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4031 (error = xfs_iread_extents(tp, ip, whichfork)))
4032 return error;
4033 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4034 if (!nextents) {
4035 *last_block = 0;
4036 return 0;
4037 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004038 ep = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4040 return 0;
4041}
4042
4043/*
4044 * Returns whether the selected fork of the inode has exactly one
4045 * block or not. For the data fork we check this matches di_size,
4046 * implying the file's range is 0..bsize-1.
4047 */
4048int /* 1=>1 block, 0=>otherwise */
4049xfs_bmap_one_block(
4050 xfs_inode_t *ip, /* incore inode */
4051 int whichfork) /* data or attr fork */
4052{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004053 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 xfs_ifork_t *ifp; /* inode fork pointer */
4055 int rval; /* return value */
4056 xfs_bmbt_irec_t s; /* internal version of extent */
4057
4058#ifndef DEBUG
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004059 if (whichfork == XFS_DATA_FORK) {
4060 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4061 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4062 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064#endif /* !DEBUG */
4065 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4066 return 0;
4067 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4068 return 0;
4069 ifp = XFS_IFORK_PTR(ip, whichfork);
4070 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004071 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 xfs_bmbt_get_all(ep, &s);
4073 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4074 if (rval && whichfork == XFS_DATA_FORK)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004075 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 return rval;
4077}
4078
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004079STATIC int
4080xfs_bmap_sanity_check(
4081 struct xfs_mount *mp,
4082 struct xfs_buf *bp,
4083 int level)
4084{
4085 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4086
4087 if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
4088 be16_to_cpu(block->bb_level) != level ||
4089 be16_to_cpu(block->bb_numrecs) == 0 ||
4090 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4091 return 0;
4092 return 1;
4093}
4094
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095/*
4096 * Read in the extents to if_extents.
4097 * All inode fields are set up by caller, we just traverse the btree
4098 * and copy the records in. If the file system cannot contain unwritten
4099 * extents, the records are checked for no "state" flags.
4100 */
4101int /* error */
4102xfs_bmap_read_extents(
4103 xfs_trans_t *tp, /* transaction pointer */
4104 xfs_inode_t *ip, /* incore inode */
4105 int whichfork) /* data or attr fork */
4106{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004107 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 xfs_fsblock_t bno; /* block # of "block" */
4109 xfs_buf_t *bp; /* buffer for "block" */
4110 int error; /* error return value */
4111 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 xfs_extnum_t i, j; /* index into the extents list */
4113 xfs_ifork_t *ifp; /* fork structure */
4114 int level; /* btree level, for checking */
4115 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10004116 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 /* REFERENCED */
4118 xfs_extnum_t room; /* number of entries there's room for */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
4120 bno = NULLFSBLOCK;
4121 mp = ip->i_mount;
4122 ifp = XFS_IFORK_PTR(ip, whichfork);
4123 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4124 XFS_EXTFMT_INODE(ip);
4125 block = ifp->if_broot;
4126 /*
4127 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4128 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004129 level = be16_to_cpu(block->bb_level);
4130 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11004131 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004132 bno = be64_to_cpu(*pp);
4133 ASSERT(bno != NULLDFSBNO);
4134 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4135 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 /*
4137 * Go down the tree until leaf level is reached, following the first
4138 * pointer (leftmost) at each level.
4139 */
4140 while (level-- > 0) {
4141 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4142 XFS_BMAP_BTREE_REF)))
4143 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004144 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004146 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 error0);
4148 if (level == 0)
4149 break;
Christoph Hellwig136341b2008-10-30 17:11:40 +11004150 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004151 bno = be64_to_cpu(*pp);
4152 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 xfs_trans_brelse(tp, bp);
4154 }
4155 /*
4156 * Here with bp and block set to the leftmost leaf node in the tree.
4157 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004158 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 i = 0;
4160 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004161 * Loop over all leaf nodes. Copy information to the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 */
4163 for (;;) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004164 xfs_bmbt_rec_t *frp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 xfs_fsblock_t nextbno;
4166 xfs_extnum_t num_recs;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004167 xfs_extnum_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
4169
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004170 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 if (unlikely(i + num_recs > room)) {
4172 ASSERT(i + num_recs <= room);
Nathan Scott3762ec62006-01-12 10:29:53 +11004173 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4174 "corrupt dinode %Lu, (btree extents).",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 (unsigned long long) ip->i_ino);
4176 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4177 XFS_ERRLEVEL_LOW,
4178 ip->i_mount);
4179 goto error0;
4180 }
4181 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004182 xfs_bmap_sanity_check(mp, bp, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 error0);
4184 /*
4185 * Read-ahead the next leaf block, if any.
4186 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004187 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 if (nextbno != NULLFSBLOCK)
4189 xfs_btree_reada_bufl(mp, nextbno, 1);
4190 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004191 * Copy records into the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11004193 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004194 start = i;
4195 for (j = 0; j < num_recs; j++, i++, frp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004196 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10004197 trp->l0 = be64_to_cpu(frp->l0);
4198 trp->l1 = be64_to_cpu(frp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 }
4200 if (exntf == XFS_EXTFMT_NOSTATE) {
4201 /*
4202 * Check all attribute bmap btree records and
4203 * any "older" data bmap btree records for a
4204 * set bit in the "extent flag" position.
4205 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004206 if (unlikely(xfs_check_nostate_extents(ifp,
4207 start, num_recs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4209 XFS_ERRLEVEL_LOW,
4210 ip->i_mount);
4211 goto error0;
4212 }
4213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 xfs_trans_brelse(tp, bp);
4215 bno = nextbno;
4216 /*
4217 * If we've reached the end, stop.
4218 */
4219 if (bno == NULLFSBLOCK)
4220 break;
4221 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4222 XFS_BMAP_BTREE_REF)))
4223 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004224 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004226 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
Eric Sandeen3a59c942007-07-11 11:09:47 +10004228 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 return 0;
4230error0:
4231 xfs_trans_brelse(tp, bp);
4232 return XFS_ERROR(EFSCORRUPTED);
4233}
4234
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004235#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004237 * Add bmap trace insert entries for all the contents of the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 */
4239void
4240xfs_bmap_trace_exlist(
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 xfs_inode_t *ip, /* incore inode pointer */
4242 xfs_extnum_t cnt, /* count of entries in the list */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004243 int whichfork, /* data or attr fork */
4244 unsigned long caller_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004246 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004248 int state = 0;
4249
4250 if (whichfork == XFS_ATTR_FORK)
4251 state |= BMAP_ATTRFORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
4253 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004254 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004255 for (idx = 0; idx < cnt; idx++)
4256 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259/*
4260 * Validate that the bmbt_irecs being returned from bmapi are valid
4261 * given the callers original parameters. Specifically check the
4262 * ranges of the returned irecs to ensure that they only extent beyond
4263 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4264 */
4265STATIC void
4266xfs_bmap_validate_ret(
4267 xfs_fileoff_t bno,
4268 xfs_filblks_t len,
4269 int flags,
4270 xfs_bmbt_irec_t *mval,
4271 int nmap,
4272 int ret_nmap)
4273{
4274 int i; /* index to map values */
4275
4276 ASSERT(ret_nmap <= nmap);
4277
4278 for (i = 0; i < ret_nmap; i++) {
4279 ASSERT(mval[i].br_blockcount > 0);
4280 if (!(flags & XFS_BMAPI_ENTIRE)) {
4281 ASSERT(mval[i].br_startoff >= bno);
4282 ASSERT(mval[i].br_blockcount <= len);
4283 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4284 bno + len);
4285 } else {
4286 ASSERT(mval[i].br_startoff < bno + len);
4287 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4288 bno);
4289 }
4290 ASSERT(i == 0 ||
4291 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4292 mval[i].br_startoff);
4293 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4294 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4295 mval[i].br_startblock != HOLESTARTBLOCK);
4296 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4297 mval[i].br_state == XFS_EXT_UNWRITTEN);
4298 }
4299}
4300#endif /* DEBUG */
4301
4302
4303/*
4304 * Map file blocks to filesystem blocks.
4305 * File range is given by the bno/len pair.
4306 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4307 * into a hole or past eof.
4308 * Only allocates blocks from a single allocation group,
4309 * to avoid locking problems.
4310 * The returned value in "firstblock" from the first call in a transaction
4311 * must be remembered and presented to subsequent calls in "firstblock".
4312 * An upper bound for the number of blocks to be allocated is supplied to
4313 * the first call in "total"; if no allocation group has that many free
4314 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4315 */
4316int /* error */
4317xfs_bmapi(
4318 xfs_trans_t *tp, /* transaction pointer */
4319 xfs_inode_t *ip, /* incore inode */
4320 xfs_fileoff_t bno, /* starting file offs. mapped */
4321 xfs_filblks_t len, /* length to map in file */
4322 int flags, /* XFS_BMAPI_... */
4323 xfs_fsblock_t *firstblock, /* first allocated block
4324 controls a.g. for allocs */
4325 xfs_extlen_t total, /* total blocks needed */
4326 xfs_bmbt_irec_t *mval, /* output: map values */
4327 int *nmap, /* i/o: mval size/count */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004328 xfs_bmap_free_t *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
4330 xfs_fsblock_t abno; /* allocated block number */
4331 xfs_extlen_t alen; /* allocated extent length */
4332 xfs_fileoff_t aoff; /* allocated file offset */
Dave Chinnerf0a0eaa2010-01-20 10:50:06 +11004333 xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 xfs_btree_cur_t *cur; /* bmap btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 xfs_fileoff_t end; /* end of mapped file region */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004336 int eof; /* we've hit the end of extents */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004337 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004339 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 xfs_ifork_t *ifp; /* inode fork pointer */
4341 xfs_extlen_t indlen; /* indirect blocks length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 xfs_extnum_t lastx; /* last useful extent number */
4343 int logflags; /* flags for transaction logging */
4344 xfs_extlen_t minleft; /* min blocks left after allocation */
4345 xfs_extlen_t minlen; /* min allocation size */
4346 xfs_mount_t *mp; /* xfs mount structure */
4347 int n; /* current extent index */
Malcolm Parsons9da096f2009-03-29 09:55:42 +02004348 int nallocs; /* number of extents alloc'd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 xfs_extnum_t nextents; /* number of extents in file */
4350 xfs_fileoff_t obno; /* old block number (offset) */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004351 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 int tmp_logflags; /* temp flags holder */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004353 int whichfork; /* data or attr fork */
4354 char inhole; /* current location is hole in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 char wasdelay; /* old extent was delayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 char wr; /* this is a write request */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004357 char rt; /* this is a realtime file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358#ifdef DEBUG
4359 xfs_fileoff_t orig_bno; /* original block number value */
4360 int orig_flags; /* original flags arg value */
4361 xfs_filblks_t orig_len; /* original value of len arg */
4362 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4363 int orig_nmap; /* original value of *nmap */
4364
4365 orig_bno = bno;
4366 orig_len = len;
4367 orig_flags = flags;
4368 orig_mval = mval;
4369 orig_nmap = *nmap;
4370#endif
4371 ASSERT(*nmap >= 1);
4372 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4373 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4374 XFS_ATTR_FORK : XFS_DATA_FORK;
4375 mp = ip->i_mount;
4376 if (unlikely(XFS_TEST_ERROR(
4377 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4378 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4379 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4380 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4381 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4382 return XFS_ERROR(EFSCORRUPTED);
4383 }
4384 if (XFS_FORCED_SHUTDOWN(mp))
4385 return XFS_ERROR(EIO);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004386 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 ifp = XFS_IFORK_PTR(ip, whichfork);
4388 ASSERT(ifp->if_ext_max ==
4389 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4390 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4391 XFS_STATS_INC(xs_blk_mapw);
4392 else
4393 XFS_STATS_INC(xs_blk_mapr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 /*
Nathan Scott39269e22006-03-14 13:33:50 +11004395 * IGSTATE flag is used to combine extents which
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 * differ only due to the state of the extents.
4397 * This technique is used from xfs_getbmap()
4398 * when the caller does not wish to see the
4399 * separation (which is the default).
4400 *
4401 * This technique is also used when writing a
4402 * buffer which has been partially written,
4403 * (usually by being flushed during a chunkread),
4404 * to ensure one write takes place. This also
4405 * prevents a change in the xfs inode extents at
4406 * this time, intentionally. This change occurs
4407 * on completion of the write operation, in
4408 * xfs_strat_comp(), where the xfs_bmapi() call
4409 * is transactioned, and the extents combined.
4410 */
Nathan Scott39269e22006-03-14 13:33:50 +11004411 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4412 wr = 0; /* no allocations are allowed */
4413 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 logflags = 0;
4415 nallocs = 0;
4416 cur = NULL;
4417 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4418 ASSERT(wr && tp);
4419 if ((error = xfs_bmap_local_to_extents(tp, ip,
4420 firstblock, total, &logflags, whichfork)))
4421 goto error0;
4422 }
4423 if (wr && *firstblock == NULLFSBLOCK) {
4424 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Christoph Hellwig16259e72005-11-02 15:11:25 +11004425 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 else
4427 minleft = 1;
4428 } else
4429 minleft = 0;
4430 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4431 (error = xfs_iread_extents(tp, ip, whichfork)))
4432 goto error0;
4433 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4434 &prev);
4435 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4436 n = 0;
4437 end = bno + len;
4438 obno = bno;
4439 bma.ip = NULL;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004440
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 while (bno < end && n < *nmap) {
4442 /*
4443 * Reading past eof, act as though there's a hole
4444 * up to end.
4445 */
4446 if (eof && !wr)
4447 got.br_startoff = end;
4448 inhole = eof || got.br_startoff > bno;
Nathan Scott39269e22006-03-14 13:33:50 +11004449 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06004450 isnullstartblock(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 /*
4452 * First, deal with the hole before the allocated space
4453 * that we found, if any.
4454 */
4455 if (wr && (inhole || wasdelay)) {
4456 /*
4457 * For the wasdelay case, we could also just
4458 * allocate the stuff asked for in this bmap call
4459 * but that wouldn't be as good.
4460 */
Christoph Hellwigcd8b0bb2010-06-23 18:11:15 +10004461 if (wasdelay) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 alen = (xfs_extlen_t)got.br_blockcount;
4463 aoff = got.br_startoff;
4464 if (lastx != NULLEXTNUM && lastx) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004465 ep = xfs_iext_get_ext(ifp, lastx - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 xfs_bmbt_get_all(ep, &prev);
4467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 } else {
4469 alen = (xfs_extlen_t)
4470 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4471 if (!eof)
4472 alen = (xfs_extlen_t)
4473 XFS_FILBLKS_MIN(alen,
4474 got.br_startoff - bno);
4475 aoff = bno;
4476 }
Nathan Scott39269e22006-03-14 13:33:50 +11004477 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4478 if (flags & XFS_BMAPI_DELAY) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11004479 xfs_extlen_t extsz;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004480
4481 /* Figure out the extent size, adjust alen */
David Chinner957d0eb2007-06-18 16:50:37 +10004482 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004483 if (extsz) {
4484 error = xfs_bmap_extsize_align(mp,
4485 &got, &prev, extsz,
Nathan Scott39269e22006-03-14 13:33:50 +11004486 rt, eof,
4487 flags&XFS_BMAPI_DELAY,
4488 flags&XFS_BMAPI_CONVERT,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004489 &aoff, &alen);
4490 ASSERT(!error);
4491 }
4492
4493 if (rt)
4494 extsz = alen / mp->m_sb.sb_rextsize;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004495
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 /*
4497 * Make a transaction-less quota reservation for
4498 * delayed allocation blocks. This number gets
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004499 * adjusted later. We return if we haven't
4500 * allocated blocks already inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02004502 error = xfs_trans_reserve_quota_nblks(
4503 NULL, ip, (long)alen, 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004504 rt ? XFS_QMOPT_RES_RTBLKS :
Christoph Hellwig7d095252009-06-08 15:33:32 +02004505 XFS_QMOPT_RES_REGBLKS);
4506 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 if (n == 0) {
4508 *nmap = 0;
4509 ASSERT(cur == NULL);
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004510 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 }
4512 break;
4513 }
4514
4515 /*
4516 * Split changing sb for alen and indlen since
4517 * they could be coming from different places.
4518 */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004519 indlen = (xfs_extlen_t)
4520 xfs_bmap_worst_indlen(ip, alen);
4521 ASSERT(indlen > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
Nathan Scottdd9f4382006-01-11 15:28:28 +11004523 if (rt) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004524 error = xfs_mod_incore_sb(mp,
4525 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004526 -((int64_t)extsz), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004527 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004528 } else {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004529 error = xfs_mod_incore_sb(mp,
4530 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004531 -((int64_t)alen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004532 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004533 }
David Chinner3bdbfb12005-09-02 16:40:47 +10004534 if (!error) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004535 error = xfs_mod_incore_sb(mp,
4536 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004537 -((int64_t)indlen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004538 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004539 if (error && rt)
4540 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004541 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004542 (int64_t)extsz, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004543 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004544 else if (error)
4545 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004546 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004547 (int64_t)alen, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004548 XFS_BMAPI_RSVBLOCKS));
David Chinner3bdbfb12005-09-02 16:40:47 +10004549 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004550
4551 if (error) {
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004552 if (XFS_IS_QUOTA_ON(mp))
Nathan Scott06d10dd2005-06-21 15:48:47 +10004553 /* unreserve the blocks now */
Nathan Scottdd9f4382006-01-11 15:28:28 +11004554 (void)
Christoph Hellwig7d095252009-06-08 15:33:32 +02004555 xfs_trans_unreserve_quota_nblks(
4556 NULL, ip,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004557 (long)alen, 0, rt ?
4558 XFS_QMOPT_RES_RTBLKS :
4559 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 break;
4561 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004562
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 ip->i_delayed_blks += alen;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004564 abno = nullstartblock(indlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 } else {
4566 /*
4567 * If first time, allocate and fill in
4568 * once-only bma fields.
4569 */
4570 if (bma.ip == NULL) {
4571 bma.tp = tp;
4572 bma.ip = ip;
4573 bma.prevp = &prev;
4574 bma.gotp = &got;
4575 bma.total = total;
4576 bma.userdata = 0;
4577 }
4578 /* Indicate if this is the first user data
4579 * in the file, or just any user data.
4580 */
Nathan Scott39269e22006-03-14 13:33:50 +11004581 if (!(flags & XFS_BMAPI_METADATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 bma.userdata = (aoff == 0) ?
4583 XFS_ALLOC_INITIAL_USER_DATA :
4584 XFS_ALLOC_USERDATA;
4585 }
4586 /*
4587 * Fill in changeable bma fields.
4588 */
4589 bma.eof = eof;
4590 bma.firstblock = *firstblock;
4591 bma.alen = alen;
4592 bma.off = aoff;
Adrian Bunk72880262006-08-30 13:41:58 +10004593 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 bma.wasdel = wasdelay;
4595 bma.minlen = minlen;
4596 bma.low = flist->xbf_low;
4597 bma.minleft = minleft;
4598 /*
4599 * Only want to do the alignment at the
4600 * eof if it is userdata and allocation length
4601 * is larger than a stripe unit.
4602 */
4603 if (mp->m_dalign && alen >= mp->m_dalign &&
Nathan Scott39269e22006-03-14 13:33:50 +11004604 (!(flags & XFS_BMAPI_METADATA)) &&
4605 (whichfork == XFS_DATA_FORK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 if ((error = xfs_bmap_isaeof(ip, aoff,
4607 whichfork, &bma.aeof)))
4608 goto error0;
4609 } else
4610 bma.aeof = 0;
4611 /*
4612 * Call allocator.
4613 */
4614 if ((error = xfs_bmap_alloc(&bma)))
4615 goto error0;
4616 /*
4617 * Copy out result fields.
4618 */
4619 abno = bma.rval;
4620 if ((flist->xbf_low = bma.low))
4621 minleft = 0;
4622 alen = bma.alen;
4623 aoff = bma.off;
4624 ASSERT(*firstblock == NULLFSBLOCK ||
4625 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4626 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4627 (flist->xbf_low &&
4628 XFS_FSB_TO_AGNO(mp, *firstblock) <
4629 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4630 *firstblock = bma.firstblock;
4631 if (cur)
4632 cur->bc_private.b.firstblock =
4633 *firstblock;
4634 if (abno == NULLFSBLOCK)
4635 break;
4636 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11004637 cur = xfs_bmbt_init_cursor(mp, tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 ip, whichfork);
4639 cur->bc_private.b.firstblock =
4640 *firstblock;
4641 cur->bc_private.b.flist = flist;
4642 }
4643 /*
4644 * Bump the number of extents we've allocated
4645 * in this call.
4646 */
4647 nallocs++;
4648 }
4649 if (cur)
4650 cur->bc_private.b.flags =
4651 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4652 got.br_startoff = aoff;
4653 got.br_startblock = abno;
4654 got.br_blockcount = alen;
4655 got.br_state = XFS_EXT_NORM; /* assume normal */
4656 /*
4657 * Determine state of extent, and the filesystem.
4658 * A wasdelay extent has been initialized, so
4659 * shouldn't be flagged as unwritten.
4660 */
Eric Sandeen62118702008-03-06 13:44:28 +11004661 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
4663 got.br_state = XFS_EXT_UNWRITTEN;
4664 }
4665 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004666 firstblock, flist, &tmp_logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004667 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 logflags |= tmp_logflags;
4669 if (error)
4670 goto error0;
4671 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004672 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4674 xfs_bmbt_get_all(ep, &got);
4675 ASSERT(got.br_startoff <= aoff);
4676 ASSERT(got.br_startoff + got.br_blockcount >=
4677 aoff + alen);
4678#ifdef DEBUG
Nathan Scott39269e22006-03-14 13:33:50 +11004679 if (flags & XFS_BMAPI_DELAY) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06004680 ASSERT(isnullstartblock(got.br_startblock));
4681 ASSERT(startblockval(got.br_startblock) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 }
4683 ASSERT(got.br_state == XFS_EXT_NORM ||
4684 got.br_state == XFS_EXT_UNWRITTEN);
4685#endif
4686 /*
4687 * Fall down into the found allocated space case.
4688 */
4689 } else if (inhole) {
4690 /*
4691 * Reading in a hole.
4692 */
4693 mval->br_startoff = bno;
4694 mval->br_startblock = HOLESTARTBLOCK;
4695 mval->br_blockcount =
4696 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4697 mval->br_state = XFS_EXT_NORM;
4698 bno += mval->br_blockcount;
4699 len -= mval->br_blockcount;
4700 mval++;
4701 n++;
4702 continue;
4703 }
4704 /*
4705 * Then deal with the allocated space we found.
4706 */
4707 ASSERT(ep != NULL);
Nathan Scott39269e22006-03-14 13:33:50 +11004708 if (!(flags & XFS_BMAPI_ENTIRE) &&
4709 (got.br_startoff + got.br_blockcount > obno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 if (obno > bno)
4711 bno = obno;
4712 ASSERT((bno >= obno) || (n == 0));
4713 ASSERT(bno < end);
4714 mval->br_startoff = bno;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004715 if (isnullstartblock(got.br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11004716 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 mval->br_startblock = DELAYSTARTBLOCK;
4718 } else
4719 mval->br_startblock =
4720 got.br_startblock +
4721 (bno - got.br_startoff);
4722 /*
4723 * Return the minimum of what we got and what we
4724 * asked for for the length. We can use the len
4725 * variable here because it is modified below
4726 * and we could have been there before coming
4727 * here if the first part of the allocation
4728 * didn't overlap what was asked for.
4729 */
4730 mval->br_blockcount =
4731 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
4732 (bno - got.br_startoff));
4733 mval->br_state = got.br_state;
4734 ASSERT(mval->br_blockcount <= len);
4735 } else {
4736 *mval = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004737 if (isnullstartblock(mval->br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11004738 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 mval->br_startblock = DELAYSTARTBLOCK;
4740 }
4741 }
4742
4743 /*
4744 * Check if writing previously allocated but
4745 * unwritten extents.
4746 */
4747 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
4748 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
4749 /*
4750 * Modify (by adding) the state flag, if writing.
4751 */
4752 ASSERT(mval->br_blockcount <= len);
4753 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11004754 cur = xfs_bmbt_init_cursor(mp,
4755 tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 cur->bc_private.b.firstblock =
4757 *firstblock;
4758 cur->bc_private.b.flist = flist;
4759 }
4760 mval->br_state = XFS_EXT_NORM;
4761 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004762 firstblock, flist, &tmp_logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004763 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 logflags |= tmp_logflags;
4765 if (error)
4766 goto error0;
4767 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004768 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4770 xfs_bmbt_get_all(ep, &got);
4771 /*
4772 * We may have combined previously unwritten
4773 * space with written space, so generate
4774 * another request.
4775 */
4776 if (mval->br_blockcount < len)
4777 continue;
4778 }
4779
Nathan Scott39269e22006-03-14 13:33:50 +11004780 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 ((mval->br_startoff + mval->br_blockcount) <= end));
Nathan Scott39269e22006-03-14 13:33:50 +11004782 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4783 (mval->br_blockcount <= len) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 (mval->br_startoff < obno));
4785 bno = mval->br_startoff + mval->br_blockcount;
4786 len = end - bno;
4787 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4788 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4789 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4790 ASSERT(mval->br_state == mval[-1].br_state);
4791 mval[-1].br_blockcount = mval->br_blockcount;
4792 mval[-1].br_state = mval->br_state;
4793 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4794 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4795 mval[-1].br_startblock != HOLESTARTBLOCK &&
4796 mval->br_startblock ==
4797 mval[-1].br_startblock + mval[-1].br_blockcount &&
Nathan Scott39269e22006-03-14 13:33:50 +11004798 ((flags & XFS_BMAPI_IGSTATE) ||
4799 mval[-1].br_state == mval->br_state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 ASSERT(mval->br_startoff ==
4801 mval[-1].br_startoff + mval[-1].br_blockcount);
4802 mval[-1].br_blockcount += mval->br_blockcount;
4803 } else if (n > 0 &&
4804 mval->br_startblock == DELAYSTARTBLOCK &&
4805 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4806 mval->br_startoff ==
4807 mval[-1].br_startoff + mval[-1].br_blockcount) {
4808 mval[-1].br_blockcount += mval->br_blockcount;
4809 mval[-1].br_state = mval->br_state;
4810 } else if (!((n == 0) &&
4811 ((mval->br_startoff + mval->br_blockcount) <=
4812 obno))) {
4813 mval++;
4814 n++;
4815 }
4816 /*
4817 * If we're done, stop now. Stop when we've allocated
4818 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4819 * the transaction may get too big.
4820 */
4821 if (bno >= end || n >= *nmap || nallocs >= *nmap)
4822 break;
4823 /*
4824 * Else go on to the next record.
4825 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004826 ep = xfs_iext_get_ext(ifp, ++lastx);
David Chinner4e5ae832007-06-05 16:24:15 +10004827 prev = got;
4828 if (lastx >= nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 eof = 1;
David Chinner4e5ae832007-06-05 16:24:15 +10004830 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 xfs_bmbt_get_all(ep, &got);
4832 }
4833 ifp->if_lastex = lastx;
4834 *nmap = n;
4835 /*
4836 * Transform from btree to extents, give it cur.
4837 */
4838 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4839 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4840 ASSERT(wr && cur);
4841 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4842 &tmp_logflags, whichfork);
4843 logflags |= tmp_logflags;
4844 if (error)
4845 goto error0;
4846 }
4847 ASSERT(ifp->if_ext_max ==
4848 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4849 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4850 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
4851 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852error0:
4853 /*
4854 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004855 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06004857 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06004859 logflags &= ~xfs_ilog_fext(whichfork);
4860 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06004862 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 /*
4864 * Log whatever the flags say, even if error. Otherwise we might miss
4865 * detecting a case where the data is changed, there's an error,
4866 * and it's not logged so we don't shutdown when we should.
4867 */
4868 if (logflags) {
4869 ASSERT(tp && wr);
4870 xfs_trans_log_inode(tp, ip, logflags);
4871 }
4872 if (cur) {
4873 if (!error) {
4874 ASSERT(*firstblock == NULLFSBLOCK ||
4875 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4876 XFS_FSB_TO_AGNO(mp,
4877 cur->bc_private.b.firstblock) ||
4878 (flist->xbf_low &&
4879 XFS_FSB_TO_AGNO(mp, *firstblock) <
4880 XFS_FSB_TO_AGNO(mp,
4881 cur->bc_private.b.firstblock)));
4882 *firstblock = cur->bc_private.b.firstblock;
4883 }
4884 xfs_btree_del_cursor(cur,
4885 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4886 }
4887 if (!error)
4888 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4889 orig_nmap, *nmap);
4890 return error;
4891}
4892
4893/*
4894 * Map file blocks to filesystem blocks, simple version.
4895 * One block (extent) only, read-only.
4896 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
4897 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
4898 * was set and all the others were clear.
4899 */
4900int /* error */
4901xfs_bmapi_single(
4902 xfs_trans_t *tp, /* transaction pointer */
4903 xfs_inode_t *ip, /* incore inode */
4904 int whichfork, /* data or attr fork */
4905 xfs_fsblock_t *fsb, /* output: mapped block */
4906 xfs_fileoff_t bno) /* starting file offs. mapped */
4907{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004908 int eof; /* we've hit the end of extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004910 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 xfs_ifork_t *ifp; /* inode fork pointer */
4912 xfs_extnum_t lastx; /* last useful extent number */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004913 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
4915 ifp = XFS_IFORK_PTR(ip, whichfork);
4916 if (unlikely(
4917 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4918 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
4919 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
4920 ip->i_mount);
4921 return XFS_ERROR(EFSCORRUPTED);
4922 }
4923 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
4924 return XFS_ERROR(EIO);
4925 XFS_STATS_INC(xs_blk_mapr);
4926 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4927 (error = xfs_iread_extents(tp, ip, whichfork)))
4928 return error;
4929 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4930 &prev);
4931 /*
4932 * Reading past eof, act as though there's a hole
4933 * up to end.
4934 */
4935 if (eof || got.br_startoff > bno) {
4936 *fsb = NULLFSBLOCK;
4937 return 0;
4938 }
Eric Sandeen9d87c312009-01-14 23:22:07 -06004939 ASSERT(!isnullstartblock(got.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 ASSERT(bno < got.br_startoff + got.br_blockcount);
4941 *fsb = got.br_startblock + (bno - got.br_startoff);
4942 ifp->if_lastex = lastx;
4943 return 0;
4944}
4945
4946/*
4947 * Unmap (remove) blocks from a file.
4948 * If nexts is nonzero then the number of extents to remove is limited to
4949 * that value. If not all extents in the block range can be removed then
4950 * *done is set.
4951 */
4952int /* error */
4953xfs_bunmapi(
4954 xfs_trans_t *tp, /* transaction pointer */
4955 struct xfs_inode *ip, /* incore inode */
4956 xfs_fileoff_t bno, /* starting offset to unmap */
4957 xfs_filblks_t len, /* length to unmap in file */
4958 int flags, /* misc flags */
4959 xfs_extnum_t nexts, /* number of extents max */
4960 xfs_fsblock_t *firstblock, /* first allocated block
4961 controls a.g. for allocs */
4962 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4963 int *done) /* set if not done yet */
4964{
4965 xfs_btree_cur_t *cur; /* bmap btree cursor */
4966 xfs_bmbt_irec_t del; /* extent being deleted */
4967 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004968 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 int error; /* error return value */
4970 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004971 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 xfs_ifork_t *ifp; /* inode fork pointer */
4973 int isrt; /* freeing in rt area */
4974 xfs_extnum_t lastx; /* last extent index used */
4975 int logflags; /* transaction logging flags */
4976 xfs_extlen_t mod; /* rt extent offset */
4977 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004978 xfs_extnum_t nextents; /* number of file extents */
4979 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 xfs_fileoff_t start; /* first file offset deleted */
4981 int tmp_logflags; /* partial logging flags */
4982 int wasdel; /* was a delayed alloc extent */
4983 int whichfork; /* data or attribute fork */
4984 int rsvd; /* OK to allocate reserved blocks */
4985 xfs_fsblock_t sum;
4986
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004987 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
4988
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4990 XFS_ATTR_FORK : XFS_DATA_FORK;
4991 ifp = XFS_IFORK_PTR(ip, whichfork);
4992 if (unlikely(
4993 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4994 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
4995 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
4996 ip->i_mount);
4997 return XFS_ERROR(EFSCORRUPTED);
4998 }
4999 mp = ip->i_mount;
5000 if (XFS_FORCED_SHUTDOWN(mp))
5001 return XFS_ERROR(EIO);
5002 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5003 ASSERT(len > 0);
5004 ASSERT(nexts >= 0);
5005 ASSERT(ifp->if_ext_max ==
5006 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5007 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5008 (error = xfs_iread_extents(tp, ip, whichfork)))
5009 return error;
5010 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5011 if (nextents == 0) {
5012 *done = 1;
5013 return 0;
5014 }
5015 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005016 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 start = bno;
5018 bno = start + len - 1;
5019 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5020 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005021
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 /*
5023 * Check to see if the given block number is past the end of the
5024 * file, back up to the last block if so...
5025 */
5026 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005027 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 xfs_bmbt_get_all(ep, &got);
5029 bno = got.br_startoff + got.br_blockcount - 1;
5030 }
5031 logflags = 0;
5032 if (ifp->if_flags & XFS_IFBROOT) {
5033 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005034 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 cur->bc_private.b.firstblock = *firstblock;
5036 cur->bc_private.b.flist = flist;
5037 cur->bc_private.b.flags = 0;
5038 } else
5039 cur = NULL;
5040 extno = 0;
5041 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5042 (nexts == 0 || extno < nexts)) {
5043 /*
5044 * Is the found extent after a hole in which bno lives?
5045 * Just back up to the previous extent, if so.
5046 */
5047 if (got.br_startoff > bno) {
5048 if (--lastx < 0)
5049 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005050 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 xfs_bmbt_get_all(ep, &got);
5052 }
5053 /*
5054 * Is the last block of this extent before the range
5055 * we're supposed to delete? If so, we're done.
5056 */
5057 bno = XFS_FILEOFF_MIN(bno,
5058 got.br_startoff + got.br_blockcount - 1);
5059 if (bno < start)
5060 break;
5061 /*
5062 * Then deal with the (possibly delayed) allocated space
5063 * we found.
5064 */
5065 ASSERT(ep != NULL);
5066 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005067 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 if (got.br_startoff < start) {
5069 del.br_startoff = start;
5070 del.br_blockcount -= start - got.br_startoff;
5071 if (!wasdel)
5072 del.br_startblock += start - got.br_startoff;
5073 }
5074 if (del.br_startoff + del.br_blockcount > bno + 1)
5075 del.br_blockcount = bno + 1 - del.br_startoff;
5076 sum = del.br_startblock + del.br_blockcount;
5077 if (isrt &&
5078 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5079 /*
5080 * Realtime extent not lined up at the end.
5081 * The extent could have been split into written
5082 * and unwritten pieces, or we could just be
5083 * unmapping part of it. But we can't really
5084 * get rid of part of a realtime extent.
5085 */
5086 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005087 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 /*
5089 * This piece is unwritten, or we're not
5090 * using unwritten extents. Skip over it.
5091 */
5092 ASSERT(bno >= mod);
5093 bno -= mod > del.br_blockcount ?
5094 del.br_blockcount : mod;
5095 if (bno < got.br_startoff) {
5096 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005097 xfs_bmbt_get_all(xfs_iext_get_ext(
5098 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 }
5100 continue;
5101 }
5102 /*
5103 * It's written, turn it unwritten.
5104 * This is better than zeroing it.
5105 */
5106 ASSERT(del.br_state == XFS_EXT_NORM);
5107 ASSERT(xfs_trans_get_block_res(tp) > 0);
5108 /*
5109 * If this spans a realtime extent boundary,
5110 * chop it back to the start of the one we end at.
5111 */
5112 if (del.br_blockcount > mod) {
5113 del.br_startoff += del.br_blockcount - mod;
5114 del.br_startblock += del.br_blockcount - mod;
5115 del.br_blockcount = mod;
5116 }
5117 del.br_state = XFS_EXT_UNWRITTEN;
5118 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005119 firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005120 XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 if (error)
5122 goto error0;
5123 goto nodelete;
5124 }
5125 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5126 /*
5127 * Realtime extent is lined up at the end but not
5128 * at the front. We'll get rid of full extents if
5129 * we can.
5130 */
5131 mod = mp->m_sb.sb_rextsize - mod;
5132 if (del.br_blockcount > mod) {
5133 del.br_blockcount -= mod;
5134 del.br_startoff += mod;
5135 del.br_startblock += mod;
5136 } else if ((del.br_startoff == start &&
5137 (del.br_state == XFS_EXT_UNWRITTEN ||
5138 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005139 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 /*
5141 * Can't make it unwritten. There isn't
5142 * a full extent here so just skip it.
5143 */
5144 ASSERT(bno >= del.br_blockcount);
5145 bno -= del.br_blockcount;
5146 if (bno < got.br_startoff) {
5147 if (--lastx >= 0)
5148 xfs_bmbt_get_all(--ep, &got);
5149 }
5150 continue;
5151 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5152 /*
5153 * This one is already unwritten.
5154 * It must have a written left neighbor.
5155 * Unwrite the killed part of that one and
5156 * try again.
5157 */
5158 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005159 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5160 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005162 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 ASSERT(del.br_startblock ==
5164 prev.br_startblock + prev.br_blockcount);
5165 if (prev.br_startoff < start) {
5166 mod = start - prev.br_startoff;
5167 prev.br_blockcount -= mod;
5168 prev.br_startblock += mod;
5169 prev.br_startoff = start;
5170 }
5171 prev.br_state = XFS_EXT_UNWRITTEN;
5172 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5173 &prev, firstblock, flist, &logflags,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005174 XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 if (error)
5176 goto error0;
5177 goto nodelete;
5178 } else {
5179 ASSERT(del.br_state == XFS_EXT_NORM);
5180 del.br_state = XFS_EXT_UNWRITTEN;
5181 error = xfs_bmap_add_extent(ip, lastx, &cur,
5182 &del, firstblock, flist, &logflags,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005183 XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 if (error)
5185 goto error0;
5186 goto nodelete;
5187 }
5188 }
5189 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005190 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005191 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005192 if (isrt) {
5193 xfs_filblks_t rtexts;
5194
5195 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5196 do_div(rtexts, mp->m_sb.sb_rextsize);
5197 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005198 (int64_t)rtexts, rsvd);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005199 (void)xfs_trans_reserve_quota_nblks(NULL,
5200 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005201 XFS_QMOPT_RES_RTBLKS);
5202 } else {
5203 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005204 (int64_t)del.br_blockcount, rsvd);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005205 (void)xfs_trans_reserve_quota_nblks(NULL,
5206 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 ip->i_delayed_blks -= del.br_blockcount;
5210 if (cur)
5211 cur->bc_private.b.flags |=
5212 XFS_BTCUR_BPRV_WASDEL;
5213 } else if (cur)
5214 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5215 /*
5216 * If it's the case where the directory code is running
5217 * with no block reservation, and the deleted block is in
5218 * the middle of its extent, and the resulting insert
5219 * of an extent would cause transformation to btree format,
5220 * then reject it. The calling code will then swap
5221 * blocks around instead.
5222 * We have to do this now, rather than waiting for the
5223 * conversion to btree format, since the transaction
5224 * will be dirty.
5225 */
5226 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5227 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5228 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5229 del.br_startoff > got.br_startoff &&
5230 del.br_startoff + del.br_blockcount <
5231 got.br_startoff + got.br_blockcount) {
5232 error = XFS_ERROR(ENOSPC);
5233 goto error0;
5234 }
5235 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005236 &tmp_logflags, whichfork, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 logflags |= tmp_logflags;
5238 if (error)
5239 goto error0;
5240 bno = del.br_startoff - 1;
5241nodelete:
5242 lastx = ifp->if_lastex;
5243 /*
5244 * If not done go on to the next (previous) record.
5245 * Reset ep in case the extents array was re-alloced.
5246 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005247 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5249 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5250 xfs_bmbt_get_startoff(ep) > bno) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005251 if (--lastx >= 0)
5252 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 }
5254 if (lastx >= 0)
5255 xfs_bmbt_get_all(ep, &got);
5256 extno++;
5257 }
5258 }
5259 ifp->if_lastex = lastx;
5260 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5261 ASSERT(ifp->if_ext_max ==
5262 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5263 /*
5264 * Convert to a btree if necessary.
5265 */
5266 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5267 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5268 ASSERT(cur == NULL);
5269 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5270 &cur, 0, &tmp_logflags, whichfork);
5271 logflags |= tmp_logflags;
5272 if (error)
5273 goto error0;
5274 }
5275 /*
5276 * transform from btree to extents, give it cur
5277 */
5278 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5279 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5280 ASSERT(cur != NULL);
5281 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5282 whichfork);
5283 logflags |= tmp_logflags;
5284 if (error)
5285 goto error0;
5286 }
5287 /*
5288 * transform from extents to local?
5289 */
5290 ASSERT(ifp->if_ext_max ==
5291 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5292 error = 0;
5293error0:
5294 /*
5295 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005296 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005298 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005300 logflags &= ~xfs_ilog_fext(whichfork);
5301 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005303 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 /*
5305 * Log inode even in the error case, if the transaction
5306 * is dirty we'll need to shut down the filesystem.
5307 */
5308 if (logflags)
5309 xfs_trans_log_inode(tp, ip, logflags);
5310 if (cur) {
5311 if (!error) {
5312 *firstblock = cur->bc_private.b.firstblock;
5313 cur->bc_private.b.allocated = 0;
5314 }
5315 xfs_btree_del_cursor(cur,
5316 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5317 }
5318 return error;
5319}
5320
5321/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005322 * returns 1 for success, 0 if we failed to map the extent.
5323 */
5324STATIC int
5325xfs_getbmapx_fix_eof_hole(
5326 xfs_inode_t *ip, /* xfs incore inode pointer */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005327 struct getbmapx *out, /* output structure */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005328 int prealloced, /* this is a file with
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005329 * preallocated data space */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005330 __int64_t end, /* last block requested */
5331 xfs_fsblock_t startblock)
5332{
5333 __int64_t fixlen;
5334 xfs_mount_t *mp; /* file system mount point */
Eric Sandeen5af317c2008-11-28 14:23:35 +11005335 xfs_ifork_t *ifp; /* inode fork pointer */
5336 xfs_extnum_t lastx; /* last extent pointer */
5337 xfs_fileoff_t fileblock;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005338
5339 if (startblock == HOLESTARTBLOCK) {
5340 mp = ip->i_mount;
5341 out->bmv_block = -1;
5342 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5343 fixlen -= out->bmv_offset;
5344 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5345 /* Came to hole at EOF. Trim it. */
5346 if (fixlen <= 0)
5347 return 0;
5348 out->bmv_length = fixlen;
5349 }
5350 } else {
Eric Sandeen5af317c2008-11-28 14:23:35 +11005351 if (startblock == DELAYSTARTBLOCK)
5352 out->bmv_block = -2;
5353 else
Eric Sandeen9d87c312009-01-14 23:22:07 -06005354 out->bmv_block = xfs_fsb_to_db(ip, startblock);
Eric Sandeen5af317c2008-11-28 14:23:35 +11005355 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5356 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5357 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5358 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5359 out->bmv_oflags |= BMV_OF_LAST;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005360 }
5361
5362 return 1;
5363}
5364
5365/*
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005366 * Get inode's extents as described in bmv, and format for output.
5367 * Calls formatter to fill the user's buffer until all extents
5368 * are mapped, until the passed-in bmv->bmv_count slots have
5369 * been filled, or until the formatter short-circuits the loop,
5370 * if it is tracking filled-in extents on its own.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 */
5372int /* error code */
5373xfs_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10005374 xfs_inode_t *ip,
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005375 struct getbmapx *bmv, /* user bmap structure */
5376 xfs_bmap_format_t formatter, /* format to user */
5377 void *arg) /* formatter arg */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378{
5379 __int64_t bmvend; /* last block requested */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005380 int error = 0; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 __int64_t fixlen; /* length for -1 case */
5382 int i; /* extent number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 int lock; /* lock state */
5384 xfs_bmbt_irec_t *map; /* buffer for user's data */
5385 xfs_mount_t *mp; /* file system mount point */
5386 int nex; /* # of user extents can do */
5387 int nexleft; /* # of user extents left */
5388 int subnex; /* # of bmapi's can do */
5389 int nmap; /* number of map entries */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005390 struct getbmapx *out; /* output structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 int whichfork; /* data or attr fork */
5392 int prealloced; /* this is a file with
5393 * preallocated data space */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005394 int iflags; /* interface flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 int bmapi_flags; /* flags for xfs_bmapi */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005396 int cur_ext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 mp = ip->i_mount;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005399 iflags = bmv->bmv_iflags;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005400 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 if (whichfork == XFS_ATTR_FORK) {
5403 if (XFS_IFORK_Q(ip)) {
5404 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5405 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5406 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5407 return XFS_ERROR(EINVAL);
5408 } else if (unlikely(
5409 ip->i_d.di_aformat != 0 &&
5410 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5411 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5412 ip->i_mount);
5413 return XFS_ERROR(EFSCORRUPTED);
5414 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005415
5416 prealloced = 0;
5417 fixlen = 1LL << 32;
5418 } else {
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005419 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5420 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5421 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5422 return XFS_ERROR(EINVAL);
5423
David Chinner957d0eb2007-06-18 16:50:37 +10005424 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005425 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 prealloced = 1;
5427 fixlen = XFS_MAXIOFFSET(mp);
5428 } else {
5429 prealloced = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005430 fixlen = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 }
5433
5434 if (bmv->bmv_length == -1) {
5435 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005436 bmv->bmv_length =
5437 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5438 } else if (bmv->bmv_length == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 bmv->bmv_entries = 0;
5440 return 0;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005441 } else if (bmv->bmv_length < 0) {
5442 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005444
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 nex = bmv->bmv_count - 1;
5446 if (nex <= 0)
5447 return XFS_ERROR(EINVAL);
5448 bmvend = bmv->bmv_offset + bmv->bmv_length;
5449
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005450
5451 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5452 return XFS_ERROR(ENOMEM);
5453 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5454 if (!out)
5455 return XFS_ERROR(ENOMEM);
5456
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005458 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5459 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5460 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5461 if (error)
5462 goto out_unlock_iolock;
Niv Sardie12070a2008-03-06 13:43:03 +11005463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005465 ASSERT(ip->i_delayed_blks == 0);
5466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467
5468 lock = xfs_ilock_map_shared(ip);
5469
5470 /*
5471 * Don't let nex be bigger than the number of extents
5472 * we can have assuming alternating holes and real extents.
5473 */
5474 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5475 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5476
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005477 bmapi_flags = xfs_bmapi_aflag(whichfork);
5478 if (!(iflags & BMV_IF_PREALLOC))
5479 bmapi_flags |= XFS_BMAPI_IGSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
5481 /*
5482 * Allocate enough space to handle "subnex" maps at a time.
5483 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005484 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 subnex = 16;
Christoph Hellwigca35dcd2009-07-18 18:14:54 -04005486 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005487 if (!map)
5488 goto out_unlock_ilock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
5490 bmv->bmv_entries = 0;
5491
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005492 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5493 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5494 error = 0;
5495 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 }
5497
5498 nexleft = nex;
5499
5500 do {
5501 nmap = (nexleft > subnex) ? subnex : nexleft;
5502 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5503 XFS_BB_TO_FSB(mp, bmv->bmv_length),
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005504 bmapi_flags, NULL, 0, map, &nmap,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005505 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 if (error)
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005507 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 ASSERT(nmap <= subnex);
5509
5510 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005511 out[cur_ext].bmv_oflags = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005512 if (map[i].br_state == XFS_EXT_UNWRITTEN)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005513 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005514 else if (map[i].br_startblock == DELAYSTARTBLOCK)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005515 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5516 out[cur_ext].bmv_offset =
5517 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5518 out[cur_ext].bmv_length =
5519 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5520 out[cur_ext].bmv_unused1 = 0;
5521 out[cur_ext].bmv_unused2 = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005522 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5523 (map[i].br_startblock != DELAYSTARTBLOCK));
Yingping Lu9af0a702005-11-02 15:09:54 +11005524 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005525 whichfork == XFS_ATTR_FORK) {
5526 /* came to the end of attribute fork */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005527 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005528 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005530
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005531 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5532 prealloced, bmvend,
5533 map[i].br_startblock))
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005534 goto out_free_map;
5535
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005536 bmv->bmv_offset =
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005537 out[cur_ext].bmv_offset +
5538 out[cur_ext].bmv_length;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005539 bmv->bmv_length =
5540 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
Tao Ma9af25462010-08-30 02:44:03 +00005541
5542 /*
5543 * In case we don't want to return the hole,
5544 * don't increase cur_ext so that we can reuse
5545 * it in the next loop.
5546 */
5547 if ((iflags & BMV_IF_NO_HOLES) &&
5548 map[i].br_startblock == HOLESTARTBLOCK) {
5549 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5550 continue;
5551 }
5552
5553 nexleft--;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005554 bmv->bmv_entries++;
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005555 cur_ext++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 }
5557 } while (nmap && nexleft && bmv->bmv_length);
5558
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005559 out_free_map:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10005560 kmem_free(map);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005561 out_unlock_ilock:
5562 xfs_iunlock_map_shared(ip, lock);
5563 out_unlock_iolock:
5564 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005565
5566 for (i = 0; i < cur_ext; i++) {
5567 int full = 0; /* user array is full */
5568
5569 /* format results & advance arg */
5570 error = formatter(&arg, &out[i], &full);
5571 if (error || full)
5572 break;
5573 }
5574
Felix Blyakher7747a0b2009-06-11 17:07:28 -05005575 kmem_free(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 return error;
5577}
5578
5579/*
5580 * Check the last inode extent to determine whether this allocation will result
5581 * in blocks being allocated at the end of the file. When we allocate new data
5582 * blocks at the end of the file which do not start at the previous data block,
5583 * we will try to align the new blocks at stripe unit boundaries.
5584 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10005585STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586xfs_bmap_isaeof(
5587 xfs_inode_t *ip, /* incore inode pointer */
5588 xfs_fileoff_t off, /* file offset in fsblocks */
5589 int whichfork, /* data or attribute fork */
5590 char *aeof) /* return value */
5591{
5592 int error; /* error return value */
5593 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005594 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005595 xfs_extnum_t nextents; /* number of file extents */
5596 xfs_bmbt_irec_t s; /* expanded extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
5598 ASSERT(whichfork == XFS_DATA_FORK);
5599 ifp = XFS_IFORK_PTR(ip, whichfork);
5600 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5601 (error = xfs_iread_extents(NULL, ip, whichfork)))
5602 return error;
5603 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5604 if (nextents == 0) {
5605 *aeof = 1;
5606 return 0;
5607 }
5608 /*
5609 * Go to the last extent
5610 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005611 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 xfs_bmbt_get_all(lastrec, &s);
5613 /*
5614 * Check we are allocating in the last extent (for delayed allocations)
5615 * or past the last extent for non-delayed allocations.
5616 */
5617 *aeof = (off >= s.br_startoff &&
5618 off < s.br_startoff + s.br_blockcount &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06005619 isnullstartblock(s.br_startblock)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 off >= s.br_startoff + s.br_blockcount;
5621 return 0;
5622}
5623
5624/*
5625 * Check if the endoff is outside the last extent. If so the caller will grow
5626 * the allocation to a stripe unit boundary.
5627 */
5628int /* error */
5629xfs_bmap_eof(
5630 xfs_inode_t *ip, /* incore inode pointer */
5631 xfs_fileoff_t endoff, /* file offset in fsblocks */
5632 int whichfork, /* data or attribute fork */
5633 int *eof) /* result value */
5634{
5635 xfs_fsblock_t blockcount; /* extent block count */
5636 int error; /* error return value */
5637 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005638 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005639 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 xfs_fileoff_t startoff; /* extent starting file offset */
5641
5642 ASSERT(whichfork == XFS_DATA_FORK);
5643 ifp = XFS_IFORK_PTR(ip, whichfork);
5644 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5645 (error = xfs_iread_extents(NULL, ip, whichfork)))
5646 return error;
5647 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5648 if (nextents == 0) {
5649 *eof = 1;
5650 return 0;
5651 }
5652 /*
5653 * Go to the last extent
5654 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005655 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 startoff = xfs_bmbt_get_startoff(lastrec);
5657 blockcount = xfs_bmbt_get_blockcount(lastrec);
5658 *eof = endoff >= startoff + blockcount;
5659 return 0;
5660}
5661
5662#ifdef DEBUG
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005663STATIC struct xfs_buf *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664xfs_bmap_get_bp(
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005665 struct xfs_btree_cur *cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 xfs_fsblock_t bno)
5667{
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005668 struct xfs_log_item_desc *lidp;
5669 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670
5671 if (!cur)
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005672 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005674 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5675 if (!cur->bc_bufs[i])
5676 break;
5677 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5678 return cur->bc_bufs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005681 /* Chase down all the log items to see if the bp is there */
5682 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
Christoph Hellwige98c4142010-06-23 18:11:15 +10005683 struct xfs_buf_log_item *bip;
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005684 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5685 if (bip->bli_item.li_type == XFS_LI_BUF &&
5686 XFS_BUF_ADDR(bip->bli_buf) == bno)
5687 return bip->bli_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 }
Christoph Hellwige98c4142010-06-23 18:11:15 +10005689
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005690 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691}
5692
Hannes Eder3180e662009-03-04 19:34:10 +01005693STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694xfs_check_block(
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005695 struct xfs_btree_block *block,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 xfs_mount_t *mp,
5697 int root,
5698 short sz)
5699{
5700 int i, j, dmxr;
Christoph Hellwig576039c2006-09-28 10:58:06 +10005701 __be64 *pp, *thispa; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 xfs_bmbt_key_t *prevp, *keyp;
5703
Christoph Hellwig16259e72005-11-02 15:11:25 +11005704 ASSERT(be16_to_cpu(block->bb_level) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705
5706 prevp = NULL;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005707 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 dmxr = mp->m_bmap_dmxr[0];
Christoph Hellwig136341b2008-10-30 17:11:40 +11005709 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710
5711 if (prevp) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005712 ASSERT(be64_to_cpu(prevp->br_startoff) <
5713 be64_to_cpu(keyp->br_startoff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 }
5715 prevp = keyp;
5716
5717 /*
5718 * Compare the block numbers to see if there are dups.
5719 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11005720 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005721 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005722 else
5723 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5724
Christoph Hellwig16259e72005-11-02 15:11:25 +11005725 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11005726 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005727 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005728 else
5729 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005730 if (*thispa == *pp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005732 __func__, j, i,
Christoph Hellwig576039c2006-09-28 10:58:06 +10005733 (unsigned long long)be64_to_cpu(*thispa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 panic("%s: ptrs are equal in node\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005735 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 }
5737 }
5738 }
5739}
5740
5741/*
5742 * Check that the extents for the inode ip are in the right order in all
5743 * btree leaves.
5744 */
5745
5746STATIC void
5747xfs_bmap_check_leaf_extents(
5748 xfs_btree_cur_t *cur, /* btree cursor or null */
5749 xfs_inode_t *ip, /* incore inode pointer */
5750 int whichfork) /* data or attr fork */
5751{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005752 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 xfs_fsblock_t bno; /* block # of "block" */
5754 xfs_buf_t *bp; /* buffer for "block" */
5755 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005756 xfs_extnum_t i=0, j; /* index into the extents list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 xfs_ifork_t *ifp; /* fork structure */
5758 int level; /* btree level, for checking */
5759 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10005760 __be64 *pp; /* pointer to block address */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005761 xfs_bmbt_rec_t *ep; /* pointer to current extent */
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005762 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005763 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 int bp_release = 0;
5765
5766 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5767 return;
5768 }
5769
5770 bno = NULLFSBLOCK;
5771 mp = ip->i_mount;
5772 ifp = XFS_IFORK_PTR(ip, whichfork);
5773 block = ifp->if_broot;
5774 /*
5775 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5776 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11005777 level = be16_to_cpu(block->bb_level);
5778 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11005780 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005781 bno = be64_to_cpu(*pp);
5782
5783 ASSERT(bno != NULLDFSBNO);
5784 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5785 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5786
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 /*
5788 * Go down the tree until leaf level is reached, following the first
5789 * pointer (leftmost) at each level.
5790 */
5791 while (level-- > 0) {
5792 /* See if buf is in cur first */
5793 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5794 if (bp) {
5795 bp_release = 0;
5796 } else {
5797 bp_release = 1;
5798 }
5799 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5800 XFS_BMAP_BTREE_REF)))
5801 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005802 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11005804 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 error0);
5806 if (level == 0)
5807 break;
5808
5809 /*
5810 * Check this block for basic sanity (increasing keys and
5811 * no duplicate blocks).
5812 */
5813
5814 xfs_check_block(block, mp, 0, 0);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005815 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005816 bno = be64_to_cpu(*pp);
5817 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 if (bp_release) {
5819 bp_release = 0;
5820 xfs_trans_brelse(NULL, bp);
5821 }
5822 }
5823
5824 /*
5825 * Here with bp and block set to the leftmost leaf node in the tree.
5826 */
5827 i = 0;
5828
5829 /*
5830 * Loop over all leaf nodes checking that all extents are in the right order.
5831 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 xfs_fsblock_t nextbno;
5834 xfs_extnum_t num_recs;
5835
5836
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005837 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838
5839 /*
5840 * Read-ahead the next leaf block, if any.
5841 */
5842
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005843 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844
5845 /*
5846 * Check all the extents to make sure they are OK.
5847 * If we had a previous block, the last entry should
5848 * conform with the first entry in this one.
5849 */
5850
Christoph Hellwig136341b2008-10-30 17:11:40 +11005851 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005852 if (i) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005853 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
5854 xfs_bmbt_disk_get_blockcount(&last) <=
5855 xfs_bmbt_disk_get_startoff(ep));
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005856 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005857 for (j = 1; j < num_recs; j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11005858 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005859 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
5860 xfs_bmbt_disk_get_blockcount(ep) <=
5861 xfs_bmbt_disk_get_startoff(nextp));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005862 ep = nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005865 last = *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 i += num_recs;
5867 if (bp_release) {
5868 bp_release = 0;
5869 xfs_trans_brelse(NULL, bp);
5870 }
5871 bno = nextbno;
5872 /*
5873 * If we've reached the end, stop.
5874 */
5875 if (bno == NULLFSBLOCK)
5876 break;
5877
5878 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5879 if (bp) {
5880 bp_release = 0;
5881 } else {
5882 bp_release = 1;
5883 }
5884 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5885 XFS_BMAP_BTREE_REF)))
5886 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005887 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 }
5889 if (bp_release) {
5890 bp_release = 0;
5891 xfs_trans_brelse(NULL, bp);
5892 }
5893 return;
5894
5895error0:
Harvey Harrison34a622b2008-04-10 12:19:21 +10005896 cmn_err(CE_WARN, "%s: at error0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 if (bp_release)
5898 xfs_trans_brelse(NULL, bp);
5899error_norelse:
5900 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005901 __func__, i);
5902 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 return;
5904}
5905#endif
5906
5907/*
5908 * Count fsblocks of the given fork.
5909 */
5910int /* error */
5911xfs_bmap_count_blocks(
5912 xfs_trans_t *tp, /* transaction pointer */
5913 xfs_inode_t *ip, /* incore inode */
5914 int whichfork, /* data or attr fork */
5915 int *count) /* out: count of blocks */
5916{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005917 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 xfs_fsblock_t bno; /* block # of "block" */
5919 xfs_ifork_t *ifp; /* fork structure */
5920 int level; /* btree level, for checking */
5921 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10005922 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
5924 bno = NULLFSBLOCK;
5925 mp = ip->i_mount;
5926 ifp = XFS_IFORK_PTR(ip, whichfork);
5927 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
Ruben Porrasc94312d2008-08-13 16:52:25 +10005928 xfs_bmap_count_leaves(ifp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
Ruben Porrasc94312d2008-08-13 16:52:25 +10005930 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 return 0;
5932 }
5933
5934 /*
5935 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5936 */
5937 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11005938 level = be16_to_cpu(block->bb_level);
5939 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11005940 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005941 bno = be64_to_cpu(*pp);
5942 ASSERT(bno != NULLDFSBNO);
5943 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5944 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005946 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
5948 mp);
5949 return XFS_ERROR(EFSCORRUPTED);
5950 }
5951
5952 return 0;
5953}
5954
5955/*
5956 * Recursively walks each level of a btree
5957 * to count total fsblocks is use.
5958 */
David Chinnera8272ce2007-11-23 16:28:09 +11005959STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960xfs_bmap_count_tree(
5961 xfs_mount_t *mp, /* file system mount point */
5962 xfs_trans_t *tp, /* transaction pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005963 xfs_ifork_t *ifp, /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 xfs_fsblock_t blockno, /* file system block number */
5965 int levelin, /* level in btree */
5966 int *count) /* Count of blocks */
5967{
5968 int error;
5969 xfs_buf_t *bp, *nbp;
5970 int level = levelin;
Christoph Hellwig576039c2006-09-28 10:58:06 +10005971 __be64 *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 xfs_fsblock_t bno = blockno;
5973 xfs_fsblock_t nextbno;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005974 struct xfs_btree_block *block, *nextblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976
5977 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
5978 return error;
5979 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005980 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981
5982 if (--level) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +02005983 /* Not at node above leaves, count this level of nodes */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005984 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 while (nextbno != NULLFSBLOCK) {
5986 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
5987 0, &nbp, XFS_BMAP_BTREE_REF)))
5988 return error;
5989 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005990 nextblock = XFS_BUF_TO_BLOCK(nbp);
5991 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 xfs_trans_brelse(tp, nbp);
5993 }
5994
5995 /* Dive to the next level */
Christoph Hellwig136341b2008-10-30 17:11:40 +11005996 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005997 bno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 if (unlikely((error =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005999 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 xfs_trans_brelse(tp, bp);
6001 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6002 XFS_ERRLEVEL_LOW, mp);
6003 return XFS_ERROR(EFSCORRUPTED);
6004 }
6005 xfs_trans_brelse(tp, bp);
6006 } else {
6007 /* count all level 1 nodes and their leaves */
6008 for (;;) {
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006009 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Christoph Hellwig16259e72005-11-02 15:11:25 +11006010 numrecs = be16_to_cpu(block->bb_numrecs);
Christoph Hellwig136341b2008-10-30 17:11:40 +11006011 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 xfs_trans_brelse(tp, bp);
6013 if (nextbno == NULLFSBLOCK)
6014 break;
6015 bno = nextbno;
6016 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6017 XFS_BMAP_BTREE_REF)))
6018 return error;
6019 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006020 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 }
6022 }
6023 return 0;
6024}
6025
6026/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006027 * Count leaf blocks given a range of extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006029STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006031 xfs_ifork_t *ifp,
6032 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 int numrecs,
6034 int *count)
6035{
6036 int b;
6037
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006038 for (b = 0; b < numrecs; b++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006039 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
Yingping Lu91e11082005-11-02 15:10:24 +11006040 *count += xfs_bmbt_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006041 }
Yingping Lu91e11082005-11-02 15:10:24 +11006042}
6043
6044/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006045 * Count leaf blocks given a range of extent records originally
6046 * in btree format.
Yingping Lu91e11082005-11-02 15:10:24 +11006047 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006048STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +11006049xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +11006050 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006051 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +11006052 int numrecs,
6053 int *count)
6054{
6055 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006056 xfs_bmbt_rec_t *frp;
Yingping Lu91e11082005-11-02 15:10:24 +11006057
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006058 for (b = 1; b <= numrecs; b++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11006059 frp = XFS_BMBT_REC_ADDR(mp, block, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 *count += xfs_bmbt_disk_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062}