blob: aa19e1fee11fe159f7368d71313f388097cd8db6 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57
58#ifdef DEBUG
59STATIC void
60xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
61#endif
62
63kmem_zone_t *xfs_bmap_free_item_zone;
64
65/*
66 * Prototypes for internal bmap routines.
67 */
68
69
70/*
71 * Called from xfs_bmap_add_attrfork to handle extents format files.
72 */
73STATIC int /* error */
74xfs_bmap_add_attrfork_extents(
75 xfs_trans_t *tp, /* transaction pointer */
76 xfs_inode_t *ip, /* incore inode pointer */
77 xfs_fsblock_t *firstblock, /* first block allocated */
78 xfs_bmap_free_t *flist, /* blocks to free at commit */
79 int *flags); /* inode logging flags */
80
81/*
82 * Called from xfs_bmap_add_attrfork to handle local format files.
83 */
84STATIC int /* error */
85xfs_bmap_add_attrfork_local(
86 xfs_trans_t *tp, /* transaction pointer */
87 xfs_inode_t *ip, /* incore inode pointer */
88 xfs_fsblock_t *firstblock, /* first block allocated */
89 xfs_bmap_free_t *flist, /* blocks to free at commit */
90 int *flags); /* inode logging flags */
91
92/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +110093 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * after allocating space (or doing a delayed allocation).
95 */
96STATIC int /* error */
97xfs_bmap_add_extent(
98 xfs_inode_t *ip, /* incore inode pointer */
99 xfs_extnum_t idx, /* extent number to update/insert */
100 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100101 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 xfs_fsblock_t *first, /* pointer to firstblock variable */
103 xfs_bmap_free_t *flist, /* list of extents to be freed */
104 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000105 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 int whichfork, /* data or attr fork */
107 int rsvd); /* OK to allocate reserved blocks */
108
109/*
110 * Called by xfs_bmap_add_extent to handle cases converting a delayed
111 * allocation to a real allocation.
112 */
113STATIC int /* error */
114xfs_bmap_add_extent_delay_real(
115 xfs_inode_t *ip, /* incore inode pointer */
116 xfs_extnum_t idx, /* extent number to update/insert */
117 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100118 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
120 xfs_fsblock_t *first, /* pointer to firstblock variable */
121 xfs_bmap_free_t *flist, /* list of extents to be freed */
122 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000123 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int rsvd); /* OK to allocate reserved blocks */
125
126/*
127 * Called by xfs_bmap_add_extent to handle cases converting a hole
128 * to a delayed allocation.
129 */
130STATIC int /* error */
131xfs_bmap_add_extent_hole_delay(
132 xfs_inode_t *ip, /* incore inode pointer */
133 xfs_extnum_t idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100134 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 int *logflagsp,/* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000136 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int rsvd); /* OK to allocate reserved blocks */
138
139/*
140 * Called by xfs_bmap_add_extent to handle cases converting a hole
141 * to a real allocation.
142 */
143STATIC int /* error */
144xfs_bmap_add_extent_hole_real(
145 xfs_inode_t *ip, /* incore inode pointer */
146 xfs_extnum_t idx, /* extent number to update/insert */
147 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100148 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000150 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 int whichfork); /* data or attr fork */
152
153/*
154 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
155 * allocation to a real allocation or vice versa.
156 */
157STATIC int /* error */
158xfs_bmap_add_extent_unwritten_real(
159 xfs_inode_t *ip, /* incore inode pointer */
160 xfs_extnum_t idx, /* extent number to update/insert */
161 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100162 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000163 int *logflagsp, /* inode logging flags */
164 xfs_extdelta_t *delta); /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166/*
167 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
168 * It figures out where to ask the underlying allocator to put the new extent.
169 */
170STATIC int /* error */
171xfs_bmap_alloc(
172 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
173
174/*
175 * Transform a btree format file with only one leaf node, where the
176 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100177 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * give up the space for the btree root and pitch the leaf block.
179 */
180STATIC int /* error */
181xfs_bmap_btree_to_extents(
182 xfs_trans_t *tp, /* transaction pointer */
183 xfs_inode_t *ip, /* incore inode pointer */
184 xfs_btree_cur_t *cur, /* btree cursor */
185 int *logflagsp, /* inode logging flags */
186 int whichfork); /* data or attr fork */
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100189 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * after removing space (or undoing a delayed allocation).
191 */
192STATIC int /* error */
193xfs_bmap_del_extent(
194 xfs_inode_t *ip, /* incore inode pointer */
195 xfs_trans_t *tp, /* current trans pointer */
196 xfs_extnum_t idx, /* extent number to update/insert */
197 xfs_bmap_free_t *flist, /* list of extents to be freed */
198 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100199 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int *logflagsp,/* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000201 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int whichfork, /* data or attr fork */
203 int rsvd); /* OK to allocate reserved blocks */
204
205/*
206 * Remove the entry "free" from the free item list. Prev points to the
207 * previous entry, unless "free" is the head of the list.
208 */
209STATIC void
210xfs_bmap_del_free(
211 xfs_bmap_free_t *flist, /* free item list header */
212 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
213 xfs_bmap_free_item_t *free); /* list item to be freed */
214
215/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * Convert an extents-format file into a btree-format file.
217 * The new file will have a root block (in the inode) and a single child block.
218 */
219STATIC int /* error */
220xfs_bmap_extents_to_btree(
221 xfs_trans_t *tp, /* transaction pointer */
222 xfs_inode_t *ip, /* incore inode pointer */
223 xfs_fsblock_t *firstblock, /* first-block-allocated */
224 xfs_bmap_free_t *flist, /* blocks freed in xaction */
225 xfs_btree_cur_t **curp, /* cursor returned to caller */
226 int wasdel, /* converting a delayed alloc */
227 int *logflagsp, /* inode logging flags */
228 int whichfork); /* data or attr fork */
229
230/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * Convert a local file to an extents file.
232 * This code is sort of bogus, since the file data needs to get
233 * logged so it won't be lost. The bmap-level manipulations are ok, though.
234 */
235STATIC int /* error */
236xfs_bmap_local_to_extents(
237 xfs_trans_t *tp, /* transaction pointer */
238 xfs_inode_t *ip, /* incore inode pointer */
239 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
240 xfs_extlen_t total, /* total blocks needed by transaction */
241 int *logflagsp, /* inode logging flags */
242 int whichfork); /* data or attr fork */
243
244/*
245 * Search the extents list for the inode, for the extent containing bno.
246 * If bno lies in a hole, point to the next entry. If bno lies past eof,
247 * *eofp will be set, and *prevp will contain the last entry (null if none).
248 * Else, *lastxp will be set to the index of the found
249 * entry; *gotp will contain the entry.
250 */
251STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
252xfs_bmap_search_extents(
253 xfs_inode_t *ip, /* incore inode pointer */
254 xfs_fileoff_t bno, /* block number searched for */
255 int whichfork, /* data or attr fork */
256 int *eofp, /* out: end of file found */
257 xfs_extnum_t *lastxp, /* out: last extent index */
258 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
259 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
260
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000261/*
262 * Check the last inode extent to determine whether this allocation will result
263 * in blocks being allocated at the end of the file. When we allocate new data
264 * blocks at the end of the file which do not start at the previous data block,
265 * we will try to align the new blocks at stripe unit boundaries.
266 */
267STATIC int /* error */
268xfs_bmap_isaeof(
269 xfs_inode_t *ip, /* incore inode pointer */
270 xfs_fileoff_t off, /* file offset in fsblocks */
271 int whichfork, /* data or attribute fork */
272 char *aeof); /* return value */
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#ifdef XFS_BMAP_TRACE
275/*
276 * Add a bmap trace buffer entry. Base routine for the others.
277 */
278STATIC void
279xfs_bmap_trace_addentry(
280 int opcode, /* operation */
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(ies) */
285 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
286 xfs_bmbt_rec_t *r1, /* first record */
287 xfs_bmbt_rec_t *r2, /* second record or null */
288 int whichfork); /* data or attr fork */
289
290/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100291 * Add bmap trace entry prior to a call to xfs_iext_remove.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
293STATIC void
294xfs_bmap_trace_delete(
Eric Sandeen3a59c942007-07-11 11:09:47 +1000295 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 char *desc, /* operation description */
297 xfs_inode_t *ip, /* incore inode pointer */
298 xfs_extnum_t idx, /* index of entry(entries) deleted */
299 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
300 int whichfork); /* data or attr fork */
301
302/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100303 * Add bmap trace entry prior to a call to xfs_iext_insert, or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 * reading in the extents list from the disk (in the btree).
305 */
306STATIC void
307xfs_bmap_trace_insert(
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(entries) inserted */
312 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
313 xfs_bmbt_irec_t *r1, /* inserted record 1 */
314 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
315 int whichfork); /* data or attr fork */
316
317/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100318 * Add bmap trace entry after updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
320STATIC void
321xfs_bmap_trace_post_update(
Eric Sandeen3a59c942007-07-11 11:09:47 +1000322 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 char *desc, /* operation description */
324 xfs_inode_t *ip, /* incore inode pointer */
325 xfs_extnum_t idx, /* index of entry updated */
326 int whichfork); /* data or attr fork */
327
328/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100329 * Add bmap trace entry prior to updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 */
331STATIC void
332xfs_bmap_trace_pre_update(
Eric Sandeen3a59c942007-07-11 11:09:47 +1000333 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 char *desc, /* operation description */
335 xfs_inode_t *ip, /* incore inode pointer */
336 xfs_extnum_t idx, /* index of entry to be updated */
337 int whichfork); /* data or attr fork */
338
Eric Sandeen3a59c942007-07-11 11:09:47 +1000339#define XFS_BMAP_TRACE_DELETE(d,ip,i,c,w) \
340 xfs_bmap_trace_delete(__FUNCTION__,d,ip,i,c,w)
341#define XFS_BMAP_TRACE_INSERT(d,ip,i,c,r1,r2,w) \
342 xfs_bmap_trace_insert(__FUNCTION__,d,ip,i,c,r1,r2,w)
343#define XFS_BMAP_TRACE_POST_UPDATE(d,ip,i,w) \
344 xfs_bmap_trace_post_update(__FUNCTION__,d,ip,i,w)
345#define XFS_BMAP_TRACE_PRE_UPDATE(d,ip,i,w) \
346 xfs_bmap_trace_pre_update(__FUNCTION__,d,ip,i,w)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347#else
Eric Sandeen3a59c942007-07-11 11:09:47 +1000348#define XFS_BMAP_TRACE_DELETE(d,ip,i,c,w)
349#define XFS_BMAP_TRACE_INSERT(d,ip,i,c,r1,r2,w)
350#define XFS_BMAP_TRACE_POST_UPDATE(d,ip,i,w)
351#define XFS_BMAP_TRACE_PRE_UPDATE(d,ip,i,w)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352#endif /* XFS_BMAP_TRACE */
353
354/*
355 * Compute the worst-case number of indirect blocks that will be used
356 * for ip's delayed extent of length "len".
357 */
358STATIC xfs_filblks_t
359xfs_bmap_worst_indlen(
360 xfs_inode_t *ip, /* incore inode pointer */
361 xfs_filblks_t len); /* delayed extent length */
362
363#ifdef DEBUG
364/*
365 * Perform various validation checks on the values being returned
366 * from xfs_bmapi().
367 */
368STATIC void
369xfs_bmap_validate_ret(
370 xfs_fileoff_t bno,
371 xfs_filblks_t len,
372 int flags,
373 xfs_bmbt_irec_t *mval,
374 int nmap,
375 int ret_nmap);
376#else
377#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
378#endif /* DEBUG */
379
380#if defined(XFS_RW_TRACE)
381STATIC void
382xfs_bunmap_trace(
383 xfs_inode_t *ip,
384 xfs_fileoff_t bno,
385 xfs_filblks_t len,
386 int flags,
387 inst_t *ra);
388#else
389#define xfs_bunmap_trace(ip, bno, len, flags, ra)
390#endif /* XFS_RW_TRACE */
391
392STATIC int
393xfs_bmap_count_tree(
394 xfs_mount_t *mp,
395 xfs_trans_t *tp,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100396 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 xfs_fsblock_t blockno,
398 int levelin,
399 int *count);
400
401STATIC int
402xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100403 xfs_ifork_t *ifp,
404 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int numrecs,
406 int *count);
407
Yingping Lu91e11082005-11-02 15:10:24 +1100408STATIC int
409xfs_bmap_disk_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100410 xfs_extnum_t idx,
411 xfs_bmbt_block_t *block,
Yingping Lu91e11082005-11-02 15:10:24 +1100412 int numrecs,
413 int *count);
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/*
416 * Bmap internal routines.
417 */
418
419/*
420 * Called from xfs_bmap_add_attrfork to handle btree format files.
421 */
422STATIC int /* error */
423xfs_bmap_add_attrfork_btree(
424 xfs_trans_t *tp, /* transaction pointer */
425 xfs_inode_t *ip, /* incore inode pointer */
426 xfs_fsblock_t *firstblock, /* first block allocated */
427 xfs_bmap_free_t *flist, /* blocks to free at commit */
428 int *flags) /* inode logging flags */
429{
430 xfs_btree_cur_t *cur; /* btree cursor */
431 int error; /* error return value */
432 xfs_mount_t *mp; /* file system mount struct */
433 int stat; /* newroot status */
434
435 mp = ip->i_mount;
436 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
437 *flags |= XFS_ILOG_DBROOT;
438 else {
439 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
440 XFS_DATA_FORK);
441 cur->bc_private.b.flist = flist;
442 cur->bc_private.b.firstblock = *firstblock;
443 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
444 goto error0;
445 ASSERT(stat == 1); /* must be at least one entry */
446 if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
447 goto error0;
448 if (stat == 0) {
449 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
450 return XFS_ERROR(ENOSPC);
451 }
452 *firstblock = cur->bc_private.b.firstblock;
453 cur->bc_private.b.allocated = 0;
454 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
455 }
456 return 0;
457error0:
458 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
459 return error;
460}
461
462/*
463 * Called from xfs_bmap_add_attrfork to handle extents format files.
464 */
465STATIC int /* error */
466xfs_bmap_add_attrfork_extents(
467 xfs_trans_t *tp, /* transaction pointer */
468 xfs_inode_t *ip, /* incore inode pointer */
469 xfs_fsblock_t *firstblock, /* first block allocated */
470 xfs_bmap_free_t *flist, /* blocks to free at commit */
471 int *flags) /* inode logging flags */
472{
473 xfs_btree_cur_t *cur; /* bmap btree cursor */
474 int error; /* error return value */
475
476 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
477 return 0;
478 cur = NULL;
479 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
480 flags, XFS_DATA_FORK);
481 if (cur) {
482 cur->bc_private.b.allocated = 0;
483 xfs_btree_del_cursor(cur,
484 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
485 }
486 return error;
487}
488
489/*
490 * Called from xfs_bmap_add_attrfork to handle local format files.
491 */
492STATIC int /* error */
493xfs_bmap_add_attrfork_local(
494 xfs_trans_t *tp, /* transaction pointer */
495 xfs_inode_t *ip, /* incore inode pointer */
496 xfs_fsblock_t *firstblock, /* first block allocated */
497 xfs_bmap_free_t *flist, /* blocks to free at commit */
498 int *flags) /* inode logging flags */
499{
500 xfs_da_args_t dargs; /* args for dir/attr code */
501 int error; /* error return value */
502 xfs_mount_t *mp; /* mount structure pointer */
503
504 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
505 return 0;
506 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
507 mp = ip->i_mount;
508 memset(&dargs, 0, sizeof(dargs));
509 dargs.dp = ip;
510 dargs.firstblock = firstblock;
511 dargs.flist = flist;
512 dargs.total = mp->m_dirblkfsbs;
513 dargs.whichfork = XFS_DATA_FORK;
514 dargs.trans = tp;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000515 error = xfs_dir2_sf_to_block(&dargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 } else
517 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
518 XFS_DATA_FORK);
519 return error;
520}
521
522/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100523 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 * after allocating space (or doing a delayed allocation).
525 */
526STATIC int /* error */
527xfs_bmap_add_extent(
528 xfs_inode_t *ip, /* incore inode pointer */
529 xfs_extnum_t idx, /* extent number to update/insert */
530 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100531 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 xfs_fsblock_t *first, /* pointer to firstblock variable */
533 xfs_bmap_free_t *flist, /* list of extents to be freed */
534 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000535 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 int whichfork, /* data or attr fork */
537 int rsvd) /* OK to use reserved data blocks */
538{
539 xfs_btree_cur_t *cur; /* btree cursor or null */
540 xfs_filblks_t da_new; /* new count del alloc blocks used */
541 xfs_filblks_t da_old; /* old count del alloc blocks used */
542 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 xfs_ifork_t *ifp; /* inode fork ptr */
544 int logflags; /* returned value */
545 xfs_extnum_t nextents; /* number of extents in file now */
546
547 XFS_STATS_INC(xs_add_exlist);
548 cur = *curp;
549 ifp = XFS_IFORK_PTR(ip, whichfork);
550 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
551 ASSERT(idx <= nextents);
552 da_old = da_new = 0;
553 error = 0;
554 /*
555 * This is the first extent added to a new/empty file.
556 * Special case this one, so other routines get to assume there are
557 * already extents in the list.
558 */
559 if (nextents == 0) {
Eric Sandeen3a59c942007-07-11 11:09:47 +1000560 XFS_BMAP_TRACE_INSERT("insert empty", ip, 0, 1, new, NULL,
561 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100562 xfs_iext_insert(ifp, 0, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 ASSERT(cur == NULL);
564 ifp->if_lastex = 0;
565 if (!ISNULLSTARTBLOCK(new->br_startblock)) {
566 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
567 logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
568 } else
569 logflags = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000570 /* DELTA: single new extent */
571 if (delta) {
572 if (delta->xed_startoff > new->br_startoff)
573 delta->xed_startoff = new->br_startoff;
574 if (delta->xed_blockcount <
575 new->br_startoff + new->br_blockcount)
576 delta->xed_blockcount = new->br_startoff +
577 new->br_blockcount;
578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580 /*
581 * Any kind of new delayed allocation goes here.
582 */
583 else if (ISNULLSTARTBLOCK(new->br_startblock)) {
584 if (cur)
585 ASSERT((cur->bc_private.b.flags &
586 XFS_BTCUR_BPRV_WASDEL) == 0);
Eric Sandeene9ed9d22007-05-08 13:48:56 +1000587 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000588 &logflags, delta, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 goto done;
590 }
591 /*
592 * Real allocation off the end of the file.
593 */
594 else if (idx == nextents) {
595 if (cur)
596 ASSERT((cur->bc_private.b.flags &
597 XFS_BTCUR_BPRV_WASDEL) == 0);
598 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000599 &logflags, delta, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 goto done;
601 } else {
602 xfs_bmbt_irec_t prev; /* old extent at offset idx */
603
604 /*
605 * Get the record referred to by idx.
606 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100607 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /*
609 * If it's a real allocation record, and the new allocation ends
610 * after the start of the referred to record, then we're filling
611 * in a delayed or unwritten allocation with a real one, or
612 * converting real back to unwritten.
613 */
614 if (!ISNULLSTARTBLOCK(new->br_startblock) &&
615 new->br_startoff + new->br_blockcount > prev.br_startoff) {
616 if (prev.br_state != XFS_EXT_UNWRITTEN &&
617 ISNULLSTARTBLOCK(prev.br_startblock)) {
618 da_old = STARTBLOCKVAL(prev.br_startblock);
619 if (cur)
620 ASSERT(cur->bc_private.b.flags &
621 XFS_BTCUR_BPRV_WASDEL);
622 if ((error = xfs_bmap_add_extent_delay_real(ip,
623 idx, &cur, new, &da_new, first, flist,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000624 &logflags, delta, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 goto done;
626 } else if (new->br_state == XFS_EXT_NORM) {
627 ASSERT(new->br_state == XFS_EXT_NORM);
628 if ((error = xfs_bmap_add_extent_unwritten_real(
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000629 ip, idx, &cur, new, &logflags, delta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 goto done;
631 } else {
632 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
633 if ((error = xfs_bmap_add_extent_unwritten_real(
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000634 ip, idx, &cur, new, &logflags, delta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 goto done;
636 }
637 ASSERT(*curp == cur || *curp == NULL);
638 }
639 /*
640 * Otherwise we're filling in a hole with an allocation.
641 */
642 else {
643 if (cur)
644 ASSERT((cur->bc_private.b.flags &
645 XFS_BTCUR_BPRV_WASDEL) == 0);
646 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000647 new, &logflags, delta, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 goto done;
649 }
650 }
651
652 ASSERT(*curp == cur || *curp == NULL);
653 /*
654 * Convert to a btree if necessary.
655 */
656 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
657 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
658 int tmp_logflags; /* partial log flag return val */
659
660 ASSERT(cur == NULL);
661 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
662 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
663 logflags |= tmp_logflags;
664 if (error)
665 goto done;
666 }
667 /*
668 * Adjust for changes in reserved delayed indirect blocks.
669 * Nothing to do for disk quotas here.
670 */
671 if (da_old || da_new) {
672 xfs_filblks_t nblks;
673
674 nblks = da_new;
675 if (cur)
676 nblks += cur->bc_private.b.allocated;
677 ASSERT(nblks <= da_old);
678 if (nblks < da_old)
679 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100680 (int64_t)(da_old - nblks), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 /*
683 * Clear out the allocated field, done with it now in any case.
684 */
685 if (cur) {
686 cur->bc_private.b.allocated = 0;
687 *curp = cur;
688 }
689done:
690#ifdef DEBUG
691 if (!error)
692 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
693#endif
694 *logflagsp = logflags;
695 return error;
696}
697
698/*
699 * Called by xfs_bmap_add_extent to handle cases converting a delayed
700 * allocation to a real allocation.
701 */
702STATIC int /* error */
703xfs_bmap_add_extent_delay_real(
704 xfs_inode_t *ip, /* incore inode pointer */
705 xfs_extnum_t idx, /* extent number to update/insert */
706 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100707 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
709 xfs_fsblock_t *first, /* pointer to firstblock variable */
710 xfs_bmap_free_t *flist, /* list of extents to be freed */
711 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000712 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 int rsvd) /* OK to use reserved data block allocation */
714{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 xfs_btree_cur_t *cur; /* btree cursor */
716 int diff; /* temp value */
717 xfs_bmbt_rec_t *ep; /* extent entry for idx */
718 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100720 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 xfs_fileoff_t new_endoff; /* end offset of new entry */
722 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
723 /* left is 0, right is 1, prev is 2 */
724 int rval=0; /* return value (logging flags) */
725 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000726 xfs_filblks_t temp=0; /* value for dnew calculations */
727 xfs_filblks_t temp2=0;/* value for dnew calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 int tmp_rval; /* partial logging flags */
729 enum { /* bit number definitions for state */
730 LEFT_CONTIG, RIGHT_CONTIG,
731 LEFT_FILLING, RIGHT_FILLING,
732 LEFT_DELAY, RIGHT_DELAY,
733 LEFT_VALID, RIGHT_VALID
734 };
735
736#define LEFT r[0]
737#define RIGHT r[1]
738#define PREV r[2]
739#define MASK(b) (1 << (b))
740#define MASK2(a,b) (MASK(a) | MASK(b))
741#define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
742#define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
743#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
744#define STATE_TEST(b) (state & MASK(b))
745#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
746 ((state &= ~MASK(b)), 0))
747#define SWITCH_STATE \
748 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
749
750 /*
751 * Set up a bunch of variables to make the tests simpler.
752 */
753 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100754 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
755 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 xfs_bmbt_get_all(ep, &PREV);
757 new_endoff = new->br_startoff + new->br_blockcount;
758 ASSERT(PREV.br_startoff <= new->br_startoff);
759 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
760 /*
761 * Set flags determining what part of the previous delayed allocation
762 * extent is being replaced by a real allocation.
763 */
764 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
765 STATE_SET(RIGHT_FILLING,
766 PREV.br_startoff + PREV.br_blockcount == new_endoff);
767 /*
768 * Check and set flags if this segment has a left neighbor.
769 * Don't set contiguous if the combined extent would be too large.
770 */
771 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100772 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
774 }
775 STATE_SET(LEFT_CONTIG,
776 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
777 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
778 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
779 LEFT.br_state == new->br_state &&
780 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
781 /*
782 * Check and set flags if this segment has a right neighbor.
783 * Don't set contiguous if the combined extent would be too large.
784 * Also check for all-three-contiguous being too large.
785 */
786 if (STATE_SET_TEST(RIGHT_VALID,
787 idx <
788 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100789 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
791 }
792 STATE_SET(RIGHT_CONTIG,
793 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
794 new_endoff == RIGHT.br_startoff &&
795 new->br_startblock + new->br_blockcount ==
796 RIGHT.br_startblock &&
797 new->br_state == RIGHT.br_state &&
798 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
799 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
800 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
801 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
802 <= MAXEXTLEN));
803 error = 0;
804 /*
805 * Switch out based on the FILLING and CONTIG state bits.
806 */
807 switch (SWITCH_STATE) {
808
809 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
810 /*
811 * Filling in all of a previously delayed allocation extent.
812 * The left and right neighbors are both contiguous with new.
813 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000814 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100816 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 LEFT.br_blockcount + PREV.br_blockcount +
818 RIGHT.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000819 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000821 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100822 xfs_iext_remove(ifp, idx, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 ip->i_df.if_lastex = idx - 1;
824 ip->i_d.di_nextents--;
825 if (cur == NULL)
826 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
827 else {
828 rval = XFS_ILOG_CORE;
829 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
830 RIGHT.br_startblock,
831 RIGHT.br_blockcount, &i)))
832 goto done;
833 ASSERT(i == 1);
834 if ((error = xfs_bmbt_delete(cur, &i)))
835 goto done;
836 ASSERT(i == 1);
837 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
838 goto done;
839 ASSERT(i == 1);
840 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
841 LEFT.br_startblock,
842 LEFT.br_blockcount +
843 PREV.br_blockcount +
844 RIGHT.br_blockcount, LEFT.br_state)))
845 goto done;
846 }
847 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000848 /* DELTA: Three in-core extents are replaced by one. */
849 temp = LEFT.br_startoff;
850 temp2 = LEFT.br_blockcount +
851 PREV.br_blockcount +
852 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 break;
854
855 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
856 /*
857 * Filling in all of a previously delayed allocation extent.
858 * The left neighbor is contiguous, the right is not.
859 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000860 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100862 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 LEFT.br_blockcount + PREV.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000864 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 XFS_DATA_FORK);
866 ip->i_df.if_lastex = idx - 1;
Eric Sandeen3a59c942007-07-11 11:09:47 +1000867 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100868 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (cur == NULL)
870 rval = XFS_ILOG_DEXT;
871 else {
872 rval = 0;
873 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
874 LEFT.br_startblock, LEFT.br_blockcount,
875 &i)))
876 goto done;
877 ASSERT(i == 1);
878 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
879 LEFT.br_startblock,
880 LEFT.br_blockcount +
881 PREV.br_blockcount, LEFT.br_state)))
882 goto done;
883 }
884 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000885 /* DELTA: Two in-core extents are replaced by one. */
886 temp = LEFT.br_startoff;
887 temp2 = LEFT.br_blockcount +
888 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 break;
890
891 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
892 /*
893 * Filling in all of a previously delayed allocation extent.
894 * The right neighbor is contiguous, the left is not.
895 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000896 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 xfs_bmbt_set_startblock(ep, new->br_startblock);
898 xfs_bmbt_set_blockcount(ep,
899 PREV.br_blockcount + RIGHT.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000900 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 ip->i_df.if_lastex = idx;
Eric Sandeen3a59c942007-07-11 11:09:47 +1000902 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100903 xfs_iext_remove(ifp, idx + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (cur == NULL)
905 rval = XFS_ILOG_DEXT;
906 else {
907 rval = 0;
908 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
909 RIGHT.br_startblock,
910 RIGHT.br_blockcount, &i)))
911 goto done;
912 ASSERT(i == 1);
913 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
914 new->br_startblock,
915 PREV.br_blockcount +
916 RIGHT.br_blockcount, PREV.br_state)))
917 goto done;
918 }
919 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000920 /* DELTA: Two in-core extents are replaced by one. */
921 temp = PREV.br_startoff;
922 temp2 = PREV.br_blockcount +
923 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 break;
925
926 case MASK2(LEFT_FILLING, RIGHT_FILLING):
927 /*
928 * Filling in all of a previously delayed allocation extent.
929 * Neither the left nor right neighbors are contiguous with
930 * the new one.
931 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000932 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 xfs_bmbt_set_startblock(ep, new->br_startblock);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000934 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 ip->i_df.if_lastex = idx;
936 ip->i_d.di_nextents++;
937 if (cur == NULL)
938 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
939 else {
940 rval = XFS_ILOG_CORE;
941 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
942 new->br_startblock, new->br_blockcount,
943 &i)))
944 goto done;
945 ASSERT(i == 0);
946 cur->bc_rec.b.br_state = XFS_EXT_NORM;
947 if ((error = xfs_bmbt_insert(cur, &i)))
948 goto done;
949 ASSERT(i == 1);
950 }
951 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000952 /* DELTA: The in-core extent described by new changed type. */
953 temp = new->br_startoff;
954 temp2 = new->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 break;
956
957 case MASK2(LEFT_FILLING, LEFT_CONTIG):
958 /*
959 * Filling in the first part of a previous delayed allocation.
960 * The left neighbor is contiguous.
961 */
Eric Sandeen3a59c942007-07-11 11:09:47 +1000962 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100963 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 LEFT.br_blockcount + new->br_blockcount);
965 xfs_bmbt_set_startoff(ep,
966 PREV.br_startoff + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +1000967 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 temp = PREV.br_blockcount - new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +1000969 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 xfs_bmbt_set_blockcount(ep, temp);
971 ip->i_df.if_lastex = idx - 1;
972 if (cur == NULL)
973 rval = XFS_ILOG_DEXT;
974 else {
975 rval = 0;
976 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
977 LEFT.br_startblock, LEFT.br_blockcount,
978 &i)))
979 goto done;
980 ASSERT(i == 1);
981 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
982 LEFT.br_startblock,
983 LEFT.br_blockcount +
984 new->br_blockcount,
985 LEFT.br_state)))
986 goto done;
987 }
988 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
989 STARTBLOCKVAL(PREV.br_startblock));
990 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +1000991 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000993 /* DELTA: The boundary between two in-core extents moved. */
994 temp = LEFT.br_startoff;
995 temp2 = LEFT.br_blockcount +
996 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 break;
998
999 case MASK(LEFT_FILLING):
1000 /*
1001 * Filling in the first part of a previous delayed allocation.
1002 * The left neighbor is not contiguous.
1003 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001004 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 xfs_bmbt_set_startoff(ep, new_endoff);
1006 temp = PREV.br_blockcount - new->br_blockcount;
1007 xfs_bmbt_set_blockcount(ep, temp);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001008 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001010 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 ip->i_df.if_lastex = idx;
1012 ip->i_d.di_nextents++;
1013 if (cur == NULL)
1014 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1015 else {
1016 rval = XFS_ILOG_CORE;
1017 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1018 new->br_startblock, new->br_blockcount,
1019 &i)))
1020 goto done;
1021 ASSERT(i == 0);
1022 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1023 if ((error = xfs_bmbt_insert(cur, &i)))
1024 goto done;
1025 ASSERT(i == 1);
1026 }
1027 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1028 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1029 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1030 first, flist, &cur, 1, &tmp_rval,
1031 XFS_DATA_FORK);
1032 rval |= tmp_rval;
1033 if (error)
1034 goto done;
1035 }
1036 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1037 STARTBLOCKVAL(PREV.br_startblock) -
1038 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001039 ep = xfs_iext_get_ext(ifp, idx + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001041 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx + 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001043 /* DELTA: One in-core extent is split in two. */
1044 temp = PREV.br_startoff;
1045 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 break;
1047
1048 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1049 /*
1050 * Filling in the last part of a previous delayed allocation.
1051 * The right neighbor is contiguous with the new allocation.
1052 */
1053 temp = PREV.br_blockcount - new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001054 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
1055 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 xfs_bmbt_set_blockcount(ep, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001057 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1058 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 new->br_blockcount + RIGHT.br_blockcount,
1060 RIGHT.br_state);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001061 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 ip->i_df.if_lastex = idx + 1;
1063 if (cur == NULL)
1064 rval = XFS_ILOG_DEXT;
1065 else {
1066 rval = 0;
1067 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1068 RIGHT.br_startblock,
1069 RIGHT.br_blockcount, &i)))
1070 goto done;
1071 ASSERT(i == 1);
1072 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1073 new->br_startblock,
1074 new->br_blockcount +
1075 RIGHT.br_blockcount,
1076 RIGHT.br_state)))
1077 goto done;
1078 }
1079 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1080 STARTBLOCKVAL(PREV.br_startblock));
1081 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001082 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001084 /* DELTA: The boundary between two in-core extents moved. */
1085 temp = PREV.br_startoff;
1086 temp2 = PREV.br_blockcount +
1087 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 break;
1089
1090 case MASK(RIGHT_FILLING):
1091 /*
1092 * Filling in the last part of a previous delayed allocation.
1093 * The right neighbor is not contiguous.
1094 */
1095 temp = PREV.br_blockcount - new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001096 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 xfs_bmbt_set_blockcount(ep, temp);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001098 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1099 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001100 xfs_iext_insert(ifp, idx + 1, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 ip->i_df.if_lastex = idx + 1;
1102 ip->i_d.di_nextents++;
1103 if (cur == NULL)
1104 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1105 else {
1106 rval = XFS_ILOG_CORE;
1107 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1108 new->br_startblock, new->br_blockcount,
1109 &i)))
1110 goto done;
1111 ASSERT(i == 0);
1112 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1113 if ((error = xfs_bmbt_insert(cur, &i)))
1114 goto done;
1115 ASSERT(i == 1);
1116 }
1117 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1118 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1119 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1120 first, flist, &cur, 1, &tmp_rval,
1121 XFS_DATA_FORK);
1122 rval |= tmp_rval;
1123 if (error)
1124 goto done;
1125 }
1126 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1127 STARTBLOCKVAL(PREV.br_startblock) -
1128 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001129 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001131 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001133 /* DELTA: One in-core extent is split in two. */
1134 temp = PREV.br_startoff;
1135 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 break;
1137
1138 case 0:
1139 /*
1140 * Filling in the middle part of a previous delayed allocation.
1141 * Contiguity is impossible here.
1142 * This case is avoided almost all the time.
1143 */
1144 temp = new->br_startoff - PREV.br_startoff;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001145 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 xfs_bmbt_set_blockcount(ep, temp);
1147 r[0] = *new;
Lachlan McIlroyd2133712006-11-21 18:55:16 +11001148 r[1].br_state = PREV.br_state;
1149 r[1].br_startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 r[1].br_startoff = new_endoff;
1151 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1152 r[1].br_blockcount = temp2;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001153 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001155 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 ip->i_df.if_lastex = idx + 1;
1157 ip->i_d.di_nextents++;
1158 if (cur == NULL)
1159 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1160 else {
1161 rval = XFS_ILOG_CORE;
1162 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1163 new->br_startblock, new->br_blockcount,
1164 &i)))
1165 goto done;
1166 ASSERT(i == 0);
1167 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1168 if ((error = xfs_bmbt_insert(cur, &i)))
1169 goto done;
1170 ASSERT(i == 1);
1171 }
1172 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1173 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1174 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1175 first, flist, &cur, 1, &tmp_rval,
1176 XFS_DATA_FORK);
1177 rval |= tmp_rval;
1178 if (error)
1179 goto done;
1180 }
1181 temp = xfs_bmap_worst_indlen(ip, temp);
1182 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1183 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
1184 (cur ? cur->bc_private.b.allocated : 0));
1185 if (diff > 0 &&
David Chinner20f4ebf2007-02-10 18:36:10 +11001186 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 /*
1188 * Ick gross gag me with a spoon.
1189 */
1190 ASSERT(0); /* want to see if this ever happens! */
1191 while (diff > 0) {
1192 if (temp) {
1193 temp--;
1194 diff--;
1195 if (!diff ||
1196 !xfs_mod_incore_sb(ip->i_mount,
David Chinner20f4ebf2007-02-10 18:36:10 +11001197 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 break;
1199 }
1200 if (temp2) {
1201 temp2--;
1202 diff--;
1203 if (!diff ||
1204 !xfs_mod_incore_sb(ip->i_mount,
David Chinner20f4ebf2007-02-10 18:36:10 +11001205 XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 break;
1207 }
1208 }
1209 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001210 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001212 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
1213 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001214 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1215 NULLSTARTBLOCK((int)temp2));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001216 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx + 2, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 *dnew = temp + temp2;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001218 /* DELTA: One in-core extent is split in three. */
1219 temp = PREV.br_startoff;
1220 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 break;
1222
1223 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1224 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1225 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1226 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1227 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1228 case MASK(LEFT_CONTIG):
1229 case MASK(RIGHT_CONTIG):
1230 /*
1231 * These cases are all impossible.
1232 */
1233 ASSERT(0);
1234 }
1235 *curp = cur;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001236 if (delta) {
1237 temp2 += temp;
1238 if (delta->xed_startoff > temp)
1239 delta->xed_startoff = temp;
1240 if (delta->xed_blockcount < temp2)
1241 delta->xed_blockcount = temp2;
1242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243done:
1244 *logflagsp = rval;
1245 return error;
1246#undef LEFT
1247#undef RIGHT
1248#undef PREV
1249#undef MASK
1250#undef MASK2
1251#undef MASK3
1252#undef MASK4
1253#undef STATE_SET
1254#undef STATE_TEST
1255#undef STATE_SET_TEST
1256#undef SWITCH_STATE
1257}
1258
1259/*
1260 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1261 * allocation to a real allocation or vice versa.
1262 */
1263STATIC int /* error */
1264xfs_bmap_add_extent_unwritten_real(
1265 xfs_inode_t *ip, /* incore inode pointer */
1266 xfs_extnum_t idx, /* extent number to update/insert */
1267 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001268 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001269 int *logflagsp, /* inode logging flags */
1270 xfs_extdelta_t *delta) /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 xfs_btree_cur_t *cur; /* btree cursor */
1273 xfs_bmbt_rec_t *ep; /* extent entry for idx */
1274 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001276 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 xfs_fileoff_t new_endoff; /* end offset of new entry */
1278 xfs_exntst_t newext; /* new extent state */
1279 xfs_exntst_t oldext; /* old extent state */
1280 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1281 /* left is 0, right is 1, prev is 2 */
1282 int rval=0; /* return value (logging flags) */
1283 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001284 xfs_filblks_t temp=0;
1285 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 enum { /* bit number definitions for state */
1287 LEFT_CONTIG, RIGHT_CONTIG,
1288 LEFT_FILLING, RIGHT_FILLING,
1289 LEFT_DELAY, RIGHT_DELAY,
1290 LEFT_VALID, RIGHT_VALID
1291 };
1292
1293#define LEFT r[0]
1294#define RIGHT r[1]
1295#define PREV r[2]
1296#define MASK(b) (1 << (b))
1297#define MASK2(a,b) (MASK(a) | MASK(b))
1298#define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1299#define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1300#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1301#define STATE_TEST(b) (state & MASK(b))
1302#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1303 ((state &= ~MASK(b)), 0))
1304#define SWITCH_STATE \
1305 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1306
1307 /*
1308 * Set up a bunch of variables to make the tests simpler.
1309 */
1310 error = 0;
1311 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001312 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1313 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 xfs_bmbt_get_all(ep, &PREV);
1315 newext = new->br_state;
1316 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1317 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1318 ASSERT(PREV.br_state == oldext);
1319 new_endoff = new->br_startoff + new->br_blockcount;
1320 ASSERT(PREV.br_startoff <= new->br_startoff);
1321 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1322 /*
1323 * Set flags determining what part of the previous oldext allocation
1324 * extent is being replaced by a newext allocation.
1325 */
1326 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
1327 STATE_SET(RIGHT_FILLING,
1328 PREV.br_startoff + PREV.br_blockcount == new_endoff);
1329 /*
1330 * Check and set flags if this segment has a left neighbor.
1331 * Don't set contiguous if the combined extent would be too large.
1332 */
1333 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001334 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
1336 }
1337 STATE_SET(LEFT_CONTIG,
1338 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1339 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1340 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1341 LEFT.br_state == newext &&
1342 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1343 /*
1344 * Check and set flags if this segment has a right neighbor.
1345 * Don't set contiguous if the combined extent would be too large.
1346 * Also check for all-three-contiguous being too large.
1347 */
1348 if (STATE_SET_TEST(RIGHT_VALID,
1349 idx <
1350 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001351 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
1353 }
1354 STATE_SET(RIGHT_CONTIG,
1355 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1356 new_endoff == RIGHT.br_startoff &&
1357 new->br_startblock + new->br_blockcount ==
1358 RIGHT.br_startblock &&
1359 newext == RIGHT.br_state &&
1360 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1361 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
1362 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
1363 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1364 <= MAXEXTLEN));
1365 /*
1366 * Switch out based on the FILLING and CONTIG state bits.
1367 */
1368 switch (SWITCH_STATE) {
1369
1370 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1371 /*
1372 * Setting all of a previous oldext extent to newext.
1373 * The left and right neighbors are both contiguous with new.
1374 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001375 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001377 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 LEFT.br_blockcount + PREV.br_blockcount +
1379 RIGHT.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001380 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001382 XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001383 xfs_iext_remove(ifp, idx, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 ip->i_df.if_lastex = idx - 1;
1385 ip->i_d.di_nextents -= 2;
1386 if (cur == NULL)
1387 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1388 else {
1389 rval = XFS_ILOG_CORE;
1390 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1391 RIGHT.br_startblock,
1392 RIGHT.br_blockcount, &i)))
1393 goto done;
1394 ASSERT(i == 1);
1395 if ((error = xfs_bmbt_delete(cur, &i)))
1396 goto done;
1397 ASSERT(i == 1);
1398 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1399 goto done;
1400 ASSERT(i == 1);
1401 if ((error = xfs_bmbt_delete(cur, &i)))
1402 goto done;
1403 ASSERT(i == 1);
1404 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1405 goto done;
1406 ASSERT(i == 1);
1407 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1408 LEFT.br_startblock,
1409 LEFT.br_blockcount + PREV.br_blockcount +
1410 RIGHT.br_blockcount, LEFT.br_state)))
1411 goto done;
1412 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001413 /* DELTA: Three in-core extents are replaced by one. */
1414 temp = LEFT.br_startoff;
1415 temp2 = LEFT.br_blockcount +
1416 PREV.br_blockcount +
1417 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 break;
1419
1420 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
1421 /*
1422 * Setting all of a previous oldext extent to newext.
1423 * The left neighbor is contiguous, the right is not.
1424 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001425 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001427 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 LEFT.br_blockcount + PREV.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001429 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 XFS_DATA_FORK);
1431 ip->i_df.if_lastex = idx - 1;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001432 XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001433 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 ip->i_d.di_nextents--;
1435 if (cur == NULL)
1436 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1437 else {
1438 rval = XFS_ILOG_CORE;
1439 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1440 PREV.br_startblock, PREV.br_blockcount,
1441 &i)))
1442 goto done;
1443 ASSERT(i == 1);
1444 if ((error = xfs_bmbt_delete(cur, &i)))
1445 goto done;
1446 ASSERT(i == 1);
1447 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1448 goto done;
1449 ASSERT(i == 1);
1450 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1451 LEFT.br_startblock,
1452 LEFT.br_blockcount + PREV.br_blockcount,
1453 LEFT.br_state)))
1454 goto done;
1455 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001456 /* DELTA: Two in-core extents are replaced by one. */
1457 temp = LEFT.br_startoff;
1458 temp2 = LEFT.br_blockcount +
1459 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 break;
1461
1462 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
1463 /*
1464 * Setting all of a previous oldext extent to newext.
1465 * The right neighbor is contiguous, the left is not.
1466 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001467 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 XFS_DATA_FORK);
1469 xfs_bmbt_set_blockcount(ep,
1470 PREV.br_blockcount + RIGHT.br_blockcount);
1471 xfs_bmbt_set_state(ep, newext);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001472 XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 XFS_DATA_FORK);
1474 ip->i_df.if_lastex = idx;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001475 XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001476 xfs_iext_remove(ifp, idx + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 ip->i_d.di_nextents--;
1478 if (cur == NULL)
1479 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1480 else {
1481 rval = XFS_ILOG_CORE;
1482 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1483 RIGHT.br_startblock,
1484 RIGHT.br_blockcount, &i)))
1485 goto done;
1486 ASSERT(i == 1);
1487 if ((error = xfs_bmbt_delete(cur, &i)))
1488 goto done;
1489 ASSERT(i == 1);
1490 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1491 goto done;
1492 ASSERT(i == 1);
1493 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1494 new->br_startblock,
1495 new->br_blockcount + RIGHT.br_blockcount,
1496 newext)))
1497 goto done;
1498 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001499 /* DELTA: Two in-core extents are replaced by one. */
1500 temp = PREV.br_startoff;
1501 temp2 = PREV.br_blockcount +
1502 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 break;
1504
1505 case MASK2(LEFT_FILLING, RIGHT_FILLING):
1506 /*
1507 * Setting all of a previous oldext extent to newext.
1508 * Neither the left nor right neighbors are contiguous with
1509 * the new one.
1510 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001511 XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 XFS_DATA_FORK);
1513 xfs_bmbt_set_state(ep, newext);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001514 XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 XFS_DATA_FORK);
1516 ip->i_df.if_lastex = idx;
1517 if (cur == NULL)
1518 rval = XFS_ILOG_DEXT;
1519 else {
1520 rval = 0;
1521 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1522 new->br_startblock, new->br_blockcount,
1523 &i)))
1524 goto done;
1525 ASSERT(i == 1);
1526 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1527 new->br_startblock, new->br_blockcount,
1528 newext)))
1529 goto done;
1530 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001531 /* DELTA: The in-core extent described by new changed type. */
1532 temp = new->br_startoff;
1533 temp2 = new->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 break;
1535
1536 case MASK2(LEFT_FILLING, LEFT_CONTIG):
1537 /*
1538 * Setting the first part of a previous oldext extent to newext.
1539 * The left neighbor is contiguous.
1540 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001541 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001543 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 LEFT.br_blockcount + new->br_blockcount);
1545 xfs_bmbt_set_startoff(ep,
1546 PREV.br_startoff + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001547 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001549 XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 XFS_DATA_FORK);
1551 xfs_bmbt_set_startblock(ep,
1552 new->br_startblock + new->br_blockcount);
1553 xfs_bmbt_set_blockcount(ep,
1554 PREV.br_blockcount - new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001555 XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 XFS_DATA_FORK);
1557 ip->i_df.if_lastex = idx - 1;
1558 if (cur == NULL)
1559 rval = XFS_ILOG_DEXT;
1560 else {
1561 rval = 0;
1562 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1563 PREV.br_startblock, PREV.br_blockcount,
1564 &i)))
1565 goto done;
1566 ASSERT(i == 1);
1567 if ((error = xfs_bmbt_update(cur,
1568 PREV.br_startoff + new->br_blockcount,
1569 PREV.br_startblock + new->br_blockcount,
1570 PREV.br_blockcount - new->br_blockcount,
1571 oldext)))
1572 goto done;
1573 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1574 goto done;
1575 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1576 LEFT.br_startblock,
1577 LEFT.br_blockcount + new->br_blockcount,
1578 LEFT.br_state))
1579 goto done;
1580 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001581 /* DELTA: The boundary between two in-core extents moved. */
1582 temp = LEFT.br_startoff;
1583 temp2 = LEFT.br_blockcount +
1584 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 break;
1586
1587 case MASK(LEFT_FILLING):
1588 /*
1589 * Setting the first part of a previous oldext extent to newext.
1590 * The left neighbor is not contiguous.
1591 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001592 XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1594 xfs_bmbt_set_startoff(ep, new_endoff);
1595 xfs_bmbt_set_blockcount(ep,
1596 PREV.br_blockcount - new->br_blockcount);
1597 xfs_bmbt_set_startblock(ep,
1598 new->br_startblock + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001599 XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx, XFS_DATA_FORK);
1600 XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001602 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 ip->i_df.if_lastex = idx;
1604 ip->i_d.di_nextents++;
1605 if (cur == NULL)
1606 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1607 else {
1608 rval = XFS_ILOG_CORE;
1609 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1610 PREV.br_startblock, PREV.br_blockcount,
1611 &i)))
1612 goto done;
1613 ASSERT(i == 1);
1614 if ((error = xfs_bmbt_update(cur,
1615 PREV.br_startoff + new->br_blockcount,
1616 PREV.br_startblock + new->br_blockcount,
1617 PREV.br_blockcount - new->br_blockcount,
1618 oldext)))
1619 goto done;
1620 cur->bc_rec.b = *new;
1621 if ((error = xfs_bmbt_insert(cur, &i)))
1622 goto done;
1623 ASSERT(i == 1);
1624 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001625 /* DELTA: One in-core extent is split in two. */
1626 temp = PREV.br_startoff;
1627 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 break;
1629
1630 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1631 /*
1632 * Setting the last part of a previous oldext extent to newext.
1633 * The right neighbor is contiguous with the new allocation.
1634 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001635 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001637 XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 XFS_DATA_FORK);
1639 xfs_bmbt_set_blockcount(ep,
1640 PREV.br_blockcount - new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001641 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001643 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1644 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 new->br_blockcount + RIGHT.br_blockcount, newext);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001646 XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 XFS_DATA_FORK);
1648 ip->i_df.if_lastex = idx + 1;
1649 if (cur == NULL)
1650 rval = XFS_ILOG_DEXT;
1651 else {
1652 rval = 0;
1653 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1654 PREV.br_startblock,
1655 PREV.br_blockcount, &i)))
1656 goto done;
1657 ASSERT(i == 1);
1658 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1659 PREV.br_startblock,
1660 PREV.br_blockcount - new->br_blockcount,
1661 oldext)))
1662 goto done;
1663 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1664 goto done;
1665 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1666 new->br_startblock,
1667 new->br_blockcount + RIGHT.br_blockcount,
1668 newext)))
1669 goto done;
1670 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001671 /* DELTA: The boundary between two in-core extents moved. */
1672 temp = PREV.br_startoff;
1673 temp2 = PREV.br_blockcount +
1674 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 break;
1676
1677 case MASK(RIGHT_FILLING):
1678 /*
1679 * Setting the last part of a previous oldext extent to newext.
1680 * The right neighbor is not contiguous.
1681 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001682 XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 xfs_bmbt_set_blockcount(ep,
1684 PREV.br_blockcount - new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001685 XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK);
1686 XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL,
1687 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001688 xfs_iext_insert(ifp, idx + 1, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 ip->i_df.if_lastex = idx + 1;
1690 ip->i_d.di_nextents++;
1691 if (cur == NULL)
1692 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1693 else {
1694 rval = XFS_ILOG_CORE;
1695 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1696 PREV.br_startblock, PREV.br_blockcount,
1697 &i)))
1698 goto done;
1699 ASSERT(i == 1);
1700 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1701 PREV.br_startblock,
1702 PREV.br_blockcount - new->br_blockcount,
1703 oldext)))
1704 goto done;
1705 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1706 new->br_startblock, new->br_blockcount,
1707 &i)))
1708 goto done;
1709 ASSERT(i == 0);
1710 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1711 if ((error = xfs_bmbt_insert(cur, &i)))
1712 goto done;
1713 ASSERT(i == 1);
1714 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001715 /* DELTA: One in-core extent is split in two. */
1716 temp = PREV.br_startoff;
1717 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 break;
1719
1720 case 0:
1721 /*
1722 * Setting the middle part of a previous oldext extent to
1723 * newext. Contiguity is impossible here.
1724 * One extent becomes three extents.
1725 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001726 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 xfs_bmbt_set_blockcount(ep,
1728 new->br_startoff - PREV.br_startoff);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001729 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 r[0] = *new;
1731 r[1].br_startoff = new_endoff;
1732 r[1].br_blockcount =
1733 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1734 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1735 r[1].br_state = oldext;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001736 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001738 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 ip->i_df.if_lastex = idx + 1;
1740 ip->i_d.di_nextents += 2;
1741 if (cur == NULL)
1742 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1743 else {
1744 rval = XFS_ILOG_CORE;
1745 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1746 PREV.br_startblock, PREV.br_blockcount,
1747 &i)))
1748 goto done;
1749 ASSERT(i == 1);
1750 /* new right extent - oldext */
1751 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1752 r[1].br_startblock, r[1].br_blockcount,
1753 r[1].br_state)))
1754 goto done;
1755 /* new left extent - oldext */
1756 PREV.br_blockcount =
1757 new->br_startoff - PREV.br_startoff;
1758 cur->bc_rec.b = PREV;
1759 if ((error = xfs_bmbt_insert(cur, &i)))
1760 goto done;
1761 ASSERT(i == 1);
1762 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1763 goto done;
1764 ASSERT(i == 1);
1765 /* new middle extent - newext */
1766 cur->bc_rec.b = *new;
1767 if ((error = xfs_bmbt_insert(cur, &i)))
1768 goto done;
1769 ASSERT(i == 1);
1770 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001771 /* DELTA: One in-core extent is split in three. */
1772 temp = PREV.br_startoff;
1773 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 break;
1775
1776 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1777 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1778 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1779 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1780 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1781 case MASK(LEFT_CONTIG):
1782 case MASK(RIGHT_CONTIG):
1783 /*
1784 * These cases are all impossible.
1785 */
1786 ASSERT(0);
1787 }
1788 *curp = cur;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001789 if (delta) {
1790 temp2 += temp;
1791 if (delta->xed_startoff > temp)
1792 delta->xed_startoff = temp;
1793 if (delta->xed_blockcount < temp2)
1794 delta->xed_blockcount = temp2;
1795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796done:
1797 *logflagsp = rval;
1798 return error;
1799#undef LEFT
1800#undef RIGHT
1801#undef PREV
1802#undef MASK
1803#undef MASK2
1804#undef MASK3
1805#undef MASK4
1806#undef STATE_SET
1807#undef STATE_TEST
1808#undef STATE_SET_TEST
1809#undef SWITCH_STATE
1810}
1811
1812/*
1813 * Called by xfs_bmap_add_extent to handle cases converting a hole
1814 * to a delayed allocation.
1815 */
1816/*ARGSUSED*/
1817STATIC int /* error */
1818xfs_bmap_add_extent_hole_delay(
1819 xfs_inode_t *ip, /* incore inode pointer */
1820 xfs_extnum_t idx, /* extent number to update/insert */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001821 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001823 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 int rsvd) /* OK to allocate reserved blocks */
1825{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001826 xfs_bmbt_rec_t *ep; /* extent record for idx */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001827 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1829 xfs_filblks_t newlen=0; /* new indirect size */
1830 xfs_filblks_t oldlen=0; /* old indirect size */
1831 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1832 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001833 xfs_filblks_t temp=0; /* temp for indirect calculations */
1834 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 enum { /* bit number definitions for state */
1836 LEFT_CONTIG, RIGHT_CONTIG,
1837 LEFT_DELAY, RIGHT_DELAY,
1838 LEFT_VALID, RIGHT_VALID
1839 };
1840
1841#define MASK(b) (1 << (b))
1842#define MASK2(a,b) (MASK(a) | MASK(b))
1843#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1844#define STATE_TEST(b) (state & MASK(b))
1845#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1846 ((state &= ~MASK(b)), 0))
1847#define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1848
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001849 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1850 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 state = 0;
1852 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1853 /*
1854 * Check and set flags if this segment has a left neighbor
1855 */
1856 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001857 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1859 }
1860 /*
1861 * Check and set flags if the current (right) segment exists.
1862 * If it doesn't exist, we're converting the hole at end-of-file.
1863 */
1864 if (STATE_SET_TEST(RIGHT_VALID,
1865 idx <
1866 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1867 xfs_bmbt_get_all(ep, &right);
1868 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1869 }
1870 /*
1871 * Set contiguity flags on the left and right neighbors.
1872 * Don't let extents get too large, even if the pieces are contiguous.
1873 */
1874 STATE_SET(LEFT_CONTIG,
1875 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1876 left.br_startoff + left.br_blockcount == new->br_startoff &&
1877 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1878 STATE_SET(RIGHT_CONTIG,
1879 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1880 new->br_startoff + new->br_blockcount == right.br_startoff &&
1881 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1882 (!STATE_TEST(LEFT_CONTIG) ||
1883 (left.br_blockcount + new->br_blockcount +
1884 right.br_blockcount <= MAXEXTLEN)));
1885 /*
1886 * Switch out based on the contiguity flags.
1887 */
1888 switch (SWITCH_STATE) {
1889
1890 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1891 /*
1892 * New allocation is contiguous with delayed allocations
1893 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001894 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 */
1896 temp = left.br_blockcount + new->br_blockcount +
1897 right.br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001898 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001900 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 oldlen = STARTBLOCKVAL(left.br_startblock) +
1902 STARTBLOCKVAL(new->br_startblock) +
1903 STARTBLOCKVAL(right.br_startblock);
1904 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001905 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1906 NULLSTARTBLOCK((int)newlen));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001907 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 XFS_DATA_FORK);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001909 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001910 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 ip->i_df.if_lastex = idx - 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001912 /* DELTA: Two in-core extents were replaced by one. */
1913 temp2 = temp;
1914 temp = left.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 break;
1916
1917 case MASK(LEFT_CONTIG):
1918 /*
1919 * New allocation is contiguous with a delayed allocation
1920 * on the left.
1921 * Merge the new allocation with the left neighbor.
1922 */
1923 temp = left.br_blockcount + new->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001924 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001926 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 oldlen = STARTBLOCKVAL(left.br_startblock) +
1928 STARTBLOCKVAL(new->br_startblock);
1929 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001930 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1931 NULLSTARTBLOCK((int)newlen));
Eric Sandeen3a59c942007-07-11 11:09:47 +10001932 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 XFS_DATA_FORK);
1934 ip->i_df.if_lastex = idx - 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001935 /* DELTA: One in-core extent grew into a hole. */
1936 temp2 = temp;
1937 temp = left.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 break;
1939
1940 case MASK(RIGHT_CONTIG):
1941 /*
1942 * New allocation is contiguous with a delayed allocation
1943 * on the right.
1944 * Merge the new allocation with the right neighbor.
1945 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10001946 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 temp = new->br_blockcount + right.br_blockcount;
1948 oldlen = STARTBLOCKVAL(new->br_startblock) +
1949 STARTBLOCKVAL(right.br_startblock);
1950 newlen = xfs_bmap_worst_indlen(ip, temp);
1951 xfs_bmbt_set_allf(ep, new->br_startoff,
1952 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
Eric Sandeen3a59c942007-07-11 11:09:47 +10001953 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 ip->i_df.if_lastex = idx;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001955 /* DELTA: One in-core extent grew into a hole. */
1956 temp2 = temp;
1957 temp = new->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 break;
1959
1960 case 0:
1961 /*
1962 * New allocation is not contiguous with another
1963 * delayed allocation.
1964 * Insert a new entry.
1965 */
1966 oldlen = newlen = 0;
Eric Sandeen3a59c942007-07-11 11:09:47 +10001967 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001969 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 ip->i_df.if_lastex = idx;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001971 /* DELTA: A new in-core extent was added in a hole. */
1972 temp2 = new->br_blockcount;
1973 temp = new->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 break;
1975 }
1976 if (oldlen != newlen) {
1977 ASSERT(oldlen > newlen);
1978 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11001979 (int64_t)(oldlen - newlen), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 /*
1981 * Nothing to do for disk quota accounting here.
1982 */
1983 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001984 if (delta) {
1985 temp2 += temp;
1986 if (delta->xed_startoff > temp)
1987 delta->xed_startoff = temp;
1988 if (delta->xed_blockcount < temp2)
1989 delta->xed_blockcount = temp2;
1990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 *logflagsp = 0;
1992 return 0;
1993#undef MASK
1994#undef MASK2
1995#undef STATE_SET
1996#undef STATE_TEST
1997#undef STATE_SET_TEST
1998#undef SWITCH_STATE
1999}
2000
2001/*
2002 * Called by xfs_bmap_add_extent to handle cases converting a hole
2003 * to a real allocation.
2004 */
2005STATIC int /* error */
2006xfs_bmap_add_extent_hole_real(
2007 xfs_inode_t *ip, /* incore inode pointer */
2008 xfs_extnum_t idx, /* extent number to update/insert */
2009 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002010 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002012 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 int whichfork) /* data or attr fork */
2014{
2015 xfs_bmbt_rec_t *ep; /* pointer to extent entry ins. point */
2016 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 int i; /* temp state */
2018 xfs_ifork_t *ifp; /* inode fork pointer */
2019 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2020 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002021 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002023 xfs_filblks_t temp=0;
2024 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 enum { /* bit number definitions for state */
2026 LEFT_CONTIG, RIGHT_CONTIG,
2027 LEFT_DELAY, RIGHT_DELAY,
2028 LEFT_VALID, RIGHT_VALID
2029 };
2030
2031#define MASK(b) (1 << (b))
2032#define MASK2(a,b) (MASK(a) | MASK(b))
2033#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
2034#define STATE_TEST(b) (state & MASK(b))
2035#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
2036 ((state &= ~MASK(b)), 0))
2037#define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
2038
2039 ifp = XFS_IFORK_PTR(ip, whichfork);
2040 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002041 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 state = 0;
2043 /*
2044 * Check and set flags if this segment has a left neighbor.
2045 */
2046 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002047 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
2049 }
2050 /*
2051 * Check and set flags if this segment has a current value.
2052 * Not true if we're inserting into the "hole" at eof.
2053 */
2054 if (STATE_SET_TEST(RIGHT_VALID,
2055 idx <
2056 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
2057 xfs_bmbt_get_all(ep, &right);
2058 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
2059 }
2060 /*
2061 * We're inserting a real allocation between "left" and "right".
2062 * Set the contiguity flags. Don't let extents get too large.
2063 */
2064 STATE_SET(LEFT_CONTIG,
2065 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
2066 left.br_startoff + left.br_blockcount == new->br_startoff &&
2067 left.br_startblock + left.br_blockcount == new->br_startblock &&
2068 left.br_state == new->br_state &&
2069 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
2070 STATE_SET(RIGHT_CONTIG,
2071 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
2072 new->br_startoff + new->br_blockcount == right.br_startoff &&
2073 new->br_startblock + new->br_blockcount ==
2074 right.br_startblock &&
2075 new->br_state == right.br_state &&
2076 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2077 (!STATE_TEST(LEFT_CONTIG) ||
2078 left.br_blockcount + new->br_blockcount +
2079 right.br_blockcount <= MAXEXTLEN));
2080
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002081 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 /*
2083 * Select which case we're in here, and implement it.
2084 */
2085 switch (SWITCH_STATE) {
2086
2087 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
2088 /*
2089 * New allocation is contiguous with real allocations on the
2090 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002091 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002093 XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002095 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 left.br_blockcount + new->br_blockcount +
2097 right.br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002098 XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 whichfork);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002100 XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002101 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 ifp->if_lastex = idx - 1;
2103 XFS_IFORK_NEXT_SET(ip, whichfork,
2104 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2105 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002106 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2107 } else {
2108 rval = XFS_ILOG_CORE;
2109 if ((error = xfs_bmbt_lookup_eq(cur,
2110 right.br_startoff,
2111 right.br_startblock,
2112 right.br_blockcount, &i)))
2113 goto done;
2114 ASSERT(i == 1);
2115 if ((error = xfs_bmbt_delete(cur, &i)))
2116 goto done;
2117 ASSERT(i == 1);
2118 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2119 goto done;
2120 ASSERT(i == 1);
2121 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2122 left.br_startblock,
2123 left.br_blockcount +
2124 new->br_blockcount +
2125 right.br_blockcount,
2126 left.br_state)))
2127 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002129 /* DELTA: Two in-core extents were replaced by one. */
2130 temp = left.br_startoff;
2131 temp2 = left.br_blockcount +
2132 new->br_blockcount +
2133 right.br_blockcount;
2134 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 case MASK(LEFT_CONTIG):
2137 /*
2138 * New allocation is contiguous with a real allocation
2139 * on the left.
2140 * Merge the new allocation with the left neighbor.
2141 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002142 XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002143 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 left.br_blockcount + new->br_blockcount);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002145 XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 ifp->if_lastex = idx - 1;
2147 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002148 rval = XFS_ILOG_FEXT(whichfork);
2149 } else {
2150 rval = 0;
2151 if ((error = xfs_bmbt_lookup_eq(cur,
2152 left.br_startoff,
2153 left.br_startblock,
2154 left.br_blockcount, &i)))
2155 goto done;
2156 ASSERT(i == 1);
2157 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2158 left.br_startblock,
2159 left.br_blockcount +
2160 new->br_blockcount,
2161 left.br_state)))
2162 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002164 /* DELTA: One in-core extent grew. */
2165 temp = left.br_startoff;
2166 temp2 = left.br_blockcount +
2167 new->br_blockcount;
2168 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 case MASK(RIGHT_CONTIG):
2171 /*
2172 * New allocation is contiguous with a real allocation
2173 * on the right.
2174 * Merge the new allocation with the right neighbor.
2175 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002176 XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2178 new->br_blockcount + right.br_blockcount,
2179 right.br_state);
Eric Sandeen3a59c942007-07-11 11:09:47 +10002180 XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 ifp->if_lastex = idx;
2182 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002183 rval = XFS_ILOG_FEXT(whichfork);
2184 } else {
2185 rval = 0;
2186 if ((error = xfs_bmbt_lookup_eq(cur,
2187 right.br_startoff,
2188 right.br_startblock,
2189 right.br_blockcount, &i)))
2190 goto done;
2191 ASSERT(i == 1);
2192 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2193 new->br_startblock,
2194 new->br_blockcount +
2195 right.br_blockcount,
2196 right.br_state)))
2197 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002199 /* DELTA: One in-core extent grew. */
2200 temp = new->br_startoff;
2201 temp2 = new->br_blockcount +
2202 right.br_blockcount;
2203 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 case 0:
2206 /*
2207 * New allocation is not contiguous with another
2208 * real allocation.
2209 * Insert a new entry.
2210 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10002211 XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002212 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 ifp->if_lastex = idx;
2214 XFS_IFORK_NEXT_SET(ip, whichfork,
2215 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2216 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002217 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2218 } else {
2219 rval = XFS_ILOG_CORE;
2220 if ((error = xfs_bmbt_lookup_eq(cur,
2221 new->br_startoff,
2222 new->br_startblock,
2223 new->br_blockcount, &i)))
2224 goto done;
2225 ASSERT(i == 0);
2226 cur->bc_rec.b.br_state = new->br_state;
2227 if ((error = xfs_bmbt_insert(cur, &i)))
2228 goto done;
2229 ASSERT(i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002231 /* DELTA: A new extent was added in a hole. */
2232 temp = new->br_startoff;
2233 temp2 = new->br_blockcount;
2234 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002236 if (delta) {
2237 temp2 += temp;
2238 if (delta->xed_startoff > temp)
2239 delta->xed_startoff = temp;
2240 if (delta->xed_blockcount < temp2)
2241 delta->xed_blockcount = temp2;
2242 }
2243done:
2244 *logflagsp = rval;
2245 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246#undef MASK
2247#undef MASK2
2248#undef STATE_SET
2249#undef STATE_TEST
2250#undef STATE_SET_TEST
2251#undef SWITCH_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
Nathan Scottdd9f4382006-01-11 15:28:28 +11002254/*
2255 * Adjust the size of the new extent based on di_extsize and rt extsize.
2256 */
2257STATIC int
2258xfs_bmap_extsize_align(
2259 xfs_mount_t *mp,
2260 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2261 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2262 xfs_extlen_t extsz, /* align to this extent size */
2263 int rt, /* is this a realtime inode? */
2264 int eof, /* is extent at end-of-file? */
2265 int delay, /* creating delalloc extent? */
2266 int convert, /* overwriting unwritten extent? */
2267 xfs_fileoff_t *offp, /* in/out: aligned offset */
2268 xfs_extlen_t *lenp) /* in/out: aligned length */
2269{
2270 xfs_fileoff_t orig_off; /* original offset */
2271 xfs_extlen_t orig_alen; /* original length */
2272 xfs_fileoff_t orig_end; /* original off+len */
2273 xfs_fileoff_t nexto; /* next file offset */
2274 xfs_fileoff_t prevo; /* previous file offset */
2275 xfs_fileoff_t align_off; /* temp for offset */
2276 xfs_extlen_t align_alen; /* temp for length */
2277 xfs_extlen_t temp; /* temp for calculations */
2278
2279 if (convert)
2280 return 0;
2281
2282 orig_off = align_off = *offp;
2283 orig_alen = align_alen = *lenp;
2284 orig_end = orig_off + orig_alen;
2285
2286 /*
2287 * If this request overlaps an existing extent, then don't
2288 * attempt to perform any additional alignment.
2289 */
2290 if (!delay && !eof &&
2291 (orig_off >= gotp->br_startoff) &&
2292 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2293 return 0;
2294 }
2295
2296 /*
2297 * If the file offset is unaligned vs. the extent size
2298 * we need to align it. This will be possible unless
2299 * the file was previously written with a kernel that didn't
2300 * perform this alignment, or if a truncate shot us in the
2301 * foot.
2302 */
2303 temp = do_mod(orig_off, extsz);
2304 if (temp) {
2305 align_alen += temp;
2306 align_off -= temp;
2307 }
2308 /*
2309 * Same adjustment for the end of the requested area.
2310 */
2311 if ((temp = (align_alen % extsz))) {
2312 align_alen += extsz - temp;
2313 }
2314 /*
2315 * If the previous block overlaps with this proposed allocation
2316 * then move the start forward without adjusting the length.
2317 */
2318 if (prevp->br_startoff != NULLFILEOFF) {
2319 if (prevp->br_startblock == HOLESTARTBLOCK)
2320 prevo = prevp->br_startoff;
2321 else
2322 prevo = prevp->br_startoff + prevp->br_blockcount;
2323 } else
2324 prevo = 0;
2325 if (align_off != orig_off && align_off < prevo)
2326 align_off = prevo;
2327 /*
2328 * If the next block overlaps with this proposed allocation
2329 * then move the start back without adjusting the length,
2330 * but not before offset 0.
2331 * This may of course make the start overlap previous block,
2332 * and if we hit the offset 0 limit then the next block
2333 * can still overlap too.
2334 */
2335 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2336 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2337 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2338 nexto = gotp->br_startoff + gotp->br_blockcount;
2339 else
2340 nexto = gotp->br_startoff;
2341 } else
2342 nexto = NULLFILEOFF;
2343 if (!eof &&
2344 align_off + align_alen != orig_end &&
2345 align_off + align_alen > nexto)
2346 align_off = nexto > align_alen ? nexto - align_alen : 0;
2347 /*
2348 * If we're now overlapping the next or previous extent that
2349 * means we can't fit an extsz piece in this hole. Just move
2350 * the start forward to the first valid spot and set
2351 * the length so we hit the end.
2352 */
2353 if (align_off != orig_off && align_off < prevo)
2354 align_off = prevo;
2355 if (align_off + align_alen != orig_end &&
2356 align_off + align_alen > nexto &&
2357 nexto != NULLFILEOFF) {
2358 ASSERT(nexto > prevo);
2359 align_alen = nexto - align_off;
2360 }
2361
2362 /*
2363 * If realtime, and the result isn't a multiple of the realtime
2364 * extent size we need to remove blocks until it is.
2365 */
2366 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2367 /*
2368 * We're not covering the original request, or
2369 * we won't be able to once we fix the length.
2370 */
2371 if (orig_off < align_off ||
2372 orig_end > align_off + align_alen ||
2373 align_alen - temp < orig_alen)
2374 return XFS_ERROR(EINVAL);
2375 /*
2376 * Try to fix it by moving the start up.
2377 */
2378 if (align_off + temp <= orig_off) {
2379 align_alen -= temp;
2380 align_off += temp;
2381 }
2382 /*
2383 * Try to fix it by moving the end in.
2384 */
2385 else if (align_off + align_alen - temp >= orig_end)
2386 align_alen -= temp;
2387 /*
2388 * Set the start to the minimum then trim the length.
2389 */
2390 else {
2391 align_alen -= orig_off - align_off;
2392 align_off = orig_off;
2393 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2394 }
2395 /*
2396 * Result doesn't cover the request, fail it.
2397 */
2398 if (orig_off < align_off || orig_end > align_off + align_alen)
2399 return XFS_ERROR(EINVAL);
2400 } else {
2401 ASSERT(orig_off >= align_off);
2402 ASSERT(orig_end <= align_off + align_alen);
2403 }
2404
2405#ifdef DEBUG
2406 if (!eof && gotp->br_startoff != NULLFILEOFF)
2407 ASSERT(align_off + align_alen <= gotp->br_startoff);
2408 if (prevp->br_startoff != NULLFILEOFF)
2409 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2410#endif
2411
2412 *lenp = align_alen;
2413 *offp = align_off;
2414 return 0;
2415}
2416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417#define XFS_ALLOC_GAP_UNITS 4
2418
Nathan Scottdd9f4382006-01-11 15:28:28 +11002419STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11002420xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2422{
2423 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2425 xfs_mount_t *mp; /* mount point structure */
2426 int nullfb; /* true if ap->firstblock isn't set */
2427 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
2429#define ISVALID(x,y) \
2430 (rt ? \
2431 (x) < mp->m_sb.sb_rblocks : \
2432 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2433 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2434 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 mp = ap->ip->i_mount;
2437 nullfb = ap->firstblock == NULLFSBLOCK;
2438 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2439 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 /*
2441 * If allocating at eof, and there's a previous real block,
2442 * try to use it's last block as our starting point.
2443 */
2444 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2445 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2446 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2447 ap->prevp->br_startblock)) {
2448 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2449 /*
2450 * Adjust for the gap between prevp and us.
2451 */
2452 adjust = ap->off -
2453 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2454 if (adjust &&
2455 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2456 ap->rval += adjust;
2457 }
2458 /*
2459 * If not at eof, then compare the two neighbor blocks.
2460 * Figure out whether either one gives us a good starting point,
2461 * and pick the better one.
2462 */
2463 else if (!ap->eof) {
2464 xfs_fsblock_t gotbno; /* right side block number */
2465 xfs_fsblock_t gotdiff=0; /* right side difference */
2466 xfs_fsblock_t prevbno; /* left side block number */
2467 xfs_fsblock_t prevdiff=0; /* left side difference */
2468
2469 /*
2470 * If there's a previous (left) block, select a requested
2471 * start block based on it.
2472 */
2473 if (ap->prevp->br_startoff != NULLFILEOFF &&
2474 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2475 (prevbno = ap->prevp->br_startblock +
2476 ap->prevp->br_blockcount) &&
2477 ISVALID(prevbno, ap->prevp->br_startblock)) {
2478 /*
2479 * Calculate gap to end of previous block.
2480 */
2481 adjust = prevdiff = ap->off -
2482 (ap->prevp->br_startoff +
2483 ap->prevp->br_blockcount);
2484 /*
2485 * Figure the startblock based on the previous block's
2486 * end and the gap size.
2487 * Heuristic!
2488 * If the gap is large relative to the piece we're
2489 * allocating, or using it gives us an invalid block
2490 * number, then just use the end of the previous block.
2491 */
2492 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2493 ISVALID(prevbno + prevdiff,
2494 ap->prevp->br_startblock))
2495 prevbno += adjust;
2496 else
2497 prevdiff += adjust;
2498 /*
2499 * If the firstblock forbids it, can't use it,
2500 * must use default.
2501 */
2502 if (!rt && !nullfb &&
2503 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2504 prevbno = NULLFSBLOCK;
2505 }
2506 /*
2507 * No previous block or can't follow it, just default.
2508 */
2509 else
2510 prevbno = NULLFSBLOCK;
2511 /*
2512 * If there's a following (right) block, select a requested
2513 * start block based on it.
2514 */
2515 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
2516 /*
2517 * Calculate gap to start of next block.
2518 */
2519 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2520 /*
2521 * Figure the startblock based on the next block's
2522 * start and the gap size.
2523 */
2524 gotbno = ap->gotp->br_startblock;
2525 /*
2526 * Heuristic!
2527 * If the gap is large relative to the piece we're
2528 * allocating, or using it gives us an invalid block
2529 * number, then just use the start of the next block
2530 * offset by our length.
2531 */
2532 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2533 ISVALID(gotbno - gotdiff, gotbno))
2534 gotbno -= adjust;
2535 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2536 gotbno -= ap->alen;
2537 gotdiff += adjust - ap->alen;
2538 } else
2539 gotdiff += adjust;
2540 /*
2541 * If the firstblock forbids it, can't use it,
2542 * must use default.
2543 */
2544 if (!rt && !nullfb &&
2545 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2546 gotbno = NULLFSBLOCK;
2547 }
2548 /*
2549 * No next block, just default.
2550 */
2551 else
2552 gotbno = NULLFSBLOCK;
2553 /*
2554 * If both valid, pick the better one, else the only good
2555 * one, else ap->rval is already set (to 0 or the inode block).
2556 */
2557 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2558 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2559 else if (prevbno != NULLFSBLOCK)
2560 ap->rval = prevbno;
2561 else if (gotbno != NULLFSBLOCK)
2562 ap->rval = gotbno;
2563 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002564#undef ISVALID
2565 return 0;
2566}
2567
2568STATIC int
2569xfs_bmap_rtalloc(
2570 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2571{
2572 xfs_alloctype_t atype = 0; /* type for allocation routines */
2573 int error; /* error return value */
2574 xfs_mount_t *mp; /* mount point structure */
2575 xfs_extlen_t prod = 0; /* product factor for allocators */
2576 xfs_extlen_t ralen = 0; /* realtime allocation length */
2577 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002578 xfs_rtblock_t rtb;
2579
2580 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002581 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002582 prod = align / mp->m_sb.sb_rextsize;
2583 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2584 align, 1, ap->eof, 0,
2585 ap->conv, &ap->off, &ap->alen);
2586 if (error)
2587 return error;
2588 ASSERT(ap->alen);
2589 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2590
2591 /*
2592 * If the offset & length are not perfectly aligned
2593 * then kill prod, it will just get us in trouble.
2594 */
2595 if (do_mod(ap->off, align) || ap->alen % align)
2596 prod = 1;
2597 /*
2598 * Set ralen to be the actual requested length in rtextents.
2599 */
2600 ralen = ap->alen / mp->m_sb.sb_rextsize;
2601 /*
2602 * If the old value was close enough to MAXEXTLEN that
2603 * we rounded up to it, cut it back so it's valid again.
2604 * Note that if it's a really large request (bigger than
2605 * MAXEXTLEN), we don't hear about that number, and can't
2606 * adjust the starting point to match it.
2607 */
2608 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2609 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2610 /*
2611 * If it's an allocation to an empty file at offset 0,
2612 * pick an extent that will space things out in the rt area.
2613 */
2614 if (ap->eof && ap->off == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10002615 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2616
Nathan Scotta365bdd2006-03-14 13:34:16 +11002617 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2618 if (error)
2619 return error;
2620 ap->rval = rtx * mp->m_sb.sb_rextsize;
2621 } else {
2622 ap->rval = 0;
2623 }
2624
2625 xfs_bmap_adjacent(ap);
2626
2627 /*
2628 * Realtime allocation, done through xfs_rtallocate_extent.
2629 */
2630 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2631 do_div(ap->rval, mp->m_sb.sb_rextsize);
2632 rtb = ap->rval;
2633 ap->alen = ralen;
2634 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2635 &ralen, atype, ap->wasdel, prod, &rtb)))
2636 return error;
2637 if (rtb == NULLFSBLOCK && prod > 1 &&
2638 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2639 ap->alen, &ralen, atype,
2640 ap->wasdel, 1, &rtb)))
2641 return error;
2642 ap->rval = rtb;
2643 if (ap->rval != NULLFSBLOCK) {
2644 ap->rval *= mp->m_sb.sb_rextsize;
2645 ralen *= mp->m_sb.sb_rextsize;
2646 ap->alen = ralen;
2647 ap->ip->i_d.di_nblocks += ralen;
2648 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2649 if (ap->wasdel)
2650 ap->ip->i_delayed_blks -= ralen;
2651 /*
2652 * Adjust the disk quota also. This was reserved
2653 * earlier.
2654 */
2655 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2656 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2657 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2658 } else {
2659 ap->alen = 0;
2660 }
2661 return 0;
2662}
2663
2664STATIC int
2665xfs_bmap_btalloc(
2666 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2667{
2668 xfs_mount_t *mp; /* mount point structure */
2669 xfs_alloctype_t atype = 0; /* type for allocation routines */
2670 xfs_extlen_t align; /* minimum allocation alignment */
2671 xfs_agnumber_t ag;
2672 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2673 xfs_agnumber_t startag;
2674 xfs_alloc_arg_t args;
2675 xfs_extlen_t blen;
2676 xfs_extlen_t delta;
2677 xfs_extlen_t longest;
2678 xfs_extlen_t need;
2679 xfs_extlen_t nextminlen = 0;
2680 xfs_perag_t *pag;
2681 int nullfb; /* true if ap->firstblock isn't set */
2682 int isaligned;
2683 int notinit;
2684 int tryagain;
2685 int error;
2686
2687 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002688 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002689 if (unlikely(align)) {
2690 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2691 align, 0, ap->eof, 0, ap->conv,
2692 &ap->off, &ap->alen);
2693 ASSERT(!error);
2694 ASSERT(ap->alen);
2695 }
2696 nullfb = ap->firstblock == NULLFSBLOCK;
2697 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10002698 if (nullfb) {
2699 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2700 ag = xfs_filestream_lookup_ag(ap->ip);
2701 ag = (ag != NULLAGNUMBER) ? ag : 0;
2702 ap->rval = XFS_AGB_TO_FSB(mp, ag, 0);
2703 } else {
2704 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2705 }
2706 } else
Nathan Scotta365bdd2006-03-14 13:34:16 +11002707 ap->rval = ap->firstblock;
2708
2709 xfs_bmap_adjacent(ap);
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 /*
2712 * If allowed, use ap->rval; otherwise must use firstblock since
2713 * it's in the right allocation group.
2714 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002715 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 ;
2717 else
2718 ap->rval = ap->firstblock;
2719 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 * Normal allocation, done through xfs_alloc_vextent.
2721 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002722 tryagain = isaligned = 0;
2723 args.tp = ap->tp;
2724 args.mp = mp;
2725 args.fsbno = ap->rval;
2726 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
Yingping Lud210a282006-06-09 14:55:18 +10002727 args.firstblock = ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002728 blen = 0;
2729 if (nullfb) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002730 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2731 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2732 else
2733 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002734 args.total = ap->total;
David Chinner2a82b8b2007-07-11 11:09:12 +10002735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10002737 * Search for an allocation group with a single extent
2738 * large enough for the request.
2739 *
2740 * If one isn't found, then adjust the minimum allocation
2741 * size to the largest space found.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002743 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
David Chinner2a82b8b2007-07-11 11:09:12 +10002744 if (startag == NULLAGNUMBER)
2745 startag = ag = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002746 notinit = 0;
2747 down_read(&mp->m_peraglock);
2748 while (blen < ap->alen) {
2749 pag = &mp->m_perag[ag];
2750 if (!pag->pagf_init &&
2751 (error = xfs_alloc_pagf_init(mp, args.tp,
2752 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2753 up_read(&mp->m_peraglock);
2754 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002756 /*
2757 * See xfs_alloc_fix_freelist...
2758 */
2759 if (pag->pagf_init) {
2760 need = XFS_MIN_FREELIST_PAG(pag, mp);
2761 delta = need > pag->pagf_flcount ?
2762 need - pag->pagf_flcount : 0;
2763 longest = (pag->pagf_longest > delta) ?
2764 (pag->pagf_longest - delta) :
2765 (pag->pagf_flcount > 0 ||
2766 pag->pagf_longest > 0);
2767 if (blen < longest)
2768 blen = longest;
2769 } else
2770 notinit = 1;
David Chinner2a82b8b2007-07-11 11:09:12 +10002771
2772 if (xfs_inode_is_filestream(ap->ip)) {
2773 if (blen >= ap->alen)
2774 break;
2775
2776 if (ap->userdata) {
2777 /*
2778 * If startag is an invalid AG, we've
2779 * come here once before and
2780 * xfs_filestream_new_ag picked the
2781 * best currently available.
2782 *
2783 * Don't continue looping, since we
2784 * could loop forever.
2785 */
2786 if (startag == NULLAGNUMBER)
2787 break;
2788
2789 error = xfs_filestream_new_ag(ap, &ag);
2790 if (error) {
2791 up_read(&mp->m_peraglock);
2792 return error;
2793 }
2794
2795 /* loop again to set 'blen'*/
2796 startag = NULLAGNUMBER;
2797 continue;
2798 }
2799 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002800 if (++ag == mp->m_sb.sb_agcount)
2801 ag = 0;
2802 if (ag == startag)
2803 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002805 up_read(&mp->m_peraglock);
2806 /*
2807 * Since the above loop did a BUF_TRYLOCK, it is
2808 * possible that there is space for this request.
2809 */
2810 if (notinit || blen < ap->minlen)
2811 args.minlen = ap->minlen;
2812 /*
2813 * If the best seen length is less than the request
2814 * length, use the best as the minimum.
2815 */
2816 else if (blen < ap->alen)
2817 args.minlen = blen;
2818 /*
2819 * Otherwise we've seen an extent as big as alen,
2820 * use that as the minimum.
2821 */
2822 else
2823 args.minlen = ap->alen;
David Chinner2a82b8b2007-07-11 11:09:12 +10002824
2825 /*
2826 * set the failure fallback case to look in the selected
2827 * AG as the stream may have moved.
2828 */
2829 if (xfs_inode_is_filestream(ap->ip))
2830 ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002831 } else if (ap->low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002832 if (xfs_inode_is_filestream(ap->ip))
2833 args.type = XFS_ALLOCTYPE_FIRST_AG;
2834 else
2835 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002836 args.total = args.minlen = ap->minlen;
2837 } else {
2838 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2839 args.total = ap->total;
2840 args.minlen = ap->minlen;
2841 }
David Chinner957d0eb2007-06-18 16:50:37 +10002842 /* apply extent size hints if obtained earlier */
2843 if (unlikely(align)) {
2844 args.prod = align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002845 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2846 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Nathan Scott6fe90e6d2006-06-19 08:40:12 +10002847 } else if (mp->m_sb.sb_blocksize >= NBPP) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11002848 args.prod = 1;
2849 args.mod = 0;
2850 } else {
2851 args.prod = NBPP >> mp->m_sb.sb_blocklog;
2852 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2853 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2854 }
2855 /*
2856 * If we are not low on available data blocks, and the
2857 * underlying logical volume manager is a stripe, and
2858 * the file offset is zero then try to allocate data
2859 * blocks on stripe unit boundary.
2860 * NOTE: ap->aeof is only set if the allocation length
2861 * is >= the stripe unit and the allocation offset is
2862 * at the end of file.
2863 */
2864 if (!ap->low && ap->aeof) {
2865 if (!ap->off) {
2866 args.alignment = mp->m_dalign;
2867 atype = args.type;
2868 isaligned = 1;
2869 /*
2870 * Adjust for alignment
2871 */
2872 if (blen > args.alignment && blen <= ap->alen)
2873 args.minlen = blen - args.alignment;
2874 args.minalignslop = 0;
2875 } else {
2876 /*
2877 * First try an exact bno allocation.
2878 * If it fails then do a near or start bno
2879 * allocation with alignment turned on.
2880 */
2881 atype = args.type;
2882 tryagain = 1;
2883 args.type = XFS_ALLOCTYPE_THIS_BNO;
2884 args.alignment = 1;
2885 /*
2886 * Compute the minlen+alignment for the
2887 * next case. Set slop so that the value
2888 * of minlen+alignment+slop doesn't go up
2889 * between the calls.
2890 */
2891 if (blen > mp->m_dalign && blen <= ap->alen)
2892 nextminlen = blen - mp->m_dalign;
2893 else
2894 nextminlen = args.minlen;
2895 if (nextminlen + mp->m_dalign > args.minlen + 1)
2896 args.minalignslop =
2897 nextminlen + mp->m_dalign -
2898 args.minlen - 1;
2899 else
2900 args.minalignslop = 0;
2901 }
2902 } else {
2903 args.alignment = 1;
2904 args.minalignslop = 0;
2905 }
2906 args.minleft = ap->minleft;
2907 args.wasdel = ap->wasdel;
2908 args.isfl = 0;
2909 args.userdata = ap->userdata;
2910 if ((error = xfs_alloc_vextent(&args)))
2911 return error;
2912 if (tryagain && args.fsbno == NULLFSBLOCK) {
2913 /*
2914 * Exact allocation failed. Now try with alignment
2915 * turned on.
2916 */
2917 args.type = atype;
2918 args.fsbno = ap->rval;
2919 args.alignment = mp->m_dalign;
2920 args.minlen = nextminlen;
2921 args.minalignslop = 0;
2922 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 if ((error = xfs_alloc_vextent(&args)))
2924 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002925 }
2926 if (isaligned && args.fsbno == NULLFSBLOCK) {
2927 /*
2928 * allocation failed, so turn off alignment and
2929 * try again.
2930 */
2931 args.type = atype;
2932 args.fsbno = ap->rval;
2933 args.alignment = 0;
2934 if ((error = xfs_alloc_vextent(&args)))
2935 return error;
2936 }
2937 if (args.fsbno == NULLFSBLOCK && nullfb &&
2938 args.minlen > ap->minlen) {
2939 args.minlen = ap->minlen;
2940 args.type = XFS_ALLOCTYPE_START_BNO;
2941 args.fsbno = ap->rval;
2942 if ((error = xfs_alloc_vextent(&args)))
2943 return error;
2944 }
2945 if (args.fsbno == NULLFSBLOCK && nullfb) {
2946 args.fsbno = 0;
2947 args.type = XFS_ALLOCTYPE_FIRST_AG;
2948 args.total = ap->minlen;
2949 args.minleft = 0;
2950 if ((error = xfs_alloc_vextent(&args)))
2951 return error;
2952 ap->low = 1;
2953 }
2954 if (args.fsbno != NULLFSBLOCK) {
2955 ap->firstblock = ap->rval = args.fsbno;
2956 ASSERT(nullfb || fb_agno == args.agno ||
2957 (ap->low && fb_agno < args.agno));
2958 ap->alen = args.len;
2959 ap->ip->i_d.di_nblocks += args.len;
2960 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2961 if (ap->wasdel)
2962 ap->ip->i_delayed_blks -= args.len;
2963 /*
2964 * Adjust the disk quota also. This was reserved
2965 * earlier.
2966 */
2967 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2968 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2969 XFS_TRANS_DQ_BCOUNT,
2970 (long) args.len);
2971 } else {
2972 ap->rval = NULLFSBLOCK;
2973 ap->alen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
2975 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002976}
2977
2978/*
2979 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2980 * It figures out where to ask the underlying allocator to put the new extent.
2981 */
2982STATIC int
2983xfs_bmap_alloc(
2984 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2985{
2986 if ((ap->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) && ap->userdata)
2987 return xfs_bmap_rtalloc(ap);
2988 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989}
2990
2991/*
2992 * Transform a btree format file with only one leaf node, where the
2993 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002994 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 * give up the space for the btree root and pitch the leaf block.
2996 */
2997STATIC int /* error */
2998xfs_bmap_btree_to_extents(
2999 xfs_trans_t *tp, /* transaction pointer */
3000 xfs_inode_t *ip, /* incore inode pointer */
3001 xfs_btree_cur_t *cur, /* btree cursor */
3002 int *logflagsp, /* inode logging flags */
3003 int whichfork) /* data or attr fork */
3004{
3005 /* REFERENCED */
3006 xfs_bmbt_block_t *cblock;/* child btree block */
3007 xfs_fsblock_t cbno; /* child block number */
3008 xfs_buf_t *cbp; /* child block's buffer */
3009 int error; /* error return value */
3010 xfs_ifork_t *ifp; /* inode fork data */
3011 xfs_mount_t *mp; /* mount point structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10003012 __be64 *pp; /* ptr to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 xfs_bmbt_block_t *rblock;/* root btree block */
3014
3015 ifp = XFS_IFORK_PTR(ip, whichfork);
3016 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3017 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
3018 rblock = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003019 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
3020 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
3022 mp = ip->i_mount;
3023 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10003024 cbno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 *logflagsp = 0;
3026#ifdef DEBUG
Christoph Hellwig576039c2006-09-28 10:58:06 +10003027 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 return error;
3029#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
3031 XFS_BMAP_BTREE_REF)))
3032 return error;
3033 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
3034 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
3035 return error;
3036 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
3037 ip->i_d.di_nblocks--;
3038 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
3039 xfs_trans_binval(tp, cbp);
3040 if (cur->bc_bufs[0] == cbp)
3041 cur->bc_bufs[0] = NULL;
3042 xfs_iroot_realloc(ip, -1, whichfork);
3043 ASSERT(ifp->if_broot == NULL);
3044 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
3045 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3046 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
3047 return 0;
3048}
3049
3050/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003051 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 * after removing space (or undoing a delayed allocation).
3053 */
3054STATIC int /* error */
3055xfs_bmap_del_extent(
3056 xfs_inode_t *ip, /* incore inode pointer */
3057 xfs_trans_t *tp, /* current transaction pointer */
3058 xfs_extnum_t idx, /* extent number to update/delete */
3059 xfs_bmap_free_t *flist, /* list of extents to be freed */
3060 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003061 xfs_bmbt_irec_t *del, /* data to remove from extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003063 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 int whichfork, /* data or attr fork */
3065 int rsvd) /* OK to allocate reserved blocks */
3066{
3067 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
3068 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
3069 xfs_fsblock_t del_endblock=0; /* first block past del */
3070 xfs_fileoff_t del_endoff; /* first offset past del */
3071 int delay; /* current block is delayed allocated */
3072 int do_fx; /* free extent at end of routine */
3073 xfs_bmbt_rec_t *ep; /* current extent entry pointer */
3074 int error; /* error return value */
3075 int flags; /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 xfs_bmbt_irec_t got; /* current extent entry */
3077 xfs_fileoff_t got_endoff; /* first offset past got */
3078 int i; /* temp state */
3079 xfs_ifork_t *ifp; /* inode fork pointer */
3080 xfs_mount_t *mp; /* mount structure */
3081 xfs_filblks_t nblks; /* quota/sb block count */
3082 xfs_bmbt_irec_t new; /* new record to be inserted */
3083 /* REFERENCED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 uint qfield; /* quota field to update */
3085 xfs_filblks_t temp; /* for indirect length calculations */
3086 xfs_filblks_t temp2; /* for indirect length calculations */
3087
3088 XFS_STATS_INC(xs_del_exlist);
3089 mp = ip->i_mount;
3090 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003091 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
3092 (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 ASSERT(del->br_blockcount > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003094 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 xfs_bmbt_get_all(ep, &got);
3096 ASSERT(got.br_startoff <= del->br_startoff);
3097 del_endoff = del->br_startoff + del->br_blockcount;
3098 got_endoff = got.br_startoff + got.br_blockcount;
3099 ASSERT(got_endoff >= del_endoff);
3100 delay = ISNULLSTARTBLOCK(got.br_startblock);
3101 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
3102 flags = 0;
3103 qfield = 0;
3104 error = 0;
3105 /*
3106 * If deleting a real allocation, must free up the disk space.
3107 */
3108 if (!delay) {
3109 flags = XFS_ILOG_CORE;
3110 /*
3111 * Realtime allocation. Free it and record di_nblocks update.
3112 */
3113 if (whichfork == XFS_DATA_FORK &&
3114 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
3115 xfs_fsblock_t bno;
3116 xfs_filblks_t len;
3117
3118 ASSERT(do_mod(del->br_blockcount,
3119 mp->m_sb.sb_rextsize) == 0);
3120 ASSERT(do_mod(del->br_startblock,
3121 mp->m_sb.sb_rextsize) == 0);
3122 bno = del->br_startblock;
3123 len = del->br_blockcount;
3124 do_div(bno, mp->m_sb.sb_rextsize);
3125 do_div(len, mp->m_sb.sb_rextsize);
3126 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3127 (xfs_extlen_t)len)))
3128 goto done;
3129 do_fx = 0;
3130 nblks = len * mp->m_sb.sb_rextsize;
3131 qfield = XFS_TRANS_DQ_RTBCOUNT;
3132 }
3133 /*
3134 * Ordinary allocation.
3135 */
3136 else {
3137 do_fx = 1;
3138 nblks = del->br_blockcount;
3139 qfield = XFS_TRANS_DQ_BCOUNT;
3140 }
3141 /*
3142 * Set up del_endblock and cur for later.
3143 */
3144 del_endblock = del->br_startblock + del->br_blockcount;
3145 if (cur) {
3146 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3147 got.br_startblock, got.br_blockcount,
3148 &i)))
3149 goto done;
3150 ASSERT(i == 1);
3151 }
3152 da_old = da_new = 0;
3153 } else {
3154 da_old = STARTBLOCKVAL(got.br_startblock);
3155 da_new = 0;
3156 nblks = 0;
3157 do_fx = 0;
3158 }
3159 /*
3160 * Set flag value to use in switch statement.
3161 * Left-contig is 2, right-contig is 1.
3162 */
3163 switch (((got.br_startoff == del->br_startoff) << 1) |
3164 (got_endoff == del_endoff)) {
3165 case 3:
3166 /*
3167 * Matches the whole extent. Delete the entry.
3168 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10003169 XFS_BMAP_TRACE_DELETE("3", ip, idx, 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003170 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 ifp->if_lastex = idx;
3172 if (delay)
3173 break;
3174 XFS_IFORK_NEXT_SET(ip, whichfork,
3175 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3176 flags |= XFS_ILOG_CORE;
3177 if (!cur) {
3178 flags |= XFS_ILOG_FEXT(whichfork);
3179 break;
3180 }
3181 if ((error = xfs_bmbt_delete(cur, &i)))
3182 goto done;
3183 ASSERT(i == 1);
3184 break;
3185
3186 case 2:
3187 /*
3188 * Deleting the first part of the extent.
3189 */
Eric Sandeen3a59c942007-07-11 11:09:47 +10003190 XFS_BMAP_TRACE_PRE_UPDATE("2", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 xfs_bmbt_set_startoff(ep, del_endoff);
3192 temp = got.br_blockcount - del->br_blockcount;
3193 xfs_bmbt_set_blockcount(ep, temp);
3194 ifp->if_lastex = idx;
3195 if (delay) {
3196 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3197 da_old);
3198 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10003199 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 whichfork);
3201 da_new = temp;
3202 break;
3203 }
3204 xfs_bmbt_set_startblock(ep, del_endblock);
Eric Sandeen3a59c942007-07-11 11:09:47 +10003205 XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 if (!cur) {
3207 flags |= XFS_ILOG_FEXT(whichfork);
3208 break;
3209 }
3210 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3211 got.br_blockcount - del->br_blockcount,
3212 got.br_state)))
3213 goto done;
3214 break;
3215
3216 case 1:
3217 /*
3218 * Deleting the last part of the extent.
3219 */
3220 temp = got.br_blockcount - del->br_blockcount;
Eric Sandeen3a59c942007-07-11 11:09:47 +10003221 XFS_BMAP_TRACE_PRE_UPDATE("1", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 xfs_bmbt_set_blockcount(ep, temp);
3223 ifp->if_lastex = idx;
3224 if (delay) {
3225 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3226 da_old);
3227 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
Eric Sandeen3a59c942007-07-11 11:09:47 +10003228 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 whichfork);
3230 da_new = temp;
3231 break;
3232 }
Eric Sandeen3a59c942007-07-11 11:09:47 +10003233 XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 if (!cur) {
3235 flags |= XFS_ILOG_FEXT(whichfork);
3236 break;
3237 }
3238 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3239 got.br_startblock,
3240 got.br_blockcount - del->br_blockcount,
3241 got.br_state)))
3242 goto done;
3243 break;
3244
3245 case 0:
3246 /*
3247 * Deleting the middle of the extent.
3248 */
3249 temp = del->br_startoff - got.br_startoff;
Eric Sandeen3a59c942007-07-11 11:09:47 +10003250 XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 xfs_bmbt_set_blockcount(ep, temp);
3252 new.br_startoff = del_endoff;
3253 temp2 = got_endoff - del_endoff;
3254 new.br_blockcount = temp2;
3255 new.br_state = got.br_state;
3256 if (!delay) {
3257 new.br_startblock = del_endblock;
3258 flags |= XFS_ILOG_CORE;
3259 if (cur) {
3260 if ((error = xfs_bmbt_update(cur,
3261 got.br_startoff,
3262 got.br_startblock, temp,
3263 got.br_state)))
3264 goto done;
3265 if ((error = xfs_bmbt_increment(cur, 0, &i)))
3266 goto done;
3267 cur->bc_rec.b = new;
3268 error = xfs_bmbt_insert(cur, &i);
3269 if (error && error != ENOSPC)
3270 goto done;
3271 /*
3272 * If get no-space back from btree insert,
3273 * it tried a split, and we have a zero
3274 * block reservation.
3275 * Fix up our state and return the error.
3276 */
3277 if (error == ENOSPC) {
3278 /*
3279 * Reset the cursor, don't trust
3280 * it after any insert operation.
3281 */
3282 if ((error = xfs_bmbt_lookup_eq(cur,
3283 got.br_startoff,
3284 got.br_startblock,
3285 temp, &i)))
3286 goto done;
3287 ASSERT(i == 1);
3288 /*
3289 * Update the btree record back
3290 * to the original value.
3291 */
3292 if ((error = xfs_bmbt_update(cur,
3293 got.br_startoff,
3294 got.br_startblock,
3295 got.br_blockcount,
3296 got.br_state)))
3297 goto done;
3298 /*
3299 * Reset the extent record back
3300 * to the original value.
3301 */
3302 xfs_bmbt_set_blockcount(ep,
3303 got.br_blockcount);
3304 flags = 0;
3305 error = XFS_ERROR(ENOSPC);
3306 goto done;
3307 }
3308 ASSERT(i == 1);
3309 } else
3310 flags |= XFS_ILOG_FEXT(whichfork);
3311 XFS_IFORK_NEXT_SET(ip, whichfork,
3312 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3313 } else {
3314 ASSERT(whichfork == XFS_DATA_FORK);
3315 temp = xfs_bmap_worst_indlen(ip, temp);
3316 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3317 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3318 new.br_startblock = NULLSTARTBLOCK((int)temp2);
3319 da_new = temp + temp2;
3320 while (da_new > da_old) {
3321 if (temp) {
3322 temp--;
3323 da_new--;
3324 xfs_bmbt_set_startblock(ep,
3325 NULLSTARTBLOCK((int)temp));
3326 }
3327 if (da_new == da_old)
3328 break;
3329 if (temp2) {
3330 temp2--;
3331 da_new--;
3332 new.br_startblock =
3333 NULLSTARTBLOCK((int)temp2);
3334 }
3335 }
3336 }
Eric Sandeen3a59c942007-07-11 11:09:47 +10003337 XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, whichfork);
3338 XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 1, &new, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003340 xfs_iext_insert(ifp, idx + 1, 1, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 ifp->if_lastex = idx + 1;
3342 break;
3343 }
3344 /*
3345 * If we need to, add to list of extents to delete.
3346 */
3347 if (do_fx)
3348 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3349 mp);
3350 /*
3351 * Adjust inode # blocks in the file.
3352 */
3353 if (nblks)
3354 ip->i_d.di_nblocks -= nblks;
3355 /*
3356 * Adjust quota data.
3357 */
3358 if (qfield)
3359 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
3360
3361 /*
3362 * Account for change in delayed indirect blocks.
3363 * Nothing to do for disk quota accounting here.
3364 */
3365 ASSERT(da_old >= da_new);
3366 if (da_old > da_new)
David Chinner20f4ebf2007-02-10 18:36:10 +11003367 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 rsvd);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003369 if (delta) {
3370 /* DELTA: report the original extent. */
3371 if (delta->xed_startoff > got.br_startoff)
3372 delta->xed_startoff = got.br_startoff;
3373 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3374 delta->xed_blockcount = got.br_startoff +
3375 got.br_blockcount;
3376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377done:
3378 *logflagsp = flags;
3379 return error;
3380}
3381
3382/*
3383 * Remove the entry "free" from the free item list. Prev points to the
3384 * previous entry, unless "free" is the head of the list.
3385 */
3386STATIC void
3387xfs_bmap_del_free(
3388 xfs_bmap_free_t *flist, /* free item list header */
3389 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3390 xfs_bmap_free_item_t *free) /* list item to be freed */
3391{
3392 if (prev)
3393 prev->xbfi_next = free->xbfi_next;
3394 else
3395 flist->xbf_first = free->xbfi_next;
3396 flist->xbf_count--;
3397 kmem_zone_free(xfs_bmap_free_item_zone, free);
3398}
3399
3400/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 * Convert an extents-format file into a btree-format file.
3402 * The new file will have a root block (in the inode) and a single child block.
3403 */
3404STATIC int /* error */
3405xfs_bmap_extents_to_btree(
3406 xfs_trans_t *tp, /* transaction pointer */
3407 xfs_inode_t *ip, /* incore inode pointer */
3408 xfs_fsblock_t *firstblock, /* first-block-allocated */
3409 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3410 xfs_btree_cur_t **curp, /* cursor returned to caller */
3411 int wasdel, /* converting a delayed alloc */
3412 int *logflagsp, /* inode logging flags */
3413 int whichfork) /* data or attr fork */
3414{
3415 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
3416 xfs_buf_t *abp; /* buffer for ablock */
3417 xfs_alloc_arg_t args; /* allocation arguments */
3418 xfs_bmbt_rec_t *arp; /* child record pointer */
3419 xfs_bmbt_block_t *block; /* btree root block */
3420 xfs_btree_cur_t *cur; /* bmap btree cursor */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003421 xfs_bmbt_rec_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003423 xfs_extnum_t i, cnt; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 xfs_ifork_t *ifp; /* inode fork pointer */
3425 xfs_bmbt_key_t *kp; /* root block key pointer */
3426 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003427 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3429
3430 ifp = XFS_IFORK_PTR(ip, whichfork);
3431 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3432 ASSERT(ifp->if_ext_max ==
3433 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3434 /*
3435 * Make space in the inode incore.
3436 */
3437 xfs_iroot_realloc(ip, 1, whichfork);
3438 ifp->if_flags |= XFS_IFBROOT;
3439 /*
3440 * Fill in the root.
3441 */
3442 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003443 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3444 block->bb_level = cpu_to_be16(1);
3445 block->bb_numrecs = cpu_to_be16(1);
3446 block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3447 block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 /*
3449 * Need a cursor. Can't allocate until bb_level is filled in.
3450 */
3451 mp = ip->i_mount;
3452 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
3453 whichfork);
3454 cur->bc_private.b.firstblock = *firstblock;
3455 cur->bc_private.b.flist = flist;
3456 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3457 /*
3458 * Convert to a btree with two levels, one record in root.
3459 */
3460 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3461 args.tp = tp;
3462 args.mp = mp;
Yingping Lud210a282006-06-09 14:55:18 +10003463 args.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 if (*firstblock == NULLFSBLOCK) {
3465 args.type = XFS_ALLOCTYPE_START_BNO;
3466 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3467 } else if (flist->xbf_low) {
3468 args.type = XFS_ALLOCTYPE_START_BNO;
3469 args.fsbno = *firstblock;
3470 } else {
3471 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3472 args.fsbno = *firstblock;
3473 }
3474 args.minlen = args.maxlen = args.prod = 1;
3475 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3476 args.minalignslop = 0;
3477 args.wasdel = wasdel;
3478 *logflagsp = 0;
3479 if ((error = xfs_alloc_vextent(&args))) {
3480 xfs_iroot_realloc(ip, -1, whichfork);
3481 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3482 return error;
3483 }
3484 /*
3485 * Allocation can't fail, the space was reserved.
3486 */
3487 ASSERT(args.fsbno != NULLFSBLOCK);
3488 ASSERT(*firstblock == NULLFSBLOCK ||
3489 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3490 (flist->xbf_low &&
3491 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3492 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3493 cur->bc_private.b.allocated++;
3494 ip->i_d.di_nblocks++;
3495 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3496 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3497 /*
3498 * Fill in the child block.
3499 */
3500 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003501 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 ablock->bb_level = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003503 ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3504 ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3506 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003507 for (cnt = i = 0; i < nextents; i++) {
3508 ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
3510 arp->l0 = INT_GET(ep->l0, ARCH_CONVERT);
3511 arp->l1 = INT_GET(ep->l1, ARCH_CONVERT);
3512 arp++; cnt++;
3513 }
3514 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11003515 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3516 ablock->bb_numrecs = cpu_to_be16(cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 /*
3518 * Fill in the root key and pointer.
3519 */
3520 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
3521 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
Christoph Hellwig8801bb92006-09-28 10:58:17 +10003522 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
Christoph Hellwig576039c2006-09-28 10:58:06 +10003524 *pp = cpu_to_be64(args.fsbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 /*
3526 * Do all this logging at the end so that
3527 * the root is at the right level.
3528 */
3529 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003530 xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 ASSERT(*curp == NULL);
3532 *curp = cur;
3533 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
3534 return 0;
3535}
3536
3537/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11003538 * Helper routine to reset inode di_forkoff field when switching
3539 * attribute fork from local to extent format - we reset it where
3540 * possible to make space available for inline data fork extents.
3541 */
3542STATIC void
3543xfs_bmap_forkoff_reset(
3544 xfs_mount_t *mp,
3545 xfs_inode_t *ip,
3546 int whichfork)
3547{
3548 if (whichfork == XFS_ATTR_FORK &&
3549 (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
3550 (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
Barry Naujoke5889e92007-02-10 18:35:58 +11003551 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
Nathan Scottd8cc8902005-11-02 10:34:53 +11003552 ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
3553 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3554 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
3555 (uint)sizeof(xfs_bmbt_rec_t);
3556 ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
3557 (uint)sizeof(xfs_bmbt_rec_t);
3558 }
3559}
3560
3561/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 * Convert a local file to an extents file.
3563 * This code is out of bounds for data forks of regular files,
3564 * since the file data needs to get logged so things will stay consistent.
3565 * (The bmap-level manipulations are ok, though).
3566 */
3567STATIC int /* error */
3568xfs_bmap_local_to_extents(
3569 xfs_trans_t *tp, /* transaction pointer */
3570 xfs_inode_t *ip, /* incore inode pointer */
3571 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3572 xfs_extlen_t total, /* total blocks needed by transaction */
3573 int *logflagsp, /* inode logging flags */
3574 int whichfork) /* data or attr fork */
3575{
3576 int error; /* error return value */
3577 int flags; /* logging flags returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 xfs_ifork_t *ifp; /* inode fork pointer */
3579
3580 /*
3581 * We don't want to deal with the case of keeping inode data inline yet.
3582 * So sending the data fork of a regular inode is invalid.
3583 */
3584 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3585 whichfork == XFS_DATA_FORK));
3586 ifp = XFS_IFORK_PTR(ip, whichfork);
3587 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3588 flags = 0;
3589 error = 0;
3590 if (ifp->if_bytes) {
3591 xfs_alloc_arg_t args; /* allocation arguments */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003592 xfs_buf_t *bp; /* buffer for extent block */
3593 xfs_bmbt_rec_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
3595 args.tp = tp;
3596 args.mp = ip->i_mount;
Yingping Lud210a282006-06-09 14:55:18 +10003597 args.firstblock = *firstblock;
Mandy Kirkconnellf020b672006-03-14 14:07:24 +11003598 ASSERT((ifp->if_flags &
3599 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 /*
3601 * Allocate a block. We know we need only one, since the
3602 * file currently fits in an inode.
3603 */
3604 if (*firstblock == NULLFSBLOCK) {
3605 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3606 args.type = XFS_ALLOCTYPE_START_BNO;
3607 } else {
3608 args.fsbno = *firstblock;
3609 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3610 }
3611 args.total = total;
3612 args.mod = args.minleft = args.alignment = args.wasdel =
3613 args.isfl = args.minalignslop = 0;
3614 args.minlen = args.maxlen = args.prod = 1;
3615 if ((error = xfs_alloc_vextent(&args)))
3616 goto done;
3617 /*
3618 * Can't fail, the space was reserved.
3619 */
3620 ASSERT(args.fsbno != NULLFSBLOCK);
3621 ASSERT(args.len == 1);
3622 *firstblock = args.fsbno;
3623 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3624 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3625 ifp->if_bytes);
3626 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003627 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003629 xfs_iext_add(ifp, 0, 1);
3630 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
Eric Sandeen3a59c942007-07-11 11:09:47 +10003632 XFS_BMAP_TRACE_POST_UPDATE("new", ip, 0, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3634 ip->i_d.di_nblocks = 1;
3635 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
3636 XFS_TRANS_DQ_BCOUNT, 1L);
3637 flags |= XFS_ILOG_FEXT(whichfork);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003638 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003640 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 ifp->if_flags &= ~XFS_IFINLINE;
3643 ifp->if_flags |= XFS_IFEXTENTS;
3644 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3645 flags |= XFS_ILOG_CORE;
3646done:
3647 *logflagsp = flags;
3648 return error;
3649}
3650
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003652 * Search the extent records for the entry containing block bno.
3653 * If bno lies in a hole, point to the next entry. If bno lies
3654 * past eof, *eofp will be set, and *prevp will contain the last
3655 * entry (null if none). Else, *lastxp will be set to the index
3656 * of the found entry; *gotp will contain the entry.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003657 */
3658xfs_bmbt_rec_t * /* pointer to found extent entry */
3659xfs_bmap_search_multi_extents(
3660 xfs_ifork_t *ifp, /* inode fork pointer */
3661 xfs_fileoff_t bno, /* block number searched for */
3662 int *eofp, /* out: end of file found */
3663 xfs_extnum_t *lastxp, /* out: last extent index */
3664 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3665 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3666{
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003667 xfs_bmbt_rec_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003668 xfs_extnum_t lastx; /* last extent index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003669
3670 /*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003671 * Initialize the extent entry structure to catch access to
3672 * uninitialized br_startblock field.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003673 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003674 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3675 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3676 gotp->br_state = XFS_EXT_INVALID;
3677#if XFS_BIG_BLKNOS
3678 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3679#else
3680 gotp->br_startblock = 0xffffa5a5;
3681#endif
3682 prevp->br_startoff = NULLFILEOFF;
3683
3684 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3685 if (lastx > 0) {
3686 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3687 }
3688 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3689 xfs_bmbt_get_all(ep, gotp);
3690 *eofp = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003691 } else {
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003692 if (lastx > 0) {
3693 *gotp = *prevp;
3694 }
3695 *eofp = 1;
3696 ep = NULL;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003697 }
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003698 *lastxp = lastx;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003699 return ep;
3700}
3701
3702/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 * Search the extents list for the inode, for the extent containing bno.
3704 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3705 * *eofp will be set, and *prevp will contain the last entry (null if none).
3706 * Else, *lastxp will be set to the index of the found
3707 * entry; *gotp will contain the entry.
3708 */
3709STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
3710xfs_bmap_search_extents(
3711 xfs_inode_t *ip, /* incore inode pointer */
3712 xfs_fileoff_t bno, /* block number searched for */
Nathan Scott572d95f2006-09-28 11:03:20 +10003713 int fork, /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 int *eofp, /* out: end of file found */
3715 xfs_extnum_t *lastxp, /* out: last extent index */
3716 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3717 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3718{
3719 xfs_ifork_t *ifp; /* inode fork pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003720 xfs_bmbt_rec_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
3722 XFS_STATS_INC(xs_look_exlist);
Nathan Scott572d95f2006-09-28 11:03:20 +10003723 ifp = XFS_IFORK_PTR(ip, fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003725 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3726
Nathan Scott572d95f2006-09-28 11:03:20 +10003727 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3728 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3729 xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount,
3730 "Access to block zero in inode %llu "
3731 "start_block: %llx start_off: %llx "
3732 "blkcnt: %llx extent-state: %x lastx: %x\n",
3733 (unsigned long long)ip->i_ino,
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003734 (unsigned long long)gotp->br_startblock,
3735 (unsigned long long)gotp->br_startoff,
3736 (unsigned long long)gotp->br_blockcount,
Nathan Scott572d95f2006-09-28 11:03:20 +10003737 gotp->br_state, *lastxp);
3738 *lastxp = NULLEXTNUM;
3739 *eofp = 1;
3740 return NULL;
3741 }
3742 return ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743}
3744
3745
3746#ifdef XFS_BMAP_TRACE
3747ktrace_t *xfs_bmap_trace_buf;
3748
3749/*
3750 * Add a bmap trace buffer entry. Base routine for the others.
3751 */
3752STATIC void
3753xfs_bmap_trace_addentry(
3754 int opcode, /* operation */
Eric Sandeen3a59c942007-07-11 11:09:47 +10003755 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 char *desc, /* operation description */
3757 xfs_inode_t *ip, /* incore inode pointer */
3758 xfs_extnum_t idx, /* index of entry(ies) */
3759 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
3760 xfs_bmbt_rec_t *r1, /* first record */
3761 xfs_bmbt_rec_t *r2, /* second record or null */
3762 int whichfork) /* data or attr fork */
3763{
3764 xfs_bmbt_rec_t tr2;
3765
3766 ASSERT(cnt == 1 || cnt == 2);
3767 ASSERT(r1 != NULL);
3768 if (cnt == 1) {
3769 ASSERT(r2 == NULL);
3770 r2 = &tr2;
3771 memset(&tr2, 0, sizeof(tr2));
3772 } else
3773 ASSERT(r2 != NULL);
3774 ktrace_enter(xfs_bmap_trace_buf,
3775 (void *)(__psint_t)(opcode | (whichfork << 16)),
3776 (void *)fname, (void *)desc, (void *)ip,
3777 (void *)(__psint_t)idx,
3778 (void *)(__psint_t)cnt,
3779 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3780 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3781 (void *)(__psunsigned_t)(r1->l0 >> 32),
3782 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3783 (void *)(__psunsigned_t)(r1->l1 >> 32),
3784 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3785 (void *)(__psunsigned_t)(r2->l0 >> 32),
3786 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3787 (void *)(__psunsigned_t)(r2->l1 >> 32),
3788 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3789 );
3790 ASSERT(ip->i_xtrace);
3791 ktrace_enter(ip->i_xtrace,
3792 (void *)(__psint_t)(opcode | (whichfork << 16)),
3793 (void *)fname, (void *)desc, (void *)ip,
3794 (void *)(__psint_t)idx,
3795 (void *)(__psint_t)cnt,
3796 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3797 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3798 (void *)(__psunsigned_t)(r1->l0 >> 32),
3799 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3800 (void *)(__psunsigned_t)(r1->l1 >> 32),
3801 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3802 (void *)(__psunsigned_t)(r2->l0 >> 32),
3803 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3804 (void *)(__psunsigned_t)(r2->l1 >> 32),
3805 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3806 );
3807}
3808
3809/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003810 * Add bmap trace entry prior to a call to xfs_iext_remove.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 */
3812STATIC void
3813xfs_bmap_trace_delete(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003814 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 char *desc, /* operation description */
3816 xfs_inode_t *ip, /* incore inode pointer */
3817 xfs_extnum_t idx, /* index of entry(entries) deleted */
3818 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3819 int whichfork) /* data or attr fork */
3820{
3821 xfs_ifork_t *ifp; /* inode fork pointer */
3822
3823 ifp = XFS_IFORK_PTR(ip, whichfork);
3824 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003825 cnt, xfs_iext_get_ext(ifp, idx),
3826 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 whichfork);
3828}
3829
3830/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003831 * Add bmap trace entry prior to a call to xfs_iext_insert, or
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 * reading in the extents list from the disk (in the btree).
3833 */
3834STATIC void
3835xfs_bmap_trace_insert(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003836 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 char *desc, /* operation description */
3838 xfs_inode_t *ip, /* incore inode pointer */
3839 xfs_extnum_t idx, /* index of entry(entries) inserted */
3840 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3841 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3842 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3843 int whichfork) /* data or attr fork */
3844{
3845 xfs_bmbt_rec_t tr1; /* compressed record 1 */
3846 xfs_bmbt_rec_t tr2; /* compressed record 2 if needed */
3847
3848 xfs_bmbt_set_all(&tr1, r1);
3849 if (cnt == 2) {
3850 ASSERT(r2 != NULL);
3851 xfs_bmbt_set_all(&tr2, r2);
3852 } else {
3853 ASSERT(cnt == 1);
3854 ASSERT(r2 == NULL);
3855 }
3856 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3857 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3858}
3859
3860/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003861 * Add bmap trace entry after updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 */
3863STATIC void
3864xfs_bmap_trace_post_update(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003865 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 char *desc, /* operation description */
3867 xfs_inode_t *ip, /* incore inode pointer */
3868 xfs_extnum_t idx, /* index of entry updated */
3869 int whichfork) /* data or attr fork */
3870{
3871 xfs_ifork_t *ifp; /* inode fork pointer */
3872
3873 ifp = XFS_IFORK_PTR(ip, whichfork);
3874 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003875 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876}
3877
3878/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003879 * Add bmap trace entry prior to updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 */
3881STATIC void
3882xfs_bmap_trace_pre_update(
Eric Sandeen3a59c942007-07-11 11:09:47 +10003883 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 char *desc, /* operation description */
3885 xfs_inode_t *ip, /* incore inode pointer */
3886 xfs_extnum_t idx, /* index of entry to be updated */
3887 int whichfork) /* data or attr fork */
3888{
3889 xfs_ifork_t *ifp; /* inode fork pointer */
3890
3891 ifp = XFS_IFORK_PTR(ip, whichfork);
3892 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003893 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894}
3895#endif /* XFS_BMAP_TRACE */
3896
3897/*
3898 * Compute the worst-case number of indirect blocks that will be used
3899 * for ip's delayed extent of length "len".
3900 */
3901STATIC xfs_filblks_t
3902xfs_bmap_worst_indlen(
3903 xfs_inode_t *ip, /* incore inode pointer */
3904 xfs_filblks_t len) /* delayed extent length */
3905{
3906 int level; /* btree level number */
3907 int maxrecs; /* maximum record count at this level */
3908 xfs_mount_t *mp; /* mount structure */
3909 xfs_filblks_t rval; /* return value */
3910
3911 mp = ip->i_mount;
3912 maxrecs = mp->m_bmap_dmxr[0];
3913 for (level = 0, rval = 0;
3914 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3915 level++) {
3916 len += maxrecs - 1;
3917 do_div(len, maxrecs);
3918 rval += len;
3919 if (len == 1)
3920 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3921 level - 1;
3922 if (level == 0)
3923 maxrecs = mp->m_bmap_dmxr[1];
3924 }
3925 return rval;
3926}
3927
3928#if defined(XFS_RW_TRACE)
3929STATIC void
3930xfs_bunmap_trace(
3931 xfs_inode_t *ip,
3932 xfs_fileoff_t bno,
3933 xfs_filblks_t len,
3934 int flags,
3935 inst_t *ra)
3936{
3937 if (ip->i_rwtrace == NULL)
3938 return;
3939 ktrace_enter(ip->i_rwtrace,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003940 (void *)(__psint_t)XFS_BUNMAP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 (void *)ip,
3942 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3943 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3944 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3945 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3946 (void *)(__psint_t)len,
3947 (void *)(__psint_t)flags,
3948 (void *)(unsigned long)current_cpu(),
3949 (void *)ra,
3950 (void *)0,
3951 (void *)0,
3952 (void *)0,
3953 (void *)0,
3954 (void *)0,
3955 (void *)0);
3956}
3957#endif
3958
3959/*
3960 * Convert inode from non-attributed to attributed.
3961 * Must not be in a transaction, ip must not be locked.
3962 */
3963int /* error code */
3964xfs_bmap_add_attrfork(
3965 xfs_inode_t *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003966 int size, /* space new attribute needs */
3967 int rsvd) /* xact may use reserved blks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003970 xfs_bmap_free_t flist; /* freed extent records */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 xfs_trans_t *tp; /* transaction pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003973 unsigned long s; /* spinlock spl value */
3974 int blks; /* space reservation */
3975 int version = 1; /* superblock attr version */
3976 int committed; /* xaction was committed */
3977 int logflags; /* logging flags */
3978 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
Nathan Scottd8cc8902005-11-02 10:34:53 +11003980 ASSERT(XFS_IFORK_Q(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 ASSERT(ip->i_df.if_ext_max ==
3982 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
Nathan Scottd8cc8902005-11-02 10:34:53 +11003983
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 mp = ip->i_mount;
3985 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3986 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3987 blks = XFS_ADDAFORK_SPACE_RES(mp);
3988 if (rsvd)
3989 tp->t_flags |= XFS_TRANS_RESERVE;
3990 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3991 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3992 goto error0;
3993 xfs_ilock(ip, XFS_ILOCK_EXCL);
3994 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
3995 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3996 XFS_QMOPT_RES_REGBLKS);
3997 if (error) {
3998 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3999 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
4000 return error;
4001 }
4002 if (XFS_IFORK_Q(ip))
4003 goto error1;
4004 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
4005 /*
4006 * For inodes coming from pre-6.2 filesystems.
4007 */
4008 ASSERT(ip->i_d.di_aformat == 0);
4009 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
4010 }
4011 ASSERT(ip->i_d.di_anextents == 0);
4012 VN_HOLD(XFS_ITOV(ip));
4013 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4014 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4015 switch (ip->i_d.di_format) {
4016 case XFS_DINODE_FMT_DEV:
4017 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
4018 break;
4019 case XFS_DINODE_FMT_UUID:
4020 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
4021 break;
4022 case XFS_DINODE_FMT_LOCAL:
4023 case XFS_DINODE_FMT_EXTENTS:
4024 case XFS_DINODE_FMT_BTREE:
Nathan Scottd8cc8902005-11-02 10:34:53 +11004025 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
4026 if (!ip->i_d.di_forkoff)
4027 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
Nathan Scott13059ff2006-01-11 15:32:01 +11004028 else if (mp->m_flags & XFS_MOUNT_ATTR2)
Nathan Scottd8cc8902005-11-02 10:34:53 +11004029 version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 break;
4031 default:
4032 ASSERT(0);
4033 error = XFS_ERROR(EINVAL);
4034 goto error1;
4035 }
4036 ip->i_df.if_ext_max =
4037 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4038 ASSERT(ip->i_afp == NULL);
4039 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
4040 ip->i_afp->if_ext_max =
4041 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4042 ip->i_afp->if_flags = XFS_IFEXTENTS;
4043 logflags = 0;
4044 XFS_BMAP_INIT(&flist, &firstblock);
4045 switch (ip->i_d.di_format) {
4046 case XFS_DINODE_FMT_LOCAL:
4047 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
4048 &logflags);
4049 break;
4050 case XFS_DINODE_FMT_EXTENTS:
4051 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
4052 &flist, &logflags);
4053 break;
4054 case XFS_DINODE_FMT_BTREE:
4055 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
4056 &logflags);
4057 break;
4058 default:
4059 error = 0;
4060 break;
4061 }
4062 if (logflags)
4063 xfs_trans_log_inode(tp, ip, logflags);
4064 if (error)
4065 goto error2;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004066 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb) ||
4067 (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +11004068 __int64_t sbfields = 0;
4069
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 s = XFS_SB_LOCK(mp);
4071 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) {
4072 XFS_SB_VERSION_ADDATTR(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11004073 sbfields |= XFS_SB_VERSIONNUM;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004074 }
4075 if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2) {
4076 XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11004077 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
Nathan Scottd8cc8902005-11-02 10:34:53 +11004078 }
Nathan Scottda087ba2005-11-02 15:00:20 +11004079 if (sbfields) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 XFS_SB_UNLOCK(mp, s);
Nathan Scottda087ba2005-11-02 15:00:20 +11004081 xfs_mod_sb(tp, sbfields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 } else
4083 XFS_SB_UNLOCK(mp, s);
4084 }
Eric Sandeenf7c99b62007-02-10 18:37:16 +11004085 if ((error = xfs_bmap_finish(&tp, &flist, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 goto error2;
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004087 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 ASSERT(ip->i_df.if_ext_max ==
4089 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4090 return error;
4091error2:
4092 xfs_bmap_cancel(&flist);
4093error1:
4094 ASSERT(ismrlocked(&ip->i_lock,MR_UPDATE));
4095 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4096error0:
4097 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4098 ASSERT(ip->i_df.if_ext_max ==
4099 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4100 return error;
4101}
4102
4103/*
4104 * Add the extent to the list of extents to be free at transaction end.
4105 * The list is maintained sorted (by block number).
4106 */
4107/* ARGSUSED */
4108void
4109xfs_bmap_add_free(
4110 xfs_fsblock_t bno, /* fs block number of extent */
4111 xfs_filblks_t len, /* length of extent */
4112 xfs_bmap_free_t *flist, /* list of extents */
4113 xfs_mount_t *mp) /* mount point structure */
4114{
4115 xfs_bmap_free_item_t *cur; /* current (next) element */
4116 xfs_bmap_free_item_t *new; /* new element */
4117 xfs_bmap_free_item_t *prev; /* previous element */
4118#ifdef DEBUG
4119 xfs_agnumber_t agno;
4120 xfs_agblock_t agbno;
4121
4122 ASSERT(bno != NULLFSBLOCK);
4123 ASSERT(len > 0);
4124 ASSERT(len <= MAXEXTLEN);
4125 ASSERT(!ISNULLSTARTBLOCK(bno));
4126 agno = XFS_FSB_TO_AGNO(mp, bno);
4127 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4128 ASSERT(agno < mp->m_sb.sb_agcount);
4129 ASSERT(agbno < mp->m_sb.sb_agblocks);
4130 ASSERT(len < mp->m_sb.sb_agblocks);
4131 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4132#endif
4133 ASSERT(xfs_bmap_free_item_zone != NULL);
4134 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4135 new->xbfi_startblock = bno;
4136 new->xbfi_blockcount = (xfs_extlen_t)len;
4137 for (prev = NULL, cur = flist->xbf_first;
4138 cur != NULL;
4139 prev = cur, cur = cur->xbfi_next) {
4140 if (cur->xbfi_startblock >= bno)
4141 break;
4142 }
4143 if (prev)
4144 prev->xbfi_next = new;
4145 else
4146 flist->xbf_first = new;
4147 new->xbfi_next = cur;
4148 flist->xbf_count++;
4149}
4150
4151/*
4152 * Compute and fill in the value of the maximum depth of a bmap btree
4153 * in this filesystem. Done once, during mount.
4154 */
4155void
4156xfs_bmap_compute_maxlevels(
4157 xfs_mount_t *mp, /* file system mount structure */
4158 int whichfork) /* data or attr fork */
4159{
4160 int level; /* btree level */
4161 uint maxblocks; /* max blocks at this level */
4162 uint maxleafents; /* max leaf entries possible */
4163 int maxrootrecs; /* max records in root block */
4164 int minleafrecs; /* min records in leaf block */
4165 int minnoderecs; /* min records in node block */
4166 int sz; /* root block size */
4167
4168 /*
4169 * The maximum number of extents in a file, hence the maximum
4170 * number of leaf entries, is controlled by the type of di_nextents
4171 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4172 * (a signed 16-bit number, xfs_aextnum_t).
4173 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11004174 if (whichfork == XFS_DATA_FORK) {
4175 maxleafents = MAXEXTNUM;
Nathan Scott13059ff2006-01-11 15:32:01 +11004176 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4177 XFS_BMDR_SPACE_CALC(MINDBTPTRS) : mp->m_attroffset;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004178 } else {
4179 maxleafents = MAXAEXTNUM;
Nathan Scott13059ff2006-01-11 15:32:01 +11004180 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4181 XFS_BMDR_SPACE_CALC(MINABTPTRS) :
4182 mp->m_sb.sb_inodesize - mp->m_attroffset;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004183 }
4184 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 minleafrecs = mp->m_bmap_dmnr[0];
4186 minnoderecs = mp->m_bmap_dmnr[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4188 for (level = 1; maxblocks > 1; level++) {
4189 if (maxblocks <= maxrootrecs)
4190 maxblocks = 1;
4191 else
4192 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4193 }
4194 mp->m_bm_maxlevels[whichfork] = level;
4195}
4196
4197/*
4198 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4199 * caller. Frees all the extents that need freeing, which must be done
4200 * last due to locking considerations. We never free any extents in
4201 * the first transaction. This is to allow the caller to make the first
4202 * transaction a synchronous one so that the pointers to the data being
4203 * broken in this transaction will be permanent before the data is actually
4204 * freed. This is necessary to prevent blocks from being reallocated
4205 * and written to before the free and reallocation are actually permanent.
4206 * We do not just make the first transaction synchronous here, because
4207 * there are more efficient ways to gain the same protection in some cases
4208 * (see the file truncation code).
4209 *
4210 * Return 1 if the given transaction was committed and a new one
4211 * started, and 0 otherwise in the committed parameter.
4212 */
4213/*ARGSUSED*/
4214int /* error */
4215xfs_bmap_finish(
4216 xfs_trans_t **tp, /* transaction pointer addr */
4217 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 int *committed) /* xact committed or not */
4219{
4220 xfs_efd_log_item_t *efd; /* extent free data */
4221 xfs_efi_log_item_t *efi; /* extent free intention */
4222 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004223 xfs_bmap_free_item_t *free; /* free extent item */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 unsigned int logres; /* new log reservation */
4225 unsigned int logcount; /* new log count */
4226 xfs_mount_t *mp; /* filesystem mount structure */
4227 xfs_bmap_free_item_t *next; /* next item on free list */
4228 xfs_trans_t *ntp; /* new transaction pointer */
4229
4230 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4231 if (flist->xbf_count == 0) {
4232 *committed = 0;
4233 return 0;
4234 }
4235 ntp = *tp;
4236 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4237 for (free = flist->xbf_first; free; free = free->xbfi_next)
4238 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4239 free->xbfi_blockcount);
4240 logres = ntp->t_log_res;
4241 logcount = ntp->t_log_count;
4242 ntp = xfs_trans_dup(*tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004243 error = xfs_trans_commit(*tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 *tp = ntp;
4245 *committed = 1;
4246 /*
4247 * We have a new transaction, so we should return committed=1,
4248 * even though we're returning an error.
4249 */
4250 if (error) {
4251 return error;
4252 }
4253 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4254 logcount)))
4255 return error;
4256 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4257 for (free = flist->xbf_first; free != NULL; free = next) {
4258 next = free->xbfi_next;
4259 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4260 free->xbfi_blockcount))) {
4261 /*
4262 * The bmap free list will be cleaned up at a
4263 * higher level. The EFI will be canceled when
4264 * this transaction is aborted.
4265 * Need to force shutdown here to make sure it
4266 * happens, since this transaction may not be
4267 * dirty yet.
4268 */
4269 mp = ntp->t_mountp;
4270 if (!XFS_FORCED_SHUTDOWN(mp))
4271 xfs_force_shutdown(mp,
4272 (error == EFSCORRUPTED) ?
Nathan Scott7d04a332006-06-09 14:58:38 +10004273 SHUTDOWN_CORRUPT_INCORE :
4274 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 return error;
4276 }
4277 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4278 free->xbfi_blockcount);
4279 xfs_bmap_del_free(flist, NULL, free);
4280 }
4281 return 0;
4282}
4283
4284/*
4285 * Free up any items left in the list.
4286 */
4287void
4288xfs_bmap_cancel(
4289 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4290{
4291 xfs_bmap_free_item_t *free; /* free list item */
4292 xfs_bmap_free_item_t *next;
4293
4294 if (flist->xbf_count == 0)
4295 return;
4296 ASSERT(flist->xbf_first != NULL);
4297 for (free = flist->xbf_first; free; free = next) {
4298 next = free->xbfi_next;
4299 xfs_bmap_del_free(flist, NULL, free);
4300 }
4301 ASSERT(flist->xbf_count == 0);
4302}
4303
4304/*
4305 * Returns the file-relative block number of the first unused block(s)
4306 * in the file with at least "len" logically contiguous blocks free.
4307 * This is the lowest-address hole if the file has holes, else the first block
4308 * past the end of file.
4309 * Return 0 if the file is currently local (in-inode).
4310 */
4311int /* error */
4312xfs_bmap_first_unused(
4313 xfs_trans_t *tp, /* transaction pointer */
4314 xfs_inode_t *ip, /* incore inode */
4315 xfs_extlen_t len, /* size of hole to find */
4316 xfs_fileoff_t *first_unused, /* unused block */
4317 int whichfork) /* data or attr fork */
4318{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
4320 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004321 int idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 xfs_ifork_t *ifp; /* inode fork pointer */
4323 xfs_fileoff_t lastaddr; /* last block number seen */
4324 xfs_fileoff_t lowest; /* lowest useful block */
4325 xfs_fileoff_t max; /* starting useful block */
4326 xfs_fileoff_t off; /* offset for this block */
4327 xfs_extnum_t nextents; /* number of extent entries */
4328
4329 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4330 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4331 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4332 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4333 *first_unused = 0;
4334 return 0;
4335 }
4336 ifp = XFS_IFORK_PTR(ip, whichfork);
4337 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4338 (error = xfs_iread_extents(tp, ip, whichfork)))
4339 return error;
4340 lowest = *first_unused;
4341 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004342 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
4343 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 off = xfs_bmbt_get_startoff(ep);
4345 /*
4346 * See if the hole before this extent will work.
4347 */
4348 if (off >= lowest + len && off - max >= len) {
4349 *first_unused = max;
4350 return 0;
4351 }
4352 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4353 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4354 }
4355 *first_unused = max;
4356 return 0;
4357}
4358
4359/*
4360 * Returns the file-relative block number of the last block + 1 before
4361 * last_block (input value) in the file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004362 * This is not based on i_size, it is based on the extent records.
4363 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 */
4365int /* error */
4366xfs_bmap_last_before(
4367 xfs_trans_t *tp, /* transaction pointer */
4368 xfs_inode_t *ip, /* incore inode */
4369 xfs_fileoff_t *last_block, /* last block */
4370 int whichfork) /* data or attr fork */
4371{
4372 xfs_fileoff_t bno; /* input file offset */
4373 int eof; /* hit end of file */
4374 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4375 int error; /* error return value */
4376 xfs_bmbt_irec_t got; /* current extent value */
4377 xfs_ifork_t *ifp; /* inode fork pointer */
4378 xfs_extnum_t lastx; /* last extent used */
4379 xfs_bmbt_irec_t prev; /* previous extent value */
4380
4381 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4382 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4383 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4384 return XFS_ERROR(EIO);
4385 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4386 *last_block = 0;
4387 return 0;
4388 }
4389 ifp = XFS_IFORK_PTR(ip, whichfork);
4390 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4391 (error = xfs_iread_extents(tp, ip, whichfork)))
4392 return error;
4393 bno = *last_block - 1;
4394 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4395 &prev);
4396 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4397 if (prev.br_startoff == NULLFILEOFF)
4398 *last_block = 0;
4399 else
4400 *last_block = prev.br_startoff + prev.br_blockcount;
4401 }
4402 /*
4403 * Otherwise *last_block is already the right answer.
4404 */
4405 return 0;
4406}
4407
4408/*
4409 * Returns the file-relative block number of the first block past eof in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004410 * the file. This is not based on i_size, it is based on the extent records.
4411 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 */
4413int /* error */
4414xfs_bmap_last_offset(
4415 xfs_trans_t *tp, /* transaction pointer */
4416 xfs_inode_t *ip, /* incore inode */
4417 xfs_fileoff_t *last_block, /* last block */
4418 int whichfork) /* data or attr fork */
4419{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4421 int error; /* error return value */
4422 xfs_ifork_t *ifp; /* inode fork pointer */
4423 xfs_extnum_t nextents; /* number of extent entries */
4424
4425 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4426 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4427 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4428 return XFS_ERROR(EIO);
4429 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4430 *last_block = 0;
4431 return 0;
4432 }
4433 ifp = XFS_IFORK_PTR(ip, whichfork);
4434 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4435 (error = xfs_iread_extents(tp, ip, whichfork)))
4436 return error;
4437 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4438 if (!nextents) {
4439 *last_block = 0;
4440 return 0;
4441 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004442 ep = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4444 return 0;
4445}
4446
4447/*
4448 * Returns whether the selected fork of the inode has exactly one
4449 * block or not. For the data fork we check this matches di_size,
4450 * implying the file's range is 0..bsize-1.
4451 */
4452int /* 1=>1 block, 0=>otherwise */
4453xfs_bmap_one_block(
4454 xfs_inode_t *ip, /* incore inode */
4455 int whichfork) /* data or attr fork */
4456{
4457 xfs_bmbt_rec_t *ep; /* ptr to fork's extent */
4458 xfs_ifork_t *ifp; /* inode fork pointer */
4459 int rval; /* return value */
4460 xfs_bmbt_irec_t s; /* internal version of extent */
4461
4462#ifndef DEBUG
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004463 if (whichfork == XFS_DATA_FORK) {
4464 return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ?
4465 (ip->i_size == ip->i_mount->m_sb.sb_blocksize) :
4466 (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468#endif /* !DEBUG */
4469 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4470 return 0;
4471 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4472 return 0;
4473 ifp = XFS_IFORK_PTR(ip, whichfork);
4474 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004475 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 xfs_bmbt_get_all(ep, &s);
4477 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4478 if (rval && whichfork == XFS_DATA_FORK)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10004479 ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 return rval;
4481}
4482
4483/*
4484 * Read in the extents to if_extents.
4485 * All inode fields are set up by caller, we just traverse the btree
4486 * and copy the records in. If the file system cannot contain unwritten
4487 * extents, the records are checked for no "state" flags.
4488 */
4489int /* error */
4490xfs_bmap_read_extents(
4491 xfs_trans_t *tp, /* transaction pointer */
4492 xfs_inode_t *ip, /* incore inode */
4493 int whichfork) /* data or attr fork */
4494{
4495 xfs_bmbt_block_t *block; /* current btree block */
4496 xfs_fsblock_t bno; /* block # of "block" */
4497 xfs_buf_t *bp; /* buffer for "block" */
4498 int error; /* error return value */
4499 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 xfs_extnum_t i, j; /* index into the extents list */
4501 xfs_ifork_t *ifp; /* fork structure */
4502 int level; /* btree level, for checking */
4503 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10004504 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 /* REFERENCED */
4506 xfs_extnum_t room; /* number of entries there's room for */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
4508 bno = NULLFSBLOCK;
4509 mp = ip->i_mount;
4510 ifp = XFS_IFORK_PTR(ip, whichfork);
4511 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4512 XFS_EXTFMT_INODE(ip);
4513 block = ifp->if_broot;
4514 /*
4515 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4516 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004517 level = be16_to_cpu(block->bb_level);
4518 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004520 bno = be64_to_cpu(*pp);
4521 ASSERT(bno != NULLDFSBNO);
4522 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4523 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 /*
4525 * Go down the tree until leaf level is reached, following the first
4526 * pointer (leftmost) at each level.
4527 */
4528 while (level-- > 0) {
4529 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4530 XFS_BMAP_BTREE_REF)))
4531 return error;
4532 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4533 XFS_WANT_CORRUPTED_GOTO(
4534 XFS_BMAP_SANITY_CHECK(mp, block, level),
4535 error0);
4536 if (level == 0)
4537 break;
Eric Sandeen2c36dde2007-02-10 18:37:33 +11004538 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004539 bno = be64_to_cpu(*pp);
4540 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 xfs_trans_brelse(tp, bp);
4542 }
4543 /*
4544 * Here with bp and block set to the leftmost leaf node in the tree.
4545 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004546 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 i = 0;
4548 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004549 * Loop over all leaf nodes. Copy information to the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 */
4551 for (;;) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004552 xfs_bmbt_rec_t *frp, *trp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 xfs_fsblock_t nextbno;
4554 xfs_extnum_t num_recs;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004555 xfs_extnum_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
4557
Christoph Hellwig16259e72005-11-02 15:11:25 +11004558 num_recs = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 if (unlikely(i + num_recs > room)) {
4560 ASSERT(i + num_recs <= room);
Nathan Scott3762ec62006-01-12 10:29:53 +11004561 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4562 "corrupt dinode %Lu, (btree extents).",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 (unsigned long long) ip->i_ino);
4564 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4565 XFS_ERRLEVEL_LOW,
4566 ip->i_mount);
4567 goto error0;
4568 }
4569 XFS_WANT_CORRUPTED_GOTO(
4570 XFS_BMAP_SANITY_CHECK(mp, block, 0),
4571 error0);
4572 /*
4573 * Read-ahead the next leaf block, if any.
4574 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004575 nextbno = be64_to_cpu(block->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 if (nextbno != NULLFSBLOCK)
4577 xfs_btree_reada_bufl(mp, nextbno, 1);
4578 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004579 * Copy records into the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 */
Eric Sandeen2c36dde2007-02-10 18:37:33 +11004581 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004582 start = i;
4583 for (j = 0; j < num_recs; j++, i++, frp++) {
4584 trp = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 trp->l0 = INT_GET(frp->l0, ARCH_CONVERT);
4586 trp->l1 = INT_GET(frp->l1, ARCH_CONVERT);
4587 }
4588 if (exntf == XFS_EXTFMT_NOSTATE) {
4589 /*
4590 * Check all attribute bmap btree records and
4591 * any "older" data bmap btree records for a
4592 * set bit in the "extent flag" position.
4593 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004594 if (unlikely(xfs_check_nostate_extents(ifp,
4595 start, num_recs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4597 XFS_ERRLEVEL_LOW,
4598 ip->i_mount);
4599 goto error0;
4600 }
4601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 xfs_trans_brelse(tp, bp);
4603 bno = nextbno;
4604 /*
4605 * If we've reached the end, stop.
4606 */
4607 if (bno == NULLFSBLOCK)
4608 break;
4609 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4610 XFS_BMAP_BTREE_REF)))
4611 return error;
4612 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4613 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004614 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
Eric Sandeen3a59c942007-07-11 11:09:47 +10004616 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 return 0;
4618error0:
4619 xfs_trans_brelse(tp, bp);
4620 return XFS_ERROR(EFSCORRUPTED);
4621}
4622
4623#ifdef XFS_BMAP_TRACE
4624/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004625 * Add bmap trace insert entries for all the contents of the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 */
4627void
4628xfs_bmap_trace_exlist(
Eric Sandeen3a59c942007-07-11 11:09:47 +10004629 const char *fname, /* function name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 xfs_inode_t *ip, /* incore inode pointer */
4631 xfs_extnum_t cnt, /* count of entries in the list */
4632 int whichfork) /* data or attr fork */
4633{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004634 xfs_bmbt_rec_t *ep; /* current extent record */
4635 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 xfs_ifork_t *ifp; /* inode fork pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004637 xfs_bmbt_irec_t s; /* file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638
4639 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004640 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4641 for (idx = 0; idx < cnt; idx++) {
4642 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 xfs_bmbt_get_all(ep, &s);
Eric Sandeen3a59c942007-07-11 11:09:47 +10004644 XFS_BMAP_TRACE_INSERT("exlist", ip, idx, 1, &s, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 whichfork);
4646 }
4647}
4648#endif
4649
4650#ifdef DEBUG
4651/*
4652 * Validate that the bmbt_irecs being returned from bmapi are valid
4653 * given the callers original parameters. Specifically check the
4654 * ranges of the returned irecs to ensure that they only extent beyond
4655 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4656 */
4657STATIC void
4658xfs_bmap_validate_ret(
4659 xfs_fileoff_t bno,
4660 xfs_filblks_t len,
4661 int flags,
4662 xfs_bmbt_irec_t *mval,
4663 int nmap,
4664 int ret_nmap)
4665{
4666 int i; /* index to map values */
4667
4668 ASSERT(ret_nmap <= nmap);
4669
4670 for (i = 0; i < ret_nmap; i++) {
4671 ASSERT(mval[i].br_blockcount > 0);
4672 if (!(flags & XFS_BMAPI_ENTIRE)) {
4673 ASSERT(mval[i].br_startoff >= bno);
4674 ASSERT(mval[i].br_blockcount <= len);
4675 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4676 bno + len);
4677 } else {
4678 ASSERT(mval[i].br_startoff < bno + len);
4679 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4680 bno);
4681 }
4682 ASSERT(i == 0 ||
4683 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4684 mval[i].br_startoff);
4685 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4686 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4687 mval[i].br_startblock != HOLESTARTBLOCK);
4688 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4689 mval[i].br_state == XFS_EXT_UNWRITTEN);
4690 }
4691}
4692#endif /* DEBUG */
4693
4694
4695/*
4696 * Map file blocks to filesystem blocks.
4697 * File range is given by the bno/len pair.
4698 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4699 * into a hole or past eof.
4700 * Only allocates blocks from a single allocation group,
4701 * to avoid locking problems.
4702 * The returned value in "firstblock" from the first call in a transaction
4703 * must be remembered and presented to subsequent calls in "firstblock".
4704 * An upper bound for the number of blocks to be allocated is supplied to
4705 * the first call in "total"; if no allocation group has that many free
4706 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4707 */
4708int /* error */
4709xfs_bmapi(
4710 xfs_trans_t *tp, /* transaction pointer */
4711 xfs_inode_t *ip, /* incore inode */
4712 xfs_fileoff_t bno, /* starting file offs. mapped */
4713 xfs_filblks_t len, /* length to map in file */
4714 int flags, /* XFS_BMAPI_... */
4715 xfs_fsblock_t *firstblock, /* first allocated block
4716 controls a.g. for allocs */
4717 xfs_extlen_t total, /* total blocks needed */
4718 xfs_bmbt_irec_t *mval, /* output: map values */
4719 int *nmap, /* i/o: mval size/count */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004720 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4721 xfs_extdelta_t *delta) /* o: change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722{
4723 xfs_fsblock_t abno; /* allocated block number */
4724 xfs_extlen_t alen; /* allocated extent length */
4725 xfs_fileoff_t aoff; /* allocated file offset */
4726 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 xfs_btree_cur_t *cur; /* bmap btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 xfs_fileoff_t end; /* end of mapped file region */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004729 int eof; /* we've hit the end of extents */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004730 xfs_bmbt_rec_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004732 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 xfs_ifork_t *ifp; /* inode fork pointer */
4734 xfs_extlen_t indlen; /* indirect blocks length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 xfs_extnum_t lastx; /* last useful extent number */
4736 int logflags; /* flags for transaction logging */
4737 xfs_extlen_t minleft; /* min blocks left after allocation */
4738 xfs_extlen_t minlen; /* min allocation size */
4739 xfs_mount_t *mp; /* xfs mount structure */
4740 int n; /* current extent index */
4741 int nallocs; /* number of extents alloc\'d */
4742 xfs_extnum_t nextents; /* number of extents in file */
4743 xfs_fileoff_t obno; /* old block number (offset) */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004744 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 int tmp_logflags; /* temp flags holder */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004746 int whichfork; /* data or attr fork */
4747 char inhole; /* current location is hole in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 char wasdelay; /* old extent was delayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 char wr; /* this is a write request */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004750 char rt; /* this is a realtime file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751#ifdef DEBUG
4752 xfs_fileoff_t orig_bno; /* original block number value */
4753 int orig_flags; /* original flags arg value */
4754 xfs_filblks_t orig_len; /* original value of len arg */
4755 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4756 int orig_nmap; /* original value of *nmap */
4757
4758 orig_bno = bno;
4759 orig_len = len;
4760 orig_flags = flags;
4761 orig_mval = mval;
4762 orig_nmap = *nmap;
4763#endif
4764 ASSERT(*nmap >= 1);
4765 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4766 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4767 XFS_ATTR_FORK : XFS_DATA_FORK;
4768 mp = ip->i_mount;
4769 if (unlikely(XFS_TEST_ERROR(
4770 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4771 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4772 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4773 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4774 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4775 return XFS_ERROR(EFSCORRUPTED);
4776 }
4777 if (XFS_FORCED_SHUTDOWN(mp))
4778 return XFS_ERROR(EIO);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004779 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 ifp = XFS_IFORK_PTR(ip, whichfork);
4781 ASSERT(ifp->if_ext_max ==
4782 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4783 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4784 XFS_STATS_INC(xs_blk_mapw);
4785 else
4786 XFS_STATS_INC(xs_blk_mapr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 /*
Nathan Scott39269e22006-03-14 13:33:50 +11004788 * IGSTATE flag is used to combine extents which
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 * differ only due to the state of the extents.
4790 * This technique is used from xfs_getbmap()
4791 * when the caller does not wish to see the
4792 * separation (which is the default).
4793 *
4794 * This technique is also used when writing a
4795 * buffer which has been partially written,
4796 * (usually by being flushed during a chunkread),
4797 * to ensure one write takes place. This also
4798 * prevents a change in the xfs inode extents at
4799 * this time, intentionally. This change occurs
4800 * on completion of the write operation, in
4801 * xfs_strat_comp(), where the xfs_bmapi() call
4802 * is transactioned, and the extents combined.
4803 */
Nathan Scott39269e22006-03-14 13:33:50 +11004804 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4805 wr = 0; /* no allocations are allowed */
4806 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 logflags = 0;
4808 nallocs = 0;
4809 cur = NULL;
4810 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4811 ASSERT(wr && tp);
4812 if ((error = xfs_bmap_local_to_extents(tp, ip,
4813 firstblock, total, &logflags, whichfork)))
4814 goto error0;
4815 }
4816 if (wr && *firstblock == NULLFSBLOCK) {
4817 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Christoph Hellwig16259e72005-11-02 15:11:25 +11004818 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 else
4820 minleft = 1;
4821 } else
4822 minleft = 0;
4823 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4824 (error = xfs_iread_extents(tp, ip, whichfork)))
4825 goto error0;
4826 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4827 &prev);
4828 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4829 n = 0;
4830 end = bno + len;
4831 obno = bno;
4832 bma.ip = NULL;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004833 if (delta) {
4834 delta->xed_startoff = NULLFILEOFF;
4835 delta->xed_blockcount = 0;
4836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 while (bno < end && n < *nmap) {
4838 /*
4839 * Reading past eof, act as though there's a hole
4840 * up to end.
4841 */
4842 if (eof && !wr)
4843 got.br_startoff = end;
4844 inhole = eof || got.br_startoff > bno;
Nathan Scott39269e22006-03-14 13:33:50 +11004845 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 ISNULLSTARTBLOCK(got.br_startblock);
4847 /*
4848 * First, deal with the hole before the allocated space
4849 * that we found, if any.
4850 */
4851 if (wr && (inhole || wasdelay)) {
4852 /*
4853 * For the wasdelay case, we could also just
4854 * allocate the stuff asked for in this bmap call
4855 * but that wouldn't be as good.
4856 */
Nathan Scott39269e22006-03-14 13:33:50 +11004857 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 alen = (xfs_extlen_t)got.br_blockcount;
4859 aoff = got.br_startoff;
4860 if (lastx != NULLEXTNUM && lastx) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004861 ep = xfs_iext_get_ext(ifp, lastx - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 xfs_bmbt_get_all(ep, &prev);
4863 }
4864 } else if (wasdelay) {
4865 alen = (xfs_extlen_t)
4866 XFS_FILBLKS_MIN(len,
4867 (got.br_startoff +
4868 got.br_blockcount) - bno);
4869 aoff = bno;
4870 } else {
4871 alen = (xfs_extlen_t)
4872 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4873 if (!eof)
4874 alen = (xfs_extlen_t)
4875 XFS_FILBLKS_MIN(alen,
4876 got.br_startoff - bno);
4877 aoff = bno;
4878 }
Nathan Scott39269e22006-03-14 13:33:50 +11004879 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4880 if (flags & XFS_BMAPI_DELAY) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11004881 xfs_extlen_t extsz;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004882
4883 /* Figure out the extent size, adjust alen */
David Chinner957d0eb2007-06-18 16:50:37 +10004884 extsz = xfs_get_extsz_hint(ip);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004885 if (extsz) {
4886 error = xfs_bmap_extsize_align(mp,
4887 &got, &prev, extsz,
Nathan Scott39269e22006-03-14 13:33:50 +11004888 rt, eof,
4889 flags&XFS_BMAPI_DELAY,
4890 flags&XFS_BMAPI_CONVERT,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004891 &aoff, &alen);
4892 ASSERT(!error);
4893 }
4894
4895 if (rt)
4896 extsz = alen / mp->m_sb.sb_rextsize;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004897
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 /*
4899 * Make a transaction-less quota reservation for
4900 * delayed allocation blocks. This number gets
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004901 * adjusted later. We return if we haven't
4902 * allocated blocks already inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 */
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004904 if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS(
Nathan Scott06d10dd2005-06-21 15:48:47 +10004905 mp, NULL, ip, (long)alen, 0,
4906 rt ? XFS_QMOPT_RES_RTBLKS :
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004907 XFS_QMOPT_RES_REGBLKS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 if (n == 0) {
4909 *nmap = 0;
4910 ASSERT(cur == NULL);
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004911 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 }
4913 break;
4914 }
4915
4916 /*
4917 * Split changing sb for alen and indlen since
4918 * they could be coming from different places.
4919 */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004920 indlen = (xfs_extlen_t)
4921 xfs_bmap_worst_indlen(ip, alen);
4922 ASSERT(indlen > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
Nathan Scottdd9f4382006-01-11 15:28:28 +11004924 if (rt) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004925 error = xfs_mod_incore_sb(mp,
4926 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004927 -((int64_t)extsz), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004928 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004929 } else {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004930 error = xfs_mod_incore_sb(mp,
4931 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004932 -((int64_t)alen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004933 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004934 }
David Chinner3bdbfb12005-09-02 16:40:47 +10004935 if (!error) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004936 error = xfs_mod_incore_sb(mp,
4937 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004938 -((int64_t)indlen), (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004939 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004940 if (error && rt)
4941 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004942 XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004943 (int64_t)extsz, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004944 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004945 else if (error)
4946 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004947 XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11004948 (int64_t)alen, (flags &
Nathan Scott39269e22006-03-14 13:33:50 +11004949 XFS_BMAPI_RSVBLOCKS));
David Chinner3bdbfb12005-09-02 16:40:47 +10004950 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004951
4952 if (error) {
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004953 if (XFS_IS_QUOTA_ON(mp))
Nathan Scott06d10dd2005-06-21 15:48:47 +10004954 /* unreserve the blocks now */
Nathan Scottdd9f4382006-01-11 15:28:28 +11004955 (void)
Nathan Scott06d10dd2005-06-21 15:48:47 +10004956 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4957 mp, NULL, ip,
4958 (long)alen, 0, rt ?
4959 XFS_QMOPT_RES_RTBLKS :
4960 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 break;
4962 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004963
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 ip->i_delayed_blks += alen;
4965 abno = NULLSTARTBLOCK(indlen);
4966 } else {
4967 /*
4968 * If first time, allocate and fill in
4969 * once-only bma fields.
4970 */
4971 if (bma.ip == NULL) {
4972 bma.tp = tp;
4973 bma.ip = ip;
4974 bma.prevp = &prev;
4975 bma.gotp = &got;
4976 bma.total = total;
4977 bma.userdata = 0;
4978 }
4979 /* Indicate if this is the first user data
4980 * in the file, or just any user data.
4981 */
Nathan Scott39269e22006-03-14 13:33:50 +11004982 if (!(flags & XFS_BMAPI_METADATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 bma.userdata = (aoff == 0) ?
4984 XFS_ALLOC_INITIAL_USER_DATA :
4985 XFS_ALLOC_USERDATA;
4986 }
4987 /*
4988 * Fill in changeable bma fields.
4989 */
4990 bma.eof = eof;
4991 bma.firstblock = *firstblock;
4992 bma.alen = alen;
4993 bma.off = aoff;
Adrian Bunk72880262006-08-30 13:41:58 +10004994 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 bma.wasdel = wasdelay;
4996 bma.minlen = minlen;
4997 bma.low = flist->xbf_low;
4998 bma.minleft = minleft;
4999 /*
5000 * Only want to do the alignment at the
5001 * eof if it is userdata and allocation length
5002 * is larger than a stripe unit.
5003 */
5004 if (mp->m_dalign && alen >= mp->m_dalign &&
Nathan Scott39269e22006-03-14 13:33:50 +11005005 (!(flags & XFS_BMAPI_METADATA)) &&
5006 (whichfork == XFS_DATA_FORK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 if ((error = xfs_bmap_isaeof(ip, aoff,
5008 whichfork, &bma.aeof)))
5009 goto error0;
5010 } else
5011 bma.aeof = 0;
5012 /*
5013 * Call allocator.
5014 */
5015 if ((error = xfs_bmap_alloc(&bma)))
5016 goto error0;
5017 /*
5018 * Copy out result fields.
5019 */
5020 abno = bma.rval;
5021 if ((flist->xbf_low = bma.low))
5022 minleft = 0;
5023 alen = bma.alen;
5024 aoff = bma.off;
5025 ASSERT(*firstblock == NULLFSBLOCK ||
5026 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5027 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
5028 (flist->xbf_low &&
5029 XFS_FSB_TO_AGNO(mp, *firstblock) <
5030 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
5031 *firstblock = bma.firstblock;
5032 if (cur)
5033 cur->bc_private.b.firstblock =
5034 *firstblock;
5035 if (abno == NULLFSBLOCK)
5036 break;
5037 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5038 cur = xfs_btree_init_cursor(mp,
5039 tp, NULL, 0, XFS_BTNUM_BMAP,
5040 ip, whichfork);
5041 cur->bc_private.b.firstblock =
5042 *firstblock;
5043 cur->bc_private.b.flist = flist;
5044 }
5045 /*
5046 * Bump the number of extents we've allocated
5047 * in this call.
5048 */
5049 nallocs++;
5050 }
5051 if (cur)
5052 cur->bc_private.b.flags =
5053 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
5054 got.br_startoff = aoff;
5055 got.br_startblock = abno;
5056 got.br_blockcount = alen;
5057 got.br_state = XFS_EXT_NORM; /* assume normal */
5058 /*
5059 * Determine state of extent, and the filesystem.
5060 * A wasdelay extent has been initialized, so
5061 * shouldn't be flagged as unwritten.
5062 */
5063 if (wr && XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5064 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5065 got.br_state = XFS_EXT_UNWRITTEN;
5066 }
5067 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005068 firstblock, flist, &tmp_logflags, delta,
5069 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 logflags |= tmp_logflags;
5071 if (error)
5072 goto error0;
5073 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005074 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5076 xfs_bmbt_get_all(ep, &got);
5077 ASSERT(got.br_startoff <= aoff);
5078 ASSERT(got.br_startoff + got.br_blockcount >=
5079 aoff + alen);
5080#ifdef DEBUG
Nathan Scott39269e22006-03-14 13:33:50 +11005081 if (flags & XFS_BMAPI_DELAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
5083 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
5084 }
5085 ASSERT(got.br_state == XFS_EXT_NORM ||
5086 got.br_state == XFS_EXT_UNWRITTEN);
5087#endif
5088 /*
5089 * Fall down into the found allocated space case.
5090 */
5091 } else if (inhole) {
5092 /*
5093 * Reading in a hole.
5094 */
5095 mval->br_startoff = bno;
5096 mval->br_startblock = HOLESTARTBLOCK;
5097 mval->br_blockcount =
5098 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5099 mval->br_state = XFS_EXT_NORM;
5100 bno += mval->br_blockcount;
5101 len -= mval->br_blockcount;
5102 mval++;
5103 n++;
5104 continue;
5105 }
5106 /*
5107 * Then deal with the allocated space we found.
5108 */
5109 ASSERT(ep != NULL);
Nathan Scott39269e22006-03-14 13:33:50 +11005110 if (!(flags & XFS_BMAPI_ENTIRE) &&
5111 (got.br_startoff + got.br_blockcount > obno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 if (obno > bno)
5113 bno = obno;
5114 ASSERT((bno >= obno) || (n == 0));
5115 ASSERT(bno < end);
5116 mval->br_startoff = bno;
5117 if (ISNULLSTARTBLOCK(got.br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11005118 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 mval->br_startblock = DELAYSTARTBLOCK;
5120 } else
5121 mval->br_startblock =
5122 got.br_startblock +
5123 (bno - got.br_startoff);
5124 /*
5125 * Return the minimum of what we got and what we
5126 * asked for for the length. We can use the len
5127 * variable here because it is modified below
5128 * and we could have been there before coming
5129 * here if the first part of the allocation
5130 * didn't overlap what was asked for.
5131 */
5132 mval->br_blockcount =
5133 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5134 (bno - got.br_startoff));
5135 mval->br_state = got.br_state;
5136 ASSERT(mval->br_blockcount <= len);
5137 } else {
5138 *mval = got;
5139 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11005140 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 mval->br_startblock = DELAYSTARTBLOCK;
5142 }
5143 }
5144
5145 /*
5146 * Check if writing previously allocated but
5147 * unwritten extents.
5148 */
5149 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5150 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5151 /*
5152 * Modify (by adding) the state flag, if writing.
5153 */
5154 ASSERT(mval->br_blockcount <= len);
5155 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5156 cur = xfs_btree_init_cursor(mp,
5157 tp, NULL, 0, XFS_BTNUM_BMAP,
5158 ip, whichfork);
5159 cur->bc_private.b.firstblock =
5160 *firstblock;
5161 cur->bc_private.b.flist = flist;
5162 }
5163 mval->br_state = XFS_EXT_NORM;
5164 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005165 firstblock, flist, &tmp_logflags, delta,
5166 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 logflags |= tmp_logflags;
5168 if (error)
5169 goto error0;
5170 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005171 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5173 xfs_bmbt_get_all(ep, &got);
5174 /*
5175 * We may have combined previously unwritten
5176 * space with written space, so generate
5177 * another request.
5178 */
5179 if (mval->br_blockcount < len)
5180 continue;
5181 }
5182
Nathan Scott39269e22006-03-14 13:33:50 +11005183 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 ((mval->br_startoff + mval->br_blockcount) <= end));
Nathan Scott39269e22006-03-14 13:33:50 +11005185 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5186 (mval->br_blockcount <= len) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 (mval->br_startoff < obno));
5188 bno = mval->br_startoff + mval->br_blockcount;
5189 len = end - bno;
5190 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5191 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5192 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5193 ASSERT(mval->br_state == mval[-1].br_state);
5194 mval[-1].br_blockcount = mval->br_blockcount;
5195 mval[-1].br_state = mval->br_state;
5196 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5197 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5198 mval[-1].br_startblock != HOLESTARTBLOCK &&
5199 mval->br_startblock ==
5200 mval[-1].br_startblock + mval[-1].br_blockcount &&
Nathan Scott39269e22006-03-14 13:33:50 +11005201 ((flags & XFS_BMAPI_IGSTATE) ||
5202 mval[-1].br_state == mval->br_state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 ASSERT(mval->br_startoff ==
5204 mval[-1].br_startoff + mval[-1].br_blockcount);
5205 mval[-1].br_blockcount += mval->br_blockcount;
5206 } else if (n > 0 &&
5207 mval->br_startblock == DELAYSTARTBLOCK &&
5208 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5209 mval->br_startoff ==
5210 mval[-1].br_startoff + mval[-1].br_blockcount) {
5211 mval[-1].br_blockcount += mval->br_blockcount;
5212 mval[-1].br_state = mval->br_state;
5213 } else if (!((n == 0) &&
5214 ((mval->br_startoff + mval->br_blockcount) <=
5215 obno))) {
5216 mval++;
5217 n++;
5218 }
5219 /*
5220 * If we're done, stop now. Stop when we've allocated
5221 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5222 * the transaction may get too big.
5223 */
5224 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5225 break;
5226 /*
5227 * Else go on to the next record.
5228 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005229 ep = xfs_iext_get_ext(ifp, ++lastx);
David Chinner4e5ae832007-06-05 16:24:15 +10005230 prev = got;
5231 if (lastx >= nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 eof = 1;
David Chinner4e5ae832007-06-05 16:24:15 +10005233 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 xfs_bmbt_get_all(ep, &got);
5235 }
5236 ifp->if_lastex = lastx;
5237 *nmap = n;
5238 /*
5239 * Transform from btree to extents, give it cur.
5240 */
5241 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5242 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5243 ASSERT(wr && cur);
5244 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5245 &tmp_logflags, whichfork);
5246 logflags |= tmp_logflags;
5247 if (error)
5248 goto error0;
5249 }
5250 ASSERT(ifp->if_ext_max ==
5251 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5252 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5253 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5254 error = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005255 if (delta && delta->xed_startoff != NULLFILEOFF) {
5256 /* A change was actually made.
5257 * Note that delta->xed_blockount is an offset at this
5258 * point and needs to be converted to a block count.
5259 */
5260 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5261 delta->xed_blockcount -= delta->xed_startoff;
5262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263error0:
5264 /*
5265 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005266 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 */
5268 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5269 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5270 logflags &= ~XFS_ILOG_FEXT(whichfork);
5271 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5272 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5273 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5274 /*
5275 * Log whatever the flags say, even if error. Otherwise we might miss
5276 * detecting a case where the data is changed, there's an error,
5277 * and it's not logged so we don't shutdown when we should.
5278 */
5279 if (logflags) {
5280 ASSERT(tp && wr);
5281 xfs_trans_log_inode(tp, ip, logflags);
5282 }
5283 if (cur) {
5284 if (!error) {
5285 ASSERT(*firstblock == NULLFSBLOCK ||
5286 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5287 XFS_FSB_TO_AGNO(mp,
5288 cur->bc_private.b.firstblock) ||
5289 (flist->xbf_low &&
5290 XFS_FSB_TO_AGNO(mp, *firstblock) <
5291 XFS_FSB_TO_AGNO(mp,
5292 cur->bc_private.b.firstblock)));
5293 *firstblock = cur->bc_private.b.firstblock;
5294 }
5295 xfs_btree_del_cursor(cur,
5296 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5297 }
5298 if (!error)
5299 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5300 orig_nmap, *nmap);
5301 return error;
5302}
5303
5304/*
5305 * Map file blocks to filesystem blocks, simple version.
5306 * One block (extent) only, read-only.
5307 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5308 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5309 * was set and all the others were clear.
5310 */
5311int /* error */
5312xfs_bmapi_single(
5313 xfs_trans_t *tp, /* transaction pointer */
5314 xfs_inode_t *ip, /* incore inode */
5315 int whichfork, /* data or attr fork */
5316 xfs_fsblock_t *fsb, /* output: mapped block */
5317 xfs_fileoff_t bno) /* starting file offs. mapped */
5318{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005319 int eof; /* we've hit the end of extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005321 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 xfs_ifork_t *ifp; /* inode fork pointer */
5323 xfs_extnum_t lastx; /* last useful extent number */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005324 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
5326 ifp = XFS_IFORK_PTR(ip, whichfork);
5327 if (unlikely(
5328 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5329 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5330 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5331 ip->i_mount);
5332 return XFS_ERROR(EFSCORRUPTED);
5333 }
5334 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5335 return XFS_ERROR(EIO);
5336 XFS_STATS_INC(xs_blk_mapr);
5337 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5338 (error = xfs_iread_extents(tp, ip, whichfork)))
5339 return error;
5340 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5341 &prev);
5342 /*
5343 * Reading past eof, act as though there's a hole
5344 * up to end.
5345 */
5346 if (eof || got.br_startoff > bno) {
5347 *fsb = NULLFSBLOCK;
5348 return 0;
5349 }
5350 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
5351 ASSERT(bno < got.br_startoff + got.br_blockcount);
5352 *fsb = got.br_startblock + (bno - got.br_startoff);
5353 ifp->if_lastex = lastx;
5354 return 0;
5355}
5356
5357/*
5358 * Unmap (remove) blocks from a file.
5359 * If nexts is nonzero then the number of extents to remove is limited to
5360 * that value. If not all extents in the block range can be removed then
5361 * *done is set.
5362 */
5363int /* error */
5364xfs_bunmapi(
5365 xfs_trans_t *tp, /* transaction pointer */
5366 struct xfs_inode *ip, /* incore inode */
5367 xfs_fileoff_t bno, /* starting offset to unmap */
5368 xfs_filblks_t len, /* length to unmap in file */
5369 int flags, /* misc flags */
5370 xfs_extnum_t nexts, /* number of extents max */
5371 xfs_fsblock_t *firstblock, /* first allocated block
5372 controls a.g. for allocs */
5373 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005374 xfs_extdelta_t *delta, /* o: change made to incore
5375 extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 int *done) /* set if not done yet */
5377{
5378 xfs_btree_cur_t *cur; /* bmap btree cursor */
5379 xfs_bmbt_irec_t del; /* extent being deleted */
5380 int eof; /* is deleting at eof */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005381 xfs_bmbt_rec_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 int error; /* error return value */
5383 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005384 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 xfs_ifork_t *ifp; /* inode fork pointer */
5386 int isrt; /* freeing in rt area */
5387 xfs_extnum_t lastx; /* last extent index used */
5388 int logflags; /* transaction logging flags */
5389 xfs_extlen_t mod; /* rt extent offset */
5390 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005391 xfs_extnum_t nextents; /* number of file extents */
5392 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 xfs_fileoff_t start; /* first file offset deleted */
5394 int tmp_logflags; /* partial logging flags */
5395 int wasdel; /* was a delayed alloc extent */
5396 int whichfork; /* data or attribute fork */
5397 int rsvd; /* OK to allocate reserved blocks */
5398 xfs_fsblock_t sum;
5399
5400 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5401 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5402 XFS_ATTR_FORK : XFS_DATA_FORK;
5403 ifp = XFS_IFORK_PTR(ip, whichfork);
5404 if (unlikely(
5405 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5406 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5407 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5408 ip->i_mount);
5409 return XFS_ERROR(EFSCORRUPTED);
5410 }
5411 mp = ip->i_mount;
5412 if (XFS_FORCED_SHUTDOWN(mp))
5413 return XFS_ERROR(EIO);
5414 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5415 ASSERT(len > 0);
5416 ASSERT(nexts >= 0);
5417 ASSERT(ifp->if_ext_max ==
5418 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5419 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5420 (error = xfs_iread_extents(tp, ip, whichfork)))
5421 return error;
5422 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5423 if (nextents == 0) {
5424 *done = 1;
5425 return 0;
5426 }
5427 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005428 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 start = bno;
5430 bno = start + len - 1;
5431 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5432 &prev);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005433 if (delta) {
5434 delta->xed_startoff = NULLFILEOFF;
5435 delta->xed_blockcount = 0;
5436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 /*
5438 * Check to see if the given block number is past the end of the
5439 * file, back up to the last block if so...
5440 */
5441 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005442 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 xfs_bmbt_get_all(ep, &got);
5444 bno = got.br_startoff + got.br_blockcount - 1;
5445 }
5446 logflags = 0;
5447 if (ifp->if_flags & XFS_IFBROOT) {
5448 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5449 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
5450 whichfork);
5451 cur->bc_private.b.firstblock = *firstblock;
5452 cur->bc_private.b.flist = flist;
5453 cur->bc_private.b.flags = 0;
5454 } else
5455 cur = NULL;
5456 extno = 0;
5457 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5458 (nexts == 0 || extno < nexts)) {
5459 /*
5460 * Is the found extent after a hole in which bno lives?
5461 * Just back up to the previous extent, if so.
5462 */
5463 if (got.br_startoff > bno) {
5464 if (--lastx < 0)
5465 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005466 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 xfs_bmbt_get_all(ep, &got);
5468 }
5469 /*
5470 * Is the last block of this extent before the range
5471 * we're supposed to delete? If so, we're done.
5472 */
5473 bno = XFS_FILEOFF_MIN(bno,
5474 got.br_startoff + got.br_blockcount - 1);
5475 if (bno < start)
5476 break;
5477 /*
5478 * Then deal with the (possibly delayed) allocated space
5479 * we found.
5480 */
5481 ASSERT(ep != NULL);
5482 del = got;
5483 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
5484 if (got.br_startoff < start) {
5485 del.br_startoff = start;
5486 del.br_blockcount -= start - got.br_startoff;
5487 if (!wasdel)
5488 del.br_startblock += start - got.br_startoff;
5489 }
5490 if (del.br_startoff + del.br_blockcount > bno + 1)
5491 del.br_blockcount = bno + 1 - del.br_startoff;
5492 sum = del.br_startblock + del.br_blockcount;
5493 if (isrt &&
5494 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5495 /*
5496 * Realtime extent not lined up at the end.
5497 * The extent could have been split into written
5498 * and unwritten pieces, or we could just be
5499 * unmapping part of it. But we can't really
5500 * get rid of part of a realtime extent.
5501 */
5502 if (del.br_state == XFS_EXT_UNWRITTEN ||
5503 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5504 /*
5505 * This piece is unwritten, or we're not
5506 * using unwritten extents. Skip over it.
5507 */
5508 ASSERT(bno >= mod);
5509 bno -= mod > del.br_blockcount ?
5510 del.br_blockcount : mod;
5511 if (bno < got.br_startoff) {
5512 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005513 xfs_bmbt_get_all(xfs_iext_get_ext(
5514 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 }
5516 continue;
5517 }
5518 /*
5519 * It's written, turn it unwritten.
5520 * This is better than zeroing it.
5521 */
5522 ASSERT(del.br_state == XFS_EXT_NORM);
5523 ASSERT(xfs_trans_get_block_res(tp) > 0);
5524 /*
5525 * If this spans a realtime extent boundary,
5526 * chop it back to the start of the one we end at.
5527 */
5528 if (del.br_blockcount > mod) {
5529 del.br_startoff += del.br_blockcount - mod;
5530 del.br_startblock += del.br_blockcount - mod;
5531 del.br_blockcount = mod;
5532 }
5533 del.br_state = XFS_EXT_UNWRITTEN;
5534 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005535 firstblock, flist, &logflags, delta,
5536 XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 if (error)
5538 goto error0;
5539 goto nodelete;
5540 }
5541 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5542 /*
5543 * Realtime extent is lined up at the end but not
5544 * at the front. We'll get rid of full extents if
5545 * we can.
5546 */
5547 mod = mp->m_sb.sb_rextsize - mod;
5548 if (del.br_blockcount > mod) {
5549 del.br_blockcount -= mod;
5550 del.br_startoff += mod;
5551 del.br_startblock += mod;
5552 } else if ((del.br_startoff == start &&
5553 (del.br_state == XFS_EXT_UNWRITTEN ||
5554 xfs_trans_get_block_res(tp) == 0)) ||
5555 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5556 /*
5557 * Can't make it unwritten. There isn't
5558 * a full extent here so just skip it.
5559 */
5560 ASSERT(bno >= del.br_blockcount);
5561 bno -= del.br_blockcount;
5562 if (bno < got.br_startoff) {
5563 if (--lastx >= 0)
5564 xfs_bmbt_get_all(--ep, &got);
5565 }
5566 continue;
5567 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5568 /*
5569 * This one is already unwritten.
5570 * It must have a written left neighbor.
5571 * Unwrite the killed part of that one and
5572 * try again.
5573 */
5574 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005575 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5576 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 ASSERT(prev.br_state == XFS_EXT_NORM);
5578 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
5579 ASSERT(del.br_startblock ==
5580 prev.br_startblock + prev.br_blockcount);
5581 if (prev.br_startoff < start) {
5582 mod = start - prev.br_startoff;
5583 prev.br_blockcount -= mod;
5584 prev.br_startblock += mod;
5585 prev.br_startoff = start;
5586 }
5587 prev.br_state = XFS_EXT_UNWRITTEN;
5588 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5589 &prev, firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005590 delta, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 if (error)
5592 goto error0;
5593 goto nodelete;
5594 } else {
5595 ASSERT(del.br_state == XFS_EXT_NORM);
5596 del.br_state = XFS_EXT_UNWRITTEN;
5597 error = xfs_bmap_add_extent(ip, lastx, &cur,
5598 &del, firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005599 delta, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 if (error)
5601 goto error0;
5602 goto nodelete;
5603 }
5604 }
5605 if (wasdel) {
5606 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005607 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005608 if (isrt) {
5609 xfs_filblks_t rtexts;
5610
5611 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5612 do_div(rtexts, mp->m_sb.sb_rextsize);
5613 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005614 (int64_t)rtexts, rsvd);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005615 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5616 NULL, ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005617 XFS_QMOPT_RES_RTBLKS);
5618 } else {
5619 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +11005620 (int64_t)del.br_blockcount, rsvd);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005621 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5622 NULL, ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 ip->i_delayed_blks -= del.br_blockcount;
5626 if (cur)
5627 cur->bc_private.b.flags |=
5628 XFS_BTCUR_BPRV_WASDEL;
5629 } else if (cur)
5630 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5631 /*
5632 * If it's the case where the directory code is running
5633 * with no block reservation, and the deleted block is in
5634 * the middle of its extent, and the resulting insert
5635 * of an extent would cause transformation to btree format,
5636 * then reject it. The calling code will then swap
5637 * blocks around instead.
5638 * We have to do this now, rather than waiting for the
5639 * conversion to btree format, since the transaction
5640 * will be dirty.
5641 */
5642 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5643 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5644 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5645 del.br_startoff > got.br_startoff &&
5646 del.br_startoff + del.br_blockcount <
5647 got.br_startoff + got.br_blockcount) {
5648 error = XFS_ERROR(ENOSPC);
5649 goto error0;
5650 }
5651 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005652 &tmp_logflags, delta, whichfork, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 logflags |= tmp_logflags;
5654 if (error)
5655 goto error0;
5656 bno = del.br_startoff - 1;
5657nodelete:
5658 lastx = ifp->if_lastex;
5659 /*
5660 * If not done go on to the next (previous) record.
5661 * Reset ep in case the extents array was re-alloced.
5662 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005663 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5665 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5666 xfs_bmbt_get_startoff(ep) > bno) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005667 if (--lastx >= 0)
5668 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 }
5670 if (lastx >= 0)
5671 xfs_bmbt_get_all(ep, &got);
5672 extno++;
5673 }
5674 }
5675 ifp->if_lastex = lastx;
5676 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5677 ASSERT(ifp->if_ext_max ==
5678 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5679 /*
5680 * Convert to a btree if necessary.
5681 */
5682 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5683 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5684 ASSERT(cur == NULL);
5685 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5686 &cur, 0, &tmp_logflags, whichfork);
5687 logflags |= tmp_logflags;
5688 if (error)
5689 goto error0;
5690 }
5691 /*
5692 * transform from btree to extents, give it cur
5693 */
5694 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5695 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5696 ASSERT(cur != NULL);
5697 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5698 whichfork);
5699 logflags |= tmp_logflags;
5700 if (error)
5701 goto error0;
5702 }
5703 /*
5704 * transform from extents to local?
5705 */
5706 ASSERT(ifp->if_ext_max ==
5707 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5708 error = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005709 if (delta && delta->xed_startoff != NULLFILEOFF) {
5710 /* A change was actually made.
5711 * Note that delta->xed_blockount is an offset at this
5712 * point and needs to be converted to a block count.
5713 */
5714 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5715 delta->xed_blockcount -= delta->xed_startoff;
5716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717error0:
5718 /*
5719 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005720 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 */
5722 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5723 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5724 logflags &= ~XFS_ILOG_FEXT(whichfork);
5725 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5726 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5727 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5728 /*
5729 * Log inode even in the error case, if the transaction
5730 * is dirty we'll need to shut down the filesystem.
5731 */
5732 if (logflags)
5733 xfs_trans_log_inode(tp, ip, logflags);
5734 if (cur) {
5735 if (!error) {
5736 *firstblock = cur->bc_private.b.firstblock;
5737 cur->bc_private.b.allocated = 0;
5738 }
5739 xfs_btree_del_cursor(cur,
5740 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5741 }
5742 return error;
5743}
5744
5745/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005746 * returns 1 for success, 0 if we failed to map the extent.
5747 */
5748STATIC int
5749xfs_getbmapx_fix_eof_hole(
5750 xfs_inode_t *ip, /* xfs incore inode pointer */
5751 struct getbmap *out, /* output structure */
5752 int prealloced, /* this is a file with
5753 * preallocated data space */
5754 __int64_t end, /* last block requested */
5755 xfs_fsblock_t startblock)
5756{
5757 __int64_t fixlen;
5758 xfs_mount_t *mp; /* file system mount point */
5759
5760 if (startblock == HOLESTARTBLOCK) {
5761 mp = ip->i_mount;
5762 out->bmv_block = -1;
5763 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size));
5764 fixlen -= out->bmv_offset;
5765 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5766 /* Came to hole at EOF. Trim it. */
5767 if (fixlen <= 0)
5768 return 0;
5769 out->bmv_length = fixlen;
5770 }
5771 } else {
5772 out->bmv_block = XFS_FSB_TO_DB(ip, startblock);
5773 }
5774
5775 return 1;
5776}
5777
5778/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 * Fcntl interface to xfs_bmapi.
5780 */
5781int /* error code */
5782xfs_getbmap(
5783 bhv_desc_t *bdp, /* XFS behavior descriptor*/
5784 struct getbmap *bmv, /* user bmap structure */
5785 void __user *ap, /* pointer to user's array */
5786 int interface) /* interface flags */
5787{
5788 __int64_t bmvend; /* last block requested */
5789 int error; /* return value */
5790 __int64_t fixlen; /* length for -1 case */
5791 int i; /* extent number */
5792 xfs_inode_t *ip; /* xfs incore inode pointer */
Nathan Scott67fcaa72006-06-09 17:00:52 +10005793 bhv_vnode_t *vp; /* corresponding vnode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 int lock; /* lock state */
5795 xfs_bmbt_irec_t *map; /* buffer for user's data */
5796 xfs_mount_t *mp; /* file system mount point */
5797 int nex; /* # of user extents can do */
5798 int nexleft; /* # of user extents left */
5799 int subnex; /* # of bmapi's can do */
5800 int nmap; /* number of map entries */
5801 struct getbmap out; /* output structure */
5802 int whichfork; /* data or attr fork */
5803 int prealloced; /* this is a file with
5804 * preallocated data space */
5805 int sh_unwritten; /* true, if unwritten */
5806 /* extents listed separately */
5807 int bmapi_flags; /* flags for xfs_bmapi */
5808 __int32_t oflags; /* getbmapx bmv_oflags field */
5809
5810 vp = BHV_TO_VNODE(bdp);
5811 ip = XFS_BHVTOI(bdp);
5812 mp = ip->i_mount;
5813
5814 whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5815 sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
5816
5817 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5818 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5819 * bit is set for the file, generate a read event in order
5820 * that the DMAPI application may do its thing before we return
5821 * the extents. Usually this means restoring user file data to
5822 * regions of the file that look like holes.
5823 *
5824 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5825 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5826 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5827 * could misinterpret holes in a DMAPI file as true holes,
5828 * when in fact they may represent offline user data.
5829 */
5830 if ( (interface & BMV_IF_NO_DMAPI_READ) == 0
5831 && DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)
5832 && whichfork == XFS_DATA_FORK) {
5833
5834 error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, 0, 0, 0, NULL);
5835 if (error)
5836 return XFS_ERROR(error);
5837 }
5838
5839 if (whichfork == XFS_ATTR_FORK) {
5840 if (XFS_IFORK_Q(ip)) {
5841 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5842 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5843 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5844 return XFS_ERROR(EINVAL);
5845 } else if (unlikely(
5846 ip->i_d.di_aformat != 0 &&
5847 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5848 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5849 ip->i_mount);
5850 return XFS_ERROR(EFSCORRUPTED);
5851 }
5852 } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5853 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5854 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5855 return XFS_ERROR(EINVAL);
5856 if (whichfork == XFS_DATA_FORK) {
David Chinner957d0eb2007-06-18 16:50:37 +10005857 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005858 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 prealloced = 1;
5860 fixlen = XFS_MAXIOFFSET(mp);
5861 } else {
5862 prealloced = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005863 fixlen = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 }
5865 } else {
5866 prealloced = 0;
5867 fixlen = 1LL << 32;
5868 }
5869
5870 if (bmv->bmv_length == -1) {
5871 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5872 bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
5873 (__int64_t)0);
5874 } else if (bmv->bmv_length < 0)
5875 return XFS_ERROR(EINVAL);
5876 if (bmv->bmv_length == 0) {
5877 bmv->bmv_entries = 0;
5878 return 0;
5879 }
5880 nex = bmv->bmv_count - 1;
5881 if (nex <= 0)
5882 return XFS_ERROR(EINVAL);
5883 bmvend = bmv->bmv_offset + bmv->bmv_length;
5884
5885 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5886
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10005887 if (whichfork == XFS_DATA_FORK &&
5888 (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
Nathan Scott67fcaa72006-06-09 17:00:52 +10005890 error = bhv_vop_flush_pages(vp, (xfs_off_t)0, -1, 0, FI_REMAPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 }
5892
5893 ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
5894
5895 lock = xfs_ilock_map_shared(ip);
5896
5897 /*
5898 * Don't let nex be bigger than the number of extents
5899 * we can have assuming alternating holes and real extents.
5900 */
5901 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5902 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5903
5904 bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
5905 ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
5906
5907 /*
5908 * Allocate enough space to handle "subnex" maps at a time.
5909 */
5910 subnex = 16;
5911 map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
5912
5913 bmv->bmv_entries = 0;
5914
5915 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
5916 error = 0;
5917 goto unlock_and_return;
5918 }
5919
5920 nexleft = nex;
5921
5922 do {
5923 nmap = (nexleft > subnex) ? subnex : nexleft;
5924 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5925 XFS_BB_TO_FSB(mp, bmv->bmv_length),
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005926 bmapi_flags, NULL, 0, map, &nmap,
5927 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 if (error)
5929 goto unlock_and_return;
5930 ASSERT(nmap <= subnex);
5931
5932 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5933 nexleft--;
5934 oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
5935 BMV_OF_PREALLOC : 0;
5936 out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
5937 out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5938 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
Yingping Lu9af0a702005-11-02 15:09:54 +11005939 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005940 whichfork == XFS_ATTR_FORK) {
5941 /* came to the end of attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 goto unlock_and_return;
5943 } else {
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005944 if (!xfs_getbmapx_fix_eof_hole(ip, &out,
5945 prealloced, bmvend,
5946 map[i].br_startblock)) {
5947 goto unlock_and_return;
5948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949
5950 /* return either getbmap/getbmapx structure. */
5951 if (interface & BMV_IF_EXTENDED) {
5952 struct getbmapx outx;
5953
5954 GETBMAP_CONVERT(out,outx);
5955 outx.bmv_oflags = oflags;
5956 outx.bmv_unused1 = outx.bmv_unused2 = 0;
5957 if (copy_to_user(ap, &outx,
5958 sizeof(outx))) {
5959 error = XFS_ERROR(EFAULT);
5960 goto unlock_and_return;
5961 }
5962 } else {
5963 if (copy_to_user(ap, &out,
5964 sizeof(out))) {
5965 error = XFS_ERROR(EFAULT);
5966 goto unlock_and_return;
5967 }
5968 }
5969 bmv->bmv_offset =
5970 out.bmv_offset + out.bmv_length;
5971 bmv->bmv_length = MAX((__int64_t)0,
5972 (__int64_t)(bmvend - bmv->bmv_offset));
5973 bmv->bmv_entries++;
5974 ap = (interface & BMV_IF_EXTENDED) ?
5975 (void __user *)
5976 ((struct getbmapx __user *)ap + 1) :
5977 (void __user *)
5978 ((struct getbmap __user *)ap + 1);
5979 }
5980 }
5981 } while (nmap && nexleft && bmv->bmv_length);
5982
5983unlock_and_return:
5984 xfs_iunlock_map_shared(ip, lock);
5985 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5986
5987 kmem_free(map, subnex * sizeof(*map));
5988
5989 return error;
5990}
5991
5992/*
5993 * Check the last inode extent to determine whether this allocation will result
5994 * in blocks being allocated at the end of the file. When we allocate new data
5995 * blocks at the end of the file which do not start at the previous data block,
5996 * we will try to align the new blocks at stripe unit boundaries.
5997 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10005998STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999xfs_bmap_isaeof(
6000 xfs_inode_t *ip, /* incore inode pointer */
6001 xfs_fileoff_t off, /* file offset in fsblocks */
6002 int whichfork, /* data or attribute fork */
6003 char *aeof) /* return value */
6004{
6005 int error; /* error return value */
6006 xfs_ifork_t *ifp; /* inode fork pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006007 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
6008 xfs_extnum_t nextents; /* number of file extents */
6009 xfs_bmbt_irec_t s; /* expanded extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
6011 ASSERT(whichfork == XFS_DATA_FORK);
6012 ifp = XFS_IFORK_PTR(ip, whichfork);
6013 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6014 (error = xfs_iread_extents(NULL, ip, whichfork)))
6015 return error;
6016 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6017 if (nextents == 0) {
6018 *aeof = 1;
6019 return 0;
6020 }
6021 /*
6022 * Go to the last extent
6023 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006024 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 xfs_bmbt_get_all(lastrec, &s);
6026 /*
6027 * Check we are allocating in the last extent (for delayed allocations)
6028 * or past the last extent for non-delayed allocations.
6029 */
6030 *aeof = (off >= s.br_startoff &&
6031 off < s.br_startoff + s.br_blockcount &&
6032 ISNULLSTARTBLOCK(s.br_startblock)) ||
6033 off >= s.br_startoff + s.br_blockcount;
6034 return 0;
6035}
6036
6037/*
6038 * Check if the endoff is outside the last extent. If so the caller will grow
6039 * the allocation to a stripe unit boundary.
6040 */
6041int /* error */
6042xfs_bmap_eof(
6043 xfs_inode_t *ip, /* incore inode pointer */
6044 xfs_fileoff_t endoff, /* file offset in fsblocks */
6045 int whichfork, /* data or attribute fork */
6046 int *eof) /* result value */
6047{
6048 xfs_fsblock_t blockcount; /* extent block count */
6049 int error; /* error return value */
6050 xfs_ifork_t *ifp; /* inode fork pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006051 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
6052 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 xfs_fileoff_t startoff; /* extent starting file offset */
6054
6055 ASSERT(whichfork == XFS_DATA_FORK);
6056 ifp = XFS_IFORK_PTR(ip, whichfork);
6057 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6058 (error = xfs_iread_extents(NULL, ip, whichfork)))
6059 return error;
6060 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6061 if (nextents == 0) {
6062 *eof = 1;
6063 return 0;
6064 }
6065 /*
6066 * Go to the last extent
6067 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006068 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 startoff = xfs_bmbt_get_startoff(lastrec);
6070 blockcount = xfs_bmbt_get_blockcount(lastrec);
6071 *eof = endoff >= startoff + blockcount;
6072 return 0;
6073}
6074
6075#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076STATIC
6077xfs_buf_t *
6078xfs_bmap_get_bp(
6079 xfs_btree_cur_t *cur,
6080 xfs_fsblock_t bno)
6081{
6082 int i;
6083 xfs_buf_t *bp;
6084
6085 if (!cur)
6086 return(NULL);
6087
6088 bp = NULL;
6089 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6090 bp = cur->bc_bufs[i];
6091 if (!bp) break;
6092 if (XFS_BUF_ADDR(bp) == bno)
6093 break; /* Found it */
6094 }
6095 if (i == XFS_BTREE_MAXLEVELS)
6096 bp = NULL;
6097
6098 if (!bp) { /* Chase down all the log items to see if the bp is there */
6099 xfs_log_item_chunk_t *licp;
6100 xfs_trans_t *tp;
6101
6102 tp = cur->bc_tp;
6103 licp = &tp->t_items;
6104 while (!bp && licp != NULL) {
6105 if (XFS_LIC_ARE_ALL_FREE(licp)) {
6106 licp = licp->lic_next;
6107 continue;
6108 }
6109 for (i = 0; i < licp->lic_unused; i++) {
6110 xfs_log_item_desc_t *lidp;
6111 xfs_log_item_t *lip;
6112 xfs_buf_log_item_t *bip;
6113 xfs_buf_t *lbp;
6114
6115 if (XFS_LIC_ISFREE(licp, i)) {
6116 continue;
6117 }
6118
6119 lidp = XFS_LIC_SLOT(licp, i);
6120 lip = lidp->lid_item;
6121 if (lip->li_type != XFS_LI_BUF)
6122 continue;
6123
6124 bip = (xfs_buf_log_item_t *)lip;
6125 lbp = bip->bli_buf;
6126
6127 if (XFS_BUF_ADDR(lbp) == bno) {
6128 bp = lbp;
6129 break; /* Found it */
6130 }
6131 }
6132 licp = licp->lic_next;
6133 }
6134 }
6135 return(bp);
6136}
6137
6138void
6139xfs_check_block(
6140 xfs_bmbt_block_t *block,
6141 xfs_mount_t *mp,
6142 int root,
6143 short sz)
6144{
6145 int i, j, dmxr;
Christoph Hellwig576039c2006-09-28 10:58:06 +10006146 __be64 *pp, *thispa; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 xfs_bmbt_key_t *prevp, *keyp;
6148
Christoph Hellwig16259e72005-11-02 15:11:25 +11006149 ASSERT(be16_to_cpu(block->bb_level) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150
6151 prevp = NULL;
Christoph Hellwig16259e72005-11-02 15:11:25 +11006152 for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153 dmxr = mp->m_bmap_dmxr[0];
6154
6155 if (root) {
6156 keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
6157 } else {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006158 keyp = XFS_BTREE_KEY_ADDR(xfs_bmbt, block, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 }
6160
6161 if (prevp) {
6162 xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
6163 }
6164 prevp = keyp;
6165
6166 /*
6167 * Compare the block numbers to see if there are dups.
6168 */
6169
6170 if (root) {
6171 pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
6172 } else {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006173 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, i, dmxr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11006175 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 if (root) {
6177 thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz);
6178 } else {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006179 thispa = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, j,
6180 dmxr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 }
Christoph Hellwig576039c2006-09-28 10:58:06 +10006182 if (*thispa == *pp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
6184 __FUNCTION__, j, i,
Christoph Hellwig576039c2006-09-28 10:58:06 +10006185 (unsigned long long)be64_to_cpu(*thispa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 panic("%s: ptrs are equal in node\n",
6187 __FUNCTION__);
6188 }
6189 }
6190 }
6191}
6192
6193/*
6194 * Check that the extents for the inode ip are in the right order in all
6195 * btree leaves.
6196 */
6197
6198STATIC void
6199xfs_bmap_check_leaf_extents(
6200 xfs_btree_cur_t *cur, /* btree cursor or null */
6201 xfs_inode_t *ip, /* incore inode pointer */
6202 int whichfork) /* data or attr fork */
6203{
6204 xfs_bmbt_block_t *block; /* current btree block */
6205 xfs_fsblock_t bno; /* block # of "block" */
6206 xfs_buf_t *bp; /* buffer for "block" */
6207 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006208 xfs_extnum_t i=0, j; /* index into the extents list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 xfs_ifork_t *ifp; /* fork structure */
6210 int level; /* btree level, for checking */
6211 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10006212 __be64 *pp; /* pointer to block address */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006213 xfs_bmbt_rec_t *ep; /* pointer to current extent */
6214 xfs_bmbt_rec_t *lastp; /* pointer to previous extent */
6215 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 int bp_release = 0;
6217
6218 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6219 return;
6220 }
6221
6222 bno = NULLFSBLOCK;
6223 mp = ip->i_mount;
6224 ifp = XFS_IFORK_PTR(ip, whichfork);
6225 block = ifp->if_broot;
6226 /*
6227 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6228 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11006229 level = be16_to_cpu(block->bb_level);
6230 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6232 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006233 bno = be64_to_cpu(*pp);
6234
6235 ASSERT(bno != NULLDFSBNO);
6236 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6237 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6238
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 /*
6240 * Go down the tree until leaf level is reached, following the first
6241 * pointer (leftmost) at each level.
6242 */
6243 while (level-- > 0) {
6244 /* See if buf is in cur first */
6245 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6246 if (bp) {
6247 bp_release = 0;
6248 } else {
6249 bp_release = 1;
6250 }
6251 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6252 XFS_BMAP_BTREE_REF)))
6253 goto error_norelse;
6254 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6255 XFS_WANT_CORRUPTED_GOTO(
6256 XFS_BMAP_SANITY_CHECK(mp, block, level),
6257 error0);
6258 if (level == 0)
6259 break;
6260
6261 /*
6262 * Check this block for basic sanity (increasing keys and
6263 * no duplicate blocks).
6264 */
6265
6266 xfs_check_block(block, mp, 0, 0);
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006267 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006268 bno = be64_to_cpu(*pp);
6269 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 if (bp_release) {
6271 bp_release = 0;
6272 xfs_trans_brelse(NULL, bp);
6273 }
6274 }
6275
6276 /*
6277 * Here with bp and block set to the leftmost leaf node in the tree.
6278 */
6279 i = 0;
6280
6281 /*
6282 * Loop over all leaf nodes checking that all extents are in the right order.
6283 */
6284 lastp = NULL;
6285 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 xfs_fsblock_t nextbno;
6287 xfs_extnum_t num_recs;
6288
6289
Christoph Hellwig16259e72005-11-02 15:11:25 +11006290 num_recs = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291
6292 /*
6293 * Read-ahead the next leaf block, if any.
6294 */
6295
Christoph Hellwig16259e72005-11-02 15:11:25 +11006296 nextbno = be64_to_cpu(block->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297
6298 /*
6299 * Check all the extents to make sure they are OK.
6300 * If we had a previous block, the last entry should
6301 * conform with the first entry in this one.
6302 */
6303
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006304 ep = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006305 for (j = 1; j < num_recs; j++) {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006306 nextp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, j + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 if (lastp) {
6308 xfs_btree_check_rec(XFS_BTNUM_BMAP,
6309 (void *)lastp, (void *)ep);
6310 }
6311 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006312 (void *)(nextp));
6313 lastp = ep;
6314 ep = nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316
6317 i += num_recs;
6318 if (bp_release) {
6319 bp_release = 0;
6320 xfs_trans_brelse(NULL, bp);
6321 }
6322 bno = nextbno;
6323 /*
6324 * If we've reached the end, stop.
6325 */
6326 if (bno == NULLFSBLOCK)
6327 break;
6328
6329 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6330 if (bp) {
6331 bp_release = 0;
6332 } else {
6333 bp_release = 1;
6334 }
6335 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6336 XFS_BMAP_BTREE_REF)))
6337 goto error_norelse;
6338 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6339 }
6340 if (bp_release) {
6341 bp_release = 0;
6342 xfs_trans_brelse(NULL, bp);
6343 }
6344 return;
6345
6346error0:
6347 cmn_err(CE_WARN, "%s: at error0", __FUNCTION__);
6348 if (bp_release)
6349 xfs_trans_brelse(NULL, bp);
6350error_norelse:
6351 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
Christoph Hellwigda1650a2005-11-02 10:21:35 +11006352 __FUNCTION__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__);
6354 return;
6355}
6356#endif
6357
6358/*
6359 * Count fsblocks of the given fork.
6360 */
6361int /* error */
6362xfs_bmap_count_blocks(
6363 xfs_trans_t *tp, /* transaction pointer */
6364 xfs_inode_t *ip, /* incore inode */
6365 int whichfork, /* data or attr fork */
6366 int *count) /* out: count of blocks */
6367{
6368 xfs_bmbt_block_t *block; /* current btree block */
6369 xfs_fsblock_t bno; /* block # of "block" */
6370 xfs_ifork_t *ifp; /* fork structure */
6371 int level; /* btree level, for checking */
6372 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10006373 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374
6375 bno = NULLFSBLOCK;
6376 mp = ip->i_mount;
6377 ifp = XFS_IFORK_PTR(ip, whichfork);
6378 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006379 if (unlikely(xfs_bmap_count_leaves(ifp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6381 count) < 0)) {
6382 XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)",
6383 XFS_ERRLEVEL_LOW, mp);
6384 return XFS_ERROR(EFSCORRUPTED);
6385 }
6386 return 0;
6387 }
6388
6389 /*
6390 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6391 */
6392 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11006393 level = be16_to_cpu(block->bb_level);
6394 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006396 bno = be64_to_cpu(*pp);
6397 ASSERT(bno != NULLDFSBNO);
6398 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6399 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006401 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6403 mp);
6404 return XFS_ERROR(EFSCORRUPTED);
6405 }
6406
6407 return 0;
6408}
6409
6410/*
6411 * Recursively walks each level of a btree
6412 * to count total fsblocks is use.
6413 */
6414int /* error */
6415xfs_bmap_count_tree(
6416 xfs_mount_t *mp, /* file system mount point */
6417 xfs_trans_t *tp, /* transaction pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006418 xfs_ifork_t *ifp, /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 xfs_fsblock_t blockno, /* file system block number */
6420 int levelin, /* level in btree */
6421 int *count) /* Count of blocks */
6422{
6423 int error;
6424 xfs_buf_t *bp, *nbp;
6425 int level = levelin;
Christoph Hellwig576039c2006-09-28 10:58:06 +10006426 __be64 *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 xfs_fsblock_t bno = blockno;
6428 xfs_fsblock_t nextbno;
6429 xfs_bmbt_block_t *block, *nextblock;
6430 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431
6432 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6433 return error;
6434 *count += 1;
6435 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6436
6437 if (--level) {
6438 /* Not at node above leafs, count this level of nodes */
Christoph Hellwig16259e72005-11-02 15:11:25 +11006439 nextbno = be64_to_cpu(block->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 while (nextbno != NULLFSBLOCK) {
6441 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6442 0, &nbp, XFS_BMAP_BTREE_REF)))
6443 return error;
6444 *count += 1;
6445 nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11006446 nextbno = be64_to_cpu(nextblock->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 xfs_trans_brelse(tp, nbp);
6448 }
6449
6450 /* Dive to the next level */
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006451 pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006452 bno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 if (unlikely((error =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006454 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 xfs_trans_brelse(tp, bp);
6456 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6457 XFS_ERRLEVEL_LOW, mp);
6458 return XFS_ERROR(EFSCORRUPTED);
6459 }
6460 xfs_trans_brelse(tp, bp);
6461 } else {
6462 /* count all level 1 nodes and their leaves */
6463 for (;;) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11006464 nextbno = be64_to_cpu(block->bb_rightsib);
6465 numrecs = be16_to_cpu(block->bb_numrecs);
Eric Sandeene9ed9d22007-05-08 13:48:56 +10006466 if (unlikely(xfs_bmap_disk_count_leaves(0,
6467 block, numrecs, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 xfs_trans_brelse(tp, bp);
6469 XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
6470 XFS_ERRLEVEL_LOW, mp);
6471 return XFS_ERROR(EFSCORRUPTED);
6472 }
6473 xfs_trans_brelse(tp, bp);
6474 if (nextbno == NULLFSBLOCK)
6475 break;
6476 bno = nextbno;
6477 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6478 XFS_BMAP_BTREE_REF)))
6479 return error;
6480 *count += 1;
6481 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6482 }
6483 }
6484 return 0;
6485}
6486
6487/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006488 * Count leaf blocks given a range of extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 */
6490int
6491xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006492 xfs_ifork_t *ifp,
6493 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494 int numrecs,
6495 int *count)
6496{
6497 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006498 xfs_bmbt_rec_t *frp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006500 for (b = 0; b < numrecs; b++) {
6501 frp = xfs_iext_get_ext(ifp, idx + b);
Yingping Lu91e11082005-11-02 15:10:24 +11006502 *count += xfs_bmbt_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006503 }
Yingping Lu91e11082005-11-02 15:10:24 +11006504 return 0;
6505}
6506
6507/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006508 * Count leaf blocks given a range of extent records originally
6509 * in btree format.
Yingping Lu91e11082005-11-02 15:10:24 +11006510 */
6511int
6512xfs_bmap_disk_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006513 xfs_extnum_t idx,
6514 xfs_bmbt_block_t *block,
Yingping Lu91e11082005-11-02 15:10:24 +11006515 int numrecs,
6516 int *count)
6517{
6518 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006519 xfs_bmbt_rec_t *frp;
Yingping Lu91e11082005-11-02 15:10:24 +11006520
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006521 for (b = 1; b <= numrecs; b++) {
Eric Sandeen2c36dde2007-02-10 18:37:33 +11006522 frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, idx + b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523 *count += xfs_bmbt_disk_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525 return 0;
6526}