blob: 7d6c4ace805219dda03dac57ffb129650f4bce00 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58
59#ifdef DEBUG
60STATIC void
61xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
62#endif
63
64kmem_zone_t *xfs_bmap_free_item_zone;
65
66/*
67 * Prototypes for internal bmap routines.
68 */
69
70
71/*
72 * Called from xfs_bmap_add_attrfork to handle extents format files.
73 */
74STATIC int /* error */
75xfs_bmap_add_attrfork_extents(
76 xfs_trans_t *tp, /* transaction pointer */
77 xfs_inode_t *ip, /* incore inode pointer */
78 xfs_fsblock_t *firstblock, /* first block allocated */
79 xfs_bmap_free_t *flist, /* blocks to free at commit */
80 int *flags); /* inode logging flags */
81
82/*
83 * Called from xfs_bmap_add_attrfork to handle local format files.
84 */
85STATIC int /* error */
86xfs_bmap_add_attrfork_local(
87 xfs_trans_t *tp, /* transaction pointer */
88 xfs_inode_t *ip, /* incore inode pointer */
89 xfs_fsblock_t *firstblock, /* first block allocated */
90 xfs_bmap_free_t *flist, /* blocks to free at commit */
91 int *flags); /* inode logging flags */
92
93/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +110094 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * after allocating space (or doing a delayed allocation).
96 */
97STATIC int /* error */
98xfs_bmap_add_extent(
99 xfs_inode_t *ip, /* incore inode pointer */
100 xfs_extnum_t idx, /* extent number to update/insert */
101 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100102 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 xfs_fsblock_t *first, /* pointer to firstblock variable */
104 xfs_bmap_free_t *flist, /* list of extents to be freed */
105 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000106 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int whichfork, /* data or attr fork */
108 int rsvd); /* OK to allocate reserved blocks */
109
110/*
111 * Called by xfs_bmap_add_extent to handle cases converting a delayed
112 * allocation to a real allocation.
113 */
114STATIC int /* error */
115xfs_bmap_add_extent_delay_real(
116 xfs_inode_t *ip, /* incore inode pointer */
117 xfs_extnum_t idx, /* extent number to update/insert */
118 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100119 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
121 xfs_fsblock_t *first, /* pointer to firstblock variable */
122 xfs_bmap_free_t *flist, /* list of extents to be freed */
123 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000124 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 int rsvd); /* OK to allocate reserved blocks */
126
127/*
128 * Called by xfs_bmap_add_extent to handle cases converting a hole
129 * to a delayed allocation.
130 */
131STATIC int /* error */
132xfs_bmap_add_extent_hole_delay(
133 xfs_inode_t *ip, /* incore inode pointer */
134 xfs_extnum_t idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100135 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int *logflagsp,/* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000137 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 int rsvd); /* OK to allocate reserved blocks */
139
140/*
141 * Called by xfs_bmap_add_extent to handle cases converting a hole
142 * to a real allocation.
143 */
144STATIC int /* error */
145xfs_bmap_add_extent_hole_real(
146 xfs_inode_t *ip, /* incore inode pointer */
147 xfs_extnum_t idx, /* extent number to update/insert */
148 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100149 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000151 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 int whichfork); /* data or attr fork */
153
154/*
155 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
156 * allocation to a real allocation or vice versa.
157 */
158STATIC int /* error */
159xfs_bmap_add_extent_unwritten_real(
160 xfs_inode_t *ip, /* incore inode pointer */
161 xfs_extnum_t idx, /* extent number to update/insert */
162 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100163 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000164 int *logflagsp, /* inode logging flags */
165 xfs_extdelta_t *delta); /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167/*
168 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
169 * It figures out where to ask the underlying allocator to put the new extent.
170 */
171STATIC int /* error */
172xfs_bmap_alloc(
173 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
174
175/*
176 * Transform a btree format file with only one leaf node, where the
177 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100178 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * give up the space for the btree root and pitch the leaf block.
180 */
181STATIC int /* error */
182xfs_bmap_btree_to_extents(
183 xfs_trans_t *tp, /* transaction pointer */
184 xfs_inode_t *ip, /* incore inode pointer */
185 xfs_btree_cur_t *cur, /* btree cursor */
186 int *logflagsp, /* inode logging flags */
187 int whichfork); /* data or attr fork */
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100190 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * after removing space (or undoing a delayed allocation).
192 */
193STATIC int /* error */
194xfs_bmap_del_extent(
195 xfs_inode_t *ip, /* incore inode pointer */
196 xfs_trans_t *tp, /* current trans pointer */
197 xfs_extnum_t idx, /* extent number to update/insert */
198 xfs_bmap_free_t *flist, /* list of extents to be freed */
199 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100200 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 int *logflagsp,/* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000202 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int whichfork, /* data or attr fork */
204 int rsvd); /* OK to allocate reserved blocks */
205
206/*
207 * Remove the entry "free" from the free item list. Prev points to the
208 * previous entry, unless "free" is the head of the list.
209 */
210STATIC void
211xfs_bmap_del_free(
212 xfs_bmap_free_t *flist, /* free item list header */
213 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
214 xfs_bmap_free_item_t *free); /* list item to be freed */
215
216/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * Convert an extents-format file into a btree-format file.
218 * The new file will have a root block (in the inode) and a single child block.
219 */
220STATIC int /* error */
221xfs_bmap_extents_to_btree(
222 xfs_trans_t *tp, /* transaction pointer */
223 xfs_inode_t *ip, /* incore inode pointer */
224 xfs_fsblock_t *firstblock, /* first-block-allocated */
225 xfs_bmap_free_t *flist, /* blocks freed in xaction */
226 xfs_btree_cur_t **curp, /* cursor returned to caller */
227 int wasdel, /* converting a delayed alloc */
228 int *logflagsp, /* inode logging flags */
229 int whichfork); /* data or attr fork */
230
231/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * Convert a local file to an extents file.
233 * This code is sort of bogus, since the file data needs to get
234 * logged so it won't be lost. The bmap-level manipulations are ok, though.
235 */
236STATIC int /* error */
237xfs_bmap_local_to_extents(
238 xfs_trans_t *tp, /* transaction pointer */
239 xfs_inode_t *ip, /* incore inode pointer */
240 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
241 xfs_extlen_t total, /* total blocks needed by transaction */
242 int *logflagsp, /* inode logging flags */
243 int whichfork); /* data or attr fork */
244
245/*
246 * Search the extents list for the inode, for the extent containing bno.
247 * If bno lies in a hole, point to the next entry. If bno lies past eof,
248 * *eofp will be set, and *prevp will contain the last entry (null if none).
249 * Else, *lastxp will be set to the index of the found
250 * entry; *gotp will contain the entry.
251 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000252STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253xfs_bmap_search_extents(
254 xfs_inode_t *ip, /* incore inode pointer */
255 xfs_fileoff_t bno, /* block number searched for */
256 int whichfork, /* data or attr fork */
257 int *eofp, /* out: end of file found */
258 xfs_extnum_t *lastxp, /* out: last extent index */
259 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
260 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
261
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000262/*
263 * Check the last inode extent to determine whether this allocation will result
264 * in blocks being allocated at the end of the file. When we allocate new data
265 * blocks at the end of the file which do not start at the previous data block,
266 * we will try to align the new blocks at stripe unit boundaries.
267 */
268STATIC int /* error */
269xfs_bmap_isaeof(
270 xfs_inode_t *ip, /* incore inode pointer */
271 xfs_fileoff_t off, /* file offset in fsblocks */
272 int whichfork, /* data or attribute fork */
273 char *aeof); /* return value */
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#ifdef XFS_BMAP_TRACE
276/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100277 * Add bmap trace entry prior to a call to xfs_iext_remove.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
279STATIC void
280xfs_bmap_trace_delete(
Eric Sandeen3a59c942007-07-11 11:09:47 +1000281 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 char *desc, /* operation description */
283 xfs_inode_t *ip, /* incore inode pointer */
284 xfs_extnum_t idx, /* index of entry(entries) deleted */
285 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
286 int whichfork); /* data or attr fork */
287
288/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100289 * Add bmap trace entry prior to a call to xfs_iext_insert, or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 * reading in the extents list from the disk (in the btree).
291 */
292STATIC void
293xfs_bmap_trace_insert(
Eric Sandeen3a59c942007-07-11 11:09:47 +1000294 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 char *desc, /* operation description */
296 xfs_inode_t *ip, /* incore inode pointer */
297 xfs_extnum_t idx, /* index of entry(entries) inserted */
298 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
299 xfs_bmbt_irec_t *r1, /* inserted record 1 */
300 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
301 int whichfork); /* data or attr fork */
302
303/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100304 * Add bmap trace entry after updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 */
306STATIC void
307xfs_bmap_trace_post_update(
Eric Sandeen3a59c942007-07-11 11:09:47 +1000308 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 char *desc, /* operation description */
310 xfs_inode_t *ip, /* incore inode pointer */
311 xfs_extnum_t idx, /* index of entry updated */
312 int whichfork); /* data or attr fork */
313
314/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100315 * Add bmap trace entry prior to updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
317STATIC void
318xfs_bmap_trace_pre_update(
Eric Sandeen3a59c942007-07-11 11:09:47 +1000319 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 char *desc, /* operation description */
321 xfs_inode_t *ip, /* incore inode pointer */
322 xfs_extnum_t idx, /* index of entry to be updated */
323 int whichfork); /* data or attr fork */
324
Eric Sandeen3a59c942007-07-11 11:09:47 +1000325#define XFS_BMAP_TRACE_DELETE(d,ip,i,c,w) \
Harvey Harrison34a622b2008-04-10 12:19:21 +1000326 xfs_bmap_trace_delete(__func__,d,ip,i,c,w)
Eric Sandeen3a59c942007-07-11 11:09:47 +1000327#define XFS_BMAP_TRACE_INSERT(d,ip,i,c,r1,r2,w) \
Harvey Harrison34a622b2008-04-10 12:19:21 +1000328 xfs_bmap_trace_insert(__func__,d,ip,i,c,r1,r2,w)
Eric Sandeen3a59c942007-07-11 11:09:47 +1000329#define XFS_BMAP_TRACE_POST_UPDATE(d,ip,i,w) \
Harvey Harrison34a622b2008-04-10 12:19:21 +1000330 xfs_bmap_trace_post_update(__func__,d,ip,i,w)
Eric Sandeen3a59c942007-07-11 11:09:47 +1000331#define XFS_BMAP_TRACE_PRE_UPDATE(d,ip,i,w) \
Harvey Harrison34a622b2008-04-10 12:19:21 +1000332 xfs_bmap_trace_pre_update(__func__,d,ip,i,w)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333#else
Eric Sandeen3a59c942007-07-11 11:09:47 +1000334#define XFS_BMAP_TRACE_DELETE(d,ip,i,c,w)
335#define XFS_BMAP_TRACE_INSERT(d,ip,i,c,r1,r2,w)
336#define XFS_BMAP_TRACE_POST_UPDATE(d,ip,i,w)
337#define XFS_BMAP_TRACE_PRE_UPDATE(d,ip,i,w)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338#endif /* XFS_BMAP_TRACE */
339
340/*
341 * Compute the worst-case number of indirect blocks that will be used
342 * for ip's delayed extent of length "len".
343 */
344STATIC xfs_filblks_t
345xfs_bmap_worst_indlen(
346 xfs_inode_t *ip, /* incore inode pointer */
347 xfs_filblks_t len); /* delayed extent length */
348
349#ifdef DEBUG
350/*
351 * Perform various validation checks on the values being returned
352 * from xfs_bmapi().
353 */
354STATIC void
355xfs_bmap_validate_ret(
356 xfs_fileoff_t bno,
357 xfs_filblks_t len,
358 int flags,
359 xfs_bmbt_irec_t *mval,
360 int nmap,
361 int ret_nmap);
362#else
363#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
364#endif /* DEBUG */
365
366#if defined(XFS_RW_TRACE)
367STATIC void
368xfs_bunmap_trace(
369 xfs_inode_t *ip,
370 xfs_fileoff_t bno,
371 xfs_filblks_t len,
372 int flags,
373 inst_t *ra);
374#else
375#define xfs_bunmap_trace(ip, bno, len, flags, ra)
376#endif /* XFS_RW_TRACE */
377
378STATIC int
379xfs_bmap_count_tree(
380 xfs_mount_t *mp,
381 xfs_trans_t *tp,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100382 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 xfs_fsblock_t blockno,
384 int levelin,
385 int *count);
386
Ruben Porrasc94312d2008-08-13 16:52:25 +1000387STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100389 xfs_ifork_t *ifp,
390 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 int numrecs,
392 int *count);
393
Ruben Porrasc94312d2008-08-13 16:52:25 +1000394STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +1100395xfs_bmap_disk_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100396 xfs_extnum_t idx,
397 xfs_bmbt_block_t *block,
Yingping Lu91e11082005-11-02 15:10:24 +1100398 int numrecs,
399 int *count);
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401/*
402 * Bmap internal routines.
403 */
404
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100405STATIC int /* error */
406xfs_bmbt_lookup_eq(
407 struct xfs_btree_cur *cur,
408 xfs_fileoff_t off,
409 xfs_fsblock_t bno,
410 xfs_filblks_t len,
411 int *stat) /* success/failure */
412{
413 cur->bc_rec.b.br_startoff = off;
414 cur->bc_rec.b.br_startblock = bno;
415 cur->bc_rec.b.br_blockcount = len;
416 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
417}
418
419STATIC int /* error */
420xfs_bmbt_lookup_ge(
421 struct xfs_btree_cur *cur,
422 xfs_fileoff_t off,
423 xfs_fsblock_t bno,
424 xfs_filblks_t len,
425 int *stat) /* success/failure */
426{
427 cur->bc_rec.b.br_startoff = off;
428 cur->bc_rec.b.br_startblock = bno;
429 cur->bc_rec.b.br_blockcount = len;
430 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
431}
432
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100433/*
434* Update the record referred to by cur to the value given
435 * by [off, bno, len, state].
436 * This either works (return 0) or gets an EFSCORRUPTED error.
437 */
438STATIC int
439xfs_bmbt_update(
440 struct xfs_btree_cur *cur,
441 xfs_fileoff_t off,
442 xfs_fsblock_t bno,
443 xfs_filblks_t len,
444 xfs_exntst_t state)
445{
446 union xfs_btree_rec rec;
447
448 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
449 return xfs_btree_update(cur, &rec);
450}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/*
453 * Called from xfs_bmap_add_attrfork to handle btree format files.
454 */
455STATIC int /* error */
456xfs_bmap_add_attrfork_btree(
457 xfs_trans_t *tp, /* transaction pointer */
458 xfs_inode_t *ip, /* incore inode pointer */
459 xfs_fsblock_t *firstblock, /* first block allocated */
460 xfs_bmap_free_t *flist, /* blocks to free at commit */
461 int *flags) /* inode logging flags */
462{
463 xfs_btree_cur_t *cur; /* btree cursor */
464 int error; /* error return value */
465 xfs_mount_t *mp; /* file system mount struct */
466 int stat; /* newroot status */
467
468 mp = ip->i_mount;
469 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
470 *flags |= XFS_ILOG_DBROOT;
471 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100472 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 cur->bc_private.b.flist = flist;
474 cur->bc_private.b.firstblock = *firstblock;
475 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
476 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000477 /* must be at least one entry */
478 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
480 goto error0;
481 if (stat == 0) {
482 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
483 return XFS_ERROR(ENOSPC);
484 }
485 *firstblock = cur->bc_private.b.firstblock;
486 cur->bc_private.b.allocated = 0;
487 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
488 }
489 return 0;
490error0:
491 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
492 return error;
493}
494
495/*
496 * Called from xfs_bmap_add_attrfork to handle extents format files.
497 */
498STATIC int /* error */
499xfs_bmap_add_attrfork_extents(
500 xfs_trans_t *tp, /* transaction pointer */
501 xfs_inode_t *ip, /* incore inode pointer */
502 xfs_fsblock_t *firstblock, /* first block allocated */
503 xfs_bmap_free_t *flist, /* blocks to free at commit */
504 int *flags) /* inode logging flags */
505{
506 xfs_btree_cur_t *cur; /* bmap btree cursor */
507 int error; /* error return value */
508
509 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
510 return 0;
511 cur = NULL;
512 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
513 flags, XFS_DATA_FORK);
514 if (cur) {
515 cur->bc_private.b.allocated = 0;
516 xfs_btree_del_cursor(cur,
517 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
518 }
519 return error;
520}
521
522/*
523 * Called from xfs_bmap_add_attrfork to handle local format files.
524 */
525STATIC int /* error */
526xfs_bmap_add_attrfork_local(
527 xfs_trans_t *tp, /* transaction pointer */
528 xfs_inode_t *ip, /* incore inode pointer */
529 xfs_fsblock_t *firstblock, /* first block allocated */
530 xfs_bmap_free_t *flist, /* blocks to free at commit */
531 int *flags) /* inode logging flags */
532{
533 xfs_da_args_t dargs; /* args for dir/attr code */
534 int error; /* error return value */
535 xfs_mount_t *mp; /* mount structure pointer */
536
537 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
538 return 0;
539 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
540 mp = ip->i_mount;
541 memset(&dargs, 0, sizeof(dargs));
542 dargs.dp = ip;
543 dargs.firstblock = firstblock;
544 dargs.flist = flist;
545 dargs.total = mp->m_dirblkfsbs;
546 dargs.whichfork = XFS_DATA_FORK;
547 dargs.trans = tp;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000548 error = xfs_dir2_sf_to_block(&dargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 } else
550 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
551 XFS_DATA_FORK);
552 return error;
553}
554
555/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100556 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * after allocating space (or doing a delayed allocation).
558 */
559STATIC int /* error */
560xfs_bmap_add_extent(
561 xfs_inode_t *ip, /* incore inode pointer */
562 xfs_extnum_t idx, /* extent number to update/insert */
563 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100564 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 xfs_fsblock_t *first, /* pointer to firstblock variable */
566 xfs_bmap_free_t *flist, /* list of extents to be freed */
567 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000568 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 int whichfork, /* data or attr fork */
570 int rsvd) /* OK to use reserved data blocks */
571{
572 xfs_btree_cur_t *cur; /* btree cursor or null */
573 xfs_filblks_t da_new; /* new count del alloc blocks used */
574 xfs_filblks_t da_old; /* old count del alloc blocks used */
575 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 xfs_ifork_t *ifp; /* inode fork ptr */
577 int logflags; /* returned value */
578 xfs_extnum_t nextents; /* number of extents in file now */
579
580 XFS_STATS_INC(xs_add_exlist);
581 cur = *curp;
582 ifp = XFS_IFORK_PTR(ip, whichfork);
583 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
584 ASSERT(idx <= nextents);
585 da_old = da_new = 0;
586 error = 0;
587 /*
588 * This is the first extent added to a new/empty file.
589 * Special case this one, so other routines get to assume there are
590 * already extents in the list.
591 */
592 if (nextents == 0) {
Eric Sandeen3a59c942007-07-11 11:09:47 +1000593 XFS_BMAP_TRACE_INSERT("insert empty", ip, 0, 1, new, NULL,
594 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100595 xfs_iext_insert(ifp, 0, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 ASSERT(cur == NULL);
597 ifp->if_lastex = 0;
598 if (!ISNULLSTARTBLOCK(new->br_startblock)) {
599 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
600 logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
601 } else
602 logflags = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000603 /* DELTA: single new extent */
604 if (delta) {
605 if (delta->xed_startoff > new->br_startoff)
606 delta->xed_startoff = new->br_startoff;
607 if (delta->xed_blockcount <
608 new->br_startoff + new->br_blockcount)
609 delta->xed_blockcount = new->br_startoff +
610 new->br_blockcount;
611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613 /*
614 * Any kind of new delayed allocation goes here.
615 */
616 else if (ISNULLSTARTBLOCK(new->br_startblock)) {
617 if (cur)
618 ASSERT((cur->bc_private.b.flags &
619 XFS_BTCUR_BPRV_WASDEL) == 0);
Eric Sandeene9ed9d22007-05-08 13:48:56 +1000620 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000621 &logflags, delta, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 goto done;
623 }
624 /*
625 * Real allocation off the end of the file.
626 */
627 else if (idx == nextents) {
628 if (cur)
629 ASSERT((cur->bc_private.b.flags &
630 XFS_BTCUR_BPRV_WASDEL) == 0);
631 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000632 &logflags, delta, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 goto done;
634 } else {
635 xfs_bmbt_irec_t prev; /* old extent at offset idx */
636
637 /*
638 * Get the record referred to by idx.
639 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100640 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /*
642 * If it's a real allocation record, and the new allocation ends
643 * after the start of the referred to record, then we're filling
644 * in a delayed or unwritten allocation with a real one, or
645 * converting real back to unwritten.
646 */
647 if (!ISNULLSTARTBLOCK(new->br_startblock) &&
648 new->br_startoff + new->br_blockcount > prev.br_startoff) {
649 if (prev.br_state != XFS_EXT_UNWRITTEN &&
650 ISNULLSTARTBLOCK(prev.br_startblock)) {
651 da_old = STARTBLOCKVAL(prev.br_startblock);
652 if (cur)
653 ASSERT(cur->bc_private.b.flags &
654 XFS_BTCUR_BPRV_WASDEL);
655 if ((error = xfs_bmap_add_extent_delay_real(ip,
656 idx, &cur, new, &da_new, first, flist,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000657 &logflags, delta, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 goto done;
659 } else if (new->br_state == XFS_EXT_NORM) {
660 ASSERT(new->br_state == XFS_EXT_NORM);
661 if ((error = xfs_bmap_add_extent_unwritten_real(
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000662 ip, idx, &cur, new, &logflags, delta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto done;
664 } else {
665 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
666 if ((error = xfs_bmap_add_extent_unwritten_real(
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000667 ip, idx, &cur, new, &logflags, delta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 goto done;
669 }
670 ASSERT(*curp == cur || *curp == NULL);
671 }
672 /*
673 * Otherwise we're filling in a hole with an allocation.
674 */
675 else {
676 if (cur)
677 ASSERT((cur->bc_private.b.flags &
678 XFS_BTCUR_BPRV_WASDEL) == 0);
679 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000680 new, &logflags, delta, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 goto done;
682 }
683 }
684
685 ASSERT(*curp == cur || *curp == NULL);
686 /*
687 * Convert to a btree if necessary.
688 */
689 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
690 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
691 int tmp_logflags; /* partial log flag return val */
692
693 ASSERT(cur == NULL);
694 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
695 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
696 logflags |= tmp_logflags;
697 if (error)
698 goto done;
699 }
700 /*
701 * Adjust for changes in reserved delayed indirect blocks.
702 * Nothing to do for disk quotas here.
703 */
704 if (da_old || da_new) {
705 xfs_filblks_t nblks;
706
707 nblks = da_new;
708 if (cur)
709 nblks += cur->bc_private.b.allocated;
710 ASSERT(nblks <= da_old);
711 if (nblks < da_old)
712 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100713 (int64_t)(da_old - nblks), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 /*
716 * Clear out the allocated field, done with it now in any case.
717 */
718 if (cur) {
719 cur->bc_private.b.allocated = 0;
720 *curp = cur;
721 }
722done:
723#ifdef DEBUG
724 if (!error)
725 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
726#endif
727 *logflagsp = logflags;
728 return error;
729}
730
731/*
732 * Called by xfs_bmap_add_extent to handle cases converting a delayed
733 * allocation to a real allocation.
734 */
735STATIC int /* error */
736xfs_bmap_add_extent_delay_real(
737 xfs_inode_t *ip, /* incore inode pointer */
738 xfs_extnum_t idx, /* extent number to update/insert */
739 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100740 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
742 xfs_fsblock_t *first, /* pointer to firstblock variable */
743 xfs_bmap_free_t *flist, /* list of extents to be freed */
744 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000745 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int rsvd) /* OK to use reserved data block allocation */
747{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 xfs_btree_cur_t *cur; /* btree cursor */
749 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000750 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100753 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 xfs_fileoff_t new_endoff; /* end offset of new entry */
755 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
756 /* left is 0, right is 1, prev is 2 */
757 int rval=0; /* return value (logging flags) */
758 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000759 xfs_filblks_t temp=0; /* value for dnew calculations */
760 xfs_filblks_t temp2=0;/* value for dnew calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 int tmp_rval; /* partial logging flags */
762 enum { /* bit number definitions for state */
763 LEFT_CONTIG, RIGHT_CONTIG,
764 LEFT_FILLING, RIGHT_FILLING,
765 LEFT_DELAY, RIGHT_DELAY,
766 LEFT_VALID, RIGHT_VALID
767 };
768
769#define LEFT r[0]
770#define RIGHT r[1]
771#define PREV r[2]
772#define MASK(b) (1 << (b))
773#define MASK2(a,b) (MASK(a) | MASK(b))
774#define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
775#define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
776#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
777#define STATE_TEST(b) (state & MASK(b))
778#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
779 ((state &= ~MASK(b)), 0))
780#define SWITCH_STATE \
781 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
782
783 /*
784 * Set up a bunch of variables to make the tests simpler.
785 */
786 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100787 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
788 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 xfs_bmbt_get_all(ep, &PREV);
790 new_endoff = new->br_startoff + new->br_blockcount;
791 ASSERT(PREV.br_startoff <= new->br_startoff);
792 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
793 /*
794 * Set flags determining what part of the previous delayed allocation
795 * extent is being replaced by a real allocation.
796 */
797 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
798 STATE_SET(RIGHT_FILLING,
799 PREV.br_startoff + PREV.br_blockcount == new_endoff);
800 /*
801 * Check and set flags if this segment has a left neighbor.
802 * Don't set contiguous if the combined extent would be too large.
803 */
804 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100805 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
807 }
808 STATE_SET(LEFT_CONTIG,
809 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
810 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
811 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
812 LEFT.br_state == new->br_state &&
813 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
814 /*
815 * Check and set flags if this segment has a right neighbor.
816 * Don't set contiguous if the combined extent would be too large.
817 * Also check for all-three-contiguous being too large.
818 */
819 if (STATE_SET_TEST(RIGHT_VALID,
820 idx <
821 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100822 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
824 }
825 STATE_SET(RIGHT_CONTIG,
826 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
827 new_endoff == RIGHT.br_startoff &&
828 new->br_startblock + new->br_blockcount ==
829 RIGHT.br_startblock &&
830 new->br_state == RIGHT.br_state &&
831 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
832 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
833 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
834 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
835 <= MAXEXTLEN));
836 error = 0;
837 /*
838 * Switch out based on the FILLING and CONTIG state bits.
839 */
840 switch (SWITCH_STATE) {
841
842 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
843 /*
844 * Filling in all of a previously delayed allocation extent.
845 * The left and right neighbors are both contiguous with new.
846 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000847 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100849 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 LEFT.br_blockcount + PREV.br_blockcount +
851 RIGHT.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000852 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000854 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100855 xfs_iext_remove(ifp, idx, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 ip->i_df.if_lastex = idx - 1;
857 ip->i_d.di_nextents--;
858 if (cur == NULL)
859 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
860 else {
861 rval = XFS_ILOG_CORE;
862 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
863 RIGHT.br_startblock,
864 RIGHT.br_blockcount, &i)))
865 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000866 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if ((error = xfs_bmbt_delete(cur, &i)))
868 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000869 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100870 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000872 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
874 LEFT.br_startblock,
875 LEFT.br_blockcount +
876 PREV.br_blockcount +
877 RIGHT.br_blockcount, LEFT.br_state)))
878 goto done;
879 }
880 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000881 /* DELTA: Three in-core extents are replaced by one. */
882 temp = LEFT.br_startoff;
883 temp2 = LEFT.br_blockcount +
884 PREV.br_blockcount +
885 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 break;
887
888 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
889 /*
890 * Filling in all of a previously delayed allocation extent.
891 * The left neighbor is contiguous, the right is not.
892 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000893 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100895 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 LEFT.br_blockcount + PREV.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000897 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 XFS_DATA_FORK);
899 ip->i_df.if_lastex = idx - 1;
Eric Sandeen3a59c942007-07-11 11:09:47 +1000900 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100901 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (cur == NULL)
903 rval = XFS_ILOG_DEXT;
904 else {
905 rval = 0;
906 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
907 LEFT.br_startblock, LEFT.br_blockcount,
908 &i)))
909 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000910 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
912 LEFT.br_startblock,
913 LEFT.br_blockcount +
914 PREV.br_blockcount, LEFT.br_state)))
915 goto done;
916 }
917 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000918 /* DELTA: Two in-core extents are replaced by one. */
919 temp = LEFT.br_startoff;
920 temp2 = LEFT.br_blockcount +
921 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 break;
923
924 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
925 /*
926 * Filling in all of a previously delayed allocation extent.
927 * The right neighbor is contiguous, the left is not.
928 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000929 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 xfs_bmbt_set_startblock(ep, new->br_startblock);
931 xfs_bmbt_set_blockcount(ep,
932 PREV.br_blockcount + RIGHT.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000933 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 ip->i_df.if_lastex = idx;
Eric Sandeen3a59c942007-07-11 11:09:47 +1000935 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100936 xfs_iext_remove(ifp, idx + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (cur == NULL)
938 rval = XFS_ILOG_DEXT;
939 else {
940 rval = 0;
941 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
942 RIGHT.br_startblock,
943 RIGHT.br_blockcount, &i)))
944 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000945 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
947 new->br_startblock,
948 PREV.br_blockcount +
949 RIGHT.br_blockcount, PREV.br_state)))
950 goto done;
951 }
952 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000953 /* DELTA: Two in-core extents are replaced by one. */
954 temp = PREV.br_startoff;
955 temp2 = PREV.br_blockcount +
956 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 break;
958
959 case MASK2(LEFT_FILLING, RIGHT_FILLING):
960 /*
961 * Filling in all of a previously delayed allocation extent.
962 * Neither the left nor right neighbors are contiguous with
963 * the new one.
964 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000965 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 xfs_bmbt_set_startblock(ep, new->br_startblock);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000967 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 ip->i_df.if_lastex = idx;
969 ip->i_d.di_nextents++;
970 if (cur == NULL)
971 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
972 else {
973 rval = XFS_ILOG_CORE;
974 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
975 new->br_startblock, new->br_blockcount,
976 &i)))
977 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000978 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 cur->bc_rec.b.br_state = XFS_EXT_NORM;
980 if ((error = xfs_bmbt_insert(cur, &i)))
981 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000982 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000985 /* DELTA: The in-core extent described by new changed type. */
986 temp = new->br_startoff;
987 temp2 = new->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 break;
989
990 case MASK2(LEFT_FILLING, LEFT_CONTIG):
991 /*
992 * Filling in the first part of a previous delayed allocation.
993 * The left neighbor is contiguous.
994 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000995 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100996 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 LEFT.br_blockcount + new->br_blockcount);
998 xfs_bmbt_set_startoff(ep,
999 PREV.br_startoff + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001000 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 temp = PREV.br_blockcount - new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001002 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 xfs_bmbt_set_blockcount(ep, temp);
1004 ip->i_df.if_lastex = idx - 1;
1005 if (cur == NULL)
1006 rval = XFS_ILOG_DEXT;
1007 else {
1008 rval = 0;
1009 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
1010 LEFT.br_startblock, LEFT.br_blockcount,
1011 &i)))
1012 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001013 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1015 LEFT.br_startblock,
1016 LEFT.br_blockcount +
1017 new->br_blockcount,
1018 LEFT.br_state)))
1019 goto done;
1020 }
1021 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1022 STARTBLOCKVAL(PREV.br_startblock));
1023 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001024 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001026 /* DELTA: The boundary between two in-core extents moved. */
1027 temp = LEFT.br_startoff;
1028 temp2 = LEFT.br_blockcount +
1029 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 break;
1031
1032 case MASK(LEFT_FILLING):
1033 /*
1034 * Filling in the first part of a previous delayed allocation.
1035 * The left neighbor is not contiguous.
1036 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001037 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 xfs_bmbt_set_startoff(ep, new_endoff);
1039 temp = PREV.br_blockcount - new->br_blockcount;
1040 xfs_bmbt_set_blockcount(ep, temp);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001041 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001043 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 ip->i_df.if_lastex = idx;
1045 ip->i_d.di_nextents++;
1046 if (cur == NULL)
1047 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1048 else {
1049 rval = XFS_ILOG_CORE;
1050 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1051 new->br_startblock, new->br_blockcount,
1052 &i)))
1053 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001054 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1056 if ((error = xfs_bmbt_insert(cur, &i)))
1057 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001058 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1061 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1062 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1063 first, flist, &cur, 1, &tmp_rval,
1064 XFS_DATA_FORK);
1065 rval |= tmp_rval;
1066 if (error)
1067 goto done;
1068 }
1069 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1070 STARTBLOCKVAL(PREV.br_startblock) -
1071 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001072 ep = xfs_iext_get_ext(ifp, idx + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001074 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx + 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001076 /* DELTA: One in-core extent is split in two. */
1077 temp = PREV.br_startoff;
1078 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 break;
1080
1081 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1082 /*
1083 * Filling in the last part of a previous delayed allocation.
1084 * The right neighbor is contiguous with the new allocation.
1085 */
1086 temp = PREV.br_blockcount - new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001087 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
1088 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 xfs_bmbt_set_blockcount(ep, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001090 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1091 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 new->br_blockcount + RIGHT.br_blockcount,
1093 RIGHT.br_state);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001094 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 ip->i_df.if_lastex = idx + 1;
1096 if (cur == NULL)
1097 rval = XFS_ILOG_DEXT;
1098 else {
1099 rval = 0;
1100 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1101 RIGHT.br_startblock,
1102 RIGHT.br_blockcount, &i)))
1103 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001104 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1106 new->br_startblock,
1107 new->br_blockcount +
1108 RIGHT.br_blockcount,
1109 RIGHT.br_state)))
1110 goto done;
1111 }
1112 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1113 STARTBLOCKVAL(PREV.br_startblock));
1114 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001115 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001117 /* DELTA: The boundary between two in-core extents moved. */
1118 temp = PREV.br_startoff;
1119 temp2 = PREV.br_blockcount +
1120 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 break;
1122
1123 case MASK(RIGHT_FILLING):
1124 /*
1125 * Filling in the last part of a previous delayed allocation.
1126 * The right neighbor is not contiguous.
1127 */
1128 temp = PREV.br_blockcount - new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001129 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 xfs_bmbt_set_blockcount(ep, temp);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001131 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1132 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001133 xfs_iext_insert(ifp, idx + 1, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 ip->i_df.if_lastex = idx + 1;
1135 ip->i_d.di_nextents++;
1136 if (cur == NULL)
1137 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1138 else {
1139 rval = XFS_ILOG_CORE;
1140 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1141 new->br_startblock, new->br_blockcount,
1142 &i)))
1143 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001144 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1146 if ((error = xfs_bmbt_insert(cur, &i)))
1147 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001148 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1151 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1152 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1153 first, flist, &cur, 1, &tmp_rval,
1154 XFS_DATA_FORK);
1155 rval |= tmp_rval;
1156 if (error)
1157 goto done;
1158 }
1159 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1160 STARTBLOCKVAL(PREV.br_startblock) -
1161 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001162 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001164 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001166 /* DELTA: One in-core extent is split in two. */
1167 temp = PREV.br_startoff;
1168 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 break;
1170
1171 case 0:
1172 /*
1173 * Filling in the middle part of a previous delayed allocation.
1174 * Contiguity is impossible here.
1175 * This case is avoided almost all the time.
1176 */
1177 temp = new->br_startoff - PREV.br_startoff;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001178 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 xfs_bmbt_set_blockcount(ep, temp);
1180 r[0] = *new;
Lachlan McIlroyd2133712006-11-21 18:55:16 +11001181 r[1].br_state = PREV.br_state;
1182 r[1].br_startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 r[1].br_startoff = new_endoff;
1184 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1185 r[1].br_blockcount = temp2;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001186 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001188 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 ip->i_df.if_lastex = idx + 1;
1190 ip->i_d.di_nextents++;
1191 if (cur == NULL)
1192 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1193 else {
1194 rval = XFS_ILOG_CORE;
1195 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1196 new->br_startblock, new->br_blockcount,
1197 &i)))
1198 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001199 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1201 if ((error = xfs_bmbt_insert(cur, &i)))
1202 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001203 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1206 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1207 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1208 first, flist, &cur, 1, &tmp_rval,
1209 XFS_DATA_FORK);
1210 rval |= tmp_rval;
1211 if (error)
1212 goto done;
1213 }
1214 temp = xfs_bmap_worst_indlen(ip, temp);
1215 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1216 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
1217 (cur ? cur->bc_private.b.allocated : 0));
1218 if (diff > 0 &&
David Chinner20f4ebf2007-02-10 18:36:10 +11001219 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 /*
1221 * Ick gross gag me with a spoon.
1222 */
1223 ASSERT(0); /* want to see if this ever happens! */
1224 while (diff > 0) {
1225 if (temp) {
1226 temp--;
1227 diff--;
1228 if (!diff ||
1229 !xfs_mod_incore_sb(ip->i_mount,
David Chinner20f4ebf2007-02-10 18:36:10 +11001230 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 break;
1232 }
1233 if (temp2) {
1234 temp2--;
1235 diff--;
1236 if (!diff ||
1237 !xfs_mod_incore_sb(ip->i_mount,
David Chinner20f4ebf2007-02-10 18:36:10 +11001238 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 break;
1240 }
1241 }
1242 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001243 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001245 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
1246 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001247 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1248 NULLSTARTBLOCK((int)temp2));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001249 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 *dnew = temp + temp2;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001251 /* DELTA: One in-core extent is split in three. */
1252 temp = PREV.br_startoff;
1253 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 break;
1255
1256 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1257 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1258 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1259 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1260 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1261 case MASK(LEFT_CONTIG):
1262 case MASK(RIGHT_CONTIG):
1263 /*
1264 * These cases are all impossible.
1265 */
1266 ASSERT(0);
1267 }
1268 *curp = cur;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001269 if (delta) {
1270 temp2 += temp;
1271 if (delta->xed_startoff > temp)
1272 delta->xed_startoff = temp;
1273 if (delta->xed_blockcount < temp2)
1274 delta->xed_blockcount = temp2;
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276done:
1277 *logflagsp = rval;
1278 return error;
1279#undef LEFT
1280#undef RIGHT
1281#undef PREV
1282#undef MASK
1283#undef MASK2
1284#undef MASK3
1285#undef MASK4
1286#undef STATE_SET
1287#undef STATE_TEST
1288#undef STATE_SET_TEST
1289#undef SWITCH_STATE
1290}
1291
1292/*
1293 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1294 * allocation to a real allocation or vice versa.
1295 */
1296STATIC int /* error */
1297xfs_bmap_add_extent_unwritten_real(
1298 xfs_inode_t *ip, /* incore inode pointer */
1299 xfs_extnum_t idx, /* extent number to update/insert */
1300 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001301 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001302 int *logflagsp, /* inode logging flags */
1303 xfs_extdelta_t *delta) /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001306 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001309 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 xfs_fileoff_t new_endoff; /* end offset of new entry */
1311 xfs_exntst_t newext; /* new extent state */
1312 xfs_exntst_t oldext; /* old extent state */
1313 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1314 /* left is 0, right is 1, prev is 2 */
1315 int rval=0; /* return value (logging flags) */
1316 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001317 xfs_filblks_t temp=0;
1318 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 enum { /* bit number definitions for state */
1320 LEFT_CONTIG, RIGHT_CONTIG,
1321 LEFT_FILLING, RIGHT_FILLING,
1322 LEFT_DELAY, RIGHT_DELAY,
1323 LEFT_VALID, RIGHT_VALID
1324 };
1325
1326#define LEFT r[0]
1327#define RIGHT r[1]
1328#define PREV r[2]
1329#define MASK(b) (1 << (b))
1330#define MASK2(a,b) (MASK(a) | MASK(b))
1331#define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1332#define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1333#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1334#define STATE_TEST(b) (state & MASK(b))
1335#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1336 ((state &= ~MASK(b)), 0))
1337#define SWITCH_STATE \
1338 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1339
1340 /*
1341 * Set up a bunch of variables to make the tests simpler.
1342 */
1343 error = 0;
1344 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001345 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1346 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 xfs_bmbt_get_all(ep, &PREV);
1348 newext = new->br_state;
1349 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1350 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1351 ASSERT(PREV.br_state == oldext);
1352 new_endoff = new->br_startoff + new->br_blockcount;
1353 ASSERT(PREV.br_startoff <= new->br_startoff);
1354 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1355 /*
1356 * Set flags determining what part of the previous oldext allocation
1357 * extent is being replaced by a newext allocation.
1358 */
1359 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
1360 STATE_SET(RIGHT_FILLING,
1361 PREV.br_startoff + PREV.br_blockcount == new_endoff);
1362 /*
1363 * Check and set flags if this segment has a left neighbor.
1364 * Don't set contiguous if the combined extent would be too large.
1365 */
1366 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001367 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
1369 }
1370 STATE_SET(LEFT_CONTIG,
1371 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1372 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1373 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1374 LEFT.br_state == newext &&
1375 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1376 /*
1377 * Check and set flags if this segment has a right neighbor.
1378 * Don't set contiguous if the combined extent would be too large.
1379 * Also check for all-three-contiguous being too large.
1380 */
1381 if (STATE_SET_TEST(RIGHT_VALID,
1382 idx <
1383 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001384 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
1386 }
1387 STATE_SET(RIGHT_CONTIG,
1388 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1389 new_endoff == RIGHT.br_startoff &&
1390 new->br_startblock + new->br_blockcount ==
1391 RIGHT.br_startblock &&
1392 newext == RIGHT.br_state &&
1393 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1394 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
1395 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
1396 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1397 <= MAXEXTLEN));
1398 /*
1399 * Switch out based on the FILLING and CONTIG state bits.
1400 */
1401 switch (SWITCH_STATE) {
1402
1403 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1404 /*
1405 * Setting all of a previous oldext extent to newext.
1406 * The left and right neighbors are both contiguous with new.
1407 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001408 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001410 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 LEFT.br_blockcount + PREV.br_blockcount +
1412 RIGHT.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001413 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001415 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001416 xfs_iext_remove(ifp, idx, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 ip->i_df.if_lastex = idx - 1;
1418 ip->i_d.di_nextents -= 2;
1419 if (cur == NULL)
1420 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1421 else {
1422 rval = XFS_ILOG_CORE;
1423 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1424 RIGHT.br_startblock,
1425 RIGHT.br_blockcount, &i)))
1426 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001427 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if ((error = xfs_bmbt_delete(cur, &i)))
1429 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001430 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001431 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001433 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if ((error = xfs_bmbt_delete(cur, &i)))
1435 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001436 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001437 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001439 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1441 LEFT.br_startblock,
1442 LEFT.br_blockcount + PREV.br_blockcount +
1443 RIGHT.br_blockcount, LEFT.br_state)))
1444 goto done;
1445 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001446 /* DELTA: Three in-core extents are replaced by one. */
1447 temp = LEFT.br_startoff;
1448 temp2 = LEFT.br_blockcount +
1449 PREV.br_blockcount +
1450 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 break;
1452
1453 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
1454 /*
1455 * Setting all of a previous oldext extent to newext.
1456 * The left neighbor is contiguous, the right is not.
1457 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001458 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001460 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 LEFT.br_blockcount + PREV.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001462 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 XFS_DATA_FORK);
1464 ip->i_df.if_lastex = idx - 1;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001465 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001466 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 ip->i_d.di_nextents--;
1468 if (cur == NULL)
1469 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1470 else {
1471 rval = XFS_ILOG_CORE;
1472 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1473 PREV.br_startblock, PREV.br_blockcount,
1474 &i)))
1475 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001476 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if ((error = xfs_bmbt_delete(cur, &i)))
1478 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001479 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001480 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001482 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1484 LEFT.br_startblock,
1485 LEFT.br_blockcount + PREV.br_blockcount,
1486 LEFT.br_state)))
1487 goto done;
1488 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001489 /* DELTA: Two in-core extents are replaced by one. */
1490 temp = LEFT.br_startoff;
1491 temp2 = LEFT.br_blockcount +
1492 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 break;
1494
1495 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
1496 /*
1497 * Setting all of a previous oldext extent to newext.
1498 * The right neighbor is contiguous, the left is not.
1499 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001500 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 XFS_DATA_FORK);
1502 xfs_bmbt_set_blockcount(ep,
1503 PREV.br_blockcount + RIGHT.br_blockcount);
1504 xfs_bmbt_set_state(ep, newext);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001505 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 XFS_DATA_FORK);
1507 ip->i_df.if_lastex = idx;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001508 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001509 xfs_iext_remove(ifp, idx + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 ip->i_d.di_nextents--;
1511 if (cur == NULL)
1512 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1513 else {
1514 rval = XFS_ILOG_CORE;
1515 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1516 RIGHT.br_startblock,
1517 RIGHT.br_blockcount, &i)))
1518 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001519 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if ((error = xfs_bmbt_delete(cur, &i)))
1521 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001522 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001523 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001525 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1527 new->br_startblock,
1528 new->br_blockcount + RIGHT.br_blockcount,
1529 newext)))
1530 goto done;
1531 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001532 /* DELTA: Two in-core extents are replaced by one. */
1533 temp = PREV.br_startoff;
1534 temp2 = PREV.br_blockcount +
1535 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 break;
1537
1538 case MASK2(LEFT_FILLING, RIGHT_FILLING):
1539 /*
1540 * Setting all of a previous oldext extent to newext.
1541 * Neither the left nor right neighbors are contiguous with
1542 * the new one.
1543 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001544 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 XFS_DATA_FORK);
1546 xfs_bmbt_set_state(ep, newext);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001547 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 XFS_DATA_FORK);
1549 ip->i_df.if_lastex = idx;
1550 if (cur == NULL)
1551 rval = XFS_ILOG_DEXT;
1552 else {
1553 rval = 0;
1554 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1555 new->br_startblock, new->br_blockcount,
1556 &i)))
1557 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001558 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1560 new->br_startblock, new->br_blockcount,
1561 newext)))
1562 goto done;
1563 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001564 /* DELTA: The in-core extent described by new changed type. */
1565 temp = new->br_startoff;
1566 temp2 = new->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 break;
1568
1569 case MASK2(LEFT_FILLING, LEFT_CONTIG):
1570 /*
1571 * Setting the first part of a previous oldext extent to newext.
1572 * The left neighbor is contiguous.
1573 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001574 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001576 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 LEFT.br_blockcount + new->br_blockcount);
1578 xfs_bmbt_set_startoff(ep,
1579 PREV.br_startoff + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001580 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001582 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 XFS_DATA_FORK);
1584 xfs_bmbt_set_startblock(ep,
1585 new->br_startblock + new->br_blockcount);
1586 xfs_bmbt_set_blockcount(ep,
1587 PREV.br_blockcount - new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001588 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 XFS_DATA_FORK);
1590 ip->i_df.if_lastex = idx - 1;
1591 if (cur == NULL)
1592 rval = XFS_ILOG_DEXT;
1593 else {
1594 rval = 0;
1595 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1596 PREV.br_startblock, PREV.br_blockcount,
1597 &i)))
1598 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001599 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if ((error = xfs_bmbt_update(cur,
1601 PREV.br_startoff + new->br_blockcount,
1602 PREV.br_startblock + new->br_blockcount,
1603 PREV.br_blockcount - new->br_blockcount,
1604 oldext)))
1605 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001606 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 goto done;
1608 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1609 LEFT.br_startblock,
1610 LEFT.br_blockcount + new->br_blockcount,
1611 LEFT.br_state))
1612 goto done;
1613 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001614 /* DELTA: The boundary between two in-core extents moved. */
1615 temp = LEFT.br_startoff;
1616 temp2 = LEFT.br_blockcount +
1617 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 break;
1619
1620 case MASK(LEFT_FILLING):
1621 /*
1622 * Setting the first part of a previous oldext extent to newext.
1623 * The left neighbor is not contiguous.
1624 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001625 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1627 xfs_bmbt_set_startoff(ep, new_endoff);
1628 xfs_bmbt_set_blockcount(ep,
1629 PREV.br_blockcount - new->br_blockcount);
1630 xfs_bmbt_set_startblock(ep,
1631 new->br_startblock + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001632 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1633 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001635 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 ip->i_df.if_lastex = idx;
1637 ip->i_d.di_nextents++;
1638 if (cur == NULL)
1639 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1640 else {
1641 rval = XFS_ILOG_CORE;
1642 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1643 PREV.br_startblock, PREV.br_blockcount,
1644 &i)))
1645 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001646 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if ((error = xfs_bmbt_update(cur,
1648 PREV.br_startoff + new->br_blockcount,
1649 PREV.br_startblock + new->br_blockcount,
1650 PREV.br_blockcount - new->br_blockcount,
1651 oldext)))
1652 goto done;
1653 cur->bc_rec.b = *new;
1654 if ((error = xfs_bmbt_insert(cur, &i)))
1655 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001656 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001658 /* DELTA: One in-core extent is split in two. */
1659 temp = PREV.br_startoff;
1660 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 break;
1662
1663 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1664 /*
1665 * Setting the last part of a previous oldext extent to newext.
1666 * The right neighbor is contiguous with the new allocation.
1667 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001668 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001670 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 XFS_DATA_FORK);
1672 xfs_bmbt_set_blockcount(ep,
1673 PREV.br_blockcount - new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001674 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001676 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1677 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 new->br_blockcount + RIGHT.br_blockcount, newext);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001679 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 XFS_DATA_FORK);
1681 ip->i_df.if_lastex = idx + 1;
1682 if (cur == NULL)
1683 rval = XFS_ILOG_DEXT;
1684 else {
1685 rval = 0;
1686 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1687 PREV.br_startblock,
1688 PREV.br_blockcount, &i)))
1689 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001690 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1692 PREV.br_startblock,
1693 PREV.br_blockcount - new->br_blockcount,
1694 oldext)))
1695 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001696 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 goto done;
1698 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1699 new->br_startblock,
1700 new->br_blockcount + RIGHT.br_blockcount,
1701 newext)))
1702 goto done;
1703 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001704 /* DELTA: The boundary between two in-core extents moved. */
1705 temp = PREV.br_startoff;
1706 temp2 = PREV.br_blockcount +
1707 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 break;
1709
1710 case MASK(RIGHT_FILLING):
1711 /*
1712 * Setting the last part of a previous oldext extent to newext.
1713 * The right neighbor is not contiguous.
1714 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001715 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 xfs_bmbt_set_blockcount(ep,
1717 PREV.br_blockcount - new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001718 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1719 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1720 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001721 xfs_iext_insert(ifp, idx + 1, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 ip->i_df.if_lastex = idx + 1;
1723 ip->i_d.di_nextents++;
1724 if (cur == NULL)
1725 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1726 else {
1727 rval = XFS_ILOG_CORE;
1728 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1729 PREV.br_startblock, PREV.br_blockcount,
1730 &i)))
1731 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001732 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1734 PREV.br_startblock,
1735 PREV.br_blockcount - new->br_blockcount,
1736 oldext)))
1737 goto done;
1738 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1739 new->br_startblock, new->br_blockcount,
1740 &i)))
1741 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001742 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1744 if ((error = xfs_bmbt_insert(cur, &i)))
1745 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001746 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001748 /* DELTA: One in-core extent is split in two. */
1749 temp = PREV.br_startoff;
1750 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 break;
1752
1753 case 0:
1754 /*
1755 * Setting the middle part of a previous oldext extent to
1756 * newext. Contiguity is impossible here.
1757 * One extent becomes three extents.
1758 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001759 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 xfs_bmbt_set_blockcount(ep,
1761 new->br_startoff - PREV.br_startoff);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001762 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 r[0] = *new;
1764 r[1].br_startoff = new_endoff;
1765 r[1].br_blockcount =
1766 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1767 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1768 r[1].br_state = oldext;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001769 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001771 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 ip->i_df.if_lastex = idx + 1;
1773 ip->i_d.di_nextents += 2;
1774 if (cur == NULL)
1775 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1776 else {
1777 rval = XFS_ILOG_CORE;
1778 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1779 PREV.br_startblock, PREV.br_blockcount,
1780 &i)))
1781 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001782 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /* new right extent - oldext */
1784 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1785 r[1].br_startblock, r[1].br_blockcount,
1786 r[1].br_state)))
1787 goto done;
1788 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10001790 cur->bc_rec.b.br_blockcount =
1791 new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if ((error = xfs_bmbt_insert(cur, &i)))
1793 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001794 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001795 /*
1796 * Reset the cursor to the position of the new extent
1797 * we are about to insert as we can't trust it after
1798 * the previous insert.
1799 */
1800 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1801 new->br_startblock, new->br_blockcount,
1802 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001804 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001806 cur->bc_rec.b.br_state = new->br_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if ((error = xfs_bmbt_insert(cur, &i)))
1808 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001809 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001811 /* DELTA: One in-core extent is split in three. */
1812 temp = PREV.br_startoff;
1813 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 break;
1815
1816 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1817 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1818 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1819 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1820 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1821 case MASK(LEFT_CONTIG):
1822 case MASK(RIGHT_CONTIG):
1823 /*
1824 * These cases are all impossible.
1825 */
1826 ASSERT(0);
1827 }
1828 *curp = cur;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001829 if (delta) {
1830 temp2 += temp;
1831 if (delta->xed_startoff > temp)
1832 delta->xed_startoff = temp;
1833 if (delta->xed_blockcount < temp2)
1834 delta->xed_blockcount = temp2;
1835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836done:
1837 *logflagsp = rval;
1838 return error;
1839#undef LEFT
1840#undef RIGHT
1841#undef PREV
1842#undef MASK
1843#undef MASK2
1844#undef MASK3
1845#undef MASK4
1846#undef STATE_SET
1847#undef STATE_TEST
1848#undef STATE_SET_TEST
1849#undef SWITCH_STATE
1850}
1851
1852/*
1853 * Called by xfs_bmap_add_extent to handle cases converting a hole
1854 * to a delayed allocation.
1855 */
1856/*ARGSUSED*/
1857STATIC int /* error */
1858xfs_bmap_add_extent_hole_delay(
1859 xfs_inode_t *ip, /* incore inode pointer */
1860 xfs_extnum_t idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001861 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001863 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 int rsvd) /* OK to allocate reserved blocks */
1865{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001866 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001867 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1869 xfs_filblks_t newlen=0; /* new indirect size */
1870 xfs_filblks_t oldlen=0; /* old indirect size */
1871 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1872 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001873 xfs_filblks_t temp=0; /* temp for indirect calculations */
1874 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 enum { /* bit number definitions for state */
1876 LEFT_CONTIG, RIGHT_CONTIG,
1877 LEFT_DELAY, RIGHT_DELAY,
1878 LEFT_VALID, RIGHT_VALID
1879 };
1880
1881#define MASK(b) (1 << (b))
1882#define MASK2(a,b) (MASK(a) | MASK(b))
1883#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1884#define STATE_TEST(b) (state & MASK(b))
1885#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1886 ((state &= ~MASK(b)), 0))
1887#define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1888
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001889 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1890 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 state = 0;
1892 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1893 /*
1894 * Check and set flags if this segment has a left neighbor
1895 */
1896 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001897 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1899 }
1900 /*
1901 * Check and set flags if the current (right) segment exists.
1902 * If it doesn't exist, we're converting the hole at end-of-file.
1903 */
1904 if (STATE_SET_TEST(RIGHT_VALID,
1905 idx <
1906 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1907 xfs_bmbt_get_all(ep, &right);
1908 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1909 }
1910 /*
1911 * Set contiguity flags on the left and right neighbors.
1912 * Don't let extents get too large, even if the pieces are contiguous.
1913 */
1914 STATE_SET(LEFT_CONTIG,
1915 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1916 left.br_startoff + left.br_blockcount == new->br_startoff &&
1917 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1918 STATE_SET(RIGHT_CONTIG,
1919 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1920 new->br_startoff + new->br_blockcount == right.br_startoff &&
1921 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1922 (!STATE_TEST(LEFT_CONTIG) ||
1923 (left.br_blockcount + new->br_blockcount +
1924 right.br_blockcount <= MAXEXTLEN)));
1925 /*
1926 * Switch out based on the contiguity flags.
1927 */
1928 switch (SWITCH_STATE) {
1929
1930 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1931 /*
1932 * New allocation is contiguous with delayed allocations
1933 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001934 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 */
1936 temp = left.br_blockcount + new->br_blockcount +
1937 right.br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001938 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001940 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 oldlen = STARTBLOCKVAL(left.br_startblock) +
1942 STARTBLOCKVAL(new->br_startblock) +
1943 STARTBLOCKVAL(right.br_startblock);
1944 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001945 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1946 NULLSTARTBLOCK((int)newlen));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001947 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001949 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001950 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 ip->i_df.if_lastex = idx - 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001952 /* DELTA: Two in-core extents were replaced by one. */
1953 temp2 = temp;
1954 temp = left.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 break;
1956
1957 case MASK(LEFT_CONTIG):
1958 /*
1959 * New allocation is contiguous with a delayed allocation
1960 * on the left.
1961 * Merge the new allocation with the left neighbor.
1962 */
1963 temp = left.br_blockcount + new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001964 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001966 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 oldlen = STARTBLOCKVAL(left.br_startblock) +
1968 STARTBLOCKVAL(new->br_startblock);
1969 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001970 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1971 NULLSTARTBLOCK((int)newlen));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001972 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 XFS_DATA_FORK);
1974 ip->i_df.if_lastex = idx - 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001975 /* DELTA: One in-core extent grew into a hole. */
1976 temp2 = temp;
1977 temp = left.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 break;
1979
1980 case MASK(RIGHT_CONTIG):
1981 /*
1982 * New allocation is contiguous with a delayed allocation
1983 * on the right.
1984 * Merge the new allocation with the right neighbor.
1985 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001986 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 temp = new->br_blockcount + right.br_blockcount;
1988 oldlen = STARTBLOCKVAL(new->br_startblock) +
1989 STARTBLOCKVAL(right.br_startblock);
1990 newlen = xfs_bmap_worst_indlen(ip, temp);
1991 xfs_bmbt_set_allf(ep, new->br_startoff,
1992 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001993 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 ip->i_df.if_lastex = idx;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001995 /* DELTA: One in-core extent grew into a hole. */
1996 temp2 = temp;
1997 temp = new->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 break;
1999
2000 case 0:
2001 /*
2002 * New allocation is not contiguous with another
2003 * delayed allocation.
2004 * Insert a new entry.
2005 */
2006 oldlen = newlen = 0;
Eric Sandeen3a59c942007-07-11 11:09:47 +10002007 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002009 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 ip->i_df.if_lastex = idx;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002011 /* DELTA: A new in-core extent was added in a hole. */
2012 temp2 = new->br_blockcount;
2013 temp = new->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 break;
2015 }
2016 if (oldlen != newlen) {
2017 ASSERT(oldlen > newlen);
2018 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11002019 (int64_t)(oldlen - newlen), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 /*
2021 * Nothing to do for disk quota accounting here.
2022 */
2023 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002024 if (delta) {
2025 temp2 += temp;
2026 if (delta->xed_startoff > temp)
2027 delta->xed_startoff = temp;
2028 if (delta->xed_blockcount < temp2)
2029 delta->xed_blockcount = temp2;
2030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 *logflagsp = 0;
2032 return 0;
2033#undef MASK
2034#undef MASK2
2035#undef STATE_SET
2036#undef STATE_TEST
2037#undef STATE_SET_TEST
2038#undef SWITCH_STATE
2039}
2040
2041/*
2042 * Called by xfs_bmap_add_extent to handle cases converting a hole
2043 * to a real allocation.
2044 */
2045STATIC int /* error */
2046xfs_bmap_add_extent_hole_real(
2047 xfs_inode_t *ip, /* incore inode pointer */
2048 xfs_extnum_t idx, /* extent number to update/insert */
2049 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002050 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002052 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 int whichfork) /* data or attr fork */
2054{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002055 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 int i; /* temp state */
2058 xfs_ifork_t *ifp; /* inode fork pointer */
2059 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2060 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002061 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002063 xfs_filblks_t temp=0;
2064 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 enum { /* bit number definitions for state */
2066 LEFT_CONTIG, RIGHT_CONTIG,
2067 LEFT_DELAY, RIGHT_DELAY,
2068 LEFT_VALID, RIGHT_VALID
2069 };
2070
2071#define MASK(b) (1 << (b))
2072#define MASK2(a,b) (MASK(a) | MASK(b))
2073#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
2074#define STATE_TEST(b) (state & MASK(b))
2075#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
2076 ((state &= ~MASK(b)), 0))
2077#define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
2078
2079 ifp = XFS_IFORK_PTR(ip, whichfork);
2080 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002081 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 state = 0;
2083 /*
2084 * Check and set flags if this segment has a left neighbor.
2085 */
2086 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002087 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
2089 }
2090 /*
2091 * Check and set flags if this segment has a current value.
2092 * Not true if we're inserting into the "hole" at eof.
2093 */
2094 if (STATE_SET_TEST(RIGHT_VALID,
2095 idx <
2096 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
2097 xfs_bmbt_get_all(ep, &right);
2098 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
2099 }
2100 /*
2101 * We're inserting a real allocation between "left" and "right".
2102 * Set the contiguity flags. Don't let extents get too large.
2103 */
2104 STATE_SET(LEFT_CONTIG,
2105 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
2106 left.br_startoff + left.br_blockcount == new->br_startoff &&
2107 left.br_startblock + left.br_blockcount == new->br_startblock &&
2108 left.br_state == new->br_state &&
2109 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
2110 STATE_SET(RIGHT_CONTIG,
2111 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
2112 new->br_startoff + new->br_blockcount == right.br_startoff &&
2113 new->br_startblock + new->br_blockcount ==
2114 right.br_startblock &&
2115 new->br_state == right.br_state &&
2116 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2117 (!STATE_TEST(LEFT_CONTIG) ||
2118 left.br_blockcount + new->br_blockcount +
2119 right.br_blockcount <= MAXEXTLEN));
2120
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002121 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 /*
2123 * Select which case we're in here, and implement it.
2124 */
2125 switch (SWITCH_STATE) {
2126
2127 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
2128 /*
2129 * New allocation is contiguous with real allocations on the
2130 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002131 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002133 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002135 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 left.br_blockcount + new->br_blockcount +
2137 right.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002138 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 whichfork);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002140 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002141 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 ifp->if_lastex = idx - 1;
2143 XFS_IFORK_NEXT_SET(ip, whichfork,
2144 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2145 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002146 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2147 } else {
2148 rval = XFS_ILOG_CORE;
2149 if ((error = xfs_bmbt_lookup_eq(cur,
2150 right.br_startoff,
2151 right.br_startblock,
2152 right.br_blockcount, &i)))
2153 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002154 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002155 if ((error = xfs_bmbt_delete(cur, &i)))
2156 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002157 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002158 if ((error = xfs_btree_decrement(cur, 0, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002159 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002160 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002161 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2162 left.br_startblock,
2163 left.br_blockcount +
2164 new->br_blockcount +
2165 right.br_blockcount,
2166 left.br_state)))
2167 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002169 /* DELTA: Two in-core extents were replaced by one. */
2170 temp = left.br_startoff;
2171 temp2 = left.br_blockcount +
2172 new->br_blockcount +
2173 right.br_blockcount;
2174 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 case MASK(LEFT_CONTIG):
2177 /*
2178 * New allocation is contiguous with a real allocation
2179 * on the left.
2180 * Merge the new allocation with the left neighbor.
2181 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002182 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002183 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 left.br_blockcount + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002185 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 ifp->if_lastex = idx - 1;
2187 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002188 rval = XFS_ILOG_FEXT(whichfork);
2189 } else {
2190 rval = 0;
2191 if ((error = xfs_bmbt_lookup_eq(cur,
2192 left.br_startoff,
2193 left.br_startblock,
2194 left.br_blockcount, &i)))
2195 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002196 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002197 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2198 left.br_startblock,
2199 left.br_blockcount +
2200 new->br_blockcount,
2201 left.br_state)))
2202 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002204 /* DELTA: One in-core extent grew. */
2205 temp = left.br_startoff;
2206 temp2 = left.br_blockcount +
2207 new->br_blockcount;
2208 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 case MASK(RIGHT_CONTIG):
2211 /*
2212 * New allocation is contiguous with a real allocation
2213 * on the right.
2214 * Merge the new allocation with the right neighbor.
2215 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002216 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2218 new->br_blockcount + right.br_blockcount,
2219 right.br_state);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002220 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 ifp->if_lastex = idx;
2222 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002223 rval = XFS_ILOG_FEXT(whichfork);
2224 } else {
2225 rval = 0;
2226 if ((error = xfs_bmbt_lookup_eq(cur,
2227 right.br_startoff,
2228 right.br_startblock,
2229 right.br_blockcount, &i)))
2230 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002231 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002232 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2233 new->br_startblock,
2234 new->br_blockcount +
2235 right.br_blockcount,
2236 right.br_state)))
2237 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002239 /* DELTA: One in-core extent grew. */
2240 temp = new->br_startoff;
2241 temp2 = new->br_blockcount +
2242 right.br_blockcount;
2243 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245 case 0:
2246 /*
2247 * New allocation is not contiguous with another
2248 * real allocation.
2249 * Insert a new entry.
2250 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002251 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002252 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 ifp->if_lastex = idx;
2254 XFS_IFORK_NEXT_SET(ip, whichfork,
2255 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2256 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002257 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2258 } else {
2259 rval = XFS_ILOG_CORE;
2260 if ((error = xfs_bmbt_lookup_eq(cur,
2261 new->br_startoff,
2262 new->br_startblock,
2263 new->br_blockcount, &i)))
2264 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002265 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002266 cur->bc_rec.b.br_state = new->br_state;
2267 if ((error = xfs_bmbt_insert(cur, &i)))
2268 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002269 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002271 /* DELTA: A new extent was added in a hole. */
2272 temp = new->br_startoff;
2273 temp2 = new->br_blockcount;
2274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002276 if (delta) {
2277 temp2 += temp;
2278 if (delta->xed_startoff > temp)
2279 delta->xed_startoff = temp;
2280 if (delta->xed_blockcount < temp2)
2281 delta->xed_blockcount = temp2;
2282 }
2283done:
2284 *logflagsp = rval;
2285 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286#undef MASK
2287#undef MASK2
2288#undef STATE_SET
2289#undef STATE_TEST
2290#undef STATE_SET_TEST
2291#undef SWITCH_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
2293
Nathan Scottdd9f4382006-01-11 15:28:28 +11002294/*
2295 * Adjust the size of the new extent based on di_extsize and rt extsize.
2296 */
2297STATIC int
2298xfs_bmap_extsize_align(
2299 xfs_mount_t *mp,
2300 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2301 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2302 xfs_extlen_t extsz, /* align to this extent size */
2303 int rt, /* is this a realtime inode? */
2304 int eof, /* is extent at end-of-file? */
2305 int delay, /* creating delalloc extent? */
2306 int convert, /* overwriting unwritten extent? */
2307 xfs_fileoff_t *offp, /* in/out: aligned offset */
2308 xfs_extlen_t *lenp) /* in/out: aligned length */
2309{
2310 xfs_fileoff_t orig_off; /* original offset */
2311 xfs_extlen_t orig_alen; /* original length */
2312 xfs_fileoff_t orig_end; /* original off+len */
2313 xfs_fileoff_t nexto; /* next file offset */
2314 xfs_fileoff_t prevo; /* previous file offset */
2315 xfs_fileoff_t align_off; /* temp for offset */
2316 xfs_extlen_t align_alen; /* temp for length */
2317 xfs_extlen_t temp; /* temp for calculations */
2318
2319 if (convert)
2320 return 0;
2321
2322 orig_off = align_off = *offp;
2323 orig_alen = align_alen = *lenp;
2324 orig_end = orig_off + orig_alen;
2325
2326 /*
2327 * If this request overlaps an existing extent, then don't
2328 * attempt to perform any additional alignment.
2329 */
2330 if (!delay && !eof &&
2331 (orig_off >= gotp->br_startoff) &&
2332 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2333 return 0;
2334 }
2335
2336 /*
2337 * If the file offset is unaligned vs. the extent size
2338 * we need to align it. This will be possible unless
2339 * the file was previously written with a kernel that didn't
2340 * perform this alignment, or if a truncate shot us in the
2341 * foot.
2342 */
2343 temp = do_mod(orig_off, extsz);
2344 if (temp) {
2345 align_alen += temp;
2346 align_off -= temp;
2347 }
2348 /*
2349 * Same adjustment for the end of the requested area.
2350 */
2351 if ((temp = (align_alen % extsz))) {
2352 align_alen += extsz - temp;
2353 }
2354 /*
2355 * If the previous block overlaps with this proposed allocation
2356 * then move the start forward without adjusting the length.
2357 */
2358 if (prevp->br_startoff != NULLFILEOFF) {
2359 if (prevp->br_startblock == HOLESTARTBLOCK)
2360 prevo = prevp->br_startoff;
2361 else
2362 prevo = prevp->br_startoff + prevp->br_blockcount;
2363 } else
2364 prevo = 0;
2365 if (align_off != orig_off && align_off < prevo)
2366 align_off = prevo;
2367 /*
2368 * If the next block overlaps with this proposed allocation
2369 * then move the start back without adjusting the length,
2370 * but not before offset 0.
2371 * This may of course make the start overlap previous block,
2372 * and if we hit the offset 0 limit then the next block
2373 * can still overlap too.
2374 */
2375 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2376 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2377 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2378 nexto = gotp->br_startoff + gotp->br_blockcount;
2379 else
2380 nexto = gotp->br_startoff;
2381 } else
2382 nexto = NULLFILEOFF;
2383 if (!eof &&
2384 align_off + align_alen != orig_end &&
2385 align_off + align_alen > nexto)
2386 align_off = nexto > align_alen ? nexto - align_alen : 0;
2387 /*
2388 * If we're now overlapping the next or previous extent that
2389 * means we can't fit an extsz piece in this hole. Just move
2390 * the start forward to the first valid spot and set
2391 * the length so we hit the end.
2392 */
2393 if (align_off != orig_off && align_off < prevo)
2394 align_off = prevo;
2395 if (align_off + align_alen != orig_end &&
2396 align_off + align_alen > nexto &&
2397 nexto != NULLFILEOFF) {
2398 ASSERT(nexto > prevo);
2399 align_alen = nexto - align_off;
2400 }
2401
2402 /*
2403 * If realtime, and the result isn't a multiple of the realtime
2404 * extent size we need to remove blocks until it is.
2405 */
2406 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2407 /*
2408 * We're not covering the original request, or
2409 * we won't be able to once we fix the length.
2410 */
2411 if (orig_off < align_off ||
2412 orig_end > align_off + align_alen ||
2413 align_alen - temp < orig_alen)
2414 return XFS_ERROR(EINVAL);
2415 /*
2416 * Try to fix it by moving the start up.
2417 */
2418 if (align_off + temp <= orig_off) {
2419 align_alen -= temp;
2420 align_off += temp;
2421 }
2422 /*
2423 * Try to fix it by moving the end in.
2424 */
2425 else if (align_off + align_alen - temp >= orig_end)
2426 align_alen -= temp;
2427 /*
2428 * Set the start to the minimum then trim the length.
2429 */
2430 else {
2431 align_alen -= orig_off - align_off;
2432 align_off = orig_off;
2433 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2434 }
2435 /*
2436 * Result doesn't cover the request, fail it.
2437 */
2438 if (orig_off < align_off || orig_end > align_off + align_alen)
2439 return XFS_ERROR(EINVAL);
2440 } else {
2441 ASSERT(orig_off >= align_off);
2442 ASSERT(orig_end <= align_off + align_alen);
2443 }
2444
2445#ifdef DEBUG
2446 if (!eof && gotp->br_startoff != NULLFILEOFF)
2447 ASSERT(align_off + align_alen <= gotp->br_startoff);
2448 if (prevp->br_startoff != NULLFILEOFF)
2449 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2450#endif
2451
2452 *lenp = align_alen;
2453 *offp = align_off;
2454 return 0;
2455}
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457#define XFS_ALLOC_GAP_UNITS 4
2458
David Chinnerc2b1cba2008-04-10 12:21:40 +10002459STATIC void
Nathan Scotta365bdd2006-03-14 13:34:16 +11002460xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2462{
2463 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2465 xfs_mount_t *mp; /* mount point structure */
2466 int nullfb; /* true if ap->firstblock isn't set */
2467 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469#define ISVALID(x,y) \
2470 (rt ? \
2471 (x) < mp->m_sb.sb_rblocks : \
2472 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2473 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2474 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 mp = ap->ip->i_mount;
2477 nullfb = ap->firstblock == NULLFSBLOCK;
2478 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2479 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 /*
2481 * If allocating at eof, and there's a previous real block,
2482 * try to use it's last block as our starting point.
2483 */
2484 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2485 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2486 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2487 ap->prevp->br_startblock)) {
2488 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2489 /*
2490 * Adjust for the gap between prevp and us.
2491 */
2492 adjust = ap->off -
2493 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2494 if (adjust &&
2495 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2496 ap->rval += adjust;
2497 }
2498 /*
2499 * If not at eof, then compare the two neighbor blocks.
2500 * Figure out whether either one gives us a good starting point,
2501 * and pick the better one.
2502 */
2503 else if (!ap->eof) {
2504 xfs_fsblock_t gotbno; /* right side block number */
2505 xfs_fsblock_t gotdiff=0; /* right side difference */
2506 xfs_fsblock_t prevbno; /* left side block number */
2507 xfs_fsblock_t prevdiff=0; /* left side difference */
2508
2509 /*
2510 * If there's a previous (left) block, select a requested
2511 * start block based on it.
2512 */
2513 if (ap->prevp->br_startoff != NULLFILEOFF &&
2514 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2515 (prevbno = ap->prevp->br_startblock +
2516 ap->prevp->br_blockcount) &&
2517 ISVALID(prevbno, ap->prevp->br_startblock)) {
2518 /*
2519 * Calculate gap to end of previous block.
2520 */
2521 adjust = prevdiff = ap->off -
2522 (ap->prevp->br_startoff +
2523 ap->prevp->br_blockcount);
2524 /*
2525 * Figure the startblock based on the previous block's
2526 * end and the gap size.
2527 * Heuristic!
2528 * If the gap is large relative to the piece we're
2529 * allocating, or using it gives us an invalid block
2530 * number, then just use the end of the previous block.
2531 */
2532 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2533 ISVALID(prevbno + prevdiff,
2534 ap->prevp->br_startblock))
2535 prevbno += adjust;
2536 else
2537 prevdiff += adjust;
2538 /*
2539 * If the firstblock forbids it, can't use it,
2540 * must use default.
2541 */
2542 if (!rt && !nullfb &&
2543 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2544 prevbno = NULLFSBLOCK;
2545 }
2546 /*
2547 * No previous block or can't follow it, just default.
2548 */
2549 else
2550 prevbno = NULLFSBLOCK;
2551 /*
2552 * If there's a following (right) block, select a requested
2553 * start block based on it.
2554 */
2555 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
2556 /*
2557 * Calculate gap to start of next block.
2558 */
2559 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2560 /*
2561 * Figure the startblock based on the next block's
2562 * start and the gap size.
2563 */
2564 gotbno = ap->gotp->br_startblock;
2565 /*
2566 * Heuristic!
2567 * If the gap is large relative to the piece we're
2568 * allocating, or using it gives us an invalid block
2569 * number, then just use the start of the next block
2570 * offset by our length.
2571 */
2572 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2573 ISVALID(gotbno - gotdiff, gotbno))
2574 gotbno -= adjust;
2575 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2576 gotbno -= ap->alen;
2577 gotdiff += adjust - ap->alen;
2578 } else
2579 gotdiff += adjust;
2580 /*
2581 * If the firstblock forbids it, can't use it,
2582 * must use default.
2583 */
2584 if (!rt && !nullfb &&
2585 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2586 gotbno = NULLFSBLOCK;
2587 }
2588 /*
2589 * No next block, just default.
2590 */
2591 else
2592 gotbno = NULLFSBLOCK;
2593 /*
2594 * If both valid, pick the better one, else the only good
2595 * one, else ap->rval is already set (to 0 or the inode block).
2596 */
2597 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2598 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2599 else if (prevbno != NULLFSBLOCK)
2600 ap->rval = prevbno;
2601 else if (gotbno != NULLFSBLOCK)
2602 ap->rval = gotbno;
2603 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002604#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11002605}
2606
2607STATIC int
2608xfs_bmap_rtalloc(
2609 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2610{
2611 xfs_alloctype_t atype = 0; /* type for allocation routines */
2612 int error; /* error return value */
2613 xfs_mount_t *mp; /* mount point structure */
2614 xfs_extlen_t prod = 0; /* product factor for allocators */
2615 xfs_extlen_t ralen = 0; /* realtime allocation length */
2616 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002617 xfs_rtblock_t rtb;
2618
2619 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002620 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002621 prod = align / mp->m_sb.sb_rextsize;
2622 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2623 align, 1, ap->eof, 0,
2624 ap->conv, &ap->off, &ap->alen);
2625 if (error)
2626 return error;
2627 ASSERT(ap->alen);
2628 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2629
2630 /*
2631 * If the offset & length are not perfectly aligned
2632 * then kill prod, it will just get us in trouble.
2633 */
2634 if (do_mod(ap->off, align) || ap->alen % align)
2635 prod = 1;
2636 /*
2637 * Set ralen to be the actual requested length in rtextents.
2638 */
2639 ralen = ap->alen / mp->m_sb.sb_rextsize;
2640 /*
2641 * If the old value was close enough to MAXEXTLEN that
2642 * we rounded up to it, cut it back so it's valid again.
2643 * Note that if it's a really large request (bigger than
2644 * MAXEXTLEN), we don't hear about that number, and can't
2645 * adjust the starting point to match it.
2646 */
2647 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2648 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2649 /*
2650 * If it's an allocation to an empty file at offset 0,
2651 * pick an extent that will space things out in the rt area.
2652 */
2653 if (ap->eof && ap->off == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10002654 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2655
Nathan Scotta365bdd2006-03-14 13:34:16 +11002656 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2657 if (error)
2658 return error;
2659 ap->rval = rtx * mp->m_sb.sb_rextsize;
2660 } else {
2661 ap->rval = 0;
2662 }
2663
2664 xfs_bmap_adjacent(ap);
2665
2666 /*
2667 * Realtime allocation, done through xfs_rtallocate_extent.
2668 */
2669 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2670 do_div(ap->rval, mp->m_sb.sb_rextsize);
2671 rtb = ap->rval;
2672 ap->alen = ralen;
2673 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2674 &ralen, atype, ap->wasdel, prod, &rtb)))
2675 return error;
2676 if (rtb == NULLFSBLOCK && prod > 1 &&
2677 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2678 ap->alen, &ralen, atype,
2679 ap->wasdel, 1, &rtb)))
2680 return error;
2681 ap->rval = rtb;
2682 if (ap->rval != NULLFSBLOCK) {
2683 ap->rval *= mp->m_sb.sb_rextsize;
2684 ralen *= mp->m_sb.sb_rextsize;
2685 ap->alen = ralen;
2686 ap->ip->i_d.di_nblocks += ralen;
2687 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2688 if (ap->wasdel)
2689 ap->ip->i_delayed_blks -= ralen;
2690 /*
2691 * Adjust the disk quota also. This was reserved
2692 * earlier.
2693 */
2694 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2695 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2696 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2697 } else {
2698 ap->alen = 0;
2699 }
2700 return 0;
2701}
2702
2703STATIC int
2704xfs_bmap_btalloc(
2705 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2706{
2707 xfs_mount_t *mp; /* mount point structure */
2708 xfs_alloctype_t atype = 0; /* type for allocation routines */
2709 xfs_extlen_t align; /* minimum allocation alignment */
2710 xfs_agnumber_t ag;
2711 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2712 xfs_agnumber_t startag;
2713 xfs_alloc_arg_t args;
2714 xfs_extlen_t blen;
2715 xfs_extlen_t delta;
2716 xfs_extlen_t longest;
2717 xfs_extlen_t need;
2718 xfs_extlen_t nextminlen = 0;
2719 xfs_perag_t *pag;
2720 int nullfb; /* true if ap->firstblock isn't set */
2721 int isaligned;
2722 int notinit;
2723 int tryagain;
2724 int error;
2725
2726 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002727 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002728 if (unlikely(align)) {
2729 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2730 align, 0, ap->eof, 0, ap->conv,
2731 &ap->off, &ap->alen);
2732 ASSERT(!error);
2733 ASSERT(ap->alen);
2734 }
2735 nullfb = ap->firstblock == NULLFSBLOCK;
2736 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10002737 if (nullfb) {
2738 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2739 ag = xfs_filestream_lookup_ag(ap->ip);
2740 ag = (ag != NULLAGNUMBER) ? ag : 0;
2741 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2742 } else {
2743 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2744 }
2745 } else
Nathan Scotta365bdd2006-03-14 13:34:16 +11002746 ap->rval = ap->firstblock;
2747
2748 xfs_bmap_adjacent(ap);
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 /*
2751 * If allowed, use ap->rval; otherwise must use firstblock since
2752 * it's in the right allocation group.
2753 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002754 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 ;
2756 else
2757 ap->rval = ap->firstblock;
2758 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 * Normal allocation, done through xfs_alloc_vextent.
2760 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002761 tryagain = isaligned = 0;
2762 args.tp = ap->tp;
2763 args.mp = mp;
2764 args.fsbno = ap->rval;
2765 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
Yingping Lud210a282006-06-09 14:55:18 +10002766 args.firstblock = ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002767 blen = 0;
2768 if (nullfb) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002769 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2770 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2771 else
2772 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002773 args.total = ap->total;
David Chinner2a82b8b2007-07-11 11:09:12 +10002774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10002776 * Search for an allocation group with a single extent
2777 * large enough for the request.
2778 *
2779 * If one isn't found, then adjust the minimum allocation
2780 * size to the largest space found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002782 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
David Chinner2a82b8b2007-07-11 11:09:12 +10002783 if (startag == NULLAGNUMBER)
2784 startag = ag = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002785 notinit = 0;
2786 down_read(&mp->m_peraglock);
2787 while (blen < ap->alen) {
2788 pag = &mp->m_perag[ag];
2789 if (!pag->pagf_init &&
2790 (error = xfs_alloc_pagf_init(mp, args.tp,
2791 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2792 up_read(&mp->m_peraglock);
2793 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002795 /*
2796 * See xfs_alloc_fix_freelist...
2797 */
2798 if (pag->pagf_init) {
2799 need = XFS_MIN_FREELIST_PAG(pag, mp);
2800 delta = need > pag->pagf_flcount ?
2801 need - pag->pagf_flcount : 0;
2802 longest = (pag->pagf_longest > delta) ?
2803 (pag->pagf_longest - delta) :
2804 (pag->pagf_flcount > 0 ||
2805 pag->pagf_longest > 0);
2806 if (blen < longest)
2807 blen = longest;
2808 } else
2809 notinit = 1;
David Chinner2a82b8b2007-07-11 11:09:12 +10002810
2811 if (xfs_inode_is_filestream(ap->ip)) {
2812 if (blen >= ap->alen)
2813 break;
2814
2815 if (ap->userdata) {
2816 /*
2817 * If startag is an invalid AG, we've
2818 * come here once before and
2819 * xfs_filestream_new_ag picked the
2820 * best currently available.
2821 *
2822 * Don't continue looping, since we
2823 * could loop forever.
2824 */
2825 if (startag == NULLAGNUMBER)
2826 break;
2827
2828 error = xfs_filestream_new_ag(ap, &ag);
2829 if (error) {
2830 up_read(&mp->m_peraglock);
2831 return error;
2832 }
2833
2834 /* loop again to set 'blen'*/
2835 startag = NULLAGNUMBER;
2836 continue;
2837 }
2838 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002839 if (++ag == mp->m_sb.sb_agcount)
2840 ag = 0;
2841 if (ag == startag)
2842 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002844 up_read(&mp->m_peraglock);
2845 /*
2846 * Since the above loop did a BUF_TRYLOCK, it is
2847 * possible that there is space for this request.
2848 */
2849 if (notinit || blen < ap->minlen)
2850 args.minlen = ap->minlen;
2851 /*
2852 * If the best seen length is less than the request
2853 * length, use the best as the minimum.
2854 */
2855 else if (blen < ap->alen)
2856 args.minlen = blen;
2857 /*
2858 * Otherwise we've seen an extent as big as alen,
2859 * use that as the minimum.
2860 */
2861 else
2862 args.minlen = ap->alen;
David Chinner2a82b8b2007-07-11 11:09:12 +10002863
2864 /*
2865 * set the failure fallback case to look in the selected
2866 * AG as the stream may have moved.
2867 */
2868 if (xfs_inode_is_filestream(ap->ip))
2869 ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002870 } else if (ap->low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002871 if (xfs_inode_is_filestream(ap->ip))
2872 args.type = XFS_ALLOCTYPE_FIRST_AG;
2873 else
2874 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002875 args.total = args.minlen = ap->minlen;
2876 } else {
2877 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2878 args.total = ap->total;
2879 args.minlen = ap->minlen;
2880 }
David Chinner957d0eb2007-06-18 16:50:37 +10002881 /* apply extent size hints if obtained earlier */
2882 if (unlikely(align)) {
2883 args.prod = align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002884 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2885 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11002886 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11002887 args.prod = 1;
2888 args.mod = 0;
2889 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002890 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002891 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2892 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2893 }
2894 /*
2895 * If we are not low on available data blocks, and the
2896 * underlying logical volume manager is a stripe, and
2897 * the file offset is zero then try to allocate data
2898 * blocks on stripe unit boundary.
2899 * NOTE: ap->aeof is only set if the allocation length
2900 * is >= the stripe unit and the allocation offset is
2901 * at the end of file.
2902 */
2903 if (!ap->low && ap->aeof) {
2904 if (!ap->off) {
2905 args.alignment = mp->m_dalign;
2906 atype = args.type;
2907 isaligned = 1;
2908 /*
2909 * Adjust for alignment
2910 */
2911 if (blen > args.alignment && blen <= ap->alen)
2912 args.minlen = blen - args.alignment;
2913 args.minalignslop = 0;
2914 } else {
2915 /*
2916 * First try an exact bno allocation.
2917 * If it fails then do a near or start bno
2918 * allocation with alignment turned on.
2919 */
2920 atype = args.type;
2921 tryagain = 1;
2922 args.type = XFS_ALLOCTYPE_THIS_BNO;
2923 args.alignment = 1;
2924 /*
2925 * Compute the minlen+alignment for the
2926 * next case. Set slop so that the value
2927 * of minlen+alignment+slop doesn't go up
2928 * between the calls.
2929 */
2930 if (blen > mp->m_dalign && blen <= ap->alen)
2931 nextminlen = blen - mp->m_dalign;
2932 else
2933 nextminlen = args.minlen;
2934 if (nextminlen + mp->m_dalign > args.minlen + 1)
2935 args.minalignslop =
2936 nextminlen + mp->m_dalign -
2937 args.minlen - 1;
2938 else
2939 args.minalignslop = 0;
2940 }
2941 } else {
2942 args.alignment = 1;
2943 args.minalignslop = 0;
2944 }
2945 args.minleft = ap->minleft;
2946 args.wasdel = ap->wasdel;
2947 args.isfl = 0;
2948 args.userdata = ap->userdata;
2949 if ((error = xfs_alloc_vextent(&args)))
2950 return error;
2951 if (tryagain && args.fsbno == NULLFSBLOCK) {
2952 /*
2953 * Exact allocation failed. Now try with alignment
2954 * turned on.
2955 */
2956 args.type = atype;
2957 args.fsbno = ap->rval;
2958 args.alignment = mp->m_dalign;
2959 args.minlen = nextminlen;
2960 args.minalignslop = 0;
2961 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 if ((error = xfs_alloc_vextent(&args)))
2963 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002964 }
2965 if (isaligned && args.fsbno == NULLFSBLOCK) {
2966 /*
2967 * allocation failed, so turn off alignment and
2968 * try again.
2969 */
2970 args.type = atype;
2971 args.fsbno = ap->rval;
2972 args.alignment = 0;
2973 if ((error = xfs_alloc_vextent(&args)))
2974 return error;
2975 }
2976 if (args.fsbno == NULLFSBLOCK && nullfb &&
2977 args.minlen > ap->minlen) {
2978 args.minlen = ap->minlen;
2979 args.type = XFS_ALLOCTYPE_START_BNO;
2980 args.fsbno = ap->rval;
2981 if ((error = xfs_alloc_vextent(&args)))
2982 return error;
2983 }
2984 if (args.fsbno == NULLFSBLOCK && nullfb) {
2985 args.fsbno = 0;
2986 args.type = XFS_ALLOCTYPE_FIRST_AG;
2987 args.total = ap->minlen;
2988 args.minleft = 0;
2989 if ((error = xfs_alloc_vextent(&args)))
2990 return error;
2991 ap->low = 1;
2992 }
2993 if (args.fsbno != NULLFSBLOCK) {
2994 ap->firstblock = ap->rval = args.fsbno;
2995 ASSERT(nullfb || fb_agno == args.agno ||
2996 (ap->low && fb_agno < args.agno));
2997 ap->alen = args.len;
2998 ap->ip->i_d.di_nblocks += args.len;
2999 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3000 if (ap->wasdel)
3001 ap->ip->i_delayed_blks -= args.len;
3002 /*
3003 * Adjust the disk quota also. This was reserved
3004 * earlier.
3005 */
3006 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
3007 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3008 XFS_TRANS_DQ_BCOUNT,
3009 (long) args.len);
3010 } else {
3011 ap->rval = NULLFSBLOCK;
3012 ap->alen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 }
3014 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003015}
3016
3017/*
3018 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3019 * It figures out where to ask the underlying allocator to put the new extent.
3020 */
3021STATIC int
3022xfs_bmap_alloc(
3023 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
3024{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003025 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003026 return xfs_bmap_rtalloc(ap);
3027 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028}
3029
3030/*
3031 * Transform a btree format file with only one leaf node, where the
3032 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003033 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 * give up the space for the btree root and pitch the leaf block.
3035 */
3036STATIC int /* error */
3037xfs_bmap_btree_to_extents(
3038 xfs_trans_t *tp, /* transaction pointer */
3039 xfs_inode_t *ip, /* incore inode pointer */
3040 xfs_btree_cur_t *cur, /* btree cursor */
3041 int *logflagsp, /* inode logging flags */
3042 int whichfork) /* data or attr fork */
3043{
3044 /* REFERENCED */
3045 xfs_bmbt_block_t *cblock;/* child btree block */
3046 xfs_fsblock_t cbno; /* child block number */
3047 xfs_buf_t *cbp; /* child block's buffer */
3048 int error; /* error return value */
3049 xfs_ifork_t *ifp; /* inode fork data */
3050 xfs_mount_t *mp; /* mount point structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10003051 __be64 *pp; /* ptr to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 xfs_bmbt_block_t *rblock;/* root btree block */
3053
3054 ifp = XFS_IFORK_PTR(ip, whichfork);
3055 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3056 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
3057 rblock = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003058 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
3059 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
3061 mp = ip->i_mount;
3062 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10003063 cbno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 *logflagsp = 0;
3065#ifdef DEBUG
Christoph Hellwig576039c2006-09-28 10:58:06 +10003066 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 return error;
3068#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
3070 XFS_BMAP_BTREE_REF)))
3071 return error;
3072 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
3073 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
3074 return error;
3075 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
3076 ip->i_d.di_nblocks--;
3077 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
3078 xfs_trans_binval(tp, cbp);
3079 if (cur->bc_bufs[0] == cbp)
3080 cur->bc_bufs[0] = NULL;
3081 xfs_iroot_realloc(ip, -1, whichfork);
3082 ASSERT(ifp->if_broot == NULL);
3083 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
3084 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3085 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
3086 return 0;
3087}
3088
3089/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003090 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 * after removing space (or undoing a delayed allocation).
3092 */
3093STATIC int /* error */
3094xfs_bmap_del_extent(
3095 xfs_inode_t *ip, /* incore inode pointer */
3096 xfs_trans_t *tp, /* current transaction pointer */
3097 xfs_extnum_t idx, /* extent number to update/delete */
3098 xfs_bmap_free_t *flist, /* list of extents to be freed */
3099 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003100 xfs_bmbt_irec_t *del, /* data to remove from extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003102 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 int whichfork, /* data or attr fork */
3104 int rsvd) /* OK to allocate reserved blocks */
3105{
3106 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
3107 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
3108 xfs_fsblock_t del_endblock=0; /* first block past del */
3109 xfs_fileoff_t del_endoff; /* first offset past del */
3110 int delay; /* current block is delayed allocated */
3111 int do_fx; /* free extent at end of routine */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003112 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 int error; /* error return value */
3114 int flags; /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 xfs_bmbt_irec_t got; /* current extent entry */
3116 xfs_fileoff_t got_endoff; /* first offset past got */
3117 int i; /* temp state */
3118 xfs_ifork_t *ifp; /* inode fork pointer */
3119 xfs_mount_t *mp; /* mount structure */
3120 xfs_filblks_t nblks; /* quota/sb block count */
3121 xfs_bmbt_irec_t new; /* new record to be inserted */
3122 /* REFERENCED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 uint qfield; /* quota field to update */
3124 xfs_filblks_t temp; /* for indirect length calculations */
3125 xfs_filblks_t temp2; /* for indirect length calculations */
3126
3127 XFS_STATS_INC(xs_del_exlist);
3128 mp = ip->i_mount;
3129 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003130 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
3131 (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 ASSERT(del->br_blockcount > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003133 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 xfs_bmbt_get_all(ep, &got);
3135 ASSERT(got.br_startoff <= del->br_startoff);
3136 del_endoff = del->br_startoff + del->br_blockcount;
3137 got_endoff = got.br_startoff + got.br_blockcount;
3138 ASSERT(got_endoff >= del_endoff);
3139 delay = ISNULLSTARTBLOCK(got.br_startblock);
3140 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
3141 flags = 0;
3142 qfield = 0;
3143 error = 0;
3144 /*
3145 * If deleting a real allocation, must free up the disk space.
3146 */
3147 if (!delay) {
3148 flags = XFS_ILOG_CORE;
3149 /*
3150 * Realtime allocation. Free it and record di_nblocks update.
3151 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003152 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 xfs_fsblock_t bno;
3154 xfs_filblks_t len;
3155
3156 ASSERT(do_mod(del->br_blockcount,
3157 mp->m_sb.sb_rextsize) == 0);
3158 ASSERT(do_mod(del->br_startblock,
3159 mp->m_sb.sb_rextsize) == 0);
3160 bno = del->br_startblock;
3161 len = del->br_blockcount;
3162 do_div(bno, mp->m_sb.sb_rextsize);
3163 do_div(len, mp->m_sb.sb_rextsize);
3164 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3165 (xfs_extlen_t)len)))
3166 goto done;
3167 do_fx = 0;
3168 nblks = len * mp->m_sb.sb_rextsize;
3169 qfield = XFS_TRANS_DQ_RTBCOUNT;
3170 }
3171 /*
3172 * Ordinary allocation.
3173 */
3174 else {
3175 do_fx = 1;
3176 nblks = del->br_blockcount;
3177 qfield = XFS_TRANS_DQ_BCOUNT;
3178 }
3179 /*
3180 * Set up del_endblock and cur for later.
3181 */
3182 del_endblock = del->br_startblock + del->br_blockcount;
3183 if (cur) {
3184 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3185 got.br_startblock, got.br_blockcount,
3186 &i)))
3187 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003188 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 }
3190 da_old = da_new = 0;
3191 } else {
3192 da_old = STARTBLOCKVAL(got.br_startblock);
3193 da_new = 0;
3194 nblks = 0;
3195 do_fx = 0;
3196 }
3197 /*
3198 * Set flag value to use in switch statement.
3199 * Left-contig is 2, right-contig is 1.
3200 */
3201 switch (((got.br_startoff == del->br_startoff) << 1) |
3202 (got_endoff == del_endoff)) {
3203 case 3:
3204 /*
3205 * Matches the whole extent. Delete the entry.
3206 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10003207 XFS_BMAP_TRACE_DELETE("3", ip, idx, 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003208 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 ifp->if_lastex = idx;
3210 if (delay)
3211 break;
3212 XFS_IFORK_NEXT_SET(ip, whichfork,
3213 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3214 flags |= XFS_ILOG_CORE;
3215 if (!cur) {
3216 flags |= XFS_ILOG_FEXT(whichfork);
3217 break;
3218 }
3219 if ((error = xfs_bmbt_delete(cur, &i)))
3220 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003221 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 break;
3223
3224 case 2:
3225 /*
3226 * Deleting the first part of the extent.
3227 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10003228 XFS_BMAP_TRACE_PRE_UPDATE("2", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 xfs_bmbt_set_startoff(ep, del_endoff);
3230 temp = got.br_blockcount - del->br_blockcount;
3231 xfs_bmbt_set_blockcount(ep, temp);
3232 ifp->if_lastex = idx;
3233 if (delay) {
3234 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3235 da_old);
3236 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10003237 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 whichfork);
3239 da_new = temp;
3240 break;
3241 }
3242 xfs_bmbt_set_startblock(ep, del_endblock);
Eric Sandeen3a59c942007-07-11 11:09:47 +10003243 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 if (!cur) {
3245 flags |= XFS_ILOG_FEXT(whichfork);
3246 break;
3247 }
3248 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3249 got.br_blockcount - del->br_blockcount,
3250 got.br_state)))
3251 goto done;
3252 break;
3253
3254 case 1:
3255 /*
3256 * Deleting the last part of the extent.
3257 */
3258 temp = got.br_blockcount - del->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10003259 XFS_BMAP_TRACE_PRE_UPDATE("1", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 xfs_bmbt_set_blockcount(ep, temp);
3261 ifp->if_lastex = idx;
3262 if (delay) {
3263 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3264 da_old);
3265 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10003266 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 whichfork);
3268 da_new = temp;
3269 break;
3270 }
Eric Sandeen3a59c942007-07-11 11:09:47 +10003271 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 if (!cur) {
3273 flags |= XFS_ILOG_FEXT(whichfork);
3274 break;
3275 }
3276 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3277 got.br_startblock,
3278 got.br_blockcount - del->br_blockcount,
3279 got.br_state)))
3280 goto done;
3281 break;
3282
3283 case 0:
3284 /*
3285 * Deleting the middle of the extent.
3286 */
3287 temp = del->br_startoff - got.br_startoff;
Eric Sandeen3a59c942007-07-11 11:09:47 +10003288 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 xfs_bmbt_set_blockcount(ep, temp);
3290 new.br_startoff = del_endoff;
3291 temp2 = got_endoff - del_endoff;
3292 new.br_blockcount = temp2;
3293 new.br_state = got.br_state;
3294 if (!delay) {
3295 new.br_startblock = del_endblock;
3296 flags |= XFS_ILOG_CORE;
3297 if (cur) {
3298 if ((error = xfs_bmbt_update(cur,
3299 got.br_startoff,
3300 got.br_startblock, temp,
3301 got.br_state)))
3302 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11003303 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 goto done;
3305 cur->bc_rec.b = new;
3306 error = xfs_bmbt_insert(cur, &i);
3307 if (error && error != ENOSPC)
3308 goto done;
3309 /*
3310 * If get no-space back from btree insert,
3311 * it tried a split, and we have a zero
3312 * block reservation.
3313 * Fix up our state and return the error.
3314 */
3315 if (error == ENOSPC) {
3316 /*
3317 * Reset the cursor, don't trust
3318 * it after any insert operation.
3319 */
3320 if ((error = xfs_bmbt_lookup_eq(cur,
3321 got.br_startoff,
3322 got.br_startblock,
3323 temp, &i)))
3324 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003325 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 /*
3327 * Update the btree record back
3328 * to the original value.
3329 */
3330 if ((error = xfs_bmbt_update(cur,
3331 got.br_startoff,
3332 got.br_startblock,
3333 got.br_blockcount,
3334 got.br_state)))
3335 goto done;
3336 /*
3337 * Reset the extent record back
3338 * to the original value.
3339 */
3340 xfs_bmbt_set_blockcount(ep,
3341 got.br_blockcount);
3342 flags = 0;
3343 error = XFS_ERROR(ENOSPC);
3344 goto done;
3345 }
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003346 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 } else
3348 flags |= XFS_ILOG_FEXT(whichfork);
3349 XFS_IFORK_NEXT_SET(ip, whichfork,
3350 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3351 } else {
3352 ASSERT(whichfork == XFS_DATA_FORK);
3353 temp = xfs_bmap_worst_indlen(ip, temp);
3354 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3355 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3356 new.br_startblock = NULLSTARTBLOCK((int)temp2);
3357 da_new = temp + temp2;
3358 while (da_new > da_old) {
3359 if (temp) {
3360 temp--;
3361 da_new--;
3362 xfs_bmbt_set_startblock(ep,
3363 NULLSTARTBLOCK((int)temp));
3364 }
3365 if (da_new == da_old)
3366 break;
3367 if (temp2) {
3368 temp2--;
3369 da_new--;
3370 new.br_startblock =
3371 NULLSTARTBLOCK((int)temp2);
3372 }
3373 }
3374 }
Eric Sandeen3a59c942007-07-11 11:09:47 +10003375 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, whichfork);
3376 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 1, &new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003378 xfs_iext_insert(ifp, idx + 1, 1, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 ifp->if_lastex = idx + 1;
3380 break;
3381 }
3382 /*
3383 * If we need to, add to list of extents to delete.
3384 */
3385 if (do_fx)
3386 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3387 mp);
3388 /*
3389 * Adjust inode # blocks in the file.
3390 */
3391 if (nblks)
3392 ip->i_d.di_nblocks -= nblks;
3393 /*
3394 * Adjust quota data.
3395 */
3396 if (qfield)
3397 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
3398
3399 /*
3400 * Account for change in delayed indirect blocks.
3401 * Nothing to do for disk quota accounting here.
3402 */
3403 ASSERT(da_old >= da_new);
3404 if (da_old > da_new)
David Chinner20f4ebf2007-02-10 18:36:10 +11003405 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 rsvd);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003407 if (delta) {
3408 /* DELTA: report the original extent. */
3409 if (delta->xed_startoff > got.br_startoff)
3410 delta->xed_startoff = got.br_startoff;
3411 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3412 delta->xed_blockcount = got.br_startoff +
3413 got.br_blockcount;
3414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415done:
3416 *logflagsp = flags;
3417 return error;
3418}
3419
3420/*
3421 * Remove the entry "free" from the free item list. Prev points to the
3422 * previous entry, unless "free" is the head of the list.
3423 */
3424STATIC void
3425xfs_bmap_del_free(
3426 xfs_bmap_free_t *flist, /* free item list header */
3427 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3428 xfs_bmap_free_item_t *free) /* list item to be freed */
3429{
3430 if (prev)
3431 prev->xbfi_next = free->xbfi_next;
3432 else
3433 flist->xbf_first = free->xbfi_next;
3434 flist->xbf_count--;
3435 kmem_zone_free(xfs_bmap_free_item_zone, free);
3436}
3437
3438/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 * Convert an extents-format file into a btree-format file.
3440 * The new file will have a root block (in the inode) and a single child block.
3441 */
3442STATIC int /* error */
3443xfs_bmap_extents_to_btree(
3444 xfs_trans_t *tp, /* transaction pointer */
3445 xfs_inode_t *ip, /* incore inode pointer */
3446 xfs_fsblock_t *firstblock, /* first-block-allocated */
3447 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3448 xfs_btree_cur_t **curp, /* cursor returned to caller */
3449 int wasdel, /* converting a delayed alloc */
3450 int *logflagsp, /* inode logging flags */
3451 int whichfork) /* data or attr fork */
3452{
3453 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
3454 xfs_buf_t *abp; /* buffer for ablock */
3455 xfs_alloc_arg_t args; /* allocation arguments */
3456 xfs_bmbt_rec_t *arp; /* child record pointer */
3457 xfs_bmbt_block_t *block; /* btree root block */
3458 xfs_btree_cur_t *cur; /* bmap btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003459 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003461 xfs_extnum_t i, cnt; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 xfs_ifork_t *ifp; /* inode fork pointer */
3463 xfs_bmbt_key_t *kp; /* root block key pointer */
3464 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003465 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3467
3468 ifp = XFS_IFORK_PTR(ip, whichfork);
3469 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3470 ASSERT(ifp->if_ext_max ==
3471 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3472 /*
3473 * Make space in the inode incore.
3474 */
3475 xfs_iroot_realloc(ip, 1, whichfork);
3476 ifp->if_flags |= XFS_IFBROOT;
3477 /*
3478 * Fill in the root.
3479 */
3480 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003481 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3482 block->bb_level = cpu_to_be16(1);
3483 block->bb_numrecs = cpu_to_be16(1);
3484 block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3485 block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 /*
3487 * Need a cursor. Can't allocate until bb_level is filled in.
3488 */
3489 mp = ip->i_mount;
Christoph Hellwig561f7d12008-10-30 16:53:59 +11003490 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 cur->bc_private.b.firstblock = *firstblock;
3492 cur->bc_private.b.flist = flist;
3493 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3494 /*
3495 * Convert to a btree with two levels, one record in root.
3496 */
3497 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3498 args.tp = tp;
3499 args.mp = mp;
Yingping Lud210a282006-06-09 14:55:18 +10003500 args.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 if (*firstblock == NULLFSBLOCK) {
3502 args.type = XFS_ALLOCTYPE_START_BNO;
3503 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3504 } else if (flist->xbf_low) {
3505 args.type = XFS_ALLOCTYPE_START_BNO;
3506 args.fsbno = *firstblock;
3507 } else {
3508 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3509 args.fsbno = *firstblock;
3510 }
3511 args.minlen = args.maxlen = args.prod = 1;
3512 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3513 args.minalignslop = 0;
3514 args.wasdel = wasdel;
3515 *logflagsp = 0;
3516 if ((error = xfs_alloc_vextent(&args))) {
3517 xfs_iroot_realloc(ip, -1, whichfork);
3518 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3519 return error;
3520 }
3521 /*
3522 * Allocation can't fail, the space was reserved.
3523 */
3524 ASSERT(args.fsbno != NULLFSBLOCK);
3525 ASSERT(*firstblock == NULLFSBLOCK ||
3526 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3527 (flist->xbf_low &&
3528 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3529 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3530 cur->bc_private.b.allocated++;
3531 ip->i_d.di_nblocks++;
3532 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3533 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3534 /*
3535 * Fill in the child block.
3536 */
3537 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003538 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 ablock->bb_level = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003540 ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3541 ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3543 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003544 for (cnt = i = 0; i < nextents; i++) {
3545 ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10003547 arp->l0 = cpu_to_be64(ep->l0);
3548 arp->l1 = cpu_to_be64(ep->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 arp++; cnt++;
3550 }
3551 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11003552 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3553 ablock->bb_numrecs = cpu_to_be16(cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 /*
3555 * Fill in the root key and pointer.
3556 */
3557 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
3558 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
Christoph Hellwig8801bb92006-09-28 10:58:17 +10003559 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
Christoph Hellwig576039c2006-09-28 10:58:06 +10003561 *pp = cpu_to_be64(args.fsbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 /*
3563 * Do all this logging at the end so that
3564 * the root is at the right level.
3565 */
3566 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003567 xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 ASSERT(*curp == NULL);
3569 *curp = cur;
3570 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
3571 return 0;
3572}
3573
3574/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11003575 * Helper routine to reset inode di_forkoff field when switching
3576 * attribute fork from local to extent format - we reset it where
3577 * possible to make space available for inline data fork extents.
3578 */
3579STATIC void
3580xfs_bmap_forkoff_reset(
3581 xfs_mount_t *mp,
3582 xfs_inode_t *ip,
3583 int whichfork)
3584{
3585 if (whichfork == XFS_ATTR_FORK &&
3586 (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
3587 (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
Barry Naujoke5889e92007-02-10 18:35:58 +11003588 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
Nathan Scottd8cc8902005-11-02 10:34:53 +11003589 ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
3590 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3591 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
3592 (uint)sizeof(xfs_bmbt_rec_t);
3593 ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
3594 (uint)sizeof(xfs_bmbt_rec_t);
3595 }
3596}
3597
3598/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 * Convert a local file to an extents file.
3600 * This code is out of bounds for data forks of regular files,
3601 * since the file data needs to get logged so things will stay consistent.
3602 * (The bmap-level manipulations are ok, though).
3603 */
3604STATIC int /* error */
3605xfs_bmap_local_to_extents(
3606 xfs_trans_t *tp, /* transaction pointer */
3607 xfs_inode_t *ip, /* incore inode pointer */
3608 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3609 xfs_extlen_t total, /* total blocks needed by transaction */
3610 int *logflagsp, /* inode logging flags */
3611 int whichfork) /* data or attr fork */
3612{
3613 int error; /* error return value */
3614 int flags; /* logging flags returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 xfs_ifork_t *ifp; /* inode fork pointer */
3616
3617 /*
3618 * We don't want to deal with the case of keeping inode data inline yet.
3619 * So sending the data fork of a regular inode is invalid.
3620 */
3621 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3622 whichfork == XFS_DATA_FORK));
3623 ifp = XFS_IFORK_PTR(ip, whichfork);
3624 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3625 flags = 0;
3626 error = 0;
3627 if (ifp->if_bytes) {
3628 xfs_alloc_arg_t args; /* allocation arguments */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003629 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003630 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
3632 args.tp = tp;
3633 args.mp = ip->i_mount;
Yingping Lud210a282006-06-09 14:55:18 +10003634 args.firstblock = *firstblock;
Mandy Kirkconnellf020b672006-03-14 14:07:24 +11003635 ASSERT((ifp->if_flags &
3636 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 /*
3638 * Allocate a block. We know we need only one, since the
3639 * file currently fits in an inode.
3640 */
3641 if (*firstblock == NULLFSBLOCK) {
3642 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3643 args.type = XFS_ALLOCTYPE_START_BNO;
3644 } else {
3645 args.fsbno = *firstblock;
3646 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3647 }
3648 args.total = total;
3649 args.mod = args.minleft = args.alignment = args.wasdel =
3650 args.isfl = args.minalignslop = 0;
3651 args.minlen = args.maxlen = args.prod = 1;
3652 if ((error = xfs_alloc_vextent(&args)))
3653 goto done;
3654 /*
3655 * Can't fail, the space was reserved.
3656 */
3657 ASSERT(args.fsbno != NULLFSBLOCK);
3658 ASSERT(args.len == 1);
3659 *firstblock = args.fsbno;
3660 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3661 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3662 ifp->if_bytes);
3663 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003664 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003666 xfs_iext_add(ifp, 0, 1);
3667 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
Eric Sandeen3a59c942007-07-11 11:09:47 +10003669 XFS_BMAP_TRACE_POST_UPDATE("new", ip, 0, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3671 ip->i_d.di_nblocks = 1;
3672 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
3673 XFS_TRANS_DQ_BCOUNT, 1L);
3674 flags |= XFS_ILOG_FEXT(whichfork);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003675 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003677 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 ifp->if_flags &= ~XFS_IFINLINE;
3680 ifp->if_flags |= XFS_IFEXTENTS;
3681 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3682 flags |= XFS_ILOG_CORE;
3683done:
3684 *logflagsp = flags;
3685 return error;
3686}
3687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003689 * Search the extent records for the entry containing block bno.
3690 * If bno lies in a hole, point to the next entry. If bno lies
3691 * past eof, *eofp will be set, and *prevp will contain the last
3692 * entry (null if none). Else, *lastxp will be set to the index
3693 * of the found entry; *gotp will contain the entry.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003694 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003695xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003696xfs_bmap_search_multi_extents(
3697 xfs_ifork_t *ifp, /* inode fork pointer */
3698 xfs_fileoff_t bno, /* block number searched for */
3699 int *eofp, /* out: end of file found */
3700 xfs_extnum_t *lastxp, /* out: last extent index */
3701 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3702 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3703{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003704 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003705 xfs_extnum_t lastx; /* last extent index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003706
3707 /*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003708 * Initialize the extent entry structure to catch access to
3709 * uninitialized br_startblock field.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003710 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003711 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3712 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3713 gotp->br_state = XFS_EXT_INVALID;
3714#if XFS_BIG_BLKNOS
3715 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3716#else
3717 gotp->br_startblock = 0xffffa5a5;
3718#endif
3719 prevp->br_startoff = NULLFILEOFF;
3720
3721 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3722 if (lastx > 0) {
3723 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3724 }
3725 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3726 xfs_bmbt_get_all(ep, gotp);
3727 *eofp = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003728 } else {
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003729 if (lastx > 0) {
3730 *gotp = *prevp;
3731 }
3732 *eofp = 1;
3733 ep = NULL;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003734 }
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003735 *lastxp = lastx;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003736 return ep;
3737}
3738
3739/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 * Search the extents list for the inode, for the extent containing bno.
3741 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3742 * *eofp will be set, and *prevp will contain the last entry (null if none).
3743 * Else, *lastxp will be set to the index of the found
3744 * entry; *gotp will contain the entry.
3745 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003746STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747xfs_bmap_search_extents(
3748 xfs_inode_t *ip, /* incore inode pointer */
3749 xfs_fileoff_t bno, /* block number searched for */
Nathan Scott572d95f2006-09-28 11:03:20 +10003750 int fork, /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 int *eofp, /* out: end of file found */
3752 xfs_extnum_t *lastxp, /* out: last extent index */
3753 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3754 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3755{
3756 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003757 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
3759 XFS_STATS_INC(xs_look_exlist);
Nathan Scott572d95f2006-09-28 11:03:20 +10003760 ifp = XFS_IFORK_PTR(ip, fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003762 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3763
Nathan Scott572d95f2006-09-28 11:03:20 +10003764 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3765 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3766 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3767 "Access to block zero in inode %llu "
3768 "start_block: %llx start_off: %llx "
3769 "blkcnt: %llx extent-state: %x lastx: %x\n",
3770 (unsigned long long)ip->i_ino,
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003771 (unsigned long long)gotp->br_startblock,
3772 (unsigned long long)gotp->br_startoff,
3773 (unsigned long long)gotp->br_blockcount,
Nathan Scott572d95f2006-09-28 11:03:20 +10003774 gotp->br_state, *lastxp);
3775 *lastxp = NULLEXTNUM;
3776 *eofp = 1;
3777 return NULL;
3778 }
3779 return ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780}
3781
3782
3783#ifdef XFS_BMAP_TRACE
3784ktrace_t *xfs_bmap_trace_buf;
3785
3786/*
3787 * Add a bmap trace buffer entry. Base routine for the others.
3788 */
3789STATIC void
3790xfs_bmap_trace_addentry(
3791 int opcode, /* operation */
Eric Sandeen3a59c942007-07-11 11:09:47 +10003792 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 char *desc, /* operation description */
3794 xfs_inode_t *ip, /* incore inode pointer */
3795 xfs_extnum_t idx, /* index of entry(ies) */
3796 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003797 xfs_bmbt_rec_host_t *r1, /* first record */
3798 xfs_bmbt_rec_host_t *r2, /* second record or null */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 int whichfork) /* data or attr fork */
3800{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003801 xfs_bmbt_rec_host_t tr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
3803 ASSERT(cnt == 1 || cnt == 2);
3804 ASSERT(r1 != NULL);
3805 if (cnt == 1) {
3806 ASSERT(r2 == NULL);
3807 r2 = &tr2;
3808 memset(&tr2, 0, sizeof(tr2));
3809 } else
3810 ASSERT(r2 != NULL);
3811 ktrace_enter(xfs_bmap_trace_buf,
3812 (void *)(__psint_t)(opcode | (whichfork << 16)),
3813 (void *)fname, (void *)desc, (void *)ip,
3814 (void *)(__psint_t)idx,
3815 (void *)(__psint_t)cnt,
3816 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3817 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3818 (void *)(__psunsigned_t)(r1->l0 >> 32),
3819 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3820 (void *)(__psunsigned_t)(r1->l1 >> 32),
3821 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3822 (void *)(__psunsigned_t)(r2->l0 >> 32),
3823 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3824 (void *)(__psunsigned_t)(r2->l1 >> 32),
3825 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3826 );
3827 ASSERT(ip->i_xtrace);
3828 ktrace_enter(ip->i_xtrace,
3829 (void *)(__psint_t)(opcode | (whichfork << 16)),
3830 (void *)fname, (void *)desc, (void *)ip,
3831 (void *)(__psint_t)idx,
3832 (void *)(__psint_t)cnt,
3833 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3834 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3835 (void *)(__psunsigned_t)(r1->l0 >> 32),
3836 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3837 (void *)(__psunsigned_t)(r1->l1 >> 32),
3838 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3839 (void *)(__psunsigned_t)(r2->l0 >> 32),
3840 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3841 (void *)(__psunsigned_t)(r2->l1 >> 32),
3842 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3843 );
3844}
3845
3846/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003847 * Add bmap trace entry prior to a call to xfs_iext_remove.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 */
3849STATIC void
3850xfs_bmap_trace_delete(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003851 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 char *desc, /* operation description */
3853 xfs_inode_t *ip, /* incore inode pointer */
3854 xfs_extnum_t idx, /* index of entry(entries) deleted */
3855 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3856 int whichfork) /* data or attr fork */
3857{
3858 xfs_ifork_t *ifp; /* inode fork pointer */
3859
3860 ifp = XFS_IFORK_PTR(ip, whichfork);
3861 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003862 cnt, xfs_iext_get_ext(ifp, idx),
3863 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 whichfork);
3865}
3866
3867/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003868 * Add bmap trace entry prior to a call to xfs_iext_insert, or
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 * reading in the extents list from the disk (in the btree).
3870 */
3871STATIC void
3872xfs_bmap_trace_insert(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003873 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 char *desc, /* operation description */
3875 xfs_inode_t *ip, /* incore inode pointer */
3876 xfs_extnum_t idx, /* index of entry(entries) inserted */
3877 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3878 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3879 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3880 int whichfork) /* data or attr fork */
3881{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003882 xfs_bmbt_rec_host_t tr1; /* compressed record 1 */
3883 xfs_bmbt_rec_host_t tr2; /* compressed record 2 if needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
3885 xfs_bmbt_set_all(&tr1, r1);
3886 if (cnt == 2) {
3887 ASSERT(r2 != NULL);
3888 xfs_bmbt_set_all(&tr2, r2);
3889 } else {
3890 ASSERT(cnt == 1);
3891 ASSERT(r2 == NULL);
3892 }
3893 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3894 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3895}
3896
3897/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003898 * Add bmap trace entry after updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 */
3900STATIC void
3901xfs_bmap_trace_post_update(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003902 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 char *desc, /* operation description */
3904 xfs_inode_t *ip, /* incore inode pointer */
3905 xfs_extnum_t idx, /* index of entry updated */
3906 int whichfork) /* data or attr fork */
3907{
3908 xfs_ifork_t *ifp; /* inode fork pointer */
3909
3910 ifp = XFS_IFORK_PTR(ip, whichfork);
3911 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003912 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913}
3914
3915/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003916 * Add bmap trace entry prior to updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 */
3918STATIC void
3919xfs_bmap_trace_pre_update(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003920 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 char *desc, /* operation description */
3922 xfs_inode_t *ip, /* incore inode pointer */
3923 xfs_extnum_t idx, /* index of entry to be updated */
3924 int whichfork) /* data or attr fork */
3925{
3926 xfs_ifork_t *ifp; /* inode fork pointer */
3927
3928 ifp = XFS_IFORK_PTR(ip, whichfork);
3929 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003930 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931}
3932#endif /* XFS_BMAP_TRACE */
3933
3934/*
3935 * Compute the worst-case number of indirect blocks that will be used
3936 * for ip's delayed extent of length "len".
3937 */
3938STATIC xfs_filblks_t
3939xfs_bmap_worst_indlen(
3940 xfs_inode_t *ip, /* incore inode pointer */
3941 xfs_filblks_t len) /* delayed extent length */
3942{
3943 int level; /* btree level number */
3944 int maxrecs; /* maximum record count at this level */
3945 xfs_mount_t *mp; /* mount structure */
3946 xfs_filblks_t rval; /* return value */
3947
3948 mp = ip->i_mount;
3949 maxrecs = mp->m_bmap_dmxr[0];
3950 for (level = 0, rval = 0;
3951 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3952 level++) {
3953 len += maxrecs - 1;
3954 do_div(len, maxrecs);
3955 rval += len;
3956 if (len == 1)
3957 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3958 level - 1;
3959 if (level == 0)
3960 maxrecs = mp->m_bmap_dmxr[1];
3961 }
3962 return rval;
3963}
3964
3965#if defined(XFS_RW_TRACE)
3966STATIC void
3967xfs_bunmap_trace(
3968 xfs_inode_t *ip,
3969 xfs_fileoff_t bno,
3970 xfs_filblks_t len,
3971 int flags,
3972 inst_t *ra)
3973{
3974 if (ip->i_rwtrace == NULL)
3975 return;
3976 ktrace_enter(ip->i_rwtrace,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003977 (void *)(__psint_t)XFS_BUNMAP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 (void *)ip,
3979 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3980 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3981 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3982 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3983 (void *)(__psint_t)len,
3984 (void *)(__psint_t)flags,
3985 (void *)(unsigned long)current_cpu(),
3986 (void *)ra,
3987 (void *)0,
3988 (void *)0,
3989 (void *)0,
3990 (void *)0,
3991 (void *)0,
3992 (void *)0);
3993}
3994#endif
3995
3996/*
3997 * Convert inode from non-attributed to attributed.
3998 * Must not be in a transaction, ip must not be locked.
3999 */
4000int /* error code */
4001xfs_bmap_add_attrfork(
4002 xfs_inode_t *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11004003 int size, /* space new attribute needs */
4004 int rsvd) /* xact may use reserved blks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004007 xfs_bmap_free_t flist; /* freed extent records */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 xfs_trans_t *tp; /* transaction pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11004010 int blks; /* space reservation */
4011 int version = 1; /* superblock attr version */
4012 int committed; /* xaction was committed */
4013 int logflags; /* logging flags */
4014 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
Nathan Scottd8cc8902005-11-02 10:34:53 +11004016 ASSERT(XFS_IFORK_Q(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 ASSERT(ip->i_df.if_ext_max ==
4018 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
Nathan Scottd8cc8902005-11-02 10:34:53 +11004019
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 mp = ip->i_mount;
4021 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
4022 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
4023 blks = XFS_ADDAFORK_SPACE_RES(mp);
4024 if (rsvd)
4025 tp->t_flags |= XFS_TRANS_RESERVE;
4026 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
4027 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
4028 goto error0;
4029 xfs_ilock(ip, XFS_ILOCK_EXCL);
4030 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
4031 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
4032 XFS_QMOPT_RES_REGBLKS);
4033 if (error) {
4034 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4035 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
4036 return error;
4037 }
4038 if (XFS_IFORK_Q(ip))
4039 goto error1;
4040 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
4041 /*
4042 * For inodes coming from pre-6.2 filesystems.
4043 */
4044 ASSERT(ip->i_d.di_aformat == 0);
4045 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
4046 }
4047 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwig0b1f91772008-08-13 16:13:09 +10004048 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4050 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4051 switch (ip->i_d.di_format) {
4052 case XFS_DINODE_FMT_DEV:
4053 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
4054 break;
4055 case XFS_DINODE_FMT_UUID:
4056 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
4057 break;
4058 case XFS_DINODE_FMT_LOCAL:
4059 case XFS_DINODE_FMT_EXTENTS:
4060 case XFS_DINODE_FMT_BTREE:
Nathan Scottd8cc8902005-11-02 10:34:53 +11004061 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
4062 if (!ip->i_d.di_forkoff)
4063 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
Nathan Scott13059ff2006-01-11 15:32:01 +11004064 else if (mp->m_flags & XFS_MOUNT_ATTR2)
Nathan Scottd8cc8902005-11-02 10:34:53 +11004065 version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 break;
4067 default:
4068 ASSERT(0);
4069 error = XFS_ERROR(EINVAL);
4070 goto error1;
4071 }
4072 ip->i_df.if_ext_max =
4073 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4074 ASSERT(ip->i_afp == NULL);
4075 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
4076 ip->i_afp->if_ext_max =
4077 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4078 ip->i_afp->if_flags = XFS_IFEXTENTS;
4079 logflags = 0;
4080 XFS_BMAP_INIT(&flist, &firstblock);
4081 switch (ip->i_d.di_format) {
4082 case XFS_DINODE_FMT_LOCAL:
4083 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
4084 &logflags);
4085 break;
4086 case XFS_DINODE_FMT_EXTENTS:
4087 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
4088 &flist, &logflags);
4089 break;
4090 case XFS_DINODE_FMT_BTREE:
4091 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
4092 &logflags);
4093 break;
4094 default:
4095 error = 0;
4096 break;
4097 }
4098 if (logflags)
4099 xfs_trans_log_inode(tp, ip, logflags);
4100 if (error)
4101 goto error2;
Eric Sandeen62118702008-03-06 13:44:28 +11004102 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
4103 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +11004104 __int64_t sbfields = 0;
4105
Eric Sandeen3685c2a2007-10-11 17:42:32 +10004106 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +11004107 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
4108 xfs_sb_version_addattr(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11004109 sbfields |= XFS_SB_VERSIONNUM;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004110 }
Eric Sandeen62118702008-03-06 13:44:28 +11004111 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
4112 xfs_sb_version_addattr2(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11004113 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
Nathan Scottd8cc8902005-11-02 10:34:53 +11004114 }
Nathan Scottda087ba2005-11-02 15:00:20 +11004115 if (sbfields) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10004116 spin_unlock(&mp->m_sb_lock);
Nathan Scottda087ba2005-11-02 15:00:20 +11004117 xfs_mod_sb(tp, sbfields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +10004119 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 }
Eric Sandeenf7c99b62007-02-10 18:37:16 +11004121 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 goto error2;
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004123 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 ASSERT(ip->i_df.if_ext_max ==
4125 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4126 return error;
4127error2:
4128 xfs_bmap_cancel(&flist);
4129error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4131error0:
4132 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4133 ASSERT(ip->i_df.if_ext_max ==
4134 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4135 return error;
4136}
4137
4138/*
4139 * Add the extent to the list of extents to be free at transaction end.
4140 * The list is maintained sorted (by block number).
4141 */
4142/* ARGSUSED */
4143void
4144xfs_bmap_add_free(
4145 xfs_fsblock_t bno, /* fs block number of extent */
4146 xfs_filblks_t len, /* length of extent */
4147 xfs_bmap_free_t *flist, /* list of extents */
4148 xfs_mount_t *mp) /* mount point structure */
4149{
4150 xfs_bmap_free_item_t *cur; /* current (next) element */
4151 xfs_bmap_free_item_t *new; /* new element */
4152 xfs_bmap_free_item_t *prev; /* previous element */
4153#ifdef DEBUG
4154 xfs_agnumber_t agno;
4155 xfs_agblock_t agbno;
4156
4157 ASSERT(bno != NULLFSBLOCK);
4158 ASSERT(len > 0);
4159 ASSERT(len <= MAXEXTLEN);
4160 ASSERT(!ISNULLSTARTBLOCK(bno));
4161 agno = XFS_FSB_TO_AGNO(mp, bno);
4162 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4163 ASSERT(agno < mp->m_sb.sb_agcount);
4164 ASSERT(agbno < mp->m_sb.sb_agblocks);
4165 ASSERT(len < mp->m_sb.sb_agblocks);
4166 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4167#endif
4168 ASSERT(xfs_bmap_free_item_zone != NULL);
4169 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4170 new->xbfi_startblock = bno;
4171 new->xbfi_blockcount = (xfs_extlen_t)len;
4172 for (prev = NULL, cur = flist->xbf_first;
4173 cur != NULL;
4174 prev = cur, cur = cur->xbfi_next) {
4175 if (cur->xbfi_startblock >= bno)
4176 break;
4177 }
4178 if (prev)
4179 prev->xbfi_next = new;
4180 else
4181 flist->xbf_first = new;
4182 new->xbfi_next = cur;
4183 flist->xbf_count++;
4184}
4185
4186/*
4187 * Compute and fill in the value of the maximum depth of a bmap btree
4188 * in this filesystem. Done once, during mount.
4189 */
4190void
4191xfs_bmap_compute_maxlevels(
4192 xfs_mount_t *mp, /* file system mount structure */
4193 int whichfork) /* data or attr fork */
4194{
4195 int level; /* btree level */
4196 uint maxblocks; /* max blocks at this level */
4197 uint maxleafents; /* max leaf entries possible */
4198 int maxrootrecs; /* max records in root block */
4199 int minleafrecs; /* min records in leaf block */
4200 int minnoderecs; /* min records in node block */
4201 int sz; /* root block size */
4202
4203 /*
4204 * The maximum number of extents in a file, hence the maximum
4205 * number of leaf entries, is controlled by the type of di_nextents
4206 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4207 * (a signed 16-bit number, xfs_aextnum_t).
Tim Shimmin6d1337b2008-04-17 16:50:16 +10004208 *
4209 * Note that we can no longer assume that if we are in ATTR1 that
4210 * the fork offset of all the inodes will be (m_attroffset >> 3)
4211 * because we could have mounted with ATTR2 and then mounted back
4212 * with ATTR1, keeping the di_forkoff's fixed but probably at
4213 * various positions. Therefore, for both ATTR1 and ATTR2
4214 * we have to assume the worst case scenario of a minimum size
4215 * available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11004217 if (whichfork == XFS_DATA_FORK) {
4218 maxleafents = MAXEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10004219 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11004220 } else {
4221 maxleafents = MAXAEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10004222 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11004223 }
4224 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 minleafrecs = mp->m_bmap_dmnr[0];
4226 minnoderecs = mp->m_bmap_dmnr[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4228 for (level = 1; maxblocks > 1; level++) {
4229 if (maxblocks <= maxrootrecs)
4230 maxblocks = 1;
4231 else
4232 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4233 }
4234 mp->m_bm_maxlevels[whichfork] = level;
4235}
4236
4237/*
4238 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4239 * caller. Frees all the extents that need freeing, which must be done
4240 * last due to locking considerations. We never free any extents in
4241 * the first transaction. This is to allow the caller to make the first
4242 * transaction a synchronous one so that the pointers to the data being
4243 * broken in this transaction will be permanent before the data is actually
4244 * freed. This is necessary to prevent blocks from being reallocated
4245 * and written to before the free and reallocation are actually permanent.
4246 * We do not just make the first transaction synchronous here, because
4247 * there are more efficient ways to gain the same protection in some cases
4248 * (see the file truncation code).
4249 *
4250 * Return 1 if the given transaction was committed and a new one
4251 * started, and 0 otherwise in the committed parameter.
4252 */
4253/*ARGSUSED*/
4254int /* error */
4255xfs_bmap_finish(
4256 xfs_trans_t **tp, /* transaction pointer addr */
4257 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 int *committed) /* xact committed or not */
4259{
4260 xfs_efd_log_item_t *efd; /* extent free data */
4261 xfs_efi_log_item_t *efi; /* extent free intention */
4262 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004263 xfs_bmap_free_item_t *free; /* free extent item */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 unsigned int logres; /* new log reservation */
4265 unsigned int logcount; /* new log count */
4266 xfs_mount_t *mp; /* filesystem mount structure */
4267 xfs_bmap_free_item_t *next; /* next item on free list */
4268 xfs_trans_t *ntp; /* new transaction pointer */
4269
4270 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4271 if (flist->xbf_count == 0) {
4272 *committed = 0;
4273 return 0;
4274 }
4275 ntp = *tp;
4276 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4277 for (free = flist->xbf_first; free; free = free->xbfi_next)
4278 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4279 free->xbfi_blockcount);
4280 logres = ntp->t_log_res;
4281 logcount = ntp->t_log_count;
4282 ntp = xfs_trans_dup(*tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004283 error = xfs_trans_commit(*tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 *tp = ntp;
4285 *committed = 1;
4286 /*
4287 * We have a new transaction, so we should return committed=1,
4288 * even though we're returning an error.
4289 */
4290 if (error) {
4291 return error;
4292 }
4293 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4294 logcount)))
4295 return error;
4296 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4297 for (free = flist->xbf_first; free != NULL; free = next) {
4298 next = free->xbfi_next;
4299 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4300 free->xbfi_blockcount))) {
4301 /*
4302 * The bmap free list will be cleaned up at a
4303 * higher level. The EFI will be canceled when
4304 * this transaction is aborted.
4305 * Need to force shutdown here to make sure it
4306 * happens, since this transaction may not be
4307 * dirty yet.
4308 */
4309 mp = ntp->t_mountp;
4310 if (!XFS_FORCED_SHUTDOWN(mp))
4311 xfs_force_shutdown(mp,
4312 (error == EFSCORRUPTED) ?
Nathan Scott7d04a332006-06-09 14:58:38 +10004313 SHUTDOWN_CORRUPT_INCORE :
4314 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 return error;
4316 }
4317 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4318 free->xbfi_blockcount);
4319 xfs_bmap_del_free(flist, NULL, free);
4320 }
4321 return 0;
4322}
4323
4324/*
4325 * Free up any items left in the list.
4326 */
4327void
4328xfs_bmap_cancel(
4329 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4330{
4331 xfs_bmap_free_item_t *free; /* free list item */
4332 xfs_bmap_free_item_t *next;
4333
4334 if (flist->xbf_count == 0)
4335 return;
4336 ASSERT(flist->xbf_first != NULL);
4337 for (free = flist->xbf_first; free; free = next) {
4338 next = free->xbfi_next;
4339 xfs_bmap_del_free(flist, NULL, free);
4340 }
4341 ASSERT(flist->xbf_count == 0);
4342}
4343
4344/*
4345 * Returns the file-relative block number of the first unused block(s)
4346 * in the file with at least "len" logically contiguous blocks free.
4347 * This is the lowest-address hole if the file has holes, else the first block
4348 * past the end of file.
4349 * Return 0 if the file is currently local (in-inode).
4350 */
4351int /* error */
4352xfs_bmap_first_unused(
4353 xfs_trans_t *tp, /* transaction pointer */
4354 xfs_inode_t *ip, /* incore inode */
4355 xfs_extlen_t len, /* size of hole to find */
4356 xfs_fileoff_t *first_unused, /* unused block */
4357 int whichfork) /* data or attr fork */
4358{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004360 int idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 xfs_ifork_t *ifp; /* inode fork pointer */
4362 xfs_fileoff_t lastaddr; /* last block number seen */
4363 xfs_fileoff_t lowest; /* lowest useful block */
4364 xfs_fileoff_t max; /* starting useful block */
4365 xfs_fileoff_t off; /* offset for this block */
4366 xfs_extnum_t nextents; /* number of extent entries */
4367
4368 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4369 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4370 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4371 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4372 *first_unused = 0;
4373 return 0;
4374 }
4375 ifp = XFS_IFORK_PTR(ip, whichfork);
4376 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4377 (error = xfs_iread_extents(tp, ip, whichfork)))
4378 return error;
4379 lowest = *first_unused;
4380 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004381 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004382 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 off = xfs_bmbt_get_startoff(ep);
4384 /*
4385 * See if the hole before this extent will work.
4386 */
4387 if (off >= lowest + len && off - max >= len) {
4388 *first_unused = max;
4389 return 0;
4390 }
4391 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4392 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4393 }
4394 *first_unused = max;
4395 return 0;
4396}
4397
4398/*
4399 * Returns the file-relative block number of the last block + 1 before
4400 * last_block (input value) in the file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004401 * This is not based on i_size, it is based on the extent records.
4402 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 */
4404int /* error */
4405xfs_bmap_last_before(
4406 xfs_trans_t *tp, /* transaction pointer */
4407 xfs_inode_t *ip, /* incore inode */
4408 xfs_fileoff_t *last_block, /* last block */
4409 int whichfork) /* data or attr fork */
4410{
4411 xfs_fileoff_t bno; /* input file offset */
4412 int eof; /* hit end of file */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004413 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 int error; /* error return value */
4415 xfs_bmbt_irec_t got; /* current extent value */
4416 xfs_ifork_t *ifp; /* inode fork pointer */
4417 xfs_extnum_t lastx; /* last extent used */
4418 xfs_bmbt_irec_t prev; /* previous extent value */
4419
4420 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4421 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4422 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4423 return XFS_ERROR(EIO);
4424 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4425 *last_block = 0;
4426 return 0;
4427 }
4428 ifp = XFS_IFORK_PTR(ip, whichfork);
4429 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4430 (error = xfs_iread_extents(tp, ip, whichfork)))
4431 return error;
4432 bno = *last_block - 1;
4433 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4434 &prev);
4435 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4436 if (prev.br_startoff == NULLFILEOFF)
4437 *last_block = 0;
4438 else
4439 *last_block = prev.br_startoff + prev.br_blockcount;
4440 }
4441 /*
4442 * Otherwise *last_block is already the right answer.
4443 */
4444 return 0;
4445}
4446
4447/*
4448 * Returns the file-relative block number of the first block past eof in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004449 * the file. This is not based on i_size, it is based on the extent records.
4450 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 */
4452int /* error */
4453xfs_bmap_last_offset(
4454 xfs_trans_t *tp, /* transaction pointer */
4455 xfs_inode_t *ip, /* incore inode */
4456 xfs_fileoff_t *last_block, /* last block */
4457 int whichfork) /* data or attr fork */
4458{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004459 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 int error; /* error return value */
4461 xfs_ifork_t *ifp; /* inode fork pointer */
4462 xfs_extnum_t nextents; /* number of extent entries */
4463
4464 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4465 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4466 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4467 return XFS_ERROR(EIO);
4468 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4469 *last_block = 0;
4470 return 0;
4471 }
4472 ifp = XFS_IFORK_PTR(ip, whichfork);
4473 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4474 (error = xfs_iread_extents(tp, ip, whichfork)))
4475 return error;
4476 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4477 if (!nextents) {
4478 *last_block = 0;
4479 return 0;
4480 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004481 ep = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4483 return 0;
4484}
4485
4486/*
4487 * Returns whether the selected fork of the inode has exactly one
4488 * block or not. For the data fork we check this matches di_size,
4489 * implying the file's range is 0..bsize-1.
4490 */
4491int /* 1=>1 block, 0=>otherwise */
4492xfs_bmap_one_block(
4493 xfs_inode_t *ip, /* incore inode */
4494 int whichfork) /* data or attr fork */
4495{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004496 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 xfs_ifork_t *ifp; /* inode fork pointer */
4498 int rval; /* return value */
4499 xfs_bmbt_irec_t s; /* internal version of extent */
4500
4501#ifndef DEBUG
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004502 if (whichfork == XFS_DATA_FORK) {
4503 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4504 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4505 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507#endif /* !DEBUG */
4508 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4509 return 0;
4510 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4511 return 0;
4512 ifp = XFS_IFORK_PTR(ip, whichfork);
4513 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004514 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 xfs_bmbt_get_all(ep, &s);
4516 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4517 if (rval && whichfork == XFS_DATA_FORK)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004518 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 return rval;
4520}
4521
4522/*
4523 * Read in the extents to if_extents.
4524 * All inode fields are set up by caller, we just traverse the btree
4525 * and copy the records in. If the file system cannot contain unwritten
4526 * extents, the records are checked for no "state" flags.
4527 */
4528int /* error */
4529xfs_bmap_read_extents(
4530 xfs_trans_t *tp, /* transaction pointer */
4531 xfs_inode_t *ip, /* incore inode */
4532 int whichfork) /* data or attr fork */
4533{
4534 xfs_bmbt_block_t *block; /* current btree block */
4535 xfs_fsblock_t bno; /* block # of "block" */
4536 xfs_buf_t *bp; /* buffer for "block" */
4537 int error; /* error return value */
4538 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 xfs_extnum_t i, j; /* index into the extents list */
4540 xfs_ifork_t *ifp; /* fork structure */
4541 int level; /* btree level, for checking */
4542 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10004543 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 /* REFERENCED */
4545 xfs_extnum_t room; /* number of entries there's room for */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
4547 bno = NULLFSBLOCK;
4548 mp = ip->i_mount;
4549 ifp = XFS_IFORK_PTR(ip, whichfork);
4550 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4551 XFS_EXTFMT_INODE(ip);
4552 block = ifp->if_broot;
4553 /*
4554 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4555 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004556 level = be16_to_cpu(block->bb_level);
4557 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004559 bno = be64_to_cpu(*pp);
4560 ASSERT(bno != NULLDFSBNO);
4561 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4562 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 /*
4564 * Go down the tree until leaf level is reached, following the first
4565 * pointer (leftmost) at each level.
4566 */
4567 while (level-- > 0) {
4568 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4569 XFS_BMAP_BTREE_REF)))
4570 return error;
4571 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4572 XFS_WANT_CORRUPTED_GOTO(
4573 XFS_BMAP_SANITY_CHECK(mp, block, level),
4574 error0);
4575 if (level == 0)
4576 break;
Eric Sandeen2c36dde2007-02-10 18:37:33 +11004577 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004578 bno = be64_to_cpu(*pp);
4579 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 xfs_trans_brelse(tp, bp);
4581 }
4582 /*
4583 * Here with bp and block set to the leftmost leaf node in the tree.
4584 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004585 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 i = 0;
4587 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004588 * Loop over all leaf nodes. Copy information to the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 */
4590 for (;;) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004591 xfs_bmbt_rec_t *frp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 xfs_fsblock_t nextbno;
4593 xfs_extnum_t num_recs;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004594 xfs_extnum_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595
4596
Christoph Hellwig16259e72005-11-02 15:11:25 +11004597 num_recs = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 if (unlikely(i + num_recs > room)) {
4599 ASSERT(i + num_recs <= room);
Nathan Scott3762ec62006-01-12 10:29:53 +11004600 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4601 "corrupt dinode %Lu, (btree extents).",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 (unsigned long long) ip->i_ino);
4603 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4604 XFS_ERRLEVEL_LOW,
4605 ip->i_mount);
4606 goto error0;
4607 }
4608 XFS_WANT_CORRUPTED_GOTO(
4609 XFS_BMAP_SANITY_CHECK(mp, block, 0),
4610 error0);
4611 /*
4612 * Read-ahead the next leaf block, if any.
4613 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004614 nextbno = be64_to_cpu(block->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 if (nextbno != NULLFSBLOCK)
4616 xfs_btree_reada_bufl(mp, nextbno, 1);
4617 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004618 * Copy records into the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 */
Eric Sandeen2c36dde2007-02-10 18:37:33 +11004620 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004621 start = i;
4622 for (j = 0; j < num_recs; j++, i++, frp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004623 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10004624 trp->l0 = be64_to_cpu(frp->l0);
4625 trp->l1 = be64_to_cpu(frp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 }
4627 if (exntf == XFS_EXTFMT_NOSTATE) {
4628 /*
4629 * Check all attribute bmap btree records and
4630 * any "older" data bmap btree records for a
4631 * set bit in the "extent flag" position.
4632 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004633 if (unlikely(xfs_check_nostate_extents(ifp,
4634 start, num_recs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4636 XFS_ERRLEVEL_LOW,
4637 ip->i_mount);
4638 goto error0;
4639 }
4640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 xfs_trans_brelse(tp, bp);
4642 bno = nextbno;
4643 /*
4644 * If we've reached the end, stop.
4645 */
4646 if (bno == NULLFSBLOCK)
4647 break;
4648 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4649 XFS_BMAP_BTREE_REF)))
4650 return error;
4651 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4652 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004653 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
Eric Sandeen3a59c942007-07-11 11:09:47 +10004655 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 return 0;
4657error0:
4658 xfs_trans_brelse(tp, bp);
4659 return XFS_ERROR(EFSCORRUPTED);
4660}
4661
4662#ifdef XFS_BMAP_TRACE
4663/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004664 * Add bmap trace insert entries for all the contents of the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 */
4666void
4667xfs_bmap_trace_exlist(
Eric Sandeen3a59c942007-07-11 11:09:47 +10004668 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 xfs_inode_t *ip, /* incore inode pointer */
4670 xfs_extnum_t cnt, /* count of entries in the list */
4671 int whichfork) /* data or attr fork */
4672{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004673 xfs_bmbt_rec_host_t *ep; /* current extent record */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004674 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 xfs_ifork_t *ifp; /* inode fork pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004676 xfs_bmbt_irec_t s; /* file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
4678 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004679 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4680 for (idx = 0; idx < cnt; idx++) {
4681 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 xfs_bmbt_get_all(ep, &s);
Eric Sandeen3a59c942007-07-11 11:09:47 +10004683 XFS_BMAP_TRACE_INSERT("exlist", ip, idx, 1, &s, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 whichfork);
4685 }
4686}
4687#endif
4688
4689#ifdef DEBUG
4690/*
4691 * Validate that the bmbt_irecs being returned from bmapi are valid
4692 * given the callers original parameters. Specifically check the
4693 * ranges of the returned irecs to ensure that they only extent beyond
4694 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4695 */
4696STATIC void
4697xfs_bmap_validate_ret(
4698 xfs_fileoff_t bno,
4699 xfs_filblks_t len,
4700 int flags,
4701 xfs_bmbt_irec_t *mval,
4702 int nmap,
4703 int ret_nmap)
4704{
4705 int i; /* index to map values */
4706
4707 ASSERT(ret_nmap <= nmap);
4708
4709 for (i = 0; i < ret_nmap; i++) {
4710 ASSERT(mval[i].br_blockcount > 0);
4711 if (!(flags & XFS_BMAPI_ENTIRE)) {
4712 ASSERT(mval[i].br_startoff >= bno);
4713 ASSERT(mval[i].br_blockcount <= len);
4714 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4715 bno + len);
4716 } else {
4717 ASSERT(mval[i].br_startoff < bno + len);
4718 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4719 bno);
4720 }
4721 ASSERT(i == 0 ||
4722 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4723 mval[i].br_startoff);
4724 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4725 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4726 mval[i].br_startblock != HOLESTARTBLOCK);
4727 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4728 mval[i].br_state == XFS_EXT_UNWRITTEN);
4729 }
4730}
4731#endif /* DEBUG */
4732
4733
4734/*
4735 * Map file blocks to filesystem blocks.
4736 * File range is given by the bno/len pair.
4737 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4738 * into a hole or past eof.
4739 * Only allocates blocks from a single allocation group,
4740 * to avoid locking problems.
4741 * The returned value in "firstblock" from the first call in a transaction
4742 * must be remembered and presented to subsequent calls in "firstblock".
4743 * An upper bound for the number of blocks to be allocated is supplied to
4744 * the first call in "total"; if no allocation group has that many free
4745 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4746 */
4747int /* error */
4748xfs_bmapi(
4749 xfs_trans_t *tp, /* transaction pointer */
4750 xfs_inode_t *ip, /* incore inode */
4751 xfs_fileoff_t bno, /* starting file offs. mapped */
4752 xfs_filblks_t len, /* length to map in file */
4753 int flags, /* XFS_BMAPI_... */
4754 xfs_fsblock_t *firstblock, /* first allocated block
4755 controls a.g. for allocs */
4756 xfs_extlen_t total, /* total blocks needed */
4757 xfs_bmbt_irec_t *mval, /* output: map values */
4758 int *nmap, /* i/o: mval size/count */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004759 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4760 xfs_extdelta_t *delta) /* o: change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761{
4762 xfs_fsblock_t abno; /* allocated block number */
4763 xfs_extlen_t alen; /* allocated extent length */
4764 xfs_fileoff_t aoff; /* allocated file offset */
4765 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 xfs_btree_cur_t *cur; /* bmap btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 xfs_fileoff_t end; /* end of mapped file region */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004768 int eof; /* we've hit the end of extents */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004769 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004771 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 xfs_ifork_t *ifp; /* inode fork pointer */
4773 xfs_extlen_t indlen; /* indirect blocks length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 xfs_extnum_t lastx; /* last useful extent number */
4775 int logflags; /* flags for transaction logging */
4776 xfs_extlen_t minleft; /* min blocks left after allocation */
4777 xfs_extlen_t minlen; /* min allocation size */
4778 xfs_mount_t *mp; /* xfs mount structure */
4779 int n; /* current extent index */
4780 int nallocs; /* number of extents alloc\'d */
4781 xfs_extnum_t nextents; /* number of extents in file */
4782 xfs_fileoff_t obno; /* old block number (offset) */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004783 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 int tmp_logflags; /* temp flags holder */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004785 int whichfork; /* data or attr fork */
4786 char inhole; /* current location is hole in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 char wasdelay; /* old extent was delayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 char wr; /* this is a write request */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004789 char rt; /* this is a realtime file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790#ifdef DEBUG
4791 xfs_fileoff_t orig_bno; /* original block number value */
4792 int orig_flags; /* original flags arg value */
4793 xfs_filblks_t orig_len; /* original value of len arg */
4794 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4795 int orig_nmap; /* original value of *nmap */
4796
4797 orig_bno = bno;
4798 orig_len = len;
4799 orig_flags = flags;
4800 orig_mval = mval;
4801 orig_nmap = *nmap;
4802#endif
4803 ASSERT(*nmap >= 1);
4804 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4805 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4806 XFS_ATTR_FORK : XFS_DATA_FORK;
4807 mp = ip->i_mount;
4808 if (unlikely(XFS_TEST_ERROR(
4809 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4810 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4811 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4812 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4813 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4814 return XFS_ERROR(EFSCORRUPTED);
4815 }
4816 if (XFS_FORCED_SHUTDOWN(mp))
4817 return XFS_ERROR(EIO);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004818 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 ifp = XFS_IFORK_PTR(ip, whichfork);
4820 ASSERT(ifp->if_ext_max ==
4821 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4822 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4823 XFS_STATS_INC(xs_blk_mapw);
4824 else
4825 XFS_STATS_INC(xs_blk_mapr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 /*
Nathan Scott39269e22006-03-14 13:33:50 +11004827 * IGSTATE flag is used to combine extents which
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 * differ only due to the state of the extents.
4829 * This technique is used from xfs_getbmap()
4830 * when the caller does not wish to see the
4831 * separation (which is the default).
4832 *
4833 * This technique is also used when writing a
4834 * buffer which has been partially written,
4835 * (usually by being flushed during a chunkread),
4836 * to ensure one write takes place. This also
4837 * prevents a change in the xfs inode extents at
4838 * this time, intentionally. This change occurs
4839 * on completion of the write operation, in
4840 * xfs_strat_comp(), where the xfs_bmapi() call
4841 * is transactioned, and the extents combined.
4842 */
Nathan Scott39269e22006-03-14 13:33:50 +11004843 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4844 wr = 0; /* no allocations are allowed */
4845 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 logflags = 0;
4847 nallocs = 0;
4848 cur = NULL;
4849 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4850 ASSERT(wr && tp);
4851 if ((error = xfs_bmap_local_to_extents(tp, ip,
4852 firstblock, total, &logflags, whichfork)))
4853 goto error0;
4854 }
4855 if (wr && *firstblock == NULLFSBLOCK) {
4856 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Christoph Hellwig16259e72005-11-02 15:11:25 +11004857 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 else
4859 minleft = 1;
4860 } else
4861 minleft = 0;
4862 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4863 (error = xfs_iread_extents(tp, ip, whichfork)))
4864 goto error0;
4865 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4866 &prev);
4867 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4868 n = 0;
4869 end = bno + len;
4870 obno = bno;
4871 bma.ip = NULL;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004872 if (delta) {
4873 delta->xed_startoff = NULLFILEOFF;
4874 delta->xed_blockcount = 0;
4875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 while (bno < end && n < *nmap) {
4877 /*
4878 * Reading past eof, act as though there's a hole
4879 * up to end.
4880 */
4881 if (eof && !wr)
4882 got.br_startoff = end;
4883 inhole = eof || got.br_startoff > bno;
Nathan Scott39269e22006-03-14 13:33:50 +11004884 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 ISNULLSTARTBLOCK(got.br_startblock);
4886 /*
4887 * First, deal with the hole before the allocated space
4888 * that we found, if any.
4889 */
4890 if (wr && (inhole || wasdelay)) {
4891 /*
4892 * For the wasdelay case, we could also just
4893 * allocate the stuff asked for in this bmap call
4894 * but that wouldn't be as good.
4895 */
Nathan Scott39269e22006-03-14 13:33:50 +11004896 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 alen = (xfs_extlen_t)got.br_blockcount;
4898 aoff = got.br_startoff;
4899 if (lastx != NULLEXTNUM && lastx) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004900 ep = xfs_iext_get_ext(ifp, lastx - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 xfs_bmbt_get_all(ep, &prev);
4902 }
4903 } else if (wasdelay) {
4904 alen = (xfs_extlen_t)
4905 XFS_FILBLKS_MIN(len,
4906 (got.br_startoff +
4907 got.br_blockcount) - bno);
4908 aoff = bno;
4909 } else {
4910 alen = (xfs_extlen_t)
4911 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4912 if (!eof)
4913 alen = (xfs_extlen_t)
4914 XFS_FILBLKS_MIN(alen,
4915 got.br_startoff - bno);
4916 aoff = bno;
4917 }
Nathan Scott39269e22006-03-14 13:33:50 +11004918 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4919 if (flags & XFS_BMAPI_DELAY) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11004920 xfs_extlen_t extsz;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004921
4922 /* Figure out the extent size, adjust alen */
David Chinner957d0eb2007-06-18 16:50:37 +10004923 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004924 if (extsz) {
4925 error = xfs_bmap_extsize_align(mp,
4926 &got, &prev, extsz,
Nathan Scott39269e22006-03-14 13:33:50 +11004927 rt, eof,
4928 flags&XFS_BMAPI_DELAY,
4929 flags&XFS_BMAPI_CONVERT,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004930 &aoff, &alen);
4931 ASSERT(!error);
4932 }
4933
4934 if (rt)
4935 extsz = alen / mp->m_sb.sb_rextsize;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004936
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 /*
4938 * Make a transaction-less quota reservation for
4939 * delayed allocation blocks. This number gets
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004940 * adjusted later. We return if we haven't
4941 * allocated blocks already inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 */
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004943 if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS(
Nathan Scott06d10dd2005-06-21 15:48:47 +10004944 mp, NULL, ip, (long)alen, 0,
4945 rt ? XFS_QMOPT_RES_RTBLKS :
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004946 XFS_QMOPT_RES_REGBLKS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 if (n == 0) {
4948 *nmap = 0;
4949 ASSERT(cur == NULL);
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004950 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 }
4952 break;
4953 }
4954
4955 /*
4956 * Split changing sb for alen and indlen since
4957 * they could be coming from different places.
4958 */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004959 indlen = (xfs_extlen_t)
4960 xfs_bmap_worst_indlen(ip, alen);
4961 ASSERT(indlen > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
Nathan Scottdd9f4382006-01-11 15:28:28 +11004963 if (rt) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004964 error = xfs_mod_incore_sb(mp,
4965 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004966 -((int64_t)extsz), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004967 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004968 } else {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004969 error = xfs_mod_incore_sb(mp,
4970 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004971 -((int64_t)alen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004972 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004973 }
David Chinner3bdbfb12005-09-02 16:40:47 +10004974 if (!error) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004975 error = xfs_mod_incore_sb(mp,
4976 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004977 -((int64_t)indlen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004978 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004979 if (error && rt)
4980 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004981 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004982 (int64_t)extsz, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004983 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004984 else if (error)
4985 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004986 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004987 (int64_t)alen, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004988 XFS_BMAPI_RSVBLOCKS));
David Chinner3bdbfb12005-09-02 16:40:47 +10004989 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004990
4991 if (error) {
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004992 if (XFS_IS_QUOTA_ON(mp))
Nathan Scott06d10dd2005-06-21 15:48:47 +10004993 /* unreserve the blocks now */
Nathan Scottdd9f4382006-01-11 15:28:28 +11004994 (void)
Nathan Scott06d10dd2005-06-21 15:48:47 +10004995 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4996 mp, NULL, ip,
4997 (long)alen, 0, rt ?
4998 XFS_QMOPT_RES_RTBLKS :
4999 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 break;
5001 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005002
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 ip->i_delayed_blks += alen;
5004 abno = NULLSTARTBLOCK(indlen);
5005 } else {
5006 /*
5007 * If first time, allocate and fill in
5008 * once-only bma fields.
5009 */
5010 if (bma.ip == NULL) {
5011 bma.tp = tp;
5012 bma.ip = ip;
5013 bma.prevp = &prev;
5014 bma.gotp = &got;
5015 bma.total = total;
5016 bma.userdata = 0;
5017 }
5018 /* Indicate if this is the first user data
5019 * in the file, or just any user data.
5020 */
Nathan Scott39269e22006-03-14 13:33:50 +11005021 if (!(flags & XFS_BMAPI_METADATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 bma.userdata = (aoff == 0) ?
5023 XFS_ALLOC_INITIAL_USER_DATA :
5024 XFS_ALLOC_USERDATA;
5025 }
5026 /*
5027 * Fill in changeable bma fields.
5028 */
5029 bma.eof = eof;
5030 bma.firstblock = *firstblock;
5031 bma.alen = alen;
5032 bma.off = aoff;
Adrian Bunk72880262006-08-30 13:41:58 +10005033 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 bma.wasdel = wasdelay;
5035 bma.minlen = minlen;
5036 bma.low = flist->xbf_low;
5037 bma.minleft = minleft;
5038 /*
5039 * Only want to do the alignment at the
5040 * eof if it is userdata and allocation length
5041 * is larger than a stripe unit.
5042 */
5043 if (mp->m_dalign && alen >= mp->m_dalign &&
Nathan Scott39269e22006-03-14 13:33:50 +11005044 (!(flags & XFS_BMAPI_METADATA)) &&
5045 (whichfork == XFS_DATA_FORK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 if ((error = xfs_bmap_isaeof(ip, aoff,
5047 whichfork, &bma.aeof)))
5048 goto error0;
5049 } else
5050 bma.aeof = 0;
5051 /*
5052 * Call allocator.
5053 */
5054 if ((error = xfs_bmap_alloc(&bma)))
5055 goto error0;
5056 /*
5057 * Copy out result fields.
5058 */
5059 abno = bma.rval;
5060 if ((flist->xbf_low = bma.low))
5061 minleft = 0;
5062 alen = bma.alen;
5063 aoff = bma.off;
5064 ASSERT(*firstblock == NULLFSBLOCK ||
5065 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5066 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
5067 (flist->xbf_low &&
5068 XFS_FSB_TO_AGNO(mp, *firstblock) <
5069 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
5070 *firstblock = bma.firstblock;
5071 if (cur)
5072 cur->bc_private.b.firstblock =
5073 *firstblock;
5074 if (abno == NULLFSBLOCK)
5075 break;
5076 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005077 cur = xfs_bmbt_init_cursor(mp, tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 ip, whichfork);
5079 cur->bc_private.b.firstblock =
5080 *firstblock;
5081 cur->bc_private.b.flist = flist;
5082 }
5083 /*
5084 * Bump the number of extents we've allocated
5085 * in this call.
5086 */
5087 nallocs++;
5088 }
5089 if (cur)
5090 cur->bc_private.b.flags =
5091 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
5092 got.br_startoff = aoff;
5093 got.br_startblock = abno;
5094 got.br_blockcount = alen;
5095 got.br_state = XFS_EXT_NORM; /* assume normal */
5096 /*
5097 * Determine state of extent, and the filesystem.
5098 * A wasdelay extent has been initialized, so
5099 * shouldn't be flagged as unwritten.
5100 */
Eric Sandeen62118702008-03-06 13:44:28 +11005101 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5103 got.br_state = XFS_EXT_UNWRITTEN;
5104 }
5105 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005106 firstblock, flist, &tmp_logflags, delta,
5107 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 logflags |= tmp_logflags;
5109 if (error)
5110 goto error0;
5111 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005112 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5114 xfs_bmbt_get_all(ep, &got);
5115 ASSERT(got.br_startoff <= aoff);
5116 ASSERT(got.br_startoff + got.br_blockcount >=
5117 aoff + alen);
5118#ifdef DEBUG
Nathan Scott39269e22006-03-14 13:33:50 +11005119 if (flags & XFS_BMAPI_DELAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
5121 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
5122 }
5123 ASSERT(got.br_state == XFS_EXT_NORM ||
5124 got.br_state == XFS_EXT_UNWRITTEN);
5125#endif
5126 /*
5127 * Fall down into the found allocated space case.
5128 */
5129 } else if (inhole) {
5130 /*
5131 * Reading in a hole.
5132 */
5133 mval->br_startoff = bno;
5134 mval->br_startblock = HOLESTARTBLOCK;
5135 mval->br_blockcount =
5136 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5137 mval->br_state = XFS_EXT_NORM;
5138 bno += mval->br_blockcount;
5139 len -= mval->br_blockcount;
5140 mval++;
5141 n++;
5142 continue;
5143 }
5144 /*
5145 * Then deal with the allocated space we found.
5146 */
5147 ASSERT(ep != NULL);
Nathan Scott39269e22006-03-14 13:33:50 +11005148 if (!(flags & XFS_BMAPI_ENTIRE) &&
5149 (got.br_startoff + got.br_blockcount > obno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 if (obno > bno)
5151 bno = obno;
5152 ASSERT((bno >= obno) || (n == 0));
5153 ASSERT(bno < end);
5154 mval->br_startoff = bno;
5155 if (ISNULLSTARTBLOCK(got.br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11005156 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 mval->br_startblock = DELAYSTARTBLOCK;
5158 } else
5159 mval->br_startblock =
5160 got.br_startblock +
5161 (bno - got.br_startoff);
5162 /*
5163 * Return the minimum of what we got and what we
5164 * asked for for the length. We can use the len
5165 * variable here because it is modified below
5166 * and we could have been there before coming
5167 * here if the first part of the allocation
5168 * didn't overlap what was asked for.
5169 */
5170 mval->br_blockcount =
5171 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5172 (bno - got.br_startoff));
5173 mval->br_state = got.br_state;
5174 ASSERT(mval->br_blockcount <= len);
5175 } else {
5176 *mval = got;
5177 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11005178 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 mval->br_startblock = DELAYSTARTBLOCK;
5180 }
5181 }
5182
5183 /*
5184 * Check if writing previously allocated but
5185 * unwritten extents.
5186 */
5187 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5188 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5189 /*
5190 * Modify (by adding) the state flag, if writing.
5191 */
5192 ASSERT(mval->br_blockcount <= len);
5193 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005194 cur = xfs_bmbt_init_cursor(mp,
5195 tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 cur->bc_private.b.firstblock =
5197 *firstblock;
5198 cur->bc_private.b.flist = flist;
5199 }
5200 mval->br_state = XFS_EXT_NORM;
5201 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005202 firstblock, flist, &tmp_logflags, delta,
5203 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 logflags |= tmp_logflags;
5205 if (error)
5206 goto error0;
5207 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005208 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5210 xfs_bmbt_get_all(ep, &got);
5211 /*
5212 * We may have combined previously unwritten
5213 * space with written space, so generate
5214 * another request.
5215 */
5216 if (mval->br_blockcount < len)
5217 continue;
5218 }
5219
Nathan Scott39269e22006-03-14 13:33:50 +11005220 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 ((mval->br_startoff + mval->br_blockcount) <= end));
Nathan Scott39269e22006-03-14 13:33:50 +11005222 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5223 (mval->br_blockcount <= len) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 (mval->br_startoff < obno));
5225 bno = mval->br_startoff + mval->br_blockcount;
5226 len = end - bno;
5227 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5228 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5229 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5230 ASSERT(mval->br_state == mval[-1].br_state);
5231 mval[-1].br_blockcount = mval->br_blockcount;
5232 mval[-1].br_state = mval->br_state;
5233 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5234 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5235 mval[-1].br_startblock != HOLESTARTBLOCK &&
5236 mval->br_startblock ==
5237 mval[-1].br_startblock + mval[-1].br_blockcount &&
Nathan Scott39269e22006-03-14 13:33:50 +11005238 ((flags & XFS_BMAPI_IGSTATE) ||
5239 mval[-1].br_state == mval->br_state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 ASSERT(mval->br_startoff ==
5241 mval[-1].br_startoff + mval[-1].br_blockcount);
5242 mval[-1].br_blockcount += mval->br_blockcount;
5243 } else if (n > 0 &&
5244 mval->br_startblock == DELAYSTARTBLOCK &&
5245 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5246 mval->br_startoff ==
5247 mval[-1].br_startoff + mval[-1].br_blockcount) {
5248 mval[-1].br_blockcount += mval->br_blockcount;
5249 mval[-1].br_state = mval->br_state;
5250 } else if (!((n == 0) &&
5251 ((mval->br_startoff + mval->br_blockcount) <=
5252 obno))) {
5253 mval++;
5254 n++;
5255 }
5256 /*
5257 * If we're done, stop now. Stop when we've allocated
5258 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5259 * the transaction may get too big.
5260 */
5261 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5262 break;
5263 /*
5264 * Else go on to the next record.
5265 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005266 ep = xfs_iext_get_ext(ifp, ++lastx);
David Chinner4e5ae832007-06-05 16:24:15 +10005267 prev = got;
5268 if (lastx >= nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 eof = 1;
David Chinner4e5ae832007-06-05 16:24:15 +10005270 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 xfs_bmbt_get_all(ep, &got);
5272 }
5273 ifp->if_lastex = lastx;
5274 *nmap = n;
5275 /*
5276 * Transform from btree to extents, give it cur.
5277 */
5278 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5279 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5280 ASSERT(wr && cur);
5281 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5282 &tmp_logflags, whichfork);
5283 logflags |= tmp_logflags;
5284 if (error)
5285 goto error0;
5286 }
5287 ASSERT(ifp->if_ext_max ==
5288 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5289 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5290 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5291 error = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005292 if (delta && delta->xed_startoff != NULLFILEOFF) {
5293 /* A change was actually made.
5294 * Note that delta->xed_blockount is an offset at this
5295 * point and needs to be converted to a block count.
5296 */
5297 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5298 delta->xed_blockcount -= delta->xed_startoff;
5299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300error0:
5301 /*
5302 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005303 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 */
5305 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5306 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5307 logflags &= ~XFS_ILOG_FEXT(whichfork);
5308 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5309 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5310 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5311 /*
5312 * Log whatever the flags say, even if error. Otherwise we might miss
5313 * detecting a case where the data is changed, there's an error,
5314 * and it's not logged so we don't shutdown when we should.
5315 */
5316 if (logflags) {
5317 ASSERT(tp && wr);
5318 xfs_trans_log_inode(tp, ip, logflags);
5319 }
5320 if (cur) {
5321 if (!error) {
5322 ASSERT(*firstblock == NULLFSBLOCK ||
5323 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5324 XFS_FSB_TO_AGNO(mp,
5325 cur->bc_private.b.firstblock) ||
5326 (flist->xbf_low &&
5327 XFS_FSB_TO_AGNO(mp, *firstblock) <
5328 XFS_FSB_TO_AGNO(mp,
5329 cur->bc_private.b.firstblock)));
5330 *firstblock = cur->bc_private.b.firstblock;
5331 }
5332 xfs_btree_del_cursor(cur,
5333 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5334 }
5335 if (!error)
5336 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5337 orig_nmap, *nmap);
5338 return error;
5339}
5340
5341/*
5342 * Map file blocks to filesystem blocks, simple version.
5343 * One block (extent) only, read-only.
5344 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5345 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5346 * was set and all the others were clear.
5347 */
5348int /* error */
5349xfs_bmapi_single(
5350 xfs_trans_t *tp, /* transaction pointer */
5351 xfs_inode_t *ip, /* incore inode */
5352 int whichfork, /* data or attr fork */
5353 xfs_fsblock_t *fsb, /* output: mapped block */
5354 xfs_fileoff_t bno) /* starting file offs. mapped */
5355{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005356 int eof; /* we've hit the end of extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005358 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 xfs_ifork_t *ifp; /* inode fork pointer */
5360 xfs_extnum_t lastx; /* last useful extent number */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005361 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362
5363 ifp = XFS_IFORK_PTR(ip, whichfork);
5364 if (unlikely(
5365 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5366 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5367 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5368 ip->i_mount);
5369 return XFS_ERROR(EFSCORRUPTED);
5370 }
5371 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5372 return XFS_ERROR(EIO);
5373 XFS_STATS_INC(xs_blk_mapr);
5374 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5375 (error = xfs_iread_extents(tp, ip, whichfork)))
5376 return error;
5377 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5378 &prev);
5379 /*
5380 * Reading past eof, act as though there's a hole
5381 * up to end.
5382 */
5383 if (eof || got.br_startoff > bno) {
5384 *fsb = NULLFSBLOCK;
5385 return 0;
5386 }
5387 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
5388 ASSERT(bno < got.br_startoff + got.br_blockcount);
5389 *fsb = got.br_startblock + (bno - got.br_startoff);
5390 ifp->if_lastex = lastx;
5391 return 0;
5392}
5393
5394/*
5395 * Unmap (remove) blocks from a file.
5396 * If nexts is nonzero then the number of extents to remove is limited to
5397 * that value. If not all extents in the block range can be removed then
5398 * *done is set.
5399 */
5400int /* error */
5401xfs_bunmapi(
5402 xfs_trans_t *tp, /* transaction pointer */
5403 struct xfs_inode *ip, /* incore inode */
5404 xfs_fileoff_t bno, /* starting offset to unmap */
5405 xfs_filblks_t len, /* length to unmap in file */
5406 int flags, /* misc flags */
5407 xfs_extnum_t nexts, /* number of extents max */
5408 xfs_fsblock_t *firstblock, /* first allocated block
5409 controls a.g. for allocs */
5410 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005411 xfs_extdelta_t *delta, /* o: change made to incore
5412 extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 int *done) /* set if not done yet */
5414{
5415 xfs_btree_cur_t *cur; /* bmap btree cursor */
5416 xfs_bmbt_irec_t del; /* extent being deleted */
5417 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005418 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 int error; /* error return value */
5420 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005421 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 xfs_ifork_t *ifp; /* inode fork pointer */
5423 int isrt; /* freeing in rt area */
5424 xfs_extnum_t lastx; /* last extent index used */
5425 int logflags; /* transaction logging flags */
5426 xfs_extlen_t mod; /* rt extent offset */
5427 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005428 xfs_extnum_t nextents; /* number of file extents */
5429 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 xfs_fileoff_t start; /* first file offset deleted */
5431 int tmp_logflags; /* partial logging flags */
5432 int wasdel; /* was a delayed alloc extent */
5433 int whichfork; /* data or attribute fork */
5434 int rsvd; /* OK to allocate reserved blocks */
5435 xfs_fsblock_t sum;
5436
5437 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5438 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5439 XFS_ATTR_FORK : XFS_DATA_FORK;
5440 ifp = XFS_IFORK_PTR(ip, whichfork);
5441 if (unlikely(
5442 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5443 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5444 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5445 ip->i_mount);
5446 return XFS_ERROR(EFSCORRUPTED);
5447 }
5448 mp = ip->i_mount;
5449 if (XFS_FORCED_SHUTDOWN(mp))
5450 return XFS_ERROR(EIO);
5451 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5452 ASSERT(len > 0);
5453 ASSERT(nexts >= 0);
5454 ASSERT(ifp->if_ext_max ==
5455 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5456 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5457 (error = xfs_iread_extents(tp, ip, whichfork)))
5458 return error;
5459 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5460 if (nextents == 0) {
5461 *done = 1;
5462 return 0;
5463 }
5464 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005465 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 start = bno;
5467 bno = start + len - 1;
5468 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5469 &prev);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005470 if (delta) {
5471 delta->xed_startoff = NULLFILEOFF;
5472 delta->xed_blockcount = 0;
5473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 /*
5475 * Check to see if the given block number is past the end of the
5476 * file, back up to the last block if so...
5477 */
5478 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005479 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 xfs_bmbt_get_all(ep, &got);
5481 bno = got.br_startoff + got.br_blockcount - 1;
5482 }
5483 logflags = 0;
5484 if (ifp->if_flags & XFS_IFBROOT) {
5485 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005486 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 cur->bc_private.b.firstblock = *firstblock;
5488 cur->bc_private.b.flist = flist;
5489 cur->bc_private.b.flags = 0;
5490 } else
5491 cur = NULL;
5492 extno = 0;
5493 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5494 (nexts == 0 || extno < nexts)) {
5495 /*
5496 * Is the found extent after a hole in which bno lives?
5497 * Just back up to the previous extent, if so.
5498 */
5499 if (got.br_startoff > bno) {
5500 if (--lastx < 0)
5501 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005502 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 xfs_bmbt_get_all(ep, &got);
5504 }
5505 /*
5506 * Is the last block of this extent before the range
5507 * we're supposed to delete? If so, we're done.
5508 */
5509 bno = XFS_FILEOFF_MIN(bno,
5510 got.br_startoff + got.br_blockcount - 1);
5511 if (bno < start)
5512 break;
5513 /*
5514 * Then deal with the (possibly delayed) allocated space
5515 * we found.
5516 */
5517 ASSERT(ep != NULL);
5518 del = got;
5519 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
5520 if (got.br_startoff < start) {
5521 del.br_startoff = start;
5522 del.br_blockcount -= start - got.br_startoff;
5523 if (!wasdel)
5524 del.br_startblock += start - got.br_startoff;
5525 }
5526 if (del.br_startoff + del.br_blockcount > bno + 1)
5527 del.br_blockcount = bno + 1 - del.br_startoff;
5528 sum = del.br_startblock + del.br_blockcount;
5529 if (isrt &&
5530 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5531 /*
5532 * Realtime extent not lined up at the end.
5533 * The extent could have been split into written
5534 * and unwritten pieces, or we could just be
5535 * unmapping part of it. But we can't really
5536 * get rid of part of a realtime extent.
5537 */
5538 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005539 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 /*
5541 * This piece is unwritten, or we're not
5542 * using unwritten extents. Skip over it.
5543 */
5544 ASSERT(bno >= mod);
5545 bno -= mod > del.br_blockcount ?
5546 del.br_blockcount : mod;
5547 if (bno < got.br_startoff) {
5548 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005549 xfs_bmbt_get_all(xfs_iext_get_ext(
5550 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 }
5552 continue;
5553 }
5554 /*
5555 * It's written, turn it unwritten.
5556 * This is better than zeroing it.
5557 */
5558 ASSERT(del.br_state == XFS_EXT_NORM);
5559 ASSERT(xfs_trans_get_block_res(tp) > 0);
5560 /*
5561 * If this spans a realtime extent boundary,
5562 * chop it back to the start of the one we end at.
5563 */
5564 if (del.br_blockcount > mod) {
5565 del.br_startoff += del.br_blockcount - mod;
5566 del.br_startblock += del.br_blockcount - mod;
5567 del.br_blockcount = mod;
5568 }
5569 del.br_state = XFS_EXT_UNWRITTEN;
5570 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005571 firstblock, flist, &logflags, delta,
5572 XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 if (error)
5574 goto error0;
5575 goto nodelete;
5576 }
5577 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5578 /*
5579 * Realtime extent is lined up at the end but not
5580 * at the front. We'll get rid of full extents if
5581 * we can.
5582 */
5583 mod = mp->m_sb.sb_rextsize - mod;
5584 if (del.br_blockcount > mod) {
5585 del.br_blockcount -= mod;
5586 del.br_startoff += mod;
5587 del.br_startblock += mod;
5588 } else if ((del.br_startoff == start &&
5589 (del.br_state == XFS_EXT_UNWRITTEN ||
5590 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005591 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 /*
5593 * Can't make it unwritten. There isn't
5594 * a full extent here so just skip it.
5595 */
5596 ASSERT(bno >= del.br_blockcount);
5597 bno -= del.br_blockcount;
5598 if (bno < got.br_startoff) {
5599 if (--lastx >= 0)
5600 xfs_bmbt_get_all(--ep, &got);
5601 }
5602 continue;
5603 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5604 /*
5605 * This one is already unwritten.
5606 * It must have a written left neighbor.
5607 * Unwrite the killed part of that one and
5608 * try again.
5609 */
5610 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005611 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5612 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 ASSERT(prev.br_state == XFS_EXT_NORM);
5614 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
5615 ASSERT(del.br_startblock ==
5616 prev.br_startblock + prev.br_blockcount);
5617 if (prev.br_startoff < start) {
5618 mod = start - prev.br_startoff;
5619 prev.br_blockcount -= mod;
5620 prev.br_startblock += mod;
5621 prev.br_startoff = start;
5622 }
5623 prev.br_state = XFS_EXT_UNWRITTEN;
5624 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5625 &prev, firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005626 delta, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 if (error)
5628 goto error0;
5629 goto nodelete;
5630 } else {
5631 ASSERT(del.br_state == XFS_EXT_NORM);
5632 del.br_state = XFS_EXT_UNWRITTEN;
5633 error = xfs_bmap_add_extent(ip, lastx, &cur,
5634 &del, firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005635 delta, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 if (error)
5637 goto error0;
5638 goto nodelete;
5639 }
5640 }
5641 if (wasdel) {
5642 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005643 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005644 if (isrt) {
5645 xfs_filblks_t rtexts;
5646
5647 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5648 do_div(rtexts, mp->m_sb.sb_rextsize);
5649 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005650 (int64_t)rtexts, rsvd);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005651 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5652 NULL, ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005653 XFS_QMOPT_RES_RTBLKS);
5654 } else {
5655 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005656 (int64_t)del.br_blockcount, rsvd);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005657 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5658 NULL, ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 ip->i_delayed_blks -= del.br_blockcount;
5662 if (cur)
5663 cur->bc_private.b.flags |=
5664 XFS_BTCUR_BPRV_WASDEL;
5665 } else if (cur)
5666 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5667 /*
5668 * If it's the case where the directory code is running
5669 * with no block reservation, and the deleted block is in
5670 * the middle of its extent, and the resulting insert
5671 * of an extent would cause transformation to btree format,
5672 * then reject it. The calling code will then swap
5673 * blocks around instead.
5674 * We have to do this now, rather than waiting for the
5675 * conversion to btree format, since the transaction
5676 * will be dirty.
5677 */
5678 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5679 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5680 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5681 del.br_startoff > got.br_startoff &&
5682 del.br_startoff + del.br_blockcount <
5683 got.br_startoff + got.br_blockcount) {
5684 error = XFS_ERROR(ENOSPC);
5685 goto error0;
5686 }
5687 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005688 &tmp_logflags, delta, whichfork, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 logflags |= tmp_logflags;
5690 if (error)
5691 goto error0;
5692 bno = del.br_startoff - 1;
5693nodelete:
5694 lastx = ifp->if_lastex;
5695 /*
5696 * If not done go on to the next (previous) record.
5697 * Reset ep in case the extents array was re-alloced.
5698 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005699 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5701 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5702 xfs_bmbt_get_startoff(ep) > bno) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005703 if (--lastx >= 0)
5704 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 }
5706 if (lastx >= 0)
5707 xfs_bmbt_get_all(ep, &got);
5708 extno++;
5709 }
5710 }
5711 ifp->if_lastex = lastx;
5712 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5713 ASSERT(ifp->if_ext_max ==
5714 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5715 /*
5716 * Convert to a btree if necessary.
5717 */
5718 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5719 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5720 ASSERT(cur == NULL);
5721 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5722 &cur, 0, &tmp_logflags, whichfork);
5723 logflags |= tmp_logflags;
5724 if (error)
5725 goto error0;
5726 }
5727 /*
5728 * transform from btree to extents, give it cur
5729 */
5730 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5731 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5732 ASSERT(cur != NULL);
5733 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5734 whichfork);
5735 logflags |= tmp_logflags;
5736 if (error)
5737 goto error0;
5738 }
5739 /*
5740 * transform from extents to local?
5741 */
5742 ASSERT(ifp->if_ext_max ==
5743 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5744 error = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005745 if (delta && delta->xed_startoff != NULLFILEOFF) {
5746 /* A change was actually made.
5747 * Note that delta->xed_blockount is an offset at this
5748 * point and needs to be converted to a block count.
5749 */
5750 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5751 delta->xed_blockcount -= delta->xed_startoff;
5752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753error0:
5754 /*
5755 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005756 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 */
5758 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5759 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5760 logflags &= ~XFS_ILOG_FEXT(whichfork);
5761 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5762 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5763 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5764 /*
5765 * Log inode even in the error case, if the transaction
5766 * is dirty we'll need to shut down the filesystem.
5767 */
5768 if (logflags)
5769 xfs_trans_log_inode(tp, ip, logflags);
5770 if (cur) {
5771 if (!error) {
5772 *firstblock = cur->bc_private.b.firstblock;
5773 cur->bc_private.b.allocated = 0;
5774 }
5775 xfs_btree_del_cursor(cur,
5776 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5777 }
5778 return error;
5779}
5780
5781/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005782 * returns 1 for success, 0 if we failed to map the extent.
5783 */
5784STATIC int
5785xfs_getbmapx_fix_eof_hole(
5786 xfs_inode_t *ip, /* xfs incore inode pointer */
5787 struct getbmap *out, /* output structure */
5788 int prealloced, /* this is a file with
5789 * preallocated data space */
5790 __int64_t end, /* last block requested */
5791 xfs_fsblock_t startblock)
5792{
5793 __int64_t fixlen;
5794 xfs_mount_t *mp; /* file system mount point */
5795
5796 if (startblock == HOLESTARTBLOCK) {
5797 mp = ip->i_mount;
5798 out->bmv_block = -1;
5799 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5800 fixlen -= out->bmv_offset;
5801 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5802 /* Came to hole at EOF. Trim it. */
5803 if (fixlen <= 0)
5804 return 0;
5805 out->bmv_length = fixlen;
5806 }
5807 } else {
5808 out->bmv_block = XFS_FSB_TO_DB(ip, startblock);
5809 }
5810
5811 return 1;
5812}
5813
5814/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 * Fcntl interface to xfs_bmapi.
5816 */
5817int /* error code */
5818xfs_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10005819 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 struct getbmap *bmv, /* user bmap structure */
5821 void __user *ap, /* pointer to user's array */
5822 int interface) /* interface flags */
5823{
5824 __int64_t bmvend; /* last block requested */
5825 int error; /* return value */
5826 __int64_t fixlen; /* length for -1 case */
5827 int i; /* extent number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 int lock; /* lock state */
5829 xfs_bmbt_irec_t *map; /* buffer for user's data */
5830 xfs_mount_t *mp; /* file system mount point */
5831 int nex; /* # of user extents can do */
5832 int nexleft; /* # of user extents left */
5833 int subnex; /* # of bmapi's can do */
5834 int nmap; /* number of map entries */
5835 struct getbmap out; /* output structure */
5836 int whichfork; /* data or attr fork */
5837 int prealloced; /* this is a file with
5838 * preallocated data space */
5839 int sh_unwritten; /* true, if unwritten */
5840 /* extents listed separately */
5841 int bmapi_flags; /* flags for xfs_bmapi */
5842 __int32_t oflags; /* getbmapx bmv_oflags field */
5843
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 mp = ip->i_mount;
5845
5846 whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5847 sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
5848
5849 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5850 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5851 * bit is set for the file, generate a read event in order
5852 * that the DMAPI application may do its thing before we return
5853 * the extents. Usually this means restoring user file data to
5854 * regions of the file that look like holes.
5855 *
5856 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5857 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5858 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5859 * could misinterpret holes in a DMAPI file as true holes,
5860 * when in fact they may represent offline user data.
5861 */
Christoph Hellwigeb9df392007-08-16 18:42:07 +10005862 if ((interface & BMV_IF_NO_DMAPI_READ) == 0 &&
5863 DM_EVENT_ENABLED(ip, DM_EVENT_READ) &&
5864 whichfork == XFS_DATA_FORK) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11005865 error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, 0, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 if (error)
5867 return XFS_ERROR(error);
5868 }
5869
5870 if (whichfork == XFS_ATTR_FORK) {
5871 if (XFS_IFORK_Q(ip)) {
5872 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5873 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5874 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5875 return XFS_ERROR(EINVAL);
5876 } else if (unlikely(
5877 ip->i_d.di_aformat != 0 &&
5878 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5879 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5880 ip->i_mount);
5881 return XFS_ERROR(EFSCORRUPTED);
5882 }
5883 } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5884 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5885 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5886 return XFS_ERROR(EINVAL);
5887 if (whichfork == XFS_DATA_FORK) {
David Chinner957d0eb2007-06-18 16:50:37 +10005888 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005889 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 prealloced = 1;
5891 fixlen = XFS_MAXIOFFSET(mp);
5892 } else {
5893 prealloced = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005894 fixlen = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 }
5896 } else {
5897 prealloced = 0;
5898 fixlen = 1LL << 32;
5899 }
5900
5901 if (bmv->bmv_length == -1) {
5902 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5903 bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
5904 (__int64_t)0);
5905 } else if (bmv->bmv_length < 0)
5906 return XFS_ERROR(EINVAL);
5907 if (bmv->bmv_length == 0) {
5908 bmv->bmv_entries = 0;
5909 return 0;
5910 }
5911 nex = bmv->bmv_count - 1;
5912 if (nex <= 0)
5913 return XFS_ERROR(EINVAL);
5914 bmvend = bmv->bmv_offset + bmv->bmv_length;
5915
5916 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5917
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005918 if (whichfork == XFS_DATA_FORK &&
5919 (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
Christoph Hellwig739bfb22007-08-29 10:58:01 +10005921 error = xfs_flush_pages(ip, (xfs_off_t)0,
5922 -1, 0, FI_REMAPF);
Niv Sardie12070a2008-03-06 13:43:03 +11005923 if (error) {
5924 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5925 return error;
5926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 }
5928
5929 ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
5930
5931 lock = xfs_ilock_map_shared(ip);
5932
5933 /*
5934 * Don't let nex be bigger than the number of extents
5935 * we can have assuming alternating holes and real extents.
5936 */
5937 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5938 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5939
5940 bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
5941 ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
5942
5943 /*
5944 * Allocate enough space to handle "subnex" maps at a time.
5945 */
5946 subnex = 16;
5947 map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
5948
5949 bmv->bmv_entries = 0;
5950
5951 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
5952 error = 0;
5953 goto unlock_and_return;
5954 }
5955
5956 nexleft = nex;
5957
5958 do {
5959 nmap = (nexleft > subnex) ? subnex : nexleft;
5960 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5961 XFS_BB_TO_FSB(mp, bmv->bmv_length),
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005962 bmapi_flags, NULL, 0, map, &nmap,
5963 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 if (error)
5965 goto unlock_and_return;
5966 ASSERT(nmap <= subnex);
5967
5968 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5969 nexleft--;
5970 oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
5971 BMV_OF_PREALLOC : 0;
5972 out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
5973 out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5974 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
Yingping Lu9af0a702005-11-02 15:09:54 +11005975 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005976 whichfork == XFS_ATTR_FORK) {
5977 /* came to the end of attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 goto unlock_and_return;
5979 } else {
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005980 if (!xfs_getbmapx_fix_eof_hole(ip, &out,
5981 prealloced, bmvend,
5982 map[i].br_startblock)) {
5983 goto unlock_and_return;
5984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985
5986 /* return either getbmap/getbmapx structure. */
5987 if (interface & BMV_IF_EXTENDED) {
5988 struct getbmapx outx;
5989
5990 GETBMAP_CONVERT(out,outx);
5991 outx.bmv_oflags = oflags;
5992 outx.bmv_unused1 = outx.bmv_unused2 = 0;
5993 if (copy_to_user(ap, &outx,
5994 sizeof(outx))) {
5995 error = XFS_ERROR(EFAULT);
5996 goto unlock_and_return;
5997 }
5998 } else {
5999 if (copy_to_user(ap, &out,
6000 sizeof(out))) {
6001 error = XFS_ERROR(EFAULT);
6002 goto unlock_and_return;
6003 }
6004 }
6005 bmv->bmv_offset =
6006 out.bmv_offset + out.bmv_length;
6007 bmv->bmv_length = MAX((__int64_t)0,
6008 (__int64_t)(bmvend - bmv->bmv_offset));
6009 bmv->bmv_entries++;
6010 ap = (interface & BMV_IF_EXTENDED) ?
6011 (void __user *)
6012 ((struct getbmapx __user *)ap + 1) :
6013 (void __user *)
6014 ((struct getbmap __user *)ap + 1);
6015 }
6016 }
6017 } while (nmap && nexleft && bmv->bmv_length);
6018
6019unlock_and_return:
6020 xfs_iunlock_map_shared(ip, lock);
6021 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
6022
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10006023 kmem_free(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024
6025 return error;
6026}
6027
6028/*
6029 * Check the last inode extent to determine whether this allocation will result
6030 * in blocks being allocated at the end of the file. When we allocate new data
6031 * blocks at the end of the file which do not start at the previous data block,
6032 * we will try to align the new blocks at stripe unit boundaries.
6033 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10006034STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035xfs_bmap_isaeof(
6036 xfs_inode_t *ip, /* incore inode pointer */
6037 xfs_fileoff_t off, /* file offset in fsblocks */
6038 int whichfork, /* data or attribute fork */
6039 char *aeof) /* return value */
6040{
6041 int error; /* error return value */
6042 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006043 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006044 xfs_extnum_t nextents; /* number of file extents */
6045 xfs_bmbt_irec_t s; /* expanded extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046
6047 ASSERT(whichfork == XFS_DATA_FORK);
6048 ifp = XFS_IFORK_PTR(ip, whichfork);
6049 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6050 (error = xfs_iread_extents(NULL, ip, whichfork)))
6051 return error;
6052 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6053 if (nextents == 0) {
6054 *aeof = 1;
6055 return 0;
6056 }
6057 /*
6058 * Go to the last extent
6059 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006060 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 xfs_bmbt_get_all(lastrec, &s);
6062 /*
6063 * Check we are allocating in the last extent (for delayed allocations)
6064 * or past the last extent for non-delayed allocations.
6065 */
6066 *aeof = (off >= s.br_startoff &&
6067 off < s.br_startoff + s.br_blockcount &&
6068 ISNULLSTARTBLOCK(s.br_startblock)) ||
6069 off >= s.br_startoff + s.br_blockcount;
6070 return 0;
6071}
6072
6073/*
6074 * Check if the endoff is outside the last extent. If so the caller will grow
6075 * the allocation to a stripe unit boundary.
6076 */
6077int /* error */
6078xfs_bmap_eof(
6079 xfs_inode_t *ip, /* incore inode pointer */
6080 xfs_fileoff_t endoff, /* file offset in fsblocks */
6081 int whichfork, /* data or attribute fork */
6082 int *eof) /* result value */
6083{
6084 xfs_fsblock_t blockcount; /* extent block count */
6085 int error; /* error return value */
6086 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006087 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006088 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 xfs_fileoff_t startoff; /* extent starting file offset */
6090
6091 ASSERT(whichfork == XFS_DATA_FORK);
6092 ifp = XFS_IFORK_PTR(ip, whichfork);
6093 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6094 (error = xfs_iread_extents(NULL, ip, whichfork)))
6095 return error;
6096 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6097 if (nextents == 0) {
6098 *eof = 1;
6099 return 0;
6100 }
6101 /*
6102 * Go to the last extent
6103 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006104 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 startoff = xfs_bmbt_get_startoff(lastrec);
6106 blockcount = xfs_bmbt_get_blockcount(lastrec);
6107 *eof = endoff >= startoff + blockcount;
6108 return 0;
6109}
6110
6111#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112STATIC
6113xfs_buf_t *
6114xfs_bmap_get_bp(
6115 xfs_btree_cur_t *cur,
6116 xfs_fsblock_t bno)
6117{
6118 int i;
6119 xfs_buf_t *bp;
6120
6121 if (!cur)
6122 return(NULL);
6123
6124 bp = NULL;
6125 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6126 bp = cur->bc_bufs[i];
6127 if (!bp) break;
6128 if (XFS_BUF_ADDR(bp) == bno)
6129 break; /* Found it */
6130 }
6131 if (i == XFS_BTREE_MAXLEVELS)
6132 bp = NULL;
6133
6134 if (!bp) { /* Chase down all the log items to see if the bp is there */
6135 xfs_log_item_chunk_t *licp;
6136 xfs_trans_t *tp;
6137
6138 tp = cur->bc_tp;
6139 licp = &tp->t_items;
6140 while (!bp && licp != NULL) {
Eric Sandeen39dab9d2008-08-13 16:10:52 +10006141 if (xfs_lic_are_all_free(licp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 licp = licp->lic_next;
6143 continue;
6144 }
6145 for (i = 0; i < licp->lic_unused; i++) {
6146 xfs_log_item_desc_t *lidp;
6147 xfs_log_item_t *lip;
6148 xfs_buf_log_item_t *bip;
6149 xfs_buf_t *lbp;
6150
Eric Sandeen39dab9d2008-08-13 16:10:52 +10006151 if (xfs_lic_isfree(licp, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 continue;
6153 }
6154
Eric Sandeen39dab9d2008-08-13 16:10:52 +10006155 lidp = xfs_lic_slot(licp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 lip = lidp->lid_item;
6157 if (lip->li_type != XFS_LI_BUF)
6158 continue;
6159
6160 bip = (xfs_buf_log_item_t *)lip;
6161 lbp = bip->bli_buf;
6162
6163 if (XFS_BUF_ADDR(lbp) == bno) {
6164 bp = lbp;
6165 break; /* Found it */
6166 }
6167 }
6168 licp = licp->lic_next;
6169 }
6170 }
6171 return(bp);
6172}
6173
6174void
6175xfs_check_block(
6176 xfs_bmbt_block_t *block,
6177 xfs_mount_t *mp,
6178 int root,
6179 short sz)
6180{
6181 int i, j, dmxr;
Christoph Hellwig576039c2006-09-28 10:58:06 +10006182 __be64 *pp, *thispa; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 xfs_bmbt_key_t *prevp, *keyp;
6184
Christoph Hellwig16259e72005-11-02 15:11:25 +11006185 ASSERT(be16_to_cpu(block->bb_level) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186
6187 prevp = NULL;
Christoph Hellwig16259e72005-11-02 15:11:25 +11006188 for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 dmxr = mp->m_bmap_dmxr[0];
6190
6191 if (root) {
6192 keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
6193 } else {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006194 keyp = XFS_BTREE_KEY_ADDR(xfs_bmbt, block, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 }
6196
6197 if (prevp) {
6198 xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
6199 }
6200 prevp = keyp;
6201
6202 /*
6203 * Compare the block numbers to see if there are dups.
6204 */
6205
6206 if (root) {
6207 pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
6208 } else {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006209 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, i, dmxr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11006211 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 if (root) {
6213 thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
6214 } else {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006215 thispa = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, j,
6216 dmxr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 }
Christoph Hellwig576039c2006-09-28 10:58:06 +10006218 if (*thispa == *pp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
Harvey Harrison34a622b2008-04-10 12:19:21 +10006220 __func__, j, i,
Christoph Hellwig576039c2006-09-28 10:58:06 +10006221 (unsigned long long)be64_to_cpu(*thispa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 panic("%s: ptrs are equal in node\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +10006223 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 }
6225 }
6226 }
6227}
6228
6229/*
6230 * Check that the extents for the inode ip are in the right order in all
6231 * btree leaves.
6232 */
6233
6234STATIC void
6235xfs_bmap_check_leaf_extents(
6236 xfs_btree_cur_t *cur, /* btree cursor or null */
6237 xfs_inode_t *ip, /* incore inode pointer */
6238 int whichfork) /* data or attr fork */
6239{
6240 xfs_bmbt_block_t *block; /* current btree block */
6241 xfs_fsblock_t bno; /* block # of "block" */
6242 xfs_buf_t *bp; /* buffer for "block" */
6243 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006244 xfs_extnum_t i=0, j; /* index into the extents list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 xfs_ifork_t *ifp; /* fork structure */
6246 int level; /* btree level, for checking */
6247 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10006248 __be64 *pp; /* pointer to block address */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006249 xfs_bmbt_rec_t *ep; /* pointer to current extent */
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006250 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006251 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 int bp_release = 0;
6253
6254 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6255 return;
6256 }
6257
6258 bno = NULLFSBLOCK;
6259 mp = ip->i_mount;
6260 ifp = XFS_IFORK_PTR(ip, whichfork);
6261 block = ifp->if_broot;
6262 /*
6263 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6264 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11006265 level = be16_to_cpu(block->bb_level);
6266 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6268 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006269 bno = be64_to_cpu(*pp);
6270
6271 ASSERT(bno != NULLDFSBNO);
6272 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6273 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6274
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 /*
6276 * Go down the tree until leaf level is reached, following the first
6277 * pointer (leftmost) at each level.
6278 */
6279 while (level-- > 0) {
6280 /* See if buf is in cur first */
6281 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6282 if (bp) {
6283 bp_release = 0;
6284 } else {
6285 bp_release = 1;
6286 }
6287 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6288 XFS_BMAP_BTREE_REF)))
6289 goto error_norelse;
6290 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6291 XFS_WANT_CORRUPTED_GOTO(
6292 XFS_BMAP_SANITY_CHECK(mp, block, level),
6293 error0);
6294 if (level == 0)
6295 break;
6296
6297 /*
6298 * Check this block for basic sanity (increasing keys and
6299 * no duplicate blocks).
6300 */
6301
6302 xfs_check_block(block, mp, 0, 0);
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006303 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006304 bno = be64_to_cpu(*pp);
6305 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 if (bp_release) {
6307 bp_release = 0;
6308 xfs_trans_brelse(NULL, bp);
6309 }
6310 }
6311
6312 /*
6313 * Here with bp and block set to the leftmost leaf node in the tree.
6314 */
6315 i = 0;
6316
6317 /*
6318 * Loop over all leaf nodes checking that all extents are in the right order.
6319 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 xfs_fsblock_t nextbno;
6322 xfs_extnum_t num_recs;
6323
6324
Christoph Hellwig16259e72005-11-02 15:11:25 +11006325 num_recs = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326
6327 /*
6328 * Read-ahead the next leaf block, if any.
6329 */
6330
Christoph Hellwig16259e72005-11-02 15:11:25 +11006331 nextbno = be64_to_cpu(block->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332
6333 /*
6334 * Check all the extents to make sure they are OK.
6335 * If we had a previous block, the last entry should
6336 * conform with the first entry in this one.
6337 */
6338
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006339 ep = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006340 if (i) {
6341 xfs_btree_check_rec(XFS_BTNUM_BMAP, &last, ep);
6342 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006343 for (j = 1; j < num_recs; j++) {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006344 nextp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, j + 1);
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006345 xfs_btree_check_rec(XFS_BTNUM_BMAP, ep, nextp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006346 ep = nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006349 last = *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 i += num_recs;
6351 if (bp_release) {
6352 bp_release = 0;
6353 xfs_trans_brelse(NULL, bp);
6354 }
6355 bno = nextbno;
6356 /*
6357 * If we've reached the end, stop.
6358 */
6359 if (bno == NULLFSBLOCK)
6360 break;
6361
6362 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6363 if (bp) {
6364 bp_release = 0;
6365 } else {
6366 bp_release = 1;
6367 }
6368 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6369 XFS_BMAP_BTREE_REF)))
6370 goto error_norelse;
6371 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6372 }
6373 if (bp_release) {
6374 bp_release = 0;
6375 xfs_trans_brelse(NULL, bp);
6376 }
6377 return;
6378
6379error0:
Harvey Harrison34a622b2008-04-10 12:19:21 +10006380 cmn_err(CE_WARN, "%s: at error0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 if (bp_release)
6382 xfs_trans_brelse(NULL, bp);
6383error_norelse:
6384 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
Harvey Harrison34a622b2008-04-10 12:19:21 +10006385 __func__, i);
6386 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 return;
6388}
6389#endif
6390
6391/*
6392 * Count fsblocks of the given fork.
6393 */
6394int /* error */
6395xfs_bmap_count_blocks(
6396 xfs_trans_t *tp, /* transaction pointer */
6397 xfs_inode_t *ip, /* incore inode */
6398 int whichfork, /* data or attr fork */
6399 int *count) /* out: count of blocks */
6400{
6401 xfs_bmbt_block_t *block; /* current btree block */
6402 xfs_fsblock_t bno; /* block # of "block" */
6403 xfs_ifork_t *ifp; /* fork structure */
6404 int level; /* btree level, for checking */
6405 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10006406 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407
6408 bno = NULLFSBLOCK;
6409 mp = ip->i_mount;
6410 ifp = XFS_IFORK_PTR(ip, whichfork);
6411 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
Ruben Porrasc94312d2008-08-13 16:52:25 +10006412 xfs_bmap_count_leaves(ifp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
Ruben Porrasc94312d2008-08-13 16:52:25 +10006414 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415 return 0;
6416 }
6417
6418 /*
6419 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6420 */
6421 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11006422 level = be16_to_cpu(block->bb_level);
6423 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006425 bno = be64_to_cpu(*pp);
6426 ASSERT(bno != NULLDFSBNO);
6427 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6428 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006430 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6432 mp);
6433 return XFS_ERROR(EFSCORRUPTED);
6434 }
6435
6436 return 0;
6437}
6438
6439/*
6440 * Recursively walks each level of a btree
6441 * to count total fsblocks is use.
6442 */
David Chinnera8272ce2007-11-23 16:28:09 +11006443STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444xfs_bmap_count_tree(
6445 xfs_mount_t *mp, /* file system mount point */
6446 xfs_trans_t *tp, /* transaction pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006447 xfs_ifork_t *ifp, /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 xfs_fsblock_t blockno, /* file system block number */
6449 int levelin, /* level in btree */
6450 int *count) /* Count of blocks */
6451{
6452 int error;
6453 xfs_buf_t *bp, *nbp;
6454 int level = levelin;
Christoph Hellwig576039c2006-09-28 10:58:06 +10006455 __be64 *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 xfs_fsblock_t bno = blockno;
6457 xfs_fsblock_t nextbno;
6458 xfs_bmbt_block_t *block, *nextblock;
6459 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460
6461 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6462 return error;
6463 *count += 1;
6464 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6465
6466 if (--level) {
6467 /* Not at node above leafs, count this level of nodes */
Christoph Hellwig16259e72005-11-02 15:11:25 +11006468 nextbno = be64_to_cpu(block->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 while (nextbno != NULLFSBLOCK) {
6470 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6471 0, &nbp, XFS_BMAP_BTREE_REF)))
6472 return error;
6473 *count += 1;
6474 nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11006475 nextbno = be64_to_cpu(nextblock->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 xfs_trans_brelse(tp, nbp);
6477 }
6478
6479 /* Dive to the next level */
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006480 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006481 bno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 if (unlikely((error =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006483 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 xfs_trans_brelse(tp, bp);
6485 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6486 XFS_ERRLEVEL_LOW, mp);
6487 return XFS_ERROR(EFSCORRUPTED);
6488 }
6489 xfs_trans_brelse(tp, bp);
6490 } else {
6491 /* count all level 1 nodes and their leaves */
6492 for (;;) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11006493 nextbno = be64_to_cpu(block->bb_rightsib);
6494 numrecs = be16_to_cpu(block->bb_numrecs);
Ruben Porrasc94312d2008-08-13 16:52:25 +10006495 xfs_bmap_disk_count_leaves(0, block, numrecs, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 xfs_trans_brelse(tp, bp);
6497 if (nextbno == NULLFSBLOCK)
6498 break;
6499 bno = nextbno;
6500 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6501 XFS_BMAP_BTREE_REF)))
6502 return error;
6503 *count += 1;
6504 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6505 }
6506 }
6507 return 0;
6508}
6509
6510/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006511 * Count leaf blocks given a range of extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006513STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006515 xfs_ifork_t *ifp,
6516 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 int numrecs,
6518 int *count)
6519{
6520 int b;
6521
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006522 for (b = 0; b < numrecs; b++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006523 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
Yingping Lu91e11082005-11-02 15:10:24 +11006524 *count += xfs_bmbt_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006525 }
Yingping Lu91e11082005-11-02 15:10:24 +11006526}
6527
6528/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006529 * Count leaf blocks given a range of extent records originally
6530 * in btree format.
Yingping Lu91e11082005-11-02 15:10:24 +11006531 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006532STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +11006533xfs_bmap_disk_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006534 xfs_extnum_t idx,
6535 xfs_bmbt_block_t *block,
Yingping Lu91e11082005-11-02 15:10:24 +11006536 int numrecs,
6537 int *count)
6538{
6539 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006540 xfs_bmbt_rec_t *frp;
Yingping Lu91e11082005-11-02 15:10:24 +11006541
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006542 for (b = 1; b <= numrecs; b++) {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006543 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, idx + b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544 *count += xfs_bmbt_disk_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546}