blob: bf46fae303af9826296dc9767fd8d696e2118862 [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"
55
56
57#ifdef DEBUG
58STATIC void
59xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork);
60#endif
61
62kmem_zone_t *xfs_bmap_free_item_zone;
63
64/*
65 * Prototypes for internal bmap routines.
66 */
67
68
69/*
70 * Called from xfs_bmap_add_attrfork to handle extents format files.
71 */
72STATIC int /* error */
73xfs_bmap_add_attrfork_extents(
74 xfs_trans_t *tp, /* transaction pointer */
75 xfs_inode_t *ip, /* incore inode pointer */
76 xfs_fsblock_t *firstblock, /* first block allocated */
77 xfs_bmap_free_t *flist, /* blocks to free at commit */
78 int *flags); /* inode logging flags */
79
80/*
81 * Called from xfs_bmap_add_attrfork to handle local format files.
82 */
83STATIC int /* error */
84xfs_bmap_add_attrfork_local(
85 xfs_trans_t *tp, /* transaction pointer */
86 xfs_inode_t *ip, /* incore inode pointer */
87 xfs_fsblock_t *firstblock, /* first block allocated */
88 xfs_bmap_free_t *flist, /* blocks to free at commit */
89 int *flags); /* inode logging flags */
90
91/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +110092 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * after allocating space (or doing a delayed allocation).
94 */
95STATIC int /* error */
96xfs_bmap_add_extent(
97 xfs_inode_t *ip, /* incore inode pointer */
98 xfs_extnum_t idx, /* extent number to update/insert */
99 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100100 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 xfs_fsblock_t *first, /* pointer to firstblock variable */
102 xfs_bmap_free_t *flist, /* list of extents to be freed */
103 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000104 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int whichfork, /* data or attr fork */
106 int rsvd); /* OK to allocate reserved blocks */
107
108/*
109 * Called by xfs_bmap_add_extent to handle cases converting a delayed
110 * allocation to a real allocation.
111 */
112STATIC int /* error */
113xfs_bmap_add_extent_delay_real(
114 xfs_inode_t *ip, /* incore inode pointer */
115 xfs_extnum_t idx, /* extent number to update/insert */
116 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100117 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
119 xfs_fsblock_t *first, /* pointer to firstblock variable */
120 xfs_bmap_free_t *flist, /* list of extents to be freed */
121 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000122 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int rsvd); /* OK to allocate reserved blocks */
124
125/*
126 * Called by xfs_bmap_add_extent to handle cases converting a hole
127 * to a delayed allocation.
128 */
129STATIC int /* error */
130xfs_bmap_add_extent_hole_delay(
131 xfs_inode_t *ip, /* incore inode pointer */
132 xfs_extnum_t idx, /* extent number to update/insert */
133 xfs_btree_cur_t *cur, /* if null, not a btree */
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
188#ifdef DEBUG
189/*
190 * Check that the extents list for the inode ip is in the right order.
191 */
192STATIC void
193xfs_bmap_check_extents(
194 xfs_inode_t *ip, /* incore inode pointer */
195 int whichfork); /* data or attr fork */
196#endif
197
198/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100199 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * after removing space (or undoing a delayed allocation).
201 */
202STATIC int /* error */
203xfs_bmap_del_extent(
204 xfs_inode_t *ip, /* incore inode pointer */
205 xfs_trans_t *tp, /* current trans pointer */
206 xfs_extnum_t idx, /* extent number to update/insert */
207 xfs_bmap_free_t *flist, /* list of extents to be freed */
208 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100209 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 int *logflagsp,/* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000211 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 int whichfork, /* data or attr fork */
213 int rsvd); /* OK to allocate reserved blocks */
214
215/*
216 * Remove the entry "free" from the free item list. Prev points to the
217 * previous entry, unless "free" is the head of the list.
218 */
219STATIC void
220xfs_bmap_del_free(
221 xfs_bmap_free_t *flist, /* free item list header */
222 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
223 xfs_bmap_free_item_t *free); /* list item to be freed */
224
225/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 * Convert an extents-format file into a btree-format file.
227 * The new file will have a root block (in the inode) and a single child block.
228 */
229STATIC int /* error */
230xfs_bmap_extents_to_btree(
231 xfs_trans_t *tp, /* transaction pointer */
232 xfs_inode_t *ip, /* incore inode pointer */
233 xfs_fsblock_t *firstblock, /* first-block-allocated */
234 xfs_bmap_free_t *flist, /* blocks freed in xaction */
235 xfs_btree_cur_t **curp, /* cursor returned to caller */
236 int wasdel, /* converting a delayed alloc */
237 int *logflagsp, /* inode logging flags */
238 int whichfork); /* data or attr fork */
239
240/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * Convert a local file to an extents file.
242 * This code is sort of bogus, since the file data needs to get
243 * logged so it won't be lost. The bmap-level manipulations are ok, though.
244 */
245STATIC int /* error */
246xfs_bmap_local_to_extents(
247 xfs_trans_t *tp, /* transaction pointer */
248 xfs_inode_t *ip, /* incore inode pointer */
249 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
250 xfs_extlen_t total, /* total blocks needed by transaction */
251 int *logflagsp, /* inode logging flags */
252 int whichfork); /* data or attr fork */
253
254/*
255 * Search the extents list for the inode, for the extent containing bno.
256 * If bno lies in a hole, point to the next entry. If bno lies past eof,
257 * *eofp will be set, and *prevp will contain the last entry (null if none).
258 * Else, *lastxp will be set to the index of the found
259 * entry; *gotp will contain the entry.
260 */
261STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
262xfs_bmap_search_extents(
263 xfs_inode_t *ip, /* incore inode pointer */
264 xfs_fileoff_t bno, /* block number searched for */
265 int whichfork, /* data or attr fork */
266 int *eofp, /* out: end of file found */
267 xfs_extnum_t *lastxp, /* out: last extent index */
268 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
269 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
270
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000271/*
272 * Check the last inode extent to determine whether this allocation will result
273 * in blocks being allocated at the end of the file. When we allocate new data
274 * blocks at the end of the file which do not start at the previous data block,
275 * we will try to align the new blocks at stripe unit boundaries.
276 */
277STATIC int /* error */
278xfs_bmap_isaeof(
279 xfs_inode_t *ip, /* incore inode pointer */
280 xfs_fileoff_t off, /* file offset in fsblocks */
281 int whichfork, /* data or attribute fork */
282 char *aeof); /* return value */
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284#ifdef XFS_BMAP_TRACE
285/*
286 * Add a bmap trace buffer entry. Base routine for the others.
287 */
288STATIC void
289xfs_bmap_trace_addentry(
290 int opcode, /* operation */
291 char *fname, /* function name */
292 char *desc, /* operation description */
293 xfs_inode_t *ip, /* incore inode pointer */
294 xfs_extnum_t idx, /* index of entry(ies) */
295 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
296 xfs_bmbt_rec_t *r1, /* first record */
297 xfs_bmbt_rec_t *r2, /* second record or null */
298 int whichfork); /* data or attr fork */
299
300/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100301 * Add bmap trace entry prior to a call to xfs_iext_remove.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 */
303STATIC void
304xfs_bmap_trace_delete(
305 char *fname, /* function name */
306 char *desc, /* operation description */
307 xfs_inode_t *ip, /* incore inode pointer */
308 xfs_extnum_t idx, /* index of entry(entries) deleted */
309 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
310 int whichfork); /* data or attr fork */
311
312/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100313 * Add bmap trace entry prior to a call to xfs_iext_insert, or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * reading in the extents list from the disk (in the btree).
315 */
316STATIC void
317xfs_bmap_trace_insert(
318 char *fname, /* function name */
319 char *desc, /* operation description */
320 xfs_inode_t *ip, /* incore inode pointer */
321 xfs_extnum_t idx, /* index of entry(entries) inserted */
322 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
323 xfs_bmbt_irec_t *r1, /* inserted record 1 */
324 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
325 int whichfork); /* data or attr fork */
326
327/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100328 * Add bmap trace entry after updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
330STATIC void
331xfs_bmap_trace_post_update(
332 char *fname, /* function name */
333 char *desc, /* operation description */
334 xfs_inode_t *ip, /* incore inode pointer */
335 xfs_extnum_t idx, /* index of entry updated */
336 int whichfork); /* data or attr fork */
337
338/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100339 * Add bmap trace entry prior to updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
341STATIC void
342xfs_bmap_trace_pre_update(
343 char *fname, /* function name */
344 char *desc, /* operation description */
345 xfs_inode_t *ip, /* incore inode pointer */
346 xfs_extnum_t idx, /* index of entry to be updated */
347 int whichfork); /* data or attr fork */
348
349#else
350#define xfs_bmap_trace_delete(f,d,ip,i,c,w)
351#define xfs_bmap_trace_insert(f,d,ip,i,c,r1,r2,w)
352#define xfs_bmap_trace_post_update(f,d,ip,i,w)
353#define xfs_bmap_trace_pre_update(f,d,ip,i,w)
354#endif /* XFS_BMAP_TRACE */
355
356/*
357 * Compute the worst-case number of indirect blocks that will be used
358 * for ip's delayed extent of length "len".
359 */
360STATIC xfs_filblks_t
361xfs_bmap_worst_indlen(
362 xfs_inode_t *ip, /* incore inode pointer */
363 xfs_filblks_t len); /* delayed extent length */
364
365#ifdef DEBUG
366/*
367 * Perform various validation checks on the values being returned
368 * from xfs_bmapi().
369 */
370STATIC void
371xfs_bmap_validate_ret(
372 xfs_fileoff_t bno,
373 xfs_filblks_t len,
374 int flags,
375 xfs_bmbt_irec_t *mval,
376 int nmap,
377 int ret_nmap);
378#else
379#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
380#endif /* DEBUG */
381
382#if defined(XFS_RW_TRACE)
383STATIC void
384xfs_bunmap_trace(
385 xfs_inode_t *ip,
386 xfs_fileoff_t bno,
387 xfs_filblks_t len,
388 int flags,
389 inst_t *ra);
390#else
391#define xfs_bunmap_trace(ip, bno, len, flags, ra)
392#endif /* XFS_RW_TRACE */
393
394STATIC int
395xfs_bmap_count_tree(
396 xfs_mount_t *mp,
397 xfs_trans_t *tp,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100398 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 xfs_fsblock_t blockno,
400 int levelin,
401 int *count);
402
403STATIC int
404xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100405 xfs_ifork_t *ifp,
406 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 int numrecs,
408 int *count);
409
Yingping Lu91e11082005-11-02 15:10:24 +1100410STATIC int
411xfs_bmap_disk_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100412 xfs_ifork_t *ifp,
413 xfs_mount_t *mp,
414 xfs_extnum_t idx,
415 xfs_bmbt_block_t *block,
Yingping Lu91e11082005-11-02 15:10:24 +1100416 int numrecs,
417 int *count);
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/*
420 * Bmap internal routines.
421 */
422
423/*
424 * Called from xfs_bmap_add_attrfork to handle btree format files.
425 */
426STATIC int /* error */
427xfs_bmap_add_attrfork_btree(
428 xfs_trans_t *tp, /* transaction pointer */
429 xfs_inode_t *ip, /* incore inode pointer */
430 xfs_fsblock_t *firstblock, /* first block allocated */
431 xfs_bmap_free_t *flist, /* blocks to free at commit */
432 int *flags) /* inode logging flags */
433{
434 xfs_btree_cur_t *cur; /* btree cursor */
435 int error; /* error return value */
436 xfs_mount_t *mp; /* file system mount struct */
437 int stat; /* newroot status */
438
439 mp = ip->i_mount;
440 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
441 *flags |= XFS_ILOG_DBROOT;
442 else {
443 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
444 XFS_DATA_FORK);
445 cur->bc_private.b.flist = flist;
446 cur->bc_private.b.firstblock = *firstblock;
447 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
448 goto error0;
449 ASSERT(stat == 1); /* must be at least one entry */
450 if ((error = xfs_bmbt_newroot(cur, flags, &stat)))
451 goto error0;
452 if (stat == 0) {
453 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
454 return XFS_ERROR(ENOSPC);
455 }
456 *firstblock = cur->bc_private.b.firstblock;
457 cur->bc_private.b.allocated = 0;
458 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
459 }
460 return 0;
461error0:
462 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
463 return error;
464}
465
466/*
467 * Called from xfs_bmap_add_attrfork to handle extents format files.
468 */
469STATIC int /* error */
470xfs_bmap_add_attrfork_extents(
471 xfs_trans_t *tp, /* transaction pointer */
472 xfs_inode_t *ip, /* incore inode pointer */
473 xfs_fsblock_t *firstblock, /* first block allocated */
474 xfs_bmap_free_t *flist, /* blocks to free at commit */
475 int *flags) /* inode logging flags */
476{
477 xfs_btree_cur_t *cur; /* bmap btree cursor */
478 int error; /* error return value */
479
480 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
481 return 0;
482 cur = NULL;
483 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
484 flags, XFS_DATA_FORK);
485 if (cur) {
486 cur->bc_private.b.allocated = 0;
487 xfs_btree_del_cursor(cur,
488 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
489 }
490 return error;
491}
492
493/*
494 * Called from xfs_bmap_add_attrfork to handle local format files.
495 */
496STATIC int /* error */
497xfs_bmap_add_attrfork_local(
498 xfs_trans_t *tp, /* transaction pointer */
499 xfs_inode_t *ip, /* incore inode pointer */
500 xfs_fsblock_t *firstblock, /* first block allocated */
501 xfs_bmap_free_t *flist, /* blocks to free at commit */
502 int *flags) /* inode logging flags */
503{
504 xfs_da_args_t dargs; /* args for dir/attr code */
505 int error; /* error return value */
506 xfs_mount_t *mp; /* mount structure pointer */
507
508 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
509 return 0;
510 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
511 mp = ip->i_mount;
512 memset(&dargs, 0, sizeof(dargs));
513 dargs.dp = ip;
514 dargs.firstblock = firstblock;
515 dargs.flist = flist;
516 dargs.total = mp->m_dirblkfsbs;
517 dargs.whichfork = XFS_DATA_FORK;
518 dargs.trans = tp;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000519 error = xfs_dir2_sf_to_block(&dargs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 } else
521 error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
522 XFS_DATA_FORK);
523 return error;
524}
525
526/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100527 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 * after allocating space (or doing a delayed allocation).
529 */
530STATIC int /* error */
531xfs_bmap_add_extent(
532 xfs_inode_t *ip, /* incore inode pointer */
533 xfs_extnum_t idx, /* extent number to update/insert */
534 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100535 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 xfs_fsblock_t *first, /* pointer to firstblock variable */
537 xfs_bmap_free_t *flist, /* list of extents to be freed */
538 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000539 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 int whichfork, /* data or attr fork */
541 int rsvd) /* OK to use reserved data blocks */
542{
543 xfs_btree_cur_t *cur; /* btree cursor or null */
544 xfs_filblks_t da_new; /* new count del alloc blocks used */
545 xfs_filblks_t da_old; /* old count del alloc blocks used */
546 int error; /* error return value */
547#ifdef XFS_BMAP_TRACE
548 static char fname[] = "xfs_bmap_add_extent";
549#endif
550 xfs_ifork_t *ifp; /* inode fork ptr */
551 int logflags; /* returned value */
552 xfs_extnum_t nextents; /* number of extents in file now */
553
554 XFS_STATS_INC(xs_add_exlist);
555 cur = *curp;
556 ifp = XFS_IFORK_PTR(ip, whichfork);
557 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
558 ASSERT(idx <= nextents);
559 da_old = da_new = 0;
560 error = 0;
561 /*
562 * This is the first extent added to a new/empty file.
563 * Special case this one, so other routines get to assume there are
564 * already extents in the list.
565 */
566 if (nextents == 0) {
567 xfs_bmap_trace_insert(fname, "insert empty", ip, 0, 1, new,
568 NULL, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100569 xfs_iext_insert(ifp, 0, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 ASSERT(cur == NULL);
571 ifp->if_lastex = 0;
572 if (!ISNULLSTARTBLOCK(new->br_startblock)) {
573 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
574 logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
575 } else
576 logflags = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000577 /* DELTA: single new extent */
578 if (delta) {
579 if (delta->xed_startoff > new->br_startoff)
580 delta->xed_startoff = new->br_startoff;
581 if (delta->xed_blockcount <
582 new->br_startoff + new->br_blockcount)
583 delta->xed_blockcount = new->br_startoff +
584 new->br_blockcount;
585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587 /*
588 * Any kind of new delayed allocation goes here.
589 */
590 else if (ISNULLSTARTBLOCK(new->br_startblock)) {
591 if (cur)
592 ASSERT((cur->bc_private.b.flags &
593 XFS_BTCUR_BPRV_WASDEL) == 0);
594 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, cur, new,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000595 &logflags, delta, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 goto done;
597 }
598 /*
599 * Real allocation off the end of the file.
600 */
601 else if (idx == nextents) {
602 if (cur)
603 ASSERT((cur->bc_private.b.flags &
604 XFS_BTCUR_BPRV_WASDEL) == 0);
605 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000606 &logflags, delta, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 goto done;
608 } else {
609 xfs_bmbt_irec_t prev; /* old extent at offset idx */
610
611 /*
612 * Get the record referred to by idx.
613 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100614 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /*
616 * If it's a real allocation record, and the new allocation ends
617 * after the start of the referred to record, then we're filling
618 * in a delayed or unwritten allocation with a real one, or
619 * converting real back to unwritten.
620 */
621 if (!ISNULLSTARTBLOCK(new->br_startblock) &&
622 new->br_startoff + new->br_blockcount > prev.br_startoff) {
623 if (prev.br_state != XFS_EXT_UNWRITTEN &&
624 ISNULLSTARTBLOCK(prev.br_startblock)) {
625 da_old = STARTBLOCKVAL(prev.br_startblock);
626 if (cur)
627 ASSERT(cur->bc_private.b.flags &
628 XFS_BTCUR_BPRV_WASDEL);
629 if ((error = xfs_bmap_add_extent_delay_real(ip,
630 idx, &cur, new, &da_new, first, flist,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000631 &logflags, delta, rsvd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 goto done;
633 } else if (new->br_state == XFS_EXT_NORM) {
634 ASSERT(new->br_state == XFS_EXT_NORM);
635 if ((error = xfs_bmap_add_extent_unwritten_real(
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000636 ip, idx, &cur, new, &logflags, delta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 goto done;
638 } else {
639 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
640 if ((error = xfs_bmap_add_extent_unwritten_real(
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000641 ip, idx, &cur, new, &logflags, delta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 goto done;
643 }
644 ASSERT(*curp == cur || *curp == NULL);
645 }
646 /*
647 * Otherwise we're filling in a hole with an allocation.
648 */
649 else {
650 if (cur)
651 ASSERT((cur->bc_private.b.flags &
652 XFS_BTCUR_BPRV_WASDEL) == 0);
653 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000654 new, &logflags, delta, whichfork)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 goto done;
656 }
657 }
658
659 ASSERT(*curp == cur || *curp == NULL);
660 /*
661 * Convert to a btree if necessary.
662 */
663 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
664 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
665 int tmp_logflags; /* partial log flag return val */
666
667 ASSERT(cur == NULL);
668 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
669 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
670 logflags |= tmp_logflags;
671 if (error)
672 goto done;
673 }
674 /*
675 * Adjust for changes in reserved delayed indirect blocks.
676 * Nothing to do for disk quotas here.
677 */
678 if (da_old || da_new) {
679 xfs_filblks_t nblks;
680
681 nblks = da_new;
682 if (cur)
683 nblks += cur->bc_private.b.allocated;
684 ASSERT(nblks <= da_old);
685 if (nblks < da_old)
686 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
687 (int)(da_old - nblks), rsvd);
688 }
689 /*
690 * Clear out the allocated field, done with it now in any case.
691 */
692 if (cur) {
693 cur->bc_private.b.allocated = 0;
694 *curp = cur;
695 }
696done:
697#ifdef DEBUG
698 if (!error)
699 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
700#endif
701 *logflagsp = logflags;
702 return error;
703}
704
705/*
706 * Called by xfs_bmap_add_extent to handle cases converting a delayed
707 * allocation to a real allocation.
708 */
709STATIC int /* error */
710xfs_bmap_add_extent_delay_real(
711 xfs_inode_t *ip, /* incore inode pointer */
712 xfs_extnum_t idx, /* extent number to update/insert */
713 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100714 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
716 xfs_fsblock_t *first, /* pointer to firstblock variable */
717 xfs_bmap_free_t *flist, /* list of extents to be freed */
718 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000719 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 int rsvd) /* OK to use reserved data block allocation */
721{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 xfs_btree_cur_t *cur; /* btree cursor */
723 int diff; /* temp value */
724 xfs_bmbt_rec_t *ep; /* extent entry for idx */
725 int error; /* error return value */
726#ifdef XFS_BMAP_TRACE
727 static char fname[] = "xfs_bmap_add_extent_delay_real";
728#endif
729 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100730 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 xfs_fileoff_t new_endoff; /* end offset of new entry */
732 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
733 /* left is 0, right is 1, prev is 2 */
734 int rval=0; /* return value (logging flags) */
735 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000736 xfs_filblks_t temp=0; /* value for dnew calculations */
737 xfs_filblks_t temp2=0;/* value for dnew calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 int tmp_rval; /* partial logging flags */
739 enum { /* bit number definitions for state */
740 LEFT_CONTIG, RIGHT_CONTIG,
741 LEFT_FILLING, RIGHT_FILLING,
742 LEFT_DELAY, RIGHT_DELAY,
743 LEFT_VALID, RIGHT_VALID
744 };
745
746#define LEFT r[0]
747#define RIGHT r[1]
748#define PREV r[2]
749#define MASK(b) (1 << (b))
750#define MASK2(a,b) (MASK(a) | MASK(b))
751#define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
752#define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
753#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
754#define STATE_TEST(b) (state & MASK(b))
755#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
756 ((state &= ~MASK(b)), 0))
757#define SWITCH_STATE \
758 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
759
760 /*
761 * Set up a bunch of variables to make the tests simpler.
762 */
763 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100764 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
765 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 xfs_bmbt_get_all(ep, &PREV);
767 new_endoff = new->br_startoff + new->br_blockcount;
768 ASSERT(PREV.br_startoff <= new->br_startoff);
769 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
770 /*
771 * Set flags determining what part of the previous delayed allocation
772 * extent is being replaced by a real allocation.
773 */
774 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
775 STATE_SET(RIGHT_FILLING,
776 PREV.br_startoff + PREV.br_blockcount == new_endoff);
777 /*
778 * Check and set flags if this segment has a left neighbor.
779 * Don't set contiguous if the combined extent would be too large.
780 */
781 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100782 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
784 }
785 STATE_SET(LEFT_CONTIG,
786 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
787 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
788 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
789 LEFT.br_state == new->br_state &&
790 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
791 /*
792 * Check and set flags if this segment has a right neighbor.
793 * Don't set contiguous if the combined extent would be too large.
794 * Also check for all-three-contiguous being too large.
795 */
796 if (STATE_SET_TEST(RIGHT_VALID,
797 idx <
798 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100799 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
801 }
802 STATE_SET(RIGHT_CONTIG,
803 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
804 new_endoff == RIGHT.br_startoff &&
805 new->br_startblock + new->br_blockcount ==
806 RIGHT.br_startblock &&
807 new->br_state == RIGHT.br_state &&
808 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
809 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
810 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
811 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
812 <= MAXEXTLEN));
813 error = 0;
814 /*
815 * Switch out based on the FILLING and CONTIG state bits.
816 */
817 switch (SWITCH_STATE) {
818
819 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
820 /*
821 * Filling in all of a previously delayed allocation extent.
822 * The left and right neighbors are both contiguous with new.
823 */
824 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
825 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100826 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 LEFT.br_blockcount + PREV.br_blockcount +
828 RIGHT.br_blockcount);
829 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
830 XFS_DATA_FORK);
831 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
832 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100833 xfs_iext_remove(ifp, idx, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 ip->i_df.if_lastex = idx - 1;
835 ip->i_d.di_nextents--;
836 if (cur == NULL)
837 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
838 else {
839 rval = XFS_ILOG_CORE;
840 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
841 RIGHT.br_startblock,
842 RIGHT.br_blockcount, &i)))
843 goto done;
844 ASSERT(i == 1);
845 if ((error = xfs_bmbt_delete(cur, &i)))
846 goto done;
847 ASSERT(i == 1);
848 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
849 goto done;
850 ASSERT(i == 1);
851 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
852 LEFT.br_startblock,
853 LEFT.br_blockcount +
854 PREV.br_blockcount +
855 RIGHT.br_blockcount, LEFT.br_state)))
856 goto done;
857 }
858 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000859 /* DELTA: Three in-core extents are replaced by one. */
860 temp = LEFT.br_startoff;
861 temp2 = LEFT.br_blockcount +
862 PREV.br_blockcount +
863 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 break;
865
866 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
867 /*
868 * Filling in all of a previously delayed allocation extent.
869 * The left neighbor is contiguous, the right is not.
870 */
871 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
872 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100873 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 LEFT.br_blockcount + PREV.br_blockcount);
875 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
876 XFS_DATA_FORK);
877 ip->i_df.if_lastex = idx - 1;
878 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
879 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100880 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (cur == NULL)
882 rval = XFS_ILOG_DEXT;
883 else {
884 rval = 0;
885 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
886 LEFT.br_startblock, LEFT.br_blockcount,
887 &i)))
888 goto done;
889 ASSERT(i == 1);
890 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
891 LEFT.br_startblock,
892 LEFT.br_blockcount +
893 PREV.br_blockcount, LEFT.br_state)))
894 goto done;
895 }
896 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000897 /* DELTA: Two in-core extents are replaced by one. */
898 temp = LEFT.br_startoff;
899 temp2 = LEFT.br_blockcount +
900 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 break;
902
903 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
904 /*
905 * Filling in all of a previously delayed allocation extent.
906 * The right neighbor is contiguous, the left is not.
907 */
908 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
909 XFS_DATA_FORK);
910 xfs_bmbt_set_startblock(ep, new->br_startblock);
911 xfs_bmbt_set_blockcount(ep,
912 PREV.br_blockcount + RIGHT.br_blockcount);
913 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
914 XFS_DATA_FORK);
915 ip->i_df.if_lastex = idx;
916 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
917 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100918 xfs_iext_remove(ifp, idx + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (cur == NULL)
920 rval = XFS_ILOG_DEXT;
921 else {
922 rval = 0;
923 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
924 RIGHT.br_startblock,
925 RIGHT.br_blockcount, &i)))
926 goto done;
927 ASSERT(i == 1);
928 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
929 new->br_startblock,
930 PREV.br_blockcount +
931 RIGHT.br_blockcount, PREV.br_state)))
932 goto done;
933 }
934 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000935 /* DELTA: Two in-core extents are replaced by one. */
936 temp = PREV.br_startoff;
937 temp2 = PREV.br_blockcount +
938 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 break;
940
941 case MASK2(LEFT_FILLING, RIGHT_FILLING):
942 /*
943 * Filling in all of a previously delayed allocation extent.
944 * Neither the left nor right neighbors are contiguous with
945 * the new one.
946 */
947 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
948 XFS_DATA_FORK);
949 xfs_bmbt_set_startblock(ep, new->br_startblock);
950 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
951 XFS_DATA_FORK);
952 ip->i_df.if_lastex = idx;
953 ip->i_d.di_nextents++;
954 if (cur == NULL)
955 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
956 else {
957 rval = XFS_ILOG_CORE;
958 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
959 new->br_startblock, new->br_blockcount,
960 &i)))
961 goto done;
962 ASSERT(i == 0);
963 cur->bc_rec.b.br_state = XFS_EXT_NORM;
964 if ((error = xfs_bmbt_insert(cur, &i)))
965 goto done;
966 ASSERT(i == 1);
967 }
968 *dnew = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +1000969 /* DELTA: The in-core extent described by new changed type. */
970 temp = new->br_startoff;
971 temp2 = new->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 break;
973
974 case MASK2(LEFT_FILLING, LEFT_CONTIG):
975 /*
976 * Filling in the first part of a previous delayed allocation.
977 * The left neighbor is contiguous.
978 */
979 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
980 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100981 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 LEFT.br_blockcount + new->br_blockcount);
983 xfs_bmbt_set_startoff(ep,
984 PREV.br_startoff + new->br_blockcount);
985 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
986 XFS_DATA_FORK);
987 temp = PREV.br_blockcount - new->br_blockcount;
988 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
989 XFS_DATA_FORK);
990 xfs_bmbt_set_blockcount(ep, temp);
991 ip->i_df.if_lastex = idx - 1;
992 if (cur == NULL)
993 rval = XFS_ILOG_DEXT;
994 else {
995 rval = 0;
996 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
997 LEFT.br_startblock, LEFT.br_blockcount,
998 &i)))
999 goto done;
1000 ASSERT(i == 1);
1001 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1002 LEFT.br_startblock,
1003 LEFT.br_blockcount +
1004 new->br_blockcount,
1005 LEFT.br_state)))
1006 goto done;
1007 }
1008 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1009 STARTBLOCKVAL(PREV.br_startblock));
1010 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1011 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
1012 XFS_DATA_FORK);
1013 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001014 /* DELTA: The boundary between two in-core extents moved. */
1015 temp = LEFT.br_startoff;
1016 temp2 = LEFT.br_blockcount +
1017 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 break;
1019
1020 case MASK(LEFT_FILLING):
1021 /*
1022 * Filling in the first part of a previous delayed allocation.
1023 * The left neighbor is not contiguous.
1024 */
1025 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1026 xfs_bmbt_set_startoff(ep, new_endoff);
1027 temp = PREV.br_blockcount - new->br_blockcount;
1028 xfs_bmbt_set_blockcount(ep, temp);
1029 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
1030 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001031 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 ip->i_df.if_lastex = idx;
1033 ip->i_d.di_nextents++;
1034 if (cur == NULL)
1035 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1036 else {
1037 rval = XFS_ILOG_CORE;
1038 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1039 new->br_startblock, new->br_blockcount,
1040 &i)))
1041 goto done;
1042 ASSERT(i == 0);
1043 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1044 if ((error = xfs_bmbt_insert(cur, &i)))
1045 goto done;
1046 ASSERT(i == 1);
1047 }
1048 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1049 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1050 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1051 first, flist, &cur, 1, &tmp_rval,
1052 XFS_DATA_FORK);
1053 rval |= tmp_rval;
1054 if (error)
1055 goto done;
1056 }
1057 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1058 STARTBLOCKVAL(PREV.br_startblock) -
1059 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001060 ep = xfs_iext_get_ext(ifp, idx + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1062 xfs_bmap_trace_post_update(fname, "LF", ip, idx + 1,
1063 XFS_DATA_FORK);
1064 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001065 /* DELTA: One in-core extent is split in two. */
1066 temp = PREV.br_startoff;
1067 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 break;
1069
1070 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1071 /*
1072 * Filling in the last part of a previous delayed allocation.
1073 * The right neighbor is contiguous with the new allocation.
1074 */
1075 temp = PREV.br_blockcount - new->br_blockcount;
1076 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1077 XFS_DATA_FORK);
1078 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1079 XFS_DATA_FORK);
1080 xfs_bmbt_set_blockcount(ep, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001081 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1082 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 new->br_blockcount + RIGHT.br_blockcount,
1084 RIGHT.br_state);
1085 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1086 XFS_DATA_FORK);
1087 ip->i_df.if_lastex = idx + 1;
1088 if (cur == NULL)
1089 rval = XFS_ILOG_DEXT;
1090 else {
1091 rval = 0;
1092 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1093 RIGHT.br_startblock,
1094 RIGHT.br_blockcount, &i)))
1095 goto done;
1096 ASSERT(i == 1);
1097 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1098 new->br_startblock,
1099 new->br_blockcount +
1100 RIGHT.br_blockcount,
1101 RIGHT.br_state)))
1102 goto done;
1103 }
1104 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1105 STARTBLOCKVAL(PREV.br_startblock));
1106 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1107 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1108 XFS_DATA_FORK);
1109 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001110 /* DELTA: The boundary between two in-core extents moved. */
1111 temp = PREV.br_startoff;
1112 temp2 = PREV.br_blockcount +
1113 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 break;
1115
1116 case MASK(RIGHT_FILLING):
1117 /*
1118 * Filling in the last part of a previous delayed allocation.
1119 * The right neighbor is not contiguous.
1120 */
1121 temp = PREV.br_blockcount - new->br_blockcount;
1122 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1123 xfs_bmbt_set_blockcount(ep, temp);
1124 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1125 new, NULL, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001126 xfs_iext_insert(ifp, idx + 1, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 ip->i_df.if_lastex = idx + 1;
1128 ip->i_d.di_nextents++;
1129 if (cur == NULL)
1130 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1131 else {
1132 rval = XFS_ILOG_CORE;
1133 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1134 new->br_startblock, new->br_blockcount,
1135 &i)))
1136 goto done;
1137 ASSERT(i == 0);
1138 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1139 if ((error = xfs_bmbt_insert(cur, &i)))
1140 goto done;
1141 ASSERT(i == 1);
1142 }
1143 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1144 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1145 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1146 first, flist, &cur, 1, &tmp_rval,
1147 XFS_DATA_FORK);
1148 rval |= tmp_rval;
1149 if (error)
1150 goto done;
1151 }
1152 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
1153 STARTBLOCKVAL(PREV.br_startblock) -
1154 (cur ? cur->bc_private.b.allocated : 0));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001155 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1157 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1158 *dnew = temp;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001159 /* DELTA: One in-core extent is split in two. */
1160 temp = PREV.br_startoff;
1161 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 break;
1163
1164 case 0:
1165 /*
1166 * Filling in the middle part of a previous delayed allocation.
1167 * Contiguity is impossible here.
1168 * This case is avoided almost all the time.
1169 */
1170 temp = new->br_startoff - PREV.br_startoff;
1171 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1172 xfs_bmbt_set_blockcount(ep, temp);
1173 r[0] = *new;
1174 r[1].br_startoff = new_endoff;
1175 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
1176 r[1].br_blockcount = temp2;
1177 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1178 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001179 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 ip->i_df.if_lastex = idx + 1;
1181 ip->i_d.di_nextents++;
1182 if (cur == NULL)
1183 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1184 else {
1185 rval = XFS_ILOG_CORE;
1186 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1187 new->br_startblock, new->br_blockcount,
1188 &i)))
1189 goto done;
1190 ASSERT(i == 0);
1191 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1192 if ((error = xfs_bmbt_insert(cur, &i)))
1193 goto done;
1194 ASSERT(i == 1);
1195 }
1196 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1197 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
1198 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
1199 first, flist, &cur, 1, &tmp_rval,
1200 XFS_DATA_FORK);
1201 rval |= tmp_rval;
1202 if (error)
1203 goto done;
1204 }
1205 temp = xfs_bmap_worst_indlen(ip, temp);
1206 temp2 = xfs_bmap_worst_indlen(ip, temp2);
1207 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
1208 (cur ? cur->bc_private.b.allocated : 0));
1209 if (diff > 0 &&
1210 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -diff, rsvd)) {
1211 /*
1212 * Ick gross gag me with a spoon.
1213 */
1214 ASSERT(0); /* want to see if this ever happens! */
1215 while (diff > 0) {
1216 if (temp) {
1217 temp--;
1218 diff--;
1219 if (!diff ||
1220 !xfs_mod_incore_sb(ip->i_mount,
1221 XFS_SBS_FDBLOCKS, -diff, rsvd))
1222 break;
1223 }
1224 if (temp2) {
1225 temp2--;
1226 diff--;
1227 if (!diff ||
1228 !xfs_mod_incore_sb(ip->i_mount,
1229 XFS_SBS_FDBLOCKS, -diff, rsvd))
1230 break;
1231 }
1232 }
1233 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001234 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
1236 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1237 xfs_bmap_trace_pre_update(fname, "0", ip, idx + 2,
1238 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001239 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2),
1240 NULLSTARTBLOCK((int)temp2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 xfs_bmap_trace_post_update(fname, "0", ip, idx + 2,
1242 XFS_DATA_FORK);
1243 *dnew = temp + temp2;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001244 /* DELTA: One in-core extent is split in three. */
1245 temp = PREV.br_startoff;
1246 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break;
1248
1249 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1250 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1251 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1252 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1253 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1254 case MASK(LEFT_CONTIG):
1255 case MASK(RIGHT_CONTIG):
1256 /*
1257 * These cases are all impossible.
1258 */
1259 ASSERT(0);
1260 }
1261 *curp = cur;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001262 if (delta) {
1263 temp2 += temp;
1264 if (delta->xed_startoff > temp)
1265 delta->xed_startoff = temp;
1266 if (delta->xed_blockcount < temp2)
1267 delta->xed_blockcount = temp2;
1268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269done:
1270 *logflagsp = rval;
1271 return error;
1272#undef LEFT
1273#undef RIGHT
1274#undef PREV
1275#undef MASK
1276#undef MASK2
1277#undef MASK3
1278#undef MASK4
1279#undef STATE_SET
1280#undef STATE_TEST
1281#undef STATE_SET_TEST
1282#undef SWITCH_STATE
1283}
1284
1285/*
1286 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
1287 * allocation to a real allocation or vice versa.
1288 */
1289STATIC int /* error */
1290xfs_bmap_add_extent_unwritten_real(
1291 xfs_inode_t *ip, /* incore inode pointer */
1292 xfs_extnum_t idx, /* extent number to update/insert */
1293 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001294 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001295 int *logflagsp, /* inode logging flags */
1296 xfs_extdelta_t *delta) /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 xfs_btree_cur_t *cur; /* btree cursor */
1299 xfs_bmbt_rec_t *ep; /* extent entry for idx */
1300 int error; /* error return value */
1301#ifdef XFS_BMAP_TRACE
1302 static char fname[] = "xfs_bmap_add_extent_unwritten_real";
1303#endif
1304 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001305 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 xfs_fileoff_t new_endoff; /* end offset of new entry */
1307 xfs_exntst_t newext; /* new extent state */
1308 xfs_exntst_t oldext; /* old extent state */
1309 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1310 /* left is 0, right is 1, prev is 2 */
1311 int rval=0; /* return value (logging flags) */
1312 int state = 0;/* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001313 xfs_filblks_t temp=0;
1314 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 enum { /* bit number definitions for state */
1316 LEFT_CONTIG, RIGHT_CONTIG,
1317 LEFT_FILLING, RIGHT_FILLING,
1318 LEFT_DELAY, RIGHT_DELAY,
1319 LEFT_VALID, RIGHT_VALID
1320 };
1321
1322#define LEFT r[0]
1323#define RIGHT r[1]
1324#define PREV r[2]
1325#define MASK(b) (1 << (b))
1326#define MASK2(a,b) (MASK(a) | MASK(b))
1327#define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
1328#define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
1329#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1330#define STATE_TEST(b) (state & MASK(b))
1331#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1332 ((state &= ~MASK(b)), 0))
1333#define SWITCH_STATE \
1334 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
1335
1336 /*
1337 * Set up a bunch of variables to make the tests simpler.
1338 */
1339 error = 0;
1340 cur = *curp;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001341 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1342 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 xfs_bmbt_get_all(ep, &PREV);
1344 newext = new->br_state;
1345 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1346 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1347 ASSERT(PREV.br_state == oldext);
1348 new_endoff = new->br_startoff + new->br_blockcount;
1349 ASSERT(PREV.br_startoff <= new->br_startoff);
1350 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1351 /*
1352 * Set flags determining what part of the previous oldext allocation
1353 * extent is being replaced by a newext allocation.
1354 */
1355 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
1356 STATE_SET(RIGHT_FILLING,
1357 PREV.br_startoff + PREV.br_blockcount == new_endoff);
1358 /*
1359 * Check and set flags if this segment has a left neighbor.
1360 * Don't set contiguous if the combined extent would be too large.
1361 */
1362 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001363 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
1365 }
1366 STATE_SET(LEFT_CONTIG,
1367 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1368 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1369 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1370 LEFT.br_state == newext &&
1371 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1372 /*
1373 * Check and set flags if this segment has a right neighbor.
1374 * Don't set contiguous if the combined extent would be too large.
1375 * Also check for all-three-contiguous being too large.
1376 */
1377 if (STATE_SET_TEST(RIGHT_VALID,
1378 idx <
1379 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001380 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
1382 }
1383 STATE_SET(RIGHT_CONTIG,
1384 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1385 new_endoff == RIGHT.br_startoff &&
1386 new->br_startblock + new->br_blockcount ==
1387 RIGHT.br_startblock &&
1388 newext == RIGHT.br_state &&
1389 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1390 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
1391 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
1392 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1393 <= MAXEXTLEN));
1394 /*
1395 * Switch out based on the FILLING and CONTIG state bits.
1396 */
1397 switch (SWITCH_STATE) {
1398
1399 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1400 /*
1401 * Setting all of a previous oldext extent to newext.
1402 * The left and right neighbors are both contiguous with new.
1403 */
1404 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1405 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001406 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 LEFT.br_blockcount + PREV.br_blockcount +
1408 RIGHT.br_blockcount);
1409 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
1410 XFS_DATA_FORK);
1411 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
1412 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001413 xfs_iext_remove(ifp, idx, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 ip->i_df.if_lastex = idx - 1;
1415 ip->i_d.di_nextents -= 2;
1416 if (cur == NULL)
1417 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1418 else {
1419 rval = XFS_ILOG_CORE;
1420 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1421 RIGHT.br_startblock,
1422 RIGHT.br_blockcount, &i)))
1423 goto done;
1424 ASSERT(i == 1);
1425 if ((error = xfs_bmbt_delete(cur, &i)))
1426 goto done;
1427 ASSERT(i == 1);
1428 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1429 goto done;
1430 ASSERT(i == 1);
1431 if ((error = xfs_bmbt_delete(cur, &i)))
1432 goto done;
1433 ASSERT(i == 1);
1434 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1435 goto done;
1436 ASSERT(i == 1);
1437 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1438 LEFT.br_startblock,
1439 LEFT.br_blockcount + PREV.br_blockcount +
1440 RIGHT.br_blockcount, LEFT.br_state)))
1441 goto done;
1442 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001443 /* DELTA: Three in-core extents are replaced by one. */
1444 temp = LEFT.br_startoff;
1445 temp2 = LEFT.br_blockcount +
1446 PREV.br_blockcount +
1447 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 break;
1449
1450 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
1451 /*
1452 * Setting all of a previous oldext extent to newext.
1453 * The left neighbor is contiguous, the right is not.
1454 */
1455 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
1456 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001457 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 LEFT.br_blockcount + PREV.br_blockcount);
1459 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
1460 XFS_DATA_FORK);
1461 ip->i_df.if_lastex = idx - 1;
1462 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
1463 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001464 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 ip->i_d.di_nextents--;
1466 if (cur == NULL)
1467 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1468 else {
1469 rval = XFS_ILOG_CORE;
1470 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1471 PREV.br_startblock, PREV.br_blockcount,
1472 &i)))
1473 goto done;
1474 ASSERT(i == 1);
1475 if ((error = xfs_bmbt_delete(cur, &i)))
1476 goto done;
1477 ASSERT(i == 1);
1478 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1479 goto done;
1480 ASSERT(i == 1);
1481 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1482 LEFT.br_startblock,
1483 LEFT.br_blockcount + PREV.br_blockcount,
1484 LEFT.br_state)))
1485 goto done;
1486 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001487 /* DELTA: Two in-core extents are replaced by one. */
1488 temp = LEFT.br_startoff;
1489 temp2 = LEFT.br_blockcount +
1490 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 break;
1492
1493 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
1494 /*
1495 * Setting all of a previous oldext extent to newext.
1496 * The right neighbor is contiguous, the left is not.
1497 */
1498 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
1499 XFS_DATA_FORK);
1500 xfs_bmbt_set_blockcount(ep,
1501 PREV.br_blockcount + RIGHT.br_blockcount);
1502 xfs_bmbt_set_state(ep, newext);
1503 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
1504 XFS_DATA_FORK);
1505 ip->i_df.if_lastex = idx;
1506 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
1507 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001508 xfs_iext_remove(ifp, idx + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 ip->i_d.di_nextents--;
1510 if (cur == NULL)
1511 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1512 else {
1513 rval = XFS_ILOG_CORE;
1514 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1515 RIGHT.br_startblock,
1516 RIGHT.br_blockcount, &i)))
1517 goto done;
1518 ASSERT(i == 1);
1519 if ((error = xfs_bmbt_delete(cur, &i)))
1520 goto done;
1521 ASSERT(i == 1);
1522 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1523 goto done;
1524 ASSERT(i == 1);
1525 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1526 new->br_startblock,
1527 new->br_blockcount + RIGHT.br_blockcount,
1528 newext)))
1529 goto done;
1530 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001531 /* DELTA: Two in-core extents are replaced by one. */
1532 temp = PREV.br_startoff;
1533 temp2 = PREV.br_blockcount +
1534 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 break;
1536
1537 case MASK2(LEFT_FILLING, RIGHT_FILLING):
1538 /*
1539 * Setting all of a previous oldext extent to newext.
1540 * Neither the left nor right neighbors are contiguous with
1541 * the new one.
1542 */
1543 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
1544 XFS_DATA_FORK);
1545 xfs_bmbt_set_state(ep, newext);
1546 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
1547 XFS_DATA_FORK);
1548 ip->i_df.if_lastex = idx;
1549 if (cur == NULL)
1550 rval = XFS_ILOG_DEXT;
1551 else {
1552 rval = 0;
1553 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1554 new->br_startblock, new->br_blockcount,
1555 &i)))
1556 goto done;
1557 ASSERT(i == 1);
1558 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1559 new->br_startblock, new->br_blockcount,
1560 newext)))
1561 goto done;
1562 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001563 /* DELTA: The in-core extent described by new changed type. */
1564 temp = new->br_startoff;
1565 temp2 = new->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 break;
1567
1568 case MASK2(LEFT_FILLING, LEFT_CONTIG):
1569 /*
1570 * Setting the first part of a previous oldext extent to newext.
1571 * The left neighbor is contiguous.
1572 */
1573 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
1574 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001575 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 LEFT.br_blockcount + new->br_blockcount);
1577 xfs_bmbt_set_startoff(ep,
1578 PREV.br_startoff + new->br_blockcount);
1579 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
1580 XFS_DATA_FORK);
1581 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
1582 XFS_DATA_FORK);
1583 xfs_bmbt_set_startblock(ep,
1584 new->br_startblock + new->br_blockcount);
1585 xfs_bmbt_set_blockcount(ep,
1586 PREV.br_blockcount - new->br_blockcount);
1587 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
1588 XFS_DATA_FORK);
1589 ip->i_df.if_lastex = idx - 1;
1590 if (cur == NULL)
1591 rval = XFS_ILOG_DEXT;
1592 else {
1593 rval = 0;
1594 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1595 PREV.br_startblock, PREV.br_blockcount,
1596 &i)))
1597 goto done;
1598 ASSERT(i == 1);
1599 if ((error = xfs_bmbt_update(cur,
1600 PREV.br_startoff + new->br_blockcount,
1601 PREV.br_startblock + new->br_blockcount,
1602 PREV.br_blockcount - new->br_blockcount,
1603 oldext)))
1604 goto done;
1605 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
1606 goto done;
1607 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1608 LEFT.br_startblock,
1609 LEFT.br_blockcount + new->br_blockcount,
1610 LEFT.br_state))
1611 goto done;
1612 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001613 /* DELTA: The boundary between two in-core extents moved. */
1614 temp = LEFT.br_startoff;
1615 temp2 = LEFT.br_blockcount +
1616 PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 break;
1618
1619 case MASK(LEFT_FILLING):
1620 /*
1621 * Setting the first part of a previous oldext extent to newext.
1622 * The left neighbor is not contiguous.
1623 */
1624 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1625 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1626 xfs_bmbt_set_startoff(ep, new_endoff);
1627 xfs_bmbt_set_blockcount(ep,
1628 PREV.br_blockcount - new->br_blockcount);
1629 xfs_bmbt_set_startblock(ep,
1630 new->br_startblock + new->br_blockcount);
1631 xfs_bmap_trace_post_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1632 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
1633 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001634 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 ip->i_df.if_lastex = idx;
1636 ip->i_d.di_nextents++;
1637 if (cur == NULL)
1638 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1639 else {
1640 rval = XFS_ILOG_CORE;
1641 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1642 PREV.br_startblock, PREV.br_blockcount,
1643 &i)))
1644 goto done;
1645 ASSERT(i == 1);
1646 if ((error = xfs_bmbt_update(cur,
1647 PREV.br_startoff + new->br_blockcount,
1648 PREV.br_startblock + new->br_blockcount,
1649 PREV.br_blockcount - new->br_blockcount,
1650 oldext)))
1651 goto done;
1652 cur->bc_rec.b = *new;
1653 if ((error = xfs_bmbt_insert(cur, &i)))
1654 goto done;
1655 ASSERT(i == 1);
1656 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001657 /* DELTA: One in-core extent is split in two. */
1658 temp = PREV.br_startoff;
1659 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 break;
1661
1662 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1663 /*
1664 * Setting the last part of a previous oldext extent to newext.
1665 * The right neighbor is contiguous with the new allocation.
1666 */
1667 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1668 XFS_DATA_FORK);
1669 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1670 XFS_DATA_FORK);
1671 xfs_bmbt_set_blockcount(ep,
1672 PREV.br_blockcount - new->br_blockcount);
1673 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1674 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001675 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1),
1676 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 new->br_blockcount + RIGHT.br_blockcount, newext);
1678 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1679 XFS_DATA_FORK);
1680 ip->i_df.if_lastex = idx + 1;
1681 if (cur == NULL)
1682 rval = XFS_ILOG_DEXT;
1683 else {
1684 rval = 0;
1685 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1686 PREV.br_startblock,
1687 PREV.br_blockcount, &i)))
1688 goto done;
1689 ASSERT(i == 1);
1690 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1691 PREV.br_startblock,
1692 PREV.br_blockcount - new->br_blockcount,
1693 oldext)))
1694 goto done;
1695 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1696 goto done;
1697 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1698 new->br_startblock,
1699 new->br_blockcount + RIGHT.br_blockcount,
1700 newext)))
1701 goto done;
1702 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001703 /* DELTA: The boundary between two in-core extents moved. */
1704 temp = PREV.br_startoff;
1705 temp2 = PREV.br_blockcount +
1706 RIGHT.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 break;
1708
1709 case MASK(RIGHT_FILLING):
1710 /*
1711 * Setting the last part of a previous oldext extent to newext.
1712 * The right neighbor is not contiguous.
1713 */
1714 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1715 xfs_bmbt_set_blockcount(ep,
1716 PREV.br_blockcount - new->br_blockcount);
1717 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1718 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1719 new, NULL, XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001720 xfs_iext_insert(ifp, idx + 1, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 ip->i_df.if_lastex = idx + 1;
1722 ip->i_d.di_nextents++;
1723 if (cur == NULL)
1724 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1725 else {
1726 rval = XFS_ILOG_CORE;
1727 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1728 PREV.br_startblock, PREV.br_blockcount,
1729 &i)))
1730 goto done;
1731 ASSERT(i == 1);
1732 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1733 PREV.br_startblock,
1734 PREV.br_blockcount - new->br_blockcount,
1735 oldext)))
1736 goto done;
1737 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1738 new->br_startblock, new->br_blockcount,
1739 &i)))
1740 goto done;
1741 ASSERT(i == 0);
1742 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1743 if ((error = xfs_bmbt_insert(cur, &i)))
1744 goto done;
1745 ASSERT(i == 1);
1746 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001747 /* DELTA: One in-core extent is split in two. */
1748 temp = PREV.br_startoff;
1749 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 break;
1751
1752 case 0:
1753 /*
1754 * Setting the middle part of a previous oldext extent to
1755 * newext. Contiguity is impossible here.
1756 * One extent becomes three extents.
1757 */
1758 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1759 xfs_bmbt_set_blockcount(ep,
1760 new->br_startoff - PREV.br_startoff);
1761 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1762 r[0] = *new;
1763 r[1].br_startoff = new_endoff;
1764 r[1].br_blockcount =
1765 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1766 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1767 r[1].br_state = oldext;
1768 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1769 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001770 xfs_iext_insert(ifp, idx + 1, 2, &r[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 ip->i_df.if_lastex = idx + 1;
1772 ip->i_d.di_nextents += 2;
1773 if (cur == NULL)
1774 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1775 else {
1776 rval = XFS_ILOG_CORE;
1777 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1778 PREV.br_startblock, PREV.br_blockcount,
1779 &i)))
1780 goto done;
1781 ASSERT(i == 1);
1782 /* new right extent - oldext */
1783 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1784 r[1].br_startblock, r[1].br_blockcount,
1785 r[1].br_state)))
1786 goto done;
1787 /* new left extent - oldext */
1788 PREV.br_blockcount =
1789 new->br_startoff - PREV.br_startoff;
1790 cur->bc_rec.b = PREV;
1791 if ((error = xfs_bmbt_insert(cur, &i)))
1792 goto done;
1793 ASSERT(i == 1);
1794 if ((error = xfs_bmbt_increment(cur, 0, &i)))
1795 goto done;
1796 ASSERT(i == 1);
1797 /* new middle extent - newext */
1798 cur->bc_rec.b = *new;
1799 if ((error = xfs_bmbt_insert(cur, &i)))
1800 goto done;
1801 ASSERT(i == 1);
1802 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001803 /* DELTA: One in-core extent is split in three. */
1804 temp = PREV.br_startoff;
1805 temp2 = PREV.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 break;
1807
1808 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1809 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1810 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1811 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1812 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1813 case MASK(LEFT_CONTIG):
1814 case MASK(RIGHT_CONTIG):
1815 /*
1816 * These cases are all impossible.
1817 */
1818 ASSERT(0);
1819 }
1820 *curp = cur;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001821 if (delta) {
1822 temp2 += temp;
1823 if (delta->xed_startoff > temp)
1824 delta->xed_startoff = temp;
1825 if (delta->xed_blockcount < temp2)
1826 delta->xed_blockcount = temp2;
1827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828done:
1829 *logflagsp = rval;
1830 return error;
1831#undef LEFT
1832#undef RIGHT
1833#undef PREV
1834#undef MASK
1835#undef MASK2
1836#undef MASK3
1837#undef MASK4
1838#undef STATE_SET
1839#undef STATE_TEST
1840#undef STATE_SET_TEST
1841#undef SWITCH_STATE
1842}
1843
1844/*
1845 * Called by xfs_bmap_add_extent to handle cases converting a hole
1846 * to a delayed allocation.
1847 */
1848/*ARGSUSED*/
1849STATIC int /* error */
1850xfs_bmap_add_extent_hole_delay(
1851 xfs_inode_t *ip, /* incore inode pointer */
1852 xfs_extnum_t idx, /* extent number to update/insert */
1853 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001854 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001856 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 int rsvd) /* OK to allocate reserved blocks */
1858{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001859 xfs_bmbt_rec_t *ep; /* extent record for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860#ifdef XFS_BMAP_TRACE
1861 static char fname[] = "xfs_bmap_add_extent_hole_delay";
1862#endif
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001863 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1865 xfs_filblks_t newlen=0; /* new indirect size */
1866 xfs_filblks_t oldlen=0; /* old indirect size */
1867 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1868 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001869 xfs_filblks_t temp=0; /* temp for indirect calculations */
1870 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 enum { /* bit number definitions for state */
1872 LEFT_CONTIG, RIGHT_CONTIG,
1873 LEFT_DELAY, RIGHT_DELAY,
1874 LEFT_VALID, RIGHT_VALID
1875 };
1876
1877#define MASK(b) (1 << (b))
1878#define MASK2(a,b) (MASK(a) | MASK(b))
1879#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1880#define STATE_TEST(b) (state & MASK(b))
1881#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1882 ((state &= ~MASK(b)), 0))
1883#define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1884
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001885 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1886 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 state = 0;
1888 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1889 /*
1890 * Check and set flags if this segment has a left neighbor
1891 */
1892 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001893 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1895 }
1896 /*
1897 * Check and set flags if the current (right) segment exists.
1898 * If it doesn't exist, we're converting the hole at end-of-file.
1899 */
1900 if (STATE_SET_TEST(RIGHT_VALID,
1901 idx <
1902 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1903 xfs_bmbt_get_all(ep, &right);
1904 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1905 }
1906 /*
1907 * Set contiguity flags on the left and right neighbors.
1908 * Don't let extents get too large, even if the pieces are contiguous.
1909 */
1910 STATE_SET(LEFT_CONTIG,
1911 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1912 left.br_startoff + left.br_blockcount == new->br_startoff &&
1913 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1914 STATE_SET(RIGHT_CONTIG,
1915 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1916 new->br_startoff + new->br_blockcount == right.br_startoff &&
1917 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1918 (!STATE_TEST(LEFT_CONTIG) ||
1919 (left.br_blockcount + new->br_blockcount +
1920 right.br_blockcount <= MAXEXTLEN)));
1921 /*
1922 * Switch out based on the contiguity flags.
1923 */
1924 switch (SWITCH_STATE) {
1925
1926 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1927 /*
1928 * New allocation is contiguous with delayed allocations
1929 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001930 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 */
1932 temp = left.br_blockcount + new->br_blockcount +
1933 right.br_blockcount;
1934 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
1935 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001936 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 oldlen = STARTBLOCKVAL(left.br_startblock) +
1938 STARTBLOCKVAL(new->br_startblock) +
1939 STARTBLOCKVAL(right.br_startblock);
1940 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001941 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1942 NULLSTARTBLOCK((int)newlen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
1944 XFS_DATA_FORK);
1945 xfs_bmap_trace_delete(fname, "LC|RC", ip, idx, 1,
1946 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001947 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 ip->i_df.if_lastex = idx - 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001949 /* DELTA: Two in-core extents were replaced by one. */
1950 temp2 = temp;
1951 temp = left.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 break;
1953
1954 case MASK(LEFT_CONTIG):
1955 /*
1956 * New allocation is contiguous with a delayed allocation
1957 * on the left.
1958 * Merge the new allocation with the left neighbor.
1959 */
1960 temp = left.br_blockcount + new->br_blockcount;
1961 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1,
1962 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001963 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 oldlen = STARTBLOCKVAL(left.br_startblock) +
1965 STARTBLOCKVAL(new->br_startblock);
1966 newlen = xfs_bmap_worst_indlen(ip, temp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001967 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1),
1968 NULLSTARTBLOCK((int)newlen));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1,
1970 XFS_DATA_FORK);
1971 ip->i_df.if_lastex = idx - 1;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001972 /* DELTA: One in-core extent grew into a hole. */
1973 temp2 = temp;
1974 temp = left.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 break;
1976
1977 case MASK(RIGHT_CONTIG):
1978 /*
1979 * New allocation is contiguous with a delayed allocation
1980 * on the right.
1981 * Merge the new allocation with the right neighbor.
1982 */
1983 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1984 temp = new->br_blockcount + right.br_blockcount;
1985 oldlen = STARTBLOCKVAL(new->br_startblock) +
1986 STARTBLOCKVAL(right.br_startblock);
1987 newlen = xfs_bmap_worst_indlen(ip, temp);
1988 xfs_bmbt_set_allf(ep, new->br_startoff,
1989 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
1990 xfs_bmap_trace_post_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1991 ip->i_df.if_lastex = idx;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001992 /* DELTA: One in-core extent grew into a hole. */
1993 temp2 = temp;
1994 temp = new->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 break;
1996
1997 case 0:
1998 /*
1999 * New allocation is not contiguous with another
2000 * delayed allocation.
2001 * Insert a new entry.
2002 */
2003 oldlen = newlen = 0;
2004 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
2005 XFS_DATA_FORK);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002006 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 ip->i_df.if_lastex = idx;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002008 /* DELTA: A new in-core extent was added in a hole. */
2009 temp2 = new->br_blockcount;
2010 temp = new->br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 break;
2012 }
2013 if (oldlen != newlen) {
2014 ASSERT(oldlen > newlen);
2015 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
2016 (int)(oldlen - newlen), rsvd);
2017 /*
2018 * Nothing to do for disk quota accounting here.
2019 */
2020 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002021 if (delta) {
2022 temp2 += temp;
2023 if (delta->xed_startoff > temp)
2024 delta->xed_startoff = temp;
2025 if (delta->xed_blockcount < temp2)
2026 delta->xed_blockcount = temp2;
2027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 *logflagsp = 0;
2029 return 0;
2030#undef MASK
2031#undef MASK2
2032#undef STATE_SET
2033#undef STATE_TEST
2034#undef STATE_SET_TEST
2035#undef SWITCH_STATE
2036}
2037
2038/*
2039 * Called by xfs_bmap_add_extent to handle cases converting a hole
2040 * to a real allocation.
2041 */
2042STATIC int /* error */
2043xfs_bmap_add_extent_hole_real(
2044 xfs_inode_t *ip, /* incore inode pointer */
2045 xfs_extnum_t idx, /* extent number to update/insert */
2046 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002047 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002049 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 int whichfork) /* data or attr fork */
2051{
2052 xfs_bmbt_rec_t *ep; /* pointer to extent entry ins. point */
2053 int error; /* error return value */
2054#ifdef XFS_BMAP_TRACE
2055 static char fname[] = "xfs_bmap_add_extent_hole_real";
2056#endif
2057 int i; /* temp state */
2058 xfs_ifork_t *ifp; /* inode fork pointer */
2059 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2060 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002061 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002063 xfs_filblks_t temp=0;
2064 xfs_filblks_t temp2=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 enum { /* bit number definitions for state */
2066 LEFT_CONTIG, RIGHT_CONTIG,
2067 LEFT_DELAY, RIGHT_DELAY,
2068 LEFT_VALID, RIGHT_VALID
2069 };
2070
2071#define MASK(b) (1 << (b))
2072#define MASK2(a,b) (MASK(a) | MASK(b))
2073#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
2074#define STATE_TEST(b) (state & MASK(b))
2075#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
2076 ((state &= ~MASK(b)), 0))
2077#define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
2078
2079 ifp = XFS_IFORK_PTR(ip, whichfork);
2080 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002081 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 state = 0;
2083 /*
2084 * Check and set flags if this segment has a left neighbor.
2085 */
2086 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002087 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
2089 }
2090 /*
2091 * Check and set flags if this segment has a current value.
2092 * Not true if we're inserting into the "hole" at eof.
2093 */
2094 if (STATE_SET_TEST(RIGHT_VALID,
2095 idx <
2096 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
2097 xfs_bmbt_get_all(ep, &right);
2098 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
2099 }
2100 /*
2101 * We're inserting a real allocation between "left" and "right".
2102 * Set the contiguity flags. Don't let extents get too large.
2103 */
2104 STATE_SET(LEFT_CONTIG,
2105 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
2106 left.br_startoff + left.br_blockcount == new->br_startoff &&
2107 left.br_startblock + left.br_blockcount == new->br_startblock &&
2108 left.br_state == new->br_state &&
2109 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
2110 STATE_SET(RIGHT_CONTIG,
2111 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
2112 new->br_startoff + new->br_blockcount == right.br_startoff &&
2113 new->br_startblock + new->br_blockcount ==
2114 right.br_startblock &&
2115 new->br_state == right.br_state &&
2116 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2117 (!STATE_TEST(LEFT_CONTIG) ||
2118 left.br_blockcount + new->br_blockcount +
2119 right.br_blockcount <= MAXEXTLEN));
2120
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002121 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 /*
2123 * Select which case we're in here, and implement it.
2124 */
2125 switch (SWITCH_STATE) {
2126
2127 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
2128 /*
2129 * New allocation is contiguous with real allocations on the
2130 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002131 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 */
2133 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
2134 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002135 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 left.br_blockcount + new->br_blockcount +
2137 right.br_blockcount);
2138 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
2139 whichfork);
2140 xfs_bmap_trace_delete(fname, "LC|RC", ip,
2141 idx, 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002142 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 ifp->if_lastex = idx - 1;
2144 XFS_IFORK_NEXT_SET(ip, whichfork,
2145 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2146 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002147 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2148 } else {
2149 rval = XFS_ILOG_CORE;
2150 if ((error = xfs_bmbt_lookup_eq(cur,
2151 right.br_startoff,
2152 right.br_startblock,
2153 right.br_blockcount, &i)))
2154 goto done;
2155 ASSERT(i == 1);
2156 if ((error = xfs_bmbt_delete(cur, &i)))
2157 goto done;
2158 ASSERT(i == 1);
2159 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2160 goto done;
2161 ASSERT(i == 1);
2162 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2163 left.br_startblock,
2164 left.br_blockcount +
2165 new->br_blockcount +
2166 right.br_blockcount,
2167 left.br_state)))
2168 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002170 /* DELTA: Two in-core extents were replaced by one. */
2171 temp = left.br_startoff;
2172 temp2 = left.br_blockcount +
2173 new->br_blockcount +
2174 right.br_blockcount;
2175 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 case MASK(LEFT_CONTIG):
2178 /*
2179 * New allocation is contiguous with a real allocation
2180 * on the left.
2181 * Merge the new allocation with the left neighbor.
2182 */
2183 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002184 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 left.br_blockcount + new->br_blockcount);
2186 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1, whichfork);
2187 ifp->if_lastex = idx - 1;
2188 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002189 rval = XFS_ILOG_FEXT(whichfork);
2190 } else {
2191 rval = 0;
2192 if ((error = xfs_bmbt_lookup_eq(cur,
2193 left.br_startoff,
2194 left.br_startblock,
2195 left.br_blockcount, &i)))
2196 goto done;
2197 ASSERT(i == 1);
2198 if ((error = xfs_bmbt_update(cur, left.br_startoff,
2199 left.br_startblock,
2200 left.br_blockcount +
2201 new->br_blockcount,
2202 left.br_state)))
2203 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002205 /* DELTA: One in-core extent grew. */
2206 temp = left.br_startoff;
2207 temp2 = left.br_blockcount +
2208 new->br_blockcount;
2209 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
2211 case MASK(RIGHT_CONTIG):
2212 /*
2213 * New allocation is contiguous with a real allocation
2214 * on the right.
2215 * Merge the new allocation with the right neighbor.
2216 */
2217 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, whichfork);
2218 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
2219 new->br_blockcount + right.br_blockcount,
2220 right.br_state);
2221 xfs_bmap_trace_post_update(fname, "RC", ip, idx, whichfork);
2222 ifp->if_lastex = idx;
2223 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002224 rval = XFS_ILOG_FEXT(whichfork);
2225 } else {
2226 rval = 0;
2227 if ((error = xfs_bmbt_lookup_eq(cur,
2228 right.br_startoff,
2229 right.br_startblock,
2230 right.br_blockcount, &i)))
2231 goto done;
2232 ASSERT(i == 1);
2233 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2234 new->br_startblock,
2235 new->br_blockcount +
2236 right.br_blockcount,
2237 right.br_state)))
2238 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002240 /* DELTA: One in-core extent grew. */
2241 temp = new->br_startoff;
2242 temp2 = new->br_blockcount +
2243 right.br_blockcount;
2244 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246 case 0:
2247 /*
2248 * New allocation is not contiguous with another
2249 * real allocation.
2250 * Insert a new entry.
2251 */
2252 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
2253 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002254 xfs_iext_insert(ifp, idx, 1, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 ifp->if_lastex = idx;
2256 XFS_IFORK_NEXT_SET(ip, whichfork,
2257 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2258 if (cur == NULL) {
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002259 rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2260 } else {
2261 rval = XFS_ILOG_CORE;
2262 if ((error = xfs_bmbt_lookup_eq(cur,
2263 new->br_startoff,
2264 new->br_startblock,
2265 new->br_blockcount, &i)))
2266 goto done;
2267 ASSERT(i == 0);
2268 cur->bc_rec.b.br_state = new->br_state;
2269 if ((error = xfs_bmbt_insert(cur, &i)))
2270 goto done;
2271 ASSERT(i == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002273 /* DELTA: A new extent was added in a hole. */
2274 temp = new->br_startoff;
2275 temp2 = new->br_blockcount;
2276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002278 if (delta) {
2279 temp2 += temp;
2280 if (delta->xed_startoff > temp)
2281 delta->xed_startoff = temp;
2282 if (delta->xed_blockcount < temp2)
2283 delta->xed_blockcount = temp2;
2284 }
2285done:
2286 *logflagsp = rval;
2287 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288#undef MASK
2289#undef MASK2
2290#undef STATE_SET
2291#undef STATE_TEST
2292#undef STATE_SET_TEST
2293#undef SWITCH_STATE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
Nathan Scottdd9f4382006-01-11 15:28:28 +11002296/*
2297 * Adjust the size of the new extent based on di_extsize and rt extsize.
2298 */
2299STATIC int
2300xfs_bmap_extsize_align(
2301 xfs_mount_t *mp,
2302 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2303 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2304 xfs_extlen_t extsz, /* align to this extent size */
2305 int rt, /* is this a realtime inode? */
2306 int eof, /* is extent at end-of-file? */
2307 int delay, /* creating delalloc extent? */
2308 int convert, /* overwriting unwritten extent? */
2309 xfs_fileoff_t *offp, /* in/out: aligned offset */
2310 xfs_extlen_t *lenp) /* in/out: aligned length */
2311{
2312 xfs_fileoff_t orig_off; /* original offset */
2313 xfs_extlen_t orig_alen; /* original length */
2314 xfs_fileoff_t orig_end; /* original off+len */
2315 xfs_fileoff_t nexto; /* next file offset */
2316 xfs_fileoff_t prevo; /* previous file offset */
2317 xfs_fileoff_t align_off; /* temp for offset */
2318 xfs_extlen_t align_alen; /* temp for length */
2319 xfs_extlen_t temp; /* temp for calculations */
2320
2321 if (convert)
2322 return 0;
2323
2324 orig_off = align_off = *offp;
2325 orig_alen = align_alen = *lenp;
2326 orig_end = orig_off + orig_alen;
2327
2328 /*
2329 * If this request overlaps an existing extent, then don't
2330 * attempt to perform any additional alignment.
2331 */
2332 if (!delay && !eof &&
2333 (orig_off >= gotp->br_startoff) &&
2334 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2335 return 0;
2336 }
2337
2338 /*
2339 * If the file offset is unaligned vs. the extent size
2340 * we need to align it. This will be possible unless
2341 * the file was previously written with a kernel that didn't
2342 * perform this alignment, or if a truncate shot us in the
2343 * foot.
2344 */
2345 temp = do_mod(orig_off, extsz);
2346 if (temp) {
2347 align_alen += temp;
2348 align_off -= temp;
2349 }
2350 /*
2351 * Same adjustment for the end of the requested area.
2352 */
2353 if ((temp = (align_alen % extsz))) {
2354 align_alen += extsz - temp;
2355 }
2356 /*
2357 * If the previous block overlaps with this proposed allocation
2358 * then move the start forward without adjusting the length.
2359 */
2360 if (prevp->br_startoff != NULLFILEOFF) {
2361 if (prevp->br_startblock == HOLESTARTBLOCK)
2362 prevo = prevp->br_startoff;
2363 else
2364 prevo = prevp->br_startoff + prevp->br_blockcount;
2365 } else
2366 prevo = 0;
2367 if (align_off != orig_off && align_off < prevo)
2368 align_off = prevo;
2369 /*
2370 * If the next block overlaps with this proposed allocation
2371 * then move the start back without adjusting the length,
2372 * but not before offset 0.
2373 * This may of course make the start overlap previous block,
2374 * and if we hit the offset 0 limit then the next block
2375 * can still overlap too.
2376 */
2377 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2378 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2379 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2380 nexto = gotp->br_startoff + gotp->br_blockcount;
2381 else
2382 nexto = gotp->br_startoff;
2383 } else
2384 nexto = NULLFILEOFF;
2385 if (!eof &&
2386 align_off + align_alen != orig_end &&
2387 align_off + align_alen > nexto)
2388 align_off = nexto > align_alen ? nexto - align_alen : 0;
2389 /*
2390 * If we're now overlapping the next or previous extent that
2391 * means we can't fit an extsz piece in this hole. Just move
2392 * the start forward to the first valid spot and set
2393 * the length so we hit the end.
2394 */
2395 if (align_off != orig_off && align_off < prevo)
2396 align_off = prevo;
2397 if (align_off + align_alen != orig_end &&
2398 align_off + align_alen > nexto &&
2399 nexto != NULLFILEOFF) {
2400 ASSERT(nexto > prevo);
2401 align_alen = nexto - align_off;
2402 }
2403
2404 /*
2405 * If realtime, and the result isn't a multiple of the realtime
2406 * extent size we need to remove blocks until it is.
2407 */
2408 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2409 /*
2410 * We're not covering the original request, or
2411 * we won't be able to once we fix the length.
2412 */
2413 if (orig_off < align_off ||
2414 orig_end > align_off + align_alen ||
2415 align_alen - temp < orig_alen)
2416 return XFS_ERROR(EINVAL);
2417 /*
2418 * Try to fix it by moving the start up.
2419 */
2420 if (align_off + temp <= orig_off) {
2421 align_alen -= temp;
2422 align_off += temp;
2423 }
2424 /*
2425 * Try to fix it by moving the end in.
2426 */
2427 else if (align_off + align_alen - temp >= orig_end)
2428 align_alen -= temp;
2429 /*
2430 * Set the start to the minimum then trim the length.
2431 */
2432 else {
2433 align_alen -= orig_off - align_off;
2434 align_off = orig_off;
2435 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2436 }
2437 /*
2438 * Result doesn't cover the request, fail it.
2439 */
2440 if (orig_off < align_off || orig_end > align_off + align_alen)
2441 return XFS_ERROR(EINVAL);
2442 } else {
2443 ASSERT(orig_off >= align_off);
2444 ASSERT(orig_end <= align_off + align_alen);
2445 }
2446
2447#ifdef DEBUG
2448 if (!eof && gotp->br_startoff != NULLFILEOFF)
2449 ASSERT(align_off + align_alen <= gotp->br_startoff);
2450 if (prevp->br_startoff != NULLFILEOFF)
2451 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2452#endif
2453
2454 *lenp = align_alen;
2455 *offp = align_off;
2456 return 0;
2457}
2458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459#define XFS_ALLOC_GAP_UNITS 4
2460
Nathan Scottdd9f4382006-01-11 15:28:28 +11002461STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11002462xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2464{
2465 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2467 xfs_mount_t *mp; /* mount point structure */
2468 int nullfb; /* true if ap->firstblock isn't set */
2469 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471#define ISVALID(x,y) \
2472 (rt ? \
2473 (x) < mp->m_sb.sb_rblocks : \
2474 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2475 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2476 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 mp = ap->ip->i_mount;
2479 nullfb = ap->firstblock == NULLFSBLOCK;
2480 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2481 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 /*
2483 * If allocating at eof, and there's a previous real block,
2484 * try to use it's last block as our starting point.
2485 */
2486 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
2487 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2488 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2489 ap->prevp->br_startblock)) {
2490 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
2491 /*
2492 * Adjust for the gap between prevp and us.
2493 */
2494 adjust = ap->off -
2495 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
2496 if (adjust &&
2497 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2498 ap->rval += adjust;
2499 }
2500 /*
2501 * If not at eof, then compare the two neighbor blocks.
2502 * Figure out whether either one gives us a good starting point,
2503 * and pick the better one.
2504 */
2505 else if (!ap->eof) {
2506 xfs_fsblock_t gotbno; /* right side block number */
2507 xfs_fsblock_t gotdiff=0; /* right side difference */
2508 xfs_fsblock_t prevbno; /* left side block number */
2509 xfs_fsblock_t prevdiff=0; /* left side difference */
2510
2511 /*
2512 * If there's a previous (left) block, select a requested
2513 * start block based on it.
2514 */
2515 if (ap->prevp->br_startoff != NULLFILEOFF &&
2516 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
2517 (prevbno = ap->prevp->br_startblock +
2518 ap->prevp->br_blockcount) &&
2519 ISVALID(prevbno, ap->prevp->br_startblock)) {
2520 /*
2521 * Calculate gap to end of previous block.
2522 */
2523 adjust = prevdiff = ap->off -
2524 (ap->prevp->br_startoff +
2525 ap->prevp->br_blockcount);
2526 /*
2527 * Figure the startblock based on the previous block's
2528 * end and the gap size.
2529 * Heuristic!
2530 * If the gap is large relative to the piece we're
2531 * allocating, or using it gives us an invalid block
2532 * number, then just use the end of the previous block.
2533 */
2534 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2535 ISVALID(prevbno + prevdiff,
2536 ap->prevp->br_startblock))
2537 prevbno += adjust;
2538 else
2539 prevdiff += 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, prevbno) != fb_agno)
2546 prevbno = NULLFSBLOCK;
2547 }
2548 /*
2549 * No previous block or can't follow it, just default.
2550 */
2551 else
2552 prevbno = NULLFSBLOCK;
2553 /*
2554 * If there's a following (right) block, select a requested
2555 * start block based on it.
2556 */
2557 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
2558 /*
2559 * Calculate gap to start of next block.
2560 */
2561 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
2562 /*
2563 * Figure the startblock based on the next block's
2564 * start and the gap size.
2565 */
2566 gotbno = ap->gotp->br_startblock;
2567 /*
2568 * Heuristic!
2569 * If the gap is large relative to the piece we're
2570 * allocating, or using it gives us an invalid block
2571 * number, then just use the start of the next block
2572 * offset by our length.
2573 */
2574 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
2575 ISVALID(gotbno - gotdiff, gotbno))
2576 gotbno -= adjust;
2577 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2578 gotbno -= ap->alen;
2579 gotdiff += adjust - ap->alen;
2580 } else
2581 gotdiff += adjust;
2582 /*
2583 * If the firstblock forbids it, can't use it,
2584 * must use default.
2585 */
2586 if (!rt && !nullfb &&
2587 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2588 gotbno = NULLFSBLOCK;
2589 }
2590 /*
2591 * No next block, just default.
2592 */
2593 else
2594 gotbno = NULLFSBLOCK;
2595 /*
2596 * If both valid, pick the better one, else the only good
2597 * one, else ap->rval is already set (to 0 or the inode block).
2598 */
2599 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2600 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
2601 else if (prevbno != NULLFSBLOCK)
2602 ap->rval = prevbno;
2603 else if (gotbno != NULLFSBLOCK)
2604 ap->rval = gotbno;
2605 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002606#undef ISVALID
2607 return 0;
2608}
2609
2610STATIC int
2611xfs_bmap_rtalloc(
2612 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2613{
2614 xfs_alloctype_t atype = 0; /* type for allocation routines */
2615 int error; /* error return value */
2616 xfs_mount_t *mp; /* mount point structure */
2617 xfs_extlen_t prod = 0; /* product factor for allocators */
2618 xfs_extlen_t ralen = 0; /* realtime allocation length */
2619 xfs_extlen_t align; /* minimum allocation alignment */
2620 xfs_rtblock_t rtx; /* realtime extent number */
2621 xfs_rtblock_t rtb;
2622
2623 mp = ap->ip->i_mount;
2624 align = ap->ip->i_d.di_extsize ?
2625 ap->ip->i_d.di_extsize : mp->m_sb.sb_rextsize;
2626 prod = align / mp->m_sb.sb_rextsize;
2627 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2628 align, 1, ap->eof, 0,
2629 ap->conv, &ap->off, &ap->alen);
2630 if (error)
2631 return error;
2632 ASSERT(ap->alen);
2633 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
2634
2635 /*
2636 * If the offset & length are not perfectly aligned
2637 * then kill prod, it will just get us in trouble.
2638 */
2639 if (do_mod(ap->off, align) || ap->alen % align)
2640 prod = 1;
2641 /*
2642 * Set ralen to be the actual requested length in rtextents.
2643 */
2644 ralen = ap->alen / mp->m_sb.sb_rextsize;
2645 /*
2646 * If the old value was close enough to MAXEXTLEN that
2647 * we rounded up to it, cut it back so it's valid again.
2648 * Note that if it's a really large request (bigger than
2649 * MAXEXTLEN), we don't hear about that number, and can't
2650 * adjust the starting point to match it.
2651 */
2652 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2653 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2654 /*
2655 * If it's an allocation to an empty file at offset 0,
2656 * pick an extent that will space things out in the rt area.
2657 */
2658 if (ap->eof && ap->off == 0) {
2659 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2660 if (error)
2661 return error;
2662 ap->rval = rtx * mp->m_sb.sb_rextsize;
2663 } else {
2664 ap->rval = 0;
2665 }
2666
2667 xfs_bmap_adjacent(ap);
2668
2669 /*
2670 * Realtime allocation, done through xfs_rtallocate_extent.
2671 */
2672 atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2673 do_div(ap->rval, mp->m_sb.sb_rextsize);
2674 rtb = ap->rval;
2675 ap->alen = ralen;
2676 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
2677 &ralen, atype, ap->wasdel, prod, &rtb)))
2678 return error;
2679 if (rtb == NULLFSBLOCK && prod > 1 &&
2680 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
2681 ap->alen, &ralen, atype,
2682 ap->wasdel, 1, &rtb)))
2683 return error;
2684 ap->rval = rtb;
2685 if (ap->rval != NULLFSBLOCK) {
2686 ap->rval *= mp->m_sb.sb_rextsize;
2687 ralen *= mp->m_sb.sb_rextsize;
2688 ap->alen = ralen;
2689 ap->ip->i_d.di_nblocks += ralen;
2690 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2691 if (ap->wasdel)
2692 ap->ip->i_delayed_blks -= ralen;
2693 /*
2694 * Adjust the disk quota also. This was reserved
2695 * earlier.
2696 */
2697 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2698 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2699 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2700 } else {
2701 ap->alen = 0;
2702 }
2703 return 0;
2704}
2705
2706STATIC int
2707xfs_bmap_btalloc(
2708 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2709{
2710 xfs_mount_t *mp; /* mount point structure */
2711 xfs_alloctype_t atype = 0; /* type for allocation routines */
2712 xfs_extlen_t align; /* minimum allocation alignment */
2713 xfs_agnumber_t ag;
2714 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2715 xfs_agnumber_t startag;
2716 xfs_alloc_arg_t args;
2717 xfs_extlen_t blen;
2718 xfs_extlen_t delta;
2719 xfs_extlen_t longest;
2720 xfs_extlen_t need;
2721 xfs_extlen_t nextminlen = 0;
2722 xfs_perag_t *pag;
2723 int nullfb; /* true if ap->firstblock isn't set */
2724 int isaligned;
2725 int notinit;
2726 int tryagain;
2727 int error;
2728
2729 mp = ap->ip->i_mount;
2730 align = (ap->userdata && ap->ip->i_d.di_extsize &&
2731 (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)) ?
2732 ap->ip->i_d.di_extsize : 0;
2733 if (unlikely(align)) {
2734 error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp,
2735 align, 0, ap->eof, 0, ap->conv,
2736 &ap->off, &ap->alen);
2737 ASSERT(!error);
2738 ASSERT(ap->alen);
2739 }
2740 nullfb = ap->firstblock == NULLFSBLOCK;
2741 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
2742 if (nullfb)
2743 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2744 else
2745 ap->rval = ap->firstblock;
2746
2747 xfs_bmap_adjacent(ap);
2748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 /*
2750 * If allowed, use ap->rval; otherwise must use firstblock since
2751 * it's in the right allocation group.
2752 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002753 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 ;
2755 else
2756 ap->rval = ap->firstblock;
2757 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 * Normal allocation, done through xfs_alloc_vextent.
2759 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002760 tryagain = isaligned = 0;
2761 args.tp = ap->tp;
2762 args.mp = mp;
2763 args.fsbno = ap->rval;
2764 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
Yingping Lud210a282006-06-09 14:55:18 +10002765 args.firstblock = ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002766 blen = 0;
2767 if (nullfb) {
2768 args.type = XFS_ALLOCTYPE_START_BNO;
2769 args.total = ap->total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 /*
Nathan Scotta365bdd2006-03-14 13:34:16 +11002771 * Find the longest available space.
2772 * We're going to try for the whole allocation at once.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002774 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2775 notinit = 0;
2776 down_read(&mp->m_peraglock);
2777 while (blen < ap->alen) {
2778 pag = &mp->m_perag[ag];
2779 if (!pag->pagf_init &&
2780 (error = xfs_alloc_pagf_init(mp, args.tp,
2781 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2782 up_read(&mp->m_peraglock);
2783 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002785 /*
2786 * See xfs_alloc_fix_freelist...
2787 */
2788 if (pag->pagf_init) {
2789 need = XFS_MIN_FREELIST_PAG(pag, mp);
2790 delta = need > pag->pagf_flcount ?
2791 need - pag->pagf_flcount : 0;
2792 longest = (pag->pagf_longest > delta) ?
2793 (pag->pagf_longest - delta) :
2794 (pag->pagf_flcount > 0 ||
2795 pag->pagf_longest > 0);
2796 if (blen < longest)
2797 blen = longest;
2798 } else
2799 notinit = 1;
2800 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;
2824 } else if (ap->low) {
Yingping Lud210a282006-06-09 14:55:18 +10002825 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002826 args.total = args.minlen = ap->minlen;
2827 } else {
2828 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2829 args.total = ap->total;
2830 args.minlen = ap->minlen;
2831 }
2832 if (unlikely(ap->userdata && ap->ip->i_d.di_extsize &&
2833 (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE))) {
2834 args.prod = ap->ip->i_d.di_extsize;
2835 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2836 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Nathan Scott6fe90e6d2006-06-19 08:40:12 +10002837 } else if (mp->m_sb.sb_blocksize >= NBPP) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11002838 args.prod = 1;
2839 args.mod = 0;
2840 } else {
2841 args.prod = NBPP >> mp->m_sb.sb_blocklog;
2842 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2843 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2844 }
2845 /*
2846 * If we are not low on available data blocks, and the
2847 * underlying logical volume manager is a stripe, and
2848 * the file offset is zero then try to allocate data
2849 * blocks on stripe unit boundary.
2850 * NOTE: ap->aeof is only set if the allocation length
2851 * is >= the stripe unit and the allocation offset is
2852 * at the end of file.
2853 */
2854 if (!ap->low && ap->aeof) {
2855 if (!ap->off) {
2856 args.alignment = mp->m_dalign;
2857 atype = args.type;
2858 isaligned = 1;
2859 /*
2860 * Adjust for alignment
2861 */
2862 if (blen > args.alignment && blen <= ap->alen)
2863 args.minlen = blen - args.alignment;
2864 args.minalignslop = 0;
2865 } else {
2866 /*
2867 * First try an exact bno allocation.
2868 * If it fails then do a near or start bno
2869 * allocation with alignment turned on.
2870 */
2871 atype = args.type;
2872 tryagain = 1;
2873 args.type = XFS_ALLOCTYPE_THIS_BNO;
2874 args.alignment = 1;
2875 /*
2876 * Compute the minlen+alignment for the
2877 * next case. Set slop so that the value
2878 * of minlen+alignment+slop doesn't go up
2879 * between the calls.
2880 */
2881 if (blen > mp->m_dalign && blen <= ap->alen)
2882 nextminlen = blen - mp->m_dalign;
2883 else
2884 nextminlen = args.minlen;
2885 if (nextminlen + mp->m_dalign > args.minlen + 1)
2886 args.minalignslop =
2887 nextminlen + mp->m_dalign -
2888 args.minlen - 1;
2889 else
2890 args.minalignslop = 0;
2891 }
2892 } else {
2893 args.alignment = 1;
2894 args.minalignslop = 0;
2895 }
2896 args.minleft = ap->minleft;
2897 args.wasdel = ap->wasdel;
2898 args.isfl = 0;
2899 args.userdata = ap->userdata;
2900 if ((error = xfs_alloc_vextent(&args)))
2901 return error;
2902 if (tryagain && args.fsbno == NULLFSBLOCK) {
2903 /*
2904 * Exact allocation failed. Now try with alignment
2905 * turned on.
2906 */
2907 args.type = atype;
2908 args.fsbno = ap->rval;
2909 args.alignment = mp->m_dalign;
2910 args.minlen = nextminlen;
2911 args.minalignslop = 0;
2912 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 if ((error = xfs_alloc_vextent(&args)))
2914 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002915 }
2916 if (isaligned && args.fsbno == NULLFSBLOCK) {
2917 /*
2918 * allocation failed, so turn off alignment and
2919 * try again.
2920 */
2921 args.type = atype;
2922 args.fsbno = ap->rval;
2923 args.alignment = 0;
2924 if ((error = xfs_alloc_vextent(&args)))
2925 return error;
2926 }
2927 if (args.fsbno == NULLFSBLOCK && nullfb &&
2928 args.minlen > ap->minlen) {
2929 args.minlen = ap->minlen;
2930 args.type = XFS_ALLOCTYPE_START_BNO;
2931 args.fsbno = ap->rval;
2932 if ((error = xfs_alloc_vextent(&args)))
2933 return error;
2934 }
2935 if (args.fsbno == NULLFSBLOCK && nullfb) {
2936 args.fsbno = 0;
2937 args.type = XFS_ALLOCTYPE_FIRST_AG;
2938 args.total = ap->minlen;
2939 args.minleft = 0;
2940 if ((error = xfs_alloc_vextent(&args)))
2941 return error;
2942 ap->low = 1;
2943 }
2944 if (args.fsbno != NULLFSBLOCK) {
2945 ap->firstblock = ap->rval = args.fsbno;
2946 ASSERT(nullfb || fb_agno == args.agno ||
2947 (ap->low && fb_agno < args.agno));
2948 ap->alen = args.len;
2949 ap->ip->i_d.di_nblocks += args.len;
2950 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2951 if (ap->wasdel)
2952 ap->ip->i_delayed_blks -= args.len;
2953 /*
2954 * Adjust the disk quota also. This was reserved
2955 * earlier.
2956 */
2957 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2958 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2959 XFS_TRANS_DQ_BCOUNT,
2960 (long) args.len);
2961 } else {
2962 ap->rval = NULLFSBLOCK;
2963 ap->alen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
2965 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002966}
2967
2968/*
2969 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2970 * It figures out where to ask the underlying allocator to put the new extent.
2971 */
2972STATIC int
2973xfs_bmap_alloc(
2974 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2975{
2976 if ((ap->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) && ap->userdata)
2977 return xfs_bmap_rtalloc(ap);
2978 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979}
2980
2981/*
2982 * Transform a btree format file with only one leaf node, where the
2983 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002984 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 * give up the space for the btree root and pitch the leaf block.
2986 */
2987STATIC int /* error */
2988xfs_bmap_btree_to_extents(
2989 xfs_trans_t *tp, /* transaction pointer */
2990 xfs_inode_t *ip, /* incore inode pointer */
2991 xfs_btree_cur_t *cur, /* btree cursor */
2992 int *logflagsp, /* inode logging flags */
2993 int whichfork) /* data or attr fork */
2994{
2995 /* REFERENCED */
2996 xfs_bmbt_block_t *cblock;/* child btree block */
2997 xfs_fsblock_t cbno; /* child block number */
2998 xfs_buf_t *cbp; /* child block's buffer */
2999 int error; /* error return value */
3000 xfs_ifork_t *ifp; /* inode fork data */
3001 xfs_mount_t *mp; /* mount point structure */
3002 xfs_bmbt_ptr_t *pp; /* ptr to block address */
3003 xfs_bmbt_block_t *rblock;/* root btree block */
3004
3005 ifp = XFS_IFORK_PTR(ip, whichfork);
3006 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3007 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
3008 rblock = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003009 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
3010 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
3012 mp = ip->i_mount;
3013 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
3014 *logflagsp = 0;
3015#ifdef DEBUG
3016 if ((error = xfs_btree_check_lptr(cur, INT_GET(*pp, ARCH_CONVERT), 1)))
3017 return error;
3018#endif
3019 cbno = INT_GET(*pp, ARCH_CONVERT);
3020 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
3021 XFS_BMAP_BTREE_REF)))
3022 return error;
3023 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
3024 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
3025 return error;
3026 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
3027 ip->i_d.di_nblocks--;
3028 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
3029 xfs_trans_binval(tp, cbp);
3030 if (cur->bc_bufs[0] == cbp)
3031 cur->bc_bufs[0] = NULL;
3032 xfs_iroot_realloc(ip, -1, whichfork);
3033 ASSERT(ifp->if_broot == NULL);
3034 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
3035 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3036 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
3037 return 0;
3038}
3039
3040/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003041 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 * after removing space (or undoing a delayed allocation).
3043 */
3044STATIC int /* error */
3045xfs_bmap_del_extent(
3046 xfs_inode_t *ip, /* incore inode pointer */
3047 xfs_trans_t *tp, /* current transaction pointer */
3048 xfs_extnum_t idx, /* extent number to update/delete */
3049 xfs_bmap_free_t *flist, /* list of extents to be freed */
3050 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003051 xfs_bmbt_irec_t *del, /* data to remove from extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 int *logflagsp, /* inode logging flags */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003053 xfs_extdelta_t *delta, /* Change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 int whichfork, /* data or attr fork */
3055 int rsvd) /* OK to allocate reserved blocks */
3056{
3057 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
3058 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
3059 xfs_fsblock_t del_endblock=0; /* first block past del */
3060 xfs_fileoff_t del_endoff; /* first offset past del */
3061 int delay; /* current block is delayed allocated */
3062 int do_fx; /* free extent at end of routine */
3063 xfs_bmbt_rec_t *ep; /* current extent entry pointer */
3064 int error; /* error return value */
3065 int flags; /* inode logging flags */
3066#ifdef XFS_BMAP_TRACE
3067 static char fname[] = "xfs_bmap_del_extent";
3068#endif
3069 xfs_bmbt_irec_t got; /* current extent entry */
3070 xfs_fileoff_t got_endoff; /* first offset past got */
3071 int i; /* temp state */
3072 xfs_ifork_t *ifp; /* inode fork pointer */
3073 xfs_mount_t *mp; /* mount structure */
3074 xfs_filblks_t nblks; /* quota/sb block count */
3075 xfs_bmbt_irec_t new; /* new record to be inserted */
3076 /* REFERENCED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 uint qfield; /* quota field to update */
3078 xfs_filblks_t temp; /* for indirect length calculations */
3079 xfs_filblks_t temp2; /* for indirect length calculations */
3080
3081 XFS_STATS_INC(xs_del_exlist);
3082 mp = ip->i_mount;
3083 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003084 ASSERT((idx >= 0) && (idx < ifp->if_bytes /
3085 (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 ASSERT(del->br_blockcount > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003087 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 xfs_bmbt_get_all(ep, &got);
3089 ASSERT(got.br_startoff <= del->br_startoff);
3090 del_endoff = del->br_startoff + del->br_blockcount;
3091 got_endoff = got.br_startoff + got.br_blockcount;
3092 ASSERT(got_endoff >= del_endoff);
3093 delay = ISNULLSTARTBLOCK(got.br_startblock);
3094 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
3095 flags = 0;
3096 qfield = 0;
3097 error = 0;
3098 /*
3099 * If deleting a real allocation, must free up the disk space.
3100 */
3101 if (!delay) {
3102 flags = XFS_ILOG_CORE;
3103 /*
3104 * Realtime allocation. Free it and record di_nblocks update.
3105 */
3106 if (whichfork == XFS_DATA_FORK &&
3107 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
3108 xfs_fsblock_t bno;
3109 xfs_filblks_t len;
3110
3111 ASSERT(do_mod(del->br_blockcount,
3112 mp->m_sb.sb_rextsize) == 0);
3113 ASSERT(do_mod(del->br_startblock,
3114 mp->m_sb.sb_rextsize) == 0);
3115 bno = del->br_startblock;
3116 len = del->br_blockcount;
3117 do_div(bno, mp->m_sb.sb_rextsize);
3118 do_div(len, mp->m_sb.sb_rextsize);
3119 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
3120 (xfs_extlen_t)len)))
3121 goto done;
3122 do_fx = 0;
3123 nblks = len * mp->m_sb.sb_rextsize;
3124 qfield = XFS_TRANS_DQ_RTBCOUNT;
3125 }
3126 /*
3127 * Ordinary allocation.
3128 */
3129 else {
3130 do_fx = 1;
3131 nblks = del->br_blockcount;
3132 qfield = XFS_TRANS_DQ_BCOUNT;
3133 }
3134 /*
3135 * Set up del_endblock and cur for later.
3136 */
3137 del_endblock = del->br_startblock + del->br_blockcount;
3138 if (cur) {
3139 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
3140 got.br_startblock, got.br_blockcount,
3141 &i)))
3142 goto done;
3143 ASSERT(i == 1);
3144 }
3145 da_old = da_new = 0;
3146 } else {
3147 da_old = STARTBLOCKVAL(got.br_startblock);
3148 da_new = 0;
3149 nblks = 0;
3150 do_fx = 0;
3151 }
3152 /*
3153 * Set flag value to use in switch statement.
3154 * Left-contig is 2, right-contig is 1.
3155 */
3156 switch (((got.br_startoff == del->br_startoff) << 1) |
3157 (got_endoff == del_endoff)) {
3158 case 3:
3159 /*
3160 * Matches the whole extent. Delete the entry.
3161 */
3162 xfs_bmap_trace_delete(fname, "3", ip, idx, 1, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003163 xfs_iext_remove(ifp, idx, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 ifp->if_lastex = idx;
3165 if (delay)
3166 break;
3167 XFS_IFORK_NEXT_SET(ip, whichfork,
3168 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
3169 flags |= XFS_ILOG_CORE;
3170 if (!cur) {
3171 flags |= XFS_ILOG_FEXT(whichfork);
3172 break;
3173 }
3174 if ((error = xfs_bmbt_delete(cur, &i)))
3175 goto done;
3176 ASSERT(i == 1);
3177 break;
3178
3179 case 2:
3180 /*
3181 * Deleting the first part of the extent.
3182 */
3183 xfs_bmap_trace_pre_update(fname, "2", ip, idx, whichfork);
3184 xfs_bmbt_set_startoff(ep, del_endoff);
3185 temp = got.br_blockcount - del->br_blockcount;
3186 xfs_bmbt_set_blockcount(ep, temp);
3187 ifp->if_lastex = idx;
3188 if (delay) {
3189 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3190 da_old);
3191 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3192 xfs_bmap_trace_post_update(fname, "2", ip, idx,
3193 whichfork);
3194 da_new = temp;
3195 break;
3196 }
3197 xfs_bmbt_set_startblock(ep, del_endblock);
3198 xfs_bmap_trace_post_update(fname, "2", ip, idx, whichfork);
3199 if (!cur) {
3200 flags |= XFS_ILOG_FEXT(whichfork);
3201 break;
3202 }
3203 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
3204 got.br_blockcount - del->br_blockcount,
3205 got.br_state)))
3206 goto done;
3207 break;
3208
3209 case 1:
3210 /*
3211 * Deleting the last part of the extent.
3212 */
3213 temp = got.br_blockcount - del->br_blockcount;
3214 xfs_bmap_trace_pre_update(fname, "1", ip, idx, whichfork);
3215 xfs_bmbt_set_blockcount(ep, temp);
3216 ifp->if_lastex = idx;
3217 if (delay) {
3218 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
3219 da_old);
3220 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3221 xfs_bmap_trace_post_update(fname, "1", ip, idx,
3222 whichfork);
3223 da_new = temp;
3224 break;
3225 }
3226 xfs_bmap_trace_post_update(fname, "1", ip, idx, whichfork);
3227 if (!cur) {
3228 flags |= XFS_ILOG_FEXT(whichfork);
3229 break;
3230 }
3231 if ((error = xfs_bmbt_update(cur, got.br_startoff,
3232 got.br_startblock,
3233 got.br_blockcount - del->br_blockcount,
3234 got.br_state)))
3235 goto done;
3236 break;
3237
3238 case 0:
3239 /*
3240 * Deleting the middle of the extent.
3241 */
3242 temp = del->br_startoff - got.br_startoff;
3243 xfs_bmap_trace_pre_update(fname, "0", ip, idx, whichfork);
3244 xfs_bmbt_set_blockcount(ep, temp);
3245 new.br_startoff = del_endoff;
3246 temp2 = got_endoff - del_endoff;
3247 new.br_blockcount = temp2;
3248 new.br_state = got.br_state;
3249 if (!delay) {
3250 new.br_startblock = del_endblock;
3251 flags |= XFS_ILOG_CORE;
3252 if (cur) {
3253 if ((error = xfs_bmbt_update(cur,
3254 got.br_startoff,
3255 got.br_startblock, temp,
3256 got.br_state)))
3257 goto done;
3258 if ((error = xfs_bmbt_increment(cur, 0, &i)))
3259 goto done;
3260 cur->bc_rec.b = new;
3261 error = xfs_bmbt_insert(cur, &i);
3262 if (error && error != ENOSPC)
3263 goto done;
3264 /*
3265 * If get no-space back from btree insert,
3266 * it tried a split, and we have a zero
3267 * block reservation.
3268 * Fix up our state and return the error.
3269 */
3270 if (error == ENOSPC) {
3271 /*
3272 * Reset the cursor, don't trust
3273 * it after any insert operation.
3274 */
3275 if ((error = xfs_bmbt_lookup_eq(cur,
3276 got.br_startoff,
3277 got.br_startblock,
3278 temp, &i)))
3279 goto done;
3280 ASSERT(i == 1);
3281 /*
3282 * Update the btree record back
3283 * to the original value.
3284 */
3285 if ((error = xfs_bmbt_update(cur,
3286 got.br_startoff,
3287 got.br_startblock,
3288 got.br_blockcount,
3289 got.br_state)))
3290 goto done;
3291 /*
3292 * Reset the extent record back
3293 * to the original value.
3294 */
3295 xfs_bmbt_set_blockcount(ep,
3296 got.br_blockcount);
3297 flags = 0;
3298 error = XFS_ERROR(ENOSPC);
3299 goto done;
3300 }
3301 ASSERT(i == 1);
3302 } else
3303 flags |= XFS_ILOG_FEXT(whichfork);
3304 XFS_IFORK_NEXT_SET(ip, whichfork,
3305 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3306 } else {
3307 ASSERT(whichfork == XFS_DATA_FORK);
3308 temp = xfs_bmap_worst_indlen(ip, temp);
3309 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
3310 temp2 = xfs_bmap_worst_indlen(ip, temp2);
3311 new.br_startblock = NULLSTARTBLOCK((int)temp2);
3312 da_new = temp + temp2;
3313 while (da_new > da_old) {
3314 if (temp) {
3315 temp--;
3316 da_new--;
3317 xfs_bmbt_set_startblock(ep,
3318 NULLSTARTBLOCK((int)temp));
3319 }
3320 if (da_new == da_old)
3321 break;
3322 if (temp2) {
3323 temp2--;
3324 da_new--;
3325 new.br_startblock =
3326 NULLSTARTBLOCK((int)temp2);
3327 }
3328 }
3329 }
3330 xfs_bmap_trace_post_update(fname, "0", ip, idx, whichfork);
3331 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 1, &new, NULL,
3332 whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003333 xfs_iext_insert(ifp, idx + 1, 1, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 ifp->if_lastex = idx + 1;
3335 break;
3336 }
3337 /*
3338 * If we need to, add to list of extents to delete.
3339 */
3340 if (do_fx)
3341 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3342 mp);
3343 /*
3344 * Adjust inode # blocks in the file.
3345 */
3346 if (nblks)
3347 ip->i_d.di_nblocks -= nblks;
3348 /*
3349 * Adjust quota data.
3350 */
3351 if (qfield)
3352 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
3353
3354 /*
3355 * Account for change in delayed indirect blocks.
3356 * Nothing to do for disk quota accounting here.
3357 */
3358 ASSERT(da_old >= da_new);
3359 if (da_old > da_new)
3360 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int)(da_old - da_new),
3361 rsvd);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003362 if (delta) {
3363 /* DELTA: report the original extent. */
3364 if (delta->xed_startoff > got.br_startoff)
3365 delta->xed_startoff = got.br_startoff;
3366 if (delta->xed_blockcount < got.br_startoff+got.br_blockcount)
3367 delta->xed_blockcount = got.br_startoff +
3368 got.br_blockcount;
3369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370done:
3371 *logflagsp = flags;
3372 return error;
3373}
3374
3375/*
3376 * Remove the entry "free" from the free item list. Prev points to the
3377 * previous entry, unless "free" is the head of the list.
3378 */
3379STATIC void
3380xfs_bmap_del_free(
3381 xfs_bmap_free_t *flist, /* free item list header */
3382 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3383 xfs_bmap_free_item_t *free) /* list item to be freed */
3384{
3385 if (prev)
3386 prev->xbfi_next = free->xbfi_next;
3387 else
3388 flist->xbf_first = free->xbfi_next;
3389 flist->xbf_count--;
3390 kmem_zone_free(xfs_bmap_free_item_zone, free);
3391}
3392
3393/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 * Convert an extents-format file into a btree-format file.
3395 * The new file will have a root block (in the inode) and a single child block.
3396 */
3397STATIC int /* error */
3398xfs_bmap_extents_to_btree(
3399 xfs_trans_t *tp, /* transaction pointer */
3400 xfs_inode_t *ip, /* incore inode pointer */
3401 xfs_fsblock_t *firstblock, /* first-block-allocated */
3402 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3403 xfs_btree_cur_t **curp, /* cursor returned to caller */
3404 int wasdel, /* converting a delayed alloc */
3405 int *logflagsp, /* inode logging flags */
3406 int whichfork) /* data or attr fork */
3407{
3408 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
3409 xfs_buf_t *abp; /* buffer for ablock */
3410 xfs_alloc_arg_t args; /* allocation arguments */
3411 xfs_bmbt_rec_t *arp; /* child record pointer */
3412 xfs_bmbt_block_t *block; /* btree root block */
3413 xfs_btree_cur_t *cur; /* bmap btree cursor */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003414 xfs_bmbt_rec_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003416 xfs_extnum_t i, cnt; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 xfs_ifork_t *ifp; /* inode fork pointer */
3418 xfs_bmbt_key_t *kp; /* root block key pointer */
3419 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003420 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3422
3423 ifp = XFS_IFORK_PTR(ip, whichfork);
3424 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3425 ASSERT(ifp->if_ext_max ==
3426 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3427 /*
3428 * Make space in the inode incore.
3429 */
3430 xfs_iroot_realloc(ip, 1, whichfork);
3431 ifp->if_flags |= XFS_IFBROOT;
3432 /*
3433 * Fill in the root.
3434 */
3435 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003436 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3437 block->bb_level = cpu_to_be16(1);
3438 block->bb_numrecs = cpu_to_be16(1);
3439 block->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3440 block->bb_rightsib = cpu_to_be64(NULLDFSBNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 /*
3442 * Need a cursor. Can't allocate until bb_level is filled in.
3443 */
3444 mp = ip->i_mount;
3445 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
3446 whichfork);
3447 cur->bc_private.b.firstblock = *firstblock;
3448 cur->bc_private.b.flist = flist;
3449 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3450 /*
3451 * Convert to a btree with two levels, one record in root.
3452 */
3453 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3454 args.tp = tp;
3455 args.mp = mp;
Yingping Lud210a282006-06-09 14:55:18 +10003456 args.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 if (*firstblock == NULLFSBLOCK) {
3458 args.type = XFS_ALLOCTYPE_START_BNO;
3459 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3460 } else if (flist->xbf_low) {
3461 args.type = XFS_ALLOCTYPE_START_BNO;
3462 args.fsbno = *firstblock;
3463 } else {
3464 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3465 args.fsbno = *firstblock;
3466 }
3467 args.minlen = args.maxlen = args.prod = 1;
3468 args.total = args.minleft = args.alignment = args.mod = args.isfl =
3469 args.minalignslop = 0;
3470 args.wasdel = wasdel;
3471 *logflagsp = 0;
3472 if ((error = xfs_alloc_vextent(&args))) {
3473 xfs_iroot_realloc(ip, -1, whichfork);
3474 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3475 return error;
3476 }
3477 /*
3478 * Allocation can't fail, the space was reserved.
3479 */
3480 ASSERT(args.fsbno != NULLFSBLOCK);
3481 ASSERT(*firstblock == NULLFSBLOCK ||
3482 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3483 (flist->xbf_low &&
3484 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3485 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3486 cur->bc_private.b.allocated++;
3487 ip->i_d.di_nblocks++;
3488 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3489 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3490 /*
3491 * Fill in the child block.
3492 */
3493 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003494 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 ablock->bb_level = 0;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003496 ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO);
3497 ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3499 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003500 for (cnt = i = 0; i < nextents; i++) {
3501 ep = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
3503 arp->l0 = INT_GET(ep->l0, ARCH_CONVERT);
3504 arp->l1 = INT_GET(ep->l1, ARCH_CONVERT);
3505 arp++; cnt++;
3506 }
3507 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11003508 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3509 ablock->bb_numrecs = cpu_to_be16(cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 /*
3511 * Fill in the root key and pointer.
3512 */
3513 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
3514 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
3515 INT_SET(kp->br_startoff, ARCH_CONVERT, xfs_bmbt_disk_get_startoff(arp));
3516 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
3517 INT_SET(*pp, ARCH_CONVERT, args.fsbno);
3518 /*
3519 * Do all this logging at the end so that
3520 * the root is at the right level.
3521 */
3522 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003523 xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 ASSERT(*curp == NULL);
3525 *curp = cur;
3526 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
3527 return 0;
3528}
3529
3530/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11003531 * Helper routine to reset inode di_forkoff field when switching
3532 * attribute fork from local to extent format - we reset it where
3533 * possible to make space available for inline data fork extents.
3534 */
3535STATIC void
3536xfs_bmap_forkoff_reset(
3537 xfs_mount_t *mp,
3538 xfs_inode_t *ip,
3539 int whichfork)
3540{
3541 if (whichfork == XFS_ATTR_FORK &&
3542 (ip->i_d.di_format != XFS_DINODE_FMT_DEV) &&
3543 (ip->i_d.di_format != XFS_DINODE_FMT_UUID) &&
3544 ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) {
3545 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
3546 ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) /
3547 (uint)sizeof(xfs_bmbt_rec_t);
3548 ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) /
3549 (uint)sizeof(xfs_bmbt_rec_t);
3550 }
3551}
3552
3553/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 * Convert a local file to an extents file.
3555 * This code is out of bounds for data forks of regular files,
3556 * since the file data needs to get logged so things will stay consistent.
3557 * (The bmap-level manipulations are ok, though).
3558 */
3559STATIC int /* error */
3560xfs_bmap_local_to_extents(
3561 xfs_trans_t *tp, /* transaction pointer */
3562 xfs_inode_t *ip, /* incore inode pointer */
3563 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3564 xfs_extlen_t total, /* total blocks needed by transaction */
3565 int *logflagsp, /* inode logging flags */
3566 int whichfork) /* data or attr fork */
3567{
3568 int error; /* error return value */
3569 int flags; /* logging flags returned */
3570#ifdef XFS_BMAP_TRACE
3571 static char fname[] = "xfs_bmap_local_to_extents";
3572#endif
3573 xfs_ifork_t *ifp; /* inode fork pointer */
3574
3575 /*
3576 * We don't want to deal with the case of keeping inode data inline yet.
3577 * So sending the data fork of a regular inode is invalid.
3578 */
3579 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
3580 whichfork == XFS_DATA_FORK));
3581 ifp = XFS_IFORK_PTR(ip, whichfork);
3582 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3583 flags = 0;
3584 error = 0;
3585 if (ifp->if_bytes) {
3586 xfs_alloc_arg_t args; /* allocation arguments */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003587 xfs_buf_t *bp; /* buffer for extent block */
3588 xfs_bmbt_rec_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
3590 args.tp = tp;
3591 args.mp = ip->i_mount;
Yingping Lud210a282006-06-09 14:55:18 +10003592 args.firstblock = *firstblock;
Mandy Kirkconnellf020b672006-03-14 14:07:24 +11003593 ASSERT((ifp->if_flags &
3594 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 /*
3596 * Allocate a block. We know we need only one, since the
3597 * file currently fits in an inode.
3598 */
3599 if (*firstblock == NULLFSBLOCK) {
3600 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3601 args.type = XFS_ALLOCTYPE_START_BNO;
3602 } else {
3603 args.fsbno = *firstblock;
3604 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3605 }
3606 args.total = total;
3607 args.mod = args.minleft = args.alignment = args.wasdel =
3608 args.isfl = args.minalignslop = 0;
3609 args.minlen = args.maxlen = args.prod = 1;
3610 if ((error = xfs_alloc_vextent(&args)))
3611 goto done;
3612 /*
3613 * Can't fail, the space was reserved.
3614 */
3615 ASSERT(args.fsbno != NULLFSBLOCK);
3616 ASSERT(args.len == 1);
3617 *firstblock = args.fsbno;
3618 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3619 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
3620 ifp->if_bytes);
3621 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003622 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003624 xfs_iext_add(ifp, 0, 1);
3625 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3627 xfs_bmap_trace_post_update(fname, "new", ip, 0, whichfork);
3628 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3629 ip->i_d.di_nblocks = 1;
3630 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
3631 XFS_TRANS_DQ_BCOUNT, 1L);
3632 flags |= XFS_ILOG_FEXT(whichfork);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003633 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003635 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 ifp->if_flags &= ~XFS_IFINLINE;
3638 ifp->if_flags |= XFS_IFEXTENTS;
3639 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3640 flags |= XFS_ILOG_CORE;
3641done:
3642 *logflagsp = flags;
3643 return error;
3644}
3645
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003647 * Search the extent records for the entry containing block bno.
3648 * If bno lies in a hole, point to the next entry. If bno lies
3649 * past eof, *eofp will be set, and *prevp will contain the last
3650 * entry (null if none). Else, *lastxp will be set to the index
3651 * of the found entry; *gotp will contain the entry.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003652 */
3653xfs_bmbt_rec_t * /* pointer to found extent entry */
3654xfs_bmap_search_multi_extents(
3655 xfs_ifork_t *ifp, /* inode fork pointer */
3656 xfs_fileoff_t bno, /* block number searched for */
3657 int *eofp, /* out: end of file found */
3658 xfs_extnum_t *lastxp, /* out: last extent index */
3659 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3660 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3661{
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003662 xfs_bmbt_rec_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003663 xfs_extnum_t lastx; /* last extent index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003664
3665 /*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003666 * Initialize the extent entry structure to catch access to
3667 * uninitialized br_startblock field.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003668 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003669 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3670 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3671 gotp->br_state = XFS_EXT_INVALID;
3672#if XFS_BIG_BLKNOS
3673 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3674#else
3675 gotp->br_startblock = 0xffffa5a5;
3676#endif
3677 prevp->br_startoff = NULLFILEOFF;
3678
3679 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3680 if (lastx > 0) {
3681 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3682 }
3683 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3684 xfs_bmbt_get_all(ep, gotp);
3685 *eofp = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003686 } else {
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003687 if (lastx > 0) {
3688 *gotp = *prevp;
3689 }
3690 *eofp = 1;
3691 ep = NULL;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003692 }
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003693 *lastxp = lastx;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003694 return ep;
3695}
3696
3697/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 * Search the extents list for the inode, for the extent containing bno.
3699 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3700 * *eofp will be set, and *prevp will contain the last entry (null if none).
3701 * Else, *lastxp will be set to the index of the found
3702 * entry; *gotp will contain the entry.
3703 */
3704STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
3705xfs_bmap_search_extents(
3706 xfs_inode_t *ip, /* incore inode pointer */
3707 xfs_fileoff_t bno, /* block number searched for */
3708 int whichfork, /* data or attr fork */
3709 int *eofp, /* out: end of file found */
3710 xfs_extnum_t *lastxp, /* out: last extent index */
3711 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3712 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3713{
3714 xfs_ifork_t *ifp; /* inode fork pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003715 xfs_bmbt_rec_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 int rt; /* realtime flag */
3717
3718 XFS_STATS_INC(xs_look_exlist);
3719 ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003721 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3722
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003723 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
3724 if (unlikely(!rt && !gotp->br_startblock && (*lastxp != NULLEXTNUM))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 cmn_err(CE_PANIC,"Access to block zero: fs: <%s> inode: %lld "
3726 "start_block : %llx start_off : %llx blkcnt : %llx "
3727 "extent-state : %x \n",
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003728 (ip->i_mount)->m_fsname, (long long)ip->i_ino,
3729 (unsigned long long)gotp->br_startblock,
3730 (unsigned long long)gotp->br_startoff,
3731 (unsigned long long)gotp->br_blockcount,
3732 gotp->br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 }
3734 return ep;
3735}
3736
3737
3738#ifdef XFS_BMAP_TRACE
3739ktrace_t *xfs_bmap_trace_buf;
3740
3741/*
3742 * Add a bmap trace buffer entry. Base routine for the others.
3743 */
3744STATIC void
3745xfs_bmap_trace_addentry(
3746 int opcode, /* operation */
3747 char *fname, /* function name */
3748 char *desc, /* operation description */
3749 xfs_inode_t *ip, /* incore inode pointer */
3750 xfs_extnum_t idx, /* index of entry(ies) */
3751 xfs_extnum_t cnt, /* count of entries, 1 or 2 */
3752 xfs_bmbt_rec_t *r1, /* first record */
3753 xfs_bmbt_rec_t *r2, /* second record or null */
3754 int whichfork) /* data or attr fork */
3755{
3756 xfs_bmbt_rec_t tr2;
3757
3758 ASSERT(cnt == 1 || cnt == 2);
3759 ASSERT(r1 != NULL);
3760 if (cnt == 1) {
3761 ASSERT(r2 == NULL);
3762 r2 = &tr2;
3763 memset(&tr2, 0, sizeof(tr2));
3764 } else
3765 ASSERT(r2 != NULL);
3766 ktrace_enter(xfs_bmap_trace_buf,
3767 (void *)(__psint_t)(opcode | (whichfork << 16)),
3768 (void *)fname, (void *)desc, (void *)ip,
3769 (void *)(__psint_t)idx,
3770 (void *)(__psint_t)cnt,
3771 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3772 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3773 (void *)(__psunsigned_t)(r1->l0 >> 32),
3774 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3775 (void *)(__psunsigned_t)(r1->l1 >> 32),
3776 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3777 (void *)(__psunsigned_t)(r2->l0 >> 32),
3778 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3779 (void *)(__psunsigned_t)(r2->l1 >> 32),
3780 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3781 );
3782 ASSERT(ip->i_xtrace);
3783 ktrace_enter(ip->i_xtrace,
3784 (void *)(__psint_t)(opcode | (whichfork << 16)),
3785 (void *)fname, (void *)desc, (void *)ip,
3786 (void *)(__psint_t)idx,
3787 (void *)(__psint_t)cnt,
3788 (void *)(__psunsigned_t)(ip->i_ino >> 32),
3789 (void *)(__psunsigned_t)(unsigned)ip->i_ino,
3790 (void *)(__psunsigned_t)(r1->l0 >> 32),
3791 (void *)(__psunsigned_t)(unsigned)(r1->l0),
3792 (void *)(__psunsigned_t)(r1->l1 >> 32),
3793 (void *)(__psunsigned_t)(unsigned)(r1->l1),
3794 (void *)(__psunsigned_t)(r2->l0 >> 32),
3795 (void *)(__psunsigned_t)(unsigned)(r2->l0),
3796 (void *)(__psunsigned_t)(r2->l1 >> 32),
3797 (void *)(__psunsigned_t)(unsigned)(r2->l1)
3798 );
3799}
3800
3801/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003802 * Add bmap trace entry prior to a call to xfs_iext_remove.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 */
3804STATIC void
3805xfs_bmap_trace_delete(
3806 char *fname, /* function name */
3807 char *desc, /* operation description */
3808 xfs_inode_t *ip, /* incore inode pointer */
3809 xfs_extnum_t idx, /* index of entry(entries) deleted */
3810 xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */
3811 int whichfork) /* data or attr fork */
3812{
3813 xfs_ifork_t *ifp; /* inode fork pointer */
3814
3815 ifp = XFS_IFORK_PTR(ip, whichfork);
3816 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003817 cnt, xfs_iext_get_ext(ifp, idx),
3818 cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 whichfork);
3820}
3821
3822/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003823 * Add bmap trace entry prior to a call to xfs_iext_insert, or
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 * reading in the extents list from the disk (in the btree).
3825 */
3826STATIC void
3827xfs_bmap_trace_insert(
3828 char *fname, /* function name */
3829 char *desc, /* operation description */
3830 xfs_inode_t *ip, /* incore inode pointer */
3831 xfs_extnum_t idx, /* index of entry(entries) inserted */
3832 xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */
3833 xfs_bmbt_irec_t *r1, /* inserted record 1 */
3834 xfs_bmbt_irec_t *r2, /* inserted record 2 or null */
3835 int whichfork) /* data or attr fork */
3836{
3837 xfs_bmbt_rec_t tr1; /* compressed record 1 */
3838 xfs_bmbt_rec_t tr2; /* compressed record 2 if needed */
3839
3840 xfs_bmbt_set_all(&tr1, r1);
3841 if (cnt == 2) {
3842 ASSERT(r2 != NULL);
3843 xfs_bmbt_set_all(&tr2, r2);
3844 } else {
3845 ASSERT(cnt == 1);
3846 ASSERT(r2 == NULL);
3847 }
3848 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx,
3849 cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork);
3850}
3851
3852/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003853 * Add bmap trace entry after updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 */
3855STATIC void
3856xfs_bmap_trace_post_update(
3857 char *fname, /* function name */
3858 char *desc, /* operation description */
3859 xfs_inode_t *ip, /* incore inode pointer */
3860 xfs_extnum_t idx, /* index of entry updated */
3861 int whichfork) /* data or attr fork */
3862{
3863 xfs_ifork_t *ifp; /* inode fork pointer */
3864
3865 ifp = XFS_IFORK_PTR(ip, whichfork);
3866 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003867 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868}
3869
3870/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003871 * Add bmap trace entry prior to updating an extent record in place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 */
3873STATIC void
3874xfs_bmap_trace_pre_update(
3875 char *fname, /* function name */
3876 char *desc, /* operation description */
3877 xfs_inode_t *ip, /* incore inode pointer */
3878 xfs_extnum_t idx, /* index of entry to be updated */
3879 int whichfork) /* data or attr fork */
3880{
3881 xfs_ifork_t *ifp; /* inode fork pointer */
3882
3883 ifp = XFS_IFORK_PTR(ip, whichfork);
3884 xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003885 xfs_iext_get_ext(ifp, idx), NULL, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886}
3887#endif /* XFS_BMAP_TRACE */
3888
3889/*
3890 * Compute the worst-case number of indirect blocks that will be used
3891 * for ip's delayed extent of length "len".
3892 */
3893STATIC xfs_filblks_t
3894xfs_bmap_worst_indlen(
3895 xfs_inode_t *ip, /* incore inode pointer */
3896 xfs_filblks_t len) /* delayed extent length */
3897{
3898 int level; /* btree level number */
3899 int maxrecs; /* maximum record count at this level */
3900 xfs_mount_t *mp; /* mount structure */
3901 xfs_filblks_t rval; /* return value */
3902
3903 mp = ip->i_mount;
3904 maxrecs = mp->m_bmap_dmxr[0];
3905 for (level = 0, rval = 0;
3906 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3907 level++) {
3908 len += maxrecs - 1;
3909 do_div(len, maxrecs);
3910 rval += len;
3911 if (len == 1)
3912 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3913 level - 1;
3914 if (level == 0)
3915 maxrecs = mp->m_bmap_dmxr[1];
3916 }
3917 return rval;
3918}
3919
3920#if defined(XFS_RW_TRACE)
3921STATIC void
3922xfs_bunmap_trace(
3923 xfs_inode_t *ip,
3924 xfs_fileoff_t bno,
3925 xfs_filblks_t len,
3926 int flags,
3927 inst_t *ra)
3928{
3929 if (ip->i_rwtrace == NULL)
3930 return;
3931 ktrace_enter(ip->i_rwtrace,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003932 (void *)(__psint_t)XFS_BUNMAP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 (void *)ip,
3934 (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff),
3935 (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff),
3936 (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff),
3937 (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff),
3938 (void *)(__psint_t)len,
3939 (void *)(__psint_t)flags,
3940 (void *)(unsigned long)current_cpu(),
3941 (void *)ra,
3942 (void *)0,
3943 (void *)0,
3944 (void *)0,
3945 (void *)0,
3946 (void *)0,
3947 (void *)0);
3948}
3949#endif
3950
3951/*
3952 * Convert inode from non-attributed to attributed.
3953 * Must not be in a transaction, ip must not be locked.
3954 */
3955int /* error code */
3956xfs_bmap_add_attrfork(
3957 xfs_inode_t *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003958 int size, /* space new attribute needs */
3959 int rsvd) /* xact may use reserved blks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003962 xfs_bmap_free_t flist; /* freed extent records */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 xfs_trans_t *tp; /* transaction pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003965 unsigned long s; /* spinlock spl value */
3966 int blks; /* space reservation */
3967 int version = 1; /* superblock attr version */
3968 int committed; /* xaction was committed */
3969 int logflags; /* logging flags */
3970 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
Nathan Scottd8cc8902005-11-02 10:34:53 +11003972 ASSERT(XFS_IFORK_Q(ip) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 ASSERT(ip->i_df.if_ext_max ==
3974 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
Nathan Scottd8cc8902005-11-02 10:34:53 +11003975
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 mp = ip->i_mount;
3977 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3978 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3979 blks = XFS_ADDAFORK_SPACE_RES(mp);
3980 if (rsvd)
3981 tp->t_flags |= XFS_TRANS_RESERVE;
3982 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3983 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3984 goto error0;
3985 xfs_ilock(ip, XFS_ILOCK_EXCL);
3986 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ?
3987 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3988 XFS_QMOPT_RES_REGBLKS);
3989 if (error) {
3990 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3991 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3992 return error;
3993 }
3994 if (XFS_IFORK_Q(ip))
3995 goto error1;
3996 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3997 /*
3998 * For inodes coming from pre-6.2 filesystems.
3999 */
4000 ASSERT(ip->i_d.di_aformat == 0);
4001 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
4002 }
4003 ASSERT(ip->i_d.di_anextents == 0);
4004 VN_HOLD(XFS_ITOV(ip));
4005 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4006 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4007 switch (ip->i_d.di_format) {
4008 case XFS_DINODE_FMT_DEV:
4009 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
4010 break;
4011 case XFS_DINODE_FMT_UUID:
4012 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
4013 break;
4014 case XFS_DINODE_FMT_LOCAL:
4015 case XFS_DINODE_FMT_EXTENTS:
4016 case XFS_DINODE_FMT_BTREE:
Nathan Scottd8cc8902005-11-02 10:34:53 +11004017 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
4018 if (!ip->i_d.di_forkoff)
4019 ip->i_d.di_forkoff = mp->m_attroffset >> 3;
Nathan Scott13059ff2006-01-11 15:32:01 +11004020 else if (mp->m_flags & XFS_MOUNT_ATTR2)
Nathan Scottd8cc8902005-11-02 10:34:53 +11004021 version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 break;
4023 default:
4024 ASSERT(0);
4025 error = XFS_ERROR(EINVAL);
4026 goto error1;
4027 }
4028 ip->i_df.if_ext_max =
4029 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4030 ASSERT(ip->i_afp == NULL);
4031 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
4032 ip->i_afp->if_ext_max =
4033 XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
4034 ip->i_afp->if_flags = XFS_IFEXTENTS;
4035 logflags = 0;
4036 XFS_BMAP_INIT(&flist, &firstblock);
4037 switch (ip->i_d.di_format) {
4038 case XFS_DINODE_FMT_LOCAL:
4039 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
4040 &logflags);
4041 break;
4042 case XFS_DINODE_FMT_EXTENTS:
4043 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
4044 &flist, &logflags);
4045 break;
4046 case XFS_DINODE_FMT_BTREE:
4047 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
4048 &logflags);
4049 break;
4050 default:
4051 error = 0;
4052 break;
4053 }
4054 if (logflags)
4055 xfs_trans_log_inode(tp, ip, logflags);
4056 if (error)
4057 goto error2;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004058 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb) ||
4059 (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +11004060 __int64_t sbfields = 0;
4061
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 s = XFS_SB_LOCK(mp);
4063 if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) {
4064 XFS_SB_VERSION_ADDATTR(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11004065 sbfields |= XFS_SB_VERSIONNUM;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004066 }
4067 if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2) {
4068 XFS_SB_VERSION_ADDATTR2(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11004069 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
Nathan Scottd8cc8902005-11-02 10:34:53 +11004070 }
Nathan Scottda087ba2005-11-02 15:00:20 +11004071 if (sbfields) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 XFS_SB_UNLOCK(mp, s);
Nathan Scottda087ba2005-11-02 15:00:20 +11004073 xfs_mod_sb(tp, sbfields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 } else
4075 XFS_SB_UNLOCK(mp, s);
4076 }
4077 if ((error = xfs_bmap_finish(&tp, &flist, firstblock, &committed)))
4078 goto error2;
4079 error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES, NULL);
4080 ASSERT(ip->i_df.if_ext_max ==
4081 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4082 return error;
4083error2:
4084 xfs_bmap_cancel(&flist);
4085error1:
4086 ASSERT(ismrlocked(&ip->i_lock,MR_UPDATE));
4087 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4088error0:
4089 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
4090 ASSERT(ip->i_df.if_ext_max ==
4091 XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t));
4092 return error;
4093}
4094
4095/*
4096 * Add the extent to the list of extents to be free at transaction end.
4097 * The list is maintained sorted (by block number).
4098 */
4099/* ARGSUSED */
4100void
4101xfs_bmap_add_free(
4102 xfs_fsblock_t bno, /* fs block number of extent */
4103 xfs_filblks_t len, /* length of extent */
4104 xfs_bmap_free_t *flist, /* list of extents */
4105 xfs_mount_t *mp) /* mount point structure */
4106{
4107 xfs_bmap_free_item_t *cur; /* current (next) element */
4108 xfs_bmap_free_item_t *new; /* new element */
4109 xfs_bmap_free_item_t *prev; /* previous element */
4110#ifdef DEBUG
4111 xfs_agnumber_t agno;
4112 xfs_agblock_t agbno;
4113
4114 ASSERT(bno != NULLFSBLOCK);
4115 ASSERT(len > 0);
4116 ASSERT(len <= MAXEXTLEN);
4117 ASSERT(!ISNULLSTARTBLOCK(bno));
4118 agno = XFS_FSB_TO_AGNO(mp, bno);
4119 agbno = XFS_FSB_TO_AGBNO(mp, bno);
4120 ASSERT(agno < mp->m_sb.sb_agcount);
4121 ASSERT(agbno < mp->m_sb.sb_agblocks);
4122 ASSERT(len < mp->m_sb.sb_agblocks);
4123 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
4124#endif
4125 ASSERT(xfs_bmap_free_item_zone != NULL);
4126 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
4127 new->xbfi_startblock = bno;
4128 new->xbfi_blockcount = (xfs_extlen_t)len;
4129 for (prev = NULL, cur = flist->xbf_first;
4130 cur != NULL;
4131 prev = cur, cur = cur->xbfi_next) {
4132 if (cur->xbfi_startblock >= bno)
4133 break;
4134 }
4135 if (prev)
4136 prev->xbfi_next = new;
4137 else
4138 flist->xbf_first = new;
4139 new->xbfi_next = cur;
4140 flist->xbf_count++;
4141}
4142
4143/*
4144 * Compute and fill in the value of the maximum depth of a bmap btree
4145 * in this filesystem. Done once, during mount.
4146 */
4147void
4148xfs_bmap_compute_maxlevels(
4149 xfs_mount_t *mp, /* file system mount structure */
4150 int whichfork) /* data or attr fork */
4151{
4152 int level; /* btree level */
4153 uint maxblocks; /* max blocks at this level */
4154 uint maxleafents; /* max leaf entries possible */
4155 int maxrootrecs; /* max records in root block */
4156 int minleafrecs; /* min records in leaf block */
4157 int minnoderecs; /* min records in node block */
4158 int sz; /* root block size */
4159
4160 /*
4161 * The maximum number of extents in a file, hence the maximum
4162 * number of leaf entries, is controlled by the type of di_nextents
4163 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
4164 * (a signed 16-bit number, xfs_aextnum_t).
4165 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11004166 if (whichfork == XFS_DATA_FORK) {
4167 maxleafents = MAXEXTNUM;
Nathan Scott13059ff2006-01-11 15:32:01 +11004168 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4169 XFS_BMDR_SPACE_CALC(MINDBTPTRS) : mp->m_attroffset;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004170 } else {
4171 maxleafents = MAXAEXTNUM;
Nathan Scott13059ff2006-01-11 15:32:01 +11004172 sz = (mp->m_flags & XFS_MOUNT_ATTR2) ?
4173 XFS_BMDR_SPACE_CALC(MINABTPTRS) :
4174 mp->m_sb.sb_inodesize - mp->m_attroffset;
Nathan Scottd8cc8902005-11-02 10:34:53 +11004175 }
4176 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 minleafrecs = mp->m_bmap_dmnr[0];
4178 minnoderecs = mp->m_bmap_dmnr[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
4180 for (level = 1; maxblocks > 1; level++) {
4181 if (maxblocks <= maxrootrecs)
4182 maxblocks = 1;
4183 else
4184 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
4185 }
4186 mp->m_bm_maxlevels[whichfork] = level;
4187}
4188
4189/*
4190 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
4191 * caller. Frees all the extents that need freeing, which must be done
4192 * last due to locking considerations. We never free any extents in
4193 * the first transaction. This is to allow the caller to make the first
4194 * transaction a synchronous one so that the pointers to the data being
4195 * broken in this transaction will be permanent before the data is actually
4196 * freed. This is necessary to prevent blocks from being reallocated
4197 * and written to before the free and reallocation are actually permanent.
4198 * We do not just make the first transaction synchronous here, because
4199 * there are more efficient ways to gain the same protection in some cases
4200 * (see the file truncation code).
4201 *
4202 * Return 1 if the given transaction was committed and a new one
4203 * started, and 0 otherwise in the committed parameter.
4204 */
4205/*ARGSUSED*/
4206int /* error */
4207xfs_bmap_finish(
4208 xfs_trans_t **tp, /* transaction pointer addr */
4209 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4210 xfs_fsblock_t firstblock, /* controlled ag for allocs */
4211 int *committed) /* xact committed or not */
4212{
4213 xfs_efd_log_item_t *efd; /* extent free data */
4214 xfs_efi_log_item_t *efi; /* extent free intention */
4215 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004216 xfs_bmap_free_item_t *free; /* free extent item */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 unsigned int logres; /* new log reservation */
4218 unsigned int logcount; /* new log count */
4219 xfs_mount_t *mp; /* filesystem mount structure */
4220 xfs_bmap_free_item_t *next; /* next item on free list */
4221 xfs_trans_t *ntp; /* new transaction pointer */
4222
4223 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
4224 if (flist->xbf_count == 0) {
4225 *committed = 0;
4226 return 0;
4227 }
4228 ntp = *tp;
4229 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
4230 for (free = flist->xbf_first; free; free = free->xbfi_next)
4231 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
4232 free->xbfi_blockcount);
4233 logres = ntp->t_log_res;
4234 logcount = ntp->t_log_count;
4235 ntp = xfs_trans_dup(*tp);
4236 error = xfs_trans_commit(*tp, 0, NULL);
4237 *tp = ntp;
4238 *committed = 1;
4239 /*
4240 * We have a new transaction, so we should return committed=1,
4241 * even though we're returning an error.
4242 */
4243 if (error) {
4244 return error;
4245 }
4246 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
4247 logcount)))
4248 return error;
4249 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
4250 for (free = flist->xbf_first; free != NULL; free = next) {
4251 next = free->xbfi_next;
4252 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
4253 free->xbfi_blockcount))) {
4254 /*
4255 * The bmap free list will be cleaned up at a
4256 * higher level. The EFI will be canceled when
4257 * this transaction is aborted.
4258 * Need to force shutdown here to make sure it
4259 * happens, since this transaction may not be
4260 * dirty yet.
4261 */
4262 mp = ntp->t_mountp;
4263 if (!XFS_FORCED_SHUTDOWN(mp))
4264 xfs_force_shutdown(mp,
4265 (error == EFSCORRUPTED) ?
Nathan Scott7d04a332006-06-09 14:58:38 +10004266 SHUTDOWN_CORRUPT_INCORE :
4267 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 return error;
4269 }
4270 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
4271 free->xbfi_blockcount);
4272 xfs_bmap_del_free(flist, NULL, free);
4273 }
4274 return 0;
4275}
4276
4277/*
4278 * Free up any items left in the list.
4279 */
4280void
4281xfs_bmap_cancel(
4282 xfs_bmap_free_t *flist) /* list of bmap_free_items */
4283{
4284 xfs_bmap_free_item_t *free; /* free list item */
4285 xfs_bmap_free_item_t *next;
4286
4287 if (flist->xbf_count == 0)
4288 return;
4289 ASSERT(flist->xbf_first != NULL);
4290 for (free = flist->xbf_first; free; free = next) {
4291 next = free->xbfi_next;
4292 xfs_bmap_del_free(flist, NULL, free);
4293 }
4294 ASSERT(flist->xbf_count == 0);
4295}
4296
4297/*
4298 * Returns the file-relative block number of the first unused block(s)
4299 * in the file with at least "len" logically contiguous blocks free.
4300 * This is the lowest-address hole if the file has holes, else the first block
4301 * past the end of file.
4302 * Return 0 if the file is currently local (in-inode).
4303 */
4304int /* error */
4305xfs_bmap_first_unused(
4306 xfs_trans_t *tp, /* transaction pointer */
4307 xfs_inode_t *ip, /* incore inode */
4308 xfs_extlen_t len, /* size of hole to find */
4309 xfs_fileoff_t *first_unused, /* unused block */
4310 int whichfork) /* data or attr fork */
4311{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
4313 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004314 int idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 xfs_ifork_t *ifp; /* inode fork pointer */
4316 xfs_fileoff_t lastaddr; /* last block number seen */
4317 xfs_fileoff_t lowest; /* lowest useful block */
4318 xfs_fileoff_t max; /* starting useful block */
4319 xfs_fileoff_t off; /* offset for this block */
4320 xfs_extnum_t nextents; /* number of extent entries */
4321
4322 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
4323 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
4324 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
4325 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4326 *first_unused = 0;
4327 return 0;
4328 }
4329 ifp = XFS_IFORK_PTR(ip, whichfork);
4330 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4331 (error = xfs_iread_extents(tp, ip, whichfork)))
4332 return error;
4333 lowest = *first_unused;
4334 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004335 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
4336 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 off = xfs_bmbt_get_startoff(ep);
4338 /*
4339 * See if the hole before this extent will work.
4340 */
4341 if (off >= lowest + len && off - max >= len) {
4342 *first_unused = max;
4343 return 0;
4344 }
4345 lastaddr = off + xfs_bmbt_get_blockcount(ep);
4346 max = XFS_FILEOFF_MAX(lastaddr, lowest);
4347 }
4348 *first_unused = max;
4349 return 0;
4350}
4351
4352/*
4353 * Returns the file-relative block number of the last block + 1 before
4354 * last_block (input value) in the file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004355 * This is not based on i_size, it is based on the extent records.
4356 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 */
4358int /* error */
4359xfs_bmap_last_before(
4360 xfs_trans_t *tp, /* transaction pointer */
4361 xfs_inode_t *ip, /* incore inode */
4362 xfs_fileoff_t *last_block, /* last block */
4363 int whichfork) /* data or attr fork */
4364{
4365 xfs_fileoff_t bno; /* input file offset */
4366 int eof; /* hit end of file */
4367 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4368 int error; /* error return value */
4369 xfs_bmbt_irec_t got; /* current extent value */
4370 xfs_ifork_t *ifp; /* inode fork pointer */
4371 xfs_extnum_t lastx; /* last extent used */
4372 xfs_bmbt_irec_t prev; /* previous extent value */
4373
4374 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4375 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4376 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4377 return XFS_ERROR(EIO);
4378 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4379 *last_block = 0;
4380 return 0;
4381 }
4382 ifp = XFS_IFORK_PTR(ip, whichfork);
4383 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4384 (error = xfs_iread_extents(tp, ip, whichfork)))
4385 return error;
4386 bno = *last_block - 1;
4387 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4388 &prev);
4389 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
4390 if (prev.br_startoff == NULLFILEOFF)
4391 *last_block = 0;
4392 else
4393 *last_block = prev.br_startoff + prev.br_blockcount;
4394 }
4395 /*
4396 * Otherwise *last_block is already the right answer.
4397 */
4398 return 0;
4399}
4400
4401/*
4402 * Returns the file-relative block number of the first block past eof in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004403 * the file. This is not based on i_size, it is based on the extent records.
4404 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 */
4406int /* error */
4407xfs_bmap_last_offset(
4408 xfs_trans_t *tp, /* transaction pointer */
4409 xfs_inode_t *ip, /* incore inode */
4410 xfs_fileoff_t *last_block, /* last block */
4411 int whichfork) /* data or attr fork */
4412{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 xfs_bmbt_rec_t *ep; /* pointer to last extent */
4414 int error; /* error return value */
4415 xfs_ifork_t *ifp; /* inode fork pointer */
4416 xfs_extnum_t nextents; /* number of extent entries */
4417
4418 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4419 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4420 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
4421 return XFS_ERROR(EIO);
4422 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4423 *last_block = 0;
4424 return 0;
4425 }
4426 ifp = XFS_IFORK_PTR(ip, whichfork);
4427 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4428 (error = xfs_iread_extents(tp, ip, whichfork)))
4429 return error;
4430 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4431 if (!nextents) {
4432 *last_block = 0;
4433 return 0;
4434 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004435 ep = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
4437 return 0;
4438}
4439
4440/*
4441 * Returns whether the selected fork of the inode has exactly one
4442 * block or not. For the data fork we check this matches di_size,
4443 * implying the file's range is 0..bsize-1.
4444 */
4445int /* 1=>1 block, 0=>otherwise */
4446xfs_bmap_one_block(
4447 xfs_inode_t *ip, /* incore inode */
4448 int whichfork) /* data or attr fork */
4449{
4450 xfs_bmbt_rec_t *ep; /* ptr to fork's extent */
4451 xfs_ifork_t *ifp; /* inode fork pointer */
4452 int rval; /* return value */
4453 xfs_bmbt_irec_t s; /* internal version of extent */
4454
4455#ifndef DEBUG
4456 if (whichfork == XFS_DATA_FORK)
4457 return ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize;
4458#endif /* !DEBUG */
4459 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4460 return 0;
4461 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4462 return 0;
4463 ifp = XFS_IFORK_PTR(ip, whichfork);
4464 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004465 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 xfs_bmbt_get_all(ep, &s);
4467 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4468 if (rval && whichfork == XFS_DATA_FORK)
4469 ASSERT(ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
4470 return rval;
4471}
4472
4473/*
4474 * Read in the extents to if_extents.
4475 * All inode fields are set up by caller, we just traverse the btree
4476 * and copy the records in. If the file system cannot contain unwritten
4477 * extents, the records are checked for no "state" flags.
4478 */
4479int /* error */
4480xfs_bmap_read_extents(
4481 xfs_trans_t *tp, /* transaction pointer */
4482 xfs_inode_t *ip, /* incore inode */
4483 int whichfork) /* data or attr fork */
4484{
4485 xfs_bmbt_block_t *block; /* current btree block */
4486 xfs_fsblock_t bno; /* block # of "block" */
4487 xfs_buf_t *bp; /* buffer for "block" */
4488 int error; /* error return value */
4489 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
4490#ifdef XFS_BMAP_TRACE
4491 static char fname[] = "xfs_bmap_read_extents";
4492#endif
4493 xfs_extnum_t i, j; /* index into the extents list */
4494 xfs_ifork_t *ifp; /* fork structure */
4495 int level; /* btree level, for checking */
4496 xfs_mount_t *mp; /* file system mount structure */
4497 xfs_bmbt_ptr_t *pp; /* pointer to block address */
4498 /* REFERENCED */
4499 xfs_extnum_t room; /* number of entries there's room for */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
4501 bno = NULLFSBLOCK;
4502 mp = ip->i_mount;
4503 ifp = XFS_IFORK_PTR(ip, whichfork);
4504 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4505 XFS_EXTFMT_INODE(ip);
4506 block = ifp->if_broot;
4507 /*
4508 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4509 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004510 level = be16_to_cpu(block->bb_level);
4511 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
4513 ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
4514 ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
4515 ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
4516 bno = INT_GET(*pp, ARCH_CONVERT);
4517 /*
4518 * Go down the tree until leaf level is reached, following the first
4519 * pointer (leftmost) at each level.
4520 */
4521 while (level-- > 0) {
4522 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4523 XFS_BMAP_BTREE_REF)))
4524 return error;
4525 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4526 XFS_WANT_CORRUPTED_GOTO(
4527 XFS_BMAP_SANITY_CHECK(mp, block, level),
4528 error0);
4529 if (level == 0)
4530 break;
4531 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
4532 1, mp->m_bmap_dmxr[1]);
4533 XFS_WANT_CORRUPTED_GOTO(
4534 XFS_FSB_SANITY_CHECK(mp, INT_GET(*pp, ARCH_CONVERT)),
4535 error0);
4536 bno = INT_GET(*pp, ARCH_CONVERT);
4537 xfs_trans_brelse(tp, bp);
4538 }
4539 /*
4540 * Here with bp and block set to the leftmost leaf node in the tree.
4541 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004542 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 i = 0;
4544 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004545 * Loop over all leaf nodes. Copy information to the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 */
4547 for (;;) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004548 xfs_bmbt_rec_t *frp, *trp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 xfs_fsblock_t nextbno;
4550 xfs_extnum_t num_recs;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004551 xfs_extnum_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552
4553
Christoph Hellwig16259e72005-11-02 15:11:25 +11004554 num_recs = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 if (unlikely(i + num_recs > room)) {
4556 ASSERT(i + num_recs <= room);
Nathan Scott3762ec62006-01-12 10:29:53 +11004557 xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
4558 "corrupt dinode %Lu, (btree extents).",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 (unsigned long long) ip->i_ino);
4560 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
4561 XFS_ERRLEVEL_LOW,
4562 ip->i_mount);
4563 goto error0;
4564 }
4565 XFS_WANT_CORRUPTED_GOTO(
4566 XFS_BMAP_SANITY_CHECK(mp, block, 0),
4567 error0);
4568 /*
4569 * Read-ahead the next leaf block, if any.
4570 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004571 nextbno = be64_to_cpu(block->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 if (nextbno != NULLFSBLOCK)
4573 xfs_btree_reada_bufl(mp, nextbno, 1);
4574 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004575 * Copy records into the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 */
4577 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
4578 block, 1, mp->m_bmap_dmxr[0]);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004579 start = i;
4580 for (j = 0; j < num_recs; j++, i++, frp++) {
4581 trp = xfs_iext_get_ext(ifp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 trp->l0 = INT_GET(frp->l0, ARCH_CONVERT);
4583 trp->l1 = INT_GET(frp->l1, ARCH_CONVERT);
4584 }
4585 if (exntf == XFS_EXTFMT_NOSTATE) {
4586 /*
4587 * Check all attribute bmap btree records and
4588 * any "older" data bmap btree records for a
4589 * set bit in the "extent flag" position.
4590 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004591 if (unlikely(xfs_check_nostate_extents(ifp,
4592 start, num_recs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4594 XFS_ERRLEVEL_LOW,
4595 ip->i_mount);
4596 goto error0;
4597 }
4598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 xfs_trans_brelse(tp, bp);
4600 bno = nextbno;
4601 /*
4602 * If we've reached the end, stop.
4603 */
4604 if (bno == NULLFSBLOCK)
4605 break;
4606 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4607 XFS_BMAP_BTREE_REF)))
4608 return error;
4609 block = XFS_BUF_TO_BMBT_BLOCK(bp);
4610 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004611 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4613 xfs_bmap_trace_exlist(fname, ip, i, whichfork);
4614 return 0;
4615error0:
4616 xfs_trans_brelse(tp, bp);
4617 return XFS_ERROR(EFSCORRUPTED);
4618}
4619
4620#ifdef XFS_BMAP_TRACE
4621/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004622 * Add bmap trace insert entries for all the contents of the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 */
4624void
4625xfs_bmap_trace_exlist(
4626 char *fname, /* function name */
4627 xfs_inode_t *ip, /* incore inode pointer */
4628 xfs_extnum_t cnt, /* count of entries in the list */
4629 int whichfork) /* data or attr fork */
4630{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004631 xfs_bmbt_rec_t *ep; /* current extent record */
4632 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 xfs_ifork_t *ifp; /* inode fork pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004634 xfs_bmbt_irec_t s; /* file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635
4636 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004637 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4638 for (idx = 0; idx < cnt; idx++) {
4639 ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 xfs_bmbt_get_all(ep, &s);
4641 xfs_bmap_trace_insert(fname, "exlist", ip, idx, 1, &s, NULL,
4642 whichfork);
4643 }
4644}
4645#endif
4646
4647#ifdef DEBUG
4648/*
4649 * Validate that the bmbt_irecs being returned from bmapi are valid
4650 * given the callers original parameters. Specifically check the
4651 * ranges of the returned irecs to ensure that they only extent beyond
4652 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4653 */
4654STATIC void
4655xfs_bmap_validate_ret(
4656 xfs_fileoff_t bno,
4657 xfs_filblks_t len,
4658 int flags,
4659 xfs_bmbt_irec_t *mval,
4660 int nmap,
4661 int ret_nmap)
4662{
4663 int i; /* index to map values */
4664
4665 ASSERT(ret_nmap <= nmap);
4666
4667 for (i = 0; i < ret_nmap; i++) {
4668 ASSERT(mval[i].br_blockcount > 0);
4669 if (!(flags & XFS_BMAPI_ENTIRE)) {
4670 ASSERT(mval[i].br_startoff >= bno);
4671 ASSERT(mval[i].br_blockcount <= len);
4672 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4673 bno + len);
4674 } else {
4675 ASSERT(mval[i].br_startoff < bno + len);
4676 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4677 bno);
4678 }
4679 ASSERT(i == 0 ||
4680 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4681 mval[i].br_startoff);
4682 if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY))
4683 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4684 mval[i].br_startblock != HOLESTARTBLOCK);
4685 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4686 mval[i].br_state == XFS_EXT_UNWRITTEN);
4687 }
4688}
4689#endif /* DEBUG */
4690
4691
4692/*
4693 * Map file blocks to filesystem blocks.
4694 * File range is given by the bno/len pair.
4695 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
4696 * into a hole or past eof.
4697 * Only allocates blocks from a single allocation group,
4698 * to avoid locking problems.
4699 * The returned value in "firstblock" from the first call in a transaction
4700 * must be remembered and presented to subsequent calls in "firstblock".
4701 * An upper bound for the number of blocks to be allocated is supplied to
4702 * the first call in "total"; if no allocation group has that many free
4703 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4704 */
4705int /* error */
4706xfs_bmapi(
4707 xfs_trans_t *tp, /* transaction pointer */
4708 xfs_inode_t *ip, /* incore inode */
4709 xfs_fileoff_t bno, /* starting file offs. mapped */
4710 xfs_filblks_t len, /* length to map in file */
4711 int flags, /* XFS_BMAPI_... */
4712 xfs_fsblock_t *firstblock, /* first allocated block
4713 controls a.g. for allocs */
4714 xfs_extlen_t total, /* total blocks needed */
4715 xfs_bmbt_irec_t *mval, /* output: map values */
4716 int *nmap, /* i/o: mval size/count */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004717 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4718 xfs_extdelta_t *delta) /* o: change made to incore extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719{
4720 xfs_fsblock_t abno; /* allocated block number */
4721 xfs_extlen_t alen; /* allocated extent length */
4722 xfs_fileoff_t aoff; /* allocated file offset */
4723 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 xfs_btree_cur_t *cur; /* bmap btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 xfs_fileoff_t end; /* end of mapped file region */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004726 int eof; /* we've hit the end of extents */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004727 xfs_bmbt_rec_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004729 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 xfs_ifork_t *ifp; /* inode fork pointer */
4731 xfs_extlen_t indlen; /* indirect blocks length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 xfs_extnum_t lastx; /* last useful extent number */
4733 int logflags; /* flags for transaction logging */
4734 xfs_extlen_t minleft; /* min blocks left after allocation */
4735 xfs_extlen_t minlen; /* min allocation size */
4736 xfs_mount_t *mp; /* xfs mount structure */
4737 int n; /* current extent index */
4738 int nallocs; /* number of extents alloc\'d */
4739 xfs_extnum_t nextents; /* number of extents in file */
4740 xfs_fileoff_t obno; /* old block number (offset) */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004741 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 int tmp_logflags; /* temp flags holder */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004743 int whichfork; /* data or attr fork */
4744 char inhole; /* current location is hole in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 char wasdelay; /* old extent was delayed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 char wr; /* this is a write request */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004747 char rt; /* this is a realtime file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748#ifdef DEBUG
4749 xfs_fileoff_t orig_bno; /* original block number value */
4750 int orig_flags; /* original flags arg value */
4751 xfs_filblks_t orig_len; /* original value of len arg */
4752 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
4753 int orig_nmap; /* original value of *nmap */
4754
4755 orig_bno = bno;
4756 orig_len = len;
4757 orig_flags = flags;
4758 orig_mval = mval;
4759 orig_nmap = *nmap;
4760#endif
4761 ASSERT(*nmap >= 1);
4762 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
4763 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4764 XFS_ATTR_FORK : XFS_DATA_FORK;
4765 mp = ip->i_mount;
4766 if (unlikely(XFS_TEST_ERROR(
4767 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4768 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4769 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4770 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4771 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
4772 return XFS_ERROR(EFSCORRUPTED);
4773 }
4774 if (XFS_FORCED_SHUTDOWN(mp))
4775 return XFS_ERROR(EIO);
Nathan Scottdd9f4382006-01-11 15:28:28 +11004776 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 ifp = XFS_IFORK_PTR(ip, whichfork);
4778 ASSERT(ifp->if_ext_max ==
4779 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4780 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
4781 XFS_STATS_INC(xs_blk_mapw);
4782 else
4783 XFS_STATS_INC(xs_blk_mapr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 /*
Nathan Scott39269e22006-03-14 13:33:50 +11004785 * IGSTATE flag is used to combine extents which
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 * differ only due to the state of the extents.
4787 * This technique is used from xfs_getbmap()
4788 * when the caller does not wish to see the
4789 * separation (which is the default).
4790 *
4791 * This technique is also used when writing a
4792 * buffer which has been partially written,
4793 * (usually by being flushed during a chunkread),
4794 * to ensure one write takes place. This also
4795 * prevents a change in the xfs inode extents at
4796 * this time, intentionally. This change occurs
4797 * on completion of the write operation, in
4798 * xfs_strat_comp(), where the xfs_bmapi() call
4799 * is transactioned, and the extents combined.
4800 */
Nathan Scott39269e22006-03-14 13:33:50 +11004801 if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */
4802 wr = 0; /* no allocations are allowed */
4803 ASSERT(wr || !(flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 logflags = 0;
4805 nallocs = 0;
4806 cur = NULL;
4807 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4808 ASSERT(wr && tp);
4809 if ((error = xfs_bmap_local_to_extents(tp, ip,
4810 firstblock, total, &logflags, whichfork)))
4811 goto error0;
4812 }
4813 if (wr && *firstblock == NULLFSBLOCK) {
4814 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Christoph Hellwig16259e72005-11-02 15:11:25 +11004815 minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 else
4817 minleft = 1;
4818 } else
4819 minleft = 0;
4820 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4821 (error = xfs_iread_extents(tp, ip, whichfork)))
4822 goto error0;
4823 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4824 &prev);
4825 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4826 n = 0;
4827 end = bno + len;
4828 obno = bno;
4829 bma.ip = NULL;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10004830 if (delta) {
4831 delta->xed_startoff = NULLFILEOFF;
4832 delta->xed_blockcount = 0;
4833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 while (bno < end && n < *nmap) {
4835 /*
4836 * Reading past eof, act as though there's a hole
4837 * up to end.
4838 */
4839 if (eof && !wr)
4840 got.br_startoff = end;
4841 inhole = eof || got.br_startoff > bno;
Nathan Scott39269e22006-03-14 13:33:50 +11004842 wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 ISNULLSTARTBLOCK(got.br_startblock);
4844 /*
4845 * First, deal with the hole before the allocated space
4846 * that we found, if any.
4847 */
4848 if (wr && (inhole || wasdelay)) {
4849 /*
4850 * For the wasdelay case, we could also just
4851 * allocate the stuff asked for in this bmap call
4852 * but that wouldn't be as good.
4853 */
Nathan Scott39269e22006-03-14 13:33:50 +11004854 if (wasdelay && !(flags & XFS_BMAPI_EXACT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 alen = (xfs_extlen_t)got.br_blockcount;
4856 aoff = got.br_startoff;
4857 if (lastx != NULLEXTNUM && lastx) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004858 ep = xfs_iext_get_ext(ifp, lastx - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 xfs_bmbt_get_all(ep, &prev);
4860 }
4861 } else if (wasdelay) {
4862 alen = (xfs_extlen_t)
4863 XFS_FILBLKS_MIN(len,
4864 (got.br_startoff +
4865 got.br_blockcount) - bno);
4866 aoff = bno;
4867 } else {
4868 alen = (xfs_extlen_t)
4869 XFS_FILBLKS_MIN(len, MAXEXTLEN);
4870 if (!eof)
4871 alen = (xfs_extlen_t)
4872 XFS_FILBLKS_MIN(alen,
4873 got.br_startoff - bno);
4874 aoff = bno;
4875 }
Nathan Scott39269e22006-03-14 13:33:50 +11004876 minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1;
4877 if (flags & XFS_BMAPI_DELAY) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11004878 xfs_extlen_t extsz;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004879
4880 /* Figure out the extent size, adjust alen */
4881 if (rt) {
4882 if (!(extsz = ip->i_d.di_extsize))
4883 extsz = mp->m_sb.sb_rextsize;
Nathan Scottdd9f4382006-01-11 15:28:28 +11004884 } else {
4885 extsz = ip->i_d.di_extsize;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004886 }
Nathan Scottdd9f4382006-01-11 15:28:28 +11004887 if (extsz) {
4888 error = xfs_bmap_extsize_align(mp,
4889 &got, &prev, extsz,
Nathan Scott39269e22006-03-14 13:33:50 +11004890 rt, eof,
4891 flags&XFS_BMAPI_DELAY,
4892 flags&XFS_BMAPI_CONVERT,
Nathan Scottdd9f4382006-01-11 15:28:28 +11004893 &aoff, &alen);
4894 ASSERT(!error);
4895 }
4896
4897 if (rt)
4898 extsz = alen / mp->m_sb.sb_rextsize;
Nathan Scott06d10dd2005-06-21 15:48:47 +10004899
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 /*
4901 * Make a transaction-less quota reservation for
4902 * delayed allocation blocks. This number gets
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004903 * adjusted later. We return if we haven't
4904 * allocated blocks already inside this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 */
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004906 if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS(
Nathan Scott06d10dd2005-06-21 15:48:47 +10004907 mp, NULL, ip, (long)alen, 0,
4908 rt ? XFS_QMOPT_RES_RTBLKS :
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004909 XFS_QMOPT_RES_REGBLKS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 if (n == 0) {
4911 *nmap = 0;
4912 ASSERT(cur == NULL);
Nathan Scott9a2a7de2006-03-31 13:04:49 +10004913 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 }
4915 break;
4916 }
4917
4918 /*
4919 * Split changing sb for alen and indlen since
4920 * they could be coming from different places.
4921 */
Nathan Scott06d10dd2005-06-21 15:48:47 +10004922 indlen = (xfs_extlen_t)
4923 xfs_bmap_worst_indlen(ip, alen);
4924 ASSERT(indlen > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
Nathan Scottdd9f4382006-01-11 15:28:28 +11004926 if (rt) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004927 error = xfs_mod_incore_sb(mp,
4928 XFS_SBS_FREXTENTS,
Nathan Scott39269e22006-03-14 13:33:50 +11004929 -(extsz), (flags &
4930 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004931 } else {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004932 error = xfs_mod_incore_sb(mp,
4933 XFS_SBS_FDBLOCKS,
Nathan Scott39269e22006-03-14 13:33:50 +11004934 -(alen), (flags &
4935 XFS_BMAPI_RSVBLOCKS));
Nathan Scottdd9f4382006-01-11 15:28:28 +11004936 }
David Chinner3bdbfb12005-09-02 16:40:47 +10004937 if (!error) {
Nathan Scott06d10dd2005-06-21 15:48:47 +10004938 error = xfs_mod_incore_sb(mp,
4939 XFS_SBS_FDBLOCKS,
Nathan Scott39269e22006-03-14 13:33:50 +11004940 -(indlen), (flags &
4941 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004942 if (error && rt)
4943 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004944 XFS_SBS_FREXTENTS,
Nathan Scott39269e22006-03-14 13:33:50 +11004945 extsz, (flags &
4946 XFS_BMAPI_RSVBLOCKS));
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004947 else if (error)
4948 xfs_mod_incore_sb(mp,
David Chinner3bdbfb12005-09-02 16:40:47 +10004949 XFS_SBS_FDBLOCKS,
Nathan Scott39269e22006-03-14 13:33:50 +11004950 alen, (flags &
4951 XFS_BMAPI_RSVBLOCKS));
David Chinner3bdbfb12005-09-02 16:40:47 +10004952 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004953
4954 if (error) {
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11004955 if (XFS_IS_QUOTA_ON(mp))
Nathan Scott06d10dd2005-06-21 15:48:47 +10004956 /* unreserve the blocks now */
Nathan Scottdd9f4382006-01-11 15:28:28 +11004957 (void)
Nathan Scott06d10dd2005-06-21 15:48:47 +10004958 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(
4959 mp, NULL, ip,
4960 (long)alen, 0, rt ?
4961 XFS_QMOPT_RES_RTBLKS :
4962 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 break;
4964 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10004965
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 ip->i_delayed_blks += alen;
4967 abno = NULLSTARTBLOCK(indlen);
4968 } else {
4969 /*
4970 * If first time, allocate and fill in
4971 * once-only bma fields.
4972 */
4973 if (bma.ip == NULL) {
4974 bma.tp = tp;
4975 bma.ip = ip;
4976 bma.prevp = &prev;
4977 bma.gotp = &got;
4978 bma.total = total;
4979 bma.userdata = 0;
4980 }
4981 /* Indicate if this is the first user data
4982 * in the file, or just any user data.
4983 */
Nathan Scott39269e22006-03-14 13:33:50 +11004984 if (!(flags & XFS_BMAPI_METADATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 bma.userdata = (aoff == 0) ?
4986 XFS_ALLOC_INITIAL_USER_DATA :
4987 XFS_ALLOC_USERDATA;
4988 }
4989 /*
4990 * Fill in changeable bma fields.
4991 */
4992 bma.eof = eof;
4993 bma.firstblock = *firstblock;
4994 bma.alen = alen;
4995 bma.off = aoff;
Adrian Bunk72880262006-08-30 13:41:58 +10004996 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 bma.wasdel = wasdelay;
4998 bma.minlen = minlen;
4999 bma.low = flist->xbf_low;
5000 bma.minleft = minleft;
5001 /*
5002 * Only want to do the alignment at the
5003 * eof if it is userdata and allocation length
5004 * is larger than a stripe unit.
5005 */
5006 if (mp->m_dalign && alen >= mp->m_dalign &&
Nathan Scott39269e22006-03-14 13:33:50 +11005007 (!(flags & XFS_BMAPI_METADATA)) &&
5008 (whichfork == XFS_DATA_FORK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 if ((error = xfs_bmap_isaeof(ip, aoff,
5010 whichfork, &bma.aeof)))
5011 goto error0;
5012 } else
5013 bma.aeof = 0;
5014 /*
5015 * Call allocator.
5016 */
5017 if ((error = xfs_bmap_alloc(&bma)))
5018 goto error0;
5019 /*
5020 * Copy out result fields.
5021 */
5022 abno = bma.rval;
5023 if ((flist->xbf_low = bma.low))
5024 minleft = 0;
5025 alen = bma.alen;
5026 aoff = bma.off;
5027 ASSERT(*firstblock == NULLFSBLOCK ||
5028 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5029 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
5030 (flist->xbf_low &&
5031 XFS_FSB_TO_AGNO(mp, *firstblock) <
5032 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
5033 *firstblock = bma.firstblock;
5034 if (cur)
5035 cur->bc_private.b.firstblock =
5036 *firstblock;
5037 if (abno == NULLFSBLOCK)
5038 break;
5039 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5040 cur = xfs_btree_init_cursor(mp,
5041 tp, NULL, 0, XFS_BTNUM_BMAP,
5042 ip, whichfork);
5043 cur->bc_private.b.firstblock =
5044 *firstblock;
5045 cur->bc_private.b.flist = flist;
5046 }
5047 /*
5048 * Bump the number of extents we've allocated
5049 * in this call.
5050 */
5051 nallocs++;
5052 }
5053 if (cur)
5054 cur->bc_private.b.flags =
5055 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
5056 got.br_startoff = aoff;
5057 got.br_startblock = abno;
5058 got.br_blockcount = alen;
5059 got.br_state = XFS_EXT_NORM; /* assume normal */
5060 /*
5061 * Determine state of extent, and the filesystem.
5062 * A wasdelay extent has been initialized, so
5063 * shouldn't be flagged as unwritten.
5064 */
5065 if (wr && XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5066 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
5067 got.br_state = XFS_EXT_UNWRITTEN;
5068 }
5069 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005070 firstblock, flist, &tmp_logflags, delta,
5071 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 logflags |= tmp_logflags;
5073 if (error)
5074 goto error0;
5075 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005076 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5078 xfs_bmbt_get_all(ep, &got);
5079 ASSERT(got.br_startoff <= aoff);
5080 ASSERT(got.br_startoff + got.br_blockcount >=
5081 aoff + alen);
5082#ifdef DEBUG
Nathan Scott39269e22006-03-14 13:33:50 +11005083 if (flags & XFS_BMAPI_DELAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
5085 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
5086 }
5087 ASSERT(got.br_state == XFS_EXT_NORM ||
5088 got.br_state == XFS_EXT_UNWRITTEN);
5089#endif
5090 /*
5091 * Fall down into the found allocated space case.
5092 */
5093 } else if (inhole) {
5094 /*
5095 * Reading in a hole.
5096 */
5097 mval->br_startoff = bno;
5098 mval->br_startblock = HOLESTARTBLOCK;
5099 mval->br_blockcount =
5100 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
5101 mval->br_state = XFS_EXT_NORM;
5102 bno += mval->br_blockcount;
5103 len -= mval->br_blockcount;
5104 mval++;
5105 n++;
5106 continue;
5107 }
5108 /*
5109 * Then deal with the allocated space we found.
5110 */
5111 ASSERT(ep != NULL);
Nathan Scott39269e22006-03-14 13:33:50 +11005112 if (!(flags & XFS_BMAPI_ENTIRE) &&
5113 (got.br_startoff + got.br_blockcount > obno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 if (obno > bno)
5115 bno = obno;
5116 ASSERT((bno >= obno) || (n == 0));
5117 ASSERT(bno < end);
5118 mval->br_startoff = bno;
5119 if (ISNULLSTARTBLOCK(got.br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11005120 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 mval->br_startblock = DELAYSTARTBLOCK;
5122 } else
5123 mval->br_startblock =
5124 got.br_startblock +
5125 (bno - got.br_startoff);
5126 /*
5127 * Return the minimum of what we got and what we
5128 * asked for for the length. We can use the len
5129 * variable here because it is modified below
5130 * and we could have been there before coming
5131 * here if the first part of the allocation
5132 * didn't overlap what was asked for.
5133 */
5134 mval->br_blockcount =
5135 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
5136 (bno - got.br_startoff));
5137 mval->br_state = got.br_state;
5138 ASSERT(mval->br_blockcount <= len);
5139 } else {
5140 *mval = got;
5141 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
Nathan Scott39269e22006-03-14 13:33:50 +11005142 ASSERT(!wr || (flags & XFS_BMAPI_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 mval->br_startblock = DELAYSTARTBLOCK;
5144 }
5145 }
5146
5147 /*
5148 * Check if writing previously allocated but
5149 * unwritten extents.
5150 */
5151 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
5152 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
5153 /*
5154 * Modify (by adding) the state flag, if writing.
5155 */
5156 ASSERT(mval->br_blockcount <= len);
5157 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
5158 cur = xfs_btree_init_cursor(mp,
5159 tp, NULL, 0, XFS_BTNUM_BMAP,
5160 ip, whichfork);
5161 cur->bc_private.b.firstblock =
5162 *firstblock;
5163 cur->bc_private.b.flist = flist;
5164 }
5165 mval->br_state = XFS_EXT_NORM;
5166 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005167 firstblock, flist, &tmp_logflags, delta,
5168 whichfork, (flags & XFS_BMAPI_RSVBLOCKS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 logflags |= tmp_logflags;
5170 if (error)
5171 goto error0;
5172 lastx = ifp->if_lastex;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005173 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5175 xfs_bmbt_get_all(ep, &got);
5176 /*
5177 * We may have combined previously unwritten
5178 * space with written space, so generate
5179 * another request.
5180 */
5181 if (mval->br_blockcount < len)
5182 continue;
5183 }
5184
Nathan Scott39269e22006-03-14 13:33:50 +11005185 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 ((mval->br_startoff + mval->br_blockcount) <= end));
Nathan Scott39269e22006-03-14 13:33:50 +11005187 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
5188 (mval->br_blockcount <= len) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 (mval->br_startoff < obno));
5190 bno = mval->br_startoff + mval->br_blockcount;
5191 len = end - bno;
5192 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
5193 ASSERT(mval->br_startblock == mval[-1].br_startblock);
5194 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
5195 ASSERT(mval->br_state == mval[-1].br_state);
5196 mval[-1].br_blockcount = mval->br_blockcount;
5197 mval[-1].br_state = mval->br_state;
5198 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
5199 mval[-1].br_startblock != DELAYSTARTBLOCK &&
5200 mval[-1].br_startblock != HOLESTARTBLOCK &&
5201 mval->br_startblock ==
5202 mval[-1].br_startblock + mval[-1].br_blockcount &&
Nathan Scott39269e22006-03-14 13:33:50 +11005203 ((flags & XFS_BMAPI_IGSTATE) ||
5204 mval[-1].br_state == mval->br_state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 ASSERT(mval->br_startoff ==
5206 mval[-1].br_startoff + mval[-1].br_blockcount);
5207 mval[-1].br_blockcount += mval->br_blockcount;
5208 } else if (n > 0 &&
5209 mval->br_startblock == DELAYSTARTBLOCK &&
5210 mval[-1].br_startblock == DELAYSTARTBLOCK &&
5211 mval->br_startoff ==
5212 mval[-1].br_startoff + mval[-1].br_blockcount) {
5213 mval[-1].br_blockcount += mval->br_blockcount;
5214 mval[-1].br_state = mval->br_state;
5215 } else if (!((n == 0) &&
5216 ((mval->br_startoff + mval->br_blockcount) <=
5217 obno))) {
5218 mval++;
5219 n++;
5220 }
5221 /*
5222 * If we're done, stop now. Stop when we've allocated
5223 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5224 * the transaction may get too big.
5225 */
5226 if (bno >= end || n >= *nmap || nallocs >= *nmap)
5227 break;
5228 /*
5229 * Else go on to the next record.
5230 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005231 ep = xfs_iext_get_ext(ifp, ++lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 if (lastx >= nextents) {
5233 eof = 1;
5234 prev = got;
5235 } else
5236 xfs_bmbt_get_all(ep, &got);
5237 }
5238 ifp->if_lastex = lastx;
5239 *nmap = n;
5240 /*
5241 * Transform from btree to extents, give it cur.
5242 */
5243 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5244 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5245 ASSERT(wr && cur);
5246 error = xfs_bmap_btree_to_extents(tp, ip, cur,
5247 &tmp_logflags, whichfork);
5248 logflags |= tmp_logflags;
5249 if (error)
5250 goto error0;
5251 }
5252 ASSERT(ifp->if_ext_max ==
5253 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5254 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5255 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
5256 error = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005257 if (delta && delta->xed_startoff != NULLFILEOFF) {
5258 /* A change was actually made.
5259 * Note that delta->xed_blockount is an offset at this
5260 * point and needs to be converted to a block count.
5261 */
5262 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5263 delta->xed_blockcount -= delta->xed_startoff;
5264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265error0:
5266 /*
5267 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005268 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 */
5270 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5271 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5272 logflags &= ~XFS_ILOG_FEXT(whichfork);
5273 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5274 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5275 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5276 /*
5277 * Log whatever the flags say, even if error. Otherwise we might miss
5278 * detecting a case where the data is changed, there's an error,
5279 * and it's not logged so we don't shutdown when we should.
5280 */
5281 if (logflags) {
5282 ASSERT(tp && wr);
5283 xfs_trans_log_inode(tp, ip, logflags);
5284 }
5285 if (cur) {
5286 if (!error) {
5287 ASSERT(*firstblock == NULLFSBLOCK ||
5288 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5289 XFS_FSB_TO_AGNO(mp,
5290 cur->bc_private.b.firstblock) ||
5291 (flist->xbf_low &&
5292 XFS_FSB_TO_AGNO(mp, *firstblock) <
5293 XFS_FSB_TO_AGNO(mp,
5294 cur->bc_private.b.firstblock)));
5295 *firstblock = cur->bc_private.b.firstblock;
5296 }
5297 xfs_btree_del_cursor(cur,
5298 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5299 }
5300 if (!error)
5301 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5302 orig_nmap, *nmap);
5303 return error;
5304}
5305
5306/*
5307 * Map file blocks to filesystem blocks, simple version.
5308 * One block (extent) only, read-only.
5309 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
5310 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
5311 * was set and all the others were clear.
5312 */
5313int /* error */
5314xfs_bmapi_single(
5315 xfs_trans_t *tp, /* transaction pointer */
5316 xfs_inode_t *ip, /* incore inode */
5317 int whichfork, /* data or attr fork */
5318 xfs_fsblock_t *fsb, /* output: mapped block */
5319 xfs_fileoff_t bno) /* starting file offs. mapped */
5320{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005321 int eof; /* we've hit the end of extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 int error; /* error return */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005323 xfs_bmbt_irec_t got; /* current file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 xfs_ifork_t *ifp; /* inode fork pointer */
5325 xfs_extnum_t lastx; /* last useful extent number */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005326 xfs_bmbt_irec_t prev; /* previous file extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327
5328 ifp = XFS_IFORK_PTR(ip, whichfork);
5329 if (unlikely(
5330 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
5331 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
5332 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
5333 ip->i_mount);
5334 return XFS_ERROR(EFSCORRUPTED);
5335 }
5336 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
5337 return XFS_ERROR(EIO);
5338 XFS_STATS_INC(xs_blk_mapr);
5339 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5340 (error = xfs_iread_extents(tp, ip, whichfork)))
5341 return error;
5342 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5343 &prev);
5344 /*
5345 * Reading past eof, act as though there's a hole
5346 * up to end.
5347 */
5348 if (eof || got.br_startoff > bno) {
5349 *fsb = NULLFSBLOCK;
5350 return 0;
5351 }
5352 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
5353 ASSERT(bno < got.br_startoff + got.br_blockcount);
5354 *fsb = got.br_startblock + (bno - got.br_startoff);
5355 ifp->if_lastex = lastx;
5356 return 0;
5357}
5358
5359/*
5360 * Unmap (remove) blocks from a file.
5361 * If nexts is nonzero then the number of extents to remove is limited to
5362 * that value. If not all extents in the block range can be removed then
5363 * *done is set.
5364 */
5365int /* error */
5366xfs_bunmapi(
5367 xfs_trans_t *tp, /* transaction pointer */
5368 struct xfs_inode *ip, /* incore inode */
5369 xfs_fileoff_t bno, /* starting offset to unmap */
5370 xfs_filblks_t len, /* length to unmap in file */
5371 int flags, /* misc flags */
5372 xfs_extnum_t nexts, /* number of extents max */
5373 xfs_fsblock_t *firstblock, /* first allocated block
5374 controls a.g. for allocs */
5375 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005376 xfs_extdelta_t *delta, /* o: change made to incore
5377 extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 int *done) /* set if not done yet */
5379{
5380 xfs_btree_cur_t *cur; /* bmap btree cursor */
5381 xfs_bmbt_irec_t del; /* extent being deleted */
5382 int eof; /* is deleting at eof */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005383 xfs_bmbt_rec_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 int error; /* error return value */
5385 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005386 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 xfs_ifork_t *ifp; /* inode fork pointer */
5388 int isrt; /* freeing in rt area */
5389 xfs_extnum_t lastx; /* last extent index used */
5390 int logflags; /* transaction logging flags */
5391 xfs_extlen_t mod; /* rt extent offset */
5392 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005393 xfs_extnum_t nextents; /* number of file extents */
5394 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 xfs_fileoff_t start; /* first file offset deleted */
5396 int tmp_logflags; /* partial logging flags */
5397 int wasdel; /* was a delayed alloc extent */
5398 int whichfork; /* data or attribute fork */
5399 int rsvd; /* OK to allocate reserved blocks */
5400 xfs_fsblock_t sum;
5401
5402 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
5403 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5404 XFS_ATTR_FORK : XFS_DATA_FORK;
5405 ifp = XFS_IFORK_PTR(ip, whichfork);
5406 if (unlikely(
5407 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5408 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5409 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5410 ip->i_mount);
5411 return XFS_ERROR(EFSCORRUPTED);
5412 }
5413 mp = ip->i_mount;
5414 if (XFS_FORCED_SHUTDOWN(mp))
5415 return XFS_ERROR(EIO);
5416 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
5417 ASSERT(len > 0);
5418 ASSERT(nexts >= 0);
5419 ASSERT(ifp->if_ext_max ==
5420 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5421 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5422 (error = xfs_iread_extents(tp, ip, whichfork)))
5423 return error;
5424 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5425 if (nextents == 0) {
5426 *done = 1;
5427 return 0;
5428 }
5429 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005430 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 start = bno;
5432 bno = start + len - 1;
5433 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5434 &prev);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005435 if (delta) {
5436 delta->xed_startoff = NULLFILEOFF;
5437 delta->xed_blockcount = 0;
5438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 /*
5440 * Check to see if the given block number is past the end of the
5441 * file, back up to the last block if so...
5442 */
5443 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005444 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 xfs_bmbt_get_all(ep, &got);
5446 bno = got.br_startoff + got.br_blockcount - 1;
5447 }
5448 logflags = 0;
5449 if (ifp->if_flags & XFS_IFBROOT) {
5450 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5451 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
5452 whichfork);
5453 cur->bc_private.b.firstblock = *firstblock;
5454 cur->bc_private.b.flist = flist;
5455 cur->bc_private.b.flags = 0;
5456 } else
5457 cur = NULL;
5458 extno = 0;
5459 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5460 (nexts == 0 || extno < nexts)) {
5461 /*
5462 * Is the found extent after a hole in which bno lives?
5463 * Just back up to the previous extent, if so.
5464 */
5465 if (got.br_startoff > bno) {
5466 if (--lastx < 0)
5467 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005468 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 xfs_bmbt_get_all(ep, &got);
5470 }
5471 /*
5472 * Is the last block of this extent before the range
5473 * we're supposed to delete? If so, we're done.
5474 */
5475 bno = XFS_FILEOFF_MIN(bno,
5476 got.br_startoff + got.br_blockcount - 1);
5477 if (bno < start)
5478 break;
5479 /*
5480 * Then deal with the (possibly delayed) allocated space
5481 * we found.
5482 */
5483 ASSERT(ep != NULL);
5484 del = got;
5485 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
5486 if (got.br_startoff < start) {
5487 del.br_startoff = start;
5488 del.br_blockcount -= start - got.br_startoff;
5489 if (!wasdel)
5490 del.br_startblock += start - got.br_startoff;
5491 }
5492 if (del.br_startoff + del.br_blockcount > bno + 1)
5493 del.br_blockcount = bno + 1 - del.br_startoff;
5494 sum = del.br_startblock + del.br_blockcount;
5495 if (isrt &&
5496 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5497 /*
5498 * Realtime extent not lined up at the end.
5499 * The extent could have been split into written
5500 * and unwritten pieces, or we could just be
5501 * unmapping part of it. But we can't really
5502 * get rid of part of a realtime extent.
5503 */
5504 if (del.br_state == XFS_EXT_UNWRITTEN ||
5505 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5506 /*
5507 * This piece is unwritten, or we're not
5508 * using unwritten extents. Skip over it.
5509 */
5510 ASSERT(bno >= mod);
5511 bno -= mod > del.br_blockcount ?
5512 del.br_blockcount : mod;
5513 if (bno < got.br_startoff) {
5514 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005515 xfs_bmbt_get_all(xfs_iext_get_ext(
5516 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 }
5518 continue;
5519 }
5520 /*
5521 * It's written, turn it unwritten.
5522 * This is better than zeroing it.
5523 */
5524 ASSERT(del.br_state == XFS_EXT_NORM);
5525 ASSERT(xfs_trans_get_block_res(tp) > 0);
5526 /*
5527 * If this spans a realtime extent boundary,
5528 * chop it back to the start of the one we end at.
5529 */
5530 if (del.br_blockcount > mod) {
5531 del.br_startoff += del.br_blockcount - mod;
5532 del.br_startblock += del.br_blockcount - mod;
5533 del.br_blockcount = mod;
5534 }
5535 del.br_state = XFS_EXT_UNWRITTEN;
5536 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005537 firstblock, flist, &logflags, delta,
5538 XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 if (error)
5540 goto error0;
5541 goto nodelete;
5542 }
5543 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5544 /*
5545 * Realtime extent is lined up at the end but not
5546 * at the front. We'll get rid of full extents if
5547 * we can.
5548 */
5549 mod = mp->m_sb.sb_rextsize - mod;
5550 if (del.br_blockcount > mod) {
5551 del.br_blockcount -= mod;
5552 del.br_startoff += mod;
5553 del.br_startblock += mod;
5554 } else if ((del.br_startoff == start &&
5555 (del.br_state == XFS_EXT_UNWRITTEN ||
5556 xfs_trans_get_block_res(tp) == 0)) ||
5557 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
5558 /*
5559 * Can't make it unwritten. There isn't
5560 * a full extent here so just skip it.
5561 */
5562 ASSERT(bno >= del.br_blockcount);
5563 bno -= del.br_blockcount;
5564 if (bno < got.br_startoff) {
5565 if (--lastx >= 0)
5566 xfs_bmbt_get_all(--ep, &got);
5567 }
5568 continue;
5569 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5570 /*
5571 * This one is already unwritten.
5572 * It must have a written left neighbor.
5573 * Unwrite the killed part of that one and
5574 * try again.
5575 */
5576 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005577 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5578 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 ASSERT(prev.br_state == XFS_EXT_NORM);
5580 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
5581 ASSERT(del.br_startblock ==
5582 prev.br_startblock + prev.br_blockcount);
5583 if (prev.br_startoff < start) {
5584 mod = start - prev.br_startoff;
5585 prev.br_blockcount -= mod;
5586 prev.br_startblock += mod;
5587 prev.br_startoff = start;
5588 }
5589 prev.br_state = XFS_EXT_UNWRITTEN;
5590 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
5591 &prev, firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005592 delta, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 if (error)
5594 goto error0;
5595 goto nodelete;
5596 } else {
5597 ASSERT(del.br_state == XFS_EXT_NORM);
5598 del.br_state = XFS_EXT_UNWRITTEN;
5599 error = xfs_bmap_add_extent(ip, lastx, &cur,
5600 &del, firstblock, flist, &logflags,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005601 delta, XFS_DATA_FORK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 if (error)
5603 goto error0;
5604 goto nodelete;
5605 }
5606 }
5607 if (wasdel) {
5608 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005609 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005610 if (isrt) {
5611 xfs_filblks_t rtexts;
5612
5613 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5614 do_div(rtexts, mp->m_sb.sb_rextsize);
5615 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5616 (int)rtexts, rsvd);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005617 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5618 NULL, ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005619 XFS_QMOPT_RES_RTBLKS);
5620 } else {
5621 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
5622 (int)del.br_blockcount, rsvd);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005623 (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp,
5624 NULL, ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 ip->i_delayed_blks -= del.br_blockcount;
5628 if (cur)
5629 cur->bc_private.b.flags |=
5630 XFS_BTCUR_BPRV_WASDEL;
5631 } else if (cur)
5632 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5633 /*
5634 * If it's the case where the directory code is running
5635 * with no block reservation, and the deleted block is in
5636 * the middle of its extent, and the resulting insert
5637 * of an extent would cause transformation to btree format,
5638 * then reject it. The calling code will then swap
5639 * blocks around instead.
5640 * We have to do this now, rather than waiting for the
5641 * conversion to btree format, since the transaction
5642 * will be dirty.
5643 */
5644 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5645 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5646 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
5647 del.br_startoff > got.br_startoff &&
5648 del.br_startoff + del.br_blockcount <
5649 got.br_startoff + got.br_blockcount) {
5650 error = XFS_ERROR(ENOSPC);
5651 goto error0;
5652 }
5653 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005654 &tmp_logflags, delta, whichfork, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 logflags |= tmp_logflags;
5656 if (error)
5657 goto error0;
5658 bno = del.br_startoff - 1;
5659nodelete:
5660 lastx = ifp->if_lastex;
5661 /*
5662 * If not done go on to the next (previous) record.
5663 * Reset ep in case the extents array was re-alloced.
5664 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005665 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5667 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
5668 xfs_bmbt_get_startoff(ep) > bno) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005669 if (--lastx >= 0)
5670 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 }
5672 if (lastx >= 0)
5673 xfs_bmbt_get_all(ep, &got);
5674 extno++;
5675 }
5676 }
5677 ifp->if_lastex = lastx;
5678 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5679 ASSERT(ifp->if_ext_max ==
5680 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5681 /*
5682 * Convert to a btree if necessary.
5683 */
5684 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5685 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
5686 ASSERT(cur == NULL);
5687 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5688 &cur, 0, &tmp_logflags, whichfork);
5689 logflags |= tmp_logflags;
5690 if (error)
5691 goto error0;
5692 }
5693 /*
5694 * transform from btree to extents, give it cur
5695 */
5696 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
5697 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
5698 ASSERT(cur != NULL);
5699 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5700 whichfork);
5701 logflags |= tmp_logflags;
5702 if (error)
5703 goto error0;
5704 }
5705 /*
5706 * transform from extents to local?
5707 */
5708 ASSERT(ifp->if_ext_max ==
5709 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
5710 error = 0;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005711 if (delta && delta->xed_startoff != NULLFILEOFF) {
5712 /* A change was actually made.
5713 * Note that delta->xed_blockount is an offset at this
5714 * point and needs to be converted to a block count.
5715 */
5716 ASSERT(delta->xed_blockcount > delta->xed_startoff);
5717 delta->xed_blockcount -= delta->xed_startoff;
5718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719error0:
5720 /*
5721 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005722 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 */
5724 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
5725 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5726 logflags &= ~XFS_ILOG_FEXT(whichfork);
5727 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
5728 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5729 logflags &= ~XFS_ILOG_FBROOT(whichfork);
5730 /*
5731 * Log inode even in the error case, if the transaction
5732 * is dirty we'll need to shut down the filesystem.
5733 */
5734 if (logflags)
5735 xfs_trans_log_inode(tp, ip, logflags);
5736 if (cur) {
5737 if (!error) {
5738 *firstblock = cur->bc_private.b.firstblock;
5739 cur->bc_private.b.allocated = 0;
5740 }
5741 xfs_btree_del_cursor(cur,
5742 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5743 }
5744 return error;
5745}
5746
5747/*
5748 * Fcntl interface to xfs_bmapi.
5749 */
5750int /* error code */
5751xfs_getbmap(
5752 bhv_desc_t *bdp, /* XFS behavior descriptor*/
5753 struct getbmap *bmv, /* user bmap structure */
5754 void __user *ap, /* pointer to user's array */
5755 int interface) /* interface flags */
5756{
5757 __int64_t bmvend; /* last block requested */
5758 int error; /* return value */
5759 __int64_t fixlen; /* length for -1 case */
5760 int i; /* extent number */
5761 xfs_inode_t *ip; /* xfs incore inode pointer */
Nathan Scott67fcaa72006-06-09 17:00:52 +10005762 bhv_vnode_t *vp; /* corresponding vnode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 int lock; /* lock state */
5764 xfs_bmbt_irec_t *map; /* buffer for user's data */
5765 xfs_mount_t *mp; /* file system mount point */
5766 int nex; /* # of user extents can do */
5767 int nexleft; /* # of user extents left */
5768 int subnex; /* # of bmapi's can do */
5769 int nmap; /* number of map entries */
5770 struct getbmap out; /* output structure */
5771 int whichfork; /* data or attr fork */
5772 int prealloced; /* this is a file with
5773 * preallocated data space */
5774 int sh_unwritten; /* true, if unwritten */
5775 /* extents listed separately */
5776 int bmapi_flags; /* flags for xfs_bmapi */
5777 __int32_t oflags; /* getbmapx bmv_oflags field */
5778
5779 vp = BHV_TO_VNODE(bdp);
5780 ip = XFS_BHVTOI(bdp);
5781 mp = ip->i_mount;
5782
5783 whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5784 sh_unwritten = (interface & BMV_IF_PREALLOC) != 0;
5785
5786 /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not
5787 * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ
5788 * bit is set for the file, generate a read event in order
5789 * that the DMAPI application may do its thing before we return
5790 * the extents. Usually this means restoring user file data to
5791 * regions of the file that look like holes.
5792 *
5793 * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify
5794 * BMV_IF_NO_DMAPI_READ so that read events are generated.
5795 * If this were not true, callers of ioctl( XFS_IOC_GETBMAP )
5796 * could misinterpret holes in a DMAPI file as true holes,
5797 * when in fact they may represent offline user data.
5798 */
5799 if ( (interface & BMV_IF_NO_DMAPI_READ) == 0
5800 && DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ)
5801 && whichfork == XFS_DATA_FORK) {
5802
5803 error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, 0, 0, 0, NULL);
5804 if (error)
5805 return XFS_ERROR(error);
5806 }
5807
5808 if (whichfork == XFS_ATTR_FORK) {
5809 if (XFS_IFORK_Q(ip)) {
5810 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5811 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5812 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5813 return XFS_ERROR(EINVAL);
5814 } else if (unlikely(
5815 ip->i_d.di_aformat != 0 &&
5816 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5817 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5818 ip->i_mount);
5819 return XFS_ERROR(EFSCORRUPTED);
5820 }
5821 } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5822 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5823 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5824 return XFS_ERROR(EINVAL);
5825 if (whichfork == XFS_DATA_FORK) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11005826 if ((ip->i_d.di_extsize && (ip->i_d.di_flags &
5827 (XFS_DIFLAG_REALTIME|XFS_DIFLAG_EXTSIZE))) ||
5828 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 prealloced = 1;
5830 fixlen = XFS_MAXIOFFSET(mp);
5831 } else {
5832 prealloced = 0;
5833 fixlen = ip->i_d.di_size;
5834 }
5835 } else {
5836 prealloced = 0;
5837 fixlen = 1LL << 32;
5838 }
5839
5840 if (bmv->bmv_length == -1) {
5841 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5842 bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset),
5843 (__int64_t)0);
5844 } else if (bmv->bmv_length < 0)
5845 return XFS_ERROR(EINVAL);
5846 if (bmv->bmv_length == 0) {
5847 bmv->bmv_entries = 0;
5848 return 0;
5849 }
5850 nex = bmv->bmv_count - 1;
5851 if (nex <= 0)
5852 return XFS_ERROR(EINVAL);
5853 bmvend = bmv->bmv_offset + bmv->bmv_length;
5854
5855 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5856
5857 if (whichfork == XFS_DATA_FORK && ip->i_delayed_blks) {
5858 /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */
Nathan Scott67fcaa72006-06-09 17:00:52 +10005859 error = bhv_vop_flush_pages(vp, (xfs_off_t)0, -1, 0, FI_REMAPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 }
5861
5862 ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0);
5863
5864 lock = xfs_ilock_map_shared(ip);
5865
5866 /*
5867 * Don't let nex be bigger than the number of extents
5868 * we can have assuming alternating holes and real extents.
5869 */
5870 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5871 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5872
5873 bmapi_flags = XFS_BMAPI_AFLAG(whichfork) |
5874 ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE);
5875
5876 /*
5877 * Allocate enough space to handle "subnex" maps at a time.
5878 */
5879 subnex = 16;
5880 map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP);
5881
5882 bmv->bmv_entries = 0;
5883
5884 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) {
5885 error = 0;
5886 goto unlock_and_return;
5887 }
5888
5889 nexleft = nex;
5890
5891 do {
5892 nmap = (nexleft > subnex) ? subnex : nexleft;
5893 error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5894 XFS_BB_TO_FSB(mp, bmv->bmv_length),
Olaf Weber3e57ecf2006-06-09 14:48:12 +10005895 bmapi_flags, NULL, 0, map, &nmap,
5896 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 if (error)
5898 goto unlock_and_return;
5899 ASSERT(nmap <= subnex);
5900
5901 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5902 nexleft--;
5903 oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ?
5904 BMV_OF_PREALLOC : 0;
5905 out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff);
5906 out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5907 ASSERT(map[i].br_startblock != DELAYSTARTBLOCK);
Yingping Lu9af0a702005-11-02 15:09:54 +11005908 if (map[i].br_startblock == HOLESTARTBLOCK &&
5909 ((prealloced && out.bmv_offset + out.bmv_length == bmvend) ||
5910 whichfork == XFS_ATTR_FORK )) {
5911 /*
5912 * came to hole at end of file or the end of
5913 attribute fork
5914 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 goto unlock_and_return;
5916 } else {
5917 out.bmv_block =
5918 (map[i].br_startblock == HOLESTARTBLOCK) ?
5919 -1 :
5920 XFS_FSB_TO_DB(ip, map[i].br_startblock);
5921
5922 /* return either getbmap/getbmapx structure. */
5923 if (interface & BMV_IF_EXTENDED) {
5924 struct getbmapx outx;
5925
5926 GETBMAP_CONVERT(out,outx);
5927 outx.bmv_oflags = oflags;
5928 outx.bmv_unused1 = outx.bmv_unused2 = 0;
5929 if (copy_to_user(ap, &outx,
5930 sizeof(outx))) {
5931 error = XFS_ERROR(EFAULT);
5932 goto unlock_and_return;
5933 }
5934 } else {
5935 if (copy_to_user(ap, &out,
5936 sizeof(out))) {
5937 error = XFS_ERROR(EFAULT);
5938 goto unlock_and_return;
5939 }
5940 }
5941 bmv->bmv_offset =
5942 out.bmv_offset + out.bmv_length;
5943 bmv->bmv_length = MAX((__int64_t)0,
5944 (__int64_t)(bmvend - bmv->bmv_offset));
5945 bmv->bmv_entries++;
5946 ap = (interface & BMV_IF_EXTENDED) ?
5947 (void __user *)
5948 ((struct getbmapx __user *)ap + 1) :
5949 (void __user *)
5950 ((struct getbmap __user *)ap + 1);
5951 }
5952 }
5953 } while (nmap && nexleft && bmv->bmv_length);
5954
5955unlock_and_return:
5956 xfs_iunlock_map_shared(ip, lock);
5957 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5958
5959 kmem_free(map, subnex * sizeof(*map));
5960
5961 return error;
5962}
5963
5964/*
5965 * Check the last inode extent to determine whether this allocation will result
5966 * in blocks being allocated at the end of the file. When we allocate new data
5967 * blocks at the end of the file which do not start at the previous data block,
5968 * we will try to align the new blocks at stripe unit boundaries.
5969 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10005970STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971xfs_bmap_isaeof(
5972 xfs_inode_t *ip, /* incore inode pointer */
5973 xfs_fileoff_t off, /* file offset in fsblocks */
5974 int whichfork, /* data or attribute fork */
5975 char *aeof) /* return value */
5976{
5977 int error; /* error return value */
5978 xfs_ifork_t *ifp; /* inode fork pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005979 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
5980 xfs_extnum_t nextents; /* number of file extents */
5981 xfs_bmbt_irec_t s; /* expanded extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
5983 ASSERT(whichfork == XFS_DATA_FORK);
5984 ifp = XFS_IFORK_PTR(ip, whichfork);
5985 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5986 (error = xfs_iread_extents(NULL, ip, whichfork)))
5987 return error;
5988 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5989 if (nextents == 0) {
5990 *aeof = 1;
5991 return 0;
5992 }
5993 /*
5994 * Go to the last extent
5995 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005996 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 xfs_bmbt_get_all(lastrec, &s);
5998 /*
5999 * Check we are allocating in the last extent (for delayed allocations)
6000 * or past the last extent for non-delayed allocations.
6001 */
6002 *aeof = (off >= s.br_startoff &&
6003 off < s.br_startoff + s.br_blockcount &&
6004 ISNULLSTARTBLOCK(s.br_startblock)) ||
6005 off >= s.br_startoff + s.br_blockcount;
6006 return 0;
6007}
6008
6009/*
6010 * Check if the endoff is outside the last extent. If so the caller will grow
6011 * the allocation to a stripe unit boundary.
6012 */
6013int /* error */
6014xfs_bmap_eof(
6015 xfs_inode_t *ip, /* incore inode pointer */
6016 xfs_fileoff_t endoff, /* file offset in fsblocks */
6017 int whichfork, /* data or attribute fork */
6018 int *eof) /* result value */
6019{
6020 xfs_fsblock_t blockcount; /* extent block count */
6021 int error; /* error return value */
6022 xfs_ifork_t *ifp; /* inode fork pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006023 xfs_bmbt_rec_t *lastrec; /* extent record pointer */
6024 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 xfs_fileoff_t startoff; /* extent starting file offset */
6026
6027 ASSERT(whichfork == XFS_DATA_FORK);
6028 ifp = XFS_IFORK_PTR(ip, whichfork);
6029 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
6030 (error = xfs_iread_extents(NULL, ip, whichfork)))
6031 return error;
6032 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
6033 if (nextents == 0) {
6034 *eof = 1;
6035 return 0;
6036 }
6037 /*
6038 * Go to the last extent
6039 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006040 lastrec = xfs_iext_get_ext(ifp, nextents - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 startoff = xfs_bmbt_get_startoff(lastrec);
6042 blockcount = xfs_bmbt_get_blockcount(lastrec);
6043 *eof = endoff >= startoff + blockcount;
6044 return 0;
6045}
6046
6047#ifdef DEBUG
6048/*
6049 * Check that the extents list for the inode ip is in the right order.
6050 */
6051STATIC void
6052xfs_bmap_check_extents(
6053 xfs_inode_t *ip, /* incore inode pointer */
6054 int whichfork) /* data or attr fork */
6055{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 xfs_bmbt_rec_t *ep; /* current extent entry */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006057 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 xfs_ifork_t *ifp; /* inode fork pointer */
6059 xfs_extnum_t nextents; /* number of extents in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006060 xfs_bmbt_rec_t *nextp; /* next extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
6062 ifp = XFS_IFORK_PTR(ip, whichfork);
6063 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006065 ep = xfs_iext_get_ext(ifp, 0);
6066 for (idx = 0; idx < nextents - 1; idx++) {
6067 nextp = xfs_iext_get_ext(ifp, idx + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006069 (void *)(nextp));
6070 ep = nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 }
6072}
6073
6074STATIC
6075xfs_buf_t *
6076xfs_bmap_get_bp(
6077 xfs_btree_cur_t *cur,
6078 xfs_fsblock_t bno)
6079{
6080 int i;
6081 xfs_buf_t *bp;
6082
6083 if (!cur)
6084 return(NULL);
6085
6086 bp = NULL;
6087 for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
6088 bp = cur->bc_bufs[i];
6089 if (!bp) break;
6090 if (XFS_BUF_ADDR(bp) == bno)
6091 break; /* Found it */
6092 }
6093 if (i == XFS_BTREE_MAXLEVELS)
6094 bp = NULL;
6095
6096 if (!bp) { /* Chase down all the log items to see if the bp is there */
6097 xfs_log_item_chunk_t *licp;
6098 xfs_trans_t *tp;
6099
6100 tp = cur->bc_tp;
6101 licp = &tp->t_items;
6102 while (!bp && licp != NULL) {
6103 if (XFS_LIC_ARE_ALL_FREE(licp)) {
6104 licp = licp->lic_next;
6105 continue;
6106 }
6107 for (i = 0; i < licp->lic_unused; i++) {
6108 xfs_log_item_desc_t *lidp;
6109 xfs_log_item_t *lip;
6110 xfs_buf_log_item_t *bip;
6111 xfs_buf_t *lbp;
6112
6113 if (XFS_LIC_ISFREE(licp, i)) {
6114 continue;
6115 }
6116
6117 lidp = XFS_LIC_SLOT(licp, i);
6118 lip = lidp->lid_item;
6119 if (lip->li_type != XFS_LI_BUF)
6120 continue;
6121
6122 bip = (xfs_buf_log_item_t *)lip;
6123 lbp = bip->bli_buf;
6124
6125 if (XFS_BUF_ADDR(lbp) == bno) {
6126 bp = lbp;
6127 break; /* Found it */
6128 }
6129 }
6130 licp = licp->lic_next;
6131 }
6132 }
6133 return(bp);
6134}
6135
6136void
6137xfs_check_block(
6138 xfs_bmbt_block_t *block,
6139 xfs_mount_t *mp,
6140 int root,
6141 short sz)
6142{
6143 int i, j, dmxr;
6144 xfs_bmbt_ptr_t *pp, *thispa; /* pointer to block address */
6145 xfs_bmbt_key_t *prevp, *keyp;
6146
Christoph Hellwig16259e72005-11-02 15:11:25 +11006147 ASSERT(be16_to_cpu(block->bb_level) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148
6149 prevp = NULL;
Christoph Hellwig16259e72005-11-02 15:11:25 +11006150 for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 dmxr = mp->m_bmap_dmxr[0];
6152
6153 if (root) {
6154 keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz);
6155 } else {
6156 keyp = XFS_BTREE_KEY_ADDR(mp->m_sb.sb_blocksize,
6157 xfs_bmbt, block, i, dmxr);
6158 }
6159
6160 if (prevp) {
6161 xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp);
6162 }
6163 prevp = keyp;
6164
6165 /*
6166 * Compare the block numbers to see if there are dups.
6167 */
6168
6169 if (root) {
6170 pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz);
6171 } else {
6172 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6173 xfs_bmbt, block, i, dmxr);
6174 }
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 {
6179 thispa = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6180 xfs_bmbt, block, j, dmxr);
6181 }
6182 if (INT_GET(*thispa, ARCH_CONVERT) ==
6183 INT_GET(*pp, ARCH_CONVERT)) {
6184 cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld",
6185 __FUNCTION__, j, i,
6186 INT_GET(*thispa, ARCH_CONVERT));
6187 panic("%s: ptrs are equal in node\n",
6188 __FUNCTION__);
6189 }
6190 }
6191 }
6192}
6193
6194/*
6195 * Check that the extents for the inode ip are in the right order in all
6196 * btree leaves.
6197 */
6198
6199STATIC void
6200xfs_bmap_check_leaf_extents(
6201 xfs_btree_cur_t *cur, /* btree cursor or null */
6202 xfs_inode_t *ip, /* incore inode pointer */
6203 int whichfork) /* data or attr fork */
6204{
6205 xfs_bmbt_block_t *block; /* current btree block */
6206 xfs_fsblock_t bno; /* block # of "block" */
6207 xfs_buf_t *bp; /* buffer for "block" */
6208 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006209 xfs_extnum_t i=0, j; /* index into the extents list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 xfs_ifork_t *ifp; /* fork structure */
6211 int level; /* btree level, for checking */
6212 xfs_mount_t *mp; /* file system mount structure */
6213 xfs_bmbt_ptr_t *pp; /* pointer to block address */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006214 xfs_bmbt_rec_t *ep; /* pointer to current extent */
6215 xfs_bmbt_rec_t *lastp; /* pointer to previous extent */
6216 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 int bp_release = 0;
6218
6219 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
6220 return;
6221 }
6222
6223 bno = NULLFSBLOCK;
6224 mp = ip->i_mount;
6225 ifp = XFS_IFORK_PTR(ip, whichfork);
6226 block = ifp->if_broot;
6227 /*
6228 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6229 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11006230 level = be16_to_cpu(block->bb_level);
6231 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
6233 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6234 ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
6235 ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
6236 ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
6237 bno = INT_GET(*pp, ARCH_CONVERT);
6238 /*
6239 * Go down the tree until leaf level is reached, following the first
6240 * pointer (leftmost) at each level.
6241 */
6242 while (level-- > 0) {
6243 /* See if buf is in cur first */
6244 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6245 if (bp) {
6246 bp_release = 0;
6247 } else {
6248 bp_release = 1;
6249 }
6250 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6251 XFS_BMAP_BTREE_REF)))
6252 goto error_norelse;
6253 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6254 XFS_WANT_CORRUPTED_GOTO(
6255 XFS_BMAP_SANITY_CHECK(mp, block, level),
6256 error0);
6257 if (level == 0)
6258 break;
6259
6260 /*
6261 * Check this block for basic sanity (increasing keys and
6262 * no duplicate blocks).
6263 */
6264
6265 xfs_check_block(block, mp, 0, 0);
6266 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
6267 1, mp->m_bmap_dmxr[1]);
6268 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, INT_GET(*pp, ARCH_CONVERT)), error0);
6269 bno = INT_GET(*pp, ARCH_CONVERT);
6270 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
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006304 ep = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 block, 1, mp->m_bmap_dmxr[0]);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006306 for (j = 1; j < num_recs; j++) {
6307 nextp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
6308 block, j + 1, mp->m_bmap_dmxr[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 if (lastp) {
6310 xfs_btree_check_rec(XFS_BTNUM_BMAP,
6311 (void *)lastp, (void *)ep);
6312 }
6313 xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006314 (void *)(nextp));
6315 lastp = ep;
6316 ep = nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318
6319 i += num_recs;
6320 if (bp_release) {
6321 bp_release = 0;
6322 xfs_trans_brelse(NULL, bp);
6323 }
6324 bno = nextbno;
6325 /*
6326 * If we've reached the end, stop.
6327 */
6328 if (bno == NULLFSBLOCK)
6329 break;
6330
6331 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6332 if (bp) {
6333 bp_release = 0;
6334 } else {
6335 bp_release = 1;
6336 }
6337 if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6338 XFS_BMAP_BTREE_REF)))
6339 goto error_norelse;
6340 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6341 }
6342 if (bp_release) {
6343 bp_release = 0;
6344 xfs_trans_brelse(NULL, bp);
6345 }
6346 return;
6347
6348error0:
6349 cmn_err(CE_WARN, "%s: at error0", __FUNCTION__);
6350 if (bp_release)
6351 xfs_trans_brelse(NULL, bp);
6352error_norelse:
6353 cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents",
Christoph Hellwigda1650a2005-11-02 10:21:35 +11006354 __FUNCTION__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__);
6356 return;
6357}
6358#endif
6359
6360/*
6361 * Count fsblocks of the given fork.
6362 */
6363int /* error */
6364xfs_bmap_count_blocks(
6365 xfs_trans_t *tp, /* transaction pointer */
6366 xfs_inode_t *ip, /* incore inode */
6367 int whichfork, /* data or attr fork */
6368 int *count) /* out: count of blocks */
6369{
6370 xfs_bmbt_block_t *block; /* current btree block */
6371 xfs_fsblock_t bno; /* block # of "block" */
6372 xfs_ifork_t *ifp; /* fork structure */
6373 int level; /* btree level, for checking */
6374 xfs_mount_t *mp; /* file system mount structure */
6375 xfs_bmbt_ptr_t *pp; /* pointer to block address */
6376
6377 bno = NULLFSBLOCK;
6378 mp = ip->i_mount;
6379 ifp = XFS_IFORK_PTR(ip, whichfork);
6380 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006381 if (unlikely(xfs_bmap_count_leaves(ifp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6383 count) < 0)) {
6384 XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)",
6385 XFS_ERRLEVEL_LOW, mp);
6386 return XFS_ERROR(EFSCORRUPTED);
6387 }
6388 return 0;
6389 }
6390
6391 /*
6392 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6393 */
6394 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11006395 level = be16_to_cpu(block->bb_level);
6396 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
6398 ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
6399 ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
6400 ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
6401 bno = INT_GET(*pp, ARCH_CONVERT);
6402
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006403 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6405 mp);
6406 return XFS_ERROR(EFSCORRUPTED);
6407 }
6408
6409 return 0;
6410}
6411
6412/*
6413 * Recursively walks each level of a btree
6414 * to count total fsblocks is use.
6415 */
6416int /* error */
6417xfs_bmap_count_tree(
6418 xfs_mount_t *mp, /* file system mount point */
6419 xfs_trans_t *tp, /* transaction pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006420 xfs_ifork_t *ifp, /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 xfs_fsblock_t blockno, /* file system block number */
6422 int levelin, /* level in btree */
6423 int *count) /* Count of blocks */
6424{
6425 int error;
6426 xfs_buf_t *bp, *nbp;
6427 int level = levelin;
6428 xfs_bmbt_ptr_t *pp;
6429 xfs_fsblock_t bno = blockno;
6430 xfs_fsblock_t nextbno;
6431 xfs_bmbt_block_t *block, *nextblock;
6432 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433
6434 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF)))
6435 return error;
6436 *count += 1;
6437 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6438
6439 if (--level) {
6440 /* Not at node above leafs, count this level of nodes */
Christoph Hellwig16259e72005-11-02 15:11:25 +11006441 nextbno = be64_to_cpu(block->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442 while (nextbno != NULLFSBLOCK) {
6443 if ((error = xfs_btree_read_bufl(mp, tp, nextbno,
6444 0, &nbp, XFS_BMAP_BTREE_REF)))
6445 return error;
6446 *count += 1;
6447 nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11006448 nextbno = be64_to_cpu(nextblock->bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449 xfs_trans_brelse(tp, nbp);
6450 }
6451
6452 /* Dive to the next level */
6453 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize,
6454 xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]);
6455 bno = INT_GET(*pp, ARCH_CONVERT);
6456 if (unlikely((error =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006457 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458 xfs_trans_brelse(tp, bp);
6459 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6460 XFS_ERRLEVEL_LOW, mp);
6461 return XFS_ERROR(EFSCORRUPTED);
6462 }
6463 xfs_trans_brelse(tp, bp);
6464 } else {
6465 /* count all level 1 nodes and their leaves */
6466 for (;;) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11006467 nextbno = be64_to_cpu(block->bb_rightsib);
6468 numrecs = be16_to_cpu(block->bb_numrecs);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006469 if (unlikely(xfs_bmap_disk_count_leaves(ifp, mp,
6470 0, block, numrecs, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 xfs_trans_brelse(tp, bp);
6472 XFS_ERROR_REPORT("xfs_bmap_count_tree(2)",
6473 XFS_ERRLEVEL_LOW, mp);
6474 return XFS_ERROR(EFSCORRUPTED);
6475 }
6476 xfs_trans_brelse(tp, bp);
6477 if (nextbno == NULLFSBLOCK)
6478 break;
6479 bno = nextbno;
6480 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6481 XFS_BMAP_BTREE_REF)))
6482 return error;
6483 *count += 1;
6484 block = XFS_BUF_TO_BMBT_BLOCK(bp);
6485 }
6486 }
6487 return 0;
6488}
6489
6490/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006491 * Count leaf blocks given a range of extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 */
6493int
6494xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006495 xfs_ifork_t *ifp,
6496 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497 int numrecs,
6498 int *count)
6499{
6500 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006501 xfs_bmbt_rec_t *frp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006503 for (b = 0; b < numrecs; b++) {
6504 frp = xfs_iext_get_ext(ifp, idx + b);
Yingping Lu91e11082005-11-02 15:10:24 +11006505 *count += xfs_bmbt_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006506 }
Yingping Lu91e11082005-11-02 15:10:24 +11006507 return 0;
6508}
6509
6510/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006511 * Count leaf blocks given a range of extent records originally
6512 * in btree format.
Yingping Lu91e11082005-11-02 15:10:24 +11006513 */
6514int
6515xfs_bmap_disk_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006516 xfs_ifork_t *ifp,
6517 xfs_mount_t *mp,
6518 xfs_extnum_t idx,
6519 xfs_bmbt_block_t *block,
Yingping Lu91e11082005-11-02 15:10:24 +11006520 int numrecs,
6521 int *count)
6522{
6523 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006524 xfs_bmbt_rec_t *frp;
Yingping Lu91e11082005-11-02 15:10:24 +11006525
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006526 for (b = 1; b <= numrecs; b++) {
6527 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize,
6528 xfs_bmbt, block, idx + b, mp->m_bmap_dmxr[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 *count += xfs_bmbt_disk_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531 return 0;
6532}