blob: 4111cd3966c764b3c41300d1e7a51a7579ead435 [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)
Christoph Hellwig96540c72010-09-30 02:25:55 +0000617 xfs_icsb_modify_counters(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 &&
Christoph Hellwig96540c72010-09-30 02:25:55 +00001082 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
1083 -((int64_t)diff), rsvd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /*
1085 * Ick gross gag me with a spoon.
1086 */
1087 ASSERT(0); /* want to see if this ever happens! */
1088 while (diff > 0) {
1089 if (temp) {
1090 temp--;
1091 diff--;
1092 if (!diff ||
Christoph Hellwig96540c72010-09-30 02:25:55 +00001093 !xfs_icsb_modify_counters(ip->i_mount,
1094 XFS_SBS_FDBLOCKS,
1095 -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 break;
1097 }
1098 if (temp2) {
1099 temp2--;
1100 diff--;
1101 if (!diff ||
Christoph Hellwig96540c72010-09-30 02:25:55 +00001102 !xfs_icsb_modify_counters(ip->i_mount,
1103 XFS_SBS_FDBLOCKS,
1104 -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 break;
1106 }
1107 }
1108 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001109 ep = xfs_iext_get_ext(ifp, idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001110 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001111 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1112 trace_xfs_bmap_pre_update(ip, idx + 2, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001113 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001114 nullstartblock((int)temp2));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001115 trace_xfs_bmap_post_update(ip, idx + 2, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 *dnew = temp + temp2;
1117 break;
1118
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001119 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1120 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1121 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1122 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1123 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1124 case BMAP_LEFT_CONTIG:
1125 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /*
1127 * These cases are all impossible.
1128 */
1129 ASSERT(0);
1130 }
1131 *curp = cur;
1132done:
1133 *logflagsp = rval;
1134 return error;
1135#undef LEFT
1136#undef RIGHT
1137#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
1140/*
1141 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1142 * allocation to a real allocation or vice versa.
1143 */
1144STATIC int /* error */
1145xfs_bmap_add_extent_unwritten_real(
1146 xfs_inode_t *ip, /* incore inode pointer */
1147 xfs_extnum_t idx, /* extent number to update/insert */
1148 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001149 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001150 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001153 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001156 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 xfs_fileoff_t new_endoff; /* end offset of new entry */
1158 xfs_exntst_t newext; /* new extent state */
1159 xfs_exntst_t oldext; /* old extent state */
1160 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1161 /* left is 0, right is 1, prev is 2 */
1162 int rval=0; /* return value (logging flags) */
1163 int state = 0;/* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165#define LEFT r[0]
1166#define RIGHT r[1]
1167#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /*
1169 * Set up a bunch of variables to make the tests simpler.
1170 */
1171 error = 0;
1172 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001173 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1174 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 xfs_bmbt_get_all(ep, &PREV);
1176 newext = new->br_state;
1177 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1178 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1179 ASSERT(PREV.br_state == oldext);
1180 new_endoff = new->br_startoff + new->br_blockcount;
1181 ASSERT(PREV.br_startoff <= new->br_startoff);
1182 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /*
1185 * Set flags determining what part of the previous oldext allocation
1186 * extent is being replaced by a newext allocation.
1187 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001188 if (PREV.br_startoff == new->br_startoff)
1189 state |= BMAP_LEFT_FILLING;
1190 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1191 state |= BMAP_RIGHT_FILLING;
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /*
1194 * Check and set flags if this segment has a left neighbor.
1195 * Don't set contiguous if the combined extent would be too large.
1196 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001197 if (idx > 0) {
1198 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001199 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001200
1201 if (isnullstartblock(LEFT.br_startblock))
1202 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001204
1205 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1206 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1207 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1208 LEFT.br_state == newext &&
1209 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1210 state |= BMAP_LEFT_CONTIG;
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 /*
1213 * Check and set flags if this segment has a right neighbor.
1214 * Don't set contiguous if the combined extent would be too large.
1215 * Also check for all-three-contiguous being too large.
1216 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001217 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1218 state |= BMAP_RIGHT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001219 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001220 if (isnullstartblock(RIGHT.br_startblock))
1221 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001223
1224 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1225 new_endoff == RIGHT.br_startoff &&
1226 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1227 newext == RIGHT.br_state &&
1228 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1229 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1230 BMAP_RIGHT_FILLING)) !=
1231 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1232 BMAP_RIGHT_FILLING) ||
1233 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1234 <= MAXEXTLEN))
1235 state |= BMAP_RIGHT_CONTIG;
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 /*
1238 * Switch out based on the FILLING and CONTIG state bits.
1239 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001240 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1241 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1242 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1243 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /*
1245 * Setting all of a previous oldext extent to newext.
1246 * The left and right neighbors are both contiguous with new.
1247 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001248 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001249 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 LEFT.br_blockcount + PREV.br_blockcount +
1251 RIGHT.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001252 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1253
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001254 xfs_iext_remove(ip, idx, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 ip->i_df.if_lastex = idx - 1;
1256 ip->i_d.di_nextents -= 2;
1257 if (cur == NULL)
1258 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1259 else {
1260 rval = XFS_ILOG_CORE;
1261 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1262 RIGHT.br_startblock,
1263 RIGHT.br_blockcount, &i)))
1264 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001265 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001266 if ((error = xfs_btree_delete(cur, &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 Hellwig8df4da42008-10-30 16:55:58 +11001269 if ((error = xfs_btree_decrement(cur, 0, &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 Hellwig91cca5df2008-10-30 16:58:01 +11001272 if ((error = xfs_btree_delete(cur, &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);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001275 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001277 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1279 LEFT.br_startblock,
1280 LEFT.br_blockcount + PREV.br_blockcount +
1281 RIGHT.br_blockcount, LEFT.br_state)))
1282 goto done;
1283 }
1284 break;
1285
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001286 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 /*
1288 * Setting all of a previous oldext extent to newext.
1289 * The left neighbor is contiguous, the right is not.
1290 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001291 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001292 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001294 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 ip->i_df.if_lastex = idx - 1;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001297 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 ip->i_d.di_nextents--;
1299 if (cur == NULL)
1300 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1301 else {
1302 rval = XFS_ILOG_CORE;
1303 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1304 PREV.br_startblock, PREV.br_blockcount,
1305 &i)))
1306 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001307 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001308 if ((error = xfs_btree_delete(cur, &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);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001311 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001313 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1315 LEFT.br_startblock,
1316 LEFT.br_blockcount + PREV.br_blockcount,
1317 LEFT.br_state)))
1318 goto done;
1319 }
1320 break;
1321
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001322 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 /*
1324 * Setting all of a previous oldext extent to newext.
1325 * The right neighbor is contiguous, the left is not.
1326 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001327 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 xfs_bmbt_set_blockcount(ep,
1329 PREV.br_blockcount + RIGHT.br_blockcount);
1330 xfs_bmbt_set_state(ep, newext);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001331 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 ip->i_df.if_lastex = idx;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001333 xfs_iext_remove(ip, idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 ip->i_d.di_nextents--;
1335 if (cur == NULL)
1336 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1337 else {
1338 rval = XFS_ILOG_CORE;
1339 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1340 RIGHT.br_startblock,
1341 RIGHT.br_blockcount, &i)))
1342 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001343 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001344 if ((error = xfs_btree_delete(cur, &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);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001347 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001349 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1351 new->br_startblock,
1352 new->br_blockcount + RIGHT.br_blockcount,
1353 newext)))
1354 goto done;
1355 }
1356 break;
1357
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001358 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /*
1360 * Setting all of a previous oldext extent to newext.
1361 * Neither the left nor right neighbors are contiguous with
1362 * the new one.
1363 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001364 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 xfs_bmbt_set_state(ep, newext);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001366 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 ip->i_df.if_lastex = idx;
1369 if (cur == NULL)
1370 rval = XFS_ILOG_DEXT;
1371 else {
1372 rval = 0;
1373 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1374 new->br_startblock, new->br_blockcount,
1375 &i)))
1376 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001377 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1379 new->br_startblock, new->br_blockcount,
1380 newext)))
1381 goto done;
1382 }
1383 break;
1384
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001385 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /*
1387 * Setting the first part of a previous oldext extent to newext.
1388 * The left neighbor is contiguous.
1389 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001390 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001391 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 LEFT.br_blockcount + new->br_blockcount);
1393 xfs_bmbt_set_startoff(ep,
1394 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001395 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1396
1397 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 xfs_bmbt_set_startblock(ep,
1399 new->br_startblock + new->br_blockcount);
1400 xfs_bmbt_set_blockcount(ep,
1401 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001402 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 ip->i_df.if_lastex = idx - 1;
1405 if (cur == NULL)
1406 rval = XFS_ILOG_DEXT;
1407 else {
1408 rval = 0;
1409 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1410 PREV.br_startblock, PREV.br_blockcount,
1411 &i)))
1412 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001413 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if ((error = xfs_bmbt_update(cur,
1415 PREV.br_startoff + new->br_blockcount,
1416 PREV.br_startblock + new->br_blockcount,
1417 PREV.br_blockcount - new->br_blockcount,
1418 oldext)))
1419 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001420 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 goto done;
1422 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1423 LEFT.br_startblock,
1424 LEFT.br_blockcount + new->br_blockcount,
1425 LEFT.br_state))
1426 goto done;
1427 }
1428 break;
1429
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001430 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 /*
1432 * Setting the first part of a previous oldext extent to newext.
1433 * The left neighbor is not contiguous.
1434 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001435 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1437 xfs_bmbt_set_startoff(ep, new_endoff);
1438 xfs_bmbt_set_blockcount(ep,
1439 PREV.br_blockcount - new->br_blockcount);
1440 xfs_bmbt_set_startblock(ep,
1441 new->br_startblock + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001442 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1443
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001444 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 ip->i_df.if_lastex = idx;
1446 ip->i_d.di_nextents++;
1447 if (cur == NULL)
1448 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1449 else {
1450 rval = XFS_ILOG_CORE;
1451 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1452 PREV.br_startblock, PREV.br_blockcount,
1453 &i)))
1454 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001455 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if ((error = xfs_bmbt_update(cur,
1457 PREV.br_startoff + new->br_blockcount,
1458 PREV.br_startblock + new->br_blockcount,
1459 PREV.br_blockcount - new->br_blockcount,
1460 oldext)))
1461 goto done;
1462 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001463 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001465 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467 break;
1468
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001469 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /*
1471 * Setting the last part of a previous oldext extent to newext.
1472 * The right neighbor is contiguous with the new allocation.
1473 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001474 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1475 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 xfs_bmbt_set_blockcount(ep,
1477 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001478 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001479 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1480 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001482 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 ip->i_df.if_lastex = idx + 1;
1485 if (cur == NULL)
1486 rval = XFS_ILOG_DEXT;
1487 else {
1488 rval = 0;
1489 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1490 PREV.br_startblock,
1491 PREV.br_blockcount, &i)))
1492 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001493 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1495 PREV.br_startblock,
1496 PREV.br_blockcount - new->br_blockcount,
1497 oldext)))
1498 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001499 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 goto done;
1501 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1502 new->br_startblock,
1503 new->br_blockcount + RIGHT.br_blockcount,
1504 newext)))
1505 goto done;
1506 }
1507 break;
1508
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001509 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /*
1511 * Setting the last part of a previous oldext extent to newext.
1512 * The right neighbor is not contiguous.
1513 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001514 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 xfs_bmbt_set_blockcount(ep,
1516 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001517 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1518
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001519 xfs_iext_insert(ip, idx + 1, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 ip->i_df.if_lastex = idx + 1;
1521 ip->i_d.di_nextents++;
1522 if (cur == NULL)
1523 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1524 else {
1525 rval = XFS_ILOG_CORE;
1526 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1527 PREV.br_startblock, PREV.br_blockcount,
1528 &i)))
1529 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001530 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1532 PREV.br_startblock,
1533 PREV.br_blockcount - new->br_blockcount,
1534 oldext)))
1535 goto done;
1536 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1537 new->br_startblock, new->br_blockcount,
1538 &i)))
1539 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001540 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001542 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001544 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546 break;
1547
1548 case 0:
1549 /*
1550 * Setting the middle part of a previous oldext extent to
1551 * newext. Contiguity is impossible here.
1552 * One extent becomes three extents.
1553 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001554 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 xfs_bmbt_set_blockcount(ep,
1556 new->br_startoff - PREV.br_startoff);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001557 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 r[0] = *new;
1560 r[1].br_startoff = new_endoff;
1561 r[1].br_blockcount =
1562 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1563 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1564 r[1].br_state = oldext;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001565 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 ip->i_df.if_lastex = idx + 1;
1567 ip->i_d.di_nextents += 2;
1568 if (cur == NULL)
1569 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1570 else {
1571 rval = XFS_ILOG_CORE;
1572 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1573 PREV.br_startblock, PREV.br_blockcount,
1574 &i)))
1575 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001576 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 /* new right extent - oldext */
1578 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1579 r[1].br_startblock, r[1].br_blockcount,
1580 r[1].br_state)))
1581 goto done;
1582 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10001584 cur->bc_rec.b.br_blockcount =
1585 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001586 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001588 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001589 /*
1590 * Reset the cursor to the position of the new extent
1591 * we are about to insert as we can't trust it after
1592 * the previous insert.
1593 */
1594 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1595 new->br_startblock, new->br_blockcount,
1596 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001598 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001600 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001601 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001603 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
1605 break;
1606
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001607 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1608 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1609 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1610 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1611 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1612 case BMAP_LEFT_CONTIG:
1613 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 /*
1615 * These cases are all impossible.
1616 */
1617 ASSERT(0);
1618 }
1619 *curp = cur;
1620done:
1621 *logflagsp = rval;
1622 return error;
1623#undef LEFT
1624#undef RIGHT
1625#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
1628/*
1629 * Called by xfs_bmap_add_extent to handle cases converting a hole
1630 * to a delayed allocation.
1631 */
1632/*ARGSUSED*/
1633STATIC int /* error */
1634xfs_bmap_add_extent_hole_delay(
1635 xfs_inode_t *ip, /* incore inode pointer */
1636 xfs_extnum_t idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001637 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 int *logflagsp, /* inode logging flags */
1639 int rsvd) /* OK to allocate reserved blocks */
1640{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001641 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001642 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1644 xfs_filblks_t newlen=0; /* new indirect size */
1645 xfs_filblks_t oldlen=0; /* old indirect size */
1646 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1647 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001648 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001650 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1651 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001653 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 /*
1656 * Check and set flags if this segment has a left neighbor
1657 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001658 if (idx > 0) {
1659 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001660 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001661
1662 if (isnullstartblock(left.br_startblock))
1663 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 /*
1667 * Check and set flags if the current (right) segment exists.
1668 * If it doesn't exist, we're converting the hole at end-of-file.
1669 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001670 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1671 state |= BMAP_RIGHT_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 xfs_bmbt_get_all(ep, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001673
1674 if (isnullstartblock(right.br_startblock))
1675 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 /*
1679 * Set contiguity flags on the left and right neighbors.
1680 * Don't let extents get too large, even if the pieces are contiguous.
1681 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001682 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1683 left.br_startoff + left.br_blockcount == new->br_startoff &&
1684 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1685 state |= BMAP_LEFT_CONTIG;
1686
1687 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1688 new->br_startoff + new->br_blockcount == right.br_startoff &&
1689 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1690 (!(state & BMAP_LEFT_CONTIG) ||
1691 (left.br_blockcount + new->br_blockcount +
1692 right.br_blockcount <= MAXEXTLEN)))
1693 state |= BMAP_RIGHT_CONTIG;
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /*
1696 * Switch out based on the contiguity flags.
1697 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001698 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1699 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /*
1701 * New allocation is contiguous with delayed allocations
1702 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001703 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 */
1705 temp = left.br_blockcount + new->br_blockcount +
1706 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001707
1708 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001709 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001710 oldlen = startblockval(left.br_startblock) +
1711 startblockval(new->br_startblock) +
1712 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001714 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001715 nullstartblock((int)newlen));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001716 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1717
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001718 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 ip->i_df.if_lastex = idx - 1;
1720 break;
1721
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001722 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 /*
1724 * New allocation is contiguous with a delayed allocation
1725 * on the left.
1726 * Merge the new allocation with the left neighbor.
1727 */
1728 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001729 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001730 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001731 oldlen = startblockval(left.br_startblock) +
1732 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001734 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001735 nullstartblock((int)newlen));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001736 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 ip->i_df.if_lastex = idx - 1;
1739 break;
1740
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001741 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 /*
1743 * New allocation is contiguous with a delayed allocation
1744 * on the right.
1745 * Merge the new allocation with the right neighbor.
1746 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001747 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001749 oldlen = startblockval(new->br_startblock) +
1750 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 newlen = xfs_bmap_worst_indlen(ip, temp);
1752 xfs_bmbt_set_allf(ep, new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06001753 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001754 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 ip->i_df.if_lastex = idx;
1757 break;
1758
1759 case 0:
1760 /*
1761 * New allocation is not contiguous with another
1762 * delayed allocation.
1763 * Insert a new entry.
1764 */
1765 oldlen = newlen = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001766 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 ip->i_df.if_lastex = idx;
1768 break;
1769 }
1770 if (oldlen != newlen) {
1771 ASSERT(oldlen > newlen);
Christoph Hellwig96540c72010-09-30 02:25:55 +00001772 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11001773 (int64_t)(oldlen - newlen), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 /*
1775 * Nothing to do for disk quota accounting here.
1776 */
1777 }
1778 *logflagsp = 0;
1779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
1782/*
1783 * Called by xfs_bmap_add_extent to handle cases converting a hole
1784 * to a real allocation.
1785 */
1786STATIC int /* error */
1787xfs_bmap_add_extent_hole_real(
1788 xfs_inode_t *ip, /* incore inode pointer */
1789 xfs_extnum_t idx, /* extent number to update/insert */
1790 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001791 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 int *logflagsp, /* inode logging flags */
1793 int whichfork) /* data or attr fork */
1794{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001795 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 int i; /* temp state */
1798 xfs_ifork_t *ifp; /* inode fork pointer */
1799 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1800 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001801 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 ifp = XFS_IFORK_PTR(ip, whichfork);
1805 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001806 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 state = 0;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001808
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001809 if (whichfork == XFS_ATTR_FORK)
1810 state |= BMAP_ATTRFORK;
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /*
1813 * Check and set flags if this segment has a left neighbor.
1814 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001815 if (idx > 0) {
1816 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001817 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001818 if (isnullstartblock(left.br_startblock))
1819 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 /*
1823 * Check and set flags if this segment has a current value.
1824 * Not true if we're inserting into the "hole" at eof.
1825 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001826 if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1827 state |= BMAP_RIGHT_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 xfs_bmbt_get_all(ep, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001829 if (isnullstartblock(right.br_startblock))
1830 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 /*
1834 * We're inserting a real allocation between "left" and "right".
1835 * Set the contiguity flags. Don't let extents get too large.
1836 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001837 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1838 left.br_startoff + left.br_blockcount == new->br_startoff &&
1839 left.br_startblock + left.br_blockcount == new->br_startblock &&
1840 left.br_state == new->br_state &&
1841 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1842 state |= BMAP_LEFT_CONTIG;
1843
1844 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1845 new->br_startoff + new->br_blockcount == right.br_startoff &&
1846 new->br_startblock + new->br_blockcount == right.br_startblock &&
1847 new->br_state == right.br_state &&
1848 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1849 (!(state & BMAP_LEFT_CONTIG) ||
1850 left.br_blockcount + new->br_blockcount +
1851 right.br_blockcount <= MAXEXTLEN))
1852 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001854 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 /*
1856 * Select which case we're in here, and implement it.
1857 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001858 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1859 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 /*
1861 * New allocation is contiguous with real allocations on the
1862 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001863 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001865 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001866 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 left.br_blockcount + new->br_blockcount +
1868 right.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001869 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1870
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001871 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 ifp->if_lastex = idx - 1;
1873 XFS_IFORK_NEXT_SET(ip, whichfork,
1874 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1875 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001876 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001877 } else {
1878 rval = XFS_ILOG_CORE;
1879 if ((error = xfs_bmbt_lookup_eq(cur,
1880 right.br_startoff,
1881 right.br_startblock,
1882 right.br_blockcount, &i)))
1883 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001884 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001885 if ((error = xfs_btree_delete(cur, &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);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001888 if ((error = xfs_btree_decrement(cur, 0, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001889 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001890 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001891 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1892 left.br_startblock,
1893 left.br_blockcount +
1894 new->br_blockcount +
1895 right.br_blockcount,
1896 left.br_state)))
1897 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001899 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001901 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /*
1903 * New allocation is contiguous with a real allocation
1904 * on the left.
1905 * Merge the new allocation with the left neighbor.
1906 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001907 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001908 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 left.br_blockcount + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001910 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 ifp->if_lastex = idx - 1;
1913 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001914 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001915 } else {
1916 rval = 0;
1917 if ((error = xfs_bmbt_lookup_eq(cur,
1918 left.br_startoff,
1919 left.br_startblock,
1920 left.br_blockcount, &i)))
1921 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001922 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001923 if ((error = xfs_bmbt_update(cur, left.br_startoff,
1924 left.br_startblock,
1925 left.br_blockcount +
1926 new->br_blockcount,
1927 left.br_state)))
1928 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001930 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001932 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 /*
1934 * New allocation is contiguous with a real allocation
1935 * on the right.
1936 * Merge the new allocation with the right neighbor.
1937 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001938 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
1940 new->br_blockcount + right.br_blockcount,
1941 right.br_state);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001942 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 ifp->if_lastex = idx;
1945 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001946 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001947 } else {
1948 rval = 0;
1949 if ((error = xfs_bmbt_lookup_eq(cur,
1950 right.br_startoff,
1951 right.br_startblock,
1952 right.br_blockcount, &i)))
1953 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001954 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001955 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1956 new->br_startblock,
1957 new->br_blockcount +
1958 right.br_blockcount,
1959 right.br_state)))
1960 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001962 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 case 0:
1965 /*
1966 * New allocation is not contiguous with another
1967 * real allocation.
1968 * Insert a new entry.
1969 */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001970 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 ifp->if_lastex = idx;
1972 XFS_IFORK_NEXT_SET(ip, whichfork,
1973 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
1974 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001975 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001976 } else {
1977 rval = XFS_ILOG_CORE;
1978 if ((error = xfs_bmbt_lookup_eq(cur,
1979 new->br_startoff,
1980 new->br_startblock,
1981 new->br_blockcount, &i)))
1982 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001983 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001984 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001985 if ((error = xfs_btree_insert(cur, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001986 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001987 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001989 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001991done:
1992 *logflagsp = rval;
1993 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994}
1995
Nathan Scottdd9f4382006-01-11 15:28:28 +11001996/*
1997 * Adjust the size of the new extent based on di_extsize and rt extsize.
1998 */
1999STATIC int
2000xfs_bmap_extsize_align(
2001 xfs_mount_t *mp,
2002 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2003 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2004 xfs_extlen_t extsz, /* align to this extent size */
2005 int rt, /* is this a realtime inode? */
2006 int eof, /* is extent at end-of-file? */
2007 int delay, /* creating delalloc extent? */
2008 int convert, /* overwriting unwritten extent? */
2009 xfs_fileoff_t *offp, /* in/out: aligned offset */
2010 xfs_extlen_t *lenp) /* in/out: aligned length */
2011{
2012 xfs_fileoff_t orig_off; /* original offset */
2013 xfs_extlen_t orig_alen; /* original length */
2014 xfs_fileoff_t orig_end; /* original off+len */
2015 xfs_fileoff_t nexto; /* next file offset */
2016 xfs_fileoff_t prevo; /* previous file offset */
2017 xfs_fileoff_t align_off; /* temp for offset */
2018 xfs_extlen_t align_alen; /* temp for length */
2019 xfs_extlen_t temp; /* temp for calculations */
2020
2021 if (convert)
2022 return 0;
2023
2024 orig_off = align_off = *offp;
2025 orig_alen = align_alen = *lenp;
2026 orig_end = orig_off + orig_alen;
2027
2028 /*
2029 * If this request overlaps an existing extent, then don't
2030 * attempt to perform any additional alignment.
2031 */
2032 if (!delay && !eof &&
2033 (orig_off >= gotp->br_startoff) &&
2034 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2035 return 0;
2036 }
2037
2038 /*
2039 * If the file offset is unaligned vs. the extent size
2040 * we need to align it. This will be possible unless
2041 * the file was previously written with a kernel that didn't
2042 * perform this alignment, or if a truncate shot us in the
2043 * foot.
2044 */
2045 temp = do_mod(orig_off, extsz);
2046 if (temp) {
2047 align_alen += temp;
2048 align_off -= temp;
2049 }
2050 /*
2051 * Same adjustment for the end of the requested area.
2052 */
2053 if ((temp = (align_alen % extsz))) {
2054 align_alen += extsz - temp;
2055 }
2056 /*
2057 * If the previous block overlaps with this proposed allocation
2058 * then move the start forward without adjusting the length.
2059 */
2060 if (prevp->br_startoff != NULLFILEOFF) {
2061 if (prevp->br_startblock == HOLESTARTBLOCK)
2062 prevo = prevp->br_startoff;
2063 else
2064 prevo = prevp->br_startoff + prevp->br_blockcount;
2065 } else
2066 prevo = 0;
2067 if (align_off != orig_off && align_off < prevo)
2068 align_off = prevo;
2069 /*
2070 * If the next block overlaps with this proposed allocation
2071 * then move the start back without adjusting the length,
2072 * but not before offset 0.
2073 * This may of course make the start overlap previous block,
2074 * and if we hit the offset 0 limit then the next block
2075 * can still overlap too.
2076 */
2077 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2078 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2079 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2080 nexto = gotp->br_startoff + gotp->br_blockcount;
2081 else
2082 nexto = gotp->br_startoff;
2083 } else
2084 nexto = NULLFILEOFF;
2085 if (!eof &&
2086 align_off + align_alen != orig_end &&
2087 align_off + align_alen > nexto)
2088 align_off = nexto > align_alen ? nexto - align_alen : 0;
2089 /*
2090 * If we're now overlapping the next or previous extent that
2091 * means we can't fit an extsz piece in this hole. Just move
2092 * the start forward to the first valid spot and set
2093 * the length so we hit the end.
2094 */
2095 if (align_off != orig_off && align_off < prevo)
2096 align_off = prevo;
2097 if (align_off + align_alen != orig_end &&
2098 align_off + align_alen > nexto &&
2099 nexto != NULLFILEOFF) {
2100 ASSERT(nexto > prevo);
2101 align_alen = nexto - align_off;
2102 }
2103
2104 /*
2105 * If realtime, and the result isn't a multiple of the realtime
2106 * extent size we need to remove blocks until it is.
2107 */
2108 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2109 /*
2110 * We're not covering the original request, or
2111 * we won't be able to once we fix the length.
2112 */
2113 if (orig_off < align_off ||
2114 orig_end > align_off + align_alen ||
2115 align_alen - temp < orig_alen)
2116 return XFS_ERROR(EINVAL);
2117 /*
2118 * Try to fix it by moving the start up.
2119 */
2120 if (align_off + temp <= orig_off) {
2121 align_alen -= temp;
2122 align_off += temp;
2123 }
2124 /*
2125 * Try to fix it by moving the end in.
2126 */
2127 else if (align_off + align_alen - temp >= orig_end)
2128 align_alen -= temp;
2129 /*
2130 * Set the start to the minimum then trim the length.
2131 */
2132 else {
2133 align_alen -= orig_off - align_off;
2134 align_off = orig_off;
2135 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2136 }
2137 /*
2138 * Result doesn't cover the request, fail it.
2139 */
2140 if (orig_off < align_off || orig_end > align_off + align_alen)
2141 return XFS_ERROR(EINVAL);
2142 } else {
2143 ASSERT(orig_off >= align_off);
2144 ASSERT(orig_end <= align_off + align_alen);
2145 }
2146
2147#ifdef DEBUG
2148 if (!eof && gotp->br_startoff != NULLFILEOFF)
2149 ASSERT(align_off + align_alen <= gotp->br_startoff);
2150 if (prevp->br_startoff != NULLFILEOFF)
2151 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2152#endif
2153
2154 *lenp = align_alen;
2155 *offp = align_off;
2156 return 0;
2157}
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159#define XFS_ALLOC_GAP_UNITS 4
2160
David Chinnerc2b1cba2008-04-10 12:21:40 +10002161STATIC void
Nathan Scotta365bdd2006-03-14 13:34:16 +11002162xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2164{
2165 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2167 xfs_mount_t *mp; /* mount point structure */
2168 int nullfb; /* true if ap->firstblock isn't set */
2169 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171#define ISVALID(x,y) \
2172 (rt ? \
2173 (x) < mp->m_sb.sb_rblocks : \
2174 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2175 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2176 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 mp = ap->ip->i_mount;
2179 nullfb = ap->firstblock == NULLFSBLOCK;
2180 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2181 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 /*
2183 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002184 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 */
2186 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002187 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2189 ap->prevp->br_startblock)) {
2190 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2191 /*
2192 * Adjust for the gap between prevp and us.
2193 */
2194 adjust = ap->off -
2195 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2196 if (adjust &&
2197 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2198 ap->rval += adjust;
2199 }
2200 /*
2201 * If not at eof, then compare the two neighbor blocks.
2202 * Figure out whether either one gives us a good starting point,
2203 * and pick the better one.
2204 */
2205 else if (!ap->eof) {
2206 xfs_fsblock_t gotbno; /* right side block number */
2207 xfs_fsblock_t gotdiff=0; /* right side difference */
2208 xfs_fsblock_t prevbno; /* left side block number */
2209 xfs_fsblock_t prevdiff=0; /* left side difference */
2210
2211 /*
2212 * If there's a previous (left) block, select a requested
2213 * start block based on it.
2214 */
2215 if (ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002216 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 (prevbno = ap->prevp->br_startblock +
2218 ap->prevp->br_blockcount) &&
2219 ISVALID(prevbno, ap->prevp->br_startblock)) {
2220 /*
2221 * Calculate gap to end of previous block.
2222 */
2223 adjust = prevdiff = ap->off -
2224 (ap->prevp->br_startoff +
2225 ap->prevp->br_blockcount);
2226 /*
2227 * Figure the startblock based on the previous block's
2228 * end and the gap size.
2229 * Heuristic!
2230 * If the gap is large relative to the piece we're
2231 * allocating, or using it gives us an invalid block
2232 * number, then just use the end of the previous block.
2233 */
2234 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2235 ISVALID(prevbno + prevdiff,
2236 ap->prevp->br_startblock))
2237 prevbno += adjust;
2238 else
2239 prevdiff += adjust;
2240 /*
2241 * If the firstblock forbids it, can't use it,
2242 * must use default.
2243 */
2244 if (!rt && !nullfb &&
2245 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2246 prevbno = NULLFSBLOCK;
2247 }
2248 /*
2249 * No previous block or can't follow it, just default.
2250 */
2251 else
2252 prevbno = NULLFSBLOCK;
2253 /*
2254 * If there's a following (right) block, select a requested
2255 * start block based on it.
2256 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002257 if (!isnullstartblock(ap->gotp->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 /*
2259 * Calculate gap to start of next block.
2260 */
2261 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2262 /*
2263 * Figure the startblock based on the next block's
2264 * start and the gap size.
2265 */
2266 gotbno = ap->gotp->br_startblock;
2267 /*
2268 * Heuristic!
2269 * If the gap is large relative to the piece we're
2270 * allocating, or using it gives us an invalid block
2271 * number, then just use the start of the next block
2272 * offset by our length.
2273 */
2274 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2275 ISVALID(gotbno - gotdiff, gotbno))
2276 gotbno -= adjust;
2277 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2278 gotbno -= ap->alen;
2279 gotdiff += adjust - ap->alen;
2280 } else
2281 gotdiff += adjust;
2282 /*
2283 * If the firstblock forbids it, can't use it,
2284 * must use default.
2285 */
2286 if (!rt && !nullfb &&
2287 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2288 gotbno = NULLFSBLOCK;
2289 }
2290 /*
2291 * No next block, just default.
2292 */
2293 else
2294 gotbno = NULLFSBLOCK;
2295 /*
2296 * If both valid, pick the better one, else the only good
2297 * one, else ap->rval is already set (to 0 or the inode block).
2298 */
2299 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2300 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2301 else if (prevbno != NULLFSBLOCK)
2302 ap->rval = prevbno;
2303 else if (gotbno != NULLFSBLOCK)
2304 ap->rval = gotbno;
2305 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002306#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11002307}
2308
2309STATIC int
2310xfs_bmap_rtalloc(
2311 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2312{
2313 xfs_alloctype_t atype = 0; /* type for allocation routines */
2314 int error; /* error return value */
2315 xfs_mount_t *mp; /* mount point structure */
2316 xfs_extlen_t prod = 0; /* product factor for allocators */
2317 xfs_extlen_t ralen = 0; /* realtime allocation length */
2318 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002319 xfs_rtblock_t rtb;
2320
2321 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002322 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002323 prod = align / mp->m_sb.sb_rextsize;
2324 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2325 align, 1, ap->eof, 0,
2326 ap->conv, &ap->off, &ap->alen);
2327 if (error)
2328 return error;
2329 ASSERT(ap->alen);
2330 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2331
2332 /*
2333 * If the offset & length are not perfectly aligned
2334 * then kill prod, it will just get us in trouble.
2335 */
2336 if (do_mod(ap->off, align) || ap->alen % align)
2337 prod = 1;
2338 /*
2339 * Set ralen to be the actual requested length in rtextents.
2340 */
2341 ralen = ap->alen / mp->m_sb.sb_rextsize;
2342 /*
2343 * If the old value was close enough to MAXEXTLEN that
2344 * we rounded up to it, cut it back so it's valid again.
2345 * Note that if it's a really large request (bigger than
2346 * MAXEXTLEN), we don't hear about that number, and can't
2347 * adjust the starting point to match it.
2348 */
2349 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2350 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2351 /*
2352 * If it's an allocation to an empty file at offset 0,
2353 * pick an extent that will space things out in the rt area.
2354 */
2355 if (ap->eof && ap->off == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10002356 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2357
Nathan Scotta365bdd2006-03-14 13:34:16 +11002358 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2359 if (error)
2360 return error;
2361 ap->rval = rtx * mp->m_sb.sb_rextsize;
2362 } else {
2363 ap->rval = 0;
2364 }
2365
2366 xfs_bmap_adjacent(ap);
2367
2368 /*
2369 * Realtime allocation, done through xfs_rtallocate_extent.
2370 */
2371 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2372 do_div(ap->rval, mp->m_sb.sb_rextsize);
2373 rtb = ap->rval;
2374 ap->alen = ralen;
2375 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2376 &ralen, atype, ap->wasdel, prod, &rtb)))
2377 return error;
2378 if (rtb == NULLFSBLOCK && prod > 1 &&
2379 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2380 ap->alen, &ralen, atype,
2381 ap->wasdel, 1, &rtb)))
2382 return error;
2383 ap->rval = rtb;
2384 if (ap->rval != NULLFSBLOCK) {
2385 ap->rval *= mp->m_sb.sb_rextsize;
2386 ralen *= mp->m_sb.sb_rextsize;
2387 ap->alen = ralen;
2388 ap->ip->i_d.di_nblocks += ralen;
2389 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2390 if (ap->wasdel)
2391 ap->ip->i_delayed_blks -= ralen;
2392 /*
2393 * Adjust the disk quota also. This was reserved
2394 * earlier.
2395 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002396 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002397 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2398 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2399 } else {
2400 ap->alen = 0;
2401 }
2402 return 0;
2403}
2404
2405STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00002406xfs_bmap_btalloc_nullfb(
2407 struct xfs_bmalloca *ap,
2408 struct xfs_alloc_arg *args,
2409 xfs_extlen_t *blen)
2410{
2411 struct xfs_mount *mp = ap->ip->i_mount;
2412 struct xfs_perag *pag;
2413 xfs_agnumber_t ag, startag;
2414 int notinit = 0;
2415 int error;
2416
2417 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2418 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2419 else
2420 args->type = XFS_ALLOCTYPE_START_BNO;
2421 args->total = ap->total;
2422
2423 /*
2424 * Search for an allocation group with a single extent large enough
2425 * for the request. If one isn't found, then adjust the minimum
2426 * allocation size to the largest space found.
2427 */
2428 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2429 if (startag == NULLAGNUMBER)
2430 startag = ag = 0;
2431
2432 pag = xfs_perag_get(mp, ag);
2433 while (*blen < ap->alen) {
2434 if (!pag->pagf_init) {
2435 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2436 XFS_ALLOC_FLAG_TRYLOCK);
2437 if (error) {
2438 xfs_perag_put(pag);
2439 return error;
2440 }
2441 }
2442
2443 /*
2444 * See xfs_alloc_fix_freelist...
2445 */
2446 if (pag->pagf_init) {
2447 xfs_extlen_t longest;
2448 longest = xfs_alloc_longest_free_extent(mp, pag);
2449 if (*blen < longest)
2450 *blen = longest;
2451 } else
2452 notinit = 1;
2453
2454 if (xfs_inode_is_filestream(ap->ip)) {
2455 if (*blen >= ap->alen)
2456 break;
2457
2458 if (ap->userdata) {
2459 /*
2460 * If startag is an invalid AG, we've
2461 * come here once before and
2462 * xfs_filestream_new_ag picked the
2463 * best currently available.
2464 *
2465 * Don't continue looping, since we
2466 * could loop forever.
2467 */
2468 if (startag == NULLAGNUMBER)
2469 break;
2470
2471 error = xfs_filestream_new_ag(ap, &ag);
2472 xfs_perag_put(pag);
2473 if (error)
2474 return error;
2475
2476 /* loop again to set 'blen'*/
2477 startag = NULLAGNUMBER;
2478 pag = xfs_perag_get(mp, ag);
2479 continue;
2480 }
2481 }
2482 if (++ag == mp->m_sb.sb_agcount)
2483 ag = 0;
2484 if (ag == startag)
2485 break;
2486 xfs_perag_put(pag);
2487 pag = xfs_perag_get(mp, ag);
2488 }
2489 xfs_perag_put(pag);
2490
2491 /*
2492 * Since the above loop did a BUF_TRYLOCK, it is
2493 * possible that there is space for this request.
2494 */
2495 if (notinit || *blen < ap->minlen)
2496 args->minlen = ap->minlen;
2497 /*
2498 * If the best seen length is less than the request
2499 * length, use the best as the minimum.
2500 */
2501 else if (*blen < ap->alen)
2502 args->minlen = *blen;
2503 /*
2504 * Otherwise we've seen an extent as big as alen,
2505 * use that as the minimum.
2506 */
2507 else
2508 args->minlen = ap->alen;
2509
2510 /*
2511 * set the failure fallback case to look in the selected
2512 * AG as the stream may have moved.
2513 */
2514 if (xfs_inode_is_filestream(ap->ip))
2515 ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2516
2517 return 0;
2518}
2519
2520STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11002521xfs_bmap_btalloc(
2522 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2523{
2524 xfs_mount_t *mp; /* mount point structure */
2525 xfs_alloctype_t atype = 0; /* type for allocation routines */
2526 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002527 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00002528 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002529 xfs_alloc_arg_t args;
2530 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002531 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002532 int nullfb; /* true if ap->firstblock isn't set */
2533 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002534 int tryagain;
2535 int error;
2536
2537 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002538 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002539 if (unlikely(align)) {
2540 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2541 align, 0, ap->eof, 0, ap->conv,
2542 &ap->off, &ap->alen);
2543 ASSERT(!error);
2544 ASSERT(ap->alen);
2545 }
2546 nullfb = ap->firstblock == NULLFSBLOCK;
2547 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10002548 if (nullfb) {
2549 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2550 ag = xfs_filestream_lookup_ag(ap->ip);
2551 ag = (ag != NULLAGNUMBER) ? ag : 0;
2552 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2553 } else {
2554 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2555 }
2556 } else
Nathan Scotta365bdd2006-03-14 13:34:16 +11002557 ap->rval = ap->firstblock;
2558
2559 xfs_bmap_adjacent(ap);
2560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 /*
2562 * If allowed, use ap->rval; otherwise must use firstblock since
2563 * it's in the right allocation group.
2564 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002565 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 ;
2567 else
2568 ap->rval = ap->firstblock;
2569 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 * Normal allocation, done through xfs_alloc_vextent.
2571 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002572 tryagain = isaligned = 0;
2573 args.tp = ap->tp;
2574 args.mp = mp;
2575 args.fsbno = ap->rval;
2576 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
Yingping Lud210a282006-06-09 14:55:18 +10002577 args.firstblock = ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002578 blen = 0;
2579 if (nullfb) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00002580 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2581 if (error)
2582 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002583 } else if (ap->low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002584 if (xfs_inode_is_filestream(ap->ip))
2585 args.type = XFS_ALLOCTYPE_FIRST_AG;
2586 else
2587 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002588 args.total = args.minlen = ap->minlen;
2589 } else {
2590 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2591 args.total = ap->total;
2592 args.minlen = ap->minlen;
2593 }
David Chinner957d0eb2007-06-18 16:50:37 +10002594 /* apply extent size hints if obtained earlier */
2595 if (unlikely(align)) {
2596 args.prod = align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002597 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2598 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11002599 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11002600 args.prod = 1;
2601 args.mod = 0;
2602 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002603 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002604 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2605 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2606 }
2607 /*
2608 * If we are not low on available data blocks, and the
2609 * underlying logical volume manager is a stripe, and
2610 * the file offset is zero then try to allocate data
2611 * blocks on stripe unit boundary.
2612 * NOTE: ap->aeof is only set if the allocation length
2613 * is >= the stripe unit and the allocation offset is
2614 * at the end of file.
2615 */
2616 if (!ap->low && ap->aeof) {
2617 if (!ap->off) {
2618 args.alignment = mp->m_dalign;
2619 atype = args.type;
2620 isaligned = 1;
2621 /*
2622 * Adjust for alignment
2623 */
2624 if (blen > args.alignment && blen <= ap->alen)
2625 args.minlen = blen - args.alignment;
2626 args.minalignslop = 0;
2627 } else {
2628 /*
2629 * First try an exact bno allocation.
2630 * If it fails then do a near or start bno
2631 * allocation with alignment turned on.
2632 */
2633 atype = args.type;
2634 tryagain = 1;
2635 args.type = XFS_ALLOCTYPE_THIS_BNO;
2636 args.alignment = 1;
2637 /*
2638 * Compute the minlen+alignment for the
2639 * next case. Set slop so that the value
2640 * of minlen+alignment+slop doesn't go up
2641 * between the calls.
2642 */
2643 if (blen > mp->m_dalign && blen <= ap->alen)
2644 nextminlen = blen - mp->m_dalign;
2645 else
2646 nextminlen = args.minlen;
2647 if (nextminlen + mp->m_dalign > args.minlen + 1)
2648 args.minalignslop =
2649 nextminlen + mp->m_dalign -
2650 args.minlen - 1;
2651 else
2652 args.minalignslop = 0;
2653 }
2654 } else {
2655 args.alignment = 1;
2656 args.minalignslop = 0;
2657 }
2658 args.minleft = ap->minleft;
2659 args.wasdel = ap->wasdel;
2660 args.isfl = 0;
2661 args.userdata = ap->userdata;
2662 if ((error = xfs_alloc_vextent(&args)))
2663 return error;
2664 if (tryagain && args.fsbno == NULLFSBLOCK) {
2665 /*
2666 * Exact allocation failed. Now try with alignment
2667 * turned on.
2668 */
2669 args.type = atype;
2670 args.fsbno = ap->rval;
2671 args.alignment = mp->m_dalign;
2672 args.minlen = nextminlen;
2673 args.minalignslop = 0;
2674 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 if ((error = xfs_alloc_vextent(&args)))
2676 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002677 }
2678 if (isaligned && args.fsbno == NULLFSBLOCK) {
2679 /*
2680 * allocation failed, so turn off alignment and
2681 * try again.
2682 */
2683 args.type = atype;
2684 args.fsbno = ap->rval;
2685 args.alignment = 0;
2686 if ((error = xfs_alloc_vextent(&args)))
2687 return error;
2688 }
2689 if (args.fsbno == NULLFSBLOCK && nullfb &&
2690 args.minlen > ap->minlen) {
2691 args.minlen = ap->minlen;
2692 args.type = XFS_ALLOCTYPE_START_BNO;
2693 args.fsbno = ap->rval;
2694 if ((error = xfs_alloc_vextent(&args)))
2695 return error;
2696 }
2697 if (args.fsbno == NULLFSBLOCK && nullfb) {
2698 args.fsbno = 0;
2699 args.type = XFS_ALLOCTYPE_FIRST_AG;
2700 args.total = ap->minlen;
2701 args.minleft = 0;
2702 if ((error = xfs_alloc_vextent(&args)))
2703 return error;
2704 ap->low = 1;
2705 }
2706 if (args.fsbno != NULLFSBLOCK) {
2707 ap->firstblock = ap->rval = args.fsbno;
2708 ASSERT(nullfb || fb_agno == args.agno ||
2709 (ap->low && fb_agno < args.agno));
2710 ap->alen = args.len;
2711 ap->ip->i_d.di_nblocks += args.len;
2712 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2713 if (ap->wasdel)
2714 ap->ip->i_delayed_blks -= args.len;
2715 /*
2716 * Adjust the disk quota also. This was reserved
2717 * earlier.
2718 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002719 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002720 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2721 XFS_TRANS_DQ_BCOUNT,
2722 (long) args.len);
2723 } else {
2724 ap->rval = NULLFSBLOCK;
2725 ap->alen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 }
2727 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002728}
2729
2730/*
2731 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2732 * It figures out where to ask the underlying allocator to put the new extent.
2733 */
2734STATIC int
2735xfs_bmap_alloc(
2736 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2737{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002738 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002739 return xfs_bmap_rtalloc(ap);
2740 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
2742
2743/*
2744 * Transform a btree format file with only one leaf node, where the
2745 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002746 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 * give up the space for the btree root and pitch the leaf block.
2748 */
2749STATIC int /* error */
2750xfs_bmap_btree_to_extents(
2751 xfs_trans_t *tp, /* transaction pointer */
2752 xfs_inode_t *ip, /* incore inode pointer */
2753 xfs_btree_cur_t *cur, /* btree cursor */
2754 int *logflagsp, /* inode logging flags */
2755 int whichfork) /* data or attr fork */
2756{
2757 /* REFERENCED */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002758 struct xfs_btree_block *cblock;/* child btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 xfs_fsblock_t cbno; /* child block number */
2760 xfs_buf_t *cbp; /* child block's buffer */
2761 int error; /* error return value */
2762 xfs_ifork_t *ifp; /* inode fork data */
2763 xfs_mount_t *mp; /* mount point structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10002764 __be64 *pp; /* ptr to block address */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002765 struct xfs_btree_block *rblock;/* root btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Christoph Hellwig60197e82008-10-30 17:11:19 +11002767 mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 ifp = XFS_IFORK_PTR(ip, whichfork);
2769 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2770 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2771 rblock = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11002772 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2773 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
Christoph Hellwig60197e82008-10-30 17:11:19 +11002774 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2775 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10002776 cbno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 *logflagsp = 0;
2778#ifdef DEBUG
Christoph Hellwig576039c2006-09-28 10:58:06 +10002779 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return error;
2781#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2783 XFS_BMAP_BTREE_REF)))
2784 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002785 cblock = XFS_BUF_TO_BLOCK(cbp);
2786 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 return error;
2788 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2789 ip->i_d.di_nblocks--;
Christoph Hellwig7d095252009-06-08 15:33:32 +02002790 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 xfs_trans_binval(tp, cbp);
2792 if (cur->bc_bufs[0] == cbp)
2793 cur->bc_bufs[0] = NULL;
2794 xfs_iroot_realloc(ip, -1, whichfork);
2795 ASSERT(ifp->if_broot == NULL);
2796 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2797 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002798 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 return 0;
2800}
2801
2802/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002803 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 * after removing space (or undoing a delayed allocation).
2805 */
2806STATIC int /* error */
2807xfs_bmap_del_extent(
2808 xfs_inode_t *ip, /* incore inode pointer */
2809 xfs_trans_t *tp, /* current transaction pointer */
2810 xfs_extnum_t idx, /* extent number to update/delete */
2811 xfs_bmap_free_t *flist, /* list of extents to be freed */
2812 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002813 xfs_bmbt_irec_t *del, /* data to remove from extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 int *logflagsp, /* inode logging flags */
2815 int whichfork, /* data or attr fork */
2816 int rsvd) /* OK to allocate reserved blocks */
2817{
2818 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2819 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2820 xfs_fsblock_t del_endblock=0; /* first block past del */
2821 xfs_fileoff_t del_endoff; /* first offset past del */
2822 int delay; /* current block is delayed allocated */
2823 int do_fx; /* free extent at end of routine */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002824 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 int error; /* error return value */
2826 int flags; /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 xfs_bmbt_irec_t got; /* current extent entry */
2828 xfs_fileoff_t got_endoff; /* first offset past got */
2829 int i; /* temp state */
2830 xfs_ifork_t *ifp; /* inode fork pointer */
2831 xfs_mount_t *mp; /* mount structure */
2832 xfs_filblks_t nblks; /* quota/sb block count */
2833 xfs_bmbt_irec_t new; /* new record to be inserted */
2834 /* REFERENCED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 uint qfield; /* quota field to update */
2836 xfs_filblks_t temp; /* for indirect length calculations */
2837 xfs_filblks_t temp2; /* for indirect length calculations */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002838 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
2840 XFS_STATS_INC(xs_del_exlist);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002841
2842 if (whichfork == XFS_ATTR_FORK)
2843 state |= BMAP_ATTRFORK;
2844
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 mp = ip->i_mount;
2846 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002847 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
2848 (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 ASSERT(del->br_blockcount > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002850 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 xfs_bmbt_get_all(ep, &got);
2852 ASSERT(got.br_startoff <= del->br_startoff);
2853 del_endoff = del->br_startoff + del->br_blockcount;
2854 got_endoff = got.br_startoff + got.br_blockcount;
2855 ASSERT(got_endoff >= del_endoff);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002856 delay = isnullstartblock(got.br_startblock);
2857 ASSERT(isnullstartblock(del->br_startblock) == delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 flags = 0;
2859 qfield = 0;
2860 error = 0;
2861 /*
2862 * If deleting a real allocation, must free up the disk space.
2863 */
2864 if (!delay) {
2865 flags = XFS_ILOG_CORE;
2866 /*
2867 * Realtime allocation. Free it and record di_nblocks update.
2868 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002869 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 xfs_fsblock_t bno;
2871 xfs_filblks_t len;
2872
2873 ASSERT(do_mod(del->br_blockcount,
2874 mp->m_sb.sb_rextsize) == 0);
2875 ASSERT(do_mod(del->br_startblock,
2876 mp->m_sb.sb_rextsize) == 0);
2877 bno = del->br_startblock;
2878 len = del->br_blockcount;
2879 do_div(bno, mp->m_sb.sb_rextsize);
2880 do_div(len, mp->m_sb.sb_rextsize);
2881 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
2882 (xfs_extlen_t)len)))
2883 goto done;
2884 do_fx = 0;
2885 nblks = len * mp->m_sb.sb_rextsize;
2886 qfield = XFS_TRANS_DQ_RTBCOUNT;
2887 }
2888 /*
2889 * Ordinary allocation.
2890 */
2891 else {
2892 do_fx = 1;
2893 nblks = del->br_blockcount;
2894 qfield = XFS_TRANS_DQ_BCOUNT;
2895 }
2896 /*
2897 * Set up del_endblock and cur for later.
2898 */
2899 del_endblock = del->br_startblock + del->br_blockcount;
2900 if (cur) {
2901 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2902 got.br_startblock, got.br_blockcount,
2903 &i)))
2904 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002905 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 }
2907 da_old = da_new = 0;
2908 } else {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002909 da_old = startblockval(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 da_new = 0;
2911 nblks = 0;
2912 do_fx = 0;
2913 }
2914 /*
2915 * Set flag value to use in switch statement.
2916 * Left-contig is 2, right-contig is 1.
2917 */
2918 switch (((got.br_startoff == del->br_startoff) << 1) |
2919 (got_endoff == del_endoff)) {
2920 case 3:
2921 /*
2922 * Matches the whole extent. Delete the entry.
2923 */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002924 xfs_iext_remove(ip, idx, 1,
2925 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 ifp->if_lastex = idx;
2927 if (delay)
2928 break;
2929 XFS_IFORK_NEXT_SET(ip, whichfork,
2930 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2931 flags |= XFS_ILOG_CORE;
2932 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002933 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 break;
2935 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002936 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002938 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 break;
2940
2941 case 2:
2942 /*
2943 * Deleting the first part of the extent.
2944 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002945 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 xfs_bmbt_set_startoff(ep, del_endoff);
2947 temp = got.br_blockcount - del->br_blockcount;
2948 xfs_bmbt_set_blockcount(ep, temp);
2949 ifp->if_lastex = idx;
2950 if (delay) {
2951 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2952 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002953 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002954 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 da_new = temp;
2956 break;
2957 }
2958 xfs_bmbt_set_startblock(ep, del_endblock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002959 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002961 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 break;
2963 }
2964 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2965 got.br_blockcount - del->br_blockcount,
2966 got.br_state)))
2967 goto done;
2968 break;
2969
2970 case 1:
2971 /*
2972 * Deleting the last part of the extent.
2973 */
2974 temp = got.br_blockcount - del->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002975 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 xfs_bmbt_set_blockcount(ep, temp);
2977 ifp->if_lastex = idx;
2978 if (delay) {
2979 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2980 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002981 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002982 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 da_new = temp;
2984 break;
2985 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002986 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002988 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 break;
2990 }
2991 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2992 got.br_startblock,
2993 got.br_blockcount - del->br_blockcount,
2994 got.br_state)))
2995 goto done;
2996 break;
2997
2998 case 0:
2999 /*
3000 * Deleting the middle of the extent.
3001 */
3002 temp = del->br_startoff - got.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003003 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 xfs_bmbt_set_blockcount(ep, temp);
3005 new.br_startoff = del_endoff;
3006 temp2 = got_endoff - del_endoff;
3007 new.br_blockcount = temp2;
3008 new.br_state = got.br_state;
3009 if (!delay) {
3010 new.br_startblock = del_endblock;
3011 flags |= XFS_ILOG_CORE;
3012 if (cur) {
3013 if ((error = xfs_bmbt_update(cur,
3014 got.br_startoff,
3015 got.br_startblock, temp,
3016 got.br_state)))
3017 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11003018 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 goto done;
3020 cur->bc_rec.b = new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003021 error = xfs_btree_insert(cur, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 if (error && error != ENOSPC)
3023 goto done;
3024 /*
3025 * If get no-space back from btree insert,
3026 * it tried a split, and we have a zero
3027 * block reservation.
3028 * Fix up our state and return the error.
3029 */
3030 if (error == ENOSPC) {
3031 /*
3032 * Reset the cursor, don't trust
3033 * it after any insert operation.
3034 */
3035 if ((error = xfs_bmbt_lookup_eq(cur,
3036 got.br_startoff,
3037 got.br_startblock,
3038 temp, &i)))
3039 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003040 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 /*
3042 * Update the btree record back
3043 * to the original value.
3044 */
3045 if ((error = xfs_bmbt_update(cur,
3046 got.br_startoff,
3047 got.br_startblock,
3048 got.br_blockcount,
3049 got.br_state)))
3050 goto done;
3051 /*
3052 * Reset the extent record back
3053 * to the original value.
3054 */
3055 xfs_bmbt_set_blockcount(ep,
3056 got.br_blockcount);
3057 flags = 0;
3058 error = XFS_ERROR(ENOSPC);
3059 goto done;
3060 }
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003061 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 } else
Eric Sandeen9d87c312009-01-14 23:22:07 -06003063 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 XFS_IFORK_NEXT_SET(ip, whichfork,
3065 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3066 } else {
3067 ASSERT(whichfork == XFS_DATA_FORK);
3068 temp = xfs_bmap_worst_indlen(ip, temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003069 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003071 new.br_startblock = nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 da_new = temp + temp2;
3073 while (da_new > da_old) {
3074 if (temp) {
3075 temp--;
3076 da_new--;
3077 xfs_bmbt_set_startblock(ep,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003078 nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 }
3080 if (da_new == da_old)
3081 break;
3082 if (temp2) {
3083 temp2--;
3084 da_new--;
3085 new.br_startblock =
Eric Sandeen9d87c312009-01-14 23:22:07 -06003086 nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 }
3088 }
3089 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003090 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3091 xfs_iext_insert(ip, idx + 1, 1, &new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 ifp->if_lastex = idx + 1;
3093 break;
3094 }
3095 /*
3096 * If we need to, add to list of extents to delete.
3097 */
3098 if (do_fx)
3099 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3100 mp);
3101 /*
3102 * Adjust inode # blocks in the file.
3103 */
3104 if (nblks)
3105 ip->i_d.di_nblocks -= nblks;
3106 /*
3107 * Adjust quota data.
3108 */
3109 if (qfield)
Christoph Hellwig7d095252009-06-08 15:33:32 +02003110 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
3112 /*
3113 * Account for change in delayed indirect blocks.
3114 * Nothing to do for disk quota accounting here.
3115 */
3116 ASSERT(da_old >= da_new);
Christoph Hellwig96540c72010-09-30 02:25:55 +00003117 if (da_old > da_new) {
3118 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
3119 (int64_t)(da_old - da_new), rsvd);
3120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121done:
3122 *logflagsp = flags;
3123 return error;
3124}
3125
3126/*
3127 * Remove the entry "free" from the free item list. Prev points to the
3128 * previous entry, unless "free" is the head of the list.
3129 */
3130STATIC void
3131xfs_bmap_del_free(
3132 xfs_bmap_free_t *flist, /* free item list header */
3133 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3134 xfs_bmap_free_item_t *free) /* list item to be freed */
3135{
3136 if (prev)
3137 prev->xbfi_next = free->xbfi_next;
3138 else
3139 flist->xbf_first = free->xbfi_next;
3140 flist->xbf_count--;
3141 kmem_zone_free(xfs_bmap_free_item_zone, free);
3142}
3143
3144/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 * Convert an extents-format file into a btree-format file.
3146 * The new file will have a root block (in the inode) and a single child block.
3147 */
3148STATIC int /* error */
3149xfs_bmap_extents_to_btree(
3150 xfs_trans_t *tp, /* transaction pointer */
3151 xfs_inode_t *ip, /* incore inode pointer */
3152 xfs_fsblock_t *firstblock, /* first-block-allocated */
3153 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3154 xfs_btree_cur_t **curp, /* cursor returned to caller */
3155 int wasdel, /* converting a delayed alloc */
3156 int *logflagsp, /* inode logging flags */
3157 int whichfork) /* data or attr fork */
3158{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003159 struct xfs_btree_block *ablock; /* allocated (child) bt block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 xfs_buf_t *abp; /* buffer for ablock */
3161 xfs_alloc_arg_t args; /* allocation arguments */
3162 xfs_bmbt_rec_t *arp; /* child record pointer */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003163 struct xfs_btree_block *block; /* btree root block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 xfs_btree_cur_t *cur; /* bmap btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003165 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003167 xfs_extnum_t i, cnt; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 xfs_ifork_t *ifp; /* inode fork pointer */
3169 xfs_bmbt_key_t *kp; /* root block key pointer */
3170 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003171 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3173
3174 ifp = XFS_IFORK_PTR(ip, whichfork);
3175 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3176 ASSERT(ifp->if_ext_max ==
3177 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3178 /*
3179 * Make space in the inode incore.
3180 */
3181 xfs_iroot_realloc(ip, 1, whichfork);
3182 ifp->if_flags |= XFS_IFBROOT;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003183
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 /*
3185 * Fill in the root.
3186 */
3187 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003188 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3189 block->bb_level = cpu_to_be16(1);
3190 block->bb_numrecs = cpu_to_be16(1);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003191 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3192 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 /*
3195 * Need a cursor. Can't allocate until bb_level is filled in.
3196 */
3197 mp = ip->i_mount;
Christoph Hellwig561f7d12008-10-30 16:53:59 +11003198 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 cur->bc_private.b.firstblock = *firstblock;
3200 cur->bc_private.b.flist = flist;
3201 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3202 /*
3203 * Convert to a btree with two levels, one record in root.
3204 */
3205 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3206 args.tp = tp;
3207 args.mp = mp;
Yingping Lud210a282006-06-09 14:55:18 +10003208 args.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 if (*firstblock == NULLFSBLOCK) {
3210 args.type = XFS_ALLOCTYPE_START_BNO;
3211 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3212 } else if (flist->xbf_low) {
3213 args.type = XFS_ALLOCTYPE_START_BNO;
3214 args.fsbno = *firstblock;
3215 } else {
3216 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3217 args.fsbno = *firstblock;
3218 }
3219 args.minlen = args.maxlen = args.prod = 1;
3220 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3221 args.minalignslop = 0;
3222 args.wasdel = wasdel;
3223 *logflagsp = 0;
3224 if ((error = xfs_alloc_vextent(&args))) {
3225 xfs_iroot_realloc(ip, -1, whichfork);
3226 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3227 return error;
3228 }
3229 /*
3230 * Allocation can't fail, the space was reserved.
3231 */
3232 ASSERT(args.fsbno != NULLFSBLOCK);
3233 ASSERT(*firstblock == NULLFSBLOCK ||
3234 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3235 (flist->xbf_low &&
3236 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3237 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3238 cur->bc_private.b.allocated++;
3239 ip->i_d.di_nblocks++;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003240 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3242 /*
3243 * Fill in the child block.
3244 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003245 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003246 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 ablock->bb_level = 0;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003248 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3249 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003250 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003252 for (cnt = i = 0; i < nextents; i++) {
3253 ep = xfs_iext_get_ext(ifp, i);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003254 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10003255 arp->l0 = cpu_to_be64(ep->l0);
3256 arp->l1 = cpu_to_be64(ep->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 arp++; cnt++;
3258 }
3259 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11003260 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003261 xfs_btree_set_numrecs(ablock, cnt);
3262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 /*
3264 * Fill in the root key and pointer.
3265 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11003266 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3267 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Christoph Hellwig8801bb92006-09-28 10:58:17 +10003268 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
Christoph Hellwig136341b2008-10-30 17:11:40 +11003269 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3270 be16_to_cpu(block->bb_level)));
Christoph Hellwig576039c2006-09-28 10:58:06 +10003271 *pp = cpu_to_be64(args.fsbno);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003272
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 /*
3274 * Do all this logging at the end so that
3275 * the root is at the right level.
3276 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11003277 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3278 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 ASSERT(*curp == NULL);
3280 *curp = cur;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003281 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 return 0;
3283}
3284
3285/*
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003286 * Calculate the default attribute fork offset for newly created inodes.
3287 */
3288uint
3289xfs_default_attroffset(
3290 struct xfs_inode *ip)
3291{
3292 struct xfs_mount *mp = ip->i_mount;
3293 uint offset;
3294
3295 if (mp->m_sb.sb_inodesize == 256) {
3296 offset = XFS_LITINO(mp) -
3297 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3298 } else {
3299 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3300 }
3301
3302 ASSERT(offset < XFS_LITINO(mp));
3303 return offset;
3304}
3305
3306/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11003307 * Helper routine to reset inode di_forkoff field when switching
3308 * attribute fork from local to extent format - we reset it where
3309 * possible to make space available for inline data fork extents.
3310 */
3311STATIC void
3312xfs_bmap_forkoff_reset(
3313 xfs_mount_t *mp,
3314 xfs_inode_t *ip,
3315 int whichfork)
3316{
3317 if (whichfork == XFS_ATTR_FORK &&
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003318 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3319 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3320 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3321 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3322
3323 if (dfl_forkoff > ip->i_d.di_forkoff) {
3324 ip->i_d.di_forkoff = dfl_forkoff;
3325 ip->i_df.if_ext_max =
3326 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3327 ip->i_afp->if_ext_max =
3328 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3329 }
Nathan Scottd8cc8902005-11-02 10:34:53 +11003330 }
3331}
3332
3333/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 * Convert a local file to an extents file.
3335 * This code is out of bounds for data forks of regular files,
3336 * since the file data needs to get logged so things will stay consistent.
3337 * (The bmap-level manipulations are ok, though).
3338 */
3339STATIC int /* error */
3340xfs_bmap_local_to_extents(
3341 xfs_trans_t *tp, /* transaction pointer */
3342 xfs_inode_t *ip, /* incore inode pointer */
3343 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3344 xfs_extlen_t total, /* total blocks needed by transaction */
3345 int *logflagsp, /* inode logging flags */
3346 int whichfork) /* data or attr fork */
3347{
3348 int error; /* error return value */
3349 int flags; /* logging flags returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 xfs_ifork_t *ifp; /* inode fork pointer */
3351
3352 /*
3353 * We don't want to deal with the case of keeping inode data inline yet.
3354 * So sending the data fork of a regular inode is invalid.
3355 */
3356 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3357 whichfork == XFS_DATA_FORK));
3358 ifp = XFS_IFORK_PTR(ip, whichfork);
3359 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3360 flags = 0;
3361 error = 0;
3362 if (ifp->if_bytes) {
3363 xfs_alloc_arg_t args; /* allocation arguments */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003364 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003365 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
3367 args.tp = tp;
3368 args.mp = ip->i_mount;
Yingping Lud210a282006-06-09 14:55:18 +10003369 args.firstblock = *firstblock;
Mandy Kirkconnellf020b672006-03-14 14:07:24 +11003370 ASSERT((ifp->if_flags &
3371 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 /*
3373 * Allocate a block. We know we need only one, since the
3374 * file currently fits in an inode.
3375 */
3376 if (*firstblock == NULLFSBLOCK) {
3377 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3378 args.type = XFS_ALLOCTYPE_START_BNO;
3379 } else {
3380 args.fsbno = *firstblock;
3381 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3382 }
3383 args.total = total;
3384 args.mod = args.minleft = args.alignment = args.wasdel =
3385 args.isfl = args.minalignslop = 0;
3386 args.minlen = args.maxlen = args.prod = 1;
3387 if ((error = xfs_alloc_vextent(&args)))
3388 goto done;
3389 /*
3390 * Can't fail, the space was reserved.
3391 */
3392 ASSERT(args.fsbno != NULLFSBLOCK);
3393 ASSERT(args.len == 1);
3394 *firstblock = args.fsbno;
3395 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3396 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3397 ifp->if_bytes);
3398 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003399 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003401 xfs_iext_add(ifp, 0, 1);
3402 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003404 trace_xfs_bmap_post_update(ip, 0,
3405 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3406 _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3408 ip->i_d.di_nblocks = 1;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003409 xfs_trans_mod_dquot_byino(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 XFS_TRANS_DQ_BCOUNT, 1L);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003411 flags |= xfs_ilog_fext(whichfork);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003412 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003414 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 ifp->if_flags &= ~XFS_IFINLINE;
3417 ifp->if_flags |= XFS_IFEXTENTS;
3418 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3419 flags |= XFS_ILOG_CORE;
3420done:
3421 *logflagsp = flags;
3422 return error;
3423}
3424
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003426 * Search the extent records for the entry containing block bno.
3427 * If bno lies in a hole, point to the next entry. If bno lies
3428 * past eof, *eofp will be set, and *prevp will contain the last
3429 * entry (null if none). Else, *lastxp will be set to the index
3430 * of the found entry; *gotp will contain the entry.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003431 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003432STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003433xfs_bmap_search_multi_extents(
3434 xfs_ifork_t *ifp, /* inode fork pointer */
3435 xfs_fileoff_t bno, /* block number searched for */
3436 int *eofp, /* out: end of file found */
3437 xfs_extnum_t *lastxp, /* out: last extent index */
3438 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3439 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3440{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003441 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003442 xfs_extnum_t lastx; /* last extent index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003443
3444 /*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003445 * Initialize the extent entry structure to catch access to
3446 * uninitialized br_startblock field.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003447 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003448 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3449 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3450 gotp->br_state = XFS_EXT_INVALID;
3451#if XFS_BIG_BLKNOS
3452 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3453#else
3454 gotp->br_startblock = 0xffffa5a5;
3455#endif
3456 prevp->br_startoff = NULLFILEOFF;
3457
3458 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3459 if (lastx > 0) {
3460 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3461 }
3462 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3463 xfs_bmbt_get_all(ep, gotp);
3464 *eofp = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003465 } else {
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003466 if (lastx > 0) {
3467 *gotp = *prevp;
3468 }
3469 *eofp = 1;
3470 ep = NULL;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003471 }
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003472 *lastxp = lastx;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003473 return ep;
3474}
3475
3476/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 * Search the extents list for the inode, for the extent containing bno.
3478 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3479 * *eofp will be set, and *prevp will contain the last entry (null if none).
3480 * Else, *lastxp will be set to the index of the found
3481 * entry; *gotp will contain the entry.
3482 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003483STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484xfs_bmap_search_extents(
3485 xfs_inode_t *ip, /* incore inode pointer */
3486 xfs_fileoff_t bno, /* block number searched for */
Nathan Scott572d95f2006-09-28 11:03:20 +10003487 int fork, /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 int *eofp, /* out: end of file found */
3489 xfs_extnum_t *lastxp, /* out: last extent index */
3490 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3491 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3492{
3493 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003494 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
3496 XFS_STATS_INC(xs_look_exlist);
Nathan Scott572d95f2006-09-28 11:03:20 +10003497 ifp = XFS_IFORK_PTR(ip, fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003499 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3500
Nathan Scott572d95f2006-09-28 11:03:20 +10003501 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3502 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3503 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3504 "Access to block zero in inode %llu "
3505 "start_block: %llx start_off: %llx "
3506 "blkcnt: %llx extent-state: %x lastx: %x\n",
3507 (unsigned long long)ip->i_ino,
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003508 (unsigned long long)gotp->br_startblock,
3509 (unsigned long long)gotp->br_startoff,
3510 (unsigned long long)gotp->br_blockcount,
Nathan Scott572d95f2006-09-28 11:03:20 +10003511 gotp->br_state, *lastxp);
3512 *lastxp = NULLEXTNUM;
3513 *eofp = 1;
3514 return NULL;
3515 }
3516 return ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517}
3518
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519/*
3520 * Compute the worst-case number of indirect blocks that will be used
3521 * for ip's delayed extent of length "len".
3522 */
3523STATIC xfs_filblks_t
3524xfs_bmap_worst_indlen(
3525 xfs_inode_t *ip, /* incore inode pointer */
3526 xfs_filblks_t len) /* delayed extent length */
3527{
3528 int level; /* btree level number */
3529 int maxrecs; /* maximum record count at this level */
3530 xfs_mount_t *mp; /* mount structure */
3531 xfs_filblks_t rval; /* return value */
3532
3533 mp = ip->i_mount;
3534 maxrecs = mp->m_bmap_dmxr[0];
3535 for (level = 0, rval = 0;
3536 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3537 level++) {
3538 len += maxrecs - 1;
3539 do_div(len, maxrecs);
3540 rval += len;
3541 if (len == 1)
3542 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3543 level - 1;
3544 if (level == 0)
3545 maxrecs = mp->m_bmap_dmxr[1];
3546 }
3547 return rval;
3548}
3549
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550/*
3551 * Convert inode from non-attributed to attributed.
3552 * Must not be in a transaction, ip must not be locked.
3553 */
3554int /* error code */
3555xfs_bmap_add_attrfork(
3556 xfs_inode_t *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003557 int size, /* space new attribute needs */
3558 int rsvd) /* xact may use reserved blks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003561 xfs_bmap_free_t flist; /* freed extent records */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 xfs_trans_t *tp; /* transaction pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003564 int blks; /* space reservation */
3565 int version = 1; /* superblock attr version */
3566 int committed; /* xaction was committed */
3567 int logflags; /* logging flags */
3568 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Nathan Scottd8cc8902005-11-02 10:34:53 +11003570 ASSERT(XFS_IFORK_Q(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 ASSERT(ip->i_df.if_ext_max ==
3572 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
Nathan Scottd8cc8902005-11-02 10:34:53 +11003573
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 mp = ip->i_mount;
3575 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3576 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3577 blks = XFS_ADDAFORK_SPACE_RES(mp);
3578 if (rsvd)
3579 tp->t_flags |= XFS_TRANS_RESERVE;
3580 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3581 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3582 goto error0;
3583 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02003584 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3586 XFS_QMOPT_RES_REGBLKS);
3587 if (error) {
3588 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3589 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3590 return error;
3591 }
3592 if (XFS_IFORK_Q(ip))
3593 goto error1;
3594 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3595 /*
3596 * For inodes coming from pre-6.2 filesystems.
3597 */
3598 ASSERT(ip->i_d.di_aformat == 0);
3599 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3600 }
3601 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwig898621d2010-06-24 11:36:58 +10003602
3603 xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig898621d2010-06-24 11:36:58 +10003605
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 switch (ip->i_d.di_format) {
3607 case XFS_DINODE_FMT_DEV:
3608 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3609 break;
3610 case XFS_DINODE_FMT_UUID:
3611 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3612 break;
3613 case XFS_DINODE_FMT_LOCAL:
3614 case XFS_DINODE_FMT_EXTENTS:
3615 case XFS_DINODE_FMT_BTREE:
Nathan Scottd8cc8902005-11-02 10:34:53 +11003616 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3617 if (!ip->i_d.di_forkoff)
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003618 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
Nathan Scott13059ff2006-01-11 15:32:01 +11003619 else if (mp->m_flags & XFS_MOUNT_ATTR2)
Nathan Scottd8cc8902005-11-02 10:34:53 +11003620 version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 break;
3622 default:
3623 ASSERT(0);
3624 error = XFS_ERROR(EINVAL);
3625 goto error1;
3626 }
3627 ip->i_df.if_ext_max =
3628 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3629 ASSERT(ip->i_afp == NULL);
3630 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3631 ip->i_afp->if_ext_max =
3632 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3633 ip->i_afp->if_flags = XFS_IFEXTENTS;
3634 logflags = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003635 xfs_bmap_init(&flist, &firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 switch (ip->i_d.di_format) {
3637 case XFS_DINODE_FMT_LOCAL:
3638 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3639 &logflags);
3640 break;
3641 case XFS_DINODE_FMT_EXTENTS:
3642 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3643 &flist, &logflags);
3644 break;
3645 case XFS_DINODE_FMT_BTREE:
3646 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3647 &logflags);
3648 break;
3649 default:
3650 error = 0;
3651 break;
3652 }
3653 if (logflags)
3654 xfs_trans_log_inode(tp, ip, logflags);
3655 if (error)
3656 goto error2;
Eric Sandeen62118702008-03-06 13:44:28 +11003657 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3658 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +11003659 __int64_t sbfields = 0;
3660
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003661 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +11003662 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3663 xfs_sb_version_addattr(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003664 sbfields |= XFS_SB_VERSIONNUM;
Nathan Scottd8cc8902005-11-02 10:34:53 +11003665 }
Eric Sandeen62118702008-03-06 13:44:28 +11003666 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3667 xfs_sb_version_addattr2(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003668 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003669 }
Nathan Scottda087ba2005-11-02 15:00:20 +11003670 if (sbfields) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003671 spin_unlock(&mp->m_sb_lock);
Nathan Scottda087ba2005-11-02 15:00:20 +11003672 xfs_mod_sb(tp, sbfields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003674 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 }
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003676 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 goto error2;
Dave Chinner713bf882010-03-08 11:26:23 +11003678 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 ASSERT(ip->i_df.if_ext_max ==
3680 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3681 return error;
3682error2:
3683 xfs_bmap_cancel(&flist);
3684error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3686error0:
3687 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3688 ASSERT(ip->i_df.if_ext_max ==
3689 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3690 return error;
3691}
3692
3693/*
3694 * Add the extent to the list of extents to be free at transaction end.
3695 * The list is maintained sorted (by block number).
3696 */
3697/* ARGSUSED */
3698void
3699xfs_bmap_add_free(
3700 xfs_fsblock_t bno, /* fs block number of extent */
3701 xfs_filblks_t len, /* length of extent */
3702 xfs_bmap_free_t *flist, /* list of extents */
3703 xfs_mount_t *mp) /* mount point structure */
3704{
3705 xfs_bmap_free_item_t *cur; /* current (next) element */
3706 xfs_bmap_free_item_t *new; /* new element */
3707 xfs_bmap_free_item_t *prev; /* previous element */
3708#ifdef DEBUG
3709 xfs_agnumber_t agno;
3710 xfs_agblock_t agbno;
3711
3712 ASSERT(bno != NULLFSBLOCK);
3713 ASSERT(len > 0);
3714 ASSERT(len <= MAXEXTLEN);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003715 ASSERT(!isnullstartblock(bno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 agno = XFS_FSB_TO_AGNO(mp, bno);
3717 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3718 ASSERT(agno < mp->m_sb.sb_agcount);
3719 ASSERT(agbno < mp->m_sb.sb_agblocks);
3720 ASSERT(len < mp->m_sb.sb_agblocks);
3721 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3722#endif
3723 ASSERT(xfs_bmap_free_item_zone != NULL);
3724 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3725 new->xbfi_startblock = bno;
3726 new->xbfi_blockcount = (xfs_extlen_t)len;
3727 for (prev = NULL, cur = flist->xbf_first;
3728 cur != NULL;
3729 prev = cur, cur = cur->xbfi_next) {
3730 if (cur->xbfi_startblock >= bno)
3731 break;
3732 }
3733 if (prev)
3734 prev->xbfi_next = new;
3735 else
3736 flist->xbf_first = new;
3737 new->xbfi_next = cur;
3738 flist->xbf_count++;
3739}
3740
3741/*
3742 * Compute and fill in the value of the maximum depth of a bmap btree
3743 * in this filesystem. Done once, during mount.
3744 */
3745void
3746xfs_bmap_compute_maxlevels(
3747 xfs_mount_t *mp, /* file system mount structure */
3748 int whichfork) /* data or attr fork */
3749{
3750 int level; /* btree level */
3751 uint maxblocks; /* max blocks at this level */
3752 uint maxleafents; /* max leaf entries possible */
3753 int maxrootrecs; /* max records in root block */
3754 int minleafrecs; /* min records in leaf block */
3755 int minnoderecs; /* min records in node block */
3756 int sz; /* root block size */
3757
3758 /*
3759 * The maximum number of extents in a file, hence the maximum
3760 * number of leaf entries, is controlled by the type of di_nextents
3761 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3762 * (a signed 16-bit number, xfs_aextnum_t).
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003763 *
3764 * Note that we can no longer assume that if we are in ATTR1 that
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003765 * the fork offset of all the inodes will be
3766 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3767 * with ATTR2 and then mounted back with ATTR1, keeping the
3768 * di_forkoff's fixed but probably at various positions. Therefore,
3769 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3770 * of a minimum size available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003772 if (whichfork == XFS_DATA_FORK) {
3773 maxleafents = MAXEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003774 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003775 } else {
3776 maxleafents = MAXAEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003777 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003778 }
Christoph Hellwig60197e82008-10-30 17:11:19 +11003779 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 minleafrecs = mp->m_bmap_dmnr[0];
3781 minnoderecs = mp->m_bmap_dmnr[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3783 for (level = 1; maxblocks > 1; level++) {
3784 if (maxblocks <= maxrootrecs)
3785 maxblocks = 1;
3786 else
3787 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3788 }
3789 mp->m_bm_maxlevels[whichfork] = level;
3790}
3791
3792/*
3793 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3794 * caller. Frees all the extents that need freeing, which must be done
3795 * last due to locking considerations. We never free any extents in
3796 * the first transaction. This is to allow the caller to make the first
3797 * transaction a synchronous one so that the pointers to the data being
3798 * broken in this transaction will be permanent before the data is actually
3799 * freed. This is necessary to prevent blocks from being reallocated
3800 * and written to before the free and reallocation are actually permanent.
3801 * We do not just make the first transaction synchronous here, because
3802 * there are more efficient ways to gain the same protection in some cases
3803 * (see the file truncation code).
3804 *
3805 * Return 1 if the given transaction was committed and a new one
3806 * started, and 0 otherwise in the committed parameter.
3807 */
3808/*ARGSUSED*/
3809int /* error */
3810xfs_bmap_finish(
3811 xfs_trans_t **tp, /* transaction pointer addr */
3812 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 int *committed) /* xact committed or not */
3814{
3815 xfs_efd_log_item_t *efd; /* extent free data */
3816 xfs_efi_log_item_t *efi; /* extent free intention */
3817 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003818 xfs_bmap_free_item_t *free; /* free extent item */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 unsigned int logres; /* new log reservation */
3820 unsigned int logcount; /* new log count */
3821 xfs_mount_t *mp; /* filesystem mount structure */
3822 xfs_bmap_free_item_t *next; /* next item on free list */
3823 xfs_trans_t *ntp; /* new transaction pointer */
3824
3825 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3826 if (flist->xbf_count == 0) {
3827 *committed = 0;
3828 return 0;
3829 }
3830 ntp = *tp;
3831 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3832 for (free = flist->xbf_first; free; free = free->xbfi_next)
3833 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3834 free->xbfi_blockcount);
3835 logres = ntp->t_log_res;
3836 logcount = ntp->t_log_count;
3837 ntp = xfs_trans_dup(*tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003838 error = xfs_trans_commit(*tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 *tp = ntp;
3840 *committed = 1;
3841 /*
3842 * We have a new transaction, so we should return committed=1,
3843 * even though we're returning an error.
3844 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003845 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 return error;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003847
3848 /*
3849 * transaction commit worked ok so we can drop the extra ticket
3850 * reference that we gained in xfs_trans_dup()
3851 */
3852 xfs_log_ticket_put(ntp->t_ticket);
3853
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3855 logcount)))
3856 return error;
3857 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3858 for (free = flist->xbf_first; free != NULL; free = next) {
3859 next = free->xbfi_next;
3860 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3861 free->xbfi_blockcount))) {
3862 /*
3863 * The bmap free list will be cleaned up at a
3864 * higher level. The EFI will be canceled when
3865 * this transaction is aborted.
3866 * Need to force shutdown here to make sure it
3867 * happens, since this transaction may not be
3868 * dirty yet.
3869 */
3870 mp = ntp->t_mountp;
3871 if (!XFS_FORCED_SHUTDOWN(mp))
3872 xfs_force_shutdown(mp,
3873 (error == EFSCORRUPTED) ?
Nathan Scott7d04a332006-06-09 14:58:38 +10003874 SHUTDOWN_CORRUPT_INCORE :
3875 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 return error;
3877 }
3878 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3879 free->xbfi_blockcount);
3880 xfs_bmap_del_free(flist, NULL, free);
3881 }
3882 return 0;
3883}
3884
3885/*
3886 * Free up any items left in the list.
3887 */
3888void
3889xfs_bmap_cancel(
3890 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3891{
3892 xfs_bmap_free_item_t *free; /* free list item */
3893 xfs_bmap_free_item_t *next;
3894
3895 if (flist->xbf_count == 0)
3896 return;
3897 ASSERT(flist->xbf_first != NULL);
3898 for (free = flist->xbf_first; free; free = next) {
3899 next = free->xbfi_next;
3900 xfs_bmap_del_free(flist, NULL, free);
3901 }
3902 ASSERT(flist->xbf_count == 0);
3903}
3904
3905/*
3906 * Returns the file-relative block number of the first unused block(s)
3907 * in the file with at least "len" logically contiguous blocks free.
3908 * This is the lowest-address hole if the file has holes, else the first block
3909 * past the end of file.
3910 * Return 0 if the file is currently local (in-inode).
3911 */
3912int /* error */
3913xfs_bmap_first_unused(
3914 xfs_trans_t *tp, /* transaction pointer */
3915 xfs_inode_t *ip, /* incore inode */
3916 xfs_extlen_t len, /* size of hole to find */
3917 xfs_fileoff_t *first_unused, /* unused block */
3918 int whichfork) /* data or attr fork */
3919{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003921 int idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 xfs_ifork_t *ifp; /* inode fork pointer */
3923 xfs_fileoff_t lastaddr; /* last block number seen */
3924 xfs_fileoff_t lowest; /* lowest useful block */
3925 xfs_fileoff_t max; /* starting useful block */
3926 xfs_fileoff_t off; /* offset for this block */
3927 xfs_extnum_t nextents; /* number of extent entries */
3928
3929 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3930 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3931 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3932 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3933 *first_unused = 0;
3934 return 0;
3935 }
3936 ifp = XFS_IFORK_PTR(ip, whichfork);
3937 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3938 (error = xfs_iread_extents(tp, ip, whichfork)))
3939 return error;
3940 lowest = *first_unused;
3941 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003942 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003943 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 off = xfs_bmbt_get_startoff(ep);
3945 /*
3946 * See if the hole before this extent will work.
3947 */
3948 if (off >= lowest + len && off - max >= len) {
3949 *first_unused = max;
3950 return 0;
3951 }
3952 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3953 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3954 }
3955 *first_unused = max;
3956 return 0;
3957}
3958
3959/*
3960 * Returns the file-relative block number of the last block + 1 before
3961 * last_block (input value) in the file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003962 * This is not based on i_size, it is based on the extent records.
3963 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 */
3965int /* error */
3966xfs_bmap_last_before(
3967 xfs_trans_t *tp, /* transaction pointer */
3968 xfs_inode_t *ip, /* incore inode */
3969 xfs_fileoff_t *last_block, /* last block */
3970 int whichfork) /* data or attr fork */
3971{
3972 xfs_fileoff_t bno; /* input file offset */
3973 int eof; /* hit end of file */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003974 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 int error; /* error return value */
3976 xfs_bmbt_irec_t got; /* current extent value */
3977 xfs_ifork_t *ifp; /* inode fork pointer */
3978 xfs_extnum_t lastx; /* last extent used */
3979 xfs_bmbt_irec_t prev; /* previous extent value */
3980
3981 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3982 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3983 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3984 return XFS_ERROR(EIO);
3985 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3986 *last_block = 0;
3987 return 0;
3988 }
3989 ifp = XFS_IFORK_PTR(ip, whichfork);
3990 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3991 (error = xfs_iread_extents(tp, ip, whichfork)))
3992 return error;
3993 bno = *last_block - 1;
3994 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3995 &prev);
3996 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3997 if (prev.br_startoff == NULLFILEOFF)
3998 *last_block = 0;
3999 else
4000 *last_block = prev.br_startoff + prev.br_blockcount;
4001 }
4002 /*
4003 * Otherwise *last_block is already the right answer.
4004 */
4005 return 0;
4006}
4007
4008/*
4009 * Returns the file-relative block number of the first block past eof in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004010 * the file. This is not based on i_size, it is based on the extent records.
4011 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 */
4013int /* error */
4014xfs_bmap_last_offset(
4015 xfs_trans_t *tp, /* transaction pointer */
4016 xfs_inode_t *ip, /* incore inode */
4017 xfs_fileoff_t *last_block, /* last block */
4018 int whichfork) /* data or attr fork */
4019{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004020 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 int error; /* error return value */
4022 xfs_ifork_t *ifp; /* inode fork pointer */
4023 xfs_extnum_t nextents; /* number of extent entries */
4024
4025 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4026 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4027 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4028 return XFS_ERROR(EIO);
4029 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4030 *last_block = 0;
4031 return 0;
4032 }
4033 ifp = XFS_IFORK_PTR(ip, whichfork);
4034 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4035 (error = xfs_iread_extents(tp, ip, whichfork)))
4036 return error;
4037 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4038 if (!nextents) {
4039 *last_block = 0;
4040 return 0;
4041 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004042 ep = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4044 return 0;
4045}
4046
4047/*
4048 * Returns whether the selected fork of the inode has exactly one
4049 * block or not. For the data fork we check this matches di_size,
4050 * implying the file's range is 0..bsize-1.
4051 */
4052int /* 1=>1 block, 0=>otherwise */
4053xfs_bmap_one_block(
4054 xfs_inode_t *ip, /* incore inode */
4055 int whichfork) /* data or attr fork */
4056{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004057 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 xfs_ifork_t *ifp; /* inode fork pointer */
4059 int rval; /* return value */
4060 xfs_bmbt_irec_t s; /* internal version of extent */
4061
4062#ifndef DEBUG
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004063 if (whichfork == XFS_DATA_FORK) {
4064 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4065 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4066 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068#endif /* !DEBUG */
4069 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4070 return 0;
4071 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4072 return 0;
4073 ifp = XFS_IFORK_PTR(ip, whichfork);
4074 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004075 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 xfs_bmbt_get_all(ep, &s);
4077 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4078 if (rval && whichfork == XFS_DATA_FORK)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004079 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 return rval;
4081}
4082
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004083STATIC int
4084xfs_bmap_sanity_check(
4085 struct xfs_mount *mp,
4086 struct xfs_buf *bp,
4087 int level)
4088{
4089 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4090
4091 if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
4092 be16_to_cpu(block->bb_level) != level ||
4093 be16_to_cpu(block->bb_numrecs) == 0 ||
4094 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4095 return 0;
4096 return 1;
4097}
4098
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099/*
4100 * Read in the extents to if_extents.
4101 * All inode fields are set up by caller, we just traverse the btree
4102 * and copy the records in. If the file system cannot contain unwritten
4103 * extents, the records are checked for no "state" flags.
4104 */
4105int /* error */
4106xfs_bmap_read_extents(
4107 xfs_trans_t *tp, /* transaction pointer */
4108 xfs_inode_t *ip, /* incore inode */
4109 int whichfork) /* data or attr fork */
4110{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004111 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 xfs_fsblock_t bno; /* block # of "block" */
4113 xfs_buf_t *bp; /* buffer for "block" */
4114 int error; /* error return value */
4115 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 xfs_extnum_t i, j; /* index into the extents list */
4117 xfs_ifork_t *ifp; /* fork structure */
4118 int level; /* btree level, for checking */
4119 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10004120 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 /* REFERENCED */
4122 xfs_extnum_t room; /* number of entries there's room for */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
4124 bno = NULLFSBLOCK;
4125 mp = ip->i_mount;
4126 ifp = XFS_IFORK_PTR(ip, whichfork);
4127 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4128 XFS_EXTFMT_INODE(ip);
4129 block = ifp->if_broot;
4130 /*
4131 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4132 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004133 level = be16_to_cpu(block->bb_level);
4134 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11004135 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004136 bno = be64_to_cpu(*pp);
4137 ASSERT(bno != NULLDFSBNO);
4138 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4139 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 /*
4141 * Go down the tree until leaf level is reached, following the first
4142 * pointer (leftmost) at each level.
4143 */
4144 while (level-- > 0) {
4145 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4146 XFS_BMAP_BTREE_REF)))
4147 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004148 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004150 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 error0);
4152 if (level == 0)
4153 break;
Christoph Hellwig136341b2008-10-30 17:11:40 +11004154 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004155 bno = be64_to_cpu(*pp);
4156 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 xfs_trans_brelse(tp, bp);
4158 }
4159 /*
4160 * Here with bp and block set to the leftmost leaf node in the tree.
4161 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004162 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 i = 0;
4164 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004165 * Loop over all leaf nodes. Copy information to the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 */
4167 for (;;) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004168 xfs_bmbt_rec_t *frp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 xfs_fsblock_t nextbno;
4170 xfs_extnum_t num_recs;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004171 xfs_extnum_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
4173
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004174 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 if (unlikely(i + num_recs > room)) {
4176 ASSERT(i + num_recs <= room);
Nathan Scott3762ec62006-01-12 10:29:53 +11004177 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4178 "corrupt dinode %Lu, (btree extents).",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 (unsigned long long) ip->i_ino);
4180 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4181 XFS_ERRLEVEL_LOW,
4182 ip->i_mount);
4183 goto error0;
4184 }
4185 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004186 xfs_bmap_sanity_check(mp, bp, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 error0);
4188 /*
4189 * Read-ahead the next leaf block, if any.
4190 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004191 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 if (nextbno != NULLFSBLOCK)
4193 xfs_btree_reada_bufl(mp, nextbno, 1);
4194 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004195 * Copy records into the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11004197 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004198 start = i;
4199 for (j = 0; j < num_recs; j++, i++, frp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004200 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10004201 trp->l0 = be64_to_cpu(frp->l0);
4202 trp->l1 = be64_to_cpu(frp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 }
4204 if (exntf == XFS_EXTFMT_NOSTATE) {
4205 /*
4206 * Check all attribute bmap btree records and
4207 * any "older" data bmap btree records for a
4208 * set bit in the "extent flag" position.
4209 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004210 if (unlikely(xfs_check_nostate_extents(ifp,
4211 start, num_recs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4213 XFS_ERRLEVEL_LOW,
4214 ip->i_mount);
4215 goto error0;
4216 }
4217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 xfs_trans_brelse(tp, bp);
4219 bno = nextbno;
4220 /*
4221 * If we've reached the end, stop.
4222 */
4223 if (bno == NULLFSBLOCK)
4224 break;
4225 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4226 XFS_BMAP_BTREE_REF)))
4227 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004228 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004230 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
Eric Sandeen3a59c942007-07-11 11:09:47 +10004232 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 return 0;
4234error0:
4235 xfs_trans_brelse(tp, bp);
4236 return XFS_ERROR(EFSCORRUPTED);
4237}
4238
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004239#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004241 * Add bmap trace insert entries for all the contents of the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 */
4243void
4244xfs_bmap_trace_exlist(
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 xfs_inode_t *ip, /* incore inode pointer */
4246 xfs_extnum_t cnt, /* count of entries in the list */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004247 int whichfork, /* data or attr fork */
4248 unsigned long caller_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004250 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004252 int state = 0;
4253
4254 if (whichfork == XFS_ATTR_FORK)
4255 state |= BMAP_ATTRFORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
4257 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004258 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004259 for (idx = 0; idx < cnt; idx++)
4260 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263/*
4264 * Validate that the bmbt_irecs being returned from bmapi are valid
4265 * given the callers original parameters. Specifically check the
4266 * ranges of the returned irecs to ensure that they only extent beyond
4267 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4268 */
4269STATIC void
4270xfs_bmap_validate_ret(
4271 xfs_fileoff_t bno,
4272 xfs_filblks_t len,
4273 int flags,
4274 xfs_bmbt_irec_t *mval,
4275 int nmap,
4276 int ret_nmap)
4277{
4278 int i; /* index to map values */
4279
4280 ASSERT(ret_nmap <= nmap);
4281
4282 for (i = 0; i < ret_nmap; i++) {
4283 ASSERT(mval[i].br_blockcount > 0);
4284 if (!(flags & XFS_BMAPI_ENTIRE)) {
4285 ASSERT(mval[i].br_startoff >= bno);
4286 ASSERT(mval[i].br_blockcount <= len);
4287 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4288 bno + len);
4289 } else {
4290 ASSERT(mval[i].br_startoff < bno + len);
4291 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4292 bno);
4293 }
4294 ASSERT(i == 0 ||
4295 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4296 mval[i].br_startoff);
4297 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4298 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4299 mval[i].br_startblock != HOLESTARTBLOCK);
4300 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4301 mval[i].br_state == XFS_EXT_UNWRITTEN);
4302 }
4303}
4304#endif /* DEBUG */
4305
4306
4307/*
4308 * Map file blocks to filesystem blocks.
4309 * File range is given by the bno/len pair.
4310 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4311 * into a hole or past eof.
4312 * Only allocates blocks from a single allocation group,
4313 * to avoid locking problems.
4314 * The returned value in "firstblock" from the first call in a transaction
4315 * must be remembered and presented to subsequent calls in "firstblock".
4316 * An upper bound for the number of blocks to be allocated is supplied to
4317 * the first call in "total"; if no allocation group has that many free
4318 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4319 */
4320int /* error */
4321xfs_bmapi(
4322 xfs_trans_t *tp, /* transaction pointer */
4323 xfs_inode_t *ip, /* incore inode */
4324 xfs_fileoff_t bno, /* starting file offs. mapped */
4325 xfs_filblks_t len, /* length to map in file */
4326 int flags, /* XFS_BMAPI_... */
4327 xfs_fsblock_t *firstblock, /* first allocated block
4328 controls a.g. for allocs */
4329 xfs_extlen_t total, /* total blocks needed */
4330 xfs_bmbt_irec_t *mval, /* output: map values */
4331 int *nmap, /* i/o: mval size/count */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004332 xfs_bmap_free_t *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333{
4334 xfs_fsblock_t abno; /* allocated block number */
4335 xfs_extlen_t alen; /* allocated extent length */
4336 xfs_fileoff_t aoff; /* allocated file offset */
Dave Chinnerf0a0eaa2010-01-20 10:50:06 +11004337 xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 xfs_btree_cur_t *cur; /* bmap btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 xfs_fileoff_t end; /* end of mapped file region */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004340 int eof; /* we've hit the end of extents */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004341 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004343 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 xfs_ifork_t *ifp; /* inode fork pointer */
4345 xfs_extlen_t indlen; /* indirect blocks length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 xfs_extnum_t lastx; /* last useful extent number */
4347 int logflags; /* flags for transaction logging */
4348 xfs_extlen_t minleft; /* min blocks left after allocation */
4349 xfs_extlen_t minlen; /* min allocation size */
4350 xfs_mount_t *mp; /* xfs mount structure */
4351 int n; /* current extent index */
Malcolm Parsons9da096f2009-03-29 09:55:42 +02004352 int nallocs; /* number of extents alloc'd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 xfs_extnum_t nextents; /* number of extents in file */
4354 xfs_fileoff_t obno; /* old block number (offset) */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004355 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 int tmp_logflags; /* temp flags holder */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004357 int whichfork; /* data or attr fork */
4358 char inhole; /* current location is hole in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 char wasdelay; /* old extent was delayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 char wr; /* this is a write request */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004361 char rt; /* this is a realtime file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362#ifdef DEBUG
4363 xfs_fileoff_t orig_bno; /* original block number value */
4364 int orig_flags; /* original flags arg value */
4365 xfs_filblks_t orig_len; /* original value of len arg */
4366 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4367 int orig_nmap; /* original value of *nmap */
4368
4369 orig_bno = bno;
4370 orig_len = len;
4371 orig_flags = flags;
4372 orig_mval = mval;
4373 orig_nmap = *nmap;
4374#endif
4375 ASSERT(*nmap >= 1);
4376 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4377 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4378 XFS_ATTR_FORK : XFS_DATA_FORK;
4379 mp = ip->i_mount;
4380 if (unlikely(XFS_TEST_ERROR(
4381 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4382 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4383 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4384 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4385 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4386 return XFS_ERROR(EFSCORRUPTED);
4387 }
4388 if (XFS_FORCED_SHUTDOWN(mp))
4389 return XFS_ERROR(EIO);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004390 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 ifp = XFS_IFORK_PTR(ip, whichfork);
4392 ASSERT(ifp->if_ext_max ==
4393 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4394 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4395 XFS_STATS_INC(xs_blk_mapw);
4396 else
4397 XFS_STATS_INC(xs_blk_mapr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 /*
Nathan Scott39269e22006-03-14 13:33:50 +11004399 * IGSTATE flag is used to combine extents which
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 * differ only due to the state of the extents.
4401 * This technique is used from xfs_getbmap()
4402 * when the caller does not wish to see the
4403 * separation (which is the default).
4404 *
4405 * This technique is also used when writing a
4406 * buffer which has been partially written,
4407 * (usually by being flushed during a chunkread),
4408 * to ensure one write takes place. This also
4409 * prevents a change in the xfs inode extents at
4410 * this time, intentionally. This change occurs
4411 * on completion of the write operation, in
4412 * xfs_strat_comp(), where the xfs_bmapi() call
4413 * is transactioned, and the extents combined.
4414 */
Nathan Scott39269e22006-03-14 13:33:50 +11004415 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4416 wr = 0; /* no allocations are allowed */
4417 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 logflags = 0;
4419 nallocs = 0;
4420 cur = NULL;
4421 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4422 ASSERT(wr && tp);
4423 if ((error = xfs_bmap_local_to_extents(tp, ip,
4424 firstblock, total, &logflags, whichfork)))
4425 goto error0;
4426 }
4427 if (wr && *firstblock == NULLFSBLOCK) {
4428 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Christoph Hellwig16259e72005-11-02 15:11:25 +11004429 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 else
4431 minleft = 1;
4432 } else
4433 minleft = 0;
4434 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4435 (error = xfs_iread_extents(tp, ip, whichfork)))
4436 goto error0;
4437 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4438 &prev);
4439 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4440 n = 0;
4441 end = bno + len;
4442 obno = bno;
4443 bma.ip = NULL;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004444
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 while (bno < end && n < *nmap) {
4446 /*
4447 * Reading past eof, act as though there's a hole
4448 * up to end.
4449 */
4450 if (eof && !wr)
4451 got.br_startoff = end;
4452 inhole = eof || got.br_startoff > bno;
Nathan Scott39269e22006-03-14 13:33:50 +11004453 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06004454 isnullstartblock(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 /*
4456 * First, deal with the hole before the allocated space
4457 * that we found, if any.
4458 */
4459 if (wr && (inhole || wasdelay)) {
4460 /*
4461 * For the wasdelay case, we could also just
4462 * allocate the stuff asked for in this bmap call
4463 * but that wouldn't be as good.
4464 */
Christoph Hellwigcd8b0bb2010-06-23 18:11:15 +10004465 if (wasdelay) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 alen = (xfs_extlen_t)got.br_blockcount;
4467 aoff = got.br_startoff;
4468 if (lastx != NULLEXTNUM && lastx) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004469 ep = xfs_iext_get_ext(ifp, lastx - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 xfs_bmbt_get_all(ep, &prev);
4471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 } else {
4473 alen = (xfs_extlen_t)
4474 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4475 if (!eof)
4476 alen = (xfs_extlen_t)
4477 XFS_FILBLKS_MIN(alen,
4478 got.br_startoff - bno);
4479 aoff = bno;
4480 }
Nathan Scott39269e22006-03-14 13:33:50 +11004481 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4482 if (flags & XFS_BMAPI_DELAY) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11004483 xfs_extlen_t extsz;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004484
4485 /* Figure out the extent size, adjust alen */
David Chinner957d0eb2007-06-18 16:50:37 +10004486 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004487 if (extsz) {
4488 error = xfs_bmap_extsize_align(mp,
4489 &got, &prev, extsz,
Nathan Scott39269e22006-03-14 13:33:50 +11004490 rt, eof,
4491 flags&XFS_BMAPI_DELAY,
4492 flags&XFS_BMAPI_CONVERT,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004493 &aoff, &alen);
4494 ASSERT(!error);
4495 }
4496
4497 if (rt)
4498 extsz = alen / mp->m_sb.sb_rextsize;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004499
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 /*
4501 * Make a transaction-less quota reservation for
4502 * delayed allocation blocks. This number gets
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004503 * adjusted later. We return if we haven't
4504 * allocated blocks already inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02004506 error = xfs_trans_reserve_quota_nblks(
4507 NULL, ip, (long)alen, 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004508 rt ? XFS_QMOPT_RES_RTBLKS :
Christoph Hellwig7d095252009-06-08 15:33:32 +02004509 XFS_QMOPT_RES_REGBLKS);
4510 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 if (n == 0) {
4512 *nmap = 0;
4513 ASSERT(cur == NULL);
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004514 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 }
4516 break;
4517 }
4518
4519 /*
4520 * Split changing sb for alen and indlen since
4521 * they could be coming from different places.
4522 */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004523 indlen = (xfs_extlen_t)
4524 xfs_bmap_worst_indlen(ip, alen);
4525 ASSERT(indlen > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526
Nathan Scottdd9f4382006-01-11 15:28:28 +11004527 if (rt) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004528 error = xfs_mod_incore_sb(mp,
4529 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004530 -((int64_t)extsz), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004531 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004532 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00004533 error = xfs_icsb_modify_counters(mp,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004534 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004535 -((int64_t)alen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004536 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004537 }
David Chinner3bdbfb12005-09-02 16:40:47 +10004538 if (!error) {
Christoph Hellwig96540c72010-09-30 02:25:55 +00004539 error = xfs_icsb_modify_counters(mp,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004540 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004541 -((int64_t)indlen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004542 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004543 if (error && rt)
4544 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004545 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004546 (int64_t)extsz, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004547 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004548 else if (error)
Christoph Hellwig96540c72010-09-30 02:25:55 +00004549 xfs_icsb_modify_counters(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004550 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004551 (int64_t)alen, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004552 XFS_BMAPI_RSVBLOCKS));
David Chinner3bdbfb12005-09-02 16:40:47 +10004553 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004554
4555 if (error) {
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004556 if (XFS_IS_QUOTA_ON(mp))
Nathan Scott06d10dd2005-06-21 15:48:47 +10004557 /* unreserve the blocks now */
Nathan Scottdd9f4382006-01-11 15:28:28 +11004558 (void)
Christoph Hellwig7d095252009-06-08 15:33:32 +02004559 xfs_trans_unreserve_quota_nblks(
4560 NULL, ip,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004561 (long)alen, 0, rt ?
4562 XFS_QMOPT_RES_RTBLKS :
4563 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 break;
4565 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004566
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 ip->i_delayed_blks += alen;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004568 abno = nullstartblock(indlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 } else {
4570 /*
4571 * If first time, allocate and fill in
4572 * once-only bma fields.
4573 */
4574 if (bma.ip == NULL) {
4575 bma.tp = tp;
4576 bma.ip = ip;
4577 bma.prevp = &prev;
4578 bma.gotp = &got;
4579 bma.total = total;
4580 bma.userdata = 0;
4581 }
4582 /* Indicate if this is the first user data
4583 * in the file, or just any user data.
4584 */
Nathan Scott39269e22006-03-14 13:33:50 +11004585 if (!(flags & XFS_BMAPI_METADATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 bma.userdata = (aoff == 0) ?
4587 XFS_ALLOC_INITIAL_USER_DATA :
4588 XFS_ALLOC_USERDATA;
4589 }
4590 /*
4591 * Fill in changeable bma fields.
4592 */
4593 bma.eof = eof;
4594 bma.firstblock = *firstblock;
4595 bma.alen = alen;
4596 bma.off = aoff;
Adrian Bunk72880262006-08-30 13:41:58 +10004597 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 bma.wasdel = wasdelay;
4599 bma.minlen = minlen;
4600 bma.low = flist->xbf_low;
4601 bma.minleft = minleft;
4602 /*
4603 * Only want to do the alignment at the
4604 * eof if it is userdata and allocation length
4605 * is larger than a stripe unit.
4606 */
4607 if (mp->m_dalign && alen >= mp->m_dalign &&
Nathan Scott39269e22006-03-14 13:33:50 +11004608 (!(flags & XFS_BMAPI_METADATA)) &&
4609 (whichfork == XFS_DATA_FORK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 if ((error = xfs_bmap_isaeof(ip, aoff,
4611 whichfork, &bma.aeof)))
4612 goto error0;
4613 } else
4614 bma.aeof = 0;
4615 /*
4616 * Call allocator.
4617 */
4618 if ((error = xfs_bmap_alloc(&bma)))
4619 goto error0;
4620 /*
4621 * Copy out result fields.
4622 */
4623 abno = bma.rval;
4624 if ((flist->xbf_low = bma.low))
4625 minleft = 0;
4626 alen = bma.alen;
4627 aoff = bma.off;
4628 ASSERT(*firstblock == NULLFSBLOCK ||
4629 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4630 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4631 (flist->xbf_low &&
4632 XFS_FSB_TO_AGNO(mp, *firstblock) <
4633 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4634 *firstblock = bma.firstblock;
4635 if (cur)
4636 cur->bc_private.b.firstblock =
4637 *firstblock;
4638 if (abno == NULLFSBLOCK)
4639 break;
4640 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11004641 cur = xfs_bmbt_init_cursor(mp, tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 ip, whichfork);
4643 cur->bc_private.b.firstblock =
4644 *firstblock;
4645 cur->bc_private.b.flist = flist;
4646 }
4647 /*
4648 * Bump the number of extents we've allocated
4649 * in this call.
4650 */
4651 nallocs++;
4652 }
4653 if (cur)
4654 cur->bc_private.b.flags =
4655 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4656 got.br_startoff = aoff;
4657 got.br_startblock = abno;
4658 got.br_blockcount = alen;
4659 got.br_state = XFS_EXT_NORM; /* assume normal */
4660 /*
4661 * Determine state of extent, and the filesystem.
4662 * A wasdelay extent has been initialized, so
4663 * shouldn't be flagged as unwritten.
4664 */
Eric Sandeen62118702008-03-06 13:44:28 +11004665 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
4667 got.br_state = XFS_EXT_UNWRITTEN;
4668 }
4669 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004670 firstblock, flist, &tmp_logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004671 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 logflags |= tmp_logflags;
4673 if (error)
4674 goto error0;
4675 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004676 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4678 xfs_bmbt_get_all(ep, &got);
4679 ASSERT(got.br_startoff <= aoff);
4680 ASSERT(got.br_startoff + got.br_blockcount >=
4681 aoff + alen);
4682#ifdef DEBUG
Nathan Scott39269e22006-03-14 13:33:50 +11004683 if (flags & XFS_BMAPI_DELAY) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06004684 ASSERT(isnullstartblock(got.br_startblock));
4685 ASSERT(startblockval(got.br_startblock) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 }
4687 ASSERT(got.br_state == XFS_EXT_NORM ||
4688 got.br_state == XFS_EXT_UNWRITTEN);
4689#endif
4690 /*
4691 * Fall down into the found allocated space case.
4692 */
4693 } else if (inhole) {
4694 /*
4695 * Reading in a hole.
4696 */
4697 mval->br_startoff = bno;
4698 mval->br_startblock = HOLESTARTBLOCK;
4699 mval->br_blockcount =
4700 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4701 mval->br_state = XFS_EXT_NORM;
4702 bno += mval->br_blockcount;
4703 len -= mval->br_blockcount;
4704 mval++;
4705 n++;
4706 continue;
4707 }
4708 /*
4709 * Then deal with the allocated space we found.
4710 */
4711 ASSERT(ep != NULL);
Nathan Scott39269e22006-03-14 13:33:50 +11004712 if (!(flags & XFS_BMAPI_ENTIRE) &&
4713 (got.br_startoff + got.br_blockcount > obno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 if (obno > bno)
4715 bno = obno;
4716 ASSERT((bno >= obno) || (n == 0));
4717 ASSERT(bno < end);
4718 mval->br_startoff = bno;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004719 if (isnullstartblock(got.br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11004720 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 mval->br_startblock = DELAYSTARTBLOCK;
4722 } else
4723 mval->br_startblock =
4724 got.br_startblock +
4725 (bno - got.br_startoff);
4726 /*
4727 * Return the minimum of what we got and what we
4728 * asked for for the length. We can use the len
4729 * variable here because it is modified below
4730 * and we could have been there before coming
4731 * here if the first part of the allocation
4732 * didn't overlap what was asked for.
4733 */
4734 mval->br_blockcount =
4735 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
4736 (bno - got.br_startoff));
4737 mval->br_state = got.br_state;
4738 ASSERT(mval->br_blockcount <= len);
4739 } else {
4740 *mval = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004741 if (isnullstartblock(mval->br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11004742 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 mval->br_startblock = DELAYSTARTBLOCK;
4744 }
4745 }
4746
4747 /*
4748 * Check if writing previously allocated but
4749 * unwritten extents.
4750 */
Dave Chinner44722352010-08-24 12:02:11 +10004751 if (wr &&
4752 ((mval->br_state == XFS_EXT_UNWRITTEN &&
4753 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) ||
4754 (mval->br_state == XFS_EXT_NORM &&
4755 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT)) ==
4756 (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 /*
4758 * Modify (by adding) the state flag, if writing.
4759 */
4760 ASSERT(mval->br_blockcount <= len);
4761 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11004762 cur = xfs_bmbt_init_cursor(mp,
4763 tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 cur->bc_private.b.firstblock =
4765 *firstblock;
4766 cur->bc_private.b.flist = flist;
4767 }
Dave Chinner44722352010-08-24 12:02:11 +10004768 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4769 ? XFS_EXT_NORM
4770 : XFS_EXT_UNWRITTEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004772 firstblock, flist, &tmp_logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004773 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 logflags |= tmp_logflags;
4775 if (error)
4776 goto error0;
4777 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004778 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4780 xfs_bmbt_get_all(ep, &got);
4781 /*
4782 * We may have combined previously unwritten
4783 * space with written space, so generate
4784 * another request.
4785 */
4786 if (mval->br_blockcount < len)
4787 continue;
4788 }
4789
Nathan Scott39269e22006-03-14 13:33:50 +11004790 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 ((mval->br_startoff + mval->br_blockcount) <= end));
Nathan Scott39269e22006-03-14 13:33:50 +11004792 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4793 (mval->br_blockcount <= len) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 (mval->br_startoff < obno));
4795 bno = mval->br_startoff + mval->br_blockcount;
4796 len = end - bno;
4797 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4798 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4799 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4800 ASSERT(mval->br_state == mval[-1].br_state);
4801 mval[-1].br_blockcount = mval->br_blockcount;
4802 mval[-1].br_state = mval->br_state;
4803 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4804 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4805 mval[-1].br_startblock != HOLESTARTBLOCK &&
4806 mval->br_startblock ==
4807 mval[-1].br_startblock + mval[-1].br_blockcount &&
Nathan Scott39269e22006-03-14 13:33:50 +11004808 ((flags & XFS_BMAPI_IGSTATE) ||
4809 mval[-1].br_state == mval->br_state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 ASSERT(mval->br_startoff ==
4811 mval[-1].br_startoff + mval[-1].br_blockcount);
4812 mval[-1].br_blockcount += mval->br_blockcount;
4813 } else if (n > 0 &&
4814 mval->br_startblock == DELAYSTARTBLOCK &&
4815 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4816 mval->br_startoff ==
4817 mval[-1].br_startoff + mval[-1].br_blockcount) {
4818 mval[-1].br_blockcount += mval->br_blockcount;
4819 mval[-1].br_state = mval->br_state;
4820 } else if (!((n == 0) &&
4821 ((mval->br_startoff + mval->br_blockcount) <=
4822 obno))) {
4823 mval++;
4824 n++;
4825 }
4826 /*
4827 * If we're done, stop now. Stop when we've allocated
4828 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4829 * the transaction may get too big.
4830 */
4831 if (bno >= end || n >= *nmap || nallocs >= *nmap)
4832 break;
4833 /*
4834 * Else go on to the next record.
4835 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004836 ep = xfs_iext_get_ext(ifp, ++lastx);
David Chinner4e5ae832007-06-05 16:24:15 +10004837 prev = got;
4838 if (lastx >= nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 eof = 1;
David Chinner4e5ae832007-06-05 16:24:15 +10004840 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 xfs_bmbt_get_all(ep, &got);
4842 }
4843 ifp->if_lastex = lastx;
4844 *nmap = n;
4845 /*
4846 * Transform from btree to extents, give it cur.
4847 */
4848 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4849 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4850 ASSERT(wr && cur);
4851 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4852 &tmp_logflags, whichfork);
4853 logflags |= tmp_logflags;
4854 if (error)
4855 goto error0;
4856 }
4857 ASSERT(ifp->if_ext_max ==
4858 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4859 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4860 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
4861 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862error0:
4863 /*
4864 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004865 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06004867 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06004869 logflags &= ~xfs_ilog_fext(whichfork);
4870 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06004872 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 /*
4874 * Log whatever the flags say, even if error. Otherwise we might miss
4875 * detecting a case where the data is changed, there's an error,
4876 * and it's not logged so we don't shutdown when we should.
4877 */
4878 if (logflags) {
4879 ASSERT(tp && wr);
4880 xfs_trans_log_inode(tp, ip, logflags);
4881 }
4882 if (cur) {
4883 if (!error) {
4884 ASSERT(*firstblock == NULLFSBLOCK ||
4885 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4886 XFS_FSB_TO_AGNO(mp,
4887 cur->bc_private.b.firstblock) ||
4888 (flist->xbf_low &&
4889 XFS_FSB_TO_AGNO(mp, *firstblock) <
4890 XFS_FSB_TO_AGNO(mp,
4891 cur->bc_private.b.firstblock)));
4892 *firstblock = cur->bc_private.b.firstblock;
4893 }
4894 xfs_btree_del_cursor(cur,
4895 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4896 }
4897 if (!error)
4898 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4899 orig_nmap, *nmap);
4900 return error;
4901}
4902
4903/*
4904 * Map file blocks to filesystem blocks, simple version.
4905 * One block (extent) only, read-only.
4906 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
4907 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
4908 * was set and all the others were clear.
4909 */
4910int /* error */
4911xfs_bmapi_single(
4912 xfs_trans_t *tp, /* transaction pointer */
4913 xfs_inode_t *ip, /* incore inode */
4914 int whichfork, /* data or attr fork */
4915 xfs_fsblock_t *fsb, /* output: mapped block */
4916 xfs_fileoff_t bno) /* starting file offs. mapped */
4917{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004918 int eof; /* we've hit the end of extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004920 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 xfs_ifork_t *ifp; /* inode fork pointer */
4922 xfs_extnum_t lastx; /* last useful extent number */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004923 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
4925 ifp = XFS_IFORK_PTR(ip, whichfork);
4926 if (unlikely(
4927 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4928 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
4929 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
4930 ip->i_mount);
4931 return XFS_ERROR(EFSCORRUPTED);
4932 }
4933 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
4934 return XFS_ERROR(EIO);
4935 XFS_STATS_INC(xs_blk_mapr);
4936 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4937 (error = xfs_iread_extents(tp, ip, whichfork)))
4938 return error;
4939 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4940 &prev);
4941 /*
4942 * Reading past eof, act as though there's a hole
4943 * up to end.
4944 */
4945 if (eof || got.br_startoff > bno) {
4946 *fsb = NULLFSBLOCK;
4947 return 0;
4948 }
Eric Sandeen9d87c312009-01-14 23:22:07 -06004949 ASSERT(!isnullstartblock(got.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 ASSERT(bno < got.br_startoff + got.br_blockcount);
4951 *fsb = got.br_startblock + (bno - got.br_startoff);
4952 ifp->if_lastex = lastx;
4953 return 0;
4954}
4955
4956/*
4957 * Unmap (remove) blocks from a file.
4958 * If nexts is nonzero then the number of extents to remove is limited to
4959 * that value. If not all extents in the block range can be removed then
4960 * *done is set.
4961 */
4962int /* error */
4963xfs_bunmapi(
4964 xfs_trans_t *tp, /* transaction pointer */
4965 struct xfs_inode *ip, /* incore inode */
4966 xfs_fileoff_t bno, /* starting offset to unmap */
4967 xfs_filblks_t len, /* length to unmap in file */
4968 int flags, /* misc flags */
4969 xfs_extnum_t nexts, /* number of extents max */
4970 xfs_fsblock_t *firstblock, /* first allocated block
4971 controls a.g. for allocs */
4972 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4973 int *done) /* set if not done yet */
4974{
4975 xfs_btree_cur_t *cur; /* bmap btree cursor */
4976 xfs_bmbt_irec_t del; /* extent being deleted */
4977 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004978 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 int error; /* error return value */
4980 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004981 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 xfs_ifork_t *ifp; /* inode fork pointer */
4983 int isrt; /* freeing in rt area */
4984 xfs_extnum_t lastx; /* last extent index used */
4985 int logflags; /* transaction logging flags */
4986 xfs_extlen_t mod; /* rt extent offset */
4987 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004988 xfs_extnum_t nextents; /* number of file extents */
4989 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 xfs_fileoff_t start; /* first file offset deleted */
4991 int tmp_logflags; /* partial logging flags */
4992 int wasdel; /* was a delayed alloc extent */
4993 int whichfork; /* data or attribute fork */
4994 int rsvd; /* OK to allocate reserved blocks */
4995 xfs_fsblock_t sum;
4996
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004997 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
4998
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5000 XFS_ATTR_FORK : XFS_DATA_FORK;
5001 ifp = XFS_IFORK_PTR(ip, whichfork);
5002 if (unlikely(
5003 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5004 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5005 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5006 ip->i_mount);
5007 return XFS_ERROR(EFSCORRUPTED);
5008 }
5009 mp = ip->i_mount;
5010 if (XFS_FORCED_SHUTDOWN(mp))
5011 return XFS_ERROR(EIO);
5012 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5013 ASSERT(len > 0);
5014 ASSERT(nexts >= 0);
5015 ASSERT(ifp->if_ext_max ==
5016 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5017 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5018 (error = xfs_iread_extents(tp, ip, whichfork)))
5019 return error;
5020 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5021 if (nextents == 0) {
5022 *done = 1;
5023 return 0;
5024 }
5025 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005026 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 start = bno;
5028 bno = start + len - 1;
5029 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5030 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005031
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 /*
5033 * Check to see if the given block number is past the end of the
5034 * file, back up to the last block if so...
5035 */
5036 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005037 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 xfs_bmbt_get_all(ep, &got);
5039 bno = got.br_startoff + got.br_blockcount - 1;
5040 }
5041 logflags = 0;
5042 if (ifp->if_flags & XFS_IFBROOT) {
5043 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005044 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 cur->bc_private.b.firstblock = *firstblock;
5046 cur->bc_private.b.flist = flist;
5047 cur->bc_private.b.flags = 0;
5048 } else
5049 cur = NULL;
5050 extno = 0;
5051 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5052 (nexts == 0 || extno < nexts)) {
5053 /*
5054 * Is the found extent after a hole in which bno lives?
5055 * Just back up to the previous extent, if so.
5056 */
5057 if (got.br_startoff > bno) {
5058 if (--lastx < 0)
5059 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005060 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 xfs_bmbt_get_all(ep, &got);
5062 }
5063 /*
5064 * Is the last block of this extent before the range
5065 * we're supposed to delete? If so, we're done.
5066 */
5067 bno = XFS_FILEOFF_MIN(bno,
5068 got.br_startoff + got.br_blockcount - 1);
5069 if (bno < start)
5070 break;
5071 /*
5072 * Then deal with the (possibly delayed) allocated space
5073 * we found.
5074 */
5075 ASSERT(ep != NULL);
5076 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005077 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 if (got.br_startoff < start) {
5079 del.br_startoff = start;
5080 del.br_blockcount -= start - got.br_startoff;
5081 if (!wasdel)
5082 del.br_startblock += start - got.br_startoff;
5083 }
5084 if (del.br_startoff + del.br_blockcount > bno + 1)
5085 del.br_blockcount = bno + 1 - del.br_startoff;
5086 sum = del.br_startblock + del.br_blockcount;
5087 if (isrt &&
5088 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5089 /*
5090 * Realtime extent not lined up at the end.
5091 * The extent could have been split into written
5092 * and unwritten pieces, or we could just be
5093 * unmapping part of it. But we can't really
5094 * get rid of part of a realtime extent.
5095 */
5096 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005097 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 /*
5099 * This piece is unwritten, or we're not
5100 * using unwritten extents. Skip over it.
5101 */
5102 ASSERT(bno >= mod);
5103 bno -= mod > del.br_blockcount ?
5104 del.br_blockcount : mod;
5105 if (bno < got.br_startoff) {
5106 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005107 xfs_bmbt_get_all(xfs_iext_get_ext(
5108 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 }
5110 continue;
5111 }
5112 /*
5113 * It's written, turn it unwritten.
5114 * This is better than zeroing it.
5115 */
5116 ASSERT(del.br_state == XFS_EXT_NORM);
5117 ASSERT(xfs_trans_get_block_res(tp) > 0);
5118 /*
5119 * If this spans a realtime extent boundary,
5120 * chop it back to the start of the one we end at.
5121 */
5122 if (del.br_blockcount > mod) {
5123 del.br_startoff += del.br_blockcount - mod;
5124 del.br_startblock += del.br_blockcount - mod;
5125 del.br_blockcount = mod;
5126 }
5127 del.br_state = XFS_EXT_UNWRITTEN;
5128 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005129 firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005130 XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 if (error)
5132 goto error0;
5133 goto nodelete;
5134 }
5135 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5136 /*
5137 * Realtime extent is lined up at the end but not
5138 * at the front. We'll get rid of full extents if
5139 * we can.
5140 */
5141 mod = mp->m_sb.sb_rextsize - mod;
5142 if (del.br_blockcount > mod) {
5143 del.br_blockcount -= mod;
5144 del.br_startoff += mod;
5145 del.br_startblock += mod;
5146 } else if ((del.br_startoff == start &&
5147 (del.br_state == XFS_EXT_UNWRITTEN ||
5148 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005149 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 /*
5151 * Can't make it unwritten. There isn't
5152 * a full extent here so just skip it.
5153 */
5154 ASSERT(bno >= del.br_blockcount);
5155 bno -= del.br_blockcount;
5156 if (bno < got.br_startoff) {
5157 if (--lastx >= 0)
5158 xfs_bmbt_get_all(--ep, &got);
5159 }
5160 continue;
5161 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5162 /*
5163 * This one is already unwritten.
5164 * It must have a written left neighbor.
5165 * Unwrite the killed part of that one and
5166 * try again.
5167 */
5168 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005169 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5170 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005172 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 ASSERT(del.br_startblock ==
5174 prev.br_startblock + prev.br_blockcount);
5175 if (prev.br_startoff < start) {
5176 mod = start - prev.br_startoff;
5177 prev.br_blockcount -= mod;
5178 prev.br_startblock += mod;
5179 prev.br_startoff = start;
5180 }
5181 prev.br_state = XFS_EXT_UNWRITTEN;
5182 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5183 &prev, firstblock, flist, &logflags,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005184 XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 if (error)
5186 goto error0;
5187 goto nodelete;
5188 } else {
5189 ASSERT(del.br_state == XFS_EXT_NORM);
5190 del.br_state = XFS_EXT_UNWRITTEN;
5191 error = xfs_bmap_add_extent(ip, lastx, &cur,
5192 &del, firstblock, flist, &logflags,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005193 XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 if (error)
5195 goto error0;
5196 goto nodelete;
5197 }
5198 }
5199 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005200 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005201 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005202 if (isrt) {
5203 xfs_filblks_t rtexts;
5204
5205 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5206 do_div(rtexts, mp->m_sb.sb_rextsize);
5207 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005208 (int64_t)rtexts, rsvd);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005209 (void)xfs_trans_reserve_quota_nblks(NULL,
5210 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005211 XFS_QMOPT_RES_RTBLKS);
5212 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00005213 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005214 (int64_t)del.br_blockcount, rsvd);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005215 (void)xfs_trans_reserve_quota_nblks(NULL,
5216 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 ip->i_delayed_blks -= del.br_blockcount;
5220 if (cur)
5221 cur->bc_private.b.flags |=
5222 XFS_BTCUR_BPRV_WASDEL;
5223 } else if (cur)
5224 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5225 /*
5226 * If it's the case where the directory code is running
5227 * with no block reservation, and the deleted block is in
5228 * the middle of its extent, and the resulting insert
5229 * of an extent would cause transformation to btree format,
5230 * then reject it. The calling code will then swap
5231 * blocks around instead.
5232 * We have to do this now, rather than waiting for the
5233 * conversion to btree format, since the transaction
5234 * will be dirty.
5235 */
5236 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5237 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5238 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5239 del.br_startoff > got.br_startoff &&
5240 del.br_startoff + del.br_blockcount <
5241 got.br_startoff + got.br_blockcount) {
5242 error = XFS_ERROR(ENOSPC);
5243 goto error0;
5244 }
5245 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005246 &tmp_logflags, whichfork, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 logflags |= tmp_logflags;
5248 if (error)
5249 goto error0;
5250 bno = del.br_startoff - 1;
5251nodelete:
5252 lastx = ifp->if_lastex;
5253 /*
5254 * If not done go on to the next (previous) record.
5255 * Reset ep in case the extents array was re-alloced.
5256 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005257 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5259 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5260 xfs_bmbt_get_startoff(ep) > bno) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005261 if (--lastx >= 0)
5262 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 }
5264 if (lastx >= 0)
5265 xfs_bmbt_get_all(ep, &got);
5266 extno++;
5267 }
5268 }
5269 ifp->if_lastex = lastx;
5270 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5271 ASSERT(ifp->if_ext_max ==
5272 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5273 /*
5274 * Convert to a btree if necessary.
5275 */
5276 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5277 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5278 ASSERT(cur == NULL);
5279 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5280 &cur, 0, &tmp_logflags, whichfork);
5281 logflags |= tmp_logflags;
5282 if (error)
5283 goto error0;
5284 }
5285 /*
5286 * transform from btree to extents, give it cur
5287 */
5288 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5289 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5290 ASSERT(cur != NULL);
5291 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5292 whichfork);
5293 logflags |= tmp_logflags;
5294 if (error)
5295 goto error0;
5296 }
5297 /*
5298 * transform from extents to local?
5299 */
5300 ASSERT(ifp->if_ext_max ==
5301 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5302 error = 0;
5303error0:
5304 /*
5305 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005306 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005308 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005310 logflags &= ~xfs_ilog_fext(whichfork);
5311 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005313 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 /*
5315 * Log inode even in the error case, if the transaction
5316 * is dirty we'll need to shut down the filesystem.
5317 */
5318 if (logflags)
5319 xfs_trans_log_inode(tp, ip, logflags);
5320 if (cur) {
5321 if (!error) {
5322 *firstblock = cur->bc_private.b.firstblock;
5323 cur->bc_private.b.allocated = 0;
5324 }
5325 xfs_btree_del_cursor(cur,
5326 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5327 }
5328 return error;
5329}
5330
5331/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005332 * returns 1 for success, 0 if we failed to map the extent.
5333 */
5334STATIC int
5335xfs_getbmapx_fix_eof_hole(
5336 xfs_inode_t *ip, /* xfs incore inode pointer */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005337 struct getbmapx *out, /* output structure */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005338 int prealloced, /* this is a file with
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005339 * preallocated data space */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005340 __int64_t end, /* last block requested */
5341 xfs_fsblock_t startblock)
5342{
5343 __int64_t fixlen;
5344 xfs_mount_t *mp; /* file system mount point */
Eric Sandeen5af317c2008-11-28 14:23:35 +11005345 xfs_ifork_t *ifp; /* inode fork pointer */
5346 xfs_extnum_t lastx; /* last extent pointer */
5347 xfs_fileoff_t fileblock;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005348
5349 if (startblock == HOLESTARTBLOCK) {
5350 mp = ip->i_mount;
5351 out->bmv_block = -1;
5352 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5353 fixlen -= out->bmv_offset;
5354 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5355 /* Came to hole at EOF. Trim it. */
5356 if (fixlen <= 0)
5357 return 0;
5358 out->bmv_length = fixlen;
5359 }
5360 } else {
Eric Sandeen5af317c2008-11-28 14:23:35 +11005361 if (startblock == DELAYSTARTBLOCK)
5362 out->bmv_block = -2;
5363 else
Eric Sandeen9d87c312009-01-14 23:22:07 -06005364 out->bmv_block = xfs_fsb_to_db(ip, startblock);
Eric Sandeen5af317c2008-11-28 14:23:35 +11005365 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5366 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5367 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5368 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5369 out->bmv_oflags |= BMV_OF_LAST;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005370 }
5371
5372 return 1;
5373}
5374
5375/*
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005376 * Get inode's extents as described in bmv, and format for output.
5377 * Calls formatter to fill the user's buffer until all extents
5378 * are mapped, until the passed-in bmv->bmv_count slots have
5379 * been filled, or until the formatter short-circuits the loop,
5380 * if it is tracking filled-in extents on its own.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 */
5382int /* error code */
5383xfs_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10005384 xfs_inode_t *ip,
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005385 struct getbmapx *bmv, /* user bmap structure */
5386 xfs_bmap_format_t formatter, /* format to user */
5387 void *arg) /* formatter arg */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388{
5389 __int64_t bmvend; /* last block requested */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005390 int error = 0; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 __int64_t fixlen; /* length for -1 case */
5392 int i; /* extent number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 int lock; /* lock state */
5394 xfs_bmbt_irec_t *map; /* buffer for user's data */
5395 xfs_mount_t *mp; /* file system mount point */
5396 int nex; /* # of user extents can do */
5397 int nexleft; /* # of user extents left */
5398 int subnex; /* # of bmapi's can do */
5399 int nmap; /* number of map entries */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005400 struct getbmapx *out; /* output structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 int whichfork; /* data or attr fork */
5402 int prealloced; /* this is a file with
5403 * preallocated data space */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005404 int iflags; /* interface flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 int bmapi_flags; /* flags for xfs_bmapi */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005406 int cur_ext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 mp = ip->i_mount;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005409 iflags = bmv->bmv_iflags;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005410 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 if (whichfork == XFS_ATTR_FORK) {
5413 if (XFS_IFORK_Q(ip)) {
5414 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5415 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5416 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5417 return XFS_ERROR(EINVAL);
5418 } else if (unlikely(
5419 ip->i_d.di_aformat != 0 &&
5420 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5421 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5422 ip->i_mount);
5423 return XFS_ERROR(EFSCORRUPTED);
5424 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005425
5426 prealloced = 0;
5427 fixlen = 1LL << 32;
5428 } else {
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005429 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5430 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5431 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5432 return XFS_ERROR(EINVAL);
5433
David Chinner957d0eb2007-06-18 16:50:37 +10005434 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005435 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 prealloced = 1;
5437 fixlen = XFS_MAXIOFFSET(mp);
5438 } else {
5439 prealloced = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005440 fixlen = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 }
5443
5444 if (bmv->bmv_length == -1) {
5445 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005446 bmv->bmv_length =
5447 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5448 } else if (bmv->bmv_length == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 bmv->bmv_entries = 0;
5450 return 0;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005451 } else if (bmv->bmv_length < 0) {
5452 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005454
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 nex = bmv->bmv_count - 1;
5456 if (nex <= 0)
5457 return XFS_ERROR(EINVAL);
5458 bmvend = bmv->bmv_offset + bmv->bmv_length;
5459
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005460
5461 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5462 return XFS_ERROR(ENOMEM);
5463 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5464 if (!out)
5465 return XFS_ERROR(ENOMEM);
5466
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005468 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5469 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5470 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5471 if (error)
5472 goto out_unlock_iolock;
Niv Sardie12070a2008-03-06 13:43:03 +11005473 }
Dave Chinner309c8482010-11-30 15:16:02 +11005474 /*
5475 * even after flushing the inode, there can still be delalloc
5476 * blocks on the inode beyond EOF due to speculative
5477 * preallocation. These are not removed until the release
5478 * function is called or the inode is inactivated. Hence we
5479 * cannot assert here that ip->i_delayed_blks == 0.
5480 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482
5483 lock = xfs_ilock_map_shared(ip);
5484
5485 /*
5486 * Don't let nex be bigger than the number of extents
5487 * we can have assuming alternating holes and real extents.
5488 */
5489 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5490 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5491
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005492 bmapi_flags = xfs_bmapi_aflag(whichfork);
5493 if (!(iflags & BMV_IF_PREALLOC))
5494 bmapi_flags |= XFS_BMAPI_IGSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495
5496 /*
5497 * Allocate enough space to handle "subnex" maps at a time.
5498 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005499 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 subnex = 16;
Christoph Hellwigca35dcd2009-07-18 18:14:54 -04005501 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005502 if (!map)
5503 goto out_unlock_ilock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
5505 bmv->bmv_entries = 0;
5506
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005507 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5508 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5509 error = 0;
5510 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 }
5512
5513 nexleft = nex;
5514
5515 do {
5516 nmap = (nexleft > subnex) ? subnex : nexleft;
5517 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5518 XFS_BB_TO_FSB(mp, bmv->bmv_length),
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005519 bmapi_flags, NULL, 0, map, &nmap,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005520 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 if (error)
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005522 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 ASSERT(nmap <= subnex);
5524
5525 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005526 out[cur_ext].bmv_oflags = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005527 if (map[i].br_state == XFS_EXT_UNWRITTEN)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005528 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005529 else if (map[i].br_startblock == DELAYSTARTBLOCK)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005530 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5531 out[cur_ext].bmv_offset =
5532 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5533 out[cur_ext].bmv_length =
5534 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5535 out[cur_ext].bmv_unused1 = 0;
5536 out[cur_ext].bmv_unused2 = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005537 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5538 (map[i].br_startblock != DELAYSTARTBLOCK));
Yingping Lu9af0a702005-11-02 15:09:54 +11005539 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005540 whichfork == XFS_ATTR_FORK) {
5541 /* came to the end of attribute fork */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005542 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005543 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005545
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005546 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5547 prealloced, bmvend,
5548 map[i].br_startblock))
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005549 goto out_free_map;
5550
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005551 bmv->bmv_offset =
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005552 out[cur_ext].bmv_offset +
5553 out[cur_ext].bmv_length;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005554 bmv->bmv_length =
5555 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
Tao Ma9af25462010-08-30 02:44:03 +00005556
5557 /*
5558 * In case we don't want to return the hole,
5559 * don't increase cur_ext so that we can reuse
5560 * it in the next loop.
5561 */
5562 if ((iflags & BMV_IF_NO_HOLES) &&
5563 map[i].br_startblock == HOLESTARTBLOCK) {
5564 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5565 continue;
5566 }
5567
5568 nexleft--;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005569 bmv->bmv_entries++;
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005570 cur_ext++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 }
5572 } while (nmap && nexleft && bmv->bmv_length);
5573
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005574 out_free_map:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10005575 kmem_free(map);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005576 out_unlock_ilock:
5577 xfs_iunlock_map_shared(ip, lock);
5578 out_unlock_iolock:
5579 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005580
5581 for (i = 0; i < cur_ext; i++) {
5582 int full = 0; /* user array is full */
5583
5584 /* format results & advance arg */
5585 error = formatter(&arg, &out[i], &full);
5586 if (error || full)
5587 break;
5588 }
5589
Felix Blyakher7747a0b2009-06-11 17:07:28 -05005590 kmem_free(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 return error;
5592}
5593
5594/*
5595 * Check the last inode extent to determine whether this allocation will result
5596 * in blocks being allocated at the end of the file. When we allocate new data
5597 * blocks at the end of the file which do not start at the previous data block,
5598 * we will try to align the new blocks at stripe unit boundaries.
5599 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10005600STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601xfs_bmap_isaeof(
5602 xfs_inode_t *ip, /* incore inode pointer */
5603 xfs_fileoff_t off, /* file offset in fsblocks */
5604 int whichfork, /* data or attribute fork */
5605 char *aeof) /* return value */
5606{
5607 int error; /* error return value */
5608 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005609 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005610 xfs_extnum_t nextents; /* number of file extents */
5611 xfs_bmbt_irec_t s; /* expanded extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612
5613 ASSERT(whichfork == XFS_DATA_FORK);
5614 ifp = XFS_IFORK_PTR(ip, whichfork);
5615 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5616 (error = xfs_iread_extents(NULL, ip, whichfork)))
5617 return error;
5618 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5619 if (nextents == 0) {
5620 *aeof = 1;
5621 return 0;
5622 }
5623 /*
5624 * Go to the last extent
5625 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005626 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 xfs_bmbt_get_all(lastrec, &s);
5628 /*
5629 * Check we are allocating in the last extent (for delayed allocations)
5630 * or past the last extent for non-delayed allocations.
5631 */
5632 *aeof = (off >= s.br_startoff &&
5633 off < s.br_startoff + s.br_blockcount &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06005634 isnullstartblock(s.br_startblock)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 off >= s.br_startoff + s.br_blockcount;
5636 return 0;
5637}
5638
5639/*
5640 * Check if the endoff is outside the last extent. If so the caller will grow
5641 * the allocation to a stripe unit boundary.
5642 */
5643int /* error */
5644xfs_bmap_eof(
5645 xfs_inode_t *ip, /* incore inode pointer */
5646 xfs_fileoff_t endoff, /* file offset in fsblocks */
5647 int whichfork, /* data or attribute fork */
5648 int *eof) /* result value */
5649{
5650 xfs_fsblock_t blockcount; /* extent block count */
5651 int error; /* error return value */
5652 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005653 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005654 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 xfs_fileoff_t startoff; /* extent starting file offset */
5656
5657 ASSERT(whichfork == XFS_DATA_FORK);
5658 ifp = XFS_IFORK_PTR(ip, whichfork);
5659 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5660 (error = xfs_iread_extents(NULL, ip, whichfork)))
5661 return error;
5662 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5663 if (nextents == 0) {
5664 *eof = 1;
5665 return 0;
5666 }
5667 /*
5668 * Go to the last extent
5669 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005670 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 startoff = xfs_bmbt_get_startoff(lastrec);
5672 blockcount = xfs_bmbt_get_blockcount(lastrec);
5673 *eof = endoff >= startoff + blockcount;
5674 return 0;
5675}
5676
5677#ifdef DEBUG
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005678STATIC struct xfs_buf *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679xfs_bmap_get_bp(
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005680 struct xfs_btree_cur *cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 xfs_fsblock_t bno)
5682{
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005683 struct xfs_log_item_desc *lidp;
5684 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685
5686 if (!cur)
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005687 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005689 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5690 if (!cur->bc_bufs[i])
5691 break;
5692 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5693 return cur->bc_bufs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005696 /* Chase down all the log items to see if the bp is there */
5697 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
Christoph Hellwige98c4142010-06-23 18:11:15 +10005698 struct xfs_buf_log_item *bip;
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005699 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5700 if (bip->bli_item.li_type == XFS_LI_BUF &&
5701 XFS_BUF_ADDR(bip->bli_buf) == bno)
5702 return bip->bli_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 }
Christoph Hellwige98c4142010-06-23 18:11:15 +10005704
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005705 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706}
5707
Hannes Eder3180e662009-03-04 19:34:10 +01005708STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709xfs_check_block(
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005710 struct xfs_btree_block *block,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 xfs_mount_t *mp,
5712 int root,
5713 short sz)
5714{
5715 int i, j, dmxr;
Christoph Hellwig576039c2006-09-28 10:58:06 +10005716 __be64 *pp, *thispa; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 xfs_bmbt_key_t *prevp, *keyp;
5718
Christoph Hellwig16259e72005-11-02 15:11:25 +11005719 ASSERT(be16_to_cpu(block->bb_level) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
5721 prevp = NULL;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005722 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 dmxr = mp->m_bmap_dmxr[0];
Christoph Hellwig136341b2008-10-30 17:11:40 +11005724 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725
5726 if (prevp) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005727 ASSERT(be64_to_cpu(prevp->br_startoff) <
5728 be64_to_cpu(keyp->br_startoff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 }
5730 prevp = keyp;
5731
5732 /*
5733 * Compare the block numbers to see if there are dups.
5734 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11005735 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005736 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005737 else
5738 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5739
Christoph Hellwig16259e72005-11-02 15:11:25 +11005740 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11005741 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005742 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005743 else
5744 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005745 if (*thispa == *pp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005747 __func__, j, i,
Christoph Hellwig576039c2006-09-28 10:58:06 +10005748 (unsigned long long)be64_to_cpu(*thispa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 panic("%s: ptrs are equal in node\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005750 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 }
5752 }
5753 }
5754}
5755
5756/*
5757 * Check that the extents for the inode ip are in the right order in all
5758 * btree leaves.
5759 */
5760
5761STATIC void
5762xfs_bmap_check_leaf_extents(
5763 xfs_btree_cur_t *cur, /* btree cursor or null */
5764 xfs_inode_t *ip, /* incore inode pointer */
5765 int whichfork) /* data or attr fork */
5766{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005767 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 xfs_fsblock_t bno; /* block # of "block" */
5769 xfs_buf_t *bp; /* buffer for "block" */
5770 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005771 xfs_extnum_t i=0, j; /* index into the extents list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 xfs_ifork_t *ifp; /* fork structure */
5773 int level; /* btree level, for checking */
5774 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10005775 __be64 *pp; /* pointer to block address */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005776 xfs_bmbt_rec_t *ep; /* pointer to current extent */
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005777 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005778 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 int bp_release = 0;
5780
5781 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5782 return;
5783 }
5784
5785 bno = NULLFSBLOCK;
5786 mp = ip->i_mount;
5787 ifp = XFS_IFORK_PTR(ip, whichfork);
5788 block = ifp->if_broot;
5789 /*
5790 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5791 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11005792 level = be16_to_cpu(block->bb_level);
5793 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11005795 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005796 bno = be64_to_cpu(*pp);
5797
5798 ASSERT(bno != NULLDFSBNO);
5799 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5800 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5801
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 /*
5803 * Go down the tree until leaf level is reached, following the first
5804 * pointer (leftmost) at each level.
5805 */
5806 while (level-- > 0) {
5807 /* See if buf is in cur first */
5808 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5809 if (bp) {
5810 bp_release = 0;
5811 } else {
5812 bp_release = 1;
5813 }
5814 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5815 XFS_BMAP_BTREE_REF)))
5816 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005817 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11005819 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 error0);
5821 if (level == 0)
5822 break;
5823
5824 /*
5825 * Check this block for basic sanity (increasing keys and
5826 * no duplicate blocks).
5827 */
5828
5829 xfs_check_block(block, mp, 0, 0);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005830 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005831 bno = be64_to_cpu(*pp);
5832 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 if (bp_release) {
5834 bp_release = 0;
5835 xfs_trans_brelse(NULL, bp);
5836 }
5837 }
5838
5839 /*
5840 * Here with bp and block set to the leftmost leaf node in the tree.
5841 */
5842 i = 0;
5843
5844 /*
5845 * Loop over all leaf nodes checking that all extents are in the right order.
5846 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 xfs_fsblock_t nextbno;
5849 xfs_extnum_t num_recs;
5850
5851
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005852 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853
5854 /*
5855 * Read-ahead the next leaf block, if any.
5856 */
5857
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005858 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859
5860 /*
5861 * Check all the extents to make sure they are OK.
5862 * If we had a previous block, the last entry should
5863 * conform with the first entry in this one.
5864 */
5865
Christoph Hellwig136341b2008-10-30 17:11:40 +11005866 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005867 if (i) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005868 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
5869 xfs_bmbt_disk_get_blockcount(&last) <=
5870 xfs_bmbt_disk_get_startoff(ep));
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005871 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005872 for (j = 1; j < num_recs; j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11005873 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005874 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
5875 xfs_bmbt_disk_get_blockcount(ep) <=
5876 xfs_bmbt_disk_get_startoff(nextp));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005877 ep = nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005880 last = *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 i += num_recs;
5882 if (bp_release) {
5883 bp_release = 0;
5884 xfs_trans_brelse(NULL, bp);
5885 }
5886 bno = nextbno;
5887 /*
5888 * If we've reached the end, stop.
5889 */
5890 if (bno == NULLFSBLOCK)
5891 break;
5892
5893 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5894 if (bp) {
5895 bp_release = 0;
5896 } else {
5897 bp_release = 1;
5898 }
5899 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5900 XFS_BMAP_BTREE_REF)))
5901 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005902 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 }
5904 if (bp_release) {
5905 bp_release = 0;
5906 xfs_trans_brelse(NULL, bp);
5907 }
5908 return;
5909
5910error0:
Harvey Harrison34a622b2008-04-10 12:19:21 +10005911 cmn_err(CE_WARN, "%s: at error0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 if (bp_release)
5913 xfs_trans_brelse(NULL, bp);
5914error_norelse:
5915 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005916 __func__, i);
5917 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 return;
5919}
5920#endif
5921
5922/*
5923 * Count fsblocks of the given fork.
5924 */
5925int /* error */
5926xfs_bmap_count_blocks(
5927 xfs_trans_t *tp, /* transaction pointer */
5928 xfs_inode_t *ip, /* incore inode */
5929 int whichfork, /* data or attr fork */
5930 int *count) /* out: count of blocks */
5931{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005932 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 xfs_fsblock_t bno; /* block # of "block" */
5934 xfs_ifork_t *ifp; /* fork structure */
5935 int level; /* btree level, for checking */
5936 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10005937 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938
5939 bno = NULLFSBLOCK;
5940 mp = ip->i_mount;
5941 ifp = XFS_IFORK_PTR(ip, whichfork);
5942 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
Ruben Porrasc94312d2008-08-13 16:52:25 +10005943 xfs_bmap_count_leaves(ifp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
Ruben Porrasc94312d2008-08-13 16:52:25 +10005945 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 return 0;
5947 }
5948
5949 /*
5950 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5951 */
5952 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11005953 level = be16_to_cpu(block->bb_level);
5954 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11005955 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005956 bno = be64_to_cpu(*pp);
5957 ASSERT(bno != NULLDFSBNO);
5958 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5959 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005961 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
5963 mp);
5964 return XFS_ERROR(EFSCORRUPTED);
5965 }
5966
5967 return 0;
5968}
5969
5970/*
5971 * Recursively walks each level of a btree
5972 * to count total fsblocks is use.
5973 */
David Chinnera8272ce2007-11-23 16:28:09 +11005974STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975xfs_bmap_count_tree(
5976 xfs_mount_t *mp, /* file system mount point */
5977 xfs_trans_t *tp, /* transaction pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005978 xfs_ifork_t *ifp, /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 xfs_fsblock_t blockno, /* file system block number */
5980 int levelin, /* level in btree */
5981 int *count) /* Count of blocks */
5982{
5983 int error;
5984 xfs_buf_t *bp, *nbp;
5985 int level = levelin;
Christoph Hellwig576039c2006-09-28 10:58:06 +10005986 __be64 *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 xfs_fsblock_t bno = blockno;
5988 xfs_fsblock_t nextbno;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005989 struct xfs_btree_block *block, *nextblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991
5992 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
5993 return error;
5994 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005995 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996
5997 if (--level) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +02005998 /* Not at node above leaves, count this level of nodes */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005999 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 while (nextbno != NULLFSBLOCK) {
6001 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6002 0, &nbp, XFS_BMAP_BTREE_REF)))
6003 return error;
6004 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006005 nextblock = XFS_BUF_TO_BLOCK(nbp);
6006 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 xfs_trans_brelse(tp, nbp);
6008 }
6009
6010 /* Dive to the next level */
Christoph Hellwig136341b2008-10-30 17:11:40 +11006011 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006012 bno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 if (unlikely((error =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006014 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 xfs_trans_brelse(tp, bp);
6016 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6017 XFS_ERRLEVEL_LOW, mp);
6018 return XFS_ERROR(EFSCORRUPTED);
6019 }
6020 xfs_trans_brelse(tp, bp);
6021 } else {
6022 /* count all level 1 nodes and their leaves */
6023 for (;;) {
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006024 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Christoph Hellwig16259e72005-11-02 15:11:25 +11006025 numrecs = be16_to_cpu(block->bb_numrecs);
Christoph Hellwig136341b2008-10-30 17:11:40 +11006026 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 xfs_trans_brelse(tp, bp);
6028 if (nextbno == NULLFSBLOCK)
6029 break;
6030 bno = nextbno;
6031 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6032 XFS_BMAP_BTREE_REF)))
6033 return error;
6034 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006035 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 }
6037 }
6038 return 0;
6039}
6040
6041/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006042 * Count leaf blocks given a range of extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006044STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006046 xfs_ifork_t *ifp,
6047 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 int numrecs,
6049 int *count)
6050{
6051 int b;
6052
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006053 for (b = 0; b < numrecs; b++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006054 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
Yingping Lu91e11082005-11-02 15:10:24 +11006055 *count += xfs_bmbt_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006056 }
Yingping Lu91e11082005-11-02 15:10:24 +11006057}
6058
6059/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006060 * Count leaf blocks given a range of extent records originally
6061 * in btree format.
Yingping Lu91e11082005-11-02 15:10:24 +11006062 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006063STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +11006064xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +11006065 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006066 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +11006067 int numrecs,
6068 int *count)
6069{
6070 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006071 xfs_bmbt_rec_t *frp;
Yingping Lu91e11082005-11-02 15:10:24 +11006072
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006073 for (b = 1; b <= numrecs; b++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11006074 frp = XFS_BMBT_REC_ADDR(mp, block, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 *count += xfs_bmbt_disk_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077}
Dave Chinnerc726de42010-11-30 15:14:39 +11006078
6079/*
6080 * dead simple method of punching delalyed allocation blocks from a range in
6081 * the inode. Walks a block at a time so will be slow, but is only executed in
6082 * rare error cases so the overhead is not critical. This will alays punch out
6083 * both the start and end blocks, even if the ranges only partially overlap
6084 * them, so it is up to the caller to ensure that partial blocks are not
6085 * passed in.
6086 */
6087int
6088xfs_bmap_punch_delalloc_range(
6089 struct xfs_inode *ip,
6090 xfs_fileoff_t start_fsb,
6091 xfs_fileoff_t length)
6092{
6093 xfs_fileoff_t remaining = length;
6094 int error = 0;
6095
6096 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6097
6098 do {
6099 int done;
6100 xfs_bmbt_irec_t imap;
6101 int nimaps = 1;
6102 xfs_fsblock_t firstblock;
6103 xfs_bmap_free_t flist;
6104
6105 /*
6106 * Map the range first and check that it is a delalloc extent
6107 * before trying to unmap the range. Otherwise we will be
6108 * trying to remove a real extent (which requires a
6109 * transaction) or a hole, which is probably a bad idea...
6110 */
6111 error = xfs_bmapi(NULL, ip, start_fsb, 1,
6112 XFS_BMAPI_ENTIRE, NULL, 0, &imap,
6113 &nimaps, NULL);
6114
6115 if (error) {
6116 /* something screwed, just bail */
6117 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
6118 xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
6119 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6120 ip->i_ino, start_fsb);
6121 }
6122 break;
6123 }
6124 if (!nimaps) {
6125 /* nothing there */
6126 goto next_block;
6127 }
6128 if (imap.br_startblock != DELAYSTARTBLOCK) {
6129 /* been converted, ignore */
6130 goto next_block;
6131 }
6132 WARN_ON(imap.br_blockcount == 0);
6133
6134 /*
6135 * Note: while we initialise the firstblock/flist pair, they
6136 * should never be used because blocks should never be
6137 * allocated or freed for a delalloc extent and hence we need
6138 * don't cancel or finish them after the xfs_bunmapi() call.
6139 */
6140 xfs_bmap_init(&flist, &firstblock);
6141 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6142 &flist, &done);
6143 if (error)
6144 break;
6145
6146 ASSERT(!flist.xbf_count && !flist.xbf_first);
6147next_block:
6148 start_fsb++;
6149 remaining--;
6150 } while(remaining > 0);
6151
6152 return error;
6153}