blob: 6154ca030724abe6fec4ec9129897105e4092501 [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(
Christoph Hellwig136341b2008-10-30 17:11:40 +1100396 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100397 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +1100398 int numrecs,
399 int *count);
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401/*
402 * Bmap internal routines.
403 */
404
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100405STATIC int /* error */
406xfs_bmbt_lookup_eq(
407 struct xfs_btree_cur *cur,
408 xfs_fileoff_t off,
409 xfs_fsblock_t bno,
410 xfs_filblks_t len,
411 int *stat) /* success/failure */
412{
413 cur->bc_rec.b.br_startoff = off;
414 cur->bc_rec.b.br_startblock = bno;
415 cur->bc_rec.b.br_blockcount = len;
416 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
417}
418
419STATIC int /* error */
420xfs_bmbt_lookup_ge(
421 struct xfs_btree_cur *cur,
422 xfs_fileoff_t off,
423 xfs_fsblock_t bno,
424 xfs_filblks_t len,
425 int *stat) /* success/failure */
426{
427 cur->bc_rec.b.br_startoff = off;
428 cur->bc_rec.b.br_startblock = bno;
429 cur->bc_rec.b.br_blockcount = len;
430 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
431}
432
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100433/*
434* Update the record referred to by cur to the value given
435 * by [off, bno, len, state].
436 * This either works (return 0) or gets an EFSCORRUPTED error.
437 */
438STATIC int
439xfs_bmbt_update(
440 struct xfs_btree_cur *cur,
441 xfs_fileoff_t off,
442 xfs_fsblock_t bno,
443 xfs_filblks_t len,
444 xfs_exntst_t state)
445{
446 union xfs_btree_rec rec;
447
448 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
449 return xfs_btree_update(cur, &rec);
450}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/*
453 * Called from xfs_bmap_add_attrfork to handle btree format files.
454 */
455STATIC int /* error */
456xfs_bmap_add_attrfork_btree(
457 xfs_trans_t *tp, /* transaction pointer */
458 xfs_inode_t *ip, /* incore inode pointer */
459 xfs_fsblock_t *firstblock, /* first block allocated */
460 xfs_bmap_free_t *flist, /* blocks to free at commit */
461 int *flags) /* inode logging flags */
462{
463 xfs_btree_cur_t *cur; /* btree cursor */
464 int error; /* error return value */
465 xfs_mount_t *mp; /* file system mount struct */
466 int stat; /* newroot status */
467
468 mp = ip->i_mount;
469 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
470 *flags |= XFS_ILOG_DBROOT;
471 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100472 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 cur->bc_private.b.flist = flist;
474 cur->bc_private.b.firstblock = *firstblock;
475 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
476 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000477 /* must be at least one entry */
478 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100479 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 goto error0;
481 if (stat == 0) {
482 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
483 return XFS_ERROR(ENOSPC);
484 }
485 *firstblock = cur->bc_private.b.firstblock;
486 cur->bc_private.b.allocated = 0;
487 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
488 }
489 return 0;
490error0:
491 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
492 return error;
493}
494
495/*
496 * Called from xfs_bmap_add_attrfork to handle extents format files.
497 */
498STATIC int /* error */
499xfs_bmap_add_attrfork_extents(
500 xfs_trans_t *tp, /* transaction pointer */
501 xfs_inode_t *ip, /* incore inode pointer */
502 xfs_fsblock_t *firstblock, /* first block allocated */
503 xfs_bmap_free_t *flist, /* blocks to free at commit */
504 int *flags) /* inode logging flags */
505{
506 xfs_btree_cur_t *cur; /* bmap btree cursor */
507 int error; /* error return value */
508
509 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
510 return 0;
511 cur = NULL;
512 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
513 flags, XFS_DATA_FORK);
514 if (cur) {
515 cur->bc_private.b.allocated = 0;
516 xfs_btree_del_cursor(cur,
517 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
518 }
519 return error;
520}
521
522/*
523 * Called from xfs_bmap_add_attrfork to handle local format files.
524 */
525STATIC int /* error */
526xfs_bmap_add_attrfork_local(
527 xfs_trans_t *tp, /* transaction pointer */
528 xfs_inode_t *ip, /* incore inode pointer */
529 xfs_fsblock_t *firstblock, /* first block allocated */
530 xfs_bmap_free_t *flist, /* blocks to free at commit */
531 int *flags) /* inode logging flags */
532{
533 xfs_da_args_t dargs; /* args for dir/attr code */
534 int error; /* error return value */
535 xfs_mount_t *mp; /* mount structure pointer */
536
537 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
538 return 0;
539 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
540 mp = ip->i_mount;
541 memset(&dargs, 0, sizeof(dargs));
542 dargs.dp = ip;
543 dargs.firstblock = firstblock;
544 dargs.flist = flist;
545 dargs.total = mp->m_dirblkfsbs;
546 dargs.whichfork = XFS_DATA_FORK;
547 dargs.trans = tp;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000548 error = xfs_dir2_sf_to_block(&dargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 } else
550 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
551 XFS_DATA_FORK);
552 return error;
553}
554
555/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100556 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * after allocating space (or doing a delayed allocation).
558 */
559STATIC int /* error */
560xfs_bmap_add_extent(
561 xfs_inode_t *ip, /* incore inode pointer */
562 xfs_extnum_t idx, /* extent number to update/insert */
563 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100564 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 xfs_fsblock_t *first, /* pointer to firstblock variable */
566 xfs_bmap_free_t *flist, /* list of extents to be freed */
567 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000568 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 int whichfork, /* data or attr fork */
570 int rsvd) /* OK to use reserved data blocks */
571{
572 xfs_btree_cur_t *cur; /* btree cursor or null */
573 xfs_filblks_t da_new; /* new count del alloc blocks used */
574 xfs_filblks_t da_old; /* old count del alloc blocks used */
575 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 xfs_ifork_t *ifp; /* inode fork ptr */
577 int logflags; /* returned value */
578 xfs_extnum_t nextents; /* number of extents in file now */
579
580 XFS_STATS_INC(xs_add_exlist);
581 cur = *curp;
582 ifp = XFS_IFORK_PTR(ip, whichfork);
583 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
584 ASSERT(idx <= nextents);
585 da_old = da_new = 0;
586 error = 0;
587 /*
588 * This is the first extent added to a new/empty file.
589 * Special case this one, so other routines get to assume there are
590 * already extents in the list.
591 */
592 if (nextents == 0) {
Eric Sandeen3a59c942007-07-11 11:09:47 +1000593 XFS_BMAP_TRACE_INSERT("insert empty", ip, 0, 1, new, NULL,
594 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100595 xfs_iext_insert(ifp, 0, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 ASSERT(cur == NULL);
597 ifp->if_lastex = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -0600598 if (!isnullstartblock(new->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600600 logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 } else
602 logflags = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000603 /* DELTA: single new extent */
604 if (delta) {
605 if (delta->xed_startoff > new->br_startoff)
606 delta->xed_startoff = new->br_startoff;
607 if (delta->xed_blockcount <
608 new->br_startoff + new->br_blockcount)
609 delta->xed_blockcount = new->br_startoff +
610 new->br_blockcount;
611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613 /*
614 * Any kind of new delayed allocation goes here.
615 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600616 else if (isnullstartblock(new->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (cur)
618 ASSERT((cur->bc_private.b.flags &
619 XFS_BTCUR_BPRV_WASDEL) == 0);
Eric Sandeene9ed9d22007-05-08 13:48:56 +1000620 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000621 &logflags, delta, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 goto done;
623 }
624 /*
625 * Real allocation off the end of the file.
626 */
627 else if (idx == nextents) {
628 if (cur)
629 ASSERT((cur->bc_private.b.flags &
630 XFS_BTCUR_BPRV_WASDEL) == 0);
631 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000632 &logflags, delta, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 goto done;
634 } else {
635 xfs_bmbt_irec_t prev; /* old extent at offset idx */
636
637 /*
638 * Get the record referred to by idx.
639 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100640 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /*
642 * If it's a real allocation record, and the new allocation ends
643 * after the start of the referred to record, then we're filling
644 * in a delayed or unwritten allocation with a real one, or
645 * converting real back to unwritten.
646 */
Eric Sandeen9d87c312009-01-14 23:22:07 -0600647 if (!isnullstartblock(new->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 new->br_startoff + new->br_blockcount > prev.br_startoff) {
649 if (prev.br_state != XFS_EXT_UNWRITTEN &&
Eric Sandeen9d87c312009-01-14 23:22:07 -0600650 isnullstartblock(prev.br_startblock)) {
651 da_old = startblockval(prev.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (cur)
653 ASSERT(cur->bc_private.b.flags &
654 XFS_BTCUR_BPRV_WASDEL);
655 if ((error = xfs_bmap_add_extent_delay_real(ip,
656 idx, &cur, new, &da_new, first, flist,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000657 &logflags, delta, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 goto done;
659 } else if (new->br_state == XFS_EXT_NORM) {
660 ASSERT(new->br_state == XFS_EXT_NORM);
661 if ((error = xfs_bmap_add_extent_unwritten_real(
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000662 ip, idx, &cur, new, &logflags, delta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto done;
664 } else {
665 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
666 if ((error = xfs_bmap_add_extent_unwritten_real(
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000667 ip, idx, &cur, new, &logflags, delta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 goto done;
669 }
670 ASSERT(*curp == cur || *curp == NULL);
671 }
672 /*
673 * Otherwise we're filling in a hole with an allocation.
674 */
675 else {
676 if (cur)
677 ASSERT((cur->bc_private.b.flags &
678 XFS_BTCUR_BPRV_WASDEL) == 0);
679 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000680 new, &logflags, delta, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 goto done;
682 }
683 }
684
685 ASSERT(*curp == cur || *curp == NULL);
686 /*
687 * Convert to a btree if necessary.
688 */
689 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
690 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
691 int tmp_logflags; /* partial log flag return val */
692
693 ASSERT(cur == NULL);
694 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
695 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
696 logflags |= tmp_logflags;
697 if (error)
698 goto done;
699 }
700 /*
701 * Adjust for changes in reserved delayed indirect blocks.
702 * Nothing to do for disk quotas here.
703 */
704 if (da_old || da_new) {
705 xfs_filblks_t nblks;
706
707 nblks = da_new;
708 if (cur)
709 nblks += cur->bc_private.b.allocated;
710 ASSERT(nblks <= da_old);
711 if (nblks < da_old)
712 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100713 (int64_t)(da_old - nblks), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 /*
716 * Clear out the allocated field, done with it now in any case.
717 */
718 if (cur) {
719 cur->bc_private.b.allocated = 0;
720 *curp = cur;
721 }
722done:
723#ifdef DEBUG
724 if (!error)
725 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
726#endif
727 *logflagsp = logflags;
728 return error;
729}
730
731/*
732 * Called by xfs_bmap_add_extent to handle cases converting a delayed
733 * allocation to a real allocation.
734 */
735STATIC int /* error */
736xfs_bmap_add_extent_delay_real(
737 xfs_inode_t *ip, /* incore inode pointer */
738 xfs_extnum_t idx, /* extent number to update/insert */
739 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100740 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
742 xfs_fsblock_t *first, /* pointer to firstblock variable */
743 xfs_bmap_free_t *flist, /* list of extents to be freed */
744 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000745 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int rsvd) /* OK to use reserved data block allocation */
747{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 xfs_btree_cur_t *cur; /* btree cursor */
749 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000750 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100753 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 xfs_fileoff_t new_endoff; /* end offset of new entry */
755 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
756 /* left is 0, right is 1, prev is 2 */
757 int rval=0; /* return value (logging flags) */
758 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000759 xfs_filblks_t temp=0; /* value for dnew calculations */
760 xfs_filblks_t temp2=0;/* value for dnew calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 int tmp_rval; /* partial logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763#define LEFT r[0]
764#define RIGHT r[1]
765#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 /*
768 * Set up a bunch of variables to make the tests simpler.
769 */
770 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100771 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
772 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 xfs_bmbt_get_all(ep, &PREV);
774 new_endoff = new->br_startoff + new->br_blockcount;
775 ASSERT(PREV.br_startoff <= new->br_startoff);
776 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /*
779 * Set flags determining what part of the previous delayed allocation
780 * extent is being replaced by a real allocation.
781 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000782 if (PREV.br_startoff == new->br_startoff)
783 state |= BMAP_LEFT_FILLING;
784 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
785 state |= BMAP_RIGHT_FILLING;
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /*
788 * Check and set flags if this segment has a left neighbor.
789 * Don't set contiguous if the combined extent would be too large.
790 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000791 if (idx > 0) {
792 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100793 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000794
795 if (isnullstartblock(LEFT.br_startblock))
796 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000798
799 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
800 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
801 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
802 LEFT.br_state == new->br_state &&
803 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
804 state |= BMAP_LEFT_CONTIG;
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /*
807 * Check and set flags if this segment has a right neighbor.
808 * Don't set contiguous if the combined extent would be too large.
809 * Also check for all-three-contiguous being too large.
810 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000811 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
812 state |= BMAP_RIGHT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100813 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000814
815 if (isnullstartblock(RIGHT.br_startblock))
816 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000818
819 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
820 new_endoff == RIGHT.br_startoff &&
821 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
822 new->br_state == RIGHT.br_state &&
823 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
824 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
825 BMAP_RIGHT_FILLING)) !=
826 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
827 BMAP_RIGHT_FILLING) ||
828 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
829 <= MAXEXTLEN))
830 state |= BMAP_RIGHT_CONTIG;
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 error = 0;
833 /*
834 * Switch out based on the FILLING and CONTIG state bits.
835 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000836 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
837 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
838 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
839 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 /*
841 * Filling in all of a previously delayed allocation extent.
842 * The left and right neighbors are both contiguous with new.
843 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000844 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100846 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 LEFT.br_blockcount + PREV.br_blockcount +
848 RIGHT.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000849 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000851 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100852 xfs_iext_remove(ifp, idx, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 ip->i_df.if_lastex = idx - 1;
854 ip->i_d.di_nextents--;
855 if (cur == NULL)
856 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
857 else {
858 rval = XFS_ILOG_CORE;
859 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
860 RIGHT.br_startblock,
861 RIGHT.br_blockcount, &i)))
862 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000863 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +1100864 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000866 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +1100867 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000869 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
871 LEFT.br_startblock,
872 LEFT.br_blockcount +
873 PREV.br_blockcount +
874 RIGHT.br_blockcount, LEFT.br_state)))
875 goto done;
876 }
877 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000878 /* DELTA: Three in-core extents are replaced by one. */
879 temp = LEFT.br_startoff;
880 temp2 = LEFT.br_blockcount +
881 PREV.br_blockcount +
882 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 break;
884
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000885 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /*
887 * Filling in all of a previously delayed allocation extent.
888 * The left neighbor is contiguous, the right is not.
889 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000890 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100892 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 LEFT.br_blockcount + PREV.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000894 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 XFS_DATA_FORK);
896 ip->i_df.if_lastex = idx - 1;
Eric Sandeen3a59c942007-07-11 11:09:47 +1000897 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100898 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (cur == NULL)
900 rval = XFS_ILOG_DEXT;
901 else {
902 rval = 0;
903 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
904 LEFT.br_startblock, LEFT.br_blockcount,
905 &i)))
906 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000907 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
909 LEFT.br_startblock,
910 LEFT.br_blockcount +
911 PREV.br_blockcount, LEFT.br_state)))
912 goto done;
913 }
914 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000915 /* DELTA: Two in-core extents are replaced by one. */
916 temp = LEFT.br_startoff;
917 temp2 = LEFT.br_blockcount +
918 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 break;
920
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000921 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 /*
923 * Filling in all of a previously delayed allocation extent.
924 * The right neighbor is contiguous, the left is not.
925 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000926 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 xfs_bmbt_set_startblock(ep, new->br_startblock);
928 xfs_bmbt_set_blockcount(ep,
929 PREV.br_blockcount + RIGHT.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000930 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 ip->i_df.if_lastex = idx;
Eric Sandeen3a59c942007-07-11 11:09:47 +1000932 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100933 xfs_iext_remove(ifp, idx + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (cur == NULL)
935 rval = XFS_ILOG_DEXT;
936 else {
937 rval = 0;
938 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
939 RIGHT.br_startblock,
940 RIGHT.br_blockcount, &i)))
941 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000942 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
944 new->br_startblock,
945 PREV.br_blockcount +
946 RIGHT.br_blockcount, PREV.br_state)))
947 goto done;
948 }
949 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000950 /* DELTA: Two in-core extents are replaced by one. */
951 temp = PREV.br_startoff;
952 temp2 = PREV.br_blockcount +
953 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 break;
955
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000956 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 /*
958 * Filling in all of a previously delayed allocation extent.
959 * Neither the left nor right neighbors are contiguous with
960 * the new one.
961 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000962 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 xfs_bmbt_set_startblock(ep, new->br_startblock);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000964 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 ip->i_df.if_lastex = idx;
966 ip->i_d.di_nextents++;
967 if (cur == NULL)
968 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
969 else {
970 rval = XFS_ILOG_CORE;
971 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
972 new->br_startblock, new->br_blockcount,
973 &i)))
974 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000975 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100977 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000979 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000982 /* DELTA: The in-core extent described by new changed type. */
983 temp = new->br_startoff;
984 temp2 = new->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 break;
986
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000987 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /*
989 * Filling in the first part of a previous delayed allocation.
990 * The left neighbor is contiguous.
991 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000992 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100993 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 LEFT.br_blockcount + new->br_blockcount);
995 xfs_bmbt_set_startoff(ep,
996 PREV.br_startoff + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000997 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 temp = PREV.br_blockcount - new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +1000999 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 xfs_bmbt_set_blockcount(ep, temp);
1001 ip->i_df.if_lastex = idx - 1;
1002 if (cur == NULL)
1003 rval = XFS_ILOG_DEXT;
1004 else {
1005 rval = 0;
1006 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
1007 LEFT.br_startblock, LEFT.br_blockcount,
1008 &i)))
1009 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001010 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1012 LEFT.br_startblock,
1013 LEFT.br_blockcount +
1014 new->br_blockcount,
1015 LEFT.br_state)))
1016 goto done;
1017 }
1018 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001019 startblockval(PREV.br_startblock));
1020 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001021 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001023 /* DELTA: The boundary between two in-core extents moved. */
1024 temp = LEFT.br_startoff;
1025 temp2 = LEFT.br_blockcount +
1026 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 break;
1028
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001029 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /*
1031 * Filling in the first part of a previous delayed allocation.
1032 * The left neighbor is not contiguous.
1033 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001034 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 xfs_bmbt_set_startoff(ep, new_endoff);
1036 temp = PREV.br_blockcount - new->br_blockcount;
1037 xfs_bmbt_set_blockcount(ep, temp);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001038 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001040 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 ip->i_df.if_lastex = idx;
1042 ip->i_d.di_nextents++;
1043 if (cur == NULL)
1044 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1045 else {
1046 rval = XFS_ILOG_CORE;
1047 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1048 new->br_startblock, new->br_blockcount,
1049 &i)))
1050 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001051 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001053 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001055 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1058 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1059 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1060 first, flist, &cur, 1, &tmp_rval,
1061 XFS_DATA_FORK);
1062 rval |= tmp_rval;
1063 if (error)
1064 goto done;
1065 }
1066 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001067 startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001069 ep = xfs_iext_get_ext(ifp, idx + 1);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001070 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001071 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx + 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001073 /* DELTA: One in-core extent is split in two. */
1074 temp = PREV.br_startoff;
1075 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 break;
1077
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001078 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /*
1080 * Filling in the last part of a previous delayed allocation.
1081 * The right neighbor is contiguous with the new allocation.
1082 */
1083 temp = PREV.br_blockcount - new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001084 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
1085 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 xfs_bmbt_set_blockcount(ep, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001087 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1088 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 new->br_blockcount + RIGHT.br_blockcount,
1090 RIGHT.br_state);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001091 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 ip->i_df.if_lastex = idx + 1;
1093 if (cur == NULL)
1094 rval = XFS_ILOG_DEXT;
1095 else {
1096 rval = 0;
1097 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1098 RIGHT.br_startblock,
1099 RIGHT.br_blockcount, &i)))
1100 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001101 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1103 new->br_startblock,
1104 new->br_blockcount +
1105 RIGHT.br_blockcount,
1106 RIGHT.br_state)))
1107 goto done;
1108 }
1109 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001110 startblockval(PREV.br_startblock));
1111 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001112 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001114 /* DELTA: The boundary between two in-core extents moved. */
1115 temp = PREV.br_startoff;
1116 temp2 = PREV.br_blockcount +
1117 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 break;
1119
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001120 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 /*
1122 * Filling in the last part of a previous delayed allocation.
1123 * The right neighbor is not contiguous.
1124 */
1125 temp = PREV.br_blockcount - new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001126 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 xfs_bmbt_set_blockcount(ep, temp);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001128 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1129 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001130 xfs_iext_insert(ifp, idx + 1, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 ip->i_df.if_lastex = idx + 1;
1132 ip->i_d.di_nextents++;
1133 if (cur == NULL)
1134 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1135 else {
1136 rval = XFS_ILOG_CORE;
1137 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1138 new->br_startblock, new->br_blockcount,
1139 &i)))
1140 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001141 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001143 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001145 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1148 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1149 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1150 first, flist, &cur, 1, &tmp_rval,
1151 XFS_DATA_FORK);
1152 rval |= tmp_rval;
1153 if (error)
1154 goto done;
1155 }
1156 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001157 startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001159 ep = xfs_iext_get_ext(ifp, idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001160 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001161 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001163 /* DELTA: One in-core extent is split in two. */
1164 temp = PREV.br_startoff;
1165 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 break;
1167
1168 case 0:
1169 /*
1170 * Filling in the middle part of a previous delayed allocation.
1171 * Contiguity is impossible here.
1172 * This case is avoided almost all the time.
1173 */
1174 temp = new->br_startoff - PREV.br_startoff;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001175 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 xfs_bmbt_set_blockcount(ep, temp);
1177 r[0] = *new;
Lachlan McIlroyd2133712006-11-21 18:55:16 +11001178 r[1].br_state = PREV.br_state;
1179 r[1].br_startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 r[1].br_startoff = new_endoff;
1181 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1182 r[1].br_blockcount = temp2;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001183 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001185 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 ip->i_df.if_lastex = idx + 1;
1187 ip->i_d.di_nextents++;
1188 if (cur == NULL)
1189 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1190 else {
1191 rval = XFS_ILOG_CORE;
1192 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1193 new->br_startblock, new->br_blockcount,
1194 &i)))
1195 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001196 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001198 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001200 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1203 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1204 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1205 first, flist, &cur, 1, &tmp_rval,
1206 XFS_DATA_FORK);
1207 rval |= tmp_rval;
1208 if (error)
1209 goto done;
1210 }
1211 temp = xfs_bmap_worst_indlen(ip, temp);
1212 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001213 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 (cur ? cur->bc_private.b.allocated : 0));
1215 if (diff > 0 &&
David Chinner20f4ebf2007-02-10 18:36:10 +11001216 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 /*
1218 * Ick gross gag me with a spoon.
1219 */
1220 ASSERT(0); /* want to see if this ever happens! */
1221 while (diff > 0) {
1222 if (temp) {
1223 temp--;
1224 diff--;
1225 if (!diff ||
1226 !xfs_mod_incore_sb(ip->i_mount,
David Chinner20f4ebf2007-02-10 18:36:10 +11001227 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 break;
1229 }
1230 if (temp2) {
1231 temp2--;
1232 diff--;
1233 if (!diff ||
1234 !xfs_mod_incore_sb(ip->i_mount,
David Chinner20f4ebf2007-02-10 18:36:10 +11001235 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 break;
1237 }
1238 }
1239 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001240 ep = xfs_iext_get_ext(ifp, idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001241 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001242 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
1243 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001244 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001245 nullstartblock((int)temp2));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001246 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 *dnew = temp + temp2;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001248 /* DELTA: One in-core extent is split in three. */
1249 temp = PREV.br_startoff;
1250 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 break;
1252
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001253 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1254 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1255 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1256 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1257 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1258 case BMAP_LEFT_CONTIG:
1259 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 /*
1261 * These cases are all impossible.
1262 */
1263 ASSERT(0);
1264 }
1265 *curp = cur;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001266 if (delta) {
1267 temp2 += temp;
1268 if (delta->xed_startoff > temp)
1269 delta->xed_startoff = temp;
1270 if (delta->xed_blockcount < temp2)
1271 delta->xed_blockcount = temp2;
1272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273done:
1274 *logflagsp = rval;
1275 return error;
1276#undef LEFT
1277#undef RIGHT
1278#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
1281/*
1282 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1283 * allocation to a real allocation or vice versa.
1284 */
1285STATIC int /* error */
1286xfs_bmap_add_extent_unwritten_real(
1287 xfs_inode_t *ip, /* incore inode pointer */
1288 xfs_extnum_t idx, /* extent number to update/insert */
1289 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001290 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001291 int *logflagsp, /* inode logging flags */
1292 xfs_extdelta_t *delta) /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001295 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001298 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 xfs_fileoff_t new_endoff; /* end offset of new entry */
1300 xfs_exntst_t newext; /* new extent state */
1301 xfs_exntst_t oldext; /* old extent state */
1302 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1303 /* left is 0, right is 1, prev is 2 */
1304 int rval=0; /* return value (logging flags) */
1305 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001306 xfs_filblks_t temp=0;
1307 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309#define LEFT r[0]
1310#define RIGHT r[1]
1311#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 /*
1313 * Set up a bunch of variables to make the tests simpler.
1314 */
1315 error = 0;
1316 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001317 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1318 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 xfs_bmbt_get_all(ep, &PREV);
1320 newext = new->br_state;
1321 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1322 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1323 ASSERT(PREV.br_state == oldext);
1324 new_endoff = new->br_startoff + new->br_blockcount;
1325 ASSERT(PREV.br_startoff <= new->br_startoff);
1326 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 /*
1329 * Set flags determining what part of the previous oldext allocation
1330 * extent is being replaced by a newext allocation.
1331 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001332 if (PREV.br_startoff == new->br_startoff)
1333 state |= BMAP_LEFT_FILLING;
1334 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1335 state |= BMAP_RIGHT_FILLING;
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /*
1338 * Check and set flags if this segment has a left neighbor.
1339 * Don't set contiguous if the combined extent would be too large.
1340 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001341 if (idx > 0) {
1342 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001343 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001344
1345 if (isnullstartblock(LEFT.br_startblock))
1346 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001348
1349 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1350 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1351 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1352 LEFT.br_state == newext &&
1353 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1354 state |= BMAP_LEFT_CONTIG;
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /*
1357 * Check and set flags if this segment has a right neighbor.
1358 * Don't set contiguous if the combined extent would be too large.
1359 * Also check for all-three-contiguous being too large.
1360 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001361 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1362 state |= BMAP_RIGHT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001363 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001364 if (isnullstartblock(RIGHT.br_startblock))
1365 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001367
1368 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1369 new_endoff == RIGHT.br_startoff &&
1370 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1371 newext == RIGHT.br_state &&
1372 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1373 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1374 BMAP_RIGHT_FILLING)) !=
1375 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1376 BMAP_RIGHT_FILLING) ||
1377 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1378 <= MAXEXTLEN))
1379 state |= BMAP_RIGHT_CONTIG;
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 /*
1382 * Switch out based on the FILLING and CONTIG state bits.
1383 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001384 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1385 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1386 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1387 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 /*
1389 * Setting all of a previous oldext extent to newext.
1390 * The left and right neighbors are both contiguous with new.
1391 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001392 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001394 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 LEFT.br_blockcount + PREV.br_blockcount +
1396 RIGHT.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001397 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001399 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001400 xfs_iext_remove(ifp, idx, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 ip->i_df.if_lastex = idx - 1;
1402 ip->i_d.di_nextents -= 2;
1403 if (cur == NULL)
1404 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1405 else {
1406 rval = XFS_ILOG_CORE;
1407 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1408 RIGHT.br_startblock,
1409 RIGHT.br_blockcount, &i)))
1410 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001411 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001412 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001414 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001415 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001417 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001418 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001420 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001421 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001423 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1425 LEFT.br_startblock,
1426 LEFT.br_blockcount + PREV.br_blockcount +
1427 RIGHT.br_blockcount, LEFT.br_state)))
1428 goto done;
1429 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001430 /* DELTA: Three in-core extents are replaced by one. */
1431 temp = LEFT.br_startoff;
1432 temp2 = LEFT.br_blockcount +
1433 PREV.br_blockcount +
1434 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 break;
1436
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001437 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 /*
1439 * Setting all of a previous oldext extent to newext.
1440 * The left neighbor is contiguous, the right is not.
1441 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001442 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001444 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 LEFT.br_blockcount + PREV.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001446 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 XFS_DATA_FORK);
1448 ip->i_df.if_lastex = idx - 1;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001449 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001450 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 ip->i_d.di_nextents--;
1452 if (cur == NULL)
1453 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1454 else {
1455 rval = XFS_ILOG_CORE;
1456 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1457 PREV.br_startblock, PREV.br_blockcount,
1458 &i)))
1459 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001460 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001461 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001463 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001464 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001466 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1468 LEFT.br_startblock,
1469 LEFT.br_blockcount + PREV.br_blockcount,
1470 LEFT.br_state)))
1471 goto done;
1472 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001473 /* DELTA: Two in-core extents are replaced by one. */
1474 temp = LEFT.br_startoff;
1475 temp2 = LEFT.br_blockcount +
1476 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 break;
1478
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001479 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 /*
1481 * Setting all of a previous oldext extent to newext.
1482 * The right neighbor is contiguous, the left is not.
1483 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001484 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 XFS_DATA_FORK);
1486 xfs_bmbt_set_blockcount(ep,
1487 PREV.br_blockcount + RIGHT.br_blockcount);
1488 xfs_bmbt_set_state(ep, newext);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001489 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 XFS_DATA_FORK);
1491 ip->i_df.if_lastex = idx;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001492 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001493 xfs_iext_remove(ifp, idx + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 ip->i_d.di_nextents--;
1495 if (cur == NULL)
1496 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1497 else {
1498 rval = XFS_ILOG_CORE;
1499 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1500 RIGHT.br_startblock,
1501 RIGHT.br_blockcount, &i)))
1502 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001503 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001504 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001506 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001507 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001509 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1511 new->br_startblock,
1512 new->br_blockcount + RIGHT.br_blockcount,
1513 newext)))
1514 goto done;
1515 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001516 /* DELTA: Two in-core extents are replaced by one. */
1517 temp = PREV.br_startoff;
1518 temp2 = PREV.br_blockcount +
1519 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 break;
1521
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001522 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 /*
1524 * Setting all of a previous oldext extent to newext.
1525 * Neither the left nor right neighbors are contiguous with
1526 * the new one.
1527 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001528 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 XFS_DATA_FORK);
1530 xfs_bmbt_set_state(ep, newext);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001531 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 XFS_DATA_FORK);
1533 ip->i_df.if_lastex = idx;
1534 if (cur == NULL)
1535 rval = XFS_ILOG_DEXT;
1536 else {
1537 rval = 0;
1538 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1539 new->br_startblock, new->br_blockcount,
1540 &i)))
1541 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001542 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1544 new->br_startblock, new->br_blockcount,
1545 newext)))
1546 goto done;
1547 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001548 /* DELTA: The in-core extent described by new changed type. */
1549 temp = new->br_startoff;
1550 temp2 = new->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 break;
1552
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001553 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /*
1555 * Setting the first part of a previous oldext extent to newext.
1556 * The left neighbor is contiguous.
1557 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001558 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001560 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 LEFT.br_blockcount + new->br_blockcount);
1562 xfs_bmbt_set_startoff(ep,
1563 PREV.br_startoff + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001564 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001566 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 XFS_DATA_FORK);
1568 xfs_bmbt_set_startblock(ep,
1569 new->br_startblock + new->br_blockcount);
1570 xfs_bmbt_set_blockcount(ep,
1571 PREV.br_blockcount - new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001572 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 XFS_DATA_FORK);
1574 ip->i_df.if_lastex = idx - 1;
1575 if (cur == NULL)
1576 rval = XFS_ILOG_DEXT;
1577 else {
1578 rval = 0;
1579 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1580 PREV.br_startblock, PREV.br_blockcount,
1581 &i)))
1582 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001583 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if ((error = xfs_bmbt_update(cur,
1585 PREV.br_startoff + new->br_blockcount,
1586 PREV.br_startblock + new->br_blockcount,
1587 PREV.br_blockcount - new->br_blockcount,
1588 oldext)))
1589 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001590 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 goto done;
1592 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1593 LEFT.br_startblock,
1594 LEFT.br_blockcount + new->br_blockcount,
1595 LEFT.br_state))
1596 goto done;
1597 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001598 /* DELTA: The boundary between two in-core extents moved. */
1599 temp = LEFT.br_startoff;
1600 temp2 = LEFT.br_blockcount +
1601 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 break;
1603
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001604 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 /*
1606 * Setting the first part of a previous oldext extent to newext.
1607 * The left neighbor is not contiguous.
1608 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001609 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1611 xfs_bmbt_set_startoff(ep, new_endoff);
1612 xfs_bmbt_set_blockcount(ep,
1613 PREV.br_blockcount - new->br_blockcount);
1614 xfs_bmbt_set_startblock(ep,
1615 new->br_startblock + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001616 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1617 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001619 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 ip->i_df.if_lastex = idx;
1621 ip->i_d.di_nextents++;
1622 if (cur == NULL)
1623 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1624 else {
1625 rval = XFS_ILOG_CORE;
1626 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1627 PREV.br_startblock, PREV.br_blockcount,
1628 &i)))
1629 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001630 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if ((error = xfs_bmbt_update(cur,
1632 PREV.br_startoff + new->br_blockcount,
1633 PREV.br_startblock + new->br_blockcount,
1634 PREV.br_blockcount - new->br_blockcount,
1635 oldext)))
1636 goto done;
1637 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001638 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001640 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001642 /* DELTA: One in-core extent is split in two. */
1643 temp = PREV.br_startoff;
1644 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 break;
1646
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001647 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 /*
1649 * Setting the last part of a previous oldext extent to newext.
1650 * The right neighbor is contiguous with the new allocation.
1651 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001652 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001654 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 XFS_DATA_FORK);
1656 xfs_bmbt_set_blockcount(ep,
1657 PREV.br_blockcount - new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001658 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001660 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1661 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 new->br_blockcount + RIGHT.br_blockcount, newext);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001663 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 XFS_DATA_FORK);
1665 ip->i_df.if_lastex = idx + 1;
1666 if (cur == NULL)
1667 rval = XFS_ILOG_DEXT;
1668 else {
1669 rval = 0;
1670 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1671 PREV.br_startblock,
1672 PREV.br_blockcount, &i)))
1673 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001674 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1676 PREV.br_startblock,
1677 PREV.br_blockcount - new->br_blockcount,
1678 oldext)))
1679 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001680 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 goto done;
1682 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1683 new->br_startblock,
1684 new->br_blockcount + RIGHT.br_blockcount,
1685 newext)))
1686 goto done;
1687 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001688 /* DELTA: The boundary between two in-core extents moved. */
1689 temp = PREV.br_startoff;
1690 temp2 = PREV.br_blockcount +
1691 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 break;
1693
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001694 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /*
1696 * Setting the last part of a previous oldext extent to newext.
1697 * The right neighbor is not contiguous.
1698 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001699 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 xfs_bmbt_set_blockcount(ep,
1701 PREV.br_blockcount - new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001702 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1703 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1704 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001705 xfs_iext_insert(ifp, idx + 1, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 ip->i_df.if_lastex = idx + 1;
1707 ip->i_d.di_nextents++;
1708 if (cur == NULL)
1709 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1710 else {
1711 rval = XFS_ILOG_CORE;
1712 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1713 PREV.br_startblock, PREV.br_blockcount,
1714 &i)))
1715 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001716 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1718 PREV.br_startblock,
1719 PREV.br_blockcount - new->br_blockcount,
1720 oldext)))
1721 goto done;
1722 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1723 new->br_startblock, new->br_blockcount,
1724 &i)))
1725 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001726 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001728 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001730 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001732 /* DELTA: One in-core extent is split in two. */
1733 temp = PREV.br_startoff;
1734 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 break;
1736
1737 case 0:
1738 /*
1739 * Setting the middle part of a previous oldext extent to
1740 * newext. Contiguity is impossible here.
1741 * One extent becomes three extents.
1742 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001743 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 xfs_bmbt_set_blockcount(ep,
1745 new->br_startoff - PREV.br_startoff);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001746 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 r[0] = *new;
1748 r[1].br_startoff = new_endoff;
1749 r[1].br_blockcount =
1750 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1751 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1752 r[1].br_state = oldext;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001753 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001755 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 ip->i_df.if_lastex = idx + 1;
1757 ip->i_d.di_nextents += 2;
1758 if (cur == NULL)
1759 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1760 else {
1761 rval = XFS_ILOG_CORE;
1762 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1763 PREV.br_startblock, PREV.br_blockcount,
1764 &i)))
1765 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001766 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 /* new right extent - oldext */
1768 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1769 r[1].br_startblock, r[1].br_blockcount,
1770 r[1].br_state)))
1771 goto done;
1772 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10001774 cur->bc_rec.b.br_blockcount =
1775 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001776 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001778 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001779 /*
1780 * Reset the cursor to the position of the new extent
1781 * we are about to insert as we can't trust it after
1782 * the previous insert.
1783 */
1784 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1785 new->br_startblock, new->br_blockcount,
1786 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001788 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001790 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001791 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001793 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001795 /* DELTA: One in-core extent is split in three. */
1796 temp = PREV.br_startoff;
1797 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 break;
1799
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001800 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1801 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1802 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1803 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1804 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1805 case BMAP_LEFT_CONTIG:
1806 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 /*
1808 * These cases are all impossible.
1809 */
1810 ASSERT(0);
1811 }
1812 *curp = cur;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001813 if (delta) {
1814 temp2 += temp;
1815 if (delta->xed_startoff > temp)
1816 delta->xed_startoff = temp;
1817 if (delta->xed_blockcount < temp2)
1818 delta->xed_blockcount = temp2;
1819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820done:
1821 *logflagsp = rval;
1822 return error;
1823#undef LEFT
1824#undef RIGHT
1825#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
1828/*
1829 * Called by xfs_bmap_add_extent to handle cases converting a hole
1830 * to a delayed allocation.
1831 */
1832/*ARGSUSED*/
1833STATIC int /* error */
1834xfs_bmap_add_extent_hole_delay(
1835 xfs_inode_t *ip, /* incore inode pointer */
1836 xfs_extnum_t idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001837 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001839 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 int rsvd) /* OK to allocate reserved blocks */
1841{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001842 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001843 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1845 xfs_filblks_t newlen=0; /* new indirect size */
1846 xfs_filblks_t oldlen=0; /* old indirect size */
1847 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1848 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001849 xfs_filblks_t temp=0; /* temp for indirect calculations */
1850 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001852 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1853 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001855 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /*
1858 * Check and set flags if this segment has a left neighbor
1859 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001860 if (idx > 0) {
1861 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001862 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001863
1864 if (isnullstartblock(left.br_startblock))
1865 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /*
1869 * Check and set flags if the current (right) segment exists.
1870 * If it doesn't exist, we're converting the hole at end-of-file.
1871 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001872 if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1873 state |= BMAP_RIGHT_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 xfs_bmbt_get_all(ep, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001875
1876 if (isnullstartblock(right.br_startblock))
1877 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 /*
1881 * Set contiguity flags on the left and right neighbors.
1882 * Don't let extents get too large, even if the pieces are contiguous.
1883 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001884 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1885 left.br_startoff + left.br_blockcount == new->br_startoff &&
1886 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1887 state |= BMAP_LEFT_CONTIG;
1888
1889 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1890 new->br_startoff + new->br_blockcount == right.br_startoff &&
1891 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1892 (!(state & BMAP_LEFT_CONTIG) ||
1893 (left.br_blockcount + new->br_blockcount +
1894 right.br_blockcount <= MAXEXTLEN)))
1895 state |= BMAP_RIGHT_CONTIG;
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /*
1898 * Switch out based on the contiguity flags.
1899 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001900 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1901 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /*
1903 * New allocation is contiguous with delayed allocations
1904 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001905 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 */
1907 temp = left.br_blockcount + new->br_blockcount +
1908 right.br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001909 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001911 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001912 oldlen = startblockval(left.br_startblock) +
1913 startblockval(new->br_startblock) +
1914 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001916 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001917 nullstartblock((int)newlen));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001918 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001920 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001921 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 ip->i_df.if_lastex = idx - 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001923 /* DELTA: Two in-core extents were replaced by one. */
1924 temp2 = temp;
1925 temp = left.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 break;
1927
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001928 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 /*
1930 * New allocation is contiguous with a delayed allocation
1931 * on the left.
1932 * Merge the new allocation with the left neighbor.
1933 */
1934 temp = left.br_blockcount + new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001935 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001937 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001938 oldlen = startblockval(left.br_startblock) +
1939 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001941 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001942 nullstartblock((int)newlen));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001943 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 XFS_DATA_FORK);
1945 ip->i_df.if_lastex = idx - 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001946 /* DELTA: One in-core extent grew into a hole. */
1947 temp2 = temp;
1948 temp = left.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 break;
1950
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001951 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 /*
1953 * New allocation is contiguous with a delayed allocation
1954 * on the right.
1955 * Merge the new allocation with the right neighbor.
1956 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001957 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001959 oldlen = startblockval(new->br_startblock) +
1960 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 newlen = xfs_bmap_worst_indlen(ip, temp);
1962 xfs_bmbt_set_allf(ep, new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06001963 nullstartblock((int)newlen), temp, right.br_state);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001964 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 ip->i_df.if_lastex = idx;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001966 /* DELTA: One in-core extent grew into a hole. */
1967 temp2 = temp;
1968 temp = new->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 break;
1970
1971 case 0:
1972 /*
1973 * New allocation is not contiguous with another
1974 * delayed allocation.
1975 * Insert a new entry.
1976 */
1977 oldlen = newlen = 0;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001978 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001980 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 ip->i_df.if_lastex = idx;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001982 /* DELTA: A new in-core extent was added in a hole. */
1983 temp2 = new->br_blockcount;
1984 temp = new->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 break;
1986 }
1987 if (oldlen != newlen) {
1988 ASSERT(oldlen > newlen);
1989 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11001990 (int64_t)(oldlen - newlen), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 /*
1992 * Nothing to do for disk quota accounting here.
1993 */
1994 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001995 if (delta) {
1996 temp2 += temp;
1997 if (delta->xed_startoff > temp)
1998 delta->xed_startoff = temp;
1999 if (delta->xed_blockcount < temp2)
2000 delta->xed_blockcount = temp2;
2001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 *logflagsp = 0;
2003 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
2006/*
2007 * Called by xfs_bmap_add_extent to handle cases converting a hole
2008 * to a real allocation.
2009 */
2010STATIC int /* error */
2011xfs_bmap_add_extent_hole_real(
2012 xfs_inode_t *ip, /* incore inode pointer */
2013 xfs_extnum_t idx, /* extent number to update/insert */
2014 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002015 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002017 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 int whichfork) /* data or attr fork */
2019{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002020 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 int i; /* temp state */
2023 xfs_ifork_t *ifp; /* inode fork pointer */
2024 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2025 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002026 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002028 xfs_filblks_t temp=0;
2029 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 ifp = XFS_IFORK_PTR(ip, whichfork);
2032 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002033 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 state = 0;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 /*
2037 * Check and set flags if this segment has a left neighbor.
2038 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002039 if (idx > 0) {
2040 state |= BMAP_LEFT_VALID;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002041 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002042 if (isnullstartblock(left.br_startblock))
2043 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 /*
2047 * Check and set flags if this segment has a current value.
2048 * Not true if we're inserting into the "hole" at eof.
2049 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002050 if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
2051 state |= BMAP_RIGHT_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 xfs_bmbt_get_all(ep, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002053 if (isnullstartblock(right.br_startblock))
2054 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 /*
2058 * We're inserting a real allocation between "left" and "right".
2059 * Set the contiguity flags. Don't let extents get too large.
2060 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002061 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2062 left.br_startoff + left.br_blockcount == new->br_startoff &&
2063 left.br_startblock + left.br_blockcount == new->br_startblock &&
2064 left.br_state == new->br_state &&
2065 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2066 state |= BMAP_LEFT_CONTIG;
2067
2068 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2069 new->br_startoff + new->br_blockcount == right.br_startoff &&
2070 new->br_startblock + new->br_blockcount == right.br_startblock &&
2071 new->br_state == right.br_state &&
2072 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2073 (!(state & BMAP_LEFT_CONTIG) ||
2074 left.br_blockcount + new->br_blockcount +
2075 right.br_blockcount <= MAXEXTLEN))
2076 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002078 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 /*
2080 * Select which case we're in here, and implement it.
2081 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002082 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2083 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 /*
2085 * New allocation is contiguous with real allocations on the
2086 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002087 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002089 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002091 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 left.br_blockcount + new->br_blockcount +
2093 right.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002094 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 whichfork);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002096 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002097 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 ifp->if_lastex = idx - 1;
2099 XFS_IFORK_NEXT_SET(ip, whichfork,
2100 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2101 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002102 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002103 } else {
2104 rval = XFS_ILOG_CORE;
2105 if ((error = xfs_bmbt_lookup_eq(cur,
2106 right.br_startoff,
2107 right.br_startblock,
2108 right.br_blockcount, &i)))
2109 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002110 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002111 if ((error = xfs_btree_delete(cur, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002112 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002113 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002114 if ((error = xfs_btree_decrement(cur, 0, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002115 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002116 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002117 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2118 left.br_startblock,
2119 left.br_blockcount +
2120 new->br_blockcount +
2121 right.br_blockcount,
2122 left.br_state)))
2123 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002125 /* DELTA: Two in-core extents were replaced by one. */
2126 temp = left.br_startoff;
2127 temp2 = left.br_blockcount +
2128 new->br_blockcount +
2129 right.br_blockcount;
2130 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002132 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 /*
2134 * New allocation is contiguous with a real allocation
2135 * on the left.
2136 * Merge the new allocation with the left neighbor.
2137 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002138 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002139 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 left.br_blockcount + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002141 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 ifp->if_lastex = idx - 1;
2143 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002144 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002145 } else {
2146 rval = 0;
2147 if ((error = xfs_bmbt_lookup_eq(cur,
2148 left.br_startoff,
2149 left.br_startblock,
2150 left.br_blockcount, &i)))
2151 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002152 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002153 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2154 left.br_startblock,
2155 left.br_blockcount +
2156 new->br_blockcount,
2157 left.br_state)))
2158 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002160 /* DELTA: One in-core extent grew. */
2161 temp = left.br_startoff;
2162 temp2 = left.br_blockcount +
2163 new->br_blockcount;
2164 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002166 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 /*
2168 * New allocation is contiguous with a real allocation
2169 * on the right.
2170 * Merge the new allocation with the right neighbor.
2171 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002172 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2174 new->br_blockcount + right.br_blockcount,
2175 right.br_state);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002176 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 ifp->if_lastex = idx;
2178 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002179 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002180 } else {
2181 rval = 0;
2182 if ((error = xfs_bmbt_lookup_eq(cur,
2183 right.br_startoff,
2184 right.br_startblock,
2185 right.br_blockcount, &i)))
2186 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002187 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002188 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2189 new->br_startblock,
2190 new->br_blockcount +
2191 right.br_blockcount,
2192 right.br_state)))
2193 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002195 /* DELTA: One in-core extent grew. */
2196 temp = new->br_startoff;
2197 temp2 = new->br_blockcount +
2198 right.br_blockcount;
2199 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201 case 0:
2202 /*
2203 * New allocation is not contiguous with another
2204 * real allocation.
2205 * Insert a new entry.
2206 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002207 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002208 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 ifp->if_lastex = idx;
2210 XFS_IFORK_NEXT_SET(ip, whichfork,
2211 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2212 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002213 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002214 } else {
2215 rval = XFS_ILOG_CORE;
2216 if ((error = xfs_bmbt_lookup_eq(cur,
2217 new->br_startoff,
2218 new->br_startblock,
2219 new->br_blockcount, &i)))
2220 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002221 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002222 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002223 if ((error = xfs_btree_insert(cur, &i)))
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002224 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002225 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002227 /* DELTA: A new extent was added in a hole. */
2228 temp = new->br_startoff;
2229 temp2 = new->br_blockcount;
2230 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002232 if (delta) {
2233 temp2 += temp;
2234 if (delta->xed_startoff > temp)
2235 delta->xed_startoff = temp;
2236 if (delta->xed_blockcount < temp2)
2237 delta->xed_blockcount = temp2;
2238 }
2239done:
2240 *logflagsp = rval;
2241 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Nathan Scottdd9f4382006-01-11 15:28:28 +11002244/*
2245 * Adjust the size of the new extent based on di_extsize and rt extsize.
2246 */
2247STATIC int
2248xfs_bmap_extsize_align(
2249 xfs_mount_t *mp,
2250 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2251 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2252 xfs_extlen_t extsz, /* align to this extent size */
2253 int rt, /* is this a realtime inode? */
2254 int eof, /* is extent at end-of-file? */
2255 int delay, /* creating delalloc extent? */
2256 int convert, /* overwriting unwritten extent? */
2257 xfs_fileoff_t *offp, /* in/out: aligned offset */
2258 xfs_extlen_t *lenp) /* in/out: aligned length */
2259{
2260 xfs_fileoff_t orig_off; /* original offset */
2261 xfs_extlen_t orig_alen; /* original length */
2262 xfs_fileoff_t orig_end; /* original off+len */
2263 xfs_fileoff_t nexto; /* next file offset */
2264 xfs_fileoff_t prevo; /* previous file offset */
2265 xfs_fileoff_t align_off; /* temp for offset */
2266 xfs_extlen_t align_alen; /* temp for length */
2267 xfs_extlen_t temp; /* temp for calculations */
2268
2269 if (convert)
2270 return 0;
2271
2272 orig_off = align_off = *offp;
2273 orig_alen = align_alen = *lenp;
2274 orig_end = orig_off + orig_alen;
2275
2276 /*
2277 * If this request overlaps an existing extent, then don't
2278 * attempt to perform any additional alignment.
2279 */
2280 if (!delay && !eof &&
2281 (orig_off >= gotp->br_startoff) &&
2282 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2283 return 0;
2284 }
2285
2286 /*
2287 * If the file offset is unaligned vs. the extent size
2288 * we need to align it. This will be possible unless
2289 * the file was previously written with a kernel that didn't
2290 * perform this alignment, or if a truncate shot us in the
2291 * foot.
2292 */
2293 temp = do_mod(orig_off, extsz);
2294 if (temp) {
2295 align_alen += temp;
2296 align_off -= temp;
2297 }
2298 /*
2299 * Same adjustment for the end of the requested area.
2300 */
2301 if ((temp = (align_alen % extsz))) {
2302 align_alen += extsz - temp;
2303 }
2304 /*
2305 * If the previous block overlaps with this proposed allocation
2306 * then move the start forward without adjusting the length.
2307 */
2308 if (prevp->br_startoff != NULLFILEOFF) {
2309 if (prevp->br_startblock == HOLESTARTBLOCK)
2310 prevo = prevp->br_startoff;
2311 else
2312 prevo = prevp->br_startoff + prevp->br_blockcount;
2313 } else
2314 prevo = 0;
2315 if (align_off != orig_off && align_off < prevo)
2316 align_off = prevo;
2317 /*
2318 * If the next block overlaps with this proposed allocation
2319 * then move the start back without adjusting the length,
2320 * but not before offset 0.
2321 * This may of course make the start overlap previous block,
2322 * and if we hit the offset 0 limit then the next block
2323 * can still overlap too.
2324 */
2325 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2326 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2327 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2328 nexto = gotp->br_startoff + gotp->br_blockcount;
2329 else
2330 nexto = gotp->br_startoff;
2331 } else
2332 nexto = NULLFILEOFF;
2333 if (!eof &&
2334 align_off + align_alen != orig_end &&
2335 align_off + align_alen > nexto)
2336 align_off = nexto > align_alen ? nexto - align_alen : 0;
2337 /*
2338 * If we're now overlapping the next or previous extent that
2339 * means we can't fit an extsz piece in this hole. Just move
2340 * the start forward to the first valid spot and set
2341 * the length so we hit the end.
2342 */
2343 if (align_off != orig_off && align_off < prevo)
2344 align_off = prevo;
2345 if (align_off + align_alen != orig_end &&
2346 align_off + align_alen > nexto &&
2347 nexto != NULLFILEOFF) {
2348 ASSERT(nexto > prevo);
2349 align_alen = nexto - align_off;
2350 }
2351
2352 /*
2353 * If realtime, and the result isn't a multiple of the realtime
2354 * extent size we need to remove blocks until it is.
2355 */
2356 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2357 /*
2358 * We're not covering the original request, or
2359 * we won't be able to once we fix the length.
2360 */
2361 if (orig_off < align_off ||
2362 orig_end > align_off + align_alen ||
2363 align_alen - temp < orig_alen)
2364 return XFS_ERROR(EINVAL);
2365 /*
2366 * Try to fix it by moving the start up.
2367 */
2368 if (align_off + temp <= orig_off) {
2369 align_alen -= temp;
2370 align_off += temp;
2371 }
2372 /*
2373 * Try to fix it by moving the end in.
2374 */
2375 else if (align_off + align_alen - temp >= orig_end)
2376 align_alen -= temp;
2377 /*
2378 * Set the start to the minimum then trim the length.
2379 */
2380 else {
2381 align_alen -= orig_off - align_off;
2382 align_off = orig_off;
2383 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2384 }
2385 /*
2386 * Result doesn't cover the request, fail it.
2387 */
2388 if (orig_off < align_off || orig_end > align_off + align_alen)
2389 return XFS_ERROR(EINVAL);
2390 } else {
2391 ASSERT(orig_off >= align_off);
2392 ASSERT(orig_end <= align_off + align_alen);
2393 }
2394
2395#ifdef DEBUG
2396 if (!eof && gotp->br_startoff != NULLFILEOFF)
2397 ASSERT(align_off + align_alen <= gotp->br_startoff);
2398 if (prevp->br_startoff != NULLFILEOFF)
2399 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2400#endif
2401
2402 *lenp = align_alen;
2403 *offp = align_off;
2404 return 0;
2405}
2406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407#define XFS_ALLOC_GAP_UNITS 4
2408
David Chinnerc2b1cba2008-04-10 12:21:40 +10002409STATIC void
Nathan Scotta365bdd2006-03-14 13:34:16 +11002410xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2412{
2413 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2415 xfs_mount_t *mp; /* mount point structure */
2416 int nullfb; /* true if ap->firstblock isn't set */
2417 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
2419#define ISVALID(x,y) \
2420 (rt ? \
2421 (x) < mp->m_sb.sb_rblocks : \
2422 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2423 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2424 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 mp = ap->ip->i_mount;
2427 nullfb = ap->firstblock == NULLFSBLOCK;
2428 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2429 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 /*
2431 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002432 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 */
2434 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002435 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2437 ap->prevp->br_startblock)) {
2438 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2439 /*
2440 * Adjust for the gap between prevp and us.
2441 */
2442 adjust = ap->off -
2443 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2444 if (adjust &&
2445 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2446 ap->rval += adjust;
2447 }
2448 /*
2449 * If not at eof, then compare the two neighbor blocks.
2450 * Figure out whether either one gives us a good starting point,
2451 * and pick the better one.
2452 */
2453 else if (!ap->eof) {
2454 xfs_fsblock_t gotbno; /* right side block number */
2455 xfs_fsblock_t gotdiff=0; /* right side difference */
2456 xfs_fsblock_t prevbno; /* left side block number */
2457 xfs_fsblock_t prevdiff=0; /* left side difference */
2458
2459 /*
2460 * If there's a previous (left) block, select a requested
2461 * start block based on it.
2462 */
2463 if (ap->prevp->br_startoff != NULLFILEOFF &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06002464 !isnullstartblock(ap->prevp->br_startblock) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 (prevbno = ap->prevp->br_startblock +
2466 ap->prevp->br_blockcount) &&
2467 ISVALID(prevbno, ap->prevp->br_startblock)) {
2468 /*
2469 * Calculate gap to end of previous block.
2470 */
2471 adjust = prevdiff = ap->off -
2472 (ap->prevp->br_startoff +
2473 ap->prevp->br_blockcount);
2474 /*
2475 * Figure the startblock based on the previous block's
2476 * end and the gap size.
2477 * Heuristic!
2478 * If the gap is large relative to the piece we're
2479 * allocating, or using it gives us an invalid block
2480 * number, then just use the end of the previous block.
2481 */
2482 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2483 ISVALID(prevbno + prevdiff,
2484 ap->prevp->br_startblock))
2485 prevbno += adjust;
2486 else
2487 prevdiff += adjust;
2488 /*
2489 * If the firstblock forbids it, can't use it,
2490 * must use default.
2491 */
2492 if (!rt && !nullfb &&
2493 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2494 prevbno = NULLFSBLOCK;
2495 }
2496 /*
2497 * No previous block or can't follow it, just default.
2498 */
2499 else
2500 prevbno = NULLFSBLOCK;
2501 /*
2502 * If there's a following (right) block, select a requested
2503 * start block based on it.
2504 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06002505 if (!isnullstartblock(ap->gotp->br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 /*
2507 * Calculate gap to start of next block.
2508 */
2509 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2510 /*
2511 * Figure the startblock based on the next block's
2512 * start and the gap size.
2513 */
2514 gotbno = ap->gotp->br_startblock;
2515 /*
2516 * Heuristic!
2517 * If the gap is large relative to the piece we're
2518 * allocating, or using it gives us an invalid block
2519 * number, then just use the start of the next block
2520 * offset by our length.
2521 */
2522 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2523 ISVALID(gotbno - gotdiff, gotbno))
2524 gotbno -= adjust;
2525 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2526 gotbno -= ap->alen;
2527 gotdiff += adjust - ap->alen;
2528 } else
2529 gotdiff += adjust;
2530 /*
2531 * If the firstblock forbids it, can't use it,
2532 * must use default.
2533 */
2534 if (!rt && !nullfb &&
2535 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2536 gotbno = NULLFSBLOCK;
2537 }
2538 /*
2539 * No next block, just default.
2540 */
2541 else
2542 gotbno = NULLFSBLOCK;
2543 /*
2544 * If both valid, pick the better one, else the only good
2545 * one, else ap->rval is already set (to 0 or the inode block).
2546 */
2547 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2548 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2549 else if (prevbno != NULLFSBLOCK)
2550 ap->rval = prevbno;
2551 else if (gotbno != NULLFSBLOCK)
2552 ap->rval = gotbno;
2553 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002554#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11002555}
2556
2557STATIC int
2558xfs_bmap_rtalloc(
2559 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2560{
2561 xfs_alloctype_t atype = 0; /* type for allocation routines */
2562 int error; /* error return value */
2563 xfs_mount_t *mp; /* mount point structure */
2564 xfs_extlen_t prod = 0; /* product factor for allocators */
2565 xfs_extlen_t ralen = 0; /* realtime allocation length */
2566 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002567 xfs_rtblock_t rtb;
2568
2569 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002570 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002571 prod = align / mp->m_sb.sb_rextsize;
2572 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2573 align, 1, ap->eof, 0,
2574 ap->conv, &ap->off, &ap->alen);
2575 if (error)
2576 return error;
2577 ASSERT(ap->alen);
2578 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2579
2580 /*
2581 * If the offset & length are not perfectly aligned
2582 * then kill prod, it will just get us in trouble.
2583 */
2584 if (do_mod(ap->off, align) || ap->alen % align)
2585 prod = 1;
2586 /*
2587 * Set ralen to be the actual requested length in rtextents.
2588 */
2589 ralen = ap->alen / mp->m_sb.sb_rextsize;
2590 /*
2591 * If the old value was close enough to MAXEXTLEN that
2592 * we rounded up to it, cut it back so it's valid again.
2593 * Note that if it's a really large request (bigger than
2594 * MAXEXTLEN), we don't hear about that number, and can't
2595 * adjust the starting point to match it.
2596 */
2597 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2598 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2599 /*
2600 * If it's an allocation to an empty file at offset 0,
2601 * pick an extent that will space things out in the rt area.
2602 */
2603 if (ap->eof && ap->off == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10002604 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2605
Nathan Scotta365bdd2006-03-14 13:34:16 +11002606 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2607 if (error)
2608 return error;
2609 ap->rval = rtx * mp->m_sb.sb_rextsize;
2610 } else {
2611 ap->rval = 0;
2612 }
2613
2614 xfs_bmap_adjacent(ap);
2615
2616 /*
2617 * Realtime allocation, done through xfs_rtallocate_extent.
2618 */
2619 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2620 do_div(ap->rval, mp->m_sb.sb_rextsize);
2621 rtb = ap->rval;
2622 ap->alen = ralen;
2623 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2624 &ralen, atype, ap->wasdel, prod, &rtb)))
2625 return error;
2626 if (rtb == NULLFSBLOCK && prod > 1 &&
2627 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2628 ap->alen, &ralen, atype,
2629 ap->wasdel, 1, &rtb)))
2630 return error;
2631 ap->rval = rtb;
2632 if (ap->rval != NULLFSBLOCK) {
2633 ap->rval *= mp->m_sb.sb_rextsize;
2634 ralen *= mp->m_sb.sb_rextsize;
2635 ap->alen = ralen;
2636 ap->ip->i_d.di_nblocks += ralen;
2637 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2638 if (ap->wasdel)
2639 ap->ip->i_delayed_blks -= ralen;
2640 /*
2641 * Adjust the disk quota also. This was reserved
2642 * earlier.
2643 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002644 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002645 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2646 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2647 } else {
2648 ap->alen = 0;
2649 }
2650 return 0;
2651}
2652
2653STATIC int
2654xfs_bmap_btalloc(
2655 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2656{
2657 xfs_mount_t *mp; /* mount point structure */
2658 xfs_alloctype_t atype = 0; /* type for allocation routines */
2659 xfs_extlen_t align; /* minimum allocation alignment */
2660 xfs_agnumber_t ag;
2661 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2662 xfs_agnumber_t startag;
2663 xfs_alloc_arg_t args;
2664 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002665 xfs_extlen_t nextminlen = 0;
2666 xfs_perag_t *pag;
2667 int nullfb; /* true if ap->firstblock isn't set */
2668 int isaligned;
2669 int notinit;
2670 int tryagain;
2671 int error;
2672
2673 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002674 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002675 if (unlikely(align)) {
2676 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2677 align, 0, ap->eof, 0, ap->conv,
2678 &ap->off, &ap->alen);
2679 ASSERT(!error);
2680 ASSERT(ap->alen);
2681 }
2682 nullfb = ap->firstblock == NULLFSBLOCK;
2683 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10002684 if (nullfb) {
2685 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2686 ag = xfs_filestream_lookup_ag(ap->ip);
2687 ag = (ag != NULLAGNUMBER) ? ag : 0;
2688 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2689 } else {
2690 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2691 }
2692 } else
Nathan Scotta365bdd2006-03-14 13:34:16 +11002693 ap->rval = ap->firstblock;
2694
2695 xfs_bmap_adjacent(ap);
2696
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 /*
2698 * If allowed, use ap->rval; otherwise must use firstblock since
2699 * it's in the right allocation group.
2700 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002701 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 ;
2703 else
2704 ap->rval = ap->firstblock;
2705 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 * Normal allocation, done through xfs_alloc_vextent.
2707 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002708 tryagain = isaligned = 0;
2709 args.tp = ap->tp;
2710 args.mp = mp;
2711 args.fsbno = ap->rval;
2712 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
Yingping Lud210a282006-06-09 14:55:18 +10002713 args.firstblock = ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002714 blen = 0;
2715 if (nullfb) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002716 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2717 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2718 else
2719 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002720 args.total = ap->total;
David Chinner2a82b8b2007-07-11 11:09:12 +10002721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10002723 * Search for an allocation group with a single extent
2724 * large enough for the request.
2725 *
2726 * If one isn't found, then adjust the minimum allocation
2727 * size to the largest space found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002729 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
David Chinner2a82b8b2007-07-11 11:09:12 +10002730 if (startag == NULLAGNUMBER)
2731 startag = ag = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002732 notinit = 0;
2733 down_read(&mp->m_peraglock);
2734 while (blen < ap->alen) {
2735 pag = &mp->m_perag[ag];
2736 if (!pag->pagf_init &&
2737 (error = xfs_alloc_pagf_init(mp, args.tp,
2738 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2739 up_read(&mp->m_peraglock);
2740 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002742 /*
2743 * See xfs_alloc_fix_freelist...
2744 */
2745 if (pag->pagf_init) {
Dave Chinner6cc87642009-03-16 08:29:46 +01002746 xfs_extlen_t longest;
2747 longest = xfs_alloc_longest_free_extent(mp, pag);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002748 if (blen < longest)
2749 blen = longest;
2750 } else
2751 notinit = 1;
David Chinner2a82b8b2007-07-11 11:09:12 +10002752
2753 if (xfs_inode_is_filestream(ap->ip)) {
2754 if (blen >= ap->alen)
2755 break;
2756
2757 if (ap->userdata) {
2758 /*
2759 * If startag is an invalid AG, we've
2760 * come here once before and
2761 * xfs_filestream_new_ag picked the
2762 * best currently available.
2763 *
2764 * Don't continue looping, since we
2765 * could loop forever.
2766 */
2767 if (startag == NULLAGNUMBER)
2768 break;
2769
2770 error = xfs_filestream_new_ag(ap, &ag);
2771 if (error) {
2772 up_read(&mp->m_peraglock);
2773 return error;
2774 }
2775
2776 /* loop again to set 'blen'*/
2777 startag = NULLAGNUMBER;
2778 continue;
2779 }
2780 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002781 if (++ag == mp->m_sb.sb_agcount)
2782 ag = 0;
2783 if (ag == startag)
2784 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002786 up_read(&mp->m_peraglock);
2787 /*
2788 * Since the above loop did a BUF_TRYLOCK, it is
2789 * possible that there is space for this request.
2790 */
2791 if (notinit || blen < ap->minlen)
2792 args.minlen = ap->minlen;
2793 /*
2794 * If the best seen length is less than the request
2795 * length, use the best as the minimum.
2796 */
2797 else if (blen < ap->alen)
2798 args.minlen = blen;
2799 /*
2800 * Otherwise we've seen an extent as big as alen,
2801 * use that as the minimum.
2802 */
2803 else
2804 args.minlen = ap->alen;
David Chinner2a82b8b2007-07-11 11:09:12 +10002805
2806 /*
2807 * set the failure fallback case to look in the selected
2808 * AG as the stream may have moved.
2809 */
2810 if (xfs_inode_is_filestream(ap->ip))
2811 ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002812 } else if (ap->low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002813 if (xfs_inode_is_filestream(ap->ip))
2814 args.type = XFS_ALLOCTYPE_FIRST_AG;
2815 else
2816 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002817 args.total = args.minlen = ap->minlen;
2818 } else {
2819 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2820 args.total = ap->total;
2821 args.minlen = ap->minlen;
2822 }
David Chinner957d0eb2007-06-18 16:50:37 +10002823 /* apply extent size hints if obtained earlier */
2824 if (unlikely(align)) {
2825 args.prod = align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002826 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2827 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11002828 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11002829 args.prod = 1;
2830 args.mod = 0;
2831 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002832 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002833 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2834 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2835 }
2836 /*
2837 * If we are not low on available data blocks, and the
2838 * underlying logical volume manager is a stripe, and
2839 * the file offset is zero then try to allocate data
2840 * blocks on stripe unit boundary.
2841 * NOTE: ap->aeof is only set if the allocation length
2842 * is >= the stripe unit and the allocation offset is
2843 * at the end of file.
2844 */
2845 if (!ap->low && ap->aeof) {
2846 if (!ap->off) {
2847 args.alignment = mp->m_dalign;
2848 atype = args.type;
2849 isaligned = 1;
2850 /*
2851 * Adjust for alignment
2852 */
2853 if (blen > args.alignment && blen <= ap->alen)
2854 args.minlen = blen - args.alignment;
2855 args.minalignslop = 0;
2856 } else {
2857 /*
2858 * First try an exact bno allocation.
2859 * If it fails then do a near or start bno
2860 * allocation with alignment turned on.
2861 */
2862 atype = args.type;
2863 tryagain = 1;
2864 args.type = XFS_ALLOCTYPE_THIS_BNO;
2865 args.alignment = 1;
2866 /*
2867 * Compute the minlen+alignment for the
2868 * next case. Set slop so that the value
2869 * of minlen+alignment+slop doesn't go up
2870 * between the calls.
2871 */
2872 if (blen > mp->m_dalign && blen <= ap->alen)
2873 nextminlen = blen - mp->m_dalign;
2874 else
2875 nextminlen = args.minlen;
2876 if (nextminlen + mp->m_dalign > args.minlen + 1)
2877 args.minalignslop =
2878 nextminlen + mp->m_dalign -
2879 args.minlen - 1;
2880 else
2881 args.minalignslop = 0;
2882 }
2883 } else {
2884 args.alignment = 1;
2885 args.minalignslop = 0;
2886 }
2887 args.minleft = ap->minleft;
2888 args.wasdel = ap->wasdel;
2889 args.isfl = 0;
2890 args.userdata = ap->userdata;
2891 if ((error = xfs_alloc_vextent(&args)))
2892 return error;
2893 if (tryagain && args.fsbno == NULLFSBLOCK) {
2894 /*
2895 * Exact allocation failed. Now try with alignment
2896 * turned on.
2897 */
2898 args.type = atype;
2899 args.fsbno = ap->rval;
2900 args.alignment = mp->m_dalign;
2901 args.minlen = nextminlen;
2902 args.minalignslop = 0;
2903 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 if ((error = xfs_alloc_vextent(&args)))
2905 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002906 }
2907 if (isaligned && args.fsbno == NULLFSBLOCK) {
2908 /*
2909 * allocation failed, so turn off alignment and
2910 * try again.
2911 */
2912 args.type = atype;
2913 args.fsbno = ap->rval;
2914 args.alignment = 0;
2915 if ((error = xfs_alloc_vextent(&args)))
2916 return error;
2917 }
2918 if (args.fsbno == NULLFSBLOCK && nullfb &&
2919 args.minlen > ap->minlen) {
2920 args.minlen = ap->minlen;
2921 args.type = XFS_ALLOCTYPE_START_BNO;
2922 args.fsbno = ap->rval;
2923 if ((error = xfs_alloc_vextent(&args)))
2924 return error;
2925 }
2926 if (args.fsbno == NULLFSBLOCK && nullfb) {
2927 args.fsbno = 0;
2928 args.type = XFS_ALLOCTYPE_FIRST_AG;
2929 args.total = ap->minlen;
2930 args.minleft = 0;
2931 if ((error = xfs_alloc_vextent(&args)))
2932 return error;
2933 ap->low = 1;
2934 }
2935 if (args.fsbno != NULLFSBLOCK) {
2936 ap->firstblock = ap->rval = args.fsbno;
2937 ASSERT(nullfb || fb_agno == args.agno ||
2938 (ap->low && fb_agno < args.agno));
2939 ap->alen = args.len;
2940 ap->ip->i_d.di_nblocks += args.len;
2941 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2942 if (ap->wasdel)
2943 ap->ip->i_delayed_blks -= args.len;
2944 /*
2945 * Adjust the disk quota also. This was reserved
2946 * earlier.
2947 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002948 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002949 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2950 XFS_TRANS_DQ_BCOUNT,
2951 (long) args.len);
2952 } else {
2953 ap->rval = NULLFSBLOCK;
2954 ap->alen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 }
2956 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002957}
2958
2959/*
2960 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2961 * It figures out where to ask the underlying allocator to put the new extent.
2962 */
2963STATIC int
2964xfs_bmap_alloc(
2965 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2966{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002967 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002968 return xfs_bmap_rtalloc(ap);
2969 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
2971
2972/*
2973 * Transform a btree format file with only one leaf node, where the
2974 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002975 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 * give up the space for the btree root and pitch the leaf block.
2977 */
2978STATIC int /* error */
2979xfs_bmap_btree_to_extents(
2980 xfs_trans_t *tp, /* transaction pointer */
2981 xfs_inode_t *ip, /* incore inode pointer */
2982 xfs_btree_cur_t *cur, /* btree cursor */
2983 int *logflagsp, /* inode logging flags */
2984 int whichfork) /* data or attr fork */
2985{
2986 /* REFERENCED */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002987 struct xfs_btree_block *cblock;/* child btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 xfs_fsblock_t cbno; /* child block number */
2989 xfs_buf_t *cbp; /* child block's buffer */
2990 int error; /* error return value */
2991 xfs_ifork_t *ifp; /* inode fork data */
2992 xfs_mount_t *mp; /* mount point structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10002993 __be64 *pp; /* ptr to block address */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002994 struct xfs_btree_block *rblock;/* root btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Christoph Hellwig60197e82008-10-30 17:11:19 +11002996 mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 ifp = XFS_IFORK_PTR(ip, whichfork);
2998 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2999 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
3000 rblock = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003001 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
3002 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
Christoph Hellwig60197e82008-10-30 17:11:19 +11003003 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
3004 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10003005 cbno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 *logflagsp = 0;
3007#ifdef DEBUG
Christoph Hellwig576039c2006-09-28 10:58:06 +10003008 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 return error;
3010#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
3012 XFS_BMAP_BTREE_REF)))
3013 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003014 cblock = XFS_BUF_TO_BLOCK(cbp);
3015 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 return error;
3017 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
3018 ip->i_d.di_nblocks--;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003019 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 xfs_trans_binval(tp, cbp);
3021 if (cur->bc_bufs[0] == cbp)
3022 cur->bc_bufs[0] = NULL;
3023 xfs_iroot_realloc(ip, -1, whichfork);
3024 ASSERT(ifp->if_broot == NULL);
3025 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
3026 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003027 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 return 0;
3029}
3030
3031/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003032 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 * after removing space (or undoing a delayed allocation).
3034 */
3035STATIC int /* error */
3036xfs_bmap_del_extent(
3037 xfs_inode_t *ip, /* incore inode pointer */
3038 xfs_trans_t *tp, /* current transaction pointer */
3039 xfs_extnum_t idx, /* extent number to update/delete */
3040 xfs_bmap_free_t *flist, /* list of extents to be freed */
3041 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003042 xfs_bmbt_irec_t *del, /* data to remove from extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003044 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 int whichfork, /* data or attr fork */
3046 int rsvd) /* OK to allocate reserved blocks */
3047{
3048 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
3049 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
3050 xfs_fsblock_t del_endblock=0; /* first block past del */
3051 xfs_fileoff_t del_endoff; /* first offset past del */
3052 int delay; /* current block is delayed allocated */
3053 int do_fx; /* free extent at end of routine */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003054 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 int error; /* error return value */
3056 int flags; /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 xfs_bmbt_irec_t got; /* current extent entry */
3058 xfs_fileoff_t got_endoff; /* first offset past got */
3059 int i; /* temp state */
3060 xfs_ifork_t *ifp; /* inode fork pointer */
3061 xfs_mount_t *mp; /* mount structure */
3062 xfs_filblks_t nblks; /* quota/sb block count */
3063 xfs_bmbt_irec_t new; /* new record to be inserted */
3064 /* REFERENCED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 uint qfield; /* quota field to update */
3066 xfs_filblks_t temp; /* for indirect length calculations */
3067 xfs_filblks_t temp2; /* for indirect length calculations */
3068
3069 XFS_STATS_INC(xs_del_exlist);
3070 mp = ip->i_mount;
3071 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003072 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
3073 (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 ASSERT(del->br_blockcount > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003075 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 xfs_bmbt_get_all(ep, &got);
3077 ASSERT(got.br_startoff <= del->br_startoff);
3078 del_endoff = del->br_startoff + del->br_blockcount;
3079 got_endoff = got.br_startoff + got.br_blockcount;
3080 ASSERT(got_endoff >= del_endoff);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003081 delay = isnullstartblock(got.br_startblock);
3082 ASSERT(isnullstartblock(del->br_startblock) == delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 flags = 0;
3084 qfield = 0;
3085 error = 0;
3086 /*
3087 * If deleting a real allocation, must free up the disk space.
3088 */
3089 if (!delay) {
3090 flags = XFS_ILOG_CORE;
3091 /*
3092 * Realtime allocation. Free it and record di_nblocks update.
3093 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003094 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 xfs_fsblock_t bno;
3096 xfs_filblks_t len;
3097
3098 ASSERT(do_mod(del->br_blockcount,
3099 mp->m_sb.sb_rextsize) == 0);
3100 ASSERT(do_mod(del->br_startblock,
3101 mp->m_sb.sb_rextsize) == 0);
3102 bno = del->br_startblock;
3103 len = del->br_blockcount;
3104 do_div(bno, mp->m_sb.sb_rextsize);
3105 do_div(len, mp->m_sb.sb_rextsize);
3106 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3107 (xfs_extlen_t)len)))
3108 goto done;
3109 do_fx = 0;
3110 nblks = len * mp->m_sb.sb_rextsize;
3111 qfield = XFS_TRANS_DQ_RTBCOUNT;
3112 }
3113 /*
3114 * Ordinary allocation.
3115 */
3116 else {
3117 do_fx = 1;
3118 nblks = del->br_blockcount;
3119 qfield = XFS_TRANS_DQ_BCOUNT;
3120 }
3121 /*
3122 * Set up del_endblock and cur for later.
3123 */
3124 del_endblock = del->br_startblock + del->br_blockcount;
3125 if (cur) {
3126 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3127 got.br_startblock, got.br_blockcount,
3128 &i)))
3129 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003130 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 }
3132 da_old = da_new = 0;
3133 } else {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003134 da_old = startblockval(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 da_new = 0;
3136 nblks = 0;
3137 do_fx = 0;
3138 }
3139 /*
3140 * Set flag value to use in switch statement.
3141 * Left-contig is 2, right-contig is 1.
3142 */
3143 switch (((got.br_startoff == del->br_startoff) << 1) |
3144 (got_endoff == del_endoff)) {
3145 case 3:
3146 /*
3147 * Matches the whole extent. Delete the entry.
3148 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10003149 XFS_BMAP_TRACE_DELETE("3", ip, idx, 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003150 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 ifp->if_lastex = idx;
3152 if (delay)
3153 break;
3154 XFS_IFORK_NEXT_SET(ip, whichfork,
3155 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3156 flags |= XFS_ILOG_CORE;
3157 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003158 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 break;
3160 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003161 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003163 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 break;
3165
3166 case 2:
3167 /*
3168 * Deleting the first part of the extent.
3169 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10003170 XFS_BMAP_TRACE_PRE_UPDATE("2", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 xfs_bmbt_set_startoff(ep, del_endoff);
3172 temp = got.br_blockcount - del->br_blockcount;
3173 xfs_bmbt_set_blockcount(ep, temp);
3174 ifp->if_lastex = idx;
3175 if (delay) {
3176 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3177 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003178 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10003179 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 whichfork);
3181 da_new = temp;
3182 break;
3183 }
3184 xfs_bmbt_set_startblock(ep, del_endblock);
Eric Sandeen3a59c942007-07-11 11:09:47 +10003185 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003187 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 break;
3189 }
3190 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3191 got.br_blockcount - del->br_blockcount,
3192 got.br_state)))
3193 goto done;
3194 break;
3195
3196 case 1:
3197 /*
3198 * Deleting the last part of the extent.
3199 */
3200 temp = got.br_blockcount - del->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10003201 XFS_BMAP_TRACE_PRE_UPDATE("1", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 xfs_bmbt_set_blockcount(ep, temp);
3203 ifp->if_lastex = idx;
3204 if (delay) {
3205 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3206 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003207 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10003208 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 whichfork);
3210 da_new = temp;
3211 break;
3212 }
Eric Sandeen3a59c942007-07-11 11:09:47 +10003213 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003215 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 break;
3217 }
3218 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3219 got.br_startblock,
3220 got.br_blockcount - del->br_blockcount,
3221 got.br_state)))
3222 goto done;
3223 break;
3224
3225 case 0:
3226 /*
3227 * Deleting the middle of the extent.
3228 */
3229 temp = del->br_startoff - got.br_startoff;
Eric Sandeen3a59c942007-07-11 11:09:47 +10003230 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 xfs_bmbt_set_blockcount(ep, temp);
3232 new.br_startoff = del_endoff;
3233 temp2 = got_endoff - del_endoff;
3234 new.br_blockcount = temp2;
3235 new.br_state = got.br_state;
3236 if (!delay) {
3237 new.br_startblock = del_endblock;
3238 flags |= XFS_ILOG_CORE;
3239 if (cur) {
3240 if ((error = xfs_bmbt_update(cur,
3241 got.br_startoff,
3242 got.br_startblock, temp,
3243 got.br_state)))
3244 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11003245 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 goto done;
3247 cur->bc_rec.b = new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003248 error = xfs_btree_insert(cur, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 if (error && error != ENOSPC)
3250 goto done;
3251 /*
3252 * If get no-space back from btree insert,
3253 * it tried a split, and we have a zero
3254 * block reservation.
3255 * Fix up our state and return the error.
3256 */
3257 if (error == ENOSPC) {
3258 /*
3259 * Reset the cursor, don't trust
3260 * it after any insert operation.
3261 */
3262 if ((error = xfs_bmbt_lookup_eq(cur,
3263 got.br_startoff,
3264 got.br_startblock,
3265 temp, &i)))
3266 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003267 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 /*
3269 * Update the btree record back
3270 * to the original value.
3271 */
3272 if ((error = xfs_bmbt_update(cur,
3273 got.br_startoff,
3274 got.br_startblock,
3275 got.br_blockcount,
3276 got.br_state)))
3277 goto done;
3278 /*
3279 * Reset the extent record back
3280 * to the original value.
3281 */
3282 xfs_bmbt_set_blockcount(ep,
3283 got.br_blockcount);
3284 flags = 0;
3285 error = XFS_ERROR(ENOSPC);
3286 goto done;
3287 }
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003288 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 } else
Eric Sandeen9d87c312009-01-14 23:22:07 -06003290 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 XFS_IFORK_NEXT_SET(ip, whichfork,
3292 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3293 } else {
3294 ASSERT(whichfork == XFS_DATA_FORK);
3295 temp = xfs_bmap_worst_indlen(ip, temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003296 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003298 new.br_startblock = nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 da_new = temp + temp2;
3300 while (da_new > da_old) {
3301 if (temp) {
3302 temp--;
3303 da_new--;
3304 xfs_bmbt_set_startblock(ep,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003305 nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 }
3307 if (da_new == da_old)
3308 break;
3309 if (temp2) {
3310 temp2--;
3311 da_new--;
3312 new.br_startblock =
Eric Sandeen9d87c312009-01-14 23:22:07 -06003313 nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 }
3315 }
3316 }
Eric Sandeen3a59c942007-07-11 11:09:47 +10003317 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, whichfork);
3318 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 1, &new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003320 xfs_iext_insert(ifp, idx + 1, 1, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 ifp->if_lastex = idx + 1;
3322 break;
3323 }
3324 /*
3325 * If we need to, add to list of extents to delete.
3326 */
3327 if (do_fx)
3328 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3329 mp);
3330 /*
3331 * Adjust inode # blocks in the file.
3332 */
3333 if (nblks)
3334 ip->i_d.di_nblocks -= nblks;
3335 /*
3336 * Adjust quota data.
3337 */
3338 if (qfield)
Christoph Hellwig7d095252009-06-08 15:33:32 +02003339 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
3341 /*
3342 * Account for change in delayed indirect blocks.
3343 * Nothing to do for disk quota accounting here.
3344 */
3345 ASSERT(da_old >= da_new);
3346 if (da_old > da_new)
David Chinner20f4ebf2007-02-10 18:36:10 +11003347 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 rsvd);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003349 if (delta) {
3350 /* DELTA: report the original extent. */
3351 if (delta->xed_startoff > got.br_startoff)
3352 delta->xed_startoff = got.br_startoff;
3353 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3354 delta->xed_blockcount = got.br_startoff +
3355 got.br_blockcount;
3356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357done:
3358 *logflagsp = flags;
3359 return error;
3360}
3361
3362/*
3363 * Remove the entry "free" from the free item list. Prev points to the
3364 * previous entry, unless "free" is the head of the list.
3365 */
3366STATIC void
3367xfs_bmap_del_free(
3368 xfs_bmap_free_t *flist, /* free item list header */
3369 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3370 xfs_bmap_free_item_t *free) /* list item to be freed */
3371{
3372 if (prev)
3373 prev->xbfi_next = free->xbfi_next;
3374 else
3375 flist->xbf_first = free->xbfi_next;
3376 flist->xbf_count--;
3377 kmem_zone_free(xfs_bmap_free_item_zone, free);
3378}
3379
3380/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 * Convert an extents-format file into a btree-format file.
3382 * The new file will have a root block (in the inode) and a single child block.
3383 */
3384STATIC int /* error */
3385xfs_bmap_extents_to_btree(
3386 xfs_trans_t *tp, /* transaction pointer */
3387 xfs_inode_t *ip, /* incore inode pointer */
3388 xfs_fsblock_t *firstblock, /* first-block-allocated */
3389 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3390 xfs_btree_cur_t **curp, /* cursor returned to caller */
3391 int wasdel, /* converting a delayed alloc */
3392 int *logflagsp, /* inode logging flags */
3393 int whichfork) /* data or attr fork */
3394{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003395 struct xfs_btree_block *ablock; /* allocated (child) bt block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 xfs_buf_t *abp; /* buffer for ablock */
3397 xfs_alloc_arg_t args; /* allocation arguments */
3398 xfs_bmbt_rec_t *arp; /* child record pointer */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003399 struct xfs_btree_block *block; /* btree root block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 xfs_btree_cur_t *cur; /* bmap btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003401 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003403 xfs_extnum_t i, cnt; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 xfs_ifork_t *ifp; /* inode fork pointer */
3405 xfs_bmbt_key_t *kp; /* root block key pointer */
3406 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003407 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3409
3410 ifp = XFS_IFORK_PTR(ip, whichfork);
3411 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3412 ASSERT(ifp->if_ext_max ==
3413 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3414 /*
3415 * Make space in the inode incore.
3416 */
3417 xfs_iroot_realloc(ip, 1, whichfork);
3418 ifp->if_flags |= XFS_IFBROOT;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003419
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 /*
3421 * Fill in the root.
3422 */
3423 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003424 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3425 block->bb_level = cpu_to_be16(1);
3426 block->bb_numrecs = cpu_to_be16(1);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003427 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3428 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3429
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 /*
3431 * Need a cursor. Can't allocate until bb_level is filled in.
3432 */
3433 mp = ip->i_mount;
Christoph Hellwig561f7d12008-10-30 16:53:59 +11003434 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 cur->bc_private.b.firstblock = *firstblock;
3436 cur->bc_private.b.flist = flist;
3437 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3438 /*
3439 * Convert to a btree with two levels, one record in root.
3440 */
3441 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3442 args.tp = tp;
3443 args.mp = mp;
Yingping Lud210a282006-06-09 14:55:18 +10003444 args.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 if (*firstblock == NULLFSBLOCK) {
3446 args.type = XFS_ALLOCTYPE_START_BNO;
3447 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3448 } else if (flist->xbf_low) {
3449 args.type = XFS_ALLOCTYPE_START_BNO;
3450 args.fsbno = *firstblock;
3451 } else {
3452 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3453 args.fsbno = *firstblock;
3454 }
3455 args.minlen = args.maxlen = args.prod = 1;
3456 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3457 args.minalignslop = 0;
3458 args.wasdel = wasdel;
3459 *logflagsp = 0;
3460 if ((error = xfs_alloc_vextent(&args))) {
3461 xfs_iroot_realloc(ip, -1, whichfork);
3462 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3463 return error;
3464 }
3465 /*
3466 * Allocation can't fail, the space was reserved.
3467 */
3468 ASSERT(args.fsbno != NULLFSBLOCK);
3469 ASSERT(*firstblock == NULLFSBLOCK ||
3470 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3471 (flist->xbf_low &&
3472 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3473 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3474 cur->bc_private.b.allocated++;
3475 ip->i_d.di_nblocks++;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003476 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3478 /*
3479 * Fill in the child block.
3480 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003481 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003482 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 ablock->bb_level = 0;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003484 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3485 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003486 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003488 for (cnt = i = 0; i < nextents; i++) {
3489 ep = xfs_iext_get_ext(ifp, i);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003490 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10003491 arp->l0 = cpu_to_be64(ep->l0);
3492 arp->l1 = cpu_to_be64(ep->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 arp++; cnt++;
3494 }
3495 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11003496 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003497 xfs_btree_set_numrecs(ablock, cnt);
3498
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 /*
3500 * Fill in the root key and pointer.
3501 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11003502 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3503 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Christoph Hellwig8801bb92006-09-28 10:58:17 +10003504 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
Christoph Hellwig136341b2008-10-30 17:11:40 +11003505 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3506 be16_to_cpu(block->bb_level)));
Christoph Hellwig576039c2006-09-28 10:58:06 +10003507 *pp = cpu_to_be64(args.fsbno);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003508
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 /*
3510 * Do all this logging at the end so that
3511 * the root is at the right level.
3512 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11003513 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3514 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 ASSERT(*curp == NULL);
3516 *curp = cur;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003517 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 return 0;
3519}
3520
3521/*
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003522 * Calculate the default attribute fork offset for newly created inodes.
3523 */
3524uint
3525xfs_default_attroffset(
3526 struct xfs_inode *ip)
3527{
3528 struct xfs_mount *mp = ip->i_mount;
3529 uint offset;
3530
3531 if (mp->m_sb.sb_inodesize == 256) {
3532 offset = XFS_LITINO(mp) -
3533 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3534 } else {
3535 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3536 }
3537
3538 ASSERT(offset < XFS_LITINO(mp));
3539 return offset;
3540}
3541
3542/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11003543 * Helper routine to reset inode di_forkoff field when switching
3544 * attribute fork from local to extent format - we reset it where
3545 * possible to make space available for inline data fork extents.
3546 */
3547STATIC void
3548xfs_bmap_forkoff_reset(
3549 xfs_mount_t *mp,
3550 xfs_inode_t *ip,
3551 int whichfork)
3552{
3553 if (whichfork == XFS_ATTR_FORK &&
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003554 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3555 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3556 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3557 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3558
3559 if (dfl_forkoff > ip->i_d.di_forkoff) {
3560 ip->i_d.di_forkoff = dfl_forkoff;
3561 ip->i_df.if_ext_max =
3562 XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
3563 ip->i_afp->if_ext_max =
3564 XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t);
3565 }
Nathan Scottd8cc8902005-11-02 10:34:53 +11003566 }
3567}
3568
3569/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 * Convert a local file to an extents file.
3571 * This code is out of bounds for data forks of regular files,
3572 * since the file data needs to get logged so things will stay consistent.
3573 * (The bmap-level manipulations are ok, though).
3574 */
3575STATIC int /* error */
3576xfs_bmap_local_to_extents(
3577 xfs_trans_t *tp, /* transaction pointer */
3578 xfs_inode_t *ip, /* incore inode pointer */
3579 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3580 xfs_extlen_t total, /* total blocks needed by transaction */
3581 int *logflagsp, /* inode logging flags */
3582 int whichfork) /* data or attr fork */
3583{
3584 int error; /* error return value */
3585 int flags; /* logging flags returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 xfs_ifork_t *ifp; /* inode fork pointer */
3587
3588 /*
3589 * We don't want to deal with the case of keeping inode data inline yet.
3590 * So sending the data fork of a regular inode is invalid.
3591 */
3592 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3593 whichfork == XFS_DATA_FORK));
3594 ifp = XFS_IFORK_PTR(ip, whichfork);
3595 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3596 flags = 0;
3597 error = 0;
3598 if (ifp->if_bytes) {
3599 xfs_alloc_arg_t args; /* allocation arguments */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003600 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003601 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
3603 args.tp = tp;
3604 args.mp = ip->i_mount;
Yingping Lud210a282006-06-09 14:55:18 +10003605 args.firstblock = *firstblock;
Mandy Kirkconnellf020b672006-03-14 14:07:24 +11003606 ASSERT((ifp->if_flags &
3607 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 /*
3609 * Allocate a block. We know we need only one, since the
3610 * file currently fits in an inode.
3611 */
3612 if (*firstblock == NULLFSBLOCK) {
3613 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3614 args.type = XFS_ALLOCTYPE_START_BNO;
3615 } else {
3616 args.fsbno = *firstblock;
3617 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3618 }
3619 args.total = total;
3620 args.mod = args.minleft = args.alignment = args.wasdel =
3621 args.isfl = args.minalignslop = 0;
3622 args.minlen = args.maxlen = args.prod = 1;
3623 if ((error = xfs_alloc_vextent(&args)))
3624 goto done;
3625 /*
3626 * Can't fail, the space was reserved.
3627 */
3628 ASSERT(args.fsbno != NULLFSBLOCK);
3629 ASSERT(args.len == 1);
3630 *firstblock = args.fsbno;
3631 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3632 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3633 ifp->if_bytes);
3634 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003635 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003637 xfs_iext_add(ifp, 0, 1);
3638 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
Eric Sandeen3a59c942007-07-11 11:09:47 +10003640 XFS_BMAP_TRACE_POST_UPDATE("new", ip, 0, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3642 ip->i_d.di_nblocks = 1;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003643 xfs_trans_mod_dquot_byino(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 XFS_TRANS_DQ_BCOUNT, 1L);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003645 flags |= xfs_ilog_fext(whichfork);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003646 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003648 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 ifp->if_flags &= ~XFS_IFINLINE;
3651 ifp->if_flags |= XFS_IFEXTENTS;
3652 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3653 flags |= XFS_ILOG_CORE;
3654done:
3655 *logflagsp = flags;
3656 return error;
3657}
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003660 * Search the extent records for the entry containing block bno.
3661 * If bno lies in a hole, point to the next entry. If bno lies
3662 * past eof, *eofp will be set, and *prevp will contain the last
3663 * entry (null if none). Else, *lastxp will be set to the index
3664 * of the found entry; *gotp will contain the entry.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003665 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003666STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003667xfs_bmap_search_multi_extents(
3668 xfs_ifork_t *ifp, /* inode fork pointer */
3669 xfs_fileoff_t bno, /* block number searched for */
3670 int *eofp, /* out: end of file found */
3671 xfs_extnum_t *lastxp, /* out: last extent index */
3672 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3673 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3674{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003675 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003676 xfs_extnum_t lastx; /* last extent index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003677
3678 /*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003679 * Initialize the extent entry structure to catch access to
3680 * uninitialized br_startblock field.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003681 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003682 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3683 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3684 gotp->br_state = XFS_EXT_INVALID;
3685#if XFS_BIG_BLKNOS
3686 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3687#else
3688 gotp->br_startblock = 0xffffa5a5;
3689#endif
3690 prevp->br_startoff = NULLFILEOFF;
3691
3692 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3693 if (lastx > 0) {
3694 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3695 }
3696 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3697 xfs_bmbt_get_all(ep, gotp);
3698 *eofp = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003699 } else {
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003700 if (lastx > 0) {
3701 *gotp = *prevp;
3702 }
3703 *eofp = 1;
3704 ep = NULL;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003705 }
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003706 *lastxp = lastx;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003707 return ep;
3708}
3709
3710/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 * Search the extents list for the inode, for the extent containing bno.
3712 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3713 * *eofp will be set, and *prevp will contain the last entry (null if none).
3714 * Else, *lastxp will be set to the index of the found
3715 * entry; *gotp will contain the entry.
3716 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003717STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718xfs_bmap_search_extents(
3719 xfs_inode_t *ip, /* incore inode pointer */
3720 xfs_fileoff_t bno, /* block number searched for */
Nathan Scott572d95f2006-09-28 11:03:20 +10003721 int fork, /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 int *eofp, /* out: end of file found */
3723 xfs_extnum_t *lastxp, /* out: last extent index */
3724 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3725 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3726{
3727 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003728 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
3730 XFS_STATS_INC(xs_look_exlist);
Nathan Scott572d95f2006-09-28 11:03:20 +10003731 ifp = XFS_IFORK_PTR(ip, fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003733 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3734
Nathan Scott572d95f2006-09-28 11:03:20 +10003735 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3736 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3737 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3738 "Access to block zero in inode %llu "
3739 "start_block: %llx start_off: %llx "
3740 "blkcnt: %llx extent-state: %x lastx: %x\n",
3741 (unsigned long long)ip->i_ino,
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003742 (unsigned long long)gotp->br_startblock,
3743 (unsigned long long)gotp->br_startoff,
3744 (unsigned long long)gotp->br_blockcount,
Nathan Scott572d95f2006-09-28 11:03:20 +10003745 gotp->br_state, *lastxp);
3746 *lastxp = NULLEXTNUM;
3747 *eofp = 1;
3748 return NULL;
3749 }
3750 return ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751}
3752
3753
3754#ifdef XFS_BMAP_TRACE
3755ktrace_t *xfs_bmap_trace_buf;
3756
3757/*
3758 * Add a bmap trace buffer entry. Base routine for the others.
3759 */
3760STATIC void
3761xfs_bmap_trace_addentry(
3762 int opcode, /* operation */
Eric Sandeen3a59c942007-07-11 11:09:47 +10003763 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 char *desc, /* operation description */
3765 xfs_inode_t *ip, /* incore inode pointer */
3766 xfs_extnum_t idx, /* index of entry(ies) */
3767 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003768 xfs_bmbt_rec_host_t *r1, /* first record */
3769 xfs_bmbt_rec_host_t *r2, /* second record or null */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 int whichfork) /* data or attr fork */
3771{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003772 xfs_bmbt_rec_host_t tr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
3774 ASSERT(cnt == 1 || cnt == 2);
3775 ASSERT(r1 != NULL);
3776 if (cnt == 1) {
3777 ASSERT(r2 == NULL);
3778 r2 = &tr2;
3779 memset(&tr2, 0, sizeof(tr2));
3780 } else
3781 ASSERT(r2 != NULL);
3782 ktrace_enter(xfs_bmap_trace_buf,
3783 (void *)(__psint_t)(opcode | (whichfork << 16)),
3784 (void *)fname, (void *)desc, (void *)ip,
3785 (void *)(__psint_t)idx,
3786 (void *)(__psint_t)cnt,
3787 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3788 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3789 (void *)(__psunsigned_t)(r1->l0 >> 32),
3790 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3791 (void *)(__psunsigned_t)(r1->l1 >> 32),
3792 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3793 (void *)(__psunsigned_t)(r2->l0 >> 32),
3794 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3795 (void *)(__psunsigned_t)(r2->l1 >> 32),
3796 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3797 );
3798 ASSERT(ip->i_xtrace);
3799 ktrace_enter(ip->i_xtrace,
3800 (void *)(__psint_t)(opcode | (whichfork << 16)),
3801 (void *)fname, (void *)desc, (void *)ip,
3802 (void *)(__psint_t)idx,
3803 (void *)(__psint_t)cnt,
3804 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3805 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3806 (void *)(__psunsigned_t)(r1->l0 >> 32),
3807 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3808 (void *)(__psunsigned_t)(r1->l1 >> 32),
3809 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3810 (void *)(__psunsigned_t)(r2->l0 >> 32),
3811 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3812 (void *)(__psunsigned_t)(r2->l1 >> 32),
3813 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3814 );
3815}
3816
3817/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003818 * Add bmap trace entry prior to a call to xfs_iext_remove.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 */
3820STATIC void
3821xfs_bmap_trace_delete(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003822 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 char *desc, /* operation description */
3824 xfs_inode_t *ip, /* incore inode pointer */
3825 xfs_extnum_t idx, /* index of entry(entries) deleted */
3826 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3827 int whichfork) /* data or attr fork */
3828{
3829 xfs_ifork_t *ifp; /* inode fork pointer */
3830
3831 ifp = XFS_IFORK_PTR(ip, whichfork);
3832 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003833 cnt, xfs_iext_get_ext(ifp, idx),
3834 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 whichfork);
3836}
3837
3838/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003839 * Add bmap trace entry prior to a call to xfs_iext_insert, or
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 * reading in the extents list from the disk (in the btree).
3841 */
3842STATIC void
3843xfs_bmap_trace_insert(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003844 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 char *desc, /* operation description */
3846 xfs_inode_t *ip, /* incore inode pointer */
3847 xfs_extnum_t idx, /* index of entry(entries) inserted */
3848 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3849 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3850 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3851 int whichfork) /* data or attr fork */
3852{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003853 xfs_bmbt_rec_host_t tr1; /* compressed record 1 */
3854 xfs_bmbt_rec_host_t tr2; /* compressed record 2 if needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
3856 xfs_bmbt_set_all(&tr1, r1);
3857 if (cnt == 2) {
3858 ASSERT(r2 != NULL);
3859 xfs_bmbt_set_all(&tr2, r2);
3860 } else {
3861 ASSERT(cnt == 1);
3862 ASSERT(r2 == NULL);
3863 }
3864 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3865 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3866}
3867
3868/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003869 * Add bmap trace entry after updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 */
3871STATIC void
3872xfs_bmap_trace_post_update(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003873 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 char *desc, /* operation description */
3875 xfs_inode_t *ip, /* incore inode pointer */
3876 xfs_extnum_t idx, /* index of entry updated */
3877 int whichfork) /* data or attr fork */
3878{
3879 xfs_ifork_t *ifp; /* inode fork pointer */
3880
3881 ifp = XFS_IFORK_PTR(ip, whichfork);
3882 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003883 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884}
3885
3886/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003887 * Add bmap trace entry prior to updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 */
3889STATIC void
3890xfs_bmap_trace_pre_update(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003891 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 char *desc, /* operation description */
3893 xfs_inode_t *ip, /* incore inode pointer */
3894 xfs_extnum_t idx, /* index of entry to be updated */
3895 int whichfork) /* data or attr fork */
3896{
3897 xfs_ifork_t *ifp; /* inode fork pointer */
3898
3899 ifp = XFS_IFORK_PTR(ip, whichfork);
3900 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003901 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902}
3903#endif /* XFS_BMAP_TRACE */
3904
3905/*
3906 * Compute the worst-case number of indirect blocks that will be used
3907 * for ip's delayed extent of length "len".
3908 */
3909STATIC xfs_filblks_t
3910xfs_bmap_worst_indlen(
3911 xfs_inode_t *ip, /* incore inode pointer */
3912 xfs_filblks_t len) /* delayed extent length */
3913{
3914 int level; /* btree level number */
3915 int maxrecs; /* maximum record count at this level */
3916 xfs_mount_t *mp; /* mount structure */
3917 xfs_filblks_t rval; /* return value */
3918
3919 mp = ip->i_mount;
3920 maxrecs = mp->m_bmap_dmxr[0];
3921 for (level = 0, rval = 0;
3922 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3923 level++) {
3924 len += maxrecs - 1;
3925 do_div(len, maxrecs);
3926 rval += len;
3927 if (len == 1)
3928 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3929 level - 1;
3930 if (level == 0)
3931 maxrecs = mp->m_bmap_dmxr[1];
3932 }
3933 return rval;
3934}
3935
3936#if defined(XFS_RW_TRACE)
3937STATIC void
3938xfs_bunmap_trace(
3939 xfs_inode_t *ip,
3940 xfs_fileoff_t bno,
3941 xfs_filblks_t len,
3942 int flags,
3943 inst_t *ra)
3944{
3945 if (ip->i_rwtrace == NULL)
3946 return;
3947 ktrace_enter(ip->i_rwtrace,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003948 (void *)(__psint_t)XFS_BUNMAP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 (void *)ip,
3950 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3951 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3952 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3953 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3954 (void *)(__psint_t)len,
3955 (void *)(__psint_t)flags,
3956 (void *)(unsigned long)current_cpu(),
3957 (void *)ra,
3958 (void *)0,
3959 (void *)0,
3960 (void *)0,
3961 (void *)0,
3962 (void *)0,
3963 (void *)0);
3964}
3965#endif
3966
3967/*
3968 * Convert inode from non-attributed to attributed.
3969 * Must not be in a transaction, ip must not be locked.
3970 */
3971int /* error code */
3972xfs_bmap_add_attrfork(
3973 xfs_inode_t *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003974 int size, /* space new attribute needs */
3975 int rsvd) /* xact may use reserved blks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003978 xfs_bmap_free_t flist; /* freed extent records */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 xfs_trans_t *tp; /* transaction pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003981 int blks; /* space reservation */
3982 int version = 1; /* superblock attr version */
3983 int committed; /* xaction was committed */
3984 int logflags; /* logging flags */
3985 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
Nathan Scottd8cc8902005-11-02 10:34:53 +11003987 ASSERT(XFS_IFORK_Q(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 ASSERT(ip->i_df.if_ext_max ==
3989 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
Nathan Scottd8cc8902005-11-02 10:34:53 +11003990
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 mp = ip->i_mount;
3992 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3993 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3994 blks = XFS_ADDAFORK_SPACE_RES(mp);
3995 if (rsvd)
3996 tp->t_flags |= XFS_TRANS_RESERVE;
3997 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3998 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3999 goto error0;
4000 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02004001 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
4003 XFS_QMOPT_RES_REGBLKS);
4004 if (error) {
4005 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4006 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
4007 return error;
4008 }
4009 if (XFS_IFORK_Q(ip))
4010 goto error1;
4011 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
4012 /*
4013 * For inodes coming from pre-6.2 filesystems.
4014 */
4015 ASSERT(ip->i_d.di_aformat == 0);
4016 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
4017 }
4018 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwig0b1f91772008-08-13 16:13:09 +10004019 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4021 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4022 switch (ip->i_d.di_format) {
4023 case XFS_DINODE_FMT_DEV:
4024 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
4025 break;
4026 case XFS_DINODE_FMT_UUID:
4027 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
4028 break;
4029 case XFS_DINODE_FMT_LOCAL:
4030 case XFS_DINODE_FMT_EXTENTS:
4031 case XFS_DINODE_FMT_BTREE:
Nathan Scottd8cc8902005-11-02 10:34:53 +11004032 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
4033 if (!ip->i_d.di_forkoff)
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02004034 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
Nathan Scott13059ff2006-01-11 15:32:01 +11004035 else if (mp->m_flags & XFS_MOUNT_ATTR2)
Nathan Scottd8cc8902005-11-02 10:34:53 +11004036 version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 break;
4038 default:
4039 ASSERT(0);
4040 error = XFS_ERROR(EINVAL);
4041 goto error1;
4042 }
4043 ip->i_df.if_ext_max =
4044 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4045 ASSERT(ip->i_afp == NULL);
4046 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
4047 ip->i_afp->if_ext_max =
4048 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4049 ip->i_afp->if_flags = XFS_IFEXTENTS;
4050 logflags = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004051 xfs_bmap_init(&flist, &firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 switch (ip->i_d.di_format) {
4053 case XFS_DINODE_FMT_LOCAL:
4054 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
4055 &logflags);
4056 break;
4057 case XFS_DINODE_FMT_EXTENTS:
4058 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
4059 &flist, &logflags);
4060 break;
4061 case XFS_DINODE_FMT_BTREE:
4062 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
4063 &logflags);
4064 break;
4065 default:
4066 error = 0;
4067 break;
4068 }
4069 if (logflags)
4070 xfs_trans_log_inode(tp, ip, logflags);
4071 if (error)
4072 goto error2;
Eric Sandeen62118702008-03-06 13:44:28 +11004073 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
4074 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +11004075 __int64_t sbfields = 0;
4076
Eric Sandeen3685c2a2007-10-11 17:42:32 +10004077 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +11004078 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
4079 xfs_sb_version_addattr(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11004080 sbfields |= XFS_SB_VERSIONNUM;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004081 }
Eric Sandeen62118702008-03-06 13:44:28 +11004082 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
4083 xfs_sb_version_addattr2(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11004084 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
Nathan Scottd8cc8902005-11-02 10:34:53 +11004085 }
Nathan Scottda087ba2005-11-02 15:00:20 +11004086 if (sbfields) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10004087 spin_unlock(&mp->m_sb_lock);
Nathan Scottda087ba2005-11-02 15:00:20 +11004088 xfs_mod_sb(tp, sbfields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +10004090 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 }
Eric Sandeenf7c99b62007-02-10 18:37:16 +11004092 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 goto error2;
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004094 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 ASSERT(ip->i_df.if_ext_max ==
4096 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4097 return error;
4098error2:
4099 xfs_bmap_cancel(&flist);
4100error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4102error0:
4103 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4104 ASSERT(ip->i_df.if_ext_max ==
4105 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4106 return error;
4107}
4108
4109/*
4110 * Add the extent to the list of extents to be free at transaction end.
4111 * The list is maintained sorted (by block number).
4112 */
4113/* ARGSUSED */
4114void
4115xfs_bmap_add_free(
4116 xfs_fsblock_t bno, /* fs block number of extent */
4117 xfs_filblks_t len, /* length of extent */
4118 xfs_bmap_free_t *flist, /* list of extents */
4119 xfs_mount_t *mp) /* mount point structure */
4120{
4121 xfs_bmap_free_item_t *cur; /* current (next) element */
4122 xfs_bmap_free_item_t *new; /* new element */
4123 xfs_bmap_free_item_t *prev; /* previous element */
4124#ifdef DEBUG
4125 xfs_agnumber_t agno;
4126 xfs_agblock_t agbno;
4127
4128 ASSERT(bno != NULLFSBLOCK);
4129 ASSERT(len > 0);
4130 ASSERT(len <= MAXEXTLEN);
Eric Sandeen9d87c312009-01-14 23:22:07 -06004131 ASSERT(!isnullstartblock(bno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 agno = XFS_FSB_TO_AGNO(mp, bno);
4133 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4134 ASSERT(agno < mp->m_sb.sb_agcount);
4135 ASSERT(agbno < mp->m_sb.sb_agblocks);
4136 ASSERT(len < mp->m_sb.sb_agblocks);
4137 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4138#endif
4139 ASSERT(xfs_bmap_free_item_zone != NULL);
4140 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4141 new->xbfi_startblock = bno;
4142 new->xbfi_blockcount = (xfs_extlen_t)len;
4143 for (prev = NULL, cur = flist->xbf_first;
4144 cur != NULL;
4145 prev = cur, cur = cur->xbfi_next) {
4146 if (cur->xbfi_startblock >= bno)
4147 break;
4148 }
4149 if (prev)
4150 prev->xbfi_next = new;
4151 else
4152 flist->xbf_first = new;
4153 new->xbfi_next = cur;
4154 flist->xbf_count++;
4155}
4156
4157/*
4158 * Compute and fill in the value of the maximum depth of a bmap btree
4159 * in this filesystem. Done once, during mount.
4160 */
4161void
4162xfs_bmap_compute_maxlevels(
4163 xfs_mount_t *mp, /* file system mount structure */
4164 int whichfork) /* data or attr fork */
4165{
4166 int level; /* btree level */
4167 uint maxblocks; /* max blocks at this level */
4168 uint maxleafents; /* max leaf entries possible */
4169 int maxrootrecs; /* max records in root block */
4170 int minleafrecs; /* min records in leaf block */
4171 int minnoderecs; /* min records in node block */
4172 int sz; /* root block size */
4173
4174 /*
4175 * The maximum number of extents in a file, hence the maximum
4176 * number of leaf entries, is controlled by the type of di_nextents
4177 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4178 * (a signed 16-bit number, xfs_aextnum_t).
Tim Shimmin6d1337b2008-04-17 16:50:16 +10004179 *
4180 * Note that we can no longer assume that if we are in ATTR1 that
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02004181 * the fork offset of all the inodes will be
4182 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
4183 * with ATTR2 and then mounted back with ATTR1, keeping the
4184 * di_forkoff's fixed but probably at various positions. Therefore,
4185 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
4186 * of a minimum size available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11004188 if (whichfork == XFS_DATA_FORK) {
4189 maxleafents = MAXEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10004190 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11004191 } else {
4192 maxleafents = MAXAEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10004193 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11004194 }
Christoph Hellwig60197e82008-10-30 17:11:19 +11004195 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 minleafrecs = mp->m_bmap_dmnr[0];
4197 minnoderecs = mp->m_bmap_dmnr[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4199 for (level = 1; maxblocks > 1; level++) {
4200 if (maxblocks <= maxrootrecs)
4201 maxblocks = 1;
4202 else
4203 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4204 }
4205 mp->m_bm_maxlevels[whichfork] = level;
4206}
4207
4208/*
4209 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4210 * caller. Frees all the extents that need freeing, which must be done
4211 * last due to locking considerations. We never free any extents in
4212 * the first transaction. This is to allow the caller to make the first
4213 * transaction a synchronous one so that the pointers to the data being
4214 * broken in this transaction will be permanent before the data is actually
4215 * freed. This is necessary to prevent blocks from being reallocated
4216 * and written to before the free and reallocation are actually permanent.
4217 * We do not just make the first transaction synchronous here, because
4218 * there are more efficient ways to gain the same protection in some cases
4219 * (see the file truncation code).
4220 *
4221 * Return 1 if the given transaction was committed and a new one
4222 * started, and 0 otherwise in the committed parameter.
4223 */
4224/*ARGSUSED*/
4225int /* error */
4226xfs_bmap_finish(
4227 xfs_trans_t **tp, /* transaction pointer addr */
4228 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 int *committed) /* xact committed or not */
4230{
4231 xfs_efd_log_item_t *efd; /* extent free data */
4232 xfs_efi_log_item_t *efi; /* extent free intention */
4233 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004234 xfs_bmap_free_item_t *free; /* free extent item */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 unsigned int logres; /* new log reservation */
4236 unsigned int logcount; /* new log count */
4237 xfs_mount_t *mp; /* filesystem mount structure */
4238 xfs_bmap_free_item_t *next; /* next item on free list */
4239 xfs_trans_t *ntp; /* new transaction pointer */
4240
4241 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4242 if (flist->xbf_count == 0) {
4243 *committed = 0;
4244 return 0;
4245 }
4246 ntp = *tp;
4247 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4248 for (free = flist->xbf_first; free; free = free->xbfi_next)
4249 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4250 free->xbfi_blockcount);
4251 logres = ntp->t_log_res;
4252 logcount = ntp->t_log_count;
4253 ntp = xfs_trans_dup(*tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004254 error = xfs_trans_commit(*tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 *tp = ntp;
4256 *committed = 1;
4257 /*
4258 * We have a new transaction, so we should return committed=1,
4259 * even though we're returning an error.
4260 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11004261 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 return error;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11004263
4264 /*
4265 * transaction commit worked ok so we can drop the extra ticket
4266 * reference that we gained in xfs_trans_dup()
4267 */
4268 xfs_log_ticket_put(ntp->t_ticket);
4269
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4271 logcount)))
4272 return error;
4273 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4274 for (free = flist->xbf_first; free != NULL; free = next) {
4275 next = free->xbfi_next;
4276 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4277 free->xbfi_blockcount))) {
4278 /*
4279 * The bmap free list will be cleaned up at a
4280 * higher level. The EFI will be canceled when
4281 * this transaction is aborted.
4282 * Need to force shutdown here to make sure it
4283 * happens, since this transaction may not be
4284 * dirty yet.
4285 */
4286 mp = ntp->t_mountp;
4287 if (!XFS_FORCED_SHUTDOWN(mp))
4288 xfs_force_shutdown(mp,
4289 (error == EFSCORRUPTED) ?
Nathan Scott7d04a332006-06-09 14:58:38 +10004290 SHUTDOWN_CORRUPT_INCORE :
4291 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 return error;
4293 }
4294 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4295 free->xbfi_blockcount);
4296 xfs_bmap_del_free(flist, NULL, free);
4297 }
4298 return 0;
4299}
4300
4301/*
4302 * Free up any items left in the list.
4303 */
4304void
4305xfs_bmap_cancel(
4306 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4307{
4308 xfs_bmap_free_item_t *free; /* free list item */
4309 xfs_bmap_free_item_t *next;
4310
4311 if (flist->xbf_count == 0)
4312 return;
4313 ASSERT(flist->xbf_first != NULL);
4314 for (free = flist->xbf_first; free; free = next) {
4315 next = free->xbfi_next;
4316 xfs_bmap_del_free(flist, NULL, free);
4317 }
4318 ASSERT(flist->xbf_count == 0);
4319}
4320
4321/*
4322 * Returns the file-relative block number of the first unused block(s)
4323 * in the file with at least "len" logically contiguous blocks free.
4324 * This is the lowest-address hole if the file has holes, else the first block
4325 * past the end of file.
4326 * Return 0 if the file is currently local (in-inode).
4327 */
4328int /* error */
4329xfs_bmap_first_unused(
4330 xfs_trans_t *tp, /* transaction pointer */
4331 xfs_inode_t *ip, /* incore inode */
4332 xfs_extlen_t len, /* size of hole to find */
4333 xfs_fileoff_t *first_unused, /* unused block */
4334 int whichfork) /* data or attr fork */
4335{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004337 int idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 xfs_ifork_t *ifp; /* inode fork pointer */
4339 xfs_fileoff_t lastaddr; /* last block number seen */
4340 xfs_fileoff_t lowest; /* lowest useful block */
4341 xfs_fileoff_t max; /* starting useful block */
4342 xfs_fileoff_t off; /* offset for this block */
4343 xfs_extnum_t nextents; /* number of extent entries */
4344
4345 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4346 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4347 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4348 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4349 *first_unused = 0;
4350 return 0;
4351 }
4352 ifp = XFS_IFORK_PTR(ip, whichfork);
4353 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4354 (error = xfs_iread_extents(tp, ip, whichfork)))
4355 return error;
4356 lowest = *first_unused;
4357 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004358 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004359 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 off = xfs_bmbt_get_startoff(ep);
4361 /*
4362 * See if the hole before this extent will work.
4363 */
4364 if (off >= lowest + len && off - max >= len) {
4365 *first_unused = max;
4366 return 0;
4367 }
4368 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4369 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4370 }
4371 *first_unused = max;
4372 return 0;
4373}
4374
4375/*
4376 * Returns the file-relative block number of the last block + 1 before
4377 * last_block (input value) in the file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004378 * This is not based on i_size, it is based on the extent records.
4379 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 */
4381int /* error */
4382xfs_bmap_last_before(
4383 xfs_trans_t *tp, /* transaction pointer */
4384 xfs_inode_t *ip, /* incore inode */
4385 xfs_fileoff_t *last_block, /* last block */
4386 int whichfork) /* data or attr fork */
4387{
4388 xfs_fileoff_t bno; /* input file offset */
4389 int eof; /* hit end of file */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004390 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 int error; /* error return value */
4392 xfs_bmbt_irec_t got; /* current extent value */
4393 xfs_ifork_t *ifp; /* inode fork pointer */
4394 xfs_extnum_t lastx; /* last extent used */
4395 xfs_bmbt_irec_t prev; /* previous extent value */
4396
4397 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4398 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4399 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4400 return XFS_ERROR(EIO);
4401 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4402 *last_block = 0;
4403 return 0;
4404 }
4405 ifp = XFS_IFORK_PTR(ip, whichfork);
4406 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4407 (error = xfs_iread_extents(tp, ip, whichfork)))
4408 return error;
4409 bno = *last_block - 1;
4410 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4411 &prev);
4412 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4413 if (prev.br_startoff == NULLFILEOFF)
4414 *last_block = 0;
4415 else
4416 *last_block = prev.br_startoff + prev.br_blockcount;
4417 }
4418 /*
4419 * Otherwise *last_block is already the right answer.
4420 */
4421 return 0;
4422}
4423
4424/*
4425 * Returns the file-relative block number of the first block past eof in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004426 * the file. This is not based on i_size, it is based on the extent records.
4427 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 */
4429int /* error */
4430xfs_bmap_last_offset(
4431 xfs_trans_t *tp, /* transaction pointer */
4432 xfs_inode_t *ip, /* incore inode */
4433 xfs_fileoff_t *last_block, /* last block */
4434 int whichfork) /* data or attr fork */
4435{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004436 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 int error; /* error return value */
4438 xfs_ifork_t *ifp; /* inode fork pointer */
4439 xfs_extnum_t nextents; /* number of extent entries */
4440
4441 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4442 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4443 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4444 return XFS_ERROR(EIO);
4445 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4446 *last_block = 0;
4447 return 0;
4448 }
4449 ifp = XFS_IFORK_PTR(ip, whichfork);
4450 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4451 (error = xfs_iread_extents(tp, ip, whichfork)))
4452 return error;
4453 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4454 if (!nextents) {
4455 *last_block = 0;
4456 return 0;
4457 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004458 ep = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4460 return 0;
4461}
4462
4463/*
4464 * Returns whether the selected fork of the inode has exactly one
4465 * block or not. For the data fork we check this matches di_size,
4466 * implying the file's range is 0..bsize-1.
4467 */
4468int /* 1=>1 block, 0=>otherwise */
4469xfs_bmap_one_block(
4470 xfs_inode_t *ip, /* incore inode */
4471 int whichfork) /* data or attr fork */
4472{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004473 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 xfs_ifork_t *ifp; /* inode fork pointer */
4475 int rval; /* return value */
4476 xfs_bmbt_irec_t s; /* internal version of extent */
4477
4478#ifndef DEBUG
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004479 if (whichfork == XFS_DATA_FORK) {
4480 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4481 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4482 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484#endif /* !DEBUG */
4485 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4486 return 0;
4487 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4488 return 0;
4489 ifp = XFS_IFORK_PTR(ip, whichfork);
4490 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004491 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 xfs_bmbt_get_all(ep, &s);
4493 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4494 if (rval && whichfork == XFS_DATA_FORK)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004495 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 return rval;
4497}
4498
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004499STATIC int
4500xfs_bmap_sanity_check(
4501 struct xfs_mount *mp,
4502 struct xfs_buf *bp,
4503 int level)
4504{
4505 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4506
4507 if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC ||
4508 be16_to_cpu(block->bb_level) != level ||
4509 be16_to_cpu(block->bb_numrecs) == 0 ||
4510 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4511 return 0;
4512 return 1;
4513}
4514
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515/*
4516 * Read in the extents to if_extents.
4517 * All inode fields are set up by caller, we just traverse the btree
4518 * and copy the records in. If the file system cannot contain unwritten
4519 * extents, the records are checked for no "state" flags.
4520 */
4521int /* error */
4522xfs_bmap_read_extents(
4523 xfs_trans_t *tp, /* transaction pointer */
4524 xfs_inode_t *ip, /* incore inode */
4525 int whichfork) /* data or attr fork */
4526{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004527 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 xfs_fsblock_t bno; /* block # of "block" */
4529 xfs_buf_t *bp; /* buffer for "block" */
4530 int error; /* error return value */
4531 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 xfs_extnum_t i, j; /* index into the extents list */
4533 xfs_ifork_t *ifp; /* fork structure */
4534 int level; /* btree level, for checking */
4535 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10004536 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 /* REFERENCED */
4538 xfs_extnum_t room; /* number of entries there's room for */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
4540 bno = NULLFSBLOCK;
4541 mp = ip->i_mount;
4542 ifp = XFS_IFORK_PTR(ip, whichfork);
4543 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4544 XFS_EXTFMT_INODE(ip);
4545 block = ifp->if_broot;
4546 /*
4547 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4548 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004549 level = be16_to_cpu(block->bb_level);
4550 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11004551 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004552 bno = be64_to_cpu(*pp);
4553 ASSERT(bno != NULLDFSBNO);
4554 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4555 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 /*
4557 * Go down the tree until leaf level is reached, following the first
4558 * pointer (leftmost) at each level.
4559 */
4560 while (level-- > 0) {
4561 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4562 XFS_BMAP_BTREE_REF)))
4563 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004564 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004566 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 error0);
4568 if (level == 0)
4569 break;
Christoph Hellwig136341b2008-10-30 17:11:40 +11004570 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004571 bno = be64_to_cpu(*pp);
4572 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 xfs_trans_brelse(tp, bp);
4574 }
4575 /*
4576 * Here with bp and block set to the leftmost leaf node in the tree.
4577 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004578 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 i = 0;
4580 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004581 * Loop over all leaf nodes. Copy information to the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 */
4583 for (;;) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004584 xfs_bmbt_rec_t *frp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 xfs_fsblock_t nextbno;
4586 xfs_extnum_t num_recs;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004587 xfs_extnum_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588
4589
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004590 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 if (unlikely(i + num_recs > room)) {
4592 ASSERT(i + num_recs <= room);
Nathan Scott3762ec62006-01-12 10:29:53 +11004593 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4594 "corrupt dinode %Lu, (btree extents).",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 (unsigned long long) ip->i_ino);
4596 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4597 XFS_ERRLEVEL_LOW,
4598 ip->i_mount);
4599 goto error0;
4600 }
4601 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004602 xfs_bmap_sanity_check(mp, bp, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 error0);
4604 /*
4605 * Read-ahead the next leaf block, if any.
4606 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004607 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 if (nextbno != NULLFSBLOCK)
4609 xfs_btree_reada_bufl(mp, nextbno, 1);
4610 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004611 * Copy records into the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11004613 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004614 start = i;
4615 for (j = 0; j < num_recs; j++, i++, frp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004616 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10004617 trp->l0 = be64_to_cpu(frp->l0);
4618 trp->l1 = be64_to_cpu(frp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 }
4620 if (exntf == XFS_EXTFMT_NOSTATE) {
4621 /*
4622 * Check all attribute bmap btree records and
4623 * any "older" data bmap btree records for a
4624 * set bit in the "extent flag" position.
4625 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004626 if (unlikely(xfs_check_nostate_extents(ifp,
4627 start, num_recs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4629 XFS_ERRLEVEL_LOW,
4630 ip->i_mount);
4631 goto error0;
4632 }
4633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 xfs_trans_brelse(tp, bp);
4635 bno = nextbno;
4636 /*
4637 * If we've reached the end, stop.
4638 */
4639 if (bno == NULLFSBLOCK)
4640 break;
4641 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4642 XFS_BMAP_BTREE_REF)))
4643 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004644 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004646 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
Eric Sandeen3a59c942007-07-11 11:09:47 +10004648 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 return 0;
4650error0:
4651 xfs_trans_brelse(tp, bp);
4652 return XFS_ERROR(EFSCORRUPTED);
4653}
4654
4655#ifdef XFS_BMAP_TRACE
4656/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004657 * Add bmap trace insert entries for all the contents of the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 */
4659void
4660xfs_bmap_trace_exlist(
Eric Sandeen3a59c942007-07-11 11:09:47 +10004661 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 xfs_inode_t *ip, /* incore inode pointer */
4663 xfs_extnum_t cnt, /* count of entries in the list */
4664 int whichfork) /* data or attr fork */
4665{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004666 xfs_bmbt_rec_host_t *ep; /* current extent record */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004667 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 xfs_ifork_t *ifp; /* inode fork pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004669 xfs_bmbt_irec_t s; /* file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
4671 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004672 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4673 for (idx = 0; idx < cnt; idx++) {
4674 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 xfs_bmbt_get_all(ep, &s);
Eric Sandeen3a59c942007-07-11 11:09:47 +10004676 XFS_BMAP_TRACE_INSERT("exlist", ip, idx, 1, &s, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 whichfork);
4678 }
4679}
4680#endif
4681
4682#ifdef DEBUG
4683/*
4684 * Validate that the bmbt_irecs being returned from bmapi are valid
4685 * given the callers original parameters. Specifically check the
4686 * ranges of the returned irecs to ensure that they only extent beyond
4687 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4688 */
4689STATIC void
4690xfs_bmap_validate_ret(
4691 xfs_fileoff_t bno,
4692 xfs_filblks_t len,
4693 int flags,
4694 xfs_bmbt_irec_t *mval,
4695 int nmap,
4696 int ret_nmap)
4697{
4698 int i; /* index to map values */
4699
4700 ASSERT(ret_nmap <= nmap);
4701
4702 for (i = 0; i < ret_nmap; i++) {
4703 ASSERT(mval[i].br_blockcount > 0);
4704 if (!(flags & XFS_BMAPI_ENTIRE)) {
4705 ASSERT(mval[i].br_startoff >= bno);
4706 ASSERT(mval[i].br_blockcount <= len);
4707 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4708 bno + len);
4709 } else {
4710 ASSERT(mval[i].br_startoff < bno + len);
4711 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4712 bno);
4713 }
4714 ASSERT(i == 0 ||
4715 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4716 mval[i].br_startoff);
4717 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4718 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4719 mval[i].br_startblock != HOLESTARTBLOCK);
4720 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4721 mval[i].br_state == XFS_EXT_UNWRITTEN);
4722 }
4723}
4724#endif /* DEBUG */
4725
4726
4727/*
4728 * Map file blocks to filesystem blocks.
4729 * File range is given by the bno/len pair.
4730 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4731 * into a hole or past eof.
4732 * Only allocates blocks from a single allocation group,
4733 * to avoid locking problems.
4734 * The returned value in "firstblock" from the first call in a transaction
4735 * must be remembered and presented to subsequent calls in "firstblock".
4736 * An upper bound for the number of blocks to be allocated is supplied to
4737 * the first call in "total"; if no allocation group has that many free
4738 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4739 */
4740int /* error */
4741xfs_bmapi(
4742 xfs_trans_t *tp, /* transaction pointer */
4743 xfs_inode_t *ip, /* incore inode */
4744 xfs_fileoff_t bno, /* starting file offs. mapped */
4745 xfs_filblks_t len, /* length to map in file */
4746 int flags, /* XFS_BMAPI_... */
4747 xfs_fsblock_t *firstblock, /* first allocated block
4748 controls a.g. for allocs */
4749 xfs_extlen_t total, /* total blocks needed */
4750 xfs_bmbt_irec_t *mval, /* output: map values */
4751 int *nmap, /* i/o: mval size/count */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004752 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4753 xfs_extdelta_t *delta) /* o: change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754{
4755 xfs_fsblock_t abno; /* allocated block number */
4756 xfs_extlen_t alen; /* allocated extent length */
4757 xfs_fileoff_t aoff; /* allocated file offset */
4758 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 xfs_btree_cur_t *cur; /* bmap btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 xfs_fileoff_t end; /* end of mapped file region */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004761 int eof; /* we've hit the end of extents */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004762 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004764 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 xfs_ifork_t *ifp; /* inode fork pointer */
4766 xfs_extlen_t indlen; /* indirect blocks length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 xfs_extnum_t lastx; /* last useful extent number */
4768 int logflags; /* flags for transaction logging */
4769 xfs_extlen_t minleft; /* min blocks left after allocation */
4770 xfs_extlen_t minlen; /* min allocation size */
4771 xfs_mount_t *mp; /* xfs mount structure */
4772 int n; /* current extent index */
Malcolm Parsons9da096f2009-03-29 09:55:42 +02004773 int nallocs; /* number of extents alloc'd */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 xfs_extnum_t nextents; /* number of extents in file */
4775 xfs_fileoff_t obno; /* old block number (offset) */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004776 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 int tmp_logflags; /* temp flags holder */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004778 int whichfork; /* data or attr fork */
4779 char inhole; /* current location is hole in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 char wasdelay; /* old extent was delayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 char wr; /* this is a write request */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004782 char rt; /* this is a realtime file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783#ifdef DEBUG
4784 xfs_fileoff_t orig_bno; /* original block number value */
4785 int orig_flags; /* original flags arg value */
4786 xfs_filblks_t orig_len; /* original value of len arg */
4787 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4788 int orig_nmap; /* original value of *nmap */
4789
4790 orig_bno = bno;
4791 orig_len = len;
4792 orig_flags = flags;
4793 orig_mval = mval;
4794 orig_nmap = *nmap;
4795#endif
4796 ASSERT(*nmap >= 1);
4797 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4798 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4799 XFS_ATTR_FORK : XFS_DATA_FORK;
4800 mp = ip->i_mount;
4801 if (unlikely(XFS_TEST_ERROR(
4802 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4803 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4804 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4805 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4806 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4807 return XFS_ERROR(EFSCORRUPTED);
4808 }
4809 if (XFS_FORCED_SHUTDOWN(mp))
4810 return XFS_ERROR(EIO);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004811 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 ifp = XFS_IFORK_PTR(ip, whichfork);
4813 ASSERT(ifp->if_ext_max ==
4814 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4815 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4816 XFS_STATS_INC(xs_blk_mapw);
4817 else
4818 XFS_STATS_INC(xs_blk_mapr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 /*
Nathan Scott39269e22006-03-14 13:33:50 +11004820 * IGSTATE flag is used to combine extents which
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 * differ only due to the state of the extents.
4822 * This technique is used from xfs_getbmap()
4823 * when the caller does not wish to see the
4824 * separation (which is the default).
4825 *
4826 * This technique is also used when writing a
4827 * buffer which has been partially written,
4828 * (usually by being flushed during a chunkread),
4829 * to ensure one write takes place. This also
4830 * prevents a change in the xfs inode extents at
4831 * this time, intentionally. This change occurs
4832 * on completion of the write operation, in
4833 * xfs_strat_comp(), where the xfs_bmapi() call
4834 * is transactioned, and the extents combined.
4835 */
Nathan Scott39269e22006-03-14 13:33:50 +11004836 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4837 wr = 0; /* no allocations are allowed */
4838 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 logflags = 0;
4840 nallocs = 0;
4841 cur = NULL;
4842 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4843 ASSERT(wr && tp);
4844 if ((error = xfs_bmap_local_to_extents(tp, ip,
4845 firstblock, total, &logflags, whichfork)))
4846 goto error0;
4847 }
4848 if (wr && *firstblock == NULLFSBLOCK) {
4849 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Christoph Hellwig16259e72005-11-02 15:11:25 +11004850 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 else
4852 minleft = 1;
4853 } else
4854 minleft = 0;
4855 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4856 (error = xfs_iread_extents(tp, ip, whichfork)))
4857 goto error0;
4858 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4859 &prev);
4860 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4861 n = 0;
4862 end = bno + len;
4863 obno = bno;
4864 bma.ip = NULL;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004865 if (delta) {
4866 delta->xed_startoff = NULLFILEOFF;
4867 delta->xed_blockcount = 0;
4868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 while (bno < end && n < *nmap) {
4870 /*
4871 * Reading past eof, act as though there's a hole
4872 * up to end.
4873 */
4874 if (eof && !wr)
4875 got.br_startoff = end;
4876 inhole = eof || got.br_startoff > bno;
Nathan Scott39269e22006-03-14 13:33:50 +11004877 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06004878 isnullstartblock(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 /*
4880 * First, deal with the hole before the allocated space
4881 * that we found, if any.
4882 */
4883 if (wr && (inhole || wasdelay)) {
4884 /*
4885 * For the wasdelay case, we could also just
4886 * allocate the stuff asked for in this bmap call
4887 * but that wouldn't be as good.
4888 */
Nathan Scott39269e22006-03-14 13:33:50 +11004889 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 alen = (xfs_extlen_t)got.br_blockcount;
4891 aoff = got.br_startoff;
4892 if (lastx != NULLEXTNUM && lastx) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004893 ep = xfs_iext_get_ext(ifp, lastx - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 xfs_bmbt_get_all(ep, &prev);
4895 }
4896 } else if (wasdelay) {
4897 alen = (xfs_extlen_t)
4898 XFS_FILBLKS_MIN(len,
4899 (got.br_startoff +
4900 got.br_blockcount) - bno);
4901 aoff = bno;
4902 } else {
4903 alen = (xfs_extlen_t)
4904 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4905 if (!eof)
4906 alen = (xfs_extlen_t)
4907 XFS_FILBLKS_MIN(alen,
4908 got.br_startoff - bno);
4909 aoff = bno;
4910 }
Nathan Scott39269e22006-03-14 13:33:50 +11004911 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4912 if (flags & XFS_BMAPI_DELAY) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11004913 xfs_extlen_t extsz;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004914
4915 /* Figure out the extent size, adjust alen */
David Chinner957d0eb2007-06-18 16:50:37 +10004916 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004917 if (extsz) {
4918 error = xfs_bmap_extsize_align(mp,
4919 &got, &prev, extsz,
Nathan Scott39269e22006-03-14 13:33:50 +11004920 rt, eof,
4921 flags&XFS_BMAPI_DELAY,
4922 flags&XFS_BMAPI_CONVERT,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004923 &aoff, &alen);
4924 ASSERT(!error);
4925 }
4926
4927 if (rt)
4928 extsz = alen / mp->m_sb.sb_rextsize;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004929
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 /*
4931 * Make a transaction-less quota reservation for
4932 * delayed allocation blocks. This number gets
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004933 * adjusted later. We return if we haven't
4934 * allocated blocks already inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02004936 error = xfs_trans_reserve_quota_nblks(
4937 NULL, ip, (long)alen, 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004938 rt ? XFS_QMOPT_RES_RTBLKS :
Christoph Hellwig7d095252009-06-08 15:33:32 +02004939 XFS_QMOPT_RES_REGBLKS);
4940 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 if (n == 0) {
4942 *nmap = 0;
4943 ASSERT(cur == NULL);
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004944 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 }
4946 break;
4947 }
4948
4949 /*
4950 * Split changing sb for alen and indlen since
4951 * they could be coming from different places.
4952 */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004953 indlen = (xfs_extlen_t)
4954 xfs_bmap_worst_indlen(ip, alen);
4955 ASSERT(indlen > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956
Nathan Scottdd9f4382006-01-11 15:28:28 +11004957 if (rt) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004958 error = xfs_mod_incore_sb(mp,
4959 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004960 -((int64_t)extsz), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004961 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004962 } else {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004963 error = xfs_mod_incore_sb(mp,
4964 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004965 -((int64_t)alen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004966 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004967 }
David Chinner3bdbfb12005-09-02 16:40:47 +10004968 if (!error) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004969 error = xfs_mod_incore_sb(mp,
4970 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004971 -((int64_t)indlen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004972 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004973 if (error && rt)
4974 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004975 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004976 (int64_t)extsz, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004977 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004978 else if (error)
4979 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004980 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004981 (int64_t)alen, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004982 XFS_BMAPI_RSVBLOCKS));
David Chinner3bdbfb12005-09-02 16:40:47 +10004983 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004984
4985 if (error) {
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004986 if (XFS_IS_QUOTA_ON(mp))
Nathan Scott06d10dd2005-06-21 15:48:47 +10004987 /* unreserve the blocks now */
Nathan Scottdd9f4382006-01-11 15:28:28 +11004988 (void)
Christoph Hellwig7d095252009-06-08 15:33:32 +02004989 xfs_trans_unreserve_quota_nblks(
4990 NULL, ip,
Nathan Scott06d10dd2005-06-21 15:48:47 +10004991 (long)alen, 0, rt ?
4992 XFS_QMOPT_RES_RTBLKS :
4993 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 break;
4995 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004996
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 ip->i_delayed_blks += alen;
Eric Sandeen9d87c312009-01-14 23:22:07 -06004998 abno = nullstartblock(indlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 } else {
5000 /*
5001 * If first time, allocate and fill in
5002 * once-only bma fields.
5003 */
5004 if (bma.ip == NULL) {
5005 bma.tp = tp;
5006 bma.ip = ip;
5007 bma.prevp = &prev;
5008 bma.gotp = &got;
5009 bma.total = total;
5010 bma.userdata = 0;
5011 }
5012 /* Indicate if this is the first user data
5013 * in the file, or just any user data.
5014 */
Nathan Scott39269e22006-03-14 13:33:50 +11005015 if (!(flags & XFS_BMAPI_METADATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 bma.userdata = (aoff == 0) ?
5017 XFS_ALLOC_INITIAL_USER_DATA :
5018 XFS_ALLOC_USERDATA;
5019 }
5020 /*
5021 * Fill in changeable bma fields.
5022 */
5023 bma.eof = eof;
5024 bma.firstblock = *firstblock;
5025 bma.alen = alen;
5026 bma.off = aoff;
Adrian Bunk72880262006-08-30 13:41:58 +10005027 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 bma.wasdel = wasdelay;
5029 bma.minlen = minlen;
5030 bma.low = flist->xbf_low;
5031 bma.minleft = minleft;
5032 /*
5033 * Only want to do the alignment at the
5034 * eof if it is userdata and allocation length
5035 * is larger than a stripe unit.
5036 */
5037 if (mp->m_dalign && alen >= mp->m_dalign &&
Nathan Scott39269e22006-03-14 13:33:50 +11005038 (!(flags & XFS_BMAPI_METADATA)) &&
5039 (whichfork == XFS_DATA_FORK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 if ((error = xfs_bmap_isaeof(ip, aoff,
5041 whichfork, &bma.aeof)))
5042 goto error0;
5043 } else
5044 bma.aeof = 0;
5045 /*
5046 * Call allocator.
5047 */
5048 if ((error = xfs_bmap_alloc(&bma)))
5049 goto error0;
5050 /*
5051 * Copy out result fields.
5052 */
5053 abno = bma.rval;
5054 if ((flist->xbf_low = bma.low))
5055 minleft = 0;
5056 alen = bma.alen;
5057 aoff = bma.off;
5058 ASSERT(*firstblock == NULLFSBLOCK ||
5059 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5060 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
5061 (flist->xbf_low &&
5062 XFS_FSB_TO_AGNO(mp, *firstblock) <
5063 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
5064 *firstblock = bma.firstblock;
5065 if (cur)
5066 cur->bc_private.b.firstblock =
5067 *firstblock;
5068 if (abno == NULLFSBLOCK)
5069 break;
5070 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005071 cur = xfs_bmbt_init_cursor(mp, tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 ip, whichfork);
5073 cur->bc_private.b.firstblock =
5074 *firstblock;
5075 cur->bc_private.b.flist = flist;
5076 }
5077 /*
5078 * Bump the number of extents we've allocated
5079 * in this call.
5080 */
5081 nallocs++;
5082 }
5083 if (cur)
5084 cur->bc_private.b.flags =
5085 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
5086 got.br_startoff = aoff;
5087 got.br_startblock = abno;
5088 got.br_blockcount = alen;
5089 got.br_state = XFS_EXT_NORM; /* assume normal */
5090 /*
5091 * Determine state of extent, and the filesystem.
5092 * A wasdelay extent has been initialized, so
5093 * shouldn't be flagged as unwritten.
5094 */
Eric Sandeen62118702008-03-06 13:44:28 +11005095 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5097 got.br_state = XFS_EXT_UNWRITTEN;
5098 }
5099 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005100 firstblock, flist, &tmp_logflags, delta,
5101 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 logflags |= tmp_logflags;
5103 if (error)
5104 goto error0;
5105 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005106 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5108 xfs_bmbt_get_all(ep, &got);
5109 ASSERT(got.br_startoff <= aoff);
5110 ASSERT(got.br_startoff + got.br_blockcount >=
5111 aoff + alen);
5112#ifdef DEBUG
Nathan Scott39269e22006-03-14 13:33:50 +11005113 if (flags & XFS_BMAPI_DELAY) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005114 ASSERT(isnullstartblock(got.br_startblock));
5115 ASSERT(startblockval(got.br_startblock) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 }
5117 ASSERT(got.br_state == XFS_EXT_NORM ||
5118 got.br_state == XFS_EXT_UNWRITTEN);
5119#endif
5120 /*
5121 * Fall down into the found allocated space case.
5122 */
5123 } else if (inhole) {
5124 /*
5125 * Reading in a hole.
5126 */
5127 mval->br_startoff = bno;
5128 mval->br_startblock = HOLESTARTBLOCK;
5129 mval->br_blockcount =
5130 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5131 mval->br_state = XFS_EXT_NORM;
5132 bno += mval->br_blockcount;
5133 len -= mval->br_blockcount;
5134 mval++;
5135 n++;
5136 continue;
5137 }
5138 /*
5139 * Then deal with the allocated space we found.
5140 */
5141 ASSERT(ep != NULL);
Nathan Scott39269e22006-03-14 13:33:50 +11005142 if (!(flags & XFS_BMAPI_ENTIRE) &&
5143 (got.br_startoff + got.br_blockcount > obno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 if (obno > bno)
5145 bno = obno;
5146 ASSERT((bno >= obno) || (n == 0));
5147 ASSERT(bno < end);
5148 mval->br_startoff = bno;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005149 if (isnullstartblock(got.br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11005150 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 mval->br_startblock = DELAYSTARTBLOCK;
5152 } else
5153 mval->br_startblock =
5154 got.br_startblock +
5155 (bno - got.br_startoff);
5156 /*
5157 * Return the minimum of what we got and what we
5158 * asked for for the length. We can use the len
5159 * variable here because it is modified below
5160 * and we could have been there before coming
5161 * here if the first part of the allocation
5162 * didn't overlap what was asked for.
5163 */
5164 mval->br_blockcount =
5165 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5166 (bno - got.br_startoff));
5167 mval->br_state = got.br_state;
5168 ASSERT(mval->br_blockcount <= len);
5169 } else {
5170 *mval = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005171 if (isnullstartblock(mval->br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11005172 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 mval->br_startblock = DELAYSTARTBLOCK;
5174 }
5175 }
5176
5177 /*
5178 * Check if writing previously allocated but
5179 * unwritten extents.
5180 */
5181 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5182 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5183 /*
5184 * Modify (by adding) the state flag, if writing.
5185 */
5186 ASSERT(mval->br_blockcount <= len);
5187 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005188 cur = xfs_bmbt_init_cursor(mp,
5189 tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 cur->bc_private.b.firstblock =
5191 *firstblock;
5192 cur->bc_private.b.flist = flist;
5193 }
5194 mval->br_state = XFS_EXT_NORM;
5195 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005196 firstblock, flist, &tmp_logflags, delta,
5197 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 logflags |= tmp_logflags;
5199 if (error)
5200 goto error0;
5201 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005202 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5204 xfs_bmbt_get_all(ep, &got);
5205 /*
5206 * We may have combined previously unwritten
5207 * space with written space, so generate
5208 * another request.
5209 */
5210 if (mval->br_blockcount < len)
5211 continue;
5212 }
5213
Nathan Scott39269e22006-03-14 13:33:50 +11005214 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 ((mval->br_startoff + mval->br_blockcount) <= end));
Nathan Scott39269e22006-03-14 13:33:50 +11005216 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5217 (mval->br_blockcount <= len) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 (mval->br_startoff < obno));
5219 bno = mval->br_startoff + mval->br_blockcount;
5220 len = end - bno;
5221 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5222 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5223 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5224 ASSERT(mval->br_state == mval[-1].br_state);
5225 mval[-1].br_blockcount = mval->br_blockcount;
5226 mval[-1].br_state = mval->br_state;
5227 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5228 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5229 mval[-1].br_startblock != HOLESTARTBLOCK &&
5230 mval->br_startblock ==
5231 mval[-1].br_startblock + mval[-1].br_blockcount &&
Nathan Scott39269e22006-03-14 13:33:50 +11005232 ((flags & XFS_BMAPI_IGSTATE) ||
5233 mval[-1].br_state == mval->br_state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 ASSERT(mval->br_startoff ==
5235 mval[-1].br_startoff + mval[-1].br_blockcount);
5236 mval[-1].br_blockcount += mval->br_blockcount;
5237 } else if (n > 0 &&
5238 mval->br_startblock == DELAYSTARTBLOCK &&
5239 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5240 mval->br_startoff ==
5241 mval[-1].br_startoff + mval[-1].br_blockcount) {
5242 mval[-1].br_blockcount += mval->br_blockcount;
5243 mval[-1].br_state = mval->br_state;
5244 } else if (!((n == 0) &&
5245 ((mval->br_startoff + mval->br_blockcount) <=
5246 obno))) {
5247 mval++;
5248 n++;
5249 }
5250 /*
5251 * If we're done, stop now. Stop when we've allocated
5252 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5253 * the transaction may get too big.
5254 */
5255 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5256 break;
5257 /*
5258 * Else go on to the next record.
5259 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005260 ep = xfs_iext_get_ext(ifp, ++lastx);
David Chinner4e5ae832007-06-05 16:24:15 +10005261 prev = got;
5262 if (lastx >= nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 eof = 1;
David Chinner4e5ae832007-06-05 16:24:15 +10005264 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 xfs_bmbt_get_all(ep, &got);
5266 }
5267 ifp->if_lastex = lastx;
5268 *nmap = n;
5269 /*
5270 * Transform from btree to extents, give it cur.
5271 */
5272 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5273 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5274 ASSERT(wr && cur);
5275 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5276 &tmp_logflags, whichfork);
5277 logflags |= tmp_logflags;
5278 if (error)
5279 goto error0;
5280 }
5281 ASSERT(ifp->if_ext_max ==
5282 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5283 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5284 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5285 error = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005286 if (delta && delta->xed_startoff != NULLFILEOFF) {
5287 /* A change was actually made.
5288 * Note that delta->xed_blockount is an offset at this
5289 * point and needs to be converted to a block count.
5290 */
5291 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5292 delta->xed_blockcount -= delta->xed_startoff;
5293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294error0:
5295 /*
5296 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005297 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005299 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005301 logflags &= ~xfs_ilog_fext(whichfork);
5302 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005304 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 /*
5306 * Log whatever the flags say, even if error. Otherwise we might miss
5307 * detecting a case where the data is changed, there's an error,
5308 * and it's not logged so we don't shutdown when we should.
5309 */
5310 if (logflags) {
5311 ASSERT(tp && wr);
5312 xfs_trans_log_inode(tp, ip, logflags);
5313 }
5314 if (cur) {
5315 if (!error) {
5316 ASSERT(*firstblock == NULLFSBLOCK ||
5317 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5318 XFS_FSB_TO_AGNO(mp,
5319 cur->bc_private.b.firstblock) ||
5320 (flist->xbf_low &&
5321 XFS_FSB_TO_AGNO(mp, *firstblock) <
5322 XFS_FSB_TO_AGNO(mp,
5323 cur->bc_private.b.firstblock)));
5324 *firstblock = cur->bc_private.b.firstblock;
5325 }
5326 xfs_btree_del_cursor(cur,
5327 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5328 }
5329 if (!error)
5330 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5331 orig_nmap, *nmap);
5332 return error;
5333}
5334
5335/*
5336 * Map file blocks to filesystem blocks, simple version.
5337 * One block (extent) only, read-only.
5338 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5339 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5340 * was set and all the others were clear.
5341 */
5342int /* error */
5343xfs_bmapi_single(
5344 xfs_trans_t *tp, /* transaction pointer */
5345 xfs_inode_t *ip, /* incore inode */
5346 int whichfork, /* data or attr fork */
5347 xfs_fsblock_t *fsb, /* output: mapped block */
5348 xfs_fileoff_t bno) /* starting file offs. mapped */
5349{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005350 int eof; /* we've hit the end of extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005352 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 xfs_ifork_t *ifp; /* inode fork pointer */
5354 xfs_extnum_t lastx; /* last useful extent number */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005355 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356
5357 ifp = XFS_IFORK_PTR(ip, whichfork);
5358 if (unlikely(
5359 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5360 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5361 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5362 ip->i_mount);
5363 return XFS_ERROR(EFSCORRUPTED);
5364 }
5365 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5366 return XFS_ERROR(EIO);
5367 XFS_STATS_INC(xs_blk_mapr);
5368 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5369 (error = xfs_iread_extents(tp, ip, whichfork)))
5370 return error;
5371 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5372 &prev);
5373 /*
5374 * Reading past eof, act as though there's a hole
5375 * up to end.
5376 */
5377 if (eof || got.br_startoff > bno) {
5378 *fsb = NULLFSBLOCK;
5379 return 0;
5380 }
Eric Sandeen9d87c312009-01-14 23:22:07 -06005381 ASSERT(!isnullstartblock(got.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 ASSERT(bno < got.br_startoff + got.br_blockcount);
5383 *fsb = got.br_startblock + (bno - got.br_startoff);
5384 ifp->if_lastex = lastx;
5385 return 0;
5386}
5387
5388/*
5389 * Unmap (remove) blocks from a file.
5390 * If nexts is nonzero then the number of extents to remove is limited to
5391 * that value. If not all extents in the block range can be removed then
5392 * *done is set.
5393 */
5394int /* error */
5395xfs_bunmapi(
5396 xfs_trans_t *tp, /* transaction pointer */
5397 struct xfs_inode *ip, /* incore inode */
5398 xfs_fileoff_t bno, /* starting offset to unmap */
5399 xfs_filblks_t len, /* length to unmap in file */
5400 int flags, /* misc flags */
5401 xfs_extnum_t nexts, /* number of extents max */
5402 xfs_fsblock_t *firstblock, /* first allocated block
5403 controls a.g. for allocs */
5404 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005405 xfs_extdelta_t *delta, /* o: change made to incore
5406 extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 int *done) /* set if not done yet */
5408{
5409 xfs_btree_cur_t *cur; /* bmap btree cursor */
5410 xfs_bmbt_irec_t del; /* extent being deleted */
5411 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005412 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 int error; /* error return value */
5414 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005415 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 xfs_ifork_t *ifp; /* inode fork pointer */
5417 int isrt; /* freeing in rt area */
5418 xfs_extnum_t lastx; /* last extent index used */
5419 int logflags; /* transaction logging flags */
5420 xfs_extlen_t mod; /* rt extent offset */
5421 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005422 xfs_extnum_t nextents; /* number of file extents */
5423 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 xfs_fileoff_t start; /* first file offset deleted */
5425 int tmp_logflags; /* partial logging flags */
5426 int wasdel; /* was a delayed alloc extent */
5427 int whichfork; /* data or attribute fork */
5428 int rsvd; /* OK to allocate reserved blocks */
5429 xfs_fsblock_t sum;
5430
5431 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5432 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5433 XFS_ATTR_FORK : XFS_DATA_FORK;
5434 ifp = XFS_IFORK_PTR(ip, whichfork);
5435 if (unlikely(
5436 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5437 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5438 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5439 ip->i_mount);
5440 return XFS_ERROR(EFSCORRUPTED);
5441 }
5442 mp = ip->i_mount;
5443 if (XFS_FORCED_SHUTDOWN(mp))
5444 return XFS_ERROR(EIO);
5445 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5446 ASSERT(len > 0);
5447 ASSERT(nexts >= 0);
5448 ASSERT(ifp->if_ext_max ==
5449 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5450 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5451 (error = xfs_iread_extents(tp, ip, whichfork)))
5452 return error;
5453 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5454 if (nextents == 0) {
5455 *done = 1;
5456 return 0;
5457 }
5458 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005459 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 start = bno;
5461 bno = start + len - 1;
5462 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5463 &prev);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005464 if (delta) {
5465 delta->xed_startoff = NULLFILEOFF;
5466 delta->xed_blockcount = 0;
5467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 /*
5469 * Check to see if the given block number is past the end of the
5470 * file, back up to the last block if so...
5471 */
5472 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005473 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 xfs_bmbt_get_all(ep, &got);
5475 bno = got.br_startoff + got.br_blockcount - 1;
5476 }
5477 logflags = 0;
5478 if (ifp->if_flags & XFS_IFBROOT) {
5479 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005480 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 cur->bc_private.b.firstblock = *firstblock;
5482 cur->bc_private.b.flist = flist;
5483 cur->bc_private.b.flags = 0;
5484 } else
5485 cur = NULL;
5486 extno = 0;
5487 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5488 (nexts == 0 || extno < nexts)) {
5489 /*
5490 * Is the found extent after a hole in which bno lives?
5491 * Just back up to the previous extent, if so.
5492 */
5493 if (got.br_startoff > bno) {
5494 if (--lastx < 0)
5495 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005496 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 xfs_bmbt_get_all(ep, &got);
5498 }
5499 /*
5500 * Is the last block of this extent before the range
5501 * we're supposed to delete? If so, we're done.
5502 */
5503 bno = XFS_FILEOFF_MIN(bno,
5504 got.br_startoff + got.br_blockcount - 1);
5505 if (bno < start)
5506 break;
5507 /*
5508 * Then deal with the (possibly delayed) allocated space
5509 * we found.
5510 */
5511 ASSERT(ep != NULL);
5512 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005513 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 if (got.br_startoff < start) {
5515 del.br_startoff = start;
5516 del.br_blockcount -= start - got.br_startoff;
5517 if (!wasdel)
5518 del.br_startblock += start - got.br_startoff;
5519 }
5520 if (del.br_startoff + del.br_blockcount > bno + 1)
5521 del.br_blockcount = bno + 1 - del.br_startoff;
5522 sum = del.br_startblock + del.br_blockcount;
5523 if (isrt &&
5524 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5525 /*
5526 * Realtime extent not lined up at the end.
5527 * The extent could have been split into written
5528 * and unwritten pieces, or we could just be
5529 * unmapping part of it. But we can't really
5530 * get rid of part of a realtime extent.
5531 */
5532 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005533 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 /*
5535 * This piece is unwritten, or we're not
5536 * using unwritten extents. Skip over it.
5537 */
5538 ASSERT(bno >= mod);
5539 bno -= mod > del.br_blockcount ?
5540 del.br_blockcount : mod;
5541 if (bno < got.br_startoff) {
5542 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005543 xfs_bmbt_get_all(xfs_iext_get_ext(
5544 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 }
5546 continue;
5547 }
5548 /*
5549 * It's written, turn it unwritten.
5550 * This is better than zeroing it.
5551 */
5552 ASSERT(del.br_state == XFS_EXT_NORM);
5553 ASSERT(xfs_trans_get_block_res(tp) > 0);
5554 /*
5555 * If this spans a realtime extent boundary,
5556 * chop it back to the start of the one we end at.
5557 */
5558 if (del.br_blockcount > mod) {
5559 del.br_startoff += del.br_blockcount - mod;
5560 del.br_startblock += del.br_blockcount - mod;
5561 del.br_blockcount = mod;
5562 }
5563 del.br_state = XFS_EXT_UNWRITTEN;
5564 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005565 firstblock, flist, &logflags, delta,
5566 XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 if (error)
5568 goto error0;
5569 goto nodelete;
5570 }
5571 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5572 /*
5573 * Realtime extent is lined up at the end but not
5574 * at the front. We'll get rid of full extents if
5575 * we can.
5576 */
5577 mod = mp->m_sb.sb_rextsize - mod;
5578 if (del.br_blockcount > mod) {
5579 del.br_blockcount -= mod;
5580 del.br_startoff += mod;
5581 del.br_startblock += mod;
5582 } else if ((del.br_startoff == start &&
5583 (del.br_state == XFS_EXT_UNWRITTEN ||
5584 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005585 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 /*
5587 * Can't make it unwritten. There isn't
5588 * a full extent here so just skip it.
5589 */
5590 ASSERT(bno >= del.br_blockcount);
5591 bno -= del.br_blockcount;
5592 if (bno < got.br_startoff) {
5593 if (--lastx >= 0)
5594 xfs_bmbt_get_all(--ep, &got);
5595 }
5596 continue;
5597 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5598 /*
5599 * This one is already unwritten.
5600 * It must have a written left neighbor.
5601 * Unwrite the killed part of that one and
5602 * try again.
5603 */
5604 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005605 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5606 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005608 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 ASSERT(del.br_startblock ==
5610 prev.br_startblock + prev.br_blockcount);
5611 if (prev.br_startoff < start) {
5612 mod = start - prev.br_startoff;
5613 prev.br_blockcount -= mod;
5614 prev.br_startblock += mod;
5615 prev.br_startoff = start;
5616 }
5617 prev.br_state = XFS_EXT_UNWRITTEN;
5618 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5619 &prev, firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005620 delta, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 if (error)
5622 goto error0;
5623 goto nodelete;
5624 } else {
5625 ASSERT(del.br_state == XFS_EXT_NORM);
5626 del.br_state = XFS_EXT_UNWRITTEN;
5627 error = xfs_bmap_add_extent(ip, lastx, &cur,
5628 &del, firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005629 delta, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 if (error)
5631 goto error0;
5632 goto nodelete;
5633 }
5634 }
5635 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005636 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005637 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005638 if (isrt) {
5639 xfs_filblks_t rtexts;
5640
5641 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5642 do_div(rtexts, mp->m_sb.sb_rextsize);
5643 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005644 (int64_t)rtexts, rsvd);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005645 (void)xfs_trans_reserve_quota_nblks(NULL,
5646 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005647 XFS_QMOPT_RES_RTBLKS);
5648 } else {
5649 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005650 (int64_t)del.br_blockcount, rsvd);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005651 (void)xfs_trans_reserve_quota_nblks(NULL,
5652 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 ip->i_delayed_blks -= del.br_blockcount;
5656 if (cur)
5657 cur->bc_private.b.flags |=
5658 XFS_BTCUR_BPRV_WASDEL;
5659 } else if (cur)
5660 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5661 /*
5662 * If it's the case where the directory code is running
5663 * with no block reservation, and the deleted block is in
5664 * the middle of its extent, and the resulting insert
5665 * of an extent would cause transformation to btree format,
5666 * then reject it. The calling code will then swap
5667 * blocks around instead.
5668 * We have to do this now, rather than waiting for the
5669 * conversion to btree format, since the transaction
5670 * will be dirty.
5671 */
5672 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5673 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5674 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5675 del.br_startoff > got.br_startoff &&
5676 del.br_startoff + del.br_blockcount <
5677 got.br_startoff + got.br_blockcount) {
5678 error = XFS_ERROR(ENOSPC);
5679 goto error0;
5680 }
5681 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005682 &tmp_logflags, delta, whichfork, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 logflags |= tmp_logflags;
5684 if (error)
5685 goto error0;
5686 bno = del.br_startoff - 1;
5687nodelete:
5688 lastx = ifp->if_lastex;
5689 /*
5690 * If not done go on to the next (previous) record.
5691 * Reset ep in case the extents array was re-alloced.
5692 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005693 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5695 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5696 xfs_bmbt_get_startoff(ep) > bno) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005697 if (--lastx >= 0)
5698 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 }
5700 if (lastx >= 0)
5701 xfs_bmbt_get_all(ep, &got);
5702 extno++;
5703 }
5704 }
5705 ifp->if_lastex = lastx;
5706 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5707 ASSERT(ifp->if_ext_max ==
5708 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5709 /*
5710 * Convert to a btree if necessary.
5711 */
5712 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5713 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5714 ASSERT(cur == NULL);
5715 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5716 &cur, 0, &tmp_logflags, whichfork);
5717 logflags |= tmp_logflags;
5718 if (error)
5719 goto error0;
5720 }
5721 /*
5722 * transform from btree to extents, give it cur
5723 */
5724 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5725 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5726 ASSERT(cur != NULL);
5727 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5728 whichfork);
5729 logflags |= tmp_logflags;
5730 if (error)
5731 goto error0;
5732 }
5733 /*
5734 * transform from extents to local?
5735 */
5736 ASSERT(ifp->if_ext_max ==
5737 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5738 error = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005739 if (delta && delta->xed_startoff != NULLFILEOFF) {
5740 /* A change was actually made.
5741 * Note that delta->xed_blockount is an offset at this
5742 * point and needs to be converted to a block count.
5743 */
5744 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5745 delta->xed_blockcount -= delta->xed_startoff;
5746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747error0:
5748 /*
5749 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005750 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005752 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005754 logflags &= ~xfs_ilog_fext(whichfork);
5755 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005757 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 /*
5759 * Log inode even in the error case, if the transaction
5760 * is dirty we'll need to shut down the filesystem.
5761 */
5762 if (logflags)
5763 xfs_trans_log_inode(tp, ip, logflags);
5764 if (cur) {
5765 if (!error) {
5766 *firstblock = cur->bc_private.b.firstblock;
5767 cur->bc_private.b.allocated = 0;
5768 }
5769 xfs_btree_del_cursor(cur,
5770 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5771 }
5772 return error;
5773}
5774
5775/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005776 * returns 1 for success, 0 if we failed to map the extent.
5777 */
5778STATIC int
5779xfs_getbmapx_fix_eof_hole(
5780 xfs_inode_t *ip, /* xfs incore inode pointer */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005781 struct getbmapx *out, /* output structure */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005782 int prealloced, /* this is a file with
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005783 * preallocated data space */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005784 __int64_t end, /* last block requested */
5785 xfs_fsblock_t startblock)
5786{
5787 __int64_t fixlen;
5788 xfs_mount_t *mp; /* file system mount point */
Eric Sandeen5af317c2008-11-28 14:23:35 +11005789 xfs_ifork_t *ifp; /* inode fork pointer */
5790 xfs_extnum_t lastx; /* last extent pointer */
5791 xfs_fileoff_t fileblock;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005792
5793 if (startblock == HOLESTARTBLOCK) {
5794 mp = ip->i_mount;
5795 out->bmv_block = -1;
5796 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5797 fixlen -= out->bmv_offset;
5798 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5799 /* Came to hole at EOF. Trim it. */
5800 if (fixlen <= 0)
5801 return 0;
5802 out->bmv_length = fixlen;
5803 }
5804 } else {
Eric Sandeen5af317c2008-11-28 14:23:35 +11005805 if (startblock == DELAYSTARTBLOCK)
5806 out->bmv_block = -2;
5807 else
Eric Sandeen9d87c312009-01-14 23:22:07 -06005808 out->bmv_block = xfs_fsb_to_db(ip, startblock);
Eric Sandeen5af317c2008-11-28 14:23:35 +11005809 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5810 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5811 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5812 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5813 out->bmv_oflags |= BMV_OF_LAST;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005814 }
5815
5816 return 1;
5817}
5818
5819/*
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005820 * Get inode's extents as described in bmv, and format for output.
5821 * Calls formatter to fill the user's buffer until all extents
5822 * are mapped, until the passed-in bmv->bmv_count slots have
5823 * been filled, or until the formatter short-circuits the loop,
5824 * if it is tracking filled-in extents on its own.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 */
5826int /* error code */
5827xfs_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10005828 xfs_inode_t *ip,
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005829 struct getbmapx *bmv, /* user bmap structure */
5830 xfs_bmap_format_t formatter, /* format to user */
5831 void *arg) /* formatter arg */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832{
5833 __int64_t bmvend; /* last block requested */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005834 int error = 0; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 __int64_t fixlen; /* length for -1 case */
5836 int i; /* extent number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 int lock; /* lock state */
5838 xfs_bmbt_irec_t *map; /* buffer for user's data */
5839 xfs_mount_t *mp; /* file system mount point */
5840 int nex; /* # of user extents can do */
5841 int nexleft; /* # of user extents left */
5842 int subnex; /* # of bmapi's can do */
5843 int nmap; /* number of map entries */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005844 struct getbmapx *out; /* output structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 int whichfork; /* data or attr fork */
5846 int prealloced; /* this is a file with
5847 * preallocated data space */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005848 int iflags; /* interface flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 int bmapi_flags; /* flags for xfs_bmapi */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005850 int cur_ext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 mp = ip->i_mount;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005853 iflags = bmv->bmv_iflags;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005854 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 if (whichfork == XFS_ATTR_FORK) {
5857 if (XFS_IFORK_Q(ip)) {
5858 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5859 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5860 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5861 return XFS_ERROR(EINVAL);
5862 } else if (unlikely(
5863 ip->i_d.di_aformat != 0 &&
5864 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5865 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5866 ip->i_mount);
5867 return XFS_ERROR(EFSCORRUPTED);
5868 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005869
5870 prealloced = 0;
5871 fixlen = 1LL << 32;
5872 } else {
5873 /*
5874 * If the BMV_IF_NO_DMAPI_READ interface bit specified, do
5875 * not generate a DMAPI read event. Otherwise, if the
5876 * DM_EVENT_READ bit is set for the file, generate a read
5877 * event in order that the DMAPI application may do its thing
5878 * before we return the extents. Usually this means restoring
5879 * user file data to regions of the file that look like holes.
5880 *
5881 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5882 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5883 * If this were not true, callers of ioctl(XFS_IOC_GETBMAP)
5884 * could misinterpret holes in a DMAPI file as true holes,
5885 * when in fact they may represent offline user data.
5886 */
5887 if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) &&
5888 !(iflags & BMV_IF_NO_DMAPI_READ)) {
5889 error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip,
5890 0, 0, 0, NULL);
5891 if (error)
5892 return XFS_ERROR(error);
5893 }
5894
5895 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5896 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5897 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5898 return XFS_ERROR(EINVAL);
5899
David Chinner957d0eb2007-06-18 16:50:37 +10005900 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005901 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 prealloced = 1;
5903 fixlen = XFS_MAXIOFFSET(mp);
5904 } else {
5905 prealloced = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005906 fixlen = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 }
5909
5910 if (bmv->bmv_length == -1) {
5911 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005912 bmv->bmv_length =
5913 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5914 } else if (bmv->bmv_length == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 bmv->bmv_entries = 0;
5916 return 0;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005917 } else if (bmv->bmv_length < 0) {
5918 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005920
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 nex = bmv->bmv_count - 1;
5922 if (nex <= 0)
5923 return XFS_ERROR(EINVAL);
5924 bmvend = bmv->bmv_offset + bmv->bmv_length;
5925
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005926
5927 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5928 return XFS_ERROR(ENOMEM);
5929 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5930 if (!out)
5931 return XFS_ERROR(ENOMEM);
5932
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005934 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5935 if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) {
5936 error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF);
5937 if (error)
5938 goto out_unlock_iolock;
Niv Sardie12070a2008-03-06 13:43:03 +11005939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005941 ASSERT(ip->i_delayed_blks == 0);
5942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943
5944 lock = xfs_ilock_map_shared(ip);
5945
5946 /*
5947 * Don't let nex be bigger than the number of extents
5948 * we can have assuming alternating holes and real extents.
5949 */
5950 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5951 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5952
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005953 bmapi_flags = xfs_bmapi_aflag(whichfork);
5954 if (!(iflags & BMV_IF_PREALLOC))
5955 bmapi_flags |= XFS_BMAPI_IGSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956
5957 /*
5958 * Allocate enough space to handle "subnex" maps at a time.
5959 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005960 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 subnex = 16;
Christoph Hellwigca35dcd2009-07-18 18:14:54 -04005962 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005963 if (!map)
5964 goto out_unlock_ilock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965
5966 bmv->bmv_entries = 0;
5967
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005968 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5969 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5970 error = 0;
5971 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 }
5973
5974 nexleft = nex;
5975
5976 do {
5977 nmap = (nexleft > subnex) ? subnex : nexleft;
5978 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5979 XFS_BB_TO_FSB(mp, bmv->bmv_length),
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005980 bmapi_flags, NULL, 0, map, &nmap,
5981 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 if (error)
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005983 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 ASSERT(nmap <= subnex);
5985
5986 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005987 out[cur_ext].bmv_oflags = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005988 if (map[i].br_state == XFS_EXT_UNWRITTEN)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005989 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005990 else if (map[i].br_startblock == DELAYSTARTBLOCK)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005991 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5992 out[cur_ext].bmv_offset =
5993 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5994 out[cur_ext].bmv_length =
5995 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5996 out[cur_ext].bmv_unused1 = 0;
5997 out[cur_ext].bmv_unused2 = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005998 ASSERT(((iflags & BMV_IF_DELALLOC) != 0) ||
5999 (map[i].br_startblock != DELAYSTARTBLOCK));
Yingping Lu9af0a702005-11-02 15:09:54 +11006000 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10006001 whichfork == XFS_ATTR_FORK) {
6002 /* came to the end of attribute fork */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006003 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006004 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006006
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006007 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
6008 prealloced, bmvend,
6009 map[i].br_startblock))
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006010 goto out_free_map;
6011
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006012 nexleft--;
6013 bmv->bmv_offset =
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006014 out[cur_ext].bmv_offset +
6015 out[cur_ext].bmv_length;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006016 bmv->bmv_length =
6017 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
6018 bmv->bmv_entries++;
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006019 cur_ext++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 }
6021 } while (nmap && nexleft && bmv->bmv_length);
6022
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006023 out_free_map:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10006024 kmem_free(map);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006025 out_unlock_ilock:
6026 xfs_iunlock_map_shared(ip, lock);
6027 out_unlock_iolock:
6028 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006029
6030 for (i = 0; i < cur_ext; i++) {
6031 int full = 0; /* user array is full */
6032
6033 /* format results & advance arg */
6034 error = formatter(&arg, &out[i], &full);
6035 if (error || full)
6036 break;
6037 }
6038
Felix Blyakher7747a0b2009-06-11 17:07:28 -05006039 kmem_free(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 return error;
6041}
6042
6043/*
6044 * Check the last inode extent to determine whether this allocation will result
6045 * in blocks being allocated at the end of the file. When we allocate new data
6046 * blocks at the end of the file which do not start at the previous data block,
6047 * we will try to align the new blocks at stripe unit boundaries.
6048 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10006049STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050xfs_bmap_isaeof(
6051 xfs_inode_t *ip, /* incore inode pointer */
6052 xfs_fileoff_t off, /* file offset in fsblocks */
6053 int whichfork, /* data or attribute fork */
6054 char *aeof) /* return value */
6055{
6056 int error; /* error return value */
6057 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006058 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006059 xfs_extnum_t nextents; /* number of file extents */
6060 xfs_bmbt_irec_t s; /* expanded extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
6062 ASSERT(whichfork == XFS_DATA_FORK);
6063 ifp = XFS_IFORK_PTR(ip, whichfork);
6064 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6065 (error = xfs_iread_extents(NULL, ip, whichfork)))
6066 return error;
6067 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6068 if (nextents == 0) {
6069 *aeof = 1;
6070 return 0;
6071 }
6072 /*
6073 * Go to the last extent
6074 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006075 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 xfs_bmbt_get_all(lastrec, &s);
6077 /*
6078 * Check we are allocating in the last extent (for delayed allocations)
6079 * or past the last extent for non-delayed allocations.
6080 */
6081 *aeof = (off >= s.br_startoff &&
6082 off < s.br_startoff + s.br_blockcount &&
Eric Sandeen9d87c312009-01-14 23:22:07 -06006083 isnullstartblock(s.br_startblock)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 off >= s.br_startoff + s.br_blockcount;
6085 return 0;
6086}
6087
6088/*
6089 * Check if the endoff is outside the last extent. If so the caller will grow
6090 * the allocation to a stripe unit boundary.
6091 */
6092int /* error */
6093xfs_bmap_eof(
6094 xfs_inode_t *ip, /* incore inode pointer */
6095 xfs_fileoff_t endoff, /* file offset in fsblocks */
6096 int whichfork, /* data or attribute fork */
6097 int *eof) /* result value */
6098{
6099 xfs_fsblock_t blockcount; /* extent block count */
6100 int error; /* error return value */
6101 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006102 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006103 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 xfs_fileoff_t startoff; /* extent starting file offset */
6105
6106 ASSERT(whichfork == XFS_DATA_FORK);
6107 ifp = XFS_IFORK_PTR(ip, whichfork);
6108 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6109 (error = xfs_iread_extents(NULL, ip, whichfork)))
6110 return error;
6111 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6112 if (nextents == 0) {
6113 *eof = 1;
6114 return 0;
6115 }
6116 /*
6117 * Go to the last extent
6118 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006119 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 startoff = xfs_bmbt_get_startoff(lastrec);
6121 blockcount = xfs_bmbt_get_blockcount(lastrec);
6122 *eof = endoff >= startoff + blockcount;
6123 return 0;
6124}
6125
6126#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127STATIC
6128xfs_buf_t *
6129xfs_bmap_get_bp(
6130 xfs_btree_cur_t *cur,
6131 xfs_fsblock_t bno)
6132{
6133 int i;
6134 xfs_buf_t *bp;
6135
6136 if (!cur)
6137 return(NULL);
6138
6139 bp = NULL;
6140 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6141 bp = cur->bc_bufs[i];
6142 if (!bp) break;
6143 if (XFS_BUF_ADDR(bp) == bno)
6144 break; /* Found it */
6145 }
6146 if (i == XFS_BTREE_MAXLEVELS)
6147 bp = NULL;
6148
6149 if (!bp) { /* Chase down all the log items to see if the bp is there */
6150 xfs_log_item_chunk_t *licp;
6151 xfs_trans_t *tp;
6152
6153 tp = cur->bc_tp;
6154 licp = &tp->t_items;
6155 while (!bp && licp != NULL) {
Eric Sandeen39dab9d2008-08-13 16:10:52 +10006156 if (xfs_lic_are_all_free(licp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 licp = licp->lic_next;
6158 continue;
6159 }
6160 for (i = 0; i < licp->lic_unused; i++) {
6161 xfs_log_item_desc_t *lidp;
6162 xfs_log_item_t *lip;
6163 xfs_buf_log_item_t *bip;
6164 xfs_buf_t *lbp;
6165
Eric Sandeen39dab9d2008-08-13 16:10:52 +10006166 if (xfs_lic_isfree(licp, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 continue;
6168 }
6169
Eric Sandeen39dab9d2008-08-13 16:10:52 +10006170 lidp = xfs_lic_slot(licp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171 lip = lidp->lid_item;
6172 if (lip->li_type != XFS_LI_BUF)
6173 continue;
6174
6175 bip = (xfs_buf_log_item_t *)lip;
6176 lbp = bip->bli_buf;
6177
6178 if (XFS_BUF_ADDR(lbp) == bno) {
6179 bp = lbp;
6180 break; /* Found it */
6181 }
6182 }
6183 licp = licp->lic_next;
6184 }
6185 }
6186 return(bp);
6187}
6188
Hannes Eder3180e662009-03-04 19:34:10 +01006189STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190xfs_check_block(
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006191 struct xfs_btree_block *block,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 xfs_mount_t *mp,
6193 int root,
6194 short sz)
6195{
6196 int i, j, dmxr;
Christoph Hellwig576039c2006-09-28 10:58:06 +10006197 __be64 *pp, *thispa; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 xfs_bmbt_key_t *prevp, *keyp;
6199
Christoph Hellwig16259e72005-11-02 15:11:25 +11006200 ASSERT(be16_to_cpu(block->bb_level) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201
6202 prevp = NULL;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006203 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 dmxr = mp->m_bmap_dmxr[0];
Christoph Hellwig136341b2008-10-30 17:11:40 +11006205 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206
6207 if (prevp) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11006208 ASSERT(be64_to_cpu(prevp->br_startoff) <
6209 be64_to_cpu(keyp->br_startoff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 }
6211 prevp = keyp;
6212
6213 /*
6214 * Compare the block numbers to see if there are dups.
6215 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11006216 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11006217 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11006218 else
6219 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
6220
Christoph Hellwig16259e72005-11-02 15:11:25 +11006221 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11006222 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11006223 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11006224 else
6225 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006226 if (*thispa == *pp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
Harvey Harrison34a622b2008-04-10 12:19:21 +10006228 __func__, j, i,
Christoph Hellwig576039c2006-09-28 10:58:06 +10006229 (unsigned long long)be64_to_cpu(*thispa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 panic("%s: ptrs are equal in node\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +10006231 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 }
6233 }
6234 }
6235}
6236
6237/*
6238 * Check that the extents for the inode ip are in the right order in all
6239 * btree leaves.
6240 */
6241
6242STATIC void
6243xfs_bmap_check_leaf_extents(
6244 xfs_btree_cur_t *cur, /* btree cursor or null */
6245 xfs_inode_t *ip, /* incore inode pointer */
6246 int whichfork) /* data or attr fork */
6247{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006248 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 xfs_fsblock_t bno; /* block # of "block" */
6250 xfs_buf_t *bp; /* buffer for "block" */
6251 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006252 xfs_extnum_t i=0, j; /* index into the extents list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 xfs_ifork_t *ifp; /* fork structure */
6254 int level; /* btree level, for checking */
6255 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10006256 __be64 *pp; /* pointer to block address */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006257 xfs_bmbt_rec_t *ep; /* pointer to current extent */
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006258 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006259 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 int bp_release = 0;
6261
6262 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6263 return;
6264 }
6265
6266 bno = NULLFSBLOCK;
6267 mp = ip->i_mount;
6268 ifp = XFS_IFORK_PTR(ip, whichfork);
6269 block = ifp->if_broot;
6270 /*
6271 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6272 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11006273 level = be16_to_cpu(block->bb_level);
6274 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11006276 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006277 bno = be64_to_cpu(*pp);
6278
6279 ASSERT(bno != NULLDFSBNO);
6280 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6281 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6282
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 /*
6284 * Go down the tree until leaf level is reached, following the first
6285 * pointer (leftmost) at each level.
6286 */
6287 while (level-- > 0) {
6288 /* See if buf is in cur first */
6289 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6290 if (bp) {
6291 bp_release = 0;
6292 } else {
6293 bp_release = 1;
6294 }
6295 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6296 XFS_BMAP_BTREE_REF)))
6297 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006298 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11006300 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 error0);
6302 if (level == 0)
6303 break;
6304
6305 /*
6306 * Check this block for basic sanity (increasing keys and
6307 * no duplicate blocks).
6308 */
6309
6310 xfs_check_block(block, mp, 0, 0);
Christoph Hellwig136341b2008-10-30 17:11:40 +11006311 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006312 bno = be64_to_cpu(*pp);
6313 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 if (bp_release) {
6315 bp_release = 0;
6316 xfs_trans_brelse(NULL, bp);
6317 }
6318 }
6319
6320 /*
6321 * Here with bp and block set to the leftmost leaf node in the tree.
6322 */
6323 i = 0;
6324
6325 /*
6326 * Loop over all leaf nodes checking that all extents are in the right order.
6327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329 xfs_fsblock_t nextbno;
6330 xfs_extnum_t num_recs;
6331
6332
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006333 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334
6335 /*
6336 * Read-ahead the next leaf block, if any.
6337 */
6338
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006339 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340
6341 /*
6342 * Check all the extents to make sure they are OK.
6343 * If we had a previous block, the last entry should
6344 * conform with the first entry in this one.
6345 */
6346
Christoph Hellwig136341b2008-10-30 17:11:40 +11006347 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006348 if (i) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11006349 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
6350 xfs_bmbt_disk_get_blockcount(&last) <=
6351 xfs_bmbt_disk_get_startoff(ep));
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006352 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006353 for (j = 1; j < num_recs; j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11006354 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
Christoph Hellwig4a26e662008-10-30 16:58:32 +11006355 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
6356 xfs_bmbt_disk_get_blockcount(ep) <=
6357 xfs_bmbt_disk_get_startoff(nextp));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006358 ep = nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006361 last = *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 i += num_recs;
6363 if (bp_release) {
6364 bp_release = 0;
6365 xfs_trans_brelse(NULL, bp);
6366 }
6367 bno = nextbno;
6368 /*
6369 * If we've reached the end, stop.
6370 */
6371 if (bno == NULLFSBLOCK)
6372 break;
6373
6374 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6375 if (bp) {
6376 bp_release = 0;
6377 } else {
6378 bp_release = 1;
6379 }
6380 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6381 XFS_BMAP_BTREE_REF)))
6382 goto error_norelse;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006383 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 }
6385 if (bp_release) {
6386 bp_release = 0;
6387 xfs_trans_brelse(NULL, bp);
6388 }
6389 return;
6390
6391error0:
Harvey Harrison34a622b2008-04-10 12:19:21 +10006392 cmn_err(CE_WARN, "%s: at error0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 if (bp_release)
6394 xfs_trans_brelse(NULL, bp);
6395error_norelse:
6396 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
Harvey Harrison34a622b2008-04-10 12:19:21 +10006397 __func__, i);
6398 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 return;
6400}
6401#endif
6402
6403/*
6404 * Count fsblocks of the given fork.
6405 */
6406int /* error */
6407xfs_bmap_count_blocks(
6408 xfs_trans_t *tp, /* transaction pointer */
6409 xfs_inode_t *ip, /* incore inode */
6410 int whichfork, /* data or attr fork */
6411 int *count) /* out: count of blocks */
6412{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006413 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 xfs_fsblock_t bno; /* block # of "block" */
6415 xfs_ifork_t *ifp; /* fork structure */
6416 int level; /* btree level, for checking */
6417 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10006418 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419
6420 bno = NULLFSBLOCK;
6421 mp = ip->i_mount;
6422 ifp = XFS_IFORK_PTR(ip, whichfork);
6423 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
Ruben Porrasc94312d2008-08-13 16:52:25 +10006424 xfs_bmap_count_leaves(ifp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
Ruben Porrasc94312d2008-08-13 16:52:25 +10006426 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 return 0;
6428 }
6429
6430 /*
6431 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6432 */
6433 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11006434 level = be16_to_cpu(block->bb_level);
6435 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11006436 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006437 bno = be64_to_cpu(*pp);
6438 ASSERT(bno != NULLDFSBNO);
6439 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6440 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006442 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6444 mp);
6445 return XFS_ERROR(EFSCORRUPTED);
6446 }
6447
6448 return 0;
6449}
6450
6451/*
6452 * Recursively walks each level of a btree
6453 * to count total fsblocks is use.
6454 */
David Chinnera8272ce2007-11-23 16:28:09 +11006455STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456xfs_bmap_count_tree(
6457 xfs_mount_t *mp, /* file system mount point */
6458 xfs_trans_t *tp, /* transaction pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006459 xfs_ifork_t *ifp, /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 xfs_fsblock_t blockno, /* file system block number */
6461 int levelin, /* level in btree */
6462 int *count) /* Count of blocks */
6463{
6464 int error;
6465 xfs_buf_t *bp, *nbp;
6466 int level = levelin;
Christoph Hellwig576039c2006-09-28 10:58:06 +10006467 __be64 *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 xfs_fsblock_t bno = blockno;
6469 xfs_fsblock_t nextbno;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006470 struct xfs_btree_block *block, *nextblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472
6473 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6474 return error;
6475 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006476 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477
6478 if (--level) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +02006479 /* Not at node above leaves, count this level of nodes */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006480 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 while (nextbno != NULLFSBLOCK) {
6482 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6483 0, &nbp, XFS_BMAP_BTREE_REF)))
6484 return error;
6485 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006486 nextblock = XFS_BUF_TO_BLOCK(nbp);
6487 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488 xfs_trans_brelse(tp, nbp);
6489 }
6490
6491 /* Dive to the next level */
Christoph Hellwig136341b2008-10-30 17:11:40 +11006492 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006493 bno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494 if (unlikely((error =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006495 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 xfs_trans_brelse(tp, bp);
6497 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6498 XFS_ERRLEVEL_LOW, mp);
6499 return XFS_ERROR(EFSCORRUPTED);
6500 }
6501 xfs_trans_brelse(tp, bp);
6502 } else {
6503 /* count all level 1 nodes and their leaves */
6504 for (;;) {
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006505 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Christoph Hellwig16259e72005-11-02 15:11:25 +11006506 numrecs = be16_to_cpu(block->bb_numrecs);
Christoph Hellwig136341b2008-10-30 17:11:40 +11006507 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 xfs_trans_brelse(tp, bp);
6509 if (nextbno == NULLFSBLOCK)
6510 break;
6511 bno = nextbno;
6512 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6513 XFS_BMAP_BTREE_REF)))
6514 return error;
6515 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006516 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 }
6518 }
6519 return 0;
6520}
6521
6522/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006523 * Count leaf blocks given a range of extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006525STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006527 xfs_ifork_t *ifp,
6528 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 int numrecs,
6530 int *count)
6531{
6532 int b;
6533
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006534 for (b = 0; b < numrecs; b++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006535 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
Yingping Lu91e11082005-11-02 15:10:24 +11006536 *count += xfs_bmbt_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006537 }
Yingping Lu91e11082005-11-02 15:10:24 +11006538}
6539
6540/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006541 * Count leaf blocks given a range of extent records originally
6542 * in btree format.
Yingping Lu91e11082005-11-02 15:10:24 +11006543 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006544STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +11006545xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +11006546 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006547 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +11006548 int numrecs,
6549 int *count)
6550{
6551 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006552 xfs_bmbt_rec_t *frp;
Yingping Lu91e11082005-11-02 15:10:24 +11006553
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006554 for (b = 1; b <= numrecs; b++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11006555 frp = XFS_BMBT_REC_ADDR(mp, block, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 *count += xfs_bmbt_disk_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558}