blob: 1869fb97381968046491d9349684ff12bc038cee [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"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_btree.h"
37#include "xfs_dmapi.h"
38#include "xfs_mount.h"
39#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_itable.h"
Nathan Scottf6c2d1f2006-06-20 13:04:51 +100041#include "xfs_dir2_data.h"
42#include "xfs_dir2_leaf.h"
43#include "xfs_dir2_block.h"
Nathan Scotta844f452005-11-02 14:38:42 +110044#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_extfree_item.h"
46#include "xfs_alloc.h"
47#include "xfs_bmap.h"
48#include "xfs_rtalloc.h"
49#include "xfs_error.h"
Nathan Scottd8cc8902005-11-02 10:34:53 +110050#include "xfs_attr_leaf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "xfs_rw.h"
52#include "xfs_quota.h"
53#include "xfs_trans_space.h"
54#include "xfs_buf_item.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100055#include "xfs_filestream.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100056#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000057#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59
60#ifdef DEBUG
61STATIC void
62xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
63#endif
64
65kmem_zone_t *xfs_bmap_free_item_zone;
66
67/*
68 * Prototypes for internal bmap routines.
69 */
70
71
72/*
73 * Called from xfs_bmap_add_attrfork to handle extents format files.
74 */
75STATIC int /* error */
76xfs_bmap_add_attrfork_extents(
77 xfs_trans_t *tp, /* transaction pointer */
78 xfs_inode_t *ip, /* incore inode pointer */
79 xfs_fsblock_t *firstblock, /* first block allocated */
80 xfs_bmap_free_t *flist, /* blocks to free at commit */
81 int *flags); /* inode logging flags */
82
83/*
84 * Called from xfs_bmap_add_attrfork to handle local format files.
85 */
86STATIC int /* error */
87xfs_bmap_add_attrfork_local(
88 xfs_trans_t *tp, /* transaction pointer */
89 xfs_inode_t *ip, /* incore inode pointer */
90 xfs_fsblock_t *firstblock, /* first block allocated */
91 xfs_bmap_free_t *flist, /* blocks to free at commit */
92 int *flags); /* inode logging flags */
93
94/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +110095 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 * after allocating space (or doing a delayed allocation).
97 */
98STATIC int /* error */
99xfs_bmap_add_extent(
100 xfs_inode_t *ip, /* incore inode pointer */
101 xfs_extnum_t idx, /* extent number to update/insert */
102 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100103 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 xfs_fsblock_t *first, /* pointer to firstblock variable */
105 xfs_bmap_free_t *flist, /* list of extents to be freed */
106 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000107 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 int whichfork, /* data or attr fork */
109 int rsvd); /* OK to allocate reserved blocks */
110
111/*
112 * Called by xfs_bmap_add_extent to handle cases converting a delayed
113 * allocation to a real allocation.
114 */
115STATIC int /* error */
116xfs_bmap_add_extent_delay_real(
117 xfs_inode_t *ip, /* incore inode pointer */
118 xfs_extnum_t idx, /* extent number to update/insert */
119 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100120 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
122 xfs_fsblock_t *first, /* pointer to firstblock variable */
123 xfs_bmap_free_t *flist, /* list of extents to be freed */
124 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000125 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int rsvd); /* OK to allocate reserved blocks */
127
128/*
129 * Called by xfs_bmap_add_extent to handle cases converting a hole
130 * to a delayed allocation.
131 */
132STATIC int /* error */
133xfs_bmap_add_extent_hole_delay(
134 xfs_inode_t *ip, /* incore inode pointer */
135 xfs_extnum_t idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100136 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int *logflagsp,/* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000138 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int rsvd); /* OK to allocate reserved blocks */
140
141/*
142 * Called by xfs_bmap_add_extent to handle cases converting a hole
143 * to a real allocation.
144 */
145STATIC int /* error */
146xfs_bmap_add_extent_hole_real(
147 xfs_inode_t *ip, /* incore inode pointer */
148 xfs_extnum_t idx, /* extent number to update/insert */
149 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100150 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000152 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int whichfork); /* data or attr fork */
154
155/*
156 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
157 * allocation to a real allocation or vice versa.
158 */
159STATIC int /* error */
160xfs_bmap_add_extent_unwritten_real(
161 xfs_inode_t *ip, /* incore inode pointer */
162 xfs_extnum_t idx, /* extent number to update/insert */
163 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100164 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000165 int *logflagsp, /* inode logging flags */
166 xfs_extdelta_t *delta); /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168/*
169 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
170 * It figures out where to ask the underlying allocator to put the new extent.
171 */
172STATIC int /* error */
173xfs_bmap_alloc(
174 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
175
176/*
177 * Transform a btree format file with only one leaf node, where the
178 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100179 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * give up the space for the btree root and pitch the leaf block.
181 */
182STATIC int /* error */
183xfs_bmap_btree_to_extents(
184 xfs_trans_t *tp, /* transaction pointer */
185 xfs_inode_t *ip, /* incore inode pointer */
186 xfs_btree_cur_t *cur, /* btree cursor */
187 int *logflagsp, /* inode logging flags */
188 int whichfork); /* data or attr fork */
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100191 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * after removing space (or undoing a delayed allocation).
193 */
194STATIC int /* error */
195xfs_bmap_del_extent(
196 xfs_inode_t *ip, /* incore inode pointer */
197 xfs_trans_t *tp, /* current trans pointer */
198 xfs_extnum_t idx, /* extent number to update/insert */
199 xfs_bmap_free_t *flist, /* list of extents to be freed */
200 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100201 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int *logflagsp,/* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000203 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 int whichfork, /* data or attr fork */
205 int rsvd); /* OK to allocate reserved blocks */
206
207/*
208 * Remove the entry "free" from the free item list. Prev points to the
209 * previous entry, unless "free" is the head of the list.
210 */
211STATIC void
212xfs_bmap_del_free(
213 xfs_bmap_free_t *flist, /* free item list header */
214 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
215 xfs_bmap_free_item_t *free); /* list item to be freed */
216
217/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * Convert an extents-format file into a btree-format file.
219 * The new file will have a root block (in the inode) and a single child block.
220 */
221STATIC int /* error */
222xfs_bmap_extents_to_btree(
223 xfs_trans_t *tp, /* transaction pointer */
224 xfs_inode_t *ip, /* incore inode pointer */
225 xfs_fsblock_t *firstblock, /* first-block-allocated */
226 xfs_bmap_free_t *flist, /* blocks freed in xaction */
227 xfs_btree_cur_t **curp, /* cursor returned to caller */
228 int wasdel, /* converting a delayed alloc */
229 int *logflagsp, /* inode logging flags */
230 int whichfork); /* data or attr fork */
231
232/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 * Convert a local file to an extents file.
234 * This code is sort of bogus, since the file data needs to get
235 * logged so it won't be lost. The bmap-level manipulations are ok, though.
236 */
237STATIC int /* error */
238xfs_bmap_local_to_extents(
239 xfs_trans_t *tp, /* transaction pointer */
240 xfs_inode_t *ip, /* incore inode pointer */
241 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
242 xfs_extlen_t total, /* total blocks needed by transaction */
243 int *logflagsp, /* inode logging flags */
244 int whichfork); /* data or attr fork */
245
246/*
247 * Search the extents list for the inode, for the extent containing bno.
248 * If bno lies in a hole, point to the next entry. If bno lies past eof,
249 * *eofp will be set, and *prevp will contain the last entry (null if none).
250 * Else, *lastxp will be set to the index of the found
251 * entry; *gotp will contain the entry.
252 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000253STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254xfs_bmap_search_extents(
255 xfs_inode_t *ip, /* incore inode pointer */
256 xfs_fileoff_t bno, /* block number searched for */
257 int whichfork, /* data or attr fork */
258 int *eofp, /* out: end of file found */
259 xfs_extnum_t *lastxp, /* out: last extent index */
260 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
261 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
262
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000263/*
264 * Check the last inode extent to determine whether this allocation will result
265 * in blocks being allocated at the end of the file. When we allocate new data
266 * blocks at the end of the file which do not start at the previous data block,
267 * we will try to align the new blocks at stripe unit boundaries.
268 */
269STATIC int /* error */
270xfs_bmap_isaeof(
271 xfs_inode_t *ip, /* incore inode pointer */
272 xfs_fileoff_t off, /* file offset in fsblocks */
273 int whichfork, /* data or attribute fork */
274 char *aeof); /* return value */
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276/*
277 * Compute the worst-case number of indirect blocks that will be used
278 * for ip's delayed extent of length "len".
279 */
280STATIC xfs_filblks_t
281xfs_bmap_worst_indlen(
282 xfs_inode_t *ip, /* incore inode pointer */
283 xfs_filblks_t len); /* delayed extent length */
284
285#ifdef DEBUG
286/*
287 * Perform various validation checks on the values being returned
288 * from xfs_bmapi().
289 */
290STATIC void
291xfs_bmap_validate_ret(
292 xfs_fileoff_t bno,
293 xfs_filblks_t len,
294 int flags,
295 xfs_bmbt_irec_t *mval,
296 int nmap,
297 int ret_nmap);
298#else
299#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
300#endif /* DEBUG */
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302STATIC int
303xfs_bmap_count_tree(
304 xfs_mount_t *mp,
305 xfs_trans_t *tp,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100306 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 xfs_fsblock_t blockno,
308 int levelin,
309 int *count);
310
Ruben Porrasc94312d2008-08-13 16:52:25 +1000311STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100313 xfs_ifork_t *ifp,
314 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 int numrecs,
316 int *count);
317
Ruben Porrasc94312d2008-08-13 16:52:25 +1000318STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +1100319xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +1100320 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100321 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +1100322 int numrecs,
323 int *count);
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/*
326 * Bmap internal routines.
327 */
328
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100329STATIC int /* error */
330xfs_bmbt_lookup_eq(
331 struct xfs_btree_cur *cur,
332 xfs_fileoff_t off,
333 xfs_fsblock_t bno,
334 xfs_filblks_t len,
335 int *stat) /* success/failure */
336{
337 cur->bc_rec.b.br_startoff = off;
338 cur->bc_rec.b.br_startblock = bno;
339 cur->bc_rec.b.br_blockcount = len;
340 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
341}
342
343STATIC int /* error */
344xfs_bmbt_lookup_ge(
345 struct xfs_btree_cur *cur,
346 xfs_fileoff_t off,
347 xfs_fsblock_t bno,
348 xfs_filblks_t len,
349 int *stat) /* success/failure */
350{
351 cur->bc_rec.b.br_startoff = off;
352 cur->bc_rec.b.br_startblock = bno;
353 cur->bc_rec.b.br_blockcount = len;
354 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
355}
356
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100357/*
358* Update the record referred to by cur to the value given
359 * by [off, bno, len, state].
360 * This either works (return 0) or gets an EFSCORRUPTED error.
361 */
362STATIC int
363xfs_bmbt_update(
364 struct xfs_btree_cur *cur,
365 xfs_fileoff_t off,
366 xfs_fsblock_t bno,
367 xfs_filblks_t len,
368 xfs_exntst_t state)
369{
370 union xfs_btree_rec rec;
371
372 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
373 return xfs_btree_update(cur, &rec);
374}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/*
377 * Called from xfs_bmap_add_attrfork to handle btree format files.
378 */
379STATIC int /* error */
380xfs_bmap_add_attrfork_btree(
381 xfs_trans_t *tp, /* transaction pointer */
382 xfs_inode_t *ip, /* incore inode pointer */
383 xfs_fsblock_t *firstblock, /* first block allocated */
384 xfs_bmap_free_t *flist, /* blocks to free at commit */
385 int *flags) /* inode logging flags */
386{
387 xfs_btree_cur_t *cur; /* btree cursor */
388 int error; /* error return value */
389 xfs_mount_t *mp; /* file system mount struct */
390 int stat; /* newroot status */
391
392 mp = ip->i_mount;
393 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
394 *flags |= XFS_ILOG_DBROOT;
395 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100396 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 cur->bc_private.b.flist = flist;
398 cur->bc_private.b.firstblock = *firstblock;
399 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
400 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000401 /* must be at least one entry */
402 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100403 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 goto error0;
405 if (stat == 0) {
406 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
407 return XFS_ERROR(ENOSPC);
408 }
409 *firstblock = cur->bc_private.b.firstblock;
410 cur->bc_private.b.allocated = 0;
411 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
412 }
413 return 0;
414error0:
415 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
416 return error;
417}
418
419/*
420 * Called from xfs_bmap_add_attrfork to handle extents format files.
421 */
422STATIC int /* error */
423xfs_bmap_add_attrfork_extents(
424 xfs_trans_t *tp, /* transaction pointer */
425 xfs_inode_t *ip, /* incore inode pointer */
426 xfs_fsblock_t *firstblock, /* first block allocated */
427 xfs_bmap_free_t *flist, /* blocks to free at commit */
428 int *flags) /* inode logging flags */
429{
430 xfs_btree_cur_t *cur; /* bmap btree cursor */
431 int error; /* error return value */
432
433 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
434 return 0;
435 cur = NULL;
436 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
437 flags, XFS_DATA_FORK);
438 if (cur) {
439 cur->bc_private.b.allocated = 0;
440 xfs_btree_del_cursor(cur,
441 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
442 }
443 return error;
444}
445
446/*
447 * Called from xfs_bmap_add_attrfork to handle local format files.
448 */
449STATIC int /* error */
450xfs_bmap_add_attrfork_local(
451 xfs_trans_t *tp, /* transaction pointer */
452 xfs_inode_t *ip, /* incore inode pointer */
453 xfs_fsblock_t *firstblock, /* first block allocated */
454 xfs_bmap_free_t *flist, /* blocks to free at commit */
455 int *flags) /* inode logging flags */
456{
457 xfs_da_args_t dargs; /* args for dir/attr code */
458 int error; /* error return value */
459 xfs_mount_t *mp; /* mount structure pointer */
460
461 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
462 return 0;
463 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
464 mp = ip->i_mount;
465 memset(&dargs, 0, sizeof(dargs));
466 dargs.dp = ip;
467 dargs.firstblock = firstblock;
468 dargs.flist = flist;
469 dargs.total = mp->m_dirblkfsbs;
470 dargs.whichfork = XFS_DATA_FORK;
471 dargs.trans = tp;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000472 error = xfs_dir2_sf_to_block(&dargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 } else
474 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
475 XFS_DATA_FORK);
476 return error;
477}
478
479/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100480 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 * after allocating space (or doing a delayed allocation).
482 */
483STATIC int /* error */
484xfs_bmap_add_extent(
485 xfs_inode_t *ip, /* incore inode pointer */
486 xfs_extnum_t idx, /* extent number to update/insert */
487 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100488 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 xfs_fsblock_t *first, /* pointer to firstblock variable */
490 xfs_bmap_free_t *flist, /* list of extents to be freed */
491 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000492 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 int whichfork, /* data or attr fork */
494 int rsvd) /* OK to use reserved data blocks */
495{
496 xfs_btree_cur_t *cur; /* btree cursor or null */
497 xfs_filblks_t da_new; /* new count del alloc blocks used */
498 xfs_filblks_t da_old; /* old count del alloc blocks used */
499 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 xfs_ifork_t *ifp; /* inode fork ptr */
501 int logflags; /* returned value */
502 xfs_extnum_t nextents; /* number of extents in file now */
503
504 XFS_STATS_INC(xs_add_exlist);
505 cur = *curp;
506 ifp = XFS_IFORK_PTR(ip, whichfork);
507 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
508 ASSERT(idx <= nextents);
509 da_old = da_new = 0;
510 error = 0;
511 /*
512 * This is the first extent added to a new/empty file.
513 * Special case this one, so other routines get to assume there are
514 * already extents in the list.
515 */
516 if (nextents == 0) {
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000517 xfs_iext_insert(ip, 0, 1, new,
518 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 ASSERT(cur == NULL);
521 ifp->if_lastex = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600522 if (!isnullstartblock(new->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600524 logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 } else
526 logflags = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000527 /* DELTA: single new extent */
528 if (delta) {
529 if (delta->xed_startoff > new->br_startoff)
530 delta->xed_startoff = new->br_startoff;
531 if (delta->xed_blockcount <
532 new->br_startoff + new->br_blockcount)
533 delta->xed_blockcount = new->br_startoff +
534 new->br_blockcount;
535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537 /*
538 * Any kind of new delayed allocation goes here.
539 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600540 else if (isnullstartblock(new->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (cur)
542 ASSERT((cur->bc_private.b.flags &
543 XFS_BTCUR_BPRV_WASDEL) == 0);
Eric Sandeene9ed9d22007-05-08 13:48:56 +1000544 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000545 &logflags, delta, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 goto done;
547 }
548 /*
549 * Real allocation off the end of the file.
550 */
551 else if (idx == nextents) {
552 if (cur)
553 ASSERT((cur->bc_private.b.flags &
554 XFS_BTCUR_BPRV_WASDEL) == 0);
555 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000556 &logflags, delta, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 goto done;
558 } else {
559 xfs_bmbt_irec_t prev; /* old extent at offset idx */
560
561 /*
562 * Get the record referred to by idx.
563 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100564 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /*
566 * If it's a real allocation record, and the new allocation ends
567 * after the start of the referred to record, then we're filling
568 * in a delayed or unwritten allocation with a real one, or
569 * converting real back to unwritten.
570 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600571 if (!isnullstartblock(new->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 new->br_startoff + new->br_blockcount > prev.br_startoff) {
573 if (prev.br_state != XFS_EXT_UNWRITTEN &&
Eric Sandeen9d87c312009-01-14 23:22:07 -0600574 isnullstartblock(prev.br_startblock)) {
575 da_old = startblockval(prev.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (cur)
577 ASSERT(cur->bc_private.b.flags &
578 XFS_BTCUR_BPRV_WASDEL);
579 if ((error = xfs_bmap_add_extent_delay_real(ip,
580 idx, &cur, new, &da_new, first, flist,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000581 &logflags, delta, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 goto done;
583 } else if (new->br_state == XFS_EXT_NORM) {
584 ASSERT(new->br_state == XFS_EXT_NORM);
585 if ((error = xfs_bmap_add_extent_unwritten_real(
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000586 ip, idx, &cur, new, &logflags, delta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 goto done;
588 } else {
589 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
590 if ((error = xfs_bmap_add_extent_unwritten_real(
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000591 ip, idx, &cur, new, &logflags, delta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 goto done;
593 }
594 ASSERT(*curp == cur || *curp == NULL);
595 }
596 /*
597 * Otherwise we're filling in a hole with an allocation.
598 */
599 else {
600 if (cur)
601 ASSERT((cur->bc_private.b.flags &
602 XFS_BTCUR_BPRV_WASDEL) == 0);
603 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000604 new, &logflags, delta, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 goto done;
606 }
607 }
608
609 ASSERT(*curp == cur || *curp == NULL);
610 /*
611 * Convert to a btree if necessary.
612 */
613 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
614 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
615 int tmp_logflags; /* partial log flag return val */
616
617 ASSERT(cur == NULL);
618 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
619 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
620 logflags |= tmp_logflags;
621 if (error)
622 goto done;
623 }
624 /*
625 * Adjust for changes in reserved delayed indirect blocks.
626 * Nothing to do for disk quotas here.
627 */
628 if (da_old || da_new) {
629 xfs_filblks_t nblks;
630
631 nblks = da_new;
632 if (cur)
633 nblks += cur->bc_private.b.allocated;
634 ASSERT(nblks <= da_old);
635 if (nblks < da_old)
636 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100637 (int64_t)(da_old - nblks), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639 /*
640 * Clear out the allocated field, done with it now in any case.
641 */
642 if (cur) {
643 cur->bc_private.b.allocated = 0;
644 *curp = cur;
645 }
646done:
647#ifdef DEBUG
648 if (!error)
649 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
650#endif
651 *logflagsp = logflags;
652 return error;
653}
654
655/*
656 * Called by xfs_bmap_add_extent to handle cases converting a delayed
657 * allocation to a real allocation.
658 */
659STATIC int /* error */
660xfs_bmap_add_extent_delay_real(
661 xfs_inode_t *ip, /* incore inode pointer */
662 xfs_extnum_t idx, /* extent number to update/insert */
663 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100664 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
666 xfs_fsblock_t *first, /* pointer to firstblock variable */
667 xfs_bmap_free_t *flist, /* list of extents to be freed */
668 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000669 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 int rsvd) /* OK to use reserved data block allocation */
671{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 xfs_btree_cur_t *cur; /* btree cursor */
673 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000674 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100677 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 xfs_fileoff_t new_endoff; /* end offset of new entry */
679 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
680 /* left is 0, right is 1, prev is 2 */
681 int rval=0; /* return value (logging flags) */
682 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000683 xfs_filblks_t temp=0; /* value for dnew calculations */
684 xfs_filblks_t temp2=0;/* value for dnew calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 int tmp_rval; /* partial logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687#define LEFT r[0]
688#define RIGHT r[1]
689#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 /*
692 * Set up a bunch of variables to make the tests simpler.
693 */
694 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100695 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
696 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 xfs_bmbt_get_all(ep, &PREV);
698 new_endoff = new->br_startoff + new->br_blockcount;
699 ASSERT(PREV.br_startoff <= new->br_startoff);
700 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /*
703 * Set flags determining what part of the previous delayed allocation
704 * extent is being replaced by a real allocation.
705 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000706 if (PREV.br_startoff == new->br_startoff)
707 state |= BMAP_LEFT_FILLING;
708 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
709 state |= BMAP_RIGHT_FILLING;
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /*
712 * Check and set flags if this segment has a left neighbor.
713 * Don't set contiguous if the combined extent would be too large.
714 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000715 if (idx > 0) {
716 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100717 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000718
719 if (isnullstartblock(LEFT.br_startblock))
720 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000722
723 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
724 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
725 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
726 LEFT.br_state == new->br_state &&
727 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
728 state |= BMAP_LEFT_CONTIG;
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /*
731 * Check and set flags if this segment has a right neighbor.
732 * Don't set contiguous if the combined extent would be too large.
733 * Also check for all-three-contiguous being too large.
734 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000735 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
736 state |= BMAP_RIGHT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100737 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000738
739 if (isnullstartblock(RIGHT.br_startblock))
740 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000742
743 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
744 new_endoff == RIGHT.br_startoff &&
745 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
746 new->br_state == RIGHT.br_state &&
747 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
748 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
749 BMAP_RIGHT_FILLING)) !=
750 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
751 BMAP_RIGHT_FILLING) ||
752 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
753 <= MAXEXTLEN))
754 state |= BMAP_RIGHT_CONTIG;
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 error = 0;
757 /*
758 * Switch out based on the FILLING and CONTIG state bits.
759 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000760 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
761 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
762 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
763 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /*
765 * Filling in all of a previously delayed allocation extent.
766 * The left and right neighbors are both contiguous with new.
767 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000768 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100769 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 LEFT.br_blockcount + PREV.br_blockcount +
771 RIGHT.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000772 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
773
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000774 xfs_iext_remove(ip, idx, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 ip->i_df.if_lastex = idx - 1;
776 ip->i_d.di_nextents--;
777 if (cur == NULL)
778 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
779 else {
780 rval = XFS_ILOG_CORE;
781 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
782 RIGHT.br_startblock,
783 RIGHT.br_blockcount, &i)))
784 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000785 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100786 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000788 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100789 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000791 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
793 LEFT.br_startblock,
794 LEFT.br_blockcount +
795 PREV.br_blockcount +
796 RIGHT.br_blockcount, LEFT.br_state)))
797 goto done;
798 }
799 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000800 /* DELTA: Three in-core extents are replaced by one. */
801 temp = LEFT.br_startoff;
802 temp2 = LEFT.br_blockcount +
803 PREV.br_blockcount +
804 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 break;
806
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000807 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /*
809 * Filling in all of a previously delayed allocation extent.
810 * The left neighbor is contiguous, the right is not.
811 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000812 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100813 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000815 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 ip->i_df.if_lastex = idx - 1;
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000818 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (cur == NULL)
820 rval = XFS_ILOG_DEXT;
821 else {
822 rval = 0;
823 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
824 LEFT.br_startblock, LEFT.br_blockcount,
825 &i)))
826 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000827 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
829 LEFT.br_startblock,
830 LEFT.br_blockcount +
831 PREV.br_blockcount, LEFT.br_state)))
832 goto done;
833 }
834 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000835 /* DELTA: Two in-core extents are replaced by one. */
836 temp = LEFT.br_startoff;
837 temp2 = LEFT.br_blockcount +
838 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 break;
840
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000841 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 /*
843 * Filling in all of a previously delayed allocation extent.
844 * The right neighbor is contiguous, the left is not.
845 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000846 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 xfs_bmbt_set_startblock(ep, new->br_startblock);
848 xfs_bmbt_set_blockcount(ep,
849 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000850 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 ip->i_df.if_lastex = idx;
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000853 xfs_iext_remove(ip, idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (cur == NULL)
855 rval = XFS_ILOG_DEXT;
856 else {
857 rval = 0;
858 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
859 RIGHT.br_startblock,
860 RIGHT.br_blockcount, &i)))
861 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000862 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
864 new->br_startblock,
865 PREV.br_blockcount +
866 RIGHT.br_blockcount, PREV.br_state)))
867 goto done;
868 }
869 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000870 /* DELTA: Two in-core extents are replaced by one. */
871 temp = PREV.br_startoff;
872 temp2 = PREV.br_blockcount +
873 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 break;
875
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000876 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 /*
878 * Filling in all of a previously delayed allocation extent.
879 * Neither the left nor right neighbors are contiguous with
880 * the new one.
881 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000882 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000884 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 ip->i_df.if_lastex = idx;
887 ip->i_d.di_nextents++;
888 if (cur == NULL)
889 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
890 else {
891 rval = XFS_ILOG_CORE;
892 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
893 new->br_startblock, new->br_blockcount,
894 &i)))
895 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000896 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100898 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000900 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000903 /* DELTA: The in-core extent described by new changed type. */
904 temp = new->br_startoff;
905 temp2 = new->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 break;
907
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000908 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /*
910 * Filling in the first part of a previous delayed allocation.
911 * The left neighbor is contiguous.
912 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000913 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100914 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 LEFT.br_blockcount + new->br_blockcount);
916 xfs_bmbt_set_startoff(ep,
917 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000918 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000921 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 xfs_bmbt_set_blockcount(ep, temp);
923 ip->i_df.if_lastex = idx - 1;
924 if (cur == NULL)
925 rval = XFS_ILOG_DEXT;
926 else {
927 rval = 0;
928 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
929 LEFT.br_startblock, LEFT.br_blockcount,
930 &i)))
931 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000932 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
934 LEFT.br_startblock,
935 LEFT.br_blockcount +
936 new->br_blockcount,
937 LEFT.br_state)))
938 goto done;
939 }
940 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600941 startblockval(PREV.br_startblock));
942 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000943 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000945 /* DELTA: The boundary between two in-core extents moved. */
946 temp = LEFT.br_startoff;
947 temp2 = LEFT.br_blockcount +
948 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 break;
950
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000951 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /*
953 * Filling in the first part of a previous delayed allocation.
954 * The left neighbor is not contiguous.
955 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000956 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 xfs_bmbt_set_startoff(ep, new_endoff);
958 temp = PREV.br_blockcount - new->br_blockcount;
959 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig6ef35542009-11-25 00:00:21 +0000960 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 ip->i_df.if_lastex = idx;
962 ip->i_d.di_nextents++;
963 if (cur == NULL)
964 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
965 else {
966 rval = XFS_ILOG_CORE;
967 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
968 new->br_startblock, new->br_blockcount,
969 &i)))
970 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000971 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100973 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000975 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
978 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
979 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
980 first, flist, &cur, 1, &tmp_rval,
981 XFS_DATA_FORK);
982 rval |= tmp_rval;
983 if (error)
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) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100989 ep = xfs_iext_get_ext(ifp, idx + 1);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600990 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000991 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000993 /* DELTA: One in-core extent is split in two. */
994 temp = PREV.br_startoff;
995 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 break;
997
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000998 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /*
1000 * Filling in the last part of a previous delayed allocation.
1001 * The right neighbor is contiguous with the new allocation.
1002 */
1003 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001004 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1005 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 xfs_bmbt_set_blockcount(ep, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001007 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1008 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 new->br_blockcount + RIGHT.br_blockcount,
1010 RIGHT.br_state);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001011 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 ip->i_df.if_lastex = idx + 1;
1013 if (cur == NULL)
1014 rval = XFS_ILOG_DEXT;
1015 else {
1016 rval = 0;
1017 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1018 RIGHT.br_startblock,
1019 RIGHT.br_blockcount, &i)))
1020 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001021 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1023 new->br_startblock,
1024 new->br_blockcount +
1025 RIGHT.br_blockcount,
1026 RIGHT.br_state)))
1027 goto done;
1028 }
1029 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001030 startblockval(PREV.br_startblock));
1031 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;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001034 /* DELTA: The boundary between two in-core extents moved. */
1035 temp = PREV.br_startoff;
1036 temp2 = PREV.br_blockcount +
1037 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 break;
1039
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001040 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /*
1042 * Filling in the last part of a previous delayed allocation.
1043 * The right neighbor is not contiguous.
1044 */
1045 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001046 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001048 xfs_iext_insert(ip, idx + 1, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 ip->i_df.if_lastex = idx + 1;
1050 ip->i_d.di_nextents++;
1051 if (cur == NULL)
1052 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1053 else {
1054 rval = XFS_ILOG_CORE;
1055 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1056 new->br_startblock, new->br_blockcount,
1057 &i)))
1058 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001059 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001061 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001063 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1066 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1067 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1068 first, flist, &cur, 1, &tmp_rval,
1069 XFS_DATA_FORK);
1070 rval |= tmp_rval;
1071 if (error)
1072 goto done;
1073 }
1074 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001075 startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001077 ep = xfs_iext_get_ext(ifp, idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001078 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001079 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001081 /* DELTA: One in-core extent is split in two. */
1082 temp = PREV.br_startoff;
1083 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 break;
1085
1086 case 0:
1087 /*
1088 * Filling in the middle part of a previous delayed allocation.
1089 * Contiguity is impossible here.
1090 * This case is avoided almost all the time.
1091 */
1092 temp = new->br_startoff - PREV.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001093 trace_xfs_bmap_pre_update(ip, idx, 0, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 xfs_bmbt_set_blockcount(ep, temp);
1095 r[0] = *new;
Lachlan McIlroyd2133712006-11-21 18:55:16 +11001096 r[1].br_state = PREV.br_state;
1097 r[1].br_startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 r[1].br_startoff = new_endoff;
1099 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1100 r[1].br_blockcount = temp2;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001101 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 ip->i_df.if_lastex = idx + 1;
1103 ip->i_d.di_nextents++;
1104 if (cur == NULL)
1105 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1106 else {
1107 rval = XFS_ILOG_CORE;
1108 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1109 new->br_startblock, new->br_blockcount,
1110 &i)))
1111 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001112 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001114 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001116 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1119 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1120 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1121 first, flist, &cur, 1, &tmp_rval,
1122 XFS_DATA_FORK);
1123 rval |= tmp_rval;
1124 if (error)
1125 goto done;
1126 }
1127 temp = xfs_bmap_worst_indlen(ip, temp);
1128 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001129 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 (cur ? cur->bc_private.b.allocated : 0));
1131 if (diff > 0 &&
David Chinner20f4ebf2007-02-10 18:36:10 +11001132 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 /*
1134 * Ick gross gag me with a spoon.
1135 */
1136 ASSERT(0); /* want to see if this ever happens! */
1137 while (diff > 0) {
1138 if (temp) {
1139 temp--;
1140 diff--;
1141 if (!diff ||
1142 !xfs_mod_incore_sb(ip->i_mount,
David Chinner20f4ebf2007-02-10 18:36:10 +11001143 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 break;
1145 }
1146 if (temp2) {
1147 temp2--;
1148 diff--;
1149 if (!diff ||
1150 !xfs_mod_incore_sb(ip->i_mount,
David Chinner20f4ebf2007-02-10 18:36:10 +11001151 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 break;
1153 }
1154 }
1155 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001156 ep = xfs_iext_get_ext(ifp, idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001157 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001158 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1159 trace_xfs_bmap_pre_update(ip, idx + 2, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001160 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001161 nullstartblock((int)temp2));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001162 trace_xfs_bmap_post_update(ip, idx + 2, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 *dnew = temp + temp2;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001164 /* DELTA: One in-core extent is split in three. */
1165 temp = PREV.br_startoff;
1166 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 break;
1168
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001169 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1170 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1171 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1172 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1173 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1174 case BMAP_LEFT_CONTIG:
1175 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /*
1177 * These cases are all impossible.
1178 */
1179 ASSERT(0);
1180 }
1181 *curp = cur;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001182 if (delta) {
1183 temp2 += temp;
1184 if (delta->xed_startoff > temp)
1185 delta->xed_startoff = temp;
1186 if (delta->xed_blockcount < temp2)
1187 delta->xed_blockcount = temp2;
1188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189done:
1190 *logflagsp = rval;
1191 return error;
1192#undef LEFT
1193#undef RIGHT
1194#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
1197/*
1198 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1199 * allocation to a real allocation or vice versa.
1200 */
1201STATIC int /* error */
1202xfs_bmap_add_extent_unwritten_real(
1203 xfs_inode_t *ip, /* incore inode pointer */
1204 xfs_extnum_t idx, /* extent number to update/insert */
1205 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001206 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001207 int *logflagsp, /* inode logging flags */
1208 xfs_extdelta_t *delta) /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001211 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001214 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 xfs_fileoff_t new_endoff; /* end offset of new entry */
1216 xfs_exntst_t newext; /* new extent state */
1217 xfs_exntst_t oldext; /* old extent state */
1218 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1219 /* left is 0, right is 1, prev is 2 */
1220 int rval=0; /* return value (logging flags) */
1221 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001222 xfs_filblks_t temp=0;
1223 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225#define LEFT r[0]
1226#define RIGHT r[1]
1227#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 /*
1229 * Set up a bunch of variables to make the tests simpler.
1230 */
1231 error = 0;
1232 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001233 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1234 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 xfs_bmbt_get_all(ep, &PREV);
1236 newext = new->br_state;
1237 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1238 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1239 ASSERT(PREV.br_state == oldext);
1240 new_endoff = new->br_startoff + new->br_blockcount;
1241 ASSERT(PREV.br_startoff <= new->br_startoff);
1242 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /*
1245 * Set flags determining what part of the previous oldext allocation
1246 * extent is being replaced by a newext allocation.
1247 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001248 if (PREV.br_startoff == new->br_startoff)
1249 state |= BMAP_LEFT_FILLING;
1250 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1251 state |= BMAP_RIGHT_FILLING;
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /*
1254 * Check and set flags if this segment has a left neighbor.
1255 * Don't set contiguous if the combined extent would be too large.
1256 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001257 if (idx > 0) {
1258 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001259 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001260
1261 if (isnullstartblock(LEFT.br_startblock))
1262 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001264
1265 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1266 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1267 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1268 LEFT.br_state == newext &&
1269 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1270 state |= BMAP_LEFT_CONTIG;
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 /*
1273 * Check and set flags if this segment has a right neighbor.
1274 * Don't set contiguous if the combined extent would be too large.
1275 * Also check for all-three-contiguous being too large.
1276 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001277 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1278 state |= BMAP_RIGHT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001279 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001280 if (isnullstartblock(RIGHT.br_startblock))
1281 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001283
1284 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1285 new_endoff == RIGHT.br_startoff &&
1286 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1287 newext == RIGHT.br_state &&
1288 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1289 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1290 BMAP_RIGHT_FILLING)) !=
1291 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1292 BMAP_RIGHT_FILLING) ||
1293 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1294 <= MAXEXTLEN))
1295 state |= BMAP_RIGHT_CONTIG;
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 /*
1298 * Switch out based on the FILLING and CONTIG state bits.
1299 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001300 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1301 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1302 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1303 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /*
1305 * Setting all of a previous oldext extent to newext.
1306 * The left and right neighbors are both contiguous with new.
1307 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001308 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001309 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 LEFT.br_blockcount + PREV.br_blockcount +
1311 RIGHT.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001312 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1313
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001314 xfs_iext_remove(ip, idx, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 ip->i_df.if_lastex = idx - 1;
1316 ip->i_d.di_nextents -= 2;
1317 if (cur == NULL)
1318 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1319 else {
1320 rval = XFS_ILOG_CORE;
1321 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1322 RIGHT.br_startblock,
1323 RIGHT.br_blockcount, &i)))
1324 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001325 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001326 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001328 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001329 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001331 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001332 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001334 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001335 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001337 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1339 LEFT.br_startblock,
1340 LEFT.br_blockcount + PREV.br_blockcount +
1341 RIGHT.br_blockcount, LEFT.br_state)))
1342 goto done;
1343 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001344 /* DELTA: Three in-core extents are replaced by one. */
1345 temp = LEFT.br_startoff;
1346 temp2 = LEFT.br_blockcount +
1347 PREV.br_blockcount +
1348 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 break;
1350
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001351 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 /*
1353 * Setting all of a previous oldext extent to newext.
1354 * The left neighbor is contiguous, the right is not.
1355 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001356 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001357 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001359 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 ip->i_df.if_lastex = idx - 1;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001362 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 ip->i_d.di_nextents--;
1364 if (cur == NULL)
1365 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1366 else {
1367 rval = XFS_ILOG_CORE;
1368 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1369 PREV.br_startblock, PREV.br_blockcount,
1370 &i)))
1371 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001372 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001373 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001375 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001376 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001378 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1380 LEFT.br_startblock,
1381 LEFT.br_blockcount + PREV.br_blockcount,
1382 LEFT.br_state)))
1383 goto done;
1384 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001385 /* DELTA: Two in-core extents are replaced by one. */
1386 temp = LEFT.br_startoff;
1387 temp2 = LEFT.br_blockcount +
1388 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 break;
1390
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001391 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /*
1393 * Setting all of a previous oldext extent to newext.
1394 * The right neighbor is contiguous, the left is not.
1395 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001396 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 xfs_bmbt_set_blockcount(ep,
1398 PREV.br_blockcount + RIGHT.br_blockcount);
1399 xfs_bmbt_set_state(ep, newext);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001400 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 ip->i_df.if_lastex = idx;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001402 xfs_iext_remove(ip, idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 ip->i_d.di_nextents--;
1404 if (cur == NULL)
1405 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1406 else {
1407 rval = XFS_ILOG_CORE;
1408 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1409 RIGHT.br_startblock,
1410 RIGHT.br_blockcount, &i)))
1411 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001412 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001413 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001415 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001416 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001418 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1420 new->br_startblock,
1421 new->br_blockcount + RIGHT.br_blockcount,
1422 newext)))
1423 goto done;
1424 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001425 /* DELTA: Two in-core extents are replaced by one. */
1426 temp = PREV.br_startoff;
1427 temp2 = PREV.br_blockcount +
1428 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 break;
1430
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001431 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /*
1433 * Setting all of a previous oldext extent to newext.
1434 * Neither the left nor right neighbors are contiguous with
1435 * the new one.
1436 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001437 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 xfs_bmbt_set_state(ep, newext);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001439 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 ip->i_df.if_lastex = idx;
1442 if (cur == NULL)
1443 rval = XFS_ILOG_DEXT;
1444 else {
1445 rval = 0;
1446 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1447 new->br_startblock, new->br_blockcount,
1448 &i)))
1449 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001450 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1452 new->br_startblock, new->br_blockcount,
1453 newext)))
1454 goto done;
1455 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001456 /* DELTA: The in-core extent described by new changed type. */
1457 temp = new->br_startoff;
1458 temp2 = new->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 break;
1460
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001461 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /*
1463 * Setting the first part of a previous oldext extent to newext.
1464 * The left neighbor is contiguous.
1465 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001466 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001467 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 LEFT.br_blockcount + new->br_blockcount);
1469 xfs_bmbt_set_startoff(ep,
1470 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001471 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1472
1473 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 xfs_bmbt_set_startblock(ep,
1475 new->br_startblock + new->br_blockcount);
1476 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_);
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 ip->i_df.if_lastex = idx - 1;
1481 if (cur == NULL)
1482 rval = XFS_ILOG_DEXT;
1483 else {
1484 rval = 0;
1485 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1486 PREV.br_startblock, PREV.br_blockcount,
1487 &i)))
1488 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001489 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 if ((error = xfs_bmbt_update(cur,
1491 PREV.br_startoff + new->br_blockcount,
1492 PREV.br_startblock + new->br_blockcount,
1493 PREV.br_blockcount - new->br_blockcount,
1494 oldext)))
1495 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001496 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 goto done;
1498 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1499 LEFT.br_startblock,
1500 LEFT.br_blockcount + new->br_blockcount,
1501 LEFT.br_state))
1502 goto done;
1503 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001504 /* DELTA: The boundary between two in-core extents moved. */
1505 temp = LEFT.br_startoff;
1506 temp2 = LEFT.br_blockcount +
1507 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 break;
1509
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001510 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 /*
1512 * Setting the first part of a previous oldext extent to newext.
1513 * The left neighbor is not contiguous.
1514 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001515 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1517 xfs_bmbt_set_startoff(ep, new_endoff);
1518 xfs_bmbt_set_blockcount(ep,
1519 PREV.br_blockcount - new->br_blockcount);
1520 xfs_bmbt_set_startblock(ep,
1521 new->br_startblock + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001522 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1523
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001524 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 ip->i_df.if_lastex = idx;
1526 ip->i_d.di_nextents++;
1527 if (cur == NULL)
1528 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1529 else {
1530 rval = XFS_ILOG_CORE;
1531 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1532 PREV.br_startblock, PREV.br_blockcount,
1533 &i)))
1534 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001535 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if ((error = xfs_bmbt_update(cur,
1537 PREV.br_startoff + new->br_blockcount,
1538 PREV.br_startblock + new->br_blockcount,
1539 PREV.br_blockcount - new->br_blockcount,
1540 oldext)))
1541 goto done;
1542 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001543 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001545 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001547 /* DELTA: One in-core extent is split in two. */
1548 temp = PREV.br_startoff;
1549 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 break;
1551
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001552 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 /*
1554 * Setting the last part of a previous oldext extent to newext.
1555 * The right neighbor is contiguous with the new allocation.
1556 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001557 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
1558 trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 xfs_bmbt_set_blockcount(ep,
1560 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001561 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001562 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1563 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001565 trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_);
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 ip->i_df.if_lastex = idx + 1;
1568 if (cur == NULL)
1569 rval = XFS_ILOG_DEXT;
1570 else {
1571 rval = 0;
1572 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1573 PREV.br_startblock,
1574 PREV.br_blockcount, &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 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1578 PREV.br_startblock,
1579 PREV.br_blockcount - new->br_blockcount,
1580 oldext)))
1581 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001582 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 goto done;
1584 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1585 new->br_startblock,
1586 new->br_blockcount + RIGHT.br_blockcount,
1587 newext)))
1588 goto done;
1589 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001590 /* DELTA: The boundary between two in-core extents moved. */
1591 temp = PREV.br_startoff;
1592 temp2 = PREV.br_blockcount +
1593 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 break;
1595
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001596 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 /*
1598 * Setting the last part of a previous oldext extent to newext.
1599 * The right neighbor is not contiguous.
1600 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001601 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 xfs_bmbt_set_blockcount(ep,
1603 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001604 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1605
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001606 xfs_iext_insert(ip, idx + 1, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 ip->i_df.if_lastex = idx + 1;
1608 ip->i_d.di_nextents++;
1609 if (cur == NULL)
1610 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1611 else {
1612 rval = XFS_ILOG_CORE;
1613 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1614 PREV.br_startblock, PREV.br_blockcount,
1615 &i)))
1616 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001617 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1619 PREV.br_startblock,
1620 PREV.br_blockcount - new->br_blockcount,
1621 oldext)))
1622 goto done;
1623 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1624 new->br_startblock, new->br_blockcount,
1625 &i)))
1626 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001627 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001629 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001631 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001633 /* DELTA: One in-core extent is split in two. */
1634 temp = PREV.br_startoff;
1635 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 break;
1637
1638 case 0:
1639 /*
1640 * Setting the middle part of a previous oldext extent to
1641 * newext. Contiguity is impossible here.
1642 * One extent becomes three extents.
1643 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001644 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 xfs_bmbt_set_blockcount(ep,
1646 new->br_startoff - PREV.br_startoff);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001647 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 r[0] = *new;
1650 r[1].br_startoff = new_endoff;
1651 r[1].br_blockcount =
1652 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1653 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1654 r[1].br_state = oldext;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001655 xfs_iext_insert(ip, idx + 1, 2, &r[0], state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 ip->i_df.if_lastex = idx + 1;
1657 ip->i_d.di_nextents += 2;
1658 if (cur == NULL)
1659 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1660 else {
1661 rval = XFS_ILOG_CORE;
1662 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1663 PREV.br_startblock, PREV.br_blockcount,
1664 &i)))
1665 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001666 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 /* new right extent - oldext */
1668 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1669 r[1].br_startblock, r[1].br_blockcount,
1670 r[1].br_state)))
1671 goto done;
1672 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10001674 cur->bc_rec.b.br_blockcount =
1675 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001676 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001678 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001679 /*
1680 * Reset the cursor to the position of the new extent
1681 * we are about to insert as we can't trust it after
1682 * the previous insert.
1683 */
1684 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1685 new->br_startblock, new->br_blockcount,
1686 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001688 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001690 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001691 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001693 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001695 /* DELTA: One in-core extent is split in three. */
1696 temp = PREV.br_startoff;
1697 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 break;
1699
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001700 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1701 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1702 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1703 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1704 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1705 case BMAP_LEFT_CONTIG:
1706 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 /*
1708 * These cases are all impossible.
1709 */
1710 ASSERT(0);
1711 }
1712 *curp = cur;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001713 if (delta) {
1714 temp2 += temp;
1715 if (delta->xed_startoff > temp)
1716 delta->xed_startoff = temp;
1717 if (delta->xed_blockcount < temp2)
1718 delta->xed_blockcount = temp2;
1719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720done:
1721 *logflagsp = rval;
1722 return error;
1723#undef LEFT
1724#undef RIGHT
1725#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
1728/*
1729 * Called by xfs_bmap_add_extent to handle cases converting a hole
1730 * to a delayed allocation.
1731 */
1732/*ARGSUSED*/
1733STATIC int /* error */
1734xfs_bmap_add_extent_hole_delay(
1735 xfs_inode_t *ip, /* incore inode pointer */
1736 xfs_extnum_t idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001737 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001739 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 int rsvd) /* OK to allocate reserved blocks */
1741{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001742 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001743 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1745 xfs_filblks_t newlen=0; /* new indirect size */
1746 xfs_filblks_t oldlen=0; /* old indirect size */
1747 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1748 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001749 xfs_filblks_t temp=0; /* temp for indirect calculations */
1750 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001752 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1753 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001755 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 /*
1758 * Check and set flags if this segment has a left neighbor
1759 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001760 if (idx > 0) {
1761 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001762 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001763
1764 if (isnullstartblock(left.br_startblock))
1765 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 /*
1769 * Check and set flags if the current (right) segment exists.
1770 * If it doesn't exist, we're converting the hole at end-of-file.
1771 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001772 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1773 state |= BMAP_RIGHT_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 xfs_bmbt_get_all(ep, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001775
1776 if (isnullstartblock(right.br_startblock))
1777 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 /*
1781 * Set contiguity flags on the left and right neighbors.
1782 * Don't let extents get too large, even if the pieces are contiguous.
1783 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001784 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1785 left.br_startoff + left.br_blockcount == new->br_startoff &&
1786 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1787 state |= BMAP_LEFT_CONTIG;
1788
1789 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1790 new->br_startoff + new->br_blockcount == right.br_startoff &&
1791 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1792 (!(state & BMAP_LEFT_CONTIG) ||
1793 (left.br_blockcount + new->br_blockcount +
1794 right.br_blockcount <= MAXEXTLEN)))
1795 state |= BMAP_RIGHT_CONTIG;
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 /*
1798 * Switch out based on the contiguity flags.
1799 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001800 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1801 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 /*
1803 * New allocation is contiguous with delayed allocations
1804 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001805 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 */
1807 temp = left.br_blockcount + new->br_blockcount +
1808 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001809
1810 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001811 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001812 oldlen = startblockval(left.br_startblock) +
1813 startblockval(new->br_startblock) +
1814 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001816 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001817 nullstartblock((int)newlen));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001818 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1819
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001820 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 ip->i_df.if_lastex = idx - 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001822 /* DELTA: Two in-core extents were replaced by one. */
1823 temp2 = temp;
1824 temp = left.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 break;
1826
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001827 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /*
1829 * New allocation is contiguous with a delayed allocation
1830 * on the left.
1831 * Merge the new allocation with the left neighbor.
1832 */
1833 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001834 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001835 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001836 oldlen = startblockval(left.br_startblock) +
1837 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001839 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001840 nullstartblock((int)newlen));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001841 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 ip->i_df.if_lastex = idx - 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001844 /* DELTA: One in-core extent grew into a hole. */
1845 temp2 = temp;
1846 temp = left.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 break;
1848
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001849 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 /*
1851 * New allocation is contiguous with a delayed allocation
1852 * on the right.
1853 * Merge the new allocation with the right neighbor.
1854 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001855 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001857 oldlen = startblockval(new->br_startblock) +
1858 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 newlen = xfs_bmap_worst_indlen(ip, temp);
1860 xfs_bmbt_set_allf(ep, new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06001861 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001862 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 ip->i_df.if_lastex = idx;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001865 /* DELTA: One in-core extent grew into a hole. */
1866 temp2 = temp;
1867 temp = new->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 break;
1869
1870 case 0:
1871 /*
1872 * New allocation is not contiguous with another
1873 * delayed allocation.
1874 * Insert a new entry.
1875 */
1876 oldlen = newlen = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001877 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 ip->i_df.if_lastex = idx;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001879 /* DELTA: A new in-core extent was added in a hole. */
1880 temp2 = new->br_blockcount;
1881 temp = new->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 break;
1883 }
1884 if (oldlen != newlen) {
1885 ASSERT(oldlen > newlen);
1886 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11001887 (int64_t)(oldlen - newlen), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /*
1889 * Nothing to do for disk quota accounting here.
1890 */
1891 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001892 if (delta) {
1893 temp2 += temp;
1894 if (delta->xed_startoff > temp)
1895 delta->xed_startoff = temp;
1896 if (delta->xed_blockcount < temp2)
1897 delta->xed_blockcount = temp2;
1898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 *logflagsp = 0;
1900 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901}
1902
1903/*
1904 * Called by xfs_bmap_add_extent to handle cases converting a hole
1905 * to a real allocation.
1906 */
1907STATIC int /* error */
1908xfs_bmap_add_extent_hole_real(
1909 xfs_inode_t *ip, /* incore inode pointer */
1910 xfs_extnum_t idx, /* extent number to update/insert */
1911 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001912 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001914 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 int whichfork) /* data or attr fork */
1916{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001917 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 int i; /* temp state */
1920 xfs_ifork_t *ifp; /* inode fork pointer */
1921 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1922 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001923 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001925 xfs_filblks_t temp=0;
1926 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 ifp = XFS_IFORK_PTR(ip, whichfork);
1929 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001930 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 state = 0;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001932
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001933 if (whichfork == XFS_ATTR_FORK)
1934 state |= BMAP_ATTRFORK;
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 /*
1937 * Check and set flags if this segment has a left neighbor.
1938 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001939 if (idx > 0) {
1940 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001941 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001942 if (isnullstartblock(left.br_startblock))
1943 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 /*
1947 * Check and set flags if this segment has a current value.
1948 * Not true if we're inserting into the "hole" at eof.
1949 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001950 if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1951 state |= BMAP_RIGHT_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 xfs_bmbt_get_all(ep, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001953 if (isnullstartblock(right.br_startblock))
1954 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 /*
1958 * We're inserting a real allocation between "left" and "right".
1959 * Set the contiguity flags. Don't let extents get too large.
1960 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001961 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1962 left.br_startoff + left.br_blockcount == new->br_startoff &&
1963 left.br_startblock + left.br_blockcount == new->br_startblock &&
1964 left.br_state == new->br_state &&
1965 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1966 state |= BMAP_LEFT_CONTIG;
1967
1968 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1969 new->br_startoff + new->br_blockcount == right.br_startoff &&
1970 new->br_startblock + new->br_blockcount == right.br_startblock &&
1971 new->br_state == right.br_state &&
1972 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1973 (!(state & BMAP_LEFT_CONTIG) ||
1974 left.br_blockcount + new->br_blockcount +
1975 right.br_blockcount <= MAXEXTLEN))
1976 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001978 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 /*
1980 * Select which case we're in here, and implement it.
1981 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001982 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1983 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 /*
1985 * New allocation is contiguous with real allocations on the
1986 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001987 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001989 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001990 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 left.br_blockcount + new->br_blockcount +
1992 right.br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001993 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
1994
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001995 xfs_iext_remove(ip, idx, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 ifp->if_lastex = idx - 1;
1997 XFS_IFORK_NEXT_SET(ip, whichfork,
1998 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1999 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002000 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002001 } else {
2002 rval = XFS_ILOG_CORE;
2003 if ((error = xfs_bmbt_lookup_eq(cur,
2004 right.br_startoff,
2005 right.br_startblock,
2006 right.br_blockcount, &i)))
2007 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002008 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002009 if ((error = xfs_btree_delete(cur, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002010 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002011 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002012 if ((error = xfs_btree_decrement(cur, 0, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002013 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002014 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002015 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2016 left.br_startblock,
2017 left.br_blockcount +
2018 new->br_blockcount +
2019 right.br_blockcount,
2020 left.br_state)))
2021 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002023 /* DELTA: Two in-core extents were replaced by one. */
2024 temp = left.br_startoff;
2025 temp2 = left.br_blockcount +
2026 new->br_blockcount +
2027 right.br_blockcount;
2028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002030 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 /*
2032 * New allocation is contiguous with a real allocation
2033 * on the left.
2034 * Merge the new allocation with the left neighbor.
2035 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002036 trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002037 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 left.br_blockcount + new->br_blockcount);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002039 trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_);
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 ifp->if_lastex = idx - 1;
2042 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002043 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002044 } else {
2045 rval = 0;
2046 if ((error = xfs_bmbt_lookup_eq(cur,
2047 left.br_startoff,
2048 left.br_startblock,
2049 left.br_blockcount, &i)))
2050 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002051 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002052 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2053 left.br_startblock,
2054 left.br_blockcount +
2055 new->br_blockcount,
2056 left.br_state)))
2057 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002059 /* DELTA: One in-core extent grew. */
2060 temp = left.br_startoff;
2061 temp2 = left.br_blockcount +
2062 new->br_blockcount;
2063 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002065 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 /*
2067 * New allocation is contiguous with a real allocation
2068 * on the right.
2069 * Merge the new allocation with the right neighbor.
2070 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002071 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2073 new->br_blockcount + right.br_blockcount,
2074 right.br_state);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002075 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 ifp->if_lastex = idx;
2078 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002079 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002080 } else {
2081 rval = 0;
2082 if ((error = xfs_bmbt_lookup_eq(cur,
2083 right.br_startoff,
2084 right.br_startblock,
2085 right.br_blockcount, &i)))
2086 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002087 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002088 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2089 new->br_startblock,
2090 new->br_blockcount +
2091 right.br_blockcount,
2092 right.br_state)))
2093 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002095 /* DELTA: One in-core extent grew. */
2096 temp = new->br_startoff;
2097 temp2 = new->br_blockcount +
2098 right.br_blockcount;
2099 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 case 0:
2102 /*
2103 * New allocation is not contiguous with another
2104 * real allocation.
2105 * Insert a new entry.
2106 */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002107 xfs_iext_insert(ip, idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 ifp->if_lastex = idx;
2109 XFS_IFORK_NEXT_SET(ip, whichfork,
2110 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2111 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002112 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002113 } else {
2114 rval = XFS_ILOG_CORE;
2115 if ((error = xfs_bmbt_lookup_eq(cur,
2116 new->br_startoff,
2117 new->br_startblock,
2118 new->br_blockcount, &i)))
2119 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002120 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002121 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002122 if ((error = xfs_btree_insert(cur, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002123 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002124 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002126 /* DELTA: A new extent was added in a hole. */
2127 temp = new->br_startoff;
2128 temp2 = new->br_blockcount;
2129 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002131 if (delta) {
2132 temp2 += temp;
2133 if (delta->xed_startoff > temp)
2134 delta->xed_startoff = temp;
2135 if (delta->xed_blockcount < temp2)
2136 delta->xed_blockcount = temp2;
2137 }
2138done:
2139 *logflagsp = rval;
2140 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
Nathan Scottdd9f4382006-01-11 15:28:28 +11002143/*
2144 * Adjust the size of the new extent based on di_extsize and rt extsize.
2145 */
2146STATIC int
2147xfs_bmap_extsize_align(
2148 xfs_mount_t *mp,
2149 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2150 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2151 xfs_extlen_t extsz, /* align to this extent size */
2152 int rt, /* is this a realtime inode? */
2153 int eof, /* is extent at end-of-file? */
2154 int delay, /* creating delalloc extent? */
2155 int convert, /* overwriting unwritten extent? */
2156 xfs_fileoff_t *offp, /* in/out: aligned offset */
2157 xfs_extlen_t *lenp) /* in/out: aligned length */
2158{
2159 xfs_fileoff_t orig_off; /* original offset */
2160 xfs_extlen_t orig_alen; /* original length */
2161 xfs_fileoff_t orig_end; /* original off+len */
2162 xfs_fileoff_t nexto; /* next file offset */
2163 xfs_fileoff_t prevo; /* previous file offset */
2164 xfs_fileoff_t align_off; /* temp for offset */
2165 xfs_extlen_t align_alen; /* temp for length */
2166 xfs_extlen_t temp; /* temp for calculations */
2167
2168 if (convert)
2169 return 0;
2170
2171 orig_off = align_off = *offp;
2172 orig_alen = align_alen = *lenp;
2173 orig_end = orig_off + orig_alen;
2174
2175 /*
2176 * If this request overlaps an existing extent, then don't
2177 * attempt to perform any additional alignment.
2178 */
2179 if (!delay && !eof &&
2180 (orig_off >= gotp->br_startoff) &&
2181 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2182 return 0;
2183 }
2184
2185 /*
2186 * If the file offset is unaligned vs. the extent size
2187 * we need to align it. This will be possible unless
2188 * the file was previously written with a kernel that didn't
2189 * perform this alignment, or if a truncate shot us in the
2190 * foot.
2191 */
2192 temp = do_mod(orig_off, extsz);
2193 if (temp) {
2194 align_alen += temp;
2195 align_off -= temp;
2196 }
2197 /*
2198 * Same adjustment for the end of the requested area.
2199 */
2200 if ((temp = (align_alen % extsz))) {
2201 align_alen += extsz - temp;
2202 }
2203 /*
2204 * If the previous block overlaps with this proposed allocation
2205 * then move the start forward without adjusting the length.
2206 */
2207 if (prevp->br_startoff != NULLFILEOFF) {
2208 if (prevp->br_startblock == HOLESTARTBLOCK)
2209 prevo = prevp->br_startoff;
2210 else
2211 prevo = prevp->br_startoff + prevp->br_blockcount;
2212 } else
2213 prevo = 0;
2214 if (align_off != orig_off && align_off < prevo)
2215 align_off = prevo;
2216 /*
2217 * If the next block overlaps with this proposed allocation
2218 * then move the start back without adjusting the length,
2219 * but not before offset 0.
2220 * This may of course make the start overlap previous block,
2221 * and if we hit the offset 0 limit then the next block
2222 * can still overlap too.
2223 */
2224 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2225 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2226 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2227 nexto = gotp->br_startoff + gotp->br_blockcount;
2228 else
2229 nexto = gotp->br_startoff;
2230 } else
2231 nexto = NULLFILEOFF;
2232 if (!eof &&
2233 align_off + align_alen != orig_end &&
2234 align_off + align_alen > nexto)
2235 align_off = nexto > align_alen ? nexto - align_alen : 0;
2236 /*
2237 * If we're now overlapping the next or previous extent that
2238 * means we can't fit an extsz piece in this hole. Just move
2239 * the start forward to the first valid spot and set
2240 * the length so we hit the end.
2241 */
2242 if (align_off != orig_off && align_off < prevo)
2243 align_off = prevo;
2244 if (align_off + align_alen != orig_end &&
2245 align_off + align_alen > nexto &&
2246 nexto != NULLFILEOFF) {
2247 ASSERT(nexto > prevo);
2248 align_alen = nexto - align_off;
2249 }
2250
2251 /*
2252 * If realtime, and the result isn't a multiple of the realtime
2253 * extent size we need to remove blocks until it is.
2254 */
2255 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2256 /*
2257 * We're not covering the original request, or
2258 * we won't be able to once we fix the length.
2259 */
2260 if (orig_off < align_off ||
2261 orig_end > align_off + align_alen ||
2262 align_alen - temp < orig_alen)
2263 return XFS_ERROR(EINVAL);
2264 /*
2265 * Try to fix it by moving the start up.
2266 */
2267 if (align_off + temp <= orig_off) {
2268 align_alen -= temp;
2269 align_off += temp;
2270 }
2271 /*
2272 * Try to fix it by moving the end in.
2273 */
2274 else if (align_off + align_alen - temp >= orig_end)
2275 align_alen -= temp;
2276 /*
2277 * Set the start to the minimum then trim the length.
2278 */
2279 else {
2280 align_alen -= orig_off - align_off;
2281 align_off = orig_off;
2282 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2283 }
2284 /*
2285 * Result doesn't cover the request, fail it.
2286 */
2287 if (orig_off < align_off || orig_end > align_off + align_alen)
2288 return XFS_ERROR(EINVAL);
2289 } else {
2290 ASSERT(orig_off >= align_off);
2291 ASSERT(orig_end <= align_off + align_alen);
2292 }
2293
2294#ifdef DEBUG
2295 if (!eof && gotp->br_startoff != NULLFILEOFF)
2296 ASSERT(align_off + align_alen <= gotp->br_startoff);
2297 if (prevp->br_startoff != NULLFILEOFF)
2298 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2299#endif
2300
2301 *lenp = align_alen;
2302 *offp = align_off;
2303 return 0;
2304}
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306#define XFS_ALLOC_GAP_UNITS 4
2307
David Chinnerc2b1cba2008-04-10 12:21:40 +10002308STATIC void
Nathan Scotta365bdd2006-03-14 13:34:16 +11002309xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2311{
2312 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2314 xfs_mount_t *mp; /* mount point structure */
2315 int nullfb; /* true if ap->firstblock isn't set */
2316 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318#define ISVALID(x,y) \
2319 (rt ? \
2320 (x) < mp->m_sb.sb_rblocks : \
2321 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2322 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2323 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 mp = ap->ip->i_mount;
2326 nullfb = ap->firstblock == NULLFSBLOCK;
2327 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2328 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 /*
2330 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002331 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 */
2333 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002334 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2336 ap->prevp->br_startblock)) {
2337 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2338 /*
2339 * Adjust for the gap between prevp and us.
2340 */
2341 adjust = ap->off -
2342 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2343 if (adjust &&
2344 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2345 ap->rval += adjust;
2346 }
2347 /*
2348 * If not at eof, then compare the two neighbor blocks.
2349 * Figure out whether either one gives us a good starting point,
2350 * and pick the better one.
2351 */
2352 else if (!ap->eof) {
2353 xfs_fsblock_t gotbno; /* right side block number */
2354 xfs_fsblock_t gotdiff=0; /* right side difference */
2355 xfs_fsblock_t prevbno; /* left side block number */
2356 xfs_fsblock_t prevdiff=0; /* left side difference */
2357
2358 /*
2359 * If there's a previous (left) block, select a requested
2360 * start block based on it.
2361 */
2362 if (ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002363 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 (prevbno = ap->prevp->br_startblock +
2365 ap->prevp->br_blockcount) &&
2366 ISVALID(prevbno, ap->prevp->br_startblock)) {
2367 /*
2368 * Calculate gap to end of previous block.
2369 */
2370 adjust = prevdiff = ap->off -
2371 (ap->prevp->br_startoff +
2372 ap->prevp->br_blockcount);
2373 /*
2374 * Figure the startblock based on the previous block's
2375 * end and the gap size.
2376 * Heuristic!
2377 * If the gap is large relative to the piece we're
2378 * allocating, or using it gives us an invalid block
2379 * number, then just use the end of the previous block.
2380 */
2381 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2382 ISVALID(prevbno + prevdiff,
2383 ap->prevp->br_startblock))
2384 prevbno += adjust;
2385 else
2386 prevdiff += adjust;
2387 /*
2388 * If the firstblock forbids it, can't use it,
2389 * must use default.
2390 */
2391 if (!rt && !nullfb &&
2392 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2393 prevbno = NULLFSBLOCK;
2394 }
2395 /*
2396 * No previous block or can't follow it, just default.
2397 */
2398 else
2399 prevbno = NULLFSBLOCK;
2400 /*
2401 * If there's a following (right) block, select a requested
2402 * start block based on it.
2403 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002404 if (!isnullstartblock(ap->gotp->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 /*
2406 * Calculate gap to start of next block.
2407 */
2408 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2409 /*
2410 * Figure the startblock based on the next block's
2411 * start and the gap size.
2412 */
2413 gotbno = ap->gotp->br_startblock;
2414 /*
2415 * Heuristic!
2416 * If the gap is large relative to the piece we're
2417 * allocating, or using it gives us an invalid block
2418 * number, then just use the start of the next block
2419 * offset by our length.
2420 */
2421 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2422 ISVALID(gotbno - gotdiff, gotbno))
2423 gotbno -= adjust;
2424 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2425 gotbno -= ap->alen;
2426 gotdiff += adjust - ap->alen;
2427 } else
2428 gotdiff += adjust;
2429 /*
2430 * If the firstblock forbids it, can't use it,
2431 * must use default.
2432 */
2433 if (!rt && !nullfb &&
2434 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2435 gotbno = NULLFSBLOCK;
2436 }
2437 /*
2438 * No next block, just default.
2439 */
2440 else
2441 gotbno = NULLFSBLOCK;
2442 /*
2443 * If both valid, pick the better one, else the only good
2444 * one, else ap->rval is already set (to 0 or the inode block).
2445 */
2446 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2447 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2448 else if (prevbno != NULLFSBLOCK)
2449 ap->rval = prevbno;
2450 else if (gotbno != NULLFSBLOCK)
2451 ap->rval = gotbno;
2452 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002453#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11002454}
2455
2456STATIC int
2457xfs_bmap_rtalloc(
2458 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2459{
2460 xfs_alloctype_t atype = 0; /* type for allocation routines */
2461 int error; /* error return value */
2462 xfs_mount_t *mp; /* mount point structure */
2463 xfs_extlen_t prod = 0; /* product factor for allocators */
2464 xfs_extlen_t ralen = 0; /* realtime allocation length */
2465 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002466 xfs_rtblock_t rtb;
2467
2468 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002469 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002470 prod = align / mp->m_sb.sb_rextsize;
2471 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2472 align, 1, ap->eof, 0,
2473 ap->conv, &ap->off, &ap->alen);
2474 if (error)
2475 return error;
2476 ASSERT(ap->alen);
2477 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2478
2479 /*
2480 * If the offset & length are not perfectly aligned
2481 * then kill prod, it will just get us in trouble.
2482 */
2483 if (do_mod(ap->off, align) || ap->alen % align)
2484 prod = 1;
2485 /*
2486 * Set ralen to be the actual requested length in rtextents.
2487 */
2488 ralen = ap->alen / mp->m_sb.sb_rextsize;
2489 /*
2490 * If the old value was close enough to MAXEXTLEN that
2491 * we rounded up to it, cut it back so it's valid again.
2492 * Note that if it's a really large request (bigger than
2493 * MAXEXTLEN), we don't hear about that number, and can't
2494 * adjust the starting point to match it.
2495 */
2496 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2497 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2498 /*
2499 * If it's an allocation to an empty file at offset 0,
2500 * pick an extent that will space things out in the rt area.
2501 */
2502 if (ap->eof && ap->off == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10002503 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2504
Nathan Scotta365bdd2006-03-14 13:34:16 +11002505 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2506 if (error)
2507 return error;
2508 ap->rval = rtx * mp->m_sb.sb_rextsize;
2509 } else {
2510 ap->rval = 0;
2511 }
2512
2513 xfs_bmap_adjacent(ap);
2514
2515 /*
2516 * Realtime allocation, done through xfs_rtallocate_extent.
2517 */
2518 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2519 do_div(ap->rval, mp->m_sb.sb_rextsize);
2520 rtb = ap->rval;
2521 ap->alen = ralen;
2522 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2523 &ralen, atype, ap->wasdel, prod, &rtb)))
2524 return error;
2525 if (rtb == NULLFSBLOCK && prod > 1 &&
2526 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2527 ap->alen, &ralen, atype,
2528 ap->wasdel, 1, &rtb)))
2529 return error;
2530 ap->rval = rtb;
2531 if (ap->rval != NULLFSBLOCK) {
2532 ap->rval *= mp->m_sb.sb_rextsize;
2533 ralen *= mp->m_sb.sb_rextsize;
2534 ap->alen = ralen;
2535 ap->ip->i_d.di_nblocks += ralen;
2536 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2537 if (ap->wasdel)
2538 ap->ip->i_delayed_blks -= ralen;
2539 /*
2540 * Adjust the disk quota also. This was reserved
2541 * earlier.
2542 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002543 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002544 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2545 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2546 } else {
2547 ap->alen = 0;
2548 }
2549 return 0;
2550}
2551
2552STATIC int
2553xfs_bmap_btalloc(
2554 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2555{
2556 xfs_mount_t *mp; /* mount point structure */
2557 xfs_alloctype_t atype = 0; /* type for allocation routines */
2558 xfs_extlen_t align; /* minimum allocation alignment */
2559 xfs_agnumber_t ag;
2560 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2561 xfs_agnumber_t startag;
2562 xfs_alloc_arg_t args;
2563 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002564 xfs_extlen_t nextminlen = 0;
2565 xfs_perag_t *pag;
2566 int nullfb; /* true if ap->firstblock isn't set */
2567 int isaligned;
2568 int notinit;
2569 int tryagain;
2570 int error;
2571
2572 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002573 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002574 if (unlikely(align)) {
2575 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2576 align, 0, ap->eof, 0, ap->conv,
2577 &ap->off, &ap->alen);
2578 ASSERT(!error);
2579 ASSERT(ap->alen);
2580 }
2581 nullfb = ap->firstblock == NULLFSBLOCK;
2582 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10002583 if (nullfb) {
2584 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2585 ag = xfs_filestream_lookup_ag(ap->ip);
2586 ag = (ag != NULLAGNUMBER) ? ag : 0;
2587 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2588 } else {
2589 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2590 }
2591 } else
Nathan Scotta365bdd2006-03-14 13:34:16 +11002592 ap->rval = ap->firstblock;
2593
2594 xfs_bmap_adjacent(ap);
2595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 /*
2597 * If allowed, use ap->rval; otherwise must use firstblock since
2598 * it's in the right allocation group.
2599 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002600 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 ;
2602 else
2603 ap->rval = ap->firstblock;
2604 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 * Normal allocation, done through xfs_alloc_vextent.
2606 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002607 tryagain = isaligned = 0;
2608 args.tp = ap->tp;
2609 args.mp = mp;
2610 args.fsbno = ap->rval;
2611 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
Yingping Lud210a282006-06-09 14:55:18 +10002612 args.firstblock = ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002613 blen = 0;
2614 if (nullfb) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002615 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2616 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2617 else
2618 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002619 args.total = ap->total;
David Chinner2a82b8b2007-07-11 11:09:12 +10002620
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10002622 * Search for an allocation group with a single extent
2623 * large enough for the request.
2624 *
2625 * If one isn't found, then adjust the minimum allocation
2626 * size to the largest space found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002628 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
David Chinner2a82b8b2007-07-11 11:09:12 +10002629 if (startag == NULLAGNUMBER)
2630 startag = ag = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002631 notinit = 0;
Dave Chinner44b56e02010-01-11 11:47:43 +00002632 pag = xfs_perag_get(mp, ag);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002633 while (blen < ap->alen) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11002634 if (!pag->pagf_init &&
2635 (error = xfs_alloc_pagf_init(mp, args.tp,
2636 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
Dave Chinner44b56e02010-01-11 11:47:43 +00002637 xfs_perag_put(pag);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002638 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002640 /*
2641 * See xfs_alloc_fix_freelist...
2642 */
2643 if (pag->pagf_init) {
Dave Chinner6cc87642009-03-16 08:29:46 +01002644 xfs_extlen_t longest;
2645 longest = xfs_alloc_longest_free_extent(mp, pag);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002646 if (blen < longest)
2647 blen = longest;
2648 } else
2649 notinit = 1;
David Chinner2a82b8b2007-07-11 11:09:12 +10002650
2651 if (xfs_inode_is_filestream(ap->ip)) {
2652 if (blen >= ap->alen)
2653 break;
2654
2655 if (ap->userdata) {
2656 /*
2657 * If startag is an invalid AG, we've
2658 * come here once before and
2659 * xfs_filestream_new_ag picked the
2660 * best currently available.
2661 *
2662 * Don't continue looping, since we
2663 * could loop forever.
2664 */
2665 if (startag == NULLAGNUMBER)
2666 break;
2667
2668 error = xfs_filestream_new_ag(ap, &ag);
Dave Chinner44b56e02010-01-11 11:47:43 +00002669 xfs_perag_put(pag);
Dave Chinner1c1c6eb2010-01-11 11:47:44 +00002670 if (error)
David Chinner2a82b8b2007-07-11 11:09:12 +10002671 return error;
David Chinner2a82b8b2007-07-11 11:09:12 +10002672
2673 /* loop again to set 'blen'*/
2674 startag = NULLAGNUMBER;
Dave Chinner44b56e02010-01-11 11:47:43 +00002675 pag = xfs_perag_get(mp, ag);
David Chinner2a82b8b2007-07-11 11:09:12 +10002676 continue;
2677 }
2678 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002679 if (++ag == mp->m_sb.sb_agcount)
2680 ag = 0;
2681 if (ag == startag)
2682 break;
Dave Chinner44b56e02010-01-11 11:47:43 +00002683 xfs_perag_put(pag);
2684 pag = xfs_perag_get(mp, ag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 }
Dave Chinner44b56e02010-01-11 11:47:43 +00002686 xfs_perag_put(pag);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002687 /*
2688 * Since the above loop did a BUF_TRYLOCK, it is
2689 * possible that there is space for this request.
2690 */
2691 if (notinit || blen < ap->minlen)
2692 args.minlen = ap->minlen;
2693 /*
2694 * If the best seen length is less than the request
2695 * length, use the best as the minimum.
2696 */
2697 else if (blen < ap->alen)
2698 args.minlen = blen;
2699 /*
2700 * Otherwise we've seen an extent as big as alen,
2701 * use that as the minimum.
2702 */
2703 else
2704 args.minlen = ap->alen;
David Chinner2a82b8b2007-07-11 11:09:12 +10002705
2706 /*
2707 * set the failure fallback case to look in the selected
2708 * AG as the stream may have moved.
2709 */
2710 if (xfs_inode_is_filestream(ap->ip))
2711 ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002712 } else if (ap->low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002713 if (xfs_inode_is_filestream(ap->ip))
2714 args.type = XFS_ALLOCTYPE_FIRST_AG;
2715 else
2716 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002717 args.total = args.minlen = ap->minlen;
2718 } else {
2719 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2720 args.total = ap->total;
2721 args.minlen = ap->minlen;
2722 }
David Chinner957d0eb2007-06-18 16:50:37 +10002723 /* apply extent size hints if obtained earlier */
2724 if (unlikely(align)) {
2725 args.prod = align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002726 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2727 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11002728 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11002729 args.prod = 1;
2730 args.mod = 0;
2731 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002732 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002733 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2734 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2735 }
2736 /*
2737 * If we are not low on available data blocks, and the
2738 * underlying logical volume manager is a stripe, and
2739 * the file offset is zero then try to allocate data
2740 * blocks on stripe unit boundary.
2741 * NOTE: ap->aeof is only set if the allocation length
2742 * is >= the stripe unit and the allocation offset is
2743 * at the end of file.
2744 */
2745 if (!ap->low && ap->aeof) {
2746 if (!ap->off) {
2747 args.alignment = mp->m_dalign;
2748 atype = args.type;
2749 isaligned = 1;
2750 /*
2751 * Adjust for alignment
2752 */
2753 if (blen > args.alignment && blen <= ap->alen)
2754 args.minlen = blen - args.alignment;
2755 args.minalignslop = 0;
2756 } else {
2757 /*
2758 * First try an exact bno allocation.
2759 * If it fails then do a near or start bno
2760 * allocation with alignment turned on.
2761 */
2762 atype = args.type;
2763 tryagain = 1;
2764 args.type = XFS_ALLOCTYPE_THIS_BNO;
2765 args.alignment = 1;
2766 /*
2767 * Compute the minlen+alignment for the
2768 * next case. Set slop so that the value
2769 * of minlen+alignment+slop doesn't go up
2770 * between the calls.
2771 */
2772 if (blen > mp->m_dalign && blen <= ap->alen)
2773 nextminlen = blen - mp->m_dalign;
2774 else
2775 nextminlen = args.minlen;
2776 if (nextminlen + mp->m_dalign > args.minlen + 1)
2777 args.minalignslop =
2778 nextminlen + mp->m_dalign -
2779 args.minlen - 1;
2780 else
2781 args.minalignslop = 0;
2782 }
2783 } else {
2784 args.alignment = 1;
2785 args.minalignslop = 0;
2786 }
2787 args.minleft = ap->minleft;
2788 args.wasdel = ap->wasdel;
2789 args.isfl = 0;
2790 args.userdata = ap->userdata;
2791 if ((error = xfs_alloc_vextent(&args)))
2792 return error;
2793 if (tryagain && args.fsbno == NULLFSBLOCK) {
2794 /*
2795 * Exact allocation failed. Now try with alignment
2796 * turned on.
2797 */
2798 args.type = atype;
2799 args.fsbno = ap->rval;
2800 args.alignment = mp->m_dalign;
2801 args.minlen = nextminlen;
2802 args.minalignslop = 0;
2803 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 if ((error = xfs_alloc_vextent(&args)))
2805 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002806 }
2807 if (isaligned && args.fsbno == NULLFSBLOCK) {
2808 /*
2809 * allocation failed, so turn off alignment and
2810 * try again.
2811 */
2812 args.type = atype;
2813 args.fsbno = ap->rval;
2814 args.alignment = 0;
2815 if ((error = xfs_alloc_vextent(&args)))
2816 return error;
2817 }
2818 if (args.fsbno == NULLFSBLOCK && nullfb &&
2819 args.minlen > ap->minlen) {
2820 args.minlen = ap->minlen;
2821 args.type = XFS_ALLOCTYPE_START_BNO;
2822 args.fsbno = ap->rval;
2823 if ((error = xfs_alloc_vextent(&args)))
2824 return error;
2825 }
2826 if (args.fsbno == NULLFSBLOCK && nullfb) {
2827 args.fsbno = 0;
2828 args.type = XFS_ALLOCTYPE_FIRST_AG;
2829 args.total = ap->minlen;
2830 args.minleft = 0;
2831 if ((error = xfs_alloc_vextent(&args)))
2832 return error;
2833 ap->low = 1;
2834 }
2835 if (args.fsbno != NULLFSBLOCK) {
2836 ap->firstblock = ap->rval = args.fsbno;
2837 ASSERT(nullfb || fb_agno == args.agno ||
2838 (ap->low && fb_agno < args.agno));
2839 ap->alen = args.len;
2840 ap->ip->i_d.di_nblocks += args.len;
2841 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2842 if (ap->wasdel)
2843 ap->ip->i_delayed_blks -= args.len;
2844 /*
2845 * Adjust the disk quota also. This was reserved
2846 * earlier.
2847 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002848 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002849 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2850 XFS_TRANS_DQ_BCOUNT,
2851 (long) args.len);
2852 } else {
2853 ap->rval = NULLFSBLOCK;
2854 ap->alen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 }
2856 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002857}
2858
2859/*
2860 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2861 * It figures out where to ask the underlying allocator to put the new extent.
2862 */
2863STATIC int
2864xfs_bmap_alloc(
2865 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2866{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002867 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002868 return xfs_bmap_rtalloc(ap);
2869 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870}
2871
2872/*
2873 * Transform a btree format file with only one leaf node, where the
2874 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002875 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 * give up the space for the btree root and pitch the leaf block.
2877 */
2878STATIC int /* error */
2879xfs_bmap_btree_to_extents(
2880 xfs_trans_t *tp, /* transaction pointer */
2881 xfs_inode_t *ip, /* incore inode pointer */
2882 xfs_btree_cur_t *cur, /* btree cursor */
2883 int *logflagsp, /* inode logging flags */
2884 int whichfork) /* data or attr fork */
2885{
2886 /* REFERENCED */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002887 struct xfs_btree_block *cblock;/* child btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 xfs_fsblock_t cbno; /* child block number */
2889 xfs_buf_t *cbp; /* child block's buffer */
2890 int error; /* error return value */
2891 xfs_ifork_t *ifp; /* inode fork data */
2892 xfs_mount_t *mp; /* mount point structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10002893 __be64 *pp; /* ptr to block address */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002894 struct xfs_btree_block *rblock;/* root btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Christoph Hellwig60197e82008-10-30 17:11:19 +11002896 mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 ifp = XFS_IFORK_PTR(ip, whichfork);
2898 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2899 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2900 rblock = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11002901 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2902 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
Christoph Hellwig60197e82008-10-30 17:11:19 +11002903 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2904 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10002905 cbno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 *logflagsp = 0;
2907#ifdef DEBUG
Christoph Hellwig576039c2006-09-28 10:58:06 +10002908 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 return error;
2910#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2912 XFS_BMAP_BTREE_REF)))
2913 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002914 cblock = XFS_BUF_TO_BLOCK(cbp);
2915 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 return error;
2917 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2918 ip->i_d.di_nblocks--;
Christoph Hellwig7d095252009-06-08 15:33:32 +02002919 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 xfs_trans_binval(tp, cbp);
2921 if (cur->bc_bufs[0] == cbp)
2922 cur->bc_bufs[0] = NULL;
2923 xfs_iroot_realloc(ip, -1, whichfork);
2924 ASSERT(ifp->if_broot == NULL);
2925 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2926 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002927 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 return 0;
2929}
2930
2931/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002932 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 * after removing space (or undoing a delayed allocation).
2934 */
2935STATIC int /* error */
2936xfs_bmap_del_extent(
2937 xfs_inode_t *ip, /* incore inode pointer */
2938 xfs_trans_t *tp, /* current transaction pointer */
2939 xfs_extnum_t idx, /* extent number to update/delete */
2940 xfs_bmap_free_t *flist, /* list of extents to be freed */
2941 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002942 xfs_bmbt_irec_t *del, /* data to remove from extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002944 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 int whichfork, /* data or attr fork */
2946 int rsvd) /* OK to allocate reserved blocks */
2947{
2948 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2949 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2950 xfs_fsblock_t del_endblock=0; /* first block past del */
2951 xfs_fileoff_t del_endoff; /* first offset past del */
2952 int delay; /* current block is delayed allocated */
2953 int do_fx; /* free extent at end of routine */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002954 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 int error; /* error return value */
2956 int flags; /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 xfs_bmbt_irec_t got; /* current extent entry */
2958 xfs_fileoff_t got_endoff; /* first offset past got */
2959 int i; /* temp state */
2960 xfs_ifork_t *ifp; /* inode fork pointer */
2961 xfs_mount_t *mp; /* mount structure */
2962 xfs_filblks_t nblks; /* quota/sb block count */
2963 xfs_bmbt_irec_t new; /* new record to be inserted */
2964 /* REFERENCED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 uint qfield; /* quota field to update */
2966 xfs_filblks_t temp; /* for indirect length calculations */
2967 xfs_filblks_t temp2; /* for indirect length calculations */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002968 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
2970 XFS_STATS_INC(xs_del_exlist);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002971
2972 if (whichfork == XFS_ATTR_FORK)
2973 state |= BMAP_ATTRFORK;
2974
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 mp = ip->i_mount;
2976 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002977 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
2978 (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 ASSERT(del->br_blockcount > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002980 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 xfs_bmbt_get_all(ep, &got);
2982 ASSERT(got.br_startoff <= del->br_startoff);
2983 del_endoff = del->br_startoff + del->br_blockcount;
2984 got_endoff = got.br_startoff + got.br_blockcount;
2985 ASSERT(got_endoff >= del_endoff);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002986 delay = isnullstartblock(got.br_startblock);
2987 ASSERT(isnullstartblock(del->br_startblock) == delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 flags = 0;
2989 qfield = 0;
2990 error = 0;
2991 /*
2992 * If deleting a real allocation, must free up the disk space.
2993 */
2994 if (!delay) {
2995 flags = XFS_ILOG_CORE;
2996 /*
2997 * Realtime allocation. Free it and record di_nblocks update.
2998 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002999 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 xfs_fsblock_t bno;
3001 xfs_filblks_t len;
3002
3003 ASSERT(do_mod(del->br_blockcount,
3004 mp->m_sb.sb_rextsize) == 0);
3005 ASSERT(do_mod(del->br_startblock,
3006 mp->m_sb.sb_rextsize) == 0);
3007 bno = del->br_startblock;
3008 len = del->br_blockcount;
3009 do_div(bno, mp->m_sb.sb_rextsize);
3010 do_div(len, mp->m_sb.sb_rextsize);
3011 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3012 (xfs_extlen_t)len)))
3013 goto done;
3014 do_fx = 0;
3015 nblks = len * mp->m_sb.sb_rextsize;
3016 qfield = XFS_TRANS_DQ_RTBCOUNT;
3017 }
3018 /*
3019 * Ordinary allocation.
3020 */
3021 else {
3022 do_fx = 1;
3023 nblks = del->br_blockcount;
3024 qfield = XFS_TRANS_DQ_BCOUNT;
3025 }
3026 /*
3027 * Set up del_endblock and cur for later.
3028 */
3029 del_endblock = del->br_startblock + del->br_blockcount;
3030 if (cur) {
3031 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3032 got.br_startblock, got.br_blockcount,
3033 &i)))
3034 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003035 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 }
3037 da_old = da_new = 0;
3038 } else {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003039 da_old = startblockval(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 da_new = 0;
3041 nblks = 0;
3042 do_fx = 0;
3043 }
3044 /*
3045 * Set flag value to use in switch statement.
3046 * Left-contig is 2, right-contig is 1.
3047 */
3048 switch (((got.br_startoff == del->br_startoff) << 1) |
3049 (got_endoff == del_endoff)) {
3050 case 3:
3051 /*
3052 * Matches the whole extent. Delete the entry.
3053 */
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003054 xfs_iext_remove(ip, idx, 1,
3055 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 ifp->if_lastex = idx;
3057 if (delay)
3058 break;
3059 XFS_IFORK_NEXT_SET(ip, whichfork,
3060 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3061 flags |= XFS_ILOG_CORE;
3062 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003063 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 break;
3065 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003066 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003068 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 break;
3070
3071 case 2:
3072 /*
3073 * Deleting the first part of the extent.
3074 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003075 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 xfs_bmbt_set_startoff(ep, del_endoff);
3077 temp = got.br_blockcount - del->br_blockcount;
3078 xfs_bmbt_set_blockcount(ep, temp);
3079 ifp->if_lastex = idx;
3080 if (delay) {
3081 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3082 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003083 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003084 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 da_new = temp;
3086 break;
3087 }
3088 xfs_bmbt_set_startblock(ep, del_endblock);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003089 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003091 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 break;
3093 }
3094 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3095 got.br_blockcount - del->br_blockcount,
3096 got.br_state)))
3097 goto done;
3098 break;
3099
3100 case 1:
3101 /*
3102 * Deleting the last part of the extent.
3103 */
3104 temp = got.br_blockcount - del->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003105 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 xfs_bmbt_set_blockcount(ep, temp);
3107 ifp->if_lastex = idx;
3108 if (delay) {
3109 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3110 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003111 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003112 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 da_new = temp;
3114 break;
3115 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003116 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003118 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 break;
3120 }
3121 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3122 got.br_startblock,
3123 got.br_blockcount - del->br_blockcount,
3124 got.br_state)))
3125 goto done;
3126 break;
3127
3128 case 0:
3129 /*
3130 * Deleting the middle of the extent.
3131 */
3132 temp = del->br_startoff - got.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003133 trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 xfs_bmbt_set_blockcount(ep, temp);
3135 new.br_startoff = del_endoff;
3136 temp2 = got_endoff - del_endoff;
3137 new.br_blockcount = temp2;
3138 new.br_state = got.br_state;
3139 if (!delay) {
3140 new.br_startblock = del_endblock;
3141 flags |= XFS_ILOG_CORE;
3142 if (cur) {
3143 if ((error = xfs_bmbt_update(cur,
3144 got.br_startoff,
3145 got.br_startblock, temp,
3146 got.br_state)))
3147 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11003148 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 goto done;
3150 cur->bc_rec.b = new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003151 error = xfs_btree_insert(cur, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 if (error && error != ENOSPC)
3153 goto done;
3154 /*
3155 * If get no-space back from btree insert,
3156 * it tried a split, and we have a zero
3157 * block reservation.
3158 * Fix up our state and return the error.
3159 */
3160 if (error == ENOSPC) {
3161 /*
3162 * Reset the cursor, don't trust
3163 * it after any insert operation.
3164 */
3165 if ((error = xfs_bmbt_lookup_eq(cur,
3166 got.br_startoff,
3167 got.br_startblock,
3168 temp, &i)))
3169 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003170 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 /*
3172 * Update the btree record back
3173 * to the original value.
3174 */
3175 if ((error = xfs_bmbt_update(cur,
3176 got.br_startoff,
3177 got.br_startblock,
3178 got.br_blockcount,
3179 got.br_state)))
3180 goto done;
3181 /*
3182 * Reset the extent record back
3183 * to the original value.
3184 */
3185 xfs_bmbt_set_blockcount(ep,
3186 got.br_blockcount);
3187 flags = 0;
3188 error = XFS_ERROR(ENOSPC);
3189 goto done;
3190 }
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003191 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 } else
Eric Sandeen9d87c312009-01-14 23:22:07 -06003193 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 XFS_IFORK_NEXT_SET(ip, whichfork,
3195 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3196 } else {
3197 ASSERT(whichfork == XFS_DATA_FORK);
3198 temp = xfs_bmap_worst_indlen(ip, temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003199 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003201 new.br_startblock = nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 da_new = temp + temp2;
3203 while (da_new > da_old) {
3204 if (temp) {
3205 temp--;
3206 da_new--;
3207 xfs_bmbt_set_startblock(ep,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003208 nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 }
3210 if (da_new == da_old)
3211 break;
3212 if (temp2) {
3213 temp2--;
3214 da_new--;
3215 new.br_startblock =
Eric Sandeen9d87c312009-01-14 23:22:07 -06003216 nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 }
3218 }
3219 }
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003220 trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_);
3221 xfs_iext_insert(ip, idx + 1, 1, &new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 ifp->if_lastex = idx + 1;
3223 break;
3224 }
3225 /*
3226 * If we need to, add to list of extents to delete.
3227 */
3228 if (do_fx)
3229 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3230 mp);
3231 /*
3232 * Adjust inode # blocks in the file.
3233 */
3234 if (nblks)
3235 ip->i_d.di_nblocks -= nblks;
3236 /*
3237 * Adjust quota data.
3238 */
3239 if (qfield)
Christoph Hellwig7d095252009-06-08 15:33:32 +02003240 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
3242 /*
3243 * Account for change in delayed indirect blocks.
3244 * Nothing to do for disk quota accounting here.
3245 */
3246 ASSERT(da_old >= da_new);
3247 if (da_old > da_new)
David Chinner20f4ebf2007-02-10 18:36:10 +11003248 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 rsvd);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003250 if (delta) {
3251 /* DELTA: report the original extent. */
3252 if (delta->xed_startoff > got.br_startoff)
3253 delta->xed_startoff = got.br_startoff;
3254 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3255 delta->xed_blockcount = got.br_startoff +
3256 got.br_blockcount;
3257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258done:
3259 *logflagsp = flags;
3260 return error;
3261}
3262
3263/*
3264 * Remove the entry "free" from the free item list. Prev points to the
3265 * previous entry, unless "free" is the head of the list.
3266 */
3267STATIC void
3268xfs_bmap_del_free(
3269 xfs_bmap_free_t *flist, /* free item list header */
3270 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3271 xfs_bmap_free_item_t *free) /* list item to be freed */
3272{
3273 if (prev)
3274 prev->xbfi_next = free->xbfi_next;
3275 else
3276 flist->xbf_first = free->xbfi_next;
3277 flist->xbf_count--;
3278 kmem_zone_free(xfs_bmap_free_item_zone, free);
3279}
3280
3281/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 * Convert an extents-format file into a btree-format file.
3283 * The new file will have a root block (in the inode) and a single child block.
3284 */
3285STATIC int /* error */
3286xfs_bmap_extents_to_btree(
3287 xfs_trans_t *tp, /* transaction pointer */
3288 xfs_inode_t *ip, /* incore inode pointer */
3289 xfs_fsblock_t *firstblock, /* first-block-allocated */
3290 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3291 xfs_btree_cur_t **curp, /* cursor returned to caller */
3292 int wasdel, /* converting a delayed alloc */
3293 int *logflagsp, /* inode logging flags */
3294 int whichfork) /* data or attr fork */
3295{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003296 struct xfs_btree_block *ablock; /* allocated (child) bt block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 xfs_buf_t *abp; /* buffer for ablock */
3298 xfs_alloc_arg_t args; /* allocation arguments */
3299 xfs_bmbt_rec_t *arp; /* child record pointer */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003300 struct xfs_btree_block *block; /* btree root block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 xfs_btree_cur_t *cur; /* bmap btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003302 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003304 xfs_extnum_t i, cnt; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 xfs_ifork_t *ifp; /* inode fork pointer */
3306 xfs_bmbt_key_t *kp; /* root block key pointer */
3307 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003308 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3310
3311 ifp = XFS_IFORK_PTR(ip, whichfork);
3312 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3313 ASSERT(ifp->if_ext_max ==
3314 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3315 /*
3316 * Make space in the inode incore.
3317 */
3318 xfs_iroot_realloc(ip, 1, whichfork);
3319 ifp->if_flags |= XFS_IFBROOT;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003320
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 /*
3322 * Fill in the root.
3323 */
3324 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003325 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3326 block->bb_level = cpu_to_be16(1);
3327 block->bb_numrecs = cpu_to_be16(1);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003328 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3329 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3330
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 /*
3332 * Need a cursor. Can't allocate until bb_level is filled in.
3333 */
3334 mp = ip->i_mount;
Christoph Hellwig561f7d12008-10-30 16:53:59 +11003335 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 cur->bc_private.b.firstblock = *firstblock;
3337 cur->bc_private.b.flist = flist;
3338 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3339 /*
3340 * Convert to a btree with two levels, one record in root.
3341 */
3342 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3343 args.tp = tp;
3344 args.mp = mp;
Yingping Lud210a282006-06-09 14:55:18 +10003345 args.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 if (*firstblock == NULLFSBLOCK) {
3347 args.type = XFS_ALLOCTYPE_START_BNO;
3348 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3349 } else if (flist->xbf_low) {
3350 args.type = XFS_ALLOCTYPE_START_BNO;
3351 args.fsbno = *firstblock;
3352 } else {
3353 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3354 args.fsbno = *firstblock;
3355 }
3356 args.minlen = args.maxlen = args.prod = 1;
3357 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3358 args.minalignslop = 0;
3359 args.wasdel = wasdel;
3360 *logflagsp = 0;
3361 if ((error = xfs_alloc_vextent(&args))) {
3362 xfs_iroot_realloc(ip, -1, whichfork);
3363 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3364 return error;
3365 }
3366 /*
3367 * Allocation can't fail, the space was reserved.
3368 */
3369 ASSERT(args.fsbno != NULLFSBLOCK);
3370 ASSERT(*firstblock == NULLFSBLOCK ||
3371 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3372 (flist->xbf_low &&
3373 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3374 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3375 cur->bc_private.b.allocated++;
3376 ip->i_d.di_nblocks++;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003377 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3379 /*
3380 * Fill in the child block.
3381 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003382 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003383 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 ablock->bb_level = 0;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003385 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3386 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003387 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003389 for (cnt = i = 0; i < nextents; i++) {
3390 ep = xfs_iext_get_ext(ifp, i);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003391 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10003392 arp->l0 = cpu_to_be64(ep->l0);
3393 arp->l1 = cpu_to_be64(ep->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 arp++; cnt++;
3395 }
3396 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11003397 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003398 xfs_btree_set_numrecs(ablock, cnt);
3399
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 /*
3401 * Fill in the root key and pointer.
3402 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11003403 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3404 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Christoph Hellwig8801bb92006-09-28 10:58:17 +10003405 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
Christoph Hellwig136341b2008-10-30 17:11:40 +11003406 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3407 be16_to_cpu(block->bb_level)));
Christoph Hellwig576039c2006-09-28 10:58:06 +10003408 *pp = cpu_to_be64(args.fsbno);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 /*
3411 * Do all this logging at the end so that
3412 * the root is at the right level.
3413 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11003414 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3415 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 ASSERT(*curp == NULL);
3417 *curp = cur;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003418 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 return 0;
3420}
3421
3422/*
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003423 * Calculate the default attribute fork offset for newly created inodes.
3424 */
3425uint
3426xfs_default_attroffset(
3427 struct xfs_inode *ip)
3428{
3429 struct xfs_mount *mp = ip->i_mount;
3430 uint offset;
3431
3432 if (mp->m_sb.sb_inodesize == 256) {
3433 offset = XFS_LITINO(mp) -
3434 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3435 } else {
3436 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3437 }
3438
3439 ASSERT(offset < XFS_LITINO(mp));
3440 return offset;
3441}
3442
3443/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11003444 * Helper routine to reset inode di_forkoff field when switching
3445 * attribute fork from local to extent format - we reset it where
3446 * possible to make space available for inline data fork extents.
3447 */
3448STATIC void
3449xfs_bmap_forkoff_reset(
3450 xfs_mount_t *mp,
3451 xfs_inode_t *ip,
3452 int whichfork)
3453{
3454 if (whichfork == XFS_ATTR_FORK &&
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003455 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3456 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3457 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3458 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3459
3460 if (dfl_forkoff > ip->i_d.di_forkoff) {
3461 ip->i_d.di_forkoff = dfl_forkoff;
3462 ip->i_df.if_ext_max =
3463 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3464 ip->i_afp->if_ext_max =
3465 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3466 }
Nathan Scottd8cc8902005-11-02 10:34:53 +11003467 }
3468}
3469
3470/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 * Convert a local file to an extents file.
3472 * This code is out of bounds for data forks of regular files,
3473 * since the file data needs to get logged so things will stay consistent.
3474 * (The bmap-level manipulations are ok, though).
3475 */
3476STATIC int /* error */
3477xfs_bmap_local_to_extents(
3478 xfs_trans_t *tp, /* transaction pointer */
3479 xfs_inode_t *ip, /* incore inode pointer */
3480 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3481 xfs_extlen_t total, /* total blocks needed by transaction */
3482 int *logflagsp, /* inode logging flags */
3483 int whichfork) /* data or attr fork */
3484{
3485 int error; /* error return value */
3486 int flags; /* logging flags returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 xfs_ifork_t *ifp; /* inode fork pointer */
3488
3489 /*
3490 * We don't want to deal with the case of keeping inode data inline yet.
3491 * So sending the data fork of a regular inode is invalid.
3492 */
3493 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3494 whichfork == XFS_DATA_FORK));
3495 ifp = XFS_IFORK_PTR(ip, whichfork);
3496 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3497 flags = 0;
3498 error = 0;
3499 if (ifp->if_bytes) {
3500 xfs_alloc_arg_t args; /* allocation arguments */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003501 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003502 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
3504 args.tp = tp;
3505 args.mp = ip->i_mount;
Yingping Lud210a282006-06-09 14:55:18 +10003506 args.firstblock = *firstblock;
Mandy Kirkconnellf020b672006-03-14 14:07:24 +11003507 ASSERT((ifp->if_flags &
3508 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 /*
3510 * Allocate a block. We know we need only one, since the
3511 * file currently fits in an inode.
3512 */
3513 if (*firstblock == NULLFSBLOCK) {
3514 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3515 args.type = XFS_ALLOCTYPE_START_BNO;
3516 } else {
3517 args.fsbno = *firstblock;
3518 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3519 }
3520 args.total = total;
3521 args.mod = args.minleft = args.alignment = args.wasdel =
3522 args.isfl = args.minalignslop = 0;
3523 args.minlen = args.maxlen = args.prod = 1;
3524 if ((error = xfs_alloc_vextent(&args)))
3525 goto done;
3526 /*
3527 * Can't fail, the space was reserved.
3528 */
3529 ASSERT(args.fsbno != NULLFSBLOCK);
3530 ASSERT(args.len == 1);
3531 *firstblock = args.fsbno;
3532 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3533 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3534 ifp->if_bytes);
3535 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003536 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003538 xfs_iext_add(ifp, 0, 1);
3539 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003541 trace_xfs_bmap_post_update(ip, 0,
3542 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3543 _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3545 ip->i_d.di_nblocks = 1;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003546 xfs_trans_mod_dquot_byino(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 XFS_TRANS_DQ_BCOUNT, 1L);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003548 flags |= xfs_ilog_fext(whichfork);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003549 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003551 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 ifp->if_flags &= ~XFS_IFINLINE;
3554 ifp->if_flags |= XFS_IFEXTENTS;
3555 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3556 flags |= XFS_ILOG_CORE;
3557done:
3558 *logflagsp = flags;
3559 return error;
3560}
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003563 * Search the extent records for the entry containing block bno.
3564 * If bno lies in a hole, point to the next entry. If bno lies
3565 * past eof, *eofp will be set, and *prevp will contain the last
3566 * entry (null if none). Else, *lastxp will be set to the index
3567 * of the found entry; *gotp will contain the entry.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003568 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003569STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003570xfs_bmap_search_multi_extents(
3571 xfs_ifork_t *ifp, /* inode fork pointer */
3572 xfs_fileoff_t bno, /* block number searched for */
3573 int *eofp, /* out: end of file found */
3574 xfs_extnum_t *lastxp, /* out: last extent index */
3575 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3576 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3577{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003578 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003579 xfs_extnum_t lastx; /* last extent index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003580
3581 /*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003582 * Initialize the extent entry structure to catch access to
3583 * uninitialized br_startblock field.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003584 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003585 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3586 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3587 gotp->br_state = XFS_EXT_INVALID;
3588#if XFS_BIG_BLKNOS
3589 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3590#else
3591 gotp->br_startblock = 0xffffa5a5;
3592#endif
3593 prevp->br_startoff = NULLFILEOFF;
3594
3595 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3596 if (lastx > 0) {
3597 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3598 }
3599 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3600 xfs_bmbt_get_all(ep, gotp);
3601 *eofp = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003602 } else {
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003603 if (lastx > 0) {
3604 *gotp = *prevp;
3605 }
3606 *eofp = 1;
3607 ep = NULL;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003608 }
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003609 *lastxp = lastx;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003610 return ep;
3611}
3612
3613/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 * Search the extents list for the inode, for the extent containing bno.
3615 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3616 * *eofp will be set, and *prevp will contain the last entry (null if none).
3617 * Else, *lastxp will be set to the index of the found
3618 * entry; *gotp will contain the entry.
3619 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003620STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621xfs_bmap_search_extents(
3622 xfs_inode_t *ip, /* incore inode pointer */
3623 xfs_fileoff_t bno, /* block number searched for */
Nathan Scott572d95f2006-09-28 11:03:20 +10003624 int fork, /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 int *eofp, /* out: end of file found */
3626 xfs_extnum_t *lastxp, /* out: last extent index */
3627 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3628 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3629{
3630 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003631 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
3633 XFS_STATS_INC(xs_look_exlist);
Nathan Scott572d95f2006-09-28 11:03:20 +10003634 ifp = XFS_IFORK_PTR(ip, fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003636 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3637
Nathan Scott572d95f2006-09-28 11:03:20 +10003638 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3639 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3640 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3641 "Access to block zero in inode %llu "
3642 "start_block: %llx start_off: %llx "
3643 "blkcnt: %llx extent-state: %x lastx: %x\n",
3644 (unsigned long long)ip->i_ino,
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003645 (unsigned long long)gotp->br_startblock,
3646 (unsigned long long)gotp->br_startoff,
3647 (unsigned long long)gotp->br_blockcount,
Nathan Scott572d95f2006-09-28 11:03:20 +10003648 gotp->br_state, *lastxp);
3649 *lastxp = NULLEXTNUM;
3650 *eofp = 1;
3651 return NULL;
3652 }
3653 return ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654}
3655
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656/*
3657 * Compute the worst-case number of indirect blocks that will be used
3658 * for ip's delayed extent of length "len".
3659 */
3660STATIC xfs_filblks_t
3661xfs_bmap_worst_indlen(
3662 xfs_inode_t *ip, /* incore inode pointer */
3663 xfs_filblks_t len) /* delayed extent length */
3664{
3665 int level; /* btree level number */
3666 int maxrecs; /* maximum record count at this level */
3667 xfs_mount_t *mp; /* mount structure */
3668 xfs_filblks_t rval; /* return value */
3669
3670 mp = ip->i_mount;
3671 maxrecs = mp->m_bmap_dmxr[0];
3672 for (level = 0, rval = 0;
3673 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3674 level++) {
3675 len += maxrecs - 1;
3676 do_div(len, maxrecs);
3677 rval += len;
3678 if (len == 1)
3679 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3680 level - 1;
3681 if (level == 0)
3682 maxrecs = mp->m_bmap_dmxr[1];
3683 }
3684 return rval;
3685}
3686
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687/*
3688 * Convert inode from non-attributed to attributed.
3689 * Must not be in a transaction, ip must not be locked.
3690 */
3691int /* error code */
3692xfs_bmap_add_attrfork(
3693 xfs_inode_t *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003694 int size, /* space new attribute needs */
3695 int rsvd) /* xact may use reserved blks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003698 xfs_bmap_free_t flist; /* freed extent records */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 xfs_trans_t *tp; /* transaction pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003701 int blks; /* space reservation */
3702 int version = 1; /* superblock attr version */
3703 int committed; /* xaction was committed */
3704 int logflags; /* logging flags */
3705 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
Nathan Scottd8cc8902005-11-02 10:34:53 +11003707 ASSERT(XFS_IFORK_Q(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 ASSERT(ip->i_df.if_ext_max ==
3709 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
Nathan Scottd8cc8902005-11-02 10:34:53 +11003710
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 mp = ip->i_mount;
3712 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3713 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3714 blks = XFS_ADDAFORK_SPACE_RES(mp);
3715 if (rsvd)
3716 tp->t_flags |= XFS_TRANS_RESERVE;
3717 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3718 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3719 goto error0;
3720 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02003721 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3723 XFS_QMOPT_RES_REGBLKS);
3724 if (error) {
3725 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3726 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3727 return error;
3728 }
3729 if (XFS_IFORK_Q(ip))
3730 goto error1;
3731 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3732 /*
3733 * For inodes coming from pre-6.2 filesystems.
3734 */
3735 ASSERT(ip->i_d.di_aformat == 0);
3736 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3737 }
3738 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwig0b1f91772008-08-13 16:13:09 +10003739 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3741 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3742 switch (ip->i_d.di_format) {
3743 case XFS_DINODE_FMT_DEV:
3744 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3745 break;
3746 case XFS_DINODE_FMT_UUID:
3747 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3748 break;
3749 case XFS_DINODE_FMT_LOCAL:
3750 case XFS_DINODE_FMT_EXTENTS:
3751 case XFS_DINODE_FMT_BTREE:
Nathan Scottd8cc8902005-11-02 10:34:53 +11003752 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3753 if (!ip->i_d.di_forkoff)
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003754 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
Nathan Scott13059ff2006-01-11 15:32:01 +11003755 else if (mp->m_flags & XFS_MOUNT_ATTR2)
Nathan Scottd8cc8902005-11-02 10:34:53 +11003756 version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 break;
3758 default:
3759 ASSERT(0);
3760 error = XFS_ERROR(EINVAL);
3761 goto error1;
3762 }
3763 ip->i_df.if_ext_max =
3764 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3765 ASSERT(ip->i_afp == NULL);
3766 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3767 ip->i_afp->if_ext_max =
3768 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
3769 ip->i_afp->if_flags = XFS_IFEXTENTS;
3770 logflags = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003771 xfs_bmap_init(&flist, &firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 switch (ip->i_d.di_format) {
3773 case XFS_DINODE_FMT_LOCAL:
3774 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3775 &logflags);
3776 break;
3777 case XFS_DINODE_FMT_EXTENTS:
3778 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3779 &flist, &logflags);
3780 break;
3781 case XFS_DINODE_FMT_BTREE:
3782 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3783 &logflags);
3784 break;
3785 default:
3786 error = 0;
3787 break;
3788 }
3789 if (logflags)
3790 xfs_trans_log_inode(tp, ip, logflags);
3791 if (error)
3792 goto error2;
Eric Sandeen62118702008-03-06 13:44:28 +11003793 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3794 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +11003795 __int64_t sbfields = 0;
3796
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003797 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +11003798 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3799 xfs_sb_version_addattr(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003800 sbfields |= XFS_SB_VERSIONNUM;
Nathan Scottd8cc8902005-11-02 10:34:53 +11003801 }
Eric Sandeen62118702008-03-06 13:44:28 +11003802 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3803 xfs_sb_version_addattr2(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003804 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003805 }
Nathan Scottda087ba2005-11-02 15:00:20 +11003806 if (sbfields) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003807 spin_unlock(&mp->m_sb_lock);
Nathan Scottda087ba2005-11-02 15:00:20 +11003808 xfs_mod_sb(tp, sbfields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003810 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 }
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003812 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 goto error2;
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003814 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 ASSERT(ip->i_df.if_ext_max ==
3816 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3817 return error;
3818error2:
3819 xfs_bmap_cancel(&flist);
3820error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3822error0:
3823 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3824 ASSERT(ip->i_df.if_ext_max ==
3825 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
3826 return error;
3827}
3828
3829/*
3830 * Add the extent to the list of extents to be free at transaction end.
3831 * The list is maintained sorted (by block number).
3832 */
3833/* ARGSUSED */
3834void
3835xfs_bmap_add_free(
3836 xfs_fsblock_t bno, /* fs block number of extent */
3837 xfs_filblks_t len, /* length of extent */
3838 xfs_bmap_free_t *flist, /* list of extents */
3839 xfs_mount_t *mp) /* mount point structure */
3840{
3841 xfs_bmap_free_item_t *cur; /* current (next) element */
3842 xfs_bmap_free_item_t *new; /* new element */
3843 xfs_bmap_free_item_t *prev; /* previous element */
3844#ifdef DEBUG
3845 xfs_agnumber_t agno;
3846 xfs_agblock_t agbno;
3847
3848 ASSERT(bno != NULLFSBLOCK);
3849 ASSERT(len > 0);
3850 ASSERT(len <= MAXEXTLEN);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003851 ASSERT(!isnullstartblock(bno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 agno = XFS_FSB_TO_AGNO(mp, bno);
3853 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3854 ASSERT(agno < mp->m_sb.sb_agcount);
3855 ASSERT(agbno < mp->m_sb.sb_agblocks);
3856 ASSERT(len < mp->m_sb.sb_agblocks);
3857 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3858#endif
3859 ASSERT(xfs_bmap_free_item_zone != NULL);
3860 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3861 new->xbfi_startblock = bno;
3862 new->xbfi_blockcount = (xfs_extlen_t)len;
3863 for (prev = NULL, cur = flist->xbf_first;
3864 cur != NULL;
3865 prev = cur, cur = cur->xbfi_next) {
3866 if (cur->xbfi_startblock >= bno)
3867 break;
3868 }
3869 if (prev)
3870 prev->xbfi_next = new;
3871 else
3872 flist->xbf_first = new;
3873 new->xbfi_next = cur;
3874 flist->xbf_count++;
3875}
3876
3877/*
3878 * Compute and fill in the value of the maximum depth of a bmap btree
3879 * in this filesystem. Done once, during mount.
3880 */
3881void
3882xfs_bmap_compute_maxlevels(
3883 xfs_mount_t *mp, /* file system mount structure */
3884 int whichfork) /* data or attr fork */
3885{
3886 int level; /* btree level */
3887 uint maxblocks; /* max blocks at this level */
3888 uint maxleafents; /* max leaf entries possible */
3889 int maxrootrecs; /* max records in root block */
3890 int minleafrecs; /* min records in leaf block */
3891 int minnoderecs; /* min records in node block */
3892 int sz; /* root block size */
3893
3894 /*
3895 * The maximum number of extents in a file, hence the maximum
3896 * number of leaf entries, is controlled by the type of di_nextents
3897 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3898 * (a signed 16-bit number, xfs_aextnum_t).
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003899 *
3900 * Note that we can no longer assume that if we are in ATTR1 that
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003901 * the fork offset of all the inodes will be
3902 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3903 * with ATTR2 and then mounted back with ATTR1, keeping the
3904 * di_forkoff's fixed but probably at various positions. Therefore,
3905 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3906 * of a minimum size available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003908 if (whichfork == XFS_DATA_FORK) {
3909 maxleafents = MAXEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003910 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003911 } else {
3912 maxleafents = MAXAEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003913 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003914 }
Christoph Hellwig60197e82008-10-30 17:11:19 +11003915 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 minleafrecs = mp->m_bmap_dmnr[0];
3917 minnoderecs = mp->m_bmap_dmnr[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3919 for (level = 1; maxblocks > 1; level++) {
3920 if (maxblocks <= maxrootrecs)
3921 maxblocks = 1;
3922 else
3923 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3924 }
3925 mp->m_bm_maxlevels[whichfork] = level;
3926}
3927
3928/*
3929 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3930 * caller. Frees all the extents that need freeing, which must be done
3931 * last due to locking considerations. We never free any extents in
3932 * the first transaction. This is to allow the caller to make the first
3933 * transaction a synchronous one so that the pointers to the data being
3934 * broken in this transaction will be permanent before the data is actually
3935 * freed. This is necessary to prevent blocks from being reallocated
3936 * and written to before the free and reallocation are actually permanent.
3937 * We do not just make the first transaction synchronous here, because
3938 * there are more efficient ways to gain the same protection in some cases
3939 * (see the file truncation code).
3940 *
3941 * Return 1 if the given transaction was committed and a new one
3942 * started, and 0 otherwise in the committed parameter.
3943 */
3944/*ARGSUSED*/
3945int /* error */
3946xfs_bmap_finish(
3947 xfs_trans_t **tp, /* transaction pointer addr */
3948 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 int *committed) /* xact committed or not */
3950{
3951 xfs_efd_log_item_t *efd; /* extent free data */
3952 xfs_efi_log_item_t *efi; /* extent free intention */
3953 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003954 xfs_bmap_free_item_t *free; /* free extent item */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 unsigned int logres; /* new log reservation */
3956 unsigned int logcount; /* new log count */
3957 xfs_mount_t *mp; /* filesystem mount structure */
3958 xfs_bmap_free_item_t *next; /* next item on free list */
3959 xfs_trans_t *ntp; /* new transaction pointer */
3960
3961 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3962 if (flist->xbf_count == 0) {
3963 *committed = 0;
3964 return 0;
3965 }
3966 ntp = *tp;
3967 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3968 for (free = flist->xbf_first; free; free = free->xbfi_next)
3969 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3970 free->xbfi_blockcount);
3971 logres = ntp->t_log_res;
3972 logcount = ntp->t_log_count;
3973 ntp = xfs_trans_dup(*tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003974 error = xfs_trans_commit(*tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 *tp = ntp;
3976 *committed = 1;
3977 /*
3978 * We have a new transaction, so we should return committed=1,
3979 * even though we're returning an error.
3980 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003981 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 return error;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003983
3984 /*
3985 * transaction commit worked ok so we can drop the extra ticket
3986 * reference that we gained in xfs_trans_dup()
3987 */
3988 xfs_log_ticket_put(ntp->t_ticket);
3989
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3991 logcount)))
3992 return error;
3993 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3994 for (free = flist->xbf_first; free != NULL; free = next) {
3995 next = free->xbfi_next;
3996 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3997 free->xbfi_blockcount))) {
3998 /*
3999 * The bmap free list will be cleaned up at a
4000 * higher level. The EFI will be canceled when
4001 * this transaction is aborted.
4002 * Need to force shutdown here to make sure it
4003 * happens, since this transaction may not be
4004 * dirty yet.
4005 */
4006 mp = ntp->t_mountp;
4007 if (!XFS_FORCED_SHUTDOWN(mp))
4008 xfs_force_shutdown(mp,
4009 (error == EFSCORRUPTED) ?
Nathan Scott7d04a332006-06-09 14:58:38 +10004010 SHUTDOWN_CORRUPT_INCORE :
4011 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 return error;
4013 }
4014 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4015 free->xbfi_blockcount);
4016 xfs_bmap_del_free(flist, NULL, free);
4017 }
4018 return 0;
4019}
4020
4021/*
4022 * Free up any items left in the list.
4023 */
4024void
4025xfs_bmap_cancel(
4026 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4027{
4028 xfs_bmap_free_item_t *free; /* free list item */
4029 xfs_bmap_free_item_t *next;
4030
4031 if (flist->xbf_count == 0)
4032 return;
4033 ASSERT(flist->xbf_first != NULL);
4034 for (free = flist->xbf_first; free; free = next) {
4035 next = free->xbfi_next;
4036 xfs_bmap_del_free(flist, NULL, free);
4037 }
4038 ASSERT(flist->xbf_count == 0);
4039}
4040
4041/*
4042 * Returns the file-relative block number of the first unused block(s)
4043 * in the file with at least "len" logically contiguous blocks free.
4044 * This is the lowest-address hole if the file has holes, else the first block
4045 * past the end of file.
4046 * Return 0 if the file is currently local (in-inode).
4047 */
4048int /* error */
4049xfs_bmap_first_unused(
4050 xfs_trans_t *tp, /* transaction pointer */
4051 xfs_inode_t *ip, /* incore inode */
4052 xfs_extlen_t len, /* size of hole to find */
4053 xfs_fileoff_t *first_unused, /* unused block */
4054 int whichfork) /* data or attr fork */
4055{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004057 int idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 xfs_ifork_t *ifp; /* inode fork pointer */
4059 xfs_fileoff_t lastaddr; /* last block number seen */
4060 xfs_fileoff_t lowest; /* lowest useful block */
4061 xfs_fileoff_t max; /* starting useful block */
4062 xfs_fileoff_t off; /* offset for this block */
4063 xfs_extnum_t nextents; /* number of extent entries */
4064
4065 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4066 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4067 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4068 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4069 *first_unused = 0;
4070 return 0;
4071 }
4072 ifp = XFS_IFORK_PTR(ip, whichfork);
4073 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4074 (error = xfs_iread_extents(tp, ip, whichfork)))
4075 return error;
4076 lowest = *first_unused;
4077 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004078 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004079 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 off = xfs_bmbt_get_startoff(ep);
4081 /*
4082 * See if the hole before this extent will work.
4083 */
4084 if (off >= lowest + len && off - max >= len) {
4085 *first_unused = max;
4086 return 0;
4087 }
4088 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4089 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4090 }
4091 *first_unused = max;
4092 return 0;
4093}
4094
4095/*
4096 * Returns the file-relative block number of the last block + 1 before
4097 * last_block (input value) in the file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004098 * This is not based on i_size, it is based on the extent records.
4099 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 */
4101int /* error */
4102xfs_bmap_last_before(
4103 xfs_trans_t *tp, /* transaction pointer */
4104 xfs_inode_t *ip, /* incore inode */
4105 xfs_fileoff_t *last_block, /* last block */
4106 int whichfork) /* data or attr fork */
4107{
4108 xfs_fileoff_t bno; /* input file offset */
4109 int eof; /* hit end of file */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004110 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 int error; /* error return value */
4112 xfs_bmbt_irec_t got; /* current extent value */
4113 xfs_ifork_t *ifp; /* inode fork pointer */
4114 xfs_extnum_t lastx; /* last extent used */
4115 xfs_bmbt_irec_t prev; /* previous extent value */
4116
4117 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4118 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4119 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4120 return XFS_ERROR(EIO);
4121 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4122 *last_block = 0;
4123 return 0;
4124 }
4125 ifp = XFS_IFORK_PTR(ip, whichfork);
4126 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4127 (error = xfs_iread_extents(tp, ip, whichfork)))
4128 return error;
4129 bno = *last_block - 1;
4130 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4131 &prev);
4132 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4133 if (prev.br_startoff == NULLFILEOFF)
4134 *last_block = 0;
4135 else
4136 *last_block = prev.br_startoff + prev.br_blockcount;
4137 }
4138 /*
4139 * Otherwise *last_block is already the right answer.
4140 */
4141 return 0;
4142}
4143
4144/*
4145 * Returns the file-relative block number of the first block past eof in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004146 * the file. This is not based on i_size, it is based on the extent records.
4147 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 */
4149int /* error */
4150xfs_bmap_last_offset(
4151 xfs_trans_t *tp, /* transaction pointer */
4152 xfs_inode_t *ip, /* incore inode */
4153 xfs_fileoff_t *last_block, /* last block */
4154 int whichfork) /* data or attr fork */
4155{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004156 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 int error; /* error return value */
4158 xfs_ifork_t *ifp; /* inode fork pointer */
4159 xfs_extnum_t nextents; /* number of extent entries */
4160
4161 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4162 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4163 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4164 return XFS_ERROR(EIO);
4165 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4166 *last_block = 0;
4167 return 0;
4168 }
4169 ifp = XFS_IFORK_PTR(ip, whichfork);
4170 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4171 (error = xfs_iread_extents(tp, ip, whichfork)))
4172 return error;
4173 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4174 if (!nextents) {
4175 *last_block = 0;
4176 return 0;
4177 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004178 ep = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4180 return 0;
4181}
4182
4183/*
4184 * Returns whether the selected fork of the inode has exactly one
4185 * block or not. For the data fork we check this matches di_size,
4186 * implying the file's range is 0..bsize-1.
4187 */
4188int /* 1=>1 block, 0=>otherwise */
4189xfs_bmap_one_block(
4190 xfs_inode_t *ip, /* incore inode */
4191 int whichfork) /* data or attr fork */
4192{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004193 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 xfs_ifork_t *ifp; /* inode fork pointer */
4195 int rval; /* return value */
4196 xfs_bmbt_irec_t s; /* internal version of extent */
4197
4198#ifndef DEBUG
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004199 if (whichfork == XFS_DATA_FORK) {
4200 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4201 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4202 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204#endif /* !DEBUG */
4205 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4206 return 0;
4207 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4208 return 0;
4209 ifp = XFS_IFORK_PTR(ip, whichfork);
4210 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004211 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 xfs_bmbt_get_all(ep, &s);
4213 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4214 if (rval && whichfork == XFS_DATA_FORK)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004215 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 return rval;
4217}
4218
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004219STATIC int
4220xfs_bmap_sanity_check(
4221 struct xfs_mount *mp,
4222 struct xfs_buf *bp,
4223 int level)
4224{
4225 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4226
4227 if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
4228 be16_to_cpu(block->bb_level) != level ||
4229 be16_to_cpu(block->bb_numrecs) == 0 ||
4230 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4231 return 0;
4232 return 1;
4233}
4234
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235/*
4236 * Read in the extents to if_extents.
4237 * All inode fields are set up by caller, we just traverse the btree
4238 * and copy the records in. If the file system cannot contain unwritten
4239 * extents, the records are checked for no "state" flags.
4240 */
4241int /* error */
4242xfs_bmap_read_extents(
4243 xfs_trans_t *tp, /* transaction pointer */
4244 xfs_inode_t *ip, /* incore inode */
4245 int whichfork) /* data or attr fork */
4246{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004247 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 xfs_fsblock_t bno; /* block # of "block" */
4249 xfs_buf_t *bp; /* buffer for "block" */
4250 int error; /* error return value */
4251 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 xfs_extnum_t i, j; /* index into the extents list */
4253 xfs_ifork_t *ifp; /* fork structure */
4254 int level; /* btree level, for checking */
4255 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10004256 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 /* REFERENCED */
4258 xfs_extnum_t room; /* number of entries there's room for */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
4260 bno = NULLFSBLOCK;
4261 mp = ip->i_mount;
4262 ifp = XFS_IFORK_PTR(ip, whichfork);
4263 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4264 XFS_EXTFMT_INODE(ip);
4265 block = ifp->if_broot;
4266 /*
4267 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4268 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004269 level = be16_to_cpu(block->bb_level);
4270 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11004271 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004272 bno = be64_to_cpu(*pp);
4273 ASSERT(bno != NULLDFSBNO);
4274 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4275 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 /*
4277 * Go down the tree until leaf level is reached, following the first
4278 * pointer (leftmost) at each level.
4279 */
4280 while (level-- > 0) {
4281 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4282 XFS_BMAP_BTREE_REF)))
4283 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004284 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004286 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 error0);
4288 if (level == 0)
4289 break;
Christoph Hellwig136341b2008-10-30 17:11:40 +11004290 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004291 bno = be64_to_cpu(*pp);
4292 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 xfs_trans_brelse(tp, bp);
4294 }
4295 /*
4296 * Here with bp and block set to the leftmost leaf node in the tree.
4297 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004298 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 i = 0;
4300 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004301 * Loop over all leaf nodes. Copy information to the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 */
4303 for (;;) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004304 xfs_bmbt_rec_t *frp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 xfs_fsblock_t nextbno;
4306 xfs_extnum_t num_recs;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004307 xfs_extnum_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
4309
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004310 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 if (unlikely(i + num_recs > room)) {
4312 ASSERT(i + num_recs <= room);
Nathan Scott3762ec62006-01-12 10:29:53 +11004313 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4314 "corrupt dinode %Lu, (btree extents).",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 (unsigned long long) ip->i_ino);
4316 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4317 XFS_ERRLEVEL_LOW,
4318 ip->i_mount);
4319 goto error0;
4320 }
4321 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004322 xfs_bmap_sanity_check(mp, bp, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 error0);
4324 /*
4325 * Read-ahead the next leaf block, if any.
4326 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004327 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 if (nextbno != NULLFSBLOCK)
4329 xfs_btree_reada_bufl(mp, nextbno, 1);
4330 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004331 * Copy records into the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11004333 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004334 start = i;
4335 for (j = 0; j < num_recs; j++, i++, frp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004336 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10004337 trp->l0 = be64_to_cpu(frp->l0);
4338 trp->l1 = be64_to_cpu(frp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 }
4340 if (exntf == XFS_EXTFMT_NOSTATE) {
4341 /*
4342 * Check all attribute bmap btree records and
4343 * any "older" data bmap btree records for a
4344 * set bit in the "extent flag" position.
4345 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004346 if (unlikely(xfs_check_nostate_extents(ifp,
4347 start, num_recs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4349 XFS_ERRLEVEL_LOW,
4350 ip->i_mount);
4351 goto error0;
4352 }
4353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 xfs_trans_brelse(tp, bp);
4355 bno = nextbno;
4356 /*
4357 * If we've reached the end, stop.
4358 */
4359 if (bno == NULLFSBLOCK)
4360 break;
4361 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4362 XFS_BMAP_BTREE_REF)))
4363 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004364 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004366 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
Eric Sandeen3a59c942007-07-11 11:09:47 +10004368 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 return 0;
4370error0:
4371 xfs_trans_brelse(tp, bp);
4372 return XFS_ERROR(EFSCORRUPTED);
4373}
4374
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004375#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004377 * Add bmap trace insert entries for all the contents of the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 */
4379void
4380xfs_bmap_trace_exlist(
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 xfs_inode_t *ip, /* incore inode pointer */
4382 xfs_extnum_t cnt, /* count of entries in the list */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004383 int whichfork, /* data or attr fork */
4384 unsigned long caller_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004386 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004388 int state = 0;
4389
4390 if (whichfork == XFS_ATTR_FORK)
4391 state |= BMAP_ATTRFORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
4393 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004394 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004395 for (idx = 0; idx < cnt; idx++)
4396 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399/*
4400 * Validate that the bmbt_irecs being returned from bmapi are valid
4401 * given the callers original parameters. Specifically check the
4402 * ranges of the returned irecs to ensure that they only extent beyond
4403 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4404 */
4405STATIC void
4406xfs_bmap_validate_ret(
4407 xfs_fileoff_t bno,
4408 xfs_filblks_t len,
4409 int flags,
4410 xfs_bmbt_irec_t *mval,
4411 int nmap,
4412 int ret_nmap)
4413{
4414 int i; /* index to map values */
4415
4416 ASSERT(ret_nmap <= nmap);
4417
4418 for (i = 0; i < ret_nmap; i++) {
4419 ASSERT(mval[i].br_blockcount > 0);
4420 if (!(flags & XFS_BMAPI_ENTIRE)) {
4421 ASSERT(mval[i].br_startoff >= bno);
4422 ASSERT(mval[i].br_blockcount <= len);
4423 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4424 bno + len);
4425 } else {
4426 ASSERT(mval[i].br_startoff < bno + len);
4427 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4428 bno);
4429 }
4430 ASSERT(i == 0 ||
4431 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4432 mval[i].br_startoff);
4433 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4434 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4435 mval[i].br_startblock != HOLESTARTBLOCK);
4436 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4437 mval[i].br_state == XFS_EXT_UNWRITTEN);
4438 }
4439}
4440#endif /* DEBUG */
4441
4442
4443/*
4444 * Map file blocks to filesystem blocks.
4445 * File range is given by the bno/len pair.
4446 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4447 * into a hole or past eof.
4448 * Only allocates blocks from a single allocation group,
4449 * to avoid locking problems.
4450 * The returned value in "firstblock" from the first call in a transaction
4451 * must be remembered and presented to subsequent calls in "firstblock".
4452 * An upper bound for the number of blocks to be allocated is supplied to
4453 * the first call in "total"; if no allocation group has that many free
4454 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4455 */
4456int /* error */
4457xfs_bmapi(
4458 xfs_trans_t *tp, /* transaction pointer */
4459 xfs_inode_t *ip, /* incore inode */
4460 xfs_fileoff_t bno, /* starting file offs. mapped */
4461 xfs_filblks_t len, /* length to map in file */
4462 int flags, /* XFS_BMAPI_... */
4463 xfs_fsblock_t *firstblock, /* first allocated block
4464 controls a.g. for allocs */
4465 xfs_extlen_t total, /* total blocks needed */
4466 xfs_bmbt_irec_t *mval, /* output: map values */
4467 int *nmap, /* i/o: mval size/count */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004468 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4469 xfs_extdelta_t *delta) /* o: change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470{
4471 xfs_fsblock_t abno; /* allocated block number */
4472 xfs_extlen_t alen; /* allocated extent length */
4473 xfs_fileoff_t aoff; /* allocated file offset */
Dave Chinnerf0a0eaa2010-01-20 10:50:06 +11004474 xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 xfs_btree_cur_t *cur; /* bmap btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 xfs_fileoff_t end; /* end of mapped file region */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004477 int eof; /* we've hit the end of extents */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004478 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004480 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 xfs_ifork_t *ifp; /* inode fork pointer */
4482 xfs_extlen_t indlen; /* indirect blocks length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 xfs_extnum_t lastx; /* last useful extent number */
4484 int logflags; /* flags for transaction logging */
4485 xfs_extlen_t minleft; /* min blocks left after allocation */
4486 xfs_extlen_t minlen; /* min allocation size */
4487 xfs_mount_t *mp; /* xfs mount structure */
4488 int n; /* current extent index */
Malcolm Parsons9da096f2009-03-29 09:55:42 +02004489 int nallocs; /* number of extents alloc'd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 xfs_extnum_t nextents; /* number of extents in file */
4491 xfs_fileoff_t obno; /* old block number (offset) */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004492 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 int tmp_logflags; /* temp flags holder */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004494 int whichfork; /* data or attr fork */
4495 char inhole; /* current location is hole in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 char wasdelay; /* old extent was delayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 char wr; /* this is a write request */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004498 char rt; /* this is a realtime file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499#ifdef DEBUG
4500 xfs_fileoff_t orig_bno; /* original block number value */
4501 int orig_flags; /* original flags arg value */
4502 xfs_filblks_t orig_len; /* original value of len arg */
4503 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4504 int orig_nmap; /* original value of *nmap */
4505
4506 orig_bno = bno;
4507 orig_len = len;
4508 orig_flags = flags;
4509 orig_mval = mval;
4510 orig_nmap = *nmap;
4511#endif
4512 ASSERT(*nmap >= 1);
4513 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4514 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4515 XFS_ATTR_FORK : XFS_DATA_FORK;
4516 mp = ip->i_mount;
4517 if (unlikely(XFS_TEST_ERROR(
4518 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4519 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4520 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4521 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4522 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4523 return XFS_ERROR(EFSCORRUPTED);
4524 }
4525 if (XFS_FORCED_SHUTDOWN(mp))
4526 return XFS_ERROR(EIO);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004527 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 ifp = XFS_IFORK_PTR(ip, whichfork);
4529 ASSERT(ifp->if_ext_max ==
4530 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4531 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4532 XFS_STATS_INC(xs_blk_mapw);
4533 else
4534 XFS_STATS_INC(xs_blk_mapr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 /*
Nathan Scott39269e22006-03-14 13:33:50 +11004536 * IGSTATE flag is used to combine extents which
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 * differ only due to the state of the extents.
4538 * This technique is used from xfs_getbmap()
4539 * when the caller does not wish to see the
4540 * separation (which is the default).
4541 *
4542 * This technique is also used when writing a
4543 * buffer which has been partially written,
4544 * (usually by being flushed during a chunkread),
4545 * to ensure one write takes place. This also
4546 * prevents a change in the xfs inode extents at
4547 * this time, intentionally. This change occurs
4548 * on completion of the write operation, in
4549 * xfs_strat_comp(), where the xfs_bmapi() call
4550 * is transactioned, and the extents combined.
4551 */
Nathan Scott39269e22006-03-14 13:33:50 +11004552 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4553 wr = 0; /* no allocations are allowed */
4554 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 logflags = 0;
4556 nallocs = 0;
4557 cur = NULL;
4558 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4559 ASSERT(wr && tp);
4560 if ((error = xfs_bmap_local_to_extents(tp, ip,
4561 firstblock, total, &logflags, whichfork)))
4562 goto error0;
4563 }
4564 if (wr && *firstblock == NULLFSBLOCK) {
4565 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Christoph Hellwig16259e72005-11-02 15:11:25 +11004566 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 else
4568 minleft = 1;
4569 } else
4570 minleft = 0;
4571 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4572 (error = xfs_iread_extents(tp, ip, whichfork)))
4573 goto error0;
4574 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4575 &prev);
4576 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4577 n = 0;
4578 end = bno + len;
4579 obno = bno;
4580 bma.ip = NULL;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004581 if (delta) {
4582 delta->xed_startoff = NULLFILEOFF;
4583 delta->xed_blockcount = 0;
4584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 while (bno < end && n < *nmap) {
4586 /*
4587 * Reading past eof, act as though there's a hole
4588 * up to end.
4589 */
4590 if (eof && !wr)
4591 got.br_startoff = end;
4592 inhole = eof || got.br_startoff > bno;
Nathan Scott39269e22006-03-14 13:33:50 +11004593 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06004594 isnullstartblock(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 /*
4596 * First, deal with the hole before the allocated space
4597 * that we found, if any.
4598 */
4599 if (wr && (inhole || wasdelay)) {
4600 /*
4601 * For the wasdelay case, we could also just
4602 * allocate the stuff asked for in this bmap call
4603 * but that wouldn't be as good.
4604 */
Nathan Scott39269e22006-03-14 13:33:50 +11004605 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 alen = (xfs_extlen_t)got.br_blockcount;
4607 aoff = got.br_startoff;
4608 if (lastx != NULLEXTNUM && lastx) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004609 ep = xfs_iext_get_ext(ifp, lastx - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 xfs_bmbt_get_all(ep, &prev);
4611 }
4612 } else if (wasdelay) {
4613 alen = (xfs_extlen_t)
4614 XFS_FILBLKS_MIN(len,
4615 (got.br_startoff +
4616 got.br_blockcount) - bno);
4617 aoff = bno;
4618 } else {
4619 alen = (xfs_extlen_t)
4620 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4621 if (!eof)
4622 alen = (xfs_extlen_t)
4623 XFS_FILBLKS_MIN(alen,
4624 got.br_startoff - bno);
4625 aoff = bno;
4626 }
Nathan Scott39269e22006-03-14 13:33:50 +11004627 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4628 if (flags & XFS_BMAPI_DELAY) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11004629 xfs_extlen_t extsz;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004630
4631 /* Figure out the extent size, adjust alen */
David Chinner957d0eb2007-06-18 16:50:37 +10004632 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004633 if (extsz) {
4634 error = xfs_bmap_extsize_align(mp,
4635 &got, &prev, extsz,
Nathan Scott39269e22006-03-14 13:33:50 +11004636 rt, eof,
4637 flags&XFS_BMAPI_DELAY,
4638 flags&XFS_BMAPI_CONVERT,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004639 &aoff, &alen);
4640 ASSERT(!error);
4641 }
4642
4643 if (rt)
4644 extsz = alen / mp->m_sb.sb_rextsize;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 /*
4647 * Make a transaction-less quota reservation for
4648 * delayed allocation blocks. This number gets
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004649 * adjusted later. We return if we haven't
4650 * allocated blocks already inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02004652 error = xfs_trans_reserve_quota_nblks(
4653 NULL, ip, (long)alen, 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004654 rt ? XFS_QMOPT_RES_RTBLKS :
Christoph Hellwig7d095252009-06-08 15:33:32 +02004655 XFS_QMOPT_RES_REGBLKS);
4656 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 if (n == 0) {
4658 *nmap = 0;
4659 ASSERT(cur == NULL);
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004660 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 }
4662 break;
4663 }
4664
4665 /*
4666 * Split changing sb for alen and indlen since
4667 * they could be coming from different places.
4668 */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004669 indlen = (xfs_extlen_t)
4670 xfs_bmap_worst_indlen(ip, alen);
4671 ASSERT(indlen > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
Nathan Scottdd9f4382006-01-11 15:28:28 +11004673 if (rt) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004674 error = xfs_mod_incore_sb(mp,
4675 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004676 -((int64_t)extsz), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004677 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004678 } else {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004679 error = xfs_mod_incore_sb(mp,
4680 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004681 -((int64_t)alen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004682 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004683 }
David Chinner3bdbfb12005-09-02 16:40:47 +10004684 if (!error) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004685 error = xfs_mod_incore_sb(mp,
4686 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004687 -((int64_t)indlen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004688 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004689 if (error && rt)
4690 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004691 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004692 (int64_t)extsz, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004693 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004694 else if (error)
4695 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004696 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004697 (int64_t)alen, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004698 XFS_BMAPI_RSVBLOCKS));
David Chinner3bdbfb12005-09-02 16:40:47 +10004699 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004700
4701 if (error) {
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004702 if (XFS_IS_QUOTA_ON(mp))
Nathan Scott06d10dd2005-06-21 15:48:47 +10004703 /* unreserve the blocks now */
Nathan Scottdd9f4382006-01-11 15:28:28 +11004704 (void)
Christoph Hellwig7d095252009-06-08 15:33:32 +02004705 xfs_trans_unreserve_quota_nblks(
4706 NULL, ip,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004707 (long)alen, 0, rt ?
4708 XFS_QMOPT_RES_RTBLKS :
4709 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 break;
4711 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004712
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 ip->i_delayed_blks += alen;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004714 abno = nullstartblock(indlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 } else {
4716 /*
4717 * If first time, allocate and fill in
4718 * once-only bma fields.
4719 */
4720 if (bma.ip == NULL) {
4721 bma.tp = tp;
4722 bma.ip = ip;
4723 bma.prevp = &prev;
4724 bma.gotp = &got;
4725 bma.total = total;
4726 bma.userdata = 0;
4727 }
4728 /* Indicate if this is the first user data
4729 * in the file, or just any user data.
4730 */
Nathan Scott39269e22006-03-14 13:33:50 +11004731 if (!(flags & XFS_BMAPI_METADATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 bma.userdata = (aoff == 0) ?
4733 XFS_ALLOC_INITIAL_USER_DATA :
4734 XFS_ALLOC_USERDATA;
4735 }
4736 /*
4737 * Fill in changeable bma fields.
4738 */
4739 bma.eof = eof;
4740 bma.firstblock = *firstblock;
4741 bma.alen = alen;
4742 bma.off = aoff;
Adrian Bunk72880262006-08-30 13:41:58 +10004743 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 bma.wasdel = wasdelay;
4745 bma.minlen = minlen;
4746 bma.low = flist->xbf_low;
4747 bma.minleft = minleft;
4748 /*
4749 * Only want to do the alignment at the
4750 * eof if it is userdata and allocation length
4751 * is larger than a stripe unit.
4752 */
4753 if (mp->m_dalign && alen >= mp->m_dalign &&
Nathan Scott39269e22006-03-14 13:33:50 +11004754 (!(flags & XFS_BMAPI_METADATA)) &&
4755 (whichfork == XFS_DATA_FORK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 if ((error = xfs_bmap_isaeof(ip, aoff,
4757 whichfork, &bma.aeof)))
4758 goto error0;
4759 } else
4760 bma.aeof = 0;
4761 /*
4762 * Call allocator.
4763 */
4764 if ((error = xfs_bmap_alloc(&bma)))
4765 goto error0;
4766 /*
4767 * Copy out result fields.
4768 */
4769 abno = bma.rval;
4770 if ((flist->xbf_low = bma.low))
4771 minleft = 0;
4772 alen = bma.alen;
4773 aoff = bma.off;
4774 ASSERT(*firstblock == NULLFSBLOCK ||
4775 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4776 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
4777 (flist->xbf_low &&
4778 XFS_FSB_TO_AGNO(mp, *firstblock) <
4779 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
4780 *firstblock = bma.firstblock;
4781 if (cur)
4782 cur->bc_private.b.firstblock =
4783 *firstblock;
4784 if (abno == NULLFSBLOCK)
4785 break;
4786 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11004787 cur = xfs_bmbt_init_cursor(mp, tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 ip, whichfork);
4789 cur->bc_private.b.firstblock =
4790 *firstblock;
4791 cur->bc_private.b.flist = flist;
4792 }
4793 /*
4794 * Bump the number of extents we've allocated
4795 * in this call.
4796 */
4797 nallocs++;
4798 }
4799 if (cur)
4800 cur->bc_private.b.flags =
4801 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
4802 got.br_startoff = aoff;
4803 got.br_startblock = abno;
4804 got.br_blockcount = alen;
4805 got.br_state = XFS_EXT_NORM; /* assume normal */
4806 /*
4807 * Determine state of extent, and the filesystem.
4808 * A wasdelay extent has been initialized, so
4809 * shouldn't be flagged as unwritten.
4810 */
Eric Sandeen62118702008-03-06 13:44:28 +11004811 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
4813 got.br_state = XFS_EXT_UNWRITTEN;
4814 }
4815 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004816 firstblock, flist, &tmp_logflags, delta,
4817 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 logflags |= tmp_logflags;
4819 if (error)
4820 goto error0;
4821 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004822 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4824 xfs_bmbt_get_all(ep, &got);
4825 ASSERT(got.br_startoff <= aoff);
4826 ASSERT(got.br_startoff + got.br_blockcount >=
4827 aoff + alen);
4828#ifdef DEBUG
Nathan Scott39269e22006-03-14 13:33:50 +11004829 if (flags & XFS_BMAPI_DELAY) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06004830 ASSERT(isnullstartblock(got.br_startblock));
4831 ASSERT(startblockval(got.br_startblock) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 }
4833 ASSERT(got.br_state == XFS_EXT_NORM ||
4834 got.br_state == XFS_EXT_UNWRITTEN);
4835#endif
4836 /*
4837 * Fall down into the found allocated space case.
4838 */
4839 } else if (inhole) {
4840 /*
4841 * Reading in a hole.
4842 */
4843 mval->br_startoff = bno;
4844 mval->br_startblock = HOLESTARTBLOCK;
4845 mval->br_blockcount =
4846 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4847 mval->br_state = XFS_EXT_NORM;
4848 bno += mval->br_blockcount;
4849 len -= mval->br_blockcount;
4850 mval++;
4851 n++;
4852 continue;
4853 }
4854 /*
4855 * Then deal with the allocated space we found.
4856 */
4857 ASSERT(ep != NULL);
Nathan Scott39269e22006-03-14 13:33:50 +11004858 if (!(flags & XFS_BMAPI_ENTIRE) &&
4859 (got.br_startoff + got.br_blockcount > obno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 if (obno > bno)
4861 bno = obno;
4862 ASSERT((bno >= obno) || (n == 0));
4863 ASSERT(bno < end);
4864 mval->br_startoff = bno;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004865 if (isnullstartblock(got.br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11004866 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 mval->br_startblock = DELAYSTARTBLOCK;
4868 } else
4869 mval->br_startblock =
4870 got.br_startblock +
4871 (bno - got.br_startoff);
4872 /*
4873 * Return the minimum of what we got and what we
4874 * asked for for the length. We can use the len
4875 * variable here because it is modified below
4876 * and we could have been there before coming
4877 * here if the first part of the allocation
4878 * didn't overlap what was asked for.
4879 */
4880 mval->br_blockcount =
4881 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
4882 (bno - got.br_startoff));
4883 mval->br_state = got.br_state;
4884 ASSERT(mval->br_blockcount <= len);
4885 } else {
4886 *mval = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004887 if (isnullstartblock(mval->br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11004888 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 mval->br_startblock = DELAYSTARTBLOCK;
4890 }
4891 }
4892
4893 /*
4894 * Check if writing previously allocated but
4895 * unwritten extents.
4896 */
4897 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
4898 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
4899 /*
4900 * Modify (by adding) the state flag, if writing.
4901 */
4902 ASSERT(mval->br_blockcount <= len);
4903 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11004904 cur = xfs_bmbt_init_cursor(mp,
4905 tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 cur->bc_private.b.firstblock =
4907 *firstblock;
4908 cur->bc_private.b.flist = flist;
4909 }
4910 mval->br_state = XFS_EXT_NORM;
4911 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004912 firstblock, flist, &tmp_logflags, delta,
4913 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 logflags |= tmp_logflags;
4915 if (error)
4916 goto error0;
4917 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004918 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4920 xfs_bmbt_get_all(ep, &got);
4921 /*
4922 * We may have combined previously unwritten
4923 * space with written space, so generate
4924 * another request.
4925 */
4926 if (mval->br_blockcount < len)
4927 continue;
4928 }
4929
Nathan Scott39269e22006-03-14 13:33:50 +11004930 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 ((mval->br_startoff + mval->br_blockcount) <= end));
Nathan Scott39269e22006-03-14 13:33:50 +11004932 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4933 (mval->br_blockcount <= len) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 (mval->br_startoff < obno));
4935 bno = mval->br_startoff + mval->br_blockcount;
4936 len = end - bno;
4937 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4938 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4939 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4940 ASSERT(mval->br_state == mval[-1].br_state);
4941 mval[-1].br_blockcount = mval->br_blockcount;
4942 mval[-1].br_state = mval->br_state;
4943 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4944 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4945 mval[-1].br_startblock != HOLESTARTBLOCK &&
4946 mval->br_startblock ==
4947 mval[-1].br_startblock + mval[-1].br_blockcount &&
Nathan Scott39269e22006-03-14 13:33:50 +11004948 ((flags & XFS_BMAPI_IGSTATE) ||
4949 mval[-1].br_state == mval->br_state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 ASSERT(mval->br_startoff ==
4951 mval[-1].br_startoff + mval[-1].br_blockcount);
4952 mval[-1].br_blockcount += mval->br_blockcount;
4953 } else if (n > 0 &&
4954 mval->br_startblock == DELAYSTARTBLOCK &&
4955 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4956 mval->br_startoff ==
4957 mval[-1].br_startoff + mval[-1].br_blockcount) {
4958 mval[-1].br_blockcount += mval->br_blockcount;
4959 mval[-1].br_state = mval->br_state;
4960 } else if (!((n == 0) &&
4961 ((mval->br_startoff + mval->br_blockcount) <=
4962 obno))) {
4963 mval++;
4964 n++;
4965 }
4966 /*
4967 * If we're done, stop now. Stop when we've allocated
4968 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4969 * the transaction may get too big.
4970 */
4971 if (bno >= end || n >= *nmap || nallocs >= *nmap)
4972 break;
4973 /*
4974 * Else go on to the next record.
4975 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004976 ep = xfs_iext_get_ext(ifp, ++lastx);
David Chinner4e5ae832007-06-05 16:24:15 +10004977 prev = got;
4978 if (lastx >= nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 eof = 1;
David Chinner4e5ae832007-06-05 16:24:15 +10004980 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 xfs_bmbt_get_all(ep, &got);
4982 }
4983 ifp->if_lastex = lastx;
4984 *nmap = n;
4985 /*
4986 * Transform from btree to extents, give it cur.
4987 */
4988 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4989 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4990 ASSERT(wr && cur);
4991 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4992 &tmp_logflags, whichfork);
4993 logflags |= tmp_logflags;
4994 if (error)
4995 goto error0;
4996 }
4997 ASSERT(ifp->if_ext_max ==
4998 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4999 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5000 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5001 error = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005002 if (delta && delta->xed_startoff != NULLFILEOFF) {
5003 /* A change was actually made.
5004 * Note that delta->xed_blockount is an offset at this
5005 * point and needs to be converted to a block count.
5006 */
5007 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5008 delta->xed_blockcount -= delta->xed_startoff;
5009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010error0:
5011 /*
5012 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005013 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005015 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005017 logflags &= ~xfs_ilog_fext(whichfork);
5018 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005020 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 /*
5022 * Log whatever the flags say, even if error. Otherwise we might miss
5023 * detecting a case where the data is changed, there's an error,
5024 * and it's not logged so we don't shutdown when we should.
5025 */
5026 if (logflags) {
5027 ASSERT(tp && wr);
5028 xfs_trans_log_inode(tp, ip, logflags);
5029 }
5030 if (cur) {
5031 if (!error) {
5032 ASSERT(*firstblock == NULLFSBLOCK ||
5033 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5034 XFS_FSB_TO_AGNO(mp,
5035 cur->bc_private.b.firstblock) ||
5036 (flist->xbf_low &&
5037 XFS_FSB_TO_AGNO(mp, *firstblock) <
5038 XFS_FSB_TO_AGNO(mp,
5039 cur->bc_private.b.firstblock)));
5040 *firstblock = cur->bc_private.b.firstblock;
5041 }
5042 xfs_btree_del_cursor(cur,
5043 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5044 }
5045 if (!error)
5046 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5047 orig_nmap, *nmap);
5048 return error;
5049}
5050
5051/*
5052 * Map file blocks to filesystem blocks, simple version.
5053 * One block (extent) only, read-only.
5054 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5055 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5056 * was set and all the others were clear.
5057 */
5058int /* error */
5059xfs_bmapi_single(
5060 xfs_trans_t *tp, /* transaction pointer */
5061 xfs_inode_t *ip, /* incore inode */
5062 int whichfork, /* data or attr fork */
5063 xfs_fsblock_t *fsb, /* output: mapped block */
5064 xfs_fileoff_t bno) /* starting file offs. mapped */
5065{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005066 int eof; /* we've hit the end of extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005068 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 xfs_ifork_t *ifp; /* inode fork pointer */
5070 xfs_extnum_t lastx; /* last useful extent number */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005071 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
5073 ifp = XFS_IFORK_PTR(ip, whichfork);
5074 if (unlikely(
5075 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5076 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5077 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5078 ip->i_mount);
5079 return XFS_ERROR(EFSCORRUPTED);
5080 }
5081 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5082 return XFS_ERROR(EIO);
5083 XFS_STATS_INC(xs_blk_mapr);
5084 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5085 (error = xfs_iread_extents(tp, ip, whichfork)))
5086 return error;
5087 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5088 &prev);
5089 /*
5090 * Reading past eof, act as though there's a hole
5091 * up to end.
5092 */
5093 if (eof || got.br_startoff > bno) {
5094 *fsb = NULLFSBLOCK;
5095 return 0;
5096 }
Eric Sandeen9d87c312009-01-14 23:22:07 -06005097 ASSERT(!isnullstartblock(got.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 ASSERT(bno < got.br_startoff + got.br_blockcount);
5099 *fsb = got.br_startblock + (bno - got.br_startoff);
5100 ifp->if_lastex = lastx;
5101 return 0;
5102}
5103
5104/*
5105 * Unmap (remove) blocks from a file.
5106 * If nexts is nonzero then the number of extents to remove is limited to
5107 * that value. If not all extents in the block range can be removed then
5108 * *done is set.
5109 */
5110int /* error */
5111xfs_bunmapi(
5112 xfs_trans_t *tp, /* transaction pointer */
5113 struct xfs_inode *ip, /* incore inode */
5114 xfs_fileoff_t bno, /* starting offset to unmap */
5115 xfs_filblks_t len, /* length to unmap in file */
5116 int flags, /* misc flags */
5117 xfs_extnum_t nexts, /* number of extents max */
5118 xfs_fsblock_t *firstblock, /* first allocated block
5119 controls a.g. for allocs */
5120 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005121 xfs_extdelta_t *delta, /* o: change made to incore
5122 extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 int *done) /* set if not done yet */
5124{
5125 xfs_btree_cur_t *cur; /* bmap btree cursor */
5126 xfs_bmbt_irec_t del; /* extent being deleted */
5127 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005128 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 int error; /* error return value */
5130 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005131 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 xfs_ifork_t *ifp; /* inode fork pointer */
5133 int isrt; /* freeing in rt area */
5134 xfs_extnum_t lastx; /* last extent index used */
5135 int logflags; /* transaction logging flags */
5136 xfs_extlen_t mod; /* rt extent offset */
5137 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005138 xfs_extnum_t nextents; /* number of file extents */
5139 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 xfs_fileoff_t start; /* first file offset deleted */
5141 int tmp_logflags; /* partial logging flags */
5142 int wasdel; /* was a delayed alloc extent */
5143 int whichfork; /* data or attribute fork */
5144 int rsvd; /* OK to allocate reserved blocks */
5145 xfs_fsblock_t sum;
5146
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005147 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5148
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5150 XFS_ATTR_FORK : XFS_DATA_FORK;
5151 ifp = XFS_IFORK_PTR(ip, whichfork);
5152 if (unlikely(
5153 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5154 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5155 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5156 ip->i_mount);
5157 return XFS_ERROR(EFSCORRUPTED);
5158 }
5159 mp = ip->i_mount;
5160 if (XFS_FORCED_SHUTDOWN(mp))
5161 return XFS_ERROR(EIO);
5162 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5163 ASSERT(len > 0);
5164 ASSERT(nexts >= 0);
5165 ASSERT(ifp->if_ext_max ==
5166 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5167 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5168 (error = xfs_iread_extents(tp, ip, whichfork)))
5169 return error;
5170 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5171 if (nextents == 0) {
5172 *done = 1;
5173 return 0;
5174 }
5175 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005176 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 start = bno;
5178 bno = start + len - 1;
5179 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5180 &prev);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005181 if (delta) {
5182 delta->xed_startoff = NULLFILEOFF;
5183 delta->xed_blockcount = 0;
5184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 /*
5186 * Check to see if the given block number is past the end of the
5187 * file, back up to the last block if so...
5188 */
5189 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005190 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 xfs_bmbt_get_all(ep, &got);
5192 bno = got.br_startoff + got.br_blockcount - 1;
5193 }
5194 logflags = 0;
5195 if (ifp->if_flags & XFS_IFBROOT) {
5196 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005197 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 cur->bc_private.b.firstblock = *firstblock;
5199 cur->bc_private.b.flist = flist;
5200 cur->bc_private.b.flags = 0;
5201 } else
5202 cur = NULL;
5203 extno = 0;
5204 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5205 (nexts == 0 || extno < nexts)) {
5206 /*
5207 * Is the found extent after a hole in which bno lives?
5208 * Just back up to the previous extent, if so.
5209 */
5210 if (got.br_startoff > bno) {
5211 if (--lastx < 0)
5212 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005213 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 xfs_bmbt_get_all(ep, &got);
5215 }
5216 /*
5217 * Is the last block of this extent before the range
5218 * we're supposed to delete? If so, we're done.
5219 */
5220 bno = XFS_FILEOFF_MIN(bno,
5221 got.br_startoff + got.br_blockcount - 1);
5222 if (bno < start)
5223 break;
5224 /*
5225 * Then deal with the (possibly delayed) allocated space
5226 * we found.
5227 */
5228 ASSERT(ep != NULL);
5229 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005230 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 if (got.br_startoff < start) {
5232 del.br_startoff = start;
5233 del.br_blockcount -= start - got.br_startoff;
5234 if (!wasdel)
5235 del.br_startblock += start - got.br_startoff;
5236 }
5237 if (del.br_startoff + del.br_blockcount > bno + 1)
5238 del.br_blockcount = bno + 1 - del.br_startoff;
5239 sum = del.br_startblock + del.br_blockcount;
5240 if (isrt &&
5241 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5242 /*
5243 * Realtime extent not lined up at the end.
5244 * The extent could have been split into written
5245 * and unwritten pieces, or we could just be
5246 * unmapping part of it. But we can't really
5247 * get rid of part of a realtime extent.
5248 */
5249 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005250 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 /*
5252 * This piece is unwritten, or we're not
5253 * using unwritten extents. Skip over it.
5254 */
5255 ASSERT(bno >= mod);
5256 bno -= mod > del.br_blockcount ?
5257 del.br_blockcount : mod;
5258 if (bno < got.br_startoff) {
5259 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005260 xfs_bmbt_get_all(xfs_iext_get_ext(
5261 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 }
5263 continue;
5264 }
5265 /*
5266 * It's written, turn it unwritten.
5267 * This is better than zeroing it.
5268 */
5269 ASSERT(del.br_state == XFS_EXT_NORM);
5270 ASSERT(xfs_trans_get_block_res(tp) > 0);
5271 /*
5272 * If this spans a realtime extent boundary,
5273 * chop it back to the start of the one we end at.
5274 */
5275 if (del.br_blockcount > mod) {
5276 del.br_startoff += del.br_blockcount - mod;
5277 del.br_startblock += del.br_blockcount - mod;
5278 del.br_blockcount = mod;
5279 }
5280 del.br_state = XFS_EXT_UNWRITTEN;
5281 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005282 firstblock, flist, &logflags, delta,
5283 XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 if (error)
5285 goto error0;
5286 goto nodelete;
5287 }
5288 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5289 /*
5290 * Realtime extent is lined up at the end but not
5291 * at the front. We'll get rid of full extents if
5292 * we can.
5293 */
5294 mod = mp->m_sb.sb_rextsize - mod;
5295 if (del.br_blockcount > mod) {
5296 del.br_blockcount -= mod;
5297 del.br_startoff += mod;
5298 del.br_startblock += mod;
5299 } else if ((del.br_startoff == start &&
5300 (del.br_state == XFS_EXT_UNWRITTEN ||
5301 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005302 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 /*
5304 * Can't make it unwritten. There isn't
5305 * a full extent here so just skip it.
5306 */
5307 ASSERT(bno >= del.br_blockcount);
5308 bno -= del.br_blockcount;
5309 if (bno < got.br_startoff) {
5310 if (--lastx >= 0)
5311 xfs_bmbt_get_all(--ep, &got);
5312 }
5313 continue;
5314 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5315 /*
5316 * This one is already unwritten.
5317 * It must have a written left neighbor.
5318 * Unwrite the killed part of that one and
5319 * try again.
5320 */
5321 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005322 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5323 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005325 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 ASSERT(del.br_startblock ==
5327 prev.br_startblock + prev.br_blockcount);
5328 if (prev.br_startoff < start) {
5329 mod = start - prev.br_startoff;
5330 prev.br_blockcount -= mod;
5331 prev.br_startblock += mod;
5332 prev.br_startoff = start;
5333 }
5334 prev.br_state = XFS_EXT_UNWRITTEN;
5335 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5336 &prev, firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005337 delta, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 if (error)
5339 goto error0;
5340 goto nodelete;
5341 } else {
5342 ASSERT(del.br_state == XFS_EXT_NORM);
5343 del.br_state = XFS_EXT_UNWRITTEN;
5344 error = xfs_bmap_add_extent(ip, lastx, &cur,
5345 &del, firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005346 delta, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 if (error)
5348 goto error0;
5349 goto nodelete;
5350 }
5351 }
5352 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005353 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005354 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005355 if (isrt) {
5356 xfs_filblks_t rtexts;
5357
5358 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5359 do_div(rtexts, mp->m_sb.sb_rextsize);
5360 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005361 (int64_t)rtexts, rsvd);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005362 (void)xfs_trans_reserve_quota_nblks(NULL,
5363 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005364 XFS_QMOPT_RES_RTBLKS);
5365 } else {
5366 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005367 (int64_t)del.br_blockcount, rsvd);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005368 (void)xfs_trans_reserve_quota_nblks(NULL,
5369 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 ip->i_delayed_blks -= del.br_blockcount;
5373 if (cur)
5374 cur->bc_private.b.flags |=
5375 XFS_BTCUR_BPRV_WASDEL;
5376 } else if (cur)
5377 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5378 /*
5379 * If it's the case where the directory code is running
5380 * with no block reservation, and the deleted block is in
5381 * the middle of its extent, and the resulting insert
5382 * of an extent would cause transformation to btree format,
5383 * then reject it. The calling code will then swap
5384 * blocks around instead.
5385 * We have to do this now, rather than waiting for the
5386 * conversion to btree format, since the transaction
5387 * will be dirty.
5388 */
5389 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5390 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5391 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5392 del.br_startoff > got.br_startoff &&
5393 del.br_startoff + del.br_blockcount <
5394 got.br_startoff + got.br_blockcount) {
5395 error = XFS_ERROR(ENOSPC);
5396 goto error0;
5397 }
5398 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005399 &tmp_logflags, delta, whichfork, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 logflags |= tmp_logflags;
5401 if (error)
5402 goto error0;
5403 bno = del.br_startoff - 1;
5404nodelete:
5405 lastx = ifp->if_lastex;
5406 /*
5407 * If not done go on to the next (previous) record.
5408 * Reset ep in case the extents array was re-alloced.
5409 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005410 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5412 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5413 xfs_bmbt_get_startoff(ep) > bno) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005414 if (--lastx >= 0)
5415 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 }
5417 if (lastx >= 0)
5418 xfs_bmbt_get_all(ep, &got);
5419 extno++;
5420 }
5421 }
5422 ifp->if_lastex = lastx;
5423 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5424 ASSERT(ifp->if_ext_max ==
5425 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5426 /*
5427 * Convert to a btree if necessary.
5428 */
5429 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5430 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5431 ASSERT(cur == NULL);
5432 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5433 &cur, 0, &tmp_logflags, whichfork);
5434 logflags |= tmp_logflags;
5435 if (error)
5436 goto error0;
5437 }
5438 /*
5439 * transform from btree to extents, give it cur
5440 */
5441 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5442 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5443 ASSERT(cur != NULL);
5444 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5445 whichfork);
5446 logflags |= tmp_logflags;
5447 if (error)
5448 goto error0;
5449 }
5450 /*
5451 * transform from extents to local?
5452 */
5453 ASSERT(ifp->if_ext_max ==
5454 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5455 error = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005456 if (delta && delta->xed_startoff != NULLFILEOFF) {
5457 /* A change was actually made.
5458 * Note that delta->xed_blockount is an offset at this
5459 * point and needs to be converted to a block count.
5460 */
5461 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5462 delta->xed_blockcount -= delta->xed_startoff;
5463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464error0:
5465 /*
5466 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005467 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005469 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005471 logflags &= ~xfs_ilog_fext(whichfork);
5472 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005474 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 /*
5476 * Log inode even in the error case, if the transaction
5477 * is dirty we'll need to shut down the filesystem.
5478 */
5479 if (logflags)
5480 xfs_trans_log_inode(tp, ip, logflags);
5481 if (cur) {
5482 if (!error) {
5483 *firstblock = cur->bc_private.b.firstblock;
5484 cur->bc_private.b.allocated = 0;
5485 }
5486 xfs_btree_del_cursor(cur,
5487 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5488 }
5489 return error;
5490}
5491
5492/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005493 * returns 1 for success, 0 if we failed to map the extent.
5494 */
5495STATIC int
5496xfs_getbmapx_fix_eof_hole(
5497 xfs_inode_t *ip, /* xfs incore inode pointer */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005498 struct getbmapx *out, /* output structure */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005499 int prealloced, /* this is a file with
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005500 * preallocated data space */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005501 __int64_t end, /* last block requested */
5502 xfs_fsblock_t startblock)
5503{
5504 __int64_t fixlen;
5505 xfs_mount_t *mp; /* file system mount point */
Eric Sandeen5af317c2008-11-28 14:23:35 +11005506 xfs_ifork_t *ifp; /* inode fork pointer */
5507 xfs_extnum_t lastx; /* last extent pointer */
5508 xfs_fileoff_t fileblock;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005509
5510 if (startblock == HOLESTARTBLOCK) {
5511 mp = ip->i_mount;
5512 out->bmv_block = -1;
5513 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5514 fixlen -= out->bmv_offset;
5515 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5516 /* Came to hole at EOF. Trim it. */
5517 if (fixlen <= 0)
5518 return 0;
5519 out->bmv_length = fixlen;
5520 }
5521 } else {
Eric Sandeen5af317c2008-11-28 14:23:35 +11005522 if (startblock == DELAYSTARTBLOCK)
5523 out->bmv_block = -2;
5524 else
Eric Sandeen9d87c312009-01-14 23:22:07 -06005525 out->bmv_block = xfs_fsb_to_db(ip, startblock);
Eric Sandeen5af317c2008-11-28 14:23:35 +11005526 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5527 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5528 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5529 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5530 out->bmv_oflags |= BMV_OF_LAST;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005531 }
5532
5533 return 1;
5534}
5535
5536/*
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005537 * Get inode's extents as described in bmv, and format for output.
5538 * Calls formatter to fill the user's buffer until all extents
5539 * are mapped, until the passed-in bmv->bmv_count slots have
5540 * been filled, or until the formatter short-circuits the loop,
5541 * if it is tracking filled-in extents on its own.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 */
5543int /* error code */
5544xfs_getbmap(
Christoph Hellwig993386c12007-08-28 16:12:30 +10005545 xfs_inode_t *ip,
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005546 struct getbmapx *bmv, /* user bmap structure */
5547 xfs_bmap_format_t formatter, /* format to user */
5548 void *arg) /* formatter arg */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549{
5550 __int64_t bmvend; /* last block requested */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005551 int error = 0; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 __int64_t fixlen; /* length for -1 case */
5553 int i; /* extent number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 int lock; /* lock state */
5555 xfs_bmbt_irec_t *map; /* buffer for user's data */
5556 xfs_mount_t *mp; /* file system mount point */
5557 int nex; /* # of user extents can do */
5558 int nexleft; /* # of user extents left */
5559 int subnex; /* # of bmapi's can do */
5560 int nmap; /* number of map entries */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005561 struct getbmapx *out; /* output structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 int whichfork; /* data or attr fork */
5563 int prealloced; /* this is a file with
5564 * preallocated data space */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005565 int iflags; /* interface flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 int bmapi_flags; /* flags for xfs_bmapi */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005567 int cur_ext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 mp = ip->i_mount;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005570 iflags = bmv->bmv_iflags;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005571 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 if (whichfork == XFS_ATTR_FORK) {
5574 if (XFS_IFORK_Q(ip)) {
5575 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5576 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5577 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5578 return XFS_ERROR(EINVAL);
5579 } else if (unlikely(
5580 ip->i_d.di_aformat != 0 &&
5581 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5582 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5583 ip->i_mount);
5584 return XFS_ERROR(EFSCORRUPTED);
5585 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005586
5587 prealloced = 0;
5588 fixlen = 1LL << 32;
5589 } else {
5590 /*
5591 * If the BMV_IF_NO_DMAPI_READ interface bit specified, do
5592 * not generate a DMAPI read event. Otherwise, if the
5593 * DM_EVENT_READ bit is set for the file, generate a read
5594 * event in order that the DMAPI application may do its thing
5595 * before we return the extents. Usually this means restoring
5596 * user file data to regions of the file that look like holes.
5597 *
5598 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5599 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5600 * If this were not true, callers of ioctl(XFS_IOC_GETBMAP)
5601 * could misinterpret holes in a DMAPI file as true holes,
5602 * when in fact they may represent offline user data.
5603 */
5604 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) &&
5605 !(iflags & BMV_IF_NO_DMAPI_READ)) {
5606 error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip,
5607 0, 0, 0, NULL);
5608 if (error)
5609 return XFS_ERROR(error);
5610 }
5611
5612 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5613 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5614 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5615 return XFS_ERROR(EINVAL);
5616
David Chinner957d0eb2007-06-18 16:50:37 +10005617 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005618 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 prealloced = 1;
5620 fixlen = XFS_MAXIOFFSET(mp);
5621 } else {
5622 prealloced = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005623 fixlen = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 }
5626
5627 if (bmv->bmv_length == -1) {
5628 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005629 bmv->bmv_length =
5630 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5631 } else if (bmv->bmv_length == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 bmv->bmv_entries = 0;
5633 return 0;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005634 } else if (bmv->bmv_length < 0) {
5635 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005637
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 nex = bmv->bmv_count - 1;
5639 if (nex <= 0)
5640 return XFS_ERROR(EINVAL);
5641 bmvend = bmv->bmv_offset + bmv->bmv_length;
5642
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005643
5644 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5645 return XFS_ERROR(ENOMEM);
5646 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5647 if (!out)
5648 return XFS_ERROR(ENOMEM);
5649
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005651 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5652 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5653 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5654 if (error)
5655 goto out_unlock_iolock;
Niv Sardie12070a2008-03-06 13:43:03 +11005656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005658 ASSERT(ip->i_delayed_blks == 0);
5659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660
5661 lock = xfs_ilock_map_shared(ip);
5662
5663 /*
5664 * Don't let nex be bigger than the number of extents
5665 * we can have assuming alternating holes and real extents.
5666 */
5667 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5668 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5669
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005670 bmapi_flags = xfs_bmapi_aflag(whichfork);
5671 if (!(iflags & BMV_IF_PREALLOC))
5672 bmapi_flags |= XFS_BMAPI_IGSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673
5674 /*
5675 * Allocate enough space to handle "subnex" maps at a time.
5676 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005677 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 subnex = 16;
Christoph Hellwigca35dcd2009-07-18 18:14:54 -04005679 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005680 if (!map)
5681 goto out_unlock_ilock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
5683 bmv->bmv_entries = 0;
5684
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005685 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5686 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5687 error = 0;
5688 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 }
5690
5691 nexleft = nex;
5692
5693 do {
5694 nmap = (nexleft > subnex) ? subnex : nexleft;
5695 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5696 XFS_BB_TO_FSB(mp, bmv->bmv_length),
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005697 bmapi_flags, NULL, 0, map, &nmap,
5698 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 if (error)
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005700 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 ASSERT(nmap <= subnex);
5702
5703 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005704 out[cur_ext].bmv_oflags = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005705 if (map[i].br_state == XFS_EXT_UNWRITTEN)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005706 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005707 else if (map[i].br_startblock == DELAYSTARTBLOCK)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005708 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5709 out[cur_ext].bmv_offset =
5710 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5711 out[cur_ext].bmv_length =
5712 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5713 out[cur_ext].bmv_unused1 = 0;
5714 out[cur_ext].bmv_unused2 = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005715 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5716 (map[i].br_startblock != DELAYSTARTBLOCK));
Yingping Lu9af0a702005-11-02 15:09:54 +11005717 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005718 whichfork == XFS_ATTR_FORK) {
5719 /* came to the end of attribute fork */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005720 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005721 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005723
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005724 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5725 prealloced, bmvend,
5726 map[i].br_startblock))
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005727 goto out_free_map;
5728
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005729 nexleft--;
5730 bmv->bmv_offset =
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005731 out[cur_ext].bmv_offset +
5732 out[cur_ext].bmv_length;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005733 bmv->bmv_length =
5734 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
5735 bmv->bmv_entries++;
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005736 cur_ext++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 }
5738 } while (nmap && nexleft && bmv->bmv_length);
5739
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005740 out_free_map:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10005741 kmem_free(map);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005742 out_unlock_ilock:
5743 xfs_iunlock_map_shared(ip, lock);
5744 out_unlock_iolock:
5745 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005746
5747 for (i = 0; i < cur_ext; i++) {
5748 int full = 0; /* user array is full */
5749
5750 /* format results & advance arg */
5751 error = formatter(&arg, &out[i], &full);
5752 if (error || full)
5753 break;
5754 }
5755
Felix Blyakher7747a0b2009-06-11 17:07:28 -05005756 kmem_free(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 return error;
5758}
5759
5760/*
5761 * Check the last inode extent to determine whether this allocation will result
5762 * in blocks being allocated at the end of the file. When we allocate new data
5763 * blocks at the end of the file which do not start at the previous data block,
5764 * we will try to align the new blocks at stripe unit boundaries.
5765 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10005766STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767xfs_bmap_isaeof(
5768 xfs_inode_t *ip, /* incore inode pointer */
5769 xfs_fileoff_t off, /* file offset in fsblocks */
5770 int whichfork, /* data or attribute fork */
5771 char *aeof) /* return value */
5772{
5773 int error; /* error return value */
5774 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005775 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005776 xfs_extnum_t nextents; /* number of file extents */
5777 xfs_bmbt_irec_t s; /* expanded extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778
5779 ASSERT(whichfork == XFS_DATA_FORK);
5780 ifp = XFS_IFORK_PTR(ip, whichfork);
5781 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5782 (error = xfs_iread_extents(NULL, ip, whichfork)))
5783 return error;
5784 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5785 if (nextents == 0) {
5786 *aeof = 1;
5787 return 0;
5788 }
5789 /*
5790 * Go to the last extent
5791 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005792 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 xfs_bmbt_get_all(lastrec, &s);
5794 /*
5795 * Check we are allocating in the last extent (for delayed allocations)
5796 * or past the last extent for non-delayed allocations.
5797 */
5798 *aeof = (off >= s.br_startoff &&
5799 off < s.br_startoff + s.br_blockcount &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06005800 isnullstartblock(s.br_startblock)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 off >= s.br_startoff + s.br_blockcount;
5802 return 0;
5803}
5804
5805/*
5806 * Check if the endoff is outside the last extent. If so the caller will grow
5807 * the allocation to a stripe unit boundary.
5808 */
5809int /* error */
5810xfs_bmap_eof(
5811 xfs_inode_t *ip, /* incore inode pointer */
5812 xfs_fileoff_t endoff, /* file offset in fsblocks */
5813 int whichfork, /* data or attribute fork */
5814 int *eof) /* result value */
5815{
5816 xfs_fsblock_t blockcount; /* extent block count */
5817 int error; /* error return value */
5818 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005819 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005820 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 xfs_fileoff_t startoff; /* extent starting file offset */
5822
5823 ASSERT(whichfork == XFS_DATA_FORK);
5824 ifp = XFS_IFORK_PTR(ip, whichfork);
5825 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5826 (error = xfs_iread_extents(NULL, ip, whichfork)))
5827 return error;
5828 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5829 if (nextents == 0) {
5830 *eof = 1;
5831 return 0;
5832 }
5833 /*
5834 * Go to the last extent
5835 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005836 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 startoff = xfs_bmbt_get_startoff(lastrec);
5838 blockcount = xfs_bmbt_get_blockcount(lastrec);
5839 *eof = endoff >= startoff + blockcount;
5840 return 0;
5841}
5842
5843#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844STATIC
5845xfs_buf_t *
5846xfs_bmap_get_bp(
5847 xfs_btree_cur_t *cur,
5848 xfs_fsblock_t bno)
5849{
5850 int i;
5851 xfs_buf_t *bp;
5852
5853 if (!cur)
5854 return(NULL);
5855
5856 bp = NULL;
5857 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5858 bp = cur->bc_bufs[i];
5859 if (!bp) break;
5860 if (XFS_BUF_ADDR(bp) == bno)
5861 break; /* Found it */
5862 }
5863 if (i == XFS_BTREE_MAXLEVELS)
5864 bp = NULL;
5865
5866 if (!bp) { /* Chase down all the log items to see if the bp is there */
5867 xfs_log_item_chunk_t *licp;
5868 xfs_trans_t *tp;
5869
5870 tp = cur->bc_tp;
5871 licp = &tp->t_items;
5872 while (!bp && licp != NULL) {
Eric Sandeen39dab9d2008-08-13 16:10:52 +10005873 if (xfs_lic_are_all_free(licp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 licp = licp->lic_next;
5875 continue;
5876 }
5877 for (i = 0; i < licp->lic_unused; i++) {
5878 xfs_log_item_desc_t *lidp;
5879 xfs_log_item_t *lip;
5880 xfs_buf_log_item_t *bip;
5881 xfs_buf_t *lbp;
5882
Eric Sandeen39dab9d2008-08-13 16:10:52 +10005883 if (xfs_lic_isfree(licp, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 continue;
5885 }
5886
Eric Sandeen39dab9d2008-08-13 16:10:52 +10005887 lidp = xfs_lic_slot(licp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 lip = lidp->lid_item;
5889 if (lip->li_type != XFS_LI_BUF)
5890 continue;
5891
5892 bip = (xfs_buf_log_item_t *)lip;
5893 lbp = bip->bli_buf;
5894
5895 if (XFS_BUF_ADDR(lbp) == bno) {
5896 bp = lbp;
5897 break; /* Found it */
5898 }
5899 }
5900 licp = licp->lic_next;
5901 }
5902 }
5903 return(bp);
5904}
5905
Hannes Eder3180e662009-03-04 19:34:10 +01005906STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907xfs_check_block(
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005908 struct xfs_btree_block *block,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 xfs_mount_t *mp,
5910 int root,
5911 short sz)
5912{
5913 int i, j, dmxr;
Christoph Hellwig576039c2006-09-28 10:58:06 +10005914 __be64 *pp, *thispa; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 xfs_bmbt_key_t *prevp, *keyp;
5916
Christoph Hellwig16259e72005-11-02 15:11:25 +11005917 ASSERT(be16_to_cpu(block->bb_level) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918
5919 prevp = NULL;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005920 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 dmxr = mp->m_bmap_dmxr[0];
Christoph Hellwig136341b2008-10-30 17:11:40 +11005922 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
5924 if (prevp) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005925 ASSERT(be64_to_cpu(prevp->br_startoff) <
5926 be64_to_cpu(keyp->br_startoff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 }
5928 prevp = keyp;
5929
5930 /*
5931 * Compare the block numbers to see if there are dups.
5932 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11005933 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005934 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005935 else
5936 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5937
Christoph Hellwig16259e72005-11-02 15:11:25 +11005938 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11005939 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005940 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005941 else
5942 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005943 if (*thispa == *pp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005945 __func__, j, i,
Christoph Hellwig576039c2006-09-28 10:58:06 +10005946 (unsigned long long)be64_to_cpu(*thispa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 panic("%s: ptrs are equal in node\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005948 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 }
5950 }
5951 }
5952}
5953
5954/*
5955 * Check that the extents for the inode ip are in the right order in all
5956 * btree leaves.
5957 */
5958
5959STATIC void
5960xfs_bmap_check_leaf_extents(
5961 xfs_btree_cur_t *cur, /* btree cursor or null */
5962 xfs_inode_t *ip, /* incore inode pointer */
5963 int whichfork) /* data or attr fork */
5964{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005965 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 xfs_fsblock_t bno; /* block # of "block" */
5967 xfs_buf_t *bp; /* buffer for "block" */
5968 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005969 xfs_extnum_t i=0, j; /* index into the extents list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 xfs_ifork_t *ifp; /* fork structure */
5971 int level; /* btree level, for checking */
5972 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10005973 __be64 *pp; /* pointer to block address */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005974 xfs_bmbt_rec_t *ep; /* pointer to current extent */
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005975 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005976 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 int bp_release = 0;
5978
5979 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5980 return;
5981 }
5982
5983 bno = NULLFSBLOCK;
5984 mp = ip->i_mount;
5985 ifp = XFS_IFORK_PTR(ip, whichfork);
5986 block = ifp->if_broot;
5987 /*
5988 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5989 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11005990 level = be16_to_cpu(block->bb_level);
5991 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11005993 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005994 bno = be64_to_cpu(*pp);
5995
5996 ASSERT(bno != NULLDFSBNO);
5997 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5998 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5999
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 /*
6001 * Go down the tree until leaf level is reached, following the first
6002 * pointer (leftmost) at each level.
6003 */
6004 while (level-- > 0) {
6005 /* See if buf is in cur first */
6006 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6007 if (bp) {
6008 bp_release = 0;
6009 } else {
6010 bp_release = 1;
6011 }
6012 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6013 XFS_BMAP_BTREE_REF)))
6014 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006015 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11006017 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 error0);
6019 if (level == 0)
6020 break;
6021
6022 /*
6023 * Check this block for basic sanity (increasing keys and
6024 * no duplicate blocks).
6025 */
6026
6027 xfs_check_block(block, mp, 0, 0);
Christoph Hellwig136341b2008-10-30 17:11:40 +11006028 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006029 bno = be64_to_cpu(*pp);
6030 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 if (bp_release) {
6032 bp_release = 0;
6033 xfs_trans_brelse(NULL, bp);
6034 }
6035 }
6036
6037 /*
6038 * Here with bp and block set to the leftmost leaf node in the tree.
6039 */
6040 i = 0;
6041
6042 /*
6043 * Loop over all leaf nodes checking that all extents are in the right order.
6044 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 xfs_fsblock_t nextbno;
6047 xfs_extnum_t num_recs;
6048
6049
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006050 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051
6052 /*
6053 * Read-ahead the next leaf block, if any.
6054 */
6055
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006056 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057
6058 /*
6059 * Check all the extents to make sure they are OK.
6060 * If we had a previous block, the last entry should
6061 * conform with the first entry in this one.
6062 */
6063
Christoph Hellwig136341b2008-10-30 17:11:40 +11006064 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006065 if (i) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11006066 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
6067 xfs_bmbt_disk_get_blockcount(&last) <=
6068 xfs_bmbt_disk_get_startoff(ep));
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006069 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006070 for (j = 1; j < num_recs; j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11006071 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
Christoph Hellwig4a26e662008-10-30 16:58:32 +11006072 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
6073 xfs_bmbt_disk_get_blockcount(ep) <=
6074 xfs_bmbt_disk_get_startoff(nextp));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006075 ep = nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006078 last = *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 i += num_recs;
6080 if (bp_release) {
6081 bp_release = 0;
6082 xfs_trans_brelse(NULL, bp);
6083 }
6084 bno = nextbno;
6085 /*
6086 * If we've reached the end, stop.
6087 */
6088 if (bno == NULLFSBLOCK)
6089 break;
6090
6091 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6092 if (bp) {
6093 bp_release = 0;
6094 } else {
6095 bp_release = 1;
6096 }
6097 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6098 XFS_BMAP_BTREE_REF)))
6099 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006100 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 }
6102 if (bp_release) {
6103 bp_release = 0;
6104 xfs_trans_brelse(NULL, bp);
6105 }
6106 return;
6107
6108error0:
Harvey Harrison34a622b2008-04-10 12:19:21 +10006109 cmn_err(CE_WARN, "%s: at error0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 if (bp_release)
6111 xfs_trans_brelse(NULL, bp);
6112error_norelse:
6113 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
Harvey Harrison34a622b2008-04-10 12:19:21 +10006114 __func__, i);
6115 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 return;
6117}
6118#endif
6119
6120/*
6121 * Count fsblocks of the given fork.
6122 */
6123int /* error */
6124xfs_bmap_count_blocks(
6125 xfs_trans_t *tp, /* transaction pointer */
6126 xfs_inode_t *ip, /* incore inode */
6127 int whichfork, /* data or attr fork */
6128 int *count) /* out: count of blocks */
6129{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006130 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 xfs_fsblock_t bno; /* block # of "block" */
6132 xfs_ifork_t *ifp; /* fork structure */
6133 int level; /* btree level, for checking */
6134 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10006135 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136
6137 bno = NULLFSBLOCK;
6138 mp = ip->i_mount;
6139 ifp = XFS_IFORK_PTR(ip, whichfork);
6140 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
Ruben Porrasc94312d2008-08-13 16:52:25 +10006141 xfs_bmap_count_leaves(ifp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
Ruben Porrasc94312d2008-08-13 16:52:25 +10006143 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 return 0;
6145 }
6146
6147 /*
6148 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6149 */
6150 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11006151 level = be16_to_cpu(block->bb_level);
6152 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11006153 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006154 bno = be64_to_cpu(*pp);
6155 ASSERT(bno != NULLDFSBNO);
6156 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6157 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006159 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6161 mp);
6162 return XFS_ERROR(EFSCORRUPTED);
6163 }
6164
6165 return 0;
6166}
6167
6168/*
6169 * Recursively walks each level of a btree
6170 * to count total fsblocks is use.
6171 */
David Chinnera8272ce2007-11-23 16:28:09 +11006172STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173xfs_bmap_count_tree(
6174 xfs_mount_t *mp, /* file system mount point */
6175 xfs_trans_t *tp, /* transaction pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006176 xfs_ifork_t *ifp, /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 xfs_fsblock_t blockno, /* file system block number */
6178 int levelin, /* level in btree */
6179 int *count) /* Count of blocks */
6180{
6181 int error;
6182 xfs_buf_t *bp, *nbp;
6183 int level = levelin;
Christoph Hellwig576039c2006-09-28 10:58:06 +10006184 __be64 *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 xfs_fsblock_t bno = blockno;
6186 xfs_fsblock_t nextbno;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006187 struct xfs_btree_block *block, *nextblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189
6190 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6191 return error;
6192 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006193 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194
6195 if (--level) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +02006196 /* Not at node above leaves, count this level of nodes */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006197 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 while (nextbno != NULLFSBLOCK) {
6199 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6200 0, &nbp, XFS_BMAP_BTREE_REF)))
6201 return error;
6202 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006203 nextblock = XFS_BUF_TO_BLOCK(nbp);
6204 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 xfs_trans_brelse(tp, nbp);
6206 }
6207
6208 /* Dive to the next level */
Christoph Hellwig136341b2008-10-30 17:11:40 +11006209 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006210 bno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 if (unlikely((error =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006212 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 xfs_trans_brelse(tp, bp);
6214 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6215 XFS_ERRLEVEL_LOW, mp);
6216 return XFS_ERROR(EFSCORRUPTED);
6217 }
6218 xfs_trans_brelse(tp, bp);
6219 } else {
6220 /* count all level 1 nodes and their leaves */
6221 for (;;) {
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006222 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Christoph Hellwig16259e72005-11-02 15:11:25 +11006223 numrecs = be16_to_cpu(block->bb_numrecs);
Christoph Hellwig136341b2008-10-30 17:11:40 +11006224 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 xfs_trans_brelse(tp, bp);
6226 if (nextbno == NULLFSBLOCK)
6227 break;
6228 bno = nextbno;
6229 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6230 XFS_BMAP_BTREE_REF)))
6231 return error;
6232 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006233 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 }
6235 }
6236 return 0;
6237}
6238
6239/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006240 * Count leaf blocks given a range of extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006242STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006244 xfs_ifork_t *ifp,
6245 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 int numrecs,
6247 int *count)
6248{
6249 int b;
6250
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006251 for (b = 0; b < numrecs; b++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006252 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
Yingping Lu91e11082005-11-02 15:10:24 +11006253 *count += xfs_bmbt_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006254 }
Yingping Lu91e11082005-11-02 15:10:24 +11006255}
6256
6257/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006258 * Count leaf blocks given a range of extent records originally
6259 * in btree format.
Yingping Lu91e11082005-11-02 15:10:24 +11006260 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006261STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +11006262xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +11006263 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006264 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +11006265 int numrecs,
6266 int *count)
6267{
6268 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006269 xfs_bmbt_rec_t *frp;
Yingping Lu91e11082005-11-02 15:10:24 +11006270
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006271 for (b = 1; b <= numrecs; b++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11006272 frp = XFS_BMBT_REC_ADDR(mp, block, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 *count += xfs_bmbt_disk_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275}