blob: 6d9b5448deb25151d3ab946317d03281eb8f9c60 [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
387STATIC int
388xfs_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
Yingping Lu91e11082005-11-02 15:10:24 +1100394STATIC int
395xfs_bmap_disk_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100396 xfs_extnum_t idx,
397 xfs_bmbt_block_t *block,
Yingping Lu91e11082005-11-02 15:10:24 +1100398 int numrecs,
399 int *count);
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401/*
402 * Bmap internal routines.
403 */
404
405/*
406 * Called from xfs_bmap_add_attrfork to handle btree format files.
407 */
408STATIC int /* error */
409xfs_bmap_add_attrfork_btree(
410 xfs_trans_t *tp, /* transaction pointer */
411 xfs_inode_t *ip, /* incore inode pointer */
412 xfs_fsblock_t *firstblock, /* first block allocated */
413 xfs_bmap_free_t *flist, /* blocks to free at commit */
414 int *flags) /* inode logging flags */
415{
416 xfs_btree_cur_t *cur; /* btree cursor */
417 int error; /* error return value */
418 xfs_mount_t *mp; /* file system mount struct */
419 int stat; /* newroot status */
420
421 mp = ip->i_mount;
422 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
423 *flags |= XFS_ILOG_DBROOT;
424 else {
425 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
426 XFS_DATA_FORK);
427 cur->bc_private.b.flist = flist;
428 cur->bc_private.b.firstblock = *firstblock;
429 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
430 goto error0;
431 ASSERT(stat == 1); /* must be at least one entry */
432 if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
433 goto error0;
434 if (stat == 0) {
435 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
436 return XFS_ERROR(ENOSPC);
437 }
438 *firstblock = cur->bc_private.b.firstblock;
439 cur->bc_private.b.allocated = 0;
440 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
441 }
442 return 0;
443error0:
444 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
445 return error;
446}
447
448/*
449 * Called from xfs_bmap_add_attrfork to handle extents format files.
450 */
451STATIC int /* error */
452xfs_bmap_add_attrfork_extents(
453 xfs_trans_t *tp, /* transaction pointer */
454 xfs_inode_t *ip, /* incore inode pointer */
455 xfs_fsblock_t *firstblock, /* first block allocated */
456 xfs_bmap_free_t *flist, /* blocks to free at commit */
457 int *flags) /* inode logging flags */
458{
459 xfs_btree_cur_t *cur; /* bmap btree cursor */
460 int error; /* error return value */
461
462 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
463 return 0;
464 cur = NULL;
465 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
466 flags, XFS_DATA_FORK);
467 if (cur) {
468 cur->bc_private.b.allocated = 0;
469 xfs_btree_del_cursor(cur,
470 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
471 }
472 return error;
473}
474
475/*
476 * Called from xfs_bmap_add_attrfork to handle local format files.
477 */
478STATIC int /* error */
479xfs_bmap_add_attrfork_local(
480 xfs_trans_t *tp, /* transaction pointer */
481 xfs_inode_t *ip, /* incore inode pointer */
482 xfs_fsblock_t *firstblock, /* first block allocated */
483 xfs_bmap_free_t *flist, /* blocks to free at commit */
484 int *flags) /* inode logging flags */
485{
486 xfs_da_args_t dargs; /* args for dir/attr code */
487 int error; /* error return value */
488 xfs_mount_t *mp; /* mount structure pointer */
489
490 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
491 return 0;
492 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
493 mp = ip->i_mount;
494 memset(&dargs, 0, sizeof(dargs));
495 dargs.dp = ip;
496 dargs.firstblock = firstblock;
497 dargs.flist = flist;
498 dargs.total = mp->m_dirblkfsbs;
499 dargs.whichfork = XFS_DATA_FORK;
500 dargs.trans = tp;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000501 error = xfs_dir2_sf_to_block(&dargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 } else
503 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
504 XFS_DATA_FORK);
505 return error;
506}
507
508/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100509 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 * after allocating space (or doing a delayed allocation).
511 */
512STATIC int /* error */
513xfs_bmap_add_extent(
514 xfs_inode_t *ip, /* incore inode pointer */
515 xfs_extnum_t idx, /* extent number to update/insert */
516 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100517 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 xfs_fsblock_t *first, /* pointer to firstblock variable */
519 xfs_bmap_free_t *flist, /* list of extents to be freed */
520 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000521 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int whichfork, /* data or attr fork */
523 int rsvd) /* OK to use reserved data blocks */
524{
525 xfs_btree_cur_t *cur; /* btree cursor or null */
526 xfs_filblks_t da_new; /* new count del alloc blocks used */
527 xfs_filblks_t da_old; /* old count del alloc blocks used */
528 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 xfs_ifork_t *ifp; /* inode fork ptr */
530 int logflags; /* returned value */
531 xfs_extnum_t nextents; /* number of extents in file now */
532
533 XFS_STATS_INC(xs_add_exlist);
534 cur = *curp;
535 ifp = XFS_IFORK_PTR(ip, whichfork);
536 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
537 ASSERT(idx <= nextents);
538 da_old = da_new = 0;
539 error = 0;
540 /*
541 * This is the first extent added to a new/empty file.
542 * Special case this one, so other routines get to assume there are
543 * already extents in the list.
544 */
545 if (nextents == 0) {
Eric Sandeen3a59c942007-07-11 11:09:47 +1000546 XFS_BMAP_TRACE_INSERT("insert empty", ip, 0, 1, new, NULL,
547 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100548 xfs_iext_insert(ifp, 0, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 ASSERT(cur == NULL);
550 ifp->if_lastex = 0;
551 if (!ISNULLSTARTBLOCK(new->br_startblock)) {
552 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
553 logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
554 } else
555 logflags = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000556 /* DELTA: single new extent */
557 if (delta) {
558 if (delta->xed_startoff > new->br_startoff)
559 delta->xed_startoff = new->br_startoff;
560 if (delta->xed_blockcount <
561 new->br_startoff + new->br_blockcount)
562 delta->xed_blockcount = new->br_startoff +
563 new->br_blockcount;
564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566 /*
567 * Any kind of new delayed allocation goes here.
568 */
569 else if (ISNULLSTARTBLOCK(new->br_startblock)) {
570 if (cur)
571 ASSERT((cur->bc_private.b.flags &
572 XFS_BTCUR_BPRV_WASDEL) == 0);
Eric Sandeene9ed9d22007-05-08 13:48:56 +1000573 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000574 &logflags, delta, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 goto done;
576 }
577 /*
578 * Real allocation off the end of the file.
579 */
580 else if (idx == nextents) {
581 if (cur)
582 ASSERT((cur->bc_private.b.flags &
583 XFS_BTCUR_BPRV_WASDEL) == 0);
584 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000585 &logflags, delta, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto done;
587 } else {
588 xfs_bmbt_irec_t prev; /* old extent at offset idx */
589
590 /*
591 * Get the record referred to by idx.
592 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100593 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /*
595 * If it's a real allocation record, and the new allocation ends
596 * after the start of the referred to record, then we're filling
597 * in a delayed or unwritten allocation with a real one, or
598 * converting real back to unwritten.
599 */
600 if (!ISNULLSTARTBLOCK(new->br_startblock) &&
601 new->br_startoff + new->br_blockcount > prev.br_startoff) {
602 if (prev.br_state != XFS_EXT_UNWRITTEN &&
603 ISNULLSTARTBLOCK(prev.br_startblock)) {
604 da_old = STARTBLOCKVAL(prev.br_startblock);
605 if (cur)
606 ASSERT(cur->bc_private.b.flags &
607 XFS_BTCUR_BPRV_WASDEL);
608 if ((error = xfs_bmap_add_extent_delay_real(ip,
609 idx, &cur, new, &da_new, first, flist,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000610 &logflags, delta, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 goto done;
612 } else if (new->br_state == XFS_EXT_NORM) {
613 ASSERT(new->br_state == XFS_EXT_NORM);
614 if ((error = xfs_bmap_add_extent_unwritten_real(
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000615 ip, idx, &cur, new, &logflags, delta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 goto done;
617 } else {
618 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
619 if ((error = xfs_bmap_add_extent_unwritten_real(
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000620 ip, idx, &cur, new, &logflags, delta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 goto done;
622 }
623 ASSERT(*curp == cur || *curp == NULL);
624 }
625 /*
626 * Otherwise we're filling in a hole with an allocation.
627 */
628 else {
629 if (cur)
630 ASSERT((cur->bc_private.b.flags &
631 XFS_BTCUR_BPRV_WASDEL) == 0);
632 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000633 new, &logflags, delta, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 goto done;
635 }
636 }
637
638 ASSERT(*curp == cur || *curp == NULL);
639 /*
640 * Convert to a btree if necessary.
641 */
642 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
643 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
644 int tmp_logflags; /* partial log flag return val */
645
646 ASSERT(cur == NULL);
647 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
648 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
649 logflags |= tmp_logflags;
650 if (error)
651 goto done;
652 }
653 /*
654 * Adjust for changes in reserved delayed indirect blocks.
655 * Nothing to do for disk quotas here.
656 */
657 if (da_old || da_new) {
658 xfs_filblks_t nblks;
659
660 nblks = da_new;
661 if (cur)
662 nblks += cur->bc_private.b.allocated;
663 ASSERT(nblks <= da_old);
664 if (nblks < da_old)
665 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100666 (int64_t)(da_old - nblks), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668 /*
669 * Clear out the allocated field, done with it now in any case.
670 */
671 if (cur) {
672 cur->bc_private.b.allocated = 0;
673 *curp = cur;
674 }
675done:
676#ifdef DEBUG
677 if (!error)
678 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
679#endif
680 *logflagsp = logflags;
681 return error;
682}
683
684/*
685 * Called by xfs_bmap_add_extent to handle cases converting a delayed
686 * allocation to a real allocation.
687 */
688STATIC int /* error */
689xfs_bmap_add_extent_delay_real(
690 xfs_inode_t *ip, /* incore inode pointer */
691 xfs_extnum_t idx, /* extent number to update/insert */
692 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100693 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
695 xfs_fsblock_t *first, /* pointer to firstblock variable */
696 xfs_bmap_free_t *flist, /* list of extents to be freed */
697 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000698 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 int rsvd) /* OK to use reserved data block allocation */
700{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 xfs_btree_cur_t *cur; /* btree cursor */
702 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000703 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100706 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 xfs_fileoff_t new_endoff; /* end offset of new entry */
708 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
709 /* left is 0, right is 1, prev is 2 */
710 int rval=0; /* return value (logging flags) */
711 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000712 xfs_filblks_t temp=0; /* value for dnew calculations */
713 xfs_filblks_t temp2=0;/* value for dnew calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 int tmp_rval; /* partial logging flags */
715 enum { /* bit number definitions for state */
716 LEFT_CONTIG, RIGHT_CONTIG,
717 LEFT_FILLING, RIGHT_FILLING,
718 LEFT_DELAY, RIGHT_DELAY,
719 LEFT_VALID, RIGHT_VALID
720 };
721
722#define LEFT r[0]
723#define RIGHT r[1]
724#define PREV r[2]
725#define MASK(b) (1 << (b))
726#define MASK2(a,b) (MASK(a) | MASK(b))
727#define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
728#define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
729#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
730#define STATE_TEST(b) (state & MASK(b))
731#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
732 ((state &= ~MASK(b)), 0))
733#define SWITCH_STATE \
734 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
735
736 /*
737 * Set up a bunch of variables to make the tests simpler.
738 */
739 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100740 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
741 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 xfs_bmbt_get_all(ep, &PREV);
743 new_endoff = new->br_startoff + new->br_blockcount;
744 ASSERT(PREV.br_startoff <= new->br_startoff);
745 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
746 /*
747 * Set flags determining what part of the previous delayed allocation
748 * extent is being replaced by a real allocation.
749 */
750 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
751 STATE_SET(RIGHT_FILLING,
752 PREV.br_startoff + PREV.br_blockcount == new_endoff);
753 /*
754 * Check and set flags if this segment has a left neighbor.
755 * Don't set contiguous if the combined extent would be too large.
756 */
757 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100758 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
760 }
761 STATE_SET(LEFT_CONTIG,
762 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
763 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
764 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
765 LEFT.br_state == new->br_state &&
766 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
767 /*
768 * Check and set flags if this segment has a right neighbor.
769 * Don't set contiguous if the combined extent would be too large.
770 * Also check for all-three-contiguous being too large.
771 */
772 if (STATE_SET_TEST(RIGHT_VALID,
773 idx <
774 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100775 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
777 }
778 STATE_SET(RIGHT_CONTIG,
779 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
780 new_endoff == RIGHT.br_startoff &&
781 new->br_startblock + new->br_blockcount ==
782 RIGHT.br_startblock &&
783 new->br_state == RIGHT.br_state &&
784 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
785 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
786 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
787 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
788 <= MAXEXTLEN));
789 error = 0;
790 /*
791 * Switch out based on the FILLING and CONTIG state bits.
792 */
793 switch (SWITCH_STATE) {
794
795 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
796 /*
797 * Filling in all of a previously delayed allocation extent.
798 * The left and right neighbors are both contiguous with new.
799 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000800 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100802 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 LEFT.br_blockcount + PREV.br_blockcount +
804 RIGHT.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000805 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000807 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100808 xfs_iext_remove(ifp, idx, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 ip->i_df.if_lastex = idx - 1;
810 ip->i_d.di_nextents--;
811 if (cur == NULL)
812 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
813 else {
814 rval = XFS_ILOG_CORE;
815 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
816 RIGHT.br_startblock,
817 RIGHT.br_blockcount, &i)))
818 goto done;
819 ASSERT(i == 1);
820 if ((error = xfs_bmbt_delete(cur, &i)))
821 goto done;
822 ASSERT(i == 1);
823 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
824 goto done;
825 ASSERT(i == 1);
826 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
827 LEFT.br_startblock,
828 LEFT.br_blockcount +
829 PREV.br_blockcount +
830 RIGHT.br_blockcount, LEFT.br_state)))
831 goto done;
832 }
833 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000834 /* DELTA: Three in-core extents are replaced by one. */
835 temp = LEFT.br_startoff;
836 temp2 = LEFT.br_blockcount +
837 PREV.br_blockcount +
838 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 break;
840
841 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
842 /*
843 * Filling in all of a previously delayed allocation extent.
844 * The left neighbor is contiguous, the right is not.
845 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000846 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100848 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 LEFT.br_blockcount + PREV.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000850 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 XFS_DATA_FORK);
852 ip->i_df.if_lastex = idx - 1;
Eric Sandeen3a59c942007-07-11 11:09:47 +1000853 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100854 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (cur == NULL)
856 rval = XFS_ILOG_DEXT;
857 else {
858 rval = 0;
859 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
860 LEFT.br_startblock, LEFT.br_blockcount,
861 &i)))
862 goto done;
863 ASSERT(i == 1);
864 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
865 LEFT.br_startblock,
866 LEFT.br_blockcount +
867 PREV.br_blockcount, LEFT.br_state)))
868 goto done;
869 }
870 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000871 /* DELTA: Two in-core extents are replaced by one. */
872 temp = LEFT.br_startoff;
873 temp2 = LEFT.br_blockcount +
874 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 break;
876
877 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
878 /*
879 * Filling in all of a previously delayed allocation extent.
880 * The right neighbor is contiguous, the left is not.
881 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000882 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 xfs_bmbt_set_startblock(ep, new->br_startblock);
884 xfs_bmbt_set_blockcount(ep,
885 PREV.br_blockcount + RIGHT.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000886 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 ip->i_df.if_lastex = idx;
Eric Sandeen3a59c942007-07-11 11:09:47 +1000888 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100889 xfs_iext_remove(ifp, idx + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (cur == NULL)
891 rval = XFS_ILOG_DEXT;
892 else {
893 rval = 0;
894 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
895 RIGHT.br_startblock,
896 RIGHT.br_blockcount, &i)))
897 goto done;
898 ASSERT(i == 1);
899 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
900 new->br_startblock,
901 PREV.br_blockcount +
902 RIGHT.br_blockcount, PREV.br_state)))
903 goto done;
904 }
905 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000906 /* DELTA: Two in-core extents are replaced by one. */
907 temp = PREV.br_startoff;
908 temp2 = PREV.br_blockcount +
909 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 break;
911
912 case MASK2(LEFT_FILLING, RIGHT_FILLING):
913 /*
914 * Filling in all of a previously delayed allocation extent.
915 * Neither the left nor right neighbors are contiguous with
916 * the new one.
917 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000918 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 xfs_bmbt_set_startblock(ep, new->br_startblock);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000920 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 ip->i_df.if_lastex = idx;
922 ip->i_d.di_nextents++;
923 if (cur == NULL)
924 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
925 else {
926 rval = XFS_ILOG_CORE;
927 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
928 new->br_startblock, new->br_blockcount,
929 &i)))
930 goto done;
931 ASSERT(i == 0);
932 cur->bc_rec.b.br_state = XFS_EXT_NORM;
933 if ((error = xfs_bmbt_insert(cur, &i)))
934 goto done;
935 ASSERT(i == 1);
936 }
937 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000938 /* DELTA: The in-core extent described by new changed type. */
939 temp = new->br_startoff;
940 temp2 = new->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 break;
942
943 case MASK2(LEFT_FILLING, LEFT_CONTIG):
944 /*
945 * Filling in the first part of a previous delayed allocation.
946 * The left neighbor is contiguous.
947 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000948 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100949 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 LEFT.br_blockcount + new->br_blockcount);
951 xfs_bmbt_set_startoff(ep,
952 PREV.br_startoff + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000953 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 temp = PREV.br_blockcount - new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +1000955 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 xfs_bmbt_set_blockcount(ep, temp);
957 ip->i_df.if_lastex = idx - 1;
958 if (cur == NULL)
959 rval = XFS_ILOG_DEXT;
960 else {
961 rval = 0;
962 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
963 LEFT.br_startblock, LEFT.br_blockcount,
964 &i)))
965 goto done;
966 ASSERT(i == 1);
967 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
968 LEFT.br_startblock,
969 LEFT.br_blockcount +
970 new->br_blockcount,
971 LEFT.br_state)))
972 goto done;
973 }
974 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
975 STARTBLOCKVAL(PREV.br_startblock));
976 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +1000977 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000979 /* DELTA: The boundary between two in-core extents moved. */
980 temp = LEFT.br_startoff;
981 temp2 = LEFT.br_blockcount +
982 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 break;
984
985 case MASK(LEFT_FILLING):
986 /*
987 * Filling in the first part of a previous delayed allocation.
988 * The left neighbor is not contiguous.
989 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000990 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 xfs_bmbt_set_startoff(ep, new_endoff);
992 temp = PREV.br_blockcount - new->br_blockcount;
993 xfs_bmbt_set_blockcount(ep, temp);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000994 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100996 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 ip->i_df.if_lastex = idx;
998 ip->i_d.di_nextents++;
999 if (cur == NULL)
1000 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1001 else {
1002 rval = XFS_ILOG_CORE;
1003 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1004 new->br_startblock, new->br_blockcount,
1005 &i)))
1006 goto done;
1007 ASSERT(i == 0);
1008 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1009 if ((error = xfs_bmbt_insert(cur, &i)))
1010 goto done;
1011 ASSERT(i == 1);
1012 }
1013 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1014 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1015 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1016 first, flist, &cur, 1, &tmp_rval,
1017 XFS_DATA_FORK);
1018 rval |= tmp_rval;
1019 if (error)
1020 goto done;
1021 }
1022 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1023 STARTBLOCKVAL(PREV.br_startblock) -
1024 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001025 ep = xfs_iext_get_ext(ifp, idx + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001027 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx + 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001029 /* DELTA: One in-core extent is split in two. */
1030 temp = PREV.br_startoff;
1031 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 break;
1033
1034 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1035 /*
1036 * Filling in the last part of a previous delayed allocation.
1037 * The right neighbor is contiguous with the new allocation.
1038 */
1039 temp = PREV.br_blockcount - new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001040 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
1041 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 xfs_bmbt_set_blockcount(ep, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001043 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1044 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 new->br_blockcount + RIGHT.br_blockcount,
1046 RIGHT.br_state);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001047 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 ip->i_df.if_lastex = idx + 1;
1049 if (cur == NULL)
1050 rval = XFS_ILOG_DEXT;
1051 else {
1052 rval = 0;
1053 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1054 RIGHT.br_startblock,
1055 RIGHT.br_blockcount, &i)))
1056 goto done;
1057 ASSERT(i == 1);
1058 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1059 new->br_startblock,
1060 new->br_blockcount +
1061 RIGHT.br_blockcount,
1062 RIGHT.br_state)))
1063 goto done;
1064 }
1065 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1066 STARTBLOCKVAL(PREV.br_startblock));
1067 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001068 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001070 /* DELTA: The boundary between two in-core extents moved. */
1071 temp = PREV.br_startoff;
1072 temp2 = PREV.br_blockcount +
1073 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 break;
1075
1076 case MASK(RIGHT_FILLING):
1077 /*
1078 * Filling in the last part of a previous delayed allocation.
1079 * The right neighbor is not contiguous.
1080 */
1081 temp = PREV.br_blockcount - new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001082 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 xfs_bmbt_set_blockcount(ep, temp);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001084 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1085 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001086 xfs_iext_insert(ifp, idx + 1, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 ip->i_df.if_lastex = idx + 1;
1088 ip->i_d.di_nextents++;
1089 if (cur == NULL)
1090 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1091 else {
1092 rval = XFS_ILOG_CORE;
1093 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1094 new->br_startblock, new->br_blockcount,
1095 &i)))
1096 goto done;
1097 ASSERT(i == 0);
1098 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1099 if ((error = xfs_bmbt_insert(cur, &i)))
1100 goto done;
1101 ASSERT(i == 1);
1102 }
1103 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1104 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1105 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1106 first, flist, &cur, 1, &tmp_rval,
1107 XFS_DATA_FORK);
1108 rval |= tmp_rval;
1109 if (error)
1110 goto done;
1111 }
1112 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1113 STARTBLOCKVAL(PREV.br_startblock) -
1114 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001115 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001117 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001119 /* DELTA: One in-core extent is split in two. */
1120 temp = PREV.br_startoff;
1121 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 break;
1123
1124 case 0:
1125 /*
1126 * Filling in the middle part of a previous delayed allocation.
1127 * Contiguity is impossible here.
1128 * This case is avoided almost all the time.
1129 */
1130 temp = new->br_startoff - PREV.br_startoff;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001131 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 xfs_bmbt_set_blockcount(ep, temp);
1133 r[0] = *new;
Lachlan McIlroyd2133712006-11-21 18:55:16 +11001134 r[1].br_state = PREV.br_state;
1135 r[1].br_startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 r[1].br_startoff = new_endoff;
1137 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1138 r[1].br_blockcount = temp2;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001139 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001141 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 ip->i_df.if_lastex = idx + 1;
1143 ip->i_d.di_nextents++;
1144 if (cur == NULL)
1145 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1146 else {
1147 rval = XFS_ILOG_CORE;
1148 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1149 new->br_startblock, new->br_blockcount,
1150 &i)))
1151 goto done;
1152 ASSERT(i == 0);
1153 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1154 if ((error = xfs_bmbt_insert(cur, &i)))
1155 goto done;
1156 ASSERT(i == 1);
1157 }
1158 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1159 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1160 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1161 first, flist, &cur, 1, &tmp_rval,
1162 XFS_DATA_FORK);
1163 rval |= tmp_rval;
1164 if (error)
1165 goto done;
1166 }
1167 temp = xfs_bmap_worst_indlen(ip, temp);
1168 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1169 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
1170 (cur ? cur->bc_private.b.allocated : 0));
1171 if (diff > 0 &&
David Chinner20f4ebf2007-02-10 18:36:10 +11001172 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /*
1174 * Ick gross gag me with a spoon.
1175 */
1176 ASSERT(0); /* want to see if this ever happens! */
1177 while (diff > 0) {
1178 if (temp) {
1179 temp--;
1180 diff--;
1181 if (!diff ||
1182 !xfs_mod_incore_sb(ip->i_mount,
David Chinner20f4ebf2007-02-10 18:36:10 +11001183 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 break;
1185 }
1186 if (temp2) {
1187 temp2--;
1188 diff--;
1189 if (!diff ||
1190 !xfs_mod_incore_sb(ip->i_mount,
David Chinner20f4ebf2007-02-10 18:36:10 +11001191 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 break;
1193 }
1194 }
1195 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001196 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001198 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
1199 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001200 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1201 NULLSTARTBLOCK((int)temp2));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001202 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 *dnew = temp + temp2;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001204 /* DELTA: One in-core extent is split in three. */
1205 temp = PREV.br_startoff;
1206 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 break;
1208
1209 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1210 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1211 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1212 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1213 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1214 case MASK(LEFT_CONTIG):
1215 case MASK(RIGHT_CONTIG):
1216 /*
1217 * These cases are all impossible.
1218 */
1219 ASSERT(0);
1220 }
1221 *curp = cur;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001222 if (delta) {
1223 temp2 += temp;
1224 if (delta->xed_startoff > temp)
1225 delta->xed_startoff = temp;
1226 if (delta->xed_blockcount < temp2)
1227 delta->xed_blockcount = temp2;
1228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229done:
1230 *logflagsp = rval;
1231 return error;
1232#undef LEFT
1233#undef RIGHT
1234#undef PREV
1235#undef MASK
1236#undef MASK2
1237#undef MASK3
1238#undef MASK4
1239#undef STATE_SET
1240#undef STATE_TEST
1241#undef STATE_SET_TEST
1242#undef SWITCH_STATE
1243}
1244
1245/*
1246 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1247 * allocation to a real allocation or vice versa.
1248 */
1249STATIC int /* error */
1250xfs_bmap_add_extent_unwritten_real(
1251 xfs_inode_t *ip, /* incore inode pointer */
1252 xfs_extnum_t idx, /* extent number to update/insert */
1253 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001254 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001255 int *logflagsp, /* inode logging flags */
1256 xfs_extdelta_t *delta) /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001259 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001262 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 xfs_fileoff_t new_endoff; /* end offset of new entry */
1264 xfs_exntst_t newext; /* new extent state */
1265 xfs_exntst_t oldext; /* old extent state */
1266 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1267 /* left is 0, right is 1, prev is 2 */
1268 int rval=0; /* return value (logging flags) */
1269 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001270 xfs_filblks_t temp=0;
1271 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 enum { /* bit number definitions for state */
1273 LEFT_CONTIG, RIGHT_CONTIG,
1274 LEFT_FILLING, RIGHT_FILLING,
1275 LEFT_DELAY, RIGHT_DELAY,
1276 LEFT_VALID, RIGHT_VALID
1277 };
1278
1279#define LEFT r[0]
1280#define RIGHT r[1]
1281#define PREV r[2]
1282#define MASK(b) (1 << (b))
1283#define MASK2(a,b) (MASK(a) | MASK(b))
1284#define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1285#define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1286#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1287#define STATE_TEST(b) (state & MASK(b))
1288#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1289 ((state &= ~MASK(b)), 0))
1290#define SWITCH_STATE \
1291 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1292
1293 /*
1294 * Set up a bunch of variables to make the tests simpler.
1295 */
1296 error = 0;
1297 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001298 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1299 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 xfs_bmbt_get_all(ep, &PREV);
1301 newext = new->br_state;
1302 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1303 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1304 ASSERT(PREV.br_state == oldext);
1305 new_endoff = new->br_startoff + new->br_blockcount;
1306 ASSERT(PREV.br_startoff <= new->br_startoff);
1307 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1308 /*
1309 * Set flags determining what part of the previous oldext allocation
1310 * extent is being replaced by a newext allocation.
1311 */
1312 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
1313 STATE_SET(RIGHT_FILLING,
1314 PREV.br_startoff + PREV.br_blockcount == new_endoff);
1315 /*
1316 * Check and set flags if this segment has a left neighbor.
1317 * Don't set contiguous if the combined extent would be too large.
1318 */
1319 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001320 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
1322 }
1323 STATE_SET(LEFT_CONTIG,
1324 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1325 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1326 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1327 LEFT.br_state == newext &&
1328 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1329 /*
1330 * Check and set flags if this segment has a right neighbor.
1331 * Don't set contiguous if the combined extent would be too large.
1332 * Also check for all-three-contiguous being too large.
1333 */
1334 if (STATE_SET_TEST(RIGHT_VALID,
1335 idx <
1336 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001337 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
1339 }
1340 STATE_SET(RIGHT_CONTIG,
1341 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1342 new_endoff == RIGHT.br_startoff &&
1343 new->br_startblock + new->br_blockcount ==
1344 RIGHT.br_startblock &&
1345 newext == RIGHT.br_state &&
1346 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1347 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
1348 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
1349 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1350 <= MAXEXTLEN));
1351 /*
1352 * Switch out based on the FILLING and CONTIG state bits.
1353 */
1354 switch (SWITCH_STATE) {
1355
1356 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1357 /*
1358 * Setting all of a previous oldext extent to newext.
1359 * The left and right neighbors are both contiguous with new.
1360 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001361 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001363 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 LEFT.br_blockcount + PREV.br_blockcount +
1365 RIGHT.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001366 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001368 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001369 xfs_iext_remove(ifp, idx, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 ip->i_df.if_lastex = idx - 1;
1371 ip->i_d.di_nextents -= 2;
1372 if (cur == NULL)
1373 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1374 else {
1375 rval = XFS_ILOG_CORE;
1376 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1377 RIGHT.br_startblock,
1378 RIGHT.br_blockcount, &i)))
1379 goto done;
1380 ASSERT(i == 1);
1381 if ((error = xfs_bmbt_delete(cur, &i)))
1382 goto done;
1383 ASSERT(i == 1);
1384 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1385 goto done;
1386 ASSERT(i == 1);
1387 if ((error = xfs_bmbt_delete(cur, &i)))
1388 goto done;
1389 ASSERT(i == 1);
1390 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1391 goto done;
1392 ASSERT(i == 1);
1393 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1394 LEFT.br_startblock,
1395 LEFT.br_blockcount + PREV.br_blockcount +
1396 RIGHT.br_blockcount, LEFT.br_state)))
1397 goto done;
1398 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001399 /* DELTA: Three in-core extents are replaced by one. */
1400 temp = LEFT.br_startoff;
1401 temp2 = LEFT.br_blockcount +
1402 PREV.br_blockcount +
1403 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 break;
1405
1406 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
1407 /*
1408 * Setting all of a previous oldext extent to newext.
1409 * The left neighbor is contiguous, the right is not.
1410 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001411 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001413 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 LEFT.br_blockcount + PREV.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001415 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 XFS_DATA_FORK);
1417 ip->i_df.if_lastex = idx - 1;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001418 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001419 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 ip->i_d.di_nextents--;
1421 if (cur == NULL)
1422 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1423 else {
1424 rval = XFS_ILOG_CORE;
1425 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1426 PREV.br_startblock, PREV.br_blockcount,
1427 &i)))
1428 goto done;
1429 ASSERT(i == 1);
1430 if ((error = xfs_bmbt_delete(cur, &i)))
1431 goto done;
1432 ASSERT(i == 1);
1433 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1434 goto done;
1435 ASSERT(i == 1);
1436 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1437 LEFT.br_startblock,
1438 LEFT.br_blockcount + PREV.br_blockcount,
1439 LEFT.br_state)))
1440 goto done;
1441 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001442 /* DELTA: Two in-core extents are replaced by one. */
1443 temp = LEFT.br_startoff;
1444 temp2 = LEFT.br_blockcount +
1445 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 break;
1447
1448 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
1449 /*
1450 * Setting all of a previous oldext extent to newext.
1451 * The right neighbor is contiguous, the left is not.
1452 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001453 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 XFS_DATA_FORK);
1455 xfs_bmbt_set_blockcount(ep,
1456 PREV.br_blockcount + RIGHT.br_blockcount);
1457 xfs_bmbt_set_state(ep, newext);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001458 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 XFS_DATA_FORK);
1460 ip->i_df.if_lastex = idx;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001461 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001462 xfs_iext_remove(ifp, idx + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 ip->i_d.di_nextents--;
1464 if (cur == NULL)
1465 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1466 else {
1467 rval = XFS_ILOG_CORE;
1468 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1469 RIGHT.br_startblock,
1470 RIGHT.br_blockcount, &i)))
1471 goto done;
1472 ASSERT(i == 1);
1473 if ((error = xfs_bmbt_delete(cur, &i)))
1474 goto done;
1475 ASSERT(i == 1);
1476 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1477 goto done;
1478 ASSERT(i == 1);
1479 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1480 new->br_startblock,
1481 new->br_blockcount + RIGHT.br_blockcount,
1482 newext)))
1483 goto done;
1484 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001485 /* DELTA: Two in-core extents are replaced by one. */
1486 temp = PREV.br_startoff;
1487 temp2 = PREV.br_blockcount +
1488 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 break;
1490
1491 case MASK2(LEFT_FILLING, RIGHT_FILLING):
1492 /*
1493 * Setting all of a previous oldext extent to newext.
1494 * Neither the left nor right neighbors are contiguous with
1495 * the new one.
1496 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001497 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 XFS_DATA_FORK);
1499 xfs_bmbt_set_state(ep, newext);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001500 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 XFS_DATA_FORK);
1502 ip->i_df.if_lastex = idx;
1503 if (cur == NULL)
1504 rval = XFS_ILOG_DEXT;
1505 else {
1506 rval = 0;
1507 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1508 new->br_startblock, new->br_blockcount,
1509 &i)))
1510 goto done;
1511 ASSERT(i == 1);
1512 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1513 new->br_startblock, new->br_blockcount,
1514 newext)))
1515 goto done;
1516 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001517 /* DELTA: The in-core extent described by new changed type. */
1518 temp = new->br_startoff;
1519 temp2 = new->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 break;
1521
1522 case MASK2(LEFT_FILLING, LEFT_CONTIG):
1523 /*
1524 * Setting the first part of a previous oldext extent to newext.
1525 * The left neighbor is contiguous.
1526 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001527 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001529 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 LEFT.br_blockcount + new->br_blockcount);
1531 xfs_bmbt_set_startoff(ep,
1532 PREV.br_startoff + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001533 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001535 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 XFS_DATA_FORK);
1537 xfs_bmbt_set_startblock(ep,
1538 new->br_startblock + new->br_blockcount);
1539 xfs_bmbt_set_blockcount(ep,
1540 PREV.br_blockcount - new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001541 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 XFS_DATA_FORK);
1543 ip->i_df.if_lastex = idx - 1;
1544 if (cur == NULL)
1545 rval = XFS_ILOG_DEXT;
1546 else {
1547 rval = 0;
1548 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1549 PREV.br_startblock, PREV.br_blockcount,
1550 &i)))
1551 goto done;
1552 ASSERT(i == 1);
1553 if ((error = xfs_bmbt_update(cur,
1554 PREV.br_startoff + new->br_blockcount,
1555 PREV.br_startblock + new->br_blockcount,
1556 PREV.br_blockcount - new->br_blockcount,
1557 oldext)))
1558 goto done;
1559 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1560 goto done;
1561 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1562 LEFT.br_startblock,
1563 LEFT.br_blockcount + new->br_blockcount,
1564 LEFT.br_state))
1565 goto done;
1566 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001567 /* DELTA: The boundary between two in-core extents moved. */
1568 temp = LEFT.br_startoff;
1569 temp2 = LEFT.br_blockcount +
1570 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 break;
1572
1573 case MASK(LEFT_FILLING):
1574 /*
1575 * Setting the first part of a previous oldext extent to newext.
1576 * The left neighbor is not contiguous.
1577 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001578 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1580 xfs_bmbt_set_startoff(ep, new_endoff);
1581 xfs_bmbt_set_blockcount(ep,
1582 PREV.br_blockcount - new->br_blockcount);
1583 xfs_bmbt_set_startblock(ep,
1584 new->br_startblock + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001585 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1586 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001588 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 ip->i_df.if_lastex = idx;
1590 ip->i_d.di_nextents++;
1591 if (cur == NULL)
1592 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1593 else {
1594 rval = XFS_ILOG_CORE;
1595 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1596 PREV.br_startblock, PREV.br_blockcount,
1597 &i)))
1598 goto done;
1599 ASSERT(i == 1);
1600 if ((error = xfs_bmbt_update(cur,
1601 PREV.br_startoff + new->br_blockcount,
1602 PREV.br_startblock + new->br_blockcount,
1603 PREV.br_blockcount - new->br_blockcount,
1604 oldext)))
1605 goto done;
1606 cur->bc_rec.b = *new;
1607 if ((error = xfs_bmbt_insert(cur, &i)))
1608 goto done;
1609 ASSERT(i == 1);
1610 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001611 /* DELTA: One in-core extent is split in two. */
1612 temp = PREV.br_startoff;
1613 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 break;
1615
1616 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1617 /*
1618 * Setting the last part of a previous oldext extent to newext.
1619 * The right neighbor is contiguous with the new allocation.
1620 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001621 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001623 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 XFS_DATA_FORK);
1625 xfs_bmbt_set_blockcount(ep,
1626 PREV.br_blockcount - new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001627 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001629 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1630 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 new->br_blockcount + RIGHT.br_blockcount, newext);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001632 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 XFS_DATA_FORK);
1634 ip->i_df.if_lastex = idx + 1;
1635 if (cur == NULL)
1636 rval = XFS_ILOG_DEXT;
1637 else {
1638 rval = 0;
1639 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1640 PREV.br_startblock,
1641 PREV.br_blockcount, &i)))
1642 goto done;
1643 ASSERT(i == 1);
1644 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1645 PREV.br_startblock,
1646 PREV.br_blockcount - new->br_blockcount,
1647 oldext)))
1648 goto done;
1649 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1650 goto done;
1651 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1652 new->br_startblock,
1653 new->br_blockcount + RIGHT.br_blockcount,
1654 newext)))
1655 goto done;
1656 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001657 /* DELTA: The boundary between two in-core extents moved. */
1658 temp = PREV.br_startoff;
1659 temp2 = PREV.br_blockcount +
1660 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 break;
1662
1663 case MASK(RIGHT_FILLING):
1664 /*
1665 * Setting the last part of a previous oldext extent to newext.
1666 * The right neighbor is not contiguous.
1667 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001668 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 xfs_bmbt_set_blockcount(ep,
1670 PREV.br_blockcount - new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001671 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1672 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1673 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001674 xfs_iext_insert(ifp, idx + 1, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 ip->i_df.if_lastex = idx + 1;
1676 ip->i_d.di_nextents++;
1677 if (cur == NULL)
1678 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1679 else {
1680 rval = XFS_ILOG_CORE;
1681 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1682 PREV.br_startblock, PREV.br_blockcount,
1683 &i)))
1684 goto done;
1685 ASSERT(i == 1);
1686 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1687 PREV.br_startblock,
1688 PREV.br_blockcount - new->br_blockcount,
1689 oldext)))
1690 goto done;
1691 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1692 new->br_startblock, new->br_blockcount,
1693 &i)))
1694 goto done;
1695 ASSERT(i == 0);
1696 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1697 if ((error = xfs_bmbt_insert(cur, &i)))
1698 goto done;
1699 ASSERT(i == 1);
1700 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001701 /* DELTA: One in-core extent is split in two. */
1702 temp = PREV.br_startoff;
1703 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 break;
1705
1706 case 0:
1707 /*
1708 * Setting the middle part of a previous oldext extent to
1709 * newext. Contiguity is impossible here.
1710 * One extent becomes three extents.
1711 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001712 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 xfs_bmbt_set_blockcount(ep,
1714 new->br_startoff - PREV.br_startoff);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001715 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 r[0] = *new;
1717 r[1].br_startoff = new_endoff;
1718 r[1].br_blockcount =
1719 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1720 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1721 r[1].br_state = oldext;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001722 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001724 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 ip->i_df.if_lastex = idx + 1;
1726 ip->i_d.di_nextents += 2;
1727 if (cur == NULL)
1728 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1729 else {
1730 rval = XFS_ILOG_CORE;
1731 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1732 PREV.br_startblock, PREV.br_blockcount,
1733 &i)))
1734 goto done;
1735 ASSERT(i == 1);
1736 /* new right extent - oldext */
1737 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1738 r[1].br_startblock, r[1].br_blockcount,
1739 r[1].br_state)))
1740 goto done;
1741 /* new left extent - oldext */
1742 PREV.br_blockcount =
1743 new->br_startoff - PREV.br_startoff;
1744 cur->bc_rec.b = PREV;
1745 if ((error = xfs_bmbt_insert(cur, &i)))
1746 goto done;
1747 ASSERT(i == 1);
1748 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1749 goto done;
1750 ASSERT(i == 1);
1751 /* new middle extent - newext */
1752 cur->bc_rec.b = *new;
1753 if ((error = xfs_bmbt_insert(cur, &i)))
1754 goto done;
1755 ASSERT(i == 1);
1756 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001757 /* DELTA: One in-core extent is split in three. */
1758 temp = PREV.br_startoff;
1759 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 break;
1761
1762 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1763 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1764 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1765 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1766 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1767 case MASK(LEFT_CONTIG):
1768 case MASK(RIGHT_CONTIG):
1769 /*
1770 * These cases are all impossible.
1771 */
1772 ASSERT(0);
1773 }
1774 *curp = cur;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001775 if (delta) {
1776 temp2 += temp;
1777 if (delta->xed_startoff > temp)
1778 delta->xed_startoff = temp;
1779 if (delta->xed_blockcount < temp2)
1780 delta->xed_blockcount = temp2;
1781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782done:
1783 *logflagsp = rval;
1784 return error;
1785#undef LEFT
1786#undef RIGHT
1787#undef PREV
1788#undef MASK
1789#undef MASK2
1790#undef MASK3
1791#undef MASK4
1792#undef STATE_SET
1793#undef STATE_TEST
1794#undef STATE_SET_TEST
1795#undef SWITCH_STATE
1796}
1797
1798/*
1799 * Called by xfs_bmap_add_extent to handle cases converting a hole
1800 * to a delayed allocation.
1801 */
1802/*ARGSUSED*/
1803STATIC int /* error */
1804xfs_bmap_add_extent_hole_delay(
1805 xfs_inode_t *ip, /* incore inode pointer */
1806 xfs_extnum_t idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001807 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001809 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 int rsvd) /* OK to allocate reserved blocks */
1811{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001812 xfs_bmbt_rec_host_t *ep; /* extent record for idx */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001813 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1815 xfs_filblks_t newlen=0; /* new indirect size */
1816 xfs_filblks_t oldlen=0; /* old indirect size */
1817 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1818 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001819 xfs_filblks_t temp=0; /* temp for indirect calculations */
1820 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 enum { /* bit number definitions for state */
1822 LEFT_CONTIG, RIGHT_CONTIG,
1823 LEFT_DELAY, RIGHT_DELAY,
1824 LEFT_VALID, RIGHT_VALID
1825 };
1826
1827#define MASK(b) (1 << (b))
1828#define MASK2(a,b) (MASK(a) | MASK(b))
1829#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1830#define STATE_TEST(b) (state & MASK(b))
1831#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1832 ((state &= ~MASK(b)), 0))
1833#define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1834
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001835 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1836 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 state = 0;
1838 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1839 /*
1840 * Check and set flags if this segment has a left neighbor
1841 */
1842 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001843 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1845 }
1846 /*
1847 * Check and set flags if the current (right) segment exists.
1848 * If it doesn't exist, we're converting the hole at end-of-file.
1849 */
1850 if (STATE_SET_TEST(RIGHT_VALID,
1851 idx <
1852 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1853 xfs_bmbt_get_all(ep, &right);
1854 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1855 }
1856 /*
1857 * Set contiguity flags on the left and right neighbors.
1858 * Don't let extents get too large, even if the pieces are contiguous.
1859 */
1860 STATE_SET(LEFT_CONTIG,
1861 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1862 left.br_startoff + left.br_blockcount == new->br_startoff &&
1863 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1864 STATE_SET(RIGHT_CONTIG,
1865 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1866 new->br_startoff + new->br_blockcount == right.br_startoff &&
1867 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1868 (!STATE_TEST(LEFT_CONTIG) ||
1869 (left.br_blockcount + new->br_blockcount +
1870 right.br_blockcount <= MAXEXTLEN)));
1871 /*
1872 * Switch out based on the contiguity flags.
1873 */
1874 switch (SWITCH_STATE) {
1875
1876 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1877 /*
1878 * New allocation is contiguous with delayed allocations
1879 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001880 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 */
1882 temp = left.br_blockcount + new->br_blockcount +
1883 right.br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001884 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001886 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 oldlen = STARTBLOCKVAL(left.br_startblock) +
1888 STARTBLOCKVAL(new->br_startblock) +
1889 STARTBLOCKVAL(right.br_startblock);
1890 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001891 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1892 NULLSTARTBLOCK((int)newlen));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001893 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001895 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001896 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 ip->i_df.if_lastex = idx - 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001898 /* DELTA: Two in-core extents were replaced by one. */
1899 temp2 = temp;
1900 temp = left.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 break;
1902
1903 case MASK(LEFT_CONTIG):
1904 /*
1905 * New allocation is contiguous with a delayed allocation
1906 * on the left.
1907 * Merge the new allocation with the left neighbor.
1908 */
1909 temp = left.br_blockcount + new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001910 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001912 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 oldlen = STARTBLOCKVAL(left.br_startblock) +
1914 STARTBLOCKVAL(new->br_startblock);
1915 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),
1917 NULLSTARTBLOCK((int)newlen));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001918 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 XFS_DATA_FORK);
1920 ip->i_df.if_lastex = idx - 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001921 /* DELTA: One in-core extent grew into a hole. */
1922 temp2 = temp;
1923 temp = left.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 break;
1925
1926 case MASK(RIGHT_CONTIG):
1927 /*
1928 * New allocation is contiguous with a delayed allocation
1929 * on the right.
1930 * Merge the new allocation with the right neighbor.
1931 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001932 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 temp = new->br_blockcount + right.br_blockcount;
1934 oldlen = STARTBLOCKVAL(new->br_startblock) +
1935 STARTBLOCKVAL(right.br_startblock);
1936 newlen = xfs_bmap_worst_indlen(ip, temp);
1937 xfs_bmbt_set_allf(ep, new->br_startoff,
1938 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001939 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 ip->i_df.if_lastex = idx;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001941 /* DELTA: One in-core extent grew into a hole. */
1942 temp2 = temp;
1943 temp = new->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 break;
1945
1946 case 0:
1947 /*
1948 * New allocation is not contiguous with another
1949 * delayed allocation.
1950 * Insert a new entry.
1951 */
1952 oldlen = newlen = 0;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001953 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001955 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 ip->i_df.if_lastex = idx;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001957 /* DELTA: A new in-core extent was added in a hole. */
1958 temp2 = new->br_blockcount;
1959 temp = new->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 break;
1961 }
1962 if (oldlen != newlen) {
1963 ASSERT(oldlen > newlen);
1964 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11001965 (int64_t)(oldlen - newlen), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 /*
1967 * Nothing to do for disk quota accounting here.
1968 */
1969 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001970 if (delta) {
1971 temp2 += temp;
1972 if (delta->xed_startoff > temp)
1973 delta->xed_startoff = temp;
1974 if (delta->xed_blockcount < temp2)
1975 delta->xed_blockcount = temp2;
1976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 *logflagsp = 0;
1978 return 0;
1979#undef MASK
1980#undef MASK2
1981#undef STATE_SET
1982#undef STATE_TEST
1983#undef STATE_SET_TEST
1984#undef SWITCH_STATE
1985}
1986
1987/*
1988 * Called by xfs_bmap_add_extent to handle cases converting a hole
1989 * to a real allocation.
1990 */
1991STATIC int /* error */
1992xfs_bmap_add_extent_hole_real(
1993 xfs_inode_t *ip, /* incore inode pointer */
1994 xfs_extnum_t idx, /* extent number to update/insert */
1995 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001996 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001998 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 int whichfork) /* data or attr fork */
2000{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002001 xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 int i; /* temp state */
2004 xfs_ifork_t *ifp; /* inode fork pointer */
2005 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2006 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002007 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002009 xfs_filblks_t temp=0;
2010 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 enum { /* bit number definitions for state */
2012 LEFT_CONTIG, RIGHT_CONTIG,
2013 LEFT_DELAY, RIGHT_DELAY,
2014 LEFT_VALID, RIGHT_VALID
2015 };
2016
2017#define MASK(b) (1 << (b))
2018#define MASK2(a,b) (MASK(a) | MASK(b))
2019#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
2020#define STATE_TEST(b) (state & MASK(b))
2021#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
2022 ((state &= ~MASK(b)), 0))
2023#define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
2024
2025 ifp = XFS_IFORK_PTR(ip, whichfork);
2026 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002027 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 state = 0;
2029 /*
2030 * Check and set flags if this segment has a left neighbor.
2031 */
2032 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002033 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
2035 }
2036 /*
2037 * Check and set flags if this segment has a current value.
2038 * Not true if we're inserting into the "hole" at eof.
2039 */
2040 if (STATE_SET_TEST(RIGHT_VALID,
2041 idx <
2042 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
2043 xfs_bmbt_get_all(ep, &right);
2044 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
2045 }
2046 /*
2047 * We're inserting a real allocation between "left" and "right".
2048 * Set the contiguity flags. Don't let extents get too large.
2049 */
2050 STATE_SET(LEFT_CONTIG,
2051 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
2052 left.br_startoff + left.br_blockcount == new->br_startoff &&
2053 left.br_startblock + left.br_blockcount == new->br_startblock &&
2054 left.br_state == new->br_state &&
2055 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
2056 STATE_SET(RIGHT_CONTIG,
2057 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
2058 new->br_startoff + new->br_blockcount == right.br_startoff &&
2059 new->br_startblock + new->br_blockcount ==
2060 right.br_startblock &&
2061 new->br_state == right.br_state &&
2062 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2063 (!STATE_TEST(LEFT_CONTIG) ||
2064 left.br_blockcount + new->br_blockcount +
2065 right.br_blockcount <= MAXEXTLEN));
2066
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002067 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 /*
2069 * Select which case we're in here, and implement it.
2070 */
2071 switch (SWITCH_STATE) {
2072
2073 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
2074 /*
2075 * New allocation is contiguous with real allocations on the
2076 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002077 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002079 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002081 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 left.br_blockcount + new->br_blockcount +
2083 right.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002084 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 whichfork);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002086 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002087 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 ifp->if_lastex = idx - 1;
2089 XFS_IFORK_NEXT_SET(ip, whichfork,
2090 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2091 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002092 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2093 } else {
2094 rval = XFS_ILOG_CORE;
2095 if ((error = xfs_bmbt_lookup_eq(cur,
2096 right.br_startoff,
2097 right.br_startblock,
2098 right.br_blockcount, &i)))
2099 goto done;
2100 ASSERT(i == 1);
2101 if ((error = xfs_bmbt_delete(cur, &i)))
2102 goto done;
2103 ASSERT(i == 1);
2104 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2105 goto done;
2106 ASSERT(i == 1);
2107 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2108 left.br_startblock,
2109 left.br_blockcount +
2110 new->br_blockcount +
2111 right.br_blockcount,
2112 left.br_state)))
2113 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002115 /* DELTA: Two in-core extents were replaced by one. */
2116 temp = left.br_startoff;
2117 temp2 = left.br_blockcount +
2118 new->br_blockcount +
2119 right.br_blockcount;
2120 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 case MASK(LEFT_CONTIG):
2123 /*
2124 * New allocation is contiguous with a real allocation
2125 * on the left.
2126 * Merge the new allocation with the left neighbor.
2127 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002128 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002129 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 left.br_blockcount + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002131 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 ifp->if_lastex = idx - 1;
2133 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002134 rval = XFS_ILOG_FEXT(whichfork);
2135 } else {
2136 rval = 0;
2137 if ((error = xfs_bmbt_lookup_eq(cur,
2138 left.br_startoff,
2139 left.br_startblock,
2140 left.br_blockcount, &i)))
2141 goto done;
2142 ASSERT(i == 1);
2143 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2144 left.br_startblock,
2145 left.br_blockcount +
2146 new->br_blockcount,
2147 left.br_state)))
2148 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002150 /* DELTA: One in-core extent grew. */
2151 temp = left.br_startoff;
2152 temp2 = left.br_blockcount +
2153 new->br_blockcount;
2154 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 case MASK(RIGHT_CONTIG):
2157 /*
2158 * New allocation is contiguous with a real allocation
2159 * on the right.
2160 * Merge the new allocation with the right neighbor.
2161 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002162 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2164 new->br_blockcount + right.br_blockcount,
2165 right.br_state);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002166 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 ifp->if_lastex = idx;
2168 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002169 rval = XFS_ILOG_FEXT(whichfork);
2170 } else {
2171 rval = 0;
2172 if ((error = xfs_bmbt_lookup_eq(cur,
2173 right.br_startoff,
2174 right.br_startblock,
2175 right.br_blockcount, &i)))
2176 goto done;
2177 ASSERT(i == 1);
2178 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2179 new->br_startblock,
2180 new->br_blockcount +
2181 right.br_blockcount,
2182 right.br_state)))
2183 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002185 /* DELTA: One in-core extent grew. */
2186 temp = new->br_startoff;
2187 temp2 = new->br_blockcount +
2188 right.br_blockcount;
2189 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 case 0:
2192 /*
2193 * New allocation is not contiguous with another
2194 * real allocation.
2195 * Insert a new entry.
2196 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002197 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002198 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 ifp->if_lastex = idx;
2200 XFS_IFORK_NEXT_SET(ip, whichfork,
2201 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2202 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002203 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2204 } else {
2205 rval = XFS_ILOG_CORE;
2206 if ((error = xfs_bmbt_lookup_eq(cur,
2207 new->br_startoff,
2208 new->br_startblock,
2209 new->br_blockcount, &i)))
2210 goto done;
2211 ASSERT(i == 0);
2212 cur->bc_rec.b.br_state = new->br_state;
2213 if ((error = xfs_bmbt_insert(cur, &i)))
2214 goto done;
2215 ASSERT(i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002217 /* DELTA: A new extent was added in a hole. */
2218 temp = new->br_startoff;
2219 temp2 = new->br_blockcount;
2220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002222 if (delta) {
2223 temp2 += temp;
2224 if (delta->xed_startoff > temp)
2225 delta->xed_startoff = temp;
2226 if (delta->xed_blockcount < temp2)
2227 delta->xed_blockcount = temp2;
2228 }
2229done:
2230 *logflagsp = rval;
2231 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232#undef MASK
2233#undef MASK2
2234#undef STATE_SET
2235#undef STATE_TEST
2236#undef STATE_SET_TEST
2237#undef SWITCH_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Nathan Scottdd9f4382006-01-11 15:28:28 +11002240/*
2241 * Adjust the size of the new extent based on di_extsize and rt extsize.
2242 */
2243STATIC int
2244xfs_bmap_extsize_align(
2245 xfs_mount_t *mp,
2246 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2247 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2248 xfs_extlen_t extsz, /* align to this extent size */
2249 int rt, /* is this a realtime inode? */
2250 int eof, /* is extent at end-of-file? */
2251 int delay, /* creating delalloc extent? */
2252 int convert, /* overwriting unwritten extent? */
2253 xfs_fileoff_t *offp, /* in/out: aligned offset */
2254 xfs_extlen_t *lenp) /* in/out: aligned length */
2255{
2256 xfs_fileoff_t orig_off; /* original offset */
2257 xfs_extlen_t orig_alen; /* original length */
2258 xfs_fileoff_t orig_end; /* original off+len */
2259 xfs_fileoff_t nexto; /* next file offset */
2260 xfs_fileoff_t prevo; /* previous file offset */
2261 xfs_fileoff_t align_off; /* temp for offset */
2262 xfs_extlen_t align_alen; /* temp for length */
2263 xfs_extlen_t temp; /* temp for calculations */
2264
2265 if (convert)
2266 return 0;
2267
2268 orig_off = align_off = *offp;
2269 orig_alen = align_alen = *lenp;
2270 orig_end = orig_off + orig_alen;
2271
2272 /*
2273 * If this request overlaps an existing extent, then don't
2274 * attempt to perform any additional alignment.
2275 */
2276 if (!delay && !eof &&
2277 (orig_off >= gotp->br_startoff) &&
2278 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2279 return 0;
2280 }
2281
2282 /*
2283 * If the file offset is unaligned vs. the extent size
2284 * we need to align it. This will be possible unless
2285 * the file was previously written with a kernel that didn't
2286 * perform this alignment, or if a truncate shot us in the
2287 * foot.
2288 */
2289 temp = do_mod(orig_off, extsz);
2290 if (temp) {
2291 align_alen += temp;
2292 align_off -= temp;
2293 }
2294 /*
2295 * Same adjustment for the end of the requested area.
2296 */
2297 if ((temp = (align_alen % extsz))) {
2298 align_alen += extsz - temp;
2299 }
2300 /*
2301 * If the previous block overlaps with this proposed allocation
2302 * then move the start forward without adjusting the length.
2303 */
2304 if (prevp->br_startoff != NULLFILEOFF) {
2305 if (prevp->br_startblock == HOLESTARTBLOCK)
2306 prevo = prevp->br_startoff;
2307 else
2308 prevo = prevp->br_startoff + prevp->br_blockcount;
2309 } else
2310 prevo = 0;
2311 if (align_off != orig_off && align_off < prevo)
2312 align_off = prevo;
2313 /*
2314 * If the next block overlaps with this proposed allocation
2315 * then move the start back without adjusting the length,
2316 * but not before offset 0.
2317 * This may of course make the start overlap previous block,
2318 * and if we hit the offset 0 limit then the next block
2319 * can still overlap too.
2320 */
2321 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2322 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2323 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2324 nexto = gotp->br_startoff + gotp->br_blockcount;
2325 else
2326 nexto = gotp->br_startoff;
2327 } else
2328 nexto = NULLFILEOFF;
2329 if (!eof &&
2330 align_off + align_alen != orig_end &&
2331 align_off + align_alen > nexto)
2332 align_off = nexto > align_alen ? nexto - align_alen : 0;
2333 /*
2334 * If we're now overlapping the next or previous extent that
2335 * means we can't fit an extsz piece in this hole. Just move
2336 * the start forward to the first valid spot and set
2337 * the length so we hit the end.
2338 */
2339 if (align_off != orig_off && align_off < prevo)
2340 align_off = prevo;
2341 if (align_off + align_alen != orig_end &&
2342 align_off + align_alen > nexto &&
2343 nexto != NULLFILEOFF) {
2344 ASSERT(nexto > prevo);
2345 align_alen = nexto - align_off;
2346 }
2347
2348 /*
2349 * If realtime, and the result isn't a multiple of the realtime
2350 * extent size we need to remove blocks until it is.
2351 */
2352 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2353 /*
2354 * We're not covering the original request, or
2355 * we won't be able to once we fix the length.
2356 */
2357 if (orig_off < align_off ||
2358 orig_end > align_off + align_alen ||
2359 align_alen - temp < orig_alen)
2360 return XFS_ERROR(EINVAL);
2361 /*
2362 * Try to fix it by moving the start up.
2363 */
2364 if (align_off + temp <= orig_off) {
2365 align_alen -= temp;
2366 align_off += temp;
2367 }
2368 /*
2369 * Try to fix it by moving the end in.
2370 */
2371 else if (align_off + align_alen - temp >= orig_end)
2372 align_alen -= temp;
2373 /*
2374 * Set the start to the minimum then trim the length.
2375 */
2376 else {
2377 align_alen -= orig_off - align_off;
2378 align_off = orig_off;
2379 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2380 }
2381 /*
2382 * Result doesn't cover the request, fail it.
2383 */
2384 if (orig_off < align_off || orig_end > align_off + align_alen)
2385 return XFS_ERROR(EINVAL);
2386 } else {
2387 ASSERT(orig_off >= align_off);
2388 ASSERT(orig_end <= align_off + align_alen);
2389 }
2390
2391#ifdef DEBUG
2392 if (!eof && gotp->br_startoff != NULLFILEOFF)
2393 ASSERT(align_off + align_alen <= gotp->br_startoff);
2394 if (prevp->br_startoff != NULLFILEOFF)
2395 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2396#endif
2397
2398 *lenp = align_alen;
2399 *offp = align_off;
2400 return 0;
2401}
2402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403#define XFS_ALLOC_GAP_UNITS 4
2404
David Chinnerc2b1cba2008-04-10 12:21:40 +10002405STATIC void
Nathan Scotta365bdd2006-03-14 13:34:16 +11002406xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2408{
2409 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2411 xfs_mount_t *mp; /* mount point structure */
2412 int nullfb; /* true if ap->firstblock isn't set */
2413 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
2415#define ISVALID(x,y) \
2416 (rt ? \
2417 (x) < mp->m_sb.sb_rblocks : \
2418 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2419 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2420 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 mp = ap->ip->i_mount;
2423 nullfb = ap->firstblock == NULLFSBLOCK;
2424 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2425 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 /*
2427 * If allocating at eof, and there's a previous real block,
2428 * try to use it's last block as our starting point.
2429 */
2430 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2431 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2432 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2433 ap->prevp->br_startblock)) {
2434 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2435 /*
2436 * Adjust for the gap between prevp and us.
2437 */
2438 adjust = ap->off -
2439 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2440 if (adjust &&
2441 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2442 ap->rval += adjust;
2443 }
2444 /*
2445 * If not at eof, then compare the two neighbor blocks.
2446 * Figure out whether either one gives us a good starting point,
2447 * and pick the better one.
2448 */
2449 else if (!ap->eof) {
2450 xfs_fsblock_t gotbno; /* right side block number */
2451 xfs_fsblock_t gotdiff=0; /* right side difference */
2452 xfs_fsblock_t prevbno; /* left side block number */
2453 xfs_fsblock_t prevdiff=0; /* left side difference */
2454
2455 /*
2456 * If there's a previous (left) block, select a requested
2457 * start block based on it.
2458 */
2459 if (ap->prevp->br_startoff != NULLFILEOFF &&
2460 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2461 (prevbno = ap->prevp->br_startblock +
2462 ap->prevp->br_blockcount) &&
2463 ISVALID(prevbno, ap->prevp->br_startblock)) {
2464 /*
2465 * Calculate gap to end of previous block.
2466 */
2467 adjust = prevdiff = ap->off -
2468 (ap->prevp->br_startoff +
2469 ap->prevp->br_blockcount);
2470 /*
2471 * Figure the startblock based on the previous block's
2472 * end and the gap size.
2473 * Heuristic!
2474 * If the gap is large relative to the piece we're
2475 * allocating, or using it gives us an invalid block
2476 * number, then just use the end of the previous block.
2477 */
2478 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2479 ISVALID(prevbno + prevdiff,
2480 ap->prevp->br_startblock))
2481 prevbno += adjust;
2482 else
2483 prevdiff += adjust;
2484 /*
2485 * If the firstblock forbids it, can't use it,
2486 * must use default.
2487 */
2488 if (!rt && !nullfb &&
2489 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2490 prevbno = NULLFSBLOCK;
2491 }
2492 /*
2493 * No previous block or can't follow it, just default.
2494 */
2495 else
2496 prevbno = NULLFSBLOCK;
2497 /*
2498 * If there's a following (right) block, select a requested
2499 * start block based on it.
2500 */
2501 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
2502 /*
2503 * Calculate gap to start of next block.
2504 */
2505 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2506 /*
2507 * Figure the startblock based on the next block's
2508 * start and the gap size.
2509 */
2510 gotbno = ap->gotp->br_startblock;
2511 /*
2512 * Heuristic!
2513 * If the gap is large relative to the piece we're
2514 * allocating, or using it gives us an invalid block
2515 * number, then just use the start of the next block
2516 * offset by our length.
2517 */
2518 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2519 ISVALID(gotbno - gotdiff, gotbno))
2520 gotbno -= adjust;
2521 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2522 gotbno -= ap->alen;
2523 gotdiff += adjust - ap->alen;
2524 } else
2525 gotdiff += adjust;
2526 /*
2527 * If the firstblock forbids it, can't use it,
2528 * must use default.
2529 */
2530 if (!rt && !nullfb &&
2531 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2532 gotbno = NULLFSBLOCK;
2533 }
2534 /*
2535 * No next block, just default.
2536 */
2537 else
2538 gotbno = NULLFSBLOCK;
2539 /*
2540 * If both valid, pick the better one, else the only good
2541 * one, else ap->rval is already set (to 0 or the inode block).
2542 */
2543 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2544 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2545 else if (prevbno != NULLFSBLOCK)
2546 ap->rval = prevbno;
2547 else if (gotbno != NULLFSBLOCK)
2548 ap->rval = gotbno;
2549 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002550#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11002551}
2552
2553STATIC int
2554xfs_bmap_rtalloc(
2555 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2556{
2557 xfs_alloctype_t atype = 0; /* type for allocation routines */
2558 int error; /* error return value */
2559 xfs_mount_t *mp; /* mount point structure */
2560 xfs_extlen_t prod = 0; /* product factor for allocators */
2561 xfs_extlen_t ralen = 0; /* realtime allocation length */
2562 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002563 xfs_rtblock_t rtb;
2564
2565 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002566 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002567 prod = align / mp->m_sb.sb_rextsize;
2568 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2569 align, 1, ap->eof, 0,
2570 ap->conv, &ap->off, &ap->alen);
2571 if (error)
2572 return error;
2573 ASSERT(ap->alen);
2574 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2575
2576 /*
2577 * If the offset & length are not perfectly aligned
2578 * then kill prod, it will just get us in trouble.
2579 */
2580 if (do_mod(ap->off, align) || ap->alen % align)
2581 prod = 1;
2582 /*
2583 * Set ralen to be the actual requested length in rtextents.
2584 */
2585 ralen = ap->alen / mp->m_sb.sb_rextsize;
2586 /*
2587 * If the old value was close enough to MAXEXTLEN that
2588 * we rounded up to it, cut it back so it's valid again.
2589 * Note that if it's a really large request (bigger than
2590 * MAXEXTLEN), we don't hear about that number, and can't
2591 * adjust the starting point to match it.
2592 */
2593 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2594 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2595 /*
2596 * If it's an allocation to an empty file at offset 0,
2597 * pick an extent that will space things out in the rt area.
2598 */
2599 if (ap->eof && ap->off == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10002600 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2601
Nathan Scotta365bdd2006-03-14 13:34:16 +11002602 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2603 if (error)
2604 return error;
2605 ap->rval = rtx * mp->m_sb.sb_rextsize;
2606 } else {
2607 ap->rval = 0;
2608 }
2609
2610 xfs_bmap_adjacent(ap);
2611
2612 /*
2613 * Realtime allocation, done through xfs_rtallocate_extent.
2614 */
2615 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2616 do_div(ap->rval, mp->m_sb.sb_rextsize);
2617 rtb = ap->rval;
2618 ap->alen = ralen;
2619 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2620 &ralen, atype, ap->wasdel, prod, &rtb)))
2621 return error;
2622 if (rtb == NULLFSBLOCK && prod > 1 &&
2623 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2624 ap->alen, &ralen, atype,
2625 ap->wasdel, 1, &rtb)))
2626 return error;
2627 ap->rval = rtb;
2628 if (ap->rval != NULLFSBLOCK) {
2629 ap->rval *= mp->m_sb.sb_rextsize;
2630 ralen *= mp->m_sb.sb_rextsize;
2631 ap->alen = ralen;
2632 ap->ip->i_d.di_nblocks += ralen;
2633 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2634 if (ap->wasdel)
2635 ap->ip->i_delayed_blks -= ralen;
2636 /*
2637 * Adjust the disk quota also. This was reserved
2638 * earlier.
2639 */
2640 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2641 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2642 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2643 } else {
2644 ap->alen = 0;
2645 }
2646 return 0;
2647}
2648
2649STATIC int
2650xfs_bmap_btalloc(
2651 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2652{
2653 xfs_mount_t *mp; /* mount point structure */
2654 xfs_alloctype_t atype = 0; /* type for allocation routines */
2655 xfs_extlen_t align; /* minimum allocation alignment */
2656 xfs_agnumber_t ag;
2657 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2658 xfs_agnumber_t startag;
2659 xfs_alloc_arg_t args;
2660 xfs_extlen_t blen;
2661 xfs_extlen_t delta;
2662 xfs_extlen_t longest;
2663 xfs_extlen_t need;
2664 xfs_extlen_t nextminlen = 0;
2665 xfs_perag_t *pag;
2666 int nullfb; /* true if ap->firstblock isn't set */
2667 int isaligned;
2668 int notinit;
2669 int tryagain;
2670 int error;
2671
2672 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002673 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002674 if (unlikely(align)) {
2675 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2676 align, 0, ap->eof, 0, ap->conv,
2677 &ap->off, &ap->alen);
2678 ASSERT(!error);
2679 ASSERT(ap->alen);
2680 }
2681 nullfb = ap->firstblock == NULLFSBLOCK;
2682 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10002683 if (nullfb) {
2684 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2685 ag = xfs_filestream_lookup_ag(ap->ip);
2686 ag = (ag != NULLAGNUMBER) ? ag : 0;
2687 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2688 } else {
2689 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2690 }
2691 } else
Nathan Scotta365bdd2006-03-14 13:34:16 +11002692 ap->rval = ap->firstblock;
2693
2694 xfs_bmap_adjacent(ap);
2695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 /*
2697 * If allowed, use ap->rval; otherwise must use firstblock since
2698 * it's in the right allocation group.
2699 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002700 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 ;
2702 else
2703 ap->rval = ap->firstblock;
2704 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 * Normal allocation, done through xfs_alloc_vextent.
2706 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002707 tryagain = isaligned = 0;
2708 args.tp = ap->tp;
2709 args.mp = mp;
2710 args.fsbno = ap->rval;
2711 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
Yingping Lud210a282006-06-09 14:55:18 +10002712 args.firstblock = ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002713 blen = 0;
2714 if (nullfb) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002715 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2716 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2717 else
2718 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002719 args.total = ap->total;
David Chinner2a82b8b2007-07-11 11:09:12 +10002720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10002722 * Search for an allocation group with a single extent
2723 * large enough for the request.
2724 *
2725 * If one isn't found, then adjust the minimum allocation
2726 * size to the largest space found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002728 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
David Chinner2a82b8b2007-07-11 11:09:12 +10002729 if (startag == NULLAGNUMBER)
2730 startag = ag = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002731 notinit = 0;
2732 down_read(&mp->m_peraglock);
2733 while (blen < ap->alen) {
2734 pag = &mp->m_perag[ag];
2735 if (!pag->pagf_init &&
2736 (error = xfs_alloc_pagf_init(mp, args.tp,
2737 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2738 up_read(&mp->m_peraglock);
2739 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002741 /*
2742 * See xfs_alloc_fix_freelist...
2743 */
2744 if (pag->pagf_init) {
2745 need = XFS_MIN_FREELIST_PAG(pag, mp);
2746 delta = need > pag->pagf_flcount ?
2747 need - pag->pagf_flcount : 0;
2748 longest = (pag->pagf_longest > delta) ?
2749 (pag->pagf_longest - delta) :
2750 (pag->pagf_flcount > 0 ||
2751 pag->pagf_longest > 0);
2752 if (blen < longest)
2753 blen = longest;
2754 } else
2755 notinit = 1;
David Chinner2a82b8b2007-07-11 11:09:12 +10002756
2757 if (xfs_inode_is_filestream(ap->ip)) {
2758 if (blen >= ap->alen)
2759 break;
2760
2761 if (ap->userdata) {
2762 /*
2763 * If startag is an invalid AG, we've
2764 * come here once before and
2765 * xfs_filestream_new_ag picked the
2766 * best currently available.
2767 *
2768 * Don't continue looping, since we
2769 * could loop forever.
2770 */
2771 if (startag == NULLAGNUMBER)
2772 break;
2773
2774 error = xfs_filestream_new_ag(ap, &ag);
2775 if (error) {
2776 up_read(&mp->m_peraglock);
2777 return error;
2778 }
2779
2780 /* loop again to set 'blen'*/
2781 startag = NULLAGNUMBER;
2782 continue;
2783 }
2784 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002785 if (++ag == mp->m_sb.sb_agcount)
2786 ag = 0;
2787 if (ag == startag)
2788 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002790 up_read(&mp->m_peraglock);
2791 /*
2792 * Since the above loop did a BUF_TRYLOCK, it is
2793 * possible that there is space for this request.
2794 */
2795 if (notinit || blen < ap->minlen)
2796 args.minlen = ap->minlen;
2797 /*
2798 * If the best seen length is less than the request
2799 * length, use the best as the minimum.
2800 */
2801 else if (blen < ap->alen)
2802 args.minlen = blen;
2803 /*
2804 * Otherwise we've seen an extent as big as alen,
2805 * use that as the minimum.
2806 */
2807 else
2808 args.minlen = ap->alen;
David Chinner2a82b8b2007-07-11 11:09:12 +10002809
2810 /*
2811 * set the failure fallback case to look in the selected
2812 * AG as the stream may have moved.
2813 */
2814 if (xfs_inode_is_filestream(ap->ip))
2815 ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002816 } else if (ap->low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002817 if (xfs_inode_is_filestream(ap->ip))
2818 args.type = XFS_ALLOCTYPE_FIRST_AG;
2819 else
2820 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002821 args.total = args.minlen = ap->minlen;
2822 } else {
2823 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2824 args.total = ap->total;
2825 args.minlen = ap->minlen;
2826 }
David Chinner957d0eb2007-06-18 16:50:37 +10002827 /* apply extent size hints if obtained earlier */
2828 if (unlikely(align)) {
2829 args.prod = align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002830 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2831 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11002832 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11002833 args.prod = 1;
2834 args.mod = 0;
2835 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002836 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002837 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2838 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2839 }
2840 /*
2841 * If we are not low on available data blocks, and the
2842 * underlying logical volume manager is a stripe, and
2843 * the file offset is zero then try to allocate data
2844 * blocks on stripe unit boundary.
2845 * NOTE: ap->aeof is only set if the allocation length
2846 * is >= the stripe unit and the allocation offset is
2847 * at the end of file.
2848 */
2849 if (!ap->low && ap->aeof) {
2850 if (!ap->off) {
2851 args.alignment = mp->m_dalign;
2852 atype = args.type;
2853 isaligned = 1;
2854 /*
2855 * Adjust for alignment
2856 */
2857 if (blen > args.alignment && blen <= ap->alen)
2858 args.minlen = blen - args.alignment;
2859 args.minalignslop = 0;
2860 } else {
2861 /*
2862 * First try an exact bno allocation.
2863 * If it fails then do a near or start bno
2864 * allocation with alignment turned on.
2865 */
2866 atype = args.type;
2867 tryagain = 1;
2868 args.type = XFS_ALLOCTYPE_THIS_BNO;
2869 args.alignment = 1;
2870 /*
2871 * Compute the minlen+alignment for the
2872 * next case. Set slop so that the value
2873 * of minlen+alignment+slop doesn't go up
2874 * between the calls.
2875 */
2876 if (blen > mp->m_dalign && blen <= ap->alen)
2877 nextminlen = blen - mp->m_dalign;
2878 else
2879 nextminlen = args.minlen;
2880 if (nextminlen + mp->m_dalign > args.minlen + 1)
2881 args.minalignslop =
2882 nextminlen + mp->m_dalign -
2883 args.minlen - 1;
2884 else
2885 args.minalignslop = 0;
2886 }
2887 } else {
2888 args.alignment = 1;
2889 args.minalignslop = 0;
2890 }
2891 args.minleft = ap->minleft;
2892 args.wasdel = ap->wasdel;
2893 args.isfl = 0;
2894 args.userdata = ap->userdata;
2895 if ((error = xfs_alloc_vextent(&args)))
2896 return error;
2897 if (tryagain && args.fsbno == NULLFSBLOCK) {
2898 /*
2899 * Exact allocation failed. Now try with alignment
2900 * turned on.
2901 */
2902 args.type = atype;
2903 args.fsbno = ap->rval;
2904 args.alignment = mp->m_dalign;
2905 args.minlen = nextminlen;
2906 args.minalignslop = 0;
2907 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if ((error = xfs_alloc_vextent(&args)))
2909 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002910 }
2911 if (isaligned && args.fsbno == NULLFSBLOCK) {
2912 /*
2913 * allocation failed, so turn off alignment and
2914 * try again.
2915 */
2916 args.type = atype;
2917 args.fsbno = ap->rval;
2918 args.alignment = 0;
2919 if ((error = xfs_alloc_vextent(&args)))
2920 return error;
2921 }
2922 if (args.fsbno == NULLFSBLOCK && nullfb &&
2923 args.minlen > ap->minlen) {
2924 args.minlen = ap->minlen;
2925 args.type = XFS_ALLOCTYPE_START_BNO;
2926 args.fsbno = ap->rval;
2927 if ((error = xfs_alloc_vextent(&args)))
2928 return error;
2929 }
2930 if (args.fsbno == NULLFSBLOCK && nullfb) {
2931 args.fsbno = 0;
2932 args.type = XFS_ALLOCTYPE_FIRST_AG;
2933 args.total = ap->minlen;
2934 args.minleft = 0;
2935 if ((error = xfs_alloc_vextent(&args)))
2936 return error;
2937 ap->low = 1;
2938 }
2939 if (args.fsbno != NULLFSBLOCK) {
2940 ap->firstblock = ap->rval = args.fsbno;
2941 ASSERT(nullfb || fb_agno == args.agno ||
2942 (ap->low && fb_agno < args.agno));
2943 ap->alen = args.len;
2944 ap->ip->i_d.di_nblocks += args.len;
2945 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2946 if (ap->wasdel)
2947 ap->ip->i_delayed_blks -= args.len;
2948 /*
2949 * Adjust the disk quota also. This was reserved
2950 * earlier.
2951 */
2952 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2953 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2954 XFS_TRANS_DQ_BCOUNT,
2955 (long) args.len);
2956 } else {
2957 ap->rval = NULLFSBLOCK;
2958 ap->alen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 }
2960 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002961}
2962
2963/*
2964 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2965 * It figures out where to ask the underlying allocator to put the new extent.
2966 */
2967STATIC int
2968xfs_bmap_alloc(
2969 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2970{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002971 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002972 return xfs_bmap_rtalloc(ap);
2973 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974}
2975
2976/*
2977 * Transform a btree format file with only one leaf node, where the
2978 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002979 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 * give up the space for the btree root and pitch the leaf block.
2981 */
2982STATIC int /* error */
2983xfs_bmap_btree_to_extents(
2984 xfs_trans_t *tp, /* transaction pointer */
2985 xfs_inode_t *ip, /* incore inode pointer */
2986 xfs_btree_cur_t *cur, /* btree cursor */
2987 int *logflagsp, /* inode logging flags */
2988 int whichfork) /* data or attr fork */
2989{
2990 /* REFERENCED */
2991 xfs_bmbt_block_t *cblock;/* child btree block */
2992 xfs_fsblock_t cbno; /* child block number */
2993 xfs_buf_t *cbp; /* child block's buffer */
2994 int error; /* error return value */
2995 xfs_ifork_t *ifp; /* inode fork data */
2996 xfs_mount_t *mp; /* mount point structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10002997 __be64 *pp; /* ptr to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 xfs_bmbt_block_t *rblock;/* root btree block */
2999
3000 ifp = XFS_IFORK_PTR(ip, whichfork);
3001 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3002 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
3003 rblock = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003004 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
3005 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
3007 mp = ip->i_mount;
3008 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10003009 cbno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 *logflagsp = 0;
3011#ifdef DEBUG
Christoph Hellwig576039c2006-09-28 10:58:06 +10003012 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 return error;
3014#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
3016 XFS_BMAP_BTREE_REF)))
3017 return error;
3018 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
3019 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
3020 return error;
3021 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
3022 ip->i_d.di_nblocks--;
3023 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
3024 xfs_trans_binval(tp, cbp);
3025 if (cur->bc_bufs[0] == cbp)
3026 cur->bc_bufs[0] = NULL;
3027 xfs_iroot_realloc(ip, -1, whichfork);
3028 ASSERT(ifp->if_broot == NULL);
3029 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
3030 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3031 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
3032 return 0;
3033}
3034
3035/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003036 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 * after removing space (or undoing a delayed allocation).
3038 */
3039STATIC int /* error */
3040xfs_bmap_del_extent(
3041 xfs_inode_t *ip, /* incore inode pointer */
3042 xfs_trans_t *tp, /* current transaction pointer */
3043 xfs_extnum_t idx, /* extent number to update/delete */
3044 xfs_bmap_free_t *flist, /* list of extents to be freed */
3045 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003046 xfs_bmbt_irec_t *del, /* data to remove from extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003048 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 int whichfork, /* data or attr fork */
3050 int rsvd) /* OK to allocate reserved blocks */
3051{
3052 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
3053 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
3054 xfs_fsblock_t del_endblock=0; /* first block past del */
3055 xfs_fileoff_t del_endoff; /* first offset past del */
3056 int delay; /* current block is delayed allocated */
3057 int do_fx; /* free extent at end of routine */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003058 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 int error; /* error return value */
3060 int flags; /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 xfs_bmbt_irec_t got; /* current extent entry */
3062 xfs_fileoff_t got_endoff; /* first offset past got */
3063 int i; /* temp state */
3064 xfs_ifork_t *ifp; /* inode fork pointer */
3065 xfs_mount_t *mp; /* mount structure */
3066 xfs_filblks_t nblks; /* quota/sb block count */
3067 xfs_bmbt_irec_t new; /* new record to be inserted */
3068 /* REFERENCED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 uint qfield; /* quota field to update */
3070 xfs_filblks_t temp; /* for indirect length calculations */
3071 xfs_filblks_t temp2; /* for indirect length calculations */
3072
3073 XFS_STATS_INC(xs_del_exlist);
3074 mp = ip->i_mount;
3075 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003076 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
3077 (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 ASSERT(del->br_blockcount > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003079 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 xfs_bmbt_get_all(ep, &got);
3081 ASSERT(got.br_startoff <= del->br_startoff);
3082 del_endoff = del->br_startoff + del->br_blockcount;
3083 got_endoff = got.br_startoff + got.br_blockcount;
3084 ASSERT(got_endoff >= del_endoff);
3085 delay = ISNULLSTARTBLOCK(got.br_startblock);
3086 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
3087 flags = 0;
3088 qfield = 0;
3089 error = 0;
3090 /*
3091 * If deleting a real allocation, must free up the disk space.
3092 */
3093 if (!delay) {
3094 flags = XFS_ILOG_CORE;
3095 /*
3096 * Realtime allocation. Free it and record di_nblocks update.
3097 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003098 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 xfs_fsblock_t bno;
3100 xfs_filblks_t len;
3101
3102 ASSERT(do_mod(del->br_blockcount,
3103 mp->m_sb.sb_rextsize) == 0);
3104 ASSERT(do_mod(del->br_startblock,
3105 mp->m_sb.sb_rextsize) == 0);
3106 bno = del->br_startblock;
3107 len = del->br_blockcount;
3108 do_div(bno, mp->m_sb.sb_rextsize);
3109 do_div(len, mp->m_sb.sb_rextsize);
3110 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3111 (xfs_extlen_t)len)))
3112 goto done;
3113 do_fx = 0;
3114 nblks = len * mp->m_sb.sb_rextsize;
3115 qfield = XFS_TRANS_DQ_RTBCOUNT;
3116 }
3117 /*
3118 * Ordinary allocation.
3119 */
3120 else {
3121 do_fx = 1;
3122 nblks = del->br_blockcount;
3123 qfield = XFS_TRANS_DQ_BCOUNT;
3124 }
3125 /*
3126 * Set up del_endblock and cur for later.
3127 */
3128 del_endblock = del->br_startblock + del->br_blockcount;
3129 if (cur) {
3130 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3131 got.br_startblock, got.br_blockcount,
3132 &i)))
3133 goto done;
3134 ASSERT(i == 1);
3135 }
3136 da_old = da_new = 0;
3137 } else {
3138 da_old = STARTBLOCKVAL(got.br_startblock);
3139 da_new = 0;
3140 nblks = 0;
3141 do_fx = 0;
3142 }
3143 /*
3144 * Set flag value to use in switch statement.
3145 * Left-contig is 2, right-contig is 1.
3146 */
3147 switch (((got.br_startoff == del->br_startoff) << 1) |
3148 (got_endoff == del_endoff)) {
3149 case 3:
3150 /*
3151 * Matches the whole extent. Delete the entry.
3152 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10003153 XFS_BMAP_TRACE_DELETE("3", ip, idx, 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003154 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 ifp->if_lastex = idx;
3156 if (delay)
3157 break;
3158 XFS_IFORK_NEXT_SET(ip, whichfork,
3159 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3160 flags |= XFS_ILOG_CORE;
3161 if (!cur) {
3162 flags |= XFS_ILOG_FEXT(whichfork);
3163 break;
3164 }
3165 if ((error = xfs_bmbt_delete(cur, &i)))
3166 goto done;
3167 ASSERT(i == 1);
3168 break;
3169
3170 case 2:
3171 /*
3172 * Deleting the first part of the extent.
3173 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10003174 XFS_BMAP_TRACE_PRE_UPDATE("2", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 xfs_bmbt_set_startoff(ep, del_endoff);
3176 temp = got.br_blockcount - del->br_blockcount;
3177 xfs_bmbt_set_blockcount(ep, temp);
3178 ifp->if_lastex = idx;
3179 if (delay) {
3180 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3181 da_old);
3182 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10003183 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 whichfork);
3185 da_new = temp;
3186 break;
3187 }
3188 xfs_bmbt_set_startblock(ep, del_endblock);
Eric Sandeen3a59c942007-07-11 11:09:47 +10003189 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 if (!cur) {
3191 flags |= XFS_ILOG_FEXT(whichfork);
3192 break;
3193 }
3194 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3195 got.br_blockcount - del->br_blockcount,
3196 got.br_state)))
3197 goto done;
3198 break;
3199
3200 case 1:
3201 /*
3202 * Deleting the last part of the extent.
3203 */
3204 temp = got.br_blockcount - del->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10003205 XFS_BMAP_TRACE_PRE_UPDATE("1", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 xfs_bmbt_set_blockcount(ep, temp);
3207 ifp->if_lastex = idx;
3208 if (delay) {
3209 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3210 da_old);
3211 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10003212 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 whichfork);
3214 da_new = temp;
3215 break;
3216 }
Eric Sandeen3a59c942007-07-11 11:09:47 +10003217 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 if (!cur) {
3219 flags |= XFS_ILOG_FEXT(whichfork);
3220 break;
3221 }
3222 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3223 got.br_startblock,
3224 got.br_blockcount - del->br_blockcount,
3225 got.br_state)))
3226 goto done;
3227 break;
3228
3229 case 0:
3230 /*
3231 * Deleting the middle of the extent.
3232 */
3233 temp = del->br_startoff - got.br_startoff;
Eric Sandeen3a59c942007-07-11 11:09:47 +10003234 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 xfs_bmbt_set_blockcount(ep, temp);
3236 new.br_startoff = del_endoff;
3237 temp2 = got_endoff - del_endoff;
3238 new.br_blockcount = temp2;
3239 new.br_state = got.br_state;
3240 if (!delay) {
3241 new.br_startblock = del_endblock;
3242 flags |= XFS_ILOG_CORE;
3243 if (cur) {
3244 if ((error = xfs_bmbt_update(cur,
3245 got.br_startoff,
3246 got.br_startblock, temp,
3247 got.br_state)))
3248 goto done;
3249 if ((error = xfs_bmbt_increment(cur, 0, &i)))
3250 goto done;
3251 cur->bc_rec.b = new;
3252 error = xfs_bmbt_insert(cur, &i);
3253 if (error && error != ENOSPC)
3254 goto done;
3255 /*
3256 * If get no-space back from btree insert,
3257 * it tried a split, and we have a zero
3258 * block reservation.
3259 * Fix up our state and return the error.
3260 */
3261 if (error == ENOSPC) {
3262 /*
3263 * Reset the cursor, don't trust
3264 * it after any insert operation.
3265 */
3266 if ((error = xfs_bmbt_lookup_eq(cur,
3267 got.br_startoff,
3268 got.br_startblock,
3269 temp, &i)))
3270 goto done;
3271 ASSERT(i == 1);
3272 /*
3273 * Update the btree record back
3274 * to the original value.
3275 */
3276 if ((error = xfs_bmbt_update(cur,
3277 got.br_startoff,
3278 got.br_startblock,
3279 got.br_blockcount,
3280 got.br_state)))
3281 goto done;
3282 /*
3283 * Reset the extent record back
3284 * to the original value.
3285 */
3286 xfs_bmbt_set_blockcount(ep,
3287 got.br_blockcount);
3288 flags = 0;
3289 error = XFS_ERROR(ENOSPC);
3290 goto done;
3291 }
3292 ASSERT(i == 1);
3293 } else
3294 flags |= XFS_ILOG_FEXT(whichfork);
3295 XFS_IFORK_NEXT_SET(ip, whichfork,
3296 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3297 } else {
3298 ASSERT(whichfork == XFS_DATA_FORK);
3299 temp = xfs_bmap_worst_indlen(ip, temp);
3300 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3301 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3302 new.br_startblock = NULLSTARTBLOCK((int)temp2);
3303 da_new = temp + temp2;
3304 while (da_new > da_old) {
3305 if (temp) {
3306 temp--;
3307 da_new--;
3308 xfs_bmbt_set_startblock(ep,
3309 NULLSTARTBLOCK((int)temp));
3310 }
3311 if (da_new == da_old)
3312 break;
3313 if (temp2) {
3314 temp2--;
3315 da_new--;
3316 new.br_startblock =
3317 NULLSTARTBLOCK((int)temp2);
3318 }
3319 }
3320 }
Eric Sandeen3a59c942007-07-11 11:09:47 +10003321 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, whichfork);
3322 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 1, &new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003324 xfs_iext_insert(ifp, idx + 1, 1, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 ifp->if_lastex = idx + 1;
3326 break;
3327 }
3328 /*
3329 * If we need to, add to list of extents to delete.
3330 */
3331 if (do_fx)
3332 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3333 mp);
3334 /*
3335 * Adjust inode # blocks in the file.
3336 */
3337 if (nblks)
3338 ip->i_d.di_nblocks -= nblks;
3339 /*
3340 * Adjust quota data.
3341 */
3342 if (qfield)
3343 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
3344
3345 /*
3346 * Account for change in delayed indirect blocks.
3347 * Nothing to do for disk quota accounting here.
3348 */
3349 ASSERT(da_old >= da_new);
3350 if (da_old > da_new)
David Chinner20f4ebf2007-02-10 18:36:10 +11003351 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 rsvd);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003353 if (delta) {
3354 /* DELTA: report the original extent. */
3355 if (delta->xed_startoff > got.br_startoff)
3356 delta->xed_startoff = got.br_startoff;
3357 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3358 delta->xed_blockcount = got.br_startoff +
3359 got.br_blockcount;
3360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361done:
3362 *logflagsp = flags;
3363 return error;
3364}
3365
3366/*
3367 * Remove the entry "free" from the free item list. Prev points to the
3368 * previous entry, unless "free" is the head of the list.
3369 */
3370STATIC void
3371xfs_bmap_del_free(
3372 xfs_bmap_free_t *flist, /* free item list header */
3373 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3374 xfs_bmap_free_item_t *free) /* list item to be freed */
3375{
3376 if (prev)
3377 prev->xbfi_next = free->xbfi_next;
3378 else
3379 flist->xbf_first = free->xbfi_next;
3380 flist->xbf_count--;
3381 kmem_zone_free(xfs_bmap_free_item_zone, free);
3382}
3383
3384/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 * Convert an extents-format file into a btree-format file.
3386 * The new file will have a root block (in the inode) and a single child block.
3387 */
3388STATIC int /* error */
3389xfs_bmap_extents_to_btree(
3390 xfs_trans_t *tp, /* transaction pointer */
3391 xfs_inode_t *ip, /* incore inode pointer */
3392 xfs_fsblock_t *firstblock, /* first-block-allocated */
3393 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3394 xfs_btree_cur_t **curp, /* cursor returned to caller */
3395 int wasdel, /* converting a delayed alloc */
3396 int *logflagsp, /* inode logging flags */
3397 int whichfork) /* data or attr fork */
3398{
3399 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
3400 xfs_buf_t *abp; /* buffer for ablock */
3401 xfs_alloc_arg_t args; /* allocation arguments */
3402 xfs_bmbt_rec_t *arp; /* child record pointer */
3403 xfs_bmbt_block_t *block; /* btree root block */
3404 xfs_btree_cur_t *cur; /* bmap btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003405 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003407 xfs_extnum_t i, cnt; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 xfs_ifork_t *ifp; /* inode fork pointer */
3409 xfs_bmbt_key_t *kp; /* root block key pointer */
3410 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003411 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3413
3414 ifp = XFS_IFORK_PTR(ip, whichfork);
3415 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3416 ASSERT(ifp->if_ext_max ==
3417 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3418 /*
3419 * Make space in the inode incore.
3420 */
3421 xfs_iroot_realloc(ip, 1, whichfork);
3422 ifp->if_flags |= XFS_IFBROOT;
3423 /*
3424 * Fill in the root.
3425 */
3426 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003427 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3428 block->bb_level = cpu_to_be16(1);
3429 block->bb_numrecs = cpu_to_be16(1);
3430 block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3431 block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 /*
3433 * Need a cursor. Can't allocate until bb_level is filled in.
3434 */
3435 mp = ip->i_mount;
3436 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
3437 whichfork);
3438 cur->bc_private.b.firstblock = *firstblock;
3439 cur->bc_private.b.flist = flist;
3440 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3441 /*
3442 * Convert to a btree with two levels, one record in root.
3443 */
3444 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3445 args.tp = tp;
3446 args.mp = mp;
Yingping Lud210a282006-06-09 14:55:18 +10003447 args.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 if (*firstblock == NULLFSBLOCK) {
3449 args.type = XFS_ALLOCTYPE_START_BNO;
3450 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3451 } else if (flist->xbf_low) {
3452 args.type = XFS_ALLOCTYPE_START_BNO;
3453 args.fsbno = *firstblock;
3454 } else {
3455 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3456 args.fsbno = *firstblock;
3457 }
3458 args.minlen = args.maxlen = args.prod = 1;
3459 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3460 args.minalignslop = 0;
3461 args.wasdel = wasdel;
3462 *logflagsp = 0;
3463 if ((error = xfs_alloc_vextent(&args))) {
3464 xfs_iroot_realloc(ip, -1, whichfork);
3465 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3466 return error;
3467 }
3468 /*
3469 * Allocation can't fail, the space was reserved.
3470 */
3471 ASSERT(args.fsbno != NULLFSBLOCK);
3472 ASSERT(*firstblock == NULLFSBLOCK ||
3473 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3474 (flist->xbf_low &&
3475 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3476 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3477 cur->bc_private.b.allocated++;
3478 ip->i_d.di_nblocks++;
3479 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3480 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3481 /*
3482 * Fill in the child block.
3483 */
3484 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003485 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 ablock->bb_level = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003487 ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3488 ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3490 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003491 for (cnt = i = 0; i < nextents; i++) {
3492 ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10003494 arp->l0 = cpu_to_be64(ep->l0);
3495 arp->l1 = cpu_to_be64(ep->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 arp++; cnt++;
3497 }
3498 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11003499 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3500 ablock->bb_numrecs = cpu_to_be16(cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 /*
3502 * Fill in the root key and pointer.
3503 */
3504 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
3505 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
Christoph Hellwig8801bb92006-09-28 10:58:17 +10003506 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
Christoph Hellwig576039c2006-09-28 10:58:06 +10003508 *pp = cpu_to_be64(args.fsbno);
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 */
3513 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003514 xfs_bmbt_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;
3517 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
3518 return 0;
3519}
3520
3521/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11003522 * Helper routine to reset inode di_forkoff field when switching
3523 * attribute fork from local to extent format - we reset it where
3524 * possible to make space available for inline data fork extents.
3525 */
3526STATIC void
3527xfs_bmap_forkoff_reset(
3528 xfs_mount_t *mp,
3529 xfs_inode_t *ip,
3530 int whichfork)
3531{
3532 if (whichfork == XFS_ATTR_FORK &&
3533 (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
3534 (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
Barry Naujoke5889e92007-02-10 18:35:58 +11003535 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
Nathan Scottd8cc8902005-11-02 10:34:53 +11003536 ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
3537 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3538 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
3539 (uint)sizeof(xfs_bmbt_rec_t);
3540 ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
3541 (uint)sizeof(xfs_bmbt_rec_t);
3542 }
3543}
3544
3545/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 * Convert a local file to an extents file.
3547 * This code is out of bounds for data forks of regular files,
3548 * since the file data needs to get logged so things will stay consistent.
3549 * (The bmap-level manipulations are ok, though).
3550 */
3551STATIC int /* error */
3552xfs_bmap_local_to_extents(
3553 xfs_trans_t *tp, /* transaction pointer */
3554 xfs_inode_t *ip, /* incore inode pointer */
3555 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3556 xfs_extlen_t total, /* total blocks needed by transaction */
3557 int *logflagsp, /* inode logging flags */
3558 int whichfork) /* data or attr fork */
3559{
3560 int error; /* error return value */
3561 int flags; /* logging flags returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 xfs_ifork_t *ifp; /* inode fork pointer */
3563
3564 /*
3565 * We don't want to deal with the case of keeping inode data inline yet.
3566 * So sending the data fork of a regular inode is invalid.
3567 */
3568 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3569 whichfork == XFS_DATA_FORK));
3570 ifp = XFS_IFORK_PTR(ip, whichfork);
3571 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3572 flags = 0;
3573 error = 0;
3574 if (ifp->if_bytes) {
3575 xfs_alloc_arg_t args; /* allocation arguments */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003576 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003577 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
3579 args.tp = tp;
3580 args.mp = ip->i_mount;
Yingping Lud210a282006-06-09 14:55:18 +10003581 args.firstblock = *firstblock;
Mandy Kirkconnellf020b672006-03-14 14:07:24 +11003582 ASSERT((ifp->if_flags &
3583 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 /*
3585 * Allocate a block. We know we need only one, since the
3586 * file currently fits in an inode.
3587 */
3588 if (*firstblock == NULLFSBLOCK) {
3589 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3590 args.type = XFS_ALLOCTYPE_START_BNO;
3591 } else {
3592 args.fsbno = *firstblock;
3593 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3594 }
3595 args.total = total;
3596 args.mod = args.minleft = args.alignment = args.wasdel =
3597 args.isfl = args.minalignslop = 0;
3598 args.minlen = args.maxlen = args.prod = 1;
3599 if ((error = xfs_alloc_vextent(&args)))
3600 goto done;
3601 /*
3602 * Can't fail, the space was reserved.
3603 */
3604 ASSERT(args.fsbno != NULLFSBLOCK);
3605 ASSERT(args.len == 1);
3606 *firstblock = args.fsbno;
3607 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3608 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3609 ifp->if_bytes);
3610 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003611 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003613 xfs_iext_add(ifp, 0, 1);
3614 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
Eric Sandeen3a59c942007-07-11 11:09:47 +10003616 XFS_BMAP_TRACE_POST_UPDATE("new", ip, 0, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3618 ip->i_d.di_nblocks = 1;
3619 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
3620 XFS_TRANS_DQ_BCOUNT, 1L);
3621 flags |= XFS_ILOG_FEXT(whichfork);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003622 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003624 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 ifp->if_flags &= ~XFS_IFINLINE;
3627 ifp->if_flags |= XFS_IFEXTENTS;
3628 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3629 flags |= XFS_ILOG_CORE;
3630done:
3631 *logflagsp = flags;
3632 return error;
3633}
3634
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003636 * Search the extent records for the entry containing block bno.
3637 * If bno lies in a hole, point to the next entry. If bno lies
3638 * past eof, *eofp will be set, and *prevp will contain the last
3639 * entry (null if none). Else, *lastxp will be set to the index
3640 * of the found entry; *gotp will contain the entry.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003641 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003642xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003643xfs_bmap_search_multi_extents(
3644 xfs_ifork_t *ifp, /* inode fork pointer */
3645 xfs_fileoff_t bno, /* block number searched for */
3646 int *eofp, /* out: end of file found */
3647 xfs_extnum_t *lastxp, /* out: last extent index */
3648 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3649 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3650{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003651 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003652 xfs_extnum_t lastx; /* last extent index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003653
3654 /*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003655 * Initialize the extent entry structure to catch access to
3656 * uninitialized br_startblock field.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003657 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003658 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3659 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3660 gotp->br_state = XFS_EXT_INVALID;
3661#if XFS_BIG_BLKNOS
3662 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3663#else
3664 gotp->br_startblock = 0xffffa5a5;
3665#endif
3666 prevp->br_startoff = NULLFILEOFF;
3667
3668 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3669 if (lastx > 0) {
3670 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3671 }
3672 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3673 xfs_bmbt_get_all(ep, gotp);
3674 *eofp = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003675 } else {
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003676 if (lastx > 0) {
3677 *gotp = *prevp;
3678 }
3679 *eofp = 1;
3680 ep = NULL;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003681 }
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003682 *lastxp = lastx;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003683 return ep;
3684}
3685
3686/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 * Search the extents list for the inode, for the extent containing bno.
3688 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3689 * *eofp will be set, and *prevp will contain the last entry (null if none).
3690 * Else, *lastxp will be set to the index of the found
3691 * entry; *gotp will contain the entry.
3692 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003693STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694xfs_bmap_search_extents(
3695 xfs_inode_t *ip, /* incore inode pointer */
3696 xfs_fileoff_t bno, /* block number searched for */
Nathan Scott572d95f2006-09-28 11:03:20 +10003697 int fork, /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 int *eofp, /* out: end of file found */
3699 xfs_extnum_t *lastxp, /* out: last extent index */
3700 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3701 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3702{
3703 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003704 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705
3706 XFS_STATS_INC(xs_look_exlist);
Nathan Scott572d95f2006-09-28 11:03:20 +10003707 ifp = XFS_IFORK_PTR(ip, fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003709 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3710
Nathan Scott572d95f2006-09-28 11:03:20 +10003711 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3712 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3713 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3714 "Access to block zero in inode %llu "
3715 "start_block: %llx start_off: %llx "
3716 "blkcnt: %llx extent-state: %x lastx: %x\n",
3717 (unsigned long long)ip->i_ino,
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003718 (unsigned long long)gotp->br_startblock,
3719 (unsigned long long)gotp->br_startoff,
3720 (unsigned long long)gotp->br_blockcount,
Nathan Scott572d95f2006-09-28 11:03:20 +10003721 gotp->br_state, *lastxp);
3722 *lastxp = NULLEXTNUM;
3723 *eofp = 1;
3724 return NULL;
3725 }
3726 return ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727}
3728
3729
3730#ifdef XFS_BMAP_TRACE
3731ktrace_t *xfs_bmap_trace_buf;
3732
3733/*
3734 * Add a bmap trace buffer entry. Base routine for the others.
3735 */
3736STATIC void
3737xfs_bmap_trace_addentry(
3738 int opcode, /* operation */
Eric Sandeen3a59c942007-07-11 11:09:47 +10003739 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 char *desc, /* operation description */
3741 xfs_inode_t *ip, /* incore inode pointer */
3742 xfs_extnum_t idx, /* index of entry(ies) */
3743 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003744 xfs_bmbt_rec_host_t *r1, /* first record */
3745 xfs_bmbt_rec_host_t *r2, /* second record or null */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 int whichfork) /* data or attr fork */
3747{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003748 xfs_bmbt_rec_host_t tr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
3750 ASSERT(cnt == 1 || cnt == 2);
3751 ASSERT(r1 != NULL);
3752 if (cnt == 1) {
3753 ASSERT(r2 == NULL);
3754 r2 = &tr2;
3755 memset(&tr2, 0, sizeof(tr2));
3756 } else
3757 ASSERT(r2 != NULL);
3758 ktrace_enter(xfs_bmap_trace_buf,
3759 (void *)(__psint_t)(opcode | (whichfork << 16)),
3760 (void *)fname, (void *)desc, (void *)ip,
3761 (void *)(__psint_t)idx,
3762 (void *)(__psint_t)cnt,
3763 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3764 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3765 (void *)(__psunsigned_t)(r1->l0 >> 32),
3766 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3767 (void *)(__psunsigned_t)(r1->l1 >> 32),
3768 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3769 (void *)(__psunsigned_t)(r2->l0 >> 32),
3770 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3771 (void *)(__psunsigned_t)(r2->l1 >> 32),
3772 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3773 );
3774 ASSERT(ip->i_xtrace);
3775 ktrace_enter(ip->i_xtrace,
3776 (void *)(__psint_t)(opcode | (whichfork << 16)),
3777 (void *)fname, (void *)desc, (void *)ip,
3778 (void *)(__psint_t)idx,
3779 (void *)(__psint_t)cnt,
3780 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3781 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3782 (void *)(__psunsigned_t)(r1->l0 >> 32),
3783 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3784 (void *)(__psunsigned_t)(r1->l1 >> 32),
3785 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3786 (void *)(__psunsigned_t)(r2->l0 >> 32),
3787 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3788 (void *)(__psunsigned_t)(r2->l1 >> 32),
3789 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3790 );
3791}
3792
3793/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003794 * Add bmap trace entry prior to a call to xfs_iext_remove.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 */
3796STATIC void
3797xfs_bmap_trace_delete(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003798 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 char *desc, /* operation description */
3800 xfs_inode_t *ip, /* incore inode pointer */
3801 xfs_extnum_t idx, /* index of entry(entries) deleted */
3802 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3803 int whichfork) /* data or attr fork */
3804{
3805 xfs_ifork_t *ifp; /* inode fork pointer */
3806
3807 ifp = XFS_IFORK_PTR(ip, whichfork);
3808 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003809 cnt, xfs_iext_get_ext(ifp, idx),
3810 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 whichfork);
3812}
3813
3814/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003815 * Add bmap trace entry prior to a call to xfs_iext_insert, or
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 * reading in the extents list from the disk (in the btree).
3817 */
3818STATIC void
3819xfs_bmap_trace_insert(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003820 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 char *desc, /* operation description */
3822 xfs_inode_t *ip, /* incore inode pointer */
3823 xfs_extnum_t idx, /* index of entry(entries) inserted */
3824 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3825 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3826 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3827 int whichfork) /* data or attr fork */
3828{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003829 xfs_bmbt_rec_host_t tr1; /* compressed record 1 */
3830 xfs_bmbt_rec_host_t tr2; /* compressed record 2 if needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
3832 xfs_bmbt_set_all(&tr1, r1);
3833 if (cnt == 2) {
3834 ASSERT(r2 != NULL);
3835 xfs_bmbt_set_all(&tr2, r2);
3836 } else {
3837 ASSERT(cnt == 1);
3838 ASSERT(r2 == NULL);
3839 }
3840 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3841 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3842}
3843
3844/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003845 * Add bmap trace entry after updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 */
3847STATIC void
3848xfs_bmap_trace_post_update(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003849 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 char *desc, /* operation description */
3851 xfs_inode_t *ip, /* incore inode pointer */
3852 xfs_extnum_t idx, /* index of entry updated */
3853 int whichfork) /* data or attr fork */
3854{
3855 xfs_ifork_t *ifp; /* inode fork pointer */
3856
3857 ifp = XFS_IFORK_PTR(ip, whichfork);
3858 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003859 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860}
3861
3862/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003863 * Add bmap trace entry prior to updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 */
3865STATIC void
3866xfs_bmap_trace_pre_update(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003867 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 char *desc, /* operation description */
3869 xfs_inode_t *ip, /* incore inode pointer */
3870 xfs_extnum_t idx, /* index of entry to be updated */
3871 int whichfork) /* data or attr fork */
3872{
3873 xfs_ifork_t *ifp; /* inode fork pointer */
3874
3875 ifp = XFS_IFORK_PTR(ip, whichfork);
3876 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003877 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878}
3879#endif /* XFS_BMAP_TRACE */
3880
3881/*
3882 * Compute the worst-case number of indirect blocks that will be used
3883 * for ip's delayed extent of length "len".
3884 */
3885STATIC xfs_filblks_t
3886xfs_bmap_worst_indlen(
3887 xfs_inode_t *ip, /* incore inode pointer */
3888 xfs_filblks_t len) /* delayed extent length */
3889{
3890 int level; /* btree level number */
3891 int maxrecs; /* maximum record count at this level */
3892 xfs_mount_t *mp; /* mount structure */
3893 xfs_filblks_t rval; /* return value */
3894
3895 mp = ip->i_mount;
3896 maxrecs = mp->m_bmap_dmxr[0];
3897 for (level = 0, rval = 0;
3898 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3899 level++) {
3900 len += maxrecs - 1;
3901 do_div(len, maxrecs);
3902 rval += len;
3903 if (len == 1)
3904 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3905 level - 1;
3906 if (level == 0)
3907 maxrecs = mp->m_bmap_dmxr[1];
3908 }
3909 return rval;
3910}
3911
3912#if defined(XFS_RW_TRACE)
3913STATIC void
3914xfs_bunmap_trace(
3915 xfs_inode_t *ip,
3916 xfs_fileoff_t bno,
3917 xfs_filblks_t len,
3918 int flags,
3919 inst_t *ra)
3920{
3921 if (ip->i_rwtrace == NULL)
3922 return;
3923 ktrace_enter(ip->i_rwtrace,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003924 (void *)(__psint_t)XFS_BUNMAP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 (void *)ip,
3926 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3927 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3928 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3929 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3930 (void *)(__psint_t)len,
3931 (void *)(__psint_t)flags,
3932 (void *)(unsigned long)current_cpu(),
3933 (void *)ra,
3934 (void *)0,
3935 (void *)0,
3936 (void *)0,
3937 (void *)0,
3938 (void *)0,
3939 (void *)0);
3940}
3941#endif
3942
3943/*
3944 * Convert inode from non-attributed to attributed.
3945 * Must not be in a transaction, ip must not be locked.
3946 */
3947int /* error code */
3948xfs_bmap_add_attrfork(
3949 xfs_inode_t *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003950 int size, /* space new attribute needs */
3951 int rsvd) /* xact may use reserved blks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003954 xfs_bmap_free_t flist; /* freed extent records */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 xfs_trans_t *tp; /* transaction pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003957 int blks; /* space reservation */
3958 int version = 1; /* superblock attr version */
3959 int committed; /* xaction was committed */
3960 int logflags; /* logging flags */
3961 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
Nathan Scottd8cc8902005-11-02 10:34:53 +11003963 ASSERT(XFS_IFORK_Q(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 ASSERT(ip->i_df.if_ext_max ==
3965 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
Nathan Scottd8cc8902005-11-02 10:34:53 +11003966
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 mp = ip->i_mount;
3968 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3969 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3970 blks = XFS_ADDAFORK_SPACE_RES(mp);
3971 if (rsvd)
3972 tp->t_flags |= XFS_TRANS_RESERVE;
3973 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3974 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3975 goto error0;
3976 xfs_ilock(ip, XFS_ILOCK_EXCL);
3977 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
3978 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3979 XFS_QMOPT_RES_REGBLKS);
3980 if (error) {
3981 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3982 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3983 return error;
3984 }
3985 if (XFS_IFORK_Q(ip))
3986 goto error1;
3987 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3988 /*
3989 * For inodes coming from pre-6.2 filesystems.
3990 */
3991 ASSERT(ip->i_d.di_aformat == 0);
3992 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3993 }
3994 ASSERT(ip->i_d.di_anextents == 0);
3995 VN_HOLD(XFS_ITOV(ip));
3996 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3997 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3998 switch (ip->i_d.di_format) {
3999 case XFS_DINODE_FMT_DEV:
4000 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
4001 break;
4002 case XFS_DINODE_FMT_UUID:
4003 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
4004 break;
4005 case XFS_DINODE_FMT_LOCAL:
4006 case XFS_DINODE_FMT_EXTENTS:
4007 case XFS_DINODE_FMT_BTREE:
Nathan Scottd8cc8902005-11-02 10:34:53 +11004008 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
4009 if (!ip->i_d.di_forkoff)
4010 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
Nathan Scott13059ff2006-01-11 15:32:01 +11004011 else if (mp->m_flags & XFS_MOUNT_ATTR2)
Nathan Scottd8cc8902005-11-02 10:34:53 +11004012 version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 break;
4014 default:
4015 ASSERT(0);
4016 error = XFS_ERROR(EINVAL);
4017 goto error1;
4018 }
4019 ip->i_df.if_ext_max =
4020 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4021 ASSERT(ip->i_afp == NULL);
4022 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
4023 ip->i_afp->if_ext_max =
4024 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4025 ip->i_afp->if_flags = XFS_IFEXTENTS;
4026 logflags = 0;
4027 XFS_BMAP_INIT(&flist, &firstblock);
4028 switch (ip->i_d.di_format) {
4029 case XFS_DINODE_FMT_LOCAL:
4030 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
4031 &logflags);
4032 break;
4033 case XFS_DINODE_FMT_EXTENTS:
4034 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
4035 &flist, &logflags);
4036 break;
4037 case XFS_DINODE_FMT_BTREE:
4038 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
4039 &logflags);
4040 break;
4041 default:
4042 error = 0;
4043 break;
4044 }
4045 if (logflags)
4046 xfs_trans_log_inode(tp, ip, logflags);
4047 if (error)
4048 goto error2;
Eric Sandeen62118702008-03-06 13:44:28 +11004049 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
4050 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +11004051 __int64_t sbfields = 0;
4052
Eric Sandeen3685c2a2007-10-11 17:42:32 +10004053 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +11004054 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
4055 xfs_sb_version_addattr(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11004056 sbfields |= XFS_SB_VERSIONNUM;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004057 }
Eric Sandeen62118702008-03-06 13:44:28 +11004058 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
4059 xfs_sb_version_addattr2(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11004060 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
Nathan Scottd8cc8902005-11-02 10:34:53 +11004061 }
Nathan Scottda087ba2005-11-02 15:00:20 +11004062 if (sbfields) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10004063 spin_unlock(&mp->m_sb_lock);
Nathan Scottda087ba2005-11-02 15:00:20 +11004064 xfs_mod_sb(tp, sbfields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +10004066 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 }
Eric Sandeenf7c99b62007-02-10 18:37:16 +11004068 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 goto error2;
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004070 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 ASSERT(ip->i_df.if_ext_max ==
4072 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4073 return error;
4074error2:
4075 xfs_bmap_cancel(&flist);
4076error1:
4077 ASSERT(ismrlocked(&ip->i_lock,MR_UPDATE));
4078 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4079error0:
4080 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4081 ASSERT(ip->i_df.if_ext_max ==
4082 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4083 return error;
4084}
4085
4086/*
4087 * Add the extent to the list of extents to be free at transaction end.
4088 * The list is maintained sorted (by block number).
4089 */
4090/* ARGSUSED */
4091void
4092xfs_bmap_add_free(
4093 xfs_fsblock_t bno, /* fs block number of extent */
4094 xfs_filblks_t len, /* length of extent */
4095 xfs_bmap_free_t *flist, /* list of extents */
4096 xfs_mount_t *mp) /* mount point structure */
4097{
4098 xfs_bmap_free_item_t *cur; /* current (next) element */
4099 xfs_bmap_free_item_t *new; /* new element */
4100 xfs_bmap_free_item_t *prev; /* previous element */
4101#ifdef DEBUG
4102 xfs_agnumber_t agno;
4103 xfs_agblock_t agbno;
4104
4105 ASSERT(bno != NULLFSBLOCK);
4106 ASSERT(len > 0);
4107 ASSERT(len <= MAXEXTLEN);
4108 ASSERT(!ISNULLSTARTBLOCK(bno));
4109 agno = XFS_FSB_TO_AGNO(mp, bno);
4110 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4111 ASSERT(agno < mp->m_sb.sb_agcount);
4112 ASSERT(agbno < mp->m_sb.sb_agblocks);
4113 ASSERT(len < mp->m_sb.sb_agblocks);
4114 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4115#endif
4116 ASSERT(xfs_bmap_free_item_zone != NULL);
4117 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4118 new->xbfi_startblock = bno;
4119 new->xbfi_blockcount = (xfs_extlen_t)len;
4120 for (prev = NULL, cur = flist->xbf_first;
4121 cur != NULL;
4122 prev = cur, cur = cur->xbfi_next) {
4123 if (cur->xbfi_startblock >= bno)
4124 break;
4125 }
4126 if (prev)
4127 prev->xbfi_next = new;
4128 else
4129 flist->xbf_first = new;
4130 new->xbfi_next = cur;
4131 flist->xbf_count++;
4132}
4133
4134/*
4135 * Compute and fill in the value of the maximum depth of a bmap btree
4136 * in this filesystem. Done once, during mount.
4137 */
4138void
4139xfs_bmap_compute_maxlevels(
4140 xfs_mount_t *mp, /* file system mount structure */
4141 int whichfork) /* data or attr fork */
4142{
4143 int level; /* btree level */
4144 uint maxblocks; /* max blocks at this level */
4145 uint maxleafents; /* max leaf entries possible */
4146 int maxrootrecs; /* max records in root block */
4147 int minleafrecs; /* min records in leaf block */
4148 int minnoderecs; /* min records in node block */
4149 int sz; /* root block size */
4150
4151 /*
4152 * The maximum number of extents in a file, hence the maximum
4153 * number of leaf entries, is controlled by the type of di_nextents
4154 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4155 * (a signed 16-bit number, xfs_aextnum_t).
4156 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11004157 if (whichfork == XFS_DATA_FORK) {
4158 maxleafents = MAXEXTNUM;
Nathan Scott13059ff2006-01-11 15:32:01 +11004159 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4160 XFS_BMDR_SPACE_CALC(MINDBTPTRS) : mp->m_attroffset;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004161 } else {
4162 maxleafents = MAXAEXTNUM;
Nathan Scott13059ff2006-01-11 15:32:01 +11004163 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4164 XFS_BMDR_SPACE_CALC(MINABTPTRS) :
4165 mp->m_sb.sb_inodesize - mp->m_attroffset;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004166 }
4167 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 minleafrecs = mp->m_bmap_dmnr[0];
4169 minnoderecs = mp->m_bmap_dmnr[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4171 for (level = 1; maxblocks > 1; level++) {
4172 if (maxblocks <= maxrootrecs)
4173 maxblocks = 1;
4174 else
4175 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4176 }
4177 mp->m_bm_maxlevels[whichfork] = level;
4178}
4179
4180/*
4181 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4182 * caller. Frees all the extents that need freeing, which must be done
4183 * last due to locking considerations. We never free any extents in
4184 * the first transaction. This is to allow the caller to make the first
4185 * transaction a synchronous one so that the pointers to the data being
4186 * broken in this transaction will be permanent before the data is actually
4187 * freed. This is necessary to prevent blocks from being reallocated
4188 * and written to before the free and reallocation are actually permanent.
4189 * We do not just make the first transaction synchronous here, because
4190 * there are more efficient ways to gain the same protection in some cases
4191 * (see the file truncation code).
4192 *
4193 * Return 1 if the given transaction was committed and a new one
4194 * started, and 0 otherwise in the committed parameter.
4195 */
4196/*ARGSUSED*/
4197int /* error */
4198xfs_bmap_finish(
4199 xfs_trans_t **tp, /* transaction pointer addr */
4200 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 int *committed) /* xact committed or not */
4202{
4203 xfs_efd_log_item_t *efd; /* extent free data */
4204 xfs_efi_log_item_t *efi; /* extent free intention */
4205 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004206 xfs_bmap_free_item_t *free; /* free extent item */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 unsigned int logres; /* new log reservation */
4208 unsigned int logcount; /* new log count */
4209 xfs_mount_t *mp; /* filesystem mount structure */
4210 xfs_bmap_free_item_t *next; /* next item on free list */
4211 xfs_trans_t *ntp; /* new transaction pointer */
4212
4213 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4214 if (flist->xbf_count == 0) {
4215 *committed = 0;
4216 return 0;
4217 }
4218 ntp = *tp;
4219 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4220 for (free = flist->xbf_first; free; free = free->xbfi_next)
4221 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4222 free->xbfi_blockcount);
4223 logres = ntp->t_log_res;
4224 logcount = ntp->t_log_count;
4225 ntp = xfs_trans_dup(*tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004226 error = xfs_trans_commit(*tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 *tp = ntp;
4228 *committed = 1;
4229 /*
4230 * We have a new transaction, so we should return committed=1,
4231 * even though we're returning an error.
4232 */
4233 if (error) {
4234 return error;
4235 }
4236 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4237 logcount)))
4238 return error;
4239 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4240 for (free = flist->xbf_first; free != NULL; free = next) {
4241 next = free->xbfi_next;
4242 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4243 free->xbfi_blockcount))) {
4244 /*
4245 * The bmap free list will be cleaned up at a
4246 * higher level. The EFI will be canceled when
4247 * this transaction is aborted.
4248 * Need to force shutdown here to make sure it
4249 * happens, since this transaction may not be
4250 * dirty yet.
4251 */
4252 mp = ntp->t_mountp;
4253 if (!XFS_FORCED_SHUTDOWN(mp))
4254 xfs_force_shutdown(mp,
4255 (error == EFSCORRUPTED) ?
Nathan Scott7d04a332006-06-09 14:58:38 +10004256 SHUTDOWN_CORRUPT_INCORE :
4257 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 return error;
4259 }
4260 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4261 free->xbfi_blockcount);
4262 xfs_bmap_del_free(flist, NULL, free);
4263 }
4264 return 0;
4265}
4266
4267/*
4268 * Free up any items left in the list.
4269 */
4270void
4271xfs_bmap_cancel(
4272 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4273{
4274 xfs_bmap_free_item_t *free; /* free list item */
4275 xfs_bmap_free_item_t *next;
4276
4277 if (flist->xbf_count == 0)
4278 return;
4279 ASSERT(flist->xbf_first != NULL);
4280 for (free = flist->xbf_first; free; free = next) {
4281 next = free->xbfi_next;
4282 xfs_bmap_del_free(flist, NULL, free);
4283 }
4284 ASSERT(flist->xbf_count == 0);
4285}
4286
4287/*
4288 * Returns the file-relative block number of the first unused block(s)
4289 * in the file with at least "len" logically contiguous blocks free.
4290 * This is the lowest-address hole if the file has holes, else the first block
4291 * past the end of file.
4292 * Return 0 if the file is currently local (in-inode).
4293 */
4294int /* error */
4295xfs_bmap_first_unused(
4296 xfs_trans_t *tp, /* transaction pointer */
4297 xfs_inode_t *ip, /* incore inode */
4298 xfs_extlen_t len, /* size of hole to find */
4299 xfs_fileoff_t *first_unused, /* unused block */
4300 int whichfork) /* data or attr fork */
4301{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004303 int idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 xfs_ifork_t *ifp; /* inode fork pointer */
4305 xfs_fileoff_t lastaddr; /* last block number seen */
4306 xfs_fileoff_t lowest; /* lowest useful block */
4307 xfs_fileoff_t max; /* starting useful block */
4308 xfs_fileoff_t off; /* offset for this block */
4309 xfs_extnum_t nextents; /* number of extent entries */
4310
4311 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4312 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4313 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4314 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4315 *first_unused = 0;
4316 return 0;
4317 }
4318 ifp = XFS_IFORK_PTR(ip, whichfork);
4319 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4320 (error = xfs_iread_extents(tp, ip, whichfork)))
4321 return error;
4322 lowest = *first_unused;
4323 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004324 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004325 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 off = xfs_bmbt_get_startoff(ep);
4327 /*
4328 * See if the hole before this extent will work.
4329 */
4330 if (off >= lowest + len && off - max >= len) {
4331 *first_unused = max;
4332 return 0;
4333 }
4334 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4335 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4336 }
4337 *first_unused = max;
4338 return 0;
4339}
4340
4341/*
4342 * Returns the file-relative block number of the last block + 1 before
4343 * last_block (input value) in the file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004344 * This is not based on i_size, it is based on the extent records.
4345 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 */
4347int /* error */
4348xfs_bmap_last_before(
4349 xfs_trans_t *tp, /* transaction pointer */
4350 xfs_inode_t *ip, /* incore inode */
4351 xfs_fileoff_t *last_block, /* last block */
4352 int whichfork) /* data or attr fork */
4353{
4354 xfs_fileoff_t bno; /* input file offset */
4355 int eof; /* hit end of file */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004356 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 int error; /* error return value */
4358 xfs_bmbt_irec_t got; /* current extent value */
4359 xfs_ifork_t *ifp; /* inode fork pointer */
4360 xfs_extnum_t lastx; /* last extent used */
4361 xfs_bmbt_irec_t prev; /* previous extent value */
4362
4363 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4364 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4365 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4366 return XFS_ERROR(EIO);
4367 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4368 *last_block = 0;
4369 return 0;
4370 }
4371 ifp = XFS_IFORK_PTR(ip, whichfork);
4372 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4373 (error = xfs_iread_extents(tp, ip, whichfork)))
4374 return error;
4375 bno = *last_block - 1;
4376 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4377 &prev);
4378 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4379 if (prev.br_startoff == NULLFILEOFF)
4380 *last_block = 0;
4381 else
4382 *last_block = prev.br_startoff + prev.br_blockcount;
4383 }
4384 /*
4385 * Otherwise *last_block is already the right answer.
4386 */
4387 return 0;
4388}
4389
4390/*
4391 * Returns the file-relative block number of the first block past eof in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004392 * the file. This is not based on i_size, it is based on the extent records.
4393 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 */
4395int /* error */
4396xfs_bmap_last_offset(
4397 xfs_trans_t *tp, /* transaction pointer */
4398 xfs_inode_t *ip, /* incore inode */
4399 xfs_fileoff_t *last_block, /* last block */
4400 int whichfork) /* data or attr fork */
4401{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004402 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 int error; /* error return value */
4404 xfs_ifork_t *ifp; /* inode fork pointer */
4405 xfs_extnum_t nextents; /* number of extent entries */
4406
4407 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4408 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4409 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4410 return XFS_ERROR(EIO);
4411 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4412 *last_block = 0;
4413 return 0;
4414 }
4415 ifp = XFS_IFORK_PTR(ip, whichfork);
4416 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4417 (error = xfs_iread_extents(tp, ip, whichfork)))
4418 return error;
4419 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4420 if (!nextents) {
4421 *last_block = 0;
4422 return 0;
4423 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004424 ep = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4426 return 0;
4427}
4428
4429/*
4430 * Returns whether the selected fork of the inode has exactly one
4431 * block or not. For the data fork we check this matches di_size,
4432 * implying the file's range is 0..bsize-1.
4433 */
4434int /* 1=>1 block, 0=>otherwise */
4435xfs_bmap_one_block(
4436 xfs_inode_t *ip, /* incore inode */
4437 int whichfork) /* data or attr fork */
4438{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004439 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 xfs_ifork_t *ifp; /* inode fork pointer */
4441 int rval; /* return value */
4442 xfs_bmbt_irec_t s; /* internal version of extent */
4443
4444#ifndef DEBUG
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004445 if (whichfork == XFS_DATA_FORK) {
4446 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4447 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4448 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450#endif /* !DEBUG */
4451 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4452 return 0;
4453 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4454 return 0;
4455 ifp = XFS_IFORK_PTR(ip, whichfork);
4456 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004457 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 xfs_bmbt_get_all(ep, &s);
4459 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4460 if (rval && whichfork == XFS_DATA_FORK)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004461 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 return rval;
4463}
4464
4465/*
4466 * Read in the extents to if_extents.
4467 * All inode fields are set up by caller, we just traverse the btree
4468 * and copy the records in. If the file system cannot contain unwritten
4469 * extents, the records are checked for no "state" flags.
4470 */
4471int /* error */
4472xfs_bmap_read_extents(
4473 xfs_trans_t *tp, /* transaction pointer */
4474 xfs_inode_t *ip, /* incore inode */
4475 int whichfork) /* data or attr fork */
4476{
4477 xfs_bmbt_block_t *block; /* current btree block */
4478 xfs_fsblock_t bno; /* block # of "block" */
4479 xfs_buf_t *bp; /* buffer for "block" */
4480 int error; /* error return value */
4481 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 xfs_extnum_t i, j; /* index into the extents list */
4483 xfs_ifork_t *ifp; /* fork structure */
4484 int level; /* btree level, for checking */
4485 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10004486 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 /* REFERENCED */
4488 xfs_extnum_t room; /* number of entries there's room for */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
4490 bno = NULLFSBLOCK;
4491 mp = ip->i_mount;
4492 ifp = XFS_IFORK_PTR(ip, whichfork);
4493 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4494 XFS_EXTFMT_INODE(ip);
4495 block = ifp->if_broot;
4496 /*
4497 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4498 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004499 level = be16_to_cpu(block->bb_level);
4500 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004502 bno = be64_to_cpu(*pp);
4503 ASSERT(bno != NULLDFSBNO);
4504 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4505 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 /*
4507 * Go down the tree until leaf level is reached, following the first
4508 * pointer (leftmost) at each level.
4509 */
4510 while (level-- > 0) {
4511 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4512 XFS_BMAP_BTREE_REF)))
4513 return error;
4514 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4515 XFS_WANT_CORRUPTED_GOTO(
4516 XFS_BMAP_SANITY_CHECK(mp, block, level),
4517 error0);
4518 if (level == 0)
4519 break;
Eric Sandeen2c36dde2007-02-10 18:37:33 +11004520 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004521 bno = be64_to_cpu(*pp);
4522 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 xfs_trans_brelse(tp, bp);
4524 }
4525 /*
4526 * Here with bp and block set to the leftmost leaf node in the tree.
4527 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004528 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 i = 0;
4530 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004531 * Loop over all leaf nodes. Copy information to the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 */
4533 for (;;) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004534 xfs_bmbt_rec_t *frp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 xfs_fsblock_t nextbno;
4536 xfs_extnum_t num_recs;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004537 xfs_extnum_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538
4539
Christoph Hellwig16259e72005-11-02 15:11:25 +11004540 num_recs = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 if (unlikely(i + num_recs > room)) {
4542 ASSERT(i + num_recs <= room);
Nathan Scott3762ec62006-01-12 10:29:53 +11004543 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4544 "corrupt dinode %Lu, (btree extents).",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 (unsigned long long) ip->i_ino);
4546 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4547 XFS_ERRLEVEL_LOW,
4548 ip->i_mount);
4549 goto error0;
4550 }
4551 XFS_WANT_CORRUPTED_GOTO(
4552 XFS_BMAP_SANITY_CHECK(mp, block, 0),
4553 error0);
4554 /*
4555 * Read-ahead the next leaf block, if any.
4556 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004557 nextbno = be64_to_cpu(block->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 if (nextbno != NULLFSBLOCK)
4559 xfs_btree_reada_bufl(mp, nextbno, 1);
4560 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004561 * Copy records into the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 */
Eric Sandeen2c36dde2007-02-10 18:37:33 +11004563 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004564 start = i;
4565 for (j = 0; j < num_recs; j++, i++, frp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004566 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10004567 trp->l0 = be64_to_cpu(frp->l0);
4568 trp->l1 = be64_to_cpu(frp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 }
4570 if (exntf == XFS_EXTFMT_NOSTATE) {
4571 /*
4572 * Check all attribute bmap btree records and
4573 * any "older" data bmap btree records for a
4574 * set bit in the "extent flag" position.
4575 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004576 if (unlikely(xfs_check_nostate_extents(ifp,
4577 start, num_recs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4579 XFS_ERRLEVEL_LOW,
4580 ip->i_mount);
4581 goto error0;
4582 }
4583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 xfs_trans_brelse(tp, bp);
4585 bno = nextbno;
4586 /*
4587 * If we've reached the end, stop.
4588 */
4589 if (bno == NULLFSBLOCK)
4590 break;
4591 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4592 XFS_BMAP_BTREE_REF)))
4593 return error;
4594 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4595 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004596 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
Eric Sandeen3a59c942007-07-11 11:09:47 +10004598 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 return 0;
4600error0:
4601 xfs_trans_brelse(tp, bp);
4602 return XFS_ERROR(EFSCORRUPTED);
4603}
4604
4605#ifdef XFS_BMAP_TRACE
4606/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004607 * Add bmap trace insert entries for all the contents of the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 */
4609void
4610xfs_bmap_trace_exlist(
Eric Sandeen3a59c942007-07-11 11:09:47 +10004611 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 xfs_inode_t *ip, /* incore inode pointer */
4613 xfs_extnum_t cnt, /* count of entries in the list */
4614 int whichfork) /* data or attr fork */
4615{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004616 xfs_bmbt_rec_host_t *ep; /* current extent record */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004617 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 xfs_ifork_t *ifp; /* inode fork pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004619 xfs_bmbt_irec_t s; /* file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
4621 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004622 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4623 for (idx = 0; idx < cnt; idx++) {
4624 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 xfs_bmbt_get_all(ep, &s);
Eric Sandeen3a59c942007-07-11 11:09:47 +10004626 XFS_BMAP_TRACE_INSERT("exlist", ip, idx, 1, &s, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 whichfork);
4628 }
4629}
4630#endif
4631
4632#ifdef DEBUG
4633/*
4634 * Validate that the bmbt_irecs being returned from bmapi are valid
4635 * given the callers original parameters. Specifically check the
4636 * ranges of the returned irecs to ensure that they only extent beyond
4637 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4638 */
4639STATIC void
4640xfs_bmap_validate_ret(
4641 xfs_fileoff_t bno,
4642 xfs_filblks_t len,
4643 int flags,
4644 xfs_bmbt_irec_t *mval,
4645 int nmap,
4646 int ret_nmap)
4647{
4648 int i; /* index to map values */
4649
4650 ASSERT(ret_nmap <= nmap);
4651
4652 for (i = 0; i < ret_nmap; i++) {
4653 ASSERT(mval[i].br_blockcount > 0);
4654 if (!(flags & XFS_BMAPI_ENTIRE)) {
4655 ASSERT(mval[i].br_startoff >= bno);
4656 ASSERT(mval[i].br_blockcount <= len);
4657 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4658 bno + len);
4659 } else {
4660 ASSERT(mval[i].br_startoff < bno + len);
4661 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4662 bno);
4663 }
4664 ASSERT(i == 0 ||
4665 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4666 mval[i].br_startoff);
4667 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4668 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4669 mval[i].br_startblock != HOLESTARTBLOCK);
4670 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4671 mval[i].br_state == XFS_EXT_UNWRITTEN);
4672 }
4673}
4674#endif /* DEBUG */
4675
4676
4677/*
4678 * Map file blocks to filesystem blocks.
4679 * File range is given by the bno/len pair.
4680 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4681 * into a hole or past eof.
4682 * Only allocates blocks from a single allocation group,
4683 * to avoid locking problems.
4684 * The returned value in "firstblock" from the first call in a transaction
4685 * must be remembered and presented to subsequent calls in "firstblock".
4686 * An upper bound for the number of blocks to be allocated is supplied to
4687 * the first call in "total"; if no allocation group has that many free
4688 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4689 */
4690int /* error */
4691xfs_bmapi(
4692 xfs_trans_t *tp, /* transaction pointer */
4693 xfs_inode_t *ip, /* incore inode */
4694 xfs_fileoff_t bno, /* starting file offs. mapped */
4695 xfs_filblks_t len, /* length to map in file */
4696 int flags, /* XFS_BMAPI_... */
4697 xfs_fsblock_t *firstblock, /* first allocated block
4698 controls a.g. for allocs */
4699 xfs_extlen_t total, /* total blocks needed */
4700 xfs_bmbt_irec_t *mval, /* output: map values */
4701 int *nmap, /* i/o: mval size/count */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004702 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4703 xfs_extdelta_t *delta) /* o: change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704{
4705 xfs_fsblock_t abno; /* allocated block number */
4706 xfs_extlen_t alen; /* allocated extent length */
4707 xfs_fileoff_t aoff; /* allocated file offset */
4708 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 xfs_btree_cur_t *cur; /* bmap btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 xfs_fileoff_t end; /* end of mapped file region */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004711 int eof; /* we've hit the end of extents */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004712 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004714 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 xfs_ifork_t *ifp; /* inode fork pointer */
4716 xfs_extlen_t indlen; /* indirect blocks length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 xfs_extnum_t lastx; /* last useful extent number */
4718 int logflags; /* flags for transaction logging */
4719 xfs_extlen_t minleft; /* min blocks left after allocation */
4720 xfs_extlen_t minlen; /* min allocation size */
4721 xfs_mount_t *mp; /* xfs mount structure */
4722 int n; /* current extent index */
4723 int nallocs; /* number of extents alloc\'d */
4724 xfs_extnum_t nextents; /* number of extents in file */
4725 xfs_fileoff_t obno; /* old block number (offset) */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004726 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 int tmp_logflags; /* temp flags holder */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004728 int whichfork; /* data or attr fork */
4729 char inhole; /* current location is hole in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 char wasdelay; /* old extent was delayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 char wr; /* this is a write request */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004732 char rt; /* this is a realtime file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733#ifdef DEBUG
4734 xfs_fileoff_t orig_bno; /* original block number value */
4735 int orig_flags; /* original flags arg value */
4736 xfs_filblks_t orig_len; /* original value of len arg */
4737 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4738 int orig_nmap; /* original value of *nmap */
4739
4740 orig_bno = bno;
4741 orig_len = len;
4742 orig_flags = flags;
4743 orig_mval = mval;
4744 orig_nmap = *nmap;
4745#endif
4746 ASSERT(*nmap >= 1);
4747 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4748 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4749 XFS_ATTR_FORK : XFS_DATA_FORK;
4750 mp = ip->i_mount;
4751 if (unlikely(XFS_TEST_ERROR(
4752 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4753 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4754 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4755 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4756 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4757 return XFS_ERROR(EFSCORRUPTED);
4758 }
4759 if (XFS_FORCED_SHUTDOWN(mp))
4760 return XFS_ERROR(EIO);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004761 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 ifp = XFS_IFORK_PTR(ip, whichfork);
4763 ASSERT(ifp->if_ext_max ==
4764 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4765 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4766 XFS_STATS_INC(xs_blk_mapw);
4767 else
4768 XFS_STATS_INC(xs_blk_mapr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 /*
Nathan Scott39269e22006-03-14 13:33:50 +11004770 * IGSTATE flag is used to combine extents which
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 * differ only due to the state of the extents.
4772 * This technique is used from xfs_getbmap()
4773 * when the caller does not wish to see the
4774 * separation (which is the default).
4775 *
4776 * This technique is also used when writing a
4777 * buffer which has been partially written,
4778 * (usually by being flushed during a chunkread),
4779 * to ensure one write takes place. This also
4780 * prevents a change in the xfs inode extents at
4781 * this time, intentionally. This change occurs
4782 * on completion of the write operation, in
4783 * xfs_strat_comp(), where the xfs_bmapi() call
4784 * is transactioned, and the extents combined.
4785 */
Nathan Scott39269e22006-03-14 13:33:50 +11004786 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4787 wr = 0; /* no allocations are allowed */
4788 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 logflags = 0;
4790 nallocs = 0;
4791 cur = NULL;
4792 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4793 ASSERT(wr && tp);
4794 if ((error = xfs_bmap_local_to_extents(tp, ip,
4795 firstblock, total, &logflags, whichfork)))
4796 goto error0;
4797 }
4798 if (wr && *firstblock == NULLFSBLOCK) {
4799 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Christoph Hellwig16259e72005-11-02 15:11:25 +11004800 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 else
4802 minleft = 1;
4803 } else
4804 minleft = 0;
4805 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4806 (error = xfs_iread_extents(tp, ip, whichfork)))
4807 goto error0;
4808 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4809 &prev);
4810 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4811 n = 0;
4812 end = bno + len;
4813 obno = bno;
4814 bma.ip = NULL;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004815 if (delta) {
4816 delta->xed_startoff = NULLFILEOFF;
4817 delta->xed_blockcount = 0;
4818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 while (bno < end && n < *nmap) {
4820 /*
4821 * Reading past eof, act as though there's a hole
4822 * up to end.
4823 */
4824 if (eof && !wr)
4825 got.br_startoff = end;
4826 inhole = eof || got.br_startoff > bno;
Nathan Scott39269e22006-03-14 13:33:50 +11004827 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 ISNULLSTARTBLOCK(got.br_startblock);
4829 /*
4830 * First, deal with the hole before the allocated space
4831 * that we found, if any.
4832 */
4833 if (wr && (inhole || wasdelay)) {
4834 /*
4835 * For the wasdelay case, we could also just
4836 * allocate the stuff asked for in this bmap call
4837 * but that wouldn't be as good.
4838 */
Nathan Scott39269e22006-03-14 13:33:50 +11004839 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 alen = (xfs_extlen_t)got.br_blockcount;
4841 aoff = got.br_startoff;
4842 if (lastx != NULLEXTNUM && lastx) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004843 ep = xfs_iext_get_ext(ifp, lastx - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 xfs_bmbt_get_all(ep, &prev);
4845 }
4846 } else if (wasdelay) {
4847 alen = (xfs_extlen_t)
4848 XFS_FILBLKS_MIN(len,
4849 (got.br_startoff +
4850 got.br_blockcount) - bno);
4851 aoff = bno;
4852 } else {
4853 alen = (xfs_extlen_t)
4854 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4855 if (!eof)
4856 alen = (xfs_extlen_t)
4857 XFS_FILBLKS_MIN(alen,
4858 got.br_startoff - bno);
4859 aoff = bno;
4860 }
Nathan Scott39269e22006-03-14 13:33:50 +11004861 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4862 if (flags & XFS_BMAPI_DELAY) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11004863 xfs_extlen_t extsz;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004864
4865 /* Figure out the extent size, adjust alen */
David Chinner957d0eb2007-06-18 16:50:37 +10004866 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004867 if (extsz) {
4868 error = xfs_bmap_extsize_align(mp,
4869 &got, &prev, extsz,
Nathan Scott39269e22006-03-14 13:33:50 +11004870 rt, eof,
4871 flags&XFS_BMAPI_DELAY,
4872 flags&XFS_BMAPI_CONVERT,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004873 &aoff, &alen);
4874 ASSERT(!error);
4875 }
4876
4877 if (rt)
4878 extsz = alen / mp->m_sb.sb_rextsize;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004879
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 /*
4881 * Make a transaction-less quota reservation for
4882 * delayed allocation blocks. This number gets
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004883 * adjusted later. We return if we haven't
4884 * allocated blocks already inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 */
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004886 if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS(
Nathan Scott06d10dd2005-06-21 15:48:47 +10004887 mp, NULL, ip, (long)alen, 0,
4888 rt ? XFS_QMOPT_RES_RTBLKS :
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004889 XFS_QMOPT_RES_REGBLKS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 if (n == 0) {
4891 *nmap = 0;
4892 ASSERT(cur == NULL);
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004893 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 }
4895 break;
4896 }
4897
4898 /*
4899 * Split changing sb for alen and indlen since
4900 * they could be coming from different places.
4901 */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004902 indlen = (xfs_extlen_t)
4903 xfs_bmap_worst_indlen(ip, alen);
4904 ASSERT(indlen > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
Nathan Scottdd9f4382006-01-11 15:28:28 +11004906 if (rt) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004907 error = xfs_mod_incore_sb(mp,
4908 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004909 -((int64_t)extsz), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004910 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004911 } else {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004912 error = xfs_mod_incore_sb(mp,
4913 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004914 -((int64_t)alen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004915 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004916 }
David Chinner3bdbfb12005-09-02 16:40:47 +10004917 if (!error) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004918 error = xfs_mod_incore_sb(mp,
4919 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004920 -((int64_t)indlen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004921 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004922 if (error && rt)
4923 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004924 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004925 (int64_t)extsz, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004926 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004927 else if (error)
4928 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004929 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004930 (int64_t)alen, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004931 XFS_BMAPI_RSVBLOCKS));
David Chinner3bdbfb12005-09-02 16:40:47 +10004932 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004933
4934 if (error) {
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004935 if (XFS_IS_QUOTA_ON(mp))
Nathan Scott06d10dd2005-06-21 15:48:47 +10004936 /* unreserve the blocks now */
Nathan Scottdd9f4382006-01-11 15:28:28 +11004937 (void)
Nathan Scott06d10dd2005-06-21 15:48:47 +10004938 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4939 mp, NULL, ip,
4940 (long)alen, 0, rt ?
4941 XFS_QMOPT_RES_RTBLKS :
4942 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 break;
4944 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004945
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 ip->i_delayed_blks += alen;
4947 abno = NULLSTARTBLOCK(indlen);
4948 } else {
4949 /*
4950 * If first time, allocate and fill in
4951 * once-only bma fields.
4952 */
4953 if (bma.ip == NULL) {
4954 bma.tp = tp;
4955 bma.ip = ip;
4956 bma.prevp = &prev;
4957 bma.gotp = &got;
4958 bma.total = total;
4959 bma.userdata = 0;
4960 }
4961 /* Indicate if this is the first user data
4962 * in the file, or just any user data.
4963 */
Nathan Scott39269e22006-03-14 13:33:50 +11004964 if (!(flags & XFS_BMAPI_METADATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 bma.userdata = (aoff == 0) ?
4966 XFS_ALLOC_INITIAL_USER_DATA :
4967 XFS_ALLOC_USERDATA;
4968 }
4969 /*
4970 * Fill in changeable bma fields.
4971 */
4972 bma.eof = eof;
4973 bma.firstblock = *firstblock;
4974 bma.alen = alen;
4975 bma.off = aoff;
Adrian Bunk72880262006-08-30 13:41:58 +10004976 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 bma.wasdel = wasdelay;
4978 bma.minlen = minlen;
4979 bma.low = flist->xbf_low;
4980 bma.minleft = minleft;
4981 /*
4982 * Only want to do the alignment at the
4983 * eof if it is userdata and allocation length
4984 * is larger than a stripe unit.
4985 */
4986 if (mp->m_dalign && alen >= mp->m_dalign &&
Nathan Scott39269e22006-03-14 13:33:50 +11004987 (!(flags & XFS_BMAPI_METADATA)) &&
4988 (whichfork == XFS_DATA_FORK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 if ((error = xfs_bmap_isaeof(ip, aoff,
4990 whichfork, &bma.aeof)))
4991 goto error0;
4992 } else
4993 bma.aeof = 0;
4994 /*
4995 * Call allocator.
4996 */
4997 if ((error = xfs_bmap_alloc(&bma)))
4998 goto error0;
4999 /*
5000 * Copy out result fields.
5001 */
5002 abno = bma.rval;
5003 if ((flist->xbf_low = bma.low))
5004 minleft = 0;
5005 alen = bma.alen;
5006 aoff = bma.off;
5007 ASSERT(*firstblock == NULLFSBLOCK ||
5008 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5009 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
5010 (flist->xbf_low &&
5011 XFS_FSB_TO_AGNO(mp, *firstblock) <
5012 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
5013 *firstblock = bma.firstblock;
5014 if (cur)
5015 cur->bc_private.b.firstblock =
5016 *firstblock;
5017 if (abno == NULLFSBLOCK)
5018 break;
5019 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5020 cur = xfs_btree_init_cursor(mp,
5021 tp, NULL, 0, XFS_BTNUM_BMAP,
5022 ip, whichfork);
5023 cur->bc_private.b.firstblock =
5024 *firstblock;
5025 cur->bc_private.b.flist = flist;
5026 }
5027 /*
5028 * Bump the number of extents we've allocated
5029 * in this call.
5030 */
5031 nallocs++;
5032 }
5033 if (cur)
5034 cur->bc_private.b.flags =
5035 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
5036 got.br_startoff = aoff;
5037 got.br_startblock = abno;
5038 got.br_blockcount = alen;
5039 got.br_state = XFS_EXT_NORM; /* assume normal */
5040 /*
5041 * Determine state of extent, and the filesystem.
5042 * A wasdelay extent has been initialized, so
5043 * shouldn't be flagged as unwritten.
5044 */
Eric Sandeen62118702008-03-06 13:44:28 +11005045 if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5047 got.br_state = XFS_EXT_UNWRITTEN;
5048 }
5049 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005050 firstblock, flist, &tmp_logflags, delta,
5051 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 logflags |= tmp_logflags;
5053 if (error)
5054 goto error0;
5055 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005056 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5058 xfs_bmbt_get_all(ep, &got);
5059 ASSERT(got.br_startoff <= aoff);
5060 ASSERT(got.br_startoff + got.br_blockcount >=
5061 aoff + alen);
5062#ifdef DEBUG
Nathan Scott39269e22006-03-14 13:33:50 +11005063 if (flags & XFS_BMAPI_DELAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
5065 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
5066 }
5067 ASSERT(got.br_state == XFS_EXT_NORM ||
5068 got.br_state == XFS_EXT_UNWRITTEN);
5069#endif
5070 /*
5071 * Fall down into the found allocated space case.
5072 */
5073 } else if (inhole) {
5074 /*
5075 * Reading in a hole.
5076 */
5077 mval->br_startoff = bno;
5078 mval->br_startblock = HOLESTARTBLOCK;
5079 mval->br_blockcount =
5080 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5081 mval->br_state = XFS_EXT_NORM;
5082 bno += mval->br_blockcount;
5083 len -= mval->br_blockcount;
5084 mval++;
5085 n++;
5086 continue;
5087 }
5088 /*
5089 * Then deal with the allocated space we found.
5090 */
5091 ASSERT(ep != NULL);
Nathan Scott39269e22006-03-14 13:33:50 +11005092 if (!(flags & XFS_BMAPI_ENTIRE) &&
5093 (got.br_startoff + got.br_blockcount > obno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 if (obno > bno)
5095 bno = obno;
5096 ASSERT((bno >= obno) || (n == 0));
5097 ASSERT(bno < end);
5098 mval->br_startoff = bno;
5099 if (ISNULLSTARTBLOCK(got.br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11005100 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 mval->br_startblock = DELAYSTARTBLOCK;
5102 } else
5103 mval->br_startblock =
5104 got.br_startblock +
5105 (bno - got.br_startoff);
5106 /*
5107 * Return the minimum of what we got and what we
5108 * asked for for the length. We can use the len
5109 * variable here because it is modified below
5110 * and we could have been there before coming
5111 * here if the first part of the allocation
5112 * didn't overlap what was asked for.
5113 */
5114 mval->br_blockcount =
5115 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5116 (bno - got.br_startoff));
5117 mval->br_state = got.br_state;
5118 ASSERT(mval->br_blockcount <= len);
5119 } else {
5120 *mval = got;
5121 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11005122 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 mval->br_startblock = DELAYSTARTBLOCK;
5124 }
5125 }
5126
5127 /*
5128 * Check if writing previously allocated but
5129 * unwritten extents.
5130 */
5131 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5132 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5133 /*
5134 * Modify (by adding) the state flag, if writing.
5135 */
5136 ASSERT(mval->br_blockcount <= len);
5137 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5138 cur = xfs_btree_init_cursor(mp,
5139 tp, NULL, 0, XFS_BTNUM_BMAP,
5140 ip, whichfork);
5141 cur->bc_private.b.firstblock =
5142 *firstblock;
5143 cur->bc_private.b.flist = flist;
5144 }
5145 mval->br_state = XFS_EXT_NORM;
5146 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005147 firstblock, flist, &tmp_logflags, delta,
5148 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 logflags |= tmp_logflags;
5150 if (error)
5151 goto error0;
5152 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005153 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5155 xfs_bmbt_get_all(ep, &got);
5156 /*
5157 * We may have combined previously unwritten
5158 * space with written space, so generate
5159 * another request.
5160 */
5161 if (mval->br_blockcount < len)
5162 continue;
5163 }
5164
Nathan Scott39269e22006-03-14 13:33:50 +11005165 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 ((mval->br_startoff + mval->br_blockcount) <= end));
Nathan Scott39269e22006-03-14 13:33:50 +11005167 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5168 (mval->br_blockcount <= len) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 (mval->br_startoff < obno));
5170 bno = mval->br_startoff + mval->br_blockcount;
5171 len = end - bno;
5172 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5173 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5174 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5175 ASSERT(mval->br_state == mval[-1].br_state);
5176 mval[-1].br_blockcount = mval->br_blockcount;
5177 mval[-1].br_state = mval->br_state;
5178 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5179 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5180 mval[-1].br_startblock != HOLESTARTBLOCK &&
5181 mval->br_startblock ==
5182 mval[-1].br_startblock + mval[-1].br_blockcount &&
Nathan Scott39269e22006-03-14 13:33:50 +11005183 ((flags & XFS_BMAPI_IGSTATE) ||
5184 mval[-1].br_state == mval->br_state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 ASSERT(mval->br_startoff ==
5186 mval[-1].br_startoff + mval[-1].br_blockcount);
5187 mval[-1].br_blockcount += mval->br_blockcount;
5188 } else if (n > 0 &&
5189 mval->br_startblock == DELAYSTARTBLOCK &&
5190 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5191 mval->br_startoff ==
5192 mval[-1].br_startoff + mval[-1].br_blockcount) {
5193 mval[-1].br_blockcount += mval->br_blockcount;
5194 mval[-1].br_state = mval->br_state;
5195 } else if (!((n == 0) &&
5196 ((mval->br_startoff + mval->br_blockcount) <=
5197 obno))) {
5198 mval++;
5199 n++;
5200 }
5201 /*
5202 * If we're done, stop now. Stop when we've allocated
5203 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5204 * the transaction may get too big.
5205 */
5206 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5207 break;
5208 /*
5209 * Else go on to the next record.
5210 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005211 ep = xfs_iext_get_ext(ifp, ++lastx);
David Chinner4e5ae832007-06-05 16:24:15 +10005212 prev = got;
5213 if (lastx >= nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 eof = 1;
David Chinner4e5ae832007-06-05 16:24:15 +10005215 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 xfs_bmbt_get_all(ep, &got);
5217 }
5218 ifp->if_lastex = lastx;
5219 *nmap = n;
5220 /*
5221 * Transform from btree to extents, give it cur.
5222 */
5223 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5224 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5225 ASSERT(wr && cur);
5226 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5227 &tmp_logflags, whichfork);
5228 logflags |= tmp_logflags;
5229 if (error)
5230 goto error0;
5231 }
5232 ASSERT(ifp->if_ext_max ==
5233 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5234 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5235 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5236 error = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005237 if (delta && delta->xed_startoff != NULLFILEOFF) {
5238 /* A change was actually made.
5239 * Note that delta->xed_blockount is an offset at this
5240 * point and needs to be converted to a block count.
5241 */
5242 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5243 delta->xed_blockcount -= delta->xed_startoff;
5244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245error0:
5246 /*
5247 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005248 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 */
5250 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5251 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5252 logflags &= ~XFS_ILOG_FEXT(whichfork);
5253 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5254 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5255 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5256 /*
5257 * Log whatever the flags say, even if error. Otherwise we might miss
5258 * detecting a case where the data is changed, there's an error,
5259 * and it's not logged so we don't shutdown when we should.
5260 */
5261 if (logflags) {
5262 ASSERT(tp && wr);
5263 xfs_trans_log_inode(tp, ip, logflags);
5264 }
5265 if (cur) {
5266 if (!error) {
5267 ASSERT(*firstblock == NULLFSBLOCK ||
5268 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5269 XFS_FSB_TO_AGNO(mp,
5270 cur->bc_private.b.firstblock) ||
5271 (flist->xbf_low &&
5272 XFS_FSB_TO_AGNO(mp, *firstblock) <
5273 XFS_FSB_TO_AGNO(mp,
5274 cur->bc_private.b.firstblock)));
5275 *firstblock = cur->bc_private.b.firstblock;
5276 }
5277 xfs_btree_del_cursor(cur,
5278 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5279 }
5280 if (!error)
5281 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5282 orig_nmap, *nmap);
5283 return error;
5284}
5285
5286/*
5287 * Map file blocks to filesystem blocks, simple version.
5288 * One block (extent) only, read-only.
5289 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5290 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5291 * was set and all the others were clear.
5292 */
5293int /* error */
5294xfs_bmapi_single(
5295 xfs_trans_t *tp, /* transaction pointer */
5296 xfs_inode_t *ip, /* incore inode */
5297 int whichfork, /* data or attr fork */
5298 xfs_fsblock_t *fsb, /* output: mapped block */
5299 xfs_fileoff_t bno) /* starting file offs. mapped */
5300{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005301 int eof; /* we've hit the end of extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005303 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 xfs_ifork_t *ifp; /* inode fork pointer */
5305 xfs_extnum_t lastx; /* last useful extent number */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005306 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307
5308 ifp = XFS_IFORK_PTR(ip, whichfork);
5309 if (unlikely(
5310 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5311 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5312 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5313 ip->i_mount);
5314 return XFS_ERROR(EFSCORRUPTED);
5315 }
5316 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5317 return XFS_ERROR(EIO);
5318 XFS_STATS_INC(xs_blk_mapr);
5319 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5320 (error = xfs_iread_extents(tp, ip, whichfork)))
5321 return error;
5322 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5323 &prev);
5324 /*
5325 * Reading past eof, act as though there's a hole
5326 * up to end.
5327 */
5328 if (eof || got.br_startoff > bno) {
5329 *fsb = NULLFSBLOCK;
5330 return 0;
5331 }
5332 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
5333 ASSERT(bno < got.br_startoff + got.br_blockcount);
5334 *fsb = got.br_startblock + (bno - got.br_startoff);
5335 ifp->if_lastex = lastx;
5336 return 0;
5337}
5338
5339/*
5340 * Unmap (remove) blocks from a file.
5341 * If nexts is nonzero then the number of extents to remove is limited to
5342 * that value. If not all extents in the block range can be removed then
5343 * *done is set.
5344 */
5345int /* error */
5346xfs_bunmapi(
5347 xfs_trans_t *tp, /* transaction pointer */
5348 struct xfs_inode *ip, /* incore inode */
5349 xfs_fileoff_t bno, /* starting offset to unmap */
5350 xfs_filblks_t len, /* length to unmap in file */
5351 int flags, /* misc flags */
5352 xfs_extnum_t nexts, /* number of extents max */
5353 xfs_fsblock_t *firstblock, /* first allocated block
5354 controls a.g. for allocs */
5355 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005356 xfs_extdelta_t *delta, /* o: change made to incore
5357 extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 int *done) /* set if not done yet */
5359{
5360 xfs_btree_cur_t *cur; /* bmap btree cursor */
5361 xfs_bmbt_irec_t del; /* extent being deleted */
5362 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005363 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 int error; /* error return value */
5365 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005366 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 xfs_ifork_t *ifp; /* inode fork pointer */
5368 int isrt; /* freeing in rt area */
5369 xfs_extnum_t lastx; /* last extent index used */
5370 int logflags; /* transaction logging flags */
5371 xfs_extlen_t mod; /* rt extent offset */
5372 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005373 xfs_extnum_t nextents; /* number of file extents */
5374 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 xfs_fileoff_t start; /* first file offset deleted */
5376 int tmp_logflags; /* partial logging flags */
5377 int wasdel; /* was a delayed alloc extent */
5378 int whichfork; /* data or attribute fork */
5379 int rsvd; /* OK to allocate reserved blocks */
5380 xfs_fsblock_t sum;
5381
5382 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5383 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5384 XFS_ATTR_FORK : XFS_DATA_FORK;
5385 ifp = XFS_IFORK_PTR(ip, whichfork);
5386 if (unlikely(
5387 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5388 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5389 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5390 ip->i_mount);
5391 return XFS_ERROR(EFSCORRUPTED);
5392 }
5393 mp = ip->i_mount;
5394 if (XFS_FORCED_SHUTDOWN(mp))
5395 return XFS_ERROR(EIO);
5396 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5397 ASSERT(len > 0);
5398 ASSERT(nexts >= 0);
5399 ASSERT(ifp->if_ext_max ==
5400 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5401 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5402 (error = xfs_iread_extents(tp, ip, whichfork)))
5403 return error;
5404 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5405 if (nextents == 0) {
5406 *done = 1;
5407 return 0;
5408 }
5409 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005410 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 start = bno;
5412 bno = start + len - 1;
5413 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5414 &prev);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005415 if (delta) {
5416 delta->xed_startoff = NULLFILEOFF;
5417 delta->xed_blockcount = 0;
5418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 /*
5420 * Check to see if the given block number is past the end of the
5421 * file, back up to the last block if so...
5422 */
5423 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005424 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 xfs_bmbt_get_all(ep, &got);
5426 bno = got.br_startoff + got.br_blockcount - 1;
5427 }
5428 logflags = 0;
5429 if (ifp->if_flags & XFS_IFBROOT) {
5430 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5431 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
5432 whichfork);
5433 cur->bc_private.b.firstblock = *firstblock;
5434 cur->bc_private.b.flist = flist;
5435 cur->bc_private.b.flags = 0;
5436 } else
5437 cur = NULL;
5438 extno = 0;
5439 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5440 (nexts == 0 || extno < nexts)) {
5441 /*
5442 * Is the found extent after a hole in which bno lives?
5443 * Just back up to the previous extent, if so.
5444 */
5445 if (got.br_startoff > bno) {
5446 if (--lastx < 0)
5447 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005448 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 xfs_bmbt_get_all(ep, &got);
5450 }
5451 /*
5452 * Is the last block of this extent before the range
5453 * we're supposed to delete? If so, we're done.
5454 */
5455 bno = XFS_FILEOFF_MIN(bno,
5456 got.br_startoff + got.br_blockcount - 1);
5457 if (bno < start)
5458 break;
5459 /*
5460 * Then deal with the (possibly delayed) allocated space
5461 * we found.
5462 */
5463 ASSERT(ep != NULL);
5464 del = got;
5465 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
5466 if (got.br_startoff < start) {
5467 del.br_startoff = start;
5468 del.br_blockcount -= start - got.br_startoff;
5469 if (!wasdel)
5470 del.br_startblock += start - got.br_startoff;
5471 }
5472 if (del.br_startoff + del.br_blockcount > bno + 1)
5473 del.br_blockcount = bno + 1 - del.br_startoff;
5474 sum = del.br_startblock + del.br_blockcount;
5475 if (isrt &&
5476 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5477 /*
5478 * Realtime extent not lined up at the end.
5479 * The extent could have been split into written
5480 * and unwritten pieces, or we could just be
5481 * unmapping part of it. But we can't really
5482 * get rid of part of a realtime extent.
5483 */
5484 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005485 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 /*
5487 * This piece is unwritten, or we're not
5488 * using unwritten extents. Skip over it.
5489 */
5490 ASSERT(bno >= mod);
5491 bno -= mod > del.br_blockcount ?
5492 del.br_blockcount : mod;
5493 if (bno < got.br_startoff) {
5494 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005495 xfs_bmbt_get_all(xfs_iext_get_ext(
5496 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 }
5498 continue;
5499 }
5500 /*
5501 * It's written, turn it unwritten.
5502 * This is better than zeroing it.
5503 */
5504 ASSERT(del.br_state == XFS_EXT_NORM);
5505 ASSERT(xfs_trans_get_block_res(tp) > 0);
5506 /*
5507 * If this spans a realtime extent boundary,
5508 * chop it back to the start of the one we end at.
5509 */
5510 if (del.br_blockcount > mod) {
5511 del.br_startoff += del.br_blockcount - mod;
5512 del.br_startblock += del.br_blockcount - mod;
5513 del.br_blockcount = mod;
5514 }
5515 del.br_state = XFS_EXT_UNWRITTEN;
5516 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005517 firstblock, flist, &logflags, delta,
5518 XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 if (error)
5520 goto error0;
5521 goto nodelete;
5522 }
5523 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5524 /*
5525 * Realtime extent is lined up at the end but not
5526 * at the front. We'll get rid of full extents if
5527 * we can.
5528 */
5529 mod = mp->m_sb.sb_rextsize - mod;
5530 if (del.br_blockcount > mod) {
5531 del.br_blockcount -= mod;
5532 del.br_startoff += mod;
5533 del.br_startblock += mod;
5534 } else if ((del.br_startoff == start &&
5535 (del.br_state == XFS_EXT_UNWRITTEN ||
5536 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005537 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 /*
5539 * Can't make it unwritten. There isn't
5540 * a full extent here so just skip it.
5541 */
5542 ASSERT(bno >= del.br_blockcount);
5543 bno -= del.br_blockcount;
5544 if (bno < got.br_startoff) {
5545 if (--lastx >= 0)
5546 xfs_bmbt_get_all(--ep, &got);
5547 }
5548 continue;
5549 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5550 /*
5551 * This one is already unwritten.
5552 * It must have a written left neighbor.
5553 * Unwrite the killed part of that one and
5554 * try again.
5555 */
5556 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005557 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5558 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 ASSERT(prev.br_state == XFS_EXT_NORM);
5560 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
5561 ASSERT(del.br_startblock ==
5562 prev.br_startblock + prev.br_blockcount);
5563 if (prev.br_startoff < start) {
5564 mod = start - prev.br_startoff;
5565 prev.br_blockcount -= mod;
5566 prev.br_startblock += mod;
5567 prev.br_startoff = start;
5568 }
5569 prev.br_state = XFS_EXT_UNWRITTEN;
5570 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5571 &prev, firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005572 delta, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 if (error)
5574 goto error0;
5575 goto nodelete;
5576 } else {
5577 ASSERT(del.br_state == XFS_EXT_NORM);
5578 del.br_state = XFS_EXT_UNWRITTEN;
5579 error = xfs_bmap_add_extent(ip, lastx, &cur,
5580 &del, firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005581 delta, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 if (error)
5583 goto error0;
5584 goto nodelete;
5585 }
5586 }
5587 if (wasdel) {
5588 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005589 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005590 if (isrt) {
5591 xfs_filblks_t rtexts;
5592
5593 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5594 do_div(rtexts, mp->m_sb.sb_rextsize);
5595 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005596 (int64_t)rtexts, rsvd);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005597 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5598 NULL, ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005599 XFS_QMOPT_RES_RTBLKS);
5600 } else {
5601 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005602 (int64_t)del.br_blockcount, rsvd);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005603 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5604 NULL, ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 ip->i_delayed_blks -= del.br_blockcount;
5608 if (cur)
5609 cur->bc_private.b.flags |=
5610 XFS_BTCUR_BPRV_WASDEL;
5611 } else if (cur)
5612 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5613 /*
5614 * If it's the case where the directory code is running
5615 * with no block reservation, and the deleted block is in
5616 * the middle of its extent, and the resulting insert
5617 * of an extent would cause transformation to btree format,
5618 * then reject it. The calling code will then swap
5619 * blocks around instead.
5620 * We have to do this now, rather than waiting for the
5621 * conversion to btree format, since the transaction
5622 * will be dirty.
5623 */
5624 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5625 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5626 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5627 del.br_startoff > got.br_startoff &&
5628 del.br_startoff + del.br_blockcount <
5629 got.br_startoff + got.br_blockcount) {
5630 error = XFS_ERROR(ENOSPC);
5631 goto error0;
5632 }
5633 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005634 &tmp_logflags, delta, whichfork, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 logflags |= tmp_logflags;
5636 if (error)
5637 goto error0;
5638 bno = del.br_startoff - 1;
5639nodelete:
5640 lastx = ifp->if_lastex;
5641 /*
5642 * If not done go on to the next (previous) record.
5643 * Reset ep in case the extents array was re-alloced.
5644 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005645 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5647 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5648 xfs_bmbt_get_startoff(ep) > bno) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005649 if (--lastx >= 0)
5650 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 }
5652 if (lastx >= 0)
5653 xfs_bmbt_get_all(ep, &got);
5654 extno++;
5655 }
5656 }
5657 ifp->if_lastex = lastx;
5658 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5659 ASSERT(ifp->if_ext_max ==
5660 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5661 /*
5662 * Convert to a btree if necessary.
5663 */
5664 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5665 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5666 ASSERT(cur == NULL);
5667 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5668 &cur, 0, &tmp_logflags, whichfork);
5669 logflags |= tmp_logflags;
5670 if (error)
5671 goto error0;
5672 }
5673 /*
5674 * transform from btree to extents, give it cur
5675 */
5676 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5677 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5678 ASSERT(cur != NULL);
5679 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5680 whichfork);
5681 logflags |= tmp_logflags;
5682 if (error)
5683 goto error0;
5684 }
5685 /*
5686 * transform from extents to local?
5687 */
5688 ASSERT(ifp->if_ext_max ==
5689 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5690 error = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005691 if (delta && delta->xed_startoff != NULLFILEOFF) {
5692 /* A change was actually made.
5693 * Note that delta->xed_blockount is an offset at this
5694 * point and needs to be converted to a block count.
5695 */
5696 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5697 delta->xed_blockcount -= delta->xed_startoff;
5698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699error0:
5700 /*
5701 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005702 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 */
5704 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5705 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5706 logflags &= ~XFS_ILOG_FEXT(whichfork);
5707 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5708 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5709 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5710 /*
5711 * Log inode even in the error case, if the transaction
5712 * is dirty we'll need to shut down the filesystem.
5713 */
5714 if (logflags)
5715 xfs_trans_log_inode(tp, ip, logflags);
5716 if (cur) {
5717 if (!error) {
5718 *firstblock = cur->bc_private.b.firstblock;
5719 cur->bc_private.b.allocated = 0;
5720 }
5721 xfs_btree_del_cursor(cur,
5722 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5723 }
5724 return error;
5725}
5726
5727/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005728 * returns 1 for success, 0 if we failed to map the extent.
5729 */
5730STATIC int
5731xfs_getbmapx_fix_eof_hole(
5732 xfs_inode_t *ip, /* xfs incore inode pointer */
5733 struct getbmap *out, /* output structure */
5734 int prealloced, /* this is a file with
5735 * preallocated data space */
5736 __int64_t end, /* last block requested */
5737 xfs_fsblock_t startblock)
5738{
5739 __int64_t fixlen;
5740 xfs_mount_t *mp; /* file system mount point */
5741
5742 if (startblock == HOLESTARTBLOCK) {
5743 mp = ip->i_mount;
5744 out->bmv_block = -1;
5745 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5746 fixlen -= out->bmv_offset;
5747 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5748 /* Came to hole at EOF. Trim it. */
5749 if (fixlen <= 0)
5750 return 0;
5751 out->bmv_length = fixlen;
5752 }
5753 } else {
5754 out->bmv_block = XFS_FSB_TO_DB(ip, startblock);
5755 }
5756
5757 return 1;
5758}
5759
5760/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 * Fcntl interface to xfs_bmapi.
5762 */
5763int /* error code */
5764xfs_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10005765 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 struct getbmap *bmv, /* user bmap structure */
5767 void __user *ap, /* pointer to user's array */
5768 int interface) /* interface flags */
5769{
5770 __int64_t bmvend; /* last block requested */
5771 int error; /* return value */
5772 __int64_t fixlen; /* length for -1 case */
5773 int i; /* extent number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 int lock; /* lock state */
5775 xfs_bmbt_irec_t *map; /* buffer for user's data */
5776 xfs_mount_t *mp; /* file system mount point */
5777 int nex; /* # of user extents can do */
5778 int nexleft; /* # of user extents left */
5779 int subnex; /* # of bmapi's can do */
5780 int nmap; /* number of map entries */
5781 struct getbmap out; /* output structure */
5782 int whichfork; /* data or attr fork */
5783 int prealloced; /* this is a file with
5784 * preallocated data space */
5785 int sh_unwritten; /* true, if unwritten */
5786 /* extents listed separately */
5787 int bmapi_flags; /* flags for xfs_bmapi */
5788 __int32_t oflags; /* getbmapx bmv_oflags field */
5789
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 mp = ip->i_mount;
5791
5792 whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5793 sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
5794
5795 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5796 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5797 * bit is set for the file, generate a read event in order
5798 * that the DMAPI application may do its thing before we return
5799 * the extents. Usually this means restoring user file data to
5800 * regions of the file that look like holes.
5801 *
5802 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5803 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5804 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5805 * could misinterpret holes in a DMAPI file as true holes,
5806 * when in fact they may represent offline user data.
5807 */
Christoph Hellwigeb9df392007-08-16 18:42:07 +10005808 if ((interface & BMV_IF_NO_DMAPI_READ) == 0 &&
5809 DM_EVENT_ENABLED(ip, DM_EVENT_READ) &&
5810 whichfork == XFS_DATA_FORK) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11005811 error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, 0, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 if (error)
5813 return XFS_ERROR(error);
5814 }
5815
5816 if (whichfork == XFS_ATTR_FORK) {
5817 if (XFS_IFORK_Q(ip)) {
5818 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5819 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5820 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5821 return XFS_ERROR(EINVAL);
5822 } else if (unlikely(
5823 ip->i_d.di_aformat != 0 &&
5824 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5825 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5826 ip->i_mount);
5827 return XFS_ERROR(EFSCORRUPTED);
5828 }
5829 } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5830 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5831 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5832 return XFS_ERROR(EINVAL);
5833 if (whichfork == XFS_DATA_FORK) {
David Chinner957d0eb2007-06-18 16:50:37 +10005834 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005835 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 prealloced = 1;
5837 fixlen = XFS_MAXIOFFSET(mp);
5838 } else {
5839 prealloced = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005840 fixlen = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 }
5842 } else {
5843 prealloced = 0;
5844 fixlen = 1LL << 32;
5845 }
5846
5847 if (bmv->bmv_length == -1) {
5848 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5849 bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
5850 (__int64_t)0);
5851 } else if (bmv->bmv_length < 0)
5852 return XFS_ERROR(EINVAL);
5853 if (bmv->bmv_length == 0) {
5854 bmv->bmv_entries = 0;
5855 return 0;
5856 }
5857 nex = bmv->bmv_count - 1;
5858 if (nex <= 0)
5859 return XFS_ERROR(EINVAL);
5860 bmvend = bmv->bmv_offset + bmv->bmv_length;
5861
5862 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5863
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005864 if (whichfork == XFS_DATA_FORK &&
5865 (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
Christoph Hellwig739bfb22007-08-29 10:58:01 +10005867 error = xfs_flush_pages(ip, (xfs_off_t)0,
5868 -1, 0, FI_REMAPF);
Niv Sardie12070a2008-03-06 13:43:03 +11005869 if (error) {
5870 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5871 return error;
5872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 }
5874
5875 ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
5876
5877 lock = xfs_ilock_map_shared(ip);
5878
5879 /*
5880 * Don't let nex be bigger than the number of extents
5881 * we can have assuming alternating holes and real extents.
5882 */
5883 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5884 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5885
5886 bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
5887 ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
5888
5889 /*
5890 * Allocate enough space to handle "subnex" maps at a time.
5891 */
5892 subnex = 16;
5893 map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
5894
5895 bmv->bmv_entries = 0;
5896
5897 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
5898 error = 0;
5899 goto unlock_and_return;
5900 }
5901
5902 nexleft = nex;
5903
5904 do {
5905 nmap = (nexleft > subnex) ? subnex : nexleft;
5906 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5907 XFS_BB_TO_FSB(mp, bmv->bmv_length),
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005908 bmapi_flags, NULL, 0, map, &nmap,
5909 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 if (error)
5911 goto unlock_and_return;
5912 ASSERT(nmap <= subnex);
5913
5914 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5915 nexleft--;
5916 oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
5917 BMV_OF_PREALLOC : 0;
5918 out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
5919 out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5920 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
Yingping Lu9af0a702005-11-02 15:09:54 +11005921 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005922 whichfork == XFS_ATTR_FORK) {
5923 /* came to the end of attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 goto unlock_and_return;
5925 } else {
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005926 if (!xfs_getbmapx_fix_eof_hole(ip, &out,
5927 prealloced, bmvend,
5928 map[i].br_startblock)) {
5929 goto unlock_and_return;
5930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
5932 /* return either getbmap/getbmapx structure. */
5933 if (interface & BMV_IF_EXTENDED) {
5934 struct getbmapx outx;
5935
5936 GETBMAP_CONVERT(out,outx);
5937 outx.bmv_oflags = oflags;
5938 outx.bmv_unused1 = outx.bmv_unused2 = 0;
5939 if (copy_to_user(ap, &outx,
5940 sizeof(outx))) {
5941 error = XFS_ERROR(EFAULT);
5942 goto unlock_and_return;
5943 }
5944 } else {
5945 if (copy_to_user(ap, &out,
5946 sizeof(out))) {
5947 error = XFS_ERROR(EFAULT);
5948 goto unlock_and_return;
5949 }
5950 }
5951 bmv->bmv_offset =
5952 out.bmv_offset + out.bmv_length;
5953 bmv->bmv_length = MAX((__int64_t)0,
5954 (__int64_t)(bmvend - bmv->bmv_offset));
5955 bmv->bmv_entries++;
5956 ap = (interface & BMV_IF_EXTENDED) ?
5957 (void __user *)
5958 ((struct getbmapx __user *)ap + 1) :
5959 (void __user *)
5960 ((struct getbmap __user *)ap + 1);
5961 }
5962 }
5963 } while (nmap && nexleft && bmv->bmv_length);
5964
5965unlock_and_return:
5966 xfs_iunlock_map_shared(ip, lock);
5967 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5968
5969 kmem_free(map, subnex * sizeof(*map));
5970
5971 return error;
5972}
5973
5974/*
5975 * Check the last inode extent to determine whether this allocation will result
5976 * in blocks being allocated at the end of the file. When we allocate new data
5977 * blocks at the end of the file which do not start at the previous data block,
5978 * we will try to align the new blocks at stripe unit boundaries.
5979 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10005980STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981xfs_bmap_isaeof(
5982 xfs_inode_t *ip, /* incore inode pointer */
5983 xfs_fileoff_t off, /* file offset in fsblocks */
5984 int whichfork, /* data or attribute fork */
5985 char *aeof) /* return value */
5986{
5987 int error; /* error return value */
5988 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005989 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005990 xfs_extnum_t nextents; /* number of file extents */
5991 xfs_bmbt_irec_t s; /* expanded extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992
5993 ASSERT(whichfork == XFS_DATA_FORK);
5994 ifp = XFS_IFORK_PTR(ip, whichfork);
5995 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5996 (error = xfs_iread_extents(NULL, ip, whichfork)))
5997 return error;
5998 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5999 if (nextents == 0) {
6000 *aeof = 1;
6001 return 0;
6002 }
6003 /*
6004 * Go to the last extent
6005 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006006 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 xfs_bmbt_get_all(lastrec, &s);
6008 /*
6009 * Check we are allocating in the last extent (for delayed allocations)
6010 * or past the last extent for non-delayed allocations.
6011 */
6012 *aeof = (off >= s.br_startoff &&
6013 off < s.br_startoff + s.br_blockcount &&
6014 ISNULLSTARTBLOCK(s.br_startblock)) ||
6015 off >= s.br_startoff + s.br_blockcount;
6016 return 0;
6017}
6018
6019/*
6020 * Check if the endoff is outside the last extent. If so the caller will grow
6021 * the allocation to a stripe unit boundary.
6022 */
6023int /* error */
6024xfs_bmap_eof(
6025 xfs_inode_t *ip, /* incore inode pointer */
6026 xfs_fileoff_t endoff, /* file offset in fsblocks */
6027 int whichfork, /* data or attribute fork */
6028 int *eof) /* result value */
6029{
6030 xfs_fsblock_t blockcount; /* extent block count */
6031 int error; /* error return value */
6032 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006033 xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006034 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 xfs_fileoff_t startoff; /* extent starting file offset */
6036
6037 ASSERT(whichfork == XFS_DATA_FORK);
6038 ifp = XFS_IFORK_PTR(ip, whichfork);
6039 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6040 (error = xfs_iread_extents(NULL, ip, whichfork)))
6041 return error;
6042 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6043 if (nextents == 0) {
6044 *eof = 1;
6045 return 0;
6046 }
6047 /*
6048 * Go to the last extent
6049 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006050 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 startoff = xfs_bmbt_get_startoff(lastrec);
6052 blockcount = xfs_bmbt_get_blockcount(lastrec);
6053 *eof = endoff >= startoff + blockcount;
6054 return 0;
6055}
6056
6057#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058STATIC
6059xfs_buf_t *
6060xfs_bmap_get_bp(
6061 xfs_btree_cur_t *cur,
6062 xfs_fsblock_t bno)
6063{
6064 int i;
6065 xfs_buf_t *bp;
6066
6067 if (!cur)
6068 return(NULL);
6069
6070 bp = NULL;
6071 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6072 bp = cur->bc_bufs[i];
6073 if (!bp) break;
6074 if (XFS_BUF_ADDR(bp) == bno)
6075 break; /* Found it */
6076 }
6077 if (i == XFS_BTREE_MAXLEVELS)
6078 bp = NULL;
6079
6080 if (!bp) { /* Chase down all the log items to see if the bp is there */
6081 xfs_log_item_chunk_t *licp;
6082 xfs_trans_t *tp;
6083
6084 tp = cur->bc_tp;
6085 licp = &tp->t_items;
6086 while (!bp && licp != NULL) {
6087 if (XFS_LIC_ARE_ALL_FREE(licp)) {
6088 licp = licp->lic_next;
6089 continue;
6090 }
6091 for (i = 0; i < licp->lic_unused; i++) {
6092 xfs_log_item_desc_t *lidp;
6093 xfs_log_item_t *lip;
6094 xfs_buf_log_item_t *bip;
6095 xfs_buf_t *lbp;
6096
6097 if (XFS_LIC_ISFREE(licp, i)) {
6098 continue;
6099 }
6100
6101 lidp = XFS_LIC_SLOT(licp, i);
6102 lip = lidp->lid_item;
6103 if (lip->li_type != XFS_LI_BUF)
6104 continue;
6105
6106 bip = (xfs_buf_log_item_t *)lip;
6107 lbp = bip->bli_buf;
6108
6109 if (XFS_BUF_ADDR(lbp) == bno) {
6110 bp = lbp;
6111 break; /* Found it */
6112 }
6113 }
6114 licp = licp->lic_next;
6115 }
6116 }
6117 return(bp);
6118}
6119
6120void
6121xfs_check_block(
6122 xfs_bmbt_block_t *block,
6123 xfs_mount_t *mp,
6124 int root,
6125 short sz)
6126{
6127 int i, j, dmxr;
Christoph Hellwig576039c2006-09-28 10:58:06 +10006128 __be64 *pp, *thispa; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 xfs_bmbt_key_t *prevp, *keyp;
6130
Christoph Hellwig16259e72005-11-02 15:11:25 +11006131 ASSERT(be16_to_cpu(block->bb_level) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132
6133 prevp = NULL;
Christoph Hellwig16259e72005-11-02 15:11:25 +11006134 for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 dmxr = mp->m_bmap_dmxr[0];
6136
6137 if (root) {
6138 keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
6139 } else {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006140 keyp = XFS_BTREE_KEY_ADDR(xfs_bmbt, block, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 }
6142
6143 if (prevp) {
6144 xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
6145 }
6146 prevp = keyp;
6147
6148 /*
6149 * Compare the block numbers to see if there are dups.
6150 */
6151
6152 if (root) {
6153 pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
6154 } else {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006155 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, i, dmxr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11006157 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 if (root) {
6159 thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
6160 } else {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006161 thispa = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, j,
6162 dmxr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 }
Christoph Hellwig576039c2006-09-28 10:58:06 +10006164 if (*thispa == *pp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
Harvey Harrison34a622b2008-04-10 12:19:21 +10006166 __func__, j, i,
Christoph Hellwig576039c2006-09-28 10:58:06 +10006167 (unsigned long long)be64_to_cpu(*thispa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 panic("%s: ptrs are equal in node\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +10006169 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 }
6171 }
6172 }
6173}
6174
6175/*
6176 * Check that the extents for the inode ip are in the right order in all
6177 * btree leaves.
6178 */
6179
6180STATIC void
6181xfs_bmap_check_leaf_extents(
6182 xfs_btree_cur_t *cur, /* btree cursor or null */
6183 xfs_inode_t *ip, /* incore inode pointer */
6184 int whichfork) /* data or attr fork */
6185{
6186 xfs_bmbt_block_t *block; /* current btree block */
6187 xfs_fsblock_t bno; /* block # of "block" */
6188 xfs_buf_t *bp; /* buffer for "block" */
6189 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006190 xfs_extnum_t i=0, j; /* index into the extents list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 xfs_ifork_t *ifp; /* fork structure */
6192 int level; /* btree level, for checking */
6193 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10006194 __be64 *pp; /* pointer to block address */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006195 xfs_bmbt_rec_t *ep; /* pointer to current extent */
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006196 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006197 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 int bp_release = 0;
6199
6200 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6201 return;
6202 }
6203
6204 bno = NULLFSBLOCK;
6205 mp = ip->i_mount;
6206 ifp = XFS_IFORK_PTR(ip, whichfork);
6207 block = ifp->if_broot;
6208 /*
6209 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6210 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11006211 level = be16_to_cpu(block->bb_level);
6212 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6214 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006215 bno = be64_to_cpu(*pp);
6216
6217 ASSERT(bno != NULLDFSBNO);
6218 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6219 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6220
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 /*
6222 * Go down the tree until leaf level is reached, following the first
6223 * pointer (leftmost) at each level.
6224 */
6225 while (level-- > 0) {
6226 /* See if buf is in cur first */
6227 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6228 if (bp) {
6229 bp_release = 0;
6230 } else {
6231 bp_release = 1;
6232 }
6233 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6234 XFS_BMAP_BTREE_REF)))
6235 goto error_norelse;
6236 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6237 XFS_WANT_CORRUPTED_GOTO(
6238 XFS_BMAP_SANITY_CHECK(mp, block, level),
6239 error0);
6240 if (level == 0)
6241 break;
6242
6243 /*
6244 * Check this block for basic sanity (increasing keys and
6245 * no duplicate blocks).
6246 */
6247
6248 xfs_check_block(block, mp, 0, 0);
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006249 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006250 bno = be64_to_cpu(*pp);
6251 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 if (bp_release) {
6253 bp_release = 0;
6254 xfs_trans_brelse(NULL, bp);
6255 }
6256 }
6257
6258 /*
6259 * Here with bp and block set to the leftmost leaf node in the tree.
6260 */
6261 i = 0;
6262
6263 /*
6264 * Loop over all leaf nodes checking that all extents are in the right order.
6265 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 xfs_fsblock_t nextbno;
6268 xfs_extnum_t num_recs;
6269
6270
Christoph Hellwig16259e72005-11-02 15:11:25 +11006271 num_recs = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272
6273 /*
6274 * Read-ahead the next leaf block, if any.
6275 */
6276
Christoph Hellwig16259e72005-11-02 15:11:25 +11006277 nextbno = be64_to_cpu(block->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278
6279 /*
6280 * Check all the extents to make sure they are OK.
6281 * If we had a previous block, the last entry should
6282 * conform with the first entry in this one.
6283 */
6284
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006285 ep = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006286 if (i) {
6287 xfs_btree_check_rec(XFS_BTNUM_BMAP, &last, ep);
6288 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006289 for (j = 1; j < num_recs; j++) {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006290 nextp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, j + 1);
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006291 xfs_btree_check_rec(XFS_BTNUM_BMAP, ep, nextp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006292 ep = nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006295 last = *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 i += num_recs;
6297 if (bp_release) {
6298 bp_release = 0;
6299 xfs_trans_brelse(NULL, bp);
6300 }
6301 bno = nextbno;
6302 /*
6303 * If we've reached the end, stop.
6304 */
6305 if (bno == NULLFSBLOCK)
6306 break;
6307
6308 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6309 if (bp) {
6310 bp_release = 0;
6311 } else {
6312 bp_release = 1;
6313 }
6314 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6315 XFS_BMAP_BTREE_REF)))
6316 goto error_norelse;
6317 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6318 }
6319 if (bp_release) {
6320 bp_release = 0;
6321 xfs_trans_brelse(NULL, bp);
6322 }
6323 return;
6324
6325error0:
Harvey Harrison34a622b2008-04-10 12:19:21 +10006326 cmn_err(CE_WARN, "%s: at error0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 if (bp_release)
6328 xfs_trans_brelse(NULL, bp);
6329error_norelse:
6330 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
Harvey Harrison34a622b2008-04-10 12:19:21 +10006331 __func__, i);
6332 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 return;
6334}
6335#endif
6336
6337/*
6338 * Count fsblocks of the given fork.
6339 */
6340int /* error */
6341xfs_bmap_count_blocks(
6342 xfs_trans_t *tp, /* transaction pointer */
6343 xfs_inode_t *ip, /* incore inode */
6344 int whichfork, /* data or attr fork */
6345 int *count) /* out: count of blocks */
6346{
6347 xfs_bmbt_block_t *block; /* current btree block */
6348 xfs_fsblock_t bno; /* block # of "block" */
6349 xfs_ifork_t *ifp; /* fork structure */
6350 int level; /* btree level, for checking */
6351 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10006352 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353
6354 bno = NULLFSBLOCK;
6355 mp = ip->i_mount;
6356 ifp = XFS_IFORK_PTR(ip, whichfork);
6357 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006358 if (unlikely(xfs_bmap_count_leaves(ifp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6360 count) < 0)) {
6361 XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)",
6362 XFS_ERRLEVEL_LOW, mp);
6363 return XFS_ERROR(EFSCORRUPTED);
6364 }
6365 return 0;
6366 }
6367
6368 /*
6369 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6370 */
6371 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11006372 level = be16_to_cpu(block->bb_level);
6373 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006375 bno = be64_to_cpu(*pp);
6376 ASSERT(bno != NULLDFSBNO);
6377 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6378 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006380 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6382 mp);
6383 return XFS_ERROR(EFSCORRUPTED);
6384 }
6385
6386 return 0;
6387}
6388
6389/*
6390 * Recursively walks each level of a btree
6391 * to count total fsblocks is use.
6392 */
David Chinnera8272ce2007-11-23 16:28:09 +11006393STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394xfs_bmap_count_tree(
6395 xfs_mount_t *mp, /* file system mount point */
6396 xfs_trans_t *tp, /* transaction pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006397 xfs_ifork_t *ifp, /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 xfs_fsblock_t blockno, /* file system block number */
6399 int levelin, /* level in btree */
6400 int *count) /* Count of blocks */
6401{
6402 int error;
6403 xfs_buf_t *bp, *nbp;
6404 int level = levelin;
Christoph Hellwig576039c2006-09-28 10:58:06 +10006405 __be64 *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 xfs_fsblock_t bno = blockno;
6407 xfs_fsblock_t nextbno;
6408 xfs_bmbt_block_t *block, *nextblock;
6409 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410
6411 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6412 return error;
6413 *count += 1;
6414 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6415
6416 if (--level) {
6417 /* Not at node above leafs, count this level of nodes */
Christoph Hellwig16259e72005-11-02 15:11:25 +11006418 nextbno = be64_to_cpu(block->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 while (nextbno != NULLFSBLOCK) {
6420 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6421 0, &nbp, XFS_BMAP_BTREE_REF)))
6422 return error;
6423 *count += 1;
6424 nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11006425 nextbno = be64_to_cpu(nextblock->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 xfs_trans_brelse(tp, nbp);
6427 }
6428
6429 /* Dive to the next level */
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006430 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006431 bno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432 if (unlikely((error =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006433 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434 xfs_trans_brelse(tp, bp);
6435 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6436 XFS_ERRLEVEL_LOW, mp);
6437 return XFS_ERROR(EFSCORRUPTED);
6438 }
6439 xfs_trans_brelse(tp, bp);
6440 } else {
6441 /* count all level 1 nodes and their leaves */
6442 for (;;) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11006443 nextbno = be64_to_cpu(block->bb_rightsib);
6444 numrecs = be16_to_cpu(block->bb_numrecs);
Eric Sandeene9ed9d22007-05-08 13:48:56 +10006445 if (unlikely(xfs_bmap_disk_count_leaves(0,
6446 block, numrecs, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 xfs_trans_brelse(tp, bp);
6448 XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
6449 XFS_ERRLEVEL_LOW, mp);
6450 return XFS_ERROR(EFSCORRUPTED);
6451 }
6452 xfs_trans_brelse(tp, bp);
6453 if (nextbno == NULLFSBLOCK)
6454 break;
6455 bno = nextbno;
6456 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6457 XFS_BMAP_BTREE_REF)))
6458 return error;
6459 *count += 1;
6460 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6461 }
6462 }
6463 return 0;
6464}
6465
6466/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006467 * Count leaf blocks given a range of extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 */
David Chinnera8272ce2007-11-23 16:28:09 +11006469STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006471 xfs_ifork_t *ifp,
6472 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 int numrecs,
6474 int *count)
6475{
6476 int b;
6477
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006478 for (b = 0; b < numrecs; b++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006479 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
Yingping Lu91e11082005-11-02 15:10:24 +11006480 *count += xfs_bmbt_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006481 }
Yingping Lu91e11082005-11-02 15:10:24 +11006482 return 0;
6483}
6484
6485/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006486 * Count leaf blocks given a range of extent records originally
6487 * in btree format.
Yingping Lu91e11082005-11-02 15:10:24 +11006488 */
David Chinnera8272ce2007-11-23 16:28:09 +11006489STATIC int
Yingping Lu91e11082005-11-02 15:10:24 +11006490xfs_bmap_disk_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006491 xfs_extnum_t idx,
6492 xfs_bmbt_block_t *block,
Yingping Lu91e11082005-11-02 15:10:24 +11006493 int numrecs,
6494 int *count)
6495{
6496 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006497 xfs_bmbt_rec_t *frp;
Yingping Lu91e11082005-11-02 15:10:24 +11006498
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006499 for (b = 1; b <= numrecs; b++) {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006500 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, idx + b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501 *count += xfs_bmbt_disk_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503 return 0;
6504}