blob: b44af9211bd99027930215a316f3044dc79f1bf7 [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_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_itable.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_extfree_item.h"
39#include "xfs_alloc.h"
40#include "xfs_bmap.h"
41#include "xfs_rtalloc.h"
42#include "xfs_error.h"
Nathan Scottd8cc8902005-11-02 10:34:53 +110043#include "xfs_attr_leaf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_quota.h"
45#include "xfs_trans_space.h"
46#include "xfs_buf_item.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100047#include "xfs_filestream.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100048#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000049#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052kmem_zone_t *xfs_bmap_free_item_zone;
53
54/*
55 * Prototypes for internal bmap routines.
56 */
57
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +000058#ifdef DEBUG
59STATIC void
60xfs_bmap_check_leaf_extents(
61 struct xfs_btree_cur *cur,
62 struct xfs_inode *ip,
63 int whichfork);
64#else
65#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
66#endif
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
93 * It figures out where to ask the underlying allocator to put the new extent.
94 */
95STATIC int /* error */
96xfs_bmap_alloc(
97 xfs_bmalloca_t *ap); /* bmap alloc argument struct */
98
99/*
100 * Transform a btree format file with only one leaf node, where the
101 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100102 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * give up the space for the btree root and pitch the leaf block.
104 */
105STATIC int /* error */
106xfs_bmap_btree_to_extents(
107 xfs_trans_t *tp, /* transaction pointer */
108 xfs_inode_t *ip, /* incore inode pointer */
109 xfs_btree_cur_t *cur, /* btree cursor */
110 int *logflagsp, /* inode logging flags */
111 int whichfork); /* data or attr fork */
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * Remove the entry "free" from the free item list. Prev points to the
115 * previous entry, unless "free" is the head of the list.
116 */
117STATIC void
118xfs_bmap_del_free(
119 xfs_bmap_free_t *flist, /* free item list header */
120 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
121 xfs_bmap_free_item_t *free); /* list item to be freed */
122
123/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * Convert an extents-format file into a btree-format file.
125 * The new file will have a root block (in the inode) and a single child block.
126 */
127STATIC int /* error */
128xfs_bmap_extents_to_btree(
129 xfs_trans_t *tp, /* transaction pointer */
130 xfs_inode_t *ip, /* incore inode pointer */
131 xfs_fsblock_t *firstblock, /* first-block-allocated */
132 xfs_bmap_free_t *flist, /* blocks freed in xaction */
133 xfs_btree_cur_t **curp, /* cursor returned to caller */
134 int wasdel, /* converting a delayed alloc */
135 int *logflagsp, /* inode logging flags */
136 int whichfork); /* data or attr fork */
137
138/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * Convert a local file to an extents file.
140 * This code is sort of bogus, since the file data needs to get
141 * logged so it won't be lost. The bmap-level manipulations are ok, though.
142 */
143STATIC int /* error */
144xfs_bmap_local_to_extents(
145 xfs_trans_t *tp, /* transaction pointer */
146 xfs_inode_t *ip, /* incore inode pointer */
147 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
148 xfs_extlen_t total, /* total blocks needed by transaction */
149 int *logflagsp, /* inode logging flags */
Dave Chinner1e823792013-02-11 15:58:13 +1100150 int whichfork, /* data or attr fork */
151 void (*init_fn)(struct xfs_buf *bp,
152 struct xfs_inode *ip,
153 struct xfs_ifork *ifp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155/*
156 * Search the extents list for the inode, for the extent containing bno.
157 * If bno lies in a hole, point to the next entry. If bno lies past eof,
158 * *eofp will be set, and *prevp will contain the last entry (null if none).
159 * Else, *lastxp will be set to the index of the found
160 * entry; *gotp will contain the entry.
161 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000162STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163xfs_bmap_search_extents(
164 xfs_inode_t *ip, /* incore inode pointer */
165 xfs_fileoff_t bno, /* block number searched for */
166 int whichfork, /* data or attr fork */
167 int *eofp, /* out: end of file found */
168 xfs_extnum_t *lastxp, /* out: last extent index */
169 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
170 xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */
171
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000172/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * Compute the worst-case number of indirect blocks that will be used
174 * for ip's delayed extent of length "len".
175 */
176STATIC xfs_filblks_t
177xfs_bmap_worst_indlen(
178 xfs_inode_t *ip, /* incore inode pointer */
179 xfs_filblks_t len); /* delayed extent length */
180
181#ifdef DEBUG
182/*
183 * Perform various validation checks on the values being returned
184 * from xfs_bmapi().
185 */
186STATIC void
187xfs_bmap_validate_ret(
188 xfs_fileoff_t bno,
189 xfs_filblks_t len,
190 int flags,
191 xfs_bmbt_irec_t *mval,
192 int nmap,
193 int ret_nmap);
194#else
195#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
196#endif /* DEBUG */
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198STATIC int
199xfs_bmap_count_tree(
200 xfs_mount_t *mp,
201 xfs_trans_t *tp,
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100202 xfs_ifork_t *ifp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 xfs_fsblock_t blockno,
204 int levelin,
205 int *count);
206
Ruben Porrasc94312d2008-08-13 16:52:25 +1000207STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100209 xfs_ifork_t *ifp,
210 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 int numrecs,
212 int *count);
213
Ruben Porrasc94312d2008-08-13 16:52:25 +1000214STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +1100215xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +1100216 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100217 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +1100218 int numrecs,
219 int *count);
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*
222 * Bmap internal routines.
223 */
224
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100225STATIC int /* error */
226xfs_bmbt_lookup_eq(
227 struct xfs_btree_cur *cur,
228 xfs_fileoff_t off,
229 xfs_fsblock_t bno,
230 xfs_filblks_t len,
231 int *stat) /* success/failure */
232{
233 cur->bc_rec.b.br_startoff = off;
234 cur->bc_rec.b.br_startblock = bno;
235 cur->bc_rec.b.br_blockcount = len;
236 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
237}
238
239STATIC int /* error */
240xfs_bmbt_lookup_ge(
241 struct xfs_btree_cur *cur,
242 xfs_fileoff_t off,
243 xfs_fsblock_t bno,
244 xfs_filblks_t len,
245 int *stat) /* success/failure */
246{
247 cur->bc_rec.b.br_startoff = off;
248 cur->bc_rec.b.br_startblock = bno;
249 cur->bc_rec.b.br_blockcount = len;
250 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
251}
252
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100253/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000254 * Check if the inode needs to be converted to btree format.
255 */
256static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
257{
258 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
259 XFS_IFORK_NEXTENTS(ip, whichfork) >
260 XFS_IFORK_MAXEXT(ip, whichfork);
261}
262
263/*
264 * Check if the inode should be converted to extent format.
265 */
266static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
267{
268 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
269 XFS_IFORK_NEXTENTS(ip, whichfork) <=
270 XFS_IFORK_MAXEXT(ip, whichfork);
271}
272
273/*
274 * Update the record referred to by cur to the value given
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100275 * by [off, bno, len, state].
276 * This either works (return 0) or gets an EFSCORRUPTED error.
277 */
278STATIC int
279xfs_bmbt_update(
280 struct xfs_btree_cur *cur,
281 xfs_fileoff_t off,
282 xfs_fsblock_t bno,
283 xfs_filblks_t len,
284 xfs_exntst_t state)
285{
286 union xfs_btree_rec rec;
287
288 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
289 return xfs_btree_update(cur, &rec);
290}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*
293 * Called from xfs_bmap_add_attrfork to handle btree format files.
294 */
295STATIC int /* error */
296xfs_bmap_add_attrfork_btree(
297 xfs_trans_t *tp, /* transaction pointer */
298 xfs_inode_t *ip, /* incore inode pointer */
299 xfs_fsblock_t *firstblock, /* first block allocated */
300 xfs_bmap_free_t *flist, /* blocks to free at commit */
301 int *flags) /* inode logging flags */
302{
303 xfs_btree_cur_t *cur; /* btree cursor */
304 int error; /* error return value */
305 xfs_mount_t *mp; /* file system mount struct */
306 int stat; /* newroot status */
307
308 mp = ip->i_mount;
309 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
310 *flags |= XFS_ILOG_DBROOT;
311 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100312 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 cur->bc_private.b.flist = flist;
314 cur->bc_private.b.firstblock = *firstblock;
315 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
316 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000317 /* must be at least one entry */
318 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100319 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 goto error0;
321 if (stat == 0) {
322 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
323 return XFS_ERROR(ENOSPC);
324 }
325 *firstblock = cur->bc_private.b.firstblock;
326 cur->bc_private.b.allocated = 0;
327 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
328 }
329 return 0;
330error0:
331 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
332 return error;
333}
334
335/*
336 * Called from xfs_bmap_add_attrfork to handle extents format files.
337 */
338STATIC int /* error */
339xfs_bmap_add_attrfork_extents(
340 xfs_trans_t *tp, /* transaction pointer */
341 xfs_inode_t *ip, /* incore inode pointer */
342 xfs_fsblock_t *firstblock, /* first block allocated */
343 xfs_bmap_free_t *flist, /* blocks to free at commit */
344 int *flags) /* inode logging flags */
345{
346 xfs_btree_cur_t *cur; /* bmap btree cursor */
347 int error; /* error return value */
348
349 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
350 return 0;
351 cur = NULL;
352 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
353 flags, XFS_DATA_FORK);
354 if (cur) {
355 cur->bc_private.b.allocated = 0;
356 xfs_btree_del_cursor(cur,
357 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
358 }
359 return error;
360}
361
362/*
Dave Chinner1e823792013-02-11 15:58:13 +1100363 * Block initialisation functions for local to extent format conversion.
364 * As these get more complex, they will be moved to the relevant files,
365 * but for now they are too simple to worry about.
366 */
367STATIC void
368xfs_bmap_local_to_extents_init_fn(
369 struct xfs_buf *bp,
370 struct xfs_inode *ip,
371 struct xfs_ifork *ifp)
372{
373 bp->b_ops = &xfs_bmbt_buf_ops;
374 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
375}
376
377STATIC void
378xfs_symlink_local_to_remote(
379 struct xfs_buf *bp,
380 struct xfs_inode *ip,
381 struct xfs_ifork *ifp)
382{
383 /* remote symlink blocks are not verifiable until CRCs come along */
384 bp->b_ops = NULL;
385 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
386}
387
388/*
389 * Called from xfs_bmap_add_attrfork to handle local format files. Each
390 * different data fork content type needs a different callout to do the
391 * conversion. Some are basic and only require special block initialisation
392 * callouts for the data formating, others (directories) are so specialised they
393 * handle everything themselves.
394 *
395 * XXX (dgc): investigate whether directory conversion can use the generic
396 * formatting callout. It should be possible - it's just a very complex
397 * formatter. it would also require passing the transaction through to the init
398 * function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
400STATIC int /* error */
401xfs_bmap_add_attrfork_local(
402 xfs_trans_t *tp, /* transaction pointer */
403 xfs_inode_t *ip, /* incore inode pointer */
404 xfs_fsblock_t *firstblock, /* first block allocated */
405 xfs_bmap_free_t *flist, /* blocks to free at commit */
406 int *flags) /* inode logging flags */
407{
408 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
411 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +1100412
Al Viroabbede12011-07-26 02:31:30 -0400413 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 memset(&dargs, 0, sizeof(dargs));
415 dargs.dp = ip;
416 dargs.firstblock = firstblock;
417 dargs.flist = flist;
Dave Chinner1e823792013-02-11 15:58:13 +1100418 dargs.total = ip->i_mount->m_dirblkfsbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 dargs.whichfork = XFS_DATA_FORK;
420 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +1100421 return xfs_dir2_sf_to_block(&dargs);
422 }
423
424 if (S_ISLNK(ip->i_d.di_mode))
425 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
426 flags, XFS_DATA_FORK,
427 xfs_symlink_local_to_remote);
428
429 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
430 XFS_DATA_FORK,
431 xfs_bmap_local_to_extents_init_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000435 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
437STATIC int /* error */
438xfs_bmap_add_extent_delay_real(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000439 struct xfs_bmalloca *bma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000441 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +1000443 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100446 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 xfs_fileoff_t new_endoff; /* end offset of new entry */
448 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
449 /* left is 0, right is 1, prev is 2 */
450 int rval=0; /* return value (logging flags) */
451 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000452 xfs_filblks_t da_new; /* new count del alloc blocks used */
453 xfs_filblks_t da_old; /* old count del alloc blocks used */
454 xfs_filblks_t temp=0; /* value for da_new calculations */
455 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 int tmp_rval; /* partial logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000458 ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000459
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000460 ASSERT(bma->idx >= 0);
461 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000462 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000463 ASSERT(!bma->cur ||
464 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000465
466 XFS_STATS_INC(xs_add_exlist);
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468#define LEFT r[0]
469#define RIGHT r[1]
470#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 /*
473 * Set up a bunch of variables to make the tests simpler.
474 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000475 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 xfs_bmbt_get_all(ep, &PREV);
477 new_endoff = new->br_startoff + new->br_blockcount;
478 ASSERT(PREV.br_startoff <= new->br_startoff);
479 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000480
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000481 da_old = startblockval(PREV.br_startblock);
482 da_new = 0;
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /*
485 * Set flags determining what part of the previous delayed allocation
486 * extent is being replaced by a real allocation.
487 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000488 if (PREV.br_startoff == new->br_startoff)
489 state |= BMAP_LEFT_FILLING;
490 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
491 state |= BMAP_RIGHT_FILLING;
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /*
494 * Check and set flags if this segment has a left neighbor.
495 * Don't set contiguous if the combined extent would be too large.
496 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000497 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000498 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000499 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000500
501 if (isnullstartblock(LEFT.br_startblock))
502 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000504
505 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
506 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
507 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
508 LEFT.br_state == new->br_state &&
509 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
510 state |= BMAP_LEFT_CONTIG;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /*
513 * Check and set flags if this segment has a right neighbor.
514 * Don't set contiguous if the combined extent would be too large.
515 * Also check for all-three-contiguous being too large.
516 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000517 if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000518 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000519 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000520
521 if (isnullstartblock(RIGHT.br_startblock))
522 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000524
525 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
526 new_endoff == RIGHT.br_startoff &&
527 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
528 new->br_state == RIGHT.br_state &&
529 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
530 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
531 BMAP_RIGHT_FILLING)) !=
532 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
533 BMAP_RIGHT_FILLING) ||
534 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
535 <= MAXEXTLEN))
536 state |= BMAP_RIGHT_CONTIG;
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 error = 0;
539 /*
540 * Switch out based on the FILLING and CONTIG state bits.
541 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000542 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
543 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
544 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
545 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /*
547 * Filling in all of a previously delayed allocation extent.
548 * The left and right neighbors are both contiguous with new.
549 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000550 bma->idx--;
551 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
552 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 LEFT.br_blockcount + PREV.br_blockcount +
554 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000555 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000556
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000557 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
558 bma->ip->i_d.di_nextents--;
559 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
561 else {
562 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000563 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000565 RIGHT.br_blockcount, &i);
566 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000568 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000569 error = xfs_btree_delete(bma->cur, &i);
570 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000572 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000573 error = xfs_btree_decrement(bma->cur, 0, &i);
574 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000576 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000577 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 LEFT.br_startblock,
579 LEFT.br_blockcount +
580 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000581 RIGHT.br_blockcount, LEFT.br_state);
582 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 goto done;
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 break;
586
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000587 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /*
589 * Filling in all of a previously delayed allocation extent.
590 * The left neighbor is contiguous, the right is not.
591 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000592 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000593
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000594 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
595 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +0000596 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000597 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000598
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000599 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
600 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 rval = XFS_ILOG_DEXT;
602 else {
603 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000604 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000606 &i);
607 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000609 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000610 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 LEFT.br_startblock,
612 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000613 PREV.br_blockcount, LEFT.br_state);
614 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 goto done;
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 break;
618
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000619 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /*
621 * Filling in all of a previously delayed allocation extent.
622 * The right neighbor is contiguous, the left is not.
623 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000624 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 xfs_bmbt_set_startblock(ep, new->br_startblock);
626 xfs_bmbt_set_blockcount(ep,
627 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000628 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000629
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000630 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
631 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 rval = XFS_ILOG_DEXT;
633 else {
634 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000635 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000637 RIGHT.br_blockcount, &i);
638 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000640 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000641 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 new->br_startblock,
643 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000644 RIGHT.br_blockcount, PREV.br_state);
645 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 goto done;
647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 break;
649
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000650 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /*
652 * Filling in all of a previously delayed allocation extent.
653 * Neither the left nor right neighbors are contiguous with
654 * the new one.
655 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000656 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000658 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000659
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000660 bma->ip->i_d.di_nextents++;
661 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
663 else {
664 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000665 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000667 &i);
668 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000670 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000671 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
672 error = xfs_btree_insert(bma->cur, &i);
673 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000675 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 break;
678
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000679 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /*
681 * Filling in the first part of a previous delayed allocation.
682 * The left neighbor is contiguous.
683 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000684 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
685 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 LEFT.br_blockcount + new->br_blockcount);
687 xfs_bmbt_set_startoff(ep,
688 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000689 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000692 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000694 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 rval = XFS_ILOG_DEXT;
696 else {
697 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000698 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000700 &i);
701 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000703 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000704 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 LEFT.br_startblock,
706 LEFT.br_blockcount +
707 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000708 LEFT.br_state);
709 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 goto done;
711 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000712 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600713 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000714 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000715 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000716
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000717 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 break;
719
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000720 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /*
722 * Filling in the first part of a previous delayed allocation.
723 * The left neighbor is not contiguous.
724 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000725 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 xfs_bmbt_set_startoff(ep, new_endoff);
727 temp = PREV.br_blockcount - new->br_blockcount;
728 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000729 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
730 bma->ip->i_d.di_nextents++;
731 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
733 else {
734 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000735 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000737 &i);
738 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000740 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000741 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
742 error = xfs_btree_insert(bma->cur, &i);
743 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000745 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000747
748 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000749 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
750 bma->firstblock, bma->flist,
751 &bma->cur, 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 rval |= tmp_rval;
753 if (error)
754 goto done;
755 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000756 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600757 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000758 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
759 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000760 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000761 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 break;
763
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000764 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 /*
766 * Filling in the last part of a previous delayed allocation.
767 * The right neighbor is contiguous with the new allocation.
768 */
769 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000770 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000772 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100773 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 new->br_blockcount + RIGHT.br_blockcount,
775 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000776 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
777 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 rval = XFS_ILOG_DEXT;
779 else {
780 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000781 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000783 RIGHT.br_blockcount, &i);
784 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000786 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000787 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 new->br_startblock,
789 new->br_blockcount +
790 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000791 RIGHT.br_state);
792 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 goto done;
794 }
Christoph Hellwigec90c552011-05-23 08:52:53 +0000795
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000796 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600797 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000798 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000799 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000800 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000801
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000802 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 break;
804
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000805 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /*
807 * Filling in the last part of a previous delayed allocation.
808 * The right neighbor is not contiguous.
809 */
810 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000811 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000813 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
814 bma->ip->i_d.di_nextents++;
815 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
817 else {
818 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000819 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000821 &i);
822 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000824 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000825 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
826 error = xfs_btree_insert(bma->cur, &i);
827 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000829 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000831
832 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000833 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
834 bma->firstblock, bma->flist, &bma->cur, 1,
835 &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 rval |= tmp_rval;
837 if (error)
838 goto done;
839 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000840 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600841 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000842 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
843 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000844 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000845 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000846
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000847 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 break;
849
850 case 0:
851 /*
852 * Filling in the middle part of a previous delayed allocation.
853 * Contiguity is impossible here.
854 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +0000855 *
856 * We start with a delayed allocation:
857 *
858 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
859 * PREV @ idx
860 *
861 * and we are allocating:
862 * +rrrrrrrrrrrrrrrrr+
863 * new
864 *
865 * and we set it up for insertion as:
866 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
867 * new
868 * PREV @ idx LEFT RIGHT
869 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 */
871 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000873 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +0000874 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
875 LEFT = *new;
876 RIGHT.br_state = PREV.br_state;
877 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000878 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +0000879 RIGHT.br_startoff = new_endoff;
880 RIGHT.br_blockcount = temp2;
881 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000882 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
883 bma->ip->i_d.di_nextents++;
884 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
886 else {
887 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000888 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000890 &i);
891 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000893 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000894 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
895 error = xfs_btree_insert(bma->cur, &i);
896 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000898 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000900
901 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000902 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
903 bma->firstblock, bma->flist, &bma->cur,
904 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 rval |= tmp_rval;
906 if (error)
907 goto done;
908 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000909 temp = xfs_bmap_worst_indlen(bma->ip, temp);
910 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600911 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000912 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +0000913 if (diff > 0) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000914 error = xfs_icsb_modify_counters(bma->ip->i_mount,
Christoph Hellwigb9b984d2011-09-18 20:40:42 +0000915 XFS_SBS_FDBLOCKS,
916 -((int64_t)diff), 0);
917 ASSERT(!error);
918 if (error)
919 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +0000921
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000922 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -0600923 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000924 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
925 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
926 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -0600927 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000928 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +0000929
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000930 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000931 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 break;
933
Christoph Hellwig7574aa92009-11-25 00:00:19 +0000934 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
935 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
936 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
937 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
938 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
939 case BMAP_LEFT_CONTIG:
940 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /*
942 * These cases are all impossible.
943 */
944 ASSERT(0);
945 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000946
947 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000948 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000949 int tmp_logflags; /* partial log flag return val */
950
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000951 ASSERT(bma->cur == NULL);
952 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
953 bma->firstblock, bma->flist, &bma->cur,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000954 da_old > 0, &tmp_logflags, XFS_DATA_FORK);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000955 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000956 if (error)
957 goto done;
958 }
959
960 /* adjust for changes in reserved delayed indirect blocks */
961 if (da_old || da_new) {
962 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000963 if (bma->cur)
964 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000965 ASSERT(temp <= da_old);
966 if (temp < da_old)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000967 xfs_icsb_modify_counters(bma->ip->i_mount,
968 XFS_SBS_FDBLOCKS,
969 (int64_t)(da_old - temp), 0);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000970 }
971
972 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000973 if (bma->cur)
974 bma->cur->bc_private.b.allocated = 0;
975
976 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977done:
Christoph Hellwig572a4cf2011-09-18 20:41:04 +0000978 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return error;
980#undef LEFT
981#undef RIGHT
982#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
985/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000986 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 */
988STATIC int /* error */
989xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000990 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +0000992 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +1100994 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +0000995 xfs_fsblock_t *first, /* pointer to firstblock variable */
996 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +1000997 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001000 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001003 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 xfs_fileoff_t new_endoff; /* end offset of new entry */
1005 xfs_exntst_t newext; /* new extent state */
1006 xfs_exntst_t oldext; /* old extent state */
1007 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1008 /* left is 0, right is 1, prev is 2 */
1009 int rval=0; /* return value (logging flags) */
1010 int state = 0;/* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001012 *logflagsp = 0;
1013
1014 cur = *curp;
1015 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1016
1017 ASSERT(*idx >= 0);
1018 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
1019 ASSERT(!isnullstartblock(new->br_startblock));
1020
1021 XFS_STATS_INC(xs_add_exlist);
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023#define LEFT r[0]
1024#define RIGHT r[1]
1025#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /*
1028 * Set up a bunch of variables to make the tests simpler.
1029 */
1030 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001031 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 xfs_bmbt_get_all(ep, &PREV);
1033 newext = new->br_state;
1034 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1035 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1036 ASSERT(PREV.br_state == oldext);
1037 new_endoff = new->br_startoff + new->br_blockcount;
1038 ASSERT(PREV.br_startoff <= new->br_startoff);
1039 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /*
1042 * Set flags determining what part of the previous oldext allocation
1043 * extent is being replaced by a newext allocation.
1044 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001045 if (PREV.br_startoff == new->br_startoff)
1046 state |= BMAP_LEFT_FILLING;
1047 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1048 state |= BMAP_RIGHT_FILLING;
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /*
1051 * Check and set flags if this segment has a left neighbor.
1052 * Don't set contiguous if the combined extent would be too large.
1053 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001054 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001055 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001056 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001057
1058 if (isnullstartblock(LEFT.br_startblock))
1059 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001061
1062 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1063 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1064 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1065 LEFT.br_state == newext &&
1066 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1067 state |= BMAP_LEFT_CONTIG;
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /*
1070 * Check and set flags if this segment has a right neighbor.
1071 * Don't set contiguous if the combined extent would be too large.
1072 * Also check for all-three-contiguous being too large.
1073 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001074 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001075 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001076 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001077 if (isnullstartblock(RIGHT.br_startblock))
1078 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001080
1081 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1082 new_endoff == RIGHT.br_startoff &&
1083 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1084 newext == RIGHT.br_state &&
1085 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1086 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1087 BMAP_RIGHT_FILLING)) !=
1088 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1089 BMAP_RIGHT_FILLING) ||
1090 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1091 <= MAXEXTLEN))
1092 state |= BMAP_RIGHT_CONTIG;
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 /*
1095 * Switch out based on the FILLING and CONTIG state bits.
1096 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001097 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1098 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1099 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1100 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /*
1102 * Setting all of a previous oldext extent to newext.
1103 * The left and right neighbors are both contiguous with new.
1104 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001105 --*idx;
1106
1107 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1108 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 LEFT.br_blockcount + PREV.br_blockcount +
1110 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001111 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001112
Christoph Hellwigec90c552011-05-23 08:52:53 +00001113 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 ip->i_d.di_nextents -= 2;
1115 if (cur == NULL)
1116 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1117 else {
1118 rval = XFS_ILOG_CORE;
1119 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1120 RIGHT.br_startblock,
1121 RIGHT.br_blockcount, &i)))
1122 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001123 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001124 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001126 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001127 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001129 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001130 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001132 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001133 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001135 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1137 LEFT.br_startblock,
1138 LEFT.br_blockcount + PREV.br_blockcount +
1139 RIGHT.br_blockcount, LEFT.br_state)))
1140 goto done;
1141 }
1142 break;
1143
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001144 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 /*
1146 * Setting all of a previous oldext extent to newext.
1147 * The left neighbor is contiguous, the right is not.
1148 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001149 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001150
Christoph Hellwigec90c552011-05-23 08:52:53 +00001151 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1152 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1153 LEFT.br_blockcount + PREV.br_blockcount);
1154 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1155
1156 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 ip->i_d.di_nextents--;
1158 if (cur == NULL)
1159 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1160 else {
1161 rval = XFS_ILOG_CORE;
1162 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1163 PREV.br_startblock, PREV.br_blockcount,
1164 &i)))
1165 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001166 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001167 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001169 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001170 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001172 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1174 LEFT.br_startblock,
1175 LEFT.br_blockcount + PREV.br_blockcount,
1176 LEFT.br_state)))
1177 goto done;
1178 }
1179 break;
1180
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001181 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 /*
1183 * Setting all of a previous oldext extent to newext.
1184 * The right neighbor is contiguous, the left is not.
1185 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001186 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 xfs_bmbt_set_blockcount(ep,
1188 PREV.br_blockcount + RIGHT.br_blockcount);
1189 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001190 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1191 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 ip->i_d.di_nextents--;
1193 if (cur == NULL)
1194 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1195 else {
1196 rval = XFS_ILOG_CORE;
1197 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1198 RIGHT.br_startblock,
1199 RIGHT.br_blockcount, &i)))
1200 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001201 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11001202 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001204 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001205 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001207 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1209 new->br_startblock,
1210 new->br_blockcount + RIGHT.br_blockcount,
1211 newext)))
1212 goto done;
1213 }
1214 break;
1215
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001216 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 /*
1218 * Setting all of a previous oldext extent to newext.
1219 * Neither the left nor right neighbors are contiguous with
1220 * the new one.
1221 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001222 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001224 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (cur == NULL)
1227 rval = XFS_ILOG_DEXT;
1228 else {
1229 rval = 0;
1230 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1231 new->br_startblock, new->br_blockcount,
1232 &i)))
1233 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001234 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1236 new->br_startblock, new->br_blockcount,
1237 newext)))
1238 goto done;
1239 }
1240 break;
1241
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001242 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /*
1244 * Setting the first part of a previous oldext extent to newext.
1245 * The left neighbor is contiguous.
1246 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001247 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
1248 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 LEFT.br_blockcount + new->br_blockcount);
1250 xfs_bmbt_set_startoff(ep,
1251 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001252 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001253
Christoph Hellwigec90c552011-05-23 08:52:53 +00001254 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 xfs_bmbt_set_startblock(ep,
1256 new->br_startblock + new->br_blockcount);
1257 xfs_bmbt_set_blockcount(ep,
1258 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001259 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001260
Christoph Hellwigec90c552011-05-23 08:52:53 +00001261 --*idx;
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (cur == NULL)
1264 rval = XFS_ILOG_DEXT;
1265 else {
1266 rval = 0;
1267 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1268 PREV.br_startblock, PREV.br_blockcount,
1269 &i)))
1270 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001271 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if ((error = xfs_bmbt_update(cur,
1273 PREV.br_startoff + new->br_blockcount,
1274 PREV.br_startblock + new->br_blockcount,
1275 PREV.br_blockcount - new->br_blockcount,
1276 oldext)))
1277 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001278 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00001280 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 LEFT.br_startblock,
1282 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00001283 LEFT.br_state);
1284 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 goto done;
1286 }
1287 break;
1288
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001289 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 /*
1291 * Setting the first part of a previous oldext extent to newext.
1292 * The left neighbor is not contiguous.
1293 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001294 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1296 xfs_bmbt_set_startoff(ep, new_endoff);
1297 xfs_bmbt_set_blockcount(ep,
1298 PREV.br_blockcount - new->br_blockcount);
1299 xfs_bmbt_set_startblock(ep,
1300 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001301 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001302
Christoph Hellwigec90c552011-05-23 08:52:53 +00001303 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 ip->i_d.di_nextents++;
1305 if (cur == NULL)
1306 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1307 else {
1308 rval = XFS_ILOG_CORE;
1309 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1310 PREV.br_startblock, PREV.br_blockcount,
1311 &i)))
1312 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001313 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if ((error = xfs_bmbt_update(cur,
1315 PREV.br_startoff + new->br_blockcount,
1316 PREV.br_startblock + new->br_blockcount,
1317 PREV.br_blockcount - new->br_blockcount,
1318 oldext)))
1319 goto done;
1320 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001321 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001323 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325 break;
1326
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001327 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 /*
1329 * Setting the last part of a previous oldext extent to newext.
1330 * The right neighbor is contiguous with the new allocation.
1331 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001332 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 xfs_bmbt_set_blockcount(ep,
1334 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001335 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1336
1337 ++*idx;
1338
1339 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1340 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001341 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001343 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (cur == NULL)
1346 rval = XFS_ILOG_DEXT;
1347 else {
1348 rval = 0;
1349 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1350 PREV.br_startblock,
1351 PREV.br_blockcount, &i)))
1352 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001353 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1355 PREV.br_startblock,
1356 PREV.br_blockcount - new->br_blockcount,
1357 oldext)))
1358 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001359 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 goto done;
1361 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1362 new->br_startblock,
1363 new->br_blockcount + RIGHT.br_blockcount,
1364 newext)))
1365 goto done;
1366 }
1367 break;
1368
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001369 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 /*
1371 * Setting the last part of a previous oldext extent to newext.
1372 * The right neighbor is not contiguous.
1373 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001374 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 xfs_bmbt_set_blockcount(ep,
1376 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001377 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001378
Christoph Hellwigec90c552011-05-23 08:52:53 +00001379 ++*idx;
1380 xfs_iext_insert(ip, *idx, 1, new, state);
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 ip->i_d.di_nextents++;
1383 if (cur == NULL)
1384 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1385 else {
1386 rval = XFS_ILOG_CORE;
1387 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1388 PREV.br_startblock, PREV.br_blockcount,
1389 &i)))
1390 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001391 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1393 PREV.br_startblock,
1394 PREV.br_blockcount - new->br_blockcount,
1395 oldext)))
1396 goto done;
1397 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1398 new->br_startblock, new->br_blockcount,
1399 &i)))
1400 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001401 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001403 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001405 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407 break;
1408
1409 case 0:
1410 /*
1411 * Setting the middle part of a previous oldext extent to
1412 * newext. Contiguity is impossible here.
1413 * One extent becomes three extents.
1414 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001415 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 xfs_bmbt_set_blockcount(ep,
1417 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001418 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 r[0] = *new;
1421 r[1].br_startoff = new_endoff;
1422 r[1].br_blockcount =
1423 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1424 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1425 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001426
1427 ++*idx;
1428 xfs_iext_insert(ip, *idx, 2, &r[0], state);
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 ip->i_d.di_nextents += 2;
1431 if (cur == NULL)
1432 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1433 else {
1434 rval = XFS_ILOG_CORE;
1435 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1436 PREV.br_startblock, PREV.br_blockcount,
1437 &i)))
1438 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001439 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 /* new right extent - oldext */
1441 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1442 r[1].br_startblock, r[1].br_blockcount,
1443 r[1].br_state)))
1444 goto done;
1445 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10001447 cur->bc_rec.b.br_blockcount =
1448 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001449 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001451 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001452 /*
1453 * Reset the cursor to the position of the new extent
1454 * we are about to insert as we can't trust it after
1455 * the previous insert.
1456 */
1457 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1458 new->br_startblock, new->br_blockcount,
1459 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001461 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10001463 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001464 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001466 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
1468 break;
1469
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001470 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1471 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1472 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1473 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1474 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1475 case BMAP_LEFT_CONTIG:
1476 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 /*
1478 * These cases are all impossible.
1479 */
1480 ASSERT(0);
1481 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001482
1483 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001484 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001485 int tmp_logflags; /* partial log flag return val */
1486
1487 ASSERT(cur == NULL);
1488 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
1489 0, &tmp_logflags, XFS_DATA_FORK);
1490 *logflagsp |= tmp_logflags;
1491 if (error)
1492 goto done;
1493 }
1494
1495 /* clear out the allocated field, done with it now in any case. */
1496 if (cur) {
1497 cur->bc_private.b.allocated = 0;
1498 *curp = cur;
1499 }
1500
1501 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001503 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return error;
1505#undef LEFT
1506#undef RIGHT
1507#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
1509
1510/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00001511 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00001513STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514xfs_bmap_add_extent_hole_delay(
1515 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001516 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00001517 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001519 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1521 xfs_filblks_t newlen=0; /* new indirect size */
1522 xfs_filblks_t oldlen=0; /* old indirect size */
1523 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1524 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001525 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001527 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001529 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 /*
1532 * Check and set flags if this segment has a left neighbor
1533 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001534 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001535 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001536 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001537
1538 if (isnullstartblock(left.br_startblock))
1539 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 /*
1543 * Check and set flags if the current (right) segment exists.
1544 * If it doesn't exist, we're converting the hole at end-of-file.
1545 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001546 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001547 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00001548 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001549
1550 if (isnullstartblock(right.br_startblock))
1551 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /*
1555 * Set contiguity flags on the left and right neighbors.
1556 * Don't let extents get too large, even if the pieces are contiguous.
1557 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001558 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1559 left.br_startoff + left.br_blockcount == new->br_startoff &&
1560 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1561 state |= BMAP_LEFT_CONTIG;
1562
1563 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1564 new->br_startoff + new->br_blockcount == right.br_startoff &&
1565 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1566 (!(state & BMAP_LEFT_CONTIG) ||
1567 (left.br_blockcount + new->br_blockcount +
1568 right.br_blockcount <= MAXEXTLEN)))
1569 state |= BMAP_RIGHT_CONTIG;
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /*
1572 * Switch out based on the contiguity flags.
1573 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001574 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1575 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 /*
1577 * New allocation is contiguous with delayed allocations
1578 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001579 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001581 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 temp = left.br_blockcount + new->br_blockcount +
1583 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001584
Christoph Hellwigec90c552011-05-23 08:52:53 +00001585 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1586 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001587 oldlen = startblockval(left.br_startblock) +
1588 startblockval(new->br_startblock) +
1589 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001591 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001592 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00001593 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001594
Christoph Hellwigec90c552011-05-23 08:52:53 +00001595 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 break;
1597
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001598 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 /*
1600 * New allocation is contiguous with a delayed allocation
1601 * on the left.
1602 * Merge the new allocation with the left neighbor.
1603 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001604 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001606
1607 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1608 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06001609 oldlen = startblockval(left.br_startblock) +
1610 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001612 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001613 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00001614 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 break;
1616
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001617 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 /*
1619 * New allocation is contiguous with a delayed allocation
1620 * on the right.
1621 * Merge the new allocation with the right neighbor.
1622 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00001623 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06001625 oldlen = startblockval(new->br_startblock) +
1626 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00001628 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1629 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06001630 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001631 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 break;
1633
1634 case 0:
1635 /*
1636 * New allocation is not contiguous with another
1637 * delayed allocation.
1638 * Insert a new entry.
1639 */
1640 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00001641 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 break;
1643 }
1644 if (oldlen != newlen) {
1645 ASSERT(oldlen > newlen);
Christoph Hellwig96540c72010-09-30 02:25:55 +00001646 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00001647 (int64_t)(oldlen - newlen), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 /*
1649 * Nothing to do for disk quota accounting here.
1650 */
1651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
1654/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001655 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 */
1657STATIC int /* error */
1658xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00001659 struct xfs_bmalloca *bma,
1660 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Christoph Hellwigc6534242011-09-18 20:41:05 +00001662 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 int i; /* temp state */
1665 xfs_ifork_t *ifp; /* inode fork pointer */
1666 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1667 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001668 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Christoph Hellwigc6534242011-09-18 20:41:05 +00001671 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001672
Christoph Hellwigc6534242011-09-18 20:41:05 +00001673 ASSERT(bma->idx >= 0);
1674 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001675 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00001676 ASSERT(!bma->cur ||
1677 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001678
1679 XFS_STATS_INC(xs_add_exlist);
1680
1681 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00001682 if (whichfork == XFS_ATTR_FORK)
1683 state |= BMAP_ATTRFORK;
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 /*
1686 * Check and set flags if this segment has a left neighbor.
1687 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00001688 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001689 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00001690 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001691 if (isnullstartblock(left.br_startblock))
1692 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /*
1696 * Check and set flags if this segment has a current value.
1697 * Not true if we're inserting into the "hole" at eof.
1698 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00001699 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001700 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00001701 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001702 if (isnullstartblock(right.br_startblock))
1703 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 /*
1707 * We're inserting a real allocation between "left" and "right".
1708 * Set the contiguity flags. Don't let extents get too large.
1709 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001710 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1711 left.br_startoff + left.br_blockcount == new->br_startoff &&
1712 left.br_startblock + left.br_blockcount == new->br_startblock &&
1713 left.br_state == new->br_state &&
1714 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1715 state |= BMAP_LEFT_CONTIG;
1716
1717 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1718 new->br_startoff + new->br_blockcount == right.br_startoff &&
1719 new->br_startblock + new->br_blockcount == right.br_startblock &&
1720 new->br_state == right.br_state &&
1721 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1722 (!(state & BMAP_LEFT_CONTIG) ||
1723 left.br_blockcount + new->br_blockcount +
1724 right.br_blockcount <= MAXEXTLEN))
1725 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001727 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 /*
1729 * Select which case we're in here, and implement it.
1730 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001731 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1732 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 /*
1734 * New allocation is contiguous with real allocations on the
1735 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001736 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00001738 --bma->idx;
1739 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1740 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 left.br_blockcount + new->br_blockcount +
1742 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00001743 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001744
Christoph Hellwigc6534242011-09-18 20:41:05 +00001745 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001746
Christoph Hellwigc6534242011-09-18 20:41:05 +00001747 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
1748 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
1749 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001750 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001751 } else {
1752 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00001753 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
1754 right.br_startblock, right.br_blockcount,
1755 &i);
1756 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001757 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001758 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00001759 error = xfs_btree_delete(bma->cur, &i);
1760 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001761 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001762 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00001763 error = xfs_btree_decrement(bma->cur, 0, &i);
1764 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001765 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001766 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00001767 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001768 left.br_startblock,
1769 left.br_blockcount +
1770 new->br_blockcount +
1771 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00001772 left.br_state);
1773 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001774 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001776 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001778 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 /*
1780 * New allocation is contiguous with a real allocation
1781 * on the left.
1782 * Merge the new allocation with the left neighbor.
1783 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00001784 --bma->idx;
1785 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1786 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00001788 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001789
Christoph Hellwigc6534242011-09-18 20:41:05 +00001790 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001791 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001792 } else {
1793 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00001794 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
1795 left.br_startblock, left.br_blockcount,
1796 &i);
1797 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001798 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001799 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00001800 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001801 left.br_startblock,
1802 left.br_blockcount +
1803 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00001804 left.br_state);
1805 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001806 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001808 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001810 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 /*
1812 * New allocation is contiguous with a real allocation
1813 * on the right.
1814 * Merge the new allocation with the right neighbor.
1815 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00001816 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1817 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00001818 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 new->br_blockcount + right.br_blockcount,
1820 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00001821 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001822
Christoph Hellwigc6534242011-09-18 20:41:05 +00001823 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001824 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001825 } else {
1826 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00001827 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001828 right.br_startoff,
1829 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00001830 right.br_blockcount, &i);
1831 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001832 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001833 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00001834 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001835 new->br_startblock,
1836 new->br_blockcount +
1837 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00001838 right.br_state);
1839 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001840 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001842 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 case 0:
1845 /*
1846 * New allocation is not contiguous with another
1847 * real allocation.
1848 * Insert a new entry.
1849 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00001850 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
1851 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
1852 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
1853 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001854 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001855 } else {
1856 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00001857 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001858 new->br_startoff,
1859 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00001860 new->br_blockcount, &i);
1861 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001862 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001863 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00001864 bma->cur->bc_rec.b.br_state = new->br_state;
1865 error = xfs_btree_insert(bma->cur, &i);
1866 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001867 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001868 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001870 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001872
1873 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001874 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001875 int tmp_logflags; /* partial log flag return val */
1876
Christoph Hellwigc6534242011-09-18 20:41:05 +00001877 ASSERT(bma->cur == NULL);
1878 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1879 bma->firstblock, bma->flist, &bma->cur,
1880 0, &tmp_logflags, whichfork);
1881 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001882 if (error)
1883 goto done;
1884 }
1885
1886 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00001887 if (bma->cur)
1888 bma->cur->bc_private.b.allocated = 0;
1889
1890 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001891done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00001892 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001893 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
Nathan Scottdd9f4382006-01-11 15:28:28 +11001896/*
1897 * Adjust the size of the new extent based on di_extsize and rt extsize.
1898 */
1899STATIC int
1900xfs_bmap_extsize_align(
1901 xfs_mount_t *mp,
1902 xfs_bmbt_irec_t *gotp, /* next extent pointer */
1903 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
1904 xfs_extlen_t extsz, /* align to this extent size */
1905 int rt, /* is this a realtime inode? */
1906 int eof, /* is extent at end-of-file? */
1907 int delay, /* creating delalloc extent? */
1908 int convert, /* overwriting unwritten extent? */
1909 xfs_fileoff_t *offp, /* in/out: aligned offset */
1910 xfs_extlen_t *lenp) /* in/out: aligned length */
1911{
1912 xfs_fileoff_t orig_off; /* original offset */
1913 xfs_extlen_t orig_alen; /* original length */
1914 xfs_fileoff_t orig_end; /* original off+len */
1915 xfs_fileoff_t nexto; /* next file offset */
1916 xfs_fileoff_t prevo; /* previous file offset */
1917 xfs_fileoff_t align_off; /* temp for offset */
1918 xfs_extlen_t align_alen; /* temp for length */
1919 xfs_extlen_t temp; /* temp for calculations */
1920
1921 if (convert)
1922 return 0;
1923
1924 orig_off = align_off = *offp;
1925 orig_alen = align_alen = *lenp;
1926 orig_end = orig_off + orig_alen;
1927
1928 /*
1929 * If this request overlaps an existing extent, then don't
1930 * attempt to perform any additional alignment.
1931 */
1932 if (!delay && !eof &&
1933 (orig_off >= gotp->br_startoff) &&
1934 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
1935 return 0;
1936 }
1937
1938 /*
1939 * If the file offset is unaligned vs. the extent size
1940 * we need to align it. This will be possible unless
1941 * the file was previously written with a kernel that didn't
1942 * perform this alignment, or if a truncate shot us in the
1943 * foot.
1944 */
1945 temp = do_mod(orig_off, extsz);
1946 if (temp) {
1947 align_alen += temp;
1948 align_off -= temp;
1949 }
1950 /*
1951 * Same adjustment for the end of the requested area.
1952 */
1953 if ((temp = (align_alen % extsz))) {
1954 align_alen += extsz - temp;
1955 }
1956 /*
1957 * If the previous block overlaps with this proposed allocation
1958 * then move the start forward without adjusting the length.
1959 */
1960 if (prevp->br_startoff != NULLFILEOFF) {
1961 if (prevp->br_startblock == HOLESTARTBLOCK)
1962 prevo = prevp->br_startoff;
1963 else
1964 prevo = prevp->br_startoff + prevp->br_blockcount;
1965 } else
1966 prevo = 0;
1967 if (align_off != orig_off && align_off < prevo)
1968 align_off = prevo;
1969 /*
1970 * If the next block overlaps with this proposed allocation
1971 * then move the start back without adjusting the length,
1972 * but not before offset 0.
1973 * This may of course make the start overlap previous block,
1974 * and if we hit the offset 0 limit then the next block
1975 * can still overlap too.
1976 */
1977 if (!eof && gotp->br_startoff != NULLFILEOFF) {
1978 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
1979 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
1980 nexto = gotp->br_startoff + gotp->br_blockcount;
1981 else
1982 nexto = gotp->br_startoff;
1983 } else
1984 nexto = NULLFILEOFF;
1985 if (!eof &&
1986 align_off + align_alen != orig_end &&
1987 align_off + align_alen > nexto)
1988 align_off = nexto > align_alen ? nexto - align_alen : 0;
1989 /*
1990 * If we're now overlapping the next or previous extent that
1991 * means we can't fit an extsz piece in this hole. Just move
1992 * the start forward to the first valid spot and set
1993 * the length so we hit the end.
1994 */
1995 if (align_off != orig_off && align_off < prevo)
1996 align_off = prevo;
1997 if (align_off + align_alen != orig_end &&
1998 align_off + align_alen > nexto &&
1999 nexto != NULLFILEOFF) {
2000 ASSERT(nexto > prevo);
2001 align_alen = nexto - align_off;
2002 }
2003
2004 /*
2005 * If realtime, and the result isn't a multiple of the realtime
2006 * extent size we need to remove blocks until it is.
2007 */
2008 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2009 /*
2010 * We're not covering the original request, or
2011 * we won't be able to once we fix the length.
2012 */
2013 if (orig_off < align_off ||
2014 orig_end > align_off + align_alen ||
2015 align_alen - temp < orig_alen)
2016 return XFS_ERROR(EINVAL);
2017 /*
2018 * Try to fix it by moving the start up.
2019 */
2020 if (align_off + temp <= orig_off) {
2021 align_alen -= temp;
2022 align_off += temp;
2023 }
2024 /*
2025 * Try to fix it by moving the end in.
2026 */
2027 else if (align_off + align_alen - temp >= orig_end)
2028 align_alen -= temp;
2029 /*
2030 * Set the start to the minimum then trim the length.
2031 */
2032 else {
2033 align_alen -= orig_off - align_off;
2034 align_off = orig_off;
2035 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2036 }
2037 /*
2038 * Result doesn't cover the request, fail it.
2039 */
2040 if (orig_off < align_off || orig_end > align_off + align_alen)
2041 return XFS_ERROR(EINVAL);
2042 } else {
2043 ASSERT(orig_off >= align_off);
2044 ASSERT(orig_end <= align_off + align_alen);
2045 }
2046
2047#ifdef DEBUG
2048 if (!eof && gotp->br_startoff != NULLFILEOFF)
2049 ASSERT(align_off + align_alen <= gotp->br_startoff);
2050 if (prevp->br_startoff != NULLFILEOFF)
2051 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2052#endif
2053
2054 *lenp = align_alen;
2055 *offp = align_off;
2056 return 0;
2057}
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059#define XFS_ALLOC_GAP_UNITS 4
2060
David Chinnerc2b1cba2008-04-10 12:21:40 +10002061STATIC void
Nathan Scotta365bdd2006-03-14 13:34:16 +11002062xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2064{
2065 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
2067 xfs_mount_t *mp; /* mount point structure */
2068 int nullfb; /* true if ap->firstblock isn't set */
2069 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071#define ISVALID(x,y) \
2072 (rt ? \
2073 (x) < mp->m_sb.sb_rblocks : \
2074 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2075 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2076 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00002079 nullfb = *ap->firstblock == NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
Dave Chinner0937e0f2011-09-18 20:40:57 +00002081 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 /*
2083 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002084 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00002086 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
2087 !isnullstartblock(ap->prev.br_startblock) &&
2088 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
2089 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00002090 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 /*
2092 * Adjust for the gap between prevp and us.
2093 */
Dave Chinner3a756672011-09-18 20:40:58 +00002094 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00002095 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00002097 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
2098 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
2100 /*
2101 * If not at eof, then compare the two neighbor blocks.
2102 * Figure out whether either one gives us a good starting point,
2103 * and pick the better one.
2104 */
2105 else if (!ap->eof) {
2106 xfs_fsblock_t gotbno; /* right side block number */
2107 xfs_fsblock_t gotdiff=0; /* right side difference */
2108 xfs_fsblock_t prevbno; /* left side block number */
2109 xfs_fsblock_t prevdiff=0; /* left side difference */
2110
2111 /*
2112 * If there's a previous (left) block, select a requested
2113 * start block based on it.
2114 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00002115 if (ap->prev.br_startoff != NULLFILEOFF &&
2116 !isnullstartblock(ap->prev.br_startblock) &&
2117 (prevbno = ap->prev.br_startblock +
2118 ap->prev.br_blockcount) &&
2119 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 /*
2121 * Calculate gap to end of previous block.
2122 */
Dave Chinner3a756672011-09-18 20:40:58 +00002123 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00002124 (ap->prev.br_startoff +
2125 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 /*
2127 * Figure the startblock based on the previous block's
2128 * end and the gap size.
2129 * Heuristic!
2130 * If the gap is large relative to the piece we're
2131 * allocating, or using it gives us an invalid block
2132 * number, then just use the end of the previous block.
2133 */
Dave Chinner3a756672011-09-18 20:40:58 +00002134 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00002136 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 prevbno += adjust;
2138 else
2139 prevdiff += adjust;
2140 /*
2141 * If the firstblock forbids it, can't use it,
2142 * must use default.
2143 */
2144 if (!rt && !nullfb &&
2145 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2146 prevbno = NULLFSBLOCK;
2147 }
2148 /*
2149 * No previous block or can't follow it, just default.
2150 */
2151 else
2152 prevbno = NULLFSBLOCK;
2153 /*
2154 * If there's a following (right) block, select a requested
2155 * start block based on it.
2156 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00002157 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 /*
2159 * Calculate gap to start of next block.
2160 */
Dave Chinner3a756672011-09-18 20:40:58 +00002161 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 /*
2163 * Figure the startblock based on the next block's
2164 * start and the gap size.
2165 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00002166 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 /*
2168 * Heuristic!
2169 * If the gap is large relative to the piece we're
2170 * allocating, or using it gives us an invalid block
2171 * number, then just use the start of the next block
2172 * offset by our length.
2173 */
Dave Chinner3a756672011-09-18 20:40:58 +00002174 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 ISVALID(gotbno - gotdiff, gotbno))
2176 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00002177 else if (ISVALID(gotbno - ap->length, gotbno)) {
2178 gotbno -= ap->length;
2179 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 } else
2181 gotdiff += adjust;
2182 /*
2183 * If the firstblock forbids it, can't use it,
2184 * must use default.
2185 */
2186 if (!rt && !nullfb &&
2187 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2188 gotbno = NULLFSBLOCK;
2189 }
2190 /*
2191 * No next block, just default.
2192 */
2193 else
2194 gotbno = NULLFSBLOCK;
2195 /*
2196 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00002197 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 */
2199 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00002200 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00002202 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00002204 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11002206#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11002207}
2208
2209STATIC int
2210xfs_bmap_rtalloc(
2211 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2212{
2213 xfs_alloctype_t atype = 0; /* type for allocation routines */
2214 int error; /* error return value */
2215 xfs_mount_t *mp; /* mount point structure */
2216 xfs_extlen_t prod = 0; /* product factor for allocators */
2217 xfs_extlen_t ralen = 0; /* realtime allocation length */
2218 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002219 xfs_rtblock_t rtb;
2220
2221 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002222 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002223 prod = align / mp->m_sb.sb_rextsize;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00002224 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002225 align, 1, ap->eof, 0,
Dave Chinner3a756672011-09-18 20:40:58 +00002226 ap->conv, &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002227 if (error)
2228 return error;
Dave Chinner3a756672011-09-18 20:40:58 +00002229 ASSERT(ap->length);
2230 ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002231
2232 /*
2233 * If the offset & length are not perfectly aligned
2234 * then kill prod, it will just get us in trouble.
2235 */
Dave Chinner3a756672011-09-18 20:40:58 +00002236 if (do_mod(ap->offset, align) || ap->length % align)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002237 prod = 1;
2238 /*
2239 * Set ralen to be the actual requested length in rtextents.
2240 */
Dave Chinner3a756672011-09-18 20:40:58 +00002241 ralen = ap->length / mp->m_sb.sb_rextsize;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002242 /*
2243 * If the old value was close enough to MAXEXTLEN that
2244 * we rounded up to it, cut it back so it's valid again.
2245 * Note that if it's a really large request (bigger than
2246 * MAXEXTLEN), we don't hear about that number, and can't
2247 * adjust the starting point to match it.
2248 */
2249 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2250 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
Christoph Hellwig04e99452011-01-25 09:06:19 +00002251
2252 /*
2253 * Lock out other modifications to the RT bitmap inode.
2254 */
Christoph Hellwig1050c712011-02-13 13:25:31 +00002255 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +00002256 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Christoph Hellwig04e99452011-01-25 09:06:19 +00002257
Nathan Scotta365bdd2006-03-14 13:34:16 +11002258 /*
2259 * If it's an allocation to an empty file at offset 0,
2260 * pick an extent that will space things out in the rt area.
2261 */
Dave Chinner3a756672011-09-18 20:40:58 +00002262 if (ap->eof && ap->offset == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10002263 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
2264
Nathan Scotta365bdd2006-03-14 13:34:16 +11002265 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2266 if (error)
2267 return error;
Dave Chinner3a756672011-09-18 20:40:58 +00002268 ap->blkno = rtx * mp->m_sb.sb_rextsize;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002269 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00002270 ap->blkno = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002271 }
2272
2273 xfs_bmap_adjacent(ap);
2274
2275 /*
2276 * Realtime allocation, done through xfs_rtallocate_extent.
2277 */
Dave Chinner3a756672011-09-18 20:40:58 +00002278 atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2279 do_div(ap->blkno, mp->m_sb.sb_rextsize);
2280 rtb = ap->blkno;
2281 ap->length = ralen;
2282 if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002283 &ralen, atype, ap->wasdel, prod, &rtb)))
2284 return error;
2285 if (rtb == NULLFSBLOCK && prod > 1 &&
Dave Chinner3a756672011-09-18 20:40:58 +00002286 (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1,
2287 ap->length, &ralen, atype,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002288 ap->wasdel, 1, &rtb)))
2289 return error;
Dave Chinner3a756672011-09-18 20:40:58 +00002290 ap->blkno = rtb;
2291 if (ap->blkno != NULLFSBLOCK) {
2292 ap->blkno *= mp->m_sb.sb_rextsize;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002293 ralen *= mp->m_sb.sb_rextsize;
Dave Chinner3a756672011-09-18 20:40:58 +00002294 ap->length = ralen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002295 ap->ip->i_d.di_nblocks += ralen;
2296 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2297 if (ap->wasdel)
2298 ap->ip->i_delayed_blks -= ralen;
2299 /*
2300 * Adjust the disk quota also. This was reserved
2301 * earlier.
2302 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002303 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002304 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2305 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2306 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00002307 ap->length = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002308 }
2309 return 0;
2310}
2311
2312STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00002313xfs_bmap_btalloc_nullfb(
2314 struct xfs_bmalloca *ap,
2315 struct xfs_alloc_arg *args,
2316 xfs_extlen_t *blen)
2317{
2318 struct xfs_mount *mp = ap->ip->i_mount;
2319 struct xfs_perag *pag;
2320 xfs_agnumber_t ag, startag;
2321 int notinit = 0;
2322 int error;
2323
2324 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2325 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2326 else
2327 args->type = XFS_ALLOCTYPE_START_BNO;
2328 args->total = ap->total;
2329
2330 /*
2331 * Search for an allocation group with a single extent large enough
2332 * for the request. If one isn't found, then adjust the minimum
2333 * allocation size to the largest space found.
2334 */
2335 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2336 if (startag == NULLAGNUMBER)
2337 startag = ag = 0;
2338
2339 pag = xfs_perag_get(mp, ag);
Dave Chinner14b064c2011-01-27 12:16:28 +11002340 while (*blen < args->maxlen) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00002341 if (!pag->pagf_init) {
2342 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2343 XFS_ALLOC_FLAG_TRYLOCK);
2344 if (error) {
2345 xfs_perag_put(pag);
2346 return error;
2347 }
2348 }
2349
2350 /*
2351 * See xfs_alloc_fix_freelist...
2352 */
2353 if (pag->pagf_init) {
2354 xfs_extlen_t longest;
2355 longest = xfs_alloc_longest_free_extent(mp, pag);
2356 if (*blen < longest)
2357 *blen = longest;
2358 } else
2359 notinit = 1;
2360
2361 if (xfs_inode_is_filestream(ap->ip)) {
Dave Chinner14b064c2011-01-27 12:16:28 +11002362 if (*blen >= args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00002363 break;
2364
2365 if (ap->userdata) {
2366 /*
2367 * If startag is an invalid AG, we've
2368 * come here once before and
2369 * xfs_filestream_new_ag picked the
2370 * best currently available.
2371 *
2372 * Don't continue looping, since we
2373 * could loop forever.
2374 */
2375 if (startag == NULLAGNUMBER)
2376 break;
2377
2378 error = xfs_filestream_new_ag(ap, &ag);
2379 xfs_perag_put(pag);
2380 if (error)
2381 return error;
2382
2383 /* loop again to set 'blen'*/
2384 startag = NULLAGNUMBER;
2385 pag = xfs_perag_get(mp, ag);
2386 continue;
2387 }
2388 }
2389 if (++ag == mp->m_sb.sb_agcount)
2390 ag = 0;
2391 if (ag == startag)
2392 break;
2393 xfs_perag_put(pag);
2394 pag = xfs_perag_get(mp, ag);
2395 }
2396 xfs_perag_put(pag);
2397
2398 /*
2399 * Since the above loop did a BUF_TRYLOCK, it is
2400 * possible that there is space for this request.
2401 */
2402 if (notinit || *blen < ap->minlen)
2403 args->minlen = ap->minlen;
2404 /*
2405 * If the best seen length is less than the request
2406 * length, use the best as the minimum.
2407 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002408 else if (*blen < args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00002409 args->minlen = *blen;
2410 /*
Dave Chinner14b064c2011-01-27 12:16:28 +11002411 * Otherwise we've seen an extent as big as maxlen,
Christoph Hellwigc467c042010-02-15 23:34:42 +00002412 * use that as the minimum.
2413 */
2414 else
Dave Chinner14b064c2011-01-27 12:16:28 +11002415 args->minlen = args->maxlen;
Christoph Hellwigc467c042010-02-15 23:34:42 +00002416
2417 /*
2418 * set the failure fallback case to look in the selected
2419 * AG as the stream may have moved.
2420 */
2421 if (xfs_inode_is_filestream(ap->ip))
Dave Chinner3a756672011-09-18 20:40:58 +00002422 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00002423
2424 return 0;
2425}
2426
2427STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11002428xfs_bmap_btalloc(
2429 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2430{
2431 xfs_mount_t *mp; /* mount point structure */
2432 xfs_alloctype_t atype = 0; /* type for allocation routines */
2433 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002434 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00002435 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002436 xfs_alloc_arg_t args;
2437 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002438 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002439 int nullfb; /* true if ap->firstblock isn't set */
2440 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002441 int tryagain;
2442 int error;
2443
Dave Chinnera99ebf42011-12-01 11:24:20 +00002444 ASSERT(ap->length);
2445
Nathan Scotta365bdd2006-03-14 13:34:16 +11002446 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10002447 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002448 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00002449 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002450 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00002451 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002452 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00002453 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11002454 }
Dave Chinner0937e0f2011-09-18 20:40:57 +00002455 nullfb = *ap->firstblock == NULLFSBLOCK;
2456 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10002457 if (nullfb) {
2458 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2459 ag = xfs_filestream_lookup_ag(ap->ip);
2460 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00002461 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10002462 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00002463 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10002464 }
2465 } else
Dave Chinner3a756672011-09-18 20:40:58 +00002466 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002467
2468 xfs_bmap_adjacent(ap);
2469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 /*
Dave Chinner3a756672011-09-18 20:40:58 +00002471 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 * it's in the right allocation group.
2473 */
Dave Chinner3a756672011-09-18 20:40:58 +00002474 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 ;
2476 else
Dave Chinner3a756672011-09-18 20:40:58 +00002477 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 * Normal allocation, done through xfs_alloc_vextent.
2480 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11002481 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05002482 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11002483 args.tp = ap->tp;
2484 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00002485 args.fsbno = ap->blkno;
Dave Chinner14b064c2011-01-27 12:16:28 +11002486
2487 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner3a756672011-09-18 20:40:58 +00002488 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
Dave Chinner0937e0f2011-09-18 20:40:57 +00002489 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002490 blen = 0;
2491 if (nullfb) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00002492 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2493 if (error)
2494 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00002495 } else if (ap->flist->xbf_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10002496 if (xfs_inode_is_filestream(ap->ip))
2497 args.type = XFS_ALLOCTYPE_FIRST_AG;
2498 else
2499 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002500 args.total = args.minlen = ap->minlen;
2501 } else {
2502 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2503 args.total = ap->total;
2504 args.minlen = ap->minlen;
2505 }
David Chinner957d0eb2007-06-18 16:50:37 +10002506 /* apply extent size hints if obtained earlier */
2507 if (unlikely(align)) {
2508 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00002509 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11002510 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11002511 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11002512 args.prod = 1;
2513 args.mod = 0;
2514 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11002515 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00002516 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11002517 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2518 }
2519 /*
2520 * If we are not low on available data blocks, and the
2521 * underlying logical volume manager is a stripe, and
2522 * the file offset is zero then try to allocate data
2523 * blocks on stripe unit boundary.
2524 * NOTE: ap->aeof is only set if the allocation length
2525 * is >= the stripe unit and the allocation offset is
2526 * at the end of file.
2527 */
Dave Chinner0937e0f2011-09-18 20:40:57 +00002528 if (!ap->flist->xbf_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00002529 if (!ap->offset) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11002530 args.alignment = mp->m_dalign;
2531 atype = args.type;
2532 isaligned = 1;
2533 /*
2534 * Adjust for alignment
2535 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002536 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002537 args.minlen = blen - args.alignment;
2538 args.minalignslop = 0;
2539 } else {
2540 /*
2541 * First try an exact bno allocation.
2542 * If it fails then do a near or start bno
2543 * allocation with alignment turned on.
2544 */
2545 atype = args.type;
2546 tryagain = 1;
2547 args.type = XFS_ALLOCTYPE_THIS_BNO;
2548 args.alignment = 1;
2549 /*
2550 * Compute the minlen+alignment for the
2551 * next case. Set slop so that the value
2552 * of minlen+alignment+slop doesn't go up
2553 * between the calls.
2554 */
Dave Chinner14b064c2011-01-27 12:16:28 +11002555 if (blen > mp->m_dalign && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002556 nextminlen = blen - mp->m_dalign;
2557 else
2558 nextminlen = args.minlen;
2559 if (nextminlen + mp->m_dalign > args.minlen + 1)
2560 args.minalignslop =
2561 nextminlen + mp->m_dalign -
2562 args.minlen - 1;
2563 else
2564 args.minalignslop = 0;
2565 }
2566 } else {
2567 args.alignment = 1;
2568 args.minalignslop = 0;
2569 }
2570 args.minleft = ap->minleft;
2571 args.wasdel = ap->wasdel;
2572 args.isfl = 0;
2573 args.userdata = ap->userdata;
2574 if ((error = xfs_alloc_vextent(&args)))
2575 return error;
2576 if (tryagain && args.fsbno == NULLFSBLOCK) {
2577 /*
2578 * Exact allocation failed. Now try with alignment
2579 * turned on.
2580 */
2581 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00002582 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002583 args.alignment = mp->m_dalign;
2584 args.minlen = nextminlen;
2585 args.minalignslop = 0;
2586 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 if ((error = xfs_alloc_vextent(&args)))
2588 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002589 }
2590 if (isaligned && args.fsbno == NULLFSBLOCK) {
2591 /*
2592 * allocation failed, so turn off alignment and
2593 * try again.
2594 */
2595 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00002596 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002597 args.alignment = 0;
2598 if ((error = xfs_alloc_vextent(&args)))
2599 return error;
2600 }
2601 if (args.fsbno == NULLFSBLOCK && nullfb &&
2602 args.minlen > ap->minlen) {
2603 args.minlen = ap->minlen;
2604 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00002605 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002606 if ((error = xfs_alloc_vextent(&args)))
2607 return error;
2608 }
2609 if (args.fsbno == NULLFSBLOCK && nullfb) {
2610 args.fsbno = 0;
2611 args.type = XFS_ALLOCTYPE_FIRST_AG;
2612 args.total = ap->minlen;
2613 args.minleft = 0;
2614 if ((error = xfs_alloc_vextent(&args)))
2615 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00002616 ap->flist->xbf_low = 1;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002617 }
2618 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00002619 /*
2620 * check the allocation happened at the same or higher AG than
2621 * the first block that was allocated.
2622 */
2623 ASSERT(*ap->firstblock == NULLFSBLOCK ||
2624 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
2625 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
2626 (ap->flist->xbf_low &&
2627 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
2628 XFS_FSB_TO_AGNO(mp, args.fsbno)));
2629
Dave Chinner3a756672011-09-18 20:40:58 +00002630 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00002631 if (*ap->firstblock == NULLFSBLOCK)
2632 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002633 ASSERT(nullfb || fb_agno == args.agno ||
Dave Chinner0937e0f2011-09-18 20:40:57 +00002634 (ap->flist->xbf_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00002635 ap->length = args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002636 ap->ip->i_d.di_nblocks += args.len;
2637 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2638 if (ap->wasdel)
2639 ap->ip->i_delayed_blks -= args.len;
2640 /*
2641 * Adjust the disk quota also. This was reserved
2642 * earlier.
2643 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02002644 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11002645 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2646 XFS_TRANS_DQ_BCOUNT,
2647 (long) args.len);
2648 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00002649 ap->blkno = NULLFSBLOCK;
2650 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 }
2652 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11002653}
2654
2655/*
2656 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
2657 * It figures out where to ask the underlying allocator to put the new extent.
2658 */
2659STATIC int
2660xfs_bmap_alloc(
2661 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
2662{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002663 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11002664 return xfs_bmap_rtalloc(ap);
2665 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667
2668/*
2669 * Transform a btree format file with only one leaf node, where the
2670 * extents list will fit in the inode, into an extents format file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002671 * Since the file extents are already in-core, all we have to do is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 * give up the space for the btree root and pitch the leaf block.
2673 */
2674STATIC int /* error */
2675xfs_bmap_btree_to_extents(
2676 xfs_trans_t *tp, /* transaction pointer */
2677 xfs_inode_t *ip, /* incore inode pointer */
2678 xfs_btree_cur_t *cur, /* btree cursor */
2679 int *logflagsp, /* inode logging flags */
2680 int whichfork) /* data or attr fork */
2681{
2682 /* REFERENCED */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002683 struct xfs_btree_block *cblock;/* child btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 xfs_fsblock_t cbno; /* child block number */
2685 xfs_buf_t *cbp; /* child block's buffer */
2686 int error; /* error return value */
2687 xfs_ifork_t *ifp; /* inode fork data */
2688 xfs_mount_t *mp; /* mount point structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10002689 __be64 *pp; /* ptr to block address */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002690 struct xfs_btree_block *rblock;/* root btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Christoph Hellwig60197e82008-10-30 17:11:19 +11002692 mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 ifp = XFS_IFORK_PTR(ip, whichfork);
2694 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2695 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2696 rblock = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11002697 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2698 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
Christoph Hellwig60197e82008-10-30 17:11:19 +11002699 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2700 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10002701 cbno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 *logflagsp = 0;
2703#ifdef DEBUG
Christoph Hellwig576039c2006-09-28 10:58:06 +10002704 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 return error;
2706#endif
Dave Chinner3d3e6f62012-11-12 22:54:08 +11002707 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
Dave Chinner1813dd62012-11-14 17:54:40 +11002708 &xfs_bmbt_buf_ops);
Dave Chinner3d3e6f62012-11-12 22:54:08 +11002709 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002711 cblock = XFS_BUF_TO_BLOCK(cbp);
2712 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return error;
2714 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2715 ip->i_d.di_nblocks--;
Christoph Hellwig7d095252009-06-08 15:33:32 +02002716 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 xfs_trans_binval(tp, cbp);
2718 if (cur->bc_bufs[0] == cbp)
2719 cur->bc_bufs[0] = NULL;
2720 xfs_iroot_realloc(ip, -1, whichfork);
2721 ASSERT(ifp->if_broot == NULL);
2722 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2723 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002724 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return 0;
2726}
2727
2728/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002729 * Called by xfs_bmapi to update file extent records and the btree
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 * after removing space (or undoing a delayed allocation).
2731 */
2732STATIC int /* error */
2733xfs_bmap_del_extent(
2734 xfs_inode_t *ip, /* incore inode pointer */
2735 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002736 xfs_extnum_t *idx, /* extent number to update/delete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 xfs_bmap_free_t *flist, /* list of extents to be freed */
2738 xfs_btree_cur_t *cur, /* if null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002739 xfs_bmbt_irec_t *del, /* data to remove from extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 int *logflagsp, /* inode logging flags */
Christoph Hellwig54893272011-05-11 15:04:03 +00002741 int whichfork) /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
2743 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2744 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2745 xfs_fsblock_t del_endblock=0; /* first block past del */
2746 xfs_fileoff_t del_endoff; /* first offset past del */
2747 int delay; /* current block is delayed allocated */
2748 int do_fx; /* free extent at end of routine */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002749 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 int error; /* error return value */
2751 int flags; /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 xfs_bmbt_irec_t got; /* current extent entry */
2753 xfs_fileoff_t got_endoff; /* first offset past got */
2754 int i; /* temp state */
2755 xfs_ifork_t *ifp; /* inode fork pointer */
2756 xfs_mount_t *mp; /* mount structure */
2757 xfs_filblks_t nblks; /* quota/sb block count */
2758 xfs_bmbt_irec_t new; /* new record to be inserted */
2759 /* REFERENCED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 uint qfield; /* quota field to update */
2761 xfs_filblks_t temp; /* for indirect length calculations */
2762 xfs_filblks_t temp2; /* for indirect length calculations */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002763 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 XFS_STATS_INC(xs_del_exlist);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002766
2767 if (whichfork == XFS_ATTR_FORK)
2768 state |= BMAP_ATTRFORK;
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 mp = ip->i_mount;
2771 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002772 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002773 (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 ASSERT(del->br_blockcount > 0);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002775 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 xfs_bmbt_get_all(ep, &got);
2777 ASSERT(got.br_startoff <= del->br_startoff);
2778 del_endoff = del->br_startoff + del->br_blockcount;
2779 got_endoff = got.br_startoff + got.br_blockcount;
2780 ASSERT(got_endoff >= del_endoff);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002781 delay = isnullstartblock(got.br_startblock);
2782 ASSERT(isnullstartblock(del->br_startblock) == delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 flags = 0;
2784 qfield = 0;
2785 error = 0;
2786 /*
2787 * If deleting a real allocation, must free up the disk space.
2788 */
2789 if (!delay) {
2790 flags = XFS_ILOG_CORE;
2791 /*
2792 * Realtime allocation. Free it and record di_nblocks update.
2793 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +11002794 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 xfs_fsblock_t bno;
2796 xfs_filblks_t len;
2797
2798 ASSERT(do_mod(del->br_blockcount,
2799 mp->m_sb.sb_rextsize) == 0);
2800 ASSERT(do_mod(del->br_startblock,
2801 mp->m_sb.sb_rextsize) == 0);
2802 bno = del->br_startblock;
2803 len = del->br_blockcount;
2804 do_div(bno, mp->m_sb.sb_rextsize);
2805 do_div(len, mp->m_sb.sb_rextsize);
Christoph Hellwigf3ca87382011-07-08 14:34:47 +02002806 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
2807 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 goto done;
2809 do_fx = 0;
2810 nblks = len * mp->m_sb.sb_rextsize;
2811 qfield = XFS_TRANS_DQ_RTBCOUNT;
2812 }
2813 /*
2814 * Ordinary allocation.
2815 */
2816 else {
2817 do_fx = 1;
2818 nblks = del->br_blockcount;
2819 qfield = XFS_TRANS_DQ_BCOUNT;
2820 }
2821 /*
2822 * Set up del_endblock and cur for later.
2823 */
2824 del_endblock = del->br_startblock + del->br_blockcount;
2825 if (cur) {
2826 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2827 got.br_startblock, got.br_blockcount,
2828 &i)))
2829 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002830 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 }
2832 da_old = da_new = 0;
2833 } else {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002834 da_old = startblockval(got.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 da_new = 0;
2836 nblks = 0;
2837 do_fx = 0;
2838 }
2839 /*
2840 * Set flag value to use in switch statement.
2841 * Left-contig is 2, right-contig is 1.
2842 */
2843 switch (((got.br_startoff == del->br_startoff) << 1) |
2844 (got_endoff == del_endoff)) {
2845 case 3:
2846 /*
2847 * Matches the whole extent. Delete the entry.
2848 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002849 xfs_iext_remove(ip, *idx, 1,
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002850 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
Christoph Hellwig233eebb2011-05-11 15:04:05 +00002851 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 if (delay)
2853 break;
Christoph Hellwig233eebb2011-05-11 15:04:05 +00002854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 XFS_IFORK_NEXT_SET(ip, whichfork,
2856 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2857 flags |= XFS_ILOG_CORE;
2858 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002859 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 break;
2861 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002862 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002864 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 break;
2866
2867 case 2:
2868 /*
2869 * Deleting the first part of the extent.
2870 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002871 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 xfs_bmbt_set_startoff(ep, del_endoff);
2873 temp = got.br_blockcount - del->br_blockcount;
2874 xfs_bmbt_set_blockcount(ep, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 if (delay) {
2876 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2877 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002878 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002879 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 da_new = temp;
2881 break;
2882 }
2883 xfs_bmbt_set_startblock(ep, del_endblock);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002884 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002886 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 break;
2888 }
2889 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2890 got.br_blockcount - del->br_blockcount,
2891 got.br_state)))
2892 goto done;
2893 break;
2894
2895 case 1:
2896 /*
2897 * Deleting the last part of the extent.
2898 */
2899 temp = got.br_blockcount - del->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002900 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 xfs_bmbt_set_blockcount(ep, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 if (delay) {
2903 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2904 da_old);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002905 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002906 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 da_new = temp;
2908 break;
2909 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002910 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 if (!cur) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002912 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 break;
2914 }
2915 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2916 got.br_startblock,
2917 got.br_blockcount - del->br_blockcount,
2918 got.br_state)))
2919 goto done;
2920 break;
2921
2922 case 0:
2923 /*
2924 * Deleting the middle of the extent.
2925 */
2926 temp = del->br_startoff - got.br_startoff;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002927 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 xfs_bmbt_set_blockcount(ep, temp);
2929 new.br_startoff = del_endoff;
2930 temp2 = got_endoff - del_endoff;
2931 new.br_blockcount = temp2;
2932 new.br_state = got.br_state;
2933 if (!delay) {
2934 new.br_startblock = del_endblock;
2935 flags |= XFS_ILOG_CORE;
2936 if (cur) {
2937 if ((error = xfs_bmbt_update(cur,
2938 got.br_startoff,
2939 got.br_startblock, temp,
2940 got.br_state)))
2941 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002942 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 goto done;
2944 cur->bc_rec.b = new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002945 error = xfs_btree_insert(cur, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 if (error && error != ENOSPC)
2947 goto done;
2948 /*
2949 * If get no-space back from btree insert,
2950 * it tried a split, and we have a zero
2951 * block reservation.
2952 * Fix up our state and return the error.
2953 */
2954 if (error == ENOSPC) {
2955 /*
2956 * Reset the cursor, don't trust
2957 * it after any insert operation.
2958 */
2959 if ((error = xfs_bmbt_lookup_eq(cur,
2960 got.br_startoff,
2961 got.br_startblock,
2962 temp, &i)))
2963 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002964 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 /*
2966 * Update the btree record back
2967 * to the original value.
2968 */
2969 if ((error = xfs_bmbt_update(cur,
2970 got.br_startoff,
2971 got.br_startblock,
2972 got.br_blockcount,
2973 got.br_state)))
2974 goto done;
2975 /*
2976 * Reset the extent record back
2977 * to the original value.
2978 */
2979 xfs_bmbt_set_blockcount(ep,
2980 got.br_blockcount);
2981 flags = 0;
2982 error = XFS_ERROR(ENOSPC);
2983 goto done;
2984 }
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002985 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 } else
Eric Sandeen9d87c312009-01-14 23:22:07 -06002987 flags |= xfs_ilog_fext(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 XFS_IFORK_NEXT_SET(ip, whichfork,
2989 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2990 } else {
2991 ASSERT(whichfork == XFS_DATA_FORK);
2992 temp = xfs_bmap_worst_indlen(ip, temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002993 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 temp2 = xfs_bmap_worst_indlen(ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002995 new.br_startblock = nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 da_new = temp + temp2;
2997 while (da_new > da_old) {
2998 if (temp) {
2999 temp--;
3000 da_new--;
3001 xfs_bmbt_set_startblock(ep,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003002 nullstartblock((int)temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 }
3004 if (da_new == da_old)
3005 break;
3006 if (temp2) {
3007 temp2--;
3008 da_new--;
3009 new.br_startblock =
Eric Sandeen9d87c312009-01-14 23:22:07 -06003010 nullstartblock((int)temp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 }
3012 }
3013 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00003014 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
3015 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
3016 ++*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 break;
3018 }
3019 /*
3020 * If we need to, add to list of extents to delete.
3021 */
3022 if (do_fx)
3023 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3024 mp);
3025 /*
3026 * Adjust inode # blocks in the file.
3027 */
3028 if (nblks)
3029 ip->i_d.di_nblocks -= nblks;
3030 /*
3031 * Adjust quota data.
3032 */
3033 if (qfield)
Christoph Hellwig7d095252009-06-08 15:33:32 +02003034 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
3036 /*
3037 * Account for change in delayed indirect blocks.
3038 * Nothing to do for disk quota accounting here.
3039 */
3040 ASSERT(da_old >= da_new);
Christoph Hellwig96540c72010-09-30 02:25:55 +00003041 if (da_old > da_new) {
3042 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00003043 (int64_t)(da_old - da_new), 0);
Christoph Hellwig96540c72010-09-30 02:25:55 +00003044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045done:
3046 *logflagsp = flags;
3047 return error;
3048}
3049
3050/*
3051 * Remove the entry "free" from the free item list. Prev points to the
3052 * previous entry, unless "free" is the head of the list.
3053 */
3054STATIC void
3055xfs_bmap_del_free(
3056 xfs_bmap_free_t *flist, /* free item list header */
3057 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
3058 xfs_bmap_free_item_t *free) /* list item to be freed */
3059{
3060 if (prev)
3061 prev->xbfi_next = free->xbfi_next;
3062 else
3063 flist->xbf_first = free->xbfi_next;
3064 flist->xbf_count--;
3065 kmem_zone_free(xfs_bmap_free_item_zone, free);
3066}
3067
3068/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 * Convert an extents-format file into a btree-format file.
3070 * The new file will have a root block (in the inode) and a single child block.
3071 */
3072STATIC int /* error */
3073xfs_bmap_extents_to_btree(
3074 xfs_trans_t *tp, /* transaction pointer */
3075 xfs_inode_t *ip, /* incore inode pointer */
3076 xfs_fsblock_t *firstblock, /* first-block-allocated */
3077 xfs_bmap_free_t *flist, /* blocks freed in xaction */
3078 xfs_btree_cur_t **curp, /* cursor returned to caller */
3079 int wasdel, /* converting a delayed alloc */
3080 int *logflagsp, /* inode logging flags */
3081 int whichfork) /* data or attr fork */
3082{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003083 struct xfs_btree_block *ablock; /* allocated (child) bt block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 xfs_buf_t *abp; /* buffer for ablock */
3085 xfs_alloc_arg_t args; /* allocation arguments */
3086 xfs_bmbt_rec_t *arp; /* child record pointer */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003087 struct xfs_btree_block *block; /* btree root block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 xfs_btree_cur_t *cur; /* bmap btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003089 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003091 xfs_extnum_t i, cnt; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 xfs_ifork_t *ifp; /* inode fork pointer */
3093 xfs_bmbt_key_t *kp; /* root block key pointer */
3094 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003095 xfs_extnum_t nextents; /* number of file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 xfs_bmbt_ptr_t *pp; /* root block address pointer */
3097
3098 ifp = XFS_IFORK_PTR(ip, whichfork);
3099 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 /*
3102 * Make space in the inode incore.
3103 */
3104 xfs_iroot_realloc(ip, 1, whichfork);
3105 ifp->if_flags |= XFS_IFBROOT;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 /*
3108 * Fill in the root.
3109 */
3110 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11003111 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3112 block->bb_level = cpu_to_be16(1);
3113 block->bb_numrecs = cpu_to_be16(1);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003114 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3115 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 /*
3118 * Need a cursor. Can't allocate until bb_level is filled in.
3119 */
3120 mp = ip->i_mount;
Christoph Hellwig561f7d12008-10-30 16:53:59 +11003121 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 cur->bc_private.b.firstblock = *firstblock;
3123 cur->bc_private.b.flist = flist;
3124 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3125 /*
3126 * Convert to a btree with two levels, one record in root.
3127 */
3128 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
Mark Tinguelya0041682012-09-20 13:16:45 -05003129 memset(&args, 0, sizeof(args));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 args.tp = tp;
3131 args.mp = mp;
Yingping Lud210a282006-06-09 14:55:18 +10003132 args.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 if (*firstblock == NULLFSBLOCK) {
3134 args.type = XFS_ALLOCTYPE_START_BNO;
3135 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3136 } else if (flist->xbf_low) {
3137 args.type = XFS_ALLOCTYPE_START_BNO;
3138 args.fsbno = *firstblock;
3139 } else {
3140 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3141 args.fsbno = *firstblock;
3142 }
3143 args.minlen = args.maxlen = args.prod = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 args.wasdel = wasdel;
3145 *logflagsp = 0;
3146 if ((error = xfs_alloc_vextent(&args))) {
3147 xfs_iroot_realloc(ip, -1, whichfork);
3148 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3149 return error;
3150 }
3151 /*
3152 * Allocation can't fail, the space was reserved.
3153 */
3154 ASSERT(args.fsbno != NULLFSBLOCK);
3155 ASSERT(*firstblock == NULLFSBLOCK ||
3156 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3157 (flist->xbf_low &&
3158 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3159 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3160 cur->bc_private.b.allocated++;
3161 ip->i_d.di_nblocks++;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003162 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3164 /*
3165 * Fill in the child block.
3166 */
Dave Chinner1813dd62012-11-14 17:54:40 +11003167 abp->b_ops = &xfs_bmbt_buf_ops;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003168 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003169 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 ablock->bb_level = 0;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003171 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3172 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003173 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003175 for (cnt = i = 0; i < nextents; i++) {
3176 ep = xfs_iext_get_ext(ifp, i);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003177 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10003178 arp->l0 = cpu_to_be64(ep->l0);
3179 arp->l1 = cpu_to_be64(ep->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 arp++; cnt++;
3181 }
3182 }
Christoph Hellwig16259e72005-11-02 15:11:25 +11003183 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003184 xfs_btree_set_numrecs(ablock, cnt);
3185
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 /*
3187 * Fill in the root key and pointer.
3188 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11003189 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3190 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Christoph Hellwig8801bb92006-09-28 10:58:17 +10003191 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
Christoph Hellwig136341b2008-10-30 17:11:40 +11003192 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3193 be16_to_cpu(block->bb_level)));
Christoph Hellwig576039c2006-09-28 10:58:06 +10003194 *pp = cpu_to_be64(args.fsbno);
Christoph Hellwig136341b2008-10-30 17:11:40 +11003195
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 /*
3197 * Do all this logging at the end so that
3198 * the root is at the right level.
3199 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11003200 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3201 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 ASSERT(*curp == NULL);
3203 *curp = cur;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003204 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 return 0;
3206}
3207
3208/*
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003209 * Calculate the default attribute fork offset for newly created inodes.
3210 */
3211uint
3212xfs_default_attroffset(
3213 struct xfs_inode *ip)
3214{
3215 struct xfs_mount *mp = ip->i_mount;
3216 uint offset;
3217
3218 if (mp->m_sb.sb_inodesize == 256) {
3219 offset = XFS_LITINO(mp) -
3220 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3221 } else {
3222 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3223 }
3224
3225 ASSERT(offset < XFS_LITINO(mp));
3226 return offset;
3227}
3228
3229/*
Nathan Scottd8cc8902005-11-02 10:34:53 +11003230 * Helper routine to reset inode di_forkoff field when switching
3231 * attribute fork from local to extent format - we reset it where
3232 * possible to make space available for inline data fork extents.
3233 */
3234STATIC void
3235xfs_bmap_forkoff_reset(
3236 xfs_mount_t *mp,
3237 xfs_inode_t *ip,
3238 int whichfork)
3239{
3240 if (whichfork == XFS_ATTR_FORK &&
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003241 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3242 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3243 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3244 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3245
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003246 if (dfl_forkoff > ip->i_d.di_forkoff)
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003247 ip->i_d.di_forkoff = dfl_forkoff;
Nathan Scottd8cc8902005-11-02 10:34:53 +11003248 }
3249}
3250
3251/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 * Convert a local file to an extents file.
3253 * This code is out of bounds for data forks of regular files,
3254 * since the file data needs to get logged so things will stay consistent.
3255 * (The bmap-level manipulations are ok, though).
3256 */
3257STATIC int /* error */
3258xfs_bmap_local_to_extents(
3259 xfs_trans_t *tp, /* transaction pointer */
3260 xfs_inode_t *ip, /* incore inode pointer */
3261 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
3262 xfs_extlen_t total, /* total blocks needed by transaction */
3263 int *logflagsp, /* inode logging flags */
Dave Chinner1e823792013-02-11 15:58:13 +11003264 int whichfork,
3265 void (*init_fn)(struct xfs_buf *bp,
3266 struct xfs_inode *ip,
3267 struct xfs_ifork *ifp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
3269 int error; /* error return value */
3270 int flags; /* logging flags returned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 xfs_ifork_t *ifp; /* inode fork pointer */
3272
3273 /*
3274 * We don't want to deal with the case of keeping inode data inline yet.
3275 * So sending the data fork of a regular inode is invalid.
3276 */
Al Viroabbede12011-07-26 02:31:30 -04003277 ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 ifp = XFS_IFORK_PTR(ip, whichfork);
3279 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3280 flags = 0;
3281 error = 0;
3282 if (ifp->if_bytes) {
3283 xfs_alloc_arg_t args; /* allocation arguments */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003284 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003285 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Dave Chinner1e823792013-02-11 15:58:13 +11003287 ASSERT((ifp->if_flags &
3288 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
Mark Tinguelya0041682012-09-20 13:16:45 -05003289 memset(&args, 0, sizeof(args));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 args.tp = tp;
3291 args.mp = ip->i_mount;
Yingping Lud210a282006-06-09 14:55:18 +10003292 args.firstblock = *firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 /*
3294 * Allocate a block. We know we need only one, since the
3295 * file currently fits in an inode.
3296 */
3297 if (*firstblock == NULLFSBLOCK) {
3298 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3299 args.type = XFS_ALLOCTYPE_START_BNO;
3300 } else {
3301 args.fsbno = *firstblock;
3302 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3303 }
3304 args.total = total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 args.minlen = args.maxlen = args.prod = 1;
Dave Chinner1e823792013-02-11 15:58:13 +11003306 error = xfs_alloc_vextent(&args);
3307 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 goto done;
Dave Chinner1e823792013-02-11 15:58:13 +11003309
3310 /* Can't fail, the space was reserved. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 ASSERT(args.fsbno != NULLFSBLOCK);
3312 ASSERT(args.len == 1);
3313 *firstblock = args.fsbno;
3314 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
Dave Chinner1e823792013-02-11 15:58:13 +11003315
3316 /* initialise the block and copy the data */
3317 init_fn(bp, ip, ifp);
3318
3319 /* account for the change in fork size and log everything */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003321 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003323 xfs_iext_add(ifp, 0, 1);
3324 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003326 trace_xfs_bmap_post_update(ip, 0,
3327 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3328 _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3330 ip->i_d.di_nblocks = 1;
Christoph Hellwig7d095252009-06-08 15:33:32 +02003331 xfs_trans_mod_dquot_byino(tp, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 XFS_TRANS_DQ_BCOUNT, 1L);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003333 flags |= xfs_ilog_fext(whichfork);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003334 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003336 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 ifp->if_flags &= ~XFS_IFINLINE;
3339 ifp->if_flags |= XFS_IFEXTENTS;
3340 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3341 flags |= XFS_ILOG_CORE;
3342done:
3343 *logflagsp = flags;
3344 return error;
3345}
3346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347/*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003348 * Search the extent records for the entry containing block bno.
3349 * If bno lies in a hole, point to the next entry. If bno lies
3350 * past eof, *eofp will be set, and *prevp will contain the last
3351 * entry (null if none). Else, *lastxp will be set to the index
3352 * of the found entry; *gotp will contain the entry.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003353 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003354STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003355xfs_bmap_search_multi_extents(
3356 xfs_ifork_t *ifp, /* inode fork pointer */
3357 xfs_fileoff_t bno, /* block number searched for */
3358 int *eofp, /* out: end of file found */
3359 xfs_extnum_t *lastxp, /* out: last extent index */
3360 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3361 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3362{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003363 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003364 xfs_extnum_t lastx; /* last extent index */
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003365
3366 /*
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003367 * Initialize the extent entry structure to catch access to
3368 * uninitialized br_startblock field.
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003369 */
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003370 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3371 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3372 gotp->br_state = XFS_EXT_INVALID;
3373#if XFS_BIG_BLKNOS
3374 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3375#else
3376 gotp->br_startblock = 0xffffa5a5;
3377#endif
3378 prevp->br_startoff = NULLFILEOFF;
3379
3380 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3381 if (lastx > 0) {
3382 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3383 }
3384 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3385 xfs_bmbt_get_all(ep, gotp);
3386 *eofp = 0;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003387 } else {
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003388 if (lastx > 0) {
3389 *gotp = *prevp;
3390 }
3391 *eofp = 1;
3392 ep = NULL;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003393 }
Mandy Kirkconnell8867bc92006-03-17 17:25:04 +11003394 *lastxp = lastx;
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003395 return ep;
3396}
3397
3398/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 * Search the extents list for the inode, for the extent containing bno.
3400 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3401 * *eofp will be set, and *prevp will contain the last entry (null if none).
3402 * Else, *lastxp will be set to the index of the found
3403 * entry; *gotp will contain the entry.
3404 */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003405STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406xfs_bmap_search_extents(
3407 xfs_inode_t *ip, /* incore inode pointer */
3408 xfs_fileoff_t bno, /* block number searched for */
Nathan Scott572d95f2006-09-28 11:03:20 +10003409 int fork, /* data or attr fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 int *eofp, /* out: end of file found */
3411 xfs_extnum_t *lastxp, /* out: last extent index */
3412 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3413 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3414{
3415 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003416 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
3418 XFS_STATS_INC(xs_look_exlist);
Nathan Scott572d95f2006-09-28 11:03:20 +10003419 ifp = XFS_IFORK_PTR(ip, fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
Mandy Kirkconnell0293ce32006-03-14 13:30:23 +11003421 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3422
Nathan Scott572d95f2006-09-28 11:03:20 +10003423 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3424 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
Dave Chinner6a19d932011-03-07 10:02:35 +11003425 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
Nathan Scott572d95f2006-09-28 11:03:20 +10003426 "Access to block zero in inode %llu "
3427 "start_block: %llx start_off: %llx "
3428 "blkcnt: %llx extent-state: %x lastx: %x\n",
3429 (unsigned long long)ip->i_ino,
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003430 (unsigned long long)gotp->br_startblock,
3431 (unsigned long long)gotp->br_startoff,
3432 (unsigned long long)gotp->br_blockcount,
Nathan Scott572d95f2006-09-28 11:03:20 +10003433 gotp->br_state, *lastxp);
3434 *lastxp = NULLEXTNUM;
3435 *eofp = 1;
3436 return NULL;
3437 }
3438 return ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439}
3440
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441/*
3442 * Compute the worst-case number of indirect blocks that will be used
3443 * for ip's delayed extent of length "len".
3444 */
3445STATIC xfs_filblks_t
3446xfs_bmap_worst_indlen(
3447 xfs_inode_t *ip, /* incore inode pointer */
3448 xfs_filblks_t len) /* delayed extent length */
3449{
3450 int level; /* btree level number */
3451 int maxrecs; /* maximum record count at this level */
3452 xfs_mount_t *mp; /* mount structure */
3453 xfs_filblks_t rval; /* return value */
3454
3455 mp = ip->i_mount;
3456 maxrecs = mp->m_bmap_dmxr[0];
3457 for (level = 0, rval = 0;
3458 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3459 level++) {
3460 len += maxrecs - 1;
3461 do_div(len, maxrecs);
3462 rval += len;
3463 if (len == 1)
3464 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3465 level - 1;
3466 if (level == 0)
3467 maxrecs = mp->m_bmap_dmxr[1];
3468 }
3469 return rval;
3470}
3471
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472/*
3473 * Convert inode from non-attributed to attributed.
3474 * Must not be in a transaction, ip must not be locked.
3475 */
3476int /* error code */
3477xfs_bmap_add_attrfork(
3478 xfs_inode_t *ip, /* incore inode pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003479 int size, /* space new attribute needs */
3480 int rsvd) /* xact may use reserved blks */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003483 xfs_bmap_free_t flist; /* freed extent records */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 xfs_trans_t *tp; /* transaction pointer */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003486 int blks; /* space reservation */
3487 int version = 1; /* superblock attr version */
3488 int committed; /* xaction was committed */
3489 int logflags; /* logging flags */
3490 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
Nathan Scottd8cc8902005-11-02 10:34:53 +11003492 ASSERT(XFS_IFORK_Q(ip) == 0);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003493
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 mp = ip->i_mount;
3495 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3496 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3497 blks = XFS_ADDAFORK_SPACE_RES(mp);
3498 if (rsvd)
3499 tp->t_flags |= XFS_TRANS_RESERVE;
3500 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3501 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3502 goto error0;
3503 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig7d095252009-06-08 15:33:32 +02003504 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3506 XFS_QMOPT_RES_REGBLKS);
3507 if (error) {
3508 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3509 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3510 return error;
3511 }
3512 if (XFS_IFORK_Q(ip))
3513 goto error1;
3514 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3515 /*
3516 * For inodes coming from pre-6.2 filesystems.
3517 */
3518 ASSERT(ip->i_d.di_aformat == 0);
3519 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3520 }
3521 ASSERT(ip->i_d.di_anextents == 0);
Christoph Hellwig898621d2010-06-24 11:36:58 +10003522
Christoph Hellwigddc34152011-09-19 15:00:54 +00003523 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig898621d2010-06-24 11:36:58 +10003525
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 switch (ip->i_d.di_format) {
3527 case XFS_DINODE_FMT_DEV:
3528 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3529 break;
3530 case XFS_DINODE_FMT_UUID:
3531 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3532 break;
3533 case XFS_DINODE_FMT_LOCAL:
3534 case XFS_DINODE_FMT_EXTENTS:
3535 case XFS_DINODE_FMT_BTREE:
Nathan Scottd8cc8902005-11-02 10:34:53 +11003536 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3537 if (!ip->i_d.di_forkoff)
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003538 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
Nathan Scott13059ff2006-01-11 15:32:01 +11003539 else if (mp->m_flags & XFS_MOUNT_ATTR2)
Nathan Scottd8cc8902005-11-02 10:34:53 +11003540 version = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 break;
3542 default:
3543 ASSERT(0);
3544 error = XFS_ERROR(EINVAL);
3545 goto error1;
3546 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003547
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 ASSERT(ip->i_afp == NULL);
3549 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 ip->i_afp->if_flags = XFS_IFEXTENTS;
3551 logflags = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003552 xfs_bmap_init(&flist, &firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 switch (ip->i_d.di_format) {
3554 case XFS_DINODE_FMT_LOCAL:
3555 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3556 &logflags);
3557 break;
3558 case XFS_DINODE_FMT_EXTENTS:
3559 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3560 &flist, &logflags);
3561 break;
3562 case XFS_DINODE_FMT_BTREE:
3563 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3564 &logflags);
3565 break;
3566 default:
3567 error = 0;
3568 break;
3569 }
3570 if (logflags)
3571 xfs_trans_log_inode(tp, ip, logflags);
3572 if (error)
3573 goto error2;
Eric Sandeen62118702008-03-06 13:44:28 +11003574 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3575 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Nathan Scottda087ba2005-11-02 15:00:20 +11003576 __int64_t sbfields = 0;
3577
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003578 spin_lock(&mp->m_sb_lock);
Eric Sandeen62118702008-03-06 13:44:28 +11003579 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3580 xfs_sb_version_addattr(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003581 sbfields |= XFS_SB_VERSIONNUM;
Nathan Scottd8cc8902005-11-02 10:34:53 +11003582 }
Eric Sandeen62118702008-03-06 13:44:28 +11003583 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3584 xfs_sb_version_addattr2(&mp->m_sb);
Nathan Scottda087ba2005-11-02 15:00:20 +11003585 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003586 }
Nathan Scottda087ba2005-11-02 15:00:20 +11003587 if (sbfields) {
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003588 spin_unlock(&mp->m_sb_lock);
Nathan Scottda087ba2005-11-02 15:00:20 +11003589 xfs_mod_sb(tp, sbfields);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 } else
Eric Sandeen3685c2a2007-10-11 17:42:32 +10003591 spin_unlock(&mp->m_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003593
3594 error = xfs_bmap_finish(&tp, &flist, &committed);
3595 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 goto error2;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003597 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598error2:
3599 xfs_bmap_cancel(&flist);
3600error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3602error0:
3603 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 return error;
3605}
3606
3607/*
3608 * Add the extent to the list of extents to be free at transaction end.
3609 * The list is maintained sorted (by block number).
3610 */
3611/* ARGSUSED */
3612void
3613xfs_bmap_add_free(
3614 xfs_fsblock_t bno, /* fs block number of extent */
3615 xfs_filblks_t len, /* length of extent */
3616 xfs_bmap_free_t *flist, /* list of extents */
3617 xfs_mount_t *mp) /* mount point structure */
3618{
3619 xfs_bmap_free_item_t *cur; /* current (next) element */
3620 xfs_bmap_free_item_t *new; /* new element */
3621 xfs_bmap_free_item_t *prev; /* previous element */
3622#ifdef DEBUG
3623 xfs_agnumber_t agno;
3624 xfs_agblock_t agbno;
3625
3626 ASSERT(bno != NULLFSBLOCK);
3627 ASSERT(len > 0);
3628 ASSERT(len <= MAXEXTLEN);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003629 ASSERT(!isnullstartblock(bno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 agno = XFS_FSB_TO_AGNO(mp, bno);
3631 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3632 ASSERT(agno < mp->m_sb.sb_agcount);
3633 ASSERT(agbno < mp->m_sb.sb_agblocks);
3634 ASSERT(len < mp->m_sb.sb_agblocks);
3635 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3636#endif
3637 ASSERT(xfs_bmap_free_item_zone != NULL);
3638 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3639 new->xbfi_startblock = bno;
3640 new->xbfi_blockcount = (xfs_extlen_t)len;
3641 for (prev = NULL, cur = flist->xbf_first;
3642 cur != NULL;
3643 prev = cur, cur = cur->xbfi_next) {
3644 if (cur->xbfi_startblock >= bno)
3645 break;
3646 }
3647 if (prev)
3648 prev->xbfi_next = new;
3649 else
3650 flist->xbf_first = new;
3651 new->xbfi_next = cur;
3652 flist->xbf_count++;
3653}
3654
3655/*
3656 * Compute and fill in the value of the maximum depth of a bmap btree
3657 * in this filesystem. Done once, during mount.
3658 */
3659void
3660xfs_bmap_compute_maxlevels(
3661 xfs_mount_t *mp, /* file system mount structure */
3662 int whichfork) /* data or attr fork */
3663{
3664 int level; /* btree level */
3665 uint maxblocks; /* max blocks at this level */
3666 uint maxleafents; /* max leaf entries possible */
3667 int maxrootrecs; /* max records in root block */
3668 int minleafrecs; /* min records in leaf block */
3669 int minnoderecs; /* min records in node block */
3670 int sz; /* root block size */
3671
3672 /*
3673 * The maximum number of extents in a file, hence the maximum
3674 * number of leaf entries, is controlled by the type of di_nextents
3675 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3676 * (a signed 16-bit number, xfs_aextnum_t).
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003677 *
3678 * Note that we can no longer assume that if we are in ATTR1 that
Christoph Hellwig1a5902c2009-03-29 19:26:46 +02003679 * the fork offset of all the inodes will be
3680 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
3681 * with ATTR2 and then mounted back with ATTR1, keeping the
3682 * di_forkoff's fixed but probably at various positions. Therefore,
3683 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
3684 * of a minimum size available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 */
Nathan Scottd8cc8902005-11-02 10:34:53 +11003686 if (whichfork == XFS_DATA_FORK) {
3687 maxleafents = MAXEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003688 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003689 } else {
3690 maxleafents = MAXAEXTNUM;
Tim Shimmin6d1337b2008-04-17 16:50:16 +10003691 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
Nathan Scottd8cc8902005-11-02 10:34:53 +11003692 }
Christoph Hellwig60197e82008-10-30 17:11:19 +11003693 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 minleafrecs = mp->m_bmap_dmnr[0];
3695 minnoderecs = mp->m_bmap_dmnr[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3697 for (level = 1; maxblocks > 1; level++) {
3698 if (maxblocks <= maxrootrecs)
3699 maxblocks = 1;
3700 else
3701 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3702 }
3703 mp->m_bm_maxlevels[whichfork] = level;
3704}
3705
3706/*
3707 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
3708 * caller. Frees all the extents that need freeing, which must be done
3709 * last due to locking considerations. We never free any extents in
Christoph Hellwig859f57c2011-08-27 14:45:11 +00003710 * the first transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 *
3712 * Return 1 if the given transaction was committed and a new one
3713 * started, and 0 otherwise in the committed parameter.
3714 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715int /* error */
3716xfs_bmap_finish(
3717 xfs_trans_t **tp, /* transaction pointer addr */
3718 xfs_bmap_free_t *flist, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 int *committed) /* xact committed or not */
3720{
3721 xfs_efd_log_item_t *efd; /* extent free data */
3722 xfs_efi_log_item_t *efi; /* extent free intention */
3723 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003724 xfs_bmap_free_item_t *free; /* free extent item */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 unsigned int logres; /* new log reservation */
3726 unsigned int logcount; /* new log count */
3727 xfs_mount_t *mp; /* filesystem mount structure */
3728 xfs_bmap_free_item_t *next; /* next item on free list */
3729 xfs_trans_t *ntp; /* new transaction pointer */
3730
3731 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3732 if (flist->xbf_count == 0) {
3733 *committed = 0;
3734 return 0;
3735 }
3736 ntp = *tp;
3737 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3738 for (free = flist->xbf_first; free; free = free->xbfi_next)
3739 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3740 free->xbfi_blockcount);
3741 logres = ntp->t_log_res;
3742 logcount = ntp->t_log_count;
3743 ntp = xfs_trans_dup(*tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003744 error = xfs_trans_commit(*tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 *tp = ntp;
3746 *committed = 1;
3747 /*
3748 * We have a new transaction, so we should return committed=1,
3749 * even though we're returning an error.
3750 */
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003751 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 return error;
Dave Chinnercc09c0d2008-11-17 17:37:10 +11003753
3754 /*
3755 * transaction commit worked ok so we can drop the extra ticket
3756 * reference that we gained in xfs_trans_dup()
3757 */
3758 xfs_log_ticket_put(ntp->t_ticket);
3759
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3761 logcount)))
3762 return error;
3763 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3764 for (free = flist->xbf_first; free != NULL; free = next) {
3765 next = free->xbfi_next;
3766 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3767 free->xbfi_blockcount))) {
3768 /*
3769 * The bmap free list will be cleaned up at a
3770 * higher level. The EFI will be canceled when
3771 * this transaction is aborted.
3772 * Need to force shutdown here to make sure it
3773 * happens, since this transaction may not be
3774 * dirty yet.
3775 */
3776 mp = ntp->t_mountp;
3777 if (!XFS_FORCED_SHUTDOWN(mp))
3778 xfs_force_shutdown(mp,
3779 (error == EFSCORRUPTED) ?
Nathan Scott7d04a332006-06-09 14:58:38 +10003780 SHUTDOWN_CORRUPT_INCORE :
3781 SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 return error;
3783 }
3784 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3785 free->xbfi_blockcount);
3786 xfs_bmap_del_free(flist, NULL, free);
3787 }
3788 return 0;
3789}
3790
3791/*
3792 * Free up any items left in the list.
3793 */
3794void
3795xfs_bmap_cancel(
3796 xfs_bmap_free_t *flist) /* list of bmap_free_items */
3797{
3798 xfs_bmap_free_item_t *free; /* free list item */
3799 xfs_bmap_free_item_t *next;
3800
3801 if (flist->xbf_count == 0)
3802 return;
3803 ASSERT(flist->xbf_first != NULL);
3804 for (free = flist->xbf_first; free; free = next) {
3805 next = free->xbfi_next;
3806 xfs_bmap_del_free(flist, NULL, free);
3807 }
3808 ASSERT(flist->xbf_count == 0);
3809}
3810
3811/*
3812 * Returns the file-relative block number of the first unused block(s)
3813 * in the file with at least "len" logically contiguous blocks free.
3814 * This is the lowest-address hole if the file has holes, else the first block
3815 * past the end of file.
3816 * Return 0 if the file is currently local (in-inode).
3817 */
3818int /* error */
3819xfs_bmap_first_unused(
3820 xfs_trans_t *tp, /* transaction pointer */
3821 xfs_inode_t *ip, /* incore inode */
3822 xfs_extlen_t len, /* size of hole to find */
3823 xfs_fileoff_t *first_unused, /* unused block */
3824 int whichfork) /* data or attr fork */
3825{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003827 int idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 xfs_ifork_t *ifp; /* inode fork pointer */
3829 xfs_fileoff_t lastaddr; /* last block number seen */
3830 xfs_fileoff_t lowest; /* lowest useful block */
3831 xfs_fileoff_t max; /* starting useful block */
3832 xfs_fileoff_t off; /* offset for this block */
3833 xfs_extnum_t nextents; /* number of extent entries */
3834
3835 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3836 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3837 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3838 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3839 *first_unused = 0;
3840 return 0;
3841 }
3842 ifp = XFS_IFORK_PTR(ip, whichfork);
3843 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3844 (error = xfs_iread_extents(tp, ip, whichfork)))
3845 return error;
3846 lowest = *first_unused;
3847 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003848 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003849 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 off = xfs_bmbt_get_startoff(ep);
3851 /*
3852 * See if the hole before this extent will work.
3853 */
3854 if (off >= lowest + len && off - max >= len) {
3855 *first_unused = max;
3856 return 0;
3857 }
3858 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3859 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3860 }
3861 *first_unused = max;
3862 return 0;
3863}
3864
3865/*
3866 * Returns the file-relative block number of the last block + 1 before
3867 * last_block (input value) in the file.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003868 * This is not based on i_size, it is based on the extent records.
3869 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 */
3871int /* error */
3872xfs_bmap_last_before(
3873 xfs_trans_t *tp, /* transaction pointer */
3874 xfs_inode_t *ip, /* incore inode */
3875 xfs_fileoff_t *last_block, /* last block */
3876 int whichfork) /* data or attr fork */
3877{
3878 xfs_fileoff_t bno; /* input file offset */
3879 int eof; /* hit end of file */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10003880 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 int error; /* error return value */
3882 xfs_bmbt_irec_t got; /* current extent value */
3883 xfs_ifork_t *ifp; /* inode fork pointer */
3884 xfs_extnum_t lastx; /* last extent used */
3885 xfs_bmbt_irec_t prev; /* previous extent value */
3886
3887 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3888 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3889 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3890 return XFS_ERROR(EIO);
3891 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3892 *last_block = 0;
3893 return 0;
3894 }
3895 ifp = XFS_IFORK_PTR(ip, whichfork);
3896 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3897 (error = xfs_iread_extents(tp, ip, whichfork)))
3898 return error;
3899 bno = *last_block - 1;
3900 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3901 &prev);
3902 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3903 if (prev.br_startoff == NULLFILEOFF)
3904 *last_block = 0;
3905 else
3906 *last_block = prev.br_startoff + prev.br_blockcount;
3907 }
3908 /*
3909 * Otherwise *last_block is already the right answer.
3910 */
3911 return 0;
3912}
3913
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00003914STATIC int
3915xfs_bmap_last_extent(
3916 struct xfs_trans *tp,
3917 struct xfs_inode *ip,
3918 int whichfork,
3919 struct xfs_bmbt_irec *rec,
3920 int *is_empty)
3921{
3922 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3923 int error;
3924 int nextents;
3925
3926 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3927 error = xfs_iread_extents(tp, ip, whichfork);
3928 if (error)
3929 return error;
3930 }
3931
3932 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
3933 if (nextents == 0) {
3934 *is_empty = 1;
3935 return 0;
3936 }
3937
3938 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
3939 *is_empty = 0;
3940 return 0;
3941}
3942
3943/*
3944 * Check the last inode extent to determine whether this allocation will result
3945 * in blocks being allocated at the end of the file. When we allocate new data
3946 * blocks at the end of the file which do not start at the previous data block,
3947 * we will try to align the new blocks at stripe unit boundaries.
3948 *
Dave Chinner1b164472011-09-18 20:40:55 +00003949 * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be
3950 * at, or past the EOF.
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00003951 */
3952STATIC int
3953xfs_bmap_isaeof(
Dave Chinner1b164472011-09-18 20:40:55 +00003954 struct xfs_bmalloca *bma,
3955 int whichfork)
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00003956{
3957 struct xfs_bmbt_irec rec;
3958 int is_empty;
3959 int error;
3960
Dave Chinner1b164472011-09-18 20:40:55 +00003961 bma->aeof = 0;
3962 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
3963 &is_empty);
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00003964 if (error || is_empty)
3965 return error;
3966
3967 /*
3968 * Check if we are allocation or past the last extent, or at least into
3969 * the last delayed allocated extent.
3970 */
Dave Chinner3a756672011-09-18 20:40:58 +00003971 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
3972 (bma->offset >= rec.br_startoff &&
Dave Chinner1b164472011-09-18 20:40:55 +00003973 isnullstartblock(rec.br_startblock));
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00003974 return 0;
3975}
3976
3977/*
3978 * Check if the endoff is outside the last extent. If so the caller will grow
3979 * the allocation to a stripe unit boundary. All offsets are considered outside
3980 * the end of file for an empty fork, so 1 is returned in *eof in that case.
3981 */
3982int
3983xfs_bmap_eof(
3984 struct xfs_inode *ip,
3985 xfs_fileoff_t endoff,
3986 int whichfork,
3987 int *eof)
3988{
3989 struct xfs_bmbt_irec rec;
3990 int error;
3991
3992 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
3993 if (error || *eof)
3994 return error;
3995
3996 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
3997 return 0;
3998}
3999
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000/*
4001 * Returns the file-relative block number of the first block past eof in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004002 * the file. This is not based on i_size, it is based on the extent records.
4003 * Returns 0 for local files, as they do not have extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 */
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00004005int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006xfs_bmap_last_offset(
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00004007 struct xfs_trans *tp,
4008 struct xfs_inode *ip,
4009 xfs_fileoff_t *last_block,
4010 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011{
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00004012 struct xfs_bmbt_irec rec;
4013 int is_empty;
4014 int error;
4015
4016 *last_block = 0;
4017
4018 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
4019 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
4021 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00004022 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 return XFS_ERROR(EIO);
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00004024
4025 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
4026 if (error || is_empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 return error;
Christoph Hellwig27a3f8f2011-09-18 20:40:53 +00004028
4029 *last_block = rec.br_startoff + rec.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 return 0;
4031}
4032
4033/*
4034 * Returns whether the selected fork of the inode has exactly one
4035 * block or not. For the data fork we check this matches di_size,
4036 * implying the file's range is 0..bsize-1.
4037 */
4038int /* 1=>1 block, 0=>otherwise */
4039xfs_bmap_one_block(
4040 xfs_inode_t *ip, /* incore inode */
4041 int whichfork) /* data or attr fork */
4042{
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004043 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 xfs_ifork_t *ifp; /* inode fork pointer */
4045 int rval; /* return value */
4046 xfs_bmbt_irec_t s; /* internal version of extent */
4047
4048#ifndef DEBUG
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00004049 if (whichfork == XFS_DATA_FORK)
4050 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051#endif /* !DEBUG */
4052 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4053 return 0;
4054 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4055 return 0;
4056 ifp = XFS_IFORK_PTR(ip, whichfork);
4057 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004058 ep = xfs_iext_get_ext(ifp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 xfs_bmbt_get_all(ep, &s);
4060 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4061 if (rval && whichfork == XFS_DATA_FORK)
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00004062 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 return rval;
4064}
4065
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004066STATIC int
4067xfs_bmap_sanity_check(
4068 struct xfs_mount *mp,
4069 struct xfs_buf *bp,
4070 int level)
4071{
4072 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4073
Christoph Hellwig69ef9212011-07-08 14:36:05 +02004074 if (block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC) ||
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004075 be16_to_cpu(block->bb_level) != level ||
4076 be16_to_cpu(block->bb_numrecs) == 0 ||
4077 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4078 return 0;
4079 return 1;
4080}
4081
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082/*
4083 * Read in the extents to if_extents.
4084 * All inode fields are set up by caller, we just traverse the btree
4085 * and copy the records in. If the file system cannot contain unwritten
4086 * extents, the records are checked for no "state" flags.
4087 */
4088int /* error */
4089xfs_bmap_read_extents(
4090 xfs_trans_t *tp, /* transaction pointer */
4091 xfs_inode_t *ip, /* incore inode */
4092 int whichfork) /* data or attr fork */
4093{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004094 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 xfs_fsblock_t bno; /* block # of "block" */
4096 xfs_buf_t *bp; /* buffer for "block" */
4097 int error; /* error return value */
4098 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 xfs_extnum_t i, j; /* index into the extents list */
4100 xfs_ifork_t *ifp; /* fork structure */
4101 int level; /* btree level, for checking */
4102 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10004103 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 /* REFERENCED */
4105 xfs_extnum_t room; /* number of entries there's room for */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
4107 bno = NULLFSBLOCK;
4108 mp = ip->i_mount;
4109 ifp = XFS_IFORK_PTR(ip, whichfork);
4110 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4111 XFS_EXTFMT_INODE(ip);
4112 block = ifp->if_broot;
4113 /*
4114 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
4115 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11004116 level = be16_to_cpu(block->bb_level);
4117 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11004118 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004119 bno = be64_to_cpu(*pp);
4120 ASSERT(bno != NULLDFSBNO);
4121 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4122 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 /*
4124 * Go down the tree until leaf level is reached, following the first
4125 * pointer (leftmost) at each level.
4126 */
4127 while (level-- > 0) {
Dave Chinner3d3e6f62012-11-12 22:54:08 +11004128 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
Dave Chinner1813dd62012-11-14 17:54:40 +11004129 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
Dave Chinner3d3e6f62012-11-12 22:54:08 +11004130 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004132 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004134 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 error0);
4136 if (level == 0)
4137 break;
Christoph Hellwig136341b2008-10-30 17:11:40 +11004138 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10004139 bno = be64_to_cpu(*pp);
4140 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 xfs_trans_brelse(tp, bp);
4142 }
4143 /*
4144 * Here with bp and block set to the leftmost leaf node in the tree.
4145 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004146 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 i = 0;
4148 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004149 * Loop over all leaf nodes. Copy information to the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 */
4151 for (;;) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004152 xfs_bmbt_rec_t *frp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 xfs_fsblock_t nextbno;
4154 xfs_extnum_t num_recs;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004155 xfs_extnum_t start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004157 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 if (unlikely(i + num_recs > room)) {
4159 ASSERT(i + num_recs <= room);
Dave Chinner65333b42011-03-07 10:03:35 +11004160 xfs_warn(ip->i_mount,
Nathan Scott3762ec62006-01-12 10:29:53 +11004161 "corrupt dinode %Lu, (btree extents).",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 (unsigned long long) ip->i_ino);
Dave Chinner65333b42011-03-07 10:03:35 +11004163 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
4164 XFS_ERRLEVEL_LOW, ip->i_mount, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 goto error0;
4166 }
4167 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11004168 xfs_bmap_sanity_check(mp, bp, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 error0);
4170 /*
4171 * Read-ahead the next leaf block, if any.
4172 */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004173 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 if (nextbno != NULLFSBLOCK)
Dave Chinner3d3e6f62012-11-12 22:54:08 +11004175 xfs_btree_reada_bufl(mp, nextbno, 1,
Dave Chinner1813dd62012-11-14 17:54:40 +11004176 &xfs_bmbt_buf_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 /*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004178 * Copy records into the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11004180 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004181 start = i;
4182 for (j = 0; j < num_recs; j++, i++, frp++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10004183 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
Christoph Hellwigcd8b0a92007-08-16 16:24:15 +10004184 trp->l0 = be64_to_cpu(frp->l0);
4185 trp->l1 = be64_to_cpu(frp->l1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 }
4187 if (exntf == XFS_EXTFMT_NOSTATE) {
4188 /*
4189 * Check all attribute bmap btree records and
4190 * any "older" data bmap btree records for a
4191 * set bit in the "extent flag" position.
4192 */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004193 if (unlikely(xfs_check_nostate_extents(ifp,
4194 start, num_recs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4196 XFS_ERRLEVEL_LOW,
4197 ip->i_mount);
4198 goto error0;
4199 }
4200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 xfs_trans_brelse(tp, bp);
4202 bno = nextbno;
4203 /*
4204 * If we've reached the end, stop.
4205 */
4206 if (bno == NULLFSBLOCK)
4207 break;
Dave Chinner3d3e6f62012-11-12 22:54:08 +11004208 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
Dave Chinner1813dd62012-11-14 17:54:40 +11004209 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
Dave Chinner3d3e6f62012-11-12 22:54:08 +11004210 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 return error;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11004212 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004214 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
Eric Sandeen3a59c942007-07-11 11:09:47 +10004216 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 return 0;
4218error0:
4219 xfs_trans_brelse(tp, bp);
4220 return XFS_ERROR(EFSCORRUPTED);
4221}
4222
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004223#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004225 * Add bmap trace insert entries for all the contents of the extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 */
4227void
4228xfs_bmap_trace_exlist(
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 xfs_inode_t *ip, /* incore inode pointer */
4230 xfs_extnum_t cnt, /* count of entries in the list */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004231 int whichfork, /* data or attr fork */
4232 unsigned long caller_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004234 xfs_extnum_t idx; /* extent record index */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004236 int state = 0;
4237
4238 if (whichfork == XFS_ATTR_FORK)
4239 state |= BMAP_ATTRFORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
4241 ifp = XFS_IFORK_PTR(ip, whichfork);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004242 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00004243 for (idx = 0; idx < cnt; idx++)
4244 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247/*
4248 * Validate that the bmbt_irecs being returned from bmapi are valid
4249 * given the callers original parameters. Specifically check the
4250 * ranges of the returned irecs to ensure that they only extent beyond
4251 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
4252 */
4253STATIC void
4254xfs_bmap_validate_ret(
4255 xfs_fileoff_t bno,
4256 xfs_filblks_t len,
4257 int flags,
4258 xfs_bmbt_irec_t *mval,
4259 int nmap,
4260 int ret_nmap)
4261{
4262 int i; /* index to map values */
4263
4264 ASSERT(ret_nmap <= nmap);
4265
4266 for (i = 0; i < ret_nmap; i++) {
4267 ASSERT(mval[i].br_blockcount > 0);
4268 if (!(flags & XFS_BMAPI_ENTIRE)) {
4269 ASSERT(mval[i].br_startoff >= bno);
4270 ASSERT(mval[i].br_blockcount <= len);
4271 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4272 bno + len);
4273 } else {
4274 ASSERT(mval[i].br_startoff < bno + len);
4275 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4276 bno);
4277 }
4278 ASSERT(i == 0 ||
4279 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4280 mval[i].br_startoff);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004281 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4282 mval[i].br_startblock != HOLESTARTBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4284 mval[i].br_state == XFS_EXT_UNWRITTEN);
4285 }
4286}
4287#endif /* DEBUG */
4288
4289
4290/*
Dave Chinneraef9a892011-09-18 20:40:44 +00004291 * Trim the returned map to the required bounds
4292 */
4293STATIC void
4294xfs_bmapi_trim_map(
4295 struct xfs_bmbt_irec *mval,
4296 struct xfs_bmbt_irec *got,
4297 xfs_fileoff_t *bno,
4298 xfs_filblks_t len,
4299 xfs_fileoff_t obno,
4300 xfs_fileoff_t end,
4301 int n,
4302 int flags)
4303{
4304 if ((flags & XFS_BMAPI_ENTIRE) ||
4305 got->br_startoff + got->br_blockcount <= obno) {
4306 *mval = *got;
4307 if (isnullstartblock(got->br_startblock))
4308 mval->br_startblock = DELAYSTARTBLOCK;
4309 return;
4310 }
4311
4312 if (obno > *bno)
4313 *bno = obno;
4314 ASSERT((*bno >= obno) || (n == 0));
4315 ASSERT(*bno < end);
4316 mval->br_startoff = *bno;
4317 if (isnullstartblock(got->br_startblock))
4318 mval->br_startblock = DELAYSTARTBLOCK;
4319 else
4320 mval->br_startblock = got->br_startblock +
4321 (*bno - got->br_startoff);
4322 /*
4323 * Return the minimum of what we got and what we asked for for
4324 * the length. We can use the len variable here because it is
4325 * modified below and we could have been there before coming
4326 * here if the first part of the allocation didn't overlap what
4327 * was asked for.
4328 */
4329 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
4330 got->br_blockcount - (*bno - got->br_startoff));
4331 mval->br_state = got->br_state;
4332 ASSERT(mval->br_blockcount <= len);
4333 return;
4334}
4335
4336/*
4337 * Update and validate the extent map to return
4338 */
4339STATIC void
4340xfs_bmapi_update_map(
4341 struct xfs_bmbt_irec **map,
4342 xfs_fileoff_t *bno,
4343 xfs_filblks_t *len,
4344 xfs_fileoff_t obno,
4345 xfs_fileoff_t end,
4346 int *n,
4347 int flags)
4348{
4349 xfs_bmbt_irec_t *mval = *map;
4350
4351 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4352 ((mval->br_startoff + mval->br_blockcount) <= end));
4353 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4354 (mval->br_startoff < obno));
4355
4356 *bno = mval->br_startoff + mval->br_blockcount;
4357 *len = end - *bno;
4358 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4359 /* update previous map with new information */
4360 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4361 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4362 ASSERT(mval->br_state == mval[-1].br_state);
4363 mval[-1].br_blockcount = mval->br_blockcount;
4364 mval[-1].br_state = mval->br_state;
4365 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4366 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4367 mval[-1].br_startblock != HOLESTARTBLOCK &&
4368 mval->br_startblock == mval[-1].br_startblock +
4369 mval[-1].br_blockcount &&
4370 ((flags & XFS_BMAPI_IGSTATE) ||
4371 mval[-1].br_state == mval->br_state)) {
4372 ASSERT(mval->br_startoff ==
4373 mval[-1].br_startoff + mval[-1].br_blockcount);
4374 mval[-1].br_blockcount += mval->br_blockcount;
4375 } else if (*n > 0 &&
4376 mval->br_startblock == DELAYSTARTBLOCK &&
4377 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4378 mval->br_startoff ==
4379 mval[-1].br_startoff + mval[-1].br_blockcount) {
4380 mval[-1].br_blockcount += mval->br_blockcount;
4381 mval[-1].br_state = mval->br_state;
4382 } else if (!((*n == 0) &&
4383 ((mval->br_startoff + mval->br_blockcount) <=
4384 obno))) {
4385 mval++;
4386 (*n)++;
4387 }
4388 *map = mval;
4389}
4390
4391/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00004392 * Map file blocks to filesystem blocks without allocation.
4393 */
4394int
4395xfs_bmapi_read(
4396 struct xfs_inode *ip,
4397 xfs_fileoff_t bno,
4398 xfs_filblks_t len,
4399 struct xfs_bmbt_irec *mval,
4400 int *nmap,
4401 int flags)
4402{
4403 struct xfs_mount *mp = ip->i_mount;
4404 struct xfs_ifork *ifp;
4405 struct xfs_bmbt_irec got;
4406 struct xfs_bmbt_irec prev;
4407 xfs_fileoff_t obno;
4408 xfs_fileoff_t end;
4409 xfs_extnum_t lastx;
4410 int error;
4411 int eof;
4412 int n = 0;
4413 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4414 XFS_ATTR_FORK : XFS_DATA_FORK;
4415
4416 ASSERT(*nmap >= 1);
4417 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4418 XFS_BMAPI_IGSTATE)));
4419
4420 if (unlikely(XFS_TEST_ERROR(
4421 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4422 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4423 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4424 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
4425 return XFS_ERROR(EFSCORRUPTED);
4426 }
4427
4428 if (XFS_FORCED_SHUTDOWN(mp))
4429 return XFS_ERROR(EIO);
4430
4431 XFS_STATS_INC(xs_blk_mapr);
4432
4433 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004434
4435 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4436 error = xfs_iread_extents(NULL, ip, whichfork);
4437 if (error)
4438 return error;
4439 }
4440
4441 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4442 end = bno + len;
4443 obno = bno;
4444
4445 while (bno < end && n < *nmap) {
4446 /* Reading past eof, act as though there's a hole up to end. */
4447 if (eof)
4448 got.br_startoff = end;
4449 if (got.br_startoff > bno) {
4450 /* Reading in a hole. */
4451 mval->br_startoff = bno;
4452 mval->br_startblock = HOLESTARTBLOCK;
4453 mval->br_blockcount =
4454 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4455 mval->br_state = XFS_EXT_NORM;
4456 bno += mval->br_blockcount;
4457 len -= mval->br_blockcount;
4458 mval++;
4459 n++;
4460 continue;
4461 }
4462
4463 /* set up the extent map to return. */
4464 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4465 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4466
4467 /* If we're done, stop now. */
4468 if (bno >= end || n >= *nmap)
4469 break;
4470
4471 /* Else go on to the next record. */
4472 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4473 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4474 else
4475 eof = 1;
4476 }
4477 *nmap = n;
4478 return 0;
4479}
4480
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004481STATIC int
4482xfs_bmapi_reserve_delalloc(
4483 struct xfs_inode *ip,
4484 xfs_fileoff_t aoff,
4485 xfs_filblks_t len,
4486 struct xfs_bmbt_irec *got,
4487 struct xfs_bmbt_irec *prev,
4488 xfs_extnum_t *lastx,
4489 int eof)
4490{
4491 struct xfs_mount *mp = ip->i_mount;
4492 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4493 xfs_extlen_t alen;
4494 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004495 char rt = XFS_IS_REALTIME_INODE(ip);
4496 xfs_extlen_t extsz;
4497 int error;
4498
4499 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4500 if (!eof)
4501 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4502
4503 /* Figure out the extent size, adjust alen */
4504 extsz = xfs_get_extsz_hint(ip);
4505 if (extsz) {
4506 /*
4507 * Make sure we don't exceed a single extent length when we
4508 * align the extent by reducing length we are going to
4509 * allocate by the maximum amount extent size aligment may
4510 * require.
4511 */
4512 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1));
4513 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4514 1, 0, &aoff, &alen);
4515 ASSERT(!error);
4516 }
4517
4518 if (rt)
4519 extsz = alen / mp->m_sb.sb_rextsize;
4520
4521 /*
4522 * Make a transaction-less quota reservation for delayed allocation
4523 * blocks. This number gets adjusted later. We return if we haven't
4524 * allocated blocks already inside this loop.
4525 */
4526 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4527 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4528 if (error)
4529 return error;
4530
4531 /*
4532 * Split changing sb for alen and indlen since they could be coming
4533 * from different places.
4534 */
4535 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4536 ASSERT(indlen > 0);
4537
4538 if (rt) {
4539 error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
4540 -((int64_t)extsz), 0);
4541 } else {
4542 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4543 -((int64_t)alen), 0);
4544 }
4545
4546 if (error)
4547 goto out_unreserve_quota;
4548
4549 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4550 -((int64_t)indlen), 0);
4551 if (error)
4552 goto out_unreserve_blocks;
4553
4554
4555 ip->i_delayed_blks += alen;
4556
4557 got->br_startoff = aoff;
4558 got->br_startblock = nullstartblock(indlen);
4559 got->br_blockcount = alen;
4560 got->br_state = XFS_EXT_NORM;
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004561 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004562
4563 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004564 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4565 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004566 */
4567 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4568
4569 ASSERT(got->br_startoff <= aoff);
4570 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4571 ASSERT(isnullstartblock(got->br_startblock));
4572 ASSERT(got->br_state == XFS_EXT_NORM);
4573 return 0;
4574
4575out_unreserve_blocks:
4576 if (rt)
4577 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0);
4578 else
4579 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0);
4580out_unreserve_quota:
4581 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004582 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004583 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4584 return error;
4585}
4586
Dave Chinner5c8ed202011-09-18 20:40:45 +00004587/*
Christoph Hellwig44032802011-09-18 20:40:48 +00004588 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4589 */
4590int
4591xfs_bmapi_delay(
4592 struct xfs_inode *ip, /* incore inode */
4593 xfs_fileoff_t bno, /* starting file offs. mapped */
4594 xfs_filblks_t len, /* length to map in file */
4595 struct xfs_bmbt_irec *mval, /* output: map values */
4596 int *nmap, /* i/o: mval size/count */
4597 int flags) /* XFS_BMAPI_... */
4598{
4599 struct xfs_mount *mp = ip->i_mount;
4600 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4601 struct xfs_bmbt_irec got; /* current file extent record */
4602 struct xfs_bmbt_irec prev; /* previous file extent record */
4603 xfs_fileoff_t obno; /* old block number (offset) */
4604 xfs_fileoff_t end; /* end of mapped file region */
4605 xfs_extnum_t lastx; /* last useful extent number */
4606 int eof; /* we've hit the end of extents */
4607 int n = 0; /* current extent index */
4608 int error = 0;
4609
4610 ASSERT(*nmap >= 1);
4611 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4612 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
4613
4614 if (unlikely(XFS_TEST_ERROR(
4615 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4616 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4617 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4618 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
4619 return XFS_ERROR(EFSCORRUPTED);
4620 }
4621
4622 if (XFS_FORCED_SHUTDOWN(mp))
4623 return XFS_ERROR(EIO);
4624
4625 XFS_STATS_INC(xs_blk_mapw);
4626
4627 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4628 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4629 if (error)
4630 return error;
4631 }
4632
4633 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4634 end = bno + len;
4635 obno = bno;
4636
4637 while (bno < end && n < *nmap) {
4638 if (eof || got.br_startoff > bno) {
4639 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4640 &prev, &lastx, eof);
4641 if (error) {
4642 if (n == 0) {
4643 *nmap = 0;
4644 return error;
4645 }
4646 break;
4647 }
4648 }
4649
4650 /* set up the extent map to return. */
4651 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4652 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4653
4654 /* If we're done, stop now. */
4655 if (bno >= end || n >= *nmap)
4656 break;
4657
4658 /* Else go on to the next record. */
4659 prev = got;
4660 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4661 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4662 else
4663 eof = 1;
4664 }
4665
4666 *nmap = n;
4667 return 0;
4668}
4669
4670
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004671STATIC int
Dave Chinnere04426b2012-10-05 11:06:59 +10004672__xfs_bmapi_allocate(
4673 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004674{
4675 struct xfs_mount *mp = bma->ip->i_mount;
Dave Chinnere04426b2012-10-05 11:06:59 +10004676 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004677 XFS_ATTR_FORK : XFS_DATA_FORK;
4678 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004679 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004680 int error;
4681 int rt;
4682
Dave Chinnera99ebf42011-12-01 11:24:20 +00004683 ASSERT(bma->length > 0);
4684
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004685 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(bma->ip);
4686
4687 /*
4688 * For the wasdelay case, we could also just allocate the stuff asked
4689 * for in this bmap call but that wouldn't be as good.
4690 */
4691 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004692 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4693 bma->offset = bma->got.br_startoff;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004694 if (bma->idx != NULLEXTNUM && bma->idx) {
4695 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004696 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004697 }
4698 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004699 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004700 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004701 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004702 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004703 }
4704
4705 /*
4706 * Indicate if this is the first user data in the file, or just any
4707 * user data.
4708 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004709 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004710 bma->userdata = (bma->offset == 0) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004711 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4712 }
4713
Dave Chinnere04426b2012-10-05 11:06:59 +10004714 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004715
4716 /*
4717 * Only want to do the alignment at the eof if it is userdata and
4718 * allocation length is larger than a stripe unit.
4719 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004720 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004721 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004722 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004723 if (error)
4724 return error;
4725 }
4726
4727 error = xfs_bmap_alloc(bma);
4728 if (error)
4729 return error;
4730
Dave Chinner0937e0f2011-09-18 20:40:57 +00004731 if (bma->flist->xbf_low)
4732 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004733 if (bma->cur)
4734 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004735 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004736 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004737 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4738 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4739 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4740 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004741 }
4742 /*
4743 * Bump the number of extents we've allocated
4744 * in this call.
4745 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004746 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004747
Dave Chinner29c8d172011-09-18 20:41:00 +00004748 if (bma->cur)
4749 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004750 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4751
Dave Chinner963c30c2011-09-18 20:40:59 +00004752 bma->got.br_startoff = bma->offset;
4753 bma->got.br_startblock = bma->blkno;
4754 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004755 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004756
4757 /*
4758 * A wasdelay extent has been initialized, so shouldn't be flagged
4759 * as unwritten.
4760 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004761 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004762 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004763 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004764
Christoph Hellwigc6534242011-09-18 20:41:05 +00004765 if (bma->wasdel)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00004766 error = xfs_bmap_add_extent_delay_real(bma);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004767 else
4768 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004769
Christoph Hellwigc315c902011-09-18 20:41:02 +00004770 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004771 if (error)
4772 return error;
4773
4774 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004775 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4776 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4777 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004778 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004779 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004780
Dave Chinner963c30c2011-09-18 20:40:59 +00004781 ASSERT(bma->got.br_startoff <= bma->offset);
4782 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4783 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004784 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4785 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004786 return 0;
4787}
4788
Dave Chinnere04426b2012-10-05 11:06:59 +10004789static void
4790xfs_bmapi_allocate_worker(
4791 struct work_struct *work)
4792{
4793 struct xfs_bmalloca *args = container_of(work,
4794 struct xfs_bmalloca, work);
4795 unsigned long pflags;
4796
4797 /* we are in a transaction context here */
4798 current_set_flags_nested(&pflags, PF_FSTRANS);
4799
4800 args->result = __xfs_bmapi_allocate(args);
4801 complete(args->done);
4802
4803 current_restore_flags_nested(&pflags, PF_FSTRANS);
4804}
4805
4806/*
4807 * Some allocation requests often come in with little stack to work on. Push
4808 * them off to a worker thread so there is lots of stack to use. Otherwise just
4809 * call directly to avoid the context switch overhead here.
4810 */
4811int
4812xfs_bmapi_allocate(
4813 struct xfs_bmalloca *args)
4814{
4815 DECLARE_COMPLETION_ONSTACK(done);
4816
4817 if (!args->stack_switch)
4818 return __xfs_bmapi_allocate(args);
4819
4820
4821 args->done = &done;
4822 INIT_WORK_ONSTACK(&args->work, xfs_bmapi_allocate_worker);
4823 queue_work(xfs_alloc_wq, &args->work);
4824 wait_for_completion(&done);
4825 return args->result;
4826}
4827
Dave Chinnerb447fe52011-09-18 20:40:51 +00004828STATIC int
4829xfs_bmapi_convert_unwritten(
4830 struct xfs_bmalloca *bma,
4831 struct xfs_bmbt_irec *mval,
4832 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004833 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004834{
4835 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4836 XFS_ATTR_FORK : XFS_DATA_FORK;
4837 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004838 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004839 int error;
4840
Dave Chinnerb447fe52011-09-18 20:40:51 +00004841 /* check if we need to do unwritten->real conversion */
4842 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4843 (flags & XFS_BMAPI_PREALLOC))
4844 return 0;
4845
4846 /* check if we need to do real->unwritten conversion */
4847 if (mval->br_state == XFS_EXT_NORM &&
4848 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4849 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4850 return 0;
4851
4852 /*
4853 * Modify (by adding) the state flag, if writing.
4854 */
4855 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004856 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4857 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004858 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004859 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4860 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004861 }
4862 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4863 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4864
Dave Chinnere0c3da52011-09-18 20:41:01 +00004865 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004866 &bma->cur, mval, bma->firstblock, bma->flist,
4867 &tmp_logflags);
4868 bma->logflags |= tmp_logflags;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004869 if (error)
4870 return error;
4871
4872 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004873 * Update our extent pointer, given that
4874 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4875 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004876 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004877 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004878
4879 /*
4880 * We may have combined previously unwritten space with written space,
4881 * so generate another request.
4882 */
4883 if (mval->br_blockcount < len)
4884 return EAGAIN;
4885 return 0;
4886}
4887
Christoph Hellwig44032802011-09-18 20:40:48 +00004888/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004889 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4890 * extent state if necessary. Details behaviour is controlled by the flags
4891 * parameter. Only allocates blocks from a single allocation group, to avoid
4892 * locking problems.
4893 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 * The returned value in "firstblock" from the first call in a transaction
4895 * must be remembered and presented to subsequent calls in "firstblock".
4896 * An upper bound for the number of blocks to be allocated is supplied to
4897 * the first call in "total"; if no allocation group has that many free
4898 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4899 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004900int
4901xfs_bmapi_write(
4902 struct xfs_trans *tp, /* transaction pointer */
4903 struct xfs_inode *ip, /* incore inode */
4904 xfs_fileoff_t bno, /* starting file offs. mapped */
4905 xfs_filblks_t len, /* length to map in file */
4906 int flags, /* XFS_BMAPI_... */
4907 xfs_fsblock_t *firstblock, /* first allocated block
4908 controls a.g. for allocs */
4909 xfs_extlen_t total, /* total blocks needed */
4910 struct xfs_bmbt_irec *mval, /* output: map values */
4911 int *nmap, /* i/o: mval size/count */
4912 struct xfs_bmap_free *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004914 struct xfs_mount *mp = ip->i_mount;
4915 struct xfs_ifork *ifp;
4916 struct xfs_bmalloca bma = { 0 }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004917 xfs_fileoff_t end; /* end of mapped file region */
4918 int eof; /* after the end of extents */
4919 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004920 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004921 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004922 int whichfork; /* data or attr fork */
4923 char inhole; /* current location is hole in file */
4924 char wasdelay; /* old extent was delayed */
4925
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004927 xfs_fileoff_t orig_bno; /* original block number value */
4928 int orig_flags; /* original flags arg value */
4929 xfs_filblks_t orig_len; /* original value of len arg */
4930 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4931 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
4933 orig_bno = bno;
4934 orig_len = len;
4935 orig_flags = flags;
4936 orig_mval = mval;
4937 orig_nmap = *nmap;
4938#endif
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004939
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004941 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4942 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4943 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004944 ASSERT(len > 0);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004945
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4947 XFS_ATTR_FORK : XFS_DATA_FORK;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004948
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 if (unlikely(XFS_TEST_ERROR(
4950 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4951 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4952 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4953 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004954 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 return XFS_ERROR(EFSCORRUPTED);
4956 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004957
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 if (XFS_FORCED_SHUTDOWN(mp))
4959 return XFS_ERROR(EIO);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004962
4963 XFS_STATS_INC(xs_blk_mapw);
4964
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
Dave Chinner1e823792013-02-11 15:58:13 +11004966 /*
4967 * XXX (dgc): This assumes we are only called for inodes that
4968 * contain content neutral data in local format. Anything that
4969 * contains caller-specific data in local format that needs
4970 * transformation to move to a block format needs to do the
4971 * conversion to extent format itself.
4972 *
4973 * Directory data forks and attribute forks handle this
4974 * themselves, but with the addition of metadata verifiers every
4975 * data fork in local format now contains caller specific data
4976 * and as such conversion through this function is likely to be
4977 * broken.
4978 *
4979 * The only likely user of this branch is for remote symlinks,
4980 * but we cannot overwrite the data fork contents of the symlink
4981 * (EEXIST occurs higher up the stack) and so it will never go
4982 * from local format to extent format here. Hence I don't think
4983 * this branch is ever executed intentionally and we should
4984 * consider removing it and asserting that xfs_bmapi_write()
4985 * cannot be called directly on local format forks. i.e. callers
4986 * are completely responsible for local to extent format
4987 * conversion, not xfs_bmapi_write().
4988 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004989 error = xfs_bmap_local_to_extents(tp, ip, firstblock, total,
Dave Chinner1e823792013-02-11 15:58:13 +11004990 &bma.logflags, whichfork,
4991 xfs_bmap_local_to_extents_init_fn);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004992 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 goto error0;
4994 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004995
4996 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004998 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00005000 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005001 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00005002 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005003 }
5004
5005 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5006 error = xfs_iread_extents(tp, ip, whichfork);
5007 if (error)
5008 goto error0;
5009 }
5010
Dave Chinnere0c3da52011-09-18 20:41:01 +00005011 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00005012 &bma.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 n = 0;
5014 end = bno + len;
5015 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00005016
5017 bma.tp = tp;
5018 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00005019 bma.total = total;
5020 bma.userdata = 0;
Dave Chinner0937e0f2011-09-18 20:40:57 +00005021 bma.flist = flist;
5022 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005023
Brian Foster9e96fe62013-01-17 13:11:29 -05005024 if (flags & XFS_BMAPI_STACK_SWITCH)
5025 bma.stack_switch = 1;
5026
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00005028 inhole = eof || bma.got.br_startoff > bno;
5029 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005030
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 /*
5032 * First, deal with the hole before the allocated space
5033 * that we found, if any.
5034 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005035 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00005036 bma.eof = eof;
5037 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
5038 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00005039 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10005040 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00005041
Dave Chinnera99ebf42011-12-01 11:24:20 +00005042 /*
5043 * There's a 32/64 bit type mismatch between the
5044 * allocation length request (which can be 64 bits in
5045 * length) and the bma length request, which is
5046 * xfs_extlen_t and therefore 32 bits. Hence we have to
5047 * check for 32-bit overflows and handle them here.
5048 */
5049 if (len > (xfs_filblks_t)MAXEXTLEN)
5050 bma.length = MAXEXTLEN;
5051 else
5052 bma.length = len;
5053
5054 ASSERT(len > 0);
5055 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10005056 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 if (error)
5058 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00005059 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00005060 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 }
Christoph Hellwig44032802011-09-18 20:40:48 +00005062
Dave Chinneraef9a892011-09-18 20:40:44 +00005063 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00005064 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
5065 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
Dave Chinnerb447fe52011-09-18 20:40:51 +00005067 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00005068 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005069 if (error == EAGAIN)
5070 continue;
5071 if (error)
5072 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
Dave Chinneraef9a892011-09-18 20:40:44 +00005074 /* update the extent map to return */
5075 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
5076
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 /*
5078 * If we're done, stop now. Stop when we've allocated
5079 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5080 * the transaction may get too big.
5081 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00005082 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005084
5085 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00005086 bma.prev = bma.got;
Dave Chinnere0c3da52011-09-18 20:41:01 +00005087 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
5088 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
5089 &bma.got);
5090 } else
Christoph Hellwig5690f922011-05-11 15:04:07 +00005091 eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005094
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 /*
5096 * Transform from btree to extents, give it cur.
5097 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005098 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00005099 int tmp_logflags = 0;
5100
Dave Chinner29c8d172011-09-18 20:41:00 +00005101 ASSERT(bma.cur);
5102 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00005104 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 if (error)
5106 goto error0;
5107 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005108
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005110 XFS_IFORK_NEXTENTS(ip, whichfork) >
5111 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113error0:
5114 /*
5115 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005116 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00005118 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00005120 bma.logflags &= ~xfs_ilog_fext(whichfork);
5121 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00005123 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 /*
5125 * Log whatever the flags say, even if error. Otherwise we might miss
5126 * detecting a case where the data is changed, there's an error,
5127 * and it's not logged so we don't shutdown when we should.
5128 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00005129 if (bma.logflags)
5130 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005131
Dave Chinner29c8d172011-09-18 20:41:00 +00005132 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 if (!error) {
5134 ASSERT(*firstblock == NULLFSBLOCK ||
5135 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5136 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00005137 bma.cur->bc_private.b.firstblock) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 (flist->xbf_low &&
5139 XFS_FSB_TO_AGNO(mp, *firstblock) <
5140 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00005141 bma.cur->bc_private.b.firstblock)));
5142 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 }
Dave Chinner29c8d172011-09-18 20:41:00 +00005144 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5146 }
5147 if (!error)
5148 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5149 orig_nmap, *nmap);
5150 return error;
5151}
5152
5153/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 * Unmap (remove) blocks from a file.
5155 * If nexts is nonzero then the number of extents to remove is limited to
5156 * that value. If not all extents in the block range can be removed then
5157 * *done is set.
5158 */
5159int /* error */
5160xfs_bunmapi(
5161 xfs_trans_t *tp, /* transaction pointer */
5162 struct xfs_inode *ip, /* incore inode */
5163 xfs_fileoff_t bno, /* starting offset to unmap */
5164 xfs_filblks_t len, /* length to unmap in file */
5165 int flags, /* misc flags */
5166 xfs_extnum_t nexts, /* number of extents max */
5167 xfs_fsblock_t *firstblock, /* first allocated block
5168 controls a.g. for allocs */
5169 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5170 int *done) /* set if not done yet */
5171{
5172 xfs_btree_cur_t *cur; /* bmap btree cursor */
5173 xfs_bmbt_irec_t del; /* extent being deleted */
5174 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005175 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 int error; /* error return value */
5177 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005178 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 xfs_ifork_t *ifp; /* inode fork pointer */
5180 int isrt; /* freeing in rt area */
5181 xfs_extnum_t lastx; /* last extent index used */
5182 int logflags; /* transaction logging flags */
5183 xfs_extlen_t mod; /* rt extent offset */
5184 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005185 xfs_extnum_t nextents; /* number of file extents */
5186 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 xfs_fileoff_t start; /* first file offset deleted */
5188 int tmp_logflags; /* partial logging flags */
5189 int wasdel; /* was a delayed alloc extent */
5190 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 xfs_fsblock_t sum;
5192
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005193 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5194
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5196 XFS_ATTR_FORK : XFS_DATA_FORK;
5197 ifp = XFS_IFORK_PTR(ip, whichfork);
5198 if (unlikely(
5199 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5200 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5201 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5202 ip->i_mount);
5203 return XFS_ERROR(EFSCORRUPTED);
5204 }
5205 mp = ip->i_mount;
5206 if (XFS_FORCED_SHUTDOWN(mp))
5207 return XFS_ERROR(EIO);
Christoph Hellwig54893272011-05-11 15:04:03 +00005208
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 ASSERT(len > 0);
5210 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005211
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5213 (error = xfs_iread_extents(tp, ip, whichfork)))
5214 return error;
5215 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5216 if (nextents == 0) {
5217 *done = 1;
5218 return 0;
5219 }
5220 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005221 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 start = bno;
5223 bno = start + len - 1;
5224 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5225 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005226
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 /*
5228 * Check to see if the given block number is past the end of the
5229 * file, back up to the last block if so...
5230 */
5231 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005232 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 xfs_bmbt_get_all(ep, &got);
5234 bno = got.br_startoff + got.br_blockcount - 1;
5235 }
5236 logflags = 0;
5237 if (ifp->if_flags & XFS_IFBROOT) {
5238 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005239 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 cur->bc_private.b.firstblock = *firstblock;
5241 cur->bc_private.b.flist = flist;
5242 cur->bc_private.b.flags = 0;
5243 } else
5244 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005245
5246 if (isrt) {
5247 /*
5248 * Synchronize by locking the bitmap inode.
5249 */
5250 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
5251 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5252 }
5253
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 extno = 0;
5255 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5256 (nexts == 0 || extno < nexts)) {
5257 /*
5258 * Is the found extent after a hole in which bno lives?
5259 * Just back up to the previous extent, if so.
5260 */
5261 if (got.br_startoff > bno) {
5262 if (--lastx < 0)
5263 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005264 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 xfs_bmbt_get_all(ep, &got);
5266 }
5267 /*
5268 * Is the last block of this extent before the range
5269 * we're supposed to delete? If so, we're done.
5270 */
5271 bno = XFS_FILEOFF_MIN(bno,
5272 got.br_startoff + got.br_blockcount - 1);
5273 if (bno < start)
5274 break;
5275 /*
5276 * Then deal with the (possibly delayed) allocated space
5277 * we found.
5278 */
5279 ASSERT(ep != NULL);
5280 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005281 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 if (got.br_startoff < start) {
5283 del.br_startoff = start;
5284 del.br_blockcount -= start - got.br_startoff;
5285 if (!wasdel)
5286 del.br_startblock += start - got.br_startoff;
5287 }
5288 if (del.br_startoff + del.br_blockcount > bno + 1)
5289 del.br_blockcount = bno + 1 - del.br_startoff;
5290 sum = del.br_startblock + del.br_blockcount;
5291 if (isrt &&
5292 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5293 /*
5294 * Realtime extent not lined up at the end.
5295 * The extent could have been split into written
5296 * and unwritten pieces, or we could just be
5297 * unmapping part of it. But we can't really
5298 * get rid of part of a realtime extent.
5299 */
5300 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005301 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 /*
5303 * This piece is unwritten, or we're not
5304 * using unwritten extents. Skip over it.
5305 */
5306 ASSERT(bno >= mod);
5307 bno -= mod > del.br_blockcount ?
5308 del.br_blockcount : mod;
5309 if (bno < got.br_startoff) {
5310 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005311 xfs_bmbt_get_all(xfs_iext_get_ext(
5312 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 }
5314 continue;
5315 }
5316 /*
5317 * It's written, turn it unwritten.
5318 * This is better than zeroing it.
5319 */
5320 ASSERT(del.br_state == XFS_EXT_NORM);
5321 ASSERT(xfs_trans_get_block_res(tp) > 0);
5322 /*
5323 * If this spans a realtime extent boundary,
5324 * chop it back to the start of the one we end at.
5325 */
5326 if (del.br_blockcount > mod) {
5327 del.br_startoff += del.br_blockcount - mod;
5328 del.br_startblock += del.br_blockcount - mod;
5329 del.br_blockcount = mod;
5330 }
5331 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005332 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5333 &lastx, &cur, &del, firstblock, flist,
5334 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 if (error)
5336 goto error0;
5337 goto nodelete;
5338 }
5339 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5340 /*
5341 * Realtime extent is lined up at the end but not
5342 * at the front. We'll get rid of full extents if
5343 * we can.
5344 */
5345 mod = mp->m_sb.sb_rextsize - mod;
5346 if (del.br_blockcount > mod) {
5347 del.br_blockcount -= mod;
5348 del.br_startoff += mod;
5349 del.br_startblock += mod;
5350 } else if ((del.br_startoff == start &&
5351 (del.br_state == XFS_EXT_UNWRITTEN ||
5352 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005353 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 /*
5355 * Can't make it unwritten. There isn't
5356 * a full extent here so just skip it.
5357 */
5358 ASSERT(bno >= del.br_blockcount);
5359 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005360 if (got.br_startoff > bno) {
5361 if (--lastx >= 0) {
5362 ep = xfs_iext_get_ext(ifp,
5363 lastx);
5364 xfs_bmbt_get_all(ep, &got);
5365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 }
5367 continue;
5368 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5369 /*
5370 * This one is already unwritten.
5371 * It must have a written left neighbor.
5372 * Unwrite the killed part of that one and
5373 * try again.
5374 */
5375 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005376 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5377 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005379 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 ASSERT(del.br_startblock ==
5381 prev.br_startblock + prev.br_blockcount);
5382 if (prev.br_startoff < start) {
5383 mod = start - prev.br_startoff;
5384 prev.br_blockcount -= mod;
5385 prev.br_startblock += mod;
5386 prev.br_startoff = start;
5387 }
5388 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005389 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005390 error = xfs_bmap_add_extent_unwritten_real(tp,
5391 ip, &lastx, &cur, &prev,
5392 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 if (error)
5394 goto error0;
5395 goto nodelete;
5396 } else {
5397 ASSERT(del.br_state == XFS_EXT_NORM);
5398 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005399 error = xfs_bmap_add_extent_unwritten_real(tp,
5400 ip, &lastx, &cur, &del,
5401 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 if (error)
5403 goto error0;
5404 goto nodelete;
5405 }
5406 }
5407 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005408 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005409 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005410 if (isrt) {
5411 xfs_filblks_t rtexts;
5412
5413 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5414 do_div(rtexts, mp->m_sb.sb_rextsize);
5415 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005416 (int64_t)rtexts, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005417 (void)xfs_trans_reserve_quota_nblks(NULL,
5418 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005419 XFS_QMOPT_RES_RTBLKS);
5420 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00005421 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005422 (int64_t)del.br_blockcount, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005423 (void)xfs_trans_reserve_quota_nblks(NULL,
5424 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 ip->i_delayed_blks -= del.br_blockcount;
5428 if (cur)
5429 cur->bc_private.b.flags |=
5430 XFS_BTCUR_BPRV_WASDEL;
5431 } else if (cur)
5432 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5433 /*
5434 * If it's the case where the directory code is running
5435 * with no block reservation, and the deleted block is in
5436 * the middle of its extent, and the resulting insert
5437 * of an extent would cause transformation to btree format,
5438 * then reject it. The calling code will then swap
5439 * blocks around instead.
5440 * We have to do this now, rather than waiting for the
5441 * conversion to btree format, since the transaction
5442 * will be dirty.
5443 */
5444 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5445 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005446 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5447 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 del.br_startoff > got.br_startoff &&
5449 del.br_startoff + del.br_blockcount <
5450 got.br_startoff + got.br_blockcount) {
5451 error = XFS_ERROR(ENOSPC);
5452 goto error0;
5453 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00005454 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005455 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 logflags |= tmp_logflags;
5457 if (error)
5458 goto error0;
5459 bno = del.br_startoff - 1;
5460nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 /*
5462 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005465 if (lastx >= 0) {
5466 ep = xfs_iext_get_ext(ifp, lastx);
5467 if (xfs_bmbt_get_startoff(ep) > bno) {
5468 if (--lastx >= 0)
5469 ep = xfs_iext_get_ext(ifp,
5470 lastx);
5471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 extno++;
5475 }
5476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005478
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 /*
5480 * Convert to a btree if necessary.
5481 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005482 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 ASSERT(cur == NULL);
5484 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5485 &cur, 0, &tmp_logflags, whichfork);
5486 logflags |= tmp_logflags;
5487 if (error)
5488 goto error0;
5489 }
5490 /*
5491 * transform from btree to extents, give it cur
5492 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005493 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 ASSERT(cur != NULL);
5495 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5496 whichfork);
5497 logflags |= tmp_logflags;
5498 if (error)
5499 goto error0;
5500 }
5501 /*
5502 * transform from extents to local?
5503 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 error = 0;
5505error0:
5506 /*
5507 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005508 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005510 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005512 logflags &= ~xfs_ilog_fext(whichfork);
5513 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005515 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 /*
5517 * Log inode even in the error case, if the transaction
5518 * is dirty we'll need to shut down the filesystem.
5519 */
5520 if (logflags)
5521 xfs_trans_log_inode(tp, ip, logflags);
5522 if (cur) {
5523 if (!error) {
5524 *firstblock = cur->bc_private.b.firstblock;
5525 cur->bc_private.b.allocated = 0;
5526 }
5527 xfs_btree_del_cursor(cur,
5528 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5529 }
5530 return error;
5531}
5532
5533/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005534 * returns 1 for success, 0 if we failed to map the extent.
5535 */
5536STATIC int
5537xfs_getbmapx_fix_eof_hole(
5538 xfs_inode_t *ip, /* xfs incore inode pointer */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005539 struct getbmapx *out, /* output structure */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005540 int prealloced, /* this is a file with
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005541 * preallocated data space */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005542 __int64_t end, /* last block requested */
5543 xfs_fsblock_t startblock)
5544{
5545 __int64_t fixlen;
5546 xfs_mount_t *mp; /* file system mount point */
Eric Sandeen5af317c2008-11-28 14:23:35 +11005547 xfs_ifork_t *ifp; /* inode fork pointer */
5548 xfs_extnum_t lastx; /* last extent pointer */
5549 xfs_fileoff_t fileblock;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005550
5551 if (startblock == HOLESTARTBLOCK) {
5552 mp = ip->i_mount;
5553 out->bmv_block = -1;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00005554 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005555 fixlen -= out->bmv_offset;
5556 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5557 /* Came to hole at EOF. Trim it. */
5558 if (fixlen <= 0)
5559 return 0;
5560 out->bmv_length = fixlen;
5561 }
5562 } else {
Eric Sandeen5af317c2008-11-28 14:23:35 +11005563 if (startblock == DELAYSTARTBLOCK)
5564 out->bmv_block = -2;
5565 else
Eric Sandeen9d87c312009-01-14 23:22:07 -06005566 out->bmv_block = xfs_fsb_to_db(ip, startblock);
Eric Sandeen5af317c2008-11-28 14:23:35 +11005567 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5568 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5569 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5570 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5571 out->bmv_oflags |= BMV_OF_LAST;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005572 }
5573
5574 return 1;
5575}
5576
5577/*
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005578 * Get inode's extents as described in bmv, and format for output.
5579 * Calls formatter to fill the user's buffer until all extents
5580 * are mapped, until the passed-in bmv->bmv_count slots have
5581 * been filled, or until the formatter short-circuits the loop,
5582 * if it is tracking filled-in extents on its own.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 */
5584int /* error code */
5585xfs_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10005586 xfs_inode_t *ip,
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005587 struct getbmapx *bmv, /* user bmap structure */
5588 xfs_bmap_format_t formatter, /* format to user */
5589 void *arg) /* formatter arg */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590{
5591 __int64_t bmvend; /* last block requested */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005592 int error = 0; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 __int64_t fixlen; /* length for -1 case */
5594 int i; /* extent number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 int lock; /* lock state */
5596 xfs_bmbt_irec_t *map; /* buffer for user's data */
5597 xfs_mount_t *mp; /* file system mount point */
5598 int nex; /* # of user extents can do */
5599 int nexleft; /* # of user extents left */
5600 int subnex; /* # of bmapi's can do */
5601 int nmap; /* number of map entries */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005602 struct getbmapx *out; /* output structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 int whichfork; /* data or attr fork */
5604 int prealloced; /* this is a file with
5605 * preallocated data space */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005606 int iflags; /* interface flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 int bmapi_flags; /* flags for xfs_bmapi */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005608 int cur_ext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 mp = ip->i_mount;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005611 iflags = bmv->bmv_iflags;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005612 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 if (whichfork == XFS_ATTR_FORK) {
5615 if (XFS_IFORK_Q(ip)) {
5616 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5617 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5618 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5619 return XFS_ERROR(EINVAL);
5620 } else if (unlikely(
5621 ip->i_d.di_aformat != 0 &&
5622 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5623 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5624 ip->i_mount);
5625 return XFS_ERROR(EFSCORRUPTED);
5626 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005627
5628 prealloced = 0;
5629 fixlen = 1LL << 32;
5630 } else {
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005631 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5632 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5633 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5634 return XFS_ERROR(EINVAL);
5635
David Chinner957d0eb2007-06-18 16:50:37 +10005636 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005637 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 prealloced = 1;
Dave Chinner32972382012-06-08 15:44:54 +10005639 fixlen = mp->m_super->s_maxbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 } else {
5641 prealloced = 0;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00005642 fixlen = XFS_ISIZE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 }
5645
5646 if (bmv->bmv_length == -1) {
5647 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005648 bmv->bmv_length =
5649 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5650 } else if (bmv->bmv_length == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 bmv->bmv_entries = 0;
5652 return 0;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005653 } else if (bmv->bmv_length < 0) {
5654 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005656
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 nex = bmv->bmv_count - 1;
5658 if (nex <= 0)
5659 return XFS_ERROR(EINVAL);
5660 bmvend = bmv->bmv_offset + bmv->bmv_length;
5661
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005662
5663 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5664 return XFS_ERROR(ENOMEM);
5665 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
Dave Chinnerf0742112012-03-07 04:50:22 +00005666 if (!out) {
5667 out = kmem_zalloc_large(bmv->bmv_count *
5668 sizeof(struct getbmapx));
5669 if (!out)
5670 return XFS_ERROR(ENOMEM);
5671 }
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005672
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005674 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00005675 if (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size) {
Dave Chinner4bc1ea62012-11-12 22:53:56 +11005676 error = -filemap_write_and_wait(VFS_I(ip)->i_mapping);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005677 if (error)
5678 goto out_unlock_iolock;
Niv Sardie12070a2008-03-06 13:43:03 +11005679 }
Dave Chinner309c8482010-11-30 15:16:02 +11005680 /*
5681 * even after flushing the inode, there can still be delalloc
5682 * blocks on the inode beyond EOF due to speculative
5683 * preallocation. These are not removed until the release
5684 * function is called or the inode is inactivated. Hence we
5685 * cannot assert here that ip->i_delayed_blks == 0.
5686 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
5689 lock = xfs_ilock_map_shared(ip);
5690
5691 /*
5692 * Don't let nex be bigger than the number of extents
5693 * we can have assuming alternating holes and real extents.
5694 */
5695 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5696 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5697
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005698 bmapi_flags = xfs_bmapi_aflag(whichfork);
5699 if (!(iflags & BMV_IF_PREALLOC))
5700 bmapi_flags |= XFS_BMAPI_IGSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701
5702 /*
5703 * Allocate enough space to handle "subnex" maps at a time.
5704 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005705 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 subnex = 16;
Christoph Hellwigca35dcd2009-07-18 18:14:54 -04005707 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005708 if (!map)
5709 goto out_unlock_ilock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710
5711 bmv->bmv_entries = 0;
5712
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005713 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5714 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5715 error = 0;
5716 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 }
5718
5719 nexleft = nex;
5720
5721 do {
5722 nmap = (nexleft > subnex) ? subnex : nexleft;
Dave Chinner5c8ed202011-09-18 20:40:45 +00005723 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5724 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5725 map, &nmap, bmapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 if (error)
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005727 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 ASSERT(nmap <= subnex);
5729
5730 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005731 out[cur_ext].bmv_oflags = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005732 if (map[i].br_state == XFS_EXT_UNWRITTEN)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005733 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005734 else if (map[i].br_startblock == DELAYSTARTBLOCK)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005735 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5736 out[cur_ext].bmv_offset =
5737 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5738 out[cur_ext].bmv_length =
5739 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5740 out[cur_ext].bmv_unused1 = 0;
5741 out[cur_ext].bmv_unused2 = 0;
Dave Chinner58e20772012-04-29 21:16:17 +10005742
5743 /*
5744 * delayed allocation extents that start beyond EOF can
5745 * occur due to speculative EOF allocation when the
5746 * delalloc extent is larger than the largest freespace
5747 * extent at conversion time. These extents cannot be
5748 * converted by data writeback, so can exist here even
5749 * if we are not supposed to be finding delalloc
5750 * extents.
5751 */
5752 if (map[i].br_startblock == DELAYSTARTBLOCK &&
5753 map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
5754 ASSERT((iflags & BMV_IF_DELALLOC) != 0);
5755
Yingping Lu9af0a702005-11-02 15:09:54 +11005756 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005757 whichfork == XFS_ATTR_FORK) {
5758 /* came to the end of attribute fork */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005759 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005760 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005762
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005763 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5764 prealloced, bmvend,
5765 map[i].br_startblock))
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005766 goto out_free_map;
5767
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005768 bmv->bmv_offset =
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005769 out[cur_ext].bmv_offset +
5770 out[cur_ext].bmv_length;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005771 bmv->bmv_length =
5772 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
Tao Ma9af25462010-08-30 02:44:03 +00005773
5774 /*
5775 * In case we don't want to return the hole,
5776 * don't increase cur_ext so that we can reuse
5777 * it in the next loop.
5778 */
5779 if ((iflags & BMV_IF_NO_HOLES) &&
5780 map[i].br_startblock == HOLESTARTBLOCK) {
5781 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5782 continue;
5783 }
5784
5785 nexleft--;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005786 bmv->bmv_entries++;
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005787 cur_ext++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 }
5789 } while (nmap && nexleft && bmv->bmv_length);
5790
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005791 out_free_map:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10005792 kmem_free(map);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005793 out_unlock_ilock:
5794 xfs_iunlock_map_shared(ip, lock);
5795 out_unlock_iolock:
5796 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005797
5798 for (i = 0; i < cur_ext; i++) {
5799 int full = 0; /* user array is full */
5800
5801 /* format results & advance arg */
5802 error = formatter(&arg, &out[i], &full);
5803 if (error || full)
5804 break;
5805 }
5806
Dave Chinnerf0742112012-03-07 04:50:22 +00005807 if (is_vmalloc_addr(out))
5808 kmem_free_large(out);
5809 else
5810 kmem_free(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 return error;
5812}
5813
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814#ifdef DEBUG
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005815STATIC struct xfs_buf *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816xfs_bmap_get_bp(
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005817 struct xfs_btree_cur *cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 xfs_fsblock_t bno)
5819{
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005820 struct xfs_log_item_desc *lidp;
5821 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822
5823 if (!cur)
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005824 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005826 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5827 if (!cur->bc_bufs[i])
5828 break;
5829 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5830 return cur->bc_bufs[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005833 /* Chase down all the log items to see if the bp is there */
5834 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
Christoph Hellwige98c4142010-06-23 18:11:15 +10005835 struct xfs_buf_log_item *bip;
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005836 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5837 if (bip->bli_item.li_type == XFS_LI_BUF &&
5838 XFS_BUF_ADDR(bip->bli_buf) == bno)
5839 return bip->bli_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 }
Christoph Hellwige98c4142010-06-23 18:11:15 +10005841
Christoph Hellwigecd7f082010-07-22 12:52:08 +10005842 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843}
5844
Hannes Eder3180e662009-03-04 19:34:10 +01005845STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846xfs_check_block(
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005847 struct xfs_btree_block *block,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 xfs_mount_t *mp,
5849 int root,
5850 short sz)
5851{
5852 int i, j, dmxr;
Christoph Hellwig576039c2006-09-28 10:58:06 +10005853 __be64 *pp, *thispa; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 xfs_bmbt_key_t *prevp, *keyp;
5855
Christoph Hellwig16259e72005-11-02 15:11:25 +11005856 ASSERT(be16_to_cpu(block->bb_level) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857
5858 prevp = NULL;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005859 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 dmxr = mp->m_bmap_dmxr[0];
Christoph Hellwig136341b2008-10-30 17:11:40 +11005861 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862
5863 if (prevp) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11005864 ASSERT(be64_to_cpu(prevp->br_startoff) <
5865 be64_to_cpu(keyp->br_startoff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 }
5867 prevp = keyp;
5868
5869 /*
5870 * Compare the block numbers to see if there are dups.
5871 */
Christoph Hellwig136341b2008-10-30 17:11:40 +11005872 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005873 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005874 else
5875 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5876
Christoph Hellwig16259e72005-11-02 15:11:25 +11005877 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11005878 if (root)
Christoph Hellwig60197e82008-10-30 17:11:19 +11005879 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005880 else
5881 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005882 if (*thispa == *pp) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11005883 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005884 __func__, j, i,
Christoph Hellwig576039c2006-09-28 10:58:06 +10005885 (unsigned long long)be64_to_cpu(*thispa));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 panic("%s: ptrs are equal in node\n",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005887 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 }
5889 }
5890 }
5891}
5892
5893/*
5894 * Check that the extents for the inode ip are in the right order in all
5895 * btree leaves.
5896 */
5897
5898STATIC void
5899xfs_bmap_check_leaf_extents(
5900 xfs_btree_cur_t *cur, /* btree cursor or null */
5901 xfs_inode_t *ip, /* incore inode pointer */
5902 int whichfork) /* data or attr fork */
5903{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005904 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 xfs_fsblock_t bno; /* block # of "block" */
5906 xfs_buf_t *bp; /* buffer for "block" */
5907 int error; /* error return value */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005908 xfs_extnum_t i=0, j; /* index into the extents list */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 xfs_ifork_t *ifp; /* fork structure */
5910 int level; /* btree level, for checking */
5911 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10005912 __be64 *pp; /* pointer to block address */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005913 xfs_bmbt_rec_t *ep; /* pointer to current extent */
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11005914 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005915 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 int bp_release = 0;
5917
5918 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5919 return;
5920 }
5921
5922 bno = NULLFSBLOCK;
5923 mp = ip->i_mount;
5924 ifp = XFS_IFORK_PTR(ip, whichfork);
5925 block = ifp->if_broot;
5926 /*
5927 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
5928 */
Christoph Hellwig16259e72005-11-02 15:11:25 +11005929 level = be16_to_cpu(block->bb_level);
5930 ASSERT(level > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
Christoph Hellwig60197e82008-10-30 17:11:19 +11005932 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005933 bno = be64_to_cpu(*pp);
5934
5935 ASSERT(bno != NULLDFSBNO);
5936 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5937 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5938
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 /*
5940 * Go down the tree until leaf level is reached, following the first
5941 * pointer (leftmost) at each level.
5942 */
5943 while (level-- > 0) {
5944 /* See if buf is in cur first */
Dave Chinner3d3e6f62012-11-12 22:54:08 +11005945 bp_release = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
Dave Chinner3d3e6f62012-11-12 22:54:08 +11005947 if (!bp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 bp_release = 1;
Dave Chinner3d3e6f62012-11-12 22:54:08 +11005949 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5950 XFS_BMAP_BTREE_REF,
Dave Chinner1813dd62012-11-14 17:54:40 +11005951 &xfs_bmbt_buf_ops);
Dave Chinner3d3e6f62012-11-12 22:54:08 +11005952 if (error)
5953 goto error_norelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 }
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005955 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 XFS_WANT_CORRUPTED_GOTO(
Christoph Hellwig4e8938f2008-10-30 17:14:43 +11005957 xfs_bmap_sanity_check(mp, bp, level),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 error0);
5959 if (level == 0)
5960 break;
5961
5962 /*
5963 * Check this block for basic sanity (increasing keys and
5964 * no duplicate blocks).
5965 */
5966
5967 xfs_check_block(block, mp, 0, 0);
Christoph Hellwig136341b2008-10-30 17:11:40 +11005968 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10005969 bno = be64_to_cpu(*pp);
5970 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 if (bp_release) {
5972 bp_release = 0;
5973 xfs_trans_brelse(NULL, bp);
5974 }
5975 }
5976
5977 /*
5978 * Here with bp and block set to the leftmost leaf node in the tree.
5979 */
5980 i = 0;
5981
5982 /*
5983 * Loop over all leaf nodes checking that all extents are in the right order.
5984 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 xfs_fsblock_t nextbno;
5987 xfs_extnum_t num_recs;
5988
5989
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005990 num_recs = xfs_btree_get_numrecs(block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991
5992 /*
5993 * Read-ahead the next leaf block, if any.
5994 */
5995
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11005996 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997
5998 /*
5999 * Check all the extents to make sure they are OK.
6000 * If we had a previous block, the last entry should
6001 * conform with the first entry in this one.
6002 */
6003
Christoph Hellwig136341b2008-10-30 17:11:40 +11006004 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006005 if (i) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11006006 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
6007 xfs_bmbt_disk_get_blockcount(&last) <=
6008 xfs_bmbt_disk_get_startoff(ep));
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006009 }
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006010 for (j = 1; j < num_recs; j++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11006011 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
Christoph Hellwig4a26e662008-10-30 16:58:32 +11006012 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
6013 xfs_bmbt_disk_get_blockcount(ep) <=
6014 xfs_bmbt_disk_get_startoff(nextp));
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006015 ep = nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017
Lachlan McIlroy2abdb8c2008-03-27 18:01:14 +11006018 last = *ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 i += num_recs;
6020 if (bp_release) {
6021 bp_release = 0;
6022 xfs_trans_brelse(NULL, bp);
6023 }
6024 bno = nextbno;
6025 /*
6026 * If we've reached the end, stop.
6027 */
6028 if (bno == NULLFSBLOCK)
6029 break;
6030
Dave Chinner3d3e6f62012-11-12 22:54:08 +11006031 bp_release = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
Dave Chinner3d3e6f62012-11-12 22:54:08 +11006033 if (!bp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 bp_release = 1;
Dave Chinner3d3e6f62012-11-12 22:54:08 +11006035 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6036 XFS_BMAP_BTREE_REF,
Dave Chinner1813dd62012-11-14 17:54:40 +11006037 &xfs_bmbt_buf_ops);
Dave Chinner3d3e6f62012-11-12 22:54:08 +11006038 if (error)
6039 goto error_norelse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 }
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006041 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 }
6043 if (bp_release) {
6044 bp_release = 0;
6045 xfs_trans_brelse(NULL, bp);
6046 }
6047 return;
6048
6049error0:
Dave Chinner0b932cc2011-03-07 10:08:35 +11006050 xfs_warn(mp, "%s: at error0", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 if (bp_release)
6052 xfs_trans_brelse(NULL, bp);
6053error_norelse:
Dave Chinner0b932cc2011-03-07 10:08:35 +11006054 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
Harvey Harrison34a622b2008-04-10 12:19:21 +10006055 __func__, i);
6056 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 return;
6058}
6059#endif
6060
6061/*
6062 * Count fsblocks of the given fork.
6063 */
6064int /* error */
6065xfs_bmap_count_blocks(
6066 xfs_trans_t *tp, /* transaction pointer */
6067 xfs_inode_t *ip, /* incore inode */
6068 int whichfork, /* data or attr fork */
6069 int *count) /* out: count of blocks */
6070{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006071 struct xfs_btree_block *block; /* current btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 xfs_fsblock_t bno; /* block # of "block" */
6073 xfs_ifork_t *ifp; /* fork structure */
6074 int level; /* btree level, for checking */
6075 xfs_mount_t *mp; /* file system mount structure */
Christoph Hellwig576039c2006-09-28 10:58:06 +10006076 __be64 *pp; /* pointer to block address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
6078 bno = NULLFSBLOCK;
6079 mp = ip->i_mount;
6080 ifp = XFS_IFORK_PTR(ip, whichfork);
6081 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
Ruben Porrasc94312d2008-08-13 16:52:25 +10006082 xfs_bmap_count_leaves(ifp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
Ruben Porrasc94312d2008-08-13 16:52:25 +10006084 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 return 0;
6086 }
6087
6088 /*
6089 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
6090 */
6091 block = ifp->if_broot;
Christoph Hellwig16259e72005-11-02 15:11:25 +11006092 level = be16_to_cpu(block->bb_level);
6093 ASSERT(level > 0);
Christoph Hellwig60197e82008-10-30 17:11:19 +11006094 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006095 bno = be64_to_cpu(*pp);
6096 ASSERT(bno != NULLDFSBNO);
6097 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6098 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006100 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6102 mp);
6103 return XFS_ERROR(EFSCORRUPTED);
6104 }
6105
6106 return 0;
6107}
6108
6109/*
6110 * Recursively walks each level of a btree
6111 * to count total fsblocks is use.
6112 */
David Chinnera8272ce2007-11-23 16:28:09 +11006113STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114xfs_bmap_count_tree(
6115 xfs_mount_t *mp, /* file system mount point */
6116 xfs_trans_t *tp, /* transaction pointer */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006117 xfs_ifork_t *ifp, /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 xfs_fsblock_t blockno, /* file system block number */
6119 int levelin, /* level in btree */
6120 int *count) /* Count of blocks */
6121{
6122 int error;
6123 xfs_buf_t *bp, *nbp;
6124 int level = levelin;
Christoph Hellwig576039c2006-09-28 10:58:06 +10006125 __be64 *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 xfs_fsblock_t bno = blockno;
6127 xfs_fsblock_t nextbno;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006128 struct xfs_btree_block *block, *nextblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 int numrecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130
Dave Chinner3d3e6f62012-11-12 22:54:08 +11006131 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
Dave Chinner1813dd62012-11-14 17:54:40 +11006132 &xfs_bmbt_buf_ops);
Dave Chinner3d3e6f62012-11-12 22:54:08 +11006133 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 return error;
6135 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006136 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137
6138 if (--level) {
Malcolm Parsons9da096f2009-03-29 09:55:42 +02006139 /* Not at node above leaves, count this level of nodes */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006140 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 while (nextbno != NULLFSBLOCK) {
Dave Chinner3d3e6f62012-11-12 22:54:08 +11006142 error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
6143 XFS_BMAP_BTREE_REF,
Dave Chinner1813dd62012-11-14 17:54:40 +11006144 &xfs_bmbt_buf_ops);
Dave Chinner3d3e6f62012-11-12 22:54:08 +11006145 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 return error;
6147 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006148 nextblock = XFS_BUF_TO_BLOCK(nbp);
6149 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 xfs_trans_brelse(tp, nbp);
6151 }
6152
6153 /* Dive to the next level */
Christoph Hellwig136341b2008-10-30 17:11:40 +11006154 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
Christoph Hellwig576039c2006-09-28 10:58:06 +10006155 bno = be64_to_cpu(*pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 if (unlikely((error =
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006157 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 xfs_trans_brelse(tp, bp);
6159 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6160 XFS_ERRLEVEL_LOW, mp);
6161 return XFS_ERROR(EFSCORRUPTED);
6162 }
6163 xfs_trans_brelse(tp, bp);
6164 } else {
6165 /* count all level 1 nodes and their leaves */
6166 for (;;) {
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006167 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
Christoph Hellwig16259e72005-11-02 15:11:25 +11006168 numrecs = be16_to_cpu(block->bb_numrecs);
Christoph Hellwig136341b2008-10-30 17:11:40 +11006169 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 xfs_trans_brelse(tp, bp);
6171 if (nextbno == NULLFSBLOCK)
6172 break;
6173 bno = nextbno;
Dave Chinner3d3e6f62012-11-12 22:54:08 +11006174 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6175 XFS_BMAP_BTREE_REF,
Dave Chinner1813dd62012-11-14 17:54:40 +11006176 &xfs_bmbt_buf_ops);
Dave Chinner3d3e6f62012-11-12 22:54:08 +11006177 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 return error;
6179 *count += 1;
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006180 block = XFS_BUF_TO_BLOCK(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 }
6182 }
6183 return 0;
6184}
6185
6186/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006187 * Count leaf blocks given a range of extent records.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006189STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190xfs_bmap_count_leaves(
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006191 xfs_ifork_t *ifp,
6192 xfs_extnum_t idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 int numrecs,
6194 int *count)
6195{
6196 int b;
6197
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006198 for (b = 0; b < numrecs; b++) {
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10006199 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
Yingping Lu91e11082005-11-02 15:10:24 +11006200 *count += xfs_bmbt_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006201 }
Yingping Lu91e11082005-11-02 15:10:24 +11006202}
6203
6204/*
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006205 * Count leaf blocks given a range of extent records originally
6206 * in btree format.
Yingping Lu91e11082005-11-02 15:10:24 +11006207 */
Ruben Porrasc94312d2008-08-13 16:52:25 +10006208STATIC void
Yingping Lu91e11082005-11-02 15:10:24 +11006209xfs_bmap_disk_count_leaves(
Christoph Hellwig136341b2008-10-30 17:11:40 +11006210 struct xfs_mount *mp,
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11006211 struct xfs_btree_block *block,
Yingping Lu91e11082005-11-02 15:10:24 +11006212 int numrecs,
6213 int *count)
6214{
6215 int b;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006216 xfs_bmbt_rec_t *frp;
Yingping Lu91e11082005-11-02 15:10:24 +11006217
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006218 for (b = 1; b <= numrecs; b++) {
Christoph Hellwig136341b2008-10-30 17:11:40 +11006219 frp = XFS_BMBT_REC_ADDR(mp, block, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 *count += xfs_bmbt_disk_get_blockcount(frp);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11006221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222}
Dave Chinnerc726de42010-11-30 15:14:39 +11006223
6224/*
6225 * dead simple method of punching delalyed allocation blocks from a range in
6226 * the inode. Walks a block at a time so will be slow, but is only executed in
6227 * rare error cases so the overhead is not critical. This will alays punch out
6228 * both the start and end blocks, even if the ranges only partially overlap
6229 * them, so it is up to the caller to ensure that partial blocks are not
6230 * passed in.
6231 */
6232int
6233xfs_bmap_punch_delalloc_range(
6234 struct xfs_inode *ip,
6235 xfs_fileoff_t start_fsb,
6236 xfs_fileoff_t length)
6237{
6238 xfs_fileoff_t remaining = length;
6239 int error = 0;
6240
6241 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6242
6243 do {
6244 int done;
6245 xfs_bmbt_irec_t imap;
6246 int nimaps = 1;
6247 xfs_fsblock_t firstblock;
6248 xfs_bmap_free_t flist;
6249
6250 /*
6251 * Map the range first and check that it is a delalloc extent
6252 * before trying to unmap the range. Otherwise we will be
6253 * trying to remove a real extent (which requires a
6254 * transaction) or a hole, which is probably a bad idea...
6255 */
Dave Chinner5c8ed202011-09-18 20:40:45 +00006256 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
6257 XFS_BMAPI_ENTIRE);
Dave Chinnerc726de42010-11-30 15:14:39 +11006258
6259 if (error) {
6260 /* something screwed, just bail */
6261 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner53487782011-03-07 10:05:35 +11006262 xfs_alert(ip->i_mount,
Dave Chinnerc726de42010-11-30 15:14:39 +11006263 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6264 ip->i_ino, start_fsb);
6265 }
6266 break;
6267 }
6268 if (!nimaps) {
6269 /* nothing there */
6270 goto next_block;
6271 }
6272 if (imap.br_startblock != DELAYSTARTBLOCK) {
6273 /* been converted, ignore */
6274 goto next_block;
6275 }
6276 WARN_ON(imap.br_blockcount == 0);
6277
6278 /*
6279 * Note: while we initialise the firstblock/flist pair, they
6280 * should never be used because blocks should never be
6281 * allocated or freed for a delalloc extent and hence we need
6282 * don't cancel or finish them after the xfs_bunmapi() call.
6283 */
6284 xfs_bmap_init(&flist, &firstblock);
6285 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6286 &flist, &done);
6287 if (error)
6288 break;
6289
6290 ASSERT(!flist.xbf_count && !flist.xbf_first);
6291next_block:
6292 start_fsb++;
6293 remaining--;
6294 } while(remaining > 0);
6295
6296 return error;
6297}
Dave Chinnerfd500922012-04-23 15:59:01 +10006298
6299/*
6300 * Convert the given file system block to a disk block. We have to treat it
6301 * differently based on whether the file is a real time file or not, because the
6302 * bmap code does.
6303 */
6304xfs_daddr_t
6305xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
6306{
6307 return (XFS_IS_REALTIME_INODE(ip) ? \
6308 (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \
6309 XFS_FSB_TO_DADDR((ip)->i_mount, (fsb)));
6310}