blob: a1aab9275d5ac727700a00070b89c88e1acb8e42 [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
405/*
406 * Called from xfs_bmap_add_attrfork to handle btree format files.
407 */
408STATIC int /* error */
409xfs_bmap_add_attrfork_btree(
410 xfs_trans_t *tp, /* transaction pointer */
411 xfs_inode_t *ip, /* incore inode pointer */
412 xfs_fsblock_t *firstblock, /* first block allocated */
413 xfs_bmap_free_t *flist, /* blocks to free at commit */
414 int *flags) /* inode logging flags */
415{
416 xfs_btree_cur_t *cur; /* btree cursor */
417 int error; /* error return value */
418 xfs_mount_t *mp; /* file system mount struct */
419 int stat; /* newroot status */
420
421 mp = ip->i_mount;
422 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
423 *flags |= XFS_ILOG_DBROOT;
424 else {
425 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
426 XFS_DATA_FORK);
427 cur->bc_private.b.flist = flist;
428 cur->bc_private.b.firstblock = *firstblock;
429 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
430 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000431 /* must be at least one entry */
432 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
434 goto error0;
435 if (stat == 0) {
436 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
437 return XFS_ERROR(ENOSPC);
438 }
439 *firstblock = cur->bc_private.b.firstblock;
440 cur->bc_private.b.allocated = 0;
441 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
442 }
443 return 0;
444error0:
445 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
446 return error;
447}
448
449/*
450 * Called from xfs_bmap_add_attrfork to handle extents format files.
451 */
452STATIC int /* error */
453xfs_bmap_add_attrfork_extents(
454 xfs_trans_t *tp, /* transaction pointer */
455 xfs_inode_t *ip, /* incore inode pointer */
456 xfs_fsblock_t *firstblock, /* first block allocated */
457 xfs_bmap_free_t *flist, /* blocks to free at commit */
458 int *flags) /* inode logging flags */
459{
460 xfs_btree_cur_t *cur; /* bmap btree cursor */
461 int error; /* error return value */
462
463 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
464 return 0;
465 cur = NULL;
466 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
467 flags, XFS_DATA_FORK);
468 if (cur) {
469 cur->bc_private.b.allocated = 0;
470 xfs_btree_del_cursor(cur,
471 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
472 }
473 return error;
474}
475
476/*
477 * Called from xfs_bmap_add_attrfork to handle local format files.
478 */
479STATIC int /* error */
480xfs_bmap_add_attrfork_local(
481 xfs_trans_t *tp, /* transaction pointer */
482 xfs_inode_t *ip, /* incore inode pointer */
483 xfs_fsblock_t *firstblock, /* first block allocated */
484 xfs_bmap_free_t *flist, /* blocks to free at commit */
485 int *flags) /* inode logging flags */
486{
487 xfs_da_args_t dargs; /* args for dir/attr code */
488 int error; /* error return value */
489 xfs_mount_t *mp; /* mount structure pointer */
490
491 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
492 return 0;
493 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
494 mp = ip->i_mount;
495 memset(&dargs, 0, sizeof(dargs));
496 dargs.dp = ip;
497 dargs.firstblock = firstblock;
498 dargs.flist = flist;
499 dargs.total = mp->m_dirblkfsbs;
500 dargs.whichfork = XFS_DATA_FORK;
501 dargs.trans = tp;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000502 error = xfs_dir2_sf_to_block(&dargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 } else
504 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
505 XFS_DATA_FORK);
506 return error;
507}
508
509/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100510 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * after allocating space (or doing a delayed allocation).
512 */
513STATIC int /* error */
514xfs_bmap_add_extent(
515 xfs_inode_t *ip, /* incore inode pointer */
516 xfs_extnum_t idx, /* extent number to update/insert */
517 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100518 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 xfs_fsblock_t *first, /* pointer to firstblock variable */
520 xfs_bmap_free_t *flist, /* list of extents to be freed */
521 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000522 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 int whichfork, /* data or attr fork */
524 int rsvd) /* OK to use reserved data blocks */
525{
526 xfs_btree_cur_t *cur; /* btree cursor or null */
527 xfs_filblks_t da_new; /* new count del alloc blocks used */
528 xfs_filblks_t da_old; /* old count del alloc blocks used */
529 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 xfs_ifork_t *ifp; /* inode fork ptr */
531 int logflags; /* returned value */
532 xfs_extnum_t nextents; /* number of extents in file now */
533
534 XFS_STATS_INC(xs_add_exlist);
535 cur = *curp;
536 ifp = XFS_IFORK_PTR(ip, whichfork);
537 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
538 ASSERT(idx <= nextents);
539 da_old = da_new = 0;
540 error = 0;
541 /*
542 * This is the first extent added to a new/empty file.
543 * Special case this one, so other routines get to assume there are
544 * already extents in the list.
545 */
546 if (nextents == 0) {
Eric Sandeen3a59c942007-07-11 11:09:47 +1000547 XFS_BMAP_TRACE_INSERT("insert empty", ip, 0, 1, new, NULL,
548 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100549 xfs_iext_insert(ifp, 0, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 ASSERT(cur == NULL);
551 ifp->if_lastex = 0;
552 if (!ISNULLSTARTBLOCK(new->br_startblock)) {
553 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
554 logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
555 } else
556 logflags = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000557 /* DELTA: single new extent */
558 if (delta) {
559 if (delta->xed_startoff > new->br_startoff)
560 delta->xed_startoff = new->br_startoff;
561 if (delta->xed_blockcount <
562 new->br_startoff + new->br_blockcount)
563 delta->xed_blockcount = new->br_startoff +
564 new->br_blockcount;
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567 /*
568 * Any kind of new delayed allocation goes here.
569 */
570 else if (ISNULLSTARTBLOCK(new->br_startblock)) {
571 if (cur)
572 ASSERT((cur->bc_private.b.flags &
573 XFS_BTCUR_BPRV_WASDEL) == 0);
Eric Sandeene9ed9d22007-05-08 13:48:56 +1000574 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000575 &logflags, delta, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 goto done;
577 }
578 /*
579 * Real allocation off the end of the file.
580 */
581 else if (idx == nextents) {
582 if (cur)
583 ASSERT((cur->bc_private.b.flags &
584 XFS_BTCUR_BPRV_WASDEL) == 0);
585 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000586 &logflags, delta, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 goto done;
588 } else {
589 xfs_bmbt_irec_t prev; /* old extent at offset idx */
590
591 /*
592 * Get the record referred to by idx.
593 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100594 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /*
596 * If it's a real allocation record, and the new allocation ends
597 * after the start of the referred to record, then we're filling
598 * in a delayed or unwritten allocation with a real one, or
599 * converting real back to unwritten.
600 */
601 if (!ISNULLSTARTBLOCK(new->br_startblock) &&
602 new->br_startoff + new->br_blockcount > prev.br_startoff) {
603 if (prev.br_state != XFS_EXT_UNWRITTEN &&
604 ISNULLSTARTBLOCK(prev.br_startblock)) {
605 da_old = STARTBLOCKVAL(prev.br_startblock);
606 if (cur)
607 ASSERT(cur->bc_private.b.flags &
608 XFS_BTCUR_BPRV_WASDEL);
609 if ((error = xfs_bmap_add_extent_delay_real(ip,
610 idx, &cur, new, &da_new, first, flist,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000611 &logflags, delta, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 goto done;
613 } else if (new->br_state == XFS_EXT_NORM) {
614 ASSERT(new->br_state == XFS_EXT_NORM);
615 if ((error = xfs_bmap_add_extent_unwritten_real(
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000616 ip, idx, &cur, new, &logflags, delta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 goto done;
618 } else {
619 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
620 if ((error = xfs_bmap_add_extent_unwritten_real(
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000621 ip, idx, &cur, new, &logflags, delta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 goto done;
623 }
624 ASSERT(*curp == cur || *curp == NULL);
625 }
626 /*
627 * Otherwise we're filling in a hole with an allocation.
628 */
629 else {
630 if (cur)
631 ASSERT((cur->bc_private.b.flags &
632 XFS_BTCUR_BPRV_WASDEL) == 0);
633 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000634 new, &logflags, delta, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 goto done;
636 }
637 }
638
639 ASSERT(*curp == cur || *curp == NULL);
640 /*
641 * Convert to a btree if necessary.
642 */
643 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
644 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
645 int tmp_logflags; /* partial log flag return val */
646
647 ASSERT(cur == NULL);
648 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
649 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
650 logflags |= tmp_logflags;
651 if (error)
652 goto done;
653 }
654 /*
655 * Adjust for changes in reserved delayed indirect blocks.
656 * Nothing to do for disk quotas here.
657 */
658 if (da_old || da_new) {
659 xfs_filblks_t nblks;
660
661 nblks = da_new;
662 if (cur)
663 nblks += cur->bc_private.b.allocated;
664 ASSERT(nblks <= da_old);
665 if (nblks < da_old)
666 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100667 (int64_t)(da_old - nblks), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669 /*
670 * Clear out the allocated field, done with it now in any case.
671 */
672 if (cur) {
673 cur->bc_private.b.allocated = 0;
674 *curp = cur;
675 }
676done:
677#ifdef DEBUG
678 if (!error)
679 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
680#endif
681 *logflagsp = logflags;
682 return error;
683}
684
685/*
686 * Called by xfs_bmap_add_extent to handle cases converting a delayed
687 * allocation to a real allocation.
688 */
689STATIC int /* error */
690xfs_bmap_add_extent_delay_real(
691 xfs_inode_t *ip, /* incore inode pointer */
692 xfs_extnum_t idx, /* extent number to update/insert */
693 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100694 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
696 xfs_fsblock_t *first, /* pointer to firstblock variable */
697 xfs_bmap_free_t *flist, /* list of extents to be freed */
698 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000699 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int rsvd) /* OK to use reserved data block allocation */
701{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 xfs_btree_cur_t *cur; /* btree cursor */
703 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000704 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100707 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 xfs_fileoff_t new_endoff; /* end offset of new entry */
709 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
710 /* left is 0, right is 1, prev is 2 */
711 int rval=0; /* return value (logging flags) */
712 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000713 xfs_filblks_t temp=0; /* value for dnew calculations */
714 xfs_filblks_t temp2=0;/* value for dnew calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 int tmp_rval; /* partial logging flags */
716 enum { /* bit number definitions for state */
717 LEFT_CONTIG, RIGHT_CONTIG,
718 LEFT_FILLING, RIGHT_FILLING,
719 LEFT_DELAY, RIGHT_DELAY,
720 LEFT_VALID, RIGHT_VALID
721 };
722
723#define LEFT r[0]
724#define RIGHT r[1]
725#define PREV r[2]
726#define MASK(b) (1 << (b))
727#define MASK2(a,b) (MASK(a) | MASK(b))
728#define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
729#define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
730#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
731#define STATE_TEST(b) (state & MASK(b))
732#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
733 ((state &= ~MASK(b)), 0))
734#define SWITCH_STATE \
735 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
736
737 /*
738 * Set up a bunch of variables to make the tests simpler.
739 */
740 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100741 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
742 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 xfs_bmbt_get_all(ep, &PREV);
744 new_endoff = new->br_startoff + new->br_blockcount;
745 ASSERT(PREV.br_startoff <= new->br_startoff);
746 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
747 /*
748 * Set flags determining what part of the previous delayed allocation
749 * extent is being replaced by a real allocation.
750 */
751 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
752 STATE_SET(RIGHT_FILLING,
753 PREV.br_startoff + PREV.br_blockcount == new_endoff);
754 /*
755 * Check and set flags if this segment has a left neighbor.
756 * Don't set contiguous if the combined extent would be too large.
757 */
758 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100759 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
761 }
762 STATE_SET(LEFT_CONTIG,
763 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
764 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
765 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
766 LEFT.br_state == new->br_state &&
767 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
768 /*
769 * Check and set flags if this segment has a right neighbor.
770 * Don't set contiguous if the combined extent would be too large.
771 * Also check for all-three-contiguous being too large.
772 */
773 if (STATE_SET_TEST(RIGHT_VALID,
774 idx <
775 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100776 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
778 }
779 STATE_SET(RIGHT_CONTIG,
780 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
781 new_endoff == RIGHT.br_startoff &&
782 new->br_startblock + new->br_blockcount ==
783 RIGHT.br_startblock &&
784 new->br_state == RIGHT.br_state &&
785 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
786 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
787 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
788 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
789 <= MAXEXTLEN));
790 error = 0;
791 /*
792 * Switch out based on the FILLING and CONTIG state bits.
793 */
794 switch (SWITCH_STATE) {
795
796 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
797 /*
798 * Filling in all of a previously delayed allocation extent.
799 * The left and right neighbors are both contiguous with new.
800 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000801 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100803 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 LEFT.br_blockcount + PREV.br_blockcount +
805 RIGHT.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000806 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000808 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100809 xfs_iext_remove(ifp, idx, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 ip->i_df.if_lastex = idx - 1;
811 ip->i_d.di_nextents--;
812 if (cur == NULL)
813 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
814 else {
815 rval = XFS_ILOG_CORE;
816 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
817 RIGHT.br_startblock,
818 RIGHT.br_blockcount, &i)))
819 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000820 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if ((error = xfs_bmbt_delete(cur, &i)))
822 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000823 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
825 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000826 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
828 LEFT.br_startblock,
829 LEFT.br_blockcount +
830 PREV.br_blockcount +
831 RIGHT.br_blockcount, LEFT.br_state)))
832 goto done;
833 }
834 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000835 /* DELTA: Three in-core extents are replaced by one. */
836 temp = LEFT.br_startoff;
837 temp2 = LEFT.br_blockcount +
838 PREV.br_blockcount +
839 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 break;
841
842 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
843 /*
844 * Filling in all of a previously delayed allocation extent.
845 * The left neighbor is contiguous, the right is not.
846 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000847 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", 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);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000851 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 XFS_DATA_FORK);
853 ip->i_df.if_lastex = idx - 1;
Eric Sandeen3a59c942007-07-11 11:09:47 +1000854 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100855 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (cur == NULL)
857 rval = XFS_ILOG_DEXT;
858 else {
859 rval = 0;
860 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
861 LEFT.br_startblock, LEFT.br_blockcount,
862 &i)))
863 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000864 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
866 LEFT.br_startblock,
867 LEFT.br_blockcount +
868 PREV.br_blockcount, LEFT.br_state)))
869 goto done;
870 }
871 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000872 /* DELTA: Two in-core extents are replaced by one. */
873 temp = LEFT.br_startoff;
874 temp2 = LEFT.br_blockcount +
875 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 break;
877
878 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
879 /*
880 * Filling in all of a previously delayed allocation extent.
881 * The right neighbor is contiguous, the left is not.
882 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000883 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 xfs_bmbt_set_startblock(ep, new->br_startblock);
885 xfs_bmbt_set_blockcount(ep,
886 PREV.br_blockcount + RIGHT.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000887 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 ip->i_df.if_lastex = idx;
Eric Sandeen3a59c942007-07-11 11:09:47 +1000889 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100890 xfs_iext_remove(ifp, idx + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (cur == NULL)
892 rval = XFS_ILOG_DEXT;
893 else {
894 rval = 0;
895 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
896 RIGHT.br_startblock,
897 RIGHT.br_blockcount, &i)))
898 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000899 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
901 new->br_startblock,
902 PREV.br_blockcount +
903 RIGHT.br_blockcount, PREV.br_state)))
904 goto done;
905 }
906 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000907 /* DELTA: Two in-core extents are replaced by one. */
908 temp = PREV.br_startoff;
909 temp2 = PREV.br_blockcount +
910 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 break;
912
913 case MASK2(LEFT_FILLING, RIGHT_FILLING):
914 /*
915 * Filling in all of a previously delayed allocation extent.
916 * Neither the left nor right neighbors are contiguous with
917 * the new one.
918 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000919 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 xfs_bmbt_set_startblock(ep, new->br_startblock);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000921 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 ip->i_df.if_lastex = idx;
923 ip->i_d.di_nextents++;
924 if (cur == NULL)
925 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
926 else {
927 rval = XFS_ILOG_CORE;
928 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
929 new->br_startblock, new->br_blockcount,
930 &i)))
931 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000932 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 cur->bc_rec.b.br_state = XFS_EXT_NORM;
934 if ((error = xfs_bmbt_insert(cur, &i)))
935 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000936 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000939 /* DELTA: The in-core extent described by new changed type. */
940 temp = new->br_startoff;
941 temp2 = new->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 break;
943
944 case MASK2(LEFT_FILLING, LEFT_CONTIG):
945 /*
946 * Filling in the first part of a previous delayed allocation.
947 * The left neighbor is contiguous.
948 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000949 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100950 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 LEFT.br_blockcount + new->br_blockcount);
952 xfs_bmbt_set_startoff(ep,
953 PREV.br_startoff + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000954 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 temp = PREV.br_blockcount - new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +1000956 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 xfs_bmbt_set_blockcount(ep, temp);
958 ip->i_df.if_lastex = idx - 1;
959 if (cur == NULL)
960 rval = XFS_ILOG_DEXT;
961 else {
962 rval = 0;
963 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
964 LEFT.br_startblock, LEFT.br_blockcount,
965 &i)))
966 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000967 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
969 LEFT.br_startblock,
970 LEFT.br_blockcount +
971 new->br_blockcount,
972 LEFT.br_state)))
973 goto done;
974 }
975 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
976 STARTBLOCKVAL(PREV.br_startblock));
977 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +1000978 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000980 /* DELTA: The boundary between two in-core extents moved. */
981 temp = LEFT.br_startoff;
982 temp2 = LEFT.br_blockcount +
983 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 break;
985
986 case MASK(LEFT_FILLING):
987 /*
988 * Filling in the first part of a previous delayed allocation.
989 * The left neighbor is not contiguous.
990 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000991 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 xfs_bmbt_set_startoff(ep, new_endoff);
993 temp = PREV.br_blockcount - new->br_blockcount;
994 xfs_bmbt_set_blockcount(ep, temp);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000995 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100997 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 ip->i_df.if_lastex = idx;
999 ip->i_d.di_nextents++;
1000 if (cur == NULL)
1001 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1002 else {
1003 rval = XFS_ILOG_CORE;
1004 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1005 new->br_startblock, new->br_blockcount,
1006 &i)))
1007 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001008 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1010 if ((error = xfs_bmbt_insert(cur, &i)))
1011 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001012 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1015 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1016 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1017 first, flist, &cur, 1, &tmp_rval,
1018 XFS_DATA_FORK);
1019 rval |= tmp_rval;
1020 if (error)
1021 goto done;
1022 }
1023 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1024 STARTBLOCKVAL(PREV.br_startblock) -
1025 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001026 ep = xfs_iext_get_ext(ifp, idx + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001028 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx + 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001030 /* DELTA: One in-core extent is split in two. */
1031 temp = PREV.br_startoff;
1032 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 break;
1034
1035 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1036 /*
1037 * Filling in the last part of a previous delayed allocation.
1038 * The right neighbor is contiguous with the new allocation.
1039 */
1040 temp = PREV.br_blockcount - new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001041 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
1042 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 xfs_bmbt_set_blockcount(ep, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001044 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1045 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 new->br_blockcount + RIGHT.br_blockcount,
1047 RIGHT.br_state);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001048 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 ip->i_df.if_lastex = idx + 1;
1050 if (cur == NULL)
1051 rval = XFS_ILOG_DEXT;
1052 else {
1053 rval = 0;
1054 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1055 RIGHT.br_startblock,
1056 RIGHT.br_blockcount, &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 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1060 new->br_startblock,
1061 new->br_blockcount +
1062 RIGHT.br_blockcount,
1063 RIGHT.br_state)))
1064 goto done;
1065 }
1066 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1067 STARTBLOCKVAL(PREV.br_startblock));
1068 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001069 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001071 /* DELTA: The boundary between two in-core extents moved. */
1072 temp = PREV.br_startoff;
1073 temp2 = PREV.br_blockcount +
1074 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 break;
1076
1077 case MASK(RIGHT_FILLING):
1078 /*
1079 * Filling in the last part of a previous delayed allocation.
1080 * The right neighbor is not contiguous.
1081 */
1082 temp = PREV.br_blockcount - new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001083 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 xfs_bmbt_set_blockcount(ep, temp);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001085 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1086 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001087 xfs_iext_insert(ifp, idx + 1, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 ip->i_df.if_lastex = idx + 1;
1089 ip->i_d.di_nextents++;
1090 if (cur == NULL)
1091 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1092 else {
1093 rval = XFS_ILOG_CORE;
1094 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1095 new->br_startblock, new->br_blockcount,
1096 &i)))
1097 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001098 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1100 if ((error = xfs_bmbt_insert(cur, &i)))
1101 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001102 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1105 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1106 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1107 first, flist, &cur, 1, &tmp_rval,
1108 XFS_DATA_FORK);
1109 rval |= tmp_rval;
1110 if (error)
1111 goto done;
1112 }
1113 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1114 STARTBLOCKVAL(PREV.br_startblock) -
1115 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001116 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001118 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001120 /* DELTA: One in-core extent is split in two. */
1121 temp = PREV.br_startoff;
1122 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 break;
1124
1125 case 0:
1126 /*
1127 * Filling in the middle part of a previous delayed allocation.
1128 * Contiguity is impossible here.
1129 * This case is avoided almost all the time.
1130 */
1131 temp = new->br_startoff - PREV.br_startoff;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001132 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 xfs_bmbt_set_blockcount(ep, temp);
1134 r[0] = *new;
Lachlan McIlroyd2133712006-11-21 18:55:16 +11001135 r[1].br_state = PREV.br_state;
1136 r[1].br_startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 r[1].br_startoff = new_endoff;
1138 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1139 r[1].br_blockcount = temp2;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001140 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001142 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 ip->i_df.if_lastex = idx + 1;
1144 ip->i_d.di_nextents++;
1145 if (cur == NULL)
1146 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1147 else {
1148 rval = XFS_ILOG_CORE;
1149 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1150 new->br_startblock, new->br_blockcount,
1151 &i)))
1152 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001153 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1155 if ((error = xfs_bmbt_insert(cur, &i)))
1156 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001157 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1160 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1161 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1162 first, flist, &cur, 1, &tmp_rval,
1163 XFS_DATA_FORK);
1164 rval |= tmp_rval;
1165 if (error)
1166 goto done;
1167 }
1168 temp = xfs_bmap_worst_indlen(ip, temp);
1169 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1170 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
1171 (cur ? cur->bc_private.b.allocated : 0));
1172 if (diff > 0 &&
David Chinner20f4ebf2007-02-10 18:36:10 +11001173 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 /*
1175 * Ick gross gag me with a spoon.
1176 */
1177 ASSERT(0); /* want to see if this ever happens! */
1178 while (diff > 0) {
1179 if (temp) {
1180 temp--;
1181 diff--;
1182 if (!diff ||
1183 !xfs_mod_incore_sb(ip->i_mount,
David Chinner20f4ebf2007-02-10 18:36:10 +11001184 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 break;
1186 }
1187 if (temp2) {
1188 temp2--;
1189 diff--;
1190 if (!diff ||
1191 !xfs_mod_incore_sb(ip->i_mount,
David Chinner20f4ebf2007-02-10 18:36:10 +11001192 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 break;
1194 }
1195 }
1196 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001197 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001199 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
1200 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001201 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1202 NULLSTARTBLOCK((int)temp2));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001203 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 *dnew = temp + temp2;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001205 /* DELTA: One in-core extent is split in three. */
1206 temp = PREV.br_startoff;
1207 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 break;
1209
1210 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1211 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1212 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1213 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1214 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1215 case MASK(LEFT_CONTIG):
1216 case MASK(RIGHT_CONTIG):
1217 /*
1218 * These cases are all impossible.
1219 */
1220 ASSERT(0);
1221 }
1222 *curp = cur;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001223 if (delta) {
1224 temp2 += temp;
1225 if (delta->xed_startoff > temp)
1226 delta->xed_startoff = temp;
1227 if (delta->xed_blockcount < temp2)
1228 delta->xed_blockcount = temp2;
1229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230done:
1231 *logflagsp = rval;
1232 return error;
1233#undef LEFT
1234#undef RIGHT
1235#undef PREV
1236#undef MASK
1237#undef MASK2
1238#undef MASK3
1239#undef MASK4
1240#undef STATE_SET
1241#undef STATE_TEST
1242#undef STATE_SET_TEST
1243#undef SWITCH_STATE
1244}
1245
1246/*
1247 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1248 * allocation to a real allocation or vice versa.
1249 */
1250STATIC int /* error */
1251xfs_bmap_add_extent_unwritten_real(
1252 xfs_inode_t *ip, /* incore inode pointer */
1253 xfs_extnum_t idx, /* extent number to update/insert */
1254 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001255 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001256 int *logflagsp, /* inode logging flags */
1257 xfs_extdelta_t *delta) /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001260 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001263 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 xfs_fileoff_t new_endoff; /* end offset of new entry */
1265 xfs_exntst_t newext; /* new extent state */
1266 xfs_exntst_t oldext; /* old extent state */
1267 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1268 /* left is 0, right is 1, prev is 2 */
1269 int rval=0; /* return value (logging flags) */
1270 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001271 xfs_filblks_t temp=0;
1272 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 enum { /* bit number definitions for state */
1274 LEFT_CONTIG, RIGHT_CONTIG,
1275 LEFT_FILLING, RIGHT_FILLING,
1276 LEFT_DELAY, RIGHT_DELAY,
1277 LEFT_VALID, RIGHT_VALID
1278 };
1279
1280#define LEFT r[0]
1281#define RIGHT r[1]
1282#define PREV r[2]
1283#define MASK(b) (1 << (b))
1284#define MASK2(a,b) (MASK(a) | MASK(b))
1285#define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1286#define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1287#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1288#define STATE_TEST(b) (state & MASK(b))
1289#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1290 ((state &= ~MASK(b)), 0))
1291#define SWITCH_STATE \
1292 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1293
1294 /*
1295 * Set up a bunch of variables to make the tests simpler.
1296 */
1297 error = 0;
1298 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001299 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1300 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 xfs_bmbt_get_all(ep, &PREV);
1302 newext = new->br_state;
1303 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1304 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1305 ASSERT(PREV.br_state == oldext);
1306 new_endoff = new->br_startoff + new->br_blockcount;
1307 ASSERT(PREV.br_startoff <= new->br_startoff);
1308 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1309 /*
1310 * Set flags determining what part of the previous oldext allocation
1311 * extent is being replaced by a newext allocation.
1312 */
1313 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
1314 STATE_SET(RIGHT_FILLING,
1315 PREV.br_startoff + PREV.br_blockcount == new_endoff);
1316 /*
1317 * Check and set flags if this segment has a left neighbor.
1318 * Don't set contiguous if the combined extent would be too large.
1319 */
1320 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001321 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
1323 }
1324 STATE_SET(LEFT_CONTIG,
1325 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1326 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1327 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1328 LEFT.br_state == newext &&
1329 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1330 /*
1331 * Check and set flags if this segment has a right neighbor.
1332 * Don't set contiguous if the combined extent would be too large.
1333 * Also check for all-three-contiguous being too large.
1334 */
1335 if (STATE_SET_TEST(RIGHT_VALID,
1336 idx <
1337 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001338 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
1340 }
1341 STATE_SET(RIGHT_CONTIG,
1342 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1343 new_endoff == RIGHT.br_startoff &&
1344 new->br_startblock + new->br_blockcount ==
1345 RIGHT.br_startblock &&
1346 newext == RIGHT.br_state &&
1347 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1348 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
1349 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
1350 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1351 <= MAXEXTLEN));
1352 /*
1353 * Switch out based on the FILLING and CONTIG state bits.
1354 */
1355 switch (SWITCH_STATE) {
1356
1357 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1358 /*
1359 * Setting all of a previous oldext extent to newext.
1360 * The left and right neighbors are both contiguous with new.
1361 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001362 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001364 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 LEFT.br_blockcount + PREV.br_blockcount +
1366 RIGHT.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001367 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001369 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001370 xfs_iext_remove(ifp, idx, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 ip->i_df.if_lastex = idx - 1;
1372 ip->i_d.di_nextents -= 2;
1373 if (cur == NULL)
1374 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1375 else {
1376 rval = XFS_ILOG_CORE;
1377 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1378 RIGHT.br_startblock,
1379 RIGHT.br_blockcount, &i)))
1380 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001381 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if ((error = xfs_bmbt_delete(cur, &i)))
1383 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001384 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1386 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001387 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if ((error = xfs_bmbt_delete(cur, &i)))
1389 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001390 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1392 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001393 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1395 LEFT.br_startblock,
1396 LEFT.br_blockcount + PREV.br_blockcount +
1397 RIGHT.br_blockcount, LEFT.br_state)))
1398 goto done;
1399 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001400 /* DELTA: Three in-core extents are replaced by one. */
1401 temp = LEFT.br_startoff;
1402 temp2 = LEFT.br_blockcount +
1403 PREV.br_blockcount +
1404 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 break;
1406
1407 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
1408 /*
1409 * Setting all of a previous oldext extent to newext.
1410 * The left neighbor is contiguous, the right is not.
1411 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001412 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001414 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 LEFT.br_blockcount + PREV.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001416 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 XFS_DATA_FORK);
1418 ip->i_df.if_lastex = idx - 1;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001419 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001420 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 ip->i_d.di_nextents--;
1422 if (cur == NULL)
1423 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1424 else {
1425 rval = XFS_ILOG_CORE;
1426 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1427 PREV.br_startblock, PREV.br_blockcount,
1428 &i)))
1429 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001430 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if ((error = xfs_bmbt_delete(cur, &i)))
1432 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_decrement(cur, 0, &i)))
1435 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001436 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1438 LEFT.br_startblock,
1439 LEFT.br_blockcount + PREV.br_blockcount,
1440 LEFT.br_state)))
1441 goto done;
1442 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001443 /* DELTA: Two in-core extents are replaced by one. */
1444 temp = LEFT.br_startoff;
1445 temp2 = LEFT.br_blockcount +
1446 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 break;
1448
1449 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
1450 /*
1451 * Setting all of a previous oldext extent to newext.
1452 * The right neighbor is contiguous, the left is not.
1453 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001454 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 XFS_DATA_FORK);
1456 xfs_bmbt_set_blockcount(ep,
1457 PREV.br_blockcount + RIGHT.br_blockcount);
1458 xfs_bmbt_set_state(ep, newext);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001459 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 XFS_DATA_FORK);
1461 ip->i_df.if_lastex = idx;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001462 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001463 xfs_iext_remove(ifp, idx + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 ip->i_d.di_nextents--;
1465 if (cur == NULL)
1466 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1467 else {
1468 rval = XFS_ILOG_CORE;
1469 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1470 RIGHT.br_startblock,
1471 RIGHT.br_blockcount, &i)))
1472 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001473 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if ((error = xfs_bmbt_delete(cur, &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_decrement(cur, 0, &i)))
1478 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001479 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1481 new->br_startblock,
1482 new->br_blockcount + RIGHT.br_blockcount,
1483 newext)))
1484 goto done;
1485 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001486 /* DELTA: Two in-core extents are replaced by one. */
1487 temp = PREV.br_startoff;
1488 temp2 = PREV.br_blockcount +
1489 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 break;
1491
1492 case MASK2(LEFT_FILLING, RIGHT_FILLING):
1493 /*
1494 * Setting all of a previous oldext extent to newext.
1495 * Neither the left nor right neighbors are contiguous with
1496 * the new one.
1497 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001498 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 XFS_DATA_FORK);
1500 xfs_bmbt_set_state(ep, newext);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001501 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 XFS_DATA_FORK);
1503 ip->i_df.if_lastex = idx;
1504 if (cur == NULL)
1505 rval = XFS_ILOG_DEXT;
1506 else {
1507 rval = 0;
1508 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1509 new->br_startblock, new->br_blockcount,
1510 &i)))
1511 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001512 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1514 new->br_startblock, new->br_blockcount,
1515 newext)))
1516 goto done;
1517 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001518 /* DELTA: The in-core extent described by new changed type. */
1519 temp = new->br_startoff;
1520 temp2 = new->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 break;
1522
1523 case MASK2(LEFT_FILLING, LEFT_CONTIG):
1524 /*
1525 * Setting the first part of a previous oldext extent to newext.
1526 * The left neighbor is contiguous.
1527 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001528 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001530 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 LEFT.br_blockcount + new->br_blockcount);
1532 xfs_bmbt_set_startoff(ep,
1533 PREV.br_startoff + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001534 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001536 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 XFS_DATA_FORK);
1538 xfs_bmbt_set_startblock(ep,
1539 new->br_startblock + new->br_blockcount);
1540 xfs_bmbt_set_blockcount(ep,
1541 PREV.br_blockcount - new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001542 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 XFS_DATA_FORK);
1544 ip->i_df.if_lastex = idx - 1;
1545 if (cur == NULL)
1546 rval = XFS_ILOG_DEXT;
1547 else {
1548 rval = 0;
1549 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1550 PREV.br_startblock, PREV.br_blockcount,
1551 &i)))
1552 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001553 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if ((error = xfs_bmbt_update(cur,
1555 PREV.br_startoff + new->br_blockcount,
1556 PREV.br_startblock + new->br_blockcount,
1557 PREV.br_blockcount - new->br_blockcount,
1558 oldext)))
1559 goto done;
1560 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1561 goto done;
1562 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1563 LEFT.br_startblock,
1564 LEFT.br_blockcount + new->br_blockcount,
1565 LEFT.br_state))
1566 goto done;
1567 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001568 /* DELTA: The boundary between two in-core extents moved. */
1569 temp = LEFT.br_startoff;
1570 temp2 = LEFT.br_blockcount +
1571 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 break;
1573
1574 case MASK(LEFT_FILLING):
1575 /*
1576 * Setting the first part of a previous oldext extent to newext.
1577 * The left neighbor is not contiguous.
1578 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001579 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1581 xfs_bmbt_set_startoff(ep, new_endoff);
1582 xfs_bmbt_set_blockcount(ep,
1583 PREV.br_blockcount - new->br_blockcount);
1584 xfs_bmbt_set_startblock(ep,
1585 new->br_startblock + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001586 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1587 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001589 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 ip->i_df.if_lastex = idx;
1591 ip->i_d.di_nextents++;
1592 if (cur == NULL)
1593 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1594 else {
1595 rval = XFS_ILOG_CORE;
1596 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1597 PREV.br_startblock, PREV.br_blockcount,
1598 &i)))
1599 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001600 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if ((error = xfs_bmbt_update(cur,
1602 PREV.br_startoff + new->br_blockcount,
1603 PREV.br_startblock + new->br_blockcount,
1604 PREV.br_blockcount - new->br_blockcount,
1605 oldext)))
1606 goto done;
1607 cur->bc_rec.b = *new;
1608 if ((error = xfs_bmbt_insert(cur, &i)))
1609 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001610 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001612 /* DELTA: One in-core extent is split in two. */
1613 temp = PREV.br_startoff;
1614 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 break;
1616
1617 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1618 /*
1619 * Setting the last part of a previous oldext extent to newext.
1620 * The right neighbor is contiguous with the new allocation.
1621 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001622 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001624 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 XFS_DATA_FORK);
1626 xfs_bmbt_set_blockcount(ep,
1627 PREV.br_blockcount - new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001628 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001630 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1631 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 new->br_blockcount + RIGHT.br_blockcount, newext);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001633 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 XFS_DATA_FORK);
1635 ip->i_df.if_lastex = idx + 1;
1636 if (cur == NULL)
1637 rval = XFS_ILOG_DEXT;
1638 else {
1639 rval = 0;
1640 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1641 PREV.br_startblock,
1642 PREV.br_blockcount, &i)))
1643 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001644 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1646 PREV.br_startblock,
1647 PREV.br_blockcount - new->br_blockcount,
1648 oldext)))
1649 goto done;
1650 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1651 goto done;
1652 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1653 new->br_startblock,
1654 new->br_blockcount + RIGHT.br_blockcount,
1655 newext)))
1656 goto done;
1657 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001658 /* DELTA: The boundary between two in-core extents moved. */
1659 temp = PREV.br_startoff;
1660 temp2 = PREV.br_blockcount +
1661 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 break;
1663
1664 case MASK(RIGHT_FILLING):
1665 /*
1666 * Setting the last part of a previous oldext extent to newext.
1667 * The right neighbor is not contiguous.
1668 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001669 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 xfs_bmbt_set_blockcount(ep,
1671 PREV.br_blockcount - new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001672 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1673 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1674 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001675 xfs_iext_insert(ifp, idx + 1, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 ip->i_df.if_lastex = idx + 1;
1677 ip->i_d.di_nextents++;
1678 if (cur == NULL)
1679 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1680 else {
1681 rval = XFS_ILOG_CORE;
1682 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1683 PREV.br_startblock, PREV.br_blockcount,
1684 &i)))
1685 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001686 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1688 PREV.br_startblock,
1689 PREV.br_blockcount - new->br_blockcount,
1690 oldext)))
1691 goto done;
1692 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1693 new->br_startblock, new->br_blockcount,
1694 &i)))
1695 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001696 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1698 if ((error = xfs_bmbt_insert(cur, &i)))
1699 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001700 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001702 /* DELTA: One in-core extent is split in two. */
1703 temp = PREV.br_startoff;
1704 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 break;
1706
1707 case 0:
1708 /*
1709 * Setting the middle part of a previous oldext extent to
1710 * newext. Contiguity is impossible here.
1711 * One extent becomes three extents.
1712 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001713 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 xfs_bmbt_set_blockcount(ep,
1715 new->br_startoff - PREV.br_startoff);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001716 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 r[0] = *new;
1718 r[1].br_startoff = new_endoff;
1719 r[1].br_blockcount =
1720 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1721 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1722 r[1].br_state = oldext;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001723 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001725 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 ip->i_df.if_lastex = idx + 1;
1727 ip->i_d.di_nextents += 2;
1728 if (cur == NULL)
1729 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1730 else {
1731 rval = XFS_ILOG_CORE;
1732 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1733 PREV.br_startblock, PREV.br_blockcount,
1734 &i)))
1735 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001736 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 /* new right extent - oldext */
1738 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1739 r[1].br_startblock, r[1].br_blockcount,
1740 r[1].br_state)))
1741 goto done;
1742 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10001744 cur->bc_rec.b.br_blockcount =
1745 new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if ((error = xfs_bmbt_insert(cur, &i)))
1747 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001748 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001749 /*
1750 * Reset the cursor to the position of the new extent
1751 * we are about to insert as we can't trust it after
1752 * the previous insert.
1753 */
1754 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1755 new->br_startblock, new->br_blockcount,
1756 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001758 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001760 cur->bc_rec.b.br_state = new->br_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if ((error = xfs_bmbt_insert(cur, &i)))
1762 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001763 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001765 /* DELTA: One in-core extent is split in three. */
1766 temp = PREV.br_startoff;
1767 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 break;
1769
1770 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1771 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1772 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1773 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1774 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1775 case MASK(LEFT_CONTIG):
1776 case MASK(RIGHT_CONTIG):
1777 /*
1778 * These cases are all impossible.
1779 */
1780 ASSERT(0);
1781 }
1782 *curp = cur;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001783 if (delta) {
1784 temp2 += temp;
1785 if (delta->xed_startoff > temp)
1786 delta->xed_startoff = temp;
1787 if (delta->xed_blockcount < temp2)
1788 delta->xed_blockcount = temp2;
1789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790done:
1791 *logflagsp = rval;
1792 return error;
1793#undef LEFT
1794#undef RIGHT
1795#undef PREV
1796#undef MASK
1797#undef MASK2
1798#undef MASK3
1799#undef MASK4
1800#undef STATE_SET
1801#undef STATE_TEST
1802#undef STATE_SET_TEST
1803#undef SWITCH_STATE
1804}
1805
1806/*
1807 * Called by xfs_bmap_add_extent to handle cases converting a hole
1808 * to a delayed allocation.
1809 */
1810/*ARGSUSED*/
1811STATIC int /* error */
1812xfs_bmap_add_extent_hole_delay(
1813 xfs_inode_t *ip, /* incore inode pointer */
1814 xfs_extnum_t idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001815 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001817 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 int rsvd) /* OK to allocate reserved blocks */
1819{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001820 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001821 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1823 xfs_filblks_t newlen=0; /* new indirect size */
1824 xfs_filblks_t oldlen=0; /* old indirect size */
1825 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1826 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001827 xfs_filblks_t temp=0; /* temp for indirect calculations */
1828 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 enum { /* bit number definitions for state */
1830 LEFT_CONTIG, RIGHT_CONTIG,
1831 LEFT_DELAY, RIGHT_DELAY,
1832 LEFT_VALID, RIGHT_VALID
1833 };
1834
1835#define MASK(b) (1 << (b))
1836#define MASK2(a,b) (MASK(a) | MASK(b))
1837#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1838#define STATE_TEST(b) (state & MASK(b))
1839#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1840 ((state &= ~MASK(b)), 0))
1841#define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1842
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001843 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1844 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 state = 0;
1846 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1847 /*
1848 * Check and set flags if this segment has a left neighbor
1849 */
1850 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001851 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1853 }
1854 /*
1855 * Check and set flags if the current (right) segment exists.
1856 * If it doesn't exist, we're converting the hole at end-of-file.
1857 */
1858 if (STATE_SET_TEST(RIGHT_VALID,
1859 idx <
1860 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1861 xfs_bmbt_get_all(ep, &right);
1862 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1863 }
1864 /*
1865 * Set contiguity flags on the left and right neighbors.
1866 * Don't let extents get too large, even if the pieces are contiguous.
1867 */
1868 STATE_SET(LEFT_CONTIG,
1869 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1870 left.br_startoff + left.br_blockcount == new->br_startoff &&
1871 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1872 STATE_SET(RIGHT_CONTIG,
1873 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1874 new->br_startoff + new->br_blockcount == right.br_startoff &&
1875 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1876 (!STATE_TEST(LEFT_CONTIG) ||
1877 (left.br_blockcount + new->br_blockcount +
1878 right.br_blockcount <= MAXEXTLEN)));
1879 /*
1880 * Switch out based on the contiguity flags.
1881 */
1882 switch (SWITCH_STATE) {
1883
1884 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1885 /*
1886 * New allocation is contiguous with delayed allocations
1887 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001888 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 */
1890 temp = left.br_blockcount + new->br_blockcount +
1891 right.br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001892 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001894 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 oldlen = STARTBLOCKVAL(left.br_startblock) +
1896 STARTBLOCKVAL(new->br_startblock) +
1897 STARTBLOCKVAL(right.br_startblock);
1898 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001899 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1900 NULLSTARTBLOCK((int)newlen));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001901 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001903 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001904 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 ip->i_df.if_lastex = idx - 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001906 /* DELTA: Two in-core extents were replaced by one. */
1907 temp2 = temp;
1908 temp = left.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 break;
1910
1911 case MASK(LEFT_CONTIG):
1912 /*
1913 * New allocation is contiguous with a delayed allocation
1914 * on the left.
1915 * Merge the new allocation with the left neighbor.
1916 */
1917 temp = left.br_blockcount + new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001918 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001920 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 oldlen = STARTBLOCKVAL(left.br_startblock) +
1922 STARTBLOCKVAL(new->br_startblock);
1923 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001924 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1925 NULLSTARTBLOCK((int)newlen));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001926 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 XFS_DATA_FORK);
1928 ip->i_df.if_lastex = idx - 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001929 /* DELTA: One in-core extent grew into a hole. */
1930 temp2 = temp;
1931 temp = left.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 break;
1933
1934 case MASK(RIGHT_CONTIG):
1935 /*
1936 * New allocation is contiguous with a delayed allocation
1937 * on the right.
1938 * Merge the new allocation with the right neighbor.
1939 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001940 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 temp = new->br_blockcount + right.br_blockcount;
1942 oldlen = STARTBLOCKVAL(new->br_startblock) +
1943 STARTBLOCKVAL(right.br_startblock);
1944 newlen = xfs_bmap_worst_indlen(ip, temp);
1945 xfs_bmbt_set_allf(ep, new->br_startoff,
1946 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001947 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 ip->i_df.if_lastex = idx;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001949 /* DELTA: One in-core extent grew into a hole. */
1950 temp2 = temp;
1951 temp = new->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 break;
1953
1954 case 0:
1955 /*
1956 * New allocation is not contiguous with another
1957 * delayed allocation.
1958 * Insert a new entry.
1959 */
1960 oldlen = newlen = 0;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001961 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001963 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 ip->i_df.if_lastex = idx;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001965 /* DELTA: A new in-core extent was added in a hole. */
1966 temp2 = new->br_blockcount;
1967 temp = new->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 break;
1969 }
1970 if (oldlen != newlen) {
1971 ASSERT(oldlen > newlen);
1972 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11001973 (int64_t)(oldlen - newlen), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /*
1975 * Nothing to do for disk quota accounting here.
1976 */
1977 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001978 if (delta) {
1979 temp2 += temp;
1980 if (delta->xed_startoff > temp)
1981 delta->xed_startoff = temp;
1982 if (delta->xed_blockcount < temp2)
1983 delta->xed_blockcount = temp2;
1984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 *logflagsp = 0;
1986 return 0;
1987#undef MASK
1988#undef MASK2
1989#undef STATE_SET
1990#undef STATE_TEST
1991#undef STATE_SET_TEST
1992#undef SWITCH_STATE
1993}
1994
1995/*
1996 * Called by xfs_bmap_add_extent to handle cases converting a hole
1997 * to a real allocation.
1998 */
1999STATIC int /* error */
2000xfs_bmap_add_extent_hole_real(
2001 xfs_inode_t *ip, /* incore inode pointer */
2002 xfs_extnum_t idx, /* extent number to update/insert */
2003 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002004 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002006 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 int whichfork) /* data or attr fork */
2008{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002009 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 int i; /* temp state */
2012 xfs_ifork_t *ifp; /* inode fork pointer */
2013 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2014 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002015 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002017 xfs_filblks_t temp=0;
2018 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 enum { /* bit number definitions for state */
2020 LEFT_CONTIG, RIGHT_CONTIG,
2021 LEFT_DELAY, RIGHT_DELAY,
2022 LEFT_VALID, RIGHT_VALID
2023 };
2024
2025#define MASK(b) (1 << (b))
2026#define MASK2(a,b) (MASK(a) | MASK(b))
2027#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
2028#define STATE_TEST(b) (state & MASK(b))
2029#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
2030 ((state &= ~MASK(b)), 0))
2031#define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
2032
2033 ifp = XFS_IFORK_PTR(ip, whichfork);
2034 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002035 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 state = 0;
2037 /*
2038 * Check and set flags if this segment has a left neighbor.
2039 */
2040 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002041 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
2043 }
2044 /*
2045 * Check and set flags if this segment has a current value.
2046 * Not true if we're inserting into the "hole" at eof.
2047 */
2048 if (STATE_SET_TEST(RIGHT_VALID,
2049 idx <
2050 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
2051 xfs_bmbt_get_all(ep, &right);
2052 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
2053 }
2054 /*
2055 * We're inserting a real allocation between "left" and "right".
2056 * Set the contiguity flags. Don't let extents get too large.
2057 */
2058 STATE_SET(LEFT_CONTIG,
2059 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
2060 left.br_startoff + left.br_blockcount == new->br_startoff &&
2061 left.br_startblock + left.br_blockcount == new->br_startblock &&
2062 left.br_state == new->br_state &&
2063 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
2064 STATE_SET(RIGHT_CONTIG,
2065 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
2066 new->br_startoff + new->br_blockcount == right.br_startoff &&
2067 new->br_startblock + new->br_blockcount ==
2068 right.br_startblock &&
2069 new->br_state == right.br_state &&
2070 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2071 (!STATE_TEST(LEFT_CONTIG) ||
2072 left.br_blockcount + new->br_blockcount +
2073 right.br_blockcount <= MAXEXTLEN));
2074
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002075 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 /*
2077 * Select which case we're in here, and implement it.
2078 */
2079 switch (SWITCH_STATE) {
2080
2081 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
2082 /*
2083 * New allocation is contiguous with real allocations on the
2084 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002085 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002087 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002089 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 left.br_blockcount + new->br_blockcount +
2091 right.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002092 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 whichfork);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002094 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002095 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 ifp->if_lastex = idx - 1;
2097 XFS_IFORK_NEXT_SET(ip, whichfork,
2098 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2099 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002100 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2101 } else {
2102 rval = XFS_ILOG_CORE;
2103 if ((error = xfs_bmbt_lookup_eq(cur,
2104 right.br_startoff,
2105 right.br_startblock,
2106 right.br_blockcount, &i)))
2107 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002108 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002109 if ((error = xfs_bmbt_delete(cur, &i)))
2110 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002111 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002112 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2113 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002114 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002115 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2116 left.br_startblock,
2117 left.br_blockcount +
2118 new->br_blockcount +
2119 right.br_blockcount,
2120 left.br_state)))
2121 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002123 /* DELTA: Two in-core extents were replaced by one. */
2124 temp = left.br_startoff;
2125 temp2 = left.br_blockcount +
2126 new->br_blockcount +
2127 right.br_blockcount;
2128 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 case MASK(LEFT_CONTIG):
2131 /*
2132 * New allocation is contiguous with a real allocation
2133 * on the left.
2134 * Merge the new allocation with the left neighbor.
2135 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002136 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002137 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 left.br_blockcount + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002139 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 ifp->if_lastex = idx - 1;
2141 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002142 rval = XFS_ILOG_FEXT(whichfork);
2143 } else {
2144 rval = 0;
2145 if ((error = xfs_bmbt_lookup_eq(cur,
2146 left.br_startoff,
2147 left.br_startblock,
2148 left.br_blockcount, &i)))
2149 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002150 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002151 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2152 left.br_startblock,
2153 left.br_blockcount +
2154 new->br_blockcount,
2155 left.br_state)))
2156 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002158 /* DELTA: One in-core extent grew. */
2159 temp = left.br_startoff;
2160 temp2 = left.br_blockcount +
2161 new->br_blockcount;
2162 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 case MASK(RIGHT_CONTIG):
2165 /*
2166 * New allocation is contiguous with a real allocation
2167 * on the right.
2168 * Merge the new allocation with the right neighbor.
2169 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002170 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2172 new->br_blockcount + right.br_blockcount,
2173 right.br_state);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002174 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 ifp->if_lastex = idx;
2176 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002177 rval = XFS_ILOG_FEXT(whichfork);
2178 } else {
2179 rval = 0;
2180 if ((error = xfs_bmbt_lookup_eq(cur,
2181 right.br_startoff,
2182 right.br_startblock,
2183 right.br_blockcount, &i)))
2184 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002185 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002186 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2187 new->br_startblock,
2188 new->br_blockcount +
2189 right.br_blockcount,
2190 right.br_state)))
2191 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002193 /* DELTA: One in-core extent grew. */
2194 temp = new->br_startoff;
2195 temp2 = new->br_blockcount +
2196 right.br_blockcount;
2197 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 case 0:
2200 /*
2201 * New allocation is not contiguous with another
2202 * real allocation.
2203 * Insert a new entry.
2204 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002205 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002206 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 ifp->if_lastex = idx;
2208 XFS_IFORK_NEXT_SET(ip, whichfork,
2209 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2210 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002211 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2212 } else {
2213 rval = XFS_ILOG_CORE;
2214 if ((error = xfs_bmbt_lookup_eq(cur,
2215 new->br_startoff,
2216 new->br_startblock,
2217 new->br_blockcount, &i)))
2218 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002219 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002220 cur->bc_rec.b.br_state = new->br_state;
2221 if ((error = xfs_bmbt_insert(cur, &i)))
2222 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002223 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002225 /* DELTA: A new extent was added in a hole. */
2226 temp = new->br_startoff;
2227 temp2 = new->br_blockcount;
2228 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002230 if (delta) {
2231 temp2 += temp;
2232 if (delta->xed_startoff > temp)
2233 delta->xed_startoff = temp;
2234 if (delta->xed_blockcount < temp2)
2235 delta->xed_blockcount = temp2;
2236 }
2237done:
2238 *logflagsp = rval;
2239 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240#undef MASK
2241#undef MASK2
2242#undef STATE_SET
2243#undef STATE_TEST
2244#undef STATE_SET_TEST
2245#undef SWITCH_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
Nathan Scottdd9f4382006-01-11 15:28:28 +11002248/*
2249 * Adjust the size of the new extent based on di_extsize and rt extsize.
2250 */
2251STATIC int
2252xfs_bmap_extsize_align(
2253 xfs_mount_t *mp,
2254 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2255 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2256 xfs_extlen_t extsz, /* align to this extent size */
2257 int rt, /* is this a realtime inode? */
2258 int eof, /* is extent at end-of-file? */
2259 int delay, /* creating delalloc extent? */
2260 int convert, /* overwriting unwritten extent? */
2261 xfs_fileoff_t *offp, /* in/out: aligned offset */
2262 xfs_extlen_t *lenp) /* in/out: aligned length */
2263{
2264 xfs_fileoff_t orig_off; /* original offset */
2265 xfs_extlen_t orig_alen; /* original length */
2266 xfs_fileoff_t orig_end; /* original off+len */
2267 xfs_fileoff_t nexto; /* next file offset */
2268 xfs_fileoff_t prevo; /* previous file offset */
2269 xfs_fileoff_t align_off; /* temp for offset */
2270 xfs_extlen_t align_alen; /* temp for length */
2271 xfs_extlen_t temp; /* temp for calculations */
2272
2273 if (convert)
2274 return 0;
2275
2276 orig_off = align_off = *offp;
2277 orig_alen = align_alen = *lenp;
2278 orig_end = orig_off + orig_alen;
2279
2280 /*
2281 * If this request overlaps an existing extent, then don't
2282 * attempt to perform any additional alignment.
2283 */
2284 if (!delay && !eof &&
2285 (orig_off >= gotp->br_startoff) &&
2286 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2287 return 0;
2288 }
2289
2290 /*
2291 * If the file offset is unaligned vs. the extent size
2292 * we need to align it. This will be possible unless
2293 * the file was previously written with a kernel that didn't
2294 * perform this alignment, or if a truncate shot us in the
2295 * foot.
2296 */
2297 temp = do_mod(orig_off, extsz);
2298 if (temp) {
2299 align_alen += temp;
2300 align_off -= temp;
2301 }
2302 /*
2303 * Same adjustment for the end of the requested area.
2304 */
2305 if ((temp = (align_alen % extsz))) {
2306 align_alen += extsz - temp;
2307 }
2308 /*
2309 * If the previous block overlaps with this proposed allocation
2310 * then move the start forward without adjusting the length.
2311 */
2312 if (prevp->br_startoff != NULLFILEOFF) {
2313 if (prevp->br_startblock == HOLESTARTBLOCK)
2314 prevo = prevp->br_startoff;
2315 else
2316 prevo = prevp->br_startoff + prevp->br_blockcount;
2317 } else
2318 prevo = 0;
2319 if (align_off != orig_off && align_off < prevo)
2320 align_off = prevo;
2321 /*
2322 * If the next block overlaps with this proposed allocation
2323 * then move the start back without adjusting the length,
2324 * but not before offset 0.
2325 * This may of course make the start overlap previous block,
2326 * and if we hit the offset 0 limit then the next block
2327 * can still overlap too.
2328 */
2329 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2330 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2331 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2332 nexto = gotp->br_startoff + gotp->br_blockcount;
2333 else
2334 nexto = gotp->br_startoff;
2335 } else
2336 nexto = NULLFILEOFF;
2337 if (!eof &&
2338 align_off + align_alen != orig_end &&
2339 align_off + align_alen > nexto)
2340 align_off = nexto > align_alen ? nexto - align_alen : 0;
2341 /*
2342 * If we're now overlapping the next or previous extent that
2343 * means we can't fit an extsz piece in this hole. Just move
2344 * the start forward to the first valid spot and set
2345 * the length so we hit the end.
2346 */
2347 if (align_off != orig_off && align_off < prevo)
2348 align_off = prevo;
2349 if (align_off + align_alen != orig_end &&
2350 align_off + align_alen > nexto &&
2351 nexto != NULLFILEOFF) {
2352 ASSERT(nexto > prevo);
2353 align_alen = nexto - align_off;
2354 }
2355
2356 /*
2357 * If realtime, and the result isn't a multiple of the realtime
2358 * extent size we need to remove blocks until it is.
2359 */
2360 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2361 /*
2362 * We're not covering the original request, or
2363 * we won't be able to once we fix the length.
2364 */
2365 if (orig_off < align_off ||
2366 orig_end > align_off + align_alen ||
2367 align_alen - temp < orig_alen)
2368 return XFS_ERROR(EINVAL);
2369 /*
2370 * Try to fix it by moving the start up.
2371 */
2372 if (align_off + temp <= orig_off) {
2373 align_alen -= temp;
2374 align_off += temp;
2375 }
2376 /*
2377 * Try to fix it by moving the end in.
2378 */
2379 else if (align_off + align_alen - temp >= orig_end)
2380 align_alen -= temp;
2381 /*
2382 * Set the start to the minimum then trim the length.
2383 */
2384 else {
2385 align_alen -= orig_off - align_off;
2386 align_off = orig_off;
2387 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2388 }
2389 /*
2390 * Result doesn't cover the request, fail it.
2391 */
2392 if (orig_off < align_off || orig_end > align_off + align_alen)
2393 return XFS_ERROR(EINVAL);
2394 } else {
2395 ASSERT(orig_off >= align_off);
2396 ASSERT(orig_end <= align_off + align_alen);
2397 }
2398
2399#ifdef DEBUG
2400 if (!eof && gotp->br_startoff != NULLFILEOFF)
2401 ASSERT(align_off + align_alen <= gotp->br_startoff);
2402 if (prevp->br_startoff != NULLFILEOFF)
2403 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2404#endif
2405
2406 *lenp = align_alen;
2407 *offp = align_off;
2408 return 0;
2409}
2410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411#define XFS_ALLOC_GAP_UNITS 4
2412
David Chinnerc2b1cba2008-04-10 12:21:40 +10002413STATIC void
Nathan Scotta365bdd2006-03-14 13:34:16 +11002414xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2416{
2417 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2419 xfs_mount_t *mp; /* mount point structure */
2420 int nullfb; /* true if ap->firstblock isn't set */
2421 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423#define ISVALID(x,y) \
2424 (rt ? \
2425 (x) < mp->m_sb.sb_rblocks : \
2426 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2427 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2428 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2429
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 mp = ap->ip->i_mount;
2431 nullfb = ap->firstblock == NULLFSBLOCK;
2432 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2433 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 /*
2435 * If allocating at eof, and there's a previous real block,
2436 * try to use it's last block as our starting point.
2437 */
2438 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2439 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2440 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2441 ap->prevp->br_startblock)) {
2442 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2443 /*
2444 * Adjust for the gap between prevp and us.
2445 */
2446 adjust = ap->off -
2447 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2448 if (adjust &&
2449 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2450 ap->rval += adjust;
2451 }
2452 /*
2453 * If not at eof, then compare the two neighbor blocks.
2454 * Figure out whether either one gives us a good starting point,
2455 * and pick the better one.
2456 */
2457 else if (!ap->eof) {
2458 xfs_fsblock_t gotbno; /* right side block number */
2459 xfs_fsblock_t gotdiff=0; /* right side difference */
2460 xfs_fsblock_t prevbno; /* left side block number */
2461 xfs_fsblock_t prevdiff=0; /* left side difference */
2462
2463 /*
2464 * If there's a previous (left) block, select a requested
2465 * start block based on it.
2466 */
2467 if (ap->prevp->br_startoff != NULLFILEOFF &&
2468 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2469 (prevbno = ap->prevp->br_startblock +
2470 ap->prevp->br_blockcount) &&
2471 ISVALID(prevbno, ap->prevp->br_startblock)) {
2472 /*
2473 * Calculate gap to end of previous block.
2474 */
2475 adjust = prevdiff = ap->off -
2476 (ap->prevp->br_startoff +
2477 ap->prevp->br_blockcount);
2478 /*
2479 * Figure the startblock based on the previous block's
2480 * end and the gap size.
2481 * Heuristic!
2482 * If the gap is large relative to the piece we're
2483 * allocating, or using it gives us an invalid block
2484 * number, then just use the end of the previous block.
2485 */
2486 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2487 ISVALID(prevbno + prevdiff,
2488 ap->prevp->br_startblock))
2489 prevbno += adjust;
2490 else
2491 prevdiff += adjust;
2492 /*
2493 * If the firstblock forbids it, can't use it,
2494 * must use default.
2495 */
2496 if (!rt && !nullfb &&
2497 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2498 prevbno = NULLFSBLOCK;
2499 }
2500 /*
2501 * No previous block or can't follow it, just default.
2502 */
2503 else
2504 prevbno = NULLFSBLOCK;
2505 /*
2506 * If there's a following (right) block, select a requested
2507 * start block based on it.
2508 */
2509 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
2510 /*
2511 * Calculate gap to start of next block.
2512 */
2513 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2514 /*
2515 * Figure the startblock based on the next block's
2516 * start and the gap size.
2517 */
2518 gotbno = ap->gotp->br_startblock;
2519 /*
2520 * Heuristic!
2521 * If the gap is large relative to the piece we're
2522 * allocating, or using it gives us an invalid block
2523 * number, then just use the start of the next block
2524 * offset by our length.
2525 */
2526 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2527 ISVALID(gotbno - gotdiff, gotbno))
2528 gotbno -= adjust;
2529 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2530 gotbno -= ap->alen;
2531 gotdiff += adjust - ap->alen;
2532 } else
2533 gotdiff += adjust;
2534 /*
2535 * If the firstblock forbids it, can't use it,
2536 * must use default.
2537 */
2538 if (!rt && !nullfb &&
2539 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2540 gotbno = NULLFSBLOCK;
2541 }
2542 /*
2543 * No next block, just default.
2544 */
2545 else
2546 gotbno = NULLFSBLOCK;
2547 /*
2548 * If both valid, pick the better one, else the only good
2549 * one, else ap->rval is already set (to 0 or the inode block).
2550 */
2551 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2552 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2553 else if (prevbno != NULLFSBLOCK)
2554 ap->rval = prevbno;
2555 else if (gotbno != NULLFSBLOCK)
2556 ap->rval = gotbno;
2557 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002558#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11002559}
2560
2561STATIC int
2562xfs_bmap_rtalloc(
2563 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2564{
2565 xfs_alloctype_t atype = 0; /* type for allocation routines */
2566 int error; /* error return value */
2567 xfs_mount_t *mp; /* mount point structure */
2568 xfs_extlen_t prod = 0; /* product factor for allocators */
2569 xfs_extlen_t ralen = 0; /* realtime allocation length */
2570 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002571 xfs_rtblock_t rtb;
2572
2573 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002574 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002575 prod = align / mp->m_sb.sb_rextsize;
2576 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2577 align, 1, ap->eof, 0,
2578 ap->conv, &ap->off, &ap->alen);
2579 if (error)
2580 return error;
2581 ASSERT(ap->alen);
2582 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2583
2584 /*
2585 * If the offset & length are not perfectly aligned
2586 * then kill prod, it will just get us in trouble.
2587 */
2588 if (do_mod(ap->off, align) || ap->alen % align)
2589 prod = 1;
2590 /*
2591 * Set ralen to be the actual requested length in rtextents.
2592 */
2593 ralen = ap->alen / mp->m_sb.sb_rextsize;
2594 /*
2595 * If the old value was close enough to MAXEXTLEN that
2596 * we rounded up to it, cut it back so it's valid again.
2597 * Note that if it's a really large request (bigger than
2598 * MAXEXTLEN), we don't hear about that number, and can't
2599 * adjust the starting point to match it.
2600 */
2601 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2602 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2603 /*
2604 * If it's an allocation to an empty file at offset 0,
2605 * pick an extent that will space things out in the rt area.
2606 */
2607 if (ap->eof && ap->off == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10002608 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2609
Nathan Scotta365bdd2006-03-14 13:34:16 +11002610 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2611 if (error)
2612 return error;
2613 ap->rval = rtx * mp->m_sb.sb_rextsize;
2614 } else {
2615 ap->rval = 0;
2616 }
2617
2618 xfs_bmap_adjacent(ap);
2619
2620 /*
2621 * Realtime allocation, done through xfs_rtallocate_extent.
2622 */
2623 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2624 do_div(ap->rval, mp->m_sb.sb_rextsize);
2625 rtb = ap->rval;
2626 ap->alen = ralen;
2627 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2628 &ralen, atype, ap->wasdel, prod, &rtb)))
2629 return error;
2630 if (rtb == NULLFSBLOCK && prod > 1 &&
2631 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2632 ap->alen, &ralen, atype,
2633 ap->wasdel, 1, &rtb)))
2634 return error;
2635 ap->rval = rtb;
2636 if (ap->rval != NULLFSBLOCK) {
2637 ap->rval *= mp->m_sb.sb_rextsize;
2638 ralen *= mp->m_sb.sb_rextsize;
2639 ap->alen = ralen;
2640 ap->ip->i_d.di_nblocks += ralen;
2641 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2642 if (ap->wasdel)
2643 ap->ip->i_delayed_blks -= ralen;
2644 /*
2645 * Adjust the disk quota also. This was reserved
2646 * earlier.
2647 */
2648 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2649 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2650 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2651 } else {
2652 ap->alen = 0;
2653 }
2654 return 0;
2655}
2656
2657STATIC int
2658xfs_bmap_btalloc(
2659 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2660{
2661 xfs_mount_t *mp; /* mount point structure */
2662 xfs_alloctype_t atype = 0; /* type for allocation routines */
2663 xfs_extlen_t align; /* minimum allocation alignment */
2664 xfs_agnumber_t ag;
2665 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2666 xfs_agnumber_t startag;
2667 xfs_alloc_arg_t args;
2668 xfs_extlen_t blen;
2669 xfs_extlen_t delta;
2670 xfs_extlen_t longest;
2671 xfs_extlen_t need;
2672 xfs_extlen_t nextminlen = 0;
2673 xfs_perag_t *pag;
2674 int nullfb; /* true if ap->firstblock isn't set */
2675 int isaligned;
2676 int notinit;
2677 int tryagain;
2678 int error;
2679
2680 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002681 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002682 if (unlikely(align)) {
2683 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2684 align, 0, ap->eof, 0, ap->conv,
2685 &ap->off, &ap->alen);
2686 ASSERT(!error);
2687 ASSERT(ap->alen);
2688 }
2689 nullfb = ap->firstblock == NULLFSBLOCK;
2690 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10002691 if (nullfb) {
2692 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2693 ag = xfs_filestream_lookup_ag(ap->ip);
2694 ag = (ag != NULLAGNUMBER) ? ag : 0;
2695 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2696 } else {
2697 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2698 }
2699 } else
Nathan Scotta365bdd2006-03-14 13:34:16 +11002700 ap->rval = ap->firstblock;
2701
2702 xfs_bmap_adjacent(ap);
2703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 /*
2705 * If allowed, use ap->rval; otherwise must use firstblock since
2706 * it's in the right allocation group.
2707 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002708 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 ;
2710 else
2711 ap->rval = ap->firstblock;
2712 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 * Normal allocation, done through xfs_alloc_vextent.
2714 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002715 tryagain = isaligned = 0;
2716 args.tp = ap->tp;
2717 args.mp = mp;
2718 args.fsbno = ap->rval;
2719 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
Yingping Lud210a282006-06-09 14:55:18 +10002720 args.firstblock = ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002721 blen = 0;
2722 if (nullfb) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002723 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2724 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2725 else
2726 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002727 args.total = ap->total;
David Chinner2a82b8b2007-07-11 11:09:12 +10002728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10002730 * Search for an allocation group with a single extent
2731 * large enough for the request.
2732 *
2733 * If one isn't found, then adjust the minimum allocation
2734 * size to the largest space found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002736 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
David Chinner2a82b8b2007-07-11 11:09:12 +10002737 if (startag == NULLAGNUMBER)
2738 startag = ag = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002739 notinit = 0;
2740 down_read(&mp->m_peraglock);
2741 while (blen < ap->alen) {
2742 pag = &mp->m_perag[ag];
2743 if (!pag->pagf_init &&
2744 (error = xfs_alloc_pagf_init(mp, args.tp,
2745 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2746 up_read(&mp->m_peraglock);
2747 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002749 /*
2750 * See xfs_alloc_fix_freelist...
2751 */
2752 if (pag->pagf_init) {
2753 need = XFS_MIN_FREELIST_PAG(pag, mp);
2754 delta = need > pag->pagf_flcount ?
2755 need - pag->pagf_flcount : 0;
2756 longest = (pag->pagf_longest > delta) ?
2757 (pag->pagf_longest - delta) :
2758 (pag->pagf_flcount > 0 ||
2759 pag->pagf_longest > 0);
2760 if (blen < longest)
2761 blen = longest;
2762 } else
2763 notinit = 1;
David Chinner2a82b8b2007-07-11 11:09:12 +10002764
2765 if (xfs_inode_is_filestream(ap->ip)) {
2766 if (blen >= ap->alen)
2767 break;
2768
2769 if (ap->userdata) {
2770 /*
2771 * If startag is an invalid AG, we've
2772 * come here once before and
2773 * xfs_filestream_new_ag picked the
2774 * best currently available.
2775 *
2776 * Don't continue looping, since we
2777 * could loop forever.
2778 */
2779 if (startag == NULLAGNUMBER)
2780 break;
2781
2782 error = xfs_filestream_new_ag(ap, &ag);
2783 if (error) {
2784 up_read(&mp->m_peraglock);
2785 return error;
2786 }
2787
2788 /* loop again to set 'blen'*/
2789 startag = NULLAGNUMBER;
2790 continue;
2791 }
2792 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002793 if (++ag == mp->m_sb.sb_agcount)
2794 ag = 0;
2795 if (ag == startag)
2796 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002798 up_read(&mp->m_peraglock);
2799 /*
2800 * Since the above loop did a BUF_TRYLOCK, it is
2801 * possible that there is space for this request.
2802 */
2803 if (notinit || blen < ap->minlen)
2804 args.minlen = ap->minlen;
2805 /*
2806 * If the best seen length is less than the request
2807 * length, use the best as the minimum.
2808 */
2809 else if (blen < ap->alen)
2810 args.minlen = blen;
2811 /*
2812 * Otherwise we've seen an extent as big as alen,
2813 * use that as the minimum.
2814 */
2815 else
2816 args.minlen = ap->alen;
David Chinner2a82b8b2007-07-11 11:09:12 +10002817
2818 /*
2819 * set the failure fallback case to look in the selected
2820 * AG as the stream may have moved.
2821 */
2822 if (xfs_inode_is_filestream(ap->ip))
2823 ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002824 } else if (ap->low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002825 if (xfs_inode_is_filestream(ap->ip))
2826 args.type = XFS_ALLOCTYPE_FIRST_AG;
2827 else
2828 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002829 args.total = args.minlen = ap->minlen;
2830 } else {
2831 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2832 args.total = ap->total;
2833 args.minlen = ap->minlen;
2834 }
David Chinner957d0eb2007-06-18 16:50:37 +10002835 /* apply extent size hints if obtained earlier */
2836 if (unlikely(align)) {
2837 args.prod = align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002838 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2839 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11002840 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11002841 args.prod = 1;
2842 args.mod = 0;
2843 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002844 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002845 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2846 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2847 }
2848 /*
2849 * If we are not low on available data blocks, and the
2850 * underlying logical volume manager is a stripe, and
2851 * the file offset is zero then try to allocate data
2852 * blocks on stripe unit boundary.
2853 * NOTE: ap->aeof is only set if the allocation length
2854 * is >= the stripe unit and the allocation offset is
2855 * at the end of file.
2856 */
2857 if (!ap->low && ap->aeof) {
2858 if (!ap->off) {
2859 args.alignment = mp->m_dalign;
2860 atype = args.type;
2861 isaligned = 1;
2862 /*
2863 * Adjust for alignment
2864 */
2865 if (blen > args.alignment && blen <= ap->alen)
2866 args.minlen = blen - args.alignment;
2867 args.minalignslop = 0;
2868 } else {
2869 /*
2870 * First try an exact bno allocation.
2871 * If it fails then do a near or start bno
2872 * allocation with alignment turned on.
2873 */
2874 atype = args.type;
2875 tryagain = 1;
2876 args.type = XFS_ALLOCTYPE_THIS_BNO;
2877 args.alignment = 1;
2878 /*
2879 * Compute the minlen+alignment for the
2880 * next case. Set slop so that the value
2881 * of minlen+alignment+slop doesn't go up
2882 * between the calls.
2883 */
2884 if (blen > mp->m_dalign && blen <= ap->alen)
2885 nextminlen = blen - mp->m_dalign;
2886 else
2887 nextminlen = args.minlen;
2888 if (nextminlen + mp->m_dalign > args.minlen + 1)
2889 args.minalignslop =
2890 nextminlen + mp->m_dalign -
2891 args.minlen - 1;
2892 else
2893 args.minalignslop = 0;
2894 }
2895 } else {
2896 args.alignment = 1;
2897 args.minalignslop = 0;
2898 }
2899 args.minleft = ap->minleft;
2900 args.wasdel = ap->wasdel;
2901 args.isfl = 0;
2902 args.userdata = ap->userdata;
2903 if ((error = xfs_alloc_vextent(&args)))
2904 return error;
2905 if (tryagain && args.fsbno == NULLFSBLOCK) {
2906 /*
2907 * Exact allocation failed. Now try with alignment
2908 * turned on.
2909 */
2910 args.type = atype;
2911 args.fsbno = ap->rval;
2912 args.alignment = mp->m_dalign;
2913 args.minlen = nextminlen;
2914 args.minalignslop = 0;
2915 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if ((error = xfs_alloc_vextent(&args)))
2917 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002918 }
2919 if (isaligned && args.fsbno == NULLFSBLOCK) {
2920 /*
2921 * allocation failed, so turn off alignment and
2922 * try again.
2923 */
2924 args.type = atype;
2925 args.fsbno = ap->rval;
2926 args.alignment = 0;
2927 if ((error = xfs_alloc_vextent(&args)))
2928 return error;
2929 }
2930 if (args.fsbno == NULLFSBLOCK && nullfb &&
2931 args.minlen > ap->minlen) {
2932 args.minlen = ap->minlen;
2933 args.type = XFS_ALLOCTYPE_START_BNO;
2934 args.fsbno = ap->rval;
2935 if ((error = xfs_alloc_vextent(&args)))
2936 return error;
2937 }
2938 if (args.fsbno == NULLFSBLOCK && nullfb) {
2939 args.fsbno = 0;
2940 args.type = XFS_ALLOCTYPE_FIRST_AG;
2941 args.total = ap->minlen;
2942 args.minleft = 0;
2943 if ((error = xfs_alloc_vextent(&args)))
2944 return error;
2945 ap->low = 1;
2946 }
2947 if (args.fsbno != NULLFSBLOCK) {
2948 ap->firstblock = ap->rval = args.fsbno;
2949 ASSERT(nullfb || fb_agno == args.agno ||
2950 (ap->low && fb_agno < args.agno));
2951 ap->alen = args.len;
2952 ap->ip->i_d.di_nblocks += args.len;
2953 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2954 if (ap->wasdel)
2955 ap->ip->i_delayed_blks -= args.len;
2956 /*
2957 * Adjust the disk quota also. This was reserved
2958 * earlier.
2959 */
2960 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2961 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2962 XFS_TRANS_DQ_BCOUNT,
2963 (long) args.len);
2964 } else {
2965 ap->rval = NULLFSBLOCK;
2966 ap->alen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 }
2968 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002969}
2970
2971/*
2972 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2973 * It figures out where to ask the underlying allocator to put the new extent.
2974 */
2975STATIC int
2976xfs_bmap_alloc(
2977 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2978{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002979 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002980 return xfs_bmap_rtalloc(ap);
2981 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
2984/*
2985 * Transform a btree format file with only one leaf node, where the
2986 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002987 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 * give up the space for the btree root and pitch the leaf block.
2989 */
2990STATIC int /* error */
2991xfs_bmap_btree_to_extents(
2992 xfs_trans_t *tp, /* transaction pointer */
2993 xfs_inode_t *ip, /* incore inode pointer */
2994 xfs_btree_cur_t *cur, /* btree cursor */
2995 int *logflagsp, /* inode logging flags */
2996 int whichfork) /* data or attr fork */
2997{
2998 /* REFERENCED */
2999 xfs_bmbt_block_t *cblock;/* child btree block */
3000 xfs_fsblock_t cbno; /* child block number */
3001 xfs_buf_t *cbp; /* child block's buffer */
3002 int error; /* error return value */
3003 xfs_ifork_t *ifp; /* inode fork data */
3004 xfs_mount_t *mp; /* mount point structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10003005 __be64 *pp; /* ptr to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 xfs_bmbt_block_t *rblock;/* root btree block */
3007
3008 ifp = XFS_IFORK_PTR(ip, whichfork);
3009 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3010 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
3011 rblock = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003012 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
3013 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
3015 mp = ip->i_mount;
3016 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10003017 cbno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 *logflagsp = 0;
3019#ifdef DEBUG
Christoph Hellwig576039c2006-09-28 10:58:06 +10003020 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return error;
3022#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
3024 XFS_BMAP_BTREE_REF)))
3025 return error;
3026 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
3027 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
3028 return error;
3029 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
3030 ip->i_d.di_nblocks--;
3031 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
3032 xfs_trans_binval(tp, cbp);
3033 if (cur->bc_bufs[0] == cbp)
3034 cur->bc_bufs[0] = NULL;
3035 xfs_iroot_realloc(ip, -1, whichfork);
3036 ASSERT(ifp->if_broot == NULL);
3037 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
3038 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3039 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
3040 return 0;
3041}
3042
3043/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003044 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 * after removing space (or undoing a delayed allocation).
3046 */
3047STATIC int /* error */
3048xfs_bmap_del_extent(
3049 xfs_inode_t *ip, /* incore inode pointer */
3050 xfs_trans_t *tp, /* current transaction pointer */
3051 xfs_extnum_t idx, /* extent number to update/delete */
3052 xfs_bmap_free_t *flist, /* list of extents to be freed */
3053 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003054 xfs_bmbt_irec_t *del, /* data to remove from extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003056 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 int whichfork, /* data or attr fork */
3058 int rsvd) /* OK to allocate reserved blocks */
3059{
3060 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
3061 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
3062 xfs_fsblock_t del_endblock=0; /* first block past del */
3063 xfs_fileoff_t del_endoff; /* first offset past del */
3064 int delay; /* current block is delayed allocated */
3065 int do_fx; /* free extent at end of routine */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003066 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 int error; /* error return value */
3068 int flags; /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 xfs_bmbt_irec_t got; /* current extent entry */
3070 xfs_fileoff_t got_endoff; /* first offset past got */
3071 int i; /* temp state */
3072 xfs_ifork_t *ifp; /* inode fork pointer */
3073 xfs_mount_t *mp; /* mount structure */
3074 xfs_filblks_t nblks; /* quota/sb block count */
3075 xfs_bmbt_irec_t new; /* new record to be inserted */
3076 /* REFERENCED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 uint qfield; /* quota field to update */
3078 xfs_filblks_t temp; /* for indirect length calculations */
3079 xfs_filblks_t temp2; /* for indirect length calculations */
3080
3081 XFS_STATS_INC(xs_del_exlist);
3082 mp = ip->i_mount;
3083 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003084 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
3085 (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 ASSERT(del->br_blockcount > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003087 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 xfs_bmbt_get_all(ep, &got);
3089 ASSERT(got.br_startoff <= del->br_startoff);
3090 del_endoff = del->br_startoff + del->br_blockcount;
3091 got_endoff = got.br_startoff + got.br_blockcount;
3092 ASSERT(got_endoff >= del_endoff);
3093 delay = ISNULLSTARTBLOCK(got.br_startblock);
3094 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
3095 flags = 0;
3096 qfield = 0;
3097 error = 0;
3098 /*
3099 * If deleting a real allocation, must free up the disk space.
3100 */
3101 if (!delay) {
3102 flags = XFS_ILOG_CORE;
3103 /*
3104 * Realtime allocation. Free it and record di_nblocks update.
3105 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003106 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 xfs_fsblock_t bno;
3108 xfs_filblks_t len;
3109
3110 ASSERT(do_mod(del->br_blockcount,
3111 mp->m_sb.sb_rextsize) == 0);
3112 ASSERT(do_mod(del->br_startblock,
3113 mp->m_sb.sb_rextsize) == 0);
3114 bno = del->br_startblock;
3115 len = del->br_blockcount;
3116 do_div(bno, mp->m_sb.sb_rextsize);
3117 do_div(len, mp->m_sb.sb_rextsize);
3118 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3119 (xfs_extlen_t)len)))
3120 goto done;
3121 do_fx = 0;
3122 nblks = len * mp->m_sb.sb_rextsize;
3123 qfield = XFS_TRANS_DQ_RTBCOUNT;
3124 }
3125 /*
3126 * Ordinary allocation.
3127 */
3128 else {
3129 do_fx = 1;
3130 nblks = del->br_blockcount;
3131 qfield = XFS_TRANS_DQ_BCOUNT;
3132 }
3133 /*
3134 * Set up del_endblock and cur for later.
3135 */
3136 del_endblock = del->br_startblock + del->br_blockcount;
3137 if (cur) {
3138 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3139 got.br_startblock, got.br_blockcount,
3140 &i)))
3141 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003142 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 }
3144 da_old = da_new = 0;
3145 } else {
3146 da_old = STARTBLOCKVAL(got.br_startblock);
3147 da_new = 0;
3148 nblks = 0;
3149 do_fx = 0;
3150 }
3151 /*
3152 * Set flag value to use in switch statement.
3153 * Left-contig is 2, right-contig is 1.
3154 */
3155 switch (((got.br_startoff == del->br_startoff) << 1) |
3156 (got_endoff == del_endoff)) {
3157 case 3:
3158 /*
3159 * Matches the whole extent. Delete the entry.
3160 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10003161 XFS_BMAP_TRACE_DELETE("3", ip, idx, 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003162 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 ifp->if_lastex = idx;
3164 if (delay)
3165 break;
3166 XFS_IFORK_NEXT_SET(ip, whichfork,
3167 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3168 flags |= XFS_ILOG_CORE;
3169 if (!cur) {
3170 flags |= XFS_ILOG_FEXT(whichfork);
3171 break;
3172 }
3173 if ((error = xfs_bmbt_delete(cur, &i)))
3174 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003175 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 break;
3177
3178 case 2:
3179 /*
3180 * Deleting the first part of the extent.
3181 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10003182 XFS_BMAP_TRACE_PRE_UPDATE("2", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 xfs_bmbt_set_startoff(ep, del_endoff);
3184 temp = got.br_blockcount - del->br_blockcount;
3185 xfs_bmbt_set_blockcount(ep, temp);
3186 ifp->if_lastex = idx;
3187 if (delay) {
3188 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3189 da_old);
3190 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10003191 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 whichfork);
3193 da_new = temp;
3194 break;
3195 }
3196 xfs_bmbt_set_startblock(ep, del_endblock);
Eric Sandeen3a59c942007-07-11 11:09:47 +10003197 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 if (!cur) {
3199 flags |= XFS_ILOG_FEXT(whichfork);
3200 break;
3201 }
3202 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3203 got.br_blockcount - del->br_blockcount,
3204 got.br_state)))
3205 goto done;
3206 break;
3207
3208 case 1:
3209 /*
3210 * Deleting the last part of the extent.
3211 */
3212 temp = got.br_blockcount - del->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10003213 XFS_BMAP_TRACE_PRE_UPDATE("1", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 xfs_bmbt_set_blockcount(ep, temp);
3215 ifp->if_lastex = idx;
3216 if (delay) {
3217 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3218 da_old);
3219 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10003220 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 whichfork);
3222 da_new = temp;
3223 break;
3224 }
Eric Sandeen3a59c942007-07-11 11:09:47 +10003225 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 if (!cur) {
3227 flags |= XFS_ILOG_FEXT(whichfork);
3228 break;
3229 }
3230 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3231 got.br_startblock,
3232 got.br_blockcount - del->br_blockcount,
3233 got.br_state)))
3234 goto done;
3235 break;
3236
3237 case 0:
3238 /*
3239 * Deleting the middle of the extent.
3240 */
3241 temp = del->br_startoff - got.br_startoff;
Eric Sandeen3a59c942007-07-11 11:09:47 +10003242 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 xfs_bmbt_set_blockcount(ep, temp);
3244 new.br_startoff = del_endoff;
3245 temp2 = got_endoff - del_endoff;
3246 new.br_blockcount = temp2;
3247 new.br_state = got.br_state;
3248 if (!delay) {
3249 new.br_startblock = del_endblock;
3250 flags |= XFS_ILOG_CORE;
3251 if (cur) {
3252 if ((error = xfs_bmbt_update(cur,
3253 got.br_startoff,
3254 got.br_startblock, temp,
3255 got.br_state)))
3256 goto done;
3257 if ((error = xfs_bmbt_increment(cur, 0, &i)))
3258 goto done;
3259 cur->bc_rec.b = new;
3260 error = xfs_bmbt_insert(cur, &i);
3261 if (error && error != ENOSPC)
3262 goto done;
3263 /*
3264 * If get no-space back from btree insert,
3265 * it tried a split, and we have a zero
3266 * block reservation.
3267 * Fix up our state and return the error.
3268 */
3269 if (error == ENOSPC) {
3270 /*
3271 * Reset the cursor, don't trust
3272 * it after any insert operation.
3273 */
3274 if ((error = xfs_bmbt_lookup_eq(cur,
3275 got.br_startoff,
3276 got.br_startblock,
3277 temp, &i)))
3278 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003279 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 /*
3281 * Update the btree record back
3282 * to the original value.
3283 */
3284 if ((error = xfs_bmbt_update(cur,
3285 got.br_startoff,
3286 got.br_startblock,
3287 got.br_blockcount,
3288 got.br_state)))
3289 goto done;
3290 /*
3291 * Reset the extent record back
3292 * to the original value.
3293 */
3294 xfs_bmbt_set_blockcount(ep,
3295 got.br_blockcount);
3296 flags = 0;
3297 error = XFS_ERROR(ENOSPC);
3298 goto done;
3299 }
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003300 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 } else
3302 flags |= XFS_ILOG_FEXT(whichfork);
3303 XFS_IFORK_NEXT_SET(ip, whichfork,
3304 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3305 } else {
3306 ASSERT(whichfork == XFS_DATA_FORK);
3307 temp = xfs_bmap_worst_indlen(ip, temp);
3308 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3309 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3310 new.br_startblock = NULLSTARTBLOCK((int)temp2);
3311 da_new = temp + temp2;
3312 while (da_new > da_old) {
3313 if (temp) {
3314 temp--;
3315 da_new--;
3316 xfs_bmbt_set_startblock(ep,
3317 NULLSTARTBLOCK((int)temp));
3318 }
3319 if (da_new == da_old)
3320 break;
3321 if (temp2) {
3322 temp2--;
3323 da_new--;
3324 new.br_startblock =
3325 NULLSTARTBLOCK((int)temp2);
3326 }
3327 }
3328 }
Eric Sandeen3a59c942007-07-11 11:09:47 +10003329 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, whichfork);
3330 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 1, &new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003332 xfs_iext_insert(ifp, idx + 1, 1, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 ifp->if_lastex = idx + 1;
3334 break;
3335 }
3336 /*
3337 * If we need to, add to list of extents to delete.
3338 */
3339 if (do_fx)
3340 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3341 mp);
3342 /*
3343 * Adjust inode # blocks in the file.
3344 */
3345 if (nblks)
3346 ip->i_d.di_nblocks -= nblks;
3347 /*
3348 * Adjust quota data.
3349 */
3350 if (qfield)
3351 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
3352
3353 /*
3354 * Account for change in delayed indirect blocks.
3355 * Nothing to do for disk quota accounting here.
3356 */
3357 ASSERT(da_old >= da_new);
3358 if (da_old > da_new)
David Chinner20f4ebf2007-02-10 18:36:10 +11003359 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 rsvd);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003361 if (delta) {
3362 /* DELTA: report the original extent. */
3363 if (delta->xed_startoff > got.br_startoff)
3364 delta->xed_startoff = got.br_startoff;
3365 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3366 delta->xed_blockcount = got.br_startoff +
3367 got.br_blockcount;
3368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369done:
3370 *logflagsp = flags;
3371 return error;
3372}
3373
3374/*
3375 * Remove the entry "free" from the free item list. Prev points to the
3376 * previous entry, unless "free" is the head of the list.
3377 */
3378STATIC void
3379xfs_bmap_del_free(
3380 xfs_bmap_free_t *flist, /* free item list header */
3381 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3382 xfs_bmap_free_item_t *free) /* list item to be freed */
3383{
3384 if (prev)
3385 prev->xbfi_next = free->xbfi_next;
3386 else
3387 flist->xbf_first = free->xbfi_next;
3388 flist->xbf_count--;
3389 kmem_zone_free(xfs_bmap_free_item_zone, free);
3390}
3391
3392/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 * Convert an extents-format file into a btree-format file.
3394 * The new file will have a root block (in the inode) and a single child block.
3395 */
3396STATIC int /* error */
3397xfs_bmap_extents_to_btree(
3398 xfs_trans_t *tp, /* transaction pointer */
3399 xfs_inode_t *ip, /* incore inode pointer */
3400 xfs_fsblock_t *firstblock, /* first-block-allocated */
3401 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3402 xfs_btree_cur_t **curp, /* cursor returned to caller */
3403 int wasdel, /* converting a delayed alloc */
3404 int *logflagsp, /* inode logging flags */
3405 int whichfork) /* data or attr fork */
3406{
3407 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
3408 xfs_buf_t *abp; /* buffer for ablock */
3409 xfs_alloc_arg_t args; /* allocation arguments */
3410 xfs_bmbt_rec_t *arp; /* child record pointer */
3411 xfs_bmbt_block_t *block; /* btree root block */
3412 xfs_btree_cur_t *cur; /* bmap btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003413 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003415 xfs_extnum_t i, cnt; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 xfs_ifork_t *ifp; /* inode fork pointer */
3417 xfs_bmbt_key_t *kp; /* root block key pointer */
3418 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003419 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3421
3422 ifp = XFS_IFORK_PTR(ip, whichfork);
3423 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3424 ASSERT(ifp->if_ext_max ==
3425 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3426 /*
3427 * Make space in the inode incore.
3428 */
3429 xfs_iroot_realloc(ip, 1, whichfork);
3430 ifp->if_flags |= XFS_IFBROOT;
3431 /*
3432 * Fill in the root.
3433 */
3434 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003435 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3436 block->bb_level = cpu_to_be16(1);
3437 block->bb_numrecs = cpu_to_be16(1);
3438 block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3439 block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 /*
3441 * Need a cursor. Can't allocate until bb_level is filled in.
3442 */
3443 mp = ip->i_mount;
3444 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
3445 whichfork);
3446 cur->bc_private.b.firstblock = *firstblock;
3447 cur->bc_private.b.flist = flist;
3448 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3449 /*
3450 * Convert to a btree with two levels, one record in root.
3451 */
3452 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3453 args.tp = tp;
3454 args.mp = mp;
Yingping Lud210a282006-06-09 14:55:18 +10003455 args.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 if (*firstblock == NULLFSBLOCK) {
3457 args.type = XFS_ALLOCTYPE_START_BNO;
3458 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3459 } else if (flist->xbf_low) {
3460 args.type = XFS_ALLOCTYPE_START_BNO;
3461 args.fsbno = *firstblock;
3462 } else {
3463 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3464 args.fsbno = *firstblock;
3465 }
3466 args.minlen = args.maxlen = args.prod = 1;
3467 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3468 args.minalignslop = 0;
3469 args.wasdel = wasdel;
3470 *logflagsp = 0;
3471 if ((error = xfs_alloc_vextent(&args))) {
3472 xfs_iroot_realloc(ip, -1, whichfork);
3473 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3474 return error;
3475 }
3476 /*
3477 * Allocation can't fail, the space was reserved.
3478 */
3479 ASSERT(args.fsbno != NULLFSBLOCK);
3480 ASSERT(*firstblock == NULLFSBLOCK ||
3481 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3482 (flist->xbf_low &&
3483 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3484 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3485 cur->bc_private.b.allocated++;
3486 ip->i_d.di_nblocks++;
3487 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3488 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3489 /*
3490 * Fill in the child block.
3491 */
3492 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003493 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 ablock->bb_level = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003495 ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3496 ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3498 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003499 for (cnt = i = 0; i < nextents; i++) {
3500 ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10003502 arp->l0 = cpu_to_be64(ep->l0);
3503 arp->l1 = cpu_to_be64(ep->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 arp++; cnt++;
3505 }
3506 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11003507 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3508 ablock->bb_numrecs = cpu_to_be16(cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 /*
3510 * Fill in the root key and pointer.
3511 */
3512 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
3513 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
Christoph Hellwig8801bb92006-09-28 10:58:17 +10003514 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
Christoph Hellwig576039c2006-09-28 10:58:06 +10003516 *pp = cpu_to_be64(args.fsbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 /*
3518 * Do all this logging at the end so that
3519 * the root is at the right level.
3520 */
3521 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003522 xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 ASSERT(*curp == NULL);
3524 *curp = cur;
3525 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
3526 return 0;
3527}
3528
3529/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11003530 * Helper routine to reset inode di_forkoff field when switching
3531 * attribute fork from local to extent format - we reset it where
3532 * possible to make space available for inline data fork extents.
3533 */
3534STATIC void
3535xfs_bmap_forkoff_reset(
3536 xfs_mount_t *mp,
3537 xfs_inode_t *ip,
3538 int whichfork)
3539{
3540 if (whichfork == XFS_ATTR_FORK &&
3541 (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
3542 (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
Barry Naujoke5889e92007-02-10 18:35:58 +11003543 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
Nathan Scottd8cc8902005-11-02 10:34:53 +11003544 ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
3545 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3546 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
3547 (uint)sizeof(xfs_bmbt_rec_t);
3548 ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
3549 (uint)sizeof(xfs_bmbt_rec_t);
3550 }
3551}
3552
3553/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 * Convert a local file to an extents file.
3555 * This code is out of bounds for data forks of regular files,
3556 * since the file data needs to get logged so things will stay consistent.
3557 * (The bmap-level manipulations are ok, though).
3558 */
3559STATIC int /* error */
3560xfs_bmap_local_to_extents(
3561 xfs_trans_t *tp, /* transaction pointer */
3562 xfs_inode_t *ip, /* incore inode pointer */
3563 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3564 xfs_extlen_t total, /* total blocks needed by transaction */
3565 int *logflagsp, /* inode logging flags */
3566 int whichfork) /* data or attr fork */
3567{
3568 int error; /* error return value */
3569 int flags; /* logging flags returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 xfs_ifork_t *ifp; /* inode fork pointer */
3571
3572 /*
3573 * We don't want to deal with the case of keeping inode data inline yet.
3574 * So sending the data fork of a regular inode is invalid.
3575 */
3576 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3577 whichfork == XFS_DATA_FORK));
3578 ifp = XFS_IFORK_PTR(ip, whichfork);
3579 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3580 flags = 0;
3581 error = 0;
3582 if (ifp->if_bytes) {
3583 xfs_alloc_arg_t args; /* allocation arguments */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003584 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003585 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
3587 args.tp = tp;
3588 args.mp = ip->i_mount;
Yingping Lud210a282006-06-09 14:55:18 +10003589 args.firstblock = *firstblock;
Mandy Kirkconnellf020b672006-03-14 14:07:24 +11003590 ASSERT((ifp->if_flags &
3591 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 /*
3593 * Allocate a block. We know we need only one, since the
3594 * file currently fits in an inode.
3595 */
3596 if (*firstblock == NULLFSBLOCK) {
3597 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3598 args.type = XFS_ALLOCTYPE_START_BNO;
3599 } else {
3600 args.fsbno = *firstblock;
3601 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3602 }
3603 args.total = total;
3604 args.mod = args.minleft = args.alignment = args.wasdel =
3605 args.isfl = args.minalignslop = 0;
3606 args.minlen = args.maxlen = args.prod = 1;
3607 if ((error = xfs_alloc_vextent(&args)))
3608 goto done;
3609 /*
3610 * Can't fail, the space was reserved.
3611 */
3612 ASSERT(args.fsbno != NULLFSBLOCK);
3613 ASSERT(args.len == 1);
3614 *firstblock = args.fsbno;
3615 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3616 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3617 ifp->if_bytes);
3618 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003619 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003621 xfs_iext_add(ifp, 0, 1);
3622 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
Eric Sandeen3a59c942007-07-11 11:09:47 +10003624 XFS_BMAP_TRACE_POST_UPDATE("new", ip, 0, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3626 ip->i_d.di_nblocks = 1;
3627 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
3628 XFS_TRANS_DQ_BCOUNT, 1L);
3629 flags |= XFS_ILOG_FEXT(whichfork);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003630 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003632 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 ifp->if_flags &= ~XFS_IFINLINE;
3635 ifp->if_flags |= XFS_IFEXTENTS;
3636 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3637 flags |= XFS_ILOG_CORE;
3638done:
3639 *logflagsp = flags;
3640 return error;
3641}
3642
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003644 * Search the extent records for the entry containing block bno.
3645 * If bno lies in a hole, point to the next entry. If bno lies
3646 * past eof, *eofp will be set, and *prevp will contain the last
3647 * entry (null if none). Else, *lastxp will be set to the index
3648 * of the found entry; *gotp will contain the entry.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003649 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003650xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003651xfs_bmap_search_multi_extents(
3652 xfs_ifork_t *ifp, /* inode fork pointer */
3653 xfs_fileoff_t bno, /* block number searched for */
3654 int *eofp, /* out: end of file found */
3655 xfs_extnum_t *lastxp, /* out: last extent index */
3656 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3657 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3658{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003659 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003660 xfs_extnum_t lastx; /* last extent index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003661
3662 /*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003663 * Initialize the extent entry structure to catch access to
3664 * uninitialized br_startblock field.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003665 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003666 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3667 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3668 gotp->br_state = XFS_EXT_INVALID;
3669#if XFS_BIG_BLKNOS
3670 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3671#else
3672 gotp->br_startblock = 0xffffa5a5;
3673#endif
3674 prevp->br_startoff = NULLFILEOFF;
3675
3676 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3677 if (lastx > 0) {
3678 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3679 }
3680 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3681 xfs_bmbt_get_all(ep, gotp);
3682 *eofp = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003683 } else {
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003684 if (lastx > 0) {
3685 *gotp = *prevp;
3686 }
3687 *eofp = 1;
3688 ep = NULL;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003689 }
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003690 *lastxp = lastx;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003691 return ep;
3692}
3693
3694/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 * Search the extents list for the inode, for the extent containing bno.
3696 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3697 * *eofp will be set, and *prevp will contain the last entry (null if none).
3698 * Else, *lastxp will be set to the index of the found
3699 * entry; *gotp will contain the entry.
3700 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003701STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702xfs_bmap_search_extents(
3703 xfs_inode_t *ip, /* incore inode pointer */
3704 xfs_fileoff_t bno, /* block number searched for */
Nathan Scott572d95f2006-09-28 11:03:20 +10003705 int fork, /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 int *eofp, /* out: end of file found */
3707 xfs_extnum_t *lastxp, /* out: last extent index */
3708 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3709 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3710{
3711 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003712 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
3714 XFS_STATS_INC(xs_look_exlist);
Nathan Scott572d95f2006-09-28 11:03:20 +10003715 ifp = XFS_IFORK_PTR(ip, fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003717 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3718
Nathan Scott572d95f2006-09-28 11:03:20 +10003719 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3720 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3721 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3722 "Access to block zero in inode %llu "
3723 "start_block: %llx start_off: %llx "
3724 "blkcnt: %llx extent-state: %x lastx: %x\n",
3725 (unsigned long long)ip->i_ino,
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003726 (unsigned long long)gotp->br_startblock,
3727 (unsigned long long)gotp->br_startoff,
3728 (unsigned long long)gotp->br_blockcount,
Nathan Scott572d95f2006-09-28 11:03:20 +10003729 gotp->br_state, *lastxp);
3730 *lastxp = NULLEXTNUM;
3731 *eofp = 1;
3732 return NULL;
3733 }
3734 return ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735}
3736
3737
3738#ifdef XFS_BMAP_TRACE
3739ktrace_t *xfs_bmap_trace_buf;
3740
3741/*
3742 * Add a bmap trace buffer entry. Base routine for the others.
3743 */
3744STATIC void
3745xfs_bmap_trace_addentry(
3746 int opcode, /* operation */
Eric Sandeen3a59c942007-07-11 11:09:47 +10003747 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 char *desc, /* operation description */
3749 xfs_inode_t *ip, /* incore inode pointer */
3750 xfs_extnum_t idx, /* index of entry(ies) */
3751 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003752 xfs_bmbt_rec_host_t *r1, /* first record */
3753 xfs_bmbt_rec_host_t *r2, /* second record or null */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 int whichfork) /* data or attr fork */
3755{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003756 xfs_bmbt_rec_host_t tr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
3758 ASSERT(cnt == 1 || cnt == 2);
3759 ASSERT(r1 != NULL);
3760 if (cnt == 1) {
3761 ASSERT(r2 == NULL);
3762 r2 = &tr2;
3763 memset(&tr2, 0, sizeof(tr2));
3764 } else
3765 ASSERT(r2 != NULL);
3766 ktrace_enter(xfs_bmap_trace_buf,
3767 (void *)(__psint_t)(opcode | (whichfork << 16)),
3768 (void *)fname, (void *)desc, (void *)ip,
3769 (void *)(__psint_t)idx,
3770 (void *)(__psint_t)cnt,
3771 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3772 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3773 (void *)(__psunsigned_t)(r1->l0 >> 32),
3774 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3775 (void *)(__psunsigned_t)(r1->l1 >> 32),
3776 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3777 (void *)(__psunsigned_t)(r2->l0 >> 32),
3778 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3779 (void *)(__psunsigned_t)(r2->l1 >> 32),
3780 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3781 );
3782 ASSERT(ip->i_xtrace);
3783 ktrace_enter(ip->i_xtrace,
3784 (void *)(__psint_t)(opcode | (whichfork << 16)),
3785 (void *)fname, (void *)desc, (void *)ip,
3786 (void *)(__psint_t)idx,
3787 (void *)(__psint_t)cnt,
3788 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3789 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3790 (void *)(__psunsigned_t)(r1->l0 >> 32),
3791 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3792 (void *)(__psunsigned_t)(r1->l1 >> 32),
3793 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3794 (void *)(__psunsigned_t)(r2->l0 >> 32),
3795 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3796 (void *)(__psunsigned_t)(r2->l1 >> 32),
3797 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3798 );
3799}
3800
3801/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003802 * Add bmap trace entry prior to a call to xfs_iext_remove.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 */
3804STATIC void
3805xfs_bmap_trace_delete(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003806 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 char *desc, /* operation description */
3808 xfs_inode_t *ip, /* incore inode pointer */
3809 xfs_extnum_t idx, /* index of entry(entries) deleted */
3810 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3811 int whichfork) /* data or attr fork */
3812{
3813 xfs_ifork_t *ifp; /* inode fork pointer */
3814
3815 ifp = XFS_IFORK_PTR(ip, whichfork);
3816 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003817 cnt, xfs_iext_get_ext(ifp, idx),
3818 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 whichfork);
3820}
3821
3822/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003823 * Add bmap trace entry prior to a call to xfs_iext_insert, or
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 * reading in the extents list from the disk (in the btree).
3825 */
3826STATIC void
3827xfs_bmap_trace_insert(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003828 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 char *desc, /* operation description */
3830 xfs_inode_t *ip, /* incore inode pointer */
3831 xfs_extnum_t idx, /* index of entry(entries) inserted */
3832 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3833 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3834 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3835 int whichfork) /* data or attr fork */
3836{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003837 xfs_bmbt_rec_host_t tr1; /* compressed record 1 */
3838 xfs_bmbt_rec_host_t tr2; /* compressed record 2 if needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
3840 xfs_bmbt_set_all(&tr1, r1);
3841 if (cnt == 2) {
3842 ASSERT(r2 != NULL);
3843 xfs_bmbt_set_all(&tr2, r2);
3844 } else {
3845 ASSERT(cnt == 1);
3846 ASSERT(r2 == NULL);
3847 }
3848 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3849 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3850}
3851
3852/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003853 * Add bmap trace entry after updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 */
3855STATIC void
3856xfs_bmap_trace_post_update(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003857 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 char *desc, /* operation description */
3859 xfs_inode_t *ip, /* incore inode pointer */
3860 xfs_extnum_t idx, /* index of entry updated */
3861 int whichfork) /* data or attr fork */
3862{
3863 xfs_ifork_t *ifp; /* inode fork pointer */
3864
3865 ifp = XFS_IFORK_PTR(ip, whichfork);
3866 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003867 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868}
3869
3870/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003871 * Add bmap trace entry prior to updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 */
3873STATIC void
3874xfs_bmap_trace_pre_update(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003875 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 char *desc, /* operation description */
3877 xfs_inode_t *ip, /* incore inode pointer */
3878 xfs_extnum_t idx, /* index of entry to be updated */
3879 int whichfork) /* data or attr fork */
3880{
3881 xfs_ifork_t *ifp; /* inode fork pointer */
3882
3883 ifp = XFS_IFORK_PTR(ip, whichfork);
3884 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003885 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886}
3887#endif /* XFS_BMAP_TRACE */
3888
3889/*
3890 * Compute the worst-case number of indirect blocks that will be used
3891 * for ip's delayed extent of length "len".
3892 */
3893STATIC xfs_filblks_t
3894xfs_bmap_worst_indlen(
3895 xfs_inode_t *ip, /* incore inode pointer */
3896 xfs_filblks_t len) /* delayed extent length */
3897{
3898 int level; /* btree level number */
3899 int maxrecs; /* maximum record count at this level */
3900 xfs_mount_t *mp; /* mount structure */
3901 xfs_filblks_t rval; /* return value */
3902
3903 mp = ip->i_mount;
3904 maxrecs = mp->m_bmap_dmxr[0];
3905 for (level = 0, rval = 0;
3906 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3907 level++) {
3908 len += maxrecs - 1;
3909 do_div(len, maxrecs);
3910 rval += len;
3911 if (len == 1)
3912 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3913 level - 1;
3914 if (level == 0)
3915 maxrecs = mp->m_bmap_dmxr[1];
3916 }
3917 return rval;
3918}
3919
3920#if defined(XFS_RW_TRACE)
3921STATIC void
3922xfs_bunmap_trace(
3923 xfs_inode_t *ip,
3924 xfs_fileoff_t bno,
3925 xfs_filblks_t len,
3926 int flags,
3927 inst_t *ra)
3928{
3929 if (ip->i_rwtrace == NULL)
3930 return;
3931 ktrace_enter(ip->i_rwtrace,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003932 (void *)(__psint_t)XFS_BUNMAP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 (void *)ip,
3934 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3935 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3936 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3937 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3938 (void *)(__psint_t)len,
3939 (void *)(__psint_t)flags,
3940 (void *)(unsigned long)current_cpu(),
3941 (void *)ra,
3942 (void *)0,
3943 (void *)0,
3944 (void *)0,
3945 (void *)0,
3946 (void *)0,
3947 (void *)0);
3948}
3949#endif
3950
3951/*
3952 * Convert inode from non-attributed to attributed.
3953 * Must not be in a transaction, ip must not be locked.
3954 */
3955int /* error code */
3956xfs_bmap_add_attrfork(
3957 xfs_inode_t *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003958 int size, /* space new attribute needs */
3959 int rsvd) /* xact may use reserved blks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003962 xfs_bmap_free_t flist; /* freed extent records */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 xfs_trans_t *tp; /* transaction pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003965 int blks; /* space reservation */
3966 int version = 1; /* superblock attr version */
3967 int committed; /* xaction was committed */
3968 int logflags; /* logging flags */
3969 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
Nathan Scottd8cc8902005-11-02 10:34:53 +11003971 ASSERT(XFS_IFORK_Q(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 ASSERT(ip->i_df.if_ext_max ==
3973 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
Nathan Scottd8cc8902005-11-02 10:34:53 +11003974
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 mp = ip->i_mount;
3976 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3977 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3978 blks = XFS_ADDAFORK_SPACE_RES(mp);
3979 if (rsvd)
3980 tp->t_flags |= XFS_TRANS_RESERVE;
3981 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3982 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3983 goto error0;
3984 xfs_ilock(ip, XFS_ILOCK_EXCL);
3985 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
3986 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3987 XFS_QMOPT_RES_REGBLKS);
3988 if (error) {
3989 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3990 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3991 return error;
3992 }
3993 if (XFS_IFORK_Q(ip))
3994 goto error1;
3995 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3996 /*
3997 * For inodes coming from pre-6.2 filesystems.
3998 */
3999 ASSERT(ip->i_d.di_aformat == 0);
4000 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
4001 }
4002 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwig0b1f91772008-08-13 16:13:09 +10004003 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4005 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4006 switch (ip->i_d.di_format) {
4007 case XFS_DINODE_FMT_DEV:
4008 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
4009 break;
4010 case XFS_DINODE_FMT_UUID:
4011 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
4012 break;
4013 case XFS_DINODE_FMT_LOCAL:
4014 case XFS_DINODE_FMT_EXTENTS:
4015 case XFS_DINODE_FMT_BTREE:
Nathan Scottd8cc8902005-11-02 10:34:53 +11004016 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
4017 if (!ip->i_d.di_forkoff)
4018 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
Nathan Scott13059ff2006-01-11 15:32:01 +11004019 else if (mp->m_flags & XFS_MOUNT_ATTR2)
Nathan Scottd8cc8902005-11-02 10:34:53 +11004020 version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 break;
4022 default:
4023 ASSERT(0);
4024 error = XFS_ERROR(EINVAL);
4025 goto error1;
4026 }
4027 ip->i_df.if_ext_max =
4028 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4029 ASSERT(ip->i_afp == NULL);
4030 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
4031 ip->i_afp->if_ext_max =
4032 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4033 ip->i_afp->if_flags = XFS_IFEXTENTS;
4034 logflags = 0;
4035 XFS_BMAP_INIT(&flist, &firstblock);
4036 switch (ip->i_d.di_format) {
4037 case XFS_DINODE_FMT_LOCAL:
4038 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
4039 &logflags);
4040 break;
4041 case XFS_DINODE_FMT_EXTENTS:
4042 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
4043 &flist, &logflags);
4044 break;
4045 case XFS_DINODE_FMT_BTREE:
4046 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
4047 &logflags);
4048 break;
4049 default:
4050 error = 0;
4051 break;
4052 }
4053 if (logflags)
4054 xfs_trans_log_inode(tp, ip, logflags);
4055 if (error)
4056 goto error2;
Eric Sandeen62118702008-03-06 13:44:28 +11004057 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
4058 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +11004059 __int64_t sbfields = 0;
4060
Eric Sandeen3685c2a2007-10-11 17:42:32 +10004061 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +11004062 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
4063 xfs_sb_version_addattr(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11004064 sbfields |= XFS_SB_VERSIONNUM;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004065 }
Eric Sandeen62118702008-03-06 13:44:28 +11004066 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
4067 xfs_sb_version_addattr2(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11004068 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
Nathan Scottd8cc8902005-11-02 10:34:53 +11004069 }
Nathan Scottda087ba2005-11-02 15:00:20 +11004070 if (sbfields) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10004071 spin_unlock(&mp->m_sb_lock);
Nathan Scottda087ba2005-11-02 15:00:20 +11004072 xfs_mod_sb(tp, sbfields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +10004074 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 }
Eric Sandeenf7c99b62007-02-10 18:37:16 +11004076 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 goto error2;
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004078 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 ASSERT(ip->i_df.if_ext_max ==
4080 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4081 return error;
4082error2:
4083 xfs_bmap_cancel(&flist);
4084error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4086error0:
4087 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4088 ASSERT(ip->i_df.if_ext_max ==
4089 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4090 return error;
4091}
4092
4093/*
4094 * Add the extent to the list of extents to be free at transaction end.
4095 * The list is maintained sorted (by block number).
4096 */
4097/* ARGSUSED */
4098void
4099xfs_bmap_add_free(
4100 xfs_fsblock_t bno, /* fs block number of extent */
4101 xfs_filblks_t len, /* length of extent */
4102 xfs_bmap_free_t *flist, /* list of extents */
4103 xfs_mount_t *mp) /* mount point structure */
4104{
4105 xfs_bmap_free_item_t *cur; /* current (next) element */
4106 xfs_bmap_free_item_t *new; /* new element */
4107 xfs_bmap_free_item_t *prev; /* previous element */
4108#ifdef DEBUG
4109 xfs_agnumber_t agno;
4110 xfs_agblock_t agbno;
4111
4112 ASSERT(bno != NULLFSBLOCK);
4113 ASSERT(len > 0);
4114 ASSERT(len <= MAXEXTLEN);
4115 ASSERT(!ISNULLSTARTBLOCK(bno));
4116 agno = XFS_FSB_TO_AGNO(mp, bno);
4117 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4118 ASSERT(agno < mp->m_sb.sb_agcount);
4119 ASSERT(agbno < mp->m_sb.sb_agblocks);
4120 ASSERT(len < mp->m_sb.sb_agblocks);
4121 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4122#endif
4123 ASSERT(xfs_bmap_free_item_zone != NULL);
4124 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4125 new->xbfi_startblock = bno;
4126 new->xbfi_blockcount = (xfs_extlen_t)len;
4127 for (prev = NULL, cur = flist->xbf_first;
4128 cur != NULL;
4129 prev = cur, cur = cur->xbfi_next) {
4130 if (cur->xbfi_startblock >= bno)
4131 break;
4132 }
4133 if (prev)
4134 prev->xbfi_next = new;
4135 else
4136 flist->xbf_first = new;
4137 new->xbfi_next = cur;
4138 flist->xbf_count++;
4139}
4140
4141/*
4142 * Compute and fill in the value of the maximum depth of a bmap btree
4143 * in this filesystem. Done once, during mount.
4144 */
4145void
4146xfs_bmap_compute_maxlevels(
4147 xfs_mount_t *mp, /* file system mount structure */
4148 int whichfork) /* data or attr fork */
4149{
4150 int level; /* btree level */
4151 uint maxblocks; /* max blocks at this level */
4152 uint maxleafents; /* max leaf entries possible */
4153 int maxrootrecs; /* max records in root block */
4154 int minleafrecs; /* min records in leaf block */
4155 int minnoderecs; /* min records in node block */
4156 int sz; /* root block size */
4157
4158 /*
4159 * The maximum number of extents in a file, hence the maximum
4160 * number of leaf entries, is controlled by the type of di_nextents
4161 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4162 * (a signed 16-bit number, xfs_aextnum_t).
Tim Shimmin6d1337b2008-04-17 16:50:16 +10004163 *
4164 * Note that we can no longer assume that if we are in ATTR1 that
4165 * the fork offset of all the inodes will be (m_attroffset >> 3)
4166 * because we could have mounted with ATTR2 and then mounted back
4167 * with ATTR1, keeping the di_forkoff's fixed but probably at
4168 * various positions. Therefore, for both ATTR1 and ATTR2
4169 * we have to assume the worst case scenario of a minimum size
4170 * available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11004172 if (whichfork == XFS_DATA_FORK) {
4173 maxleafents = MAXEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10004174 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11004175 } else {
4176 maxleafents = MAXAEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10004177 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11004178 }
4179 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 minleafrecs = mp->m_bmap_dmnr[0];
4181 minnoderecs = mp->m_bmap_dmnr[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4183 for (level = 1; maxblocks > 1; level++) {
4184 if (maxblocks <= maxrootrecs)
4185 maxblocks = 1;
4186 else
4187 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4188 }
4189 mp->m_bm_maxlevels[whichfork] = level;
4190}
4191
4192/*
4193 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4194 * caller. Frees all the extents that need freeing, which must be done
4195 * last due to locking considerations. We never free any extents in
4196 * the first transaction. This is to allow the caller to make the first
4197 * transaction a synchronous one so that the pointers to the data being
4198 * broken in this transaction will be permanent before the data is actually
4199 * freed. This is necessary to prevent blocks from being reallocated
4200 * and written to before the free and reallocation are actually permanent.
4201 * We do not just make the first transaction synchronous here, because
4202 * there are more efficient ways to gain the same protection in some cases
4203 * (see the file truncation code).
4204 *
4205 * Return 1 if the given transaction was committed and a new one
4206 * started, and 0 otherwise in the committed parameter.
4207 */
4208/*ARGSUSED*/
4209int /* error */
4210xfs_bmap_finish(
4211 xfs_trans_t **tp, /* transaction pointer addr */
4212 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 int *committed) /* xact committed or not */
4214{
4215 xfs_efd_log_item_t *efd; /* extent free data */
4216 xfs_efi_log_item_t *efi; /* extent free intention */
4217 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004218 xfs_bmap_free_item_t *free; /* free extent item */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 unsigned int logres; /* new log reservation */
4220 unsigned int logcount; /* new log count */
4221 xfs_mount_t *mp; /* filesystem mount structure */
4222 xfs_bmap_free_item_t *next; /* next item on free list */
4223 xfs_trans_t *ntp; /* new transaction pointer */
4224
4225 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4226 if (flist->xbf_count == 0) {
4227 *committed = 0;
4228 return 0;
4229 }
4230 ntp = *tp;
4231 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4232 for (free = flist->xbf_first; free; free = free->xbfi_next)
4233 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4234 free->xbfi_blockcount);
4235 logres = ntp->t_log_res;
4236 logcount = ntp->t_log_count;
4237 ntp = xfs_trans_dup(*tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004238 error = xfs_trans_commit(*tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 *tp = ntp;
4240 *committed = 1;
4241 /*
4242 * We have a new transaction, so we should return committed=1,
4243 * even though we're returning an error.
4244 */
4245 if (error) {
4246 return error;
4247 }
4248 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4249 logcount)))
4250 return error;
4251 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4252 for (free = flist->xbf_first; free != NULL; free = next) {
4253 next = free->xbfi_next;
4254 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4255 free->xbfi_blockcount))) {
4256 /*
4257 * The bmap free list will be cleaned up at a
4258 * higher level. The EFI will be canceled when
4259 * this transaction is aborted.
4260 * Need to force shutdown here to make sure it
4261 * happens, since this transaction may not be
4262 * dirty yet.
4263 */
4264 mp = ntp->t_mountp;
4265 if (!XFS_FORCED_SHUTDOWN(mp))
4266 xfs_force_shutdown(mp,
4267 (error == EFSCORRUPTED) ?
Nathan Scott7d04a332006-06-09 14:58:38 +10004268 SHUTDOWN_CORRUPT_INCORE :
4269 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 return error;
4271 }
4272 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4273 free->xbfi_blockcount);
4274 xfs_bmap_del_free(flist, NULL, free);
4275 }
4276 return 0;
4277}
4278
4279/*
4280 * Free up any items left in the list.
4281 */
4282void
4283xfs_bmap_cancel(
4284 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4285{
4286 xfs_bmap_free_item_t *free; /* free list item */
4287 xfs_bmap_free_item_t *next;
4288
4289 if (flist->xbf_count == 0)
4290 return;
4291 ASSERT(flist->xbf_first != NULL);
4292 for (free = flist->xbf_first; free; free = next) {
4293 next = free->xbfi_next;
4294 xfs_bmap_del_free(flist, NULL, free);
4295 }
4296 ASSERT(flist->xbf_count == 0);
4297}
4298
4299/*
4300 * Returns the file-relative block number of the first unused block(s)
4301 * in the file with at least "len" logically contiguous blocks free.
4302 * This is the lowest-address hole if the file has holes, else the first block
4303 * past the end of file.
4304 * Return 0 if the file is currently local (in-inode).
4305 */
4306int /* error */
4307xfs_bmap_first_unused(
4308 xfs_trans_t *tp, /* transaction pointer */
4309 xfs_inode_t *ip, /* incore inode */
4310 xfs_extlen_t len, /* size of hole to find */
4311 xfs_fileoff_t *first_unused, /* unused block */
4312 int whichfork) /* data or attr fork */
4313{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004315 int idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 xfs_ifork_t *ifp; /* inode fork pointer */
4317 xfs_fileoff_t lastaddr; /* last block number seen */
4318 xfs_fileoff_t lowest; /* lowest useful block */
4319 xfs_fileoff_t max; /* starting useful block */
4320 xfs_fileoff_t off; /* offset for this block */
4321 xfs_extnum_t nextents; /* number of extent entries */
4322
4323 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4324 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4325 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4326 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4327 *first_unused = 0;
4328 return 0;
4329 }
4330 ifp = XFS_IFORK_PTR(ip, whichfork);
4331 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4332 (error = xfs_iread_extents(tp, ip, whichfork)))
4333 return error;
4334 lowest = *first_unused;
4335 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004336 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004337 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 off = xfs_bmbt_get_startoff(ep);
4339 /*
4340 * See if the hole before this extent will work.
4341 */
4342 if (off >= lowest + len && off - max >= len) {
4343 *first_unused = max;
4344 return 0;
4345 }
4346 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4347 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4348 }
4349 *first_unused = max;
4350 return 0;
4351}
4352
4353/*
4354 * Returns the file-relative block number of the last block + 1 before
4355 * last_block (input value) in the file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004356 * This is not based on i_size, it is based on the extent records.
4357 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 */
4359int /* error */
4360xfs_bmap_last_before(
4361 xfs_trans_t *tp, /* transaction pointer */
4362 xfs_inode_t *ip, /* incore inode */
4363 xfs_fileoff_t *last_block, /* last block */
4364 int whichfork) /* data or attr fork */
4365{
4366 xfs_fileoff_t bno; /* input file offset */
4367 int eof; /* hit end of file */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004368 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 int error; /* error return value */
4370 xfs_bmbt_irec_t got; /* current extent value */
4371 xfs_ifork_t *ifp; /* inode fork pointer */
4372 xfs_extnum_t lastx; /* last extent used */
4373 xfs_bmbt_irec_t prev; /* previous extent value */
4374
4375 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4376 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4377 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4378 return XFS_ERROR(EIO);
4379 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4380 *last_block = 0;
4381 return 0;
4382 }
4383 ifp = XFS_IFORK_PTR(ip, whichfork);
4384 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4385 (error = xfs_iread_extents(tp, ip, whichfork)))
4386 return error;
4387 bno = *last_block - 1;
4388 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4389 &prev);
4390 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4391 if (prev.br_startoff == NULLFILEOFF)
4392 *last_block = 0;
4393 else
4394 *last_block = prev.br_startoff + prev.br_blockcount;
4395 }
4396 /*
4397 * Otherwise *last_block is already the right answer.
4398 */
4399 return 0;
4400}
4401
4402/*
4403 * Returns the file-relative block number of the first block past eof in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004404 * the file. This is not based on i_size, it is based on the extent records.
4405 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 */
4407int /* error */
4408xfs_bmap_last_offset(
4409 xfs_trans_t *tp, /* transaction pointer */
4410 xfs_inode_t *ip, /* incore inode */
4411 xfs_fileoff_t *last_block, /* last block */
4412 int whichfork) /* data or attr fork */
4413{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004414 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 int error; /* error return value */
4416 xfs_ifork_t *ifp; /* inode fork pointer */
4417 xfs_extnum_t nextents; /* number of extent entries */
4418
4419 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4420 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4421 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4422 return XFS_ERROR(EIO);
4423 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4424 *last_block = 0;
4425 return 0;
4426 }
4427 ifp = XFS_IFORK_PTR(ip, whichfork);
4428 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4429 (error = xfs_iread_extents(tp, ip, whichfork)))
4430 return error;
4431 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4432 if (!nextents) {
4433 *last_block = 0;
4434 return 0;
4435 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004436 ep = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4438 return 0;
4439}
4440
4441/*
4442 * Returns whether the selected fork of the inode has exactly one
4443 * block or not. For the data fork we check this matches di_size,
4444 * implying the file's range is 0..bsize-1.
4445 */
4446int /* 1=>1 block, 0=>otherwise */
4447xfs_bmap_one_block(
4448 xfs_inode_t *ip, /* incore inode */
4449 int whichfork) /* data or attr fork */
4450{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004451 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 xfs_ifork_t *ifp; /* inode fork pointer */
4453 int rval; /* return value */
4454 xfs_bmbt_irec_t s; /* internal version of extent */
4455
4456#ifndef DEBUG
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004457 if (whichfork == XFS_DATA_FORK) {
4458 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4459 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4460 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462#endif /* !DEBUG */
4463 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4464 return 0;
4465 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4466 return 0;
4467 ifp = XFS_IFORK_PTR(ip, whichfork);
4468 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004469 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 xfs_bmbt_get_all(ep, &s);
4471 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4472 if (rval && whichfork == XFS_DATA_FORK)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004473 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 return rval;
4475}
4476
4477/*
4478 * Read in the extents to if_extents.
4479 * All inode fields are set up by caller, we just traverse the btree
4480 * and copy the records in. If the file system cannot contain unwritten
4481 * extents, the records are checked for no "state" flags.
4482 */
4483int /* error */
4484xfs_bmap_read_extents(
4485 xfs_trans_t *tp, /* transaction pointer */
4486 xfs_inode_t *ip, /* incore inode */
4487 int whichfork) /* data or attr fork */
4488{
4489 xfs_bmbt_block_t *block; /* current btree block */
4490 xfs_fsblock_t bno; /* block # of "block" */
4491 xfs_buf_t *bp; /* buffer for "block" */
4492 int error; /* error return value */
4493 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 xfs_extnum_t i, j; /* index into the extents list */
4495 xfs_ifork_t *ifp; /* fork structure */
4496 int level; /* btree level, for checking */
4497 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10004498 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 /* REFERENCED */
4500 xfs_extnum_t room; /* number of entries there's room for */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
4502 bno = NULLFSBLOCK;
4503 mp = ip->i_mount;
4504 ifp = XFS_IFORK_PTR(ip, whichfork);
4505 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4506 XFS_EXTFMT_INODE(ip);
4507 block = ifp->if_broot;
4508 /*
4509 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4510 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004511 level = be16_to_cpu(block->bb_level);
4512 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004514 bno = be64_to_cpu(*pp);
4515 ASSERT(bno != NULLDFSBNO);
4516 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4517 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 /*
4519 * Go down the tree until leaf level is reached, following the first
4520 * pointer (leftmost) at each level.
4521 */
4522 while (level-- > 0) {
4523 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4524 XFS_BMAP_BTREE_REF)))
4525 return error;
4526 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4527 XFS_WANT_CORRUPTED_GOTO(
4528 XFS_BMAP_SANITY_CHECK(mp, block, level),
4529 error0);
4530 if (level == 0)
4531 break;
Eric Sandeen2c36dde2007-02-10 18:37:33 +11004532 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004533 bno = be64_to_cpu(*pp);
4534 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 xfs_trans_brelse(tp, bp);
4536 }
4537 /*
4538 * Here with bp and block set to the leftmost leaf node in the tree.
4539 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004540 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 i = 0;
4542 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004543 * Loop over all leaf nodes. Copy information to the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 */
4545 for (;;) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004546 xfs_bmbt_rec_t *frp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 xfs_fsblock_t nextbno;
4548 xfs_extnum_t num_recs;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004549 xfs_extnum_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550
4551
Christoph Hellwig16259e72005-11-02 15:11:25 +11004552 num_recs = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 if (unlikely(i + num_recs > room)) {
4554 ASSERT(i + num_recs <= room);
Nathan Scott3762ec62006-01-12 10:29:53 +11004555 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4556 "corrupt dinode %Lu, (btree extents).",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 (unsigned long long) ip->i_ino);
4558 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4559 XFS_ERRLEVEL_LOW,
4560 ip->i_mount);
4561 goto error0;
4562 }
4563 XFS_WANT_CORRUPTED_GOTO(
4564 XFS_BMAP_SANITY_CHECK(mp, block, 0),
4565 error0);
4566 /*
4567 * Read-ahead the next leaf block, if any.
4568 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004569 nextbno = be64_to_cpu(block->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 if (nextbno != NULLFSBLOCK)
4571 xfs_btree_reada_bufl(mp, nextbno, 1);
4572 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004573 * Copy records into the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 */
Eric Sandeen2c36dde2007-02-10 18:37:33 +11004575 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004576 start = i;
4577 for (j = 0; j < num_recs; j++, i++, frp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004578 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10004579 trp->l0 = be64_to_cpu(frp->l0);
4580 trp->l1 = be64_to_cpu(frp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 }
4582 if (exntf == XFS_EXTFMT_NOSTATE) {
4583 /*
4584 * Check all attribute bmap btree records and
4585 * any "older" data bmap btree records for a
4586 * set bit in the "extent flag" position.
4587 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004588 if (unlikely(xfs_check_nostate_extents(ifp,
4589 start, num_recs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4591 XFS_ERRLEVEL_LOW,
4592 ip->i_mount);
4593 goto error0;
4594 }
4595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 xfs_trans_brelse(tp, bp);
4597 bno = nextbno;
4598 /*
4599 * If we've reached the end, stop.
4600 */
4601 if (bno == NULLFSBLOCK)
4602 break;
4603 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4604 XFS_BMAP_BTREE_REF)))
4605 return error;
4606 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4607 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004608 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
Eric Sandeen3a59c942007-07-11 11:09:47 +10004610 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 return 0;
4612error0:
4613 xfs_trans_brelse(tp, bp);
4614 return XFS_ERROR(EFSCORRUPTED);
4615}
4616
4617#ifdef XFS_BMAP_TRACE
4618/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004619 * Add bmap trace insert entries for all the contents of the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 */
4621void
4622xfs_bmap_trace_exlist(
Eric Sandeen3a59c942007-07-11 11:09:47 +10004623 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 xfs_inode_t *ip, /* incore inode pointer */
4625 xfs_extnum_t cnt, /* count of entries in the list */
4626 int whichfork) /* data or attr fork */
4627{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004628 xfs_bmbt_rec_host_t *ep; /* current extent record */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004629 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 xfs_ifork_t *ifp; /* inode fork pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004631 xfs_bmbt_irec_t s; /* file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
4633 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004634 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4635 for (idx = 0; idx < cnt; idx++) {
4636 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 xfs_bmbt_get_all(ep, &s);
Eric Sandeen3a59c942007-07-11 11:09:47 +10004638 XFS_BMAP_TRACE_INSERT("exlist", ip, idx, 1, &s, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 whichfork);
4640 }
4641}
4642#endif
4643
4644#ifdef DEBUG
4645/*
4646 * Validate that the bmbt_irecs being returned from bmapi are valid
4647 * given the callers original parameters. Specifically check the
4648 * ranges of the returned irecs to ensure that they only extent beyond
4649 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4650 */
4651STATIC void
4652xfs_bmap_validate_ret(
4653 xfs_fileoff_t bno,
4654 xfs_filblks_t len,
4655 int flags,
4656 xfs_bmbt_irec_t *mval,
4657 int nmap,
4658 int ret_nmap)
4659{
4660 int i; /* index to map values */
4661
4662 ASSERT(ret_nmap <= nmap);
4663
4664 for (i = 0; i < ret_nmap; i++) {
4665 ASSERT(mval[i].br_blockcount > 0);
4666 if (!(flags & XFS_BMAPI_ENTIRE)) {
4667 ASSERT(mval[i].br_startoff >= bno);
4668 ASSERT(mval[i].br_blockcount <= len);
4669 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4670 bno + len);
4671 } else {
4672 ASSERT(mval[i].br_startoff < bno + len);
4673 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4674 bno);
4675 }
4676 ASSERT(i == 0 ||
4677 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4678 mval[i].br_startoff);
4679 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4680 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4681 mval[i].br_startblock != HOLESTARTBLOCK);
4682 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4683 mval[i].br_state == XFS_EXT_UNWRITTEN);
4684 }
4685}
4686#endif /* DEBUG */
4687
4688
4689/*
4690 * Map file blocks to filesystem blocks.
4691 * File range is given by the bno/len pair.
4692 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4693 * into a hole or past eof.
4694 * Only allocates blocks from a single allocation group,
4695 * to avoid locking problems.
4696 * The returned value in "firstblock" from the first call in a transaction
4697 * must be remembered and presented to subsequent calls in "firstblock".
4698 * An upper bound for the number of blocks to be allocated is supplied to
4699 * the first call in "total"; if no allocation group has that many free
4700 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4701 */
4702int /* error */
4703xfs_bmapi(
4704 xfs_trans_t *tp, /* transaction pointer */
4705 xfs_inode_t *ip, /* incore inode */
4706 xfs_fileoff_t bno, /* starting file offs. mapped */
4707 xfs_filblks_t len, /* length to map in file */
4708 int flags, /* XFS_BMAPI_... */
4709 xfs_fsblock_t *firstblock, /* first allocated block
4710 controls a.g. for allocs */
4711 xfs_extlen_t total, /* total blocks needed */
4712 xfs_bmbt_irec_t *mval, /* output: map values */
4713 int *nmap, /* i/o: mval size/count */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004714 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4715 xfs_extdelta_t *delta) /* o: change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716{
4717 xfs_fsblock_t abno; /* allocated block number */
4718 xfs_extlen_t alen; /* allocated extent length */
4719 xfs_fileoff_t aoff; /* allocated file offset */
4720 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 xfs_btree_cur_t *cur; /* bmap btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 xfs_fileoff_t end; /* end of mapped file region */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004723 int eof; /* we've hit the end of extents */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004724 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004726 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 xfs_ifork_t *ifp; /* inode fork pointer */
4728 xfs_extlen_t indlen; /* indirect blocks length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 xfs_extnum_t lastx; /* last useful extent number */
4730 int logflags; /* flags for transaction logging */
4731 xfs_extlen_t minleft; /* min blocks left after allocation */
4732 xfs_extlen_t minlen; /* min allocation size */
4733 xfs_mount_t *mp; /* xfs mount structure */
4734 int n; /* current extent index */
4735 int nallocs; /* number of extents alloc\'d */
4736 xfs_extnum_t nextents; /* number of extents in file */
4737 xfs_fileoff_t obno; /* old block number (offset) */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004738 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 int tmp_logflags; /* temp flags holder */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004740 int whichfork; /* data or attr fork */
4741 char inhole; /* current location is hole in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 char wasdelay; /* old extent was delayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 char wr; /* this is a write request */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004744 char rt; /* this is a realtime file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745#ifdef DEBUG
4746 xfs_fileoff_t orig_bno; /* original block number value */
4747 int orig_flags; /* original flags arg value */
4748 xfs_filblks_t orig_len; /* original value of len arg */
4749 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4750 int orig_nmap; /* original value of *nmap */
4751
4752 orig_bno = bno;
4753 orig_len = len;
4754 orig_flags = flags;
4755 orig_mval = mval;
4756 orig_nmap = *nmap;
4757#endif
4758 ASSERT(*nmap >= 1);
4759 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4760 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4761 XFS_ATTR_FORK : XFS_DATA_FORK;
4762 mp = ip->i_mount;
4763 if (unlikely(XFS_TEST_ERROR(
4764 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4765 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4766 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4767 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4768 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4769 return XFS_ERROR(EFSCORRUPTED);
4770 }
4771 if (XFS_FORCED_SHUTDOWN(mp))
4772 return XFS_ERROR(EIO);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004773 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 ifp = XFS_IFORK_PTR(ip, whichfork);
4775 ASSERT(ifp->if_ext_max ==
4776 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4777 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4778 XFS_STATS_INC(xs_blk_mapw);
4779 else
4780 XFS_STATS_INC(xs_blk_mapr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 /*
Nathan Scott39269e22006-03-14 13:33:50 +11004782 * IGSTATE flag is used to combine extents which
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 * differ only due to the state of the extents.
4784 * This technique is used from xfs_getbmap()
4785 * when the caller does not wish to see the
4786 * separation (which is the default).
4787 *
4788 * This technique is also used when writing a
4789 * buffer which has been partially written,
4790 * (usually by being flushed during a chunkread),
4791 * to ensure one write takes place. This also
4792 * prevents a change in the xfs inode extents at
4793 * this time, intentionally. This change occurs
4794 * on completion of the write operation, in
4795 * xfs_strat_comp(), where the xfs_bmapi() call
4796 * is transactioned, and the extents combined.
4797 */
Nathan Scott39269e22006-03-14 13:33:50 +11004798 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4799 wr = 0; /* no allocations are allowed */
4800 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 logflags = 0;
4802 nallocs = 0;
4803 cur = NULL;
4804 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4805 ASSERT(wr && tp);
4806 if ((error = xfs_bmap_local_to_extents(tp, ip,
4807 firstblock, total, &logflags, whichfork)))
4808 goto error0;
4809 }
4810 if (wr && *firstblock == NULLFSBLOCK) {
4811 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Christoph Hellwig16259e72005-11-02 15:11:25 +11004812 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 else
4814 minleft = 1;
4815 } else
4816 minleft = 0;
4817 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4818 (error = xfs_iread_extents(tp, ip, whichfork)))
4819 goto error0;
4820 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4821 &prev);
4822 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4823 n = 0;
4824 end = bno + len;
4825 obno = bno;
4826 bma.ip = NULL;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004827 if (delta) {
4828 delta->xed_startoff = NULLFILEOFF;
4829 delta->xed_blockcount = 0;
4830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 while (bno < end && n < *nmap) {
4832 /*
4833 * Reading past eof, act as though there's a hole
4834 * up to end.
4835 */
4836 if (eof && !wr)
4837 got.br_startoff = end;
4838 inhole = eof || got.br_startoff > bno;
Nathan Scott39269e22006-03-14 13:33:50 +11004839 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 ISNULLSTARTBLOCK(got.br_startblock);
4841 /*
4842 * First, deal with the hole before the allocated space
4843 * that we found, if any.
4844 */
4845 if (wr && (inhole || wasdelay)) {
4846 /*
4847 * For the wasdelay case, we could also just
4848 * allocate the stuff asked for in this bmap call
4849 * but that wouldn't be as good.
4850 */
Nathan Scott39269e22006-03-14 13:33:50 +11004851 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 alen = (xfs_extlen_t)got.br_blockcount;
4853 aoff = got.br_startoff;
4854 if (lastx != NULLEXTNUM && lastx) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004855 ep = xfs_iext_get_ext(ifp, lastx - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 xfs_bmbt_get_all(ep, &prev);
4857 }
4858 } else if (wasdelay) {
4859 alen = (xfs_extlen_t)
4860 XFS_FILBLKS_MIN(len,
4861 (got.br_startoff +
4862 got.br_blockcount) - bno);
4863 aoff = bno;
4864 } else {
4865 alen = (xfs_extlen_t)
4866 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4867 if (!eof)
4868 alen = (xfs_extlen_t)
4869 XFS_FILBLKS_MIN(alen,
4870 got.br_startoff - bno);
4871 aoff = bno;
4872 }
Nathan Scott39269e22006-03-14 13:33:50 +11004873 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4874 if (flags & XFS_BMAPI_DELAY) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11004875 xfs_extlen_t extsz;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004876
4877 /* Figure out the extent size, adjust alen */
David Chinner957d0eb2007-06-18 16:50:37 +10004878 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004879 if (extsz) {
4880 error = xfs_bmap_extsize_align(mp,
4881 &got, &prev, extsz,
Nathan Scott39269e22006-03-14 13:33:50 +11004882 rt, eof,
4883 flags&XFS_BMAPI_DELAY,
4884 flags&XFS_BMAPI_CONVERT,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004885 &aoff, &alen);
4886 ASSERT(!error);
4887 }
4888
4889 if (rt)
4890 extsz = alen / mp->m_sb.sb_rextsize;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004891
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 /*
4893 * Make a transaction-less quota reservation for
4894 * delayed allocation blocks. This number gets
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004895 * adjusted later. We return if we haven't
4896 * allocated blocks already inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 */
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004898 if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS(
Nathan Scott06d10dd2005-06-21 15:48:47 +10004899 mp, NULL, ip, (long)alen, 0,
4900 rt ? XFS_QMOPT_RES_RTBLKS :
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004901 XFS_QMOPT_RES_REGBLKS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 if (n == 0) {
4903 *nmap = 0;
4904 ASSERT(cur == NULL);
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004905 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 }
4907 break;
4908 }
4909
4910 /*
4911 * Split changing sb for alen and indlen since
4912 * they could be coming from different places.
4913 */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004914 indlen = (xfs_extlen_t)
4915 xfs_bmap_worst_indlen(ip, alen);
4916 ASSERT(indlen > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917
Nathan Scottdd9f4382006-01-11 15:28:28 +11004918 if (rt) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004919 error = xfs_mod_incore_sb(mp,
4920 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004921 -((int64_t)extsz), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004922 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004923 } else {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004924 error = xfs_mod_incore_sb(mp,
4925 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004926 -((int64_t)alen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004927 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004928 }
David Chinner3bdbfb12005-09-02 16:40:47 +10004929 if (!error) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004930 error = xfs_mod_incore_sb(mp,
4931 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004932 -((int64_t)indlen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004933 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004934 if (error && rt)
4935 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004936 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004937 (int64_t)extsz, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004938 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004939 else if (error)
4940 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004941 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004942 (int64_t)alen, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004943 XFS_BMAPI_RSVBLOCKS));
David Chinner3bdbfb12005-09-02 16:40:47 +10004944 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004945
4946 if (error) {
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004947 if (XFS_IS_QUOTA_ON(mp))
Nathan Scott06d10dd2005-06-21 15:48:47 +10004948 /* unreserve the blocks now */
Nathan Scottdd9f4382006-01-11 15:28:28 +11004949 (void)
Nathan Scott06d10dd2005-06-21 15:48:47 +10004950 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4951 mp, NULL, ip,
4952 (long)alen, 0, rt ?
4953 XFS_QMOPT_RES_RTBLKS :
4954 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 break;
4956 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004957
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 ip->i_delayed_blks += alen;
4959 abno = NULLSTARTBLOCK(indlen);
4960 } else {
4961 /*
4962 * If first time, allocate and fill in
4963 * once-only bma fields.
4964 */
4965 if (bma.ip == NULL) {
4966 bma.tp = tp;
4967 bma.ip = ip;
4968 bma.prevp = &prev;
4969 bma.gotp = &got;
4970 bma.total = total;
4971 bma.userdata = 0;
4972 }
4973 /* Indicate if this is the first user data
4974 * in the file, or just any user data.
4975 */
Nathan Scott39269e22006-03-14 13:33:50 +11004976 if (!(flags & XFS_BMAPI_METADATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 bma.userdata = (aoff == 0) ?
4978 XFS_ALLOC_INITIAL_USER_DATA :
4979 XFS_ALLOC_USERDATA;
4980 }
4981 /*
4982 * Fill in changeable bma fields.
4983 */
4984 bma.eof = eof;
4985 bma.firstblock = *firstblock;
4986 bma.alen = alen;
4987 bma.off = aoff;
Adrian Bunk72880262006-08-30 13:41:58 +10004988 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 bma.wasdel = wasdelay;
4990 bma.minlen = minlen;
4991 bma.low = flist->xbf_low;
4992 bma.minleft = minleft;
4993 /*
4994 * Only want to do the alignment at the
4995 * eof if it is userdata and allocation length
4996 * is larger than a stripe unit.
4997 */
4998 if (mp->m_dalign && alen >= mp->m_dalign &&
Nathan Scott39269e22006-03-14 13:33:50 +11004999 (!(flags & XFS_BMAPI_METADATA)) &&
5000 (whichfork == XFS_DATA_FORK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 if ((error = xfs_bmap_isaeof(ip, aoff,
5002 whichfork, &bma.aeof)))
5003 goto error0;
5004 } else
5005 bma.aeof = 0;
5006 /*
5007 * Call allocator.
5008 */
5009 if ((error = xfs_bmap_alloc(&bma)))
5010 goto error0;
5011 /*
5012 * Copy out result fields.
5013 */
5014 abno = bma.rval;
5015 if ((flist->xbf_low = bma.low))
5016 minleft = 0;
5017 alen = bma.alen;
5018 aoff = bma.off;
5019 ASSERT(*firstblock == NULLFSBLOCK ||
5020 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5021 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
5022 (flist->xbf_low &&
5023 XFS_FSB_TO_AGNO(mp, *firstblock) <
5024 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
5025 *firstblock = bma.firstblock;
5026 if (cur)
5027 cur->bc_private.b.firstblock =
5028 *firstblock;
5029 if (abno == NULLFSBLOCK)
5030 break;
5031 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5032 cur = xfs_btree_init_cursor(mp,
5033 tp, NULL, 0, XFS_BTNUM_BMAP,
5034 ip, whichfork);
5035 cur->bc_private.b.firstblock =
5036 *firstblock;
5037 cur->bc_private.b.flist = flist;
5038 }
5039 /*
5040 * Bump the number of extents we've allocated
5041 * in this call.
5042 */
5043 nallocs++;
5044 }
5045 if (cur)
5046 cur->bc_private.b.flags =
5047 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
5048 got.br_startoff = aoff;
5049 got.br_startblock = abno;
5050 got.br_blockcount = alen;
5051 got.br_state = XFS_EXT_NORM; /* assume normal */
5052 /*
5053 * Determine state of extent, and the filesystem.
5054 * A wasdelay extent has been initialized, so
5055 * shouldn't be flagged as unwritten.
5056 */
Eric Sandeen62118702008-03-06 13:44:28 +11005057 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5059 got.br_state = XFS_EXT_UNWRITTEN;
5060 }
5061 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005062 firstblock, flist, &tmp_logflags, delta,
5063 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 logflags |= tmp_logflags;
5065 if (error)
5066 goto error0;
5067 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005068 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5070 xfs_bmbt_get_all(ep, &got);
5071 ASSERT(got.br_startoff <= aoff);
5072 ASSERT(got.br_startoff + got.br_blockcount >=
5073 aoff + alen);
5074#ifdef DEBUG
Nathan Scott39269e22006-03-14 13:33:50 +11005075 if (flags & XFS_BMAPI_DELAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
5077 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
5078 }
5079 ASSERT(got.br_state == XFS_EXT_NORM ||
5080 got.br_state == XFS_EXT_UNWRITTEN);
5081#endif
5082 /*
5083 * Fall down into the found allocated space case.
5084 */
5085 } else if (inhole) {
5086 /*
5087 * Reading in a hole.
5088 */
5089 mval->br_startoff = bno;
5090 mval->br_startblock = HOLESTARTBLOCK;
5091 mval->br_blockcount =
5092 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5093 mval->br_state = XFS_EXT_NORM;
5094 bno += mval->br_blockcount;
5095 len -= mval->br_blockcount;
5096 mval++;
5097 n++;
5098 continue;
5099 }
5100 /*
5101 * Then deal with the allocated space we found.
5102 */
5103 ASSERT(ep != NULL);
Nathan Scott39269e22006-03-14 13:33:50 +11005104 if (!(flags & XFS_BMAPI_ENTIRE) &&
5105 (got.br_startoff + got.br_blockcount > obno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 if (obno > bno)
5107 bno = obno;
5108 ASSERT((bno >= obno) || (n == 0));
5109 ASSERT(bno < end);
5110 mval->br_startoff = bno;
5111 if (ISNULLSTARTBLOCK(got.br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11005112 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 mval->br_startblock = DELAYSTARTBLOCK;
5114 } else
5115 mval->br_startblock =
5116 got.br_startblock +
5117 (bno - got.br_startoff);
5118 /*
5119 * Return the minimum of what we got and what we
5120 * asked for for the length. We can use the len
5121 * variable here because it is modified below
5122 * and we could have been there before coming
5123 * here if the first part of the allocation
5124 * didn't overlap what was asked for.
5125 */
5126 mval->br_blockcount =
5127 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5128 (bno - got.br_startoff));
5129 mval->br_state = got.br_state;
5130 ASSERT(mval->br_blockcount <= len);
5131 } else {
5132 *mval = got;
5133 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11005134 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 mval->br_startblock = DELAYSTARTBLOCK;
5136 }
5137 }
5138
5139 /*
5140 * Check if writing previously allocated but
5141 * unwritten extents.
5142 */
5143 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5144 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5145 /*
5146 * Modify (by adding) the state flag, if writing.
5147 */
5148 ASSERT(mval->br_blockcount <= len);
5149 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5150 cur = xfs_btree_init_cursor(mp,
5151 tp, NULL, 0, XFS_BTNUM_BMAP,
5152 ip, whichfork);
5153 cur->bc_private.b.firstblock =
5154 *firstblock;
5155 cur->bc_private.b.flist = flist;
5156 }
5157 mval->br_state = XFS_EXT_NORM;
5158 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005159 firstblock, flist, &tmp_logflags, delta,
5160 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 logflags |= tmp_logflags;
5162 if (error)
5163 goto error0;
5164 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005165 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5167 xfs_bmbt_get_all(ep, &got);
5168 /*
5169 * We may have combined previously unwritten
5170 * space with written space, so generate
5171 * another request.
5172 */
5173 if (mval->br_blockcount < len)
5174 continue;
5175 }
5176
Nathan Scott39269e22006-03-14 13:33:50 +11005177 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 ((mval->br_startoff + mval->br_blockcount) <= end));
Nathan Scott39269e22006-03-14 13:33:50 +11005179 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5180 (mval->br_blockcount <= len) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 (mval->br_startoff < obno));
5182 bno = mval->br_startoff + mval->br_blockcount;
5183 len = end - bno;
5184 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5185 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5186 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5187 ASSERT(mval->br_state == mval[-1].br_state);
5188 mval[-1].br_blockcount = mval->br_blockcount;
5189 mval[-1].br_state = mval->br_state;
5190 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5191 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5192 mval[-1].br_startblock != HOLESTARTBLOCK &&
5193 mval->br_startblock ==
5194 mval[-1].br_startblock + mval[-1].br_blockcount &&
Nathan Scott39269e22006-03-14 13:33:50 +11005195 ((flags & XFS_BMAPI_IGSTATE) ||
5196 mval[-1].br_state == mval->br_state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 ASSERT(mval->br_startoff ==
5198 mval[-1].br_startoff + mval[-1].br_blockcount);
5199 mval[-1].br_blockcount += mval->br_blockcount;
5200 } else if (n > 0 &&
5201 mval->br_startblock == DELAYSTARTBLOCK &&
5202 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5203 mval->br_startoff ==
5204 mval[-1].br_startoff + mval[-1].br_blockcount) {
5205 mval[-1].br_blockcount += mval->br_blockcount;
5206 mval[-1].br_state = mval->br_state;
5207 } else if (!((n == 0) &&
5208 ((mval->br_startoff + mval->br_blockcount) <=
5209 obno))) {
5210 mval++;
5211 n++;
5212 }
5213 /*
5214 * If we're done, stop now. Stop when we've allocated
5215 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5216 * the transaction may get too big.
5217 */
5218 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5219 break;
5220 /*
5221 * Else go on to the next record.
5222 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005223 ep = xfs_iext_get_ext(ifp, ++lastx);
David Chinner4e5ae832007-06-05 16:24:15 +10005224 prev = got;
5225 if (lastx >= nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 eof = 1;
David Chinner4e5ae832007-06-05 16:24:15 +10005227 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 xfs_bmbt_get_all(ep, &got);
5229 }
5230 ifp->if_lastex = lastx;
5231 *nmap = n;
5232 /*
5233 * Transform from btree to extents, give it cur.
5234 */
5235 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5236 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5237 ASSERT(wr && cur);
5238 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5239 &tmp_logflags, whichfork);
5240 logflags |= tmp_logflags;
5241 if (error)
5242 goto error0;
5243 }
5244 ASSERT(ifp->if_ext_max ==
5245 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5246 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5247 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5248 error = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005249 if (delta && delta->xed_startoff != NULLFILEOFF) {
5250 /* A change was actually made.
5251 * Note that delta->xed_blockount is an offset at this
5252 * point and needs to be converted to a block count.
5253 */
5254 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5255 delta->xed_blockcount -= delta->xed_startoff;
5256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257error0:
5258 /*
5259 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005260 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 */
5262 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5263 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5264 logflags &= ~XFS_ILOG_FEXT(whichfork);
5265 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5266 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5267 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5268 /*
5269 * Log whatever the flags say, even if error. Otherwise we might miss
5270 * detecting a case where the data is changed, there's an error,
5271 * and it's not logged so we don't shutdown when we should.
5272 */
5273 if (logflags) {
5274 ASSERT(tp && wr);
5275 xfs_trans_log_inode(tp, ip, logflags);
5276 }
5277 if (cur) {
5278 if (!error) {
5279 ASSERT(*firstblock == NULLFSBLOCK ||
5280 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5281 XFS_FSB_TO_AGNO(mp,
5282 cur->bc_private.b.firstblock) ||
5283 (flist->xbf_low &&
5284 XFS_FSB_TO_AGNO(mp, *firstblock) <
5285 XFS_FSB_TO_AGNO(mp,
5286 cur->bc_private.b.firstblock)));
5287 *firstblock = cur->bc_private.b.firstblock;
5288 }
5289 xfs_btree_del_cursor(cur,
5290 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5291 }
5292 if (!error)
5293 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5294 orig_nmap, *nmap);
5295 return error;
5296}
5297
5298/*
5299 * Map file blocks to filesystem blocks, simple version.
5300 * One block (extent) only, read-only.
5301 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5302 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5303 * was set and all the others were clear.
5304 */
5305int /* error */
5306xfs_bmapi_single(
5307 xfs_trans_t *tp, /* transaction pointer */
5308 xfs_inode_t *ip, /* incore inode */
5309 int whichfork, /* data or attr fork */
5310 xfs_fsblock_t *fsb, /* output: mapped block */
5311 xfs_fileoff_t bno) /* starting file offs. mapped */
5312{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005313 int eof; /* we've hit the end of extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005315 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 xfs_ifork_t *ifp; /* inode fork pointer */
5317 xfs_extnum_t lastx; /* last useful extent number */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005318 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319
5320 ifp = XFS_IFORK_PTR(ip, whichfork);
5321 if (unlikely(
5322 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5323 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5324 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5325 ip->i_mount);
5326 return XFS_ERROR(EFSCORRUPTED);
5327 }
5328 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5329 return XFS_ERROR(EIO);
5330 XFS_STATS_INC(xs_blk_mapr);
5331 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5332 (error = xfs_iread_extents(tp, ip, whichfork)))
5333 return error;
5334 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5335 &prev);
5336 /*
5337 * Reading past eof, act as though there's a hole
5338 * up to end.
5339 */
5340 if (eof || got.br_startoff > bno) {
5341 *fsb = NULLFSBLOCK;
5342 return 0;
5343 }
5344 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
5345 ASSERT(bno < got.br_startoff + got.br_blockcount);
5346 *fsb = got.br_startblock + (bno - got.br_startoff);
5347 ifp->if_lastex = lastx;
5348 return 0;
5349}
5350
5351/*
5352 * Unmap (remove) blocks from a file.
5353 * If nexts is nonzero then the number of extents to remove is limited to
5354 * that value. If not all extents in the block range can be removed then
5355 * *done is set.
5356 */
5357int /* error */
5358xfs_bunmapi(
5359 xfs_trans_t *tp, /* transaction pointer */
5360 struct xfs_inode *ip, /* incore inode */
5361 xfs_fileoff_t bno, /* starting offset to unmap */
5362 xfs_filblks_t len, /* length to unmap in file */
5363 int flags, /* misc flags */
5364 xfs_extnum_t nexts, /* number of extents max */
5365 xfs_fsblock_t *firstblock, /* first allocated block
5366 controls a.g. for allocs */
5367 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005368 xfs_extdelta_t *delta, /* o: change made to incore
5369 extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 int *done) /* set if not done yet */
5371{
5372 xfs_btree_cur_t *cur; /* bmap btree cursor */
5373 xfs_bmbt_irec_t del; /* extent being deleted */
5374 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005375 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 int error; /* error return value */
5377 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005378 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 xfs_ifork_t *ifp; /* inode fork pointer */
5380 int isrt; /* freeing in rt area */
5381 xfs_extnum_t lastx; /* last extent index used */
5382 int logflags; /* transaction logging flags */
5383 xfs_extlen_t mod; /* rt extent offset */
5384 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005385 xfs_extnum_t nextents; /* number of file extents */
5386 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 xfs_fileoff_t start; /* first file offset deleted */
5388 int tmp_logflags; /* partial logging flags */
5389 int wasdel; /* was a delayed alloc extent */
5390 int whichfork; /* data or attribute fork */
5391 int rsvd; /* OK to allocate reserved blocks */
5392 xfs_fsblock_t sum;
5393
5394 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5395 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5396 XFS_ATTR_FORK : XFS_DATA_FORK;
5397 ifp = XFS_IFORK_PTR(ip, whichfork);
5398 if (unlikely(
5399 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5400 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5401 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5402 ip->i_mount);
5403 return XFS_ERROR(EFSCORRUPTED);
5404 }
5405 mp = ip->i_mount;
5406 if (XFS_FORCED_SHUTDOWN(mp))
5407 return XFS_ERROR(EIO);
5408 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5409 ASSERT(len > 0);
5410 ASSERT(nexts >= 0);
5411 ASSERT(ifp->if_ext_max ==
5412 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5413 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5414 (error = xfs_iread_extents(tp, ip, whichfork)))
5415 return error;
5416 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5417 if (nextents == 0) {
5418 *done = 1;
5419 return 0;
5420 }
5421 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005422 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 start = bno;
5424 bno = start + len - 1;
5425 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5426 &prev);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005427 if (delta) {
5428 delta->xed_startoff = NULLFILEOFF;
5429 delta->xed_blockcount = 0;
5430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 /*
5432 * Check to see if the given block number is past the end of the
5433 * file, back up to the last block if so...
5434 */
5435 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005436 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 xfs_bmbt_get_all(ep, &got);
5438 bno = got.br_startoff + got.br_blockcount - 1;
5439 }
5440 logflags = 0;
5441 if (ifp->if_flags & XFS_IFBROOT) {
5442 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5443 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
5444 whichfork);
5445 cur->bc_private.b.firstblock = *firstblock;
5446 cur->bc_private.b.flist = flist;
5447 cur->bc_private.b.flags = 0;
5448 } else
5449 cur = NULL;
5450 extno = 0;
5451 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5452 (nexts == 0 || extno < nexts)) {
5453 /*
5454 * Is the found extent after a hole in which bno lives?
5455 * Just back up to the previous extent, if so.
5456 */
5457 if (got.br_startoff > bno) {
5458 if (--lastx < 0)
5459 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005460 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 xfs_bmbt_get_all(ep, &got);
5462 }
5463 /*
5464 * Is the last block of this extent before the range
5465 * we're supposed to delete? If so, we're done.
5466 */
5467 bno = XFS_FILEOFF_MIN(bno,
5468 got.br_startoff + got.br_blockcount - 1);
5469 if (bno < start)
5470 break;
5471 /*
5472 * Then deal with the (possibly delayed) allocated space
5473 * we found.
5474 */
5475 ASSERT(ep != NULL);
5476 del = got;
5477 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
5478 if (got.br_startoff < start) {
5479 del.br_startoff = start;
5480 del.br_blockcount -= start - got.br_startoff;
5481 if (!wasdel)
5482 del.br_startblock += start - got.br_startoff;
5483 }
5484 if (del.br_startoff + del.br_blockcount > bno + 1)
5485 del.br_blockcount = bno + 1 - del.br_startoff;
5486 sum = del.br_startblock + del.br_blockcount;
5487 if (isrt &&
5488 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5489 /*
5490 * Realtime extent not lined up at the end.
5491 * The extent could have been split into written
5492 * and unwritten pieces, or we could just be
5493 * unmapping part of it. But we can't really
5494 * get rid of part of a realtime extent.
5495 */
5496 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005497 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 /*
5499 * This piece is unwritten, or we're not
5500 * using unwritten extents. Skip over it.
5501 */
5502 ASSERT(bno >= mod);
5503 bno -= mod > del.br_blockcount ?
5504 del.br_blockcount : mod;
5505 if (bno < got.br_startoff) {
5506 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005507 xfs_bmbt_get_all(xfs_iext_get_ext(
5508 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 }
5510 continue;
5511 }
5512 /*
5513 * It's written, turn it unwritten.
5514 * This is better than zeroing it.
5515 */
5516 ASSERT(del.br_state == XFS_EXT_NORM);
5517 ASSERT(xfs_trans_get_block_res(tp) > 0);
5518 /*
5519 * If this spans a realtime extent boundary,
5520 * chop it back to the start of the one we end at.
5521 */
5522 if (del.br_blockcount > mod) {
5523 del.br_startoff += del.br_blockcount - mod;
5524 del.br_startblock += del.br_blockcount - mod;
5525 del.br_blockcount = mod;
5526 }
5527 del.br_state = XFS_EXT_UNWRITTEN;
5528 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005529 firstblock, flist, &logflags, delta,
5530 XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 if (error)
5532 goto error0;
5533 goto nodelete;
5534 }
5535 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5536 /*
5537 * Realtime extent is lined up at the end but not
5538 * at the front. We'll get rid of full extents if
5539 * we can.
5540 */
5541 mod = mp->m_sb.sb_rextsize - mod;
5542 if (del.br_blockcount > mod) {
5543 del.br_blockcount -= mod;
5544 del.br_startoff += mod;
5545 del.br_startblock += mod;
5546 } else if ((del.br_startoff == start &&
5547 (del.br_state == XFS_EXT_UNWRITTEN ||
5548 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005549 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 /*
5551 * Can't make it unwritten. There isn't
5552 * a full extent here so just skip it.
5553 */
5554 ASSERT(bno >= del.br_blockcount);
5555 bno -= del.br_blockcount;
5556 if (bno < got.br_startoff) {
5557 if (--lastx >= 0)
5558 xfs_bmbt_get_all(--ep, &got);
5559 }
5560 continue;
5561 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5562 /*
5563 * This one is already unwritten.
5564 * It must have a written left neighbor.
5565 * Unwrite the killed part of that one and
5566 * try again.
5567 */
5568 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005569 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5570 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 ASSERT(prev.br_state == XFS_EXT_NORM);
5572 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
5573 ASSERT(del.br_startblock ==
5574 prev.br_startblock + prev.br_blockcount);
5575 if (prev.br_startoff < start) {
5576 mod = start - prev.br_startoff;
5577 prev.br_blockcount -= mod;
5578 prev.br_startblock += mod;
5579 prev.br_startoff = start;
5580 }
5581 prev.br_state = XFS_EXT_UNWRITTEN;
5582 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5583 &prev, firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005584 delta, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 if (error)
5586 goto error0;
5587 goto nodelete;
5588 } else {
5589 ASSERT(del.br_state == XFS_EXT_NORM);
5590 del.br_state = XFS_EXT_UNWRITTEN;
5591 error = xfs_bmap_add_extent(ip, lastx, &cur,
5592 &del, firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005593 delta, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 if (error)
5595 goto error0;
5596 goto nodelete;
5597 }
5598 }
5599 if (wasdel) {
5600 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005601 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005602 if (isrt) {
5603 xfs_filblks_t rtexts;
5604
5605 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5606 do_div(rtexts, mp->m_sb.sb_rextsize);
5607 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005608 (int64_t)rtexts, rsvd);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005609 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5610 NULL, ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005611 XFS_QMOPT_RES_RTBLKS);
5612 } else {
5613 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005614 (int64_t)del.br_blockcount, rsvd);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005615 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5616 NULL, ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 ip->i_delayed_blks -= del.br_blockcount;
5620 if (cur)
5621 cur->bc_private.b.flags |=
5622 XFS_BTCUR_BPRV_WASDEL;
5623 } else if (cur)
5624 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5625 /*
5626 * If it's the case where the directory code is running
5627 * with no block reservation, and the deleted block is in
5628 * the middle of its extent, and the resulting insert
5629 * of an extent would cause transformation to btree format,
5630 * then reject it. The calling code will then swap
5631 * blocks around instead.
5632 * We have to do this now, rather than waiting for the
5633 * conversion to btree format, since the transaction
5634 * will be dirty.
5635 */
5636 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5637 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5638 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5639 del.br_startoff > got.br_startoff &&
5640 del.br_startoff + del.br_blockcount <
5641 got.br_startoff + got.br_blockcount) {
5642 error = XFS_ERROR(ENOSPC);
5643 goto error0;
5644 }
5645 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005646 &tmp_logflags, delta, whichfork, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 logflags |= tmp_logflags;
5648 if (error)
5649 goto error0;
5650 bno = del.br_startoff - 1;
5651nodelete:
5652 lastx = ifp->if_lastex;
5653 /*
5654 * If not done go on to the next (previous) record.
5655 * Reset ep in case the extents array was re-alloced.
5656 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005657 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5659 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5660 xfs_bmbt_get_startoff(ep) > bno) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005661 if (--lastx >= 0)
5662 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 }
5664 if (lastx >= 0)
5665 xfs_bmbt_get_all(ep, &got);
5666 extno++;
5667 }
5668 }
5669 ifp->if_lastex = lastx;
5670 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5671 ASSERT(ifp->if_ext_max ==
5672 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5673 /*
5674 * Convert to a btree if necessary.
5675 */
5676 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5677 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5678 ASSERT(cur == NULL);
5679 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5680 &cur, 0, &tmp_logflags, whichfork);
5681 logflags |= tmp_logflags;
5682 if (error)
5683 goto error0;
5684 }
5685 /*
5686 * transform from btree to extents, give it cur
5687 */
5688 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5689 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5690 ASSERT(cur != NULL);
5691 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5692 whichfork);
5693 logflags |= tmp_logflags;
5694 if (error)
5695 goto error0;
5696 }
5697 /*
5698 * transform from extents to local?
5699 */
5700 ASSERT(ifp->if_ext_max ==
5701 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5702 error = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005703 if (delta && delta->xed_startoff != NULLFILEOFF) {
5704 /* A change was actually made.
5705 * Note that delta->xed_blockount is an offset at this
5706 * point and needs to be converted to a block count.
5707 */
5708 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5709 delta->xed_blockcount -= delta->xed_startoff;
5710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711error0:
5712 /*
5713 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005714 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 */
5716 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5717 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5718 logflags &= ~XFS_ILOG_FEXT(whichfork);
5719 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5720 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5721 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5722 /*
5723 * Log inode even in the error case, if the transaction
5724 * is dirty we'll need to shut down the filesystem.
5725 */
5726 if (logflags)
5727 xfs_trans_log_inode(tp, ip, logflags);
5728 if (cur) {
5729 if (!error) {
5730 *firstblock = cur->bc_private.b.firstblock;
5731 cur->bc_private.b.allocated = 0;
5732 }
5733 xfs_btree_del_cursor(cur,
5734 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5735 }
5736 return error;
5737}
5738
5739/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005740 * returns 1 for success, 0 if we failed to map the extent.
5741 */
5742STATIC int
5743xfs_getbmapx_fix_eof_hole(
5744 xfs_inode_t *ip, /* xfs incore inode pointer */
5745 struct getbmap *out, /* output structure */
5746 int prealloced, /* this is a file with
5747 * preallocated data space */
5748 __int64_t end, /* last block requested */
5749 xfs_fsblock_t startblock)
5750{
5751 __int64_t fixlen;
5752 xfs_mount_t *mp; /* file system mount point */
5753
5754 if (startblock == HOLESTARTBLOCK) {
5755 mp = ip->i_mount;
5756 out->bmv_block = -1;
5757 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5758 fixlen -= out->bmv_offset;
5759 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5760 /* Came to hole at EOF. Trim it. */
5761 if (fixlen <= 0)
5762 return 0;
5763 out->bmv_length = fixlen;
5764 }
5765 } else {
5766 out->bmv_block = XFS_FSB_TO_DB(ip, startblock);
5767 }
5768
5769 return 1;
5770}
5771
5772/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 * Fcntl interface to xfs_bmapi.
5774 */
5775int /* error code */
5776xfs_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10005777 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 struct getbmap *bmv, /* user bmap structure */
5779 void __user *ap, /* pointer to user's array */
5780 int interface) /* interface flags */
5781{
5782 __int64_t bmvend; /* last block requested */
5783 int error; /* return value */
5784 __int64_t fixlen; /* length for -1 case */
5785 int i; /* extent number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 int lock; /* lock state */
5787 xfs_bmbt_irec_t *map; /* buffer for user's data */
5788 xfs_mount_t *mp; /* file system mount point */
5789 int nex; /* # of user extents can do */
5790 int nexleft; /* # of user extents left */
5791 int subnex; /* # of bmapi's can do */
5792 int nmap; /* number of map entries */
5793 struct getbmap out; /* output structure */
5794 int whichfork; /* data or attr fork */
5795 int prealloced; /* this is a file with
5796 * preallocated data space */
5797 int sh_unwritten; /* true, if unwritten */
5798 /* extents listed separately */
5799 int bmapi_flags; /* flags for xfs_bmapi */
5800 __int32_t oflags; /* getbmapx bmv_oflags field */
5801
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 mp = ip->i_mount;
5803
5804 whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5805 sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
5806
5807 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5808 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5809 * bit is set for the file, generate a read event in order
5810 * that the DMAPI application may do its thing before we return
5811 * the extents. Usually this means restoring user file data to
5812 * regions of the file that look like holes.
5813 *
5814 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5815 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5816 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5817 * could misinterpret holes in a DMAPI file as true holes,
5818 * when in fact they may represent offline user data.
5819 */
Christoph Hellwigeb9df392007-08-16 18:42:07 +10005820 if ((interface & BMV_IF_NO_DMAPI_READ) == 0 &&
5821 DM_EVENT_ENABLED(ip, DM_EVENT_READ) &&
5822 whichfork == XFS_DATA_FORK) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11005823 error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, 0, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 if (error)
5825 return XFS_ERROR(error);
5826 }
5827
5828 if (whichfork == XFS_ATTR_FORK) {
5829 if (XFS_IFORK_Q(ip)) {
5830 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5831 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5832 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5833 return XFS_ERROR(EINVAL);
5834 } else if (unlikely(
5835 ip->i_d.di_aformat != 0 &&
5836 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5837 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5838 ip->i_mount);
5839 return XFS_ERROR(EFSCORRUPTED);
5840 }
5841 } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5842 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5843 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5844 return XFS_ERROR(EINVAL);
5845 if (whichfork == XFS_DATA_FORK) {
David Chinner957d0eb2007-06-18 16:50:37 +10005846 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005847 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 prealloced = 1;
5849 fixlen = XFS_MAXIOFFSET(mp);
5850 } else {
5851 prealloced = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005852 fixlen = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 }
5854 } else {
5855 prealloced = 0;
5856 fixlen = 1LL << 32;
5857 }
5858
5859 if (bmv->bmv_length == -1) {
5860 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5861 bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
5862 (__int64_t)0);
5863 } else if (bmv->bmv_length < 0)
5864 return XFS_ERROR(EINVAL);
5865 if (bmv->bmv_length == 0) {
5866 bmv->bmv_entries = 0;
5867 return 0;
5868 }
5869 nex = bmv->bmv_count - 1;
5870 if (nex <= 0)
5871 return XFS_ERROR(EINVAL);
5872 bmvend = bmv->bmv_offset + bmv->bmv_length;
5873
5874 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5875
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005876 if (whichfork == XFS_DATA_FORK &&
5877 (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
Christoph Hellwig739bfb22007-08-29 10:58:01 +10005879 error = xfs_flush_pages(ip, (xfs_off_t)0,
5880 -1, 0, FI_REMAPF);
Niv Sardie12070a2008-03-06 13:43:03 +11005881 if (error) {
5882 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5883 return error;
5884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 }
5886
5887 ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
5888
5889 lock = xfs_ilock_map_shared(ip);
5890
5891 /*
5892 * Don't let nex be bigger than the number of extents
5893 * we can have assuming alternating holes and real extents.
5894 */
5895 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5896 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5897
5898 bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
5899 ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
5900
5901 /*
5902 * Allocate enough space to handle "subnex" maps at a time.
5903 */
5904 subnex = 16;
5905 map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
5906
5907 bmv->bmv_entries = 0;
5908
5909 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
5910 error = 0;
5911 goto unlock_and_return;
5912 }
5913
5914 nexleft = nex;
5915
5916 do {
5917 nmap = (nexleft > subnex) ? subnex : nexleft;
5918 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5919 XFS_BB_TO_FSB(mp, bmv->bmv_length),
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005920 bmapi_flags, NULL, 0, map, &nmap,
5921 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 if (error)
5923 goto unlock_and_return;
5924 ASSERT(nmap <= subnex);
5925
5926 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5927 nexleft--;
5928 oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
5929 BMV_OF_PREALLOC : 0;
5930 out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
5931 out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5932 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
Yingping Lu9af0a702005-11-02 15:09:54 +11005933 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005934 whichfork == XFS_ATTR_FORK) {
5935 /* came to the end of attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 goto unlock_and_return;
5937 } else {
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005938 if (!xfs_getbmapx_fix_eof_hole(ip, &out,
5939 prealloced, bmvend,
5940 map[i].br_startblock)) {
5941 goto unlock_and_return;
5942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943
5944 /* return either getbmap/getbmapx structure. */
5945 if (interface & BMV_IF_EXTENDED) {
5946 struct getbmapx outx;
5947
5948 GETBMAP_CONVERT(out,outx);
5949 outx.bmv_oflags = oflags;
5950 outx.bmv_unused1 = outx.bmv_unused2 = 0;
5951 if (copy_to_user(ap, &outx,
5952 sizeof(outx))) {
5953 error = XFS_ERROR(EFAULT);
5954 goto unlock_and_return;
5955 }
5956 } else {
5957 if (copy_to_user(ap, &out,
5958 sizeof(out))) {
5959 error = XFS_ERROR(EFAULT);
5960 goto unlock_and_return;
5961 }
5962 }
5963 bmv->bmv_offset =
5964 out.bmv_offset + out.bmv_length;
5965 bmv->bmv_length = MAX((__int64_t)0,
5966 (__int64_t)(bmvend - bmv->bmv_offset));
5967 bmv->bmv_entries++;
5968 ap = (interface & BMV_IF_EXTENDED) ?
5969 (void __user *)
5970 ((struct getbmapx __user *)ap + 1) :
5971 (void __user *)
5972 ((struct getbmap __user *)ap + 1);
5973 }
5974 }
5975 } while (nmap && nexleft && bmv->bmv_length);
5976
5977unlock_and_return:
5978 xfs_iunlock_map_shared(ip, lock);
5979 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5980
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10005981 kmem_free(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
5983 return error;
5984}
5985
5986/*
5987 * Check the last inode extent to determine whether this allocation will result
5988 * in blocks being allocated at the end of the file. When we allocate new data
5989 * blocks at the end of the file which do not start at the previous data block,
5990 * we will try to align the new blocks at stripe unit boundaries.
5991 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10005992STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993xfs_bmap_isaeof(
5994 xfs_inode_t *ip, /* incore inode pointer */
5995 xfs_fileoff_t off, /* file offset in fsblocks */
5996 int whichfork, /* data or attribute fork */
5997 char *aeof) /* return value */
5998{
5999 int error; /* error return value */
6000 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006001 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006002 xfs_extnum_t nextents; /* number of file extents */
6003 xfs_bmbt_irec_t s; /* expanded extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004
6005 ASSERT(whichfork == XFS_DATA_FORK);
6006 ifp = XFS_IFORK_PTR(ip, whichfork);
6007 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6008 (error = xfs_iread_extents(NULL, ip, whichfork)))
6009 return error;
6010 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6011 if (nextents == 0) {
6012 *aeof = 1;
6013 return 0;
6014 }
6015 /*
6016 * Go to the last extent
6017 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006018 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 xfs_bmbt_get_all(lastrec, &s);
6020 /*
6021 * Check we are allocating in the last extent (for delayed allocations)
6022 * or past the last extent for non-delayed allocations.
6023 */
6024 *aeof = (off >= s.br_startoff &&
6025 off < s.br_startoff + s.br_blockcount &&
6026 ISNULLSTARTBLOCK(s.br_startblock)) ||
6027 off >= s.br_startoff + s.br_blockcount;
6028 return 0;
6029}
6030
6031/*
6032 * Check if the endoff is outside the last extent. If so the caller will grow
6033 * the allocation to a stripe unit boundary.
6034 */
6035int /* error */
6036xfs_bmap_eof(
6037 xfs_inode_t *ip, /* incore inode pointer */
6038 xfs_fileoff_t endoff, /* file offset in fsblocks */
6039 int whichfork, /* data or attribute fork */
6040 int *eof) /* result value */
6041{
6042 xfs_fsblock_t blockcount; /* extent block count */
6043 int error; /* error return value */
6044 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006045 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006046 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 xfs_fileoff_t startoff; /* extent starting file offset */
6048
6049 ASSERT(whichfork == XFS_DATA_FORK);
6050 ifp = XFS_IFORK_PTR(ip, whichfork);
6051 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6052 (error = xfs_iread_extents(NULL, ip, whichfork)))
6053 return error;
6054 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6055 if (nextents == 0) {
6056 *eof = 1;
6057 return 0;
6058 }
6059 /*
6060 * Go to the last extent
6061 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006062 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 startoff = xfs_bmbt_get_startoff(lastrec);
6064 blockcount = xfs_bmbt_get_blockcount(lastrec);
6065 *eof = endoff >= startoff + blockcount;
6066 return 0;
6067}
6068
6069#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070STATIC
6071xfs_buf_t *
6072xfs_bmap_get_bp(
6073 xfs_btree_cur_t *cur,
6074 xfs_fsblock_t bno)
6075{
6076 int i;
6077 xfs_buf_t *bp;
6078
6079 if (!cur)
6080 return(NULL);
6081
6082 bp = NULL;
6083 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6084 bp = cur->bc_bufs[i];
6085 if (!bp) break;
6086 if (XFS_BUF_ADDR(bp) == bno)
6087 break; /* Found it */
6088 }
6089 if (i == XFS_BTREE_MAXLEVELS)
6090 bp = NULL;
6091
6092 if (!bp) { /* Chase down all the log items to see if the bp is there */
6093 xfs_log_item_chunk_t *licp;
6094 xfs_trans_t *tp;
6095
6096 tp = cur->bc_tp;
6097 licp = &tp->t_items;
6098 while (!bp && licp != NULL) {
Eric Sandeen39dab9d2008-08-13 16:10:52 +10006099 if (xfs_lic_are_all_free(licp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 licp = licp->lic_next;
6101 continue;
6102 }
6103 for (i = 0; i < licp->lic_unused; i++) {
6104 xfs_log_item_desc_t *lidp;
6105 xfs_log_item_t *lip;
6106 xfs_buf_log_item_t *bip;
6107 xfs_buf_t *lbp;
6108
Eric Sandeen39dab9d2008-08-13 16:10:52 +10006109 if (xfs_lic_isfree(licp, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 continue;
6111 }
6112
Eric Sandeen39dab9d2008-08-13 16:10:52 +10006113 lidp = xfs_lic_slot(licp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 lip = lidp->lid_item;
6115 if (lip->li_type != XFS_LI_BUF)
6116 continue;
6117
6118 bip = (xfs_buf_log_item_t *)lip;
6119 lbp = bip->bli_buf;
6120
6121 if (XFS_BUF_ADDR(lbp) == bno) {
6122 bp = lbp;
6123 break; /* Found it */
6124 }
6125 }
6126 licp = licp->lic_next;
6127 }
6128 }
6129 return(bp);
6130}
6131
6132void
6133xfs_check_block(
6134 xfs_bmbt_block_t *block,
6135 xfs_mount_t *mp,
6136 int root,
6137 short sz)
6138{
6139 int i, j, dmxr;
Christoph Hellwig576039c2006-09-28 10:58:06 +10006140 __be64 *pp, *thispa; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 xfs_bmbt_key_t *prevp, *keyp;
6142
Christoph Hellwig16259e72005-11-02 15:11:25 +11006143 ASSERT(be16_to_cpu(block->bb_level) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144
6145 prevp = NULL;
Christoph Hellwig16259e72005-11-02 15:11:25 +11006146 for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 dmxr = mp->m_bmap_dmxr[0];
6148
6149 if (root) {
6150 keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
6151 } else {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006152 keyp = XFS_BTREE_KEY_ADDR(xfs_bmbt, block, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153 }
6154
6155 if (prevp) {
6156 xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
6157 }
6158 prevp = keyp;
6159
6160 /*
6161 * Compare the block numbers to see if there are dups.
6162 */
6163
6164 if (root) {
6165 pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
6166 } else {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006167 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, i, dmxr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11006169 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 if (root) {
6171 thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
6172 } else {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006173 thispa = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, j,
6174 dmxr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 }
Christoph Hellwig576039c2006-09-28 10:58:06 +10006176 if (*thispa == *pp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
Harvey Harrison34a622b2008-04-10 12:19:21 +10006178 __func__, j, i,
Christoph Hellwig576039c2006-09-28 10:58:06 +10006179 (unsigned long long)be64_to_cpu(*thispa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180 panic("%s: ptrs are equal in node\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +10006181 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 }
6183 }
6184 }
6185}
6186
6187/*
6188 * Check that the extents for the inode ip are in the right order in all
6189 * btree leaves.
6190 */
6191
6192STATIC void
6193xfs_bmap_check_leaf_extents(
6194 xfs_btree_cur_t *cur, /* btree cursor or null */
6195 xfs_inode_t *ip, /* incore inode pointer */
6196 int whichfork) /* data or attr fork */
6197{
6198 xfs_bmbt_block_t *block; /* current btree block */
6199 xfs_fsblock_t bno; /* block # of "block" */
6200 xfs_buf_t *bp; /* buffer for "block" */
6201 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006202 xfs_extnum_t i=0, j; /* index into the extents list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 xfs_ifork_t *ifp; /* fork structure */
6204 int level; /* btree level, for checking */
6205 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10006206 __be64 *pp; /* pointer to block address */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006207 xfs_bmbt_rec_t *ep; /* pointer to current extent */
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006208 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006209 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 int bp_release = 0;
6211
6212 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6213 return;
6214 }
6215
6216 bno = NULLFSBLOCK;
6217 mp = ip->i_mount;
6218 ifp = XFS_IFORK_PTR(ip, whichfork);
6219 block = ifp->if_broot;
6220 /*
6221 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6222 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11006223 level = be16_to_cpu(block->bb_level);
6224 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6226 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006227 bno = be64_to_cpu(*pp);
6228
6229 ASSERT(bno != NULLDFSBNO);
6230 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6231 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6232
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 /*
6234 * Go down the tree until leaf level is reached, following the first
6235 * pointer (leftmost) at each level.
6236 */
6237 while (level-- > 0) {
6238 /* See if buf is in cur first */
6239 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6240 if (bp) {
6241 bp_release = 0;
6242 } else {
6243 bp_release = 1;
6244 }
6245 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6246 XFS_BMAP_BTREE_REF)))
6247 goto error_norelse;
6248 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6249 XFS_WANT_CORRUPTED_GOTO(
6250 XFS_BMAP_SANITY_CHECK(mp, block, level),
6251 error0);
6252 if (level == 0)
6253 break;
6254
6255 /*
6256 * Check this block for basic sanity (increasing keys and
6257 * no duplicate blocks).
6258 */
6259
6260 xfs_check_block(block, mp, 0, 0);
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006261 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006262 bno = be64_to_cpu(*pp);
6263 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 if (bp_release) {
6265 bp_release = 0;
6266 xfs_trans_brelse(NULL, bp);
6267 }
6268 }
6269
6270 /*
6271 * Here with bp and block set to the leftmost leaf node in the tree.
6272 */
6273 i = 0;
6274
6275 /*
6276 * Loop over all leaf nodes checking that all extents are in the right order.
6277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 xfs_fsblock_t nextbno;
6280 xfs_extnum_t num_recs;
6281
6282
Christoph Hellwig16259e72005-11-02 15:11:25 +11006283 num_recs = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284
6285 /*
6286 * Read-ahead the next leaf block, if any.
6287 */
6288
Christoph Hellwig16259e72005-11-02 15:11:25 +11006289 nextbno = be64_to_cpu(block->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290
6291 /*
6292 * Check all the extents to make sure they are OK.
6293 * If we had a previous block, the last entry should
6294 * conform with the first entry in this one.
6295 */
6296
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006297 ep = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006298 if (i) {
6299 xfs_btree_check_rec(XFS_BTNUM_BMAP, &last, ep);
6300 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006301 for (j = 1; j < num_recs; j++) {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006302 nextp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, j + 1);
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006303 xfs_btree_check_rec(XFS_BTNUM_BMAP, ep, nextp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006304 ep = nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006307 last = *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 i += num_recs;
6309 if (bp_release) {
6310 bp_release = 0;
6311 xfs_trans_brelse(NULL, bp);
6312 }
6313 bno = nextbno;
6314 /*
6315 * If we've reached the end, stop.
6316 */
6317 if (bno == NULLFSBLOCK)
6318 break;
6319
6320 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6321 if (bp) {
6322 bp_release = 0;
6323 } else {
6324 bp_release = 1;
6325 }
6326 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6327 XFS_BMAP_BTREE_REF)))
6328 goto error_norelse;
6329 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6330 }
6331 if (bp_release) {
6332 bp_release = 0;
6333 xfs_trans_brelse(NULL, bp);
6334 }
6335 return;
6336
6337error0:
Harvey Harrison34a622b2008-04-10 12:19:21 +10006338 cmn_err(CE_WARN, "%s: at error0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 if (bp_release)
6340 xfs_trans_brelse(NULL, bp);
6341error_norelse:
6342 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
Harvey Harrison34a622b2008-04-10 12:19:21 +10006343 __func__, i);
6344 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 return;
6346}
6347#endif
6348
6349/*
6350 * Count fsblocks of the given fork.
6351 */
6352int /* error */
6353xfs_bmap_count_blocks(
6354 xfs_trans_t *tp, /* transaction pointer */
6355 xfs_inode_t *ip, /* incore inode */
6356 int whichfork, /* data or attr fork */
6357 int *count) /* out: count of blocks */
6358{
6359 xfs_bmbt_block_t *block; /* current btree block */
6360 xfs_fsblock_t bno; /* block # of "block" */
6361 xfs_ifork_t *ifp; /* fork structure */
6362 int level; /* btree level, for checking */
6363 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10006364 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365
6366 bno = NULLFSBLOCK;
6367 mp = ip->i_mount;
6368 ifp = XFS_IFORK_PTR(ip, whichfork);
6369 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
Ruben Porrasc94312d2008-08-13 16:52:25 +10006370 xfs_bmap_count_leaves(ifp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
Ruben Porrasc94312d2008-08-13 16:52:25 +10006372 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 return 0;
6374 }
6375
6376 /*
6377 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6378 */
6379 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11006380 level = be16_to_cpu(block->bb_level);
6381 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006383 bno = be64_to_cpu(*pp);
6384 ASSERT(bno != NULLDFSBNO);
6385 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6386 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006388 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6390 mp);
6391 return XFS_ERROR(EFSCORRUPTED);
6392 }
6393
6394 return 0;
6395}
6396
6397/*
6398 * Recursively walks each level of a btree
6399 * to count total fsblocks is use.
6400 */
David Chinnera8272ce2007-11-23 16:28:09 +11006401STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402xfs_bmap_count_tree(
6403 xfs_mount_t *mp, /* file system mount point */
6404 xfs_trans_t *tp, /* transaction pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006405 xfs_ifork_t *ifp, /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 xfs_fsblock_t blockno, /* file system block number */
6407 int levelin, /* level in btree */
6408 int *count) /* Count of blocks */
6409{
6410 int error;
6411 xfs_buf_t *bp, *nbp;
6412 int level = levelin;
Christoph Hellwig576039c2006-09-28 10:58:06 +10006413 __be64 *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 xfs_fsblock_t bno = blockno;
6415 xfs_fsblock_t nextbno;
6416 xfs_bmbt_block_t *block, *nextblock;
6417 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418
6419 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6420 return error;
6421 *count += 1;
6422 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6423
6424 if (--level) {
6425 /* Not at node above leafs, count this level of nodes */
Christoph Hellwig16259e72005-11-02 15:11:25 +11006426 nextbno = be64_to_cpu(block->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 while (nextbno != NULLFSBLOCK) {
6428 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6429 0, &nbp, XFS_BMAP_BTREE_REF)))
6430 return error;
6431 *count += 1;
6432 nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11006433 nextbno = be64_to_cpu(nextblock->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434 xfs_trans_brelse(tp, nbp);
6435 }
6436
6437 /* Dive to the next level */
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006438 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006439 bno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 if (unlikely((error =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006441 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442 xfs_trans_brelse(tp, bp);
6443 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6444 XFS_ERRLEVEL_LOW, mp);
6445 return XFS_ERROR(EFSCORRUPTED);
6446 }
6447 xfs_trans_brelse(tp, bp);
6448 } else {
6449 /* count all level 1 nodes and their leaves */
6450 for (;;) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11006451 nextbno = be64_to_cpu(block->bb_rightsib);
6452 numrecs = be16_to_cpu(block->bb_numrecs);
Ruben Porrasc94312d2008-08-13 16:52:25 +10006453 xfs_bmap_disk_count_leaves(0, block, numrecs, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 xfs_trans_brelse(tp, bp);
6455 if (nextbno == NULLFSBLOCK)
6456 break;
6457 bno = nextbno;
6458 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6459 XFS_BMAP_BTREE_REF)))
6460 return error;
6461 *count += 1;
6462 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6463 }
6464 }
6465 return 0;
6466}
6467
6468/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006469 * Count leaf blocks given a range of extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006471STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006473 xfs_ifork_t *ifp,
6474 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 int numrecs,
6476 int *count)
6477{
6478 int b;
6479
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006480 for (b = 0; b < numrecs; b++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006481 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
Yingping Lu91e11082005-11-02 15:10:24 +11006482 *count += xfs_bmbt_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006483 }
Yingping Lu91e11082005-11-02 15:10:24 +11006484}
6485
6486/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006487 * Count leaf blocks given a range of extent records originally
6488 * in btree format.
Yingping Lu91e11082005-11-02 15:10:24 +11006489 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006490STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +11006491xfs_bmap_disk_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006492 xfs_extnum_t idx,
6493 xfs_bmbt_block_t *block,
Yingping Lu91e11082005-11-02 15:10:24 +11006494 int numrecs,
6495 int *count)
6496{
6497 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006498 xfs_bmbt_rec_t *frp;
Yingping Lu91e11082005-11-02 15:10:24 +11006499
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006500 for (b = 1; b <= numrecs; b++) {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006501 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, idx + b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 *count += xfs_bmbt_disk_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504}